2 /* pngwrite.c - general routines to write a PNG file
4 * Last changed in libpng 1.2.15 January 5, 2007
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2007 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\n");
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. */
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
);
58 #ifdef PNG_FIXED_POINT_SUPPORTED
59 png_write_gAMA_fixed(png_ptr
, info_ptr
->int_gamma
);
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
);
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
);
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
);
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
);
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
);
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
;
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 png_write_chunk(png_ptr
, up
->name
, up
->data
, up
->size
);
120 png_ptr
->mode
|= PNG_WROTE_INFO_BEFORE_PLTE
;
125 png_write_info(png_structp png_ptr
, png_infop info_ptr
)
127 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
131 png_debug(1, "in png_write_info\n");
133 if (png_ptr
== NULL
|| info_ptr
== NULL
)
136 png_write_info_before_PLTE(png_ptr
, info_ptr
);
138 if (info_ptr
->valid
& PNG_INFO_PLTE
)
139 png_write_PLTE(png_ptr
, info_ptr
->palette
,
140 (png_uint_32
)info_ptr
->num_palette
);
141 else if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
142 png_error(png_ptr
, "Valid palette required for paletted images");
144 #if defined(PNG_WRITE_tRNS_SUPPORTED)
145 if (info_ptr
->valid
& PNG_INFO_tRNS
)
147 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
148 /* invert the alpha channel (in tRNS) */
149 if ((png_ptr
->transformations
& PNG_INVERT_ALPHA
) &&
150 info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
153 for (j
=0; j
<(int)info_ptr
->num_trans
; j
++)
154 info_ptr
->trans
[j
] = (png_byte
)(255 - info_ptr
->trans
[j
]);
157 png_write_tRNS(png_ptr
, info_ptr
->trans
, &(info_ptr
->trans_values
),
158 info_ptr
->num_trans
, info_ptr
->color_type
);
161 #if defined(PNG_WRITE_bKGD_SUPPORTED)
162 if (info_ptr
->valid
& PNG_INFO_bKGD
)
163 png_write_bKGD(png_ptr
, &(info_ptr
->background
), info_ptr
->color_type
);
165 #if defined(PNG_WRITE_hIST_SUPPORTED)
166 if (info_ptr
->valid
& PNG_INFO_hIST
)
167 png_write_hIST(png_ptr
, info_ptr
->hist
, info_ptr
->num_palette
);
169 #if defined(PNG_WRITE_oFFs_SUPPORTED)
170 if (info_ptr
->valid
& PNG_INFO_oFFs
)
171 png_write_oFFs(png_ptr
, info_ptr
->x_offset
, info_ptr
->y_offset
,
172 info_ptr
->offset_unit_type
);
174 #if defined(PNG_WRITE_pCAL_SUPPORTED)
175 if (info_ptr
->valid
& PNG_INFO_pCAL
)
176 png_write_pCAL(png_ptr
, info_ptr
->pcal_purpose
, info_ptr
->pcal_X0
,
177 info_ptr
->pcal_X1
, info_ptr
->pcal_type
, info_ptr
->pcal_nparams
,
178 info_ptr
->pcal_units
, info_ptr
->pcal_params
);
180 #if defined(PNG_WRITE_sCAL_SUPPORTED)
181 if (info_ptr
->valid
& PNG_INFO_sCAL
)
182 #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
183 png_write_sCAL(png_ptr
, (int)info_ptr
->scal_unit
,
184 info_ptr
->scal_pixel_width
, info_ptr
->scal_pixel_height
);
186 #ifdef PNG_FIXED_POINT_SUPPORTED
187 png_write_sCAL_s(png_ptr
, (int)info_ptr
->scal_unit
,
188 info_ptr
->scal_s_width
, info_ptr
->scal_s_height
);
191 "png_write_sCAL not supported; sCAL chunk not written.");
195 #if defined(PNG_WRITE_pHYs_SUPPORTED)
196 if (info_ptr
->valid
& PNG_INFO_pHYs
)
197 png_write_pHYs(png_ptr
, info_ptr
->x_pixels_per_unit
,
198 info_ptr
->y_pixels_per_unit
, info_ptr
->phys_unit_type
);
200 #if defined(PNG_WRITE_tIME_SUPPORTED)
201 if (info_ptr
->valid
& PNG_INFO_tIME
)
203 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
204 png_ptr
->mode
|= PNG_WROTE_tIME
;
207 #if defined(PNG_WRITE_sPLT_SUPPORTED)
208 if (info_ptr
->valid
& PNG_INFO_sPLT
)
209 for (i
= 0; i
< (int)info_ptr
->splt_palettes_num
; i
++)
210 png_write_sPLT(png_ptr
, info_ptr
->splt_palettes
+ i
);
212 #if defined(PNG_WRITE_TEXT_SUPPORTED)
213 /* Check to see if we need to write text chunks */
214 for (i
= 0; i
< info_ptr
->num_text
; i
++)
216 png_debug2(2, "Writing header text chunk %d, type %d\n", i
,
217 info_ptr
->text
[i
].compression
);
218 /* an internationalized chunk? */
219 if (info_ptr
->text
[i
].compression
> 0)
221 #if defined(PNG_WRITE_iTXt_SUPPORTED)
222 /* write international chunk */
223 png_write_iTXt(png_ptr
,
224 info_ptr
->text
[i
].compression
,
225 info_ptr
->text
[i
].key
,
226 info_ptr
->text
[i
].lang
,
227 info_ptr
->text
[i
].lang_key
,
228 info_ptr
->text
[i
].text
);
230 png_warning(png_ptr
, "Unable to write international text");
232 /* Mark this chunk as written */
233 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
235 /* If we want a compressed text chunk */
236 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_zTXt
)
238 #if defined(PNG_WRITE_zTXt_SUPPORTED)
239 /* write compressed chunk */
240 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
241 info_ptr
->text
[i
].text
, 0,
242 info_ptr
->text
[i
].compression
);
244 png_warning(png_ptr
, "Unable to write compressed text");
246 /* Mark this chunk as written */
247 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
249 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
251 #if defined(PNG_WRITE_tEXt_SUPPORTED)
252 /* write uncompressed chunk */
253 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
254 info_ptr
->text
[i
].text
,
257 png_warning(png_ptr
, "Unable to write uncompressed text");
259 /* Mark this chunk as written */
260 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
264 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
265 if (info_ptr
->unknown_chunks_num
)
267 png_unknown_chunk
*up
;
269 png_debug(5, "writing extra chunks\n");
271 for (up
= info_ptr
->unknown_chunks
;
272 up
< info_ptr
->unknown_chunks
+ info_ptr
->unknown_chunks_num
;
275 int keep
=png_handle_as_unknown(png_ptr
, up
->name
);
276 if (keep
!= PNG_HANDLE_CHUNK_NEVER
&&
277 up
->location
&& (up
->location
& PNG_HAVE_PLTE
) &&
278 !(up
->location
& PNG_HAVE_IDAT
) &&
279 ((up
->name
[3] & 0x20) || keep
== PNG_HANDLE_CHUNK_ALWAYS
||
280 (png_ptr
->flags
& PNG_FLAG_KEEP_UNSAFE_CHUNKS
)))
282 png_write_chunk(png_ptr
, up
->name
, up
->data
, up
->size
);
289 /* Writes the end of the PNG file. If you don't want to write comments or
290 * time information, you can pass NULL for info. If you already wrote these
291 * in png_write_info(), do not write them again here. If you have long
292 * comments, I suggest writing them here, and compressing them.
295 png_write_end(png_structp png_ptr
, png_infop info_ptr
)
297 png_debug(1, "in png_write_end\n");
300 if (!(png_ptr
->mode
& PNG_HAVE_IDAT
))
301 png_error(png_ptr
, "No IDATs written into file");
303 /* see if user wants us to write information chunks */
304 if (info_ptr
!= NULL
)
306 #if defined(PNG_WRITE_TEXT_SUPPORTED)
307 int i
; /* local index variable */
309 #if defined(PNG_WRITE_tIME_SUPPORTED)
310 /* check to see if user has supplied a time chunk */
311 if ((info_ptr
->valid
& PNG_INFO_tIME
) &&
312 !(png_ptr
->mode
& PNG_WROTE_tIME
))
313 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
315 #if defined(PNG_WRITE_TEXT_SUPPORTED)
316 /* loop through comment chunks */
317 for (i
= 0; i
< info_ptr
->num_text
; i
++)
319 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i
,
320 info_ptr
->text
[i
].compression
);
321 /* an internationalized chunk? */
322 if (info_ptr
->text
[i
].compression
> 0)
324 #if defined(PNG_WRITE_iTXt_SUPPORTED)
325 /* write international chunk */
326 png_write_iTXt(png_ptr
,
327 info_ptr
->text
[i
].compression
,
328 info_ptr
->text
[i
].key
,
329 info_ptr
->text
[i
].lang
,
330 info_ptr
->text
[i
].lang_key
,
331 info_ptr
->text
[i
].text
);
333 png_warning(png_ptr
, "Unable to write international text");
335 /* Mark this chunk as written */
336 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
338 else if (info_ptr
->text
[i
].compression
>= PNG_TEXT_COMPRESSION_zTXt
)
340 #if defined(PNG_WRITE_zTXt_SUPPORTED)
341 /* write compressed chunk */
342 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
343 info_ptr
->text
[i
].text
, 0,
344 info_ptr
->text
[i
].compression
);
346 png_warning(png_ptr
, "Unable to write compressed text");
348 /* Mark this chunk as written */
349 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
351 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
353 #if defined(PNG_WRITE_tEXt_SUPPORTED)
354 /* write uncompressed chunk */
355 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
356 info_ptr
->text
[i
].text
, 0);
358 png_warning(png_ptr
, "Unable to write uncompressed text");
361 /* Mark this chunk as written */
362 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
366 #if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
367 if (info_ptr
->unknown_chunks_num
)
369 png_unknown_chunk
*up
;
371 png_debug(5, "writing extra chunks\n");
373 for (up
= info_ptr
->unknown_chunks
;
374 up
< info_ptr
->unknown_chunks
+ info_ptr
->unknown_chunks_num
;
377 int keep
=png_handle_as_unknown(png_ptr
, up
->name
);
378 if (keep
!= PNG_HANDLE_CHUNK_NEVER
&&
379 up
->location
&& (up
->location
& PNG_AFTER_IDAT
) &&
380 ((up
->name
[3] & 0x20) || keep
== PNG_HANDLE_CHUNK_ALWAYS
||
381 (png_ptr
->flags
& PNG_FLAG_KEEP_UNSAFE_CHUNKS
)))
383 png_write_chunk(png_ptr
, up
->name
, up
->data
, up
->size
);
390 png_ptr
->mode
|= PNG_AFTER_IDAT
;
392 /* write end of PNG file */
393 png_write_IEND(png_ptr
);
396 #if defined(PNG_WRITE_tIME_SUPPORTED)
397 #if !defined(_WIN32_WCE)
398 /* "time.h" functions are not supported on WindowsCE */
400 png_convert_from_struct_tm(png_timep ptime
, struct tm FAR
* ttime
)
402 png_debug(1, "in png_convert_from_struct_tm\n");
403 ptime
->year
= (png_uint_16
)(1900 + ttime
->tm_year
);
404 ptime
->month
= (png_byte
)(ttime
->tm_mon
+ 1);
405 ptime
->day
= (png_byte
)ttime
->tm_mday
;
406 ptime
->hour
= (png_byte
)ttime
->tm_hour
;
407 ptime
->minute
= (png_byte
)ttime
->tm_min
;
408 ptime
->second
= (png_byte
)ttime
->tm_sec
;
412 png_convert_from_time_t(png_timep ptime
, time_t ttime
)
416 png_debug(1, "in png_convert_from_time_t\n");
417 tbuf
= gmtime(&ttime
);
418 png_convert_from_struct_tm(ptime
, tbuf
);
423 /* Initialize png_ptr structure, and allocate any memory needed */
425 png_create_write_struct(png_const_charp user_png_ver
, png_voidp error_ptr
,
426 png_error_ptr error_fn
, png_error_ptr warn_fn
)
428 #ifdef PNG_USER_MEM_SUPPORTED
429 return (png_create_write_struct_2(user_png_ver
, error_ptr
, error_fn
,
430 warn_fn
, png_voidp_NULL
, png_malloc_ptr_NULL
, png_free_ptr_NULL
));
433 /* Alternate initialize png_ptr structure, and allocate any memory needed */
435 png_create_write_struct_2(png_const_charp user_png_ver
, png_voidp error_ptr
,
436 png_error_ptr error_fn
, png_error_ptr warn_fn
, png_voidp mem_ptr
,
437 png_malloc_ptr malloc_fn
, png_free_ptr free_fn
)
439 #endif /* PNG_USER_MEM_SUPPORTED */
441 #ifdef PNG_SETJMP_SUPPORTED
442 #ifdef USE_FAR_KEYWORD
447 png_debug(1, "in png_create_write_struct\n");
448 #ifdef PNG_USER_MEM_SUPPORTED
449 png_ptr
= (png_structp
)png_create_struct_2(PNG_STRUCT_PNG
,
450 (png_malloc_ptr
)malloc_fn
, (png_voidp
)mem_ptr
);
452 png_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
453 #endif /* PNG_USER_MEM_SUPPORTED */
457 /* added at libpng-1.2.6 */
458 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
459 png_ptr
->user_width_max
=PNG_USER_WIDTH_MAX
;
460 png_ptr
->user_height_max
=PNG_USER_HEIGHT_MAX
;
463 #ifdef PNG_SETJMP_SUPPORTED
464 #ifdef USE_FAR_KEYWORD
467 if (setjmp(png_ptr
->jmpbuf
))
470 png_free(png_ptr
, png_ptr
->zbuf
);
472 png_destroy_struct(png_ptr
);
475 #ifdef USE_FAR_KEYWORD
476 png_memcpy(png_ptr
->jmpbuf
,jmpbuf
,png_sizeof(jmp_buf));
480 #ifdef PNG_USER_MEM_SUPPORTED
481 png_set_mem_fn(png_ptr
, mem_ptr
, malloc_fn
, free_fn
);
482 #endif /* PNG_USER_MEM_SUPPORTED */
483 png_set_error_fn(png_ptr
, error_ptr
, error_fn
, warn_fn
);
488 if(user_png_ver
[i
] != png_libpng_ver
[i
])
489 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
490 } while (png_libpng_ver
[i
++]);
492 if (png_ptr
->flags
& PNG_FLAG_LIBRARY_MISMATCH
)
494 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
495 * we must recompile any applications that use any older library version.
496 * For versions after libpng 1.0, we will be compatible, so we need
497 * only check the first digit.
499 if (user_png_ver
== NULL
|| user_png_ver
[0] != png_libpng_ver
[0] ||
500 (user_png_ver
[0] == '1' && user_png_ver
[2] != png_libpng_ver
[2]) ||
501 (user_png_ver
[0] == '0' && user_png_ver
[2] < '9'))
503 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
507 png_snprintf(msg
, 80,
508 "Application was compiled with png.h from libpng-%.20s",
510 png_warning(png_ptr
, msg
);
512 png_snprintf(msg
, 80,
513 "Application is running with png.c from libpng-%.20s",
515 png_warning(png_ptr
, msg
);
517 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
521 "Incompatible libpng version in application and library");
525 /* initialize zbuf - compression buffer */
526 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
527 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
528 (png_uint_32
)png_ptr
->zbuf_size
);
530 png_set_write_fn(png_ptr
, png_voidp_NULL
, png_rw_ptr_NULL
,
533 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
534 png_set_filter_heuristics(png_ptr
, PNG_FILTER_HEURISTIC_DEFAULT
,
535 1, png_doublep_NULL
, png_doublep_NULL
);
538 #ifdef PNG_SETJMP_SUPPORTED
539 /* Applications that neglect to set up their own setjmp() and then encounter
540 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
541 abort instead of returning. */
542 #ifdef USE_FAR_KEYWORD
545 png_memcpy(png_ptr
->jmpbuf
,jmpbuf
,png_sizeof(jmp_buf));
547 if (setjmp(png_ptr
->jmpbuf
))
554 /* Initialize png_ptr structure, and allocate any memory needed */
555 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
557 #undef png_write_init
559 png_write_init(png_structp png_ptr
)
561 /* We only come here via pre-1.0.7-compiled applications */
562 png_write_init_2(png_ptr
, "1.0.6 or earlier", 0, 0);
566 png_write_init_2(png_structp png_ptr
, png_const_charp user_png_ver
,
567 png_size_t png_struct_size
, png_size_t png_info_size
)
569 /* We only come here via pre-1.0.12-compiled applications */
570 if(png_ptr
== NULL
) return;
571 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
572 if(png_sizeof(png_struct
) > png_struct_size
||
573 png_sizeof(png_info
) > png_info_size
)
576 png_ptr
->warning_fn
=NULL
;
579 png_snprintf(msg
, 80,
580 "Application was compiled with png.h from libpng-%.20s",
582 png_warning(png_ptr
, msg
);
584 png_snprintf(msg
, 80,
585 "Application is running with png.c from libpng-%.20s",
587 png_warning(png_ptr
, msg
);
590 if(png_sizeof(png_struct
) > png_struct_size
)
592 png_ptr
->error_fn
=NULL
;
593 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
597 "The png struct allocated by the application for writing is too small.");
599 if(png_sizeof(png_info
) > png_info_size
)
601 png_ptr
->error_fn
=NULL
;
602 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
606 "The info struct allocated by the application for writing is too small.");
608 png_write_init_3(&png_ptr
, user_png_ver
, png_struct_size
);
610 #endif /* PNG_1_0_X || PNG_1_2_X */
614 png_write_init_3(png_structpp ptr_ptr
, png_const_charp user_png_ver
,
615 png_size_t png_struct_size
)
617 png_structp png_ptr
=*ptr_ptr
;
618 #ifdef PNG_SETJMP_SUPPORTED
619 jmp_buf tmp_jmp
; /* to save current jump buffer */
629 if (user_png_ver
[i
] != png_libpng_ver
[i
])
631 #ifdef PNG_LEGACY_SUPPORTED
632 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
634 png_ptr
->warning_fn
=NULL
;
636 "Application uses deprecated png_write_init() and should be recompiled.");
640 } while (png_libpng_ver
[i
++]);
642 png_debug(1, "in png_write_init_3\n");
644 #ifdef PNG_SETJMP_SUPPORTED
645 /* save jump buffer and error functions */
646 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, png_sizeof (jmp_buf));
649 if (png_sizeof(png_struct
) > png_struct_size
)
651 png_destroy_struct(png_ptr
);
652 png_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
656 /* reset all variables to 0 */
657 png_memset(png_ptr
, 0, png_sizeof (png_struct
));
659 /* added at libpng-1.2.6 */
660 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
661 png_ptr
->user_width_max
=PNG_USER_WIDTH_MAX
;
662 png_ptr
->user_height_max
=PNG_USER_HEIGHT_MAX
;
665 #ifdef PNG_SETJMP_SUPPORTED
666 /* restore jump buffer */
667 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, png_sizeof (jmp_buf));
670 png_set_write_fn(png_ptr
, png_voidp_NULL
, png_rw_ptr_NULL
,
673 /* initialize zbuf - compression buffer */
674 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
675 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
676 (png_uint_32
)png_ptr
->zbuf_size
);
678 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
679 png_set_filter_heuristics(png_ptr
, PNG_FILTER_HEURISTIC_DEFAULT
,
680 1, png_doublep_NULL
, png_doublep_NULL
);
684 /* Write a few rows of image data. If the image is interlaced,
685 * either you will have to write the 7 sub images, or, if you
686 * have called png_set_interlace_handling(), you will have to
687 * "write" the image seven times.
690 png_write_rows(png_structp png_ptr
, png_bytepp row
,
691 png_uint_32 num_rows
)
693 png_uint_32 i
; /* row counter */
694 png_bytepp rp
; /* row pointer */
696 png_debug(1, "in png_write_rows\n");
701 /* loop through the rows */
702 for (i
= 0, rp
= row
; i
< num_rows
; i
++, rp
++)
704 png_write_row(png_ptr
, *rp
);
708 /* Write the image. You only need to call this function once, even
709 * if you are writing an interlaced image.
712 png_write_image(png_structp png_ptr
, png_bytepp image
)
714 png_uint_32 i
; /* row index */
715 int pass
, num_pass
; /* pass variables */
716 png_bytepp rp
; /* points to current row */
721 png_debug(1, "in png_write_image\n");
722 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
723 /* intialize interlace handling. If image is not interlaced,
724 this will set pass to 1 */
725 num_pass
= png_set_interlace_handling(png_ptr
);
729 /* loop through passes */
730 for (pass
= 0; pass
< num_pass
; pass
++)
732 /* loop through image */
733 for (i
= 0, rp
= image
; i
< png_ptr
->height
; i
++, rp
++)
735 png_write_row(png_ptr
, *rp
);
740 /* called by user to write a row of image data */
742 png_write_row(png_structp png_ptr
, png_bytep row
)
746 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
747 png_ptr
->row_number
, png_ptr
->pass
);
749 /* initialize transformations and other stuff if first time */
750 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
752 /* make sure we wrote the header info */
753 if (!(png_ptr
->mode
& PNG_WROTE_INFO_BEFORE_PLTE
))
755 "png_write_info was never called before png_write_row.");
757 /* check for transforms that have been set but were defined out */
758 #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
759 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
760 png_warning(png_ptr
, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
762 #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
763 if (png_ptr
->transformations
& PNG_FILLER
)
764 png_warning(png_ptr
, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
766 #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
767 if (png_ptr
->transformations
& PNG_PACKSWAP
)
768 png_warning(png_ptr
, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
770 #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
771 if (png_ptr
->transformations
& PNG_PACK
)
772 png_warning(png_ptr
, "PNG_WRITE_PACK_SUPPORTED is not defined.");
774 #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
775 if (png_ptr
->transformations
& PNG_SHIFT
)
776 png_warning(png_ptr
, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
778 #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
779 if (png_ptr
->transformations
& PNG_BGR
)
780 png_warning(png_ptr
, "PNG_WRITE_BGR_SUPPORTED is not defined.");
782 #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
783 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
784 png_warning(png_ptr
, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
787 png_write_start_row(png_ptr
);
790 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
791 /* if interlaced and not interested in row, return */
792 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
794 switch (png_ptr
->pass
)
797 if (png_ptr
->row_number
& 0x07)
799 png_write_finish_row(png_ptr
);
804 if ((png_ptr
->row_number
& 0x07) || png_ptr
->width
< 5)
806 png_write_finish_row(png_ptr
);
811 if ((png_ptr
->row_number
& 0x07) != 4)
813 png_write_finish_row(png_ptr
);
818 if ((png_ptr
->row_number
& 0x03) || png_ptr
->width
< 3)
820 png_write_finish_row(png_ptr
);
825 if ((png_ptr
->row_number
& 0x03) != 2)
827 png_write_finish_row(png_ptr
);
832 if ((png_ptr
->row_number
& 0x01) || png_ptr
->width
< 2)
834 png_write_finish_row(png_ptr
);
839 if (!(png_ptr
->row_number
& 0x01))
841 png_write_finish_row(png_ptr
);
849 /* set up row info for transformations */
850 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
851 png_ptr
->row_info
.width
= png_ptr
->usr_width
;
852 png_ptr
->row_info
.channels
= png_ptr
->usr_channels
;
853 png_ptr
->row_info
.bit_depth
= png_ptr
->usr_bit_depth
;
854 png_ptr
->row_info
.pixel_depth
= (png_byte
)(png_ptr
->row_info
.bit_depth
*
855 png_ptr
->row_info
.channels
);
857 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
858 png_ptr
->row_info
.width
);
860 png_debug1(3, "row_info->color_type = %d\n", png_ptr
->row_info
.color_type
);
861 png_debug1(3, "row_info->width = %lu\n", png_ptr
->row_info
.width
);
862 png_debug1(3, "row_info->channels = %d\n", png_ptr
->row_info
.channels
);
863 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr
->row_info
.bit_depth
);
864 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr
->row_info
.pixel_depth
);
865 png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr
->row_info
.rowbytes
);
867 /* Copy user's row into buffer, leaving room for filter byte. */
868 png_memcpy_check(png_ptr
, png_ptr
->row_buf
+ 1, row
,
869 png_ptr
->row_info
.rowbytes
);
871 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
872 /* handle interlacing */
873 if (png_ptr
->interlaced
&& png_ptr
->pass
< 6 &&
874 (png_ptr
->transformations
& PNG_INTERLACE
))
876 png_do_write_interlace(&(png_ptr
->row_info
),
877 png_ptr
->row_buf
+ 1, png_ptr
->pass
);
878 /* this should always get caught above, but still ... */
879 if (!(png_ptr
->row_info
.width
))
881 png_write_finish_row(png_ptr
);
887 /* handle other transformations */
888 if (png_ptr
->transformations
)
889 png_do_write_transformations(png_ptr
);
891 #if defined(PNG_MNG_FEATURES_SUPPORTED)
892 /* Write filter_method 64 (intrapixel differencing) only if
893 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
894 * 2. Libpng did not write a PNG signature (this filter_method is only
895 * used in PNG datastreams that are embedded in MNG datastreams) and
896 * 3. The application called png_permit_mng_features with a mask that
897 * included PNG_FLAG_MNG_FILTER_64 and
898 * 4. The filter_method is 64 and
899 * 5. The color_type is RGB or RGBA
901 if((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
902 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
904 /* Intrapixel differencing */
905 png_do_write_intrapixel(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
909 /* Find a filter if necessary, filter the row and write it out. */
910 png_write_find_filter(png_ptr
, &(png_ptr
->row_info
));
912 if (png_ptr
->write_row_fn
!= NULL
)
913 (*(png_ptr
->write_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
916 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
917 /* Set the automatic flush interval or 0 to turn flushing off */
919 png_set_flush(png_structp png_ptr
, int nrows
)
921 png_debug(1, "in png_set_flush\n");
924 png_ptr
->flush_dist
= (nrows
< 0 ? 0 : nrows
);
927 /* flush the current output buffers now */
929 png_write_flush(png_structp png_ptr
)
933 png_debug(1, "in png_write_flush\n");
936 /* We have already written out all of the data */
937 if (png_ptr
->row_number
>= png_ptr
->num_rows
)
944 /* compress the data */
945 ret
= deflate(&png_ptr
->zstream
, Z_SYNC_FLUSH
);
948 /* check for compression errors */
951 if (png_ptr
->zstream
.msg
!= NULL
)
952 png_error(png_ptr
, png_ptr
->zstream
.msg
);
954 png_error(png_ptr
, "zlib error");
957 if (!(png_ptr
->zstream
.avail_out
))
959 /* write the IDAT and reset the zlib output buffer */
960 png_write_IDAT(png_ptr
, png_ptr
->zbuf
,
962 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
963 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
966 } while(wrote_IDAT
== 1);
968 /* If there is any data left to be output, write it into a new IDAT */
969 if (png_ptr
->zbuf_size
!= png_ptr
->zstream
.avail_out
)
971 /* write the IDAT and reset the zlib output buffer */
972 png_write_IDAT(png_ptr
, png_ptr
->zbuf
,
973 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
974 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
975 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
977 png_ptr
->flush_rows
= 0;
980 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
982 /* free all memory used by the write */
984 png_destroy_write_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
)
986 png_structp png_ptr
= NULL
;
987 png_infop info_ptr
= NULL
;
988 #ifdef PNG_USER_MEM_SUPPORTED
989 png_free_ptr free_fn
= NULL
;
990 png_voidp mem_ptr
= NULL
;
993 png_debug(1, "in png_destroy_write_struct\n");
994 if (png_ptr_ptr
!= NULL
)
996 png_ptr
= *png_ptr_ptr
;
997 #ifdef PNG_USER_MEM_SUPPORTED
998 free_fn
= png_ptr
->free_fn
;
999 mem_ptr
= png_ptr
->mem_ptr
;
1003 if (info_ptr_ptr
!= NULL
)
1004 info_ptr
= *info_ptr_ptr
;
1006 if (info_ptr
!= NULL
)
1008 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ALL
, -1);
1010 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1011 if (png_ptr
->num_chunk_list
)
1013 png_free(png_ptr
, png_ptr
->chunk_list
);
1014 png_ptr
->chunk_list
=NULL
;
1015 png_ptr
->num_chunk_list
=0;
1019 #ifdef PNG_USER_MEM_SUPPORTED
1020 png_destroy_struct_2((png_voidp
)info_ptr
, (png_free_ptr
)free_fn
,
1021 (png_voidp
)mem_ptr
);
1023 png_destroy_struct((png_voidp
)info_ptr
);
1025 *info_ptr_ptr
= NULL
;
1028 if (png_ptr
!= NULL
)
1030 png_write_destroy(png_ptr
);
1031 #ifdef PNG_USER_MEM_SUPPORTED
1032 png_destroy_struct_2((png_voidp
)png_ptr
, (png_free_ptr
)free_fn
,
1033 (png_voidp
)mem_ptr
);
1035 png_destroy_struct((png_voidp
)png_ptr
);
1037 *png_ptr_ptr
= NULL
;
1042 /* Free any memory used in png_ptr struct (old method) */
1044 png_write_destroy(png_structp png_ptr
)
1046 #ifdef PNG_SETJMP_SUPPORTED
1047 jmp_buf tmp_jmp
; /* save jump buffer */
1049 png_error_ptr error_fn
;
1050 png_error_ptr warning_fn
;
1051 png_voidp error_ptr
;
1052 #ifdef PNG_USER_MEM_SUPPORTED
1053 png_free_ptr free_fn
;
1056 png_debug(1, "in png_write_destroy\n");
1057 /* free any memory zlib uses */
1058 deflateEnd(&png_ptr
->zstream
);
1060 /* free our memory. png_free checks NULL for us. */
1061 png_free(png_ptr
, png_ptr
->zbuf
);
1062 png_free(png_ptr
, png_ptr
->row_buf
);
1063 png_free(png_ptr
, png_ptr
->prev_row
);
1064 png_free(png_ptr
, png_ptr
->sub_row
);
1065 png_free(png_ptr
, png_ptr
->up_row
);
1066 png_free(png_ptr
, png_ptr
->avg_row
);
1067 png_free(png_ptr
, png_ptr
->paeth_row
);
1069 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1070 png_free(png_ptr
, png_ptr
->time_buffer
);
1073 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1074 png_free(png_ptr
, png_ptr
->prev_filters
);
1075 png_free(png_ptr
, png_ptr
->filter_weights
);
1076 png_free(png_ptr
, png_ptr
->inv_filter_weights
);
1077 png_free(png_ptr
, png_ptr
->filter_costs
);
1078 png_free(png_ptr
, png_ptr
->inv_filter_costs
);
1081 #ifdef PNG_SETJMP_SUPPORTED
1082 /* reset structure */
1083 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, png_sizeof (jmp_buf));
1086 error_fn
= png_ptr
->error_fn
;
1087 warning_fn
= png_ptr
->warning_fn
;
1088 error_ptr
= png_ptr
->error_ptr
;
1089 #ifdef PNG_USER_MEM_SUPPORTED
1090 free_fn
= png_ptr
->free_fn
;
1093 png_memset(png_ptr
, 0, png_sizeof (png_struct
));
1095 png_ptr
->error_fn
= error_fn
;
1096 png_ptr
->warning_fn
= warning_fn
;
1097 png_ptr
->error_ptr
= error_ptr
;
1098 #ifdef PNG_USER_MEM_SUPPORTED
1099 png_ptr
->free_fn
= free_fn
;
1102 #ifdef PNG_SETJMP_SUPPORTED
1103 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, png_sizeof (jmp_buf));
1107 /* Allow the application to select one or more row filters to use. */
1109 png_set_filter(png_structp png_ptr
, int method
, int filters
)
1111 png_debug(1, "in png_set_filter\n");
1112 if (png_ptr
== NULL
)
1114 #if defined(PNG_MNG_FEATURES_SUPPORTED)
1115 if((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
1116 (method
== PNG_INTRAPIXEL_DIFFERENCING
))
1117 method
= PNG_FILTER_TYPE_BASE
;
1119 if (method
== PNG_FILTER_TYPE_BASE
)
1121 switch (filters
& (PNG_ALL_FILTERS
| 0x07))
1123 #ifndef PNG_NO_WRITE_FILTER
1126 case 7: png_warning(png_ptr
, "Unknown row filter for method 0");
1127 #endif /* PNG_NO_WRITE_FILTER */
1128 case PNG_FILTER_VALUE_NONE
:
1129 png_ptr
->do_filter
=PNG_FILTER_NONE
; break;
1130 #ifndef PNG_NO_WRITE_FILTER
1131 case PNG_FILTER_VALUE_SUB
:
1132 png_ptr
->do_filter
=PNG_FILTER_SUB
; break;
1133 case PNG_FILTER_VALUE_UP
:
1134 png_ptr
->do_filter
=PNG_FILTER_UP
; break;
1135 case PNG_FILTER_VALUE_AVG
:
1136 png_ptr
->do_filter
=PNG_FILTER_AVG
; break;
1137 case PNG_FILTER_VALUE_PAETH
:
1138 png_ptr
->do_filter
=PNG_FILTER_PAETH
; break;
1139 default: png_ptr
->do_filter
= (png_byte
)filters
; break;
1141 default: png_warning(png_ptr
, "Unknown row filter for method 0");
1142 #endif /* PNG_NO_WRITE_FILTER */
1145 /* If we have allocated the row_buf, this means we have already started
1146 * with the image and we should have allocated all of the filter buffers
1147 * that have been selected. If prev_row isn't already allocated, then
1148 * it is too late to start using the filters that need it, since we
1149 * will be missing the data in the previous row. If an application
1150 * wants to start and stop using particular filters during compression,
1151 * it should start out with all of the filters, and then add and
1152 * remove them after the start of compression.
1154 if (png_ptr
->row_buf
!= NULL
)
1156 #ifndef PNG_NO_WRITE_FILTER
1157 if ((png_ptr
->do_filter
& PNG_FILTER_SUB
) && png_ptr
->sub_row
== NULL
)
1159 png_ptr
->sub_row
= (png_bytep
)png_malloc(png_ptr
,
1160 (png_ptr
->rowbytes
+ 1));
1161 png_ptr
->sub_row
[0] = PNG_FILTER_VALUE_SUB
;
1164 if ((png_ptr
->do_filter
& PNG_FILTER_UP
) && png_ptr
->up_row
== NULL
)
1166 if (png_ptr
->prev_row
== NULL
)
1168 png_warning(png_ptr
, "Can't add Up filter after starting");
1169 png_ptr
->do_filter
&= ~PNG_FILTER_UP
;
1173 png_ptr
->up_row
= (png_bytep
)png_malloc(png_ptr
,
1174 (png_ptr
->rowbytes
+ 1));
1175 png_ptr
->up_row
[0] = PNG_FILTER_VALUE_UP
;
1179 if ((png_ptr
->do_filter
& PNG_FILTER_AVG
) && png_ptr
->avg_row
== NULL
)
1181 if (png_ptr
->prev_row
== NULL
)
1183 png_warning(png_ptr
, "Can't add Average filter after starting");
1184 png_ptr
->do_filter
&= ~PNG_FILTER_AVG
;
1188 png_ptr
->avg_row
= (png_bytep
)png_malloc(png_ptr
,
1189 (png_ptr
->rowbytes
+ 1));
1190 png_ptr
->avg_row
[0] = PNG_FILTER_VALUE_AVG
;
1194 if ((png_ptr
->do_filter
& PNG_FILTER_PAETH
) &&
1195 png_ptr
->paeth_row
== NULL
)
1197 if (png_ptr
->prev_row
== NULL
)
1199 png_warning(png_ptr
, "Can't add Paeth filter after starting");
1200 png_ptr
->do_filter
&= (png_byte
)(~PNG_FILTER_PAETH
);
1204 png_ptr
->paeth_row
= (png_bytep
)png_malloc(png_ptr
,
1205 (png_ptr
->rowbytes
+ 1));
1206 png_ptr
->paeth_row
[0] = PNG_FILTER_VALUE_PAETH
;
1210 if (png_ptr
->do_filter
== PNG_NO_FILTERS
)
1211 #endif /* PNG_NO_WRITE_FILTER */
1212 png_ptr
->do_filter
= PNG_FILTER_NONE
;
1216 png_error(png_ptr
, "Unknown custom filter method");
1219 /* This allows us to influence the way in which libpng chooses the "best"
1220 * filter for the current scanline. While the "minimum-sum-of-absolute-
1221 * differences metric is relatively fast and effective, there is some
1222 * question as to whether it can be improved upon by trying to keep the
1223 * filtered data going to zlib more consistent, hopefully resulting in
1224 * better compression.
1226 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
1228 png_set_filter_heuristics(png_structp png_ptr
, int heuristic_method
,
1229 int num_weights
, png_doublep filter_weights
,
1230 png_doublep filter_costs
)
1234 png_debug(1, "in png_set_filter_heuristics\n");
1235 if (png_ptr
== NULL
)
1237 if (heuristic_method
>= PNG_FILTER_HEURISTIC_LAST
)
1239 png_warning(png_ptr
, "Unknown filter heuristic method");
1243 if (heuristic_method
== PNG_FILTER_HEURISTIC_DEFAULT
)
1245 heuristic_method
= PNG_FILTER_HEURISTIC_UNWEIGHTED
;
1248 if (num_weights
< 0 || filter_weights
== NULL
||
1249 heuristic_method
== PNG_FILTER_HEURISTIC_UNWEIGHTED
)
1254 png_ptr
->num_prev_filters
= (png_byte
)num_weights
;
1255 png_ptr
->heuristic_method
= (png_byte
)heuristic_method
;
1257 if (num_weights
> 0)
1259 if (png_ptr
->prev_filters
== NULL
)
1261 png_ptr
->prev_filters
= (png_bytep
)png_malloc(png_ptr
,
1262 (png_uint_32
)(png_sizeof(png_byte
) * num_weights
));
1264 /* To make sure that the weighting starts out fairly */
1265 for (i
= 0; i
< num_weights
; i
++)
1267 png_ptr
->prev_filters
[i
] = 255;
1271 if (png_ptr
->filter_weights
== NULL
)
1273 png_ptr
->filter_weights
= (png_uint_16p
)png_malloc(png_ptr
,
1274 (png_uint_32
)(png_sizeof(png_uint_16
) * num_weights
));
1276 png_ptr
->inv_filter_weights
= (png_uint_16p
)png_malloc(png_ptr
,
1277 (png_uint_32
)(png_sizeof(png_uint_16
) * num_weights
));
1278 for (i
= 0; i
< num_weights
; i
++)
1280 png_ptr
->inv_filter_weights
[i
] =
1281 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
1285 for (i
= 0; i
< num_weights
; i
++)
1287 if (filter_weights
[i
] < 0.0)
1289 png_ptr
->inv_filter_weights
[i
] =
1290 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
1294 png_ptr
->inv_filter_weights
[i
] =
1295 (png_uint_16
)((double)PNG_WEIGHT_FACTOR
*filter_weights
[i
]+0.5);
1296 png_ptr
->filter_weights
[i
] =
1297 (png_uint_16
)((double)PNG_WEIGHT_FACTOR
/filter_weights
[i
]+0.5);
1302 /* If, in the future, there are other filter methods, this would
1303 * need to be based on png_ptr->filter.
1305 if (png_ptr
->filter_costs
== NULL
)
1307 png_ptr
->filter_costs
= (png_uint_16p
)png_malloc(png_ptr
,
1308 (png_uint_32
)(png_sizeof(png_uint_16
) * PNG_FILTER_VALUE_LAST
));
1310 png_ptr
->inv_filter_costs
= (png_uint_16p
)png_malloc(png_ptr
,
1311 (png_uint_32
)(png_sizeof(png_uint_16
) * PNG_FILTER_VALUE_LAST
));
1313 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++)
1315 png_ptr
->inv_filter_costs
[i
] =
1316 png_ptr
->filter_costs
[i
] = PNG_COST_FACTOR
;
1320 /* Here is where we set the relative costs of the different filters. We
1321 * should take the desired compression level into account when setting
1322 * the costs, so that Paeth, for instance, has a high relative cost at low
1323 * compression levels, while it has a lower relative cost at higher
1324 * compression settings. The filter types are in order of increasing
1325 * relative cost, so it would be possible to do this with an algorithm.
1327 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++)
1329 if (filter_costs
== NULL
|| filter_costs
[i
] < 0.0)
1331 png_ptr
->inv_filter_costs
[i
] =
1332 png_ptr
->filter_costs
[i
] = PNG_COST_FACTOR
;
1334 else if (filter_costs
[i
] >= 1.0)
1336 png_ptr
->inv_filter_costs
[i
] =
1337 (png_uint_16
)((double)PNG_COST_FACTOR
/ filter_costs
[i
] + 0.5);
1338 png_ptr
->filter_costs
[i
] =
1339 (png_uint_16
)((double)PNG_COST_FACTOR
* filter_costs
[i
] + 0.5);
1343 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1346 png_set_compression_level(png_structp png_ptr
, int level
)
1348 png_debug(1, "in png_set_compression_level\n");
1349 if (png_ptr
== NULL
)
1351 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_LEVEL
;
1352 png_ptr
->zlib_level
= level
;
1356 png_set_compression_mem_level(png_structp png_ptr
, int mem_level
)
1358 png_debug(1, "in png_set_compression_mem_level\n");
1359 if (png_ptr
== NULL
)
1361 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL
;
1362 png_ptr
->zlib_mem_level
= mem_level
;
1366 png_set_compression_strategy(png_structp png_ptr
, int strategy
)
1368 png_debug(1, "in png_set_compression_strategy\n");
1369 if (png_ptr
== NULL
)
1371 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_STRATEGY
;
1372 png_ptr
->zlib_strategy
= strategy
;
1376 png_set_compression_window_bits(png_structp png_ptr
, int window_bits
)
1378 if (png_ptr
== NULL
)
1380 if (window_bits
> 15)
1381 png_warning(png_ptr
, "Only compression windows <= 32k supported by PNG");
1382 else if (window_bits
< 8)
1383 png_warning(png_ptr
, "Only compression windows >= 256 supported by PNG");
1385 /* avoid libpng bug with 256-byte windows */
1386 if (window_bits
== 8)
1388 png_warning(png_ptr
, "Compression window is being reset to 512");
1392 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS
;
1393 png_ptr
->zlib_window_bits
= window_bits
;
1397 png_set_compression_method(png_structp png_ptr
, int method
)
1399 png_debug(1, "in png_set_compression_method\n");
1400 if (png_ptr
== NULL
)
1403 png_warning(png_ptr
, "Only compression method 8 is supported by PNG");
1404 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_METHOD
;
1405 png_ptr
->zlib_method
= method
;
1409 png_set_write_status_fn(png_structp png_ptr
, png_write_status_ptr write_row_fn
)
1411 if (png_ptr
== NULL
)
1413 png_ptr
->write_row_fn
= write_row_fn
;
1416 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1418 png_set_write_user_transform_fn(png_structp png_ptr
, png_user_transform_ptr
1419 write_user_transform_fn
)
1421 png_debug(1, "in png_set_write_user_transform_fn\n");
1422 if (png_ptr
== NULL
)
1424 png_ptr
->transformations
|= PNG_USER_TRANSFORM
;
1425 png_ptr
->write_user_transform_fn
= write_user_transform_fn
;
1430 #if defined(PNG_INFO_IMAGE_SUPPORTED)
1432 png_write_png(png_structp png_ptr
, png_infop info_ptr
,
1433 int transforms
, voidp params
)
1435 if (png_ptr
== NULL
|| info_ptr
== NULL
)
1437 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1438 /* invert the alpha channel from opacity to transparency */
1439 if (transforms
& PNG_TRANSFORM_INVERT_ALPHA
)
1440 png_set_invert_alpha(png_ptr
);
1443 /* Write the file header information. */
1444 png_write_info(png_ptr
, info_ptr
);
1446 /* ------ these transformations don't touch the info structure ------- */
1448 #if defined(PNG_WRITE_INVERT_SUPPORTED)
1449 /* invert monochrome pixels */
1450 if (transforms
& PNG_TRANSFORM_INVERT_MONO
)
1451 png_set_invert_mono(png_ptr
);
1454 #if defined(PNG_WRITE_SHIFT_SUPPORTED)
1455 /* Shift the pixels up to a legal bit depth and fill in
1456 * as appropriate to correctly scale the image.
1458 if ((transforms
& PNG_TRANSFORM_SHIFT
)
1459 && (info_ptr
->valid
& PNG_INFO_sBIT
))
1460 png_set_shift(png_ptr
, &info_ptr
->sig_bit
);
1463 #if defined(PNG_WRITE_PACK_SUPPORTED)
1464 /* pack pixels into bytes */
1465 if (transforms
& PNG_TRANSFORM_PACKING
)
1466 png_set_packing(png_ptr
);
1469 #if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
1470 /* swap location of alpha bytes from ARGB to RGBA */
1471 if (transforms
& PNG_TRANSFORM_SWAP_ALPHA
)
1472 png_set_swap_alpha(png_ptr
);
1475 #if defined(PNG_WRITE_FILLER_SUPPORTED)
1476 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
1477 * RGB (4 channels -> 3 channels). The second parameter is not used.
1479 if (transforms
& PNG_TRANSFORM_STRIP_FILLER
)
1480 png_set_filler(png_ptr
, 0, PNG_FILLER_BEFORE
);
1483 #if defined(PNG_WRITE_BGR_SUPPORTED)
1484 /* flip BGR pixels to RGB */
1485 if (transforms
& PNG_TRANSFORM_BGR
)
1486 png_set_bgr(png_ptr
);
1489 #if defined(PNG_WRITE_SWAP_SUPPORTED)
1490 /* swap bytes of 16-bit files to most significant byte first */
1491 if (transforms
& PNG_TRANSFORM_SWAP_ENDIAN
)
1492 png_set_swap(png_ptr
);
1495 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
1496 /* swap bits of 1, 2, 4 bit packed pixel formats */
1497 if (transforms
& PNG_TRANSFORM_PACKSWAP
)
1498 png_set_packswap(png_ptr
);
1501 /* ----------------------- end of transformations ------------------- */
1503 /* write the bits */
1504 if (info_ptr
->valid
& PNG_INFO_IDAT
)
1505 png_write_image(png_ptr
, info_ptr
->row_pointers
);
1507 /* It is REQUIRED to call this to finish writing the rest of the file */
1508 png_write_end(png_ptr
, info_ptr
);
1510 transforms
= transforms
; /* quiet compiler warnings */
1514 #endif /* PNG_WRITE_SUPPORTED */