Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand bitmaps pixel by pixel

Tags:

c++

Below is the hex 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 represent and most specifically the pixel values. This is a bitmap saved with mspaint and it's a monochrome bitmap. Below is my understanding/misunderstanding...most of this is info you get from wiki and search bmp. This is just me trying to break down my bmp which has two black pixels on the top and two white pixels on 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 is really confusing i don't see how they would equal what they are supposed to equal plus I thought rgb or bgr data is just 3 bytes? Also the offset to where pixel data can be found goes 2 bytes past the last byte in the bitmap...I feel like I'm decoding it completely wrong or something.

like image 766
itb Avatar asked Dec 15 '22 11:12

itb


1 Answers

00 00 00 00
ff ff ff 00

These bytes are the color palette. Since your bits per pixel are set to 1, there can be only 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 filler and is always set to 00.

c0 00 00 00
00 00 00 00

This is the actual pixel data. Each row of pixels must be padded to the nearest 4 bytes that can contain the data. So here, the first row is the bottom row of pixels and the second row is the top row of pixels (since BMP pixel order is bottom-up). Since we are using 1 bit per pixel, we should look at it at the byte level. Specifically, the first row of pixels is given by:

1100 0000  0000 0000  0000 0000  0000 0000

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

0000 0000  0000 0000  0000 0000  0000 0000

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

like image 168
Joseph Mansfield Avatar answered Jan 06 '23 19:01

Joseph Mansfield