11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10. Bitmap<br />

The next part of the DIB data is color table, which comprises an array of RGBQUAD objects. The bitmap<br />

information header and color table together form a bitmap header, which can be described by a BITMAPINFO<br />

structure:<br />

typedef struct tagBITMAPINFO {<br />

BITMAPINFOHEADER bmiHeader;<br />

RGBQUAD bmiColors[1];<br />

} BITMAPINFO;<br />

DIB Example<br />

Following the bitmap header are bitmap bit values. Image data is stored in the memory one pixel after<br />

another from left to right, and vertically from the bottom to the top. The following is an example of 3×4<br />

image:<br />

W B W<br />

B W B<br />

W B W<br />

B W B<br />

This image has only two colors: black and white. If we store it using monochrome DIB format (2<br />

colors), we need only 3 bits to store one raster line. For 16-color DIB format, we need 12 bits to store one<br />

line. Since each raster line must use multiple of four-byte buffers (32 bits), if one raster line can not use up<br />

all the bits, the rest will simply be left unused.<br />

The following table compares four different types of DIB formats by listing the following information:<br />

the necessary bits needed, the actual bits used, and number of bits wasted by one raster line:<br />

DIB format Bits Needed for One<br />

Raster Line<br />

Actual Bits Used by One<br />

Raster Line<br />

Bits Wasted for One<br />

Raster Line<br />

2 color 3 32 29<br />

4 color 12 32 20<br />

8 color 24 32 8<br />

24 bit 72 96 24<br />

We can define a macro that allows us to calculate the number of bytes needed for each raster line for<br />

different bitmap formats:<br />

#define WIDTHBYTES(bits) ((((bits)+31)/32)*4)<br />

Here bits represents the number of bits that are needed for one raster line, it can be obtained from<br />

BITMAPINFORHEADER by doing the following calculation:<br />

biWidth×biBitCount<br />

Now we know how to calculate the value of biSizeImage from other members of structure<br />

BITMAPINFOHEADER:<br />

WIDTHBYTES(biWidth*biBitCount)*biHeight<br />

The following is the image data for the above DIB example, assume all unused bits are set to 0:<br />

2 color bitmap (assuming in the color table, index to white color = 0, index to black color = 1):<br />

C0 00 00 00<br />

40 00 00 00<br />

C0 00 00 00<br />

40 00 00 00<br />

287

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!