Compaq Multimedia Services
for OpenVMS Alpha
Programmer's Guide


Previous Contents Index


Chapter 7
Image Formats

This chapter describes image formats including the device-independent bitmap (DIB) format, DIB format extensions for the still-image JPEG (JPEG), motion JPEG (MJPG), and YUV video data formats, and the X image format.

7.1 Device-Independent Bitmap Format

The device-independent bitmap (DIB) file format represents bitmap images in a format that is independent of a specific device. Bitmaps can be represented as 1, 4, and 8 bits/pixel, with a palette containing colors represented in 24 bits. Bitmaps can also be represented at 24 bits without a palette and in run-length encoded format.

7.2 BITMAPINFOHEADER Data Structure

The BITMAPINFOHEADER data structure is the common denominator for all bitmap information header types. Adaptive redefinition of the BITMAPINFOHEADER must provide that members of the basic BITMAPINFOHEADER be defined as both the preliminary and primary members of each redefinition of the BITMAPINFOHEADER. As a result, a pointer to a redefined or extended BITMAPINFOHEADER data structure can always be recast as a pointer to the basic BITMAPINFOHEADER from which it is derived. For more details, see Section 7.6.

The standard BITMAPINFOHEADER data structure contains information about the dimensions and color formats of DIBs.

Example 7-1 shows the BITMAPINFOHEADER data structure definition.

Example 7-1 BITMAPINFOHEADER Data Structure Definition

typedef struct tagBITMAPINFOHEADER { 
    DWORD biSize;          /* size (in bytes) of structure */ 
    DWORD biWidth;         /* width (in pixels) of DIB */ 
    DWORD biHeight;        /* height (in pixels) of DIB */ 
    WORD  biPlanes;        /* number of planes */ 
    WORD  biBitCount;      /* number of bits/pixel */ 
    DWORD biCompression;   /* compression format */ 
    DWORD biSizeImage;     /* size (in bytes) of the image */ 
    DWORD biXPelsPerMeter; /* horizontal resolution of target device */ 
    DWORD biYPelsPerMeter; /* vertical resolution of target device */ 
    DWORD biClrUsed;       /* number of colors used */ 
    DWORD biClrImportant;  /* number of colors important */ 
} BITMAPINFOHEADER; 

The BITMAPINFOHEADER data structure has the following fields:

biSize
Specifies the number of bytes required by the BITMAPINFOHEADER or extended BITMAPINFOHEADER data structure. This field is used to locate different versions of the structure and to locate the palette information, if applicable. The palette information is always located at offset biSize after the beginning of the BITMAPINFOHEADER.

biWidth
Specifies the width of the DIB in pixels.

biHeight
Specifies the height of the DIB in pixels.

biPlanes
Specifies the number of planes for the target device. This value must be 1.

biBitCount
Specifies the number of bits/pixel. This field can have one of the following values:
Value Meaning
1 The bitmap is monochrome and the color table contains two entries. Each bit in the bitmap array represents a pixel. If the bit is clear, the pixel is displayed with the color of the first entry in the color table. If the bit is set, the pixel is displayed with the color of the second entry in the color table.
4 The bitmap has a maximum of 16 colors. Each pixel in the bitmap is represented by a 4-bit index into the color table. For example, if the first byte is 0x1F, then the byte represents two pixels. The first pixel contains the color in the second color table entry, and the second pixel contains the color in the sixteenth color table entry.
8 The bitmap has a maximum of 256 colors. Each pixel in the bitmap is represented by a byte-sized index into the color table. For example, if the first byte in the bitmap is 0x1F, then the first pixel has the color of the thirty-second color table entry. This value can also be used to denote Y-only images (8-bit monochrome) for the JPEG compression format.
16 The bitmap contains two bytes per pixel. This value is used for uncompressed YUV-format video data.
24 The bitmap has a maximum of 2 24 colors. The biClrUsed field is zero, and each of the three bytes in the bitmap array represents the relative intensities of red, green, and blue, respectively, of each pixel. This value is also used for the JPEG and MJPG YCbCr image compression formats.
32 The bitmap contains 32 bits of data. The first 8 bits are padded data, followed by 3 bytes representing the relative intensity of red, green, and blue of each pixel. This value is used for the BI_BITFIELDS image formats.

The values listed in the previous table are the standard values for the biBitCount field. See Section 7.5 for 16-bit and 32-bit extensions to this field.

Some compression formats need the biBitCount field to properly decode colors in the pixel. When the value in this field is greater than eight, a color table is not necessary.

