2

This is the hex dump of a 5x5 bitmap colored with the color red:

enter image description here

My question is, how do I change only the hex values (without using a tool like Paint) in order to change the bitmap's color to e.g. the color green (0,255,0) or any color for that matter?

In extent, how do tools like Paint convert the pixels you draw into hex to save the bitmap?

1 Answer 1

2

Here is documentation for BMP: https://en.wikipedia.org/wiki/BMP_file_format#Pixel_array_.28bitmap_data.29

The short form is:

  • 14 bytes BMP header (file information)
  • 40 bytes DIB header (image information)
  • pixels

You can see in within the DIB header section the two values 5 for the pixel dimensions, and a value of hex(18) for 24-bit color (RGB).

Of particular interest is the "Example 1" in the wiki which shows a HEX dump for a 2x2 image similar to what you have here. A few notes:

  • a byte, visually, is a two digit couplet (the first byte in your image is 42).
  • counting starts at 0, so "offset 31" is the 32nd byte.
  • numbers are all HEX, so a hex(10) = dec(16). You can use windows calculator set to "programmer mode" if you need help converting.
  • "byte order" is low-to-high, so a 4 byte value of "05 00 00 00" needs to be understood as "00 00 00 05"
  • the RGB triplets in the bmp are reversed

Based on the hex dump and the documentation, your image is storing each pixel as an uncompressed RGB triplet. The first pixel is the triplet 24 1c ed, but this is reversed, so it is hex(ed1c24) which which is indeed a red color. Note that there is a 00 pad after every row of 5 pixels. The documentation says this is for "byte alignment."

So to change a pixel from red to blue, you might alter one of the triplets from 24 1c ed to ed 1c 24.

As far as software, you would open the file (or a copy) with a Hex Editor, make the changes and save it. Simply using a text editor in "text edit mode" is not enough as the text symbol "0" is not the same as the binary numeric value of 0.

Open source Notepad++ has hex editing capability via plugins.

Regarding the larger question of how Paint makes an image, well, the program uses the documentation and standards to automate the process. Its mostly a matter of convention. The "image stream" (pixel data) is stored and manipulated in memory in a similar manner as you see in the uncompressed image file.

3
  • Oh you are correct. I was confused at start, because 1) of the random "00"s and 2) from the reversed hex color values, but using a hex editor I replaced all the "24 1C ED" with "00 FF 00" to swap the image, and my example goal (changing the color from red to green) was achieved. Thanks.
    – Offtkp
    Oct 20, 2016 at 18:22
  • No problem. The docs say something about "padding for 4-byte alignment" and a full row of 5 pixels plus one byte padding = 16 bytes (or 4 sets of 4). So the amount of padding is going to vary from 0 to 3 bytes.
    – Yorik
    Oct 20, 2016 at 18:25
  • they call the byte ordering "endianness". I believe the example here is "Little-endian". If I recall, endianness was an code-reuse problem between old mac and old pc designs which prevented people from taking the time to make software cross-platform.
    – Yorik
    Oct 20, 2016 at 18:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .