aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/gregbook/writepng.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/gregbook/writepng.c')
-rw-r--r--libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/gregbook/writepng.c400
1 files changed, 400 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/gregbook/writepng.c b/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/gregbook/writepng.c
new file mode 100644
index 0000000..8373c16
--- /dev/null
+++ b/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/gregbook/writepng.c
@@ -0,0 +1,400 @@
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
65static void writepng_error_handler(png_structp png_ptr, png_const_charp msg);
66
67
68
69void writepng_version_info(void)
70{
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);
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
83int writepng_init(mainprog_info *mainprog_ptr)
84{
85 png_structp png_ptr; /* note: temporary variables! */
86 png_infop info_ptr;
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 some other method to return control to
108 * application code, so here we go: */
109
110 if (setjmp(mainprog_ptr->jmpbuf)) {
111 png_destroy_write_struct(&png_ptr, &info_ptr);
112 return 2;
113 }
114
115
116 /* make sure outfile is (re)opened in BINARY mode */
117
118 png_init_io(png_ptr, mainprog_ptr->outfile);
119
120
121 /* set the compression levels--in general, always want to leave filtering
122 * turned on (except for palette images) and allow all of the filters,
123 * which is the default; want 32K zlib window, unless entire image buffer
124 * is 16K or smaller (unknown here)--also the default; usually want max
125 * compression (NOT the default); and remaining compression flags should
126 * be left alone */
127
128 png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
129/*
130 >> this is default for no filtering; Z_FILTERED is default otherwise:
131 png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
132 >> these are all defaults:
133 png_set_compression_mem_level(png_ptr, 8);
134 png_set_compression_window_bits(png_ptr, 15);
135 png_set_compression_method(png_ptr, 8);
136 */
137
138
139 /* set the image parameters appropriately */
140
141 if (mainprog_ptr->pnmtype == 5)
142 color_type = PNG_COLOR_TYPE_GRAY;
143 else if (mainprog_ptr->pnmtype == 6)
144 color_type = PNG_COLOR_TYPE_RGB;
145 else if (mainprog_ptr->pnmtype == 8)
146 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
147 else {
148 png_destroy_write_struct(&png_ptr, &info_ptr);
149 return 11;
150 }
151
152 interlace_type = mainprog_ptr->interlaced? PNG_INTERLACE_ADAM7 :
153 PNG_INTERLACE_NONE;
154
155 png_set_IHDR(png_ptr, info_ptr, mainprog_ptr->width, mainprog_ptr->height,
156 mainprog_ptr->sample_depth, color_type, interlace_type,
157 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
158
159 if (mainprog_ptr->gamma > 0.0)
160 png_set_gAMA(png_ptr, info_ptr, mainprog_ptr->gamma);
161
162 if (mainprog_ptr->have_bg) { /* we know it's RGBA, not gray+alpha */
163 png_color_16 background;
164
165 background.red = mainprog_ptr->bg_red;
166 background.green = mainprog_ptr->bg_green;
167 background.blue = mainprog_ptr->bg_blue;
168 png_set_bKGD(png_ptr, info_ptr, &background);
169 }
170
171 if (mainprog_ptr->have_time) {
172 png_time modtime;
173
174 png_convert_from_time_t(&modtime, mainprog_ptr->modtime);
175 png_set_tIME(png_ptr, info_ptr, &modtime);
176 }
177
178 if (mainprog_ptr->have_text) {
179 png_text text[6];
180 int num_text = 0;
181
182 if (mainprog_ptr->have_text & TEXT_TITLE) {
183 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
184 text[num_text].key = "Title";
185 text[num_text].text = mainprog_ptr->title;
186 ++num_text;
187 }
188 if (mainprog_ptr->have_text & TEXT_AUTHOR) {
189 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
190 text[num_text].key = "Author";
191 text[num_text].text = mainprog_ptr->author;
192 ++num_text;
193 }
194 if (mainprog_ptr->have_text & TEXT_DESC) {
195 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
196 text[num_text].key = "Description";
197 text[num_text].text = mainprog_ptr->desc;
198 ++num_text;
199 }
200 if (mainprog_ptr->have_text & TEXT_COPY) {
201 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
202 text[num_text].key = "Copyright";
203 text[num_text].text = mainprog_ptr->copyright;
204 ++num_text;
205 }
206 if (mainprog_ptr->have_text & TEXT_EMAIL) {
207 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
208 text[num_text].key = "E-mail";
209 text[num_text].text = mainprog_ptr->email;
210 ++num_text;
211 }
212 if (mainprog_ptr->have_text & TEXT_URL) {
213 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
214 text[num_text].key = "URL";
215 text[num_text].text = mainprog_ptr->url;
216 ++num_text;
217 }
218 png_set_text(png_ptr, info_ptr, text, num_text);
219 }
220
221
222 /* write all chunks up to (but not including) first IDAT */
223
224 png_write_info(png_ptr, info_ptr);
225
226
227 /* if we wanted to write any more text info *after* the image data, we
228 * would set up text struct(s) here and call png_set_text() again, with
229 * just the new data; png_set_tIME() could also go here, but it would
230 * have no effect since we already called it above (only one tIME chunk
231 * allowed) */
232
233
234 /* set up the transformations: for now, just pack low-bit-depth pixels
235 * into bytes (one, two or four pixels per byte) */
236
237 png_set_packing(png_ptr);
238/* png_set_shift(png_ptr, &sig_bit); to scale low-bit-depth values */
239
240
241 /* make sure we save our pointers for use in writepng_encode_image() */
242
243 mainprog_ptr->png_ptr = png_ptr;
244 mainprog_ptr->info_ptr = info_ptr;
245
246
247 /* OK, that's all we need to do for now; return happy */
248
249 return 0;
250}
251
252
253
254
255
256/* returns 0 for success, 2 for libpng (longjmp) problem */
257
258int writepng_encode_image(mainprog_info *mainprog_ptr)
259{
260 png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
261 png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
262
263
264 /* as always, setjmp() must be called in every function that calls a
265 * PNG-writing libpng function */
266
267 if (setjmp(mainprog_ptr->jmpbuf)) {
268 png_destroy_write_struct(&png_ptr, &info_ptr);
269 mainprog_ptr->png_ptr = NULL;
270 mainprog_ptr->info_ptr = NULL;
271 return 2;
272 }
273
274
275 /* and now we just write the whole image; libpng takes care of interlacing
276 * for us */
277
278 png_write_image(png_ptr, mainprog_ptr->row_pointers);
279
280
281 /* since that's it, we also close out the end of the PNG file now--if we
282 * had any text or time info to write after the IDATs, second argument
283 * would be info_ptr, but we optimize slightly by sending NULL pointer: */
284
285 png_write_end(png_ptr, NULL);
286
287 return 0;
288}
289
290
291
292
293
294/* returns 0 if succeeds, 2 if libpng problem */
295
296int writepng_encode_row(mainprog_info *mainprog_ptr) /* NON-interlaced only! */
297{
298 png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
299 png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
300
301
302 /* as always, setjmp() must be called in every function that calls a
303 * PNG-writing libpng function */
304
305 if (setjmp(mainprog_ptr->jmpbuf)) {
306 png_destroy_write_struct(&png_ptr, &info_ptr);
307 mainprog_ptr->png_ptr = NULL;
308 mainprog_ptr->info_ptr = NULL;
309 return 2;
310 }
311
312
313 /* image_data points at our one row of image data */
314
315 png_write_row(png_ptr, mainprog_ptr->image_data);
316
317 return 0;
318}
319
320
321
322
323
324/* returns 0 if succeeds, 2 if libpng problem */
325
326int writepng_encode_finish(mainprog_info *mainprog_ptr) /* NON-interlaced! */
327{
328 png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
329 png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
330
331
332 /* as always, setjmp() must be called in every function that calls a
333 * PNG-writing libpng function */
334
335 if (setjmp(mainprog_ptr->jmpbuf)) {
336 png_destroy_write_struct(&png_ptr, &info_ptr);
337 mainprog_ptr->png_ptr = NULL;
338 mainprog_ptr->info_ptr = NULL;
339 return 2;
340 }
341
342
343 /* close out PNG file; if we had any text or time info to write after
344 * the IDATs, second argument would be info_ptr: */
345
346 png_write_end(png_ptr, NULL);
347
348 return 0;
349}
350
351
352
353
354
355void writepng_cleanup(mainprog_info *mainprog_ptr)
356{
357 png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
358 png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
359
360 if (png_ptr && info_ptr)
361 png_destroy_write_struct(&png_ptr, &info_ptr);
362}
363
364
365
366
367
368static void writepng_error_handler(png_structp png_ptr, png_const_charp msg)
369{
370 mainprog_info *mainprog_ptr;
371
372 /* This function, aside from the extra step of retrieving the "error
373 * pointer" (below) and the fact that it exists within the application
374 * rather than within libpng, is essentially identical to libpng's
375 * default error handler. The second point is critical: since both
376 * setjmp() and longjmp() are called from the same code, they are
377 * guaranteed to have compatible notions of how big a jmp_buf is,
378 * regardless of whether _BSD_SOURCE or anything else has (or has not)
379 * been defined. */
380
381 fprintf(stderr, "writepng libpng error: %s\n", msg);
382 fflush(stderr);
383
384 mainprog_ptr = png_get_error_ptr(png_ptr);
385 if (mainprog_ptr == NULL) { /* we are completely hosed now */
386 fprintf(stderr,
387 "writepng severe error: jmpbuf not recoverable; terminating.\n");
388 fflush(stderr);
389 exit(99);
390 }
391
392 /* Now we have our data structure we can use the information in it
393 * to return control to our own higher level code (all the points
394 * where 'setjmp' is called in this file.) This will work with other
395 * error handling mechanisms as well - libpng always calls png_error
396 * when it can proceed no further, thus, so long as the error handler
397 * is intercepted, application code can do its own error recovery.
398 */
399 longjmp(mainprog_ptr->jmpbuf, 1);
400}