CS19002 Programming and Data Structures Laboratory | Spring 2009, Section 2 |
A popular format to represent images is by a two-dimensional array of pixel values. Each pixel is an instance of color (a dot on the screen). A black/white pixel is represented by the value 0 or 1. A gray-scale pixel is typically an integer value between 0 and 255. Finally, a colored pixel is a triple (r,g,b) of three integer values r, g and b each in the range 0 to 255. In this assignment, we plan to work with gray-scale and color images.
Image files are stored in several formats (like gif, jpeg, bmp). These formats differ in various aspects including the maximum number of allowed colors, the type of compression used, and so on. Here, we work with the less common format known as pnm (portable any map). It uses no compression and stores the pixel values in a human-readable (ASCII) form.
A pnm file starts with one of the signatures P1 through P6. P1 means black-and-white images, P2 means gray-scale images, and P3 means color images. In this assignment, we will not deal with pnm files with signatures P4, P5 and P6. After this signature, come the dimensions of the image: first the horizontal dimension (width) and then the vertical dimension (height). The next item is a maximum level which you can assume to be 255 for this assignment. Then follows a listing of the pixel values of the image in the row-major order.
For a gray-scale image, each pixel value is an integer between 0 and the maximum level (255): 0 means black, 255 means white, and the intermediate values stand for different shades of gray.
For a color image, each pixel is a triple of integers each in the range 0 to 255. For example, 0,0,0 means black, 255,255,255 means white, 255,0,0 means red, 0,255,255 means cyan, 165,42,42 means brown, and so on.
A 16 x 16 gray-scale image in the pnm format is given below.
P2 16 16 255 0 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128 136 24 32 40 48 56 64 72 80 88 96 104 112 120 128 136 144 32 40 48 56 64 72 80 88 96 104 112 120 128 136 144 152 40 48 56 64 72 80 88 96 104 112 120 128 136 144 152 160 48 56 64 72 80 88 96 104 112 120 128 136 144 152 160 168 56 64 72 80 88 96 104 112 120 128 136 144 152 160 168 176 64 72 80 88 96 104 112 120 128 136 144 152 160 168 176 184 72 80 88 96 104 112 120 128 136 144 152 160 168 176 184 192 80 88 96 104 112 120 128 136 144 152 160 168 176 184 192 200 88 96 104 112 120 128 136 144 152 160 168 176 184 192 200 208 96 104 112 120 128 136 144 152 160 168 176 184 192 200 208 216 104 112 120 128 136 144 152 160 168 176 184 192 200 208 216 224 112 120 128 136 144 152 160 168 176 184 192 200 208 216 224 232 120 128 136 144 152 160 168 176 184 192 200 208 216 224 232 240
A 16 x 16 color image in the pnm format is shown below.
P3 16 16 255 0 0 0 16 0 0 32 0 0 48 0 0 64 0 0 80 0 0 96 0 0 112 0 0 128 0 0 144 0 0 160 0 0 176 0 0 192 0 0 208 0 0 224 0 0 240 0 0 0 16 0 16 16 0 32 16 0 48 16 0 64 16 0 80 16 0 96 16 0 112 16 0 128 16 0 144 16 0 160 16 0 176 16 0 192 16 0 208 16 0 224 16 0 240 16 0 0 32 0 16 32 0 32 32 0 48 32 0 64 32 0 80 32 0 96 32 0 112 32 0 128 32 0 144 32 0 160 32 0 176 32 0 192 32 0 208 32 0 224 32 0 240 32 0 0 48 0 16 48 0 32 48 0 48 48 0 64 48 0 80 48 0 96 48 0 112 48 0 128 48 0 144 48 0 160 48 0 176 48 0 192 48 0 208 48 0 224 48 0 240 48 0 0 64 0 16 64 0 32 64 0 48 64 0 64 64 0 80 64 0 96 64 0 112 64 0 128 64 0 144 64 0 160 64 0 176 64 0 192 64 0 208 64 0 224 64 0 240 64 0 0 80 0 16 80 0 32 80 0 48 80 0 64 80 0 80 80 0 96 80 0 112 80 0 128 80 0 144 80 0 160 80 0 176 80 0 192 80 0 208 80 0 224 80 0 240 80 0 0 96 0 16 96 0 32 96 0 48 96 0 64 96 0 80 96 0 96 96 0 112 96 0 128 96 0 144 96 0 160 96 0 176 96 0 192 96 0 208 96 0 224 96 0 240 96 0 0 112 0 16 112 0 32 112 0 48 112 0 64 112 0 80 112 0 96 112 0 112 112 0 128 112 0 144 112 0 160 112 0 176 112 0 192 112 0 208 112 0 224 112 0 240 112 0 0 128 0 16 128 0 32 128 0 48 128 0 64 128 0 80 128 0 96 128 0 112 128 0 128 128 0 144 128 0 160 128 0 176 128 0 192 128 0 208 128 0 224 128 0 240 128 0 0 144 0 16 144 0 32 144 0 48 144 0 64 144 0 80 144 0 96 144 0 112 144 0 128 144 0 144 144 0 160 144 0 176 144 0 192 144 0 208 144 0 224 144 0 240 144 0 0 160 0 16 160 0 32 160 0 48 160 0 64 160 0 80 160 0 96 160 0 112 160 0 128 160 0 144 160 0 160 160 0 176 160 0 192 160 0 208 160 0 224 160 0 240 160 0 0 176 0 16 176 0 32 176 0 48 176 0 64 176 0 80 176 0 96 176 0 112 176 0 128 176 0 144 176 0 160 176 0 176 176 0 192 176 0 208 176 0 224 176 0 240 176 0 0 192 0 16 192 0 32 192 0 48 192 0 64 192 0 80 192 0 96 192 0 112 192 0 128 192 0 144 192 0 160 192 0 176 192 0 192 192 0 208 192 0 224 192 0 240 192 0 0 208 0 16 208 0 32 208 0 48 208 0 64 208 0 80 208 0 96 208 0 112 208 0 128 208 0 144 208 0 160 208 0 176 208 0 192 208 0 208 208 0 224 208 0 240 208 0 0 224 0 16 224 0 32 224 0 48 224 0 64 224 0 80 224 0 96 224 0 112 224 0 128 224 0 144 224 0 160 224 0 176 224 0 192 224 0 208 224 0 224 224 0 240 224 0 0 240 0 16 240 0 32 240 0 48 240 0 64 240 0 80 240 0 96 240 0 112 240 0 128 240 0 144 240 0 160 240 0 176 240 0 192 240 0 208 240 0 224 240 0 240 240 0
In this assignment, you are supposed to read a pnm image from a file, blur the image, and store the blurred image in the pnm format in another file.
Part 1 [Credit: 70%]
First, write a function to read a pnm file in the P2 format and store the pixel values in a two-dimensional array of integers. The height and width of the images should also be read from the pnm file and stored in global variables (or in variables passed via pointers).
Then, write a function that, given a two-dimensional array of pixel values, a height and a width, saves the corresponding image in the pnm fromat (P2) in a file.
Finally, write a function that takes two two-dimensional arrays A and B as input. The array A stores the pixels of an image (the height and width of which may be either read from global variables or passed as parameters to the function). The function then applies a blurring algorithm on the input image and stores the blurred image in the array B.
I now describe a blurring algorithm. It uses a (2k+1) x (2k+1) mask for some positive integer k. In order to determine the value of the output pixel B[i][j], we look at the (2k+1) x (2k+1) subarray of the input image A with the (i,j)-th pixel at the center. All these (2k+1)2 pixel values are averaged, and this average is used to set the value of B[i][j]. If the (i,j)-th pixel is near an edge of the image, so that the entire (2k+1) x (2k+1) mask does not reside inside the image, then only the pixel values that are inside the image are used to compute the average. The larger the value of k is, the higher is the blurring effect.
Part 2 [Credit: 30%]
Write a function to read a pnm image in the P3 format and store the three components in three two-dimensional arrays.
Write a function to write a pnm image in the P3 format, given three two-dimensional arrays.
Write a function to apply the above blurring algorithm on color images. Use the identical blurring transform on each of the three color components (R, G and B) individually.
What to submit
You need to report the output of your program on the following pnm files.
Combine your C source (say, with the name a6.c) and the two output images in a single compressed archive using the following command:
tar cvzf a6.tgz a6.c light-blurred.pnm duck-blurred.pnm
Submit the file a6.tgz.
Some relevant links