]> git.saurik.com Git - wxWidgets.git/blame - src/png/pngwrite.c
Fix for #11647: text from WriteText not scrolled to be visible in wxRichTextCtrl
[wxWidgets.git] / src / png / pngwrite.c
CommitLineData
0272a10d
VZ
1
2/* pngwrite.c - general routines to write a PNG file
3 *
b61cc19c
PC
4 * Last changed in libpng 1.4.0 [January 3, 2010]
5 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
0272a10d
VZ
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
b61cc19c
PC
8 *
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
0272a10d
VZ
12 */
13
b61cc19c
PC
14/* Get internal access to png.h */
15#define PNG_NO_PEDANTIC_WARNINGS
0272a10d
VZ
16#include "png.h"
17#ifdef PNG_WRITE_SUPPORTED
b61cc19c 18#include "pngpriv.h"
0272a10d
VZ
19
20/* Writes all the PNG information. This is the suggested way to use the
21 * library. If you have a new chunk to add, make a function to write it,
22 * and put it in the correct location here. If you want the chunk written
23 * after the image data, put it in png_write_end(). I strongly encourage
24 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
25 * the chunk, as that will keep the code from breaking if you want to just
26 * write a plain PNG file. If you have long comments, I suggest writing
27 * them in png_write_end(), and compressing them.
28 */
29void PNGAPI
30png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
31{
970f6abe 32 png_debug(1, "in png_write_info_before_PLTE");
b61cc19c 33
0272a10d
VZ
34 if (png_ptr == NULL || info_ptr == NULL)
35 return;
36 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
37 {
b61cc19c
PC
38 /* Write PNG signature */
39 png_write_sig(png_ptr);
40#ifdef PNG_MNG_FEATURES_SUPPORTED
41 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
42 (png_ptr->mng_features_permitted))
0272a10d 43 {
970f6abe 44 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
b61cc19c 45 png_ptr->mng_features_permitted = 0;
0272a10d
VZ
46 }
47#endif
b61cc19c 48 /* Write IHDR information. */
0272a10d
VZ
49 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
50 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
51 info_ptr->filter_type,
b61cc19c 52#ifdef PNG_WRITE_INTERLACING_SUPPORTED
0272a10d
VZ
53 info_ptr->interlace_type);
54#else
55 0);
56#endif
b61cc19c
PC
57 /* The rest of these check to see if the valid field has the appropriate
58 * flag set, and if it does, writes the chunk.
59 */
60#ifdef PNG_WRITE_gAMA_SUPPORTED
0272a10d
VZ
61 if (info_ptr->valid & PNG_INFO_gAMA)
62 {
63# ifdef PNG_FLOATING_POINT_SUPPORTED
64 png_write_gAMA(png_ptr, info_ptr->gamma);
65#else
66#ifdef PNG_FIXED_POINT_SUPPORTED
67 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
68# endif
69#endif
70 }
71#endif
b61cc19c 72#ifdef PNG_WRITE_sRGB_SUPPORTED
0272a10d
VZ
73 if (info_ptr->valid & PNG_INFO_sRGB)
74 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
75#endif
b61cc19c 76#ifdef PNG_WRITE_iCCP_SUPPORTED
0272a10d
VZ
77 if (info_ptr->valid & PNG_INFO_iCCP)
78 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
79 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
80#endif
b61cc19c 81#ifdef PNG_WRITE_sBIT_SUPPORTED
0272a10d
VZ
82 if (info_ptr->valid & PNG_INFO_sBIT)
83 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
84#endif
b61cc19c 85#ifdef PNG_WRITE_cHRM_SUPPORTED
0272a10d
VZ
86 if (info_ptr->valid & PNG_INFO_cHRM)
87 {
88#ifdef PNG_FLOATING_POINT_SUPPORTED
89 png_write_cHRM(png_ptr,
90 info_ptr->x_white, info_ptr->y_white,
91 info_ptr->x_red, info_ptr->y_red,
92 info_ptr->x_green, info_ptr->y_green,
93 info_ptr->x_blue, info_ptr->y_blue);
94#else
95# ifdef PNG_FIXED_POINT_SUPPORTED
96 png_write_cHRM_fixed(png_ptr,
97 info_ptr->int_x_white, info_ptr->int_y_white,
98 info_ptr->int_x_red, info_ptr->int_y_red,
99 info_ptr->int_x_green, info_ptr->int_y_green,
100 info_ptr->int_x_blue, info_ptr->int_y_blue);
101# endif
102#endif
103 }
104#endif
b61cc19c 105#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
0272a10d
VZ
106 if (info_ptr->unknown_chunks_num)
107 {
b61cc19c 108 png_unknown_chunk *up;
0272a10d 109
b61cc19c 110 png_debug(5, "writing extra chunks");
0272a10d 111
b61cc19c
PC
112 for (up = info_ptr->unknown_chunks;
113 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
114 up++)
115 {
116 int keep = png_handle_as_unknown(png_ptr, up->name);
0272a10d
VZ
117 if (keep != PNG_HANDLE_CHUNK_NEVER &&
118 up->location && !(up->location & PNG_HAVE_PLTE) &&
119 !(up->location & PNG_HAVE_IDAT) &&
120 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
121 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
122 {
970f6abe
VZ
123 if (up->size == 0)
124 png_warning(png_ptr, "Writing zero-length unknown chunk");
0272a10d
VZ
125 png_write_chunk(png_ptr, up->name, up->data, up->size);
126 }
b61cc19c 127 }
0272a10d
VZ
128 }
129#endif
130 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
131 }
132}
133
134void PNGAPI
135png_write_info(png_structp png_ptr, png_infop info_ptr)
136{
137#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
138 int i;
139#endif
140
970f6abe 141 png_debug(1, "in png_write_info");
0272a10d
VZ
142
143 if (png_ptr == NULL || info_ptr == NULL)
144 return;
145
146 png_write_info_before_PLTE(png_ptr, info_ptr);
147
148 if (info_ptr->valid & PNG_INFO_PLTE)
149 png_write_PLTE(png_ptr, info_ptr->palette,
150 (png_uint_32)info_ptr->num_palette);
151 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
152 png_error(png_ptr, "Valid palette required for paletted images");
153
b61cc19c 154#ifdef PNG_WRITE_tRNS_SUPPORTED
0272a10d 155 if (info_ptr->valid & PNG_INFO_tRNS)
b61cc19c
PC
156 {
157#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
158 /* Invert the alpha channel (in tRNS) */
159 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
160 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
0272a10d 161 {
b61cc19c
PC
162 int j;
163 for (j = 0; j<(int)info_ptr->num_trans; j++)
164 info_ptr->trans_alpha[j] = (png_byte)(255 - info_ptr->trans_alpha[j]);
165 }
0272a10d 166#endif
b61cc19c 167 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
0272a10d 168 info_ptr->num_trans, info_ptr->color_type);
b61cc19c 169 }
0272a10d 170#endif
b61cc19c 171#ifdef PNG_WRITE_bKGD_SUPPORTED
0272a10d
VZ
172 if (info_ptr->valid & PNG_INFO_bKGD)
173 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
174#endif
b61cc19c 175#ifdef PNG_WRITE_hIST_SUPPORTED
0272a10d
VZ
176 if (info_ptr->valid & PNG_INFO_hIST)
177 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
178#endif
b61cc19c 179#ifdef PNG_WRITE_oFFs_SUPPORTED
0272a10d
VZ
180 if (info_ptr->valid & PNG_INFO_oFFs)
181 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
182 info_ptr->offset_unit_type);
183#endif
b61cc19c 184#ifdef PNG_WRITE_pCAL_SUPPORTED
0272a10d
VZ
185 if (info_ptr->valid & PNG_INFO_pCAL)
186 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
187 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
188 info_ptr->pcal_units, info_ptr->pcal_params);
189#endif
b61cc19c
PC
190
191#ifdef PNG_sCAL_SUPPORTED
0272a10d 192 if (info_ptr->valid & PNG_INFO_sCAL)
b61cc19c
PC
193#ifdef PNG_WRITE_sCAL_SUPPORTED
194#if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
0272a10d
VZ
195 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
196 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
b61cc19c 197#else /* !FLOATING_POINT */
0272a10d
VZ
198#ifdef PNG_FIXED_POINT_SUPPORTED
199 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
200 info_ptr->scal_s_width, info_ptr->scal_s_height);
b61cc19c
PC
201#endif /* FIXED_POINT */
202#endif /* FLOATING_POINT */
203#else /* !WRITE_sCAL */
0272a10d 204 png_warning(png_ptr,
b61cc19c
PC
205 "png_write_sCAL not supported; sCAL chunk not written");
206#endif /* WRITE_sCAL */
207#endif /* sCAL */
208
209#ifdef PNG_WRITE_pHYs_SUPPORTED
0272a10d
VZ
210 if (info_ptr->valid & PNG_INFO_pHYs)
211 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
212 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
b61cc19c
PC
213#endif /* pHYs */
214
215#ifdef PNG_WRITE_tIME_SUPPORTED
0272a10d
VZ
216 if (info_ptr->valid & PNG_INFO_tIME)
217 {
218 png_write_tIME(png_ptr, &(info_ptr->mod_time));
219 png_ptr->mode |= PNG_WROTE_tIME;
220 }
b61cc19c
PC
221#endif /* tIME */
222
223#ifdef PNG_WRITE_sPLT_SUPPORTED
0272a10d
VZ
224 if (info_ptr->valid & PNG_INFO_sPLT)
225 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
226 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
b61cc19c
PC
227#endif /* sPLT */
228
229#ifdef PNG_WRITE_TEXT_SUPPORTED
0272a10d
VZ
230 /* Check to see if we need to write text chunks */
231 for (i = 0; i < info_ptr->num_text; i++)
232 {
970f6abe 233 png_debug2(2, "Writing header text chunk %d, type %d", i,
0272a10d 234 info_ptr->text[i].compression);
b61cc19c 235 /* An internationalized chunk? */
0272a10d
VZ
236 if (info_ptr->text[i].compression > 0)
237 {
b61cc19c
PC
238#ifdef PNG_WRITE_iTXt_SUPPORTED
239 /* Write international chunk */
0272a10d
VZ
240 png_write_iTXt(png_ptr,
241 info_ptr->text[i].compression,
242 info_ptr->text[i].key,
243 info_ptr->text[i].lang,
244 info_ptr->text[i].lang_key,
245 info_ptr->text[i].text);
246#else
247 png_warning(png_ptr, "Unable to write international text");
248#endif
249 /* Mark this chunk as written */
250 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
251 }
252 /* If we want a compressed text chunk */
253 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
254 {
b61cc19c
PC
255#ifdef PNG_WRITE_zTXt_SUPPORTED
256 /* Write compressed chunk */
0272a10d
VZ
257 png_write_zTXt(png_ptr, info_ptr->text[i].key,
258 info_ptr->text[i].text, 0,
259 info_ptr->text[i].compression);
260#else
261 png_warning(png_ptr, "Unable to write compressed text");
262#endif
263 /* Mark this chunk as written */
264 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
265 }
266 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
267 {
b61cc19c
PC
268#ifdef PNG_WRITE_tEXt_SUPPORTED
269 /* Write uncompressed chunk */
0272a10d
VZ
270 png_write_tEXt(png_ptr, info_ptr->text[i].key,
271 info_ptr->text[i].text,
272 0);
b61cc19c
PC
273 /* Mark this chunk as written */
274 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
0272a10d 275#else
b61cc19c 276 /* Can't get here */
0272a10d
VZ
277 png_warning(png_ptr, "Unable to write uncompressed text");
278#endif
0272a10d
VZ
279 }
280 }
b61cc19c
PC
281#endif /* tEXt */
282
283#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
0272a10d
VZ
284 if (info_ptr->unknown_chunks_num)
285 {
b61cc19c 286 png_unknown_chunk *up;
0272a10d 287
b61cc19c 288 png_debug(5, "writing extra chunks");
0272a10d 289
b61cc19c
PC
290 for (up = info_ptr->unknown_chunks;
291 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
292 up++)
293 {
294 int keep = png_handle_as_unknown(png_ptr, up->name);
0272a10d
VZ
295 if (keep != PNG_HANDLE_CHUNK_NEVER &&
296 up->location && (up->location & PNG_HAVE_PLTE) &&
297 !(up->location & PNG_HAVE_IDAT) &&
298 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
299 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
300 {
301 png_write_chunk(png_ptr, up->name, up->data, up->size);
302 }
b61cc19c 303 }
0272a10d
VZ
304 }
305#endif
306}
307
308/* Writes the end of the PNG file. If you don't want to write comments or
309 * time information, you can pass NULL for info. If you already wrote these
310 * in png_write_info(), do not write them again here. If you have long
311 * comments, I suggest writing them here, and compressing them.
312 */
313void PNGAPI
314png_write_end(png_structp png_ptr, png_infop info_ptr)
315{
970f6abe 316 png_debug(1, "in png_write_end");
b61cc19c 317
0272a10d
VZ
318 if (png_ptr == NULL)
319 return;
320 if (!(png_ptr->mode & PNG_HAVE_IDAT))
321 png_error(png_ptr, "No IDATs written into file");
322
b61cc19c 323 /* See if user wants us to write information chunks */
0272a10d
VZ
324 if (info_ptr != NULL)
325 {
b61cc19c 326#ifdef PNG_WRITE_TEXT_SUPPORTED
0272a10d
VZ
327 int i; /* local index variable */
328#endif
b61cc19c
PC
329#ifdef PNG_WRITE_tIME_SUPPORTED
330 /* Check to see if user has supplied a time chunk */
0272a10d
VZ
331 if ((info_ptr->valid & PNG_INFO_tIME) &&
332 !(png_ptr->mode & PNG_WROTE_tIME))
333 png_write_tIME(png_ptr, &(info_ptr->mod_time));
334#endif
b61cc19c
PC
335#ifdef PNG_WRITE_TEXT_SUPPORTED
336 /* Loop through comment chunks */
0272a10d
VZ
337 for (i = 0; i < info_ptr->num_text; i++)
338 {
970f6abe 339 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
0272a10d 340 info_ptr->text[i].compression);
b61cc19c 341 /* An internationalized chunk? */
0272a10d
VZ
342 if (info_ptr->text[i].compression > 0)
343 {
b61cc19c
PC
344#ifdef PNG_WRITE_iTXt_SUPPORTED
345 /* Write international chunk */
346 png_write_iTXt(png_ptr,
347 info_ptr->text[i].compression,
348 info_ptr->text[i].key,
349 info_ptr->text[i].lang,
350 info_ptr->text[i].lang_key,
351 info_ptr->text[i].text);
0272a10d 352#else
b61cc19c 353 png_warning(png_ptr, "Unable to write international text");
0272a10d 354#endif
b61cc19c
PC
355 /* Mark this chunk as written */
356 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
0272a10d
VZ
357 }
358 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
359 {
b61cc19c
PC
360#ifdef PNG_WRITE_zTXt_SUPPORTED
361 /* Write compressed chunk */
0272a10d
VZ
362 png_write_zTXt(png_ptr, info_ptr->text[i].key,
363 info_ptr->text[i].text, 0,
364 info_ptr->text[i].compression);
365#else
366 png_warning(png_ptr, "Unable to write compressed text");
367#endif
368 /* Mark this chunk as written */
369 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
370 }
371 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
372 {
b61cc19c
PC
373#ifdef PNG_WRITE_tEXt_SUPPORTED
374 /* Write uncompressed chunk */
0272a10d
VZ
375 png_write_tEXt(png_ptr, info_ptr->text[i].key,
376 info_ptr->text[i].text, 0);
377#else
378 png_warning(png_ptr, "Unable to write uncompressed text");
379#endif
380
381 /* Mark this chunk as written */
382 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
383 }
384 }
385#endif
b61cc19c 386#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
0272a10d
VZ
387 if (info_ptr->unknown_chunks_num)
388 {
b61cc19c 389 png_unknown_chunk *up;
0272a10d 390
b61cc19c 391 png_debug(5, "writing extra chunks");
0272a10d 392
b61cc19c
PC
393 for (up = info_ptr->unknown_chunks;
394 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
395 up++)
396 {
397 int keep = png_handle_as_unknown(png_ptr, up->name);
0272a10d
VZ
398 if (keep != PNG_HANDLE_CHUNK_NEVER &&
399 up->location && (up->location & PNG_AFTER_IDAT) &&
400 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
401 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
402 {
403 png_write_chunk(png_ptr, up->name, up->data, up->size);
404 }
b61cc19c 405 }
0272a10d
VZ
406 }
407#endif
408 }
409
410 png_ptr->mode |= PNG_AFTER_IDAT;
411
b61cc19c 412 /* Write end of PNG file */
0272a10d 413 png_write_IEND(png_ptr);
970f6abe
VZ
414 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
415 * and restored again in libpng-1.2.30, may cause some applications that
416 * do not set png_ptr->output_flush_fn to crash. If your application
417 * experiences a problem, please try building libpng with
418 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
b61cc19c 419 * png-mng-implement at lists.sf.net .
970f6abe 420 */
b61cc19c
PC
421#ifdef PNG_WRITE_FLUSH_SUPPORTED
422# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
970f6abe 423 png_flush(png_ptr);
b61cc19c 424# endif
970f6abe 425#endif
0272a10d
VZ
426}
427
b61cc19c
PC
428#ifdef PNG_CONVERT_tIME_SUPPORTED
429/* "tm" structure is not supported on WindowsCE */
0272a10d
VZ
430void PNGAPI
431png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
432{
970f6abe 433 png_debug(1, "in png_convert_from_struct_tm");
b61cc19c 434
0272a10d
VZ
435 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
436 ptime->month = (png_byte)(ttime->tm_mon + 1);
437 ptime->day = (png_byte)ttime->tm_mday;
438 ptime->hour = (png_byte)ttime->tm_hour;
439 ptime->minute = (png_byte)ttime->tm_min;
440 ptime->second = (png_byte)ttime->tm_sec;
441}
442
443void PNGAPI
444png_convert_from_time_t(png_timep ptime, time_t ttime)
445{
446 struct tm *tbuf;
447
970f6abe 448 png_debug(1, "in png_convert_from_time_t");
b61cc19c 449
0272a10d
VZ
450 tbuf = gmtime(&ttime);
451 png_convert_from_struct_tm(ptime, tbuf);
452}
453#endif
0272a10d
VZ
454
455/* Initialize png_ptr structure, and allocate any memory needed */
456png_structp PNGAPI
457png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
458 png_error_ptr error_fn, png_error_ptr warn_fn)
459{
460#ifdef PNG_USER_MEM_SUPPORTED
461 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
b61cc19c 462 warn_fn, NULL, NULL, NULL));
0272a10d
VZ
463}
464
465/* Alternate initialize png_ptr structure, and allocate any memory needed */
466png_structp PNGAPI
467png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
468 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
469 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
470{
471#endif /* PNG_USER_MEM_SUPPORTED */
b61cc19c 472 volatile int png_cleanup_needed = 0;
970f6abe 473#ifdef PNG_SETJMP_SUPPORTED
b61cc19c 474 volatile
970f6abe 475#endif
b61cc19c 476 png_structp png_ptr;
0272a10d
VZ
477#ifdef PNG_SETJMP_SUPPORTED
478#ifdef USE_FAR_KEYWORD
479 jmp_buf jmpbuf;
480#endif
481#endif
482 int i;
b61cc19c 483
970f6abe 484 png_debug(1, "in png_create_write_struct");
b61cc19c 485
0272a10d
VZ
486#ifdef PNG_USER_MEM_SUPPORTED
487 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
488 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
489#else
490 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
491#endif /* PNG_USER_MEM_SUPPORTED */
492 if (png_ptr == NULL)
493 return (NULL);
494
b61cc19c 495 /* Added at libpng-1.2.6 */
0272a10d 496#ifdef PNG_SET_USER_LIMITS_SUPPORTED
b61cc19c
PC
497 png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
498 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
0272a10d
VZ
499#endif
500
501#ifdef PNG_SETJMP_SUPPORTED
b61cc19c
PC
502/* Applications that neglect to set up their own setjmp() and then
503 encounter a png_error() will longjmp here. Since the jmpbuf is
504 then meaningless we abort instead of returning. */
0272a10d
VZ
505#ifdef USE_FAR_KEYWORD
506 if (setjmp(jmpbuf))
507#else
b61cc19c 508 if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */
0272a10d 509#endif
0272a10d 510#ifdef USE_FAR_KEYWORD
b61cc19c 511 png_memcpy(png_jmpbuf(png_ptr), jmpbuf, png_sizeof(jmp_buf));
0272a10d 512#endif
b61cc19c 513 PNG_ABORT();
0272a10d
VZ
514#endif
515
516#ifdef PNG_USER_MEM_SUPPORTED
517 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
518#endif /* PNG_USER_MEM_SUPPORTED */
519 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
520
970f6abe 521 if (user_png_ver)
0272a10d 522 {
b61cc19c
PC
523 i = 0;
524 do
525 {
526 if (user_png_ver[i] != png_libpng_ver[i])
527 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
528 } while (png_libpng_ver[i++]);
970f6abe 529 }
0272a10d
VZ
530
531 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
532 {
533 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
534 * we must recompile any applications that use any older library version.
535 * For versions after libpng 1.0, we will be compatible, so we need
536 * only check the first digit.
537 */
538 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
539 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
540 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
541 {
b61cc19c 542#ifdef PNG_STDIO_SUPPORTED
0272a10d
VZ
543 char msg[80];
544 if (user_png_ver)
545 {
b61cc19c
PC
546 png_snprintf(msg, 80,
547 "Application was compiled with png.h from libpng-%.20s",
548 user_png_ver);
549 png_warning(png_ptr, msg);
0272a10d
VZ
550 }
551 png_snprintf(msg, 80,
552 "Application is running with png.c from libpng-%.20s",
553 png_libpng_ver);
554 png_warning(png_ptr, msg);
555#endif
556#ifdef PNG_ERROR_NUMBERS_SUPPORTED
b61cc19c 557 png_ptr->flags = 0;
0272a10d 558#endif
b61cc19c 559 png_warning(png_ptr,
0272a10d 560 "Incompatible libpng version in application and library");
b61cc19c 561 png_cleanup_needed = 1;
0272a10d
VZ
562 }
563 }
564
b61cc19c 565 /* Initialize zbuf - compression buffer */
0272a10d 566 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
b61cc19c 567 if (!png_cleanup_needed)
0272a10d 568 {
b61cc19c
PC
569 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
570 png_ptr->zbuf_size);
571 if (png_ptr->zbuf == NULL)
572 png_cleanup_needed = 1;
0272a10d 573 }
b61cc19c 574 if (png_cleanup_needed)
0272a10d 575 {
b61cc19c
PC
576 /* Clean up PNG structure and deallocate any memory. */
577 png_free(png_ptr, png_ptr->zbuf);
578 png_ptr->zbuf = NULL;
579#ifdef PNG_USER_MEM_SUPPORTED
580 png_destroy_struct_2((png_voidp)png_ptr,
581 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
0272a10d 582#else
b61cc19c 583 png_destroy_struct((png_voidp)png_ptr);
0272a10d 584#endif
b61cc19c
PC
585 return (NULL);
586 }
0272a10d 587
b61cc19c 588 png_set_write_fn(png_ptr, NULL, NULL, NULL);
0272a10d 589
b61cc19c 590#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
0272a10d 591 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
b61cc19c 592 1, NULL, NULL);
0272a10d 593#endif
b61cc19c
PC
594
595 return (png_ptr);
0272a10d
VZ
596}
597
b61cc19c 598
0272a10d
VZ
599/* Write a few rows of image data. If the image is interlaced,
600 * either you will have to write the 7 sub images, or, if you
601 * have called png_set_interlace_handling(), you will have to
602 * "write" the image seven times.
603 */
604void PNGAPI
605png_write_rows(png_structp png_ptr, png_bytepp row,
606 png_uint_32 num_rows)
607{
608 png_uint_32 i; /* row counter */
609 png_bytepp rp; /* row pointer */
610
970f6abe 611 png_debug(1, "in png_write_rows");
0272a10d
VZ
612
613 if (png_ptr == NULL)
614 return;
615
b61cc19c 616 /* Loop through the rows */
0272a10d
VZ
617 for (i = 0, rp = row; i < num_rows; i++, rp++)
618 {
619 png_write_row(png_ptr, *rp);
620 }
621}
622
623/* Write the image. You only need to call this function once, even
624 * if you are writing an interlaced image.
625 */
626void PNGAPI
627png_write_image(png_structp png_ptr, png_bytepp image)
628{
629 png_uint_32 i; /* row index */
630 int pass, num_pass; /* pass variables */
631 png_bytepp rp; /* points to current row */
632
633 if (png_ptr == NULL)
634 return;
635
970f6abe 636 png_debug(1, "in png_write_image");
b61cc19c
PC
637
638#ifdef PNG_WRITE_INTERLACING_SUPPORTED
639 /* Initialize interlace handling. If image is not interlaced,
640 * this will set pass to 1
641 */
0272a10d
VZ
642 num_pass = png_set_interlace_handling(png_ptr);
643#else
644 num_pass = 1;
645#endif
b61cc19c 646 /* Loop through passes */
0272a10d
VZ
647 for (pass = 0; pass < num_pass; pass++)
648 {
b61cc19c 649 /* Loop through image */
0272a10d
VZ
650 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
651 {
652 png_write_row(png_ptr, *rp);
653 }
654 }
655}
656
b61cc19c 657/* Called by user to write a row of image data */
0272a10d
VZ
658void PNGAPI
659png_write_row(png_structp png_ptr, png_bytep row)
660{
661 if (png_ptr == NULL)
662 return;
b61cc19c 663
970f6abe 664 png_debug2(1, "in png_write_row (row %ld, pass %d)",
0272a10d
VZ
665 png_ptr->row_number, png_ptr->pass);
666
b61cc19c 667 /* Initialize transformations and other stuff if first time */
0272a10d
VZ
668 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
669 {
b61cc19c
PC
670 /* Make sure we wrote the header info */
671 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
672 png_error(png_ptr,
673 "png_write_info was never called before png_write_row");
0272a10d 674
b61cc19c 675 /* Check for transforms that have been set but were defined out */
0272a10d 676#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
b61cc19c
PC
677 if (png_ptr->transformations & PNG_INVERT_MONO)
678 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
0272a10d
VZ
679#endif
680#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
b61cc19c
PC
681 if (png_ptr->transformations & PNG_FILLER)
682 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
0272a10d 683#endif
b61cc19c
PC
684#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
685 defined(PNG_READ_PACKSWAP_SUPPORTED)
686 if (png_ptr->transformations & PNG_PACKSWAP)
687 png_warning(png_ptr,
688 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
0272a10d
VZ
689#endif
690#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
b61cc19c
PC
691 if (png_ptr->transformations & PNG_PACK)
692 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
0272a10d
VZ
693#endif
694#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
b61cc19c
PC
695 if (png_ptr->transformations & PNG_SHIFT)
696 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
0272a10d
VZ
697#endif
698#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
b61cc19c
PC
699 if (png_ptr->transformations & PNG_BGR)
700 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
0272a10d
VZ
701#endif
702#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
b61cc19c
PC
703 if (png_ptr->transformations & PNG_SWAP_BYTES)
704 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
0272a10d
VZ
705#endif
706
707 png_write_start_row(png_ptr);
708 }
709
b61cc19c
PC
710#ifdef PNG_WRITE_INTERLACING_SUPPORTED
711 /* If interlaced and not interested in row, return */
0272a10d
VZ
712 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
713 {
714 switch (png_ptr->pass)
715 {
716 case 0:
717 if (png_ptr->row_number & 0x07)
718 {
719 png_write_finish_row(png_ptr);
720 return;
721 }
722 break;
723 case 1:
724 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
725 {
726 png_write_finish_row(png_ptr);
727 return;
728 }
729 break;
730 case 2:
731 if ((png_ptr->row_number & 0x07) != 4)
732 {
733 png_write_finish_row(png_ptr);
734 return;
735 }
736 break;
737 case 3:
738 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
739 {
740 png_write_finish_row(png_ptr);
741 return;
742 }
743 break;
744 case 4:
745 if ((png_ptr->row_number & 0x03) != 2)
746 {
747 png_write_finish_row(png_ptr);
748 return;
749 }
750 break;
751 case 5:
752 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
753 {
754 png_write_finish_row(png_ptr);
755 return;
756 }
757 break;
758 case 6:
759 if (!(png_ptr->row_number & 0x01))
760 {
761 png_write_finish_row(png_ptr);
762 return;
763 }
764 break;
765 }
766 }
767#endif
768
b61cc19c 769 /* Set up row info for transformations */
0272a10d
VZ
770 png_ptr->row_info.color_type = png_ptr->color_type;
771 png_ptr->row_info.width = png_ptr->usr_width;
772 png_ptr->row_info.channels = png_ptr->usr_channels;
773 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
774 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
775 png_ptr->row_info.channels);
776
777 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
778 png_ptr->row_info.width);
779
970f6abe
VZ
780 png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type);
781 png_debug1(3, "row_info->width = %lu", png_ptr->row_info.width);
782 png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels);
783 png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth);
784 png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth);
785 png_debug1(3, "row_info->rowbytes = %lu", png_ptr->row_info.rowbytes);
0272a10d
VZ
786
787 /* Copy user's row into buffer, leaving room for filter byte. */
b61cc19c 788 png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes);
0272a10d 789
b61cc19c
PC
790#ifdef PNG_WRITE_INTERLACING_SUPPORTED
791 /* Handle interlacing */
0272a10d
VZ
792 if (png_ptr->interlaced && png_ptr->pass < 6 &&
793 (png_ptr->transformations & PNG_INTERLACE))
794 {
795 png_do_write_interlace(&(png_ptr->row_info),
796 png_ptr->row_buf + 1, png_ptr->pass);
b61cc19c 797 /* This should always get caught above, but still ... */
0272a10d
VZ
798 if (!(png_ptr->row_info.width))
799 {
800 png_write_finish_row(png_ptr);
801 return;
802 }
803 }
804#endif
805
b61cc19c 806 /* Handle other transformations */
0272a10d
VZ
807 if (png_ptr->transformations)
808 png_do_write_transformations(png_ptr);
809
b61cc19c 810#ifdef PNG_MNG_FEATURES_SUPPORTED
0272a10d
VZ
811 /* Write filter_method 64 (intrapixel differencing) only if
812 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
813 * 2. Libpng did not write a PNG signature (this filter_method is only
814 * used in PNG datastreams that are embedded in MNG datastreams) and
815 * 3. The application called png_permit_mng_features with a mask that
816 * included PNG_FLAG_MNG_FILTER_64 and
817 * 4. The filter_method is 64 and
818 * 5. The color_type is RGB or RGBA
819 */
970f6abe 820 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
0272a10d
VZ
821 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
822 {
823 /* Intrapixel differencing */
824 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
825 }
826#endif
827
828 /* Find a filter if necessary, filter the row and write it out. */
829 png_write_find_filter(png_ptr, &(png_ptr->row_info));
830
831 if (png_ptr->write_row_fn != NULL)
832 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
833}
834
b61cc19c 835#ifdef PNG_WRITE_FLUSH_SUPPORTED
0272a10d
VZ
836/* Set the automatic flush interval or 0 to turn flushing off */
837void PNGAPI
838png_set_flush(png_structp png_ptr, int nrows)
839{
970f6abe 840 png_debug(1, "in png_set_flush");
b61cc19c 841
0272a10d
VZ
842 if (png_ptr == NULL)
843 return;
844 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
845}
846
b61cc19c 847/* Flush the current output buffers now */
0272a10d
VZ
848void PNGAPI
849png_write_flush(png_structp png_ptr)
850{
851 int wrote_IDAT;
852
970f6abe 853 png_debug(1, "in png_write_flush");
b61cc19c 854
0272a10d
VZ
855 if (png_ptr == NULL)
856 return;
857 /* We have already written out all of the data */
858 if (png_ptr->row_number >= png_ptr->num_rows)
b61cc19c 859 return;
0272a10d
VZ
860
861 do
862 {
863 int ret;
864
b61cc19c 865 /* Compress the data */
0272a10d
VZ
866 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
867 wrote_IDAT = 0;
868
b61cc19c 869 /* Check for compression errors */
0272a10d
VZ
870 if (ret != Z_OK)
871 {
872 if (png_ptr->zstream.msg != NULL)
873 png_error(png_ptr, png_ptr->zstream.msg);
874 else
875 png_error(png_ptr, "zlib error");
876 }
877
878 if (!(png_ptr->zstream.avail_out))
879 {
b61cc19c 880 /* Write the IDAT and reset the zlib output buffer */
0272a10d
VZ
881 png_write_IDAT(png_ptr, png_ptr->zbuf,
882 png_ptr->zbuf_size);
883 png_ptr->zstream.next_out = png_ptr->zbuf;
884 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
885 wrote_IDAT = 1;
886 }
887 } while(wrote_IDAT == 1);
888
889 /* If there is any data left to be output, write it into a new IDAT */
890 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
891 {
b61cc19c 892 /* Write the IDAT and reset the zlib output buffer */
0272a10d
VZ
893 png_write_IDAT(png_ptr, png_ptr->zbuf,
894 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
895 png_ptr->zstream.next_out = png_ptr->zbuf;
896 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
897 }
898 png_ptr->flush_rows = 0;
899 png_flush(png_ptr);
900}
901#endif /* PNG_WRITE_FLUSH_SUPPORTED */
902
b61cc19c 903/* Free all memory used by the write */
0272a10d
VZ
904void PNGAPI
905png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
906{
907 png_structp png_ptr = NULL;
908 png_infop info_ptr = NULL;
909#ifdef PNG_USER_MEM_SUPPORTED
910 png_free_ptr free_fn = NULL;
911 png_voidp mem_ptr = NULL;
912#endif
913
970f6abe 914 png_debug(1, "in png_destroy_write_struct");
b61cc19c 915
0272a10d
VZ
916 if (png_ptr_ptr != NULL)
917 {
918 png_ptr = *png_ptr_ptr;
919#ifdef PNG_USER_MEM_SUPPORTED
920 free_fn = png_ptr->free_fn;
921 mem_ptr = png_ptr->mem_ptr;
922#endif
923 }
924
970f6abe
VZ
925#ifdef PNG_USER_MEM_SUPPORTED
926 if (png_ptr != NULL)
927 {
928 free_fn = png_ptr->free_fn;
929 mem_ptr = png_ptr->mem_ptr;
930 }
931#endif
932
0272a10d
VZ
933 if (info_ptr_ptr != NULL)
934 info_ptr = *info_ptr_ptr;
935
936 if (info_ptr != NULL)
937 {
970f6abe
VZ
938 if (png_ptr != NULL)
939 {
940 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
0272a10d 941
b61cc19c 942#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
970f6abe
VZ
943 if (png_ptr->num_chunk_list)
944 {
945 png_free(png_ptr, png_ptr->chunk_list);
970f6abe
VZ
946 png_ptr->num_chunk_list = 0;
947 }
0272a10d 948#endif
970f6abe 949 }
0272a10d
VZ
950
951#ifdef PNG_USER_MEM_SUPPORTED
952 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
953 (png_voidp)mem_ptr);
954#else
955 png_destroy_struct((png_voidp)info_ptr);
956#endif
957 *info_ptr_ptr = NULL;
958 }
959
960 if (png_ptr != NULL)
961 {
962 png_write_destroy(png_ptr);
963#ifdef PNG_USER_MEM_SUPPORTED
964 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
965 (png_voidp)mem_ptr);
966#else
967 png_destroy_struct((png_voidp)png_ptr);
968#endif
969 *png_ptr_ptr = NULL;
970 }
971}
972
973
974/* Free any memory used in png_ptr struct (old method) */
975void /* PRIVATE */
976png_write_destroy(png_structp png_ptr)
977{
978#ifdef PNG_SETJMP_SUPPORTED
b61cc19c 979 jmp_buf tmp_jmp; /* Save jump buffer */
0272a10d
VZ
980#endif
981 png_error_ptr error_fn;
982 png_error_ptr warning_fn;
983 png_voidp error_ptr;
984#ifdef PNG_USER_MEM_SUPPORTED
985 png_free_ptr free_fn;
986#endif
987
970f6abe 988 png_debug(1, "in png_write_destroy");
b61cc19c
PC
989
990 /* Free any memory zlib uses */
0272a10d
VZ
991 deflateEnd(&png_ptr->zstream);
992
b61cc19c 993 /* Free our memory. png_free checks NULL for us. */
0272a10d
VZ
994 png_free(png_ptr, png_ptr->zbuf);
995 png_free(png_ptr, png_ptr->row_buf);
b61cc19c 996#ifdef PNG_WRITE_FILTER_SUPPORTED
0272a10d
VZ
997 png_free(png_ptr, png_ptr->prev_row);
998 png_free(png_ptr, png_ptr->sub_row);
999 png_free(png_ptr, png_ptr->up_row);
1000 png_free(png_ptr, png_ptr->avg_row);
1001 png_free(png_ptr, png_ptr->paeth_row);
970f6abe 1002#endif
0272a10d 1003
b61cc19c 1004#ifdef PNG_TIME_RFC1123_SUPPORTED
0272a10d
VZ
1005 png_free(png_ptr, png_ptr->time_buffer);
1006#endif
1007
b61cc19c 1008#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
0272a10d
VZ
1009 png_free(png_ptr, png_ptr->prev_filters);
1010 png_free(png_ptr, png_ptr->filter_weights);
1011 png_free(png_ptr, png_ptr->inv_filter_weights);
1012 png_free(png_ptr, png_ptr->filter_costs);
1013 png_free(png_ptr, png_ptr->inv_filter_costs);
1014#endif
1015
1016#ifdef PNG_SETJMP_SUPPORTED
b61cc19c 1017 /* Reset structure */
970f6abe 1018 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
0272a10d
VZ
1019#endif
1020
1021 error_fn = png_ptr->error_fn;
1022 warning_fn = png_ptr->warning_fn;
1023 error_ptr = png_ptr->error_ptr;
1024#ifdef PNG_USER_MEM_SUPPORTED
1025 free_fn = png_ptr->free_fn;
1026#endif
1027
970f6abe 1028 png_memset(png_ptr, 0, png_sizeof(png_struct));
0272a10d
VZ
1029
1030 png_ptr->error_fn = error_fn;
1031 png_ptr->warning_fn = warning_fn;
1032 png_ptr->error_ptr = error_ptr;
1033#ifdef PNG_USER_MEM_SUPPORTED
1034 png_ptr->free_fn = free_fn;
1035#endif
1036
1037#ifdef PNG_SETJMP_SUPPORTED
970f6abe 1038 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
0272a10d
VZ
1039#endif
1040}
1041
1042/* Allow the application to select one or more row filters to use. */
1043void PNGAPI
1044png_set_filter(png_structp png_ptr, int method, int filters)
1045{
970f6abe 1046 png_debug(1, "in png_set_filter");
b61cc19c 1047
0272a10d
VZ
1048 if (png_ptr == NULL)
1049 return;
b61cc19c 1050#ifdef PNG_MNG_FEATURES_SUPPORTED
970f6abe 1051 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
0272a10d
VZ
1052 (method == PNG_INTRAPIXEL_DIFFERENCING))
1053 method = PNG_FILTER_TYPE_BASE;
1054#endif
1055 if (method == PNG_FILTER_TYPE_BASE)
1056 {
1057 switch (filters & (PNG_ALL_FILTERS | 0x07))
1058 {
b61cc19c 1059#ifdef PNG_WRITE_FILTER_SUPPORTED
0272a10d
VZ
1060 case 5:
1061 case 6:
1062 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
b61cc19c 1063#endif /* PNG_WRITE_FILTER_SUPPORTED */
0272a10d 1064 case PNG_FILTER_VALUE_NONE:
b61cc19c
PC
1065 png_ptr->do_filter = PNG_FILTER_NONE; break;
1066#ifdef PNG_WRITE_FILTER_SUPPORTED
0272a10d 1067 case PNG_FILTER_VALUE_SUB:
b61cc19c 1068 png_ptr->do_filter = PNG_FILTER_SUB; break;
0272a10d 1069 case PNG_FILTER_VALUE_UP:
b61cc19c 1070 png_ptr->do_filter = PNG_FILTER_UP; break;
0272a10d 1071 case PNG_FILTER_VALUE_AVG:
b61cc19c 1072 png_ptr->do_filter = PNG_FILTER_AVG; break;
0272a10d 1073 case PNG_FILTER_VALUE_PAETH:
b61cc19c 1074 png_ptr->do_filter = PNG_FILTER_PAETH; break;
0272a10d
VZ
1075 default: png_ptr->do_filter = (png_byte)filters; break;
1076#else
1077 default: png_warning(png_ptr, "Unknown row filter for method 0");
b61cc19c 1078#endif /* PNG_WRITE_FILTER_SUPPORTED */
0272a10d
VZ
1079 }
1080
1081 /* If we have allocated the row_buf, this means we have already started
1082 * with the image and we should have allocated all of the filter buffers
1083 * that have been selected. If prev_row isn't already allocated, then
1084 * it is too late to start using the filters that need it, since we
1085 * will be missing the data in the previous row. If an application
1086 * wants to start and stop using particular filters during compression,
1087 * it should start out with all of the filters, and then add and
1088 * remove them after the start of compression.
1089 */
1090 if (png_ptr->row_buf != NULL)
1091 {
b61cc19c 1092#ifdef PNG_WRITE_FILTER_SUPPORTED
0272a10d
VZ
1093 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
1094 {
1095 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
1096 (png_ptr->rowbytes + 1));
1097 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1098 }
1099
1100 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
1101 {
1102 if (png_ptr->prev_row == NULL)
1103 {
1104 png_warning(png_ptr, "Can't add Up filter after starting");
1105 png_ptr->do_filter &= ~PNG_FILTER_UP;
1106 }
1107 else
1108 {
1109 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
1110 (png_ptr->rowbytes + 1));
1111 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1112 }
1113 }
1114
1115 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
1116 {
1117 if (png_ptr->prev_row == NULL)
1118 {
1119 png_warning(png_ptr, "Can't add Average filter after starting");
1120 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1121 }
1122 else
1123 {
1124 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1125 (png_ptr->rowbytes + 1));
1126 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
1127 }
1128 }
1129
1130 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
1131 png_ptr->paeth_row == NULL)
1132 {
1133 if (png_ptr->prev_row == NULL)
1134 {
1135 png_warning(png_ptr, "Can't add Paeth filter after starting");
1136 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
1137 }
1138 else
1139 {
1140 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
1141 (png_ptr->rowbytes + 1));
1142 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1143 }
1144 }
1145
1146 if (png_ptr->do_filter == PNG_NO_FILTERS)
b61cc19c 1147#endif /* PNG_WRITE_FILTER_SUPPORTED */
0272a10d
VZ
1148 png_ptr->do_filter = PNG_FILTER_NONE;
1149 }
1150 }
1151 else
1152 png_error(png_ptr, "Unknown custom filter method");
1153}
1154
1155/* This allows us to influence the way in which libpng chooses the "best"
1156 * filter for the current scanline. While the "minimum-sum-of-absolute-
1157 * differences metric is relatively fast and effective, there is some
1158 * question as to whether it can be improved upon by trying to keep the
1159 * filtered data going to zlib more consistent, hopefully resulting in
1160 * better compression.
1161 */
b61cc19c 1162#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
0272a10d
VZ
1163void PNGAPI
1164png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1165 int num_weights, png_doublep filter_weights,
1166 png_doublep filter_costs)
1167{
1168 int i;
1169
970f6abe 1170 png_debug(1, "in png_set_filter_heuristics");
b61cc19c 1171
0272a10d
VZ
1172 if (png_ptr == NULL)
1173 return;
1174 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1175 {
1176 png_warning(png_ptr, "Unknown filter heuristic method");
1177 return;
1178 }
1179
1180 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1181 {
1182 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1183 }
1184
1185 if (num_weights < 0 || filter_weights == NULL ||
1186 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1187 {
1188 num_weights = 0;
1189 }
1190
1191 png_ptr->num_prev_filters = (png_byte)num_weights;
1192 png_ptr->heuristic_method = (png_byte)heuristic_method;
1193
1194 if (num_weights > 0)
1195 {
1196 if (png_ptr->prev_filters == NULL)
1197 {
1198 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
1199 (png_uint_32)(png_sizeof(png_byte) * num_weights));
1200
1201 /* To make sure that the weighting starts out fairly */
1202 for (i = 0; i < num_weights; i++)
1203 {
1204 png_ptr->prev_filters[i] = 255;
1205 }
1206 }
1207
1208 if (png_ptr->filter_weights == NULL)
1209 {
1210 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
1211 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1212
1213 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
1214 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1215 for (i = 0; i < num_weights; i++)
1216 {
1217 png_ptr->inv_filter_weights[i] =
1218 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1219 }
1220 }
1221
1222 for (i = 0; i < num_weights; i++)
1223 {
1224 if (filter_weights[i] < 0.0)
1225 {
1226 png_ptr->inv_filter_weights[i] =
1227 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1228 }
1229 else
1230 {
1231 png_ptr->inv_filter_weights[i] =
1232 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1233 png_ptr->filter_weights[i] =
1234 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1235 }
1236 }
1237 }
1238
1239 /* If, in the future, there are other filter methods, this would
1240 * need to be based on png_ptr->filter.
1241 */
1242 if (png_ptr->filter_costs == NULL)
1243 {
1244 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
1245 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1246
1247 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
1248 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1249
1250 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1251 {
1252 png_ptr->inv_filter_costs[i] =
1253 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1254 }
1255 }
1256
1257 /* Here is where we set the relative costs of the different filters. We
1258 * should take the desired compression level into account when setting
1259 * the costs, so that Paeth, for instance, has a high relative cost at low
1260 * compression levels, while it has a lower relative cost at higher
1261 * compression settings. The filter types are in order of increasing
1262 * relative cost, so it would be possible to do this with an algorithm.
1263 */
1264 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1265 {
1266 if (filter_costs == NULL || filter_costs[i] < 0.0)
1267 {
1268 png_ptr->inv_filter_costs[i] =
1269 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1270 }
1271 else if (filter_costs[i] >= 1.0)
1272 {
1273 png_ptr->inv_filter_costs[i] =
1274 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1275 png_ptr->filter_costs[i] =
1276 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1277 }
1278 }
1279}
1280#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1281
1282void PNGAPI
1283png_set_compression_level(png_structp png_ptr, int level)
1284{
970f6abe 1285 png_debug(1, "in png_set_compression_level");
b61cc19c 1286
0272a10d
VZ
1287 if (png_ptr == NULL)
1288 return;
1289 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
1290 png_ptr->zlib_level = level;
1291}
1292
1293void PNGAPI
1294png_set_compression_mem_level(png_structp png_ptr, int mem_level)
1295{
970f6abe 1296 png_debug(1, "in png_set_compression_mem_level");
b61cc19c 1297
0272a10d
VZ
1298 if (png_ptr == NULL)
1299 return;
1300 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
1301 png_ptr->zlib_mem_level = mem_level;
1302}
1303
1304void PNGAPI
1305png_set_compression_strategy(png_structp png_ptr, int strategy)
1306{
970f6abe 1307 png_debug(1, "in png_set_compression_strategy");
b61cc19c 1308
0272a10d
VZ
1309 if (png_ptr == NULL)
1310 return;
1311 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
1312 png_ptr->zlib_strategy = strategy;
1313}
1314
1315void PNGAPI
1316png_set_compression_window_bits(png_structp png_ptr, int window_bits)
1317{
1318 if (png_ptr == NULL)
1319 return;
1320 if (window_bits > 15)
1321 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1322 else if (window_bits < 8)
1323 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1324#ifndef WBITS_8_OK
b61cc19c 1325 /* Avoid libpng bug with 256-byte windows */
0272a10d
VZ
1326 if (window_bits == 8)
1327 {
1328 png_warning(png_ptr, "Compression window is being reset to 512");
b61cc19c 1329 window_bits = 9;
0272a10d
VZ
1330 }
1331#endif
1332 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
1333 png_ptr->zlib_window_bits = window_bits;
1334}
1335
1336void PNGAPI
1337png_set_compression_method(png_structp png_ptr, int method)
1338{
970f6abe 1339 png_debug(1, "in png_set_compression_method");
b61cc19c 1340
0272a10d
VZ
1341 if (png_ptr == NULL)
1342 return;
1343 if (method != 8)
1344 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1345 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
1346 png_ptr->zlib_method = method;
1347}
1348
1349void PNGAPI
1350png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1351{
1352 if (png_ptr == NULL)
1353 return;
1354 png_ptr->write_row_fn = write_row_fn;
1355}
1356
b61cc19c 1357#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
0272a10d
VZ
1358void PNGAPI
1359png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1360 write_user_transform_fn)
1361{
970f6abe 1362 png_debug(1, "in png_set_write_user_transform_fn");
b61cc19c 1363
0272a10d
VZ
1364 if (png_ptr == NULL)
1365 return;
1366 png_ptr->transformations |= PNG_USER_TRANSFORM;
1367 png_ptr->write_user_transform_fn = write_user_transform_fn;
1368}
1369#endif
1370
1371
b61cc19c 1372#ifdef PNG_INFO_IMAGE_SUPPORTED
0272a10d
VZ
1373void PNGAPI
1374png_write_png(png_structp png_ptr, png_infop info_ptr,
1375 int transforms, voidp params)
1376{
1377 if (png_ptr == NULL || info_ptr == NULL)
1378 return;
0272a10d
VZ
1379
1380 /* Write the file header information. */
1381 png_write_info(png_ptr, info_ptr);
1382
1383 /* ------ these transformations don't touch the info structure ------- */
1384
b61cc19c
PC
1385#ifdef PNG_WRITE_INVERT_SUPPORTED
1386 /* Invert monochrome pixels */
0272a10d 1387 if (transforms & PNG_TRANSFORM_INVERT_MONO)
b61cc19c 1388 png_set_invert_mono(png_ptr);
0272a10d
VZ
1389#endif
1390
b61cc19c 1391#ifdef PNG_WRITE_SHIFT_SUPPORTED
0272a10d
VZ
1392 /* Shift the pixels up to a legal bit depth and fill in
1393 * as appropriate to correctly scale the image.
1394 */
1395 if ((transforms & PNG_TRANSFORM_SHIFT)
1396 && (info_ptr->valid & PNG_INFO_sBIT))
b61cc19c 1397 png_set_shift(png_ptr, &info_ptr->sig_bit);
0272a10d
VZ
1398#endif
1399
b61cc19c
PC
1400#ifdef PNG_WRITE_PACK_SUPPORTED
1401 /* Pack pixels into bytes */
0272a10d
VZ
1402 if (transforms & PNG_TRANSFORM_PACKING)
1403 png_set_packing(png_ptr);
1404#endif
1405
b61cc19c
PC
1406#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
1407 /* Swap location of alpha bytes from ARGB to RGBA */
0272a10d 1408 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
b61cc19c 1409 png_set_swap_alpha(png_ptr);
0272a10d
VZ
1410#endif
1411
b61cc19c 1412#ifdef PNG_WRITE_FILLER_SUPPORTED
970f6abe 1413 /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */
b61cc19c 1414 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
970f6abe 1415 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
b61cc19c 1416 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
970f6abe 1417 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
0272a10d
VZ
1418#endif
1419
b61cc19c
PC
1420#ifdef PNG_WRITE_BGR_SUPPORTED
1421 /* Flip BGR pixels to RGB */
0272a10d 1422 if (transforms & PNG_TRANSFORM_BGR)
b61cc19c 1423 png_set_bgr(png_ptr);
0272a10d
VZ
1424#endif
1425
b61cc19c
PC
1426#ifdef PNG_WRITE_SWAP_SUPPORTED
1427 /* Swap bytes of 16-bit files to most significant byte first */
0272a10d 1428 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
b61cc19c 1429 png_set_swap(png_ptr);
0272a10d
VZ
1430#endif
1431
b61cc19c
PC
1432#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
1433 /* Swap bits of 1, 2, 4 bit packed pixel formats */
0272a10d 1434 if (transforms & PNG_TRANSFORM_PACKSWAP)
b61cc19c
PC
1435 png_set_packswap(png_ptr);
1436#endif
1437
1438#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
1439 /* Invert the alpha channel from opacity to transparency */
1440 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1441 png_set_invert_alpha(png_ptr);
0272a10d
VZ
1442#endif
1443
1444 /* ----------------------- end of transformations ------------------- */
1445
b61cc19c 1446 /* Write the bits */
0272a10d
VZ
1447 if (info_ptr->valid & PNG_INFO_IDAT)
1448 png_write_image(png_ptr, info_ptr->row_pointers);
1449
1450 /* It is REQUIRED to call this to finish writing the rest of the file */
1451 png_write_end(png_ptr, info_ptr);
1452
b61cc19c 1453 transforms = transforms; /* Quiet compiler warnings */
0272a10d
VZ
1454 params = params;
1455}
1456#endif
1457#endif /* PNG_WRITE_SUPPORTED */