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 :
- "-create"
Creates a new bitmap image, expects the height, width and the ouput file name as the arguments following the command. Example :
bitmap_tool -create 800 1000 new_image.bmp
will create a new bitmap image new_image.bmp of dimensions 1000x800.
- "-invertcolor"
Inverts the color of a given bitmap image, expects the input image file name and the output file name as arguments following the command. Inverting the color is equivalent to subtracting each component of each pixel from 255. Example:
bitmap_tool -invertcolor my_image.bmp output.bmp
will create a new bitmap image output.bmp which contains the same image as my_image.bmp but with the colors inverted.
- "-stackvertically"
Combines two bitmap images height wise to generate a new bitmap image. Expects the two input image file names and the ouput file name as arguments following the command. The first image specified should be on top and the second image of the bottom, you should also make sure that the images are of the same dimensions. Example:
bitmap_tool -stackvertically image1.bmp image2.bmp output.bmp
If image1.bmp and image2.bmp are both images of height A and width B, then output.bmp is of height 2*A and width B, where image1.bmp occupies the top half and image2.bmp the bottom half
- "-stackhorizontally"
same as -stackvertically , the images are now added side by side to create a new image which is twice in width. The first image specified occupies the left half of the output image and the second image the right half. Again you need to check if the images are of the same dimensions.
- "-drawborder"
Draw a border of given thickness and color for the given bitmap image. Expects the input file name, the thickness of the border in pixels, the blue component of the border color, the green component of the border color, the red component of the border color, ouput file name as arguments following the command. Example :
bitmap_tool -drawborder image.bmp 4 255 0 0 output.bmp
will draw a border of thickness 4 pixels of color blue for the image image.bmp and save the resulting image in output.bmp
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 F | F930 |
11:30 am - 1:20 pm F | F1130 |
1:30 pm - 3:20 pm F | F130 |
3:30 pm - 5:20 pm F | F330 |
9:30 am - 11:20 am R | R930 |
11:30 am - 1:20 pm R | R1130 |
3:30 pm - 5:20 pm R | R330 |
11:30 am - 1:20 pm T | T1130 |
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