writepng.c 13 KB

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