2015:05:05:DebugGitAndBitmaps
Sommaire
Introduction
The purpose of this session is to prepare you for the malloc mini-project. We will talk again about debuggint tools like GDB and valgrind. We will remind you how pointer arithmetic works. And last but not least, we will talk about git because it's fun and good for your projects health.
GDB
Listen carefully to your kind assistants.
Valgrind
Listen carefully to your kind assistants.
BMP file reading
Our purpose now is to read the content of a BMP file, using mmap(2). First, we need to build structures to handle the BMP headers. We'll limit our code on the most common BMP format (there're 7 variations of the second header, you can change the color depth, add compression … )
Here is our data structures for the two header of the BMP format:
struct bmp_img_head { unsigned int header_size; unsigned int width; unsigned int height; short int planes; short int bit; unsigned int comp_method; unsigned int image_size; int hres; int vres; unsigned int color_number; unsigned int important_color; }; struct bmp_header { short padding; // trick to align header char magic[2]; unsigned int file_size; unsigned int reserved; unsigned int offset; struct bmp_img_head dib; unsigned char data[4]; // entry point for data };
Your goal is to:
- Check the file size using stat(2) (read the manual page carefully)
- Open the file for reading and writing using open(2)
- Map the file using mmap(2) (you'll need to write to the memory area)
- Use the pointer returned by mmap(2) (read later for the padding trick)
- Close the file descriptor using close(2)
- Convert, in place, the image to grey (using the method already seen before)
- Unmap the file using munmap(2)
There's few details you need to take care of:
- When mapping the file, you should use the protection modes PROT_READ and PROT_WRITE and the flag MAP_SHARED in order to be able to modify the file.
- The BMP header was design in 16bits world, thus the header is not correctly aligned. One solution is to pad the structure with 2 bytes at the beginning. Once you get the pointer from mmap(2), call it p, you should use the address (struct bmp_header*)((char*)p - 2) as the pointer to your structure.
- You're goal is to override the content of the file, so take care to work on a copy of your BMP file to be able to restore it each time you're screwing it.
- To access pixel, it's pretty easy: the data are made of groups of 3 bytes (one per color) representing the pixels, lines after lines. If the length of the line (in bytes) is not a multiple of 4, there will be some 0 at the end of the current line, before the beginning of the next one.
Git
You will now read this course about git, listen to your assistants' explanations, and ask questions about what you don't understand.
Appencices (or Amygdala if all of this is Intercal non-sense)
May the 4th be with you !