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