pnm2png.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * pnm2png.c --- conversion from PBM/PGM/PPM-file to PNG-file
  3. * copyright (C) 1999 by Willem van Schaik <[email protected]>
  4. *
  5. * version 1.0 - 1999.10.15 - First version.
  6. * version 1.1 - 2015.07.29 - Fixed leaks (Glenn Randers-Pehrson)
  7. *
  8. * Permission to use, copy, modify, and distribute this software and
  9. * its documentation for any purpose and without fee is hereby granted,
  10. * provided that the above copyright notice appear in all copies and
  11. * that both that copyright notice and this permission notice appear in
  12. * supporting documentation. This software is provided "as is" without
  13. * express or implied warranty.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #ifdef __TURBOC__
  18. #include <mem.h>
  19. #include <fcntl.h>
  20. #endif
  21. #include <zlib.h>
  22. #ifndef BOOL
  23. #define BOOL unsigned char
  24. #endif
  25. #ifndef TRUE
  26. #define TRUE (BOOL) 1
  27. #endif
  28. #ifndef FALSE
  29. #define FALSE (BOOL) 0
  30. #endif
  31. #define STDIN 0
  32. #define STDOUT 1
  33. #define STDERR 2
  34. /* to make pnm2png verbose so we can find problems (needs to be before png.h) */
  35. #ifndef PNG_DEBUG
  36. #define PNG_DEBUG 0
  37. #endif
  38. #include "png.h"
  39. /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
  40. #ifndef png_jmpbuf
  41. # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
  42. #endif
  43. /* function prototypes */
  44. int main (int argc, char *argv[]);
  45. void usage ();
  46. BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
  47. BOOL alpha);
  48. void get_token(FILE *pnm_file, char *token);
  49. png_uint_32 get_data (FILE *pnm_file, int depth);
  50. png_uint_32 get_value (FILE *pnm_file, int depth);
  51. /*
  52. * main
  53. */
  54. int main(int argc, char *argv[])
  55. {
  56. FILE *fp_rd = stdin;
  57. FILE *fp_al = NULL;
  58. FILE *fp_wr = stdout;
  59. BOOL interlace = FALSE;
  60. BOOL alpha = FALSE;
  61. int argi;
  62. for (argi = 1; argi < argc; argi++)
  63. {
  64. if (argv[argi][0] == '-')
  65. {
  66. switch (argv[argi][1])
  67. {
  68. case 'i':
  69. interlace = TRUE;
  70. break;
  71. case 'a':
  72. alpha = TRUE;
  73. argi++;
  74. if ((fp_al = fopen (argv[argi], "rb")) == NULL)
  75. {
  76. fprintf (stderr, "PNM2PNG\n");
  77. fprintf (stderr, "Error: alpha-channel file %s does not exist\n",
  78. argv[argi]);
  79. exit (1);
  80. }
  81. break;
  82. case 'h':
  83. case '?':
  84. usage();
  85. exit(0);
  86. break;
  87. default:
  88. fprintf (stderr, "PNM2PNG\n");
  89. fprintf (stderr, "Error: unknown option %s\n", argv[argi]);
  90. usage();
  91. exit(1);
  92. break;
  93. } /* end switch */
  94. }
  95. else if (fp_rd == stdin)
  96. {
  97. if ((fp_rd = fopen (argv[argi], "rb")) == NULL)
  98. {
  99. fprintf (stderr, "PNM2PNG\n");
  100. fprintf (stderr, "Error: file %s does not exist\n", argv[argi]);
  101. exit (1);
  102. }
  103. }
  104. else if (fp_wr == stdout)
  105. {
  106. if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
  107. {
  108. fprintf (stderr, "PNM2PNG\n");
  109. fprintf (stderr, "Error: can not create PNG-file %s\n", argv[argi]);
  110. exit (1);
  111. }
  112. }
  113. else
  114. {
  115. fprintf (stderr, "PNM2PNG\n");
  116. fprintf (stderr, "Error: too many parameters\n");
  117. usage();
  118. exit (1);
  119. }
  120. } /* end for */
  121. #ifdef __TURBOC__
  122. /* set stdin/stdout to binary, we're reading the PNM always! in binary format */
  123. if (fp_rd == stdin)
  124. {
  125. setmode (STDIN, O_BINARY);
  126. }
  127. if (fp_wr == stdout)
  128. {
  129. setmode (STDOUT, O_BINARY);
  130. }
  131. #endif
  132. /* call the conversion program itself */
  133. if (pnm2png (fp_rd, fp_wr, fp_al, interlace, alpha) == FALSE)
  134. {
  135. fprintf (stderr, "PNM2PNG\n");
  136. fprintf (stderr, "Error: unsuccessful converting to PNG-image\n");
  137. exit (1);
  138. }
  139. /* close input file */
  140. fclose (fp_rd);
  141. /* close output file */
  142. fclose (fp_wr);
  143. /* close alpha file */
  144. if (alpha)
  145. fclose (fp_al);
  146. return 0;
  147. }
  148. /*
  149. * usage
  150. */
  151. void usage()
  152. {
  153. fprintf (stderr, "PNM2PNG\n");
  154. fprintf (stderr, " by Willem van Schaik, 1999\n");
  155. #ifdef __TURBOC__
  156. fprintf (stderr, " for Turbo-C and Borland-C compilers\n");
  157. #else
  158. fprintf (stderr, " for Linux (and Unix) compilers\n");
  159. #endif
  160. fprintf (stderr, "Usage: pnm2png [options] <file>.<pnm> [<file>.png]\n");
  161. fprintf (stderr, " or: ... | pnm2png [options]\n");
  162. fprintf (stderr, "Options:\n");
  163. fprintf (stderr, " -i[nterlace] write png-file with interlacing on\n");
  164. fprintf (stderr,
  165. " -a[lpha] <file>.pgm read PNG alpha channel as pgm-file\n");
  166. fprintf (stderr, " -h | -? print this help-information\n");
  167. }
  168. /*
  169. * pnm2png
  170. */
  171. BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
  172. BOOL alpha)
  173. {
  174. png_struct *png_ptr = NULL;
  175. png_info *info_ptr = NULL;
  176. png_byte *png_pixels = NULL;
  177. png_byte **row_pointers = NULL;
  178. png_byte *pix_ptr = NULL;
  179. volatile png_uint_32 row_bytes;
  180. char type_token[16];
  181. char width_token[16];
  182. char height_token[16];
  183. char maxval_token[16];
  184. volatile int color_type=1;
  185. unsigned long ul_width=0, ul_alpha_width=0;
  186. unsigned long ul_height=0, ul_alpha_height=0;
  187. unsigned long ul_maxval=0;
  188. volatile png_uint_32 width=0, height=0;
  189. volatile png_uint_32 alpha_width=0, alpha_height=0;
  190. png_uint_32 maxval;
  191. volatile int bit_depth = 0;
  192. int channels=0;
  193. int alpha_depth = 0;
  194. int alpha_present=0;
  195. int row, col;
  196. BOOL raw, alpha_raw = FALSE;
  197. #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
  198. BOOL packed_bitmap = FALSE;
  199. #endif
  200. png_uint_32 tmp16;
  201. int i;
  202. /* read header of PNM file */
  203. get_token(pnm_file, type_token);
  204. if (type_token[0] != 'P')
  205. {
  206. return FALSE;
  207. }
  208. else if ((type_token[1] == '1') || (type_token[1] == '4'))
  209. {
  210. #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
  211. raw = (type_token[1] == '4');
  212. color_type = PNG_COLOR_TYPE_GRAY;
  213. get_token(pnm_file, width_token);
  214. sscanf (width_token, "%lu", &ul_width);
  215. width = (png_uint_32) ul_width;
  216. get_token(pnm_file, height_token);
  217. sscanf (height_token, "%lu", &ul_height);
  218. height = (png_uint_32) ul_height;
  219. bit_depth = 1;
  220. packed_bitmap = TRUE;
  221. #else
  222. fprintf (stderr, "PNM2PNG built without PNG_WRITE_INVERT_SUPPORTED and \n");
  223. fprintf (stderr, "PNG_WRITE_PACK_SUPPORTED can't read PBM (P1,P4) files\n");
  224. #endif
  225. }
  226. else if ((type_token[1] == '2') || (type_token[1] == '5'))
  227. {
  228. raw = (type_token[1] == '5');
  229. color_type = PNG_COLOR_TYPE_GRAY;
  230. get_token(pnm_file, width_token);
  231. sscanf (width_token, "%lu", &ul_width);
  232. width = (png_uint_32) ul_width;
  233. get_token(pnm_file, height_token);
  234. sscanf (height_token, "%lu", &ul_height);
  235. height = (png_uint_32) ul_height;
  236. get_token(pnm_file, maxval_token);
  237. sscanf (maxval_token, "%lu", &ul_maxval);
  238. maxval = (png_uint_32) ul_maxval;
  239. if (maxval <= 1)
  240. bit_depth = 1;
  241. else if (maxval <= 3)
  242. bit_depth = 2;
  243. else if (maxval <= 15)
  244. bit_depth = 4;
  245. else if (maxval <= 255)
  246. bit_depth = 8;
  247. else /* if (maxval <= 65535) */
  248. bit_depth = 16;
  249. }
  250. else if ((type_token[1] == '3') || (type_token[1] == '6'))
  251. {
  252. raw = (type_token[1] == '6');
  253. color_type = PNG_COLOR_TYPE_RGB;
  254. get_token(pnm_file, width_token);
  255. sscanf (width_token, "%lu", &ul_width);
  256. width = (png_uint_32) ul_width;
  257. get_token(pnm_file, height_token);
  258. sscanf (height_token, "%lu", &ul_height);
  259. height = (png_uint_32) ul_height;
  260. get_token(pnm_file, maxval_token);
  261. sscanf (maxval_token, "%lu", &ul_maxval);
  262. maxval = (png_uint_32) ul_maxval;
  263. if (maxval <= 1)
  264. bit_depth = 1;
  265. else if (maxval <= 3)
  266. bit_depth = 2;
  267. else if (maxval <= 15)
  268. bit_depth = 4;
  269. else if (maxval <= 255)
  270. bit_depth = 8;
  271. else /* if (maxval <= 65535) */
  272. bit_depth = 16;
  273. }
  274. else
  275. {
  276. return FALSE;
  277. }
  278. /* read header of PGM file with alpha channel */
  279. if (alpha)
  280. {
  281. if (color_type == PNG_COLOR_TYPE_GRAY)
  282. color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
  283. if (color_type == PNG_COLOR_TYPE_RGB)
  284. color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  285. get_token(alpha_file, type_token);
  286. if (type_token[0] != 'P')
  287. {
  288. return FALSE;
  289. }
  290. else if ((type_token[1] == '2') || (type_token[1] == '5'))
  291. {
  292. alpha_raw = (type_token[1] == '5');
  293. get_token(alpha_file, width_token);
  294. sscanf (width_token, "%lu", &ul_alpha_width);
  295. alpha_width=(png_uint_32) ul_alpha_width;
  296. if (alpha_width != width)
  297. return FALSE;
  298. get_token(alpha_file, height_token);
  299. sscanf (height_token, "%lu", &ul_alpha_height);
  300. alpha_height = (png_uint_32) ul_alpha_height;
  301. if (alpha_height != height)
  302. return FALSE;
  303. get_token(alpha_file, maxval_token);
  304. sscanf (maxval_token, "%lu", &ul_maxval);
  305. maxval = (png_uint_32) ul_maxval;
  306. if (maxval <= 1)
  307. alpha_depth = 1;
  308. else if (maxval <= 3)
  309. alpha_depth = 2;
  310. else if (maxval <= 15)
  311. alpha_depth = 4;
  312. else if (maxval <= 255)
  313. alpha_depth = 8;
  314. else /* if (maxval <= 65535) */
  315. alpha_depth = 16;
  316. if (alpha_depth != bit_depth)
  317. return FALSE;
  318. }
  319. else
  320. {
  321. return FALSE;
  322. }
  323. } /* end if alpha */
  324. /* calculate the number of channels and store alpha-presence */
  325. if (color_type == PNG_COLOR_TYPE_GRAY)
  326. channels = 1;
  327. else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  328. channels = 2;
  329. else if (color_type == PNG_COLOR_TYPE_RGB)
  330. channels = 3;
  331. else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  332. channels = 4;
  333. #if 0
  334. else
  335. channels = 0; /* cannot happen */
  336. #endif
  337. alpha_present = (channels - 1) % 2;
  338. #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
  339. if (packed_bitmap)
  340. /* row data is as many bytes as can fit width x channels x bit_depth */
  341. row_bytes = (width * channels * bit_depth + 7) / 8;
  342. else
  343. #endif
  344. /* row_bytes is the width x number of channels x (bit-depth / 8) */
  345. row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2);
  346. if ((png_pixels = (png_byte *)
  347. malloc (row_bytes * height * sizeof (png_byte))) == NULL)
  348. return FALSE;
  349. /* read data from PNM file */
  350. pix_ptr = png_pixels;
  351. for (row = 0; row < (int) height; row++)
  352. {
  353. #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
  354. if (packed_bitmap) {
  355. for (i = 0; i < (int) row_bytes; i++)
  356. /* png supports this format natively so no conversion is needed */
  357. *pix_ptr++ = get_data (pnm_file, 8);
  358. } else
  359. #endif
  360. {
  361. for (col = 0; col < (int) width; col++)
  362. {
  363. for (i = 0; i < (channels - alpha_present); i++)
  364. {
  365. if (raw)
  366. *pix_ptr++ = get_data (pnm_file, bit_depth);
  367. else
  368. if (bit_depth <= 8)
  369. *pix_ptr++ = get_value (pnm_file, bit_depth);
  370. else
  371. {
  372. tmp16 = get_value (pnm_file, bit_depth);
  373. *pix_ptr = (png_byte) ((tmp16 >> 8) & 0xFF);
  374. pix_ptr++;
  375. *pix_ptr = (png_byte) (tmp16 & 0xFF);
  376. pix_ptr++;
  377. }
  378. }
  379. if (alpha) /* read alpha-channel from pgm file */
  380. {
  381. if (alpha_raw)
  382. *pix_ptr++ = get_data (alpha_file, alpha_depth);
  383. else
  384. if (alpha_depth <= 8)
  385. *pix_ptr++ = get_value (alpha_file, bit_depth);
  386. else
  387. {
  388. tmp16 = get_value (alpha_file, bit_depth);
  389. *pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF);
  390. *pix_ptr++ = (png_byte) (tmp16 & 0xFF);
  391. }
  392. } /* if alpha */
  393. } /* if packed_bitmap */
  394. } /* end for col */
  395. } /* end for row */
  396. /* prepare the standard PNG structures */
  397. png_ptr = png_create_write_struct (png_get_libpng_ver(NULL), NULL, NULL,
  398. NULL);
  399. if (!png_ptr)
  400. {
  401. free (png_pixels);
  402. png_pixels = NULL;
  403. return FALSE;
  404. }
  405. info_ptr = png_create_info_struct (png_ptr);
  406. if (!info_ptr)
  407. {
  408. png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
  409. free (png_pixels);
  410. png_pixels = NULL;
  411. return FALSE;
  412. }
  413. #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
  414. if (packed_bitmap == TRUE)
  415. {
  416. png_set_packing (png_ptr);
  417. png_set_invert_mono (png_ptr);
  418. }
  419. #endif
  420. /* setjmp() must be called in every function that calls a PNG-reading libpng function */
  421. if (setjmp (png_jmpbuf(png_ptr)))
  422. {
  423. png_destroy_write_struct (&png_ptr, &info_ptr);
  424. free (png_pixels);
  425. png_pixels = NULL;
  426. return FALSE;
  427. }
  428. /* initialize the png structure */
  429. png_init_io (png_ptr, png_file);
  430. /* we're going to write more or less the same PNG as the input file */
  431. png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type,
  432. (!interlace) ? PNG_INTERLACE_NONE : PNG_INTERLACE_ADAM7,
  433. PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  434. /* write the file header information */
  435. png_write_info (png_ptr, info_ptr);
  436. /* if needed we will allocate memory for an new array of row-pointers */
  437. if (row_pointers == (unsigned char**) NULL)
  438. {
  439. if ((row_pointers = (png_byte **)
  440. malloc (height * sizeof (png_bytep))) == NULL)
  441. {
  442. png_destroy_write_struct (&png_ptr, &info_ptr);
  443. free (png_pixels);
  444. png_pixels = NULL;
  445. return FALSE;
  446. }
  447. }
  448. /* set the individual row_pointers to point at the correct offsets */
  449. for (i = 0; i < (int) height; i++)
  450. row_pointers[i] = png_pixels + i * row_bytes;
  451. /* write out the entire image data in one call */
  452. png_write_image (png_ptr, row_pointers);
  453. /* write the additional chunks to the PNG file (not really needed) */
  454. png_write_end (png_ptr, info_ptr);
  455. /* clean up after the write, and free any memory allocated */
  456. png_destroy_write_struct (&png_ptr, &info_ptr);
  457. if (row_pointers != (unsigned char**) NULL)
  458. free (row_pointers);
  459. if (png_pixels != (unsigned char*) NULL)
  460. free (png_pixels);
  461. return TRUE;
  462. } /* end of pnm2png */
  463. /*
  464. * get_token() - gets the first string after whitespace
  465. */
  466. void get_token(FILE *pnm_file, char *token)
  467. {
  468. int i = 0;
  469. int ret;
  470. /* remove white-space and comment lines */
  471. do
  472. {
  473. ret = fgetc(pnm_file);
  474. if (ret == '#') {
  475. /* the rest of this line is a comment */
  476. do
  477. {
  478. ret = fgetc(pnm_file);
  479. }
  480. while ((ret != '\n') && (ret != '\r') && (ret != EOF));
  481. }
  482. if (ret == EOF) break;
  483. token[i] = (unsigned char) ret;
  484. }
  485. while ((token[i] == '\n') || (token[i] == '\r') || (token[i] == ' '));
  486. /* read string */
  487. do
  488. {
  489. ret = fgetc(pnm_file);
  490. if (ret == EOF) break;
  491. i++;
  492. token[i] = (unsigned char) ret;
  493. }
  494. while ((token[i] != '\n') && (token[i] != '\r') && (token[i] != ' '));
  495. token[i] = '\0';
  496. return;
  497. }
  498. /*
  499. * get_data() - takes first byte and converts into next pixel value,
  500. * taking as much bits as defined by bit-depth and
  501. * using the bit-depth to fill up a byte (0Ah -> AAh)
  502. */
  503. png_uint_32 get_data (FILE *pnm_file, int depth)
  504. {
  505. static int bits_left = 0;
  506. static int old_value = 0;
  507. static int mask = 0;
  508. int i;
  509. png_uint_32 ret_value;
  510. if (mask == 0)
  511. for (i = 0; i < depth; i++)
  512. mask = (mask >> 1) | 0x80;
  513. if (bits_left <= 0)
  514. {
  515. old_value = fgetc (pnm_file);
  516. bits_left = 8;
  517. }
  518. ret_value = old_value & mask;
  519. for (i = 1; i < (8 / depth); i++)
  520. ret_value = ret_value || (ret_value >> depth);
  521. old_value = (old_value << depth) & 0xFF;
  522. bits_left -= depth;
  523. return ret_value;
  524. }
  525. /*
  526. * get_value() - takes first (numeric) string and converts into number,
  527. * using the bit-depth to fill up a byte (0Ah -> AAh)
  528. */
  529. png_uint_32 get_value (FILE *pnm_file, int depth)
  530. {
  531. static png_uint_32 mask = 0;
  532. png_byte token[16];
  533. unsigned long ul_ret_value;
  534. png_uint_32 ret_value;
  535. int i = 0;
  536. if (mask == 0)
  537. for (i = 0; i < depth; i++)
  538. mask = (mask << 1) | 0x01;
  539. get_token (pnm_file, (char *) token);
  540. sscanf ((const char *) token, "%lu", &ul_ret_value);
  541. ret_value = (png_uint_32) ul_ret_value;
  542. ret_value &= mask;
  543. if (depth < 8)
  544. for (i = 0; i < (8 / depth); i++)
  545. ret_value = (ret_value << depth) || ret_value;
  546. return ret_value;
  547. }
  548. /* end of source */