When the value of the biBitCount field is set to one, four, or eight, the video drivers can assume that a bitmap uses a palette or a color table.

All compression formats use the biBitCount field.

biCompression
Specifies flags that indicate the compression format used or requested. The following flags are defined:

BI_RGB
Specifies that the bitmap is not compressed. This format is valid when the biBitCount field is set to 1, 4, 8, 16, 24, or 32. See Section 7.5.1 for extensions to this format. BI_RGB is a Microsoft Windows RGB format.

BICOMP_DECXIMAGEDIB
Specifies X image format (ZPixmap format), 8 or 24 bits/pixel. Currently, scanlines are 4-byte aligned and 4-byte padded. This format is valid when the biBitCount field is set to 8 or 24.

BICOMP_DECYUVDIB
Specifies YUV format, 16 bits/pixel. This format is valid when the biBitCount field is set to 16.

JPEG_DIB
Specifies still-image JPEG compressed image data. The biBitCount field is set to 24 for YCbCr data and to 8 for Y-only data. See Section 7.7 for more details about the JPEG and MJPG formats.

MJPG_DIB
Specifies motion-image JPEG compressed image data. The biBitCount field is set to 24 for YCbCr data and to 8 for Y-only data. See Section 7.7 for more details about the JPEG and MJPG formats.

BI_BITFIELDS
Specifies that the bitmap is not compressed and that a color mask is defined in the bmiColors field in the BITMAPINFO data structure. For further information, see Section 7.5.2.1.

All compression formats use this field.

biSizeImage
Specifies the size in bytes of the image. For JPEG data (JPEG_DIB or MJPG_DIB), this is the length of the data including the EOI marker. The biSizeImage field is checked for validity only when the BITMAPINFOHEADER data structure is passed to a function with a corresponding data pointer, such as ICDecompress , ICCompress , videoFrame , and videoStreamAddBuffer . It is not required to be valid for other routines.

biXPelsPerMeter
Specifies the horizontal resolution (in pixels/meter) of the target device for the bitmap. An application can use this value to select a bitmap from a resource group that most closely matches the characteristics of the current devices. For JPEG data (JPEG_DIB or MJPG_DIB), specify 0.

biYPelsPerMeter
Specifies the vertical resolution (in pixels/meter) of the target device for the bitmap. For JPEG data (JPEG_DIB or MJPG_DIB), specify 0.

biClrUsed
Specifies the number of colors in the color table to be used.

If this value is 0, the bitmap uses the maximum number of colors corresponding to the value of the biBitCount field. If this value is nonzero and the value of the biBitCount field is less than or equal to 8, the bitmap uses the actual number of colors specified by the value of biClrUsed. If the biBitCount field is set to a value greater than 8, this field is ignored.

biClrImportant
Specifies the number of color indexes that are considered important for displaying the bitmap. If this value is zero, then all colors are important. If the compression is BICOMP_DECXIMAGEDIB and the biBitCount field is less than or equal to 8, then biClrImportant must be set. If the biBitCount field is set to a value greater than 8, this field is ignored.

7.3 Bitmap Color Table

The bitmap color table is a collection of 24-bit RGB values. There are as many entries in the color table as there are colors in the bitmap. The color table is not present for bitmaps with 24-color bits because each pixel is represented by 24-bit RGB values in the actual bitmap data area. The color table is not present on some types of compressed data, such as JPEG, because the color data is fully encapsulated within the image data.

7.3.1 RGBQUAD Data Structure

The color table consists of an array of RGBQUAD data structures.

Example 7-2 shows the RGBQUAD data structure definition.

Example 7-2 RGBQUAD Data Structure Definition

typedef struct tagRGBQUAD { 
    BYTE rgbBlue;     /* blue intensity */ 
    BYTE rgbGreen;    /* red intensity */ 
    BYTE rgbRed;      /* green intensity */ 
    BYTE rgbReserved; /* reserved for use by driver */ 
} RGBQUAD 

The RGBQUAD data structure has the following fields:

rgbBlue
Specifies the blue intensity. For X image format (the biCompression field of the BITMAPINFOHEADER data structure is set to BICOMP_DECXIMAGEDIB), values range from 0 to 255, inclusive.

rgbGreen
Specifies the green intensity. For X image format (the biCompression field of the BITMAPINFOHEADER data structure is set to BICOMP_DECXIMAGEDIB), values range from 0 to 255, inclusive.

