510 likes | 870 Views
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
E N D
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 http://www.cs.ucc.ie/j.bowen/cs1107/imageFormats/bmp/bitmapStorage.html and http://www.cs.ucc.ie/j.bowen/cs1107/imageFormats/bmp/formatDesc.html • The first of these is a local copy of this Microsoft page: http://msdn.microsoft.com/en-us/library/dd183391(v=VS.85).aspx
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
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 46 04 00 00hex • This number is in little-endianformat, so it is really 00 00 04 46hex • This is equal to 1094dec • We saw earlier that this is, indeed, the size of the file
File test.bmp • Bytes eleven to fourteen of the File Header contain 36 04 00 00hex • This number is in little-endian format, so it is really 00 00 04 36hex
File test.bmp • Bytes eleven to fourteen of the File Header contain 36 04 00 00hex • This number is in little-endian format, so it is really 00 00 04 36hex • 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 28 00 00 00hex • This number is in little-endian format, so it is really 00 00 00 28hex • As the format specifies, this is equal to 40dec
File test.bmp • Bytes five to eight of the Info Header contain 04 00 00 00hex • This number is in little-endian format, so it is really 00 00 00 04hex • 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 04 00 00 00hex • This number is in little-endian format, so it is really 00 00 00 04hex • 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 01 00hex • This number is in little-endian format, so it is really 00 01hex • 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 08 00hex • This number is in little-endian format, so it is really 00 08hex • 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 00 00hex • So the picture is not compressed
File test.bmp • Bytes twenty-one to twenty-four of the Info Header contain 10 00 00 00hex • This number is in little-endian format, so it is really 00 00 00 10hex • 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 00 00hex • This number is in little-endian format, so it is really 00 00 0B 12hex • This specifies 2834dec pixels per metre horizontally
File test.bmp • Bytes twenty-five to twenty-eight of the Info Header contain 12 0B 00 00hex • This number is in little-endian format, so it is really 00 00 0B 12hex • This specifies 2834dec pixels per metre horizontally • This agrees with the fact that PhotoPlus says that 4 pixels equals 0.141 centimetres
File test.bmp • Similarly, bytes twenty-nine to thirty-two of the Info Header contain 12 0B 00 00hex • This number is in little-endian format, so it is really 00 00 0B 12hex • Again, this specifies 2834dec pixels per metre horizontally • Which agrees with the fact that PhotoPlus says that 4 pixels equals 0.141 centimetres
File test.bmp • Bytes thirty-three to thirty-six of the Info Header contain 00 01 00 00hex • Since this is in little-endian, it is really 00 00 01 00hex • This means that there are 256dec colours in the palette
File test.bmp • Bytes thirty-three to thirty-six of the Info Header contain 00 01 00 00hex • Since this is in little-endian, it is really 00 00 01 00hex • This means that there are 256dec colours in the palette • Since there are 4 bytes per colour, the palette should occupy 04 00hex bytes • Which it does, ranging from 0036 to 0435
File test.bmp • Bytes thirty-seven to forty of the Info Header contain 02 00 00 00hex • Since this is in little-endian, it is really 00 00 00 02hex • 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 00 00 FF 00 • The fourth byte is unused 00 00 FF00, so ignore it • Since the three bytes 00 00 FF are little-endian, they are really FF 00 00 • These represent proportions of RGB (Red, Green, Blue) • Thus, the first colour in the palette consists of 255red0green0blue • That is, the first colour in the palette is pure red
File test.bmp • Bytes five to eight of the Colour Table contain FF 00 0000 • Since the three bytes FF 00 00 are little-endian, they are really 00 00 FF • Thus, the second colour in the palette consists of 0red0green255blue • 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 78dec 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 12-15hex and 16-19hex, respectively • specify the number of colours in bytes 2E-31hex • specify the colours in 4-byte blocks from byte 36hex 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-0Dhex • put the overall file size in bytes 02-05hex