turrier.fr

Source : ct|02.06.08

< Tutorials Computer, Multimedia, Chinese

Create a bitmap file with an hexadecimal editor

Introduction

An Device Independant Bitmap (DIB) picture can be created with Paint, then saved in a file with a bmp format. It can also be created, pixel per pixel, with an hexadecimal editor. To study the bmp format, with a 24 bits per pixel resolution, we will use an example.

Create then saved with Paint, with the name "essai5.bmp" for example and with a graphic resolution of 24 bits by pixel, an all red picture with a size 5 pixels x 5 pixels.

image1

image2

Open this file "essai5.bmp" with a hexadecimal editor. You will see all the bytes, which can be separated in 3 successive parts:
- A part BITMAPFILEHEADER (indicated in red);
- A part BITMAPINFOHEADER about the picture (indicated in blue);
- A part Color description of every pixel (indicated in green).

image3

This file includes a total of 134 bytes (16 lines of 8 bytes + 1 line of 6 bytes. 16x8+6=134 bytes). The RGB color values of every pixel are saved in the file according to the order B, V then R ( 00 00 FF).

The BitmapFileHeader

The first part, the bitmapfileheader, includes 14 bytes. It contains informations about the type and the size of the file:

42 4D 86 00 00 00 00 00 00 00 36 00 00 00

typedef struct tagBITMAPFILEHEADER
{
42 4D: type of file (BM)
86 00 00 00 : size of file
00 00 : 2 bytes reserved (zéro)
00 00 : 2 bytes reserveds (zéro)
36 00 00 00 : offset of pixels bytes
} BITMAPFILEHEADER;

- The size of the file is here 134 bytes (&H86=16*8+6=134).
- The offset of the pixels bits, regarding to the beginning of the file, is here equal to 54 (&H36 in hexadecimal = 54 in decimal); You can verify this. There are well 54 bytes before the first byte 00 of the pixels.

.

The BitmapInfoHeader, about the picture

The second part, the bitmapinfoheader (about the image), makes 40 bytes. It contains informations about the size and the color format of the picture.

28 00 00 00 05 00 00 00
05 00 00 00 01 00 18 00
00 00 00 00 50 00 00 00
C4 0E 00 00 C4 0E 00 00
00 00 00 00 00 00 00 00

typedef struct tagBITMAPINFOHEADER
{
28 00 00 00 : structure size
05 00 00 00 : picture width in pixels
05 00 00 00 : picture height in pixels
01 00 : number of display plans
18 00 : number of bits per pixel
00 00 00 00 : type of compression
50 00 00 00 : number of bytes in the picture
C4 OE 00 00 : horizontal resolution
C4 OE 00 00 : vertical resolution
00 00 00 00 : number of used colors
00 00 00 00 : number of important colors
} BITMAPINFOHEADER;

The sizeof the BITMAPINFOHEADER structure is 40 bytes ( &H28=40); The width of the picture, in our example, and its height are 5 pixels; The number of plans for the display is 1; The number of bits per pixel, in our example, is 24 (&H18=24 ); The type of compression is equal to 0, because the file is not compressed; The number of bytes in the picture is equal to 80 (&H50=80), in our example. Indeed, there are 5 lines containing each 5 pixels and a byte 00 at the end of lines. There are 3 bytes by pixels, so the total is ((5*3)+1)*5=80; The horizontal and vertical resolutions, in pixels per metre, are equal to C4 0E in hexadecimal value; For the number of used colors, the value 0 indicates that the bitmap can use a maximum number of colors; The number of important colors indicates the number of indexed colors required to be able to display the bitmap. If this value is in zero, all the colors are required.

The pixels

The third part contains the RGB colors values of all the pixels constituing the picture. In this part, the pixels are described in the inverse order of their display position in the screen. The pixels associated to the last line appear in the first one and the pixels associated with the first line appear lastly in the file. The size of this part is variable because it depends on the size of the picture.

0000FF 0000FF 0000FF 0000FF 0000FF 00
1st file line <-> 5th screen line

0000FF 0000FF 0000FF 0000FF 0000FF 00
2nd file line <-> 4th screen line

0000FF 0000FF 0000FF 0000FF 0000FF 00
3th file line <-> 3th screen line

0000FF 0000FF 0000FF 0000FF 0000FF 00
4th file line <-> 2nd screen line

0000FF 0000FF 0000FF 0000FF 0000FF 00
5th file line <-> 1st screen line

Every line describes pixels by going from the left towards the right of the image. The length of every line must be a multiple of 4 bytes, that is why byte 00 are added, at the end of every line of pixels, in the file.

Are:
X: The number of bytes by line, in the file;
N: The number of pixels by lines on the screen;
C: The number of additional bytes 00 in the file, such as X is a multiple of 4;
We have X = N x 3 + C = 16 x i, with i natural integer.

We have here X = 5 x 3 + 1 = 16. For this reason, only a single byte is added per line (16 is multiple of 4).

In our example ( an all red picture) the RGB color values of every pixel are the following ones:
RGB colors values blue (B=00): 0*16 + 0 = 0;
RGB colors values green (G=00): 0*16 + 0 = 0;
RGB colors values red (R=FF): 15*16 + 15 = 255;

Modify the bytes of the file

Open the file with an hexadecimal editor. Modify the colors of the pixels (from red to green) by replacing bytes 00 00 FF by 00 FF 00. Keep the supplementary byte 00 at the end of every line unchanged.

image4

Save the new so created file, with the name "essai1.bmp" for instance. Then open it with Paint. All the pixels of the picture are green now. It is indeed the expected result.

image5


Valid XHTML 1.0 Transitional

© http://turrier.fr (2007)