Appendix F - pgm.c
1:  #include < stdio.h >
2:  #include < stdlib.h >
3:  #include "pgm.h"
4:  
5:  /**********************File I/O functions*******************************/
6:  /***********************************************************************/
7:  
8:  /*Gets an ascii pgm image file, store as a color pgm.*/
9:  void getPGMfile (char filename[], PGMImage *img)
10:  {
11:    FILE *in_file;
12:    char ch;
13:    int row, col, type;
14:    int ch_int;
15:  
16:    in_file = fopen(filename, "r");
17:    if (in_file == NULL)
18:    {
19:      fprintf(stderr, "Error: Unable to open file %s\n\n", filename);
20:      exit(8);
21:    }
22:  
23:    printf("\nReading image file: %s\n", filename);
24:    
25:    /*determine pgm image type (only type three can be used)*/
26:    ch = getc(in_file);
27:    if(ch != 'P')
28:    {
29:       printf("ERROR(1): Not valid pgm/ppm file type\n");
30:       exit(1);
31:    }
32:    ch = getc(in_file);
33:    /*convert the one digit integer currently represented as a character to
34:      an integer(48 == '0')*/
35:    type = ch - 48;
36:    if((type != 2) && (type != 3) && (type != 5) && (type != 6))
37:    {
38:       printf("ERROR(2): Not valid pgm/ppm file type\n");
39:       exit(1);
40:    }
41:  
42:    while(getc(in_file) != '\n');             /* skip to end of line*/
43:  
44:    while (getc(in_file) == '#')              /* skip comment lines */
45:    {
46:      while (getc(in_file) != '\n');          /* skip to end of comment line */
47:    }
48:  
49:    /*there seems to be a difference between color and b/w.  This line is needed
50:      by b/w but doesn't effect color reading...*/
51:    fseek(in_file, -1, SEEK_CUR);             /* backup one character*/
52:  
53:    fscanf(in_file,"%d", &((*img).width));
54:    fscanf(in_file,"%d", &((*img).height));
55:    fscanf(in_file,"%d", &((*img).maxVal));
56:  
57:    printf("\n width  = %d",(*img).width);
58:    printf("\n height = %d",(*img).height);
59:    printf("\n maxVal = %d",(*img).maxVal);
60:    printf("\n");
61:   
62:    if (((*img).width  > MAX) || ((*img).height  > MAX))
63:    {
64:      printf("\n\n***ERROR - image too big for current image structure***\n\n");
65:      exit(1);
66:    }
67:  
68:    if(type == 2) /*uncompressed ascii file (B/W)*/
69:    {
70:       for (row=(*img).height-1; row >=0; row--)
71:         for (col=0; col< (*img).width; col++)
72:         {
73:            fscanf(in_file,"%d", &ch_int);
74:            (*img).data[row][col].red = ch_int;
75:            (*img).data[row][col].green = ch_int;
76:            (*img).data[row][col].blue = ch_int;
77:         }
78:    }
79:    else if(type == 3) /*uncompressed ascii file (color)*/
80:    {
81:       for (row=(*img).height-1; row >=0; row--)
82:         for (col=0; col< (*img).width; col++)
83:         {
84:  	  
85:  	  fscanf(in_file,"%d", &ch_int);
86:  	  ((*img).data[row][col].red) = (unsigned char)ch_int;
87:       
88:  	  fscanf(in_file,"%d", &ch_int);
89:  	  ((*img).data[row][col].green) = (unsigned char)ch_int;
90:  	  
91:  	  fscanf(in_file,"%d", &ch_int);
92:  	  ((*img).data[row][col].blue) = (unsigned char)ch_int;
93:         }
94:    }
95:    else if(type == 5) /*compressed file (B/W)*/
96:  /*note: this type remains untested at this time...*/
97:    {
98:       while(getc(in_file) != '\n'); /*skip to end of line*/
99:  
100:        for (row=(*img).height-1; row >=0; row--)
101:         for (col=0; col< (*img).width; col++)
102:         {
103:  	  ch = getc(in_file);
104:            (*img).data[row][col].red = ch;
105:            (*img).data[row][col].green = ch;
106:            (*img).data[row][col].blue = ch;
107:         }
108:    }
109:  
110:    else if(type == 6) /*compressed file (color)*/
111:    {
112:       while(getc(in_file) != '\n'); /*skip to end of line*/
113:  
114:       for (row=(*img).height-1; row >=0; row--)
115:         for (col=0; col< (*img).width; col++)
116:         {
117:            (*img).data[row][col].red = getc(in_file);
118:            (*img).data[row][col].green = getc(in_file);
119:            (*img).data[row][col].blue = getc(in_file);
120:         }
121:    }
122:  
123:    fclose(in_file);
124:    printf("\nDone reading file.\n");
125:  }
126:  
127:  
128:  void save(PGMImage *img)
129:  {
130:     int i, j, nr, nc, k;
131:     int red, green, blue;
132:     FILE *iop;
133:  
134:     nr = img- >height;
135:     nc = img- >width;
136:      
137:     iop = fopen("image1.pgm", "w");
138:     fprintf(iop, "P6\n");
139:     fprintf(iop, "%d %d\n", nc, nr);
140:     fprintf(iop, "255\n");
141:      
142:     k = 1;
143:     for(i = nr - 1; i  >= 0; i--)
144:     {
145:        for(j = 0; j <  nc; j++)
146:        {
147:           red = img- >data[i][j].red;
148:           green = img- >data[i][j].green;
149:           blue = img- >data[i][j].blue;
150:           if(red <  0)
151:           {
152:              printf("IMG_WRITE: Found value %d at row %d col %d\n", red, i, j);
153:              printf("           Setting red to zero\n");
154:              red = 0;
155:           }
156:           if(green <  0)   
157:           {
158:              printf("IMG_WRITE: Found value %d at row %d col %d\n", green,i, j);
159:              printf("           Setting green to zero\n");
160:              green = 0;
161:           }
162:           if(blue <  0)   
163:           {
164:              printf("IMG_WRITE: Found value %d at row %d col %d\n", blue, i, j);
165:              printf("           Setting green to zero\n");
166:              blue = 0;
167:           }
168:           if(red  > 255) 
169:           {   
170:              printf("IMG_WRITE: Found value %d at row %d col %d\n", red, i, j);
171:              printf("           Setting red to 255\n");
172:              red = 255;
173:           }
174:           if(green  > 255)
175:           {
176:              printf("IMG_WRITE: Found value %d at row %d col %d\n", green,i, j);
177:              printf("           Setting green to 255\n");
178:              green = 255;
179:           }
180:           if(blue  > 255)
181:           {
182:              printf("IMG_WRITE: Found value %d at row %d col %d\n", blue, i, j);
183:              printf("           Setting blue to 255\n");
184:              blue = 255;
185:           }
186:  
187:  	 putc(red, iop);
188:  	 putc(green, iop);
189:  	 putc(blue, iop);
190:        }
191:     }
192:     fprintf(iop, "\n");
193:     fclose(iop);
194:  }