2 /* pngwrite.c - general routines to write a PNG file
4 * Last changed in libpng 1.6.11 [June 5, 2014]
5 * Copyright (c) 1998-2014 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
15 #if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
19 #ifdef PNG_WRITE_SUPPORTED
21 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
22 /* Write out all the unknown chunks for the current given location */
24 write_unknown_chunks(png_structrp png_ptr
, png_const_inforp info_ptr
,
27 if (info_ptr
->unknown_chunks_num
)
29 png_const_unknown_chunkp up
;
31 png_debug(5, "writing extra chunks");
33 for (up
= info_ptr
->unknown_chunks
;
34 up
< info_ptr
->unknown_chunks
+ info_ptr
->unknown_chunks_num
;
36 if (up
->location
& where
)
38 /* If per-chunk unknown chunk handling is enabled use it, otherwise
39 * just write the chunks the application has set.
41 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
42 int keep
= png_handle_as_unknown(png_ptr
, up
->name
);
44 /* NOTE: this code is radically different from the read side in the
45 * matter of handling an ancillary unknown chunk. In the read side
46 * the default behavior is to discard it, in the code below the default
47 * behavior is to write it. Critical chunks are, however, only
48 * written if explicitly listed or if the default is set to write all
51 * The default handling is also slightly weird - it is not possible to
52 * stop the writing of all unsafe-to-copy chunks!
54 * TODO: REVIEW: this would seem to be a bug.
56 if (keep
!= PNG_HANDLE_CHUNK_NEVER
&&
57 ((up
->name
[3] & 0x20) /* safe-to-copy overrides everything */ ||
58 keep
== PNG_HANDLE_CHUNK_ALWAYS
||
59 (keep
== PNG_HANDLE_CHUNK_AS_DEFAULT
&&
60 png_ptr
->unknown_default
== PNG_HANDLE_CHUNK_ALWAYS
)))
63 /* TODO: review, what is wrong with a zero length unknown chunk? */
65 png_warning(png_ptr
, "Writing zero-length unknown chunk");
67 png_write_chunk(png_ptr
, up
->name
, up
->data
, up
->size
);
72 #endif /* PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED */
74 /* Writes all the PNG information. This is the suggested way to use the
75 * library. If you have a new chunk to add, make a function to write it,
76 * and put it in the correct location here. If you want the chunk written
77 * after the image data, put it in png_write_end(). I strongly encourage
78 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
79 * the chunk, as that will keep the code from breaking if you want to just
80 * write a plain PNG file. If you have long comments, I suggest writing
81 * them in png_write_end(), and compressing them.
84 png_write_info_before_PLTE(png_structrp png_ptr
, png_const_inforp info_ptr
)
86 png_debug(1, "in png_write_info_before_PLTE");
88 if (png_ptr
== NULL
|| info_ptr
== NULL
)
91 if (!(png_ptr
->mode
& PNG_WROTE_INFO_BEFORE_PLTE
))
93 /* Write PNG signature */
94 png_write_sig(png_ptr
);
96 #ifdef PNG_MNG_FEATURES_SUPPORTED
97 if ((png_ptr
->mode
&PNG_HAVE_PNG_SIGNATURE
) && \
98 (png_ptr
->mng_features_permitted
))
100 png_warning(png_ptr
, "MNG features are not allowed in a PNG datastream");
101 png_ptr
->mng_features_permitted
= 0;
105 /* Write IHDR information. */
106 png_write_IHDR(png_ptr
, info_ptr
->width
, info_ptr
->height
,
107 info_ptr
->bit_depth
, info_ptr
->color_type
, info_ptr
->compression_type
,
108 info_ptr
->filter_type
,
109 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
110 info_ptr
->interlace_type
116 /* The rest of these check to see if the valid field has the appropriate
117 * flag set, and if it does, writes the chunk.
119 * 1.6.0: COLORSPACE support controls the writing of these chunks too, and
120 * the chunks will be written if the WRITE routine is there and information
121 * is available in the COLORSPACE. (See png_colorspace_sync_info in png.c
122 * for where the valid flags get set.)
124 * Under certain circumstances the colorspace can be invalidated without
125 * syncing the info_struct 'valid' flags; this happens if libpng detects and
126 * error and calls png_error while the color space is being set, yet the
127 * application continues writing the PNG. So check the 'invalid' flag here
130 #ifdef PNG_WRITE_APNG_SUPPORTED
131 if (info_ptr
->valid
& PNG_INFO_acTL
)
132 png_write_acTL(png_ptr
, info_ptr
->num_frames
, info_ptr
->num_plays
);
134 #ifdef PNG_GAMMA_SUPPORTED
135 # ifdef PNG_WRITE_gAMA_SUPPORTED
136 if (!(info_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
) &&
137 (info_ptr
->colorspace
.flags
& PNG_COLORSPACE_FROM_gAMA
) &&
138 (info_ptr
->valid
& PNG_INFO_gAMA
))
139 png_write_gAMA_fixed(png_ptr
, info_ptr
->colorspace
.gamma
);
143 #ifdef PNG_COLORSPACE_SUPPORTED
144 /* Write only one of sRGB or an ICC profile. If a profile was supplied
145 * and it matches one of the known sRGB ones issue a warning.
147 # ifdef PNG_WRITE_iCCP_SUPPORTED
148 if (!(info_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
) &&
149 (info_ptr
->valid
& PNG_INFO_iCCP
))
151 # ifdef PNG_WRITE_sRGB_SUPPORTED
152 if (info_ptr
->valid
& PNG_INFO_sRGB
)
153 png_app_warning(png_ptr
,
154 "profile matches sRGB but writing iCCP instead");
157 png_write_iCCP(png_ptr
, info_ptr
->iccp_name
,
158 info_ptr
->iccp_profile
);
160 # ifdef PNG_WRITE_sRGB_SUPPORTED
165 # ifdef PNG_WRITE_sRGB_SUPPORTED
166 if (!(info_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
) &&
167 (info_ptr
->valid
& PNG_INFO_sRGB
))
168 png_write_sRGB(png_ptr
, info_ptr
->colorspace
.rendering_intent
);
169 # endif /* WRITE_sRGB */
170 #endif /* COLORSPACE */
172 #ifdef PNG_WRITE_sBIT_SUPPORTED
173 if (info_ptr
->valid
& PNG_INFO_sBIT
)
174 png_write_sBIT(png_ptr
, &(info_ptr
->sig_bit
), info_ptr
->color_type
);
177 #ifdef PNG_COLORSPACE_SUPPORTED
178 # ifdef PNG_WRITE_cHRM_SUPPORTED
179 if (!(info_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
) &&
180 (info_ptr
->colorspace
.flags
& PNG_COLORSPACE_FROM_cHRM
) &&
181 (info_ptr
->valid
& PNG_INFO_cHRM
))
182 png_write_cHRM_fixed(png_ptr
, &info_ptr
->colorspace
.end_points_xy
);
186 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
187 write_unknown_chunks(png_ptr
, info_ptr
, PNG_HAVE_IHDR
);
190 png_ptr
->mode
|= PNG_WROTE_INFO_BEFORE_PLTE
;
195 png_write_info(png_structrp png_ptr
, png_const_inforp info_ptr
)
197 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
201 png_debug(1, "in png_write_info");
203 if (png_ptr
== NULL
|| info_ptr
== NULL
)
206 png_write_info_before_PLTE(png_ptr
, info_ptr
);
208 if (info_ptr
->valid
& PNG_INFO_PLTE
)
209 png_write_PLTE(png_ptr
, info_ptr
->palette
,
210 (png_uint_32
)info_ptr
->num_palette
);
212 else if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
213 png_error(png_ptr
, "Valid palette required for paletted images");
215 #ifdef PNG_WRITE_tRNS_SUPPORTED
216 if (info_ptr
->valid
& PNG_INFO_tRNS
)
218 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
219 /* Invert the alpha channel (in tRNS) */
220 if ((png_ptr
->transformations
& PNG_INVERT_ALPHA
) &&
221 info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
224 for (j
= 0; j
<(int)info_ptr
->num_trans
; j
++)
225 info_ptr
->trans_alpha
[j
] =
226 (png_byte
)(255 - info_ptr
->trans_alpha
[j
]);
229 png_write_tRNS(png_ptr
, info_ptr
->trans_alpha
, &(info_ptr
->trans_color
),
230 info_ptr
->num_trans
, info_ptr
->color_type
);
233 #ifdef PNG_WRITE_bKGD_SUPPORTED
234 if (info_ptr
->valid
& PNG_INFO_bKGD
)
235 png_write_bKGD(png_ptr
, &(info_ptr
->background
), info_ptr
->color_type
);
238 #ifdef PNG_WRITE_hIST_SUPPORTED
239 if (info_ptr
->valid
& PNG_INFO_hIST
)
240 png_write_hIST(png_ptr
, info_ptr
->hist
, info_ptr
->num_palette
);
243 #ifdef PNG_WRITE_oFFs_SUPPORTED
244 if (info_ptr
->valid
& PNG_INFO_oFFs
)
245 png_write_oFFs(png_ptr
, info_ptr
->x_offset
, info_ptr
->y_offset
,
246 info_ptr
->offset_unit_type
);
249 #ifdef PNG_WRITE_pCAL_SUPPORTED
250 if (info_ptr
->valid
& PNG_INFO_pCAL
)
251 png_write_pCAL(png_ptr
, info_ptr
->pcal_purpose
, info_ptr
->pcal_X0
,
252 info_ptr
->pcal_X1
, info_ptr
->pcal_type
, info_ptr
->pcal_nparams
,
253 info_ptr
->pcal_units
, info_ptr
->pcal_params
);
256 #ifdef PNG_WRITE_sCAL_SUPPORTED
257 if (info_ptr
->valid
& PNG_INFO_sCAL
)
258 png_write_sCAL_s(png_ptr
, (int)info_ptr
->scal_unit
,
259 info_ptr
->scal_s_width
, info_ptr
->scal_s_height
);
262 #ifdef PNG_WRITE_pHYs_SUPPORTED
263 if (info_ptr
->valid
& PNG_INFO_pHYs
)
264 png_write_pHYs(png_ptr
, info_ptr
->x_pixels_per_unit
,
265 info_ptr
->y_pixels_per_unit
, info_ptr
->phys_unit_type
);
268 #ifdef PNG_WRITE_tIME_SUPPORTED
269 if (info_ptr
->valid
& PNG_INFO_tIME
)
271 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
272 png_ptr
->mode
|= PNG_WROTE_tIME
;
276 #ifdef PNG_WRITE_sPLT_SUPPORTED
277 if (info_ptr
->valid
& PNG_INFO_sPLT
)
278 for (i
= 0; i
< (int)info_ptr
->splt_palettes_num
; i
++)
279 png_write_sPLT(png_ptr
, info_ptr
->splt_palettes
+ i
);
282 #ifdef PNG_WRITE_TEXT_SUPPORTED
283 /* Check to see if we need to write text chunks */
284 for (i
= 0; i
< info_ptr
->num_text
; i
++)
286 png_debug2(2, "Writing header text chunk %d, type %d", i
,
287 info_ptr
->text
[i
].compression
);
288 /* An internationalized chunk? */
289 if (info_ptr
->text
[i
].compression
> 0)
291 #ifdef PNG_WRITE_iTXt_SUPPORTED
292 /* Write international chunk */
293 png_write_iTXt(png_ptr
,
294 info_ptr
->text
[i
].compression
,
295 info_ptr
->text
[i
].key
,
296 info_ptr
->text
[i
].lang
,
297 info_ptr
->text
[i
].lang_key
,
298 info_ptr
->text
[i
].text
);
300 png_warning(png_ptr
, "Unable to write international text");
302 /* Mark this chunk as written */
303 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
306 /* If we want a compressed text chunk */
307 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_zTXt
)
309 #ifdef PNG_WRITE_zTXt_SUPPORTED
310 /* Write compressed chunk */
311 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
312 info_ptr
->text
[i
].text
, 0,
313 info_ptr
->text
[i
].compression
);
315 png_warning(png_ptr
, "Unable to write compressed text");
317 /* Mark this chunk as written */
318 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
321 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
323 #ifdef PNG_WRITE_tEXt_SUPPORTED
324 /* Write uncompressed chunk */
325 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
326 info_ptr
->text
[i
].text
,
328 /* Mark this chunk as written */
329 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
332 png_warning(png_ptr
, "Unable to write uncompressed text");
338 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
339 write_unknown_chunks(png_ptr
, info_ptr
, PNG_HAVE_PLTE
);
343 /* Writes the end of the PNG file. If you don't want to write comments or
344 * time information, you can pass NULL for info. If you already wrote these
345 * in png_write_info(), do not write them again here. If you have long
346 * comments, I suggest writing them here, and compressing them.
349 png_write_end(png_structrp png_ptr
, png_inforp info_ptr
)
351 png_debug(1, "in png_write_end");
356 if (!(png_ptr
->mode
& PNG_HAVE_IDAT
))
357 png_error(png_ptr
, "No IDATs written into file");
359 #ifdef PNG_WRITE_APNG_SUPPORTED
360 if (png_ptr
->num_frames_written
!= png_ptr
->num_frames_to_write
)
361 png_error(png_ptr
, "Not enough frames written");
364 #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
365 if (png_ptr
->num_palette_max
> png_ptr
->num_palette
)
366 png_benign_error(png_ptr
, "Wrote palette index exceeding num_palette");
369 /* See if user wants us to write information chunks */
370 if (info_ptr
!= NULL
)
372 #ifdef PNG_WRITE_TEXT_SUPPORTED
373 int i
; /* local index variable */
375 #ifdef PNG_WRITE_tIME_SUPPORTED
376 /* Check to see if user has supplied a time chunk */
377 if ((info_ptr
->valid
& PNG_INFO_tIME
) &&
378 !(png_ptr
->mode
& PNG_WROTE_tIME
))
379 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
382 #ifdef PNG_WRITE_TEXT_SUPPORTED
383 /* Loop through comment chunks */
384 for (i
= 0; i
< info_ptr
->num_text
; i
++)
386 png_debug2(2, "Writing trailer text chunk %d, type %d", i
,
387 info_ptr
->text
[i
].compression
);
388 /* An internationalized chunk? */
389 if (info_ptr
->text
[i
].compression
> 0)
391 #ifdef PNG_WRITE_iTXt_SUPPORTED
392 /* Write international chunk */
393 png_write_iTXt(png_ptr
,
394 info_ptr
->text
[i
].compression
,
395 info_ptr
->text
[i
].key
,
396 info_ptr
->text
[i
].lang
,
397 info_ptr
->text
[i
].lang_key
,
398 info_ptr
->text
[i
].text
);
400 png_warning(png_ptr
, "Unable to write international text");
402 /* Mark this chunk as written */
403 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
406 else if (info_ptr
->text
[i
].compression
>= PNG_TEXT_COMPRESSION_zTXt
)
408 #ifdef PNG_WRITE_zTXt_SUPPORTED
409 /* Write compressed chunk */
410 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
411 info_ptr
->text
[i
].text
, 0,
412 info_ptr
->text
[i
].compression
);
414 png_warning(png_ptr
, "Unable to write compressed text");
416 /* Mark this chunk as written */
417 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
420 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
422 #ifdef PNG_WRITE_tEXt_SUPPORTED
423 /* Write uncompressed chunk */
424 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
425 info_ptr
->text
[i
].text
, 0);
427 png_warning(png_ptr
, "Unable to write uncompressed text");
430 /* Mark this chunk as written */
431 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
435 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
436 write_unknown_chunks(png_ptr
, info_ptr
, PNG_AFTER_IDAT
);
440 png_ptr
->mode
|= PNG_AFTER_IDAT
;
442 /* Write end of PNG file */
443 png_write_IEND(png_ptr
);
445 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
446 * and restored again in libpng-1.2.30, may cause some applications that
447 * do not set png_ptr->output_flush_fn to crash. If your application
448 * experiences a problem, please try building libpng with
449 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
450 * png-mng-implement at lists.sf.net .
452 #ifdef PNG_WRITE_FLUSH_SUPPORTED
453 # ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
459 #ifdef PNG_CONVERT_tIME_SUPPORTED
461 png_convert_from_struct_tm(png_timep ptime
, PNG_CONST
struct tm
* ttime
)
463 png_debug(1, "in png_convert_from_struct_tm");
465 ptime
->year
= (png_uint_16
)(1900 + ttime
->tm_year
);
466 ptime
->month
= (png_byte
)(ttime
->tm_mon
+ 1);
467 ptime
->day
= (png_byte
)ttime
->tm_mday
;
468 ptime
->hour
= (png_byte
)ttime
->tm_hour
;
469 ptime
->minute
= (png_byte
)ttime
->tm_min
;
470 ptime
->second
= (png_byte
)ttime
->tm_sec
;
474 png_convert_from_time_t(png_timep ptime
, time_t ttime
)
478 png_debug(1, "in png_convert_from_time_t");
480 tbuf
= gmtime(&ttime
);
481 png_convert_from_struct_tm(ptime
, tbuf
);
485 /* Initialize png_ptr structure, and allocate any memory needed */
486 PNG_FUNCTION(png_structp
,PNGAPI
487 png_create_write_struct
,(png_const_charp user_png_ver
, png_voidp error_ptr
,
488 png_error_ptr error_fn
, png_error_ptr warn_fn
),PNG_ALLOCATED
)
490 #ifndef PNG_USER_MEM_SUPPORTED
491 png_structrp png_ptr
= png_create_png_struct(user_png_ver
, error_ptr
,
492 error_fn
, warn_fn
, NULL
, NULL
, NULL
);
494 return png_create_write_struct_2(user_png_ver
, error_ptr
, error_fn
,
495 warn_fn
, NULL
, NULL
, NULL
);
498 /* Alternate initialize png_ptr structure, and allocate any memory needed */
499 PNG_FUNCTION(png_structp
,PNGAPI
500 png_create_write_struct_2
,(png_const_charp user_png_ver
, png_voidp error_ptr
,
501 png_error_ptr error_fn
, png_error_ptr warn_fn
, png_voidp mem_ptr
,
502 png_malloc_ptr malloc_fn
, png_free_ptr free_fn
),PNG_ALLOCATED
)
504 png_structrp png_ptr
= png_create_png_struct(user_png_ver
, error_ptr
,
505 error_fn
, warn_fn
, mem_ptr
, malloc_fn
, free_fn
);
506 #endif /* PNG_USER_MEM_SUPPORTED */
509 /* Set the zlib control values to defaults; they can be overridden by the
510 * application after the struct has been created.
512 png_ptr
->zbuffer_size
= PNG_ZBUF_SIZE
;
514 /* The 'zlib_strategy' setting is irrelevant because png_default_claim in
515 * pngwutil.c defaults it according to whether or not filters will be
516 * used, and ignores this setting.
518 png_ptr
->zlib_strategy
= PNG_Z_DEFAULT_STRATEGY
;
519 png_ptr
->zlib_level
= PNG_Z_DEFAULT_COMPRESSION
;
520 png_ptr
->zlib_mem_level
= 8;
521 png_ptr
->zlib_window_bits
= 15;
522 png_ptr
->zlib_method
= 8;
524 #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
525 png_ptr
->zlib_text_strategy
= PNG_TEXT_Z_DEFAULT_STRATEGY
;
526 png_ptr
->zlib_text_level
= PNG_TEXT_Z_DEFAULT_COMPRESSION
;
527 png_ptr
->zlib_text_mem_level
= 8;
528 png_ptr
->zlib_text_window_bits
= 15;
529 png_ptr
->zlib_text_method
= 8;
530 #endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
532 /* This is a highly dubious configuration option; by default it is off,
533 * but it may be appropriate for private builds that are testing
534 * extensions not conformant to the current specification, or of
535 * applications that must not fail to write at all costs!
537 #ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED
538 /* In stable builds only warn if an application error can be completely
541 png_ptr
->flags
|= PNG_FLAG_BENIGN_ERRORS_WARN
;
544 /* App warnings are warnings in release (or release candidate) builds but
545 * are errors during development.
547 #if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
548 png_ptr
->flags
|= PNG_FLAG_APP_WARNINGS_WARN
;
551 /* TODO: delay this, it can be done in png_init_io() (if the app doesn't
552 * do it itself) avoiding setting the default function if it is not
555 png_set_write_fn(png_ptr
, NULL
, NULL
, NULL
);
562 /* Write a few rows of image data. If the image is interlaced,
563 * either you will have to write the 7 sub images, or, if you
564 * have called png_set_interlace_handling(), you will have to
565 * "write" the image seven times.
568 png_write_rows(png_structrp png_ptr
, png_bytepp row
,
569 png_uint_32 num_rows
)
571 png_uint_32 i
; /* row counter */
572 png_bytepp rp
; /* row pointer */
574 png_debug(1, "in png_write_rows");
579 /* Loop through the rows */
580 for (i
= 0, rp
= row
; i
< num_rows
; i
++, rp
++)
582 png_write_row(png_ptr
, *rp
);
586 /* Write the image. You only need to call this function once, even
587 * if you are writing an interlaced image.
590 png_write_image(png_structrp png_ptr
, png_bytepp image
)
592 png_uint_32 i
; /* row index */
593 int pass
, num_pass
; /* pass variables */
594 png_bytepp rp
; /* points to current row */
599 png_debug(1, "in png_write_image");
601 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
602 /* Initialize interlace handling. If image is not interlaced,
603 * this will set pass to 1
605 num_pass
= png_set_interlace_handling(png_ptr
);
609 /* Loop through passes */
610 for (pass
= 0; pass
< num_pass
; pass
++)
612 /* Loop through image */
613 for (i
= 0, rp
= image
; i
< png_ptr
->height
; i
++, rp
++)
615 png_write_row(png_ptr
, *rp
);
620 #ifdef PNG_MNG_FEATURES_SUPPORTED
621 /* Performs intrapixel differencing */
623 png_do_write_intrapixel(png_row_infop row_info
, png_bytep row
)
625 png_debug(1, "in png_do_write_intrapixel");
627 if ((row_info
->color_type
& PNG_COLOR_MASK_COLOR
))
630 png_uint_32 row_width
= row_info
->width
;
631 if (row_info
->bit_depth
== 8)
636 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
639 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
645 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
647 *(rp
) = (png_byte
)((*rp
- *(rp
+ 1)) & 0xff);
648 *(rp
+ 2) = (png_byte
)((*(rp
+ 2) - *(rp
+ 1)) & 0xff);
652 #ifdef PNG_WRITE_16BIT_SUPPORTED
653 else if (row_info
->bit_depth
== 16)
658 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
661 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
667 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
669 png_uint_32 s0
= (*(rp
) << 8) | *(rp
+ 1);
670 png_uint_32 s1
= (*(rp
+ 2) << 8) | *(rp
+ 3);
671 png_uint_32 s2
= (*(rp
+ 4) << 8) | *(rp
+ 5);
672 png_uint_32 red
= (png_uint_32
)((s0
- s1
) & 0xffffL
);
673 png_uint_32 blue
= (png_uint_32
)((s2
- s1
) & 0xffffL
);
674 *(rp
) = (png_byte
)((red
>> 8) & 0xff);
675 *(rp
+ 1) = (png_byte
)(red
& 0xff);
676 *(rp
+ 4) = (png_byte
)((blue
>> 8) & 0xff);
677 *(rp
+ 5) = (png_byte
)(blue
& 0xff);
680 #endif /* PNG_WRITE_16BIT_SUPPORTED */
683 #endif /* PNG_MNG_FEATURES_SUPPORTED */
685 /* Called by user to write a row of image data */
687 png_write_row(png_structrp png_ptr
, png_const_bytep row
)
689 /* 1.5.6: moved from png_struct to be a local structure: */
690 png_row_info row_info
;
695 png_debug2(1, "in png_write_row (row %u, pass %d)",
696 png_ptr
->row_number
, png_ptr
->pass
);
698 /* Initialize transformations and other stuff if first time */
699 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
701 /* Make sure we wrote the header info */
702 if (!(png_ptr
->mode
& PNG_WROTE_INFO_BEFORE_PLTE
))
704 "png_write_info was never called before png_write_row");
706 /* Check for transforms that have been set but were defined out */
707 #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
708 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
709 png_warning(png_ptr
, "PNG_WRITE_INVERT_SUPPORTED is not defined");
712 #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
713 if (png_ptr
->transformations
& PNG_FILLER
)
714 png_warning(png_ptr
, "PNG_WRITE_FILLER_SUPPORTED is not defined");
716 #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
717 defined(PNG_READ_PACKSWAP_SUPPORTED)
718 if (png_ptr
->transformations
& PNG_PACKSWAP
)
720 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
723 #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
724 if (png_ptr
->transformations
& PNG_PACK
)
725 png_warning(png_ptr
, "PNG_WRITE_PACK_SUPPORTED is not defined");
728 #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
729 if (png_ptr
->transformations
& PNG_SHIFT
)
730 png_warning(png_ptr
, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
733 #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
734 if (png_ptr
->transformations
& PNG_BGR
)
735 png_warning(png_ptr
, "PNG_WRITE_BGR_SUPPORTED is not defined");
738 #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
739 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
740 png_warning(png_ptr
, "PNG_WRITE_SWAP_SUPPORTED is not defined");
743 png_write_start_row(png_ptr
);
746 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
747 /* If interlaced and not interested in row, return */
748 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
750 switch (png_ptr
->pass
)
753 if (png_ptr
->row_number
& 0x07)
755 png_write_finish_row(png_ptr
);
761 if ((png_ptr
->row_number
& 0x07) || png_ptr
->width
< 5)
763 png_write_finish_row(png_ptr
);
769 if ((png_ptr
->row_number
& 0x07) != 4)
771 png_write_finish_row(png_ptr
);
777 if ((png_ptr
->row_number
& 0x03) || png_ptr
->width
< 3)
779 png_write_finish_row(png_ptr
);
785 if ((png_ptr
->row_number
& 0x03) != 2)
787 png_write_finish_row(png_ptr
);
793 if ((png_ptr
->row_number
& 0x01) || png_ptr
->width
< 2)
795 png_write_finish_row(png_ptr
);
801 if (!(png_ptr
->row_number
& 0x01))
803 png_write_finish_row(png_ptr
);
808 default: /* error: ignore it */
814 /* Set up row info for transformations */
815 row_info
.color_type
= png_ptr
->color_type
;
816 row_info
.width
= png_ptr
->usr_width
;
817 row_info
.channels
= png_ptr
->usr_channels
;
818 row_info
.bit_depth
= png_ptr
->usr_bit_depth
;
819 row_info
.pixel_depth
= (png_byte
)(row_info
.bit_depth
* row_info
.channels
);
820 row_info
.rowbytes
= PNG_ROWBYTES(row_info
.pixel_depth
, row_info
.width
);
822 png_debug1(3, "row_info->color_type = %d", row_info
.color_type
);
823 png_debug1(3, "row_info->width = %u", row_info
.width
);
824 png_debug1(3, "row_info->channels = %d", row_info
.channels
);
825 png_debug1(3, "row_info->bit_depth = %d", row_info
.bit_depth
);
826 png_debug1(3, "row_info->pixel_depth = %d", row_info
.pixel_depth
);
827 png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info
.rowbytes
);
829 /* Copy user's row into buffer, leaving room for filter byte. */
830 memcpy(png_ptr
->row_buf
+ 1, row
, row_info
.rowbytes
);
832 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
833 /* Handle interlacing */
834 if (png_ptr
->interlaced
&& png_ptr
->pass
< 6 &&
835 (png_ptr
->transformations
& PNG_INTERLACE
))
837 png_do_write_interlace(&row_info
, png_ptr
->row_buf
+ 1, png_ptr
->pass
);
838 /* This should always get caught above, but still ... */
839 if (row_info
.width
== 0)
841 png_write_finish_row(png_ptr
);
847 #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
848 /* Handle other transformations */
849 if (png_ptr
->transformations
)
850 png_do_write_transformations(png_ptr
, &row_info
);
853 /* At this point the row_info pixel depth must match the 'transformed' depth,
854 * which is also the output depth.
856 if (row_info
.pixel_depth
!= png_ptr
->pixel_depth
||
857 row_info
.pixel_depth
!= png_ptr
->transformed_pixel_depth
)
858 png_error(png_ptr
, "internal write transform logic error");
860 #ifdef PNG_MNG_FEATURES_SUPPORTED
861 /* Write filter_method 64 (intrapixel differencing) only if
862 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
863 * 2. Libpng did not write a PNG signature (this filter_method is only
864 * used in PNG datastreams that are embedded in MNG datastreams) and
865 * 3. The application called png_permit_mng_features with a mask that
866 * included PNG_FLAG_MNG_FILTER_64 and
867 * 4. The filter_method is 64 and
868 * 5. The color_type is RGB or RGBA
870 if ((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
871 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
873 /* Intrapixel differencing */
874 png_do_write_intrapixel(&row_info
, png_ptr
->row_buf
+ 1);
878 /* Added at libpng-1.5.10 */
879 #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
880 /* Check for out-of-range palette index */
881 if (row_info
.color_type
== PNG_COLOR_TYPE_PALETTE
&&
882 png_ptr
->num_palette_max
>= 0)
883 png_do_check_palette_indexes(png_ptr
, &row_info
);
886 /* Find a filter if necessary, filter the row and write it out. */
887 png_write_find_filter(png_ptr
, &row_info
);
889 if (png_ptr
->write_row_fn
!= NULL
)
890 (*(png_ptr
->write_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
893 #ifdef PNG_WRITE_FLUSH_SUPPORTED
894 /* Set the automatic flush interval or 0 to turn flushing off */
896 png_set_flush(png_structrp png_ptr
, int nrows
)
898 png_debug(1, "in png_set_flush");
903 png_ptr
->flush_dist
= (nrows
< 0 ? 0 : nrows
);
906 /* Flush the current output buffers now */
908 png_write_flush(png_structrp png_ptr
)
910 png_debug(1, "in png_write_flush");
915 /* We have already written out all of the data */
916 if (png_ptr
->row_number
>= png_ptr
->num_rows
)
919 png_compress_IDAT(png_ptr
, NULL
, 0, Z_SYNC_FLUSH
);
920 png_ptr
->flush_rows
= 0;
923 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
925 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
926 static void png_reset_filter_heuristics(png_structrp png_ptr
);/* forward decl */
929 /* Free any memory used in png_ptr struct without freeing the struct itself. */
931 png_write_destroy(png_structrp png_ptr
)
933 png_debug(1, "in png_write_destroy");
935 /* Free any memory zlib uses */
936 if (png_ptr
->flags
& PNG_FLAG_ZSTREAM_INITIALIZED
)
937 deflateEnd(&png_ptr
->zstream
);
939 /* Free our memory. png_free checks NULL for us. */
940 png_free_buffer_list(png_ptr
, &png_ptr
->zbuffer_list
);
941 png_free(png_ptr
, png_ptr
->row_buf
);
942 #ifdef PNG_WRITE_FILTER_SUPPORTED
943 png_free(png_ptr
, png_ptr
->prev_row
);
944 png_free(png_ptr
, png_ptr
->sub_row
);
945 png_free(png_ptr
, png_ptr
->up_row
);
946 png_free(png_ptr
, png_ptr
->avg_row
);
947 png_free(png_ptr
, png_ptr
->paeth_row
);
950 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
951 /* Use this to save a little code space, it doesn't free the filter_costs */
952 png_reset_filter_heuristics(png_ptr
);
953 png_free(png_ptr
, png_ptr
->filter_costs
);
954 png_free(png_ptr
, png_ptr
->inv_filter_costs
);
957 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
958 png_free(png_ptr
, png_ptr
->chunk_list
);
961 /* The error handling and memory handling information is left intact at this
962 * point: the jmp_buf may still have to be freed. See png_destroy_png_struct
963 * for how this happens.
967 /* Free all memory used by the write.
968 * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
969 * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free
970 * the passed in info_structs but it would quietly fail to free any of the data
971 * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it
975 png_destroy_write_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
)
977 png_debug(1, "in png_destroy_write_struct");
979 if (png_ptr_ptr
!= NULL
)
981 png_structrp png_ptr
= *png_ptr_ptr
;
983 if (png_ptr
!= NULL
) /* added in libpng 1.6.0 */
985 png_destroy_info_struct(png_ptr
, info_ptr_ptr
);
988 png_write_destroy(png_ptr
);
989 png_destroy_png_struct(png_ptr
);
994 /* Allow the application to select one or more row filters to use. */
996 png_set_filter(png_structrp png_ptr
, int method
, int filters
)
998 png_debug(1, "in png_set_filter");
1000 if (png_ptr
== NULL
)
1003 #ifdef PNG_MNG_FEATURES_SUPPORTED
1004 if ((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
1005 (method
== PNG_INTRAPIXEL_DIFFERENCING
))
1006 method
= PNG_FILTER_TYPE_BASE
;
1009 if (method
== PNG_FILTER_TYPE_BASE
)
1011 switch (filters
& (PNG_ALL_FILTERS
| 0x07))
1013 #ifdef PNG_WRITE_FILTER_SUPPORTED
1016 case 7: png_app_error(png_ptr
, "Unknown row filter for method 0");
1018 #endif /* PNG_WRITE_FILTER_SUPPORTED */
1019 case PNG_FILTER_VALUE_NONE
:
1020 png_ptr
->do_filter
= PNG_FILTER_NONE
; break;
1022 #ifdef PNG_WRITE_FILTER_SUPPORTED
1023 case PNG_FILTER_VALUE_SUB
:
1024 png_ptr
->do_filter
= PNG_FILTER_SUB
; break;
1026 case PNG_FILTER_VALUE_UP
:
1027 png_ptr
->do_filter
= PNG_FILTER_UP
; break;
1029 case PNG_FILTER_VALUE_AVG
:
1030 png_ptr
->do_filter
= PNG_FILTER_AVG
; break;
1032 case PNG_FILTER_VALUE_PAETH
:
1033 png_ptr
->do_filter
= PNG_FILTER_PAETH
; break;
1036 png_ptr
->do_filter
= (png_byte
)filters
; break;
1039 png_app_error(png_ptr
, "Unknown row filter for method 0");
1040 #endif /* PNG_WRITE_FILTER_SUPPORTED */
1043 /* If we have allocated the row_buf, this means we have already started
1044 * with the image and we should have allocated all of the filter buffers
1045 * that have been selected. If prev_row isn't already allocated, then
1046 * it is too late to start using the filters that need it, since we
1047 * will be missing the data in the previous row. If an application
1048 * wants to start and stop using particular filters during compression,
1049 * it should start out with all of the filters, and then add and
1050 * remove them after the start of compression.
1052 if (png_ptr
->row_buf
!= NULL
)
1054 #ifdef PNG_WRITE_FILTER_SUPPORTED
1055 if ((png_ptr
->do_filter
& PNG_FILTER_SUB
) && png_ptr
->sub_row
== NULL
)
1057 png_ptr
->sub_row
= (png_bytep
)png_malloc(png_ptr
,
1058 (png_ptr
->rowbytes
+ 1));
1059 png_ptr
->sub_row
[0] = PNG_FILTER_VALUE_SUB
;
1062 if ((png_ptr
->do_filter
& PNG_FILTER_UP
) && png_ptr
->up_row
== NULL
)
1064 if (png_ptr
->prev_row
== NULL
)
1066 png_warning(png_ptr
, "Can't add Up filter after starting");
1067 png_ptr
->do_filter
= (png_byte
)(png_ptr
->do_filter
&
1073 png_ptr
->up_row
= (png_bytep
)png_malloc(png_ptr
,
1074 (png_ptr
->rowbytes
+ 1));
1075 png_ptr
->up_row
[0] = PNG_FILTER_VALUE_UP
;
1079 if ((png_ptr
->do_filter
& PNG_FILTER_AVG
) && png_ptr
->avg_row
== NULL
)
1081 if (png_ptr
->prev_row
== NULL
)
1083 png_warning(png_ptr
, "Can't add Average filter after starting");
1084 png_ptr
->do_filter
= (png_byte
)(png_ptr
->do_filter
&
1090 png_ptr
->avg_row
= (png_bytep
)png_malloc(png_ptr
,
1091 (png_ptr
->rowbytes
+ 1));
1092 png_ptr
->avg_row
[0] = PNG_FILTER_VALUE_AVG
;
1096 if ((png_ptr
->do_filter
& PNG_FILTER_PAETH
) &&
1097 png_ptr
->paeth_row
== NULL
)
1099 if (png_ptr
->prev_row
== NULL
)
1101 png_warning(png_ptr
, "Can't add Paeth filter after starting");
1102 png_ptr
->do_filter
&= (png_byte
)(~PNG_FILTER_PAETH
);
1107 png_ptr
->paeth_row
= (png_bytep
)png_malloc(png_ptr
,
1108 (png_ptr
->rowbytes
+ 1));
1109 png_ptr
->paeth_row
[0] = PNG_FILTER_VALUE_PAETH
;
1113 if (png_ptr
->do_filter
== PNG_NO_FILTERS
)
1114 #endif /* PNG_WRITE_FILTER_SUPPORTED */
1115 png_ptr
->do_filter
= PNG_FILTER_NONE
;
1119 png_error(png_ptr
, "Unknown custom filter method");
1122 /* This allows us to influence the way in which libpng chooses the "best"
1123 * filter for the current scanline. While the "minimum-sum-of-absolute-
1124 * differences metric is relatively fast and effective, there is some
1125 * question as to whether it can be improved upon by trying to keep the
1126 * filtered data going to zlib more consistent, hopefully resulting in
1127 * better compression.
1129 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
1130 /* Convenience reset API. */
1132 png_reset_filter_heuristics(png_structrp png_ptr
)
1134 /* Clear out any old values in the 'weights' - this must be done because if
1135 * the app calls set_filter_heuristics multiple times with different
1136 * 'num_weights' values we would otherwise potentially have wrong sized
1139 png_ptr
->num_prev_filters
= 0;
1140 png_ptr
->heuristic_method
= PNG_FILTER_HEURISTIC_UNWEIGHTED
;
1141 if (png_ptr
->prev_filters
!= NULL
)
1143 png_bytep old
= png_ptr
->prev_filters
;
1144 png_ptr
->prev_filters
= NULL
;
1145 png_free(png_ptr
, old
);
1147 if (png_ptr
->filter_weights
!= NULL
)
1149 png_uint_16p old
= png_ptr
->filter_weights
;
1150 png_ptr
->filter_weights
= NULL
;
1151 png_free(png_ptr
, old
);
1154 if (png_ptr
->inv_filter_weights
!= NULL
)
1156 png_uint_16p old
= png_ptr
->inv_filter_weights
;
1157 png_ptr
->inv_filter_weights
= NULL
;
1158 png_free(png_ptr
, old
);
1161 /* Leave the filter_costs - this array is fixed size. */
1165 png_init_filter_heuristics(png_structrp png_ptr
, int heuristic_method
,
1168 if (png_ptr
== NULL
)
1171 /* Clear out the arrays */
1172 png_reset_filter_heuristics(png_ptr
);
1174 /* Check arguments; the 'reset' function makes the correct settings for the
1175 * unweighted case, but we must handle the weight case by initializing the
1176 * arrays for the caller.
1178 if (heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1182 if (num_weights
> 0)
1184 png_ptr
->prev_filters
= (png_bytep
)png_malloc(png_ptr
,
1185 (png_uint_32
)((sizeof (png_byte
)) * num_weights
));
1187 /* To make sure that the weighting starts out fairly */
1188 for (i
= 0; i
< num_weights
; i
++)
1190 png_ptr
->prev_filters
[i
] = 255;
1193 png_ptr
->filter_weights
= (png_uint_16p
)png_malloc(png_ptr
,
1194 (png_uint_32
)((sizeof (png_uint_16
)) * num_weights
));
1196 png_ptr
->inv_filter_weights
= (png_uint_16p
)png_malloc(png_ptr
,
1197 (png_uint_32
)((sizeof (png_uint_16
)) * num_weights
));
1199 for (i
= 0; i
< num_weights
; i
++)
1201 png_ptr
->inv_filter_weights
[i
] =
1202 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
1205 /* Safe to set this now */
1206 png_ptr
->num_prev_filters
= (png_byte
)num_weights
;
1209 /* If, in the future, there are other filter methods, this would
1210 * need to be based on png_ptr->filter.
1212 if (png_ptr
->filter_costs
== NULL
)
1214 png_ptr
->filter_costs
= (png_uint_16p
)png_malloc(png_ptr
,
1215 (png_uint_32
)((sizeof (png_uint_16
)) * PNG_FILTER_VALUE_LAST
));
1217 png_ptr
->inv_filter_costs
= (png_uint_16p
)png_malloc(png_ptr
,
1218 (png_uint_32
)((sizeof (png_uint_16
)) * PNG_FILTER_VALUE_LAST
));
1221 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++)
1223 png_ptr
->inv_filter_costs
[i
] =
1224 png_ptr
->filter_costs
[i
] = PNG_COST_FACTOR
;
1227 /* All the arrays are inited, safe to set this: */
1228 png_ptr
->heuristic_method
= PNG_FILTER_HEURISTIC_WEIGHTED
;
1230 /* Return the 'ok' code. */
1233 else if (heuristic_method
== PNG_FILTER_HEURISTIC_DEFAULT
||
1234 heuristic_method
== PNG_FILTER_HEURISTIC_UNWEIGHTED
)
1240 png_warning(png_ptr
, "Unknown filter heuristic method");
1245 /* Provide floating and fixed point APIs */
1246 #ifdef PNG_FLOATING_POINT_SUPPORTED
1248 png_set_filter_heuristics(png_structrp png_ptr
, int heuristic_method
,
1249 int num_weights
, png_const_doublep filter_weights
,
1250 png_const_doublep filter_costs
)
1252 png_debug(1, "in png_set_filter_heuristics");
1254 /* The internal API allocates all the arrays and ensures that the elements of
1255 * those arrays are set to the default value.
1257 if (!png_init_filter_heuristics(png_ptr
, heuristic_method
, num_weights
))
1260 /* If using the weighted method copy in the weights. */
1261 if (heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1264 for (i
= 0; i
< num_weights
; i
++)
1266 if (filter_weights
[i
] <= 0.0)
1268 png_ptr
->inv_filter_weights
[i
] =
1269 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
1274 png_ptr
->inv_filter_weights
[i
] =
1275 (png_uint_16
)(PNG_WEIGHT_FACTOR
*filter_weights
[i
]+.5);
1277 png_ptr
->filter_weights
[i
] =
1278 (png_uint_16
)(PNG_WEIGHT_FACTOR
/filter_weights
[i
]+.5);
1282 /* Here is where we set the relative costs of the different filters. We
1283 * should take the desired compression level into account when setting
1284 * the costs, so that Paeth, for instance, has a high relative cost at low
1285 * compression levels, while it has a lower relative cost at higher
1286 * compression settings. The filter types are in order of increasing
1287 * relative cost, so it would be possible to do this with an algorithm.
1289 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++) if (filter_costs
[i
] >= 1.0)
1291 png_ptr
->inv_filter_costs
[i
] =
1292 (png_uint_16
)(PNG_COST_FACTOR
/ filter_costs
[i
] + .5);
1294 png_ptr
->filter_costs
[i
] =
1295 (png_uint_16
)(PNG_COST_FACTOR
* filter_costs
[i
] + .5);
1299 #endif /* FLOATING_POINT */
1301 #ifdef PNG_FIXED_POINT_SUPPORTED
1303 png_set_filter_heuristics_fixed(png_structrp png_ptr
, int heuristic_method
,
1304 int num_weights
, png_const_fixed_point_p filter_weights
,
1305 png_const_fixed_point_p filter_costs
)
1307 png_debug(1, "in png_set_filter_heuristics_fixed");
1309 /* The internal API allocates all the arrays and ensures that the elements of
1310 * those arrays are set to the default value.
1312 if (!png_init_filter_heuristics(png_ptr
, heuristic_method
, num_weights
))
1315 /* If using the weighted method copy in the weights. */
1316 if (heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1319 for (i
= 0; i
< num_weights
; i
++)
1321 if (filter_weights
[i
] <= 0)
1323 png_ptr
->inv_filter_weights
[i
] =
1324 png_ptr
->filter_weights
[i
] = PNG_WEIGHT_FACTOR
;
1329 png_ptr
->inv_filter_weights
[i
] = (png_uint_16
)
1330 ((PNG_WEIGHT_FACTOR
*filter_weights
[i
]+PNG_FP_HALF
)/PNG_FP_1
);
1332 png_ptr
->filter_weights
[i
] = (png_uint_16
)((PNG_WEIGHT_FACTOR
*
1333 PNG_FP_1
+(filter_weights
[i
]/2))/filter_weights
[i
]);
1337 /* Here is where we set the relative costs of the different filters. We
1338 * should take the desired compression level into account when setting
1339 * the costs, so that Paeth, for instance, has a high relative cost at low
1340 * compression levels, while it has a lower relative cost at higher
1341 * compression settings. The filter types are in order of increasing
1342 * relative cost, so it would be possible to do this with an algorithm.
1344 for (i
= 0; i
< PNG_FILTER_VALUE_LAST
; i
++)
1345 if (filter_costs
[i
] >= PNG_FP_1
)
1349 /* Use a 32 bit unsigned temporary here because otherwise the
1350 * intermediate value will be a 32 bit *signed* integer (ANSI rules)
1351 * and this will get the wrong answer on division.
1353 tmp
= PNG_COST_FACTOR
*PNG_FP_1
+ (filter_costs
[i
]/2);
1354 tmp
/= filter_costs
[i
];
1356 png_ptr
->inv_filter_costs
[i
] = (png_uint_16
)tmp
;
1358 tmp
= PNG_COST_FACTOR
* filter_costs
[i
] + PNG_FP_HALF
;
1361 png_ptr
->filter_costs
[i
] = (png_uint_16
)tmp
;
1365 #endif /* FIXED_POINT */
1366 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1369 png_set_compression_level(png_structrp png_ptr
, int level
)
1371 png_debug(1, "in png_set_compression_level");
1373 if (png_ptr
== NULL
)
1376 png_ptr
->zlib_level
= level
;
1380 png_set_compression_mem_level(png_structrp png_ptr
, int mem_level
)
1382 png_debug(1, "in png_set_compression_mem_level");
1384 if (png_ptr
== NULL
)
1387 png_ptr
->zlib_mem_level
= mem_level
;
1391 png_set_compression_strategy(png_structrp png_ptr
, int strategy
)
1393 png_debug(1, "in png_set_compression_strategy");
1395 if (png_ptr
== NULL
)
1398 /* The flag setting here prevents the libpng dynamic selection of strategy.
1400 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_STRATEGY
;
1401 png_ptr
->zlib_strategy
= strategy
;
1404 /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1405 * smaller value of window_bits if it can do so safely.
1408 png_set_compression_window_bits(png_structrp png_ptr
, int window_bits
)
1410 if (png_ptr
== NULL
)
1413 /* Prior to 1.6.0 this would warn but then set the window_bits value, this
1414 * meant that negative window bits values could be selected which would cause
1415 * libpng to write a non-standard PNG file with raw deflate or gzip
1416 * compressed IDAT or ancillary chunks. Such files can be read and there is
1417 * no warning on read, so this seems like a very bad idea.
1419 if (window_bits
> 15)
1421 png_warning(png_ptr
, "Only compression windows <= 32k supported by PNG");
1425 else if (window_bits
< 8)
1427 png_warning(png_ptr
, "Only compression windows >= 256 supported by PNG");
1431 png_ptr
->zlib_window_bits
= window_bits
;
1435 png_set_compression_method(png_structrp png_ptr
, int method
)
1437 png_debug(1, "in png_set_compression_method");
1439 if (png_ptr
== NULL
)
1442 /* This would produce an invalid PNG file if it worked, but it doesn't and
1443 * deflate will fault it, so it is harmless to just warn here.
1446 png_warning(png_ptr
, "Only compression method 8 is supported by PNG");
1448 png_ptr
->zlib_method
= method
;
1451 /* The following were added to libpng-1.5.4 */
1452 #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
1454 png_set_text_compression_level(png_structrp png_ptr
, int level
)
1456 png_debug(1, "in png_set_text_compression_level");
1458 if (png_ptr
== NULL
)
1461 png_ptr
->zlib_text_level
= level
;
1465 png_set_text_compression_mem_level(png_structrp png_ptr
, int mem_level
)
1467 png_debug(1, "in png_set_text_compression_mem_level");
1469 if (png_ptr
== NULL
)
1472 png_ptr
->zlib_text_mem_level
= mem_level
;
1476 png_set_text_compression_strategy(png_structrp png_ptr
, int strategy
)
1478 png_debug(1, "in png_set_text_compression_strategy");
1480 if (png_ptr
== NULL
)
1483 png_ptr
->zlib_text_strategy
= strategy
;
1486 /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1487 * smaller value of window_bits if it can do so safely.
1490 png_set_text_compression_window_bits(png_structrp png_ptr
, int window_bits
)
1492 if (png_ptr
== NULL
)
1495 if (window_bits
> 15)
1497 png_warning(png_ptr
, "Only compression windows <= 32k supported by PNG");
1501 else if (window_bits
< 8)
1503 png_warning(png_ptr
, "Only compression windows >= 256 supported by PNG");
1507 png_ptr
->zlib_text_window_bits
= window_bits
;
1511 png_set_text_compression_method(png_structrp png_ptr
, int method
)
1513 png_debug(1, "in png_set_text_compression_method");
1515 if (png_ptr
== NULL
)
1519 png_warning(png_ptr
, "Only compression method 8 is supported by PNG");
1521 png_ptr
->zlib_text_method
= method
;
1523 #endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
1524 /* end of API added to libpng-1.5.4 */
1527 png_set_write_status_fn(png_structrp png_ptr
, png_write_status_ptr write_row_fn
)
1529 if (png_ptr
== NULL
)
1532 png_ptr
->write_row_fn
= write_row_fn
;
1535 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1537 png_set_write_user_transform_fn(png_structrp png_ptr
, png_user_transform_ptr
1538 write_user_transform_fn
)
1540 png_debug(1, "in png_set_write_user_transform_fn");
1542 if (png_ptr
== NULL
)
1545 png_ptr
->transformations
|= PNG_USER_TRANSFORM
;
1546 png_ptr
->write_user_transform_fn
= write_user_transform_fn
;
1551 #ifdef PNG_INFO_IMAGE_SUPPORTED
1553 png_write_png(png_structrp png_ptr
, png_inforp info_ptr
,
1554 int transforms
, voidp params
)
1556 if (png_ptr
== NULL
|| info_ptr
== NULL
)
1559 if ((info_ptr
->valid
& PNG_INFO_IDAT
) == 0)
1561 png_app_error(png_ptr
, "no rows for png_write_image to write");
1565 /* Write the file header information. */
1566 png_write_info(png_ptr
, info_ptr
);
1568 /* ------ these transformations don't touch the info structure ------- */
1570 /* Invert monochrome pixels */
1571 if (transforms
& PNG_TRANSFORM_INVERT_MONO
)
1572 #ifdef PNG_WRITE_INVERT_SUPPORTED
1573 png_set_invert_mono(png_ptr
);
1575 png_app_error(png_ptr
, "PNG_TRANSFORM_INVERT_MONO not supported");
1578 /* Shift the pixels up to a legal bit depth and fill in
1579 * as appropriate to correctly scale the image.
1581 if (transforms
& PNG_TRANSFORM_SHIFT
)
1582 #ifdef PNG_WRITE_SHIFT_SUPPORTED
1583 if (info_ptr
->valid
& PNG_INFO_sBIT
)
1584 png_set_shift(png_ptr
, &info_ptr
->sig_bit
);
1586 png_app_error(png_ptr
, "PNG_TRANSFORM_SHIFT not supported");
1589 /* Pack pixels into bytes */
1590 if (transforms
& PNG_TRANSFORM_PACKING
)
1591 #ifdef PNG_WRITE_PACK_SUPPORTED
1592 png_set_packing(png_ptr
);
1594 png_app_error(png_ptr
, "PNG_TRANSFORM_PACKING not supported");
1597 /* Swap location of alpha bytes from ARGB to RGBA */
1598 if (transforms
& PNG_TRANSFORM_SWAP_ALPHA
)
1599 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
1600 png_set_swap_alpha(png_ptr
);
1602 png_app_error(png_ptr
, "PNG_TRANSFORM_SWAP_ALPHA not supported");
1605 /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into
1606 * RGB, note that the code expects the input color type to be G or RGB; no
1610 (PNG_TRANSFORM_STRIP_FILLER_AFTER
|PNG_TRANSFORM_STRIP_FILLER_BEFORE
))
1612 #ifdef PNG_WRITE_FILLER_SUPPORTED
1613 if (transforms
& PNG_TRANSFORM_STRIP_FILLER_AFTER
)
1615 if (transforms
& PNG_TRANSFORM_STRIP_FILLER_BEFORE
)
1616 png_app_error(png_ptr
,
1617 "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported");
1619 /* Continue if ignored - this is the pre-1.6.10 behavior */
1620 png_set_filler(png_ptr
, 0, PNG_FILLER_AFTER
);
1623 else if (transforms
& PNG_TRANSFORM_STRIP_FILLER_BEFORE
)
1624 png_set_filler(png_ptr
, 0, PNG_FILLER_BEFORE
);
1626 png_app_error(png_ptr
, "PNG_TRANSFORM_STRIP_FILLER not supported");
1630 /* Flip BGR pixels to RGB */
1631 if (transforms
& PNG_TRANSFORM_BGR
)
1632 #ifdef PNG_WRITE_BGR_SUPPORTED
1633 png_set_bgr(png_ptr
);
1635 png_app_error(png_ptr
, "PNG_TRANSFORM_BGR not supported");
1638 /* Swap bytes of 16-bit files to most significant byte first */
1639 if (transforms
& PNG_TRANSFORM_SWAP_ENDIAN
)
1640 #ifdef PNG_WRITE_SWAP_SUPPORTED
1641 png_set_swap(png_ptr
);
1643 png_app_error(png_ptr
, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
1646 /* Swap bits of 1, 2, 4 bit packed pixel formats */
1647 if (transforms
& PNG_TRANSFORM_PACKSWAP
)
1648 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
1649 png_set_packswap(png_ptr
);
1651 png_app_error(png_ptr
, "PNG_TRANSFORM_PACKSWAP not supported");
1654 /* Invert the alpha channel from opacity to transparency */
1655 if (transforms
& PNG_TRANSFORM_INVERT_ALPHA
)
1656 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
1657 png_set_invert_alpha(png_ptr
);
1659 png_app_error(png_ptr
, "PNG_TRANSFORM_INVERT_ALPHA not supported");
1662 /* ----------------------- end of transformations ------------------- */
1664 /* Write the bits */
1665 png_write_image(png_ptr
, info_ptr
->row_pointers
);
1667 /* It is REQUIRED to call this to finish writing the rest of the file */
1668 png_write_end(png_ptr
, info_ptr
);
1675 #ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
1676 #ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */
1677 /* Initialize the write structure - general purpose utility. */
1679 png_image_write_init(png_imagep image
)
1681 png_structp png_ptr
= png_create_write_struct(PNG_LIBPNG_VER_STRING
, image
,
1682 png_safe_error
, png_safe_warning
);
1684 if (png_ptr
!= NULL
)
1686 png_infop info_ptr
= png_create_info_struct(png_ptr
);
1688 if (info_ptr
!= NULL
)
1690 png_controlp control
= png_voidcast(png_controlp
,
1691 png_malloc_warn(png_ptr
, (sizeof *control
)));
1693 if (control
!= NULL
)
1695 memset(control
, 0, (sizeof *control
));
1697 control
->png_ptr
= png_ptr
;
1698 control
->info_ptr
= info_ptr
;
1699 control
->for_write
= 1;
1701 image
->opaque
= control
;
1705 /* Error clean up */
1706 png_destroy_info_struct(png_ptr
, &info_ptr
);
1709 png_destroy_write_struct(&png_ptr
, NULL
);
1712 return png_image_error(image
, "png_image_write_: out of memory");
1715 /* Arguments to png_image_write_main: */
1720 png_const_voidp buffer
;
1721 png_int_32 row_stride
;
1722 png_const_voidp colormap
;
1723 int convert_to_8bit
;
1724 /* Local variables: */
1725 png_const_voidp first_row
;
1726 ptrdiff_t row_bytes
;
1727 png_voidp local_row
;
1728 } png_image_write_control
;
1730 /* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
1731 * do any necessary byte swapping. The component order is defined by the
1732 * png_image format value.
1735 png_write_image_16bit(png_voidp argument
)
1737 png_image_write_control
*display
= png_voidcast(png_image_write_control
*,
1739 png_imagep image
= display
->image
;
1740 png_structrp png_ptr
= image
->opaque
->png_ptr
;
1742 png_const_uint_16p input_row
= png_voidcast(png_const_uint_16p
,
1743 display
->first_row
);
1744 png_uint_16p output_row
= png_voidcast(png_uint_16p
, display
->local_row
);
1745 png_uint_16p row_end
;
1746 const int channels
= (image
->format
& PNG_FORMAT_FLAG_COLOR
) ? 3 : 1;
1748 png_uint_32 y
= image
->height
;
1750 if (image
->format
& PNG_FORMAT_FLAG_ALPHA
)
1752 # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
1753 if (image
->format
& PNG_FORMAT_FLAG_AFIRST
)
1756 ++input_row
; /* To point to the first component */
1766 png_error(png_ptr
, "png_write_image: internal call error");
1768 /* Work out the output row end and count over this, note that the increment
1769 * above to 'row' means that row_end can actually be beyond the end of the
1770 * row; this is correct.
1772 row_end
= output_row
+ image
->width
* (channels
+1);
1776 png_const_uint_16p in_ptr
= input_row
;
1777 png_uint_16p out_ptr
= output_row
;
1779 while (out_ptr
< row_end
)
1781 const png_uint_16 alpha
= in_ptr
[aindex
];
1782 png_uint_32 reciprocal
= 0;
1785 out_ptr
[aindex
] = alpha
;
1787 /* Calculate a reciprocal. The correct calculation is simply
1788 * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
1789 * allows correct rounding by adding .5 before the shift. 'reciprocal'
1790 * is only initialized when required.
1792 if (alpha
> 0 && alpha
< 65535)
1793 reciprocal
= ((0xffff<<15)+(alpha
>>1))/alpha
;
1796 do /* always at least one channel */
1798 png_uint_16 component
= *in_ptr
++;
1800 /* The following gives 65535 for an alpha of 0, which is fine,
1801 * otherwise if 0/0 is represented as some other value there is more
1802 * likely to be a discontinuity which will probably damage
1803 * compression when moving from a fully transparent area to a
1804 * nearly transparent one. (The assumption here is that opaque
1805 * areas tend not to be 0 intensity.)
1807 if (component
>= alpha
)
1810 /* component<alpha, so component/alpha is less than one and
1811 * component*reciprocal is less than 2^31.
1813 else if (component
> 0 && alpha
< 65535)
1815 png_uint_32 calc
= component
* reciprocal
;
1816 calc
+= 16384; /* round to nearest */
1817 component
= (png_uint_16
)(calc
>> 15);
1820 *out_ptr
++ = component
;
1824 /* Skip to next component (skip the intervening alpha channel) */
1829 png_write_row(png_ptr
, png_voidcast(png_const_bytep
, display
->local_row
));
1830 input_row
+= display
->row_bytes
/(sizeof (png_uint_16
));
1836 /* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel
1837 * is present it must be removed from the components, the components are then
1838 * written in sRGB encoding. No components are added or removed.
1840 * Calculate an alpha reciprocal to reverse pre-multiplication. As above the
1841 * calculation can be done to 15 bits of accuracy; however, the output needs to
1842 * be scaled in the range 0..255*65535, so include that scaling here.
1844 #define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+(alpha>>1))/alpha)
1847 png_unpremultiply(png_uint_32 component
, png_uint_32 alpha
,
1848 png_uint_32 reciprocal
/*from the above macro*/)
1850 /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
1851 * is represented as some other value there is more likely to be a
1852 * discontinuity which will probably damage compression when moving from a
1853 * fully transparent area to a nearly transparent one. (The assumption here
1854 * is that opaque areas tend not to be 0 intensity.)
1856 * There is a rounding problem here; if alpha is less than 128 it will end up
1857 * as 0 when scaled to 8 bits. To avoid introducing spurious colors into the
1858 * output change for this too.
1860 if (component
>= alpha
|| alpha
< 128)
1863 /* component<alpha, so component/alpha is less than one and
1864 * component*reciprocal is less than 2^31.
1866 else if (component
> 0)
1868 /* The test is that alpha/257 (rounded) is less than 255, the first value
1869 * that becomes 255 is 65407.
1870 * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
1871 * be exact!) [Could also test reciprocal != 0]
1875 component
*= reciprocal
;
1876 component
+= 64; /* round to nearest */
1883 /* Convert the component to sRGB. */
1884 return (png_byte
)PNG_sRGB_FROM_LINEAR(component
);
1892 png_write_image_8bit(png_voidp argument
)
1894 png_image_write_control
*display
= png_voidcast(png_image_write_control
*,
1896 png_imagep image
= display
->image
;
1897 png_structrp png_ptr
= image
->opaque
->png_ptr
;
1899 png_const_uint_16p input_row
= png_voidcast(png_const_uint_16p
,
1900 display
->first_row
);
1901 png_bytep output_row
= png_voidcast(png_bytep
, display
->local_row
);
1902 png_uint_32 y
= image
->height
;
1903 const int channels
= (image
->format
& PNG_FORMAT_FLAG_COLOR
) ? 3 : 1;
1905 if (image
->format
& PNG_FORMAT_FLAG_ALPHA
)
1910 # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
1911 if (image
->format
& PNG_FORMAT_FLAG_AFIRST
)
1914 ++input_row
; /* To point to the first component */
1922 /* Use row_end in place of a loop counter: */
1923 row_end
= output_row
+ image
->width
* (channels
+1);
1927 png_const_uint_16p in_ptr
= input_row
;
1928 png_bytep out_ptr
= output_row
;
1930 while (out_ptr
< row_end
)
1932 png_uint_16 alpha
= in_ptr
[aindex
];
1933 png_byte alphabyte
= (png_byte
)PNG_DIV257(alpha
);
1934 png_uint_32 reciprocal
= 0;
1937 /* Scale and write the alpha channel. */
1938 out_ptr
[aindex
] = alphabyte
;
1940 if (alphabyte
> 0 && alphabyte
< 255)
1941 reciprocal
= UNP_RECIPROCAL(alpha
);
1944 do /* always at least one channel */
1945 *out_ptr
++ = png_unpremultiply(*in_ptr
++, alpha
, reciprocal
);
1948 /* Skip to next component (skip the intervening alpha channel) */
1951 } /* while out_ptr < row_end */
1953 png_write_row(png_ptr
, png_voidcast(png_const_bytep
,
1954 display
->local_row
));
1955 input_row
+= display
->row_bytes
/(sizeof (png_uint_16
));
1961 /* No alpha channel, so the row_end really is the end of the row and it
1962 * is sufficient to loop over the components one by one.
1964 png_bytep row_end
= output_row
+ image
->width
* channels
;
1968 png_const_uint_16p in_ptr
= input_row
;
1969 png_bytep out_ptr
= output_row
;
1971 while (out_ptr
< row_end
)
1973 png_uint_32 component
= *in_ptr
++;
1976 *out_ptr
++ = (png_byte
)PNG_sRGB_FROM_LINEAR(component
);
1979 png_write_row(png_ptr
, output_row
);
1980 input_row
+= display
->row_bytes
/(sizeof (png_uint_16
));
1988 png_image_set_PLTE(png_image_write_control
*display
)
1990 const png_imagep image
= display
->image
;
1991 const void *cmap
= display
->colormap
;
1992 const int entries
= image
->colormap_entries
> 256 ? 256 :
1993 (int)image
->colormap_entries
;
1995 /* NOTE: the caller must check for cmap != NULL and entries != 0 */
1996 const png_uint_32 format
= image
->format
;
1997 const int channels
= PNG_IMAGE_SAMPLE_CHANNELS(format
);
1999 # if defined(PNG_FORMAT_BGR_SUPPORTED) &&\
2000 defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)
2001 const int afirst
= (format
& PNG_FORMAT_FLAG_AFIRST
) != 0 &&
2002 (format
& PNG_FORMAT_FLAG_ALPHA
) != 0;
2007 # ifdef PNG_FORMAT_BGR_SUPPORTED
2008 const int bgr
= (format
& PNG_FORMAT_FLAG_BGR
) ? 2 : 0;
2014 png_color palette
[256];
2017 memset(tRNS
, 255, (sizeof tRNS
));
2018 memset(palette
, 0, (sizeof palette
));
2020 for (i
=num_trans
=0; i
<entries
; ++i
)
2022 /* This gets automatically converted to sRGB with reversal of the
2023 * pre-multiplication if the color-map has an alpha channel.
2025 if (format
& PNG_FORMAT_FLAG_LINEAR
)
2027 png_const_uint_16p entry
= png_voidcast(png_const_uint_16p
, cmap
);
2029 entry
+= i
* channels
;
2031 if (channels
& 1) /* no alpha */
2033 if (channels
>= 3) /* RGB */
2035 palette
[i
].blue
= (png_byte
)PNG_sRGB_FROM_LINEAR(255 *
2037 palette
[i
].green
= (png_byte
)PNG_sRGB_FROM_LINEAR(255 *
2039 palette
[i
].red
= (png_byte
)PNG_sRGB_FROM_LINEAR(255 *
2044 palette
[i
].blue
= palette
[i
].red
= palette
[i
].green
=
2045 (png_byte
)PNG_sRGB_FROM_LINEAR(255 * *entry
);
2050 png_uint_16 alpha
= entry
[afirst
? 0 : channels
-1];
2051 png_byte alphabyte
= (png_byte
)PNG_DIV257(alpha
);
2052 png_uint_32 reciprocal
= 0;
2054 /* Calculate a reciprocal, as in the png_write_image_8bit code above
2055 * this is designed to produce a value scaled to 255*65535 when
2056 * divided by 128 (i.e. asr 7).
2058 if (alphabyte
> 0 && alphabyte
< 255)
2059 reciprocal
= (((0xffff*0xff)<<7)+(alpha
>>1))/alpha
;
2061 tRNS
[i
] = alphabyte
;
2062 if (alphabyte
< 255)
2065 if (channels
>= 3) /* RGB */
2067 palette
[i
].blue
= png_unpremultiply(entry
[afirst
+ (2 ^ bgr
)],
2069 palette
[i
].green
= png_unpremultiply(entry
[afirst
+ 1], alpha
,
2071 palette
[i
].red
= png_unpremultiply(entry
[afirst
+ bgr
], alpha
,
2076 palette
[i
].blue
= palette
[i
].red
= palette
[i
].green
=
2077 png_unpremultiply(entry
[afirst
], alpha
, reciprocal
);
2081 else /* Color-map has sRGB values */
2083 png_const_bytep entry
= png_voidcast(png_const_bytep
, cmap
);
2085 entry
+= i
* channels
;
2090 tRNS
[i
] = entry
[afirst
? 0 : 3];
2095 palette
[i
].blue
= entry
[afirst
+ (2 ^ bgr
)];
2096 palette
[i
].green
= entry
[afirst
+ 1];
2097 palette
[i
].red
= entry
[afirst
+ bgr
];
2101 tRNS
[i
] = entry
[1 ^ afirst
];
2106 palette
[i
].blue
= palette
[i
].red
= palette
[i
].green
=
2123 png_set_PLTE(image
->opaque
->png_ptr
, image
->opaque
->info_ptr
, palette
,
2127 png_set_tRNS(image
->opaque
->png_ptr
, image
->opaque
->info_ptr
, tRNS
,
2130 image
->colormap_entries
= entries
;
2134 png_image_write_main(png_voidp argument
)
2136 png_image_write_control
*display
= png_voidcast(png_image_write_control
*,
2138 png_imagep image
= display
->image
;
2139 png_structrp png_ptr
= image
->opaque
->png_ptr
;
2140 png_inforp info_ptr
= image
->opaque
->info_ptr
;
2141 png_uint_32 format
= image
->format
;
2143 int colormap
= (format
& PNG_FORMAT_FLAG_COLORMAP
) != 0;
2144 int linear
= !colormap
&& (format
& PNG_FORMAT_FLAG_LINEAR
) != 0; /* input */
2145 int alpha
= !colormap
&& (format
& PNG_FORMAT_FLAG_ALPHA
) != 0;
2146 int write_16bit
= linear
&& !colormap
&& !display
->convert_to_8bit
;
2148 # ifdef PNG_BENIGN_ERRORS_SUPPORTED
2149 /* Make sure we error out on any bad situation */
2150 png_set_benign_errors(png_ptr
, 0/*error*/);
2153 /* Default the 'row_stride' parameter if required. */
2154 if (display
->row_stride
== 0)
2155 display
->row_stride
= PNG_IMAGE_ROW_STRIDE(*image
);
2157 /* Set the required transforms then write the rows in the correct order. */
2158 if (format
& PNG_FORMAT_FLAG_COLORMAP
)
2160 if (display
->colormap
!= NULL
&& image
->colormap_entries
> 0)
2162 png_uint_32 entries
= image
->colormap_entries
;
2164 png_set_IHDR(png_ptr
, info_ptr
, image
->width
, image
->height
,
2165 entries
> 16 ? 8 : (entries
> 4 ? 4 : (entries
> 2 ? 2 : 1)),
2166 PNG_COLOR_TYPE_PALETTE
, PNG_INTERLACE_NONE
,
2167 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
2169 png_image_set_PLTE(display
);
2173 png_error(image
->opaque
->png_ptr
,
2174 "no color-map for color-mapped image");
2178 png_set_IHDR(png_ptr
, info_ptr
, image
->width
, image
->height
,
2179 write_16bit
? 16 : 8,
2180 ((format
& PNG_FORMAT_FLAG_COLOR
) ? PNG_COLOR_MASK_COLOR
: 0) +
2181 ((format
& PNG_FORMAT_FLAG_ALPHA
) ? PNG_COLOR_MASK_ALPHA
: 0),
2182 PNG_INTERLACE_NONE
, PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
2184 /* Counter-intuitively the data transformations must be called *after*
2185 * png_write_info, not before as in the read code, but the 'set' functions
2186 * must still be called before. Just set the color space information, never
2187 * write an interlaced image.
2190 if (write_16bit
!= 0)
2192 /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
2193 png_set_gAMA_fixed(png_ptr
, info_ptr
, PNG_GAMMA_LINEAR
);
2195 if (!(image
->flags
& PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB
))
2196 png_set_cHRM_fixed(png_ptr
, info_ptr
,
2198 /* white */ 31270, 32900,
2199 /* red */ 64000, 33000,
2200 /* green */ 30000, 60000,
2201 /* blue */ 15000, 6000
2205 else if (!(image
->flags
& PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB
))
2206 png_set_sRGB(png_ptr
, info_ptr
, PNG_sRGB_INTENT_PERCEPTUAL
);
2208 /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
2209 * space must still be gamma encoded.
2212 png_set_gAMA_fixed(png_ptr
, info_ptr
, PNG_GAMMA_sRGB_INVERSE
);
2214 /* Write the file header. */
2215 png_write_info(png_ptr
, info_ptr
);
2217 /* Now set up the data transformations (*after* the header is written),
2218 * remove the handled transformations from the 'format' flags for checking.
2220 * First check for a little endian system if writing 16 bit files.
2222 if (write_16bit
!= 0)
2224 PNG_CONST png_uint_16 le
= 0x0001;
2226 if (*(png_const_bytep
)&le
)
2227 png_set_swap(png_ptr
);
2230 # ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
2231 if (format
& PNG_FORMAT_FLAG_BGR
)
2233 if (!colormap
&& (format
& PNG_FORMAT_FLAG_COLOR
) != 0)
2234 png_set_bgr(png_ptr
);
2235 format
&= ~PNG_FORMAT_FLAG_BGR
;
2239 # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
2240 if (format
& PNG_FORMAT_FLAG_AFIRST
)
2242 if (!colormap
&& (format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2243 png_set_swap_alpha(png_ptr
);
2244 format
&= ~PNG_FORMAT_FLAG_AFIRST
;
2248 /* If there are 16 or fewer color-map entries we wrote a lower bit depth
2249 * above, but the application data is still byte packed.
2251 if (colormap
&& image
->colormap_entries
<= 16)
2252 png_set_packing(png_ptr
);
2254 /* That should have handled all (both) the transforms. */
2255 if ((format
& ~(png_uint_32
)(PNG_FORMAT_FLAG_COLOR
| PNG_FORMAT_FLAG_LINEAR
|
2256 PNG_FORMAT_FLAG_ALPHA
| PNG_FORMAT_FLAG_COLORMAP
)) != 0)
2257 png_error(png_ptr
, "png_write_image: unsupported transformation");
2260 png_const_bytep row
= png_voidcast(png_const_bytep
, display
->buffer
);
2261 ptrdiff_t row_bytes
= display
->row_stride
;
2264 row_bytes
*= (sizeof (png_uint_16
));
2267 row
+= (image
->height
-1) * (-row_bytes
);
2269 display
->first_row
= row
;
2270 display
->row_bytes
= row_bytes
;
2273 /* Apply 'fast' options if the flag is set. */
2274 if ((image
->flags
& PNG_IMAGE_FLAG_FAST
) != 0)
2276 png_set_filter(png_ptr
, PNG_FILTER_TYPE_BASE
, PNG_NO_FILTERS
);
2277 /* NOTE: determined by experiment using pngstest, this reflects some
2278 * balance between the time to write the image once and the time to read
2279 * it about 50 times. The speed-up in pngstest was about 10-20% of the
2280 * total (user) time on a heavily loaded system.
2282 png_set_compression_level(png_ptr
, 3);
2285 /* Check for the cases that currently require a pre-transform on the row
2286 * before it is written. This only applies when the input is 16-bit and
2287 * either there is an alpha channel or it is converted to 8-bit.
2289 if ((linear
&& alpha
) || (!colormap
&& display
->convert_to_8bit
))
2291 png_bytep row
= png_voidcast(png_bytep
, png_malloc(png_ptr
,
2292 png_get_rowbytes(png_ptr
, info_ptr
)));
2295 display
->local_row
= row
;
2296 if (write_16bit
!= 0)
2297 result
= png_safe_execute(image
, png_write_image_16bit
, display
);
2299 result
= png_safe_execute(image
, png_write_image_8bit
, display
);
2300 display
->local_row
= NULL
;
2302 png_free(png_ptr
, row
);
2304 /* Skip the 'write_end' on error: */
2309 /* Otherwise this is the case where the input is in a format currently
2310 * supported by the rest of the libpng write code; call it directly.
2314 png_const_bytep row
= png_voidcast(png_const_bytep
, display
->first_row
);
2315 ptrdiff_t row_bytes
= display
->row_bytes
;
2316 png_uint_32 y
= image
->height
;
2320 png_write_row(png_ptr
, row
);
2325 png_write_end(png_ptr
, info_ptr
);
2330 png_image_write_to_stdio(png_imagep image
, FILE *file
, int convert_to_8bit
,
2331 const void *buffer
, png_int_32 row_stride
, const void *colormap
)
2333 /* Write the image to the given (FILE*). */
2334 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
2338 if (png_image_write_init(image
))
2340 png_image_write_control display
;
2343 /* This is slightly evil, but png_init_io doesn't do anything other
2344 * than this and we haven't changed the standard IO functions so
2345 * this saves a 'safe' function.
2347 image
->opaque
->png_ptr
->io_ptr
= file
;
2349 memset(&display
, 0, (sizeof display
));
2350 display
.image
= image
;
2351 display
.buffer
= buffer
;
2352 display
.row_stride
= row_stride
;
2353 display
.colormap
= colormap
;
2354 display
.convert_to_8bit
= convert_to_8bit
;
2356 result
= png_safe_execute(image
, png_image_write_main
, &display
);
2357 png_image_free(image
);
2366 return png_image_error(image
,
2367 "png_image_write_to_stdio: invalid argument");
2370 else if (image
!= NULL
)
2371 return png_image_error(image
,
2372 "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION");
2379 png_image_write_to_file(png_imagep image
, const char *file_name
,
2380 int convert_to_8bit
, const void *buffer
, png_int_32 row_stride
,
2381 const void *colormap
)
2383 /* Write the image to the named file. */
2384 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
2386 if (file_name
!= NULL
)
2388 FILE *fp
= fopen(file_name
, "wb");
2392 if (png_image_write_to_stdio(image
, fp
, convert_to_8bit
, buffer
,
2393 row_stride
, colormap
))
2395 int error
; /* from fflush/fclose */
2397 /* Make sure the file is flushed correctly. */
2398 if (fflush(fp
) == 0 && ferror(fp
) == 0)
2400 if (fclose(fp
) == 0)
2403 error
= errno
; /* from fclose */
2408 error
= errno
; /* from fflush or ferror */
2412 (void)remove(file_name
);
2413 /* The image has already been cleaned up; this is just used to
2414 * set the error (because the original write succeeded).
2416 return png_image_error(image
, strerror(error
));
2421 /* Clean up: just the opened file. */
2423 (void)remove(file_name
);
2429 return png_image_error(image
, strerror(errno
));
2433 return png_image_error(image
,
2434 "png_image_write_to_file: invalid argument");
2437 else if (image
!= NULL
)
2438 return png_image_error(image
,
2439 "png_image_write_to_file: incorrect PNG_IMAGE_VERSION");
2444 #endif /* PNG_STDIO_SUPPORTED */
2445 #endif /* SIMPLIFIED_WRITE */
2447 #ifdef PNG_WRITE_APNG_SUPPORTED
2449 png_write_frame_head(png_structp png_ptr
, png_infop info_ptr
,
2450 png_bytepp row_pointers
, png_uint_32 width
, png_uint_32 height
,
2451 png_uint_32 x_offset
, png_uint_32 y_offset
,
2452 png_uint_16 delay_num
, png_uint_16 delay_den
, png_byte dispose_op
,
2455 png_debug(1, "in png_write_frame_head");
2457 /* there is a chance this has been set after png_write_info was called,
2458 * so it would be set but not written. is there a way to be sure? */
2459 if (!(info_ptr
->valid
& PNG_INFO_acTL
))
2460 png_error(png_ptr
, "png_write_frame_head(): acTL not set");
2462 png_write_reset(png_ptr
);
2464 png_write_reinit(png_ptr
, info_ptr
, width
, height
);
2466 if ( !(png_ptr
->num_frames_written
== 0 &&
2467 (png_ptr
->apng_flags
& PNG_FIRST_FRAME_HIDDEN
) ) )
2468 png_write_fcTL(png_ptr
, width
, height
, x_offset
, y_offset
,
2469 delay_num
, delay_den
, dispose_op
, blend_op
);
2471 PNG_UNUSED(row_pointers
)
2475 png_write_frame_tail(png_structp png_ptr
, png_infop info_ptr
)
2477 png_debug(1, "in png_write_frame_tail");
2479 png_ptr
->num_frames_written
++;
2481 PNG_UNUSED(info_ptr
)
2483 #endif /* PNG_WRITE_APNG_SUPPORTED */
2484 #endif /* PNG_WRITE_SUPPORTED */