Understand why in the case of smallbmp 3 bytes worth

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: or, equivalently, offsets from the file’s first byte, all of them given in hex. Note that 00000036 in hexadecimal is 54 in decimal. You’re thus looking at byte 54 onward of smiley.gif. Recall that a 24 ­bit BMP’s first 14 + 40 = 54 bytes are filled with metadata. If you really want to see that metadata in addition to the bitmap, execute the command below. xxd -c 24 -g 3 smiley.bmp If smiley.bmp actually contained ASCII characters, you’d see them in xxd’s rightmost column instead of all of those dots. So, smiley.bmp is 8 pixels wide by 8 pixels tall, and it’s a 24 ­bit BMP (each of whose pixels is represented with 24 ÷ 8 = 3 bytes). Each row (aka “scanline”) thus takes up (8 pixels) × (3 bytes per pixel) = 24 bytes, which happens to be a multiple of 4. It turns out that BMPs are stored a bit differently if the number of bytes in a scanline is not, in fact, a multiple of 4. In small.bmp, for instance, is another 24 ­bit BMP, a green box that’s 3 pixels wide by 3 pixels wide. If you feel like SFTPing that file to your desktop and double ­clicking it, you’ll see that it resembles the below, albeit much smaller. 6 < 16 This is CS50. Harvard College Fall 2010 Each scanline in small.bmp thus takes up (3 pixels) × (3 bytes per pixel) = 9 bytes, which is not a multiple of 4. And so the scanline is “padded” with as many zeroes as it takes to extend the scanline’s length to a multiple of 4. In other words, between 0 and 3 bytes of padding are needed for each scanline in a 24 ­bit BMP. (Understand why?) In the case of small.bmp, 3 bytes’ worth of zeroes are needed, since (3 pixels) × (3 bytes per pixel) + (3 bytes of padding) = 12 bytes, which is indeed a multiple of 4. To “see” this padding, go ahead and run the below. xxd -c 12 -g 3 -s 54 small.bmp Note that we’re using a different value for -c than we did for smiley.bmp so that xxd outputs only 4 columns this time (3 for the green box and 1 for the padding). You should see output like the below; we’ve highlighted in green all instances of 00ff00. 0000036: 00ff00 00ff00 00ff00 000000 0000042: 00ff00 ffffff 00ff00 000000 000004e: 00ff00 00ff00 00ff00 000000 For contrast, let’s use xxd on large.bmp, which looks identical to small.bmp but, at 12 pixels by 12 pixels, is four times as large. Go ahead and execute the below; you may need to widen your window to avoid wrapping. xxd -c 36 -g 3 -s 54 large.bmp You should see output like the below; we’ve again highlighted in green all instances of 00ff...
View Full Document

This note was uploaded on 03/22/2013 for the course COMP SCI CS-50 taught by Professor Malan during the Spring '10 term at Harvard.

Ask a homework question - tutors are online