Peeking Inside a Bitmap


In order to use bitmaps in your games , you must have a basic understanding of how they are put together. More specifically , you need to have knowledge of the inner structure of a bitmap because you must write code that reads this structure and extracts information about the bitmap. This is necessary so that you can create a bitmap object that can be drawn. You'll be glad to know that bitmaps aren't too terribly complicated, as is evident in Figure 5.1.

Figure 5.1. The structure of a bitmap consists of three parts : a header, color table, and image data.

graphics/05fig01.gif

The figure reveals that every bitmap consists of three basic parts:

  • Header

  • Color table

  • Image data

The header contains information pertaining to the overall makeup of the bitmap, such as its width, height, number of bits per pixel (8 or 24), and so on. The color table contains the palette for the bitmap, which is the list of colors used throughout the image. The color table is extremely important for 8-bit bitmaps because it describes up to 256 colors used by pixels in the image. 24-bit bitmaps don't need a color table because their colors are sufficiently described by the pixels themselves . The image data is where the actual pixels of the bitmap are stored. For example, if a bitmap is 10x12, it is 10 pixels across and 12 pixels down for a total of 120 pixels. If it is an 8-bit image, each pixel requires 8 bits (1 byte) to describe its color. So, the image data for this bitmap would consist of 120 bytes. To determine the color of each pixel, you would look up the byte value in the color table to see what color the pixel is.

Of course, all this color table and image data stuff takes place automatically once you load an image and start using it with GDI functions. In other words, you only have to worry yourself with the inner workings of bitmaps when you first load the bitmap from a file or resource. Once loaded, you use a handle to a bitmap to draw it to a device context in much the same way as you drew graphics primitives to a device context in Hour 4, "Learning to Draw Basic Graphics."



Sams Teach Yourself Game Programming in 24 Hours
Sams Teach Yourself Game Programming in 24 Hours
ISBN: 067232461X
EAN: 2147483647
Year: 2002
Pages: 271

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net