ValidateBitmapInfoHeader

In the Windows SDK for Vista, the DirectShow base classes have a useful little function named ValidateBitmapInfoHeader.

BOOL ValidateBitmapInfoHeader(const BITMAPINFOHEADER *pbmi, DWORD cbSize)

This function checks a BITMAPINFOHEADER structure for some possible errors that can cause arithemetic overflow or can lead to buffer overruns (for example because the image size or color-palette size is wrong). The function returns FALSE in the following situations:

  • The cbSize member is too small, or is more than 4K (more than enough for the structure plus a 256-entry color table.
  • The width or height is zero.
  • Bits per pixel exceeds 200.
  • Calculating the (uncompressed) image size in bytes causes arithmetic overflow.
  • The palette table has more than 256 entries, or exceeds the number of possible colors given the bit depth.
  • biSizeImage is calculated incorrectly for RGB formats

This won't necessarily catch every possible error in a BITMAPINFOHEADER. In particular, it is not designed to ensure that the structure represents an actual video format or that every structure member is used correctly. For example, the documentation for BITMAPINFOHEADER says that the BI_BITFIELDS compression type is "valid for 16-bpp and 32-bpp bitmaps," but this function does not check that.

Two other functions are thin wrappers around ValidateBitmapInfoHeader:

  • CheckVideoInfoType takes an AM_MEDIA_TYPE pointer whose format block is a VIDEOINFOHEADER structure. The function calls ValidateBitmapInfoHeader on the bmiHeader field.
  • CheckVideoInfo2Type is the same thing for VIDEOINFOHEADER2 media types.

Both of these functions fail if the AM_MEDIA_TYPE doesn't have the expected format block. (Incidentally there's a typo in the syntax block for CheckVideoInfo2Type in the MSDN docs.)


This posting is provided "AS IS" with no warranties, and confers no rights.