Bottom Up
Learning Goals
- Practice working with images
- Learn about metadata
- Learn more about how
struct
s can be useful
Background
Imagine an image you need to present to your boss for a major presentation somehow got corrupted! Perhaps when your computer crashed it caused something to go awry. Fortunately, you took a course in digital forensics and upon investigating, you find that the image is intact, but there is something wrong with the metadata. When you view the image, it appears to be upside down!
Recall that a digital image file is just a sequence of bits, arranged in some fashion. A 24-bit BMP file, then, is essentially just a sequence of bits, (almost) every 24 of which happen to represent some pixel’s color. But a BMP file also contains some “metadata,” information like an image’s height and width. That metadata is stored at the beginning of the file in the form of two data structures generally referred to as “headers,” not to be confused with C’s header files. The first of these headers, called BITMAPFILEHEADER
, is 14 bytes long. (Recall that 1 byte equals 8 bits.) The second of these headers, called BITMAPINFOHEADER
, is 40 bytes long. Immediately following these headers is the actual bitmap: an array of bytes, triples of which represent a pixel’s color. However, BMP stores these triples backwards (i.e., as BGR), with 8 bits for blue, followed by 8 bits for green, followed by 8 bits for red. The image in question also stores the entire bitmap backwards, with an image’s top row at the end of the BMP file. Your job is to edit the metadata programmatically so that the bitmap’s top row is first and bottom row last.
- Hints
- Be sure to look carefully at the members of the
BITMAPINFOHEADER
struct
inbmp.h
. - If the bitmap height value is positive, the orientation of the BMP is bottom-up. If the height is negative the orientation is top-down.
- Be sure to look carefully at the members of the
Demo
Getting Started
- Log into code.cs50.io using your GitHub account.
- Click inside the terminal window and execute
cd
. - Then copy and paste
wget TODO
into your terminal to download this lab’s distribution code. - At the
$
prompt, typeunzip bottomup.zip
to create a folder calledbottomup
. - You no longer need the ZIP file, so execute
rm bottomup.zip
and respondy
at the prompt. - Now type
cd bottomup
to move into that directory. - If you now type
ls
you will see several files. - The file
copy.c
is already completed and creates a copy of the file supplied to it. - Execute
cp copy.c bottomup.c
and add a bit of code to correct the orientation of the image.
Implementation Details
Go ahead and pull up the URLs to which BITMAPFILEHEADER
and BITMAPINFOHEADER
are attributed, per the comments in bmp.h
. Take a close look at
the members of the BITMAPINFOHEADER
struct
. Use that information to write a bit of code in bottomup.c
to change the image from
bottom-up to top-down.
Thought Question
- Why do image files need metadata?
How to Test Your Code
Your program should behave per the examples below.
smiley/ $ ./bottomup harvard_bottomup.bmp harvard_topup.bmp
When your program is working correctly, you should see a new file, harvard_topup.bmp
in your bottomup
directory. Open it up and see if
the orientation of the image is correct.
You can check your code using check50
, a program that CS50 will use to test your code when you submit, by typing in the following at the $
prompt. But be sure to test it yourself as well!
TODO
To evaluate that the style of your code (indentations and spacing) is correct, type in the following at the $
prompt.
style50 bottomup.c
How to Submit
In your terminal, execute the below to submit your work.
TODO