Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / libimg / png / pngwrite.c
blob9f11d7469d4564c222b3d51c79a433f49cc8986c
2 /* pngwrite.c - general routines to write a PNG file
4 * Last changed in libpng 1.2.31 [August 21, 2008]
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 */
11 /* get internal access to png.h */
12 #define PNG_INTERNAL
13 #include "png.h"
14 #ifdef PNG_WRITE_SUPPORTED
16 /* Writes all the PNG information. This is the suggested way to use the
17 * library. If you have a new chunk to add, make a function to write it,
18 * and put it in the correct location here. If you want the chunk written
19 * after the image data, put it in png_write_end(). I strongly encourage
20 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
21 * the chunk, as that will keep the code from breaking if you want to just
22 * write a plain PNG file. If you have long comments, I suggest writing
23 * them in png_write_end(), and compressing them.
25 void PNGAPI
26 png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
28 png_debug(1, "in png_write_info_before_PLTE\n");
29 if (png_ptr == NULL || info_ptr == NULL)
30 return;
31 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
33 png_write_sig(png_ptr); /* write PNG signature */
34 #if defined(PNG_MNG_FEATURES_SUPPORTED)
35 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
37 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
38 png_ptr->mng_features_permitted=0;
40 #endif
41 /* write IHDR information. */
42 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
43 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
44 info_ptr->filter_type,
45 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
46 info_ptr->interlace_type);
47 #else
48 0);
49 #endif
50 /* the rest of these check to see if the valid field has the appropriate
51 flag set, and if it does, writes the chunk. */
52 #if defined(PNG_WRITE_gAMA_SUPPORTED)
53 if (info_ptr->valid & PNG_INFO_gAMA)
55 # ifdef PNG_FLOATING_POINT_SUPPORTED
56 png_write_gAMA(png_ptr, info_ptr->gamma);
57 #else
58 #ifdef PNG_FIXED_POINT_SUPPORTED
59 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
60 # endif
61 #endif
63 #endif
64 #if defined(PNG_WRITE_sRGB_SUPPORTED)
65 if (info_ptr->valid & PNG_INFO_sRGB)
66 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
67 #endif
68 #if defined(PNG_WRITE_iCCP_SUPPORTED)
69 if (info_ptr->valid & PNG_INFO_iCCP)
70 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
71 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
72 #endif
73 #if defined(PNG_WRITE_sBIT_SUPPORTED)
74 if (info_ptr->valid & PNG_INFO_sBIT)
75 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
76 #endif
77 #if defined(PNG_WRITE_cHRM_SUPPORTED)
78 if (info_ptr->valid & PNG_INFO_cHRM)
80 #ifdef PNG_FLOATING_POINT_SUPPORTED
81 png_write_cHRM(png_ptr,
82 info_ptr->x_white, info_ptr->y_white,
83 info_ptr->x_red, info_ptr->y_red,
84 info_ptr->x_green, info_ptr->y_green,
85 info_ptr->x_blue, info_ptr->y_blue);
86 #else
87 # ifdef PNG_FIXED_POINT_SUPPORTED
88 png_write_cHRM_fixed(png_ptr,
89 info_ptr->int_x_white, info_ptr->int_y_white,
90 info_ptr->int_x_red, info_ptr->int_y_red,
91 info_ptr->int_x_green, info_ptr->int_y_green,
92 info_ptr->int_x_blue, info_ptr->int_y_blue);
93 # endif
94 #endif
96 #endif
97 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
98 if (info_ptr->unknown_chunks_num)
100 png_unknown_chunk *up;
102 png_debug(5, "writing extra chunks\n");
104 for (up = info_ptr->unknown_chunks;
105 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
106 up++)
108 int keep=png_handle_as_unknown(png_ptr, up->name);
109 if (keep != PNG_HANDLE_CHUNK_NEVER &&
110 up->location && !(up->location & PNG_HAVE_PLTE) &&
111 !(up->location & PNG_HAVE_IDAT) &&
112 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
113 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
115 if (up->size == 0)
116 png_warning(png_ptr, "Writing zero-length unknown chunk");
117 png_write_chunk(png_ptr, up->name, up->data, up->size);
121 #endif
122 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
126 void PNGAPI
127 png_write_info(png_structp png_ptr, png_infop info_ptr)
129 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
130 int i;
131 #endif
133 png_debug(1, "in png_write_info\n");
135 if (png_ptr == NULL || info_ptr == NULL)
136 return;
138 png_write_info_before_PLTE(png_ptr, info_ptr);
140 if (info_ptr->valid & PNG_INFO_PLTE)
141 png_write_PLTE(png_ptr, info_ptr->palette,
142 (png_uint_32)info_ptr->num_palette);
143 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
144 png_error(png_ptr, "Valid palette required for paletted images");
146 #if defined(PNG_WRITE_tRNS_SUPPORTED)
147 if (info_ptr->valid & PNG_INFO_tRNS)
149 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
150 /* invert the alpha channel (in tRNS) */
151 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
152 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
154 int j;
155 for (j=0; j<(int)info_ptr->num_trans; j++)
156 info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
158 #endif
159 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
160 info_ptr->num_trans, info_ptr->color_type);
162 #endif
163 #if defined(PNG_WRITE_bKGD_SUPPORTED)
164 if (info_ptr->valid & PNG_INFO_bKGD)
165 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
166 #endif
167 #if defined(PNG_WRITE_hIST_SUPPORTED)
168 if (info_ptr->valid & PNG_INFO_hIST)
169 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
170 #endif
171 #if defined(PNG_WRITE_oFFs_SUPPORTED)
172 if (info_ptr->valid & PNG_INFO_oFFs)
173 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
174 info_ptr->offset_unit_type);
175 #endif
176 #if defined(PNG_WRITE_pCAL_SUPPORTED)
177 if (info_ptr->valid & PNG_INFO_pCAL)
178 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
179 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
180 info_ptr->pcal_units, info_ptr->pcal_params);
181 #endif
182 #if defined(PNG_WRITE_sCAL_SUPPORTED)
183 if (info_ptr->valid & PNG_INFO_sCAL)
184 #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
185 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
186 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
187 #else
188 #ifdef PNG_FIXED_POINT_SUPPORTED
189 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
190 info_ptr->scal_s_width, info_ptr->scal_s_height);
191 #else
192 png_warning(png_ptr,
193 "png_write_sCAL not supported; sCAL chunk not written.");
194 #endif
195 #endif
196 #endif
197 #if defined(PNG_WRITE_pHYs_SUPPORTED)
198 if (info_ptr->valid & PNG_INFO_pHYs)
199 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
200 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
201 #endif
202 #if defined(PNG_WRITE_tIME_SUPPORTED)
203 if (info_ptr->valid & PNG_INFO_tIME)
205 png_write_tIME(png_ptr, &(info_ptr->mod_time));
206 png_ptr->mode |= PNG_WROTE_tIME;
208 #endif
209 #if defined(PNG_WRITE_sPLT_SUPPORTED)
210 if (info_ptr->valid & PNG_INFO_sPLT)
211 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
212 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
213 #endif
214 #if defined(PNG_WRITE_TEXT_SUPPORTED)
215 /* Check to see if we need to write text chunks */
216 for (i = 0; i < info_ptr->num_text; i++)
218 png_debug2(2, "Writing header text chunk %d, type %d\n", i,
219 info_ptr->text[i].compression);
220 /* an internationalized chunk? */
221 if (info_ptr->text[i].compression > 0)
223 #if defined(PNG_WRITE_iTXt_SUPPORTED)
224 /* write international chunk */
225 png_write_iTXt(png_ptr,
226 info_ptr->text[i].compression,
227 info_ptr->text[i].key,
228 info_ptr->text[i].lang,
229 info_ptr->text[i].lang_key,
230 info_ptr->text[i].text);
231 #else
232 png_warning(png_ptr, "Unable to write international text");
233 #endif
234 /* Mark this chunk as written */
235 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
237 /* If we want a compressed text chunk */
238 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
240 #if defined(PNG_WRITE_zTXt_SUPPORTED)
241 /* write compressed chunk */
242 png_write_zTXt(png_ptr, info_ptr->text[i].key,
243 info_ptr->text[i].text, 0,
244 info_ptr->text[i].compression);
245 #else
246 png_warning(png_ptr, "Unable to write compressed text");
247 #endif
248 /* Mark this chunk as written */
249 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
251 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
253 #if defined(PNG_WRITE_tEXt_SUPPORTED)
254 /* write uncompressed chunk */
255 png_write_tEXt(png_ptr, info_ptr->text[i].key,
256 info_ptr->text[i].text,
258 #else
259 png_warning(png_ptr, "Unable to write uncompressed text");
260 #endif
261 /* Mark this chunk as written */
262 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
265 #endif
266 #if defined(PNG_WRITE_APNG_SUPPORTED)
267 if (info_ptr->valid & PNG_INFO_acTL)
268 png_write_acTL(png_ptr, info_ptr->num_frames, info_ptr->num_plays);
269 #endif
270 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
271 if (info_ptr->unknown_chunks_num)
273 png_unknown_chunk *up;
275 png_debug(5, "writing extra chunks\n");
277 for (up = info_ptr->unknown_chunks;
278 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
279 up++)
281 int keep=png_handle_as_unknown(png_ptr, up->name);
282 if (keep != PNG_HANDLE_CHUNK_NEVER &&
283 up->location && (up->location & PNG_HAVE_PLTE) &&
284 !(up->location & PNG_HAVE_IDAT) &&
285 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
286 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
288 png_write_chunk(png_ptr, up->name, up->data, up->size);
292 #endif
295 /* Writes the end of the PNG file. If you don't want to write comments or
296 * time information, you can pass NULL for info. If you already wrote these
297 * in png_write_info(), do not write them again here. If you have long
298 * comments, I suggest writing them here, and compressing them.
300 void PNGAPI
301 png_write_end(png_structp png_ptr, png_infop info_ptr)
303 png_debug(1, "in png_write_end\n");
304 if (png_ptr == NULL)
305 return;
306 if (!(png_ptr->mode & PNG_HAVE_IDAT))
307 png_error(png_ptr, "No IDATs written into file");
308 #if defined(PNG_WRITE_APNG_SUPPORTED)
309 if (png_ptr->num_frames_written != png_ptr->num_frames_to_write)
310 png_error(png_ptr, "Not enough frames written");
311 #endif
313 /* see if user wants us to write information chunks */
314 if (info_ptr != NULL)
316 #if defined(PNG_WRITE_TEXT_SUPPORTED)
317 int i; /* local index variable */
318 #endif
319 #if defined(PNG_WRITE_tIME_SUPPORTED)
320 /* check to see if user has supplied a time chunk */
321 if ((info_ptr->valid & PNG_INFO_tIME) &&
322 !(png_ptr->mode & PNG_WROTE_tIME))
323 png_write_tIME(png_ptr, &(info_ptr->mod_time));
324 #endif
325 #if defined(PNG_WRITE_TEXT_SUPPORTED)
326 /* loop through comment chunks */
327 for (i = 0; i < info_ptr->num_text; i++)
329 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
330 info_ptr->text[i].compression);
331 /* an internationalized chunk? */
332 if (info_ptr->text[i].compression > 0)
334 #if defined(PNG_WRITE_iTXt_SUPPORTED)
335 /* write international chunk */
336 png_write_iTXt(png_ptr,
337 info_ptr->text[i].compression,
338 info_ptr->text[i].key,
339 info_ptr->text[i].lang,
340 info_ptr->text[i].lang_key,
341 info_ptr->text[i].text);
342 #else
343 png_warning(png_ptr, "Unable to write international text");
344 #endif
345 /* Mark this chunk as written */
346 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
348 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
350 #if defined(PNG_WRITE_zTXt_SUPPORTED)
351 /* write compressed chunk */
352 png_write_zTXt(png_ptr, info_ptr->text[i].key,
353 info_ptr->text[i].text, 0,
354 info_ptr->text[i].compression);
355 #else
356 png_warning(png_ptr, "Unable to write compressed text");
357 #endif
358 /* Mark this chunk as written */
359 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
361 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
363 #if defined(PNG_WRITE_tEXt_SUPPORTED)
364 /* write uncompressed chunk */
365 png_write_tEXt(png_ptr, info_ptr->text[i].key,
366 info_ptr->text[i].text, 0);
367 #else
368 png_warning(png_ptr, "Unable to write uncompressed text");
369 #endif
371 /* Mark this chunk as written */
372 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
375 #endif
376 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
377 if (info_ptr->unknown_chunks_num)
379 png_unknown_chunk *up;
381 png_debug(5, "writing extra chunks\n");
383 for (up = info_ptr->unknown_chunks;
384 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
385 up++)
387 int keep=png_handle_as_unknown(png_ptr, up->name);
388 if (keep != PNG_HANDLE_CHUNK_NEVER &&
389 up->location && (up->location & PNG_AFTER_IDAT) &&
390 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
391 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
393 png_write_chunk(png_ptr, up->name, up->data, up->size);
397 #endif
400 png_ptr->mode |= PNG_AFTER_IDAT;
402 /* write end of PNG file */
403 png_write_IEND(png_ptr);
404 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
405 * and restored again in libpng-1.2.30, may cause some applications that
406 * do not set png_ptr->output_flush_fn to crash. If your application
407 * experiences a problem, please try building libpng with
408 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
409 * png-mng-implement at lists.sf.net . This kludge will be removed
410 * from libpng-1.4.0.
412 #if defined(PNG_WRITE_FLUSH_SUPPORTED) && \
413 defined(PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED)
414 png_flush(png_ptr);
415 #endif
418 #if defined(PNG_WRITE_tIME_SUPPORTED)
419 #if !defined(_WIN32_WCE)
420 /* "time.h" functions are not supported on WindowsCE */
421 void PNGAPI
422 png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
424 png_debug(1, "in png_convert_from_struct_tm\n");
425 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
426 ptime->month = (png_byte)(ttime->tm_mon + 1);
427 ptime->day = (png_byte)ttime->tm_mday;
428 ptime->hour = (png_byte)ttime->tm_hour;
429 ptime->minute = (png_byte)ttime->tm_min;
430 ptime->second = (png_byte)ttime->tm_sec;
433 void PNGAPI
434 png_convert_from_time_t(png_timep ptime, time_t ttime)
436 struct tm *tbuf;
438 png_debug(1, "in png_convert_from_time_t\n");
439 tbuf = gmtime(&ttime);
440 png_convert_from_struct_tm(ptime, tbuf);
442 #endif
443 #endif
445 /* Initialize png_ptr structure, and allocate any memory needed */
446 png_structp PNGAPI
447 png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
448 png_error_ptr error_fn, png_error_ptr warn_fn)
450 #ifdef PNG_USER_MEM_SUPPORTED
451 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
452 warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
455 /* Alternate initialize png_ptr structure, and allocate any memory needed */
456 png_structp PNGAPI
457 png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
458 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
459 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
461 #endif /* PNG_USER_MEM_SUPPORTED */
462 #ifdef PNG_SETJMP_SUPPORTED
463 volatile
464 #endif
465 png_structp png_ptr;
466 #ifdef PNG_SETJMP_SUPPORTED
467 #ifdef USE_FAR_KEYWORD
468 jmp_buf jmpbuf;
469 #endif
470 #endif
471 int i;
472 png_debug(1, "in png_create_write_struct\n");
473 #ifdef PNG_USER_MEM_SUPPORTED
474 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
475 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
476 #else
477 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
478 #endif /* PNG_USER_MEM_SUPPORTED */
479 if (png_ptr == NULL)
480 return (NULL);
482 /* added at libpng-1.2.6 */
483 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
484 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
485 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
486 #endif
488 #ifdef PNG_SETJMP_SUPPORTED
489 #ifdef USE_FAR_KEYWORD
490 if (setjmp(jmpbuf))
491 #else
492 if (setjmp(png_ptr->jmpbuf))
493 #endif
495 png_free(png_ptr, png_ptr->zbuf);
496 png_ptr->zbuf=NULL;
497 png_destroy_struct(png_ptr);
498 return (NULL);
500 #ifdef USE_FAR_KEYWORD
501 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
502 #endif
503 #endif
505 #ifdef PNG_USER_MEM_SUPPORTED
506 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
507 #endif /* PNG_USER_MEM_SUPPORTED */
508 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
510 if (user_png_ver)
512 i=0;
515 if (user_png_ver[i] != png_libpng_ver[i])
516 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
517 } while (png_libpng_ver[i++]);
520 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
522 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
523 * we must recompile any applications that use any older library version.
524 * For versions after libpng 1.0, we will be compatible, so we need
525 * only check the first digit.
527 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
528 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
529 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
531 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
532 char msg[80];
533 if (user_png_ver)
535 png_snprintf(msg, 80,
536 "Application was compiled with png.h from libpng-%.20s",
537 user_png_ver);
538 png_warning(png_ptr, msg);
540 png_snprintf(msg, 80,
541 "Application is running with png.c from libpng-%.20s",
542 png_libpng_ver);
543 png_warning(png_ptr, msg);
544 #endif
545 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
546 png_ptr->flags=0;
547 #endif
548 png_error(png_ptr,
549 "Incompatible libpng version in application and library");
553 /* initialize zbuf - compression buffer */
554 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
555 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
556 (png_uint_32)png_ptr->zbuf_size);
558 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
559 png_flush_ptr_NULL);
561 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
562 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
563 1, png_doublep_NULL, png_doublep_NULL);
564 #endif
566 #ifdef PNG_SETJMP_SUPPORTED
567 /* Applications that neglect to set up their own setjmp() and then encounter
568 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
569 abort instead of returning. */
570 #ifdef USE_FAR_KEYWORD
571 if (setjmp(jmpbuf))
572 PNG_ABORT();
573 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
574 #else
575 if (setjmp(png_ptr->jmpbuf))
576 PNG_ABORT();
577 #endif
578 #endif
579 return (png_ptr);
582 /* Initialize png_ptr structure, and allocate any memory needed */
583 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
584 /* Deprecated. */
585 #undef png_write_init
586 void PNGAPI
587 png_write_init(png_structp png_ptr)
589 /* We only come here via pre-1.0.7-compiled applications */
590 png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
593 void PNGAPI
594 png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
595 png_size_t png_struct_size, png_size_t png_info_size)
597 /* We only come here via pre-1.0.12-compiled applications */
598 if (png_ptr == NULL) return;
599 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
600 if (png_sizeof(png_struct) > png_struct_size ||
601 png_sizeof(png_info) > png_info_size)
603 char msg[80];
604 png_ptr->warning_fn=NULL;
605 if (user_png_ver)
607 png_snprintf(msg, 80,
608 "Application was compiled with png.h from libpng-%.20s",
609 user_png_ver);
610 png_warning(png_ptr, msg);
612 png_snprintf(msg, 80,
613 "Application is running with png.c from libpng-%.20s",
614 png_libpng_ver);
615 png_warning(png_ptr, msg);
617 #endif
618 if (png_sizeof(png_struct) > png_struct_size)
620 png_ptr->error_fn=NULL;
621 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
622 png_ptr->flags=0;
623 #endif
624 png_error(png_ptr,
625 "The png struct allocated by the application for writing is too small.");
627 if (png_sizeof(png_info) > png_info_size)
629 png_ptr->error_fn=NULL;
630 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
631 png_ptr->flags=0;
632 #endif
633 png_error(png_ptr,
634 "The info struct allocated by the application for writing is too small.");
636 png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
638 #endif /* PNG_1_0_X || PNG_1_2_X */
641 void PNGAPI
642 png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
643 png_size_t png_struct_size)
645 png_structp png_ptr=*ptr_ptr;
646 #ifdef PNG_SETJMP_SUPPORTED
647 jmp_buf tmp_jmp; /* to save current jump buffer */
648 #endif
650 int i = 0;
652 if (png_ptr == NULL)
653 return;
657 if (user_png_ver[i] != png_libpng_ver[i])
659 #ifdef PNG_LEGACY_SUPPORTED
660 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
661 #else
662 png_ptr->warning_fn=NULL;
663 png_warning(png_ptr,
664 "Application uses deprecated png_write_init() and should be recompiled.");
665 break;
666 #endif
668 } while (png_libpng_ver[i++]);
670 png_debug(1, "in png_write_init_3\n");
672 #ifdef PNG_SETJMP_SUPPORTED
673 /* save jump buffer and error functions */
674 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
675 #endif
677 if (png_sizeof(png_struct) > png_struct_size)
679 png_destroy_struct(png_ptr);
680 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
681 *ptr_ptr = png_ptr;
684 /* reset all variables to 0 */
685 png_memset(png_ptr, 0, png_sizeof(png_struct));
687 /* added at libpng-1.2.6 */
688 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
689 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
690 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
691 #endif
693 #ifdef PNG_SETJMP_SUPPORTED
694 /* restore jump buffer */
695 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
696 #endif
698 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
699 png_flush_ptr_NULL);
701 /* initialize zbuf - compression buffer */
702 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
703 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
704 (png_uint_32)png_ptr->zbuf_size);
706 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
707 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
708 1, png_doublep_NULL, png_doublep_NULL);
709 #endif
712 /* Write a few rows of image data. If the image is interlaced,
713 * either you will have to write the 7 sub images, or, if you
714 * have called png_set_interlace_handling(), you will have to
715 * "write" the image seven times.
717 void PNGAPI
718 png_write_rows(png_structp png_ptr, png_bytepp row,
719 png_uint_32 num_rows)
721 png_uint_32 i; /* row counter */
722 png_bytepp rp; /* row pointer */
724 png_debug(1, "in png_write_rows\n");
726 if (png_ptr == NULL)
727 return;
729 /* loop through the rows */
730 for (i = 0, rp = row; i < num_rows; i++, rp++)
732 png_write_row(png_ptr, *rp);
736 /* Write the image. You only need to call this function once, even
737 * if you are writing an interlaced image.
739 void PNGAPI
740 png_write_image(png_structp png_ptr, png_bytepp image)
742 png_uint_32 i; /* row index */
743 int pass, num_pass; /* pass variables */
744 png_bytepp rp; /* points to current row */
746 if (png_ptr == NULL)
747 return;
749 png_debug(1, "in png_write_image\n");
750 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
751 /* intialize interlace handling. If image is not interlaced,
752 this will set pass to 1 */
753 num_pass = png_set_interlace_handling(png_ptr);
754 #else
755 num_pass = 1;
756 #endif
757 /* loop through passes */
758 for (pass = 0; pass < num_pass; pass++)
760 /* loop through image */
761 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
763 png_write_row(png_ptr, *rp);
768 /* called by user to write a row of image data */
769 void PNGAPI
770 png_write_row(png_structp png_ptr, png_bytep row)
772 if (png_ptr == NULL)
773 return;
774 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
775 png_ptr->row_number, png_ptr->pass);
777 /* initialize transformations and other stuff if first time */
778 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
780 /* make sure we wrote the header info */
781 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
782 png_error(png_ptr,
783 "png_write_info was never called before png_write_row.");
785 /* check for transforms that have been set but were defined out */
786 #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
787 if (png_ptr->transformations & PNG_INVERT_MONO)
788 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
789 #endif
790 #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
791 if (png_ptr->transformations & PNG_FILLER)
792 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
793 #endif
794 #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
795 if (png_ptr->transformations & PNG_PACKSWAP)
796 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
797 #endif
798 #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
799 if (png_ptr->transformations & PNG_PACK)
800 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
801 #endif
802 #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
803 if (png_ptr->transformations & PNG_SHIFT)
804 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
805 #endif
806 #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
807 if (png_ptr->transformations & PNG_BGR)
808 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
809 #endif
810 #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
811 if (png_ptr->transformations & PNG_SWAP_BYTES)
812 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
813 #endif
815 png_write_start_row(png_ptr);
818 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
819 /* if interlaced and not interested in row, return */
820 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
822 switch (png_ptr->pass)
824 case 0:
825 if (png_ptr->row_number & 0x07)
827 png_write_finish_row(png_ptr);
828 return;
830 break;
831 case 1:
832 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
834 png_write_finish_row(png_ptr);
835 return;
837 break;
838 case 2:
839 if ((png_ptr->row_number & 0x07) != 4)
841 png_write_finish_row(png_ptr);
842 return;
844 break;
845 case 3:
846 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
848 png_write_finish_row(png_ptr);
849 return;
851 break;
852 case 4:
853 if ((png_ptr->row_number & 0x03) != 2)
855 png_write_finish_row(png_ptr);
856 return;
858 break;
859 case 5:
860 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
862 png_write_finish_row(png_ptr);
863 return;
865 break;
866 case 6:
867 if (!(png_ptr->row_number & 0x01))
869 png_write_finish_row(png_ptr);
870 return;
872 break;
875 #endif
877 /* set up row info for transformations */
878 png_ptr->row_info.color_type = png_ptr->color_type;
879 png_ptr->row_info.width = png_ptr->usr_width;
880 png_ptr->row_info.channels = png_ptr->usr_channels;
881 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
882 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
883 png_ptr->row_info.channels);
885 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
886 png_ptr->row_info.width);
888 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
889 png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
890 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
891 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
892 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
893 png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
895 /* Copy user's row into buffer, leaving room for filter byte. */
896 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
897 png_ptr->row_info.rowbytes);
899 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
900 /* handle interlacing */
901 if (png_ptr->interlaced && png_ptr->pass < 6 &&
902 (png_ptr->transformations & PNG_INTERLACE))
904 png_do_write_interlace(&(png_ptr->row_info),
905 png_ptr->row_buf + 1, png_ptr->pass);
906 /* this should always get caught above, but still ... */
907 if (!(png_ptr->row_info.width))
909 png_write_finish_row(png_ptr);
910 return;
913 #endif
915 /* handle other transformations */
916 if (png_ptr->transformations)
917 png_do_write_transformations(png_ptr);
919 #if defined(PNG_MNG_FEATURES_SUPPORTED)
920 /* Write filter_method 64 (intrapixel differencing) only if
921 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
922 * 2. Libpng did not write a PNG signature (this filter_method is only
923 * used in PNG datastreams that are embedded in MNG datastreams) and
924 * 3. The application called png_permit_mng_features with a mask that
925 * included PNG_FLAG_MNG_FILTER_64 and
926 * 4. The filter_method is 64 and
927 * 5. The color_type is RGB or RGBA
929 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
930 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
932 /* Intrapixel differencing */
933 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
935 #endif
937 /* Find a filter if necessary, filter the row and write it out. */
938 png_write_find_filter(png_ptr, &(png_ptr->row_info));
940 if (png_ptr->write_row_fn != NULL)
941 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
944 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
945 /* Set the automatic flush interval or 0 to turn flushing off */
946 void PNGAPI
947 png_set_flush(png_structp png_ptr, int nrows)
949 png_debug(1, "in png_set_flush\n");
950 if (png_ptr == NULL)
951 return;
952 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
955 /* flush the current output buffers now */
956 void PNGAPI
957 png_write_flush(png_structp png_ptr)
959 int wrote_IDAT;
961 png_debug(1, "in png_write_flush\n");
962 if (png_ptr == NULL)
963 return;
964 /* We have already written out all of the data */
965 if (png_ptr->row_number >= png_ptr->num_rows)
966 return;
970 int ret;
972 /* compress the data */
973 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
974 wrote_IDAT = 0;
976 /* check for compression errors */
977 if (ret != Z_OK)
979 if (png_ptr->zstream.msg != NULL)
980 png_error(png_ptr, png_ptr->zstream.msg);
981 else
982 png_error(png_ptr, "zlib error");
985 if (!(png_ptr->zstream.avail_out))
987 /* write the IDAT and reset the zlib output buffer */
988 png_write_IDAT(png_ptr, png_ptr->zbuf,
989 png_ptr->zbuf_size);
990 png_ptr->zstream.next_out = png_ptr->zbuf;
991 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
992 wrote_IDAT = 1;
994 } while(wrote_IDAT == 1);
996 /* If there is any data left to be output, write it into a new IDAT */
997 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
999 /* write the IDAT and reset the zlib output buffer */
1000 png_write_IDAT(png_ptr, png_ptr->zbuf,
1001 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
1002 png_ptr->zstream.next_out = png_ptr->zbuf;
1003 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1005 png_ptr->flush_rows = 0;
1006 png_flush(png_ptr);
1008 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
1010 /* free all memory used by the write */
1011 void PNGAPI
1012 png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
1014 png_structp png_ptr = NULL;
1015 png_infop info_ptr = NULL;
1016 #ifdef PNG_USER_MEM_SUPPORTED
1017 png_free_ptr free_fn = NULL;
1018 png_voidp mem_ptr = NULL;
1019 #endif
1021 png_debug(1, "in png_destroy_write_struct\n");
1022 if (png_ptr_ptr != NULL)
1024 png_ptr = *png_ptr_ptr;
1025 #ifdef PNG_USER_MEM_SUPPORTED
1026 free_fn = png_ptr->free_fn;
1027 mem_ptr = png_ptr->mem_ptr;
1028 #endif
1031 #ifdef PNG_USER_MEM_SUPPORTED
1032 if (png_ptr != NULL)
1034 free_fn = png_ptr->free_fn;
1035 mem_ptr = png_ptr->mem_ptr;
1037 #endif
1039 if (info_ptr_ptr != NULL)
1040 info_ptr = *info_ptr_ptr;
1042 if (info_ptr != NULL)
1044 if (png_ptr != NULL)
1046 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
1048 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1049 if (png_ptr->num_chunk_list)
1051 png_free(png_ptr, png_ptr->chunk_list);
1052 png_ptr->chunk_list=NULL;
1053 png_ptr->num_chunk_list = 0;
1055 #endif
1058 #ifdef PNG_USER_MEM_SUPPORTED
1059 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1060 (png_voidp)mem_ptr);
1061 #else
1062 png_destroy_struct((png_voidp)info_ptr);
1063 #endif
1064 *info_ptr_ptr = NULL;
1067 if (png_ptr != NULL)
1069 png_write_destroy(png_ptr);
1070 #ifdef PNG_USER_MEM_SUPPORTED
1071 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1072 (png_voidp)mem_ptr);
1073 #else
1074 png_destroy_struct((png_voidp)png_ptr);
1075 #endif
1076 *png_ptr_ptr = NULL;
1081 /* Free any memory used in png_ptr struct (old method) */
1082 void /* PRIVATE */
1083 png_write_destroy(png_structp png_ptr)
1085 #ifdef PNG_SETJMP_SUPPORTED
1086 jmp_buf tmp_jmp; /* save jump buffer */
1087 #endif
1088 png_error_ptr error_fn;
1089 png_error_ptr warning_fn;
1090 png_voidp error_ptr;
1091 #ifdef PNG_USER_MEM_SUPPORTED
1092 png_free_ptr free_fn;
1093 #endif
1095 png_debug(1, "in png_write_destroy\n");
1096 /* free any memory zlib uses */
1097 deflateEnd(&png_ptr->zstream);
1099 /* free our memory. png_free checks NULL for us. */
1100 png_free(png_ptr, png_ptr->zbuf);
1101 png_free(png_ptr, png_ptr->row_buf);
1102 #ifndef PNG_NO_WRITE_FILTER
1103 png_free(png_ptr, png_ptr->prev_row);
1104 png_free(png_ptr, png_ptr->sub_row);
1105 png_free(png_ptr, png_ptr->up_row);
1106 png_free(png_ptr, png_ptr->avg_row);
1107 png_free(png_ptr, png_ptr->paeth_row);
1108 #endif
1110 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1111 png_free(png_ptr, png_ptr->time_buffer);
1112 #endif
1114 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1115 png_free(png_ptr, png_ptr->prev_filters);
1116 png_free(png_ptr, png_ptr->filter_weights);
1117 png_free(png_ptr, png_ptr->inv_filter_weights);
1118 png_free(png_ptr, png_ptr->filter_costs);
1119 png_free(png_ptr, png_ptr->inv_filter_costs);
1120 #endif
1122 #ifdef PNG_SETJMP_SUPPORTED
1123 /* reset structure */
1124 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
1125 #endif
1127 error_fn = png_ptr->error_fn;
1128 warning_fn = png_ptr->warning_fn;
1129 error_ptr = png_ptr->error_ptr;
1130 #ifdef PNG_USER_MEM_SUPPORTED
1131 free_fn = png_ptr->free_fn;
1132 #endif
1134 png_memset(png_ptr, 0, png_sizeof(png_struct));
1136 png_ptr->error_fn = error_fn;
1137 png_ptr->warning_fn = warning_fn;
1138 png_ptr->error_ptr = error_ptr;
1139 #ifdef PNG_USER_MEM_SUPPORTED
1140 png_ptr->free_fn = free_fn;
1141 #endif
1143 #ifdef PNG_SETJMP_SUPPORTED
1144 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
1145 #endif
1148 /* Allow the application to select one or more row filters to use. */
1149 void PNGAPI
1150 png_set_filter(png_structp png_ptr, int method, int filters)
1152 png_debug(1, "in png_set_filter\n");
1153 if (png_ptr == NULL)
1154 return;
1155 #if defined(PNG_MNG_FEATURES_SUPPORTED)
1156 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
1157 (method == PNG_INTRAPIXEL_DIFFERENCING))
1158 method = PNG_FILTER_TYPE_BASE;
1159 #endif
1160 if (method == PNG_FILTER_TYPE_BASE)
1162 switch (filters & (PNG_ALL_FILTERS | 0x07))
1164 #ifndef PNG_NO_WRITE_FILTER
1165 case 5:
1166 case 6:
1167 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
1168 #endif /* PNG_NO_WRITE_FILTER */
1169 case PNG_FILTER_VALUE_NONE:
1170 png_ptr->do_filter=PNG_FILTER_NONE; break;
1171 #ifndef PNG_NO_WRITE_FILTER
1172 case PNG_FILTER_VALUE_SUB:
1173 png_ptr->do_filter=PNG_FILTER_SUB; break;
1174 case PNG_FILTER_VALUE_UP:
1175 png_ptr->do_filter=PNG_FILTER_UP; break;
1176 case PNG_FILTER_VALUE_AVG:
1177 png_ptr->do_filter=PNG_FILTER_AVG; break;
1178 case PNG_FILTER_VALUE_PAETH:
1179 png_ptr->do_filter=PNG_FILTER_PAETH; break;
1180 default: png_ptr->do_filter = (png_byte)filters; break;
1181 #else
1182 default: png_warning(png_ptr, "Unknown row filter for method 0");
1183 #endif /* PNG_NO_WRITE_FILTER */
1186 /* If we have allocated the row_buf, this means we have already started
1187 * with the image and we should have allocated all of the filter buffers
1188 * that have been selected. If prev_row isn't already allocated, then
1189 * it is too late to start using the filters that need it, since we
1190 * will be missing the data in the previous row. If an application
1191 * wants to start and stop using particular filters during compression,
1192 * it should start out with all of the filters, and then add and
1193 * remove them after the start of compression.
1195 if (png_ptr->row_buf != NULL)
1197 #ifndef PNG_NO_WRITE_FILTER
1198 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
1200 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
1201 (png_ptr->rowbytes + 1));
1202 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1205 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
1207 if (png_ptr->prev_row == NULL)
1209 png_warning(png_ptr, "Can't add Up filter after starting");
1210 png_ptr->do_filter &= ~PNG_FILTER_UP;
1212 else
1214 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
1215 (png_ptr->rowbytes + 1));
1216 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1220 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
1222 if (png_ptr->prev_row == NULL)
1224 png_warning(png_ptr, "Can't add Average filter after starting");
1225 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1227 else
1229 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1230 (png_ptr->rowbytes + 1));
1231 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
1235 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
1236 png_ptr->paeth_row == NULL)
1238 if (png_ptr->prev_row == NULL)
1240 png_warning(png_ptr, "Can't add Paeth filter after starting");
1241 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
1243 else
1245 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
1246 (png_ptr->rowbytes + 1));
1247 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1251 if (png_ptr->do_filter == PNG_NO_FILTERS)
1252 #endif /* PNG_NO_WRITE_FILTER */
1253 png_ptr->do_filter = PNG_FILTER_NONE;
1256 else
1257 png_error(png_ptr, "Unknown custom filter method");
1260 /* This allows us to influence the way in which libpng chooses the "best"
1261 * filter for the current scanline. While the "minimum-sum-of-absolute-
1262 * differences metric is relatively fast and effective, there is some
1263 * question as to whether it can be improved upon by trying to keep the
1264 * filtered data going to zlib more consistent, hopefully resulting in
1265 * better compression.
1267 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
1268 void PNGAPI
1269 png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1270 int num_weights, png_doublep filter_weights,
1271 png_doublep filter_costs)
1273 int i;
1275 png_debug(1, "in png_set_filter_heuristics\n");
1276 if (png_ptr == NULL)
1277 return;
1278 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1280 png_warning(png_ptr, "Unknown filter heuristic method");
1281 return;
1284 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1286 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1289 if (num_weights < 0 || filter_weights == NULL ||
1290 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1292 num_weights = 0;
1295 png_ptr->num_prev_filters = (png_byte)num_weights;
1296 png_ptr->heuristic_method = (png_byte)heuristic_method;
1298 if (num_weights > 0)
1300 if (png_ptr->prev_filters == NULL)
1302 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
1303 (png_uint_32)(png_sizeof(png_byte) * num_weights));
1305 /* To make sure that the weighting starts out fairly */
1306 for (i = 0; i < num_weights; i++)
1308 png_ptr->prev_filters[i] = 255;
1312 if (png_ptr->filter_weights == NULL)
1314 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
1315 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1317 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
1318 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1319 for (i = 0; i < num_weights; i++)
1321 png_ptr->inv_filter_weights[i] =
1322 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1326 for (i = 0; i < num_weights; i++)
1328 if (filter_weights[i] < 0.0)
1330 png_ptr->inv_filter_weights[i] =
1331 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1333 else
1335 png_ptr->inv_filter_weights[i] =
1336 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1337 png_ptr->filter_weights[i] =
1338 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1343 /* If, in the future, there are other filter methods, this would
1344 * need to be based on png_ptr->filter.
1346 if (png_ptr->filter_costs == NULL)
1348 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
1349 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1351 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
1352 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1354 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1356 png_ptr->inv_filter_costs[i] =
1357 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1361 /* Here is where we set the relative costs of the different filters. We
1362 * should take the desired compression level into account when setting
1363 * the costs, so that Paeth, for instance, has a high relative cost at low
1364 * compression levels, while it has a lower relative cost at higher
1365 * compression settings. The filter types are in order of increasing
1366 * relative cost, so it would be possible to do this with an algorithm.
1368 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1370 if (filter_costs == NULL || filter_costs[i] < 0.0)
1372 png_ptr->inv_filter_costs[i] =
1373 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1375 else if (filter_costs[i] >= 1.0)
1377 png_ptr->inv_filter_costs[i] =
1378 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1379 png_ptr->filter_costs[i] =
1380 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1384 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1386 void PNGAPI
1387 png_set_compression_level(png_structp png_ptr, int level)
1389 png_debug(1, "in png_set_compression_level\n");
1390 if (png_ptr == NULL)
1391 return;
1392 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
1393 png_ptr->zlib_level = level;
1396 void PNGAPI
1397 png_set_compression_mem_level(png_structp png_ptr, int mem_level)
1399 png_debug(1, "in png_set_compression_mem_level\n");
1400 if (png_ptr == NULL)
1401 return;
1402 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
1403 png_ptr->zlib_mem_level = mem_level;
1406 void PNGAPI
1407 png_set_compression_strategy(png_structp png_ptr, int strategy)
1409 png_debug(1, "in png_set_compression_strategy\n");
1410 if (png_ptr == NULL)
1411 return;
1412 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
1413 png_ptr->zlib_strategy = strategy;
1416 void PNGAPI
1417 png_set_compression_window_bits(png_structp png_ptr, int window_bits)
1419 if (png_ptr == NULL)
1420 return;
1421 if (window_bits > 15)
1422 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1423 else if (window_bits < 8)
1424 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1425 #ifndef WBITS_8_OK
1426 /* avoid libpng bug with 256-byte windows */
1427 if (window_bits == 8)
1429 png_warning(png_ptr, "Compression window is being reset to 512");
1430 window_bits=9;
1432 #endif
1433 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
1434 png_ptr->zlib_window_bits = window_bits;
1437 void PNGAPI
1438 png_set_compression_method(png_structp png_ptr, int method)
1440 png_debug(1, "in png_set_compression_method\n");
1441 if (png_ptr == NULL)
1442 return;
1443 if (method != 8)
1444 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1445 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
1446 png_ptr->zlib_method = method;
1449 void PNGAPI
1450 png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1452 if (png_ptr == NULL)
1453 return;
1454 png_ptr->write_row_fn = write_row_fn;
1457 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1458 void PNGAPI
1459 png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1460 write_user_transform_fn)
1462 png_debug(1, "in png_set_write_user_transform_fn\n");
1463 if (png_ptr == NULL)
1464 return;
1465 png_ptr->transformations |= PNG_USER_TRANSFORM;
1466 png_ptr->write_user_transform_fn = write_user_transform_fn;
1468 #endif
1471 #if defined(PNG_INFO_IMAGE_SUPPORTED)
1472 void PNGAPI
1473 png_write_png(png_structp png_ptr, png_infop info_ptr,
1474 int transforms, voidp params)
1476 if (png_ptr == NULL || info_ptr == NULL)
1477 return;
1478 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1479 /* invert the alpha channel from opacity to transparency */
1480 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1481 png_set_invert_alpha(png_ptr);
1482 #endif
1484 /* Write the file header information. */
1485 png_write_info(png_ptr, info_ptr);
1487 /* ------ these transformations don't touch the info structure ------- */
1489 #if defined(PNG_WRITE_INVERT_SUPPORTED)
1490 /* invert monochrome pixels */
1491 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1492 png_set_invert_mono(png_ptr);
1493 #endif
1495 #if defined(PNG_WRITE_SHIFT_SUPPORTED)
1496 /* Shift the pixels up to a legal bit depth and fill in
1497 * as appropriate to correctly scale the image.
1499 if ((transforms & PNG_TRANSFORM_SHIFT)
1500 && (info_ptr->valid & PNG_INFO_sBIT))
1501 png_set_shift(png_ptr, &info_ptr->sig_bit);
1502 #endif
1504 #if defined(PNG_WRITE_PACK_SUPPORTED)
1505 /* pack pixels into bytes */
1506 if (transforms & PNG_TRANSFORM_PACKING)
1507 png_set_packing(png_ptr);
1508 #endif
1510 #if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
1511 /* swap location of alpha bytes from ARGB to RGBA */
1512 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1513 png_set_swap_alpha(png_ptr);
1514 #endif
1516 #if defined(PNG_WRITE_FILLER_SUPPORTED)
1517 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
1518 * RGB (4 channels -> 3 channels). The second parameter is not used.
1520 if (transforms & PNG_TRANSFORM_STRIP_FILLER)
1521 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1522 #endif
1524 #if defined(PNG_WRITE_BGR_SUPPORTED)
1525 /* flip BGR pixels to RGB */
1526 if (transforms & PNG_TRANSFORM_BGR)
1527 png_set_bgr(png_ptr);
1528 #endif
1530 #if defined(PNG_WRITE_SWAP_SUPPORTED)
1531 /* swap bytes of 16-bit files to most significant byte first */
1532 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1533 png_set_swap(png_ptr);
1534 #endif
1536 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
1537 /* swap bits of 1, 2, 4 bit packed pixel formats */
1538 if (transforms & PNG_TRANSFORM_PACKSWAP)
1539 png_set_packswap(png_ptr);
1540 #endif
1542 /* ----------------------- end of transformations ------------------- */
1544 /* write the bits */
1545 if (info_ptr->valid & PNG_INFO_IDAT)
1546 png_write_image(png_ptr, info_ptr->row_pointers);
1548 /* It is REQUIRED to call this to finish writing the rest of the file */
1549 png_write_end(png_ptr, info_ptr);
1551 transforms = transforms; /* quiet compiler warnings */
1552 params = params;
1554 #endif
1556 #if defined(PNG_WRITE_APNG_SUPPORTED)
1557 void PNGAPI
1558 png_write_frame_head(png_structp png_ptr, png_infop info_ptr,
1559 png_bytepp row_pointers, png_uint_32 width, png_uint_32 height,
1560 png_uint_32 x_offset, png_uint_32 y_offset,
1561 png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
1562 png_byte blend_op)
1564 png_debug(1, "in png_write_frame_head\n");
1566 /* there is a chance this has been set after png_write_info was called,
1567 * so it would be set but not written. is there a way to be sure? */
1568 if (!(info_ptr->valid & PNG_INFO_acTL))
1569 png_error(png_ptr, "png_write_frame_head(): acTL not set");
1571 png_write_reset(png_ptr);
1573 png_write_reinit(png_ptr, info_ptr, width, height);
1575 if ( !(png_ptr->num_frames_written == 0 &&
1576 (png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN) ) )
1577 png_write_fcTL(png_ptr, width, height, x_offset, y_offset,
1578 delay_num, delay_den, dispose_op, blend_op);
1581 void PNGAPI
1582 png_write_frame_tail(png_structp png_ptr, png_infop png_info)
1584 png_debug(1, "in png_write_frame_tail\n");
1586 png_ptr->num_frames_written++;
1588 #endif /* PNG_WRITE_APNG_SUPPORTED */
1590 #endif /* PNG_WRITE_SUPPORTED */