Trying to understand pixel-based bitmaps

Below are the hexadecimal data in the bitmap

424d 46000000 0000 0000 3e000000 28000000 02000000 02000000 0100 0100 00000000 08000000 00000000 00000000 00000000 00000000 0000 0000 ffff ff00 c000 0000 0000 0000

I need to understand what all the values ​​are, and in particular, the pixel values. This is a bitmap saved with mspaint and it is a monochrome bitmap. Below is my understanding / misunderstanding ... most of this is the information you get from the wiki and bmp search. It's just that I'm trying to break my bmp, which has two black pixels on top and two white pixels at the bottom.

42 4d is BM 46 00 00 00 size of entire file 00 00 reserved 1 00 00 reserved 2 3e 00 00 00 offset to where pixel data can be found 28 00 00 00 # of bytes in this header 02 00 00 00 width of bmp 02 00 00 00 height of bmp 01 00 # of planes 01 00 # of bits per pixel 00 00 00 00 compression 08 00 00 00 size of raw data in pixel array in bytes 00 00 00 00 horizontal resolution pix/m 00 00 00 00 vertical resolution pix/m 00 00 00 00 number of colors 00 00 00 00 important colors 00 00 00 00 x=1 y=2 pixel value? is supposed to be white ff ff ff 00 x=2 y=2 pixel value? is supposed to be white c0 00 00 00 x=1 y=1 pixel value? is supposed to be black 00 00 00 00 x=2 y=1 pixel value? is supposed to be black 

The last pixel values ​​are really confusing, I don’t see how they will be equal in that they should be equal, and I thought that the rgb or bgr data is only 3 bytes? Also, the shift to where the pixel data can be found goes through 2 bytes past the last byte in the bitmap ... I feel like I am completely decoding it incorrectly or something like that.

+4
source share
2 answers
 00 00 00 00 ff ff ff 00 

These bytes are a color palette. Since your bits per pixel are set to 1, there can only be two colors in the palette. The first color is black ( 00 00 00 00 ), and the second is white ( ff ff ff 00 ). The last byte of each color is just a filler and always has the value 00 .

 c0 00 00 00 00 00 00 00 

This is the actual pixel data. Each row of pixels should be filled to the nearest 4 bytes that may contain data. So, here the first row is the bottom row of pixels, and the second row is the top row of pixels (since the BMP pixel order is ascending). Since we use 1 bit per pixel, we need to look at it at the byte level. In particular, the first row of pixels is defined as follows:

 1100 0000 0000 0000 0000 0000 0000 0000 

Since we have only two pixels in each row of pixels and only 1 bit per pixel, only the first two bits matter. In this case, 11 indicates that the first two pixels are the second color in the palette ( 1 ). Now for the second line we have:

 0000 0000 0000 0000 0000 0000 0000 0000 

and again we only need to look at the first two pixels, 00 . This means that the following pixels are the first color in the ( 0 ) palette.

+9
source

c0 00 00 00

The last 3 bytes of this block are for filling

Padding bytes (optionally 0) should be added to the end of lines to increase the length of lines to a few bytes. When an array of pixels is loaded into memory, each line should begin with a memory address that is a multiple of 4.

+3
source

Source: https://habr.com/ru/post/1479264/


All Articles