2 /* pngwrite.c - general routines to write a PNG file
4 * Last changed in libpng 1.2.37 [June 4, 2009]
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2009 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.)
11 /* Get internal access to 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.
26 png_write_info_before_PLTE(png_structp png_ptr
, png_infop info_ptr
)
28 png_debug(1, "in png_write_info_before_PLTE");
29 if (png_ptr
== NULL
|| info_ptr
== NULL
)
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;
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
);
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.
53 #if defined(PNG_WRITE_gAMA_SUPPORTED)
54 if (info_ptr
->valid
& PNG_INFO_gAMA
)
56 # ifdef PNG_FLOATING_POINT_SUPPORTED
57 png_write_gAMA(png_ptr
, info_ptr
->gamma
);
59 #ifdef PNG_FIXED_POINT_SUPPORTED
60 png_write_gAMA_fixed(png_ptr
, info_ptr
->int_gamma
);
65 #if defined(PNG_WRITE_sRGB_SUPPORTED)
66 if (info_ptr
->valid
& PNG_INFO_sRGB
)
67 png_write_sRGB(png_ptr
, (int)info_ptr
->srgb_intent
);
69 #if defined(PNG_WRITE_iCCP_SUPPORTED)
70 if (info_ptr
->valid
& PNG_INFO_iCCP
)
71 png_write_iCCP(png_ptr
, info_ptr
->iccp_name
, PNG_COMPRESSION_TYPE_BASE
,
72 info_ptr
->iccp_profile
, (int)info_ptr
->iccp_proflen
);
74 #if defined(PNG_WRITE_sBIT_SUPPORTED)
75 if (info_ptr
->valid
& PNG_INFO_sBIT
)
76 png_write_sBIT(png_ptr
, &(info_ptr
->sig_bit
), info_ptr
->color_type
);
78 #if defined(PNG_WRITE_cHRM_SUPPORTED)
79 if (info_ptr
->valid
& PNG_INFO_cHRM
)
81 #ifdef PNG_FLOATING_POINT_SUPPORTED
82 png_write_cHRM(png_ptr
,
83 info_ptr
->x_white
, info_ptr
->y_white
,
84 info_ptr
->x_red
, info_ptr
->y_red
,
85 info_ptr
->x_green
, info_ptr
->y_green
,
86 info_ptr
->x_blue
, info_ptr
->y_blue
);
88 # ifdef PNG_FIXED_POINT_SUPPORTED
89 png_write_cHRM_fixed(png_ptr
,
90 info_ptr
->int_x_white
, info_ptr
->int_y_white
,
91 info_ptr
->int_x_red
, info_ptr
->int_y_red
,
92 info_ptr
->int_x_green
, info_ptr
->int_y_green
,
93 info_ptr
->int_x_blue
, info_ptr
->int_y_blue
);
98 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
99 if (info_ptr
->unknown_chunks_num
)
101 png_unknown_chunk
*up
;
103 png_debug(5, "writing extra chunks");
105 for (up
= info_ptr
->unknown_chunks
;
106 up
< info_ptr
->unknown_chunks
+ info_ptr
->unknown_chunks_num
;
109 int keep
=png_handle_as_unknown(png_ptr
, up
->name
);
110 if (keep
!= PNG_HANDLE_CHUNK_NEVER
&&
111 up
->location
&& !(up
->location
& PNG_HAVE_PLTE
) &&
112 !(up
->location
& PNG_HAVE_IDAT
) &&
113 ((up
->name
[3] & 0x20) || keep
== PNG_HANDLE_CHUNK_ALWAYS
||
114 (png_ptr
->flags
& PNG_FLAG_KEEP_UNSAFE_CHUNKS
)))
117 png_warning(png_ptr
, "Writing zero-length unknown chunk");
118 png_write_chunk(png_ptr
, up
->name
, up
->data
, up
->size
);
123 png_ptr
->mode
|= PNG_WROTE_INFO_BEFORE_PLTE
;
128 png_write_info(png_structp png_ptr
, png_infop info_ptr
)
130 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
134 png_debug(1, "in png_write_info");
136 if (png_ptr
== NULL
|| info_ptr
== NULL
)
139 png_write_info_before_PLTE(png_ptr
, info_ptr
);
141 if (info_ptr
->valid
& PNG_INFO_PLTE
)
142 png_write_PLTE(png_ptr
, info_ptr
->palette
,
143 (png_uint_32
)info_ptr
->num_palette
);
144 else if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
145 png_error(png_ptr
, "Valid palette required for paletted images");
147 #if defined(PNG_WRITE_tRNS_SUPPORTED)
148 if (info_ptr
->valid
& PNG_INFO_tRNS
)
150 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
151 /* Invert the alpha channel (in tRNS) */
152 if ((png_ptr
->transformations
& PNG_INVERT_ALPHA
) &&
153 info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
156 for (j
=0; j
<(int)info_ptr
->num_trans
; j
++)
157 info_ptr
->trans
[j
] = (png_byte
)(255 - info_ptr
->trans
[j
]);
160 png_write_tRNS(png_ptr
, info_ptr
->trans
, &(info_ptr
->trans_values
),
161 info_ptr
->num_trans
, info_ptr
->color_type
);
164 #if defined(PNG_WRITE_bKGD_SUPPORTED)
165 if (info_ptr
->valid
& PNG_INFO_bKGD
)
166 png_write_bKGD(png_ptr
, &(info_ptr
->background
), info_ptr
->color_type
);
168 #if defined(PNG_WRITE_hIST_SUPPORTED)
169 if (info_ptr
->valid
& PNG_INFO_hIST
)
170 png_write_hIST(png_ptr
, info_ptr
->hist
, info_ptr
->num_palette
);
172 #if defined(PNG_WRITE_oFFs_SUPPORTED)
173 if (info_ptr
->valid
& PNG_INFO_oFFs
)
174 png_write_oFFs(png_ptr
, info_ptr
->x_offset
, info_ptr
->y_offset
,
175 info_ptr
->offset_unit_type
);
177 #if defined(PNG_WRITE_pCAL_SUPPORTED)
178 if (info_ptr
->valid
& PNG_INFO_pCAL
)
179 png_write_pCAL(png_ptr
, info_ptr
->pcal_purpose
, info_ptr
->pcal_X0
,
180 info_ptr
->pcal_X1
, info_ptr
->pcal_type
, info_ptr
->pcal_nparams
,
181 info_ptr
->pcal_units
, info_ptr
->pcal_params
);
184 #if defined(PNG_sCAL_SUPPORTED)
185 if (info_ptr
->valid
& PNG_INFO_sCAL
)
186 #if defined(PNG_WRITE_sCAL_SUPPORTED)
187 #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
188 png_write_sCAL(png_ptr
, (int)info_ptr
->scal_unit
,
189 info_ptr
->scal_pixel_width
, info_ptr
->scal_pixel_height
);
190 #else /* !FLOATING_POINT */
191 #ifdef PNG_FIXED_POINT_SUPPORTED
192 png_write_sCAL_s(png_ptr
, (int)info_ptr
->scal_unit
,
193 info_ptr
->scal_s_width
, info_ptr
->scal_s_height
);
194 #endif /* FIXED_POINT */
195 #endif /* FLOATING_POINT */
196 #else /* !WRITE_sCAL */
198 "png_write_sCAL not supported; sCAL chunk not written.");
199 #endif /* WRITE_sCAL */
202 #if defined(PNG_WRITE_pHYs_SUPPORTED)
203 if (info_ptr
->valid
& PNG_INFO_pHYs
)
204 png_write_pHYs(png_ptr
, info_ptr
->x_pixels_per_unit
,
205 info_ptr
->y_pixels_per_unit
, info_ptr
->phys_unit_type
);
208 #if defined(PNG_WRITE_tIME_SUPPORTED)
209 if (info_ptr
->valid
& PNG_INFO_tIME
)
211 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
212 png_ptr
->mode
|= PNG_WROTE_tIME
;
216 #if defined(PNG_WRITE_sPLT_SUPPORTED)
217 if (info_ptr
->valid
& PNG_INFO_sPLT
)
218 for (i
= 0; i
< (int)info_ptr
->splt_palettes_num
; i
++)
219 png_write_sPLT(png_ptr
, info_ptr
->splt_palettes
+ i
);
222 #if defined(PNG_WRITE_TEXT_SUPPORTED)
223 /* Check to see if we need to write text chunks */
224 for (i
= 0; i
< info_ptr
->num_text
; i
++)
226 png_debug2(2, "Writing header text chunk %d, type %d", i
,
227 info_ptr
->text
[i
].compression
);
228 /* An internationalized chunk? */
229 if (info_ptr
->text
[i
].compression
> 0)
231 #if defined(PNG_WRITE_iTXt_SUPPORTED)
232 /* Write international chunk */
233 png_write_iTXt(png_ptr
,
234 info_ptr
->text
[i
].compression
,
235 info_ptr
->text
[i
].key
,
236 info_ptr
->text
[i
].lang
,
237 info_ptr
->text
[i
].lang_key
,
238 info_ptr
->text
[i
].text
);
240 png_warning(png_ptr
, "Unable to write international text");
242 /* Mark this chunk as written */
243 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
245 /* If we want a compressed text chunk */
246 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_zTXt
)
248 #if defined(PNG_WRITE_zTXt_SUPPORTED)
249 /* Write compressed chunk */
250 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
251 info_ptr
->text
[i
].text
, 0,
252 info_ptr
->text
[i
].compression
);
254 png_warning(png_ptr
, "Unable to write compressed text");
256 /* Mark this chunk as written */
257 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
259 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
261 #if defined(PNG_WRITE_tEXt_SUPPORTED)
262 /* Write uncompressed chunk */
263 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
264 info_ptr
->text
[i
].text
,
266 /* Mark this chunk as written */
267 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
270 png_warning(png_ptr
, "Unable to write uncompressed text");
276 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
277 if (info_ptr
->unknown_chunks_num
)
279 png_unknown_chunk
*up
;
281 png_debug(5, "writing extra chunks");
283 for (up
= info_ptr
->unknown_chunks
;
284 up
< info_ptr
->unknown_chunks
+ info_ptr
->unknown_chunks_num
;
287 int keep
=png_handle_as_unknown(png_ptr
, up
->name
);
288 if (keep
!= PNG_HANDLE_CHUNK_NEVER
&&
289 up
->location
&& (up
->location
& PNG_HAVE_PLTE
) &&
290 !(up
->location
& PNG_HAVE_IDAT
) &&
291 ((up
->name
[3] & 0x20) || keep
== PNG_HANDLE_CHUNK_ALWAYS
||
292 (png_ptr
->flags
& PNG_FLAG_KEEP_UNSAFE_CHUNKS
)))
294 png_write_chunk(png_ptr
, up
->name
, up
->data
, up
->size
);
301 /* Writes the end of the PNG file. If you don't want to write comments or
302 * time information, you can pass NULL for info. If you already wrote these
303 * in png_write_info(), do not write them again here. If you have long
304 * comments, I suggest writing them here, and compressing them.
307 png_write_end(png_structp png_ptr
, png_infop info_ptr
)
309 png_debug(1, "in png_write_end");
312 if (!(png_ptr
->mode
& PNG_HAVE_IDAT
))
313 png_error(png_ptr
, "No IDATs written into file");
315 /* See if user wants us to write information chunks */
316 if (info_ptr
!= NULL
)
318 #if defined(PNG_WRITE_TEXT_SUPPORTED)
319 int i
; /* Local index variable */
321 #if defined(PNG_WRITE_tIME_SUPPORTED)
322 /* Check to see if user has supplied a time chunk */
323 if ((info_ptr
->valid
& PNG_INFO_tIME
) &&
324 !(png_ptr
->mode
& PNG_WROTE_tIME
))
325 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
327 #if defined(PNG_WRITE_TEXT_SUPPORTED)
328 /* Loop through comment chunks */
329 for (i
= 0; i
< info_ptr
->num_text
; i
++)
331 png_debug2(2, "Writing trailer text chunk %d, type %d", i
,
332 info_ptr
->text
[i
].compression
);
333 /* An internationalized chunk? */
334 if (info_ptr
->text
[i
].compression
> 0)
336 #if defined(PNG_WRITE_iTXt_SUPPORTED)
337 /* Write international chunk */
338 png_write_iTXt(png_ptr
,
339 info_ptr
->text
[i
].compression
,
340 info_ptr
->text
[i
].key
,
341 info_ptr
->text
[i
].lang
,
342 info_ptr
->text
[i
].lang_key
,
343 info_ptr
->text
[i
].text
);
345 png_warning(png_ptr
, "Unable to write international text");
347 /* Mark this chunk as written */
348 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
350 else if (info_ptr
->text
[i
].compression
>= PNG_TEXT_COMPRESSION_zTXt
)
352 #if defined(PNG_WRITE_zTXt_SUPPORTED)
353 /* Write compressed chunk */
354 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
355 info_ptr
->text
[i
].text
, 0,
356 info_ptr
->text
[i
].compression
);
358 png_warning(png_ptr
, "Unable to write compressed text");
360 /* Mark this chunk as written */
361 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
363 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
365 #if defined(PNG_WRITE_tEXt_SUPPORTED)
366 /* Write uncompressed chunk */
367 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
368 info_ptr
->text
[i
].text
, 0);
370 png_warning(png_ptr
, "Unable to write uncompressed text");
373 /* Mark this chunk as written */
374 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
378 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
379 if (info_ptr
->unknown_chunks_num
)
381 png_unknown_chunk
*up
;
383 png_debug(5, "writing extra chunks");
385 for (up
= info_ptr
->unknown_chunks
;
386 up
< info_ptr
->unknown_chunks
+ info_ptr
->unknown_chunks_num
;
389 int keep
=png_handle_as_unknown(png_ptr
, up
->name
);
390 if (keep
!= PNG_HANDLE_CHUNK_NEVER
&&
391 up
->location
&& (up
->location
& PNG_AFTER_IDAT
) &&
392 ((up
->name
[3] & 0x20) || keep
== PNG_HANDLE_CHUNK_ALWAYS
||
393 (png_ptr
->flags
& PNG_FLAG_KEEP_UNSAFE_CHUNKS
)))
395 png_write_chunk(png_ptr
, up
->name
, up
->data
, up
->size
);
402 png_ptr
->mode
|= PNG_AFTER_IDAT
;
404 /* Write end of PNG file */
405 png_write_IEND(png_ptr
);
406 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
407 * and restored again in libpng-1.2.30, may cause some applications that
408 * do not set png_ptr->output_flush_fn to crash. If your application
409 * experiences a problem, please try building libpng with
410 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
411 * png-mng-implement at lists.sf.net . This kludge will be removed
414 #if defined(PNG_WRITE_FLUSH_SUPPORTED) && \
415 defined(PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED)
420 #if defined(PNG_WRITE_tIME_SUPPORTED)
421 #if !defined(_WIN32_WCE)
422 /* "time.h" functions are not supported on WindowsCE */
424 png_convert_from_struct_tm(png_timep ptime
, struct tm FAR
* ttime
)
426 png_debug(1, "in png_convert_from_struct_tm");
427 ptime
->year
= (png_uint_16
)(1900 + ttime
->tm_year
);
428 ptime
->month
= (png_byte
)(ttime
->tm_mon
+ 1);
429 ptime
->day
= (png_byte
)ttime
->tm_mday
;
430 ptime
->hour
= (png_byte
)ttime
->tm_hour
;
431 ptime
->minute
= (png_byte
)ttime
->tm_min
;
432 ptime
->second
= (png_byte
)ttime
->tm_sec
;
436 png_convert_from_time_t(png_timep ptime
, time_t ttime
)
440 png_debug(1, "in png_convert_from_time_t");
441 tbuf
= gmtime(&ttime
);
442 png_convert_from_struct_tm(ptime
, tbuf
);
447 /* Initialize png_ptr structure, and allocate any memory needed */
449 png_create_write_struct(png_const_charp user_png_ver
, png_voidp error_ptr
,
450 png_error_ptr error_fn
, png_error_ptr warn_fn
)
452 #ifdef PNG_USER_MEM_SUPPORTED
453 return (png_create_write_struct_2(user_png_ver
, error_ptr
, error_fn
,
454 warn_fn
, png_voidp_NULL
, png_malloc_ptr_NULL
, png_free_ptr_NULL
));
457 /* Alternate initialize png_ptr structure, and allocate any memory needed */
459 png_create_write_struct_2(png_const_charp user_png_ver
, png_voidp error_ptr
,
460 png_error_ptr error_fn
, png_error_ptr warn_fn
, png_voidp mem_ptr
,
461 png_malloc_ptr malloc_fn
, png_free_ptr free_fn
)
463 #endif /* PNG_USER_MEM_SUPPORTED */
464 #ifdef PNG_SETJMP_SUPPORTED
468 #ifdef PNG_SETJMP_SUPPORTED
469 #ifdef USE_FAR_KEYWORD
474 png_debug(1, "in png_create_write_struct");
475 #ifdef PNG_USER_MEM_SUPPORTED
476 png_ptr
= (png_structp
)png_create_struct_2(PNG_STRUCT_PNG
,
477 (png_malloc_ptr
)malloc_fn
, (png_voidp
)mem_ptr
);
479 png_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
480 #endif /* PNG_USER_MEM_SUPPORTED */
484 /* Added at libpng-1.2.6 */
485 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
486 png_ptr
->user_width_max
=PNG_USER_WIDTH_MAX
;
487 png_ptr
->user_height_max
=PNG_USER_HEIGHT_MAX
;
490 #ifdef PNG_SETJMP_SUPPORTED
491 #ifdef USE_FAR_KEYWORD
494 if (setjmp(png_ptr
->jmpbuf
))
497 png_free(png_ptr
, png_ptr
->zbuf
);
499 png_destroy_struct(png_ptr
);
502 #ifdef USE_FAR_KEYWORD
503 png_memcpy(png_ptr
->jmpbuf
, jmpbuf
, png_sizeof(jmp_buf));
507 #ifdef PNG_USER_MEM_SUPPORTED
508 png_set_mem_fn(png_ptr
, mem_ptr
, malloc_fn
, free_fn
);
509 #endif /* PNG_USER_MEM_SUPPORTED */
510 png_set_error_fn(png_ptr
, error_ptr
, error_fn
, warn_fn
);
517 if (user_png_ver
[i
] != png_libpng_ver
[i
])
518 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
519 } while (png_libpng_ver
[i
++]);
522 if (png_ptr
->flags
& PNG_FLAG_LIBRARY_MISMATCH
)
524 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
525 * we must recompile any applications that use any older library version.
526 * For versions after libpng 1.0, we will be compatible, so we need
527 * only check the first digit.
529 if (user_png_ver
== NULL
|| user_png_ver
[0] != png_libpng_ver
[0] ||
530 (user_png_ver
[0] == '1' && user_png_ver
[2] != png_libpng_ver
[2]) ||
531 (user_png_ver
[0] == '0' && user_png_ver
[2] < '9'))
533 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
537 png_snprintf(msg
, 80,
538 "Application was compiled with png.h from libpng-%.20s",
540 png_warning(png_ptr
, msg
);
542 png_snprintf(msg
, 80,
543 "Application is running with png.c from libpng-%.20s",
545 png_warning(png_ptr
, msg
);
547 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
551 "Incompatible libpng version in application and library");
555 /* Initialize zbuf - compression buffer */
556 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
557 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
558 (png_uint_32
)png_ptr
->zbuf_size
);
560 png_set_write_fn(png_ptr
, png_voidp_NULL
, png_rw_ptr_NULL
,
563 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
564 png_set_filter_heuristics(png_ptr
, PNG_FILTER_HEURISTIC_DEFAULT
,
565 1, png_doublep_NULL
, png_doublep_NULL
);
568 #ifdef PNG_SETJMP_SUPPORTED
569 /* Applications that neglect to set up their own setjmp() and then encounter
570 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
571 abort instead of returning. */
572 #ifdef USE_FAR_KEYWORD
575 png_memcpy(png_ptr
->jmpbuf
, jmpbuf
, png_sizeof(jmp_buf));
577 if (setjmp(png_ptr
->jmpbuf
))
584 /* Initialize png_ptr structure, and allocate any memory needed */
585 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
587 #undef png_write_init
589 png_write_init(png_structp png_ptr
)
591 /* We only come here via pre-1.0.7-compiled applications */
592 png_write_init_2(png_ptr
, "1.0.6 or earlier", 0, 0);
596 png_write_init_2(png_structp png_ptr
, png_const_charp user_png_ver
,
597 png_size_t png_struct_size
, png_size_t png_info_size
)
599 /* We only come here via pre-1.0.12-compiled applications */
600 if (png_ptr
== NULL
) return;
601 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
602 if (png_sizeof(png_struct
) > png_struct_size
||
603 png_sizeof(png_info
) > png_info_size
)
606 png_ptr
->warning_fn
=NULL
;
609 png_snprintf(msg
, 80,
610 "Application was compiled with png.h from libpng-%.20s",
612 png_warning(png_ptr
, msg
);
614 png_snprintf(msg
, 80,
615 "Application is running with png.c from libpng-%.20s",
617 png_warning(png_ptr
, msg
);
620 if (png_sizeof(png_struct
) > png_struct_size
)
622 png_ptr
->error_fn
=NULL
;
623 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
627 "The png struct allocated by the application for writing is too small.");
629 if (png_sizeof(png_info
) > png_info_size
)
631 png_ptr
->error_fn
=NULL
;
632 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
636 "The info struct allocated by the application for writing is too small.");
638 png_write_init_3(&png_ptr
, user_png_ver
, png_struct_size
);
640 #endif /* PNG_1_0_X || PNG_1_2_X */
644 png_write_init_3(png_structpp ptr_ptr
, png_const_charp user_png_ver
,
645 png_size_t png_struct_size
)
647 png_structp png_ptr
=*ptr_ptr
;
648 #ifdef PNG_SETJMP_SUPPORTED
649 jmp_buf tmp_jmp
; /* To save current jump buffer */
659 if (user_png_ver
[i
] != png_libpng_ver
[i
])
661 #ifdef PNG_LEGACY_SUPPORTED
662 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
664 png_ptr
->warning_fn
=NULL
;
666 "Application uses deprecated png_write_init() and should be recompiled.");
670 } while (png_libpng_ver
[i
++]);
672 png_debug(1, "in png_write_init_3");
674 #ifdef PNG_SETJMP_SUPPORTED
675 /* Save jump buffer and error functions */
676 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, png_sizeof(jmp_buf));
679 if (png_sizeof(png_struct
) > png_struct_size
)
681 png_destroy_struct(png_ptr
);
682 png_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
686 /* Reset all variables to 0 */
687 png_memset(png_ptr
, 0, png_sizeof(png_struct
));
689 /* Added at libpng-1.2.6 */
690 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
691 png_ptr
->user_width_max
=PNG_USER_WIDTH_MAX
;
692 png_ptr
->user_height_max
=PNG_USER_HEIGHT_MAX
;
695 #ifdef PNG_SETJMP_SUPPORTED
696 /* Restore jump buffer */
697 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, png_sizeof(jmp_buf));
700 png_set_write_fn(png_ptr
, png_voidp_NULL
, png_rw_ptr_NULL
,
703 /* Initialize zbuf - compression buffer */
704 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
705 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
706 (png_uint_32
)png_ptr
->zbuf_size
);
708 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
709 png_set_filter_heuristics(png_ptr
, PNG_FILTER_HEURISTIC_DEFAULT
,
710 1, png_doublep_NULL
, png_doublep_NULL
);
714 /* Write a few rows of image data. If the image is interlaced,
715 * either you will have to write the 7 sub images, or, if you
716 * have called png_set_interlace_handling(), you will have to
717 * "write" the image seven times.
720 png_write_rows(png_structp png_ptr
, png_bytepp row
,
721 png_uint_32 num_rows
)
723 png_uint_32 i
; /* Row counter */
724 png_bytepp rp
; /* Row pointer */
726 png_debug(1, "in png_write_rows");
731 /* Loop through the rows */
732 for (i
= 0, rp
= row
; i
< num_rows
; i
++, rp
++)
734 png_write_row(png_ptr
, *rp
);
738 /* Write the image. You only need to call this function once, even
739 * if you are writing an interlaced image.
742 png_write_image(png_structp png_ptr
, png_bytepp image
)
744 png_uint_32 i
; /* Row index */
745 int pass
, num_pass
; /* Pass variables */
746 png_bytepp rp
; /* Points to current row */
751 png_debug(1, "in png_write_image");
752 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
753 /* Initialize interlace handling. If image is not interlaced,
754 * this will set pass to 1
756 num_pass
= png_set_interlace_handling(png_ptr
);
760 /* Loop through passes */
761 for (pass
= 0; pass
< num_pass
; pass
++)
763 /* Loop through image */
764 for (i
= 0, rp
= image
; i
< png_ptr
->height
; i
++, rp
++)
766 png_write_row(png_ptr
, *rp
);
771 /* Called by user to write a row of image data */
773 png_write_row(png_structp png_ptr
, png_bytep row
)
777 png_debug2(1, "in png_write_row (row %ld, pass %d)",
778 png_ptr
->row_number
, png_ptr
->pass
);
780 /* Initialize transformations and other stuff if first time */
781 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
783 /* Make sure we wrote the header info */
784 if (!(png_ptr
->mode
& PNG_WROTE_INFO_BEFORE_PLTE
))
786 "png_write_info was never called before png_write_row.");
788 /* Check for transforms that have been set but were defined out */
789 #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
790 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
791 png_warning(png_ptr
, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
793 #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
794 if (png_ptr
->transformations
& PNG_FILLER
)
795 png_warning(png_ptr
, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
797 #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
798 if (png_ptr
->transformations
& PNG_PACKSWAP
)
799 png_warning(png_ptr
, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
801 #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
802 if (png_ptr
->transformations
& PNG_PACK
)
803 png_warning(png_ptr
, "PNG_WRITE_PACK_SUPPORTED is not defined.");
805 #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
806 if (png_ptr
->transformations
& PNG_SHIFT
)
807 png_warning(png_ptr
, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
809 #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
810 if (png_ptr
->transformations
& PNG_BGR
)
811 png_warning(png_ptr
, "PNG_WRITE_BGR_SUPPORTED is not defined.");
813 #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
814 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
815 png_warning(png_ptr
, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
818 png_write_start_row(png_ptr
);
821 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
822 /* If interlaced and not interested in row, return */
823 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
825 switch (png_ptr
->pass
)
828 if (png_ptr
->row_number
& 0x07)
830 png_write_finish_row(png_ptr
);
835 if ((png_ptr
->row_number
& 0x07) || png_ptr
->width
< 5)
837 png_write_finish_row(png_ptr
);
842 if ((png_ptr
->row_number
& 0x07) != 4)
844 png_write_finish_row(png_ptr
);
849 if ((png_ptr
->row_number
& 0x03) || png_ptr
->width
< 3)
851 png_write_finish_row(png_ptr
);
856 if ((png_ptr
->row_number
& 0x03) != 2)
858 png_write_finish_row(png_ptr
);
863 if ((png_ptr
->row_number
& 0x01) || png_ptr
->width
< 2)
865 png_write_finish_row(png_ptr
);
870 if (!(png_ptr
->row_number
& 0x01))
872 png_write_finish_row(png_ptr
);
880 /* Set up row info for transformations */
881 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
882 png_ptr
->row_info
.width
= png_ptr
->usr_width
;
883 png_ptr
->row_info
.channels
= png_ptr
->usr_channels
;
884 png_ptr
->row_info
.bit_depth
= png_ptr
->usr_bit_depth
;
885 png_ptr
->row_info
.pixel_depth
= (png_byte
)(png_ptr
->row_info
.bit_depth
*
886 png_ptr
->row_info
.channels
);
888 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
889 png_ptr
->row_info
.width
);
891 png_debug1(3, "row_info->color_type = %d", png_ptr
->row_info
.color_type
);
892 png_debug1(3, "row_info->width = %lu", png_ptr
->row_info
.width
);
893 png_debug1(3, "row_info->channels = %d", png_ptr
->row_info
.channels
);
894 png_debug1(3, "row_info->bit_depth = %d", png_ptr
->row_info
.bit_depth
);
895 png_debug1(3, "row_info->pixel_depth = %d", png_ptr
->row_info
.pixel_depth
);
896 png_debug1(3, "row_info->rowbytes = %lu", png_ptr
->row_info
.rowbytes
);
898 /* Copy user's row into buffer, leaving room for filter byte. */
899 png_memcpy_check(png_ptr
, png_ptr
->row_buf
+ 1, row
,
900 png_ptr
->row_info
.rowbytes
);
902 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
903 /* Handle interlacing */
904 if (png_ptr
->interlaced
&& png_ptr
->pass
< 6 &&
905 (png_ptr
->transformations
& PNG_INTERLACE
))
907 png_do_write_interlace(&(png_ptr
->row_info
),
908 png_ptr
->row_buf
+ 1, png_ptr
->pass
);
909 /* This should always get caught above, but still ... */
910 if (!(png_ptr
->row_info
.width
))
912 png_write_finish_row(png_ptr
);
918 /* Handle other transformations */
919 if (png_ptr
->transformations
)
920 png_do_write_transformations(png_ptr
);
922 #if defined(PNG_MNG_FEATURES_SUPPORTED)
923 /* Write filter_method 64 (intrapixel differencing) only if
924 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
925 * 2. Libpng did not write a PNG signature (this filter_method is only
926 * used in PNG datastreams that are embedded in MNG datastreams) and
927 * 3. The application called png_permit_mng_features with a mask that
928 * included PNG_FLAG_MNG_FILTER_64 and
929 * 4. The filter_method is 64 and
930 * 5. The color_type is RGB or RGBA
932 if ((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
933 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
935 /* Intrapixel differencing */
936 png_do_write_intrapixel(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
940 /* Find a filter if necessary, filter the row and write it out. */
941 png_write_find_filter(png_ptr
, &(png_ptr
->row_info
));
943 if (png_ptr
->write_row_fn
!= NULL
)
944 (*(png_ptr
->write_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
947 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
948 /* Set the automatic flush interval or 0 to turn flushing off */
950 png_set_flush(png_structp png_ptr
, int nrows
)
952 png_debug(1, "in png_set_flush");
955 png_ptr
->flush_dist
= (nrows
< 0 ? 0 : nrows
);
958 /* Flush the current output buffers now */
960 png_write_flush(png_structp png_ptr
)
964 png_debug(1, "in png_write_flush");
967 /* We have already written out all of the data */
968 if (png_ptr
->row_number
>= png_ptr
->num_rows
)
975 /* Compress the data */
976 ret
= deflate(&png_ptr
->zstream
, Z_SYNC_FLUSH
);
979 /* Check for compression errors */
982 if (png_ptr
->zstream
.msg
!= NULL
)
983 png_error(png_ptr
, png_ptr
->zstream
.msg
);
985 png_error(png_ptr
, "zlib error");
988 if (!(png_ptr
->zstream
.avail_out
))
990 /* Write the IDAT and reset the zlib output buffer */
991 png_write_IDAT(png_ptr
, png_ptr
->zbuf
,
993 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
994 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
997 } while(wrote_IDAT
== 1);
999 /* If there is any data left to be output, write it into a new IDAT */
1000 if (png_ptr
->zbuf_size
!= png_ptr
->zstream
.avail_out
)
1002 /* Write the IDAT and reset the zlib output buffer */
1003 png_write_IDAT(png_ptr
, png_ptr
->zbuf
,
1004 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
1005 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1006 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1008 png_ptr
->flush_rows
= 0;
1011 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
1013 /* Free all memory used by the write */
1015 png_destroy_write_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
)
1017 png_structp png_ptr
= NULL
;
1018 png_infop info_ptr
= NULL
;
1019 #ifdef PNG_USER_MEM_SUPPORTED
1020 png_free_ptr free_fn
= NULL
;
1021 png_voidp mem_ptr
= NULL
;
1024 png_debug(1, "in png_destroy_write_struct");
1025 if (png_ptr_ptr
!= NULL
)
1027 png_ptr
= *png_ptr_ptr
;
1028 #ifdef PNG_USER_MEM_SUPPORTED
1029 free_fn
= png_ptr
->free_fn
;
1030 mem_ptr
= png_ptr
->mem_ptr
;
1034 #ifdef PNG_USER_MEM_SUPPORTED
1035 if (png_ptr
!= NULL
)
1037 free_fn
= png_ptr
->free_fn
;
1038 mem_ptr
= png_ptr
->mem_ptr
;
1042 if (info_ptr_ptr
!= NULL
)
1043 info_ptr
= *info_ptr_ptr
;
1045 if (info_ptr
!= NULL
)
1047 if (png_ptr
!= NULL
)
1049 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ALL
, -1);
1051 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1052 if (png_ptr
->num_chunk_list
)
1054 png_free(png_ptr
, png_ptr
->chunk_list
);
1055 png_ptr
->chunk_list
=NULL
;
1056 png_ptr
->num_chunk_list
= 0;
1061 #ifdef PNG_USER_MEM_SUPPORTED
1062 png_destroy_struct_2((png_voidp
)info_ptr
, (png_free_ptr
)free_fn
,
1063 (png_voidp
)mem_ptr
);
1065 png_destroy_struct((png_voidp
)info_ptr
);
1067 *info_ptr_ptr
= NULL
;
1070 if (png_ptr
!= NULL
)
1072 png_write_destroy(png_ptr
);
1073 #ifdef PNG_USER_MEM_SUPPORTED
1074 png_destroy_struct_2((png_voidp
)png_ptr
, (png_free_ptr
)free_fn
,
1075 (png_voidp
)mem_ptr
);
1077 png_destroy_struct((png_voidp
)png_ptr
);
1079 *png_ptr_ptr
= NULL
;
1084 /* Free any memory used in png_ptr struct (old method) */
1086 png_write_destroy(png_structp png_ptr
)
1088 #ifdef PNG_SETJMP_SUPPORTED
1089 jmp_buf tmp_jmp
; /* Save jump buffer */
1091 png_error_ptr error_fn
;
1092 png_error_ptr warning_fn
;
1093 png_voidp error_ptr
;
1094 #ifdef PNG_USER_MEM_SUPPORTED
1095 png_free_ptr free_fn
;
1098 png_debug(1, "in png_write_destroy");
1099 /* Free any memory zlib uses */
1100 deflateEnd(&png_ptr
->zstream
);
1102 /* Free our memory. png_free checks NULL for us. */
1103 png_free(png_ptr
, png_ptr
->zbuf
);
1104 png_free(png_ptr
, png_ptr
->row_buf
);
1105 #ifndef PNG_NO_WRITE_FILTER
1106 png_free(png_ptr
, png_ptr
->prev_row
);
1107 png_free(png_ptr
, png_ptr
->sub_row
);
1108 png_free(png_ptr
, png_ptr
->up_row
);
1109 png_free(png_ptr
, png_ptr
->avg_row
);
1110 png_free(png_ptr
, png_ptr
->paeth_row
);
1113 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1114 png_free(png_ptr
, png_ptr
->time_buffer
);
1117 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1118 png_free(png_ptr
, png_ptr
->prev_filters
);
1119 png_free(png_ptr
, png_ptr
->filter_weights
);
1120 png_free(png_ptr
, png_ptr
->inv_filter_weights
);
1121 png_free(png_ptr
, png_ptr
->filter_costs
);
1122 png_free(png_ptr
, png_ptr
->inv_filter_costs
);
1125 #ifdef PNG_SETJMP_SUPPORTED
1126 /* Reset structure */
1127 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, png_sizeof(jmp_buf));
1130 error_fn
= png_ptr
->error_fn
;
1131 warning_fn
= png_ptr
->warning_fn
;
1132 error_ptr
= png_ptr
->error_ptr
;
1133 #ifdef PNG_USER_MEM_SUPPORTED
1134 free_fn
= png_ptr
->free_fn
;
1137 png_memset(png_ptr
, 0, png_sizeof(png_struct
));
1139 png_ptr
->error_fn
= error_fn
;
1140 png_ptr
->warning_fn
= warning_fn
;
1141 png_ptr
->error_ptr
= error_ptr
;
1142 #ifdef PNG_USER_MEM_SUPPORTED
1143 png_ptr
->free_fn
= free_fn
;
1146 #ifdef PNG_SETJMP_SUPPORTED
1147 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, png_sizeof(jmp_buf));
1151 /* Allow the application to select one or more row filters to use. */
1153 png_set_filter(png_structp png_ptr
, int method
, int filters
)
1155 png_debug(1, "in png_set_filter");
1156 if (png_ptr
== NULL
)
1158 #if defined(PNG_MNG_FEATURES_SUPPORTED)
1159 if ((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
1160 (method
== PNG_INTRAPIXEL_DIFFERENCING
))
1161 method
= PNG_FILTER_TYPE_BASE
;
1163 if (method
== PNG_FILTER_TYPE_BASE
)
1165 switch (filters
& (PNG_ALL_FILTERS
| 0x07))
1167 #ifndef PNG_NO_WRITE_FILTER
1170 case 7: png_warning(png_ptr
, "Unknown row filter for method 0");
1171 #endif /* PNG_NO_WRITE_FILTER */
1172 case PNG_FILTER_VALUE_NONE
:
1173 png_ptr
->do_filter
=PNG_FILTER_NONE
; break;
1174 #ifndef PNG_NO_WRITE_FILTER
1175 case PNG_FILTER_VALUE_SUB
:
1176 png_ptr
->do_filter
=PNG_FILTER_SUB
; break;
1177 case PNG_FILTER_VALUE_UP
:
1178 png_ptr
->do_filter
=PNG_FILTER_UP
; break;
1179 case PNG_FILTER_VALUE_AVG
:
1180 png_ptr
->do_filter
=PNG_FILTER_AVG
; break;
1181 case PNG_FILTER_VALUE_PAETH
:
1182 png_ptr
->do_filter
=PNG_FILTER_PAETH
; break;
1183 default: png_ptr
->do_filter
= (png_byte
)filters
; break;
1185 default: png_warning(png_ptr
, "Unknown row filter for method 0");
1186 #endif /* PNG_NO_WRITE_FILTER */
1189 /* If we have allocated the row_buf, this means we have already started
1190 * with the image and we should have allocated all of the filter buffers
1191 * that have been selected. If prev_row isn't already allocated, then
1192 * it is too late to start using the filters that need it, since we
1193 * will be missing the data in the previous row. If an application
1194 * wants to start and stop using particular filters during compression,
1195 * it should start out with all of the filters, and then add and
1196 * remove them after the start of compression.
1198 if (png_ptr
->row_buf
!= NULL
)
1200 #ifndef PNG_NO_WRITE_FILTER
1201 if ((png_ptr
->do_filter
& PNG_FILTER_SUB
) && png_ptr
->sub_row
== NULL
)
1203 png_ptr
->sub_row
= (png_bytep
)png_malloc(png_ptr
,
1204 (png_ptr
->rowbytes
+ 1));
1205 png_ptr
->sub_row
[0] = PNG_FILTER_VALUE_SUB
;
1208 if ((png_ptr
->do_filter
& PNG_FILTER_UP
) && png_ptr
->up_row
== NULL
)
1210 if (png_ptr
->prev_row
== NULL
)
1212 png_warning(png_ptr
, "Can't add Up filter after starting");
1213 png_ptr
->do_filter
&= ~PNG_FILTER_UP
;
1217 png_ptr
->up_row
= (png_bytep
)png_malloc(png_ptr
,
1218 (png_ptr
->rowbytes
+ 1));
1219 png_ptr
->up_row
[0] = PNG_FILTER_VALUE_UP
;
1223 if ((png_ptr
->do_filter
& PNG_FILTER_AVG
) && png_ptr
->avg_row
== NULL
)
1225 if (png_ptr
->prev_row
== NULL
)
1227 png_warning(png_ptr
, "Can't add Average filter after starting");
1228 png_ptr
->do_filter
&= ~PNG_FILTER_AVG
;
1232 png_ptr
->avg_row
= (png_bytep
)png_malloc(png_ptr
,
1233 (png_ptr
->rowbytes
+ 1));
1234 png_ptr
->avg_row
[0] = PNG_FILTER_VALUE_AVG
;
1238 if ((png_ptr
->do_filter
& PNG_FILTER_PAETH
) &&
1239 png_ptr
->paeth_row
== NULL
)
1241 if (png_ptr
->prev_row
== NULL
)
1243 png_warning(png_ptr
, "Can't add Paeth filter after starting");
1244 png_ptr
->do_filter
&= (png_byte
)(~PNG_FILTER_PAETH
);
1248 png_ptr
->paeth_row
= (png_bytep
)png_malloc(png_ptr
,
1249 (png_ptr
->rowbytes
+ 1));
1250 png_ptr
->paeth_row
[0] = PNG_FILTER_VALUE_PAETH
;
1254 if (png_ptr
->do_filter
== PNG_NO_FILTERS
)
1255 #endif /* PNG_NO_WRITE_FILTER */
1256 png_ptr
->do_filter
= PNG_FILTER_NONE
;
1260 png_error(png_ptr
, "Unknown custom filter method");
1263 /* This allows us to influence the way in which libpng chooses the "best"
1264 * filter for the current scanline. While the "minimum-sum-of-absolute-
1265 * differences metric is relatively fast and effective, there is some
1266 * question as to whether it can be improved upon by trying to keep the
1267 * filtered data going to zlib more consistent, hopefully resulting in
1268 * better compression.
1270 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
1272 png_set_filter_heuristics(png_structp png_ptr
, int heuristic_method
,
1273 int num_weights
, png_doublep filter_weights
,
1274 png_doublep filter_costs
)
1278 png_debug(1, "in png_set_filter_heuristics");
1279 if (png_ptr
== NULL
)
1281 if (heuristic_method
>= PNG_FILTER_HEURISTIC_LAST
)
1283 png_warning(png_ptr
, "Unknown filter heuristic method");
1287 if (heuristic_method
== PNG_FILTER_HEURISTIC_DEFAULT
)
1289 heuristic_method
= PNG_FILTER_HEURISTIC_UNWEIGHTED
;
1292 if (num_weights
< 0 || filter_weights
== NULL
||
1293 heuristic_method
== PNG_FILTER_HEURISTIC_UNWEIGHTED
)
1298 png_ptr
->num_prev_filters
= (png_byte
)num_weights
;
1299 png_ptr
->heuristic_method
= (png_byte
)heuristic_method
;
1301 if (num_weights
> 0)
1303 if (png_ptr
->prev_filters
== NULL
)
1305 png_ptr
->prev_filters
= (png_bytep
)png_malloc(png_ptr
,
1306 (png_uint_32
)(png_sizeof(png_byte
) * num_weights
));
1308 /* To make sure that the weighting starts out fairly */
1309 for (i
= 0; i
< num_weights
; i
++)
1311 png_ptr
->prev_filters
[i
] = 255;
1315 if (png_ptr
->filter_weights
== NULL
)
1317 png_ptr
->filter_weights
= (png_uint_16p
)png_malloc(png_ptr
,
1318 (png_uint_32
)(png_sizeof(png_uint_16
) * num_weights
));
1320 png_ptr
->inv_filter_weights
= (png_uint_16p
)png_malloc(png_ptr
,
1321 (png_uint_32
)(png_sizeof(png_uint_16
) * num_weights
));
1322 for (i
= 0; i
< num_weights
; i
++)
1324 png_ptr
->inv_filter_weights
[i
] =
1325 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
1329 for (i
= 0; i
< num_weights
; i
++)
1331 if (filter_weights
[i
] < 0.0)
1333 png_ptr
->inv_filter_weights
[i
] =
1334 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
1338 png_ptr
->inv_filter_weights
[i
] =
1339 (png_uint_16
)((double)PNG_WEIGHT_FACTOR
*filter_weights
[i
]+0.5);
1340 png_ptr
->filter_weights
[i
] =
1341 (png_uint_16
)((double)PNG_WEIGHT_FACTOR
/filter_weights
[i
]+0.5);
1346 /* If, in the future, there are other filter methods, this would
1347 * need to be based on png_ptr->filter.
1349 if (png_ptr
->filter_costs
== NULL
)
1351 png_ptr
->filter_costs
= (png_uint_16p
)png_malloc(png_ptr
,
1352 (png_uint_32
)(png_sizeof(png_uint_16
) * PNG_FILTER_VALUE_LAST
));
1354 png_ptr
->inv_filter_costs
= (png_uint_16p
)png_malloc(png_ptr
,
1355 (png_uint_32
)(png_sizeof(png_uint_16
) * PNG_FILTER_VALUE_LAST
));
1357 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++)
1359 png_ptr
->inv_filter_costs
[i
] =
1360 png_ptr
->filter_costs
[i
] = PNG_COST_FACTOR
;
1364 /* Here is where we set the relative costs of the different filters. We
1365 * should take the desired compression level into account when setting
1366 * the costs, so that Paeth, for instance, has a high relative cost at low
1367 * compression levels, while it has a lower relative cost at higher
1368 * compression settings. The filter types are in order of increasing
1369 * relative cost, so it would be possible to do this with an algorithm.
1371 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++)
1373 if (filter_costs
== NULL
|| filter_costs
[i
] < 0.0)
1375 png_ptr
->inv_filter_costs
[i
] =
1376 png_ptr
->filter_costs
[i
] = PNG_COST_FACTOR
;
1378 else if (filter_costs
[i
] >= 1.0)
1380 png_ptr
->inv_filter_costs
[i
] =
1381 (png_uint_16
)((double)PNG_COST_FACTOR
/ filter_costs
[i
] + 0.5);
1382 png_ptr
->filter_costs
[i
] =
1383 (png_uint_16
)((double)PNG_COST_FACTOR
* filter_costs
[i
] + 0.5);
1387 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1390 png_set_compression_level(png_structp png_ptr
, int level
)
1392 png_debug(1, "in png_set_compression_level");
1393 if (png_ptr
== NULL
)
1395 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_LEVEL
;
1396 png_ptr
->zlib_level
= level
;
1400 png_set_compression_mem_level(png_structp png_ptr
, int mem_level
)
1402 png_debug(1, "in png_set_compression_mem_level");
1403 if (png_ptr
== NULL
)
1405 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL
;
1406 png_ptr
->zlib_mem_level
= mem_level
;
1410 png_set_compression_strategy(png_structp png_ptr
, int strategy
)
1412 png_debug(1, "in png_set_compression_strategy");
1413 if (png_ptr
== NULL
)
1415 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_STRATEGY
;
1416 png_ptr
->zlib_strategy
= strategy
;
1420 png_set_compression_window_bits(png_structp png_ptr
, int window_bits
)
1422 if (png_ptr
== NULL
)
1424 if (window_bits
> 15)
1425 png_warning(png_ptr
, "Only compression windows <= 32k supported by PNG");
1426 else if (window_bits
< 8)
1427 png_warning(png_ptr
, "Only compression windows >= 256 supported by PNG");
1429 /* Avoid libpng bug with 256-byte windows */
1430 if (window_bits
== 8)
1432 png_warning(png_ptr
, "Compression window is being reset to 512");
1436 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS
;
1437 png_ptr
->zlib_window_bits
= window_bits
;
1441 png_set_compression_method(png_structp png_ptr
, int method
)
1443 png_debug(1, "in png_set_compression_method");
1444 if (png_ptr
== NULL
)
1447 png_warning(png_ptr
, "Only compression method 8 is supported by PNG");
1448 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_METHOD
;
1449 png_ptr
->zlib_method
= method
;
1453 png_set_write_status_fn(png_structp png_ptr
, png_write_status_ptr write_row_fn
)
1455 if (png_ptr
== NULL
)
1457 png_ptr
->write_row_fn
= write_row_fn
;
1460 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1462 png_set_write_user_transform_fn(png_structp png_ptr
, png_user_transform_ptr
1463 write_user_transform_fn
)
1465 png_debug(1, "in png_set_write_user_transform_fn");
1466 if (png_ptr
== NULL
)
1468 png_ptr
->transformations
|= PNG_USER_TRANSFORM
;
1469 png_ptr
->write_user_transform_fn
= write_user_transform_fn
;
1474 #if defined(PNG_INFO_IMAGE_SUPPORTED)
1476 png_write_png(png_structp png_ptr
, png_infop info_ptr
,
1477 int transforms
, voidp params
)
1479 if (png_ptr
== NULL
|| info_ptr
== NULL
)
1481 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1482 /* Invert the alpha channel from opacity to transparency */
1483 if (transforms
& PNG_TRANSFORM_INVERT_ALPHA
)
1484 png_set_invert_alpha(png_ptr
);
1487 /* Write the file header information. */
1488 png_write_info(png_ptr
, info_ptr
);
1490 /* ------ these transformations don't touch the info structure ------- */
1492 #if defined(PNG_WRITE_INVERT_SUPPORTED)
1493 /* Invert monochrome pixels */
1494 if (transforms
& PNG_TRANSFORM_INVERT_MONO
)
1495 png_set_invert_mono(png_ptr
);
1498 #if defined(PNG_WRITE_SHIFT_SUPPORTED)
1499 /* Shift the pixels up to a legal bit depth and fill in
1500 * as appropriate to correctly scale the image.
1502 if ((transforms
& PNG_TRANSFORM_SHIFT
)
1503 && (info_ptr
->valid
& PNG_INFO_sBIT
))
1504 png_set_shift(png_ptr
, &info_ptr
->sig_bit
);
1507 #if defined(PNG_WRITE_PACK_SUPPORTED)
1508 /* Pack pixels into bytes */
1509 if (transforms
& PNG_TRANSFORM_PACKING
)
1510 png_set_packing(png_ptr
);
1513 #if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
1514 /* Swap location of alpha bytes from ARGB to RGBA */
1515 if (transforms
& PNG_TRANSFORM_SWAP_ALPHA
)
1516 png_set_swap_alpha(png_ptr
);
1519 #if defined(PNG_WRITE_FILLER_SUPPORTED)
1520 /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */
1521 if (transforms
& PNG_TRANSFORM_STRIP_FILLER_AFTER
)
1522 png_set_filler(png_ptr
, 0, PNG_FILLER_AFTER
);
1523 else if (transforms
& PNG_TRANSFORM_STRIP_FILLER_BEFORE
)
1524 png_set_filler(png_ptr
, 0, PNG_FILLER_BEFORE
);
1527 #if defined(PNG_WRITE_BGR_SUPPORTED)
1528 /* Flip BGR pixels to RGB */
1529 if (transforms
& PNG_TRANSFORM_BGR
)
1530 png_set_bgr(png_ptr
);
1533 #if defined(PNG_WRITE_SWAP_SUPPORTED)
1534 /* Swap bytes of 16-bit files to most significant byte first */
1535 if (transforms
& PNG_TRANSFORM_SWAP_ENDIAN
)
1536 png_set_swap(png_ptr
);
1539 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
1540 /* Swap bits of 1, 2, 4 bit packed pixel formats */
1541 if (transforms
& PNG_TRANSFORM_PACKSWAP
)
1542 png_set_packswap(png_ptr
);
1545 /* ----------------------- end of transformations ------------------- */
1547 /* Write the bits */
1548 if (info_ptr
->valid
& PNG_INFO_IDAT
)
1549 png_write_image(png_ptr
, info_ptr
->row_pointers
);
1551 /* It is REQUIRED to call this to finish writing the rest of the file */
1552 png_write_end(png_ptr
, info_ptr
);
1554 transforms
= transforms
; /* Quiet compiler warnings */
1558 #endif /* PNG_WRITE_SUPPORTED */