Visualization Library 2.0.0-b5

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
writepng.c
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------
2 
3  wpng - simple PNG-writing program writepng.c
4 
5  ---------------------------------------------------------------------------
6 
7  Copyright (c) 1998-2007 Greg Roelofs. All rights reserved.
8 
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.
13 
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):
17 
18 
19  LICENSE 1 ("BSD-like with advertising clause"):
20 
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:
24 
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:
32 
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.
36 
37 
38  LICENSE 2 (GNU GPL v2 or later):
39 
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.
44 
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.
49 
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
53 
54  ---------------------------------------------------------------------------*/
55 
56 
57 #include <stdlib.h> /* for exit() prototype */
58 
59 #include "png.h" /* libpng header; includes zlib.h and setjmp.h */
60 #include "writepng.h" /* typedefs, common macros, public prototypes */
61 
62 
63 /* local prototype */
64 
65 static void writepng_error_handler(png_structp png_ptr, png_const_charp msg);
66 
67 
68 
70 {
71  fprintf(stderr, " Compiled with libpng %s; using libpng %s.\n",
73  fprintf(stderr, " Compiled with zlib %s; using zlib %s.\n",
75 }
76 
77 
78 
79 
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 */
82 
83 int writepng_init(mainprog_info *mainprog_ptr)
84 {
85  png_structp png_ptr; /* note: temporary variables! */
87  int color_type, interlace_type;
88 
89 
90  /* could also replace libpng warning-handler (final NULL), but no need: */
91 
92  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, mainprog_ptr,
93  writepng_error_handler, NULL);
94  if (!png_ptr)
95  return 4; /* out of memory */
96 
97  info_ptr = png_create_info_struct(png_ptr);
98  if (!info_ptr) {
99  png_destroy_write_struct(&png_ptr, NULL);
100  return 4; /* out of memory */
101  }
102 
103 
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: */
108 
109  if (setjmp(mainprog_ptr->jmpbuf)) {
110  png_destroy_write_struct(&png_ptr, &info_ptr);
111  return 2;
112  }
113 
114 
115  /* make sure outfile is (re)opened in BINARY mode */
116 
117  png_init_io(png_ptr, mainprog_ptr->outfile);
118 
119 
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
125  * be left alone */
126 
128 /*
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);
135  */
136 
137 
138  /* set the image parameters appropriately */
139 
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;
146  else {
147  png_destroy_write_struct(&png_ptr, &info_ptr);
148  return 11;
149  }
150 
151  interlace_type = mainprog_ptr->interlaced? PNG_INTERLACE_ADAM7 :
153 
154  png_set_IHDR(png_ptr, info_ptr, mainprog_ptr->width, mainprog_ptr->height,
155  mainprog_ptr->sample_depth, color_type, interlace_type,
157 
158  if (mainprog_ptr->gamma > 0.0)
159  png_set_gAMA(png_ptr, info_ptr, mainprog_ptr->gamma);
160 
161  if (mainprog_ptr->have_bg) { /* we know it's RGBA, not gray+alpha */
163 
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);
168  }
169 
170  if (mainprog_ptr->have_time) {
171  png_time modtime;
172 
173  png_convert_from_time_t(&modtime, mainprog_ptr->modtime);
174  png_set_tIME(png_ptr, info_ptr, &modtime);
175  }
176 
177  if (mainprog_ptr->have_text) {
178  png_text text[6];
179  int num_text = 0;
180 
181  if (mainprog_ptr->have_text & TEXT_TITLE) {
183  text[num_text].key = "Title";
184  text[num_text].text = mainprog_ptr->title;
185  ++num_text;
186  }
187  if (mainprog_ptr->have_text & TEXT_AUTHOR) {
189  text[num_text].key = "Author";
190  text[num_text].text = mainprog_ptr->author;
191  ++num_text;
192  }
193  if (mainprog_ptr->have_text & TEXT_DESC) {
195  text[num_text].key = "Description";
196  text[num_text].text = mainprog_ptr->desc;
197  ++num_text;
198  }
199  if (mainprog_ptr->have_text & TEXT_COPY) {
201  text[num_text].key = "Copyright";
202  text[num_text].text = mainprog_ptr->copyright;
203  ++num_text;
204  }
205  if (mainprog_ptr->have_text & TEXT_EMAIL) {
207  text[num_text].key = "E-mail";
208  text[num_text].text = mainprog_ptr->email;
209  ++num_text;
210  }
211  if (mainprog_ptr->have_text & TEXT_URL) {
213  text[num_text].key = "URL";
214  text[num_text].text = mainprog_ptr->url;
215  ++num_text;
216  }
217  png_set_text(png_ptr, info_ptr, text, num_text);
218  }
219 
220 
221  /* write all chunks up to (but not including) first IDAT */
222 
223  png_write_info(png_ptr, info_ptr);
224 
225 
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
230  * allowed) */
231 
232 
233  /* set up the transformations: for now, just pack low-bit-depth pixels
234  * into bytes (one, two or four pixels per byte) */
235 
236  png_set_packing(png_ptr);
237 /* png_set_shift(png_ptr, &sig_bit); to scale low-bit-depth values */
238 
239 
240  /* make sure we save our pointers for use in writepng_encode_image() */
241 
242  mainprog_ptr->png_ptr = png_ptr;
243  mainprog_ptr->info_ptr = info_ptr;
244 
245 
246  /* OK, that's all we need to do for now; return happy */
247 
248  return 0;
249 }
250 
251 
252 
253 
254 
255 /* returns 0 for success, 2 for libpng (longjmp) problem */
256 
258 {
259  png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
260  png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
261 
262 
263  /* as always, setjmp() must be called in every function that calls a
264  * PNG-writing libpng function */
265 
266  if (setjmp(mainprog_ptr->jmpbuf)) {
268  mainprog_ptr->png_ptr = NULL;
269  mainprog_ptr->info_ptr = NULL;
270  return 2;
271  }
272 
273 
274  /* and now we just write the whole image; libpng takes care of interlacing
275  * for us */
276 
277  png_write_image(png_ptr, mainprog_ptr->row_pointers);
278 
279 
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: */
283 
284  png_write_end(png_ptr, NULL);
285 
286  return 0;
287 }
288 
289 
290 
291 
292 
293 /* returns 0 if succeeds, 2 if libpng problem */
294 
295 int writepng_encode_row(mainprog_info *mainprog_ptr) /* NON-interlaced only! */
296 {
297  png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
298  png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
299 
300 
301  /* as always, setjmp() must be called in every function that calls a
302  * PNG-writing libpng function */
303 
304  if (setjmp(mainprog_ptr->jmpbuf)) {
306  mainprog_ptr->png_ptr = NULL;
307  mainprog_ptr->info_ptr = NULL;
308  return 2;
309  }
310 
311 
312  /* image_data points at our one row of image data */
313 
314  png_write_row(png_ptr, mainprog_ptr->image_data);
315 
316  return 0;
317 }
318 
319 
320 
321 
322 
323 /* returns 0 if succeeds, 2 if libpng problem */
324 
325 int writepng_encode_finish(mainprog_info *mainprog_ptr) /* NON-interlaced! */
326 {
327  png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
328  png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
329 
330 
331  /* as always, setjmp() must be called in every function that calls a
332  * PNG-writing libpng function */
333 
334  if (setjmp(mainprog_ptr->jmpbuf)) {
336  mainprog_ptr->png_ptr = NULL;
337  mainprog_ptr->info_ptr = NULL;
338  return 2;
339  }
340 
341 
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: */
344 
345  png_write_end(png_ptr, NULL);
346 
347  return 0;
348 }
349 
350 
351 
352 
353 
354 void writepng_cleanup(mainprog_info *mainprog_ptr)
355 {
356  png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
357  png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
358 
359  if (png_ptr && info_ptr)
361 }
362 
363 
364 
365 
366 
367 static void writepng_error_handler(png_structp png_ptr, png_const_charp msg)
368 {
369  mainprog_info *mainprog_ptr;
370 
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)
378  * been defined. */
379 
380  fprintf(stderr, "writepng libpng error: %s\n", msg);
381  fflush(stderr);
382 
383  mainprog_ptr = png_get_error_ptr(png_ptr);
384  if (mainprog_ptr == NULL) { /* we are completely hosed now */
385  fprintf(stderr,
386  "writepng severe error: jmpbuf not recoverable; terminating.\n");
387  fflush(stderr);
388  exit(99);
389  }
390 
391  longjmp(mainprog_ptr->jmpbuf, 1);
392 }
char * email
Definition: writepng.h:105
void PNGAPI png_set_packing(png_structp png_ptr)
Definition: pngtrans.c:49
char * url
Definition: writepng.h:106
PNG_CONST char png_libpng_ver[18]
Definition: png.c:28
void PNGAPI png_set_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_type, int compression_type, int filter_type)
Definition: pngset.c:227
void PNGAPI png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
Definition: pngset.c:26
void PNGAPI png_write_image(png_structp png_ptr, png_bytepp image)
Definition: pngwrite.c:759
png_charp text
Definition: png.h:505
png_infop PNGAPI png_create_info_struct(png_structp png_ptr)
Definition: png.c:253
void PNGAPI png_set_compression_level(png_structp png_ptr, int level)
Definition: pngwrite.c:1305
int compression
Definition: png.h:499
#define NULL
Definition: ftobjs.h:61
#define TEXT_DESC
Definition: writepng.h:74
FILE * outfile
Definition: writepng.h:96
#define PNG_INTERLACE_NONE
Definition: png.h:887
#define PNG_COLOR_TYPE_RGB
Definition: png.h:870
#define TEXT_TITLE
Definition: writepng.h:72
int writepng_encode_image(mainprog_info *mainprog_ptr)
Definition: writepng.c:257
png_voidp PNGAPI png_get_error_ptr(png_structp png_ptr)
Definition: pngerror.c:377
png_infop png_color_16p * background
Definition: png.h:2089
#define PNG_INTERLACE_ADAM7
Definition: png.h:888
png_uint_16 blue
Definition: png.h:441
#define TEXT_URL
Definition: writepng.h:77
#define PNG_LIBPNG_VER_STRING
Definition: png.h:273
void PNGAPI png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
Definition: pngset.c:837
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:871
PNG_CONST char FAR * png_const_charp
Definition: pngconf.h:1333
#define Z_BEST_COMPRESSION
Definition: zlib.h:147
void PNGAPI png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
Definition: pngset.c:116
char * author
Definition: writepng.h:102
#define zlib_version
Definition: zlib.h:210
#define ZLIB_VERSION
Definition: zlib.h:40
int sample_depth
Definition: writepng.h:109
void writepng_cleanup(mainprog_info *mainprog_ptr)
Definition: writepng.c:354
png_charp key
Definition: png.h:504
double gamma
Definition: writepng.h:91
png_uint_16 green
Definition: png.h:440
#define PNG_TEXT_COMPRESSION_NONE
Definition: png.h:525
png_struct FAR * png_structp
Definition: png.h:973
void PNGAPI png_write_info(png_structp png_ptr, png_infop info_ptr)
Definition: pngwrite.c:135
#define TEXT_EMAIL
Definition: writepng.h:76
#define PNG_FILTER_TYPE_DEFAULT
Definition: png.h:884
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:868
void * png_ptr
Definition: readpng2.h:86
void writepng_version_info(void)
Definition: writepng.c:69
void PNGAPI png_write_end(png_structp png_ptr, png_infop info_ptr)
Definition: pngwrite.c:315
char * title
Definition: writepng.h:101
int writepng_encode_finish(mainprog_info *mainprog_ptr)
Definition: writepng.c:325
png_infop png_textp int * num_text
Definition: png.h:2255
int writepng_encode_row(mainprog_info *mainprog_ptr)
Definition: writepng.c:295
uch * image_data
Definition: readpng2.h:91
void PNGAPI png_convert_from_time_t(png_timep ptime, time_t ttime)
Definition: pngwrite.c:445
#define PNG_COMPRESSION_TYPE_DEFAULT
Definition: png.h:879
void PNGAPI png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Definition: pngwrite.c:1040
void PNGAPI png_write_row(png_structp png_ptr, png_bytep row)
Definition: pngwrite.c:791
png_structp PNGAPI png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn)
Definition: pngwrite.c:458
void PNGAPI png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr, int num_text)
Definition: pngset.c:650
png_uint_16 red
Definition: png.h:439
char * desc
Definition: writepng.h:103
png_infop info_ptr
Definition: png.h:1443
time_t modtime
Definition: writepng.h:94
#define TEXT_COPY
Definition: writepng.h:75
int writepng_init(mainprog_info *mainprog_ptr)
Definition: writepng.c:83
void * info_ptr
Definition: readpng2.h:87
uch ** row_pointers
Definition: readpng2.h:92
jmp_buf jmpbuf
Definition: readpng2.h:93
void PNGAPI png_init_io(png_structp png_ptr, png_FILE_p fp)
Definition: png.c:660
int color_type
Definition: readpng.c:72
#define TEXT_AUTHOR
Definition: writepng.h:73
char * copyright
Definition: writepng.h:104
png_info FAR * png_infop
Definition: png.h:849
png_const_charp msg
Definition: PngFile.c:23