You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

542 lines
13 KiB

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