CS240 Spring 2011:PROJECT 5


BITMAP MANIPULATION

Goal

To create, and manipulate bitmap files with 24 bits per pixel.

The files for the lab can be downloaded from here. The files can be extracted by running :

tar -xvf project05.tar
The archive contains main.c, utils.c, bmp_header.c. You need to add your code to main.c , utils.c contains some functions which will be useful to you. bmp_header.h contains the structure definitions for bitmap headers. You have also been provided a Makefile which you can use for compiling the code. Your code should be compiled to create a program bitmap_tool. The reference program bitmap_tool.org may be used whereever necessary to observe how your program is expected to behave.

Bitmap Images

Bitmap is a format for storing image data. Bitmap files can be of various types, with variations resulting from the number of bytes used for pixel, the compression etc. For this project, we will consider simple bitmap files which use 24 bits (or 3 bytes) per pixel and have no compression. Hence each pixel consists of three bytes :
blue, green, red
each varying from 0 to 255. Bitmap images which use 24 bits per pixel and have no compression are stored in .bmp files which have the following format:

Header

The header looks like the following when represented as a C structure :

struct header {
unsigned short int type;
unsigned int size;
unsigned short int reserved1, reserved2;
unsigned int offset;
};
The type is a magic number which holds the value equivalent to 'BM' and is hence equal to 'M'*256+'B', which is 19778. The size field holds the size of the bitmap file in bytes. reserved1 and reserved2 fields are not used and should be always set to 0. The offset field specifies the offset from the beginning of the bitmap file where the image data (pixels) start. This value is always 54 (40+14) for bitmap images which use 24 bits per pixel.
Note: On some machines this structure may get padded with two bytes to make it aligned, this should be avoided by either making the structure packed or by reading/writing the fields of the structure one by one from/to the bitmap file.

Information Header

The information header looks like the following when represented as a C structure :

struct information{
unsigned int size;
int width,height;
unsigned short int planes;
unsigned short int bits;
unsigned int compression;
unsigned int imagesize;
int xresolution, yresolution;
unsigned int ncolors;
unsigned int importantcolors;
};
The size field holds the size of the information header and should be 40. The width and height fields hold the width and height of the bitmap image which this header describes. The field planes should always be set to 1. The field bits stores the number of bits used per pixel, which is 24. Since no compression is used, the compression field is 0. The imagesize field is the same as the size field in header. The fields xresolution and yresolution store the number of pixels per meter along the x and y axis repectively.

Since in bitmap images using 24 bits per pixel the colors are represented by the b, g, r components of the pixel itself, no extra colors are required, hence the field ncolors is 0 for all 24 bpp(bits per pixel) images. The field importantcolors should also be 0 signifying that all colors are important.

Image Data (Pixels)

The image data is stored in the file as pixels, each 3 bytes long. The first byte represents the value of the blue component, the second byte the green component and the third byte the red component. So the color red for example would be denoted by the value 0,0,255. The pixels in a bitmap are stored starting from the bottom left corner moving bottom to top and left to right (in comparison to the normal convention where you start from the top left corner and move left to right and top to bottom).

bitmap_tool

Creating bitmaps is concerned with declaring the above headers, filling them in with appropriate values (which in our case are for 24 bpp images) and writing them to a .bmp file. Manipulating bitmap images consists of reading the headers and image data from a .bmp file, making changes to the headers and or the image data and writing them back to a .bmp file. For this project you will build a tool called bitmap_tool which performs various actions based on the commands you give it. The bitmap_tool expects the command as the first argument and the arguments to perform the operation specified by the command as the remaining arguments. The commands the corresponding arguments required are described below :
You need to check for faulty arguments that may be given to the program. But you can assume that all the bitmap images provided as input are 24 bpp bitmap images and do not have any errors in their headers.
Note : The bitmap format requires that data be stored in the little endian pattern, but LORE (which runs on a Sparc architecture) uses big endian representation of data, hence whenever you read from a bitmap file, you first need to convert the values to a big endian representation before manipulating them. This can be done by passing the pointer to the variable to convert to the functions lendianReadInt() and lendianReadShort() for converting integer and short integer variables respectively (you will not require anything beyond these for the project). The functions are provided in utils.c and return the corresponding big endian form of the variable. Also whenever you need to write a variable or value that you changed to a bitmap file, you need to convert it to the little endian form, you can do this by using the counterpart functions lendianWriteInt() and lendianWriteShort(). When you are just reading the pixels/headers from one bitmap file and writing them to another bitmap file without changing them, then you can skip the endianness coversion. Pixel components (b, g, r) need not be converted since they are only a byte long.
You have been provided two sample images eg1.bmp and eg2.bmp to test your programs on.

Submit

Before you submit make sure to test your implementation on LORE using the Makefile provided.
Your code must compile using the provided Makefile and run on LORE for you to earn points for this lab.

Type cd .. in project05 and change working directory to the parent directory of project05.

In the parent directory of project05, type turnin -v -c cs240=XXX -p prj5 project05/ to turnin your work. Replace XXX with your section number.

9:30 am - 11:20 am FF930
11:30 am - 1:20 pm FF1130
1:30 pm - 3:20 pm FF130
3:30 pm - 5:20 pm FF330
9:30 am - 11:20 am RR930
11:30 am - 1:20 pm RR1130
3:30 pm - 5:20 pm RR330
11:30 am - 1:20 pm TT1130

Now, you may use the command, turnin -c cs240=XXX -p prj5 -v to verify your submission.

This lab is due on Monday, April 25 by 11:59 pm