Using a hex editor to edit an image We can actually use hex editors to change image files Usually, we will use programs like PhotoPlus or PhotoShop But if you use a hex editor, you will appreciate the fundamental details of how images are stored in files We will change test.bmp using XVI32 First, however, we must learn some details of the.bmp format
The.bmp format A file in the Microsoft.bmp format can also be known as a.dib file A detailed description can be found here and The first of these is a local copy of this Microsoft page:
The.bmp format A.bmp file file has four sections The File Header contains general details about the file The Info Header contains general details about the image in the file The Colour Table describes the palette of colours used in the image The Pixel Data specifies the contents of the dots in the picture by specifying, for each pixel, which colour in the palette is used in the pixel
The.bmp format
File test.bmp
The first two bytes of the File Header contain the ASCII codes for BM, as specified
File test.bmp Bytes three to six of the File Header contain hex This number is in little-endian format, so it is really hex This is equal to 1094 dec We saw earlier that this is, indeed, the size of the file
File test.bmp Bytes eleven to fourteen of the File Header contain hex This number is in little- endian format, so it is really hex
File test.bmp Bytes eleven to fourteen of the File Header contain hex This number is in little- endian format, so it is really hex This number points to the start of the pixel data in the file
File test.bmp Bytes one to four of the Info Header contain hex This number is in little- endian format, so it is really hex As the format specifies, this is equal to 40 dec
File test.bmp Bytes five to eight of the Info Header contain hex This number is in little- endian format, so it is really hex This is, indeed, equal to the width of the picture in test.bmp, which is 4px wide by 4 px high
File test.bmp Bytes nine to twelve of the Info Header contain hex This number is in little- endian format, so it is really hex This is, indeed, equal to the height of the picture in test.bmp, which is 4px wide by 4 px high
File test.bmp Bytes thirteen to fourteen of the Info Header contain hex This number is in little- endian format, so it is really hex This is, indeed, equal to the number 1 which is specified in the format
File test.bmp Bytes fifteen to sixteen of the Info Header contain hex This number is in little- endian format, so it is really hex So 8 bits are used to represent each pixel, allowing for a palette which contains 256 different colours Even though only 2 colours are used in the picture
File test.bmp Bytes seventeen to twenty of the Info Header contain hex So the picture is not compressed
File test.bmp Bytes twenty-one to twenty-four of the Info Header contain hex This number is in little- endian format, so it is really hex Since the image is not compressed, this value seems to be irrelevant
File test.bmp Bytes twenty-five to twenty-eight of the Info Header contain 12 0B hex This number is in little- endian format, so it is really B 12 hex This specifies 2834 dec pixels per metre horizontally
File test.bmp Bytes twenty-five to twenty-eight of the Info Header contain 12 0B hex This number is in little- endian format, so it is really B 12 hex This specifies 2834 dec pixels per metre horizontally This agrees with the fact that PhotoPlus says that 4 pixels equals centimetres
File test.bmp Similarly, bytes twenty- nine to thirty-two of the Info Header contain 12 0B hex This number is in little- endian format, so it is really B 12 hex Again, this specifies 2834 dec pixels per metre horizontally Which agrees with the fact that PhotoPlus says that 4 pixels equals centimetres
File test.bmp Bytes thirty-three to thirty-six of the Info Header contain hex Since this is in little- endian, it is really hex This means that there are 256 dec colours in the palette
File test.bmp Bytes thirty-three to thirty-six of the Info Header contain hex Since this is in little- endian, it is really hex This means that there are 256 dec colours in the palette Since there are 4 bytes per colour, the palette should occupy hex bytes Which it does, ranging from 0036 to 0435
File test.bmp Bytes thirty-seven to forty of the Info Header contain hex Since this is in little- endian, it is really hex This means that there are only 2 important colours in the palette Which agrees with what we know -- the picture contains only blue and red pixels
File test.bmp Bytes one to four of Colour Table are FF 00 The fourth byte is unused FF 00, so ignore it Since the three bytes FF are little-endian, they are really FF These represent proportions of RGB (Red, Green, Blue) Thus, the first colour in the palette consists of 255 red 0 green 0 blue That is, the first colour in the palette is pure red
File test.bmp Bytes five to eight of the Colour Table contain FF Since the three bytes FF are little- endian, they are really FF Thus, the second colour in the palette consists of 0 red 0 green 255 blue That is, the first colour in the palette is pure blue
File test.bmp Before we look at the pixel data in the file, let's look at the image and zoom in to see it clearly
File test.bmp In the Pixel Data, the bottom row is stored first, the pixel details being given in the order left-to-right These four pixels are specified to use colour 01 in the colour table, that is the second colour, that is pure blue
File test.bmp The second row from the bottom is stored next, the pixels again in left-to- right order These four pixels are specified to use colour 00 in the colour table, that is the first colour, that is pure red
File test.bmp The third row from the bottom is stored next, the pixels again in left-to- right order These four pixels are specified to use colour 01 in the colour table, that is the second colour, that is pure blue
File test.bmp The top row is stored last, the pixels again in left-to-right order These four pixels are specified to use colour 00 in the colour table, that is the first colour, that is pure red
Changing test.bmp Let's use XVI32 to change the last byte in the file, changing it from 00 to 01 This byte represents the right-most pixel in the top row of the picture We have changed its value, making it refer to the second colour in the Colour Table, which is pure blue Let's store the modified picture in a file called test2.bmp
File test2.bmp When we open test2.bmp in PhotoPlus, we see that we have, indeed, made the change we intended The right-most pixel in the top row of the picture has been changed to blue
Storing the test.bmp image more efficiently Re-open the original test.bmp We can store its image more efficiently, by reducing the Colour Table to contain just the two colours that are used If we use XVI32 to make the appropriate changes, we get the bytes shown below -- now only 78 dec bytes are needed Let's store the modified data in a file called test3.bmp
Viewing image in test3.bmp When we open test3.bmp in PhotoPlus, we see that the image in the 78-byte file looks exactly like the image in the 1094-byte file
Changing the aspect ratio of the image We can easily convert the image in test3.bmp into an 8-by-2 image instead of a 4-by-4. because both images contain 16 bytes In XVI32, we need change only the two bytes highlighted below Let's store the modified data in a file called test4.bmp
Viewing image in test4.bmp The image has two rows, each eight pixels long
Lines must be padded out to multiples of 4 bytes Below is a file containing a 5-by-3 image, a 15-pixel image Although the image has one pixel less than the previous image, the file is larger - it contains 86 bytes instead of 78 This is because, although each line has only 5 pixels, each line must be padded out to a multiple of 4 bytes, so 8 bytes are needed for each line Below, the padding bytes are coloured light blue - zeros are used for padding here, but it appears that the padding can be anything Let's store this 15-pixel image in a file called test15d.bmp
Viewing image in test15d.bmp The image has three rows, each five pixels long As we see on next slide, the pixels are as we would predict from XVI32
Viewing image in test15d.bmp The pixels are as we would predict from XVI32 –Remember that the pixels of the top row appear last in the file –Remember that colours are represented as Blue-Green-Red-unused
A bug in PhotoPlus As we shall see later, there is a bug in the way that PhotoPlus handles.bmp files So, first, let's check that the view which PhotoPlus gives of the image in test15d.bmp is correct We will look at the image in another image editor and in some browsers
Viewing image in test15d.bmp in MS Paint The View menu has been tuned to 800% so that we can see detail The image looks the same as we expect
Viewing image in test15d.bmp in Firefox The View menu has been used to zoom in so that we can see detail The image looks the same as we expect
Viewing image in test15d.bmp in Opera The View menu has been used to zoom in so that we can see detail The image looks the same as we expect
Viewing image in test15d.bmp in MSIE Internet Explorer does not display.bmp files itself Instead, it calls MS Paint as a helper program As we have already seen, in MS Paint the image looks as expected
Absolutely basic 8-bit depth.bmp format We have seen that the common image display programs ignore much of the format for 8-bit-depth.bmp files The minimum we must do when making an 8-bit depth file is –assign the fixed values shown below to the bytes marked green –put the image width and depth in bytes hex and hex, respectively –specify the number of colours in bytes 2E-31 hex –specify the colours in 4-byte blocks from byte 36 hex onwards (two colours are shown in the example below) –specify the pixels immediately afterwards, padding the lines if necessary (16 pixels are shown in the example below) –put the address of the first pixel specification in bytes 0A-0D hex –put the overall file size in bytes hex