How does Spin Palette BMP work?

            In fact, Spinning palette BMP is normal BMP picture resolution 320x400 pixels and 256 colors or color depth 8 bits per pixels. The difference between normal BMP and spinning palette BMP is only one byte. I want to show you the BMP file format first, before we will discuss about spinning palette BMP.
 BMP file format
Offset
Size (bytes)
Description
0h
2
Value = "BM"
2h
4
File size (bytes)
6h
4
Value = 0
Ah
4
Offset to picture, PO
Eh
4
Value = 28h
12h
4
Image width (pixels)
16h
4
Image height (pixels)
1Ah
2
Value = 1
1Ch
2
Color depth, BPP (bits per pixel)
1Eh
4
Compression type. Normally = 0
22h
4
Size of compressed image, PSZ (bytes)
26h
4
H resolution (pixels per meter)
2Ah
4
V resolution (pixels per meter)
2Eh
4
Number of color used, NCL. Value = 0 for full color set
32h
4
Number of important color, NIC.
36h
 4*NCL
Color palette
PO
PSZ
Picture bitmap
 
 Purpose of palette
In order to reduce size of bitmap, we use palette to specify color of each pixels than by enumerate all intensity of red, green and blue color. The only used colors will be kept in color palette. And picture bitmap keep information of which color in palette each pixels use.
Example for picture below
   The colors would be kept are 
Thus the color palette details would look like this
Index
Red
Green
Blue
0
255
0
0
1
196
0
38
2
255
255
0
3
253
202
138
4
0
255
0
5
0
135
64
6
0
255
255
7
0
0
255
8
255
255
255
9
0
0
0
 
And the bitmap details would look like this
Like a cross-stitch pattern, isn't it?
 
Deep into file
If we save our example picture to BMP file. And use some kind of binary file viewer to look deep into this BMP. You should see....
Let's see the BMP file format again. First two bytes are BMP file signature "BM".
Next in offset 2 is size of this file = B8h.
Offset Ah keep information where is the bitmap pattern location, in this case = 5Eh.
Offset 12h and 16h are picture's width and height = 0Bh or 11x11 pixels.
Offset 1Ch indicate BPP of picture. In this case = 4 because number of color used in this picture is 10. Then the least number of bit that still give number of color much enough is 4 (2^4 = 16).
Offset 1Eh is 0 tell us this BMP has no compression.
Offset 22h should indicate size of bitmap but in this case it is 0. Don't worry there is no problem. It because of this field was neglected today by most of program include the program that generated this BMP.
Offset 2Eh tell the number of color used = Ah or 10. If this field is 0, it means picture uses full color set or uses all 2^BPP colors.
Offset 32h tell the number of important color. We will talk about this field later.
Offset 36h is start of color palette. Color palette size is 4*NCL =  40 bytes. So from offset 36h-5Dh is palette's area. It keeps color information in form "BGR_". As we can see above, first 4 bytes of palette's data are 00 00 FF 00 that means first color has B=0, G=0, R=255 and next 4 bytes of palette are 26 00 C4 00 that means next color has B=38, G=0, R=196 and so on.
And offset 5Eh is start of picture bitmap. Picture's data would be saved pixel by pixel from bottom to top, from left to right. Since BPP = 4 means 4 bits used for one pixel or data in bitmap one byte would contain 2 pixels. From figure above, byte stream of bitmap are
99 55 55 55 55 50 90 09 95 55 55 57 77 70 99 55 95 44......
Interested point is after most right pixel of bottom line's data there are few unknown bytes appear before data of next line.
99 55 55 55 55 50 90 09 95 55 55 57 77 70 99 55 95 44......
Where are they come from?
The reason is Windows' bitmap was designed for be read or writed 32 bits (4 bytes) each time. So the dummy bytes were added for this activity.
[99 55 55 55] [55 50 90 09] [95 55 55 57] [77 70 99 55] [95 44......
Spinning palette
And the most interested point is how can we make spinning palette BMP. As we mention in above section about one field at offset 32h, the number of important color field. In past when computer display technology was not powerful as today. To display high quality color is almost impossible. Displaying resource must be most efficiently used. NIC field was used for this reason. But today, this field was useless. New function was assigned for this field by Windows 95. It is used to tell number on still color. Color under this number will still while color elswhere will spin.
In example, if we have picture's palette like this
 
If NIC is 0 means all colors would still.
If NIC is F0h means all colors under F0h would still while color F0h to FFh would spin like this.
 
 That's all folk!. I have told you, it is only one byte differ from general BMP, offset 32h.
You can make your own spinning palette BMP just by modifying this NIC field.
 
Relate files
EX.BMP : Example BMP picture.
HEXVIEW.ZIP : Binary file viewer. Another freeware from QTam software.
BMP.TXT : Quick reference for BMP file format.

By Panya Thanyaprasertkul
Homepage http://www.geocities.com/SiliconValley/Campus/7779
Email khun_panya@yahoo.com


This page hosted by    Get your own Free Home Page