1 //------------------------------------------------------------------------------
4 // Desc: DirectShow base classes - implements helper functions for
7 // Copyright (c) 1992-2002 Microsoft Corporation. All rights reserved.
8 //------------------------------------------------------------------------------
14 // These are bit field masks for true colour devices
16 const DWORD bits555
[] = {0x007C00,0x0003E0,0x00001F};
17 const DWORD bits565
[] = {0x00F800,0x0007E0,0x00001F};
18 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.
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"
40 // Return the size of the bitmap as defined by this header
42 STDAPI_(DWORD
) GetBitmapSize(const BITMAPINFOHEADER
*pHeader
)
44 return DIBSIZE(*pHeader
);
48 // This is called if the header has a 16 bit colour depth and needs to work
49 // out the detailed type from the bit fields (either RGB 565 or RGB 555)
51 STDAPI_(const GUID
) GetTrueColorType(const BITMAPINFOHEADER
*pbmiHeader
)
53 BITMAPINFO
*pbmInfo
= (BITMAPINFO
*) pbmiHeader
;
54 ASSERT(pbmiHeader
->biBitCount
== 16);
56 // If its BI_RGB then it's RGB 555 by default
58 if (pbmiHeader
->biCompression
== BI_RGB
) {
59 return MEDIASUBTYPE_RGB555
;
62 // Compare the bit fields with RGB 555
64 DWORD
*pMask
= (DWORD
*) pbmInfo
->bmiColors
;
65 if (pMask
[0] == bits555
[0]) {
66 if (pMask
[1] == bits555
[1]) {
67 if (pMask
[2] == bits555
[2]) {
68 return MEDIASUBTYPE_RGB555
;
73 // Compare the bit fields with RGB 565
75 pMask
= (DWORD
*) pbmInfo
->bmiColors
;
76 if (pMask
[0] == bits565
[0]) {
77 if (pMask
[1] == bits565
[1]) {
78 if (pMask
[2] == bits565
[2]) {
79 return MEDIASUBTYPE_RGB565
;
87 // Given a BITMAPINFOHEADER structure this returns the GUID sub type that is
88 // used to describe it in format negotiations. For example a video codec fills
89 // in the format block with a VIDEOINFO structure, it also fills in the major
90 // type with MEDIATYPE_VIDEO and the subtype with a GUID that matches the bit
91 // count, for example if it is an eight bit image then MEDIASUBTYPE_RGB8
93 STDAPI_(const GUID
) GetBitmapSubtype(const BITMAPINFOHEADER
*pbmiHeader
)
97 // If it's not RGB then create a GUID from the compression type
99 if (pbmiHeader
->biCompression
!= BI_RGB
) {
100 if (pbmiHeader
->biCompression
!= BI_BITFIELDS
) {
101 FOURCCMap
FourCCMap(pbmiHeader
->biCompression
);
102 return (const GUID
) FourCCMap
;
106 // Map the RGB DIB bit depth to a image GUID
108 switch(pbmiHeader
->biBitCount
) {
109 case 1 : return MEDIASUBTYPE_RGB1
;
110 case 4 : return MEDIASUBTYPE_RGB4
;
111 case 8 : return MEDIASUBTYPE_RGB8
;
112 case 16 : return GetTrueColorType(pbmiHeader
);
113 case 24 : return MEDIASUBTYPE_RGB24
;
114 case 32 : return MEDIASUBTYPE_RGB32
;
120 // Given a video bitmap subtype we return the number of bits per pixel it uses
121 // We return a WORD bit count as thats what the BITMAPINFOHEADER uses. If the
122 // GUID subtype is not found in the table we return an invalid USHRT_MAX
124 STDAPI_(WORD
) GetBitCount(const GUID
*pSubtype
)
127 const GUID
*pMediaSubtype
;
130 // Scan the mapping list seeing if the source GUID matches any known
131 // bitmap subtypes, the list is terminated by a GUID_NULL entry
134 pMediaSubtype
= BitCountMap
[iPosition
].pSubtype
;
135 if (IsEqualGUID(*pMediaSubtype
,GUID_NULL
)) {
138 if (IsEqualGUID(*pMediaSubtype
,*pSubtype
)) {
139 return BitCountMap
[iPosition
].BitCount
;
146 // Given a bitmap subtype we return a description name that can be used for
147 // debug purposes. In a retail build this function still returns the names
148 // If the subtype isn't found in the lookup table we return string UNKNOWN
150 int LocateSubtype(const GUID
*pSubtype
)
153 const GUID
*pMediaSubtype
;
156 // Scan the mapping list seeing if the source GUID matches any known
157 // bitmap subtypes, the list is terminated by a GUID_NULL entry
160 pMediaSubtype
= BitCountMap
[iPosition
].pSubtype
;
161 if (IsEqualGUID(*pMediaSubtype
,*pSubtype
) ||
162 IsEqualGUID(*pMediaSubtype
,GUID_NULL
)
176 STDAPI_(WCHAR
*) GetSubtypeNameW(const GUID
*pSubtype
)
178 return BitCountMap
[LocateSubtype(pSubtype
)].wszName
;
181 STDAPI_(CHAR
*) GetSubtypeNameA(const GUID
*pSubtype
)
183 return BitCountMap
[LocateSubtype(pSubtype
)].pName
;
186 #ifndef GetSubtypeName
187 #error wxutil.h should have defined GetSubtypeName
189 #undef GetSubtypeName
191 // this is here for people that linked to it directly; most people
192 // would use the header file that picks the A or W version.
193 STDAPI_(CHAR
*) GetSubtypeName(const GUID
*pSubtype
)
195 return GetSubtypeNameA(pSubtype
);
199 // The mechanism for describing a bitmap format is with the BITMAPINFOHEADER
200 // This is really messy to deal with because it invariably has fields that
201 // follow it holding bit fields, palettes and the rest. This function gives
202 // the number of bytes required to hold a VIDEOINFO that represents it. This
203 // count includes the prefix information (like the rcSource rectangle) the
204 // BITMAPINFOHEADER field, and any other colour information on the end.
206 // WARNING If you want to copy a BITMAPINFOHEADER into a VIDEOINFO always make
207 // sure that you use the HEADER macro because the BITMAPINFOHEADER field isn't
208 // right at the start of the VIDEOINFO (there are a number of other fields),
210 // CopyMemory(HEADER(pVideoInfo),pbmi,sizeof(BITMAPINFOHEADER));
213 STDAPI_(LONG
) GetBitmapFormatSize(const BITMAPINFOHEADER
*pHeader
)
215 // Everyone has this to start with this
216 LONG Size
= SIZE_PREHEADER
+ pHeader
->biSize
;
218 ASSERT(pHeader
->biSize
>= sizeof(BITMAPINFOHEADER
));
220 // Does this format use a palette, if the number of colours actually used
221 // is zero then it is set to the maximum that are allowed for that colour
222 // depth (an example is 256 for eight bits). Truecolour formats may also
223 // pass a palette with them in which case the used count is non zero
225 // This would scare me.
226 ASSERT(pHeader
->biBitCount
<= iPALETTE
|| pHeader
->biClrUsed
== 0);
228 if (pHeader
->biBitCount
<= iPALETTE
|| pHeader
->biClrUsed
) {
229 LONG Entries
= (DWORD
) 1 << pHeader
->biBitCount
;
230 if (pHeader
->biClrUsed
) {
231 Entries
= pHeader
->biClrUsed
;
233 Size
+= Entries
* sizeof(RGBQUAD
);
236 // Truecolour formats may have a BI_BITFIELDS specifier for compression
237 // type which means that room for three DWORDs should be allocated that
238 // specify where in each pixel the RGB colour components may be found
240 if (pHeader
->biCompression
== BI_BITFIELDS
) {
244 // A BITMAPINFO for a palettised image may also contain a palette map that
245 // provides the information to map from a source palette to a destination
246 // palette during a BitBlt for example, because this information is only
247 // ever processed during drawing you don't normally store the palette map
248 // nor have any way of knowing if it is present in the data structure
254 // Returns TRUE if the VIDEOINFO contains a palette
256 STDAPI_(BOOL
) ContainsPalette(const VIDEOINFOHEADER
*pVideoInfo
)
258 if (PALETTISED(pVideoInfo
) == FALSE
) {
259 if (pVideoInfo
->bmiHeader
.biClrUsed
== 0) {
267 // Return a pointer to the first entry in a palette
269 STDAPI_(const RGBQUAD
*) GetBitmapPalette(const VIDEOINFOHEADER
*pVideoInfo
)
271 if (pVideoInfo
->bmiHeader
.biCompression
== BI_BITFIELDS
) {
272 return TRUECOLOR(pVideoInfo
)->bmiColors
;
274 return COLORS(pVideoInfo
);