VirtualBox

source: vbox/trunk/src/libs/libpng-1.6.42/pngwrite.c@ 104389

Last change on this file since 104389 was 103316, checked in by vboxsync, 9 months ago

libpng-1.6.42: Applied and adjusted our libpng changes to 1.6.42. bugref:8515

  • Property svn:eol-style set to native
File size: 75.3 KB
Line 
1
2/* pngwrite.c - general routines to write a PNG file
3 *
4 * Copyright (c) 2018-2024 Cosmin Truta
5 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
6 * Copyright (c) 1996-1997 Andreas Dilger
7 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
8 *
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
12 */
13
14#include "pngpriv.h"
15#ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
16# include <errno.h>
17#endif /* SIMPLIFIED_WRITE_STDIO */
18
19#ifdef PNG_WRITE_SUPPORTED
20
21#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
22/* Write out all the unknown chunks for the current given location */
23static void
24write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr,
25 unsigned int where)
26{
27 if (info_ptr->unknown_chunks_num != 0)
28 {
29 png_const_unknown_chunkp up;
30
31 png_debug(5, "writing extra chunks");
32
33 for (up = info_ptr->unknown_chunks;
34 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
35 ++up)
36 if ((up->location & where) != 0)
37 {
38 /* If per-chunk unknown chunk handling is enabled use it, otherwise
39 * just write the chunks the application has set.
40 */
41#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
42 int keep = png_handle_as_unknown(png_ptr, up->name);
43
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
49 * unknown chunks.
50 *
51 * The default handling is also slightly weird - it is not possible to
52 * stop the writing of all unsafe-to-copy chunks!
53 *
54 * TODO: REVIEW: this would seem to be a bug.
55 */
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)))
61#endif
62 {
63 /* TODO: review, what is wrong with a zero length unknown chunk? */
64 if (up->size == 0)
65 png_warning(png_ptr, "Writing zero-length unknown chunk");
66
67 png_write_chunk(png_ptr, up->name, up->data, up->size);
68 }
69 }
70 }
71}
72#endif /* WRITE_UNKNOWN_CHUNKS */
73
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_<chunk> flag, and check info_ptr->valid before
79 * writing the chunk, as that will keep the code from breaking if you want
80 * to just write a plain PNG file. If you have long comments, I suggest
81 * writing them in png_write_end(), and compressing them.
82 */
83void PNGAPI
84png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)
85{
86 png_debug(1, "in png_write_info_before_PLTE");
87
88 if (png_ptr == NULL || info_ptr == NULL)
89 return;
90
91 if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
92 {
93 /* Write PNG signature */
94 png_write_sig(png_ptr);
95
96#ifdef PNG_MNG_FEATURES_SUPPORTED
97 if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 && \
98 png_ptr->mng_features_permitted != 0)
99 {
100 png_warning(png_ptr,
101 "MNG features are not allowed in a PNG datastream");
102 png_ptr->mng_features_permitted = 0;
103 }
104#endif
105
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
112#else
113 0
114#endif
115 );
116
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.
119 *
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.)
124 *
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'
129 * flag here too.
130 */
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);
137# endif
138#endif
139
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.
143 */
144# ifdef PNG_WRITE_iCCP_SUPPORTED
145 if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
146 (info_ptr->valid & PNG_INFO_iCCP) != 0)
147 {
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");
152# endif
153
154 png_write_iCCP(png_ptr, info_ptr->iccp_name,
155 info_ptr->iccp_profile);
156 }
157# ifdef PNG_WRITE_sRGB_SUPPORTED
158 else
159# endif
160# endif
161
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 */
168
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);
172#endif
173
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);
180# endif
181#endif
182
183#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
184 write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR);
185#endif
186
187 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
188 }
189}
190
191void PNGAPI
192png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
193{
194#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
195 int i;
196#endif
197
198 png_debug(1, "in png_write_info");
199
200 if (png_ptr == NULL || info_ptr == NULL)
201 return;
202
203 png_write_info_before_PLTE(png_ptr, info_ptr);
204
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);
208
209 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
210 png_error(png_ptr, "Valid palette required for paletted images");
211
212#ifdef PNG_WRITE_tRNS_SUPPORTED
213 if ((info_ptr->valid & PNG_INFO_tRNS) !=0)
214 {
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)
219 {
220 int j, jend;
221
222 jend = info_ptr->num_trans;
223 if (jend > PNG_MAX_PALETTE_LENGTH)
224 jend = PNG_MAX_PALETTE_LENGTH;
225
226 for (j = 0; j<jend; ++j)
227 info_ptr->trans_alpha[j] =
228 (png_byte)(255 - info_ptr->trans_alpha[j]);
229 }
230#endif
231 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
232 info_ptr->num_trans, info_ptr->color_type);
233 }
234#endif
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);
238#endif
239
240#ifdef PNG_WRITE_eXIf_SUPPORTED
241 if ((info_ptr->valid & PNG_INFO_eXIf) != 0)
242 {
243 png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif);
244 png_ptr->mode |= PNG_WROTE_eXIf;
245 }
246#endif
247
248#ifdef PNG_WRITE_hIST_SUPPORTED
249 if ((info_ptr->valid & PNG_INFO_hIST) != 0)
250 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
251#endif
252
253#ifdef PNG_WRITE_oFFs_SUPPORTED
254 if ((info_ptr->valid & PNG_INFO_oFFs) != 0)
255 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
256 info_ptr->offset_unit_type);
257#endif
258
259#ifdef PNG_WRITE_pCAL_SUPPORTED
260 if ((info_ptr->valid & PNG_INFO_pCAL) != 0)
261 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
262 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
263 info_ptr->pcal_units, info_ptr->pcal_params);
264#endif
265
266#ifdef PNG_WRITE_sCAL_SUPPORTED
267 if ((info_ptr->valid & PNG_INFO_sCAL) != 0)
268 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
269 info_ptr->scal_s_width, info_ptr->scal_s_height);
270#endif /* sCAL */
271
272#ifdef PNG_WRITE_pHYs_SUPPORTED
273 if ((info_ptr->valid & PNG_INFO_pHYs) != 0)
274 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
275 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
276#endif /* pHYs */
277
278#ifdef PNG_WRITE_tIME_SUPPORTED
279 if ((info_ptr->valid & PNG_INFO_tIME) != 0)
280 {
281 png_write_tIME(png_ptr, &(info_ptr->mod_time));
282 png_ptr->mode |= PNG_WROTE_tIME;
283 }
284#endif /* tIME */
285
286#ifdef PNG_WRITE_sPLT_SUPPORTED
287 if ((info_ptr->valid & PNG_INFO_sPLT) != 0)
288 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
289 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
290#endif /* sPLT */
291
292#ifdef PNG_WRITE_TEXT_SUPPORTED
293 /* Check to see if we need to write text chunks */
294 for (i = 0; i < info_ptr->num_text; i++)
295 {
296 png_debug2(2, "Writing header text chunk %d, type %d", i,
297 info_ptr->text[i].compression);
298 /* An internationalized chunk? */
299 if (info_ptr->text[i].compression > 0)
300 {
301#ifdef PNG_WRITE_iTXt_SUPPORTED
302 /* Write international chunk */
303 png_write_iTXt(png_ptr,
304 info_ptr->text[i].compression,
305 info_ptr->text[i].key,
306 info_ptr->text[i].lang,
307 info_ptr->text[i].lang_key,
308 info_ptr->text[i].text);
309 /* Mark this chunk as written */
310 if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
311 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
312 else
313 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
314#else
315 png_warning(png_ptr, "Unable to write international text");
316#endif
317 }
318
319 /* If we want a compressed text chunk */
320 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
321 {
322#ifdef PNG_WRITE_zTXt_SUPPORTED
323 /* Write compressed chunk */
324 png_write_zTXt(png_ptr, info_ptr->text[i].key,
325 info_ptr->text[i].text, info_ptr->text[i].compression);
326 /* Mark this chunk as written */
327 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
328#else
329 png_warning(png_ptr, "Unable to write compressed text");
330#endif
331 }
332
333 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
334 {
335#ifdef PNG_WRITE_tEXt_SUPPORTED
336 /* Write uncompressed chunk */
337 png_write_tEXt(png_ptr, info_ptr->text[i].key,
338 info_ptr->text[i].text,
339 0);
340 /* Mark this chunk as written */
341 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
342#else
343 /* Can't get here */
344 png_warning(png_ptr, "Unable to write uncompressed text");
345#endif
346 }
347 }
348#endif /* tEXt */
349
350#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
351 write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE);
352#endif
353}
354
355/* Writes the end of the PNG file. If you don't want to write comments or
356 * time information, you can pass NULL for info. If you already wrote these
357 * in png_write_info(), do not write them again here. If you have long
358 * comments, I suggest writing them here, and compressing them.
359 */
360void PNGAPI
361png_write_end(png_structrp png_ptr, png_inforp info_ptr)
362{
363 png_debug(1, "in png_write_end");
364
365 if (png_ptr == NULL)
366 return;
367
368 if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
369 png_error(png_ptr, "No IDATs written into file");
370
371#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
372 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
373 png_ptr->num_palette_max >= png_ptr->num_palette)
374 png_benign_error(png_ptr, "Wrote palette index exceeding num_palette");
375#endif
376
377 /* See if user wants us to write information chunks */
378 if (info_ptr != NULL)
379 {
380#ifdef PNG_WRITE_TEXT_SUPPORTED
381 int i; /* local index variable */
382#endif
383#ifdef PNG_WRITE_tIME_SUPPORTED
384 /* Check to see if user has supplied a time chunk */
385 if ((info_ptr->valid & PNG_INFO_tIME) != 0 &&
386 (png_ptr->mode & PNG_WROTE_tIME) == 0)
387 png_write_tIME(png_ptr, &(info_ptr->mod_time));
388
389#endif
390#ifdef PNG_WRITE_TEXT_SUPPORTED
391 /* Loop through comment chunks */
392 for (i = 0; i < info_ptr->num_text; i++)
393 {
394 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
395 info_ptr->text[i].compression);
396 /* An internationalized chunk? */
397 if (info_ptr->text[i].compression > 0)
398 {
399#ifdef PNG_WRITE_iTXt_SUPPORTED
400 /* Write international chunk */
401 png_write_iTXt(png_ptr,
402 info_ptr->text[i].compression,
403 info_ptr->text[i].key,
404 info_ptr->text[i].lang,
405 info_ptr->text[i].lang_key,
406 info_ptr->text[i].text);
407 /* Mark this chunk as written */
408 if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
409 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
410 else
411 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
412#else
413 png_warning(png_ptr, "Unable to write international text");
414#endif
415 }
416
417 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
418 {
419#ifdef PNG_WRITE_zTXt_SUPPORTED
420 /* Write compressed chunk */
421 png_write_zTXt(png_ptr, info_ptr->text[i].key,
422 info_ptr->text[i].text, info_ptr->text[i].compression);
423 /* Mark this chunk as written */
424 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
425#else
426 png_warning(png_ptr, "Unable to write compressed text");
427#endif
428 }
429
430 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
431 {
432#ifdef PNG_WRITE_tEXt_SUPPORTED
433 /* Write uncompressed chunk */
434 png_write_tEXt(png_ptr, info_ptr->text[i].key,
435 info_ptr->text[i].text, 0);
436 /* Mark this chunk as written */
437 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
438#else
439 png_warning(png_ptr, "Unable to write uncompressed text");
440#endif
441 }
442 }
443#endif
444
445#ifdef PNG_WRITE_eXIf_SUPPORTED
446 if ((info_ptr->valid & PNG_INFO_eXIf) != 0 &&
447 (png_ptr->mode & PNG_WROTE_eXIf) == 0)
448 png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif);
449#endif
450
451#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
452 write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT);
453#endif
454 }
455
456 png_ptr->mode |= PNG_AFTER_IDAT;
457
458 /* Write end of PNG file */
459 png_write_IEND(png_ptr);
460
461 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
462 * and restored again in libpng-1.2.30, may cause some applications that
463 * do not set png_ptr->output_flush_fn to crash. If your application
464 * experiences a problem, please try building libpng with
465 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
466 * png-mng-implement at lists.sf.net .
467 */
468#ifdef PNG_WRITE_FLUSH_SUPPORTED
469# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
470 png_flush(png_ptr);
471# endif
472#endif
473}
474
475#ifdef PNG_CONVERT_tIME_SUPPORTED
476void PNGAPI
477png_convert_from_struct_tm(png_timep ptime, const struct tm * ttime)
478{
479 png_debug(1, "in png_convert_from_struct_tm");
480
481 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
482 ptime->month = (png_byte)(ttime->tm_mon + 1);
483 ptime->day = (png_byte)ttime->tm_mday;
484 ptime->hour = (png_byte)ttime->tm_hour;
485 ptime->minute = (png_byte)ttime->tm_min;
486 ptime->second = (png_byte)ttime->tm_sec;
487}
488
489void PNGAPI
490png_convert_from_time_t(png_timep ptime, time_t ttime)
491{
492 struct tm *tbuf;
493
494 png_debug(1, "in png_convert_from_time_t");
495
496 tbuf = gmtime(&ttime);
497 if (tbuf == NULL)
498 {
499 /* TODO: add a safe function which takes a png_ptr argument and raises
500 * a png_error if the ttime argument is invalid and the call to gmtime
501 * fails as a consequence.
502 */
503 memset(ptime, 0, sizeof(*ptime));
504 return;
505 }
506
507 png_convert_from_struct_tm(ptime, tbuf);
508}
509#endif
510
511/* Initialize png_ptr structure, and allocate any memory needed */
512PNG_FUNCTION(png_structp,PNGAPI
513png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
514 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
515{
516#ifndef PNG_USER_MEM_SUPPORTED
517 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
518 error_fn, warn_fn, NULL, NULL, NULL);
519#else
520 return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
521 warn_fn, NULL, NULL, NULL);
522}
523
524/* Alternate initialize png_ptr structure, and allocate any memory needed */
525PNG_FUNCTION(png_structp,PNGAPI
526png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
527 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
528 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
529{
530 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
531 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
532#endif /* USER_MEM */
533 if (png_ptr != NULL)
534 {
535 /* Set the zlib control values to defaults; they can be overridden by the
536 * application after the struct has been created.
537 */
538 png_ptr->zbuffer_size = PNG_ZBUF_SIZE;
539
540 /* The 'zlib_strategy' setting is irrelevant because png_default_claim in
541 * pngwutil.c defaults it according to whether or not filters will be
542 * used, and ignores this setting.
543 */
544 png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY;
545 png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION;
546 png_ptr->zlib_mem_level = 8;
547 png_ptr->zlib_window_bits = 15;
548 png_ptr->zlib_method = 8;
549
550#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
551 png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY;
552 png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION;
553 png_ptr->zlib_text_mem_level = 8;
554 png_ptr->zlib_text_window_bits = 15;
555 png_ptr->zlib_text_method = 8;
556#endif /* WRITE_COMPRESSED_TEXT */
557
558 /* This is a highly dubious configuration option; by default it is off,
559 * but it may be appropriate for private builds that are testing
560 * extensions not conformant to the current specification, or of
561 * applications that must not fail to write at all costs!
562 */
563#ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED
564 /* In stable builds only warn if an application error can be completely
565 * handled.
566 */
567 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
568#endif
569
570 /* App warnings are warnings in release (or release candidate) builds but
571 * are errors during development.
572 */
573#if PNG_RELEASE_BUILD
574 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
575#endif
576
577 /* TODO: delay this, it can be done in png_init_io() (if the app doesn't
578 * do it itself) avoiding setting the default function if it is not
579 * required.
580 */
581 png_set_write_fn(png_ptr, NULL, NULL, NULL);
582 }
583
584 return png_ptr;
585}
586
587
588/* Write a few rows of image data. If the image is interlaced,
589 * either you will have to write the 7 sub images, or, if you
590 * have called png_set_interlace_handling(), you will have to
591 * "write" the image seven times.
592 */
593void PNGAPI
594png_write_rows(png_structrp png_ptr, png_bytepp row,
595 png_uint_32 num_rows)
596{
597 png_uint_32 i; /* row counter */
598 png_bytepp rp; /* row pointer */
599
600 png_debug(1, "in png_write_rows");
601
602 if (png_ptr == NULL)
603 return;
604
605 /* Loop through the rows */
606 for (i = 0, rp = row; i < num_rows; i++, rp++)
607 {
608 png_write_row(png_ptr, *rp);
609 }
610}
611
612/* Write the image. You only need to call this function once, even
613 * if you are writing an interlaced image.
614 */
615void PNGAPI
616png_write_image(png_structrp png_ptr, png_bytepp image)
617{
618 png_uint_32 i; /* row index */
619 int pass, num_pass; /* pass variables */
620 png_bytepp rp; /* points to current row */
621
622 if (png_ptr == NULL)
623 return;
624
625 png_debug(1, "in png_write_image");
626
627#ifdef PNG_WRITE_INTERLACING_SUPPORTED
628 /* Initialize interlace handling. If image is not interlaced,
629 * this will set pass to 1
630 */
631 num_pass = png_set_interlace_handling(png_ptr);
632#else
633 num_pass = 1;
634#endif
635 /* Loop through passes */
636 for (pass = 0; pass < num_pass; pass++)
637 {
638 /* Loop through image */
639 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
640 {
641 png_write_row(png_ptr, *rp);
642 }
643 }
644}
645
646#ifdef PNG_MNG_FEATURES_SUPPORTED
647/* Performs intrapixel differencing */
648static void
649png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
650{
651 png_debug(1, "in png_do_write_intrapixel");
652
653 if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
654 {
655 int bytes_per_pixel;
656 png_uint_32 row_width = row_info->width;
657 if (row_info->bit_depth == 8)
658 {
659 png_bytep rp;
660 png_uint_32 i;
661
662 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
663 bytes_per_pixel = 3;
664
665 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
666 bytes_per_pixel = 4;
667
668 else
669 return;
670
671 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
672 {
673 *(rp) = (png_byte)(*rp - *(rp + 1));
674 *(rp + 2) = (png_byte)(*(rp + 2) - *(rp + 1));
675 }
676 }
677
678#ifdef PNG_WRITE_16BIT_SUPPORTED
679 else if (row_info->bit_depth == 16)
680 {
681 png_bytep rp;
682 png_uint_32 i;
683
684 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
685 bytes_per_pixel = 6;
686
687 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
688 bytes_per_pixel = 8;
689
690 else
691 return;
692
693 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
694 {
695 png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1);
696 png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3);
697 png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5);
698 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
699 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
700 *(rp ) = (png_byte)(red >> 8);
701 *(rp + 1) = (png_byte)red;
702 *(rp + 4) = (png_byte)(blue >> 8);
703 *(rp + 5) = (png_byte)blue;
704 }
705 }
706#endif /* WRITE_16BIT */
707 }
708}
709#endif /* MNG_FEATURES */
710
711/* Called by user to write a row of image data */
712void PNGAPI
713png_write_row(png_structrp png_ptr, png_const_bytep row)
714{
715 /* 1.5.6: moved from png_struct to be a local structure: */
716 png_row_info row_info;
717
718 png_debug2(1, "in png_write_row (row %u, pass %d)",
719 png_ptr->row_number, png_ptr->pass);
720
721 if (png_ptr == NULL)
722 return;
723
724 /* Initialize transformations and other stuff if first time */
725 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
726 {
727 /* Make sure we wrote the header info */
728 if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
729 png_error(png_ptr,
730 "png_write_info was never called before png_write_row");
731
732 /* Check for transforms that have been set but were defined out */
733#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
734 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
735 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
736#endif
737
738#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
739 if ((png_ptr->transformations & PNG_FILLER) != 0)
740 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
741#endif
742#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
743 defined(PNG_READ_PACKSWAP_SUPPORTED)
744 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
745 png_warning(png_ptr,
746 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
747#endif
748
749#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
750 if ((png_ptr->transformations & PNG_PACK) != 0)
751 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
752#endif
753
754#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
755 if ((png_ptr->transformations & PNG_SHIFT) != 0)
756 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
757#endif
758
759#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
760 if ((png_ptr->transformations & PNG_BGR) != 0)
761 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
762#endif
763
764#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
765 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
766 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
767#endif
768
769 png_write_start_row(png_ptr);
770 }
771
772#ifdef PNG_WRITE_INTERLACING_SUPPORTED
773 /* If interlaced and not interested in row, return */
774 if (png_ptr->interlaced != 0 &&
775 (png_ptr->transformations & PNG_INTERLACE) != 0)
776 {
777 switch (png_ptr->pass)
778 {
779 case 0:
780 if ((png_ptr->row_number & 0x07) != 0)
781 {
782 png_write_finish_row(png_ptr);
783 return;
784 }
785 break;
786
787 case 1:
788 if ((png_ptr->row_number & 0x07) != 0 || png_ptr->width < 5)
789 {
790 png_write_finish_row(png_ptr);
791 return;
792 }
793 break;
794
795 case 2:
796 if ((png_ptr->row_number & 0x07) != 4)
797 {
798 png_write_finish_row(png_ptr);
799 return;
800 }
801 break;
802
803 case 3:
804 if ((png_ptr->row_number & 0x03) != 0 || png_ptr->width < 3)
805 {
806 png_write_finish_row(png_ptr);
807 return;
808 }
809 break;
810
811 case 4:
812 if ((png_ptr->row_number & 0x03) != 2)
813 {
814 png_write_finish_row(png_ptr);
815 return;
816 }
817 break;
818
819 case 5:
820 if ((png_ptr->row_number & 0x01) != 0 || png_ptr->width < 2)
821 {
822 png_write_finish_row(png_ptr);
823 return;
824 }
825 break;
826
827 case 6:
828 if ((png_ptr->row_number & 0x01) == 0)
829 {
830 png_write_finish_row(png_ptr);
831 return;
832 }
833 break;
834
835 default: /* error: ignore it */
836 break;
837 }
838 }
839#endif
840
841 /* Set up row info for transformations */
842 row_info.color_type = png_ptr->color_type;
843 row_info.width = png_ptr->usr_width;
844 row_info.channels = png_ptr->usr_channels;
845 row_info.bit_depth = png_ptr->usr_bit_depth;
846 row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
847 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
848
849 png_debug1(3, "row_info->color_type = %d", row_info.color_type);
850 png_debug1(3, "row_info->width = %u", row_info.width);
851 png_debug1(3, "row_info->channels = %d", row_info.channels);
852 png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
853 png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
854 png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
855
856 /* Copy user's row into buffer, leaving room for filter byte. */
857 memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
858
859#ifdef PNG_WRITE_INTERLACING_SUPPORTED
860 /* Handle interlacing */
861 if (png_ptr->interlaced && png_ptr->pass < 6 &&
862 (png_ptr->transformations & PNG_INTERLACE) != 0)
863 {
864 png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);
865 /* This should always get caught above, but still ... */
866 if (row_info.width == 0)
867 {
868 png_write_finish_row(png_ptr);
869 return;
870 }
871 }
872#endif
873
874#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
875 /* Handle other transformations */
876 if (png_ptr->transformations != 0)
877 png_do_write_transformations(png_ptr, &row_info);
878#endif
879
880 /* At this point the row_info pixel depth must match the 'transformed' depth,
881 * which is also the output depth.
882 */
883 if (row_info.pixel_depth != png_ptr->pixel_depth ||
884 row_info.pixel_depth != png_ptr->transformed_pixel_depth)
885 png_error(png_ptr, "internal write transform logic error");
886
887#ifdef PNG_MNG_FEATURES_SUPPORTED
888 /* Write filter_method 64 (intrapixel differencing) only if
889 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
890 * 2. Libpng did not write a PNG signature (this filter_method is only
891 * used in PNG datastreams that are embedded in MNG datastreams) and
892 * 3. The application called png_permit_mng_features with a mask that
893 * included PNG_FLAG_MNG_FILTER_64 and
894 * 4. The filter_method is 64 and
895 * 5. The color_type is RGB or RGBA
896 */
897 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
898 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
899 {
900 /* Intrapixel differencing */
901 png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
902 }
903#endif
904
905/* Added at libpng-1.5.10 */
906#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
907 /* Check for out-of-range palette index */
908 if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&
909 png_ptr->num_palette_max >= 0)
910 png_do_check_palette_indexes(png_ptr, &row_info);
911#endif
912
913 /* Find a filter if necessary, filter the row and write it out. */
914 png_write_find_filter(png_ptr, &row_info);
915
916 if (png_ptr->write_row_fn != NULL)
917 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
918}
919
920#ifdef PNG_WRITE_FLUSH_SUPPORTED
921/* Set the automatic flush interval or 0 to turn flushing off */
922void PNGAPI
923png_set_flush(png_structrp png_ptr, int nrows)
924{
925 png_debug(1, "in png_set_flush");
926
927 if (png_ptr == NULL)
928 return;
929
930 png_ptr->flush_dist = (nrows < 0 ? 0 : (png_uint_32)nrows);
931}
932
933/* Flush the current output buffers now */
934void PNGAPI
935png_write_flush(png_structrp png_ptr)
936{
937 png_debug(1, "in png_write_flush");
938
939 if (png_ptr == NULL)
940 return;
941
942 /* We have already written out all of the data */
943 if (png_ptr->row_number >= png_ptr->num_rows)
944 return;
945
946 png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);
947 png_ptr->flush_rows = 0;
948 png_flush(png_ptr);
949}
950#endif /* WRITE_FLUSH */
951
952/* Free any memory used in png_ptr struct without freeing the struct itself. */
953static void
954png_write_destroy(png_structrp png_ptr)
955{
956 png_debug(1, "in png_write_destroy");
957
958 /* Free any memory zlib uses */
959 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
960 deflateEnd(&png_ptr->zstream);
961
962 /* Free our memory. png_free checks NULL for us. */
963 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
964 png_free(png_ptr, png_ptr->row_buf);
965 png_ptr->row_buf = NULL;
966#ifdef PNG_WRITE_FILTER_SUPPORTED
967 png_free(png_ptr, png_ptr->prev_row);
968 png_free(png_ptr, png_ptr->try_row);
969 png_free(png_ptr, png_ptr->tst_row);
970 png_ptr->prev_row = NULL;
971 png_ptr->try_row = NULL;
972 png_ptr->tst_row = NULL;
973#endif
974
975#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
976 png_free(png_ptr, png_ptr->chunk_list);
977 png_ptr->chunk_list = NULL;
978#endif
979
980 /* The error handling and memory handling information is left intact at this
981 * point: the jmp_buf may still have to be freed. See png_destroy_png_struct
982 * for how this happens.
983 */
984}
985
986/* Free all memory used by the write.
987 * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
988 * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free
989 * the passed in info_structs but it would quietly fail to free any of the data
990 * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it
991 * has no png_ptr.)
992 */
993void PNGAPI
994png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
995{
996 png_debug(1, "in png_destroy_write_struct");
997
998 if (png_ptr_ptr != NULL)
999 {
1000 png_structrp png_ptr = *png_ptr_ptr;
1001
1002 if (png_ptr != NULL) /* added in libpng 1.6.0 */
1003 {
1004 png_destroy_info_struct(png_ptr, info_ptr_ptr);
1005
1006 *png_ptr_ptr = NULL;
1007 png_write_destroy(png_ptr);
1008 png_destroy_png_struct(png_ptr);
1009 }
1010 }
1011}
1012
1013/* Allow the application to select one or more row filters to use. */
1014void PNGAPI
1015png_set_filter(png_structrp png_ptr, int method, int filters)
1016{
1017 png_debug(1, "in png_set_filter");
1018
1019 if (png_ptr == NULL)
1020 return;
1021
1022#ifdef PNG_MNG_FEATURES_SUPPORTED
1023 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
1024 (method == PNG_INTRAPIXEL_DIFFERENCING))
1025 method = PNG_FILTER_TYPE_BASE;
1026
1027#endif
1028 if (method == PNG_FILTER_TYPE_BASE)
1029 {
1030 switch (filters & (PNG_ALL_FILTERS | 0x07))
1031 {
1032#ifdef PNG_WRITE_FILTER_SUPPORTED
1033 case 5:
1034 case 6:
1035 case 7: png_app_error(png_ptr, "Unknown row filter for method 0");
1036#endif /* WRITE_FILTER */
1037 /* FALLTHROUGH */
1038 case PNG_FILTER_VALUE_NONE:
1039 png_ptr->do_filter = PNG_FILTER_NONE; break;
1040
1041#ifdef PNG_WRITE_FILTER_SUPPORTED
1042 case PNG_FILTER_VALUE_SUB:
1043 png_ptr->do_filter = PNG_FILTER_SUB; break;
1044
1045 case PNG_FILTER_VALUE_UP:
1046 png_ptr->do_filter = PNG_FILTER_UP; break;
1047
1048 case PNG_FILTER_VALUE_AVG:
1049 png_ptr->do_filter = PNG_FILTER_AVG; break;
1050
1051 case PNG_FILTER_VALUE_PAETH:
1052 png_ptr->do_filter = PNG_FILTER_PAETH; break;
1053
1054 default:
1055 png_ptr->do_filter = (png_byte)filters; break;
1056#else
1057 default:
1058 png_app_error(png_ptr, "Unknown row filter for method 0");
1059#endif /* WRITE_FILTER */
1060 }
1061
1062#ifdef PNG_WRITE_FILTER_SUPPORTED
1063 /* If we have allocated the row_buf, this means we have already started
1064 * with the image and we should have allocated all of the filter buffers
1065 * that have been selected. If prev_row isn't already allocated, then
1066 * it is too late to start using the filters that need it, since we
1067 * will be missing the data in the previous row. If an application
1068 * wants to start and stop using particular filters during compression,
1069 * it should start out with all of the filters, and then remove them
1070 * or add them back after the start of compression.
1071 *
1072 * NOTE: this is a nasty constraint on the code, because it means that the
1073 * prev_row buffer must be maintained even if there are currently no
1074 * 'prev_row' requiring filters active.
1075 */
1076 if (png_ptr->row_buf != NULL)
1077 {
1078 int num_filters;
1079 png_alloc_size_t buf_size;
1080
1081 /* Repeat the checks in png_write_start_row; 1 pixel high or wide
1082 * images cannot benefit from certain filters. If this isn't done here
1083 * the check below will fire on 1 pixel high images.
1084 */
1085 if (png_ptr->height == 1)
1086 filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
1087
1088 if (png_ptr->width == 1)
1089 filters &= ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH);
1090
1091 if ((filters & (PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH)) != 0
1092 && png_ptr->prev_row == NULL)
1093 {
1094 /* This is the error case, however it is benign - the previous row
1095 * is not available so the filter can't be used. Just warn here.
1096 */
1097 png_app_warning(png_ptr,
1098 "png_set_filter: UP/AVG/PAETH cannot be added after start");
1099 filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
1100 }
1101
1102 num_filters = 0;
1103
1104 if (filters & PNG_FILTER_SUB)
1105 num_filters++;
1106
1107 if (filters & PNG_FILTER_UP)
1108 num_filters++;
1109
1110 if (filters & PNG_FILTER_AVG)
1111 num_filters++;
1112
1113 if (filters & PNG_FILTER_PAETH)
1114 num_filters++;
1115
1116 /* Allocate needed row buffers if they have not already been
1117 * allocated.
1118 */
1119 buf_size = PNG_ROWBYTES(png_ptr->usr_channels * png_ptr->usr_bit_depth,
1120 png_ptr->width) + 1;
1121
1122 if (png_ptr->try_row == NULL)
1123 png_ptr->try_row = png_voidcast(png_bytep,
1124 png_malloc(png_ptr, buf_size));
1125
1126 if (num_filters > 1)
1127 {
1128 if (png_ptr->tst_row == NULL)
1129 png_ptr->tst_row = png_voidcast(png_bytep,
1130 png_malloc(png_ptr, buf_size));
1131 }
1132 }
1133 png_ptr->do_filter = (png_byte)filters;
1134#endif
1135 }
1136 else
1137 png_error(png_ptr, "Unknown custom filter method");
1138}
1139
1140#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* DEPRECATED */
1141/* Provide floating and fixed point APIs */
1142#ifdef PNG_FLOATING_POINT_SUPPORTED
1143void PNGAPI
1144png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
1145 int num_weights, png_const_doublep filter_weights,
1146 png_const_doublep filter_costs)
1147{
1148 PNG_UNUSED(png_ptr)
1149 PNG_UNUSED(heuristic_method)
1150 PNG_UNUSED(num_weights)
1151 PNG_UNUSED(filter_weights)
1152 PNG_UNUSED(filter_costs)
1153}
1154#endif /* FLOATING_POINT */
1155
1156#ifdef PNG_FIXED_POINT_SUPPORTED
1157void PNGAPI
1158png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
1159 int num_weights, png_const_fixed_point_p filter_weights,
1160 png_const_fixed_point_p filter_costs)
1161{
1162 PNG_UNUSED(png_ptr)
1163 PNG_UNUSED(heuristic_method)
1164 PNG_UNUSED(num_weights)
1165 PNG_UNUSED(filter_weights)
1166 PNG_UNUSED(filter_costs)
1167}
1168#endif /* FIXED_POINT */
1169#endif /* WRITE_WEIGHTED_FILTER */
1170
1171#ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
1172void PNGAPI
1173png_set_compression_level(png_structrp png_ptr, int level)
1174{
1175 png_debug(1, "in png_set_compression_level");
1176
1177 if (png_ptr == NULL)
1178 return;
1179
1180 png_ptr->zlib_level = level;
1181}
1182
1183void PNGAPI
1184png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
1185{
1186 png_debug(1, "in png_set_compression_mem_level");
1187
1188 if (png_ptr == NULL)
1189 return;
1190
1191 png_ptr->zlib_mem_level = mem_level;
1192}
1193
1194void PNGAPI
1195png_set_compression_strategy(png_structrp png_ptr, int strategy)
1196{
1197 png_debug(1, "in png_set_compression_strategy");
1198
1199 if (png_ptr == NULL)
1200 return;
1201
1202 /* The flag setting here prevents the libpng dynamic selection of strategy.
1203 */
1204 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
1205 png_ptr->zlib_strategy = strategy;
1206}
1207
1208/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1209 * smaller value of window_bits if it can do so safely.
1210 */
1211void PNGAPI
1212png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
1213{
1214 png_debug(1, "in png_set_compression_window_bits");
1215
1216 if (png_ptr == NULL)
1217 return;
1218
1219 /* Prior to 1.6.0 this would warn but then set the window_bits value. This
1220 * meant that negative window bits values could be selected that would cause
1221 * libpng to write a non-standard PNG file with raw deflate or gzip
1222 * compressed IDAT or ancillary chunks. Such files can be read and there is
1223 * no warning on read, so this seems like a very bad idea.
1224 */
1225 if (window_bits > 15)
1226 {
1227 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1228 window_bits = 15;
1229 }
1230
1231 else if (window_bits < 8)
1232 {
1233 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1234 window_bits = 8;
1235 }
1236
1237 png_ptr->zlib_window_bits = window_bits;
1238}
1239
1240void PNGAPI
1241png_set_compression_method(png_structrp png_ptr, int method)
1242{
1243 png_debug(1, "in png_set_compression_method");
1244
1245 if (png_ptr == NULL)
1246 return;
1247
1248 /* This would produce an invalid PNG file if it worked, but it doesn't and
1249 * deflate will fault it, so it is harmless to just warn here.
1250 */
1251 if (method != 8)
1252 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1253
1254 png_ptr->zlib_method = method;
1255}
1256#endif /* WRITE_CUSTOMIZE_COMPRESSION */
1257
1258/* The following were added to libpng-1.5.4 */
1259#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
1260void PNGAPI
1261png_set_text_compression_level(png_structrp png_ptr, int level)
1262{
1263 png_debug(1, "in png_set_text_compression_level");
1264
1265 if (png_ptr == NULL)
1266 return;
1267
1268 png_ptr->zlib_text_level = level;
1269}
1270
1271void PNGAPI
1272png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)
1273{
1274 png_debug(1, "in png_set_text_compression_mem_level");
1275
1276 if (png_ptr == NULL)
1277 return;
1278
1279 png_ptr->zlib_text_mem_level = mem_level;
1280}
1281
1282void PNGAPI
1283png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
1284{
1285 png_debug(1, "in png_set_text_compression_strategy");
1286
1287 if (png_ptr == NULL)
1288 return;
1289
1290 png_ptr->zlib_text_strategy = strategy;
1291}
1292
1293/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1294 * smaller value of window_bits if it can do so safely.
1295 */
1296void PNGAPI
1297png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
1298{
1299 png_debug(1, "in png_set_text_compression_window_bits");
1300
1301 if (png_ptr == NULL)
1302 return;
1303
1304 if (window_bits > 15)
1305 {
1306 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1307 window_bits = 15;
1308 }
1309
1310 else if (window_bits < 8)
1311 {
1312 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1313 window_bits = 8;
1314 }
1315
1316 png_ptr->zlib_text_window_bits = window_bits;
1317}
1318
1319void PNGAPI
1320png_set_text_compression_method(png_structrp png_ptr, int method)
1321{
1322 png_debug(1, "in png_set_text_compression_method");
1323
1324 if (png_ptr == NULL)
1325 return;
1326
1327 if (method != 8)
1328 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1329
1330 png_ptr->zlib_text_method = method;
1331}
1332#endif /* WRITE_CUSTOMIZE_ZTXT_COMPRESSION */
1333/* end of API added to libpng-1.5.4 */
1334
1335void PNGAPI
1336png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
1337{
1338 png_debug(1, "in png_set_write_status_fn");
1339
1340 if (png_ptr == NULL)
1341 return;
1342
1343 png_ptr->write_row_fn = write_row_fn;
1344}
1345
1346#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1347void PNGAPI
1348png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
1349 write_user_transform_fn)
1350{
1351 png_debug(1, "in png_set_write_user_transform_fn");
1352
1353 if (png_ptr == NULL)
1354 return;
1355
1356 png_ptr->transformations |= PNG_USER_TRANSFORM;
1357 png_ptr->write_user_transform_fn = write_user_transform_fn;
1358}
1359#endif
1360
1361
1362#ifdef PNG_INFO_IMAGE_SUPPORTED
1363void PNGAPI
1364png_write_png(png_structrp png_ptr, png_inforp info_ptr,
1365 int transforms, voidp params)
1366{
1367 png_debug(1, "in png_write_png");
1368
1369 if (png_ptr == NULL || info_ptr == NULL)
1370 return;
1371
1372 if ((info_ptr->valid & PNG_INFO_IDAT) == 0)
1373 {
1374 png_app_error(png_ptr, "no rows for png_write_image to write");
1375 return;
1376 }
1377
1378 /* Write the file header information. */
1379 png_write_info(png_ptr, info_ptr);
1380
1381 /* ------ these transformations don't touch the info structure ------- */
1382
1383 /* Invert monochrome pixels */
1384 if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
1385#ifdef PNG_WRITE_INVERT_SUPPORTED
1386 png_set_invert_mono(png_ptr);
1387#else
1388 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
1389#endif
1390
1391 /* Shift the pixels up to a legal bit depth and fill in
1392 * as appropriate to correctly scale the image.
1393 */
1394 if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
1395#ifdef PNG_WRITE_SHIFT_SUPPORTED
1396 if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
1397 png_set_shift(png_ptr, &info_ptr->sig_bit);
1398#else
1399 png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
1400#endif
1401
1402 /* Pack pixels into bytes */
1403 if ((transforms & PNG_TRANSFORM_PACKING) != 0)
1404#ifdef PNG_WRITE_PACK_SUPPORTED
1405 png_set_packing(png_ptr);
1406#else
1407 png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
1408#endif
1409
1410 /* Swap location of alpha bytes from ARGB to RGBA */
1411 if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
1412#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
1413 png_set_swap_alpha(png_ptr);
1414#else
1415 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
1416#endif
1417
1418 /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into
1419 * RGB, note that the code expects the input color type to be G or RGB; no
1420 * alpha channel.
1421 */
1422 if ((transforms & (PNG_TRANSFORM_STRIP_FILLER_AFTER|
1423 PNG_TRANSFORM_STRIP_FILLER_BEFORE)) != 0)
1424 {
1425#ifdef PNG_WRITE_FILLER_SUPPORTED
1426 if ((transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) != 0)
1427 {
1428 if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
1429 png_app_error(png_ptr,
1430 "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported");
1431
1432 /* Continue if ignored - this is the pre-1.6.10 behavior */
1433 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
1434 }
1435
1436 else if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
1437 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1438#else
1439 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported");
1440#endif
1441 }
1442
1443 /* Flip BGR pixels to RGB */
1444 if ((transforms & PNG_TRANSFORM_BGR) != 0)
1445#ifdef PNG_WRITE_BGR_SUPPORTED
1446 png_set_bgr(png_ptr);
1447#else
1448 png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
1449#endif
1450
1451 /* Swap bytes of 16-bit files to most significant byte first */
1452 if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
1453#ifdef PNG_WRITE_SWAP_SUPPORTED
1454 png_set_swap(png_ptr);
1455#else
1456 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
1457#endif
1458
1459 /* Swap bits of 1-bit, 2-bit, 4-bit packed pixel formats */
1460 if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
1461#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
1462 png_set_packswap(png_ptr);
1463#else
1464 png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
1465#endif
1466
1467 /* Invert the alpha channel from opacity to transparency */
1468 if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
1469#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
1470 png_set_invert_alpha(png_ptr);
1471#else
1472 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
1473#endif
1474
1475 /* ----------------------- end of transformations ------------------- */
1476
1477 /* Write the bits */
1478 png_write_image(png_ptr, info_ptr->row_pointers);
1479
1480 /* It is REQUIRED to call this to finish writing the rest of the file */
1481 png_write_end(png_ptr, info_ptr);
1482
1483 PNG_UNUSED(params)
1484}
1485#endif
1486
1487
1488#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
1489/* Initialize the write structure - general purpose utility. */
1490static int
1491png_image_write_init(png_imagep image)
1492{
1493 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
1494 png_safe_error, png_safe_warning);
1495
1496 if (png_ptr != NULL)
1497 {
1498 png_infop info_ptr = png_create_info_struct(png_ptr);
1499
1500 if (info_ptr != NULL)
1501 {
1502 png_controlp control = png_voidcast(png_controlp,
1503 png_malloc_warn(png_ptr, (sizeof *control)));
1504
1505 if (control != NULL)
1506 {
1507 memset(control, 0, (sizeof *control));
1508
1509 control->png_ptr = png_ptr;
1510 control->info_ptr = info_ptr;
1511 control->for_write = 1;
1512
1513 image->opaque = control;
1514 return 1;
1515 }
1516
1517 /* Error clean up */
1518 png_destroy_info_struct(png_ptr, &info_ptr);
1519 }
1520
1521 png_destroy_write_struct(&png_ptr, NULL);
1522 }
1523
1524 return png_image_error(image, "png_image_write_: out of memory");
1525}
1526
1527/* Arguments to png_image_write_main: */
1528typedef struct
1529{
1530 /* Arguments: */
1531 png_imagep image;
1532 png_const_voidp buffer;
1533 png_int_32 row_stride;
1534 png_const_voidp colormap;
1535 int convert_to_8bit;
1536 /* Local variables: */
1537 png_const_voidp first_row;
1538 ptrdiff_t row_bytes;
1539 png_voidp local_row;
1540 /* Byte count for memory writing */
1541 png_bytep memory;
1542 png_alloc_size_t memory_bytes; /* not used for STDIO */
1543 png_alloc_size_t output_bytes; /* running total */
1544} png_image_write_control;
1545
1546/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
1547 * do any necessary byte swapping. The component order is defined by the
1548 * png_image format value.
1549 */
1550static int
1551png_write_image_16bit(png_voidp argument)
1552{
1553 png_image_write_control *display = png_voidcast(png_image_write_control*,
1554 argument);
1555 png_imagep image = display->image;
1556 png_structrp png_ptr = image->opaque->png_ptr;
1557
1558 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
1559 display->first_row);
1560 png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
1561 png_uint_16p row_end;
1562 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ?
1563 3 : 1;
1564 int aindex = 0;
1565 png_uint_32 y = image->height;
1566
1567 if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
1568 {
1569# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
1570 if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
1571 {
1572 aindex = -1;
1573 ++input_row; /* To point to the first component */
1574 ++output_row;
1575 }
1576 else
1577 aindex = (int)channels;
1578# else
1579 aindex = (int)channels;
1580# endif
1581 }
1582
1583 else
1584 png_error(png_ptr, "png_write_image: internal call error");
1585
1586 /* Work out the output row end and count over this, note that the increment
1587 * above to 'row' means that row_end can actually be beyond the end of the
1588 * row; this is correct.
1589 */
1590 row_end = output_row + image->width * (channels+1);
1591
1592 for (; y > 0; --y)
1593 {
1594 png_const_uint_16p in_ptr = input_row;
1595 png_uint_16p out_ptr = output_row;
1596
1597 while (out_ptr < row_end)
1598 {
1599 png_uint_16 alpha = in_ptr[aindex];
1600 png_uint_32 reciprocal = 0;
1601 int c;
1602
1603 out_ptr[aindex] = alpha;
1604
1605 /* Calculate a reciprocal. The correct calculation is simply
1606 * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
1607 * allows correct rounding by adding .5 before the shift. 'reciprocal'
1608 * is only initialized when required.
1609 */
1610 if (alpha > 0 && alpha < 65535)
1611 reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
1612
1613 c = (int)channels;
1614 do /* always at least one channel */
1615 {
1616 png_uint_16 component = *in_ptr++;
1617
1618 /* The following gives 65535 for an alpha of 0, which is fine,
1619 * otherwise if 0/0 is represented as some other value there is more
1620 * likely to be a discontinuity which will probably damage
1621 * compression when moving from a fully transparent area to a
1622 * nearly transparent one. (The assumption here is that opaque
1623 * areas tend not to be 0 intensity.)
1624 */
1625 if (component >= alpha)
1626 component = 65535;
1627
1628 /* component<alpha, so component/alpha is less than one and
1629 * component*reciprocal is less than 2^31.
1630 */
1631 else if (component > 0 && alpha < 65535)
1632 {
1633 png_uint_32 calc = component * reciprocal;
1634 calc += 16384; /* round to nearest */
1635 component = (png_uint_16)(calc >> 15);
1636 }
1637
1638 *out_ptr++ = component;
1639 }
1640 while (--c > 0);
1641
1642 /* Skip to next component (skip the intervening alpha channel) */
1643 ++in_ptr;
1644 ++out_ptr;
1645 }
1646
1647 png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
1648 input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
1649 }
1650
1651 return 1;
1652}
1653
1654/* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel
1655 * is present it must be removed from the components, the components are then
1656 * written in sRGB encoding. No components are added or removed.
1657 *
1658 * Calculate an alpha reciprocal to reverse pre-multiplication. As above the
1659 * calculation can be done to 15 bits of accuracy; however, the output needs to
1660 * be scaled in the range 0..255*65535, so include that scaling here.
1661 */
1662# define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+((alpha)>>1))/(alpha))
1663
1664static png_byte
1665png_unpremultiply(png_uint_32 component, png_uint_32 alpha,
1666 png_uint_32 reciprocal/*from the above macro*/)
1667{
1668 /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
1669 * is represented as some other value there is more likely to be a
1670 * discontinuity which will probably damage compression when moving from a
1671 * fully transparent area to a nearly transparent one. (The assumption here
1672 * is that opaque areas tend not to be 0 intensity.)
1673 *
1674 * There is a rounding problem here; if alpha is less than 128 it will end up
1675 * as 0 when scaled to 8 bits. To avoid introducing spurious colors into the
1676 * output change for this too.
1677 */
1678 if (component >= alpha || alpha < 128)
1679 return 255;
1680
1681 /* component<alpha, so component/alpha is less than one and
1682 * component*reciprocal is less than 2^31.
1683 */
1684 else if (component > 0)
1685 {
1686 /* The test is that alpha/257 (rounded) is less than 255, the first value
1687 * that becomes 255 is 65407.
1688 * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
1689 * be exact!) [Could also test reciprocal != 0]
1690 */
1691 if (alpha < 65407)
1692 {
1693 component *= reciprocal;
1694 component += 64; /* round to nearest */
1695 component >>= 7;
1696 }
1697
1698 else
1699 component *= 255;
1700
1701 /* Convert the component to sRGB. */
1702 return (png_byte)PNG_sRGB_FROM_LINEAR(component);
1703 }
1704
1705 else
1706 return 0;
1707}
1708
1709static int
1710png_write_image_8bit(png_voidp argument)
1711{
1712 png_image_write_control *display = png_voidcast(png_image_write_control*,
1713 argument);
1714 png_imagep image = display->image;
1715 png_structrp png_ptr = image->opaque->png_ptr;
1716
1717 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
1718 display->first_row);
1719 png_bytep output_row = png_voidcast(png_bytep, display->local_row);
1720 png_uint_32 y = image->height;
1721 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ?
1722 3 : 1;
1723
1724 if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
1725 {
1726 png_bytep row_end;
1727 int aindex;
1728
1729# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
1730 if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
1731 {
1732 aindex = -1;
1733 ++input_row; /* To point to the first component */
1734 ++output_row;
1735 }
1736
1737 else
1738# endif
1739 aindex = (int)channels;
1740
1741 /* Use row_end in place of a loop counter: */
1742 row_end = output_row + image->width * (channels+1);
1743
1744 for (; y > 0; --y)
1745 {
1746 png_const_uint_16p in_ptr = input_row;
1747 png_bytep out_ptr = output_row;
1748
1749 while (out_ptr < row_end)
1750 {
1751 png_uint_16 alpha = in_ptr[aindex];
1752 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
1753 png_uint_32 reciprocal = 0;
1754 int c;
1755
1756 /* Scale and write the alpha channel. */
1757 out_ptr[aindex] = alphabyte;
1758
1759 if (alphabyte > 0 && alphabyte < 255)
1760 reciprocal = UNP_RECIPROCAL(alpha);
1761
1762 c = (int)channels;
1763 do /* always at least one channel */
1764 *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
1765 while (--c > 0);
1766
1767 /* Skip to next component (skip the intervening alpha channel) */
1768 ++in_ptr;
1769 ++out_ptr;
1770 } /* while out_ptr < row_end */
1771
1772 png_write_row(png_ptr, png_voidcast(png_const_bytep,
1773 display->local_row));
1774 input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
1775 } /* while y */
1776 }
1777
1778 else
1779 {
1780 /* No alpha channel, so the row_end really is the end of the row and it
1781 * is sufficient to loop over the components one by one.
1782 */
1783 png_bytep row_end = output_row + image->width * channels;
1784
1785 for (; y > 0; --y)
1786 {
1787 png_const_uint_16p in_ptr = input_row;
1788 png_bytep out_ptr = output_row;
1789
1790 while (out_ptr < row_end)
1791 {
1792 png_uint_32 component = *in_ptr++;
1793
1794 component *= 255;
1795 *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
1796 }
1797
1798 png_write_row(png_ptr, output_row);
1799 input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
1800 }
1801 }
1802
1803 return 1;
1804}
1805
1806static void
1807png_image_set_PLTE(png_image_write_control *display)
1808{
1809 png_imagep image = display->image;
1810 const void *cmap = display->colormap;
1811 int entries = image->colormap_entries > 256 ? 256 :
1812 (int)image->colormap_entries;
1813
1814 /* NOTE: the caller must check for cmap != NULL and entries != 0 */
1815 png_uint_32 format = image->format;
1816 unsigned int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
1817
1818# if defined(PNG_FORMAT_BGR_SUPPORTED) &&\
1819 defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)
1820 int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1821 (format & PNG_FORMAT_FLAG_ALPHA) != 0;
1822# else
1823# define afirst 0
1824# endif
1825
1826# ifdef PNG_FORMAT_BGR_SUPPORTED
1827 int bgr = (format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
1828# else
1829# define bgr 0
1830# endif
1831
1832 int i, num_trans;
1833 png_color palette[256];
1834 png_byte tRNS[256];
1835
1836 memset(tRNS, 255, (sizeof tRNS));
1837 memset(palette, 0, (sizeof palette));
1838
1839 for (i=num_trans=0; i<entries; ++i)
1840 {
1841 /* This gets automatically converted to sRGB with reversal of the
1842 * pre-multiplication if the color-map has an alpha channel.
1843 */
1844 if ((format & PNG_FORMAT_FLAG_LINEAR) != 0)
1845 {
1846 png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
1847
1848 entry += (unsigned int)i * channels;
1849
1850 if ((channels & 1) != 0) /* no alpha */
1851 {
1852 if (channels >= 3) /* RGB */
1853 {
1854 palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1855 entry[(2 ^ bgr)]);
1856 palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1857 entry[1]);
1858 palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1859 entry[bgr]);
1860 }
1861
1862 else /* Gray */
1863 palette[i].blue = palette[i].red = palette[i].green =
1864 (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);
1865 }
1866
1867 else /* alpha */
1868 {
1869 png_uint_16 alpha = entry[afirst ? 0 : channels-1];
1870 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
1871 png_uint_32 reciprocal = 0;
1872
1873 /* Calculate a reciprocal, as in the png_write_image_8bit code above
1874 * this is designed to produce a value scaled to 255*65535 when
1875 * divided by 128 (i.e. asr 7).
1876 */
1877 if (alphabyte > 0 && alphabyte < 255)
1878 reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
1879
1880 tRNS[i] = alphabyte;
1881 if (alphabyte < 255)
1882 num_trans = i+1;
1883
1884 if (channels >= 3) /* RGB */
1885 {
1886 palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],
1887 alpha, reciprocal);
1888 palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,
1889 reciprocal);
1890 palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,
1891 reciprocal);
1892 }
1893
1894 else /* gray */
1895 palette[i].blue = palette[i].red = palette[i].green =
1896 png_unpremultiply(entry[afirst], alpha, reciprocal);
1897 }
1898 }
1899
1900 else /* Color-map has sRGB values */
1901 {
1902 png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
1903
1904 entry += (unsigned int)i * channels;
1905
1906 switch (channels)
1907 {
1908 case 4:
1909 tRNS[i] = entry[afirst ? 0 : 3];
1910 if (tRNS[i] < 255)
1911 num_trans = i+1;
1912 /* FALLTHROUGH */
1913 case 3:
1914 palette[i].blue = entry[afirst + (2 ^ bgr)];
1915 palette[i].green = entry[afirst + 1];
1916 palette[i].red = entry[afirst + bgr];
1917 break;
1918
1919 case 2:
1920 tRNS[i] = entry[1 ^ afirst];
1921 if (tRNS[i] < 255)
1922 num_trans = i+1;
1923 /* FALLTHROUGH */
1924 case 1:
1925 palette[i].blue = palette[i].red = palette[i].green =
1926 entry[afirst];
1927 break;
1928
1929 default:
1930 break;
1931 }
1932 }
1933 }
1934
1935# ifdef afirst
1936# undef afirst
1937# endif
1938# ifdef bgr
1939# undef bgr
1940# endif
1941
1942 png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,
1943 entries);
1944
1945 if (num_trans > 0)
1946 png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
1947 num_trans, NULL);
1948
1949 image->colormap_entries = (png_uint_32)entries;
1950}
1951
1952static int
1953png_image_write_main(png_voidp argument)
1954{
1955 png_image_write_control *display = png_voidcast(png_image_write_control*,
1956 argument);
1957 png_imagep image = display->image;
1958 png_structrp png_ptr = image->opaque->png_ptr;
1959 png_inforp info_ptr = image->opaque->info_ptr;
1960 png_uint_32 format = image->format;
1961
1962 /* The following four ints are actually booleans */
1963 int colormap = (format & PNG_FORMAT_FLAG_COLORMAP);
1964 int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR); /* input */
1965 int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA);
1966 int write_16bit = linear && (display->convert_to_8bit == 0);
1967
1968# ifdef PNG_BENIGN_ERRORS_SUPPORTED
1969 /* Make sure we error out on any bad situation */
1970 png_set_benign_errors(png_ptr, 0/*error*/);
1971# endif
1972
1973 /* Default the 'row_stride' parameter if required, also check the row stride
1974 * and total image size to ensure that they are within the system limits.
1975 */
1976 {
1977 unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);
1978
1979 if (image->width <= 0x7fffffffU/channels) /* no overflow */
1980 {
1981 png_uint_32 check;
1982 png_uint_32 png_row_stride = image->width * channels;
1983
1984 if (display->row_stride == 0)
1985 display->row_stride = (png_int_32)/*SAFE*/png_row_stride;
1986
1987 if (display->row_stride < 0)
1988 check = (png_uint_32)(-display->row_stride);
1989
1990 else
1991 check = (png_uint_32)display->row_stride;
1992
1993 if (check >= png_row_stride)
1994 {
1995 /* Now check for overflow of the image buffer calculation; this
1996 * limits the whole image size to 32 bits for API compatibility with
1997 * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
1998 */
1999 if (image->height > 0xffffffffU/png_row_stride)
2000 png_error(image->opaque->png_ptr, "memory image too large");
2001 }
2002
2003 else
2004 png_error(image->opaque->png_ptr, "supplied row stride too small");
2005 }
2006
2007 else
2008 png_error(image->opaque->png_ptr, "image row stride too large");
2009 }
2010
2011 /* Set the required transforms then write the rows in the correct order. */
2012 if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0)
2013 {
2014 if (display->colormap != NULL && image->colormap_entries > 0)
2015 {
2016 png_uint_32 entries = image->colormap_entries;
2017
2018 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
2019 entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),
2020 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
2021 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2022
2023 png_image_set_PLTE(display);
2024 }
2025
2026 else
2027 png_error(image->opaque->png_ptr,
2028 "no color-map for color-mapped image");
2029 }
2030
2031 else
2032 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
2033 write_16bit ? 16 : 8,
2034 ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
2035 ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
2036 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2037
2038 /* Counter-intuitively the data transformations must be called *after*
2039 * png_write_info, not before as in the read code, but the 'set' functions
2040 * must still be called before. Just set the color space information, never
2041 * write an interlaced image.
2042 */
2043
2044 if (write_16bit != 0)
2045 {
2046 /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
2047 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
2048
2049 if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
2050 png_set_cHRM_fixed(png_ptr, info_ptr,
2051 /* color x y */
2052 /* white */ 31270, 32900,
2053 /* red */ 64000, 33000,
2054 /* green */ 30000, 60000,
2055 /* blue */ 15000, 6000
2056 );
2057 }
2058
2059 else if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
2060 png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
2061
2062 /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
2063 * space must still be gamma encoded.
2064 */
2065 else
2066 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
2067
2068 /* Write the file header. */
2069 png_write_info(png_ptr, info_ptr);
2070
2071 /* Now set up the data transformations (*after* the header is written),
2072 * remove the handled transformations from the 'format' flags for checking.
2073 *
2074 * First check for a little endian system if writing 16-bit files.
2075 */
2076 if (write_16bit != 0)
2077 {
2078 png_uint_16 le = 0x0001;
2079
2080 if ((*(png_const_bytep) & le) != 0)
2081 png_set_swap(png_ptr);
2082 }
2083
2084# ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
2085 if ((format & PNG_FORMAT_FLAG_BGR) != 0)
2086 {
2087 if (colormap == 0 && (format & PNG_FORMAT_FLAG_COLOR) != 0)
2088 png_set_bgr(png_ptr);
2089 format &= ~PNG_FORMAT_FLAG_BGR;
2090 }
2091# endif
2092
2093# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
2094 if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
2095 {
2096 if (colormap == 0 && (format & PNG_FORMAT_FLAG_ALPHA) != 0)
2097 png_set_swap_alpha(png_ptr);
2098 format &= ~PNG_FORMAT_FLAG_AFIRST;
2099 }
2100# endif
2101
2102 /* If there are 16 or fewer color-map entries we wrote a lower bit depth
2103 * above, but the application data is still byte packed.
2104 */
2105 if (colormap != 0 && image->colormap_entries <= 16)
2106 png_set_packing(png_ptr);
2107
2108 /* That should have handled all (both) the transforms. */
2109 if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
2110 PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)
2111 png_error(png_ptr, "png_write_image: unsupported transformation");
2112
2113 {
2114 png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
2115 ptrdiff_t row_bytes = display->row_stride;
2116
2117 if (linear != 0)
2118 row_bytes *= (sizeof (png_uint_16));
2119
2120 if (row_bytes < 0)
2121 row += (image->height-1) * (-row_bytes);
2122
2123 display->first_row = row;
2124 display->row_bytes = row_bytes;
2125 }
2126
2127 /* Apply 'fast' options if the flag is set. */
2128 if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)
2129 {
2130 png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);
2131 /* NOTE: determined by experiment using pngstest, this reflects some
2132 * balance between the time to write the image once and the time to read
2133 * it about 50 times. The speed-up in pngstest was about 10-20% of the
2134 * total (user) time on a heavily loaded system.
2135 */
2136# ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
2137 png_set_compression_level(png_ptr, 3);
2138# endif
2139 }
2140
2141 /* Check for the cases that currently require a pre-transform on the row
2142 * before it is written. This only applies when the input is 16-bit and
2143 * either there is an alpha channel or it is converted to 8-bit.
2144 */
2145 if ((linear != 0 && alpha != 0 ) ||
2146 (colormap == 0 && display->convert_to_8bit != 0))
2147 {
2148 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
2149 png_get_rowbytes(png_ptr, info_ptr)));
2150 int result;
2151
2152 display->local_row = row;
2153 if (write_16bit != 0)
2154 result = png_safe_execute(image, png_write_image_16bit, display);
2155 else
2156 result = png_safe_execute(image, png_write_image_8bit, display);
2157 display->local_row = NULL;
2158
2159 png_free(png_ptr, row);
2160
2161 /* Skip the 'write_end' on error: */
2162 if (result == 0)
2163 return 0;
2164 }
2165
2166 /* Otherwise this is the case where the input is in a format currently
2167 * supported by the rest of the libpng write code; call it directly.
2168 */
2169 else
2170 {
2171 png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
2172 ptrdiff_t row_bytes = display->row_bytes;
2173 png_uint_32 y = image->height;
2174
2175 for (; y > 0; --y)
2176 {
2177 png_write_row(png_ptr, row);
2178 row += row_bytes;
2179 }
2180 }
2181
2182 png_write_end(png_ptr, info_ptr);
2183 return 1;
2184}
2185
2186
2187static void (PNGCBAPI
2188image_memory_write)(png_structp png_ptr, png_bytep/*const*/ data, size_t size)
2189{
2190 png_image_write_control *display = png_voidcast(png_image_write_control*,
2191 png_ptr->io_ptr/*backdoor: png_get_io_ptr(png_ptr)*/);
2192 png_alloc_size_t ob = display->output_bytes;
2193
2194 /* Check for overflow; this should never happen: */
2195 if (size <= ((png_alloc_size_t)-1) - ob)
2196 {
2197 /* I don't think libpng ever does this, but just in case: */
2198 if (size > 0)
2199 {
2200 if (display->memory_bytes >= ob+size) /* writing */
2201 memcpy(display->memory+ob, data, size);
2202
2203 /* Always update the size: */
2204 display->output_bytes = ob+size;
2205 }
2206 }
2207
2208 else
2209 png_error(png_ptr, "png_image_write_to_memory: PNG too big");
2210}
2211
2212static void (PNGCBAPI
2213image_memory_flush)(png_structp png_ptr)
2214{
2215 PNG_UNUSED(png_ptr)
2216}
2217
2218static int
2219png_image_write_memory(png_voidp argument)
2220{
2221 png_image_write_control *display = png_voidcast(png_image_write_control*,
2222 argument);
2223
2224 /* The rest of the memory-specific init and write_main in an error protected
2225 * environment. This case needs to use callbacks for the write operations
2226 * since libpng has no built in support for writing to memory.
2227 */
2228 png_set_write_fn(display->image->opaque->png_ptr, display/*io_ptr*/,
2229 image_memory_write, image_memory_flush);
2230
2231 return png_image_write_main(display);
2232}
2233
2234int PNGAPI
2235png_image_write_to_memory(png_imagep image, void *memory,
2236 png_alloc_size_t * PNG_RESTRICT memory_bytes, int convert_to_8bit,
2237 const void *buffer, png_int_32 row_stride, const void *colormap)
2238{
2239 /* Write the image to the given buffer, or count the bytes if it is NULL */
2240 if (image != NULL && image->version == PNG_IMAGE_VERSION)
2241 {
2242 if (memory_bytes != NULL && buffer != NULL)
2243 {
2244 /* This is to give the caller an easier error detection in the NULL
2245 * case and guard against uninitialized variable problems:
2246 */
2247 if (memory == NULL)
2248 *memory_bytes = 0;
2249
2250 if (png_image_write_init(image) != 0)
2251 {
2252 png_image_write_control display;
2253 int result;
2254
2255 memset(&display, 0, (sizeof display));
2256 display.image = image;
2257 display.buffer = buffer;
2258 display.row_stride = row_stride;
2259 display.colormap = colormap;
2260 display.convert_to_8bit = convert_to_8bit;
2261 display.memory = png_voidcast(png_bytep, memory);
2262 display.memory_bytes = *memory_bytes;
2263 display.output_bytes = 0;
2264
2265 result = png_safe_execute(image, png_image_write_memory, &display);
2266 png_image_free(image);
2267
2268 /* write_memory returns true even if we ran out of buffer. */
2269 if (result)
2270 {
2271 /* On out-of-buffer this function returns '0' but still updates
2272 * memory_bytes:
2273 */
2274 if (memory != NULL && display.output_bytes > *memory_bytes)
2275 result = 0;
2276
2277 *memory_bytes = display.output_bytes;
2278 }
2279
2280 return result;
2281 }
2282
2283 else
2284 return 0;
2285 }
2286
2287 else
2288 return png_image_error(image,
2289 "png_image_write_to_memory: invalid argument");
2290 }
2291
2292 else if (image != NULL)
2293 return png_image_error(image,
2294 "png_image_write_to_memory: incorrect PNG_IMAGE_VERSION");
2295
2296 else
2297 return 0;
2298}
2299
2300#ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
2301int PNGAPI
2302png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
2303 const void *buffer, png_int_32 row_stride, const void *colormap)
2304{
2305 /* Write the image to the given (FILE*). */
2306 if (image != NULL && image->version == PNG_IMAGE_VERSION)
2307 {
2308 if (file != NULL && buffer != NULL)
2309 {
2310 if (png_image_write_init(image) != 0)
2311 {
2312 png_image_write_control display;
2313 int result;
2314
2315 /* This is slightly evil, but png_init_io doesn't do anything other
2316 * than this and we haven't changed the standard IO functions so
2317 * this saves a 'safe' function.
2318 */
2319 image->opaque->png_ptr->io_ptr = file;
2320
2321 memset(&display, 0, (sizeof display));
2322 display.image = image;
2323 display.buffer = buffer;
2324 display.row_stride = row_stride;
2325 display.colormap = colormap;
2326 display.convert_to_8bit = convert_to_8bit;
2327
2328 result = png_safe_execute(image, png_image_write_main, &display);
2329 png_image_free(image);
2330 return result;
2331 }
2332
2333 else
2334 return 0;
2335 }
2336
2337 else
2338 return png_image_error(image,
2339 "png_image_write_to_stdio: invalid argument");
2340 }
2341
2342 else if (image != NULL)
2343 return png_image_error(image,
2344 "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION");
2345
2346 else
2347 return 0;
2348}
2349
2350int PNGAPI
2351png_image_write_to_file(png_imagep image, const char *file_name,
2352 int convert_to_8bit, const void *buffer, png_int_32 row_stride,
2353 const void *colormap)
2354{
2355 /* Write the image to the named file. */
2356 if (image != NULL && image->version == PNG_IMAGE_VERSION)
2357 {
2358 if (file_name != NULL && buffer != NULL)
2359 {
2360 FILE *fp = fopen(file_name, "wb");
2361
2362 if (fp != NULL)
2363 {
2364 if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
2365 row_stride, colormap) != 0)
2366 {
2367 int error; /* from fflush/fclose */
2368
2369 /* Make sure the file is flushed correctly. */
2370 if (fflush(fp) == 0 && ferror(fp) == 0)
2371 {
2372 if (fclose(fp) == 0)
2373 return 1;
2374
2375 error = errno; /* from fclose */
2376 }
2377
2378 else
2379 {
2380 error = errno; /* from fflush or ferror */
2381 (void)fclose(fp);
2382 }
2383
2384 (void)remove(file_name);
2385 /* The image has already been cleaned up; this is just used to
2386 * set the error (because the original write succeeded).
2387 */
2388 return png_image_error(image, strerror(error));
2389 }
2390
2391 else
2392 {
2393 /* Clean up: just the opened file. */
2394 (void)fclose(fp);
2395 (void)remove(file_name);
2396 return 0;
2397 }
2398 }
2399
2400 else
2401 return png_image_error(image, strerror(errno));
2402 }
2403
2404 else
2405 return png_image_error(image,
2406 "png_image_write_to_file: invalid argument");
2407 }
2408
2409 else if (image != NULL)
2410 return png_image_error(image,
2411 "png_image_write_to_file: incorrect PNG_IMAGE_VERSION");
2412
2413 else
2414 return 0;
2415}
2416#endif /* SIMPLIFIED_WRITE_STDIO */
2417#endif /* SIMPLIFIED_WRITE */
2418#endif /* WRITE */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette