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
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 */
59
#include "png.h" /* libpng header; includes zlib.h and setjmp.h */
60
#include "writepng.h" /* typedefs, common macros, public prototypes */
65
static void writepng_error_handler(png_structp png_ptr, png_const_charp msg);
69
void writepng_version_info(void)
71
fprintf(stderr, " Compiled with libpng %s; using libpng %s.\n",
72
PNG_LIBPNG_VER_STRING, png_libpng_ver);
73
fprintf(stderr, " Compiled with zlib %s; using zlib %s.\n",
74
ZLIB_VERSION, zlib_version);
80
/* returns 0 for success, 2 for libpng problem, 4 for out of memory, 11 for
81
* unexpected pnmtype; note that outfile might be stdout */
83
int writepng_init(mainprog_info *mainprog_ptr)
85
png_structp png_ptr; /* note: temporary variables! */
87
int color_type, interlace_type;
90
/* could also replace libpng warning-handler (final NULL), but no need: */
92
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, mainprog_ptr,
93
writepng_error_handler, NULL);
95
return 4; /* out of memory */
97
info_ptr = png_create_info_struct(png_ptr);
99
png_destroy_write_struct(&png_ptr, NULL);
100
return 4; /* out of memory */
104
/* setjmp() must be called in every function that calls a PNG-writing
105
* libpng function, unless an alternate error handler was installed--
106
* but compatible error handlers must either use longjmp() themselves
107
* (as in this program) or exit immediately, so here we go: */
109
if (setjmp(mainprog_ptr->jmpbuf)) {
110
png_destroy_write_struct(&png_ptr, &info_ptr);
115
/* make sure outfile is (re)opened in BINARY mode */
117
png_init_io(png_ptr, mainprog_ptr->outfile);
120
/* set the compression levels--in general, always want to leave filtering
121
* turned on (except for palette images) and allow all of the filters,
122
* which is the default; want 32K zlib window, unless entire image buffer
123
* is 16K or smaller (unknown here)--also the default; usually want max
124
* compression (NOT the default); and remaining compression flags should
127
png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
129
>> this is default for no filtering; Z_FILTERED is default otherwise:
130
png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
131
>> these are all defaults:
132
png_set_compression_mem_level(png_ptr, 8);
133
png_set_compression_window_bits(png_ptr, 15);
134
png_set_compression_method(png_ptr, 8);
138
/* set the image parameters appropriately */
140
if (mainprog_ptr->pnmtype == 5)
141
color_type = PNG_COLOR_TYPE_GRAY;
142
else if (mainprog_ptr->pnmtype == 6)
143
color_type = PNG_COLOR_TYPE_RGB;
144
else if (mainprog_ptr->pnmtype == 8)
145
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
147
png_destroy_write_struct(&png_ptr, &info_ptr);
151
interlace_type = mainprog_ptr->interlaced? PNG_INTERLACE_ADAM7 :
154
png_set_IHDR(png_ptr, info_ptr, mainprog_ptr->width, mainprog_ptr->height,
155
mainprog_ptr->sample_depth, color_type, interlace_type,
156
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
158
if (mainprog_ptr->gamma > 0.0)
159
png_set_gAMA(png_ptr, info_ptr, mainprog_ptr->gamma);
161
if (mainprog_ptr->have_bg) { /* we know it's RGBA, not gray+alpha */
162
png_color_16 background;
164
background.red = mainprog_ptr->bg_red;
165
background.green = mainprog_ptr->bg_green;
166
background.blue = mainprog_ptr->bg_blue;
167
png_set_bKGD(png_ptr, info_ptr, &background);
170
if (mainprog_ptr->have_time) {
173
png_convert_from_time_t(&modtime, mainprog_ptr->modtime);
174
png_set_tIME(png_ptr, info_ptr, &modtime);
177
if (mainprog_ptr->have_text) {
181
if (mainprog_ptr->have_text & TEXT_TITLE) {
182
text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
183
text[num_text].key = "Title";
184
text[num_text].text = mainprog_ptr->title;
187
if (mainprog_ptr->have_text & TEXT_AUTHOR) {
188
text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
189
text[num_text].key = "Author";
190
text[num_text].text = mainprog_ptr->author;
193
if (mainprog_ptr->have_text & TEXT_DESC) {
194
text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
195
text[num_text].key = "Description";
196
text[num_text].text = mainprog_ptr->desc;
199
if (mainprog_ptr->have_text & TEXT_COPY) {
200
text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
201
text[num_text].key = "Copyright";
202
text[num_text].text = mainprog_ptr->copyright;
205
if (mainprog_ptr->have_text & TEXT_EMAIL) {
206
text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
207
text[num_text].key = "E-mail";
208
text[num_text].text = mainprog_ptr->email;
211
if (mainprog_ptr->have_text & TEXT_URL) {
212
text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
213
text[num_text].key = "URL";
214
text[num_text].text = mainprog_ptr->url;
217
png_set_text(png_ptr, info_ptr, text, num_text);
221
/* write all chunks up to (but not including) first IDAT */
223
png_write_info(png_ptr, info_ptr);
226
/* if we wanted to write any more text info *after* the image data, we
227
* would set up text struct(s) here and call png_set_text() again, with
228
* just the new data; png_set_tIME() could also go here, but it would
229
* have no effect since we already called it above (only one tIME chunk
233
/* set up the transformations: for now, just pack low-bit-depth pixels
234
* into bytes (one, two or four pixels per byte) */
236
png_set_packing(png_ptr);
237
/* png_set_shift(png_ptr, &sig_bit); to scale low-bit-depth values */
240
/* make sure we save our pointers for use in writepng_encode_image() */
242
mainprog_ptr->png_ptr = png_ptr;
243
mainprog_ptr->info_ptr = info_ptr;
246
/* OK, that's all we need to do for now; return happy */
255
/* returns 0 for success, 2 for libpng (longjmp) problem */
257
int writepng_encode_image(mainprog_info *mainprog_ptr)
259
png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
260
png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
263
/* as always, setjmp() must be called in every function that calls a
264
* PNG-writing libpng function */
266
if (setjmp(mainprog_ptr->jmpbuf)) {
267
png_destroy_write_struct(&png_ptr, &info_ptr);
268
mainprog_ptr->png_ptr = NULL;
269
mainprog_ptr->info_ptr = NULL;
274
/* and now we just write the whole image; libpng takes care of interlacing
277
png_write_image(png_ptr, mainprog_ptr->row_pointers);
280
/* since that's it, we also close out the end of the PNG file now--if we
281
* had any text or time info to write after the IDATs, second argument
282
* would be info_ptr, but we optimize slightly by sending NULL pointer: */
284
png_write_end(png_ptr, NULL);
293
/* returns 0 if succeeds, 2 if libpng problem */
295
int writepng_encode_row(mainprog_info *mainprog_ptr) /* NON-interlaced only! */
297
png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
298
png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
301
/* as always, setjmp() must be called in every function that calls a
302
* PNG-writing libpng function */
304
if (setjmp(mainprog_ptr->jmpbuf)) {
305
png_destroy_write_struct(&png_ptr, &info_ptr);
306
mainprog_ptr->png_ptr = NULL;
307
mainprog_ptr->info_ptr = NULL;
312
/* image_data points at our one row of image data */
314
png_write_row(png_ptr, mainprog_ptr->image_data);
323
/* returns 0 if succeeds, 2 if libpng problem */
325
int writepng_encode_finish(mainprog_info *mainprog_ptr) /* NON-interlaced! */
327
png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
328
png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
331
/* as always, setjmp() must be called in every function that calls a
332
* PNG-writing libpng function */
334
if (setjmp(mainprog_ptr->jmpbuf)) {
335
png_destroy_write_struct(&png_ptr, &info_ptr);
336
mainprog_ptr->png_ptr = NULL;
337
mainprog_ptr->info_ptr = NULL;
342
/* close out PNG file; if we had any text or time info to write after
343
* the IDATs, second argument would be info_ptr: */
345
png_write_end(png_ptr, NULL);
354
void writepng_cleanup(mainprog_info *mainprog_ptr)
356
png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
357
png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
359
if (png_ptr && info_ptr)
360
png_destroy_write_struct(&png_ptr, &info_ptr);
367
static void writepng_error_handler(png_structp png_ptr, png_const_charp msg)
369
mainprog_info *mainprog_ptr;
371
/* This function, aside from the extra step of retrieving the "error
372
* pointer" (below) and the fact that it exists within the application
373
* rather than within libpng, is essentially identical to libpng's
374
* default error handler. The second point is critical: since both
375
* setjmp() and longjmp() are called from the same code, they are
376
* guaranteed to have compatible notions of how big a jmp_buf is,
377
* regardless of whether _BSD_SOURCE or anything else has (or has not)
380
fprintf(stderr, "writepng libpng error: %s\n", msg);
383
mainprog_ptr = png_get_error_ptr(png_ptr);
384
if (mainprog_ptr == NULL) { /* we are completely hosed now */
386
"writepng severe error: jmpbuf not recoverable; terminating.\n");
391
longjmp(mainprog_ptr->jmpbuf, 1);