-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPGM.c
430 lines (373 loc) · 12.4 KB
/
PGM.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "PGM.h"
int read_pgm_image_dimensions(char* infilename, int* rows, int* cols)
{
FILE* fp;
//// Open the input image file for reading if a filename was given. If no
//// filename was provided, set fp to read from standard input.
if (infilename == NULL)
{
fp = stdin;
}
else
{
if ((fp = fopen(infilename, "rb")) == NULL)
{
char tmp[1024];
snprintf(tmp, 1024, "Error reading the file %s in read_pgm_image().\n", infilename);
fputs(tmp, stderr);
return(0);
}
}
// general case
char imgtype = (char)getc(fp);
char bandcode[2];
char type = '0';
bandcode[0] = (char)getc(fp);
bandcode[1] = '\0';
int intType = atoi(bandcode);
assert(intType < 256);
type = (char)intType;
char eol = (char)getc(fp);
if (eol != '\n' && eol != ' ' && eol != '\r')
{
fprintf(stderr, "read_image: bad header format.\r\n");
if (fp != stdin) fclose(fp);
return 0;
}
if (imgtype != 'P' || type != 5)
{
fprintf(stderr, "this format is not supported.\r\n");
if (fp != stdin) fclose(fp);
return(0);
}
// read comment block
while (getc(fp) == '#') while (getc(fp) != '\n');
fseek(fp, -1, SEEK_CUR);
// read columns and rows, and max value
int max;
int read = fscanf(fp, "%d %d %d", cols, rows, &max);
if(read == 2)
{
read = fscanf(fp, "%d", &max); //sometimes the max is on the next line
}
else if(read == 3)
{
//already got max
}
else
{
fprintf(stderr, "failed reading image dimensions.\r\n");
if (fp != stdin) fclose(fp);
return(0);
}
if (fp != stdin) fclose(fp);
return(1);
}
/******************************************************************************
* Function: read_pgm_image_rect
* Reads a portion of an image in PGM format. The image is read from standard input when
* infilename = NULL. Image dimensions read from the header. Comments are
* skipped. Memory to store the image is allocated by the caller.
* On failure returns 0 else returns 1.
******************************************************************************/
int read_pgm_image_rect(char* infilename, unsigned char* image, int startRow, int startCol, int rectNumRows, int rectNumCols)
{
FILE* fp;
//// Open the input image file for reading if a filename was given. If no
//// filename was provided, set fp to read from standard input.
if (infilename == NULL)
{
fp = stdin;
}
else
{
if ((fp = fopen(infilename, "rb")) == NULL)
{
char tmp[1024];
snprintf(tmp, 1024, "Error reading the file %s in read_pgm_image().\n", infilename);
fputs(tmp, stderr);
return(0);
}
}
// general case
char imgtype = (char)getc(fp);
char bandcode[2];
char type = '0';
bandcode[0] = (char)getc(fp);
bandcode[1] = '\0';
int intType = atoi(bandcode);
assert(intType < 256);
type = (char)intType;
char eol = (char)getc(fp);
if (eol != '\n' && eol != ' ' && eol != '\r')
{
fprintf(stderr, "read_image: bad header format.\r\n");
if (fp != stdin) fclose(fp);
return 0;
}
if (imgtype != 'P' || type != 5)
{
fprintf(stderr, "this format is not supported.\r\n");
if (fp != stdin) fclose(fp);
return(0);
}
// read comment block
while (getc(fp) == '#') while (getc(fp) != '\n');
fseek(fp, -1, SEEK_CUR);
// read columns and rows, and max value
int max;
int wholeImageCols;
int wholeImageRows;
size_t read = fscanf(fp, "%d %d %d", &wholeImageCols, &wholeImageRows, &max);
if (3 != read && 2 != read) //sometimes the max is on the next line
{
fprintf(stderr, "failed reading image dimensions.\r\n");
if (fp != stdin) fclose(fp);
return(0);
}
// Consume the final newline
fgetc(fp);
//validate request
if (startRow < 0 || startCol < 0 ||
rectNumRows <= 0 || (startRow + rectNumRows) > wholeImageRows ||
rectNumCols <= 0 || (startCol + rectNumCols) > wholeImageCols)
{
fprintf(stderr, "Invalid rectangle requested in read_pgm_image().\n");
if (fp != stdin) fclose(fp);
return(0);
}
// read comment block
while (getc(fp) == '#') while (getc(fp) != '\n');
fseek(fp, -1, SEEK_CUR);
int bytesPerPixel = 1; //8bit
if (max > (1 << 16) - 1)
{
char tmp[1024];
snprintf(tmp, 1024, "Greater than 16 bit not supported yet for %s.\n", infilename);
fputs(tmp, stderr);
return(0);
}
else if (max > (1 << 8) - 1)
{
//16 bit
bytesPerPixel = 2;
}
//read each row of data into the rect
long startImageData = ftell(fp);
for (int idxRow = 0; idxRow < rectNumRows; idxRow++)
{
int idxWholeImageRow = startRow + idxRow;
int idxWholeImagePixel = idxWholeImageRow * wholeImageCols + startCol;
int idxRectPixel = idxRow * rectNumCols;
if (0 != fseek(fp, startImageData + sizeof(unsigned char) * idxWholeImagePixel, SEEK_SET))
{
fprintf(stderr, "Error seeking through file in read_pgm_image().\n");
if (fp != stdin) fclose(fp);
return(0);
}
read = fread(&(image[idxRectPixel]), bytesPerPixel, rectNumCols, fp);
if (read != (size_t)rectNumCols* bytesPerPixel)
{
fprintf(stderr, "Error reading image data in read_pgm_image().\n");
if (fp != stdin) fclose(fp);
return(0);
}
}
if (fp != stdin) fclose(fp);
return(1);
}
/******************************************************************************
* Function: read_pgm_image
* Reads in an image in PGM format. The image is read from standard input when
* infilename = NULL. Image dimensions read from the header. Comments are
* skipped. Memory to store the image is allocated in this function.
* On failure returns 0 else returns 1.
******************************************************************************/
int read_pgm_image(char* infilename, unsigned char** image, int* rows, int* cols)
{
FILE* fp;
//// Open the input image file for reading if a filename was given. If no
//// filename was provided, set fp to read from standard input.
if (infilename == NULL)
{
fp = stdin;
}
else
{
if ((fp = fopen(infilename, "rb")) == NULL)
{
char tmp[1024];
snprintf(tmp, 1024, "Error reading the file %s in read_pgm_image().\n", infilename);
fputs(tmp, stderr);
return(0);
}
}
//// Verify that the image is in PGM format, read in the number of columns
//// and rows in the image and scan past all of the header information.
#if 0
// original
fgets(buf, 70, fp);
char tmp[1024];
snprintf(tmp, 1024, "firstLine=%s\n", buf);
fputs(tmp, stdout);
if (strncmp(buf, "P5", 2) != 0)
{
snprintf(tmp, 1024, "The file %s is not in PGM format in ", infilename);
fputs(tmp, stderr);
fprintf(stderr, "read_pgm_image().\n");
if (fp != stdin) fclose(fp);
return(0);
}
do { fgets(buf, 70, fp); } while (buf[0] == '#'); /* skip all comment lines */
sscanf(buf, "%d %d", cols, rows);
do { fgets(buf, 70, fp); } while (buf[0] == '#'); /* skip all comment lines */
#else
// general case
char imgtype = (char)getc(fp);
char bandcode[2];
char type = '0';
bandcode[0] = (char)getc(fp);
bandcode[1] = '\0';
int intType = atoi(bandcode);
assert(intType < 256);
type = (char)intType;
char eol = (char)getc(fp);
if (eol != '\n' && eol != ' ' && eol != '\r')
{
fprintf(stderr, "read_image: bad header format.\r\n");
if (fp != stdin) fclose(fp);
return 0;
}
if (imgtype != 'P' || type != 5)
{
fprintf(stderr, "this format is not supported.\r\n");
if (fp != stdin) fclose(fp);
return(0);
}
// read comment block
while (getc(fp) == '#') while (getc(fp) != '\n');
fseek(fp, -1, SEEK_CUR);
// read columns and rows, and max value
int max;
int read = fscanf(fp, "%d %d %d", cols, rows, &max);
if (3 != read && 2 != read) //sometimes the max is on the next line
{
fprintf(stderr, "failed reading image dimensions.\r\n");
if (fp != stdin) fclose(fp);
return(0);
}
// Consume the final newline
fgetc(fp);
// read comment block
while (getc(fp) == '#') while (getc(fp) != '\n');
fseek(fp, -1, SEEK_CUR);
#endif
//// Allocate memory to store the image then read the image from the file.
if (((*image) = (unsigned char*)malloc((*rows) * (*cols))) == NULL)
{
fprintf(stderr, "Memory allocation failure in read_pgm_image().\n");
if (fp != stdin) fclose(fp);
return(0);
}
if ((size_t)(*rows) != fread((*image), (*cols), (*rows), fp))
{
fprintf(stderr, "Error reading the image data in read_pgm_image().\n");
if (fp != stdin) fclose(fp);
free((*image));
return(0);
}
if (fp != stdin) fclose(fp);
return(1);
}
/******************************************************************************
* Function: write_pgm_image
* Writes an image in PGM format. The file is either
* written to the file specified by outfilename or to standard output if
* outfilename = NULL.
******************************************************************************/
int write_pgm_image(char* outfilename, unsigned char* image, int rows,
int cols, int maxval)
{
FILE* fp;
//// Open the output image file for writing if a filename was given. If no
//// filename was provided, set fp to write to standard output.
if (outfilename == NULL) fp = stdout;
else
{
if ((fp = fopen(outfilename, "wb")) == NULL)
{
char tmp[1024];
snprintf(tmp, 1024, "Error writing the file %s in write_pgm_image().\n", outfilename);
fputs(tmp, stderr);
return(0);
}
}
//// Write the header information to the PGM file.
fprintf(fp, "P5\n%d %d\n", cols, rows);
//if (comment != NULL)
// if (strlen(comment) <= 70) fprintf(fp, "# %s\n", comment);
fprintf(fp, "%d\n", maxval);
//// Write the image data to the file.
int bytesPerPixel = 1; //8bit
if (maxval > (1 << 16) - 1)
{
char tmp[1024];
snprintf(tmp, 1024, "Greater than 16 bit not supported yet for %s.\n", outfilename);
fputs(tmp, stderr);
return(0);
}
else if (maxval > (1 << 8) - 1)
{
//16 bit
bytesPerPixel = 2;
}
if (rows != fwrite(image, cols * bytesPerPixel, rows, fp))
{
fprintf(stderr, "Error writing the image data in write_pgm_image().\n");
if (fp != stdout) fclose(fp);
return(0);
}
if (fp != stdout) fclose(fp);
return(1);
}
/******************************************************************************
* Function: write_ppm_image
* Writes an image in 3 channel color PPM format. The file is either
* written to the file specified by outfilename or to standard output if
* outfilename = NULL.
******************************************************************************/
int write_ppm_image(char* outfilename, unsigned char* image, int rows,
int cols)
{
FILE* fp;
//// Open the output image file for writing if a filename was given. If no
//// filename was provided, set fp to write to standard output.
if (outfilename == NULL) fp = stdout;
else
{
if ((fp = fopen(outfilename, "wb")) == NULL)
{
char tmp[1024];
snprintf(tmp, 1024, "Error writing the file %s in write_ppm_image().\n", outfilename);
fputs(tmp, stderr);
return(0);
}
}
//// Write the header information to the PPM file.
fprintf(fp, "P6\n%d %d\n", cols, rows);
//if (comment != NULL)
// if (strlen(comment) <= 70) fprintf(fp, "# %s\n", comment);
fprintf(fp, "%d\n", 255);
//// Write the image data to the file.
if (rows != fwrite(image, cols * 3, rows, fp))
{
fprintf(stderr, "Error writing the image data in write_ppm_image().\n");
if (fp != stdout) fclose(fp);
return(0);
}
if (fp != stdout) fclose(fp);
return(1);
}