-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogram.c
46 lines (39 loc) · 850 Bytes
/
program.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
#include <stdio.h>
#include <stdlib.h>
/*
PGM Header format :
P5
# Created by Imlib
512 512
255
*/
int main() {
// input PGM image file
FILE *fin = fopen("test_images\\feep.pgm", "r");
// output PGM image file
FILE *fout = fopen("outputs\\new_img.pgm", "w");
int c = getc(fin);
unsigned int res = 0;
// Header line count :: generally 4 lines of header in PGM image
int count;
while (c != EOF) {
if(count <= 4) {
fprintf(fout, "%c", c); // header lines printed in document as is.
if(c == '\n') count++;
} else {
res = (unsigned int)(c-48); // following lines converted to pixel values
if(res != -16) {
if(res == -38) {
fprintf(fout, "\n");
} else {
fprintf(fout, "%d ", res);
}
}
}
c = getc(fin);
}
// closing input and output files
fclose(fin);
fclose(fout);
return 0;
}