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 />

to the nearest colors available in the system. To avoid color distortion, we must implement logical palette<br />

before drawing the bitmap.<br />

Another problem is that when we call function CBitmap::LoadBitmap(…) to load the bitmap resource,<br />

it will create a device dependent bitmap from the data stored in the resource. So after the the bitmap is<br />

loaded, it becomes DDB, which could only be understood by the device. To extract palette information<br />

from the bitmap data, we must use DIB format.<br />

To avoid color distortion, we must implement logical palette for device dependent bitmap before it is<br />

drawn to the target device. Because function CBitmap::LoadBitmap(…) does not extract palette and the<br />

bitmap data stored in the resource is in the format of DIB, we must convert the bitmap to DDB and extract<br />

palette information from it by ourselves.<br />

DIB Format<br />

A DIB comprises three parts: bitmap information header, color table, and bitmap bit values. The<br />

bitmap information header stores the information about the bitmap such as its width, height, bit count per<br />

pixel, etc. The color table contains an array of RGB colors, it can be referenced by the color indices. The<br />

bitmap bit values represent bitmap pattern by specifying an index into the color table for every pixel. The<br />

color table can also be empty, in which case the bitmap bit vlues must be actual R, G, B combinations.<br />

There are several type of DIBs: monocrome, 16 colors, 256 colors and 24 bit. The first three formats<br />

use color table and color indices to form a bitmap. The last format does not have a color table and all the<br />

pixels are represented by R, G, B combinations.<br />

The following is the format of bitmap information header:<br />

typedef struct tagBITMAPINFOHEADER{<br />

DWORD biSize;<br />

LONG biWidth;<br />

LONG biHeight;<br />

WORD biPlanes;<br />

WORD biBitCount<br />

DWORD biCompression;<br />

DWORD biSizeImage;<br />

LONG biXPelsPerMeter;<br />

LONG biYPelsPerMeter;<br />

DWORD biClrUsed;<br />

DWORD biClrImportant;<br />

} BITMAPINFOHEADER;<br />

It has 11 members, the most important ones are biSize, biWidth, biHeight, biBitCount and<br />

biSizeImage.<br />

Member biSize specifies the length of this structure, which can be specified by<br />

sizeof(BITMAPINFORHEADER). Members biWidth and biHeight specify the dimension of the bitmap<br />

image. Member biBitCount specifies how many bits are used to represent one pixel (Bit count per pixel).<br />

This factor determines the total number of colors that can be used by the bitmap and also, the size of the<br />

color table. For example, if this member is 1, the bitmap can use only two colors. In this case, the size of<br />

color table is 2. If it is 4, the bitmap can use up to 16 colors and the size of the color table is 16. The<br />

possible values of this member are 1, 4, 8, 16, 24, and 32. Member biSizeImage specifies the total number<br />

of bytes that must be allocated for storing image data. This is a very important member, because we must<br />

know its value before allocating memory.<br />

An image is composed of multiple raster lines, each raster line is made up of an array of pixels. To<br />

speed up image loading, each raster line must use a multiple of four-byte buffers (This means if we have a<br />

2-color (monochrom) 1×1 bitmap, we need four bytes instead of one byte to store only one pixel). Because<br />

of this, the value of biSizeImage can not be simply calculated by the following fomulae:<br />

biHeight*biWidth*biBitCount/8<br />

We will discuss how to calculate this value later.<br />

For bitmaps under Windows, member biPlanes is always set to 1. If the bitmap is not compressed,<br />

member biCompress should be set to BI_RGB.<br />

286

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

Saved successfully!

Ooh no, something went wrong!