pnm2png.c 16 KB

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