rgbRed
Specifies the red intensity. For X image format (the biCompression field of the BITMAPINFOHEADER data structure is set to BICOMP_DECXIMAGEDIB), values range from 0 to 255, inclusive.

Note

The range of values for rgbBlue, rgbGreen, and rgbRed differs from that for X colormaps, which takes values from 0 to 65,535, inclusive.

rgbReserved
For BICOMP_DECXIMAGEDIB, this field is set by the application to 1 to indicate to the decompressor that the color is already in use and should be reused if possible. The application sets the field to 0 to indicate to the decompressor that the color can be set to anything by the decompressor. The decompressor sets the field to 1 to indicate that it used the field and 0 to indicate that it did not. For other compression types, this field is reserved for use by the decompressor.

7.3.2 Order of Colors

The colors in the color table can be listed in the order of importance. This feature can help a device driver render a bitmap on a device that cannot display as many colors as there are in the bitmap. The driver can use the biClrImportant field of the BITMAPINFOHEADER data structure to determine which colors are important.

In BICOMP_DECXIMAGEDIB format color tables, all colors from 0 to the value of the biClrUsed field are important. Ordering is not important but applications should maintain ordering based on X Window System conventions for sharing colormaps.

7.3.3 Locating the Color Table

An application can use the biSize field of the
BITMAPINFOHEADER data structure to locate the color table. The following statement assigns the pColor variable to the byte offset of the color table from the beginning of the structure:


pColor = (RGBQUAD *)((unsigned long)pBitmapInfo + pBitmapInfo->biSize); 

Applications should use the biSize field for this operation. Do not use the sizeof(BITMAPINFOHEADER) statement because the biSize field will contain the size of the bitmap information header regardless of which extended bitmap information header is being used.

7.3.4 Interpreting the Color Table

The biBitCount field of the BITMAPINFOHEADER data structure determines the number of colors in the bitmap. Its value affects the interpretation of the color table.

7.4 Bitmap Data

The bits in the array are packed together but each line of pixels, or scanline, must be zero padded to end on a DWORD or 32-bit boundary. Segment boundaries, however, can appear anywhere in the bitmap. For RGB image bitmaps, the origin of the bitmap is the lower left corner. For X image bitmap compression (the biCompression field of the BITMAPINFOHEADER data structure is set to BICOMP_DECXIMAGEDIB), the origin of the bitmap is the upper left corner.

Figure 7-1 shows the bit organization of the 8-bit BICOMP_DECXIMAGEDIB format.

Figure 7-1 The 8-Bit BICOMP_DECXIMAGEDIB Format


In Figure 7-1, Pn is a pixel. Each pixel represents an index into the color table. The entry in the color table is the color used to render the pixel to the display.

Figure 7-2 shows the bit organization of the 24-bit BICOMP_DECXIMAGEDIB format.

Figure 7-2 The 24-Bit BICOMP_DECXIMAGEDIB Format


In Figure 7-2, Pn is a pixel, R is the red component, G is the green component, B is the blue component, and F is a filler byte (value not important and undefined). Each pixel is a 32-bit quantity, with 24 bits of significant information. The red, green, and blue components specify the red, green, and blue color values used to display the pixel. There is no associated colormap.

Figure 7-3 shows the bit organization of the 16-bit BICOMP_DECYUVDIB format.

Figure 7-3 The 16-Bit BICOMP_DECYUVDIB Format


In Figure 7-3, Pn is a pixel, Y is an 8-bit luminance value for a single pixel, U is an 8-bit chrominance value in the YUV color space (applies to 2 pixels), and V is an 8-bit chrominance value in the YUV color space (applies to 2 pixels). This is also known as the Phillips 4:2:2 YUV format.

The information for 2 pixels is contained within 32 bits. Each pixel has 16 bits of information, but some of that information is shared between pixel pairs. The numbers following U and V denote that these values are used for pixels m and n (Umn and Vmn). Each pixel has its own Y value specified, and the number following Y denotes which pixel the Y component belongs to (Ym belongs to pixel m). For instance, the 32 bits:


Y1  U12  Y2  V12 

produces 2 pixels:

Y1 U12 V12 and Y2 U12 V12

7.5 DIB Format Extensions

The DIB format extensions add the capabilities to handle new compression formats, custom compression formats, and inverted DIBs. The extensions also include an escape message to let applications query display drivers to determine their capabilities. This section includes the following information:

7.5.1 Extensions to the BI_RGB Format

Extensions to the BI_RGB format include 16 and 32 bits/pixel bitmap formats. These formats do not use a color table. They embed the colors in the WORD or DWORD representing each pixel.

