File size: 13,570 Bytes
d4035c1 | 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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | /** @file pgm.c
** @brief Portable graymap format (PGM) parser - Definition
** @author Andrea Vedaldi
**/
/* AUTORIGHTS
Copyright (C) 2007-09 Andrea Vedaldi and Brian Fulkerson
This file is part of VLFeat, available in the terms of the GNU
General Public License version 2.
*/
/** @file pgm.h
This module implements basic input and ouptut of images in PGM
format.
To extract a PGM image from an input stream, first call
::vl_pgm_extract_head() to extract the image meta data (size and
bit-depth). Then allocate the appropriate buffer to hold the image
pixels and then call ::vl_pgm_extract_data().
To insert a PGM image to a file stream use ::vl_pgm_insert().
To quickly read/write a PGM image from/to a given file, use
::vl_pgm_read_new() and ::vl_pgm_write(). To to the same from a
buffer in floating point format use ::vl_pgm_read_new_f() and
::vl_pgm_write_f().
**/
#include "pgm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/** ------------------------------------------------------------------
** @internal
** @brief Remove all characters to the next new-line
** @param f file to strip.
** @return number of characters removed.
**/
static int
remove_line(FILE* f)
{
int count = 0 ;
int c ;
while (1) {
c = fgetc(f) ;
++ count ;
switch(c) {
case '\n' :
goto quit_remove_line ;
case EOF :
-- count ;
goto quit_remove_line ;
}
}
quit_remove_line :
return count ;
}
/** ------------------------------------------------------------------
** @internal
** @brief Remove white-spaces and comments
** @param f file to strip.
** @return number of characters removed.
**/
static int
remove_blanks(FILE* f)
{
int count = 0 ;
int c ;
while (1) {
c = fgetc(f) ;
switch(c) {
case '\t' : case '\n' :
case '\r' : case ' ' :
++ count ;
break ;
case '#' :
count += 1 + remove_line(f) ;
break ;
case EOF :
goto quit_remove_blanks ;
default:
ungetc(c, f) ;
goto quit_remove_blanks ;
}
}
quit_remove_blanks:
return count ;
}
/** ------------------------------------------------------------------
** @brief Get PGM image number of pixels.
**
** @param im PGM image descriptor.
**
** The functions returns the number of pixels of the PGM image @a im.
**
** To calculate the image data size in bytes, this value must be
** multiplied by the number of byte per pixels (see
** ::vl_pgm_get_bpp()).
**
** @return number of pixels of the image.
**/
VL_EXPORT
int
vl_pgm_get_npixels (VlPgmImage const *im)
{
return im->width * im->height ;
}
/** ------------------------------------------------------------------
** @brief Get PGM image bytes per pixel
**
** @param im PGM image descriptor.
**
** The function returns the number of bytes for each pixel of the
** PGM image @a im.
**
** @return number of bytes per pixel.
**/
VL_EXPORT
int
vl_pgm_get_bpp (VlPgmImage const *im)
{
return (im->max_value >= 256) + 1 ;
}
/** ------------------------------------------------------------------
** @brief Extract PGM header from stream
**
** @param f input file.
** @param im image structure to fill.
**
** The function extracts from the file @a f the meta-data section of
** an image encoded in PGM format. The function fills the structure
** VlPgmImage according.
**
** @return error code. The function sets ::vl_err_no to
** ::VL_PGM_INV_HEAD or ::VL_PGM_INV_META depending whether the error
** occurred in decoding the header or meta section of the PGM file.
**/
VL_EXPORT
int
vl_pgm_extract_head (FILE* f, VlPgmImage *im)
{
char magic [2] ;
int c ;
int is_raw ;
int width ;
int height ;
int max_value ;
int sz ;
vl_bool good ;
/* -----------------------------------------------------------------
* check magic number
* -------------------------------------------------------------- */
sz = fread(magic, 1, 2, f) ;
if (sz < 2) {
vl_err_no = VL_ERR_PGM_INV_HEAD ;
return -1 ;
}
good = magic [0] == 'P' ;
switch (magic [1]) {
case '2' : /* ASCII format */
is_raw = 0 ;
break ;
case '5' : /* RAW format */
is_raw = 1 ;
break ;
default :
good = 0 ;
break ;
}
if( ! good ) {
vl_err_no = VL_ERR_PGM_INV_HEAD ;
return -1 ;
}
/* -----------------------------------------------------------------
* parse width, height, max_value
* -------------------------------------------------------------- */
good = 1 ;
c = remove_blanks(f) ;
good &= c > 0 ;
c = fscanf(f, "%d", &width) ;
good &= c == 1 ;
c = remove_blanks(f) ;
good &= c > 0 ;
c = fscanf(f, "%d", &height) ;
good &= c == 1 ;
c = remove_blanks(f) ;
good &= c > 0 ;
c = fscanf(f, "%d", &max_value) ;
good &= c == 1 ;
/* must end with a single blank */
c = fgetc(f) ;
good &=
c == '\n' ||
c == '\t' ||
c == ' ' ||
c == '\r' ;
if(! good) {
vl_err_no = VL_ERR_PGM_INV_META ;
return vl_err_no ;
}
if(! max_value >= 65536) {
vl_err_no = VL_ERR_PGM_INV_META ;
return vl_err_no ;
}
/* exit */
im-> width = width ;
im-> height = height ;
im-> max_value = max_value ;
im-> is_raw = is_raw ;
return 0 ;
}
/** ------------------------------------------------------------------
** @brief Extract PGM data from stream
**
** @param f input file.
** @param im PGM image descriptor.
** @param data data buffer to fill.
**
** The function extracts from the file @a f the data section of an
** image encoded in PGM format. The function fills the buffer @a data
** according. The buffer @a data should be ::vl_pgm_get_npixels() by
** ::vl_pgm_get_bpp() bytes large.
**
** @return error code.
**/
VL_EXPORT
int
vl_pgm_extract_data (FILE* f, VlPgmImage const *im, void *data)
{
int bpp = vl_pgm_get_bpp (im) ;
int data_size = vl_pgm_get_npixels (im) ;
int c ;
vl_bool good = 1 ;
/* -----------------------------------------------------------------
* read data
* -------------------------------------------------------------- */
/*
In RAW mode we read directly an array of bytes or shorts. In
the latter case, however, we must take care of the
endianess. PGM files are sorted in big-endian format. If our
architecture is little endian, we must do a conversion.
*/
if (im->is_raw) {
c = fread( data,
bpp,
data_size,
f ) ;
good = (c == data_size) ;
/* adjust endianess */
#if defined(VL_ARCH_LITTLE_ENDIAN)
if (bpp == 2) {
int i ;
vl_uint8 *pt = (vl_uint8*) data ;
for(i = 0 ; i < 2 * data_size ; i += 2) {
vl_uint8 tmp = pt [i] ;
pt [i] = pt [i+1] ;
pt [i+1] = tmp ;
}
}
#endif
}
/*
In ASCII mode we read a sequence of decimal numbers separated
by whitespaces.
*/
else {
int i ;
int unsigned v ;
for(good = 1, i = 0 ;
i < data_size && good ;
++i) {
c = fscanf(f, " %ud", &v) ;
if (bpp == 1) {
* ((vl_uint8* ) data + i) = (vl_uint8) v ;
} else {
* ((vl_uint16*) data + i) = (vl_uint16) v ;
}
good &= c == 1 ;
}
}
if(! good ) {
vl_err_no = VL_ERR_PGM_INV_DATA ;
snprintf(vl_err_msg, VL_ERR_MSG_LEN,
"Invalid PGM data") ;
return vl_err_no ;
}
return 0 ;
}
/** ------------------------------------------------------------------
** @brief Insert a PGM image into a stream
**
** @param f output file.
** @param im PGM image meta-data.
** @param data image data.
** @return error code.
**/
VL_EXPORT
int
vl_pgm_insert(FILE* f, VlPgmImage const *im, void const *data)
{
int bpp = vl_pgm_get_bpp (im) ;
int data_size = vl_pgm_get_npixels (im) ;
int c ;
/* write preamble */
fprintf(f,
"P5\n%d\n%d\n%d\n",
im->width,
im->height,
im->max_value) ;
/* take care of endianness */
#if defined(VL_ARCH_LITTLE_ENDIAN)
if (bpp == 2) {
int i ;
vl_uint8* temp = vl_malloc (2 * data_size) ;
memcpy(temp, data, 2 * data_size) ;
for(i = 0 ; i < 2 * data_size ; i += 2) {
vl_uint8 tmp = temp [i] ;
temp [i] = temp [i+1] ;
temp [i+1] = tmp ;
}
c = fwrite(temp, 2, data_size, f) ;
vl_free (temp) ;
}
else {
#endif
c = fwrite(data, bpp, data_size, f) ;
#if defined(VL_ARCH_LITTLE_ENDIAN)
}
#endif
if(c != data_size) {
vl_err_no = VL_ERR_PGM_IO ;
snprintf(vl_err_msg, VL_ERR_MSG_LEN,
"Error writing PGM data") ;
return vl_err_no ;
}
return 0 ;
}
/** ------------------------------------------------------------------
** @brief Read a PGM file
**
** @param name file name.
** @param im a pointer to the PGM image structure to fill.
** @param data a pointer to the pointer to the allocated buffer.
**
** The function reads a PGM image from file @a name and initializes the
** structure @a im and the buffer @a data accordingly.
**
** The ownership of the buffer @a data is transfered to the caller.
** @a data should be freed by means of ::vl_free().
**
** @bug Only PGM files with 1 BPP are supported.
**
** @return error code.
**/
VL_EXPORT
int vl_pgm_read_new (char const *name, VlPgmImage *im, vl_uint8** data)
{
int err = 0 ;
FILE *f = fopen (name, "rb") ;
if (! f) {
vl_err_no = VL_ERR_PGM_IO ;
snprintf(vl_err_msg, VL_ERR_MSG_LEN,
"Error opening PGM file `%s' for reading", name) ;
return vl_err_no ;
}
err = vl_pgm_extract_head(f, im) ;
if (err) {
fclose (f) ;
return err ;
}
if (vl_pgm_get_bpp(im) > 1) {
vl_err_no = VL_ERR_BAD_ARG ;
snprintf(vl_err_msg, VL_ERR_MSG_LEN,
"vl_pgm_read(): PGM with BPP > 1 not supported") ;
return vl_err_no ;
}
*data = vl_malloc (vl_pgm_get_npixels(im) * sizeof(vl_uint8)) ;
err = vl_pgm_extract_data(f, im, *data) ;
if (err) {
vl_free (data) ;
fclose (f) ;
}
fclose (f) ;
return err ;
}
/** ------------------------------------------------------------------
** @brief Read floats from a PGM file
**
** @param name file name.
** @param im a pointer to the PGM image structure to fill.
** @param data a pointer to the pointer to the allocated buffer.
**
** The function reads a PGM image from file @a name and initializes the
** structure @a im and the buffer @a data accordingly. The buffer
** @a data is an array of floats in the range [0, 1].
**
** The ownership of the buffer @a data is transfered to the caller.
** @a data should be freed by means of ::vl_free().
**
** @bug Only PGM files with 1 BPP are supported.
**
** @return error code.
**/
VL_EXPORT
int vl_pgm_read_new_f (char const *name, VlPgmImage *im, float** data)
{
int err = 0 ;
size_t npixels ;
vl_uint8 *idata ;
err = vl_pgm_read_new (name, im, &idata) ;
if (err) {
return err ;
}
npixels = vl_pgm_get_npixels(im) ;
*data = vl_malloc (sizeof(float) * npixels) ;
{
size_t k ;
float scale = 1.0f / im->max_value ;
for (k = 0 ; k < npixels ; ++ k) (*data)[k] = scale * idata[k] ;
}
vl_free (idata) ;
return err ;
}
/** ------------------------------------------------------------------
** @brief Write bytes to a PGM file
**
** @param name file name.
** @param data data to write.
** @param width width of the image.
** @param height height of the image.
**
** The function dumps the image @a data to the PGM file of the specified
** name. This is an helper function simplifying the usage of
** vl_pgm_insert().
**
** @return error code.
**/
VL_EXPORT
int vl_pgm_write (char const *name, vl_uint8 const* data, int width, int height)
{
int err = 0 ;
VlPgmImage pgm ;
FILE *f = fopen (name, "wb") ;
if (! f) {
vl_err_no = VL_ERR_PGM_IO ;
snprintf(vl_err_msg, VL_ERR_MSG_LEN,
"Error opening PGM file for writing") ;
return vl_err_no ;
}
pgm.width = width ;
pgm.height = height ;
pgm.is_raw = 1 ;
pgm.max_value = 255 ;
err = vl_pgm_insert (f, &pgm, data) ;
fclose (f) ;
return err ;
}
/** -------------------------------------------------------------------
** @brief Write floats to PGM file
**
** @param name file name.
** @param data data to write.
** @param width width of the image.
** @param height height of the image.
**
** The function dumps the image @a data to the PGM file of the specified
** name. The data is re-scaled to fit in the range 0-255.
** This is an helper function simplifying the usage of
** vl_pgm_insert().
**
** @return error code.
**/
VL_EXPORT
int vl_pgm_write_f (char const *name, float const* data, int width, int height)
{
int err = 0 ;
int k ;
float min = + VL_INFINITY_F ;
float max = - VL_INFINITY_F ;
float scale ;
vl_uint8 * buffer = vl_malloc (sizeof(float) * width * height) ;
for (k = 0 ; k < width * height ; ++k) {
min = VL_MIN(min, data [k]) ;
max = VL_MAX(max, data [k]) ;
}
scale = 255 / (max - min + VL_EPSILON_F) ;
for (k = 0 ; k < width * height ; ++k) {
buffer [k] = (vl_uint8) ((data [k] - min) * scale) ;
}
err = vl_pgm_write (name, buffer, width, height) ;
vl_free (buffer) ;
return err ;
}
|