aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/gregbook/writepng.c
diff options
context:
space:
mode:
authorDavid Walter Seikel2013-01-13 18:54:10 +1000
committerDavid Walter Seikel2013-01-13 18:54:10 +1000
commit959831f4ef5a3e797f576c3de08cd65032c997ad (patch)
treee7351908be5995f0b325b2ebeaa02d5a34b82583 /libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/gregbook/writepng.c
parentAdd info about changes to Irrlicht. (diff)
downloadSledjHamr-959831f4ef5a3e797f576c3de08cd65032c997ad.zip
SledjHamr-959831f4ef5a3e797f576c3de08cd65032c997ad.tar.gz
SledjHamr-959831f4ef5a3e797f576c3de08cd65032c997ad.tar.bz2
SledjHamr-959831f4ef5a3e797f576c3de08cd65032c997ad.tar.xz
Remove damned ancient DOS line endings from Irrlicht. Hopefully I did not go overboard.
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.c800
1 files changed, 400 insertions, 400 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
index 8373c16..f07f4a8 100644
--- a/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/gregbook/writepng.c
+++ b/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/gregbook/writepng.c
@@ -1,400 +1,400 @@
1/*--------------------------------------------------------------------------- 1/*---------------------------------------------------------------------------
2 2
3 wpng - simple PNG-writing program writepng.c 3 wpng - simple PNG-writing program writepng.c
4 4
5 --------------------------------------------------------------------------- 5 ---------------------------------------------------------------------------
6 6
7 Copyright (c) 1998-2007 Greg Roelofs. All rights reserved. 7 Copyright (c) 1998-2007 Greg Roelofs. All rights reserved.
8 8
9 This software is provided "as is," without warranty of any kind, 9 This software is provided "as is," without warranty of any kind,
10 express or implied. In no event shall the author or contributors 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 11 be held liable for any damages arising in any way from the use of
12 this software. 12 this software.
13 13
14 The contents of this file are DUAL-LICENSED. You may modify and/or 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 15 redistribute this software according to the terms of one of the
16 following two licenses (at your option): 16 following two licenses (at your option):
17 17
18 18
19 LICENSE 1 ("BSD-like with advertising clause"): 19 LICENSE 1 ("BSD-like with advertising clause"):
20 20
21 Permission is granted to anyone to use this software for any purpose, 21 Permission is granted to anyone to use this software for any purpose,
22 including commercial applications, and to alter it and redistribute 22 including commercial applications, and to alter it and redistribute
23 it freely, subject to the following restrictions: 23 it freely, subject to the following restrictions:
24 24
25 1. Redistributions of source code must retain the above copyright 25 1. Redistributions of source code must retain the above copyright
26 notice, disclaimer, and this list of conditions. 26 notice, disclaimer, and this list of conditions.
27 2. Redistributions in binary form must reproduce the above copyright 27 2. Redistributions in binary form must reproduce the above copyright
28 notice, disclaimer, and this list of conditions in the documenta- 28 notice, disclaimer, and this list of conditions in the documenta-
29 tion and/or other materials provided with the distribution. 29 tion and/or other materials provided with the distribution.
30 3. All advertising materials mentioning features or use of this 30 3. All advertising materials mentioning features or use of this
31 software must display the following acknowledgment: 31 software must display the following acknowledgment:
32 32
33 This product includes software developed by Greg Roelofs 33 This product includes software developed by Greg Roelofs
34 and contributors for the book, "PNG: The Definitive Guide," 34 and contributors for the book, "PNG: The Definitive Guide,"
35 published by O'Reilly and Associates. 35 published by O'Reilly and Associates.
36 36
37 37
38 LICENSE 2 (GNU GPL v2 or later): 38 LICENSE 2 (GNU GPL v2 or later):
39 39
40 This program is free software; you can redistribute it and/or modify 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 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 42 the Free Software Foundation; either version 2 of the License, or
43 (at your option) any later version. 43 (at your option) any later version.
44 44
45 This program is distributed in the hope that it will be useful, 45 This program is distributed in the hope that it will be useful,
46 but WITHOUT ANY WARRANTY; without even the implied warranty of 46 but WITHOUT ANY WARRANTY; without even the implied warranty of
47 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 47 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 GNU General Public License for more details. 48 GNU General Public License for more details.
49 49
50 You should have received a copy of the GNU General Public License 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, 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 52 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
53 53
54 ---------------------------------------------------------------------------*/ 54 ---------------------------------------------------------------------------*/
55 55
56 56
57#include <stdlib.h> /* for exit() prototype */ 57#include <stdlib.h> /* for exit() prototype */
58 58
59#include "png.h" /* libpng header; includes zlib.h and setjmp.h */ 59#include "png.h" /* libpng header; includes zlib.h and setjmp.h */
60#include "writepng.h" /* typedefs, common macros, public prototypes */ 60#include "writepng.h" /* typedefs, common macros, public prototypes */
61 61
62 62
63/* local prototype */ 63/* local prototype */
64 64
65static void writepng_error_handler(png_structp png_ptr, png_const_charp msg); 65static void writepng_error_handler(png_structp png_ptr, png_const_charp msg);
66 66
67 67
68 68
69void writepng_version_info(void) 69void writepng_version_info(void)
70{ 70{
71 fprintf(stderr, " Compiled with libpng %s; using libpng %s.\n", 71 fprintf(stderr, " Compiled with libpng %s; using libpng %s.\n",
72 PNG_LIBPNG_VER_STRING, png_libpng_ver); 72 PNG_LIBPNG_VER_STRING, png_libpng_ver);
73 fprintf(stderr, " Compiled with zlib %s; using zlib %s.\n", 73 fprintf(stderr, " Compiled with zlib %s; using zlib %s.\n",
74 ZLIB_VERSION, zlib_version); 74 ZLIB_VERSION, zlib_version);
75} 75}
76 76
77 77
78 78
79 79
80/* returns 0 for success, 2 for libpng problem, 4 for out of memory, 11 for 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 */ 81 * unexpected pnmtype; note that outfile might be stdout */
82 82
83int writepng_init(mainprog_info *mainprog_ptr) 83int writepng_init(mainprog_info *mainprog_ptr)
84{ 84{
85 png_structp png_ptr; /* note: temporary variables! */ 85 png_structp png_ptr; /* note: temporary variables! */
86 png_infop info_ptr; 86 png_infop info_ptr;
87 int color_type, interlace_type; 87 int color_type, interlace_type;
88 88
89 89
90 /* could also replace libpng warning-handler (final NULL), but no need: */ 90 /* could also replace libpng warning-handler (final NULL), but no need: */
91 91
92 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, mainprog_ptr, 92 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, mainprog_ptr,
93 writepng_error_handler, NULL); 93 writepng_error_handler, NULL);
94 if (!png_ptr) 94 if (!png_ptr)
95 return 4; /* out of memory */ 95 return 4; /* out of memory */
96 96
97 info_ptr = png_create_info_struct(png_ptr); 97 info_ptr = png_create_info_struct(png_ptr);
98 if (!info_ptr) { 98 if (!info_ptr) {
99 png_destroy_write_struct(&png_ptr, NULL); 99 png_destroy_write_struct(&png_ptr, NULL);
100 return 4; /* out of memory */ 100 return 4; /* out of memory */
101 } 101 }
102 102
103 103
104 /* setjmp() must be called in every function that calls a PNG-writing 104 /* setjmp() must be called in every function that calls a PNG-writing
105 * libpng function, unless an alternate error handler was installed-- 105 * libpng function, unless an alternate error handler was installed--
106 * but compatible error handlers must either use longjmp() themselves 106 * but compatible error handlers must either use longjmp() themselves
107 * (as in this program) or some other method to return control to 107 * (as in this program) or some other method to return control to
108 * application code, so here we go: */ 108 * application code, so here we go: */
109 109
110 if (setjmp(mainprog_ptr->jmpbuf)) { 110 if (setjmp(mainprog_ptr->jmpbuf)) {
111 png_destroy_write_struct(&png_ptr, &info_ptr); 111 png_destroy_write_struct(&png_ptr, &info_ptr);
112 return 2; 112 return 2;
113 } 113 }
114 114
115 115
116 /* make sure outfile is (re)opened in BINARY mode */ 116 /* make sure outfile is (re)opened in BINARY mode */
117 117
118 png_init_io(png_ptr, mainprog_ptr->outfile); 118 png_init_io(png_ptr, mainprog_ptr->outfile);
119 119
120 120
121 /* set the compression levels--in general, always want to leave filtering 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, 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 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 124 * is 16K or smaller (unknown here)--also the default; usually want max
125 * compression (NOT the default); and remaining compression flags should 125 * compression (NOT the default); and remaining compression flags should
126 * be left alone */ 126 * be left alone */
127 127
128 png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); 128 png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
129/* 129/*
130 >> this is default for no filtering; Z_FILTERED is default otherwise: 130 >> this is default for no filtering; Z_FILTERED is default otherwise:
131 png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY); 131 png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
132 >> these are all defaults: 132 >> these are all defaults:
133 png_set_compression_mem_level(png_ptr, 8); 133 png_set_compression_mem_level(png_ptr, 8);
134 png_set_compression_window_bits(png_ptr, 15); 134 png_set_compression_window_bits(png_ptr, 15);
135 png_set_compression_method(png_ptr, 8); 135 png_set_compression_method(png_ptr, 8);
136 */ 136 */
137 137
138 138
139 /* set the image parameters appropriately */ 139 /* set the image parameters appropriately */
140 140
141 if (mainprog_ptr->pnmtype == 5) 141 if (mainprog_ptr->pnmtype == 5)
142 color_type = PNG_COLOR_TYPE_GRAY; 142 color_type = PNG_COLOR_TYPE_GRAY;
143 else if (mainprog_ptr->pnmtype == 6) 143 else if (mainprog_ptr->pnmtype == 6)
144 color_type = PNG_COLOR_TYPE_RGB; 144 color_type = PNG_COLOR_TYPE_RGB;
145 else if (mainprog_ptr->pnmtype == 8) 145 else if (mainprog_ptr->pnmtype == 8)
146 color_type = PNG_COLOR_TYPE_RGB_ALPHA; 146 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
147 else { 147 else {
148 png_destroy_write_struct(&png_ptr, &info_ptr); 148 png_destroy_write_struct(&png_ptr, &info_ptr);
149 return 11; 149 return 11;
150 } 150 }
151 151
152 interlace_type = mainprog_ptr->interlaced? PNG_INTERLACE_ADAM7 : 152 interlace_type = mainprog_ptr->interlaced? PNG_INTERLACE_ADAM7 :
153 PNG_INTERLACE_NONE; 153 PNG_INTERLACE_NONE;
154 154
155 png_set_IHDR(png_ptr, info_ptr, mainprog_ptr->width, mainprog_ptr->height, 155 png_set_IHDR(png_ptr, info_ptr, mainprog_ptr->width, mainprog_ptr->height,
156 mainprog_ptr->sample_depth, color_type, interlace_type, 156 mainprog_ptr->sample_depth, color_type, interlace_type,
157 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); 157 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
158 158
159 if (mainprog_ptr->gamma > 0.0) 159 if (mainprog_ptr->gamma > 0.0)
160 png_set_gAMA(png_ptr, info_ptr, mainprog_ptr->gamma); 160 png_set_gAMA(png_ptr, info_ptr, mainprog_ptr->gamma);
161 161
162 if (mainprog_ptr->have_bg) { /* we know it's RGBA, not gray+alpha */ 162 if (mainprog_ptr->have_bg) { /* we know it's RGBA, not gray+alpha */
163 png_color_16 background; 163 png_color_16 background;
164 164
165 background.red = mainprog_ptr->bg_red; 165 background.red = mainprog_ptr->bg_red;
166 background.green = mainprog_ptr->bg_green; 166 background.green = mainprog_ptr->bg_green;
167 background.blue = mainprog_ptr->bg_blue; 167 background.blue = mainprog_ptr->bg_blue;
168 png_set_bKGD(png_ptr, info_ptr, &background); 168 png_set_bKGD(png_ptr, info_ptr, &background);
169 } 169 }
170 170
171 if (mainprog_ptr->have_time) { 171 if (mainprog_ptr->have_time) {
172 png_time modtime; 172 png_time modtime;
173 173
174 png_convert_from_time_t(&modtime, mainprog_ptr->modtime); 174 png_convert_from_time_t(&modtime, mainprog_ptr->modtime);
175 png_set_tIME(png_ptr, info_ptr, &modtime); 175 png_set_tIME(png_ptr, info_ptr, &modtime);
176 } 176 }
177 177
178 if (mainprog_ptr->have_text) { 178 if (mainprog_ptr->have_text) {
179 png_text text[6]; 179 png_text text[6];
180 int num_text = 0; 180 int num_text = 0;
181 181
182 if (mainprog_ptr->have_text & TEXT_TITLE) { 182 if (mainprog_ptr->have_text & TEXT_TITLE) {
183 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE; 183 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
184 text[num_text].key = "Title"; 184 text[num_text].key = "Title";
185 text[num_text].text = mainprog_ptr->title; 185 text[num_text].text = mainprog_ptr->title;
186 ++num_text; 186 ++num_text;
187 } 187 }
188 if (mainprog_ptr->have_text & TEXT_AUTHOR) { 188 if (mainprog_ptr->have_text & TEXT_AUTHOR) {
189 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE; 189 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
190 text[num_text].key = "Author"; 190 text[num_text].key = "Author";
191 text[num_text].text = mainprog_ptr->author; 191 text[num_text].text = mainprog_ptr->author;
192 ++num_text; 192 ++num_text;
193 } 193 }
194 if (mainprog_ptr->have_text & TEXT_DESC) { 194 if (mainprog_ptr->have_text & TEXT_DESC) {
195 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE; 195 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
196 text[num_text].key = "Description"; 196 text[num_text].key = "Description";
197 text[num_text].text = mainprog_ptr->desc; 197 text[num_text].text = mainprog_ptr->desc;
198 ++num_text; 198 ++num_text;
199 } 199 }
200 if (mainprog_ptr->have_text & TEXT_COPY) { 200 if (mainprog_ptr->have_text & TEXT_COPY) {
201 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE; 201 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
202 text[num_text].key = "Copyright"; 202 text[num_text].key = "Copyright";
203 text[num_text].text = mainprog_ptr->copyright; 203 text[num_text].text = mainprog_ptr->copyright;
204 ++num_text; 204 ++num_text;
205 } 205 }
206 if (mainprog_ptr->have_text & TEXT_EMAIL) { 206 if (mainprog_ptr->have_text & TEXT_EMAIL) {
207 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE; 207 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
208 text[num_text].key = "E-mail"; 208 text[num_text].key = "E-mail";
209 text[num_text].text = mainprog_ptr->email; 209 text[num_text].text = mainprog_ptr->email;
210 ++num_text; 210 ++num_text;
211 } 211 }
212 if (mainprog_ptr->have_text & TEXT_URL) { 212 if (mainprog_ptr->have_text & TEXT_URL) {
213 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE; 213 text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
214 text[num_text].key = "URL"; 214 text[num_text].key = "URL";
215 text[num_text].text = mainprog_ptr->url; 215 text[num_text].text = mainprog_ptr->url;
216 ++num_text; 216 ++num_text;
217 } 217 }
218 png_set_text(png_ptr, info_ptr, text, num_text); 218 png_set_text(png_ptr, info_ptr, text, num_text);
219 } 219 }
220 220
221 221
222 /* write all chunks up to (but not including) first IDAT */ 222 /* write all chunks up to (but not including) first IDAT */
223 223
224 png_write_info(png_ptr, info_ptr); 224 png_write_info(png_ptr, info_ptr);
225 225
226 226
227 /* if we wanted to write any more text info *after* the image data, we 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 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 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 230 * have no effect since we already called it above (only one tIME chunk
231 * allowed) */ 231 * allowed) */
232 232
233 233
234 /* set up the transformations: for now, just pack low-bit-depth pixels 234 /* set up the transformations: for now, just pack low-bit-depth pixels
235 * into bytes (one, two or four pixels per byte) */ 235 * into bytes (one, two or four pixels per byte) */
236 236
237 png_set_packing(png_ptr); 237 png_set_packing(png_ptr);
238/* png_set_shift(png_ptr, &sig_bit); to scale low-bit-depth values */ 238/* png_set_shift(png_ptr, &sig_bit); to scale low-bit-depth values */
239 239
240 240
241 /* make sure we save our pointers for use in writepng_encode_image() */ 241 /* make sure we save our pointers for use in writepng_encode_image() */
242 242
243 mainprog_ptr->png_ptr = png_ptr; 243 mainprog_ptr->png_ptr = png_ptr;
244 mainprog_ptr->info_ptr = info_ptr; 244 mainprog_ptr->info_ptr = info_ptr;
245 245
246 246
247 /* OK, that's all we need to do for now; return happy */ 247 /* OK, that's all we need to do for now; return happy */
248 248
249 return 0; 249 return 0;
250} 250}
251 251
252 252
253 253
254 254
255 255
256/* returns 0 for success, 2 for libpng (longjmp) problem */ 256/* returns 0 for success, 2 for libpng (longjmp) problem */
257 257
258int writepng_encode_image(mainprog_info *mainprog_ptr) 258int writepng_encode_image(mainprog_info *mainprog_ptr)
259{ 259{
260 png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr; 260 png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
261 png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr; 261 png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
262 262
263 263
264 /* as always, setjmp() must be called in every function that calls a 264 /* as always, setjmp() must be called in every function that calls a
265 * PNG-writing libpng function */ 265 * PNG-writing libpng function */
266 266
267 if (setjmp(mainprog_ptr->jmpbuf)) { 267 if (setjmp(mainprog_ptr->jmpbuf)) {
268 png_destroy_write_struct(&png_ptr, &info_ptr); 268 png_destroy_write_struct(&png_ptr, &info_ptr);
269 mainprog_ptr->png_ptr = NULL; 269 mainprog_ptr->png_ptr = NULL;
270 mainprog_ptr->info_ptr = NULL; 270 mainprog_ptr->info_ptr = NULL;
271 return 2; 271 return 2;
272 } 272 }
273 273
274 274
275 /* and now we just write the whole image; libpng takes care of interlacing 275 /* and now we just write the whole image; libpng takes care of interlacing
276 * for us */ 276 * for us */
277 277
278 png_write_image(png_ptr, mainprog_ptr->row_pointers); 278 png_write_image(png_ptr, mainprog_ptr->row_pointers);
279 279
280 280
281 /* since that's it, we also close out the end of the PNG file now--if we 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 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: */ 283 * would be info_ptr, but we optimize slightly by sending NULL pointer: */
284 284
285 png_write_end(png_ptr, NULL); 285 png_write_end(png_ptr, NULL);
286 286
287 return 0; 287 return 0;
288} 288}
289 289
290 290
291 291
292 292
293 293
294/* returns 0 if succeeds, 2 if libpng problem */ 294/* returns 0 if succeeds, 2 if libpng problem */
295 295
296int writepng_encode_row(mainprog_info *mainprog_ptr) /* NON-interlaced only! */ 296int writepng_encode_row(mainprog_info *mainprog_ptr) /* NON-interlaced only! */
297{ 297{
298 png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr; 298 png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
299 png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr; 299 png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
300 300
301 301
302 /* as always, setjmp() must be called in every function that calls a 302 /* as always, setjmp() must be called in every function that calls a
303 * PNG-writing libpng function */ 303 * PNG-writing libpng function */
304 304
305 if (setjmp(mainprog_ptr->jmpbuf)) { 305 if (setjmp(mainprog_ptr->jmpbuf)) {
306 png_destroy_write_struct(&png_ptr, &info_ptr); 306 png_destroy_write_struct(&png_ptr, &info_ptr);
307 mainprog_ptr->png_ptr = NULL; 307 mainprog_ptr->png_ptr = NULL;
308 mainprog_ptr->info_ptr = NULL; 308 mainprog_ptr->info_ptr = NULL;
309 return 2; 309 return 2;
310 } 310 }
311 311
312 312
313 /* image_data points at our one row of image data */ 313 /* image_data points at our one row of image data */
314 314
315 png_write_row(png_ptr, mainprog_ptr->image_data); 315 png_write_row(png_ptr, mainprog_ptr->image_data);
316 316
317 return 0; 317 return 0;
318} 318}
319 319
320 320
321 321
322 322
323 323
324/* returns 0 if succeeds, 2 if libpng problem */ 324/* returns 0 if succeeds, 2 if libpng problem */
325 325
326int writepng_encode_finish(mainprog_info *mainprog_ptr) /* NON-interlaced! */ 326int writepng_encode_finish(mainprog_info *mainprog_ptr) /* NON-interlaced! */
327{ 327{
328 png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr; 328 png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
329 png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr; 329 png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
330 330
331 331
332 /* as always, setjmp() must be called in every function that calls a 332 /* as always, setjmp() must be called in every function that calls a
333 * PNG-writing libpng function */ 333 * PNG-writing libpng function */
334 334
335 if (setjmp(mainprog_ptr->jmpbuf)) { 335 if (setjmp(mainprog_ptr->jmpbuf)) {
336 png_destroy_write_struct(&png_ptr, &info_ptr); 336 png_destroy_write_struct(&png_ptr, &info_ptr);
337 mainprog_ptr->png_ptr = NULL; 337 mainprog_ptr->png_ptr = NULL;
338 mainprog_ptr->info_ptr = NULL; 338 mainprog_ptr->info_ptr = NULL;
339 return 2; 339 return 2;
340 } 340 }
341 341
342 342
343 /* close out PNG file; if we had any text or time info to write after 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: */ 344 * the IDATs, second argument would be info_ptr: */
345 345
346 png_write_end(png_ptr, NULL); 346 png_write_end(png_ptr, NULL);
347 347
348 return 0; 348 return 0;
349} 349}
350 350
351 351
352 352
353 353
354 354
355void writepng_cleanup(mainprog_info *mainprog_ptr) 355void writepng_cleanup(mainprog_info *mainprog_ptr)
356{ 356{
357 png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr; 357 png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
358 png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr; 358 png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
359 359
360 if (png_ptr && info_ptr) 360 if (png_ptr && info_ptr)
361 png_destroy_write_struct(&png_ptr, &info_ptr); 361 png_destroy_write_struct(&png_ptr, &info_ptr);
362} 362}
363 363
364 364
365 365
366 366
367 367
368static void writepng_error_handler(png_structp png_ptr, png_const_charp msg) 368static void writepng_error_handler(png_structp png_ptr, png_const_charp msg)
369{ 369{
370 mainprog_info *mainprog_ptr; 370 mainprog_info *mainprog_ptr;
371 371
372 /* This function, aside from the extra step of retrieving the "error 372 /* This function, aside from the extra step of retrieving the "error
373 * pointer" (below) and the fact that it exists within the application 373 * pointer" (below) and the fact that it exists within the application
374 * rather than within libpng, is essentially identical to libpng's 374 * rather than within libpng, is essentially identical to libpng's
375 * default error handler. The second point is critical: since both 375 * default error handler. The second point is critical: since both
376 * setjmp() and longjmp() are called from the same code, they are 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, 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) 378 * regardless of whether _BSD_SOURCE or anything else has (or has not)
379 * been defined. */ 379 * been defined. */
380 380
381 fprintf(stderr, "writepng libpng error: %s\n", msg); 381 fprintf(stderr, "writepng libpng error: %s\n", msg);
382 fflush(stderr); 382 fflush(stderr);
383 383
384 mainprog_ptr = png_get_error_ptr(png_ptr); 384 mainprog_ptr = png_get_error_ptr(png_ptr);
385 if (mainprog_ptr == NULL) { /* we are completely hosed now */ 385 if (mainprog_ptr == NULL) { /* we are completely hosed now */
386 fprintf(stderr, 386 fprintf(stderr,
387 "writepng severe error: jmpbuf not recoverable; terminating.\n"); 387 "writepng severe error: jmpbuf not recoverable; terminating.\n");
388 fflush(stderr); 388 fflush(stderr);
389 exit(99); 389 exit(99);
390 } 390 }
391 391
392 /* Now we have our data structure we can use the information in it 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 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 394 * where 'setjmp' is called in this file.) This will work with other
395 * error handling mechanisms as well - libpng always calls png_error 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 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. 397 * is intercepted, application code can do its own error recovery.
398 */ 398 */
399 longjmp(mainprog_ptr->jmpbuf, 1); 399 longjmp(mainprog_ptr->jmpbuf, 1);
400} 400}