The 16-bit BI_RGB format is identified by setting the biCompression field of the BITMAPINFOHEADER data structure to BI_RGB and the biBitCount field of the BITMAPINFOHEADER data structure to 16. For this format, each pixel in the bitmap is represented by a 16-bit RGB color value. The high bit of this value is zero. The remaining bits are divided into three groups of five bits to represent the red, green, and blue color values. The group containing the five most significant bits represents red. The group containing the middle five bits represents green. The group containing the five least significant bits represents blue. (This format is also referred to as the RGB555 format. It supports 32,000 colors.)

Figure 7-4 shows the bit organization of the 16-bit BI_RGB (RGB555) format.

Figure 7-4 The 16-Bit BI_RGB Format


The 32-bit BI_RGB format is identified by setting the biCompression field of the BITMAPINFOHEADER data structure to BI_RGB and the biBitCount field of the BITMAPINFOHEADER data structure to 32. For this format, each pixel is represented by a 32-bit (4-byte) RGB color value. The first byte is zero. The second byte represents red. The third byte represents green. The last byte represents blue.

Figure 7-5 shows the bit organization of the 32-bit BI_RGB format.

Figure 7-5 The 32-Bit BI_RGB Format


Playback devices that support 1, 4, 8, and 24 bits/pixel bitmaps can use the BI_RGB format. For 16 and 32 bits/pixel bitmaps, support for the BI_RGB format is desirable.

7.5.2 The BI_BITFIELDS Format

In addition to the 16 and 32 bits/pixel BI_RGB format, the BI_BITFIELDS flag has been defined as a value for the biCompression field (compression format) of the BITMAPINFOHEADER data structure. The BI_BITFIELDS flag is valid for use with 16- and 32-bit bitmaps.

The BI_BITFIELDS flag specifies that the bitmap is not compressed and that a color mask is defined in the bmiColors field in the BITMAPINFO data structure. This format is valid when the biBitCount field of the BITMAPINFOHEADER data structure is set to 16 or 32. The BI_BITFIELDS flag is recognized only by enhanced display drivers and does not need to be supported by most display drivers.

Setting the biCompression field to BI_BITFIELDS indicates that the bmiColors field contains three DWORDS used to mask each pixel in the bitmap. The masks are used to obtain the RGB color values of the pixel. The first DWORD contains the red mask, the second DWORD contains the green mask, and the third DWORD contains the blue mask. The image bits follow the three DWORDs. The color masks have the following characteristics:

These characteristics do not restrict any one mask to a particular location in a DWORD. For example, the red mask can occupy the least significant, most significant, or central position in the DWORD combination of all three masks. The position of each mask corresponds to the color position defined for the appropriate RGB component of each pixel. For a 16-bit image, this implies that the color masks will reside in the low-ordered word of the DWORD. (For 16-bit images, set the biBitCount field of the BITMAPINFOHEADER data structure to 16; for 32-bit images, set it to 32.)

Additionally, the bits in the mask should be set only for the bit positions in the pixel that can represent color. Because unused bits in a pixel are always masked, set the unused bits in a pixel to either zero or one. For example, color masks can be used to decode the colors of a 16-bit pixel divided into three unequal groups of bits to represent the red, green, and blue color values. The group containing the five most significant bits represents red. The group containing the middle six bits represents green. The group containing the five least significant bits represents blue. (This format is also referred to as RGB565 format.)

Figure 7-6 shows the definitions of the color masks and the bit organization of a pixel with RGB565 format.

Figure 7-6 The RGB565 Format Using BI_BITFIELDS


Drivers obtain the RGB values for a pixel by masking the pixel with the DWORD corresponding to each color mask and then they map the colors to the appropriate registers for display. If an application needs to retrieve the individual color values for a pixel, it can use the color masks to separate the color components and then shift right each color component by the number of least significant zeros in the mask.

7.5.2.1 BITMAPINFO Data Structure

Example 7-3 shows the BITMAPINFO data structure definition.

Example 7-3 BITMAPINFO Data Structure Definition

typedef struct tagBITMAPINFO 
{ 
    BITMAPINFOHEADER bmiHeader; 
    RGBQUAD bmiColors[1]; 
} BITMAPINFO; 

The BITMAPINFO data structure has the following fields:

bmiHeader
Matches the standard BITMAPINFOHEADER data structure.

bmiColors
Defines the color masks used for red, green, and blue.


Previous Next Contents Index