-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_pgm.c
371 lines (309 loc) · 10.9 KB
/
io_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
/*----------------------------------------------------------------------------
The followoing functions for reading/writing PGM image files are based on
those of LSD - Line Segment Detector on digital images
Copyright 2007-2010 rafael grompone von gioi ([email protected])
*/
/*----------------------------------------------------------------------------
LSD - Line Segment Detector on digital images
Copyright 2007-2010 rafael grompone von gioi ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include "image.h"
#include "io_pgm.h"
#ifndef FALSE
#define FALSE 0
#endif /* !FALSE */
#ifndef TRUE
#define TRUE 1
#endif /* !TRUE */
/*----------------------------------------------------------------------------*/
/** Fatal error, print a message and corresponding filename to standard-error
* output and exit.
*/
static void errorf(const char * msg, const char *name)
{
fprintf(stderr,"%s %s.\n",msg,name);
exit(EXIT_FAILURE);
}
/*----------------------------------------------------------------------------*/
/*------------------------------ PGM image I/O -------------------------------*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/** Skip white characters and comments in a PGM file.
*/
static void skip_whites_and_comments(FILE * f)
{
int c;
do
{
while(isspace(c=getc(f))); /* skip spaces */
if(c=='#') /* skip comments */
while( c!='\n' && c!='\r' && c!=EOF )
c=getc(f);
}
while( c == '#' || isspace(c) );
if( c != EOF && ungetc(c,f) == EOF )
error("Error: unable to 'ungetc' while reading PGM file.");
}
/*----------------------------------------------------------------------------*/
/** Read a ASCII number from a PGM file.
*/
static unsigned int get_num(FILE * f)
{
unsigned int num;
int c;
while(isspace(c=getc(f)));
if(!isdigit(c)) error("Error: corrupted PGM file.");
num = (unsigned int) (c - '0');
while( isdigit(c=getc(f)) ) num = 10 * num + c - '0';
if( c != EOF && ungetc(c,f) == EOF )
error("Error: unable to 'ungetc' while reading PGM file.");
return num;
}
/*---------------------------------------------------------------------------*/
/** Read a PGM 8/16 Bits image file into an array of floats.
*/
float *read_pgm_float(const char * fname, int * ncol, int *nrow)
{
FILE * f;
int c,bin;
int depth,i,j;
float * data;
/* open file */
f = fopen(fname,"rb");
if( f == NULL ) errorf("Error: unable to open input image file ",fname);
/* read header */
if( getc(f) != 'P' ) errorf("Error: not a PGM file ",fname);
if( (c=getc(f)) == '2' ) bin = FALSE;
else if( c == '5' ) bin = TRUE;
else errorf("Error: not a PGM file ",fname);
skip_whites_and_comments(f);
*ncol = get_num(f); /* X size */
skip_whites_and_comments(f);
*nrow = get_num(f); /* Y size */
skip_whites_and_comments(f);
depth = get_num(f); /* depth */
if(depth==0) fprintf(stderr,
"Warning: depth=0, probably invalid PGM file\n");
/* white before data */
if(!isspace(c=getc(f))) errorf("Error: corrupted PGM file ",fname);
/* get memory */
data = (float *) calloc((*ncol) * (*nrow), sizeof(float));
if (data == NULL)
error("not enough memory.");
/* read data */
/*If the depth is less than 256, it is 1 byte. Otherwise, it is 2 bytes*/
if(depth<256)
{
for(i=0;i<*nrow;i++)
for(j=0;j<*ncol;j++)
data[ j + i * (*ncol)] = bin ?
(float) getc(f) : (float) get_num(f);
}
/*16 bits PGM Most significant byte first
* see http://netpbm.sourceforge.net/doc/pgm.html
*/
else {
for(i=0;i<*nrow;i++)
for(j=0;j<*ncol;j++)
/*most significant byte first*/
data[ j + i * (*ncol) ] = bin ? ((float) getc(f)*256) +
(float)getc(f) : (float) get_num(f);
}
/* close file if needed */
if( f != stdin && fclose(f) == EOF )
errorf("Error: unable to close file while reading PGM file ",fname);
return data;
}
/*---------------------------------------------------------------------------*/
/** Write an array of floats into a PGM file.
*/
void write_pgm_float(const char *fname, const float *data, int ncol, int nrow)
{
FILE * f;
int i,j;
int v,max,min;
/* check min and max values */
max = min = 0;
for(i=0; i< nrow; i++)
for(j=0; j<ncol; j++)
{
v = (int) data[ j + i * ncol];
if( v > max ) max = v;
if( v < min ) min = v;
}
if( min < 0 ) fprintf(stderr,
"Warning: negative values in '%s'.\n",
fname);
if( max > 255 ) fprintf(stderr,
"Warning: values exceeding 255 in '%s'.\n",
fname);
/* open file */
if( strcmp(fname,"-") == 0 ) f = stdout;
else f = fopen(fname,"w");
if( f == NULL ) errorf("Error: unable to open output image file ",fname);
/* write header */
fprintf(f,"P5\n");
fprintf(f,"%u %u\n",ncol,nrow);
fprintf(f,"%d\n",255);
/* write data */
for(i=0; i<nrow; i++)
for(j=0; j<ncol; j++)
fputc((unsigned char) data[j+i*ncol],f);
/* close file if needed */
if( f != stdout && fclose(f) == EOF )
errorf("Error: unable to close file while writing PGM file ",fname);
}
/*----------------------------------------------------------------------------*/
/** Write an array of floats into a 8bit PGM file. Normalize the max value of
the floats array to be 255.
*/
void write_pgm_normalize_given_minmax_float(const char *fname,
const float *data, int ncol,
int nrow, float min,
float max)
{
FILE * f;
int i,j;
if(max==min)
errorf(
"Error: unable to normalize to max and min values. They are equal",
fname);
/* open file */
if( strcmp(fname,"-") == 0 ) f = stdout;
else f = fopen(fname,"w");
if( f == NULL ) errorf("Error: unable to open output image file ",fname);
/* write header */
fprintf(f,"P5\n");
fprintf(f,"%u %u\n",ncol,nrow);
fprintf(f,"%d\n",255);
/* write data */
for(i=0; i<nrow; i++)
for(j=0; j<ncol; j++)
{
fputc((unsigned char) (data[j+i*ncol]-min>0) ?
255.0/(max-min)*(data[j+i*ncol]-min) : 0 ,f);
}
/* close file if needed */
if( f != stdout && fclose(f) == EOF )
errorf("Error: unable to close file while writing PGM file ",fname);
}
/*---------------------------------------------------------------------------*/
/** Write an array of floats into a 8bit PGM file.
* Normalize the maximum value of
* the floats array to be 255.
*/
void write_pgm_normalize_float(const char *fname, const float *data,
int ncol, int nrow)
{
FILE * f;
int i,j;
float v,max,min;
/* check min and max values. If min is big than zero keep zero as min*/
max = min = 0;
for(i=0; i< nrow; i++)
for(j=0; j<ncol; j++)
{
v = data[ j + i * ncol];
if( v > max ) max = v;
if( v < min ) min = v;
}
if( min < 0 ) fprintf(stderr,
"Warning: negative values in '%s' are truncated to zero.\n",fname);
/* open file */
if( strcmp(fname,"-") == 0 ) f = stdout;
else f = fopen(fname,"w");
if( f == NULL ) errorf("Error: unable to open output image file ",fname);
/* write header */
fprintf(f,"P5\n");
fprintf(f,"%u %u\n",ncol,nrow);
fprintf(f,"%d\n",255);
/* write data */
for(i=0; i<nrow; i++)
for(j=0; j<ncol; j++)
fputc((unsigned char) (data[j+i*ncol]>0)
? 255.0/max*data[j+i*ncol] : 0 ,f);
/* close file if needed */
if( f != stdout && fclose(f) == EOF )
errorf("Error: unable to close file while writing PGM file ",fname);
}
/*---------------------------------------------------------------------------*/
/** Write an array of floats into a 3channel-8bit PPM image file. Normalize
* the maximum value of the floats array to be 255.
*/
void write_ppm_normalize_float(const char *fname, const float *rdata,
const float *gdata, const float *bdata,
int ncol, int nrow)
{
FILE * f;
int i,j;
float v,max,min;
/* check global min and max values in r,g,b channels. If min is big than
*zero keep zero as min*/
max = min = 0;
/*r*/
for(i=0; i< nrow; i++)
for(j=0; j<ncol; j++)
{
v = rdata[ j + i * ncol];
if( v > max ) max = v;
if( v < min ) min = v;
}
/*g*/
for(i=0; i< nrow; i++)
for(j=0; j<ncol; j++)
{
v = gdata[ j + i * ncol];
if( v > max ) max = v;
if( v < min ) min = v;
}
/*b*/
for(i=0; i< nrow; i++)
for(j=0; j<ncol; j++)
{
v = bdata[ j + i * ncol];
if( v > max ) max = v;
if( v < min ) min = v;
}
if( min < 0 ) fprintf(stderr,
"Warning: negative values in '%s' are truncated to zero.\n",
fname);
/* open file */
if( strcmp(fname,"-") == 0 ) f = stdout;
else f = fopen(fname,"w");
if( f == NULL ) errorf("Error: unable to open output image file ",fname);
/* write header */
fprintf(f,"P6\n");
fprintf(f,"%u %u\n",ncol,nrow);
fprintf(f,"%d\n",255);
/* write data */
for(i=0; i<nrow; i++)
for(j=0; j<ncol; j++)
{
static unsigned char color[3];
/*red*/
color[0] =(rdata[j+i*ncol]>0)? 255.0/max*rdata[j+i*ncol]:0;
/*green*/
color[1] =(gdata[j+i*ncol]>0)? 255.0/max*gdata[j+i*ncol]:0;
/*blue*/
color[2] =(bdata[j+i*ncol]>0)? 255.0/max*bdata[j+i*ncol]:0;
(void) fwrite(color, 1, 3, f);
}
/* close file if needed */
if( f != stdout && fclose(f) == EOF )
errorf("Error: unable to close file while writing PPM file ",fname);
}