Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
9.2 KiB

  1. //------------------------------------------------------------------------------
  2. // File: AMVideo.cpp
  3. //
  4. // Desc: DirectShow base classes - implements helper functions for
  5. // bitmap formats.
  6. //
  7. //@@BEGIN_MSINTERNAL
  8. //
  9. // March 1995
  10. //
  11. //@@END_MSINTERNAL
  12. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  13. //------------------------------------------------------------------------------
  14. #include <streams.h>
  15. #include <limits.h>
  16. // These are bit field masks for true colour devices
  17. const DWORD bits555[] = {0x007C00,0x0003E0,0x00001F};
  18. const DWORD bits565[] = {0x00F800,0x0007E0,0x00001F};
  19. const DWORD bits888[] = {0xFF0000,0x00FF00,0x0000FF};
  20. // This maps bitmap subtypes into a bits per pixel value and also a
  21. // name. unicode and ansi versions are stored because we have to
  22. // return a pointer to a static string.
  23. const struct {
  24. const GUID *pSubtype;
  25. WORD BitCount;
  26. CHAR *pName;
  27. WCHAR *wszName;
  28. } BitCountMap[] = { &MEDIASUBTYPE_RGB1, 1, "RGB Monochrome", L"RGB Monochrome",
  29. &MEDIASUBTYPE_RGB4, 4, "RGB VGA", L"RGB VGA",
  30. &MEDIASUBTYPE_RGB8, 8, "RGB 8", L"RGB 8",
  31. &MEDIASUBTYPE_RGB565, 16, "RGB 565 (16 bit)", L"RGB 565 (16 bit)",
  32. &MEDIASUBTYPE_RGB555, 16, "RGB 555 (16 bit)", L"RGB 555 (16 bit)",
  33. &MEDIASUBTYPE_RGB24, 24, "RGB 24", L"RGB 24",
  34. &MEDIASUBTYPE_RGB32, 32, "RGB 32", L"RGB 32",
  35. &MEDIASUBTYPE_ARGB32, 32, "ARGB 32", L"ARGB 32",
  36. &MEDIASUBTYPE_Overlay, 0, "Overlay", L"Overlay",
  37. &GUID_NULL, 0, "UNKNOWN", L"UNKNOWN"
  38. };
  39. // Return the size of the bitmap as defined by this header
  40. STDAPI_(DWORD) GetBitmapSize(const BITMAPINFOHEADER *pHeader)
  41. {
  42. return DIBSIZE(*pHeader);
  43. }
  44. // This is called if the header has a 16 bit colour depth and needs to work
  45. // out the detailed type from the bit fields (either RGB 565 or RGB 555)
  46. STDAPI_(const GUID) GetTrueColorType(const BITMAPINFOHEADER *pbmiHeader)
  47. {
  48. BITMAPINFO *pbmInfo = (BITMAPINFO *) pbmiHeader;
  49. ASSERT(pbmiHeader->biBitCount == 16);
  50. // If its BI_RGB then it's RGB 555 by default
  51. if (pbmiHeader->biCompression == BI_RGB) {
  52. return MEDIASUBTYPE_RGB555;
  53. }
  54. // Compare the bit fields with RGB 555
  55. DWORD *pMask = (DWORD *) pbmInfo->bmiColors;
  56. if (pMask[0] == bits555[0]) {
  57. if (pMask[1] == bits555[1]) {
  58. if (pMask[2] == bits555[2]) {
  59. return MEDIASUBTYPE_RGB555;
  60. }
  61. }
  62. }
  63. // Compare the bit fields with RGB 565
  64. pMask = (DWORD *) pbmInfo->bmiColors;
  65. if (pMask[0] == bits565[0]) {
  66. if (pMask[1] == bits565[1]) {
  67. if (pMask[2] == bits565[2]) {
  68. return MEDIASUBTYPE_RGB565;
  69. }
  70. }
  71. }
  72. return GUID_NULL;
  73. }
  74. // Given a BITMAPINFOHEADER structure this returns the GUID sub type that is
  75. // used to describe it in format negotiations. For example a video codec fills
  76. // in the format block with a VIDEOINFO structure, it also fills in the major
  77. // type with MEDIATYPE_VIDEO and the subtype with a GUID that matches the bit
  78. // count, for example if it is an eight bit image then MEDIASUBTYPE_RGB8
  79. STDAPI_(const GUID) GetBitmapSubtype(const BITMAPINFOHEADER *pbmiHeader)
  80. {
  81. ASSERT(pbmiHeader);
  82. // If it's not RGB then create a GUID from the compression type
  83. if (pbmiHeader->biCompression != BI_RGB) {
  84. if (pbmiHeader->biCompression != BI_BITFIELDS) {
  85. FOURCCMap FourCCMap(pbmiHeader->biCompression);
  86. return (const GUID) FourCCMap;
  87. }
  88. }
  89. // Map the RGB DIB bit depth to a image GUID
  90. switch(pbmiHeader->biBitCount) {
  91. case 1 : return MEDIASUBTYPE_RGB1;
  92. case 4 : return MEDIASUBTYPE_RGB4;
  93. case 8 : return MEDIASUBTYPE_RGB8;
  94. case 16 : return GetTrueColorType(pbmiHeader);
  95. case 24 : return MEDIASUBTYPE_RGB24;
  96. case 32 : return MEDIASUBTYPE_RGB32;
  97. }
  98. return GUID_NULL;
  99. }
  100. // Given a video bitmap subtype we return the number of bits per pixel it uses
  101. // We return a WORD bit count as thats what the BITMAPINFOHEADER uses. If the
  102. // GUID subtype is not found in the table we return an invalid USHRT_MAX
  103. STDAPI_(WORD) GetBitCount(const GUID *pSubtype)
  104. {
  105. ASSERT(pSubtype);
  106. const GUID *pMediaSubtype;
  107. INT iPosition = 0;
  108. // Scan the mapping list seeing if the source GUID matches any known
  109. // bitmap subtypes, the list is terminated by a GUID_NULL entry
  110. while (TRUE) {
  111. pMediaSubtype = BitCountMap[iPosition].pSubtype;
  112. if (IsEqualGUID(*pMediaSubtype,GUID_NULL)) {
  113. return USHRT_MAX;
  114. }
  115. if (IsEqualGUID(*pMediaSubtype,*pSubtype)) {
  116. return BitCountMap[iPosition].BitCount;
  117. }
  118. iPosition++;
  119. }
  120. }
  121. // Given a bitmap subtype we return a description name that can be used for
  122. // debug purposes. In a retail build this function still returns the names
  123. // If the subtype isn't found in the lookup table we return string UNKNOWN
  124. int LocateSubtype(const GUID *pSubtype)
  125. {
  126. ASSERT(pSubtype);
  127. const GUID *pMediaSubtype;
  128. INT iPosition = 0;
  129. // Scan the mapping list seeing if the source GUID matches any known
  130. // bitmap subtypes, the list is terminated by a GUID_NULL entry
  131. while (TRUE) {
  132. pMediaSubtype = BitCountMap[iPosition].pSubtype;
  133. if (IsEqualGUID(*pMediaSubtype,*pSubtype) ||
  134. IsEqualGUID(*pMediaSubtype,GUID_NULL)
  135. )
  136. {
  137. break;
  138. }
  139. iPosition++;
  140. }
  141. return iPosition;
  142. }
  143. STDAPI_(WCHAR *) GetSubtypeNameW(const GUID *pSubtype)
  144. {
  145. return BitCountMap[LocateSubtype(pSubtype)].wszName;
  146. }
  147. STDAPI_(CHAR *) GetSubtypeNameA(const GUID *pSubtype)
  148. {
  149. return BitCountMap[LocateSubtype(pSubtype)].pName;
  150. }
  151. #ifndef GetSubtypeName
  152. #error wxutil.h should have defined GetSubtypeName
  153. #endif
  154. #undef GetSubtypeName
  155. // this is here for people that linked to it directly; most people
  156. // would use the header file that picks the A or W version.
  157. STDAPI_(CHAR *) GetSubtypeName(const GUID *pSubtype)
  158. {
  159. return GetSubtypeNameA(pSubtype);
  160. }
  161. // The mechanism for describing a bitmap format is with the BITMAPINFOHEADER
  162. // This is really messy to deal with because it invariably has fields that
  163. // follow it holding bit fields, palettes and the rest. This function gives
  164. // the number of bytes required to hold a VIDEOINFO that represents it. This
  165. // count includes the prefix information (like the rcSource rectangle) the
  166. // BITMAPINFOHEADER field, and any other colour information on the end.
  167. //
  168. // WARNING If you want to copy a BITMAPINFOHEADER into a VIDEOINFO always make
  169. // sure that you use the HEADER macro because the BITMAPINFOHEADER field isn't
  170. // right at the start of the VIDEOINFO (there are a number of other fields),
  171. //
  172. // CopyMemory(HEADER(pVideoInfo),pbmi,sizeof(BITMAPINFOHEADER));
  173. //
  174. STDAPI_(LONG) GetBitmapFormatSize(const BITMAPINFOHEADER *pHeader)
  175. {
  176. // Everyone has this to start with this
  177. LONG Size = SIZE_PREHEADER + pHeader->biSize;
  178. ASSERT(pHeader->biSize >= sizeof(BITMAPINFOHEADER));
  179. // Does this format use a palette, if the number of colours actually used
  180. // is zero then it is set to the maximum that are allowed for that colour
  181. // depth (an example is 256 for eight bits). Truecolour formats may also
  182. // pass a palette with them in which case the used count is non zero
  183. // This would scare me.
  184. ASSERT(pHeader->biBitCount <= iPALETTE || pHeader->biClrUsed == 0);
  185. if (pHeader->biBitCount <= iPALETTE || pHeader->biClrUsed) {
  186. LONG Entries = (DWORD) 1 << pHeader->biBitCount;
  187. if (pHeader->biClrUsed) {
  188. Entries = pHeader->biClrUsed;
  189. }
  190. Size += Entries * sizeof(RGBQUAD);
  191. }
  192. // Truecolour formats may have a BI_BITFIELDS specifier for compression
  193. // type which means that room for three DWORDs should be allocated that
  194. // specify where in each pixel the RGB colour components may be found
  195. if (pHeader->biCompression == BI_BITFIELDS) {
  196. Size += SIZE_MASKS;
  197. }
  198. // A BITMAPINFO for a palettised image may also contain a palette map that
  199. // provides the information to map from a source palette to a destination
  200. // palette during a BitBlt for example, because this information is only
  201. // ever processed during drawing you don't normally store the palette map
  202. // nor have any way of knowing if it is present in the data structure
  203. return Size;
  204. }
  205. // Returns TRUE if the VIDEOINFO contains a palette
  206. STDAPI_(BOOL) ContainsPalette(const VIDEOINFOHEADER *pVideoInfo)
  207. {
  208. if (PALETTISED(pVideoInfo) == FALSE) {
  209. if (pVideoInfo->bmiHeader.biClrUsed == 0) {
  210. return FALSE;
  211. }
  212. }
  213. return TRUE;
  214. }
  215. // Return a pointer to the first entry in a palette
  216. STDAPI_(const RGBQUAD *) GetBitmapPalette(const VIDEOINFOHEADER *pVideoInfo)
  217. {
  218. if (pVideoInfo->bmiHeader.biCompression == BI_BITFIELDS) {
  219. return TRUECOLOR(pVideoInfo)->bmiColors;
  220. }
  221. return COLORS(pVideoInfo);
  222. }