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