writepng.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*---------------------------------------------------------------------------
  2. wpng - simple PNG-writing program writepng.c
  3. ---------------------------------------------------------------------------
  4. Copyright (c) 1998-2007 Greg Roelofs. All rights reserved.
  5. This software is provided "as is," without warranty of any kind,
  6. express or implied. In no event shall the author or contributors
  7. be held liable for any damages arising in any way from the use of
  8. this software.
  9. The contents of this file are DUAL-LICENSED. You may modify and/or
  10. redistribute this software according to the terms of one of the
  11. following two licenses (at your option):
  12. LICENSE 1 ("BSD-like with advertising clause"):
  13. Permission is granted to anyone to use this software for any purpose,
  14. including commercial applications, and to alter it and redistribute
  15. it freely, subject to the following restrictions:
  16. 1. Redistributions of source code must retain the above copyright
  17. notice, disclaimer, and this list of conditions.
  18. 2. Redistributions in binary form must reproduce the above copyright
  19. notice, disclaimer, and this list of conditions in the documenta-
  20. tion and/or other materials provided with the distribution.
  21. 3. All advertising materials mentioning features or use of this
  22. software must display the following acknowledgment:
  23. This product includes software developed by Greg Roelofs
  24. and contributors for the book, "PNG: The Definitive Guide,"
  25. published by O'Reilly and Associates.
  26. LICENSE 2 (GNU GPL v2 or later):
  27. This program is free software; you can redistribute it and/or modify
  28. it under the terms of the GNU General Public License as published by
  29. the Free Software Foundation; either version 2 of the License, or
  30. (at your option) any later version.
  31. This program is distributed in the hope that it will be useful,
  32. but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. GNU General Public License for more details.
  35. You should have received a copy of the GNU General Public License
  36. along with this program; if not, write to the Free Software Foundation,
  37. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  38. ---------------------------------------------------------------------------*/
  39. #include <stdlib.h> /* for exit() prototype */
  40. #include <zlib.h>
  41. #include "png.h" /* libpng header, includes setjmp.h */
  42. #include "writepng.h" /* typedefs, common macros, public prototypes */
  43. /* local prototype */
  44. static void writepng_error_handler(png_structp png_ptr, png_const_charp msg);
  45. void writepng_version_info(void)
  46. {
  47. fprintf(stderr, " Compiled with libpng %s; using libpng %s.\n",
  48. PNG_LIBPNG_VER_STRING, png_libpng_ver);
  49. fprintf(stderr, " Compiled with zlib %s; using zlib %s.\n",
  50. ZLIB_VERSION, zlib_version);
  51. }
  52. /* returns 0 for success, 2 for libpng problem, 4 for out of memory, 11 for
  53. * unexpected pnmtype; note that outfile might be stdout */
  54. int writepng_init(mainprog_info *mainprog_ptr)
  55. {
  56. png_structp png_ptr; /* note: temporary variables! */
  57. png_infop info_ptr;
  58. int color_type, interlace_type;
  59. /* could also replace libpng warning-handler (final NULL), but no need: */
  60. png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, mainprog_ptr,
  61. writepng_error_handler, NULL);
  62. if (!png_ptr)
  63. return 4; /* out of memory */
  64. info_ptr = png_create_info_struct(png_ptr);
  65. if (!info_ptr) {
  66. png_destroy_write_struct(&png_ptr, NULL);
  67. return 4; /* out of memory */
  68. }
  69. /* setjmp() must be called in every function that calls a PNG-writing
  70. * libpng function, unless an alternate error handler was installed--
  71. * but compatible error handlers must either use longjmp() themselves
  72. * (as in this program) or some other method to return control to
  73. * application code, so here we go: */
  74. if (setjmp(mainprog_ptr->jmpbuf)) {
  75. png_destroy_write_struct(&png_ptr, &info_ptr);
  76. return 2;
  77. }
  78. /* make sure outfile is (re)opened in BINARY mode */
  79. png_init_io(png_ptr, mainprog_ptr->outfile);
  80. /* set the compression levels--in general, always want to leave filtering
  81. * turned on (except for palette images) and allow all of the filters,
  82. * which is the default; want 32K zlib window, unless entire image buffer
  83. * is 16K or smaller (unknown here)--also the default; usually want max
  84. * compression (NOT the default); and remaining compression flags should
  85. * be left alone */
  86. png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
  87. /*
  88. >> this is default for no filtering; Z_FILTERED is default otherwise:
  89. png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
  90. >> these are all defaults:
  91. png_set_compression_mem_level(png_ptr, 8);
  92. png_set_compression_window_bits(png_ptr, 15);
  93. png_set_compression_method(png_ptr, 8);
  94. */
  95. /* set the image parameters appropriately */
  96. if (mainprog_ptr->pnmtype == 5)
  97. color_type = PNG_COLOR_TYPE_GRAY;
  98. else if (mainprog_ptr->pnmtype == 6)
  99. color_type = PNG_COLOR_TYPE_RGB;
  100. else if (mainprog_ptr->pnmtype == 8)
  101. color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  102. else {
  103. png_destroy_write_struct(&png_ptr, &info_ptr);
  104. return 11;
  105. }
  106. interlace_type = mainprog_ptr->interlaced? PNG_INTERLACE_ADAM7 :
  107. PNG_INTERLACE_NONE;
  108. png_set_IHDR(png_ptr, info_ptr, mainprog_ptr->width, mainprog_ptr->height,
  109. mainprog_ptr->sample_depth, color_type, interlace_type,
  110. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  111. if (mainprog_ptr->gamma > 0.0)
  112. png_set_gAMA(png_ptr, info_ptr, mainprog_ptr->gamma);
  113. if (mainprog_ptr->have_bg) { /* we know it's RGBA, not gray+alpha */
  114. png_color_16 background;
  115. background.red = mainprog_ptr->bg_red;
  116. background.green = mainprog_ptr->bg_green;
  117. background.blue = mainprog_ptr->bg_blue;
  118. png_set_bKGD(png_ptr, info_ptr, &background);
  119. }
  120. if (mainprog_ptr->have_time) {
  121. png_time modtime;
  122. png_convert_from_time_t(&modtime, mainprog_ptr->modtime);
  123. png_set_tIME(png_ptr, info_ptr, &modtime);
  124. }
  125. if (mainprog_ptr->have_text) {
  126. png_text text[6];
  127. int num_text = 0;
  128. if (mainprog_ptr->have_text & TEXT_TITLE) {
  129. text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
  130. text[num_text].key = "Title";
  131. text[num_text].text = mainprog_ptr->title;
  132. ++num_text;
  133. }
  134. if (mainprog_ptr->have_text & TEXT_AUTHOR) {
  135. text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
  136. text[num_text].key = "Author";
  137. text[num_text].text = mainprog_ptr->author;
  138. ++num_text;
  139. }
  140. if (mainprog_ptr->have_text & TEXT_DESC) {
  141. text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
  142. text[num_text].key = "Description";
  143. text[num_text].text = mainprog_ptr->desc;
  144. ++num_text;
  145. }
  146. if (mainprog_ptr->have_text & TEXT_COPY) {
  147. text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
  148. text[num_text].key = "Copyright";
  149. text[num_text].text = mainprog_ptr->copyright;
  150. ++num_text;
  151. }
  152. if (mainprog_ptr->have_text & TEXT_EMAIL) {
  153. text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
  154. text[num_text].key = "E-mail";
  155. text[num_text].text = mainprog_ptr->email;
  156. ++num_text;
  157. }
  158. if (mainprog_ptr->have_text & TEXT_URL) {
  159. text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
  160. text[num_text].key = "URL";
  161. text[num_text].text = mainprog_ptr->url;
  162. ++num_text;
  163. }
  164. png_set_text(png_ptr, info_ptr, text, num_text);
  165. }
  166. /* write all chunks up to (but not including) first IDAT */
  167. png_write_info(png_ptr, info_ptr);
  168. /* if we wanted to write any more text info *after* the image data, we
  169. * would set up text struct(s) here and call png_set_text() again, with
  170. * just the new data; png_set_tIME() could also go here, but it would
  171. * have no effect since we already called it above (only one tIME chunk
  172. * allowed) */
  173. /* set up the transformations: for now, just pack low-bit-depth pixels
  174. * into bytes (one, two or four pixels per byte) */
  175. png_set_packing(png_ptr);
  176. /* png_set_shift(png_ptr, &sig_bit); to scale low-bit-depth values */
  177. /* make sure we save our pointers for use in writepng_encode_image() */
  178. mainprog_ptr->png_ptr = png_ptr;
  179. mainprog_ptr->info_ptr = info_ptr;
  180. /* OK, that's all we need to do for now; return happy */
  181. return 0;
  182. }
  183. /* returns 0 for success, 2 for libpng (longjmp) problem */
  184. int writepng_encode_image(mainprog_info *mainprog_ptr)
  185. {
  186. png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
  187. png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
  188. /* as always, setjmp() must be called in every function that calls a
  189. * PNG-writing libpng function */
  190. if (setjmp(mainprog_ptr->jmpbuf)) {
  191. png_destroy_write_struct(&png_ptr, &info_ptr);
  192. mainprog_ptr->png_ptr = NULL;
  193. mainprog_ptr->info_ptr = NULL;
  194. return 2;
  195. }
  196. /* and now we just write the whole image; libpng takes care of interlacing
  197. * for us */
  198. png_write_image(png_ptr, mainprog_ptr->row_pointers);
  199. /* since that's it, we also close out the end of the PNG file now--if we
  200. * had any text or time info to write after the IDATs, second argument
  201. * would be info_ptr, but we optimize slightly by sending NULL pointer: */
  202. png_write_end(png_ptr, NULL);
  203. return 0;
  204. }
  205. /* returns 0 if succeeds, 2 if libpng problem */
  206. int writepng_encode_row(mainprog_info *mainprog_ptr) /* NON-interlaced only! */
  207. {
  208. png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
  209. png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
  210. /* as always, setjmp() must be called in every function that calls a
  211. * PNG-writing libpng function */
  212. if (setjmp(mainprog_ptr->jmpbuf)) {
  213. png_destroy_write_struct(&png_ptr, &info_ptr);
  214. mainprog_ptr->png_ptr = NULL;
  215. mainprog_ptr->info_ptr = NULL;
  216. return 2;
  217. }
  218. /* image_data points at our one row of image data */
  219. png_write_row(png_ptr, mainprog_ptr->image_data);
  220. return 0;
  221. }
  222. /* returns 0 if succeeds, 2 if libpng problem */
  223. int writepng_encode_finish(mainprog_info *mainprog_ptr) /* NON-interlaced! */
  224. {
  225. png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
  226. png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
  227. /* as always, setjmp() must be called in every function that calls a
  228. * PNG-writing libpng function */
  229. if (setjmp(mainprog_ptr->jmpbuf)) {
  230. png_destroy_write_struct(&png_ptr, &info_ptr);
  231. mainprog_ptr->png_ptr = NULL;
  232. mainprog_ptr->info_ptr = NULL;
  233. return 2;
  234. }
  235. /* close out PNG file; if we had any text or time info to write after
  236. * the IDATs, second argument would be info_ptr: */
  237. png_write_end(png_ptr, NULL);
  238. return 0;
  239. }
  240. void writepng_cleanup(mainprog_info *mainprog_ptr)
  241. {
  242. png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
  243. png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
  244. if (png_ptr && info_ptr)
  245. png_destroy_write_struct(&png_ptr, &info_ptr);
  246. }
  247. static void writepng_error_handler(png_structp png_ptr, png_const_charp msg)
  248. {
  249. mainprog_info *mainprog_ptr;
  250. /* This function, aside from the extra step of retrieving the "error
  251. * pointer" (below) and the fact that it exists within the application
  252. * rather than within libpng, is essentially identical to libpng's
  253. * default error handler. The second point is critical: since both
  254. * setjmp() and longjmp() are called from the same code, they are
  255. * guaranteed to have compatible notions of how big a jmp_buf is,
  256. * regardless of whether _BSD_SOURCE or anything else has (or has not)
  257. * been defined. */
  258. fprintf(stderr, "writepng libpng error: %s\n", msg);
  259. fflush(stderr);
  260. mainprog_ptr = png_get_error_ptr(png_ptr);
  261. if (mainprog_ptr == NULL) { /* we are completely hosed now */
  262. fprintf(stderr,
  263. "writepng severe error: jmpbuf not recoverable; terminating.\n");
  264. fflush(stderr);
  265. exit(99);
  266. }
  267. /* Now we have our data structure we can use the information in it
  268. * to return control to our own higher level code (all the points
  269. * where 'setjmp' is called in this file.) This will work with other
  270. * error handling mechanisms as well - libpng always calls png_error
  271. * when it can proceed no further, thus, so long as the error handler
  272. * is intercepted, application code can do its own error recovery.
  273. */
  274. longjmp(mainprog_ptr->jmpbuf, 1);
  275. }