Bottom Up

Learning Goals

  • Practice working with images
  • Learn 关于 metadata
  • Learn 更多关于如何 structs can be useful

背景

Imagine an image you need to present to your boss for a major presentation somehow got corrupted! Upon investigating, you find that the image is mostly intact. Except, when you view the image, it now appears to be upside down!

回忆一下 a digital image file is just a sequence of bits, arranged in some fashion. 一份 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数据结构 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. (回忆一下 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.

Your job is to edit the metadata programmatically so that the bitmap’s top row is first and bottom row last. Best to avoid editing the pixels directly, lest you further corrupt the file!

  • 提示
    • 请务必 look carefully at the members of the BITMAPINFOHEADER struct in bmp.h.
    • If you 阅读 the documentation for each of these members, which should you modify? How so?

入门

  1. Log into cs50.dev using your GitHub account.
  2. Click inside the terminal window and execute cd.
  3. Execute wget https://cdn.cs50.net/2022/fall/实验s/4/bottomup.zip followed by Enter in order to 下载 a zip called bottomup.zip in your codespace. Take care not to overlook the space between wget and the following URL, or any other character for that matter!
  4. Now execute unzip bottomup.zip to create 的文件夹中名为 bottomup.
  5. You 不再 need the ZIP file, so you can execute rm bottomup.zip and respond with “y” followed by Enter at the prompt.

演示

实现细节

Go ahead and pull up the URLs to which BITMAPFILEHEADER and BITMAPINFOHEADER are attributed, per the comments in bmp.h. Take a 关闭 look at the members of the BITMAPINFOHEADER struct. Use that information to 编写一个 bit of code in bottomup.c to change the image from bottom-up to top-down. The code 中的问题 needn’t be very complicated, particularly if you know what you’re doing!

Thought 问题

  • Why do image files need metadata?

如何测试 Your Code

你的程序应该 behave per the例子below.

bottomup/ $ ./bottomup harvard_bottomup.bmp harvard_topdown.bmp

When your program is working correctly, you should see a new file, harvard_topdown.bmp in your bottomup directory. 打开 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 提交, by typing in the following at the $ prompt. But be sure to test it自己 as well!

check50 cs50/实验s/2023/x/bottomup

To evaluate that the style of your code (indentations and spacing) is correct, type in the following at the $ prompt.

style50 bottomup.c

如何提交

No need to 提交! This is a 练习题.