CS13002 Programming and Data Structures

Section: 5/E, Spring 2006

Assignment 6

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 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 pgm (portable gray map). It uses no compression and stores the pixel values in a human-readable (ASCII) form.

A pgm file starts with the signature P2. (There are image files with signatures P1 through P6. Different signatures refer to different image types and storage formats.) After the 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. As mentioned above, 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.

A 33x33 image in the pgm format is given below.

P2
33 33
255
  0   3   7  11  15  19  23  27  31  35  39  43  47  51  55  59  63
 67  71  75  79  83  87  91  95  99 103 107 111 115 119 123 127
  3   7  11  15  19  23  27  31  35  39  43  47  51  55  59  63  67
 71  75  79  83  87  91  95  99 103 107 111 115 119 123 127 131
  7  11  15  19  23  27  31  35  39  43  47  51  55  59  63  67  71
 75  79  83  87  91  95  99 103 107 111 115 119 123 127 131 135
 11  15  19  23  27  31  35  39  43  47  51  55  59  63  67  71  75
 79  83  87  91  95  99 103 107 111 115 119 123 127 131 135 139
 15  19  23  27  31  35  39  43  47  51  55  59  63  67  71  75  79
 83  87  91  95  99 103 107 111 115 119 123 127 131 135 139 143
 19  23  27  31  35  39  43  47  51  55  59  63  67  71  75  79  83
 87  91  95  99 103 107 111 115 119 123 127 131 135 139 143 147
 23  27  31  35  39  43  47  51  55  59  63  67  71  75  79  83  87
 91  95  99 103 107 111 115 119 123 127 131 135 139 143 147 151
 27  31  35  39  43  47  51  55  59  63  67  71  75  79  83  87  91
 95  99 103 107 111 115 119 123 127 131 135 139 143 147 151 155
 31  35  39  43  47  51  55  59  63  67  71  75  79  83  87  91  95
 99 103 107 111 115 119 123 127 131 135 139 143 147 151 155 159
 35  39  43  47  51  55  59  63  67  71  75  79  83  87  91  95  99
103 107 111 115 119 123 127 131 135 139 143 147 151 155 159 163
 39  43  47  51  55  59  63  67  71  75  79  83  87  91  95  99 103
107 111 115 119 123 127 131 135 139 143 147 151 155 159 163 167
 43  47  51  55  59  63  67  71  75  79  83  87  91  95  99 103 107
111 115 119 123 127 131 135 139 143 147 151 155 159 163 167 171
 47  51  55  59  63  67  71  75  79  83  87  91  95  99 103 107 111
115 119 123 127 131 135 139 143 147 151 155 159 163 167 171 175
 51  55  59  63  67  71  75  79  83  87  91  95  99 103 107 111 115
119 123 127 131 135 139 143 147 151 155 159 163 167 171 175 179
 55  59  63  67  71  75  79  83  87  91  95  99 103 107 111 115 119
123 127 131 135 139 143 147 151 155 159 163 167 171 175 179 183
 59  63  67  71  75  79  83  87  91  95  99 103 107 111 115 119 123
127 131 135 139 143 147 151 155 159 163 167 171 175 179 183 187
 63  67  71  75  79  83  87  91  95  99 103 107 111 115 119 123 127
131 135 139 143 147 151 155 159 163 167 171 175 179 183 187 191
 67  71  75  79  83  87  91  95  99 103 107 111 115 119 123 127 131
135 139 143 147 151 155 159 163 167 171 175 179 183 187 191 195
 71  75  79  83  87  91  95  99 103 107 111 115 119 123 127 131 135
139 143 147 151 155 159 163 167 171 175 179 183 187 191 195 199
 75  79  83  87  91  95  99 103 107 111 115 119 123 127 131 135 139
143 147 151 155 159 163 167 171 175 179 183 187 191 195 199 203
 79  83  87  91  95  99 103 107 111 115 119 123 127 131 135 139 143
147 151 155 159 163 167 171 175 179 183 187 191 195 199 203 207
 83  87  91  95  99 103 107 111 115 119 123 127 131 135 139 143 147
151 155 159 163 167 171 175 179 183 187 191 195 199 203 207 211
 87  91  95  99 103 107 111 115 119 123 127 131 135 139 143 147 151
155 159 163 167 171 175 179 183 187 191 195 199 203 207 211 215
 91  95  99 103 107 111 115 119 123 127 131 135 139 143 147 151 155
159 163 167 171 175 179 183 187 191 195 199 203 207 211 215 219
 95  99 103 107 111 115 119 123 127 131 135 139 143 147 151 155 159
163 167 171 175 179 183 187 191 195 199 203 207 211 215 219 223
 99 103 107 111 115 119 123 127 131 135 139 143 147 151 155 159 163
167 171 175 179 183 187 191 195 199 203 207 211 215 219 223 227
103 107 111 115 119 123 127 131 135 139 143 147 151 155 159 163 167
171 175 179 183 187 191 195 199 203 207 211 215 219 223 227 231
107 111 115 119 123 127 131 135 139 143 147 151 155 159 163 167 171
175 179 183 187 191 195 199 203 207 211 215 219 223 227 231 235
111 115 119 123 127 131 135 139 143 147 151 155 159 163 167 171 175
179 183 187 191 195 199 203 207 211 215 219 223 227 231 235 239
115 119 123 127 131 135 139 143 147 151 155 159 163 167 171 175 179
183 187 191 195 199 203 207 211 215 219 223 227 231 235 239 243
119 123 127 131 135 139 143 147 151 155 159 163 167 171 175 179 183
187 191 195 199 203 207 211 215 219 223 227 231 235 239 243 247
123 127 131 135 139 143 147 151 155 159 163 167 171 175 179 183 187
191 195 199 203 207 211 215 219 223 227 231 235 239 243 247 251
127 131 135 139 143 147 151 155 159 163 167 171 175 179 183 187 191
195 199 203 207 211 215 219 223 227 231 235 239 243 247 251 255

Click here to view the image.

You are given a pgm image. Save the image file in your PC. Write a C program that reads the image file and stores the pixel values in a two-dimensional array of integers. Let the size of the image be w x h.

Your next task is to halve the size of the image, i.e., to produce an image of size (w/2) x (h/2). In order to perform this, you take a 2x2 window. For computing the (i,j)-th pixel of the output image, place the window on the input image so as to cover the pixels (2i,2j), (2i,2j+1), (2i+1,2j) and (2i+1,2j+1). Read these four pixel values from the input array, compute their average and set the (i,j)-th pixel of the output image to this average.

Finally, write the scaled down image to a new image file in the pgm format. Submit your C source file and the output pgm file.

Click here to know how to do file I/O.

Some image viewers are: gimp, ee, kview and xv.

Note: The submission server does not support more than 70,000 bytes of data per assignment. The halved image corresponding to the given file is too big to fit this limit. So you halve the image twice and submit the doubly halved image.

Sample session

$./a.out
Enter the name of the input file  : src.pgm
Enter the name of the output file : intermediate.pgm
462 x 496 image read from "src.pgm".
231 x 248 image saved as "intermediate.pgm".
$./a.out
Enter the name of the input file  : intermediate.pgm
Enter the name of the output file : dest.pgm
231 x 248 image read from "intermediate.pgm".
115 x 124 image saved as "dest.pgm".


[Submission site] [Lab home] [Course home]