2 /* pngwrite.c - general routines to write a PNG file
4 * Last changed in libpng 1.6.26 [October 20, 2016]
5 * Copyright (c) 1998-2002,2004,2006-2016 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 #ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
17 #endif /* SIMPLIFIED_WRITE_STDIO */
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
!= 0)
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
) != 0)
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 /* WRITE_UNKNOWN_CHUNKS */
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
) == 0)
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
) != 0 && \
98 png_ptr
->mng_features_permitted
!= 0)
101 "MNG features are not allowed in a PNG datastream");
102 png_ptr
->mng_features_permitted
= 0;
106 /* Write IHDR information. */
107 png_write_IHDR(png_ptr
, info_ptr
->width
, info_ptr
->height
,
108 info_ptr
->bit_depth
, info_ptr
->color_type
, info_ptr
->compression_type
,
109 info_ptr
->filter_type
,
110 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
111 info_ptr
->interlace_type
117 /* The rest of these check to see if the valid field has the appropriate
118 * flag set, and if it does, writes the chunk.
120 * 1.6.0: COLORSPACE support controls the writing of these chunks too, and
121 * the chunks will be written if the WRITE routine is there and
122 * information * is available in the COLORSPACE. (See
123 * png_colorspace_sync_info in png.c for where the valid flags get set.)
125 * Under certain circumstances the colorspace can be invalidated without
126 * syncing the info_struct 'valid' flags; this happens if libpng detects
127 * an error and calls png_error while the color space is being set, yet
128 * the application continues writing the PNG. So check the 'invalid'
131 #ifdef PNG_GAMMA_SUPPORTED
132 # ifdef PNG_WRITE_gAMA_SUPPORTED
133 if ((info_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
) == 0 &&
134 (info_ptr
->colorspace
.flags
& PNG_COLORSPACE_FROM_gAMA
) != 0 &&
135 (info_ptr
->valid
& PNG_INFO_gAMA
) != 0)
136 png_write_gAMA_fixed(png_ptr
, info_ptr
->colorspace
.gamma
);
140 #ifdef PNG_COLORSPACE_SUPPORTED
141 /* Write only one of sRGB or an ICC profile. If a profile was supplied
142 * and it matches one of the known sRGB ones issue a warning.
144 # ifdef PNG_WRITE_iCCP_SUPPORTED
145 if ((info_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
) == 0 &&
146 (info_ptr
->valid
& PNG_INFO_iCCP
) != 0)
148 # ifdef PNG_WRITE_sRGB_SUPPORTED
149 if ((info_ptr
->valid
& PNG_INFO_sRGB
) != 0)
150 png_app_warning(png_ptr
,
151 "profile matches sRGB but writing iCCP instead");
154 png_write_iCCP(png_ptr
, info_ptr
->iccp_name
,
155 info_ptr
->iccp_profile
);
157 # ifdef PNG_WRITE_sRGB_SUPPORTED
162 # ifdef PNG_WRITE_sRGB_SUPPORTED
163 if ((info_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
) == 0 &&
164 (info_ptr
->valid
& PNG_INFO_sRGB
) != 0)
165 png_write_sRGB(png_ptr
, info_ptr
->colorspace
.rendering_intent
);
166 # endif /* WRITE_sRGB */
167 #endif /* COLORSPACE */
169 #ifdef PNG_WRITE_sBIT_SUPPORTED
170 if ((info_ptr
->valid
& PNG_INFO_sBIT
) != 0)
171 png_write_sBIT(png_ptr
, &(info_ptr
->sig_bit
), info_ptr
->color_type
);
174 #ifdef PNG_COLORSPACE_SUPPORTED
175 # ifdef PNG_WRITE_cHRM_SUPPORTED
176 if ((info_ptr
->colorspace
.flags
& PNG_COLORSPACE_INVALID
) == 0 &&
177 (info_ptr
->colorspace
.flags
& PNG_COLORSPACE_FROM_cHRM
) != 0 &&
178 (info_ptr
->valid
& PNG_INFO_cHRM
) != 0)
179 png_write_cHRM_fixed(png_ptr
, &info_ptr
->colorspace
.end_points_xy
);
183 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
184 write_unknown_chunks(png_ptr
, info_ptr
, PNG_HAVE_IHDR
);
187 png_ptr
->mode
|= PNG_WROTE_INFO_BEFORE_PLTE
;
192 png_write_info(png_structrp png_ptr
, png_const_inforp info_ptr
)
194 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
198 png_debug(1, "in png_write_info");
200 if (png_ptr
== NULL
|| info_ptr
== NULL
)
203 png_write_info_before_PLTE(png_ptr
, info_ptr
);
205 if ((info_ptr
->valid
& PNG_INFO_PLTE
) != 0)
206 png_write_PLTE(png_ptr
, info_ptr
->palette
,
207 (png_uint_32
)info_ptr
->num_palette
);
209 else if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
210 png_error(png_ptr
, "Valid palette required for paletted images");
212 #ifdef PNG_WRITE_tRNS_SUPPORTED
213 if ((info_ptr
->valid
& PNG_INFO_tRNS
) !=0)
215 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
216 /* Invert the alpha channel (in tRNS) */
217 if ((png_ptr
->transformations
& PNG_INVERT_ALPHA
) != 0 &&
218 info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
222 jend
= info_ptr
->num_trans
;
223 if (jend
> PNG_MAX_PALETTE_LENGTH
)
224 jend
= PNG_MAX_PALETTE_LENGTH
;
226 for (j
= 0; j
<jend
; ++j
)
227 info_ptr
->trans_alpha
[j
] =
228 (png_byte
)(255 - info_ptr
->trans_alpha
[j
]);
231 png_write_tRNS(png_ptr
, info_ptr
->trans_alpha
, &(info_ptr
->trans_color
),
232 info_ptr
->num_trans
, info_ptr
->color_type
);
235 #ifdef PNG_WRITE_bKGD_SUPPORTED
236 if ((info_ptr
->valid
& PNG_INFO_bKGD
) != 0)
237 png_write_bKGD(png_ptr
, &(info_ptr
->background
), info_ptr
->color_type
);
240 #ifdef PNG_WRITE_hIST_SUPPORTED
241 if ((info_ptr
->valid
& PNG_INFO_hIST
) != 0)
242 png_write_hIST(png_ptr
, info_ptr
->hist
, info_ptr
->num_palette
);
245 #ifdef PNG_WRITE_oFFs_SUPPORTED
246 if ((info_ptr
->valid
& PNG_INFO_oFFs
) != 0)
247 png_write_oFFs(png_ptr
, info_ptr
->x_offset
, info_ptr
->y_offset
,
248 info_ptr
->offset_unit_type
);
251 #ifdef PNG_WRITE_pCAL_SUPPORTED
252 if ((info_ptr
->valid
& PNG_INFO_pCAL
) != 0)
253 png_write_pCAL(png_ptr
, info_ptr
->pcal_purpose
, info_ptr
->pcal_X0
,
254 info_ptr
->pcal_X1
, info_ptr
->pcal_type
, info_ptr
->pcal_nparams
,
255 info_ptr
->pcal_units
, info_ptr
->pcal_params
);
258 #ifdef PNG_WRITE_sCAL_SUPPORTED
259 if ((info_ptr
->valid
& PNG_INFO_sCAL
) != 0)
260 png_write_sCAL_s(png_ptr
, (int)info_ptr
->scal_unit
,
261 info_ptr
->scal_s_width
, info_ptr
->scal_s_height
);
264 #ifdef PNG_WRITE_pHYs_SUPPORTED
265 if ((info_ptr
->valid
& PNG_INFO_pHYs
) != 0)
266 png_write_pHYs(png_ptr
, info_ptr
->x_pixels_per_unit
,
267 info_ptr
->y_pixels_per_unit
, info_ptr
->phys_unit_type
);
270 #ifdef PNG_WRITE_tIME_SUPPORTED
271 if ((info_ptr
->valid
& PNG_INFO_tIME
) != 0)
273 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
274 png_ptr
->mode
|= PNG_WROTE_tIME
;
278 #ifdef PNG_WRITE_sPLT_SUPPORTED
279 if ((info_ptr
->valid
& PNG_INFO_sPLT
) != 0)
280 for (i
= 0; i
< (int)info_ptr
->splt_palettes_num
; i
++)
281 png_write_sPLT(png_ptr
, info_ptr
->splt_palettes
+ i
);
284 #ifdef PNG_WRITE_TEXT_SUPPORTED
285 /* Check to see if we need to write text chunks */
286 for (i
= 0; i
< info_ptr
->num_text
; i
++)
288 png_debug2(2, "Writing header text chunk %d, type %d", i
,
289 info_ptr
->text
[i
].compression
);
290 /* An internationalized chunk? */
291 if (info_ptr
->text
[i
].compression
> 0)
293 #ifdef PNG_WRITE_iTXt_SUPPORTED
294 /* Write international chunk */
295 png_write_iTXt(png_ptr
,
296 info_ptr
->text
[i
].compression
,
297 info_ptr
->text
[i
].key
,
298 info_ptr
->text
[i
].lang
,
299 info_ptr
->text
[i
].lang_key
,
300 info_ptr
->text
[i
].text
);
301 /* Mark this chunk as written */
302 if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
303 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
305 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
307 png_warning(png_ptr
, "Unable to write international text");
311 /* If we want a compressed text chunk */
312 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_zTXt
)
314 #ifdef PNG_WRITE_zTXt_SUPPORTED
315 /* Write compressed chunk */
316 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
317 info_ptr
->text
[i
].text
, info_ptr
->text
[i
].compression
);
318 /* Mark this chunk as written */
319 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
321 png_warning(png_ptr
, "Unable to write compressed text");
325 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
327 #ifdef PNG_WRITE_tEXt_SUPPORTED
328 /* Write uncompressed chunk */
329 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
330 info_ptr
->text
[i
].text
,
332 /* Mark this chunk as written */
333 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
336 png_warning(png_ptr
, "Unable to write uncompressed text");
342 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
343 write_unknown_chunks(png_ptr
, info_ptr
, PNG_HAVE_PLTE
);
347 /* Writes the end of the PNG file. If you don't want to write comments or
348 * time information, you can pass NULL for info. If you already wrote these
349 * in png_write_info(), do not write them again here. If you have long
350 * comments, I suggest writing them here, and compressing them.
353 png_write_end(png_structrp png_ptr
, png_inforp info_ptr
)
355 png_debug(1, "in png_write_end");
360 if ((png_ptr
->mode
& PNG_HAVE_IDAT
) == 0)
361 png_error(png_ptr
, "No IDATs written into file");
363 #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
364 if (png_ptr
->num_palette_max
> png_ptr
->num_palette
)
365 png_benign_error(png_ptr
, "Wrote palette index exceeding num_palette");
368 /* See if user wants us to write information chunks */
369 if (info_ptr
!= NULL
)
371 #ifdef PNG_WRITE_TEXT_SUPPORTED
372 int i
; /* local index variable */
374 #ifdef PNG_WRITE_tIME_SUPPORTED
375 /* Check to see if user has supplied a time chunk */
376 if ((info_ptr
->valid
& PNG_INFO_tIME
) != 0 &&
377 (png_ptr
->mode
& PNG_WROTE_tIME
) == 0)
378 png_write_tIME(png_ptr
, &(info_ptr
->mod_time
));
381 #ifdef PNG_WRITE_TEXT_SUPPORTED
382 /* Loop through comment chunks */
383 for (i
= 0; i
< info_ptr
->num_text
; i
++)
385 png_debug2(2, "Writing trailer text chunk %d, type %d", i
,
386 info_ptr
->text
[i
].compression
);
387 /* An internationalized chunk? */
388 if (info_ptr
->text
[i
].compression
> 0)
390 #ifdef PNG_WRITE_iTXt_SUPPORTED
391 /* Write international chunk */
392 png_write_iTXt(png_ptr
,
393 info_ptr
->text
[i
].compression
,
394 info_ptr
->text
[i
].key
,
395 info_ptr
->text
[i
].lang
,
396 info_ptr
->text
[i
].lang_key
,
397 info_ptr
->text
[i
].text
);
398 /* Mark this chunk as written */
399 if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
400 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
402 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
404 png_warning(png_ptr
, "Unable to write international text");
408 else if (info_ptr
->text
[i
].compression
>= PNG_TEXT_COMPRESSION_zTXt
)
410 #ifdef PNG_WRITE_zTXt_SUPPORTED
411 /* Write compressed chunk */
412 png_write_zTXt(png_ptr
, info_ptr
->text
[i
].key
,
413 info_ptr
->text
[i
].text
, info_ptr
->text
[i
].compression
);
414 /* Mark this chunk as written */
415 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_zTXt_WR
;
417 png_warning(png_ptr
, "Unable to write compressed text");
421 else if (info_ptr
->text
[i
].compression
== PNG_TEXT_COMPRESSION_NONE
)
423 #ifdef PNG_WRITE_tEXt_SUPPORTED
424 /* Write uncompressed chunk */
425 png_write_tEXt(png_ptr
, info_ptr
->text
[i
].key
,
426 info_ptr
->text
[i
].text
, 0);
427 /* Mark this chunk as written */
428 info_ptr
->text
[i
].compression
= PNG_TEXT_COMPRESSION_NONE_WR
;
430 png_warning(png_ptr
, "Unable to write uncompressed text");
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 /* USER_MEM */
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 /* WRITE_COMPRESSED_TEXT */
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_RELEASE_BUILD
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
) != 0)
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));
648 *(rp
+ 2) = (png_byte
)(*(rp
+ 2) - *(rp
+ 1));
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
= (png_uint_32
)(*(rp
) << 8) | *(rp
+ 1);
670 png_uint_32 s1
= (png_uint_32
)(*(rp
+ 2) << 8) | *(rp
+ 3);
671 png_uint_32 s2
= (png_uint_32
)(*(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);
675 *(rp
+ 1) = (png_byte
)red
;
676 *(rp
+ 4) = (png_byte
)(blue
>> 8);
677 *(rp
+ 5) = (png_byte
)blue
;
680 #endif /* WRITE_16BIT */
683 #endif /* MNG_FEATURES */
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
) == 0)
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
) != 0)
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
) != 0)
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
) != 0)
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
) != 0)
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
) != 0)
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
) != 0)
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
) != 0)
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
!= 0 &&
749 (png_ptr
->transformations
& PNG_INTERLACE
) != 0)
751 switch (png_ptr
->pass
)
754 if ((png_ptr
->row_number
& 0x07) != 0)
756 png_write_finish_row(png_ptr
);
762 if ((png_ptr
->row_number
& 0x07) != 0 || png_ptr
->width
< 5)
764 png_write_finish_row(png_ptr
);
770 if ((png_ptr
->row_number
& 0x07) != 4)
772 png_write_finish_row(png_ptr
);
778 if ((png_ptr
->row_number
& 0x03) != 0 || png_ptr
->width
< 3)
780 png_write_finish_row(png_ptr
);
786 if ((png_ptr
->row_number
& 0x03) != 2)
788 png_write_finish_row(png_ptr
);
794 if ((png_ptr
->row_number
& 0x01) != 0 || png_ptr
->width
< 2)
796 png_write_finish_row(png_ptr
);
802 if ((png_ptr
->row_number
& 0x01) == 0)
804 png_write_finish_row(png_ptr
);
809 default: /* error: ignore it */
815 /* Set up row info for transformations */
816 row_info
.color_type
= png_ptr
->color_type
;
817 row_info
.width
= png_ptr
->usr_width
;
818 row_info
.channels
= png_ptr
->usr_channels
;
819 row_info
.bit_depth
= png_ptr
->usr_bit_depth
;
820 row_info
.pixel_depth
= (png_byte
)(row_info
.bit_depth
* row_info
.channels
);
821 row_info
.rowbytes
= PNG_ROWBYTES(row_info
.pixel_depth
, row_info
.width
);
823 png_debug1(3, "row_info->color_type = %d", row_info
.color_type
);
824 png_debug1(3, "row_info->width = %u", row_info
.width
);
825 png_debug1(3, "row_info->channels = %d", row_info
.channels
);
826 png_debug1(3, "row_info->bit_depth = %d", row_info
.bit_depth
);
827 png_debug1(3, "row_info->pixel_depth = %d", row_info
.pixel_depth
);
828 png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info
.rowbytes
);
830 /* Copy user's row into buffer, leaving room for filter byte. */
831 memcpy(png_ptr
->row_buf
+ 1, row
, row_info
.rowbytes
);
833 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
834 /* Handle interlacing */
835 if (png_ptr
->interlaced
&& png_ptr
->pass
< 6 &&
836 (png_ptr
->transformations
& PNG_INTERLACE
) != 0)
838 png_do_write_interlace(&row_info
, png_ptr
->row_buf
+ 1, png_ptr
->pass
);
839 /* This should always get caught above, but still ... */
840 if (row_info
.width
== 0)
842 png_write_finish_row(png_ptr
);
848 #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
849 /* Handle other transformations */
850 if (png_ptr
->transformations
!= 0)
851 png_do_write_transformations(png_ptr
, &row_info
);
854 /* At this point the row_info pixel depth must match the 'transformed' depth,
855 * which is also the output depth.
857 if (row_info
.pixel_depth
!= png_ptr
->pixel_depth
||
858 row_info
.pixel_depth
!= png_ptr
->transformed_pixel_depth
)
859 png_error(png_ptr
, "internal write transform logic error");
861 #ifdef PNG_MNG_FEATURES_SUPPORTED
862 /* Write filter_method 64 (intrapixel differencing) only if
863 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
864 * 2. Libpng did not write a PNG signature (this filter_method is only
865 * used in PNG datastreams that are embedded in MNG datastreams) and
866 * 3. The application called png_permit_mng_features with a mask that
867 * included PNG_FLAG_MNG_FILTER_64 and
868 * 4. The filter_method is 64 and
869 * 5. The color_type is RGB or RGBA
871 if ((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) != 0 &&
872 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
874 /* Intrapixel differencing */
875 png_do_write_intrapixel(&row_info
, png_ptr
->row_buf
+ 1);
879 /* Added at libpng-1.5.10 */
880 #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
881 /* Check for out-of-range palette index */
882 if (row_info
.color_type
== PNG_COLOR_TYPE_PALETTE
&&
883 png_ptr
->num_palette_max
>= 0)
884 png_do_check_palette_indexes(png_ptr
, &row_info
);
887 /* Find a filter if necessary, filter the row and write it out. */
888 png_write_find_filter(png_ptr
, &row_info
);
890 if (png_ptr
->write_row_fn
!= NULL
)
891 (*(png_ptr
->write_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
894 #ifdef PNG_WRITE_FLUSH_SUPPORTED
895 /* Set the automatic flush interval or 0 to turn flushing off */
897 png_set_flush(png_structrp png_ptr
, int nrows
)
899 png_debug(1, "in png_set_flush");
904 png_ptr
->flush_dist
= (nrows
< 0 ? 0 : (png_uint_32
)nrows
);
907 /* Flush the current output buffers now */
909 png_write_flush(png_structrp png_ptr
)
911 png_debug(1, "in png_write_flush");
916 /* We have already written out all of the data */
917 if (png_ptr
->row_number
>= png_ptr
->num_rows
)
920 png_compress_IDAT(png_ptr
, NULL
, 0, Z_SYNC_FLUSH
);
921 png_ptr
->flush_rows
= 0;
924 #endif /* WRITE_FLUSH */
926 /* Free any memory used in png_ptr struct without freeing the struct itself. */
928 png_write_destroy(png_structrp png_ptr
)
930 png_debug(1, "in png_write_destroy");
932 /* Free any memory zlib uses */
933 if ((png_ptr
->flags
& PNG_FLAG_ZSTREAM_INITIALIZED
) != 0)
934 deflateEnd(&png_ptr
->zstream
);
936 /* Free our memory. png_free checks NULL for us. */
937 png_free_buffer_list(png_ptr
, &png_ptr
->zbuffer_list
);
938 png_free(png_ptr
, png_ptr
->row_buf
);
939 png_ptr
->row_buf
= NULL
;
940 #ifdef PNG_WRITE_FILTER_SUPPORTED
941 png_free(png_ptr
, png_ptr
->prev_row
);
942 png_free(png_ptr
, png_ptr
->try_row
);
943 png_free(png_ptr
, png_ptr
->tst_row
);
944 png_ptr
->prev_row
= NULL
;
945 png_ptr
->try_row
= NULL
;
946 png_ptr
->tst_row
= NULL
;
949 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
950 png_free(png_ptr
, png_ptr
->chunk_list
);
951 png_ptr
->chunk_list
= NULL
;
954 /* The error handling and memory handling information is left intact at this
955 * point: the jmp_buf may still have to be freed. See png_destroy_png_struct
956 * for how this happens.
960 /* Free all memory used by the write.
961 * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
962 * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free
963 * the passed in info_structs but it would quietly fail to free any of the data
964 * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it
968 png_destroy_write_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
)
970 png_debug(1, "in png_destroy_write_struct");
972 if (png_ptr_ptr
!= NULL
)
974 png_structrp png_ptr
= *png_ptr_ptr
;
976 if (png_ptr
!= NULL
) /* added in libpng 1.6.0 */
978 png_destroy_info_struct(png_ptr
, info_ptr_ptr
);
981 png_write_destroy(png_ptr
);
982 png_destroy_png_struct(png_ptr
);
987 /* Allow the application to select one or more row filters to use. */
989 png_set_filter(png_structrp png_ptr
, int method
, int filters
)
991 png_debug(1, "in png_set_filter");
996 #ifdef PNG_MNG_FEATURES_SUPPORTED
997 if ((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) != 0 &&
998 (method
== PNG_INTRAPIXEL_DIFFERENCING
))
999 method
= PNG_FILTER_TYPE_BASE
;
1002 if (method
== PNG_FILTER_TYPE_BASE
)
1004 switch (filters
& (PNG_ALL_FILTERS
| 0x07))
1006 #ifdef PNG_WRITE_FILTER_SUPPORTED
1009 case 7: png_app_error(png_ptr
, "Unknown row filter for method 0");
1011 #endif /* WRITE_FILTER */
1012 case PNG_FILTER_VALUE_NONE
:
1013 png_ptr
->do_filter
= PNG_FILTER_NONE
; break;
1015 #ifdef PNG_WRITE_FILTER_SUPPORTED
1016 case PNG_FILTER_VALUE_SUB
:
1017 png_ptr
->do_filter
= PNG_FILTER_SUB
; break;
1019 case PNG_FILTER_VALUE_UP
:
1020 png_ptr
->do_filter
= PNG_FILTER_UP
; break;
1022 case PNG_FILTER_VALUE_AVG
:
1023 png_ptr
->do_filter
= PNG_FILTER_AVG
; break;
1025 case PNG_FILTER_VALUE_PAETH
:
1026 png_ptr
->do_filter
= PNG_FILTER_PAETH
; break;
1029 png_ptr
->do_filter
= (png_byte
)filters
; break;
1032 png_app_error(png_ptr
, "Unknown row filter for method 0");
1033 #endif /* WRITE_FILTER */
1036 #ifdef PNG_WRITE_FILTER_SUPPORTED
1037 /* If we have allocated the row_buf, this means we have already started
1038 * with the image and we should have allocated all of the filter buffers
1039 * that have been selected. If prev_row isn't already allocated, then
1040 * it is too late to start using the filters that need it, since we
1041 * will be missing the data in the previous row. If an application
1042 * wants to start and stop using particular filters during compression,
1043 * it should start out with all of the filters, and then remove them
1044 * or add them back after the start of compression.
1046 * NOTE: this is a nasty constraint on the code, because it means that the
1047 * prev_row buffer must be maintained even if there are currently no
1048 * 'prev_row' requiring filters active.
1050 if (png_ptr
->row_buf
!= NULL
)
1053 png_alloc_size_t buf_size
;
1055 /* Repeat the checks in png_write_start_row; 1 pixel high or wide
1056 * images cannot benefit from certain filters. If this isn't done here
1057 * the check below will fire on 1 pixel high images.
1059 if (png_ptr
->height
== 1)
1060 filters
&= ~(PNG_FILTER_UP
|PNG_FILTER_AVG
|PNG_FILTER_PAETH
);
1062 if (png_ptr
->width
== 1)
1063 filters
&= ~(PNG_FILTER_SUB
|PNG_FILTER_AVG
|PNG_FILTER_PAETH
);
1065 if ((filters
& (PNG_FILTER_UP
|PNG_FILTER_AVG
|PNG_FILTER_PAETH
)) != 0
1066 && png_ptr
->prev_row
== NULL
)
1068 /* This is the error case, however it is benign - the previous row
1069 * is not available so the filter can't be used. Just warn here.
1071 png_app_warning(png_ptr
,
1072 "png_set_filter: UP/AVG/PAETH cannot be added after start");
1073 filters
&= ~(PNG_FILTER_UP
|PNG_FILTER_AVG
|PNG_FILTER_PAETH
);
1078 if (filters
& PNG_FILTER_SUB
)
1081 if (filters
& PNG_FILTER_UP
)
1084 if (filters
& PNG_FILTER_AVG
)
1087 if (filters
& PNG_FILTER_PAETH
)
1090 /* Allocate needed row buffers if they have not already been
1093 buf_size
= PNG_ROWBYTES(png_ptr
->usr_channels
* png_ptr
->usr_bit_depth
,
1094 png_ptr
->width
) + 1;
1096 if (png_ptr
->try_row
== NULL
)
1097 png_ptr
->try_row
= png_voidcast(png_bytep
,
1098 png_malloc(png_ptr
, buf_size
));
1100 if (num_filters
> 1)
1102 if (png_ptr
->tst_row
== NULL
)
1103 png_ptr
->tst_row
= png_voidcast(png_bytep
,
1104 png_malloc(png_ptr
, buf_size
));
1107 png_ptr
->do_filter
= (png_byte
)filters
;
1111 png_error(png_ptr
, "Unknown custom filter method");
1114 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* DEPRECATED */
1115 /* Provide floating and fixed point APIs */
1116 #ifdef PNG_FLOATING_POINT_SUPPORTED
1118 png_set_filter_heuristics(png_structrp png_ptr
, int heuristic_method
,
1119 int num_weights
, png_const_doublep filter_weights
,
1120 png_const_doublep filter_costs
)
1123 PNG_UNUSED(heuristic_method
)
1124 PNG_UNUSED(num_weights
)
1125 PNG_UNUSED(filter_weights
)
1126 PNG_UNUSED(filter_costs
)
1128 #endif /* FLOATING_POINT */
1130 #ifdef PNG_FIXED_POINT_SUPPORTED
1132 png_set_filter_heuristics_fixed(png_structrp png_ptr
, int heuristic_method
,
1133 int num_weights
, png_const_fixed_point_p filter_weights
,
1134 png_const_fixed_point_p filter_costs
)
1137 PNG_UNUSED(heuristic_method
)
1138 PNG_UNUSED(num_weights
)
1139 PNG_UNUSED(filter_weights
)
1140 PNG_UNUSED(filter_costs
)
1142 #endif /* FIXED_POINT */
1143 #endif /* WRITE_WEIGHTED_FILTER */
1145 #ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
1147 png_set_compression_level(png_structrp png_ptr
, int level
)
1149 png_debug(1, "in png_set_compression_level");
1151 if (png_ptr
== NULL
)
1154 png_ptr
->zlib_level
= level
;
1158 png_set_compression_mem_level(png_structrp png_ptr
, int mem_level
)
1160 png_debug(1, "in png_set_compression_mem_level");
1162 if (png_ptr
== NULL
)
1165 png_ptr
->zlib_mem_level
= mem_level
;
1169 png_set_compression_strategy(png_structrp png_ptr
, int strategy
)
1171 png_debug(1, "in png_set_compression_strategy");
1173 if (png_ptr
== NULL
)
1176 /* The flag setting here prevents the libpng dynamic selection of strategy.
1178 png_ptr
->flags
|= PNG_FLAG_ZLIB_CUSTOM_STRATEGY
;
1179 png_ptr
->zlib_strategy
= strategy
;
1182 /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1183 * smaller value of window_bits if it can do so safely.
1186 png_set_compression_window_bits(png_structrp png_ptr
, int window_bits
)
1188 if (png_ptr
== NULL
)
1191 /* Prior to 1.6.0 this would warn but then set the window_bits value. This
1192 * meant that negative window bits values could be selected that would cause
1193 * libpng to write a non-standard PNG file with raw deflate or gzip
1194 * compressed IDAT or ancillary chunks. Such files can be read and there is
1195 * no warning on read, so this seems like a very bad idea.
1197 if (window_bits
> 15)
1199 png_warning(png_ptr
, "Only compression windows <= 32k supported by PNG");
1203 else if (window_bits
< 8)
1205 png_warning(png_ptr
, "Only compression windows >= 256 supported by PNG");
1209 png_ptr
->zlib_window_bits
= window_bits
;
1213 png_set_compression_method(png_structrp png_ptr
, int method
)
1215 png_debug(1, "in png_set_compression_method");
1217 if (png_ptr
== NULL
)
1220 /* This would produce an invalid PNG file if it worked, but it doesn't and
1221 * deflate will fault it, so it is harmless to just warn here.
1224 png_warning(png_ptr
, "Only compression method 8 is supported by PNG");
1226 png_ptr
->zlib_method
= method
;
1228 #endif /* WRITE_CUSTOMIZE_COMPRESSION */
1230 /* The following were added to libpng-1.5.4 */
1231 #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
1233 png_set_text_compression_level(png_structrp png_ptr
, int level
)
1235 png_debug(1, "in png_set_text_compression_level");
1237 if (png_ptr
== NULL
)
1240 png_ptr
->zlib_text_level
= level
;
1244 png_set_text_compression_mem_level(png_structrp png_ptr
, int mem_level
)
1246 png_debug(1, "in png_set_text_compression_mem_level");
1248 if (png_ptr
== NULL
)
1251 png_ptr
->zlib_text_mem_level
= mem_level
;
1255 png_set_text_compression_strategy(png_structrp png_ptr
, int strategy
)
1257 png_debug(1, "in png_set_text_compression_strategy");
1259 if (png_ptr
== NULL
)
1262 png_ptr
->zlib_text_strategy
= strategy
;
1265 /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1266 * smaller value of window_bits if it can do so safely.
1269 png_set_text_compression_window_bits(png_structrp png_ptr
, int window_bits
)
1271 if (png_ptr
== NULL
)
1274 if (window_bits
> 15)
1276 png_warning(png_ptr
, "Only compression windows <= 32k supported by PNG");
1280 else if (window_bits
< 8)
1282 png_warning(png_ptr
, "Only compression windows >= 256 supported by PNG");
1286 png_ptr
->zlib_text_window_bits
= window_bits
;
1290 png_set_text_compression_method(png_structrp png_ptr
, int method
)
1292 png_debug(1, "in png_set_text_compression_method");
1294 if (png_ptr
== NULL
)
1298 png_warning(png_ptr
, "Only compression method 8 is supported by PNG");
1300 png_ptr
->zlib_text_method
= method
;
1302 #endif /* WRITE_CUSTOMIZE_ZTXT_COMPRESSION */
1303 /* end of API added to libpng-1.5.4 */
1306 png_set_write_status_fn(png_structrp png_ptr
, png_write_status_ptr write_row_fn
)
1308 if (png_ptr
== NULL
)
1311 png_ptr
->write_row_fn
= write_row_fn
;
1314 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1316 png_set_write_user_transform_fn(png_structrp png_ptr
, png_user_transform_ptr
1317 write_user_transform_fn
)
1319 png_debug(1, "in png_set_write_user_transform_fn");
1321 if (png_ptr
== NULL
)
1324 png_ptr
->transformations
|= PNG_USER_TRANSFORM
;
1325 png_ptr
->write_user_transform_fn
= write_user_transform_fn
;
1330 #ifdef PNG_INFO_IMAGE_SUPPORTED
1332 png_write_png(png_structrp png_ptr
, png_inforp info_ptr
,
1333 int transforms
, voidp params
)
1335 if (png_ptr
== NULL
|| info_ptr
== NULL
)
1338 if ((info_ptr
->valid
& PNG_INFO_IDAT
) == 0)
1340 png_app_error(png_ptr
, "no rows for png_write_image to write");
1344 /* Write the file header information. */
1345 png_write_info(png_ptr
, info_ptr
);
1347 /* ------ these transformations don't touch the info structure ------- */
1349 /* Invert monochrome pixels */
1350 if ((transforms
& PNG_TRANSFORM_INVERT_MONO
) != 0)
1351 #ifdef PNG_WRITE_INVERT_SUPPORTED
1352 png_set_invert_mono(png_ptr
);
1354 png_app_error(png_ptr
, "PNG_TRANSFORM_INVERT_MONO not supported");
1357 /* Shift the pixels up to a legal bit depth and fill in
1358 * as appropriate to correctly scale the image.
1360 if ((transforms
& PNG_TRANSFORM_SHIFT
) != 0)
1361 #ifdef PNG_WRITE_SHIFT_SUPPORTED
1362 if ((info_ptr
->valid
& PNG_INFO_sBIT
) != 0)
1363 png_set_shift(png_ptr
, &info_ptr
->sig_bit
);
1365 png_app_error(png_ptr
, "PNG_TRANSFORM_SHIFT not supported");
1368 /* Pack pixels into bytes */
1369 if ((transforms
& PNG_TRANSFORM_PACKING
) != 0)
1370 #ifdef PNG_WRITE_PACK_SUPPORTED
1371 png_set_packing(png_ptr
);
1373 png_app_error(png_ptr
, "PNG_TRANSFORM_PACKING not supported");
1376 /* Swap location of alpha bytes from ARGB to RGBA */
1377 if ((transforms
& PNG_TRANSFORM_SWAP_ALPHA
) != 0)
1378 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
1379 png_set_swap_alpha(png_ptr
);
1381 png_app_error(png_ptr
, "PNG_TRANSFORM_SWAP_ALPHA not supported");
1384 /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into
1385 * RGB, note that the code expects the input color type to be G or RGB; no
1388 if ((transforms
& (PNG_TRANSFORM_STRIP_FILLER_AFTER
|
1389 PNG_TRANSFORM_STRIP_FILLER_BEFORE
)) != 0)
1391 #ifdef PNG_WRITE_FILLER_SUPPORTED
1392 if ((transforms
& PNG_TRANSFORM_STRIP_FILLER_AFTER
) != 0)
1394 if ((transforms
& PNG_TRANSFORM_STRIP_FILLER_BEFORE
) != 0)
1395 png_app_error(png_ptr
,
1396 "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported");
1398 /* Continue if ignored - this is the pre-1.6.10 behavior */
1399 png_set_filler(png_ptr
, 0, PNG_FILLER_AFTER
);
1402 else if ((transforms
& PNG_TRANSFORM_STRIP_FILLER_BEFORE
) != 0)
1403 png_set_filler(png_ptr
, 0, PNG_FILLER_BEFORE
);
1405 png_app_error(png_ptr
, "PNG_TRANSFORM_STRIP_FILLER not supported");
1409 /* Flip BGR pixels to RGB */
1410 if ((transforms
& PNG_TRANSFORM_BGR
) != 0)
1411 #ifdef PNG_WRITE_BGR_SUPPORTED
1412 png_set_bgr(png_ptr
);
1414 png_app_error(png_ptr
, "PNG_TRANSFORM_BGR not supported");
1417 /* Swap bytes of 16-bit files to most significant byte first */
1418 if ((transforms
& PNG_TRANSFORM_SWAP_ENDIAN
) != 0)
1419 #ifdef PNG_WRITE_SWAP_SUPPORTED
1420 png_set_swap(png_ptr
);
1422 png_app_error(png_ptr
, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
1425 /* Swap bits of 1-bit, 2-bit, 4-bit packed pixel formats */
1426 if ((transforms
& PNG_TRANSFORM_PACKSWAP
) != 0)
1427 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
1428 png_set_packswap(png_ptr
);
1430 png_app_error(png_ptr
, "PNG_TRANSFORM_PACKSWAP not supported");
1433 /* Invert the alpha channel from opacity to transparency */
1434 if ((transforms
& PNG_TRANSFORM_INVERT_ALPHA
) != 0)
1435 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
1436 png_set_invert_alpha(png_ptr
);
1438 png_app_error(png_ptr
, "PNG_TRANSFORM_INVERT_ALPHA not supported");
1441 /* ----------------------- end of transformations ------------------- */
1443 /* Write the bits */
1444 png_write_image(png_ptr
, info_ptr
->row_pointers
);
1446 /* It is REQUIRED to call this to finish writing the rest of the file */
1447 png_write_end(png_ptr
, info_ptr
);
1454 #ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
1455 /* Initialize the write structure - general purpose utility. */
1457 png_image_write_init(png_imagep image
)
1459 png_structp png_ptr
= png_create_write_struct(PNG_LIBPNG_VER_STRING
, image
,
1460 png_safe_error
, png_safe_warning
);
1462 if (png_ptr
!= NULL
)
1464 png_infop info_ptr
= png_create_info_struct(png_ptr
);
1466 if (info_ptr
!= NULL
)
1468 png_controlp control
= png_voidcast(png_controlp
,
1469 png_malloc_warn(png_ptr
, (sizeof *control
)));
1471 if (control
!= NULL
)
1473 memset(control
, 0, (sizeof *control
));
1475 control
->png_ptr
= png_ptr
;
1476 control
->info_ptr
= info_ptr
;
1477 control
->for_write
= 1;
1479 image
->opaque
= control
;
1483 /* Error clean up */
1484 png_destroy_info_struct(png_ptr
, &info_ptr
);
1487 png_destroy_write_struct(&png_ptr
, NULL
);
1490 return png_image_error(image
, "png_image_write_: out of memory");
1493 /* Arguments to png_image_write_main: */
1498 png_const_voidp buffer
;
1499 png_int_32 row_stride
;
1500 png_const_voidp colormap
;
1501 int convert_to_8bit
;
1502 /* Local variables: */
1503 png_const_voidp first_row
;
1504 ptrdiff_t row_bytes
;
1505 png_voidp local_row
;
1506 /* Byte count for memory writing */
1508 png_alloc_size_t memory_bytes
; /* not used for STDIO */
1509 png_alloc_size_t output_bytes
; /* running total */
1510 } png_image_write_control
;
1512 /* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
1513 * do any necessary byte swapping. The component order is defined by the
1514 * png_image format value.
1517 png_write_image_16bit(png_voidp argument
)
1519 png_image_write_control
*display
= png_voidcast(png_image_write_control
*,
1521 png_imagep image
= display
->image
;
1522 png_structrp png_ptr
= image
->opaque
->png_ptr
;
1524 png_const_uint_16p input_row
= png_voidcast(png_const_uint_16p
,
1525 display
->first_row
);
1526 png_uint_16p output_row
= png_voidcast(png_uint_16p
, display
->local_row
);
1527 png_uint_16p row_end
;
1528 const unsigned int channels
= (image
->format
& PNG_FORMAT_FLAG_COLOR
) != 0 ?
1531 png_uint_32 y
= image
->height
;
1533 if ((image
->format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
1535 # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
1536 if ((image
->format
& PNG_FORMAT_FLAG_AFIRST
) != 0)
1539 ++input_row
; /* To point to the first component */
1543 aindex
= (int)channels
;
1545 aindex
= (int)channels
;
1550 png_error(png_ptr
, "png_write_image: internal call error");
1552 /* Work out the output row end and count over this, note that the increment
1553 * above to 'row' means that row_end can actually be beyond the end of the
1554 * row; this is correct.
1556 row_end
= output_row
+ image
->width
* (channels
+1);
1560 png_const_uint_16p in_ptr
= input_row
;
1561 png_uint_16p out_ptr
= output_row
;
1563 while (out_ptr
< row_end
)
1565 const png_uint_16 alpha
= in_ptr
[aindex
];
1566 png_uint_32 reciprocal
= 0;
1569 out_ptr
[aindex
] = alpha
;
1571 /* Calculate a reciprocal. The correct calculation is simply
1572 * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
1573 * allows correct rounding by adding .5 before the shift. 'reciprocal'
1574 * is only initialized when required.
1576 if (alpha
> 0 && alpha
< 65535)
1577 reciprocal
= ((0xffff<<15)+(alpha
>>1))/alpha
;
1580 do /* always at least one channel */
1582 png_uint_16 component
= *in_ptr
++;
1584 /* The following gives 65535 for an alpha of 0, which is fine,
1585 * otherwise if 0/0 is represented as some other value there is more
1586 * likely to be a discontinuity which will probably damage
1587 * compression when moving from a fully transparent area to a
1588 * nearly transparent one. (The assumption here is that opaque
1589 * areas tend not to be 0 intensity.)
1591 if (component
>= alpha
)
1594 /* component<alpha, so component/alpha is less than one and
1595 * component*reciprocal is less than 2^31.
1597 else if (component
> 0 && alpha
< 65535)
1599 png_uint_32 calc
= component
* reciprocal
;
1600 calc
+= 16384; /* round to nearest */
1601 component
= (png_uint_16
)(calc
>> 15);
1604 *out_ptr
++ = component
;
1608 /* Skip to next component (skip the intervening alpha channel) */
1613 png_write_row(png_ptr
, png_voidcast(png_const_bytep
, display
->local_row
));
1614 input_row
+= (png_uint_16
)display
->row_bytes
/(sizeof (png_uint_16
));
1620 /* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel
1621 * is present it must be removed from the components, the components are then
1622 * written in sRGB encoding. No components are added or removed.
1624 * Calculate an alpha reciprocal to reverse pre-multiplication. As above the
1625 * calculation can be done to 15 bits of accuracy; however, the output needs to
1626 * be scaled in the range 0..255*65535, so include that scaling here.
1628 # define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+(alpha>>1))/alpha)
1631 png_unpremultiply(png_uint_32 component
, png_uint_32 alpha
,
1632 png_uint_32 reciprocal
/*from the above macro*/)
1634 /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
1635 * is represented as some other value there is more likely to be a
1636 * discontinuity which will probably damage compression when moving from a
1637 * fully transparent area to a nearly transparent one. (The assumption here
1638 * is that opaque areas tend not to be 0 intensity.)
1640 * There is a rounding problem here; if alpha is less than 128 it will end up
1641 * as 0 when scaled to 8 bits. To avoid introducing spurious colors into the
1642 * output change for this too.
1644 if (component
>= alpha
|| alpha
< 128)
1647 /* component<alpha, so component/alpha is less than one and
1648 * component*reciprocal is less than 2^31.
1650 else if (component
> 0)
1652 /* The test is that alpha/257 (rounded) is less than 255, the first value
1653 * that becomes 255 is 65407.
1654 * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
1655 * be exact!) [Could also test reciprocal != 0]
1659 component
*= reciprocal
;
1660 component
+= 64; /* round to nearest */
1667 /* Convert the component to sRGB. */
1668 return (png_byte
)PNG_sRGB_FROM_LINEAR(component
);
1676 png_write_image_8bit(png_voidp argument
)
1678 png_image_write_control
*display
= png_voidcast(png_image_write_control
*,
1680 png_imagep image
= display
->image
;
1681 png_structrp png_ptr
= image
->opaque
->png_ptr
;
1683 png_const_uint_16p input_row
= png_voidcast(png_const_uint_16p
,
1684 display
->first_row
);
1685 png_bytep output_row
= png_voidcast(png_bytep
, display
->local_row
);
1686 png_uint_32 y
= image
->height
;
1687 const unsigned int channels
= (image
->format
& PNG_FORMAT_FLAG_COLOR
) != 0 ?
1690 if ((image
->format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
1695 # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
1696 if ((image
->format
& PNG_FORMAT_FLAG_AFIRST
) != 0)
1699 ++input_row
; /* To point to the first component */
1705 aindex
= (int)channels
;
1707 /* Use row_end in place of a loop counter: */
1708 row_end
= output_row
+ image
->width
* (channels
+1);
1712 png_const_uint_16p in_ptr
= input_row
;
1713 png_bytep out_ptr
= output_row
;
1715 while (out_ptr
< row_end
)
1717 png_uint_16 alpha
= in_ptr
[aindex
];
1718 png_byte alphabyte
= (png_byte
)PNG_DIV257(alpha
);
1719 png_uint_32 reciprocal
= 0;
1722 /* Scale and write the alpha channel. */
1723 out_ptr
[aindex
] = alphabyte
;
1725 if (alphabyte
> 0 && alphabyte
< 255)
1726 reciprocal
= UNP_RECIPROCAL(alpha
);
1729 do /* always at least one channel */
1730 *out_ptr
++ = png_unpremultiply(*in_ptr
++, alpha
, reciprocal
);
1733 /* Skip to next component (skip the intervening alpha channel) */
1736 } /* while out_ptr < row_end */
1738 png_write_row(png_ptr
, png_voidcast(png_const_bytep
,
1739 display
->local_row
));
1740 input_row
+= (png_uint_16
)display
->row_bytes
/(sizeof (png_uint_16
));
1746 /* No alpha channel, so the row_end really is the end of the row and it
1747 * is sufficient to loop over the components one by one.
1749 png_bytep row_end
= output_row
+ image
->width
* channels
;
1753 png_const_uint_16p in_ptr
= input_row
;
1754 png_bytep out_ptr
= output_row
;
1756 while (out_ptr
< row_end
)
1758 png_uint_32 component
= *in_ptr
++;
1761 *out_ptr
++ = (png_byte
)PNG_sRGB_FROM_LINEAR(component
);
1764 png_write_row(png_ptr
, output_row
);
1765 input_row
+= (png_uint_16
)display
->row_bytes
/(sizeof (png_uint_16
));
1773 png_image_set_PLTE(png_image_write_control
*display
)
1775 const png_imagep image
= display
->image
;
1776 const void *cmap
= display
->colormap
;
1777 const int entries
= image
->colormap_entries
> 256 ? 256 :
1778 (int)image
->colormap_entries
;
1780 /* NOTE: the caller must check for cmap != NULL and entries != 0 */
1781 const png_uint_32 format
= image
->format
;
1782 const unsigned int channels
= PNG_IMAGE_SAMPLE_CHANNELS(format
);
1784 # if defined(PNG_FORMAT_BGR_SUPPORTED) &&\
1785 defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)
1786 const int afirst
= (format
& PNG_FORMAT_FLAG_AFIRST
) != 0 &&
1787 (format
& PNG_FORMAT_FLAG_ALPHA
) != 0;
1792 # ifdef PNG_FORMAT_BGR_SUPPORTED
1793 const int bgr
= (format
& PNG_FORMAT_FLAG_BGR
) != 0 ? 2 : 0;
1799 png_color palette
[256];
1802 memset(tRNS
, 255, (sizeof tRNS
));
1803 memset(palette
, 0, (sizeof palette
));
1805 for (i
=num_trans
=0; i
<entries
; ++i
)
1807 /* This gets automatically converted to sRGB with reversal of the
1808 * pre-multiplication if the color-map has an alpha channel.
1810 if ((format
& PNG_FORMAT_FLAG_LINEAR
) != 0)
1812 png_const_uint_16p entry
= png_voidcast(png_const_uint_16p
, cmap
);
1814 entry
+= (unsigned int)i
* channels
;
1816 if ((channels
& 1) != 0) /* no alpha */
1818 if (channels
>= 3) /* RGB */
1820 palette
[i
].blue
= (png_byte
)PNG_sRGB_FROM_LINEAR(255 *
1822 palette
[i
].green
= (png_byte
)PNG_sRGB_FROM_LINEAR(255 *
1824 palette
[i
].red
= (png_byte
)PNG_sRGB_FROM_LINEAR(255 *
1829 palette
[i
].blue
= palette
[i
].red
= palette
[i
].green
=
1830 (png_byte
)PNG_sRGB_FROM_LINEAR(255 * *entry
);
1835 png_uint_16 alpha
= entry
[afirst
? 0 : channels
-1];
1836 png_byte alphabyte
= (png_byte
)PNG_DIV257(alpha
);
1837 png_uint_32 reciprocal
= 0;
1839 /* Calculate a reciprocal, as in the png_write_image_8bit code above
1840 * this is designed to produce a value scaled to 255*65535 when
1841 * divided by 128 (i.e. asr 7).
1843 if (alphabyte
> 0 && alphabyte
< 255)
1844 reciprocal
= (((0xffff*0xff)<<7)+(alpha
>>1))/alpha
;
1846 tRNS
[i
] = alphabyte
;
1847 if (alphabyte
< 255)
1850 if (channels
>= 3) /* RGB */
1852 palette
[i
].blue
= png_unpremultiply(entry
[afirst
+ (2 ^ bgr
)],
1854 palette
[i
].green
= png_unpremultiply(entry
[afirst
+ 1], alpha
,
1856 palette
[i
].red
= png_unpremultiply(entry
[afirst
+ bgr
], alpha
,
1861 palette
[i
].blue
= palette
[i
].red
= palette
[i
].green
=
1862 png_unpremultiply(entry
[afirst
], alpha
, reciprocal
);
1866 else /* Color-map has sRGB values */
1868 png_const_bytep entry
= png_voidcast(png_const_bytep
, cmap
);
1870 entry
+= (unsigned int)i
* channels
;
1875 tRNS
[i
] = entry
[afirst
? 0 : 3];
1880 palette
[i
].blue
= entry
[afirst
+ (2 ^ bgr
)];
1881 palette
[i
].green
= entry
[afirst
+ 1];
1882 palette
[i
].red
= entry
[afirst
+ bgr
];
1886 tRNS
[i
] = entry
[1 ^ afirst
];
1891 palette
[i
].blue
= palette
[i
].red
= palette
[i
].green
=
1908 png_set_PLTE(image
->opaque
->png_ptr
, image
->opaque
->info_ptr
, palette
,
1912 png_set_tRNS(image
->opaque
->png_ptr
, image
->opaque
->info_ptr
, tRNS
,
1915 image
->colormap_entries
= (png_uint_32
)entries
;
1919 png_image_write_main(png_voidp argument
)
1921 png_image_write_control
*display
= png_voidcast(png_image_write_control
*,
1923 png_imagep image
= display
->image
;
1924 png_structrp png_ptr
= image
->opaque
->png_ptr
;
1925 png_inforp info_ptr
= image
->opaque
->info_ptr
;
1926 png_uint_32 format
= image
->format
;
1928 /* The following four ints are actually booleans */
1929 int colormap
= (format
& PNG_FORMAT_FLAG_COLORMAP
);
1930 int linear
= !colormap
&& (format
& PNG_FORMAT_FLAG_LINEAR
); /* input */
1931 int alpha
= !colormap
&& (format
& PNG_FORMAT_FLAG_ALPHA
);
1932 int write_16bit
= linear
&& !colormap
&& (display
->convert_to_8bit
== 0);
1934 # ifdef PNG_BENIGN_ERRORS_SUPPORTED
1935 /* Make sure we error out on any bad situation */
1936 png_set_benign_errors(png_ptr
, 0/*error*/);
1939 /* Default the 'row_stride' parameter if required, also check the row stride
1940 * and total image size to ensure that they are within the system limits.
1943 const unsigned int channels
= PNG_IMAGE_PIXEL_CHANNELS(image
->format
);
1945 if (image
->width
<= 0x7fffffffU
/channels
) /* no overflow */
1948 const png_uint_32 png_row_stride
= image
->width
* channels
;
1950 if (display
->row_stride
== 0)
1951 display
->row_stride
= (png_int_32
)/*SAFE*/png_row_stride
;
1953 if (display
->row_stride
< 0)
1954 check
= (png_uint_32
)(-display
->row_stride
);
1957 check
= (png_uint_32
)display
->row_stride
;
1959 if (check
>= png_row_stride
)
1961 /* Now check for overflow of the image buffer calculation; this
1962 * limits the whole image size to 32 bits for API compatibility with
1963 * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
1965 if (image
->height
> 0xffffffffU
/png_row_stride
)
1966 png_error(image
->opaque
->png_ptr
, "memory image too large");
1970 png_error(image
->opaque
->png_ptr
, "supplied row stride too small");
1974 png_error(image
->opaque
->png_ptr
, "image row stride too large");
1977 /* Set the required transforms then write the rows in the correct order. */
1978 if ((format
& PNG_FORMAT_FLAG_COLORMAP
) != 0)
1980 if (display
->colormap
!= NULL
&& image
->colormap_entries
> 0)
1982 png_uint_32 entries
= image
->colormap_entries
;
1984 png_set_IHDR(png_ptr
, info_ptr
, image
->width
, image
->height
,
1985 entries
> 16 ? 8 : (entries
> 4 ? 4 : (entries
> 2 ? 2 : 1)),
1986 PNG_COLOR_TYPE_PALETTE
, PNG_INTERLACE_NONE
,
1987 PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
1989 png_image_set_PLTE(display
);
1993 png_error(image
->opaque
->png_ptr
,
1994 "no color-map for color-mapped image");
1998 png_set_IHDR(png_ptr
, info_ptr
, image
->width
, image
->height
,
1999 write_16bit
? 16 : 8,
2000 ((format
& PNG_FORMAT_FLAG_COLOR
) ? PNG_COLOR_MASK_COLOR
: 0) +
2001 ((format
& PNG_FORMAT_FLAG_ALPHA
) ? PNG_COLOR_MASK_ALPHA
: 0),
2002 PNG_INTERLACE_NONE
, PNG_COMPRESSION_TYPE_BASE
, PNG_FILTER_TYPE_BASE
);
2004 /* Counter-intuitively the data transformations must be called *after*
2005 * png_write_info, not before as in the read code, but the 'set' functions
2006 * must still be called before. Just set the color space information, never
2007 * write an interlaced image.
2010 if (write_16bit
!= 0)
2012 /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
2013 png_set_gAMA_fixed(png_ptr
, info_ptr
, PNG_GAMMA_LINEAR
);
2015 if ((image
->flags
& PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB
) == 0)
2016 png_set_cHRM_fixed(png_ptr
, info_ptr
,
2018 /* white */ 31270, 32900,
2019 /* red */ 64000, 33000,
2020 /* green */ 30000, 60000,
2021 /* blue */ 15000, 6000
2025 else if ((image
->flags
& PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB
) == 0)
2026 png_set_sRGB(png_ptr
, info_ptr
, PNG_sRGB_INTENT_PERCEPTUAL
);
2028 /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
2029 * space must still be gamma encoded.
2032 png_set_gAMA_fixed(png_ptr
, info_ptr
, PNG_GAMMA_sRGB_INVERSE
);
2034 /* Write the file header. */
2035 png_write_info(png_ptr
, info_ptr
);
2037 /* Now set up the data transformations (*after* the header is written),
2038 * remove the handled transformations from the 'format' flags for checking.
2040 * First check for a little endian system if writing 16-bit files.
2042 if (write_16bit
!= 0)
2044 PNG_CONST png_uint_16 le
= 0x0001;
2046 if ((*(png_const_bytep
) & le
) != 0)
2047 png_set_swap(png_ptr
);
2050 # ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
2051 if ((format
& PNG_FORMAT_FLAG_BGR
) != 0)
2053 if (colormap
== 0 && (format
& PNG_FORMAT_FLAG_COLOR
) != 0)
2054 png_set_bgr(png_ptr
);
2055 format
&= ~PNG_FORMAT_FLAG_BGR
;
2059 # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
2060 if ((format
& PNG_FORMAT_FLAG_AFIRST
) != 0)
2062 if (colormap
== 0 && (format
& PNG_FORMAT_FLAG_ALPHA
) != 0)
2063 png_set_swap_alpha(png_ptr
);
2064 format
&= ~PNG_FORMAT_FLAG_AFIRST
;
2068 /* If there are 16 or fewer color-map entries we wrote a lower bit depth
2069 * above, but the application data is still byte packed.
2071 if (colormap
!= 0 && image
->colormap_entries
<= 16)
2072 png_set_packing(png_ptr
);
2074 /* That should have handled all (both) the transforms. */
2075 if ((format
& ~(png_uint_32
)(PNG_FORMAT_FLAG_COLOR
| PNG_FORMAT_FLAG_LINEAR
|
2076 PNG_FORMAT_FLAG_ALPHA
| PNG_FORMAT_FLAG_COLORMAP
)) != 0)
2077 png_error(png_ptr
, "png_write_image: unsupported transformation");
2080 png_const_bytep row
= png_voidcast(png_const_bytep
, display
->buffer
);
2081 ptrdiff_t row_bytes
= display
->row_stride
;
2084 row_bytes
*= (sizeof (png_uint_16
));
2087 row
+= (image
->height
-1) * (-row_bytes
);
2089 display
->first_row
= row
;
2090 display
->row_bytes
= row_bytes
;
2093 /* Apply 'fast' options if the flag is set. */
2094 if ((image
->flags
& PNG_IMAGE_FLAG_FAST
) != 0)
2096 png_set_filter(png_ptr
, PNG_FILTER_TYPE_BASE
, PNG_NO_FILTERS
);
2097 /* NOTE: determined by experiment using pngstest, this reflects some
2098 * balance between the time to write the image once and the time to read
2099 * it about 50 times. The speed-up in pngstest was about 10-20% of the
2100 * total (user) time on a heavily loaded system.
2102 # ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
2103 png_set_compression_level(png_ptr
, 3);
2107 /* Check for the cases that currently require a pre-transform on the row
2108 * before it is written. This only applies when the input is 16-bit and
2109 * either there is an alpha channel or it is converted to 8-bit.
2111 if ((linear
!= 0 && alpha
!= 0 ) ||
2112 (colormap
== 0 && display
->convert_to_8bit
!= 0))
2114 png_bytep row
= png_voidcast(png_bytep
, png_malloc(png_ptr
,
2115 png_get_rowbytes(png_ptr
, info_ptr
)));
2118 display
->local_row
= row
;
2119 if (write_16bit
!= 0)
2120 result
= png_safe_execute(image
, png_write_image_16bit
, display
);
2122 result
= png_safe_execute(image
, png_write_image_8bit
, display
);
2123 display
->local_row
= NULL
;
2125 png_free(png_ptr
, row
);
2127 /* Skip the 'write_end' on error: */
2132 /* Otherwise this is the case where the input is in a format currently
2133 * supported by the rest of the libpng write code; call it directly.
2137 png_const_bytep row
= png_voidcast(png_const_bytep
, display
->first_row
);
2138 ptrdiff_t row_bytes
= display
->row_bytes
;
2139 png_uint_32 y
= image
->height
;
2143 png_write_row(png_ptr
, row
);
2148 png_write_end(png_ptr
, info_ptr
);
2153 static void (PNGCBAPI
2154 image_memory_write
)(png_structp png_ptr
, png_bytep
/*const*/ data
,
2157 png_image_write_control
*display
= png_voidcast(png_image_write_control
*,
2158 png_ptr
->io_ptr
/*backdoor: png_get_io_ptr(png_ptr)*/);
2159 const png_alloc_size_t ob
= display
->output_bytes
;
2161 /* Check for overflow; this should never happen: */
2162 if (size
<= ((png_alloc_size_t
)-1) - ob
)
2164 /* I don't think libpng ever does this, but just in case: */
2167 if (display
->memory_bytes
>= ob
+size
) /* writing */
2168 memcpy(display
->memory
+ob
, data
, size
);
2170 /* Always update the size: */
2171 display
->output_bytes
= ob
+size
;
2176 png_error(png_ptr
, "png_image_write_to_memory: PNG too big");
2179 static void (PNGCBAPI
2180 image_memory_flush
)(png_structp png_ptr
)
2186 png_image_write_memory(png_voidp argument
)
2188 png_image_write_control
*display
= png_voidcast(png_image_write_control
*,
2191 /* The rest of the memory-specific init and write_main in an error protected
2192 * environment. This case needs to use callbacks for the write operations
2193 * since libpng has no built in support for writing to memory.
2195 png_set_write_fn(display
->image
->opaque
->png_ptr
, display
/*io_ptr*/,
2196 image_memory_write
, image_memory_flush
);
2198 return png_image_write_main(display
);
2202 png_image_write_to_memory(png_imagep image
, void *memory
,
2203 png_alloc_size_t
* PNG_RESTRICT memory_bytes
, int convert_to_8bit
,
2204 const void *buffer
, png_int_32 row_stride
, const void *colormap
)
2206 /* Write the image to the given buffer, or count the bytes if it is NULL */
2207 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
2209 if (memory_bytes
!= NULL
&& buffer
!= NULL
)
2211 /* This is to give the caller an easier error detection in the NULL
2212 * case and guard against uninitialized variable problems:
2217 if (png_image_write_init(image
) != 0)
2219 png_image_write_control display
;
2222 memset(&display
, 0, (sizeof display
));
2223 display
.image
= image
;
2224 display
.buffer
= buffer
;
2225 display
.row_stride
= row_stride
;
2226 display
.colormap
= colormap
;
2227 display
.convert_to_8bit
= convert_to_8bit
;
2228 display
.memory
= png_voidcast(png_bytep
, memory
);
2229 display
.memory_bytes
= *memory_bytes
;
2230 display
.output_bytes
= 0;
2232 result
= png_safe_execute(image
, png_image_write_memory
, &display
);
2233 png_image_free(image
);
2235 /* write_memory returns true even if we ran out of buffer. */
2238 /* On out-of-buffer this function returns '0' but still updates
2241 if (memory
!= NULL
&& display
.output_bytes
> *memory_bytes
)
2244 *memory_bytes
= display
.output_bytes
;
2255 return png_image_error(image
,
2256 "png_image_write_to_memory: invalid argument");
2259 else if (image
!= NULL
)
2260 return png_image_error(image
,
2261 "png_image_write_to_memory: incorrect PNG_IMAGE_VERSION");
2267 #ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
2269 png_image_write_to_stdio(png_imagep image
, FILE *file
, int convert_to_8bit
,
2270 const void *buffer
, png_int_32 row_stride
, const void *colormap
)
2272 /* Write the image to the given (FILE*). */
2273 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
2275 if (file
!= NULL
&& buffer
!= NULL
)
2277 if (png_image_write_init(image
) != 0)
2279 png_image_write_control display
;
2282 /* This is slightly evil, but png_init_io doesn't do anything other
2283 * than this and we haven't changed the standard IO functions so
2284 * this saves a 'safe' function.
2286 image
->opaque
->png_ptr
->io_ptr
= file
;
2288 memset(&display
, 0, (sizeof display
));
2289 display
.image
= image
;
2290 display
.buffer
= buffer
;
2291 display
.row_stride
= row_stride
;
2292 display
.colormap
= colormap
;
2293 display
.convert_to_8bit
= convert_to_8bit
;
2295 result
= png_safe_execute(image
, png_image_write_main
, &display
);
2296 png_image_free(image
);
2305 return png_image_error(image
,
2306 "png_image_write_to_stdio: invalid argument");
2309 else if (image
!= NULL
)
2310 return png_image_error(image
,
2311 "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION");
2318 png_image_write_to_file(png_imagep image
, const char *file_name
,
2319 int convert_to_8bit
, const void *buffer
, png_int_32 row_stride
,
2320 const void *colormap
)
2322 /* Write the image to the named file. */
2323 if (image
!= NULL
&& image
->version
== PNG_IMAGE_VERSION
)
2325 if (file_name
!= NULL
&& buffer
!= NULL
)
2327 FILE *fp
= fopen(file_name
, "wb");
2331 if (png_image_write_to_stdio(image
, fp
, convert_to_8bit
, buffer
,
2332 row_stride
, colormap
) != 0)
2334 int error
; /* from fflush/fclose */
2336 /* Make sure the file is flushed correctly. */
2337 if (fflush(fp
) == 0 && ferror(fp
) == 0)
2339 if (fclose(fp
) == 0)
2342 error
= errno
; /* from fclose */
2347 error
= errno
; /* from fflush or ferror */
2351 (void)remove(file_name
);
2352 /* The image has already been cleaned up; this is just used to
2353 * set the error (because the original write succeeded).
2355 return png_image_error(image
, strerror(error
));
2360 /* Clean up: just the opened file. */
2362 (void)remove(file_name
);
2368 return png_image_error(image
, strerror(errno
));
2372 return png_image_error(image
,
2373 "png_image_write_to_file: invalid argument");
2376 else if (image
!= NULL
)
2377 return png_image_error(image
,
2378 "png_image_write_to_file: incorrect PNG_IMAGE_VERSION");
2383 #endif /* SIMPLIFIED_WRITE_STDIO */
2384 #endif /* SIMPLIFIED_WRITE */