2 /* pngwrite.c - general routines to write a PNG file
4 * Last changed in libpng 1.5.1 [February 3, 2011]
5 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
16 #ifdef PNG_WRITE_SUPPORTED
18 /* Writes all the PNG information. This is the suggested way to use the
19 * library. If you have a new chunk to add, make a function to write it,
20 * and put it in the correct location here. If you want the chunk written
21 * after the image data, put it in png_write_end(). I strongly encourage
22 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
23 * the chunk, as that will keep the code from breaking if you want to just
24 * write a plain PNG file. If you have long comments, I suggest writing
25 * them in png_write_end(), and compressing them.
28 png_write_info_before_PLTE(png_structp png_ptr
, png_infop info_ptr
)
30 png_debug(1, "in png_write_info_before_PLTE");
32 if (png_ptr
== NULL
|| info_ptr
== NULL
)
35 if (!(png_ptr
->mode
& PNG_WROTE_INFO_BEFORE_PLTE
))
37 /* Write PNG signature */
38 png_write_sig(png_ptr
);
40 #ifdef PNG_MNG_FEATURES_SUPPORTED
41 if ((png_ptr
->mode
&PNG_HAVE_PNG_SIGNATURE
) && \
42 (png_ptr
->mng_features_permitted
))
44 png_warning(png_ptr
, "MNG features are not allowed in a PNG datastream");
45 png_ptr
->mng_features_permitted
= 0;
49 /* Write IHDR information. */
50 png_write_IHDR(png_ptr
, info_ptr
->width
, info_ptr
->height
,
51 info_ptr
->bit_depth
, info_ptr
->color_type
, info_ptr
->compression_type
,
52 info_ptr
->filter_type
,
53 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
54 info_ptr
->interlace_type
);
58 /* The rest of these check to see if the valid field has the appropriate
59 * flag set, and if it does, writes the chunk.
61 #ifdef PNG_WRITE_gAMA_SUPPORTED
62 if (info_ptr
->valid
& PNG_INFO_gAMA
)
63 png_write_gAMA_fixed(png_ptr
, info_ptr
->gamma
);
65 #ifdef PNG_WRITE_sRGB_SUPPORTED
66 if (info_ptr
->valid
& PNG_INFO_sRGB
)
67 png_write_sRGB(png_ptr
, (int)info_ptr
->srgb_intent
);
70 #ifdef PNG_WRITE_iCCP_SUPPORTED
71 if (info_ptr
->valid
& PNG_INFO_iCCP
)
72 png_write_iCCP(png_ptr
, info_ptr
->iccp_name
, PNG_COMPRESSION_TYPE_BASE
,
73 (png_charp
)info_ptr
->iccp_profile
, (int)info_ptr
->iccp_proflen
);
75 #ifdef PNG_WRITE_sBIT_SUPPORTED
76 if (info_ptr
->valid
& PNG_INFO_sBIT
)
77 png_write_sBIT(png_ptr
, &(info_ptr
->sig_bit
), info_ptr
->color_type
);
79 #ifdef PNG_WRITE_cHRM_SUPPORTED
80 if (info_ptr
->valid
& PNG_INFO_cHRM
)
81 png_write_cHRM_fixed(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
);
88 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
89 if (info_ptr
->unknown_chunks_num
)
91 png_unknown_chunk
*up
;
93 png_debug(5, "writing extra chunks");
95 for (up
= info_ptr
->unknown_chunks
;
96 up
< info_ptr
->unknown_chunks
+ info_ptr
->unknown_chunks_num
;
99 int keep
= png_handle_as_unknown(png_ptr
, up
->name
);
101 if (keep
!= PNG_HANDLE_CHUNK_NEVER
&&
102 up
->location
&& !(up
->location
& PNG_HAVE_PLTE
) &&
103 !(up
->location
& PNG_HAVE_IDAT
) &&
104 ((up
->name
[3] & 0x20) || keep
== PNG_HANDLE_CHUNK_ALWAYS
||
105 (png_ptr
->flags
& PNG_FLAG_KEEP_UNSAFE_CHUNKS
)))
108 png_warning(png_ptr
, "Writing zero-length unknown chunk");
110 png_write_chunk(png_ptr
, up
->name
, up
->data
, up
->size
);
115 png_ptr
->mode
|= PNG_WROTE_INFO_BEFORE_PLTE
;
120 png_write_info(png_structp png_ptr
, png_infop info_ptr
)
122 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
126 png_debug(1, "in png_write_info");
128 if (png_ptr
== NULL
|| info_ptr
== NULL
)
131 png_write_info_before_PLTE(png_ptr
, info_ptr
);
133 if (info_ptr
->valid
& PNG_INFO_PLTE
)
134 png_write_PLTE(png_ptr
, info_ptr
->palette
,
135 (png_uint_32
)info_ptr
->num_palette
);
137 else if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
138 png_error(png_ptr
, "Valid palette required for paletted images");
140 #ifdef PNG_WRITE_tRNS_SUPPORTED
141 if (info_ptr
->valid
& PNG_INFO_tRNS
)
143 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
144 /* Invert the alpha channel (in tRNS) */
145 if ((png_ptr
->transformations
& PNG_INVERT_ALPHA
) &&
146 info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
149 for (j
= 0; j
<(int)info_ptr
->num_trans
; j
++)
150 info_ptr
->trans_alpha
[j
] =
151 (png_byte
)(255 - info_ptr
->trans_alpha
[j
]);
154 png_write_tRNS(png_ptr
, info_ptr
->trans_alpha
, &(info_ptr
->trans_color
),
155 info_ptr
->num_trans
, info_ptr
->color_type
);
158 #ifdef PNG_WRITE_bKGD_SUPPORTED
159 if (info_ptr
->valid
& PNG_INFO_bKGD
)
160 png_write_bKGD(png_ptr
, &(info_ptr
->background
), info_ptr
->color_type
);
163 #ifdef PNG_WRITE_hIST_SUPPORTED
164 if (info_ptr
->valid
& PNG_INFO_hIST
)
165 png_write_hIST(png_ptr
, info_ptr
->hist
, info_ptr
->num_palette
);
168 #ifdef PNG_WRITE_oFFs_SUPPORTED
169 if (info_ptr
->valid
& PNG_INFO_oFFs
)
170 png_write_oFFs(png_ptr
, info_ptr
->x_offset
, info_ptr
->y_offset
,
171 info_ptr
->offset_unit_type
);
174 #ifdef 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
);
181 #ifdef PNG_WRITE_sCAL_SUPPORTED
182 if (info_ptr
->valid
& PNG_INFO_sCAL
)
183 png_write_sCAL_s(png_ptr
, (int)info_ptr
->scal_unit
,
184 info_ptr
->scal_s_width
, info_ptr
->scal_s_height
);
187 #ifdef PNG_WRITE_pHYs_SUPPORTED
188 if (info_ptr
->valid
& PNG_INFO_pHYs
)
189 png_write_pHYs(png_ptr
, info_ptr
->x_pixels_per_unit
,
190 info_ptr
->y_pixels_per_unit
, info_ptr
->phys_unit_type
);
193 #ifdef PNG_WRITE_tIME_SUPPORTED
194 if (info_ptr
->valid
& PNG_INFO_tIME
)
196 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
197 png_ptr
->mode
|= PNG_WROTE_tIME
;
201 #ifdef PNG_WRITE_sPLT_SUPPORTED
202 if (info_ptr
->valid
& PNG_INFO_sPLT
)
203 for (i
= 0; i
< (int)info_ptr
->splt_palettes_num
; i
++)
204 png_write_sPLT(png_ptr
, info_ptr
->splt_palettes
+ i
);
207 #ifdef PNG_WRITE_TEXT_SUPPORTED
208 /* Check to see if we need to write text chunks */
209 for (i
= 0; i
< info_ptr
->num_text
; i
++)
211 png_debug2(2, "Writing header text chunk %d, type %d", i
,
212 info_ptr
->text
[i
].compression
);
213 /* An internationalized chunk? */
214 if (info_ptr
->text
[i
].compression
> 0)
216 #ifdef PNG_WRITE_iTXt_SUPPORTED
217 /* Write international chunk */
218 png_write_iTXt(png_ptr
,
219 info_ptr
->text
[i
].compression
,
220 info_ptr
->text
[i
].key
,
221 info_ptr
->text
[i
].lang
,
222 info_ptr
->text
[i
].lang_key
,
223 info_ptr
->text
[i
].text
);
225 png_warning(png_ptr
, "Unable to write international text");
227 /* Mark this chunk as written */
228 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
231 /* If we want a compressed text chunk */
232 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_zTXt
)
234 #ifdef PNG_WRITE_zTXt_SUPPORTED
235 /* Write compressed chunk */
236 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
237 info_ptr
->text
[i
].text
, 0,
238 info_ptr
->text
[i
].compression
);
240 png_warning(png_ptr
, "Unable to write compressed text");
242 /* Mark this chunk as written */
243 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
246 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
248 #ifdef PNG_WRITE_tEXt_SUPPORTED
249 /* Write uncompressed chunk */
250 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
251 info_ptr
->text
[i
].text
,
253 /* Mark this chunk as written */
254 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
257 png_warning(png_ptr
, "Unable to write uncompressed text");
263 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
264 if (info_ptr
->unknown_chunks_num
)
266 png_unknown_chunk
*up
;
268 png_debug(5, "writing extra chunks");
270 for (up
= info_ptr
->unknown_chunks
;
271 up
< info_ptr
->unknown_chunks
+ info_ptr
->unknown_chunks_num
;
274 int keep
= png_handle_as_unknown(png_ptr
, up
->name
);
275 if (keep
!= PNG_HANDLE_CHUNK_NEVER
&&
276 up
->location
&& (up
->location
& PNG_HAVE_PLTE
) &&
277 !(up
->location
& PNG_HAVE_IDAT
) &&
278 ((up
->name
[3] & 0x20) || keep
== PNG_HANDLE_CHUNK_ALWAYS
||
279 (png_ptr
->flags
& PNG_FLAG_KEEP_UNSAFE_CHUNKS
)))
281 png_write_chunk(png_ptr
, up
->name
, up
->data
, up
->size
);
288 /* Writes the end of the PNG file. If you don't want to write comments or
289 * time information, you can pass NULL for info. If you already wrote these
290 * in png_write_info(), do not write them again here. If you have long
291 * comments, I suggest writing them here, and compressing them.
294 png_write_end(png_structp png_ptr
, png_infop info_ptr
)
296 png_debug(1, "in png_write_end");
301 if (!(png_ptr
->mode
& PNG_HAVE_IDAT
))
302 png_error(png_ptr
, "No IDATs written into file");
304 /* See if user wants us to write information chunks */
305 if (info_ptr
!= NULL
)
307 #ifdef PNG_WRITE_TEXT_SUPPORTED
308 int i
; /* local index variable */
310 #ifdef PNG_WRITE_tIME_SUPPORTED
311 /* Check to see if user has supplied a time chunk */
312 if ((info_ptr
->valid
& PNG_INFO_tIME
) &&
313 !(png_ptr
->mode
& PNG_WROTE_tIME
))
314 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
317 #ifdef PNG_WRITE_TEXT_SUPPORTED
318 /* Loop through comment chunks */
319 for (i
= 0; i
< info_ptr
->num_text
; i
++)
321 png_debug2(2, "Writing trailer text chunk %d, type %d", i
,
322 info_ptr
->text
[i
].compression
);
323 /* An internationalized chunk? */
324 if (info_ptr
->text
[i
].compression
> 0)
326 #ifdef PNG_WRITE_iTXt_SUPPORTED
327 /* Write international chunk */
328 png_write_iTXt(png_ptr
,
329 info_ptr
->text
[i
].compression
,
330 info_ptr
->text
[i
].key
,
331 info_ptr
->text
[i
].lang
,
332 info_ptr
->text
[i
].lang_key
,
333 info_ptr
->text
[i
].text
);
335 png_warning(png_ptr
, "Unable to write international text");
337 /* Mark this chunk as written */
338 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
341 else if (info_ptr
->text
[i
].compression
>= PNG_TEXT_COMPRESSION_zTXt
)
343 #ifdef PNG_WRITE_zTXt_SUPPORTED
344 /* Write compressed chunk */
345 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
346 info_ptr
->text
[i
].text
, 0,
347 info_ptr
->text
[i
].compression
);
349 png_warning(png_ptr
, "Unable to write compressed text");
351 /* Mark this chunk as written */
352 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
355 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
357 #ifdef PNG_WRITE_tEXt_SUPPORTED
358 /* Write uncompressed chunk */
359 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
360 info_ptr
->text
[i
].text
, 0);
362 png_warning(png_ptr
, "Unable to write uncompressed text");
365 /* Mark this chunk as written */
366 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
370 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
371 if (info_ptr
->unknown_chunks_num
)
373 png_unknown_chunk
*up
;
375 png_debug(5, "writing extra chunks");
377 for (up
= info_ptr
->unknown_chunks
;
378 up
< info_ptr
->unknown_chunks
+ info_ptr
->unknown_chunks_num
;
381 int keep
= png_handle_as_unknown(png_ptr
, up
->name
);
382 if (keep
!= PNG_HANDLE_CHUNK_NEVER
&&
383 up
->location
&& (up
->location
& PNG_AFTER_IDAT
) &&
384 ((up
->name
[3] & 0x20) || keep
== PNG_HANDLE_CHUNK_ALWAYS
||
385 (png_ptr
->flags
& PNG_FLAG_KEEP_UNSAFE_CHUNKS
)))
387 png_write_chunk(png_ptr
, up
->name
, up
->data
, up
->size
);
394 png_ptr
->mode
|= PNG_AFTER_IDAT
;
396 /* Write end of PNG file */
397 png_write_IEND(png_ptr
);
398 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
399 * and restored again in libpng-1.2.30, may cause some applications that
400 * do not set png_ptr->output_flush_fn to crash. If your application
401 * experiences a problem, please try building libpng with
402 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
403 * png-mng-implement at lists.sf.net .
405 #ifdef PNG_WRITE_FLUSH_SUPPORTED
406 # ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
412 #ifdef PNG_CONVERT_tIME_SUPPORTED
413 /* "tm" structure is not supported on WindowsCE */
415 png_convert_from_struct_tm(png_timep ptime
, PNG_CONST
struct tm FAR
* ttime
)
417 png_debug(1, "in png_convert_from_struct_tm");
419 ptime
->year
= (png_uint_16
)(1900 + ttime
->tm_year
);
420 ptime
->month
= (png_byte
)(ttime
->tm_mon
+ 1);
421 ptime
->day
= (png_byte
)ttime
->tm_mday
;
422 ptime
->hour
= (png_byte
)ttime
->tm_hour
;
423 ptime
->minute
= (png_byte
)ttime
->tm_min
;
424 ptime
->second
= (png_byte
)ttime
->tm_sec
;
428 png_convert_from_time_t(png_timep ptime
, time_t ttime
)
432 png_debug(1, "in png_convert_from_time_t");
434 tbuf
= gmtime(&ttime
);
435 png_convert_from_struct_tm(ptime
, tbuf
);
439 /* Initialize png_ptr structure, and allocate any memory needed */
440 PNG_FUNCTION(png_structp
,PNGAPI
441 png_create_write_struct
,(png_const_charp user_png_ver
, png_voidp error_ptr
,
442 png_error_ptr error_fn
, png_error_ptr warn_fn
),PNG_ALLOCATED
)
444 #ifdef PNG_USER_MEM_SUPPORTED
445 return (png_create_write_struct_2(user_png_ver
, error_ptr
, error_fn
,
446 warn_fn
, NULL
, NULL
, NULL
));
449 /* Alternate initialize png_ptr structure, and allocate any memory needed */
450 static void png_reset_filter_heuristics(png_structp png_ptr
); /* forward decl */
452 PNG_FUNCTION(png_structp
,PNGAPI
453 png_create_write_struct_2
,(png_const_charp user_png_ver
, png_voidp error_ptr
,
454 png_error_ptr error_fn
, png_error_ptr warn_fn
, png_voidp mem_ptr
,
455 png_malloc_ptr malloc_fn
, png_free_ptr free_fn
),PNG_ALLOCATED
)
457 #endif /* PNG_USER_MEM_SUPPORTED */
458 volatile int png_cleanup_needed
= 0;
459 #ifdef PNG_SETJMP_SUPPORTED
463 #ifdef PNG_SETJMP_SUPPORTED
464 #ifdef USE_FAR_KEYWORD
470 png_debug(1, "in png_create_write_struct");
472 #ifdef PNG_USER_MEM_SUPPORTED
473 png_ptr
= (png_structp
)png_create_struct_2(PNG_STRUCT_PNG
,
474 (png_malloc_ptr
)malloc_fn
, (png_voidp
)mem_ptr
);
476 png_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
477 #endif /* PNG_USER_MEM_SUPPORTED */
481 /* Added at libpng-1.2.6 */
482 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
483 png_ptr
->user_width_max
= PNG_USER_WIDTH_MAX
;
484 png_ptr
->user_height_max
= PNG_USER_HEIGHT_MAX
;
487 #ifdef PNG_SETJMP_SUPPORTED
488 /* Applications that neglect to set up their own setjmp() and then
489 encounter a png_error() will longjmp here. Since the jmpbuf is
490 then meaningless we abort instead of returning. */
491 #ifdef USE_FAR_KEYWORD
492 if (setjmp(png_jmpbuf
))
494 if (setjmp(png_jmpbuf(png_ptr
))) /* sets longjmp to match setjmp */
496 #ifdef USE_FAR_KEYWORD
497 png_memcpy(png_jmpbuf(png_ptr
), png_jmpbuf
, png_sizeof(jmp_buf));
502 #ifdef PNG_USER_MEM_SUPPORTED
503 png_set_mem_fn(png_ptr
, mem_ptr
, malloc_fn
, free_fn
);
504 #endif /* PNG_USER_MEM_SUPPORTED */
505 png_set_error_fn(png_ptr
, error_ptr
, error_fn
, warn_fn
);
512 if (user_png_ver
[i
] != png_libpng_ver
[i
])
513 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
514 } while (png_libpng_ver
[i
++]);
517 if (png_ptr
->flags
& PNG_FLAG_LIBRARY_MISMATCH
)
519 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
520 * we must recompile any applications that use any older library version.
521 * For versions after libpng 1.0, we will be compatible, so we need
522 * only check the first digit.
524 if (user_png_ver
== NULL
|| user_png_ver
[0] != png_libpng_ver
[0] ||
525 (user_png_ver
[0] == '1' && user_png_ver
[2] != png_libpng_ver
[2]) ||
526 (user_png_ver
[0] == '0' && user_png_ver
[2] < '9'))
528 #ifdef PNG_CONSOLE_IO_SUPPORTED
533 png_snprintf2(msg
, 80,
534 "Application built with libpng-%.20s"
535 " but running with %.20s",
538 png_warning(png_ptr
, msg
);
542 "Incompatible libpng version in application and library");
544 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
547 png_cleanup_needed
= 1;
551 /* Initialize zbuf - compression buffer */
552 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
554 if (!png_cleanup_needed
)
556 png_ptr
->zbuf
= (png_bytep
)png_malloc_warn(png_ptr
,
558 if (png_ptr
->zbuf
== NULL
)
559 png_cleanup_needed
= 1;
562 if (png_cleanup_needed
)
564 /* Clean up PNG structure and deallocate any memory. */
565 png_free(png_ptr
, png_ptr
->zbuf
);
566 png_ptr
->zbuf
= NULL
;
567 #ifdef PNG_USER_MEM_SUPPORTED
568 png_destroy_struct_2((png_voidp
)png_ptr
,
569 (png_free_ptr
)free_fn
, (png_voidp
)mem_ptr
);
571 png_destroy_struct((png_voidp
)png_ptr
);
576 png_set_write_fn(png_ptr
, NULL
, NULL
, NULL
);
578 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
579 png_reset_filter_heuristics(png_ptr
);
586 /* Write a few rows of image data. If the image is interlaced,
587 * either you will have to write the 7 sub images, or, if you
588 * have called png_set_interlace_handling(), you will have to
589 * "write" the image seven times.
592 png_write_rows(png_structp png_ptr
, png_bytepp row
,
593 png_uint_32 num_rows
)
595 png_uint_32 i
; /* row counter */
596 png_bytepp rp
; /* row pointer */
598 png_debug(1, "in png_write_rows");
603 /* Loop through the rows */
604 for (i
= 0, rp
= row
; i
< num_rows
; i
++, rp
++)
606 png_write_row(png_ptr
, *rp
);
610 /* Write the image. You only need to call this function once, even
611 * if you are writing an interlaced image.
614 png_write_image(png_structp png_ptr
, png_bytepp image
)
616 png_uint_32 i
; /* row index */
617 int pass
, num_pass
; /* pass variables */
618 png_bytepp rp
; /* points to current row */
623 png_debug(1, "in png_write_image");
625 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
626 /* Initialize interlace handling. If image is not interlaced,
627 * this will set pass to 1
629 num_pass
= png_set_interlace_handling(png_ptr
);
633 /* Loop through passes */
634 for (pass
= 0; pass
< num_pass
; pass
++)
636 /* Loop through image */
637 for (i
= 0, rp
= image
; i
< png_ptr
->height
; i
++, rp
++)
639 png_write_row(png_ptr
, *rp
);
644 /* Called by user to write a row of image data */
646 png_write_row(png_structp png_ptr
, png_const_bytep row
)
651 png_debug2(1, "in png_write_row (row %u, pass %d)",
652 png_ptr
->row_number
, png_ptr
->pass
);
654 /* Initialize transformations and other stuff if first time */
655 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
657 /* Make sure we wrote the header info */
658 if (!(png_ptr
->mode
& PNG_WROTE_INFO_BEFORE_PLTE
))
660 "png_write_info was never called before png_write_row");
662 /* Check for transforms that have been set but were defined out */
663 #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
664 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
665 png_warning(png_ptr
, "PNG_WRITE_INVERT_SUPPORTED is not defined");
668 #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
669 if (png_ptr
->transformations
& PNG_FILLER
)
670 png_warning(png_ptr
, "PNG_WRITE_FILLER_SUPPORTED is not defined");
672 #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
673 defined(PNG_READ_PACKSWAP_SUPPORTED)
674 if (png_ptr
->transformations
& PNG_PACKSWAP
)
676 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
679 #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
680 if (png_ptr
->transformations
& PNG_PACK
)
681 png_warning(png_ptr
, "PNG_WRITE_PACK_SUPPORTED is not defined");
684 #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
685 if (png_ptr
->transformations
& PNG_SHIFT
)
686 png_warning(png_ptr
, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
689 #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
690 if (png_ptr
->transformations
& PNG_BGR
)
691 png_warning(png_ptr
, "PNG_WRITE_BGR_SUPPORTED is not defined");
694 #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
695 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
696 png_warning(png_ptr
, "PNG_WRITE_SWAP_SUPPORTED is not defined");
699 png_write_start_row(png_ptr
);
702 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
703 /* If interlaced and not interested in row, return */
704 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
706 switch (png_ptr
->pass
)
709 if (png_ptr
->row_number
& 0x07)
711 png_write_finish_row(png_ptr
);
717 if ((png_ptr
->row_number
& 0x07) || png_ptr
->width
< 5)
719 png_write_finish_row(png_ptr
);
725 if ((png_ptr
->row_number
& 0x07) != 4)
727 png_write_finish_row(png_ptr
);
733 if ((png_ptr
->row_number
& 0x03) || png_ptr
->width
< 3)
735 png_write_finish_row(png_ptr
);
741 if ((png_ptr
->row_number
& 0x03) != 2)
743 png_write_finish_row(png_ptr
);
749 if ((png_ptr
->row_number
& 0x01) || png_ptr
->width
< 2)
751 png_write_finish_row(png_ptr
);
757 if (!(png_ptr
->row_number
& 0x01))
759 png_write_finish_row(png_ptr
);
764 default: /* error: ignore it */
770 /* Set up row info for transformations */
771 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
772 png_ptr
->row_info
.width
= png_ptr
->usr_width
;
773 png_ptr
->row_info
.channels
= png_ptr
->usr_channels
;
774 png_ptr
->row_info
.bit_depth
= png_ptr
->usr_bit_depth
;
775 png_ptr
->row_info
.pixel_depth
= (png_byte
)(png_ptr
->row_info
.bit_depth
*
776 png_ptr
->row_info
.channels
);
778 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
779 png_ptr
->row_info
.width
);
781 png_debug1(3, "row_info->color_type = %d", png_ptr
->row_info
.color_type
);
782 png_debug1(3, "row_info->width = %u", png_ptr
->row_info
.width
);
783 png_debug1(3, "row_info->channels = %d", png_ptr
->row_info
.channels
);
784 png_debug1(3, "row_info->bit_depth = %d", png_ptr
->row_info
.bit_depth
);
785 png_debug1(3, "row_info->pixel_depth = %d", png_ptr
->row_info
.pixel_depth
);
786 png_debug1(3, "row_info->rowbytes = %lu",
787 (unsigned long)png_ptr
->row_info
.rowbytes
);
789 /* Copy user's row into buffer, leaving room for filter byte. */
790 png_memcpy(png_ptr
->row_buf
+ 1, row
, png_ptr
->row_info
.rowbytes
);
792 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
793 /* Handle interlacing */
794 if (png_ptr
->interlaced
&& png_ptr
->pass
< 6 &&
795 (png_ptr
->transformations
& PNG_INTERLACE
))
797 png_do_write_interlace(&(png_ptr
->row_info
),
798 png_ptr
->row_buf
+ 1, png_ptr
->pass
);
799 /* This should always get caught above, but still ... */
800 if (!(png_ptr
->row_info
.width
))
802 png_write_finish_row(png_ptr
);
808 /* Handle other transformations */
809 if (png_ptr
->transformations
)
810 png_do_write_transformations(png_ptr
);
812 #ifdef PNG_MNG_FEATURES_SUPPORTED
813 /* Write filter_method 64 (intrapixel differencing) only if
814 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
815 * 2. Libpng did not write a PNG signature (this filter_method is only
816 * used in PNG datastreams that are embedded in MNG datastreams) and
817 * 3. The application called png_permit_mng_features with a mask that
818 * included PNG_FLAG_MNG_FILTER_64 and
819 * 4. The filter_method is 64 and
820 * 5. The color_type is RGB or RGBA
822 if ((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
823 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
825 /* Intrapixel differencing */
826 png_do_write_intrapixel(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
830 /* Find a filter if necessary, filter the row and write it out. */
831 png_write_find_filter(png_ptr
, &(png_ptr
->row_info
));
833 if (png_ptr
->write_row_fn
!= NULL
)
834 (*(png_ptr
->write_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
837 #ifdef PNG_WRITE_FLUSH_SUPPORTED
838 /* Set the automatic flush interval or 0 to turn flushing off */
840 png_set_flush(png_structp png_ptr
, int nrows
)
842 png_debug(1, "in png_set_flush");
847 png_ptr
->flush_dist
= (nrows
< 0 ? 0 : nrows
);
850 /* Flush the current output buffers now */
852 png_write_flush(png_structp png_ptr
)
856 png_debug(1, "in png_write_flush");
861 /* We have already written out all of the data */
862 if (png_ptr
->row_number
>= png_ptr
->num_rows
)
869 /* Compress the data */
870 ret
= deflate(&png_ptr
->zstream
, Z_SYNC_FLUSH
);
873 /* Check for compression errors */
876 if (png_ptr
->zstream
.msg
!= NULL
)
877 png_error(png_ptr
, png_ptr
->zstream
.msg
);
880 png_error(png_ptr
, "zlib error");
883 if (!(png_ptr
->zstream
.avail_out
))
885 /* Write the IDAT and reset the zlib output buffer */
886 png_write_IDAT(png_ptr
, png_ptr
->zbuf
, png_ptr
->zbuf_size
);
887 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
888 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
891 } while (wrote_IDAT
== 1);
893 /* If there is any data left to be output, write it into a new IDAT */
894 if (png_ptr
->zbuf_size
!= png_ptr
->zstream
.avail_out
)
896 /* Write the IDAT and reset the zlib output buffer */
897 png_write_IDAT(png_ptr
, png_ptr
->zbuf
,
898 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
899 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
900 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
902 png_ptr
->flush_rows
= 0;
905 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
907 /* Free all memory used by the write */
909 png_destroy_write_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
)
911 png_structp png_ptr
= NULL
;
912 png_infop info_ptr
= NULL
;
913 #ifdef PNG_USER_MEM_SUPPORTED
914 png_free_ptr free_fn
= NULL
;
915 png_voidp mem_ptr
= NULL
;
918 png_debug(1, "in png_destroy_write_struct");
920 if (png_ptr_ptr
!= NULL
)
922 png_ptr
= *png_ptr_ptr
;
923 #ifdef PNG_USER_MEM_SUPPORTED
924 free_fn
= png_ptr
->free_fn
;
925 mem_ptr
= png_ptr
->mem_ptr
;
929 #ifdef PNG_USER_MEM_SUPPORTED
932 free_fn
= png_ptr
->free_fn
;
933 mem_ptr
= png_ptr
->mem_ptr
;
937 if (info_ptr_ptr
!= NULL
)
938 info_ptr
= *info_ptr_ptr
;
940 if (info_ptr
!= NULL
)
944 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ALL
, -1);
946 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
947 if (png_ptr
->num_chunk_list
)
949 png_free(png_ptr
, png_ptr
->chunk_list
);
950 png_ptr
->num_chunk_list
= 0;
955 #ifdef PNG_USER_MEM_SUPPORTED
956 png_destroy_struct_2((png_voidp
)info_ptr
, (png_free_ptr
)free_fn
,
959 png_destroy_struct((png_voidp
)info_ptr
);
961 *info_ptr_ptr
= NULL
;
966 png_write_destroy(png_ptr
);
967 #ifdef PNG_USER_MEM_SUPPORTED
968 png_destroy_struct_2((png_voidp
)png_ptr
, (png_free_ptr
)free_fn
,
971 png_destroy_struct((png_voidp
)png_ptr
);
978 /* Free any memory used in png_ptr struct (old method) */
980 png_write_destroy(png_structp png_ptr
)
982 #ifdef PNG_SETJMP_SUPPORTED
983 jmp_buf tmp_jmp
; /* Save jump buffer */
985 png_error_ptr error_fn
;
986 png_error_ptr warning_fn
;
988 #ifdef PNG_USER_MEM_SUPPORTED
989 png_free_ptr free_fn
;
992 png_debug(1, "in png_write_destroy");
994 /* Free any memory zlib uses */
995 deflateEnd(&png_ptr
->zstream
);
997 /* Free our memory. png_free checks NULL for us. */
998 png_free(png_ptr
, png_ptr
->zbuf
);
999 png_free(png_ptr
, png_ptr
->row_buf
);
1000 #ifdef PNG_WRITE_FILTER_SUPPORTED
1001 png_free(png_ptr
, png_ptr
->prev_row
);
1002 png_free(png_ptr
, png_ptr
->sub_row
);
1003 png_free(png_ptr
, png_ptr
->up_row
);
1004 png_free(png_ptr
, png_ptr
->avg_row
);
1005 png_free(png_ptr
, png_ptr
->paeth_row
);
1008 #ifdef PNG_TIME_RFC1123_SUPPORTED
1009 png_free(png_ptr
, png_ptr
->time_buffer
);
1012 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
1013 /* Use this to save a little code space, it doesn't free the filter_costs */
1014 png_reset_filter_heuristics(png_ptr
);
1015 png_free(png_ptr
, png_ptr
->filter_costs
);
1016 png_free(png_ptr
, png_ptr
->inv_filter_costs
);
1019 #ifdef PNG_SETJMP_SUPPORTED
1020 /* Reset structure */
1021 png_memcpy(tmp_jmp
, png_ptr
->png_jmpbuf
, png_sizeof(jmp_buf));
1024 error_fn
= png_ptr
->error_fn
;
1025 warning_fn
= png_ptr
->warning_fn
;
1026 error_ptr
= png_ptr
->error_ptr
;
1027 #ifdef PNG_USER_MEM_SUPPORTED
1028 free_fn
= png_ptr
->free_fn
;
1031 png_memset(png_ptr
, 0, png_sizeof(png_struct
));
1033 png_ptr
->error_fn
= error_fn
;
1034 png_ptr
->warning_fn
= warning_fn
;
1035 png_ptr
->error_ptr
= error_ptr
;
1036 #ifdef PNG_USER_MEM_SUPPORTED
1037 png_ptr
->free_fn
= free_fn
;
1040 #ifdef PNG_SETJMP_SUPPORTED
1041 png_memcpy(png_ptr
->png_jmpbuf
, tmp_jmp
, png_sizeof(jmp_buf));
1045 /* Allow the application to select one or more row filters to use. */
1047 png_set_filter(png_structp png_ptr
, int method
, int filters
)
1049 png_debug(1, "in png_set_filter");
1051 if (png_ptr
== NULL
)
1054 #ifdef PNG_MNG_FEATURES_SUPPORTED
1055 if ((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
1056 (method
== PNG_INTRAPIXEL_DIFFERENCING
))
1057 method
= PNG_FILTER_TYPE_BASE
;
1060 if (method
== PNG_FILTER_TYPE_BASE
)
1062 switch (filters
& (PNG_ALL_FILTERS
| 0x07))
1064 #ifdef PNG_WRITE_FILTER_SUPPORTED
1067 case 7: png_warning(png_ptr
, "Unknown row filter for method 0");
1068 #endif /* PNG_WRITE_FILTER_SUPPORTED */
1069 case PNG_FILTER_VALUE_NONE
:
1070 png_ptr
->do_filter
= PNG_FILTER_NONE
; break;
1072 #ifdef PNG_WRITE_FILTER_SUPPORTED
1073 case PNG_FILTER_VALUE_SUB
:
1074 png_ptr
->do_filter
= PNG_FILTER_SUB
; break;
1076 case PNG_FILTER_VALUE_UP
:
1077 png_ptr
->do_filter
= PNG_FILTER_UP
; break;
1079 case PNG_FILTER_VALUE_AVG
:
1080 png_ptr
->do_filter
= PNG_FILTER_AVG
; break;
1082 case PNG_FILTER_VALUE_PAETH
:
1083 png_ptr
->do_filter
= PNG_FILTER_PAETH
; break;
1086 png_ptr
->do_filter
= (png_byte
)filters
; break;
1089 png_warning(png_ptr
, "Unknown row filter for method 0");
1090 #endif /* PNG_WRITE_FILTER_SUPPORTED */
1093 /* If we have allocated the row_buf, this means we have already started
1094 * with the image and we should have allocated all of the filter buffers
1095 * that have been selected. If prev_row isn't already allocated, then
1096 * it is too late to start using the filters that need it, since we
1097 * will be missing the data in the previous row. If an application
1098 * wants to start and stop using particular filters during compression,
1099 * it should start out with all of the filters, and then add and
1100 * remove them after the start of compression.
1102 if (png_ptr
->row_buf
!= NULL
)
1104 #ifdef PNG_WRITE_FILTER_SUPPORTED
1105 if ((png_ptr
->do_filter
& PNG_FILTER_SUB
) && png_ptr
->sub_row
== NULL
)
1107 png_ptr
->sub_row
= (png_bytep
)png_malloc(png_ptr
,
1108 (png_ptr
->rowbytes
+ 1));
1109 png_ptr
->sub_row
[0] = PNG_FILTER_VALUE_SUB
;
1112 if ((png_ptr
->do_filter
& PNG_FILTER_UP
) && png_ptr
->up_row
== NULL
)
1114 if (png_ptr
->prev_row
== NULL
)
1116 png_warning(png_ptr
, "Can't add Up filter after starting");
1117 png_ptr
->do_filter
= (png_byte
)(png_ptr
->do_filter
&
1123 png_ptr
->up_row
= (png_bytep
)png_malloc(png_ptr
,
1124 (png_ptr
->rowbytes
+ 1));
1125 png_ptr
->up_row
[0] = PNG_FILTER_VALUE_UP
;
1129 if ((png_ptr
->do_filter
& PNG_FILTER_AVG
) && png_ptr
->avg_row
== NULL
)
1131 if (png_ptr
->prev_row
== NULL
)
1133 png_warning(png_ptr
, "Can't add Average filter after starting");
1134 png_ptr
->do_filter
= (png_byte
)(png_ptr
->do_filter
&
1140 png_ptr
->avg_row
= (png_bytep
)png_malloc(png_ptr
,
1141 (png_ptr
->rowbytes
+ 1));
1142 png_ptr
->avg_row
[0] = PNG_FILTER_VALUE_AVG
;
1146 if ((png_ptr
->do_filter
& PNG_FILTER_PAETH
) &&
1147 png_ptr
->paeth_row
== NULL
)
1149 if (png_ptr
->prev_row
== NULL
)
1151 png_warning(png_ptr
, "Can't add Paeth filter after starting");
1152 png_ptr
->do_filter
&= (png_byte
)(~PNG_FILTER_PAETH
);
1157 png_ptr
->paeth_row
= (png_bytep
)png_malloc(png_ptr
,
1158 (png_ptr
->rowbytes
+ 1));
1159 png_ptr
->paeth_row
[0] = PNG_FILTER_VALUE_PAETH
;
1163 if (png_ptr
->do_filter
== PNG_NO_FILTERS
)
1164 #endif /* PNG_WRITE_FILTER_SUPPORTED */
1165 png_ptr
->do_filter
= PNG_FILTER_NONE
;
1169 png_error(png_ptr
, "Unknown custom filter method");
1172 /* This allows us to influence the way in which libpng chooses the "best"
1173 * filter for the current scanline. While the "minimum-sum-of-absolute-
1174 * differences metric is relatively fast and effective, there is some
1175 * question as to whether it can be improved upon by trying to keep the
1176 * filtered data going to zlib more consistent, hopefully resulting in
1177 * better compression.
1179 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
1180 /* Convenience reset API. */
1182 png_reset_filter_heuristics(png_structp png_ptr
)
1184 /* Clear out any old values in the 'weights' - this must be done because if
1185 * the app calls set_filter_heuristics multiple times with different
1186 * 'num_weights' values we would otherwise potentially have wrong sized
1189 png_ptr
->num_prev_filters
= 0;
1190 png_ptr
->heuristic_method
= PNG_FILTER_HEURISTIC_UNWEIGHTED
;
1191 if (png_ptr
->prev_filters
!= NULL
)
1193 png_bytep old
= png_ptr
->prev_filters
;
1194 png_ptr
->prev_filters
= NULL
;
1195 png_free(png_ptr
, old
);
1197 if (png_ptr
->filter_weights
!= NULL
)
1199 png_uint_16p old
= png_ptr
->filter_weights
;
1200 png_ptr
->filter_weights
= NULL
;
1201 png_free(png_ptr
, old
);
1204 if (png_ptr
->inv_filter_weights
!= NULL
)
1206 png_uint_16p old
= png_ptr
->inv_filter_weights
;
1207 png_ptr
->inv_filter_weights
= NULL
;
1208 png_free(png_ptr
, old
);
1211 /* Leave the filter_costs - this array is fixed size. */
1215 png_init_filter_heuristics(png_structp png_ptr
, int heuristic_method
,
1218 if (png_ptr
== NULL
)
1221 /* Clear out the arrays */
1222 png_reset_filter_heuristics(png_ptr
);
1224 /* Check arguments; the 'reset' function makes the correct settings for the
1225 * unweighted case, but we must handle the weight case by initializing the
1226 * arrays for the caller.
1228 if (heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1232 if (num_weights
> 0)
1234 png_ptr
->prev_filters
= (png_bytep
)png_malloc(png_ptr
,
1235 (png_uint_32
)(png_sizeof(png_byte
) * num_weights
));
1237 /* To make sure that the weighting starts out fairly */
1238 for (i
= 0; i
< num_weights
; i
++)
1240 png_ptr
->prev_filters
[i
] = 255;
1243 png_ptr
->filter_weights
= (png_uint_16p
)png_malloc(png_ptr
,
1244 (png_uint_32
)(png_sizeof(png_uint_16
) * num_weights
));
1246 png_ptr
->inv_filter_weights
= (png_uint_16p
)png_malloc(png_ptr
,
1247 (png_uint_32
)(png_sizeof(png_uint_16
) * num_weights
));
1249 for (i
= 0; i
< num_weights
; i
++)
1251 png_ptr
->inv_filter_weights
[i
] =
1252 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
1255 /* Safe to set this now */
1256 png_ptr
->num_prev_filters
= (png_byte
)num_weights
;
1259 /* If, in the future, there are other filter methods, this would
1260 * need to be based on png_ptr->filter.
1262 if (png_ptr
->filter_costs
== NULL
)
1264 png_ptr
->filter_costs
= (png_uint_16p
)png_malloc(png_ptr
,
1265 (png_uint_32
)(png_sizeof(png_uint_16
) * PNG_FILTER_VALUE_LAST
));
1267 png_ptr
->inv_filter_costs
= (png_uint_16p
)png_malloc(png_ptr
,
1268 (png_uint_32
)(png_sizeof(png_uint_16
) * PNG_FILTER_VALUE_LAST
));
1271 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++)
1273 png_ptr
->inv_filter_costs
[i
] =
1274 png_ptr
->filter_costs
[i
] = PNG_COST_FACTOR
;
1277 /* All the arrays are inited, safe to set this: */
1278 png_ptr
->heuristic_method
= PNG_FILTER_HEURISTIC_WEIGHTED
;
1280 /* Return the 'ok' code. */
1283 else if (heuristic_method
== PNG_FILTER_HEURISTIC_DEFAULT
||
1284 heuristic_method
== PNG_FILTER_HEURISTIC_UNWEIGHTED
)
1290 png_warning(png_ptr
, "Unknown filter heuristic method");
1295 /* Provide floating and fixed point APIs */
1296 #ifdef PNG_FLOATING_POINT_SUPPORTED
1298 png_set_filter_heuristics(png_structp png_ptr
, int heuristic_method
,
1299 int num_weights
, png_const_doublep filter_weights
,
1300 png_const_doublep filter_costs
)
1302 png_debug(1, "in png_set_filter_heuristics");
1304 /* The internal API allocates all the arrays and ensures that the elements of
1305 * those arrays are set to the default value.
1307 if (!png_init_filter_heuristics(png_ptr
, heuristic_method
, num_weights
))
1310 /* If using the weighted method copy in the weights. */
1311 if (heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1314 for (i
= 0; i
< num_weights
; i
++)
1316 if (filter_weights
[i
] <= 0.0)
1318 png_ptr
->inv_filter_weights
[i
] =
1319 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
1324 png_ptr
->inv_filter_weights
[i
] =
1325 (png_uint_16
)(PNG_WEIGHT_FACTOR
*filter_weights
[i
]+.5);
1327 png_ptr
->filter_weights
[i
] =
1328 (png_uint_16
)(PNG_WEIGHT_FACTOR
/filter_weights
[i
]+.5);
1332 /* Here is where we set the relative costs of the different filters. We
1333 * should take the desired compression level into account when setting
1334 * the costs, so that Paeth, for instance, has a high relative cost at low
1335 * compression levels, while it has a lower relative cost at higher
1336 * compression settings. The filter types are in order of increasing
1337 * relative cost, so it would be possible to do this with an algorithm.
1339 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++) if (filter_costs
[i
] >= 1.0)
1341 png_ptr
->inv_filter_costs
[i
] =
1342 (png_uint_16
)(PNG_COST_FACTOR
/ filter_costs
[i
] + .5);
1344 png_ptr
->filter_costs
[i
] =
1345 (png_uint_16
)(PNG_COST_FACTOR
* filter_costs
[i
] + .5);
1349 #endif /* FLOATING_POINT */
1351 #ifdef PNG_FIXED_POINT_SUPPORTED
1353 png_set_filter_heuristics_fixed(png_structp png_ptr
, int heuristic_method
,
1354 int num_weights
, png_const_fixed_point_p filter_weights
,
1355 png_const_fixed_point_p filter_costs
)
1357 png_debug(1, "in png_set_filter_heuristics_fixed");
1359 /* The internal API allocates all the arrays and ensures that the elements of
1360 * those arrays are set to the default value.
1362 if (!png_init_filter_heuristics(png_ptr
, heuristic_method
, num_weights
))
1365 /* If using the weighted method copy in the weights. */
1366 if (heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1369 for (i
= 0; i
< num_weights
; i
++)
1371 if (filter_weights
[i
] <= 0)
1373 png_ptr
->inv_filter_weights
[i
] =
1374 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
1379 png_ptr
->inv_filter_weights
[i
] = (png_uint_16
)
1380 ((PNG_WEIGHT_FACTOR
*filter_weights
[i
]+PNG_FP_HALF
)/PNG_FP_1
);
1382 png_ptr
->filter_weights
[i
] = (png_uint_16
)((PNG_WEIGHT_FACTOR
*
1383 PNG_FP_1
+(filter_weights
[i
]/2))/filter_weights
[i
]);
1387 /* Here is where we set the relative costs of the different filters. We
1388 * should take the desired compression level into account when setting
1389 * the costs, so that Paeth, for instance, has a high relative cost at low
1390 * compression levels, while it has a lower relative cost at higher
1391 * compression settings. The filter types are in order of increasing
1392 * relative cost, so it would be possible to do this with an algorithm.
1394 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++)
1395 if (filter_costs
[i
] >= PNG_FP_1
)
1399 /* Use a 32 bit unsigned temporary here because otherwise the
1400 * intermediate value will be a 32 bit *signed* integer (ANSI rules)
1401 * and this will get the wrong answer on division.
1403 tmp
= PNG_COST_FACTOR
*PNG_FP_1
+ (filter_costs
[i
]/2);
1404 tmp
/= filter_costs
[i
];
1406 png_ptr
->inv_filter_costs
[i
] = (png_uint_16
)tmp
;
1408 tmp
= PNG_COST_FACTOR
* filter_costs
[i
] + PNG_FP_HALF
;
1411 png_ptr
->filter_costs
[i
] = (png_uint_16
)tmp
;
1415 #endif /* FIXED_POINT */
1416 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1419 png_set_compression_level(png_structp png_ptr
, int level
)
1421 png_debug(1, "in png_set_compression_level");
1423 if (png_ptr
== NULL
)
1426 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_LEVEL
;
1427 png_ptr
->zlib_level
= level
;
1431 png_set_compression_mem_level(png_structp png_ptr
, int mem_level
)
1433 png_debug(1, "in png_set_compression_mem_level");
1435 if (png_ptr
== NULL
)
1438 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL
;
1439 png_ptr
->zlib_mem_level
= mem_level
;
1443 png_set_compression_strategy(png_structp png_ptr
, int strategy
)
1445 png_debug(1, "in png_set_compression_strategy");
1447 if (png_ptr
== NULL
)
1450 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_STRATEGY
;
1451 png_ptr
->zlib_strategy
= strategy
;
1455 png_set_compression_window_bits(png_structp png_ptr
, int window_bits
)
1457 if (png_ptr
== NULL
)
1460 if (window_bits
> 15)
1461 png_warning(png_ptr
, "Only compression windows <= 32k supported by PNG");
1463 else if (window_bits
< 8)
1464 png_warning(png_ptr
, "Only compression windows >= 256 supported by PNG");
1467 /* Avoid libpng bug with 256-byte windows */
1468 if (window_bits
== 8)
1470 png_warning(png_ptr
, "Compression window is being reset to 512");
1475 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS
;
1476 png_ptr
->zlib_window_bits
= window_bits
;
1480 png_set_compression_method(png_structp png_ptr
, int method
)
1482 png_debug(1, "in png_set_compression_method");
1484 if (png_ptr
== NULL
)
1488 png_warning(png_ptr
, "Only compression method 8 is supported by PNG");
1490 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_METHOD
;
1491 png_ptr
->zlib_method
= method
;
1495 png_set_write_status_fn(png_structp png_ptr
, png_write_status_ptr write_row_fn
)
1497 if (png_ptr
== NULL
)
1500 png_ptr
->write_row_fn
= write_row_fn
;
1503 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1505 png_set_write_user_transform_fn(png_structp png_ptr
, png_user_transform_ptr
1506 write_user_transform_fn
)
1508 png_debug(1, "in png_set_write_user_transform_fn");
1510 if (png_ptr
== NULL
)
1513 png_ptr
->transformations
|= PNG_USER_TRANSFORM
;
1514 png_ptr
->write_user_transform_fn
= write_user_transform_fn
;
1519 #ifdef PNG_INFO_IMAGE_SUPPORTED
1521 png_write_png(png_structp png_ptr
, png_infop info_ptr
,
1522 int transforms
, voidp params
)
1524 if (png_ptr
== NULL
|| info_ptr
== NULL
)
1527 /* Write the file header information. */
1528 png_write_info(png_ptr
, info_ptr
);
1530 /* ------ these transformations don't touch the info structure ------- */
1532 #ifdef PNG_WRITE_INVERT_SUPPORTED
1533 /* Invert monochrome pixels */
1534 if (transforms
& PNG_TRANSFORM_INVERT_MONO
)
1535 png_set_invert_mono(png_ptr
);
1538 #ifdef PNG_WRITE_SHIFT_SUPPORTED
1539 /* Shift the pixels up to a legal bit depth and fill in
1540 * as appropriate to correctly scale the image.
1542 if ((transforms
& PNG_TRANSFORM_SHIFT
)
1543 && (info_ptr
->valid
& PNG_INFO_sBIT
))
1544 png_set_shift(png_ptr
, &info_ptr
->sig_bit
);
1547 #ifdef PNG_WRITE_PACK_SUPPORTED
1548 /* Pack pixels into bytes */
1549 if (transforms
& PNG_TRANSFORM_PACKING
)
1550 png_set_packing(png_ptr
);
1553 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
1554 /* Swap location of alpha bytes from ARGB to RGBA */
1555 if (transforms
& PNG_TRANSFORM_SWAP_ALPHA
)
1556 png_set_swap_alpha(png_ptr
);
1559 #ifdef PNG_WRITE_FILLER_SUPPORTED
1560 /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */
1561 if (transforms
& PNG_TRANSFORM_STRIP_FILLER_AFTER
)
1562 png_set_filler(png_ptr
, 0, PNG_FILLER_AFTER
);
1564 else if (transforms
& PNG_TRANSFORM_STRIP_FILLER_BEFORE
)
1565 png_set_filler(png_ptr
, 0, PNG_FILLER_BEFORE
);
1568 #ifdef PNG_WRITE_BGR_SUPPORTED
1569 /* Flip BGR pixels to RGB */
1570 if (transforms
& PNG_TRANSFORM_BGR
)
1571 png_set_bgr(png_ptr
);
1574 #ifdef PNG_WRITE_SWAP_SUPPORTED
1575 /* Swap bytes of 16-bit files to most significant byte first */
1576 if (transforms
& PNG_TRANSFORM_SWAP_ENDIAN
)
1577 png_set_swap(png_ptr
);
1580 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
1581 /* Swap bits of 1, 2, 4 bit packed pixel formats */
1582 if (transforms
& PNG_TRANSFORM_PACKSWAP
)
1583 png_set_packswap(png_ptr
);
1586 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
1587 /* Invert the alpha channel from opacity to transparency */
1588 if (transforms
& PNG_TRANSFORM_INVERT_ALPHA
)
1589 png_set_invert_alpha(png_ptr
);
1592 /* ----------------------- end of transformations ------------------- */
1594 /* Write the bits */
1595 if (info_ptr
->valid
& PNG_INFO_IDAT
)
1596 png_write_image(png_ptr
, info_ptr
->row_pointers
);
1598 /* It is REQUIRED to call this to finish writing the rest of the file */
1599 png_write_end(png_ptr
, info_ptr
);
1601 PNG_UNUSED(transforms
) /* Quiet compiler warnings */
1605 #endif /* PNG_WRITE_SUPPORTED */