home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / amvideo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  8.6 KB  |  252 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (c) 1992 - 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11.  
  12. // Helper functions for bitmap formats, March 1995
  13.  
  14. #include <streams.h>
  15. #include <limits.h>
  16.  
  17. // These are bit field masks for true colour devices
  18.  
  19. const DWORD bits555[] = {0x007C00,0x0003E0,0x00001F};
  20. const DWORD bits565[] = {0x00F800,0x0007E0,0x00001F};
  21. const DWORD bits888[] = {0xFF0000,0x00FF00,0x0000FF};
  22.  
  23. // This maps bitmap subtypes into a bits per pixel value and also a name
  24.  
  25. const struct {
  26.     const GUID *pSubtype;
  27.     WORD BitCount;
  28.     TCHAR *pName;
  29. } BitCountMap[] = { &MEDIASUBTYPE_RGB1,        1,   TEXT("RGB Monochrome"),
  30.                     &MEDIASUBTYPE_RGB4,        4,   TEXT("RGB VGA"),
  31.                     &MEDIASUBTYPE_RGB8,        8,   TEXT("RGB 8"),
  32.                     &MEDIASUBTYPE_RGB565,      16,  TEXT("RGB 565 (16 bit)"),
  33.                     &MEDIASUBTYPE_RGB555,      16,  TEXT("RGB 555 (16 bit)"),
  34.                     &MEDIASUBTYPE_RGB24,       24,  TEXT("RGB 24"),
  35.                     &MEDIASUBTYPE_RGB32,       32,  TEXT("RGB 32"),
  36.                     &MEDIASUBTYPE_Overlay,     0,   TEXT("Overlay"),
  37.                     &GUID_NULL,                0,   TEXT("UNKNOWN") };
  38.  
  39.  
  40. // Return the size of the bitmap as defined by this header
  41.  
  42. STDAPI_(DWORD) GetBitmapSize(const BITMAPINFOHEADER *pHeader)
  43. {
  44.     return DIBSIZE(*pHeader);
  45. }
  46.  
  47.  
  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)
  50.  
  51. STDAPI_(const GUID) GetTrueColorType(const BITMAPINFOHEADER *pbmiHeader)
  52. {
  53.     BITMAPINFO *pbmInfo = (BITMAPINFO *) pbmiHeader;
  54.     ASSERT(pbmiHeader->biBitCount == 16);
  55.  
  56.     // If its BI_RGB then it's RGB 555 by default
  57.  
  58.     if (pbmiHeader->biCompression == BI_RGB) {
  59.         return MEDIASUBTYPE_RGB555;
  60.     }
  61.  
  62.     // Compare the bit fields with RGB 555
  63.  
  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;
  69.             }
  70.         }
  71.     }
  72.  
  73.     // Compare the bit fields with RGB 565
  74.  
  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;
  80.             }
  81.         }
  82.     }
  83.     return GUID_NULL;
  84. }
  85.  
  86.  
  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
  92.  
  93. STDAPI_(const GUID) GetBitmapSubtype(const BITMAPINFOHEADER *pbmiHeader)
  94. {
  95.     ASSERT(pbmiHeader);
  96.  
  97.     // If it's not RGB then create a GUID from the compression type
  98.  
  99.     if (pbmiHeader->biCompression != BI_RGB) {
  100.         if (pbmiHeader->biCompression != BI_BITFIELDS) {
  101.             FOURCCMap FourCCMap(pbmiHeader->biCompression);
  102.             return (const GUID) FourCCMap;
  103.         }
  104.     }
  105.  
  106.     // Map the RGB DIB bit depth to a image GUID
  107.  
  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;
  115.     }
  116.     return GUID_NULL;
  117. }
  118.  
  119.  
  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
  123.  
  124. STDAPI_(WORD) GetBitCount(const GUID *pSubtype)
  125. {
  126.     ASSERT(pSubtype);
  127.     const GUID *pMediaSubtype;
  128.     INT iPosition = 0;
  129.  
  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
  132.  
  133.     while (TRUE) {
  134.         pMediaSubtype = BitCountMap[iPosition].pSubtype;
  135.         if (IsEqualGUID(*pMediaSubtype,GUID_NULL)) {
  136.             return USHRT_MAX;
  137.         }
  138.         if (IsEqualGUID(*pMediaSubtype,*pSubtype)) {
  139.             return BitCountMap[iPosition].BitCount;
  140.         }
  141.         iPosition++;
  142.     }
  143. }
  144.  
  145.  
  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
  149.  
  150. STDAPI_(TCHAR *) GetSubtypeName(const GUID *pSubtype)
  151. {
  152.     ASSERT(pSubtype);
  153.     const GUID *pMediaSubtype;
  154.     INT iPosition = 0;
  155.  
  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
  158.  
  159.     while (TRUE) {
  160.         pMediaSubtype = BitCountMap[iPosition].pSubtype;
  161.         if (IsEqualGUID(*pMediaSubtype,*pSubtype)) {
  162.             return BitCountMap[iPosition].pName;
  163.         }
  164.         if (IsEqualGUID(*pMediaSubtype,GUID_NULL)) {
  165.             return TEXT("UNKNOWN");
  166.         }
  167.         iPosition++;
  168.     }
  169. }
  170.  
  171.  
  172. // The mechanism for describing a bitmap format is with the BITMAPINFOHEADER
  173. // This is really messy to deal with because it invariably has fields that
  174. // follow it holding bit fields, palettes and the rest. This function gives
  175. // the number of bytes required to hold a VIDEOINFO that represents it. This
  176. // count includes the prefix information (like the rcSource rectangle) the
  177. // BITMAPINFOHEADER field, and any other colour information on the end.
  178. //
  179. // WARNING If you want to copy a BITMAPINFOHEADER into a VIDEOINFO always make
  180. // sure that you use the HEADER macro because the BITMAPINFOHEADER field isn't
  181. // right at the start of the VIDEOINFO (there are a number of other fields),
  182. //
  183. //     CopyMemory(HEADER(pVideoInfo),pbmi,sizeof(BITMAPINFOHEADER));
  184. //
  185.  
  186. STDAPI_(LONG) GetBitmapFormatSize(const BITMAPINFOHEADER *pHeader)
  187. {
  188.     LONG Size = SIZE_VIDEOHEADER;   // Everyone has this to start with
  189.  
  190.     // Assume a normal header for YUV formats
  191.  
  192.     if (pHeader->biCompression != BI_RGB) {
  193.         if (pHeader->biCompression != BI_BITFIELDS) {
  194.             return SIZE_VIDEOHEADER;
  195.         }
  196.     }
  197.  
  198.     // Does this format use a palette, if the number of colours actually used
  199.     // is zero then it is set to the maximum that are allowed for that colour
  200.     // depth (an example is 256 for eight bits). Truecolour formats may also
  201.     // pass a palette with them in which case the used count is non zero
  202.  
  203.     if (pHeader->biBitCount <= iPALETTE || pHeader->biClrUsed) {
  204.         LONG Entries = (DWORD) 1 << pHeader->biBitCount;
  205.         if (pHeader->biClrUsed) {
  206.             Entries = pHeader->biClrUsed;
  207.         }
  208.         Size += Entries * sizeof(RGBQUAD);
  209.     }
  210.  
  211.     // Truecolour formats may have a BI_BITFIELDS specifier for compression
  212.     // type which means that room for three DWORDs should be allocated that
  213.     // specify where in each pixel the RGB colour components may be found
  214.  
  215.     if (pHeader->biCompression == BI_BITFIELDS) {
  216.         Size += SIZE_MASKS;
  217.     }
  218.  
  219.     // A BITMAPINFO for a palettised image may also contain a palette map that
  220.     // provides the information to map from a source palette to a destination
  221.     // palette during a BitBlt for example, because this information is only
  222.     // ever processed during drawing you don't normally store the palette map
  223.     // nor have any way of knowing if it is present in the data structure
  224.  
  225.     return Size;
  226. }
  227.  
  228.  
  229. // Returns TRUE if the VIDEOINFO contains a palette
  230.  
  231. STDAPI_(BOOL) ContainsPalette(const VIDEOINFO *pVideoInfo)
  232. {
  233.     if (PALETTISED(pVideoInfo) == FALSE) {
  234.         if (pVideoInfo->bmiHeader.biClrUsed == 0) {
  235.             return FALSE;
  236.         }
  237.     }
  238.     return TRUE;
  239. }
  240.  
  241.  
  242. // Return a pointer to the first entry in a palette
  243.  
  244. STDAPI_(const RGBQUAD *) GetBitmapPalette(const VIDEOINFO *pVideoInfo)
  245. {
  246.     if (pVideoInfo->bmiHeader.biCompression == BI_BITFIELDS) {
  247.         return pVideoInfo->TrueColorInfo.bmiColors;
  248.     }
  249.     return pVideoInfo->bmiColors;
  250. }
  251.  
  252.