Debian Codegraph @

src/thirdparty/VLFeat/pgm.c

raw
1 /** @file pgm.c
2 ** @brief Portable graymap format (PGM) parser - Definition
3 ** @author Andrea Vedaldi
4 **/
5
6 /*
7 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
8 Copyright (C) 2013 Andrea Vedaldi.
9 All rights reserved.
10
11 This file is part of the VLFeat library and is made available under
12 the terms of the BSD license (see the COPYING file).
13 */
14
15 /** @file pgm.h
16
17 This module implements basic input and ouptut of images in PGM
18 format.
19
20 Extracting an image encoded in PGM format from an imput
21 file stream involves the following steps:
22
23 - use ::vl_pgm_extract_head to extract the image meta data
24 (size and bit depth);
25 - allocate a buffer to store the image data;
26 - use ::vl_pgm_extract_data to extract the image data to the allocated
27 buffer.
28
29 Writing an image in PGM format to an ouptut file stream
30 can be done by using ::vl_pgm_insert.
31
32 To quickly read/write a PGM image from/to a given file, use
33 ::vl_pgm_read_new() and ::vl_pgm_write(). To to the same from a
34 buffer in floating point format use ::vl_pgm_read_new_f() and
35 ::vl_pgm_write_f().
36
37 **/
38
39 #include "pgm.h"
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 /** ------------------------------------------------------------------
46 ** @internal @brief Remove all characters to the next new-line.
47 ** @param f file to strip.
48 ** @return number of characters removed.
49 **/
50
51 static int
52 remove_line(FILE* f)
53 {
54 int count = 0 ;
55 int c ;
56
57 while (1) {
58 c = fgetc(f) ;
59 ++ count ;
60
61 switch(c) {
62 case '\n' :
63 goto quit_remove_line ;
64
65 case EOF :
66 -- count ;
67 goto quit_remove_line ;
68 }
69 }
70 quit_remove_line :
71 return count ;
72 }
73
74 /** ------------------------------------------------------------------
75 ** @internal @brief Remove white-spaces and comments.
76 ** @param f file to strip.
77 ** @return number of characters removed.
78 **/
79
80 static int
81 remove_blanks(FILE* f)
82 {
83 int count = 0 ;
84 int c ;
85
86 while (1) {
87 c = fgetc(f) ;
88
89 switch(c) {
90
91 case '\t' : case '\n' :
92 case '\r' : case ' ' :
93 ++ count ;
94 break ;
95
96 case '#' :
97 count += 1 + remove_line(f) ;
98 break ;
99
100 case EOF :
101 goto quit_remove_blanks ;
102
103 default:
104 ungetc(c, f) ;
105 goto quit_remove_blanks ;
106 }
107 }
108 quit_remove_blanks:
109 return count ;
110 }
111
112 /** ------------------------------------------------------------------
113 ** @brief Get PGM image number of pixels.
114 ** @param im PGM image descriptor.
115 ** @return number of pixels of the image.
116 **
117 ** The functions returns the number of pixels of the PGM image @a im.
118 **
119 ** To calculate the image data size in bytes, this value must be
120 ** multiplied by the number of byte per pixels (see
121 ** ::vl_pgm_get_bpp()).
122 **/
123
124 VL_EXPORT vl_size
125 vl_pgm_get_npixels (VlPgmImage const *im)
126 {
127 return im->width * im->height ;
128 }
129
130 /** ------------------------------------------------------------------
131 ** @brief Get PGM image bytes per pixel.
132 ** @param im PGM image descriptor.
133 ** @return number of bytes per pixel.
134 **
135 ** The function returns the number of bytes for each pixel of the
136 ** PGM image @a im.
137 **/
138
139 VL_EXPORT vl_size
140 vl_pgm_get_bpp (VlPgmImage const *im)
141 {
142 return (im->max_value >= 256) + 1 ;
143 }
144
145 /** ------------------------------------------------------------------
146 ** @brief Extract PGM header from stream.
147 ** @param f input file.
148 ** @param im image structure to fill.
149 ** @return error code.
150 **
151 ** The function extracts from the file @a f the meta-data section of
152 ** an image encoded in PGM format. The function fills the structure
153 ** ::VlPgmImage accordingly.
154 **
155 ** The error may be either ::VL_ERR_PGM_INV_HEAD or ::VL_ERR_PGM_INV_META
156 ** depending whether the error occurred in decoding the header or
157 ** meta section of the PGM file.
158 **/
159
160 VL_EXPORT int
161 vl_pgm_extract_head (FILE* f, VlPgmImage *im)
162 {
163 char magic [2] ;
164 int c ;
165 int is_raw ;
166 int width ;
167 int height ;
168 int max_value ;
169 size_t sz ;
170 vl_bool good ;
171
172 /* -----------------------------------------------------------------
173 * check magic number
174 * -------------------------------------------------------------- */
175 sz = fread(magic, 1, 2, f) ;
176
177 if (sz < 2) {
178 return vl_set_last_error(VL_ERR_PGM_INV_HEAD, "Invalid PGM header") ;
179 }
180
181 good = magic [0] == 'P' ;
182
183 switch (magic [1]) {
184 case '2' : /* ASCII format */
185 is_raw = 0 ;
186 break ;
187
188 case '5' : /* RAW format */
189 is_raw = 1 ;
190 break ;
191
192 default :
193 good = 0 ;
194 break ;
195 }
196
197 if( ! good ) {
198 return vl_set_last_error(VL_ERR_PGM_INV_HEAD, "Invalid PGM header") ;
199 }
200
201 /* -----------------------------------------------------------------
202 * parse width, height, max_value
203 * -------------------------------------------------------------- */
204 good = 1 ;
205
206 c = remove_blanks(f) ;
207 good &= c > 0 ;
208
209 c = fscanf(f, "%d", &width) ;
210 good &= c == 1 ;
211
212 c = remove_blanks(f) ;
213 good &= c > 0 ;
214
215 c = fscanf(f, "%d", &height) ;
216 good &= c == 1 ;
217
218 c = remove_blanks(f) ;
219 good &= c > 0 ;
220
221 c = fscanf(f, "%d", &max_value) ;
222 good &= c == 1 ;
223
224 /* must end with a single blank */
225 c = fgetc(f) ;
226 good &=
227 c == '\n' ||
228 c == '\t' ||
229 c == ' ' ||
230 c == '\r' ;
231
232 if(! good) {
233 return vl_set_last_error(VL_ERR_PGM_INV_META, "Invalid PGM meta information");
234 }
235
236 if(! max_value >= 65536) {
237 return vl_set_last_error(VL_ERR_PGM_INV_META, "Invalid PGM meta information");
238 }
239
240 /* exit */
241 im-> width = width ;
242 im-> height = height ;
243 im-> max_value = max_value ;
244 im-> is_raw = is_raw ;
245 return 0 ;
246 }
247
248 /** ------------------------------------------------------------------
249 ** @brief Extract PGM data from stream.
250 ** @param f input file.
251 ** @param im PGM image descriptor.
252 ** @param data data buffer to fill.
253 ** @return error code.
254 **
255 ** The function extracts from the file @a f the data section of an
256 ** image encoded in PGM format. The function fills the buffer @a data
257 ** according. The buffer @a data should be ::vl_pgm_get_npixels() by
258 ** ::vl_pgm_get_bpp() bytes large.
259 **/
260
261 VL_EXPORT
262 int
263 vl_pgm_extract_data (FILE* f, VlPgmImage const *im, void *data)
264 {
265 vl_size bpp = vl_pgm_get_bpp(im) ;
266 vl_size data_size = vl_pgm_get_npixels(im) ;
267 vl_bool good = 1 ;
268 size_t c ;
269
270 /* -----------------------------------------------------------------
271 * read data
272 * -------------------------------------------------------------- */
273
274 /*
275 In RAW mode we read directly an array of bytes or shorts. In
276 the latter case, however, we must take care of the
277 endianess. PGM files are sorted in big-endian format. If our
278 architecture is little endian, we must do a conversion.
279 */
280 if (im->is_raw) {
281
282 c = fread( data,
283 bpp,
284 data_size,
285 f ) ;
286 good = (c == data_size) ;
287
288 /* adjust endianess */
289 #if defined(VL_ARCH_LITTLE_ENDIAN)
290 if (bpp == 2) {
291 vl_uindex i ;
292 vl_uint8 *pt = (vl_uint8*) data ;
293 for(i = 0 ; i < 2 * data_size ; i += 2) {
294 vl_uint8 tmp = pt [i] ;
295 pt [i] = pt [i+1] ;
296 pt [i+1] = tmp ;
297 }
298 }
299 #endif
300 }
301 /*
302 In ASCII mode we read a sequence of decimal numbers separated
303 by whitespaces.
304 */
305 else {
306 vl_uindex i ;
307 int unsigned v ;
308 for(good = 1, i = 0 ;
309 i < data_size && good ;
310 ++i) {
311 c = fscanf(f, " %ud", &v) ;
312 if (bpp == 1) {
313 * ((vl_uint8* ) data + i) = (vl_uint8) v ;
314 } else {
315 * ((vl_uint16*) data + i) = (vl_uint16) v ;
316 }
317 good &= c == 1 ;
318 }
319 }
320
321 if(! good ) {
322 return vl_set_last_error(VL_ERR_PGM_INV_DATA, "Invalid PGM data") ;
323 }
324 return 0 ;
325 }
326
327 /** ------------------------------------------------------------------
328 ** @brief Insert a PGM image into a stream.
329 ** @param f output file.
330 ** @param im PGM image meta-data.
331 ** @param data image data.
332 ** @return error code.
333 **/
334
335 VL_EXPORT
336 int
337 vl_pgm_insert(FILE* f, VlPgmImage const *im, void const *data)
338 {
339 vl_size bpp = vl_pgm_get_bpp (im) ;
340 vl_size data_size = vl_pgm_get_npixels (im) ;
341 size_t c ;
342
343 /* write preamble */
344 fprintf(f,
345 "P5\n%d\n%d\n%d\n",
346 (signed)im->width,
347 (signed)im->height,
348 (signed)im->max_value) ;
349
350 /* take care of endianness */
351 #if defined(VL_ARCH_LITTLE_ENDIAN)
352 if (bpp == 2) {
353 vl_uindex i ;
354 vl_uint8* temp = vl_malloc (2 * data_size) ;
355 memcpy(temp, data, 2 * data_size) ;
356 for(i = 0 ; i < 2 * data_size ; i += 2) {
357 vl_uint8 tmp = temp [i] ;
358 temp [i] = temp [i+1] ;
359 temp [i+1] = tmp ;
360 }
361 c = fwrite(temp, 2, data_size, f) ;
362 vl_free (temp) ;
363 }
364 else {
365 #endif
366 c = fwrite(data, bpp, data_size, f) ;
367 #if defined(VL_ARCH_LITTLE_ENDIAN)
368 }
369 #endif
370
371 if(c != data_size) {
372 return vl_set_last_error(VL_ERR_PGM_IO, "Error writing PGM data") ;
373 }
374 return 0 ;
375 }
376
377 /** ------------------------------------------------------------------
378 ** @brief Read a PGM file.
379 ** @param name file name.
380 ** @param im a pointer to the PGM image structure to fill.
381 ** @param data a pointer to the pointer to the allocated buffer.
382 ** @return error code.
383 **
384 ** The function reads a PGM image from file @a name and initializes
385 ** the structure @a im and the buffer @a data accordingly.
386 **
387 ** The ownership of the buffer @a data is transfered to the caller.
388 ** @a data should be freed by means of ::vl_free().
389 **
390 ** @bug Only PGM files with 1 BPP are supported.
391 **/
392
393 VL_EXPORT
394 int vl_pgm_read_new (char const *name, VlPgmImage *im, vl_uint8** data)
395 {
396 int err = 0 ;
397 FILE *f = fopen (name, "rb") ;
398
399 if (! f) {
400 return vl_set_last_error(VL_ERR_PGM_IO, "Error opening PGM file `%s' for reading", name) ;
401 }
402
403 err = vl_pgm_extract_head(f, im) ;
404 if (err) {
405 fclose (f) ;
406 return err ;
407 }
408
409 if (vl_pgm_get_bpp(im) > 1) {
410 return vl_set_last_error(VL_ERR_BAD_ARG, "PGM with BPP > 1 not supported") ;
411 }
412
413 *data = vl_malloc (vl_pgm_get_npixels(im) * sizeof(vl_uint8)) ;
414 err = vl_pgm_extract_data(f, im, *data) ;
415
416 if (err) {
417 vl_free (data) ;
418 fclose (f) ;
419 }
420
421 fclose (f) ;
422 return err ;
423 }
424
425 /** ------------------------------------------------------------------
426 ** @brief Read floats from a PGM file.
427 ** @param name file name.
428 ** @param im a pointer to the PGM image structure to fill.
429 ** @param data a pointer to the pointer to the allocated buffer.
430 ** @return error code.
431 **
432 ** The function reads a PGM image from file @a name and initializes
433 ** the structure @a im and the buffer @a data accordingly. The buffer
434 ** @a data is an array of floats in the range [0, 1].
435 **
436 ** The ownership of the buffer @a data is transfered to the caller.
437 ** @a data should be freed by means of ::vl_free().
438 **
439 ** @bug Only PGM files with 1 BPP are supported.
440 **/
441
442 VL_EXPORT
443 int vl_pgm_read_new_f (char const *name, VlPgmImage *im, float** data)
444 {
445 int err = 0 ;
446 size_t npixels ;
447 vl_uint8 *idata ;
448
449 err = vl_pgm_read_new (name, im, &idata) ;
450 if (err) {
451 return err ;
452 }
453
454 npixels = vl_pgm_get_npixels(im) ;
455 *data = vl_malloc (sizeof(float) * npixels) ;
456 {
457 size_t k ;
458 float scale = 1.0f / (float)im->max_value ;
459 for (k = 0 ; k < npixels ; ++ k) (*data)[k] = scale * idata[k] ;
460 }
461
462 vl_free (idata) ;
463 return 0 ;
464 }
465
466 /** ------------------------------------------------------------------
467 ** @brief Write bytes to a PGM file.
468 ** @param name file name.
469 ** @param data data to write.
470 ** @param width width of the image.
471 ** @param height height of the image.
472 ** @return error code.
473 **
474 ** The function dumps the image @a data to the PGM file of the specified
475 ** name. This is an helper function simplifying the usage of
476 ** vl_pgm_insert().
477 **/
478
479 VL_EXPORT
480 int vl_pgm_write (char const *name, vl_uint8 const* data, int width, int height)
481 {
482 int err = 0 ;
483 VlPgmImage pgm ;
484 FILE *f = fopen (name, "wb") ;
485
486 if (! f) {
487 return vl_set_last_error(VL_ERR_PGM_IO,
488 "Error opening PGM file '%s' for writing", name) ;
489 }
490
491 pgm.width = width ;
492 pgm.height = height ;
493 pgm.is_raw = 1 ;
494 pgm.max_value = 255 ;
495
496 err = vl_pgm_insert (f, &pgm, data) ;
497 fclose (f) ;
498
499 return err ;
500 }
501
502 /** -------------------------------------------------------------------
503 ** @brief Write floats to PGM file
504 ** @param name file name.
505 ** @param data data to write.
506 ** @param width width of the image.
507 ** @param height height of the image.
508 ** @return error code.
509 **
510 ** The function dumps the image @a data to the PGM file of the
511 ** specified name. The data is re-scaled to fit in the range 0-255.
512 ** This is an helper function simplifying the usage of
513 ** vl_pgm_insert().
514 **/
515
516 VL_EXPORT
517 int vl_pgm_write_f (char const *name, float const* data, int width, int height)
518 {
519 int err = 0 ;
520 int k ;
521 float min = + VL_INFINITY_F ;
522 float max = - VL_INFINITY_F ;
523 float scale ;
524
525 vl_uint8 * buffer = vl_malloc (sizeof(float) * width * height) ;
526
527 for (k = 0 ; k < width * height ; ++k) {
528 min = VL_MIN(min, data [k]) ;
529 max = VL_MAX(max, data [k]) ;
530 }
531
532 scale = 255 / (max - min + VL_EPSILON_F) ;
533
534 for (k = 0 ; k < width * height ; ++k) {
535 buffer [k] = (vl_uint8) ((data [k] - min) * scale) ;
536 }
537
538 err = vl_pgm_write (name, buffer, width, height) ;
539
540 vl_free (buffer) ;
541 return err ;
542 }