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 | 632a84e | 2010-03-09 22:28:33 -0600 | [diff] [blame^] | 4 | * Last changed in libpng 1.5.0 [March 10, 2010] |
Glenn Randers-Pehrson | e69b55d | 2010-01-01 10:29:06 -0600 | [diff] [blame] | 5 | * Copyright (c) 1998-2010 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" |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 15 | |
Glenn Randers-Pehrson | c3cd22b | 2010-03-08 21:10:25 -0600 | [diff] [blame] | 16 | #ifdef PNG_WRITE_SUPPORTED |
| 17 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 18 | /* Writes all the PNG information. This is the suggested way to use the |
| 19 | * library. If you have a new chunk to add, make a function to write it, |
| 20 | * and put it in the correct location here. If you want the chunk written |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 21 | * 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] | 22 | * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing |
| 23 | * the chunk, as that will keep the code from breaking if you want to just |
| 24 | * write a plain PNG file. If you have long comments, I suggest writing |
| 25 | * them in png_write_end(), and compressing them. |
| 26 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 27 | void PNGAPI |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 28 | png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 29 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 30 | png_debug(1, "in png_write_info_before_PLTE"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 31 | |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 32 | if (png_ptr == NULL || info_ptr == NULL) |
| 33 | return; |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 34 | if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE)) |
| 35 | { |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 36 | /* Write PNG signature */ |
| 37 | png_write_sig(png_ptr); |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 38 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 39 | if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \ |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 40 | (png_ptr->mng_features_permitted)) |
Glenn Randers-Pehrson | 408b421 | 2000-12-18 09:33:57 -0600 | [diff] [blame] | 41 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 42 | 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] | 43 | png_ptr->mng_features_permitted = 0; |
Glenn Randers-Pehrson | 408b421 | 2000-12-18 09:33:57 -0600 | [diff] [blame] | 44 | } |
| 45 | #endif |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 46 | /* Write IHDR information. */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 47 | png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 48 | info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type, |
| 49 | info_ptr->filter_type, |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 50 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 51 | info_ptr->interlace_type); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 52 | #else |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 53 | 0); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 54 | #endif |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 55 | /* The rest of these check to see if the valid field has the appropriate |
| 56 | * flag set, and if it does, writes the chunk. |
| 57 | */ |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 58 | #ifdef PNG_WRITE_gAMA_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 59 | if (info_ptr->valid & PNG_INFO_gAMA) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 60 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 61 | # ifdef PNG_FLOATING_POINT_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 62 | png_write_gAMA(png_ptr, info_ptr->gamma); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 63 | #else |
| 64 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 65 | png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 66 | # endif |
| 67 | #endif |
| 68 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 69 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 70 | #ifdef PNG_WRITE_sRGB_SUPPORTED |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 71 | if (info_ptr->valid & PNG_INFO_sRGB) |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 72 | png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 73 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 74 | #ifdef PNG_WRITE_iCCP_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 75 | if (info_ptr->valid & PNG_INFO_iCCP) |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 76 | png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE, |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 77 | info_ptr->iccp_profile, (int)info_ptr->iccp_proflen); |
| 78 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 79 | #ifdef PNG_WRITE_sBIT_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 80 | if (info_ptr->valid & PNG_INFO_sBIT) |
| 81 | 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] | 82 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 83 | #ifdef PNG_WRITE_cHRM_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 84 | if (info_ptr->valid & PNG_INFO_cHRM) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 85 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 86 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 87 | png_write_cHRM(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 88 | info_ptr->x_white, info_ptr->y_white, |
| 89 | info_ptr->x_red, info_ptr->y_red, |
| 90 | info_ptr->x_green, info_ptr->y_green, |
| 91 | info_ptr->x_blue, info_ptr->y_blue); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 92 | #else |
| 93 | # ifdef PNG_FIXED_POINT_SUPPORTED |
| 94 | png_write_cHRM_fixed(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 95 | info_ptr->int_x_white, info_ptr->int_y_white, |
| 96 | info_ptr->int_x_red, info_ptr->int_y_red, |
| 97 | info_ptr->int_x_green, info_ptr->int_y_green, |
| 98 | info_ptr->int_x_blue, info_ptr->int_y_blue); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 99 | # endif |
| 100 | #endif |
| 101 | } |
| 102 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 103 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 104 | if (info_ptr->unknown_chunks_num) |
| 105 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 106 | png_unknown_chunk *up; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 107 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 108 | png_debug(5, "writing extra chunks"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 109 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 110 | for (up = info_ptr->unknown_chunks; |
| 111 | up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; |
| 112 | up++) |
| 113 | { |
Glenn Randers-Pehrson | 614b91d | 2009-10-17 19:00:18 -0500 | [diff] [blame] | 114 | int keep = png_handle_as_unknown(png_ptr, up->name); |
Glenn Randers-Pehrson | dff799e | 2004-08-07 21:42:49 -0500 | [diff] [blame] | 115 | if (keep != PNG_HANDLE_CHUNK_NEVER && |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 116 | up->location && !(up->location & PNG_HAVE_PLTE) && |
| 117 | !(up->location & PNG_HAVE_IDAT) && |
| 118 | ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || |
| 119 | (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 120 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 121 | if (up->size == 0) |
| 122 | png_warning(png_ptr, "Writing zero-length unknown chunk"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 123 | png_write_chunk(png_ptr, up->name, up->data, up->size); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 124 | } |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 125 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 126 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 127 | #endif |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 128 | png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE; |
| 129 | } |
| 130 | } |
| 131 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 132 | void PNGAPI |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 133 | png_write_info(png_structp png_ptr, png_infop info_ptr) |
| 134 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 135 | #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 136 | int i; |
| 137 | #endif |
| 138 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 139 | png_debug(1, "in png_write_info"); |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 140 | |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 141 | if (png_ptr == NULL || info_ptr == NULL) |
| 142 | return; |
| 143 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 144 | png_write_info_before_PLTE(png_ptr, info_ptr); |
| 145 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 146 | if (info_ptr->valid & PNG_INFO_PLTE) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 147 | png_write_PLTE(png_ptr, info_ptr->palette, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 148 | (png_uint_32)info_ptr->num_palette); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 149 | else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Glenn Randers-Pehrson | c3d51c1 | 2006-03-02 07:23:18 -0600 | [diff] [blame] | 150 | png_error(png_ptr, "Valid palette required for paletted images"); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 151 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 152 | #ifdef PNG_WRITE_tRNS_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 153 | if (info_ptr->valid & PNG_INFO_tRNS) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 154 | { |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 155 | #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 156 | /* Invert the alpha channel (in tRNS) */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 157 | if ((png_ptr->transformations & PNG_INVERT_ALPHA) && |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 158 | info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 159 | { |
| 160 | int j; |
Glenn Randers-Pehrson | 614b91d | 2009-10-17 19:00:18 -0500 | [diff] [blame] | 161 | for (j = 0; j<(int)info_ptr->num_trans; j++) |
Glenn Randers-Pehrson | 6abea75 | 2009-08-08 16:52:06 -0500 | [diff] [blame] | 162 | info_ptr->trans_alpha[j] = (png_byte)(255 - info_ptr->trans_alpha[j]); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 163 | } |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 164 | #endif |
Glenn Randers-Pehrson | 6abea75 | 2009-08-08 16:52:06 -0500 | [diff] [blame] | 165 | 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] | 166 | info_ptr->num_trans, info_ptr->color_type); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 167 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 168 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 169 | #ifdef PNG_WRITE_bKGD_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 170 | if (info_ptr->valid & PNG_INFO_bKGD) |
| 171 | png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 172 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 173 | #ifdef PNG_WRITE_hIST_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 174 | if (info_ptr->valid & PNG_INFO_hIST) |
| 175 | png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 176 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 177 | #ifdef PNG_WRITE_oFFs_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 178 | if (info_ptr->valid & PNG_INFO_oFFs) |
| 179 | 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] | 180 | info_ptr->offset_unit_type); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 181 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 182 | #ifdef PNG_WRITE_pCAL_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 183 | if (info_ptr->valid & PNG_INFO_pCAL) |
| 184 | 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] | 185 | info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams, |
| 186 | info_ptr->pcal_units, info_ptr->pcal_params); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 187 | #endif |
Glenn Randers-Pehrson | 5902059 | 2009-05-18 10:52:12 -0500 | [diff] [blame] | 188 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 189 | #ifdef PNG_sCAL_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 190 | if (info_ptr->valid & PNG_INFO_sCAL) |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 191 | #ifdef PNG_WRITE_sCAL_SUPPORTED |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 192 | #if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 193 | png_write_sCAL(png_ptr, (int)info_ptr->scal_unit, |
| 194 | info_ptr->scal_pixel_width, info_ptr->scal_pixel_height); |
Glenn Randers-Pehrson | 5902059 | 2009-05-18 10:52:12 -0500 | [diff] [blame] | 195 | #else /* !FLOATING_POINT */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 196 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 197 | png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit, |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 198 | 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] | 199 | #endif /* FIXED_POINT */ |
| 200 | #endif /* FLOATING_POINT */ |
Glenn Randers-Pehrson | 5902059 | 2009-05-18 10:52:12 -0500 | [diff] [blame] | 201 | #else /* !WRITE_sCAL */ |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 202 | png_warning(png_ptr, |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 203 | "png_write_sCAL not supported; sCAL chunk not written"); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 204 | #endif /* WRITE_sCAL */ |
| 205 | #endif /* sCAL */ |
| 206 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 207 | #ifdef PNG_WRITE_pHYs_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 208 | if (info_ptr->valid & PNG_INFO_pHYs) |
| 209 | png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 210 | 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] | 211 | #endif /* pHYs */ |
| 212 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 213 | #ifdef PNG_WRITE_tIME_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 214 | if (info_ptr->valid & PNG_INFO_tIME) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 215 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 216 | png_write_tIME(png_ptr, &(info_ptr->mod_time)); |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 217 | png_ptr->mode |= PNG_WROTE_tIME; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 218 | } |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 219 | #endif /* tIME */ |
| 220 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 221 | #ifdef PNG_WRITE_sPLT_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 222 | if (info_ptr->valid & PNG_INFO_sPLT) |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 223 | for (i = 0; i < (int)info_ptr->splt_palettes_num; i++) |
| 224 | 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] | 225 | #endif /* sPLT */ |
| 226 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 227 | #ifdef PNG_WRITE_TEXT_SUPPORTED |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 228 | /* Check to see if we need to write text chunks */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 229 | for (i = 0; i < info_ptr->num_text; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 230 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 231 | png_debug2(2, "Writing header text chunk %d, type %d", i, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 232 | info_ptr->text[i].compression); |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 233 | /* An internationalized chunk? */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 234 | if (info_ptr->text[i].compression > 0) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 235 | { |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 236 | #ifdef PNG_WRITE_iTXt_SUPPORTED |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 237 | /* Write international chunk */ |
| 238 | png_write_iTXt(png_ptr, |
| 239 | info_ptr->text[i].compression, |
| 240 | info_ptr->text[i].key, |
| 241 | info_ptr->text[i].lang, |
| 242 | info_ptr->text[i].lang_key, |
| 243 | info_ptr->text[i].text); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 244 | #else |
Glenn Randers-Pehrson | c3d51c1 | 2006-03-02 07:23:18 -0600 | [diff] [blame] | 245 | png_warning(png_ptr, "Unable to write international text"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 246 | #endif |
| 247 | /* Mark this chunk as written */ |
| 248 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; |
| 249 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 250 | /* If we want a compressed text chunk */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 251 | else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 252 | { |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 253 | #ifdef PNG_WRITE_zTXt_SUPPORTED |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 254 | /* Write compressed chunk */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 255 | png_write_zTXt(png_ptr, info_ptr->text[i].key, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 256 | info_ptr->text[i].text, 0, |
| 257 | info_ptr->text[i].compression); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 258 | #else |
Glenn Randers-Pehrson | c3d51c1 | 2006-03-02 07:23:18 -0600 | [diff] [blame] | 259 | png_warning(png_ptr, "Unable to write compressed text"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 260 | #endif |
| 261 | /* Mark this chunk as written */ |
| 262 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; |
| 263 | } |
| 264 | else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) |
| 265 | { |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 266 | #ifdef PNG_WRITE_tEXt_SUPPORTED |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 267 | /* Write uncompressed chunk */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 268 | png_write_tEXt(png_ptr, info_ptr->text[i].key, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 269 | info_ptr->text[i].text, |
| 270 | 0); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 271 | /* Mark this chunk as written */ |
| 272 | 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] | 273 | #else |
| 274 | /* Can't get here */ |
| 275 | png_warning(png_ptr, "Unable to write uncompressed text"); |
| 276 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 277 | } |
| 278 | } |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 279 | #endif /* tEXt */ |
| 280 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 281 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 282 | if (info_ptr->unknown_chunks_num) |
| 283 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 284 | png_unknown_chunk *up; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 285 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 286 | png_debug(5, "writing extra chunks"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 287 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 288 | for (up = info_ptr->unknown_chunks; |
| 289 | up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; |
| 290 | up++) |
| 291 | { |
Glenn Randers-Pehrson | 614b91d | 2009-10-17 19:00:18 -0500 | [diff] [blame] | 292 | int keep = png_handle_as_unknown(png_ptr, up->name); |
Glenn Randers-Pehrson | dff799e | 2004-08-07 21:42:49 -0500 | [diff] [blame] | 293 | if (keep != PNG_HANDLE_CHUNK_NEVER && |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 294 | up->location && (up->location & PNG_HAVE_PLTE) && |
| 295 | !(up->location & PNG_HAVE_IDAT) && |
| 296 | ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || |
| 297 | (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 298 | { |
| 299 | png_write_chunk(png_ptr, up->name, up->data, up->size); |
| 300 | } |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 301 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 302 | } |
| 303 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 304 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 305 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 306 | /* 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] | 307 | * time information, you can pass NULL for info. If you already wrote these |
| 308 | * in png_write_info(), do not write them again here. If you have long |
| 309 | * comments, I suggest writing them here, and compressing them. |
| 310 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 311 | void PNGAPI |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 312 | png_write_end(png_structp png_ptr, png_infop info_ptr) |
| 313 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 314 | png_debug(1, "in png_write_end"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 315 | |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 316 | if (png_ptr == NULL) |
| 317 | return; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 318 | if (!(png_ptr->mode & PNG_HAVE_IDAT)) |
| 319 | png_error(png_ptr, "No IDATs written into file"); |
| 320 | |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 321 | /* See if user wants us to write information chunks */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 322 | if (info_ptr != NULL) |
| 323 | { |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 324 | #ifdef PNG_WRITE_TEXT_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 325 | int i; /* local index variable */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 326 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 327 | #ifdef PNG_WRITE_tIME_SUPPORTED |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 328 | /* Check to see if user has supplied a time chunk */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 329 | if ((info_ptr->valid & PNG_INFO_tIME) && |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 330 | !(png_ptr->mode & PNG_WROTE_tIME)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 331 | png_write_tIME(png_ptr, &(info_ptr->mod_time)); |
| 332 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 333 | #ifdef PNG_WRITE_TEXT_SUPPORTED |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 334 | /* Loop through comment chunks */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 335 | for (i = 0; i < info_ptr->num_text; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 336 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 337 | png_debug2(2, "Writing trailer text chunk %d, type %d", i, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 338 | info_ptr->text[i].compression); |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 339 | /* An internationalized chunk? */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 340 | if (info_ptr->text[i].compression > 0) |
| 341 | { |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 342 | #ifdef PNG_WRITE_iTXt_SUPPORTED |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 343 | /* Write international chunk */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 344 | png_write_iTXt(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 345 | info_ptr->text[i].compression, |
| 346 | info_ptr->text[i].key, |
| 347 | info_ptr->text[i].lang, |
| 348 | info_ptr->text[i].lang_key, |
| 349 | info_ptr->text[i].text); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 350 | #else |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 351 | png_warning(png_ptr, "Unable to write international text"); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 352 | #endif |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 353 | /* Mark this chunk as written */ |
| 354 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 355 | } |
| 356 | else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 357 | { |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 358 | #ifdef PNG_WRITE_zTXt_SUPPORTED |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 359 | /* Write compressed chunk */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 360 | png_write_zTXt(png_ptr, info_ptr->text[i].key, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 361 | info_ptr->text[i].text, 0, |
| 362 | info_ptr->text[i].compression); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 363 | #else |
Glenn Randers-Pehrson | c3d51c1 | 2006-03-02 07:23:18 -0600 | [diff] [blame] | 364 | png_warning(png_ptr, "Unable to write compressed text"); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 365 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 366 | /* Mark this chunk as written */ |
| 367 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 368 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 369 | else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 370 | { |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 371 | #ifdef PNG_WRITE_tEXt_SUPPORTED |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 372 | /* Write uncompressed chunk */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 373 | png_write_tEXt(png_ptr, info_ptr->text[i].key, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 374 | info_ptr->text[i].text, 0); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 375 | #else |
Glenn Randers-Pehrson | c3d51c1 | 2006-03-02 07:23:18 -0600 | [diff] [blame] | 376 | png_warning(png_ptr, "Unable to write uncompressed text"); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 377 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 378 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 379 | /* Mark this chunk as written */ |
| 380 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 381 | } |
| 382 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 383 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 384 | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 385 | if (info_ptr->unknown_chunks_num) |
| 386 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 387 | png_unknown_chunk *up; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 388 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 389 | png_debug(5, "writing extra chunks"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 390 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 391 | for (up = info_ptr->unknown_chunks; |
| 392 | up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; |
| 393 | up++) |
| 394 | { |
Glenn Randers-Pehrson | 614b91d | 2009-10-17 19:00:18 -0500 | [diff] [blame] | 395 | int keep = png_handle_as_unknown(png_ptr, up->name); |
Glenn Randers-Pehrson | dff799e | 2004-08-07 21:42:49 -0500 | [diff] [blame] | 396 | if (keep != PNG_HANDLE_CHUNK_NEVER && |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 397 | up->location && (up->location & PNG_AFTER_IDAT) && |
| 398 | ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || |
| 399 | (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 400 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 401 | png_write_chunk(png_ptr, up->name, up->data, up->size); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 402 | } |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 403 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 404 | } |
| 405 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 406 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 407 | |
| 408 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 409 | |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 410 | /* Write end of PNG file */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 411 | png_write_IEND(png_ptr); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 412 | /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03, |
| 413 | * and restored again in libpng-1.2.30, may cause some applications that |
| 414 | * 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] | 415 | * experiences a problem, please try building libpng with |
| 416 | * 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] | 417 | * png-mng-implement at lists.sf.net . |
| 418 | */ |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 419 | #ifdef PNG_WRITE_FLUSH_SUPPORTED |
Glenn Randers-Pehrson | edcd6e1 | 2009-11-20 20:28:29 -0600 | [diff] [blame] | 420 | # ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED |
Glenn Randers-Pehrson | 32fc5ce | 2000-07-24 06:34:14 -0500 | [diff] [blame] | 421 | png_flush(png_ptr); |
Glenn Randers-Pehrson | edcd6e1 | 2009-11-20 20:28:29 -0600 | [diff] [blame] | 422 | # endif |
Glenn Randers-Pehrson | dbed41f | 2008-08-19 18:20:52 -0500 | [diff] [blame] | 423 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 424 | } |
| 425 | |
Glenn Randers-Pehrson | 65d235a | 2009-11-02 11:32:30 -0600 | [diff] [blame] | 426 | #ifdef PNG_CONVERT_tIME_SUPPORTED |
Glenn Randers-Pehrson | 99106de | 2009-11-01 16:26:14 -0600 | [diff] [blame] | 427 | /* "tm" structure is not supported on WindowsCE */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 428 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 429 | png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 430 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 431 | png_debug(1, "in png_convert_from_struct_tm"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 432 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 433 | ptime->year = (png_uint_16)(1900 + ttime->tm_year); |
| 434 | ptime->month = (png_byte)(ttime->tm_mon + 1); |
| 435 | ptime->day = (png_byte)ttime->tm_mday; |
| 436 | ptime->hour = (png_byte)ttime->tm_hour; |
| 437 | ptime->minute = (png_byte)ttime->tm_min; |
| 438 | ptime->second = (png_byte)ttime->tm_sec; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 439 | } |
| 440 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 441 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 442 | png_convert_from_time_t(png_timep ptime, time_t ttime) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 443 | { |
| 444 | struct tm *tbuf; |
| 445 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 446 | png_debug(1, "in png_convert_from_time_t"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 447 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 448 | tbuf = gmtime(&ttime); |
| 449 | png_convert_from_struct_tm(ptime, tbuf); |
| 450 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 451 | #endif |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 452 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 453 | /* Initialize png_ptr structure, and allocate any memory needed */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 454 | png_structp PNGAPI |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 455 | png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 456 | png_error_ptr error_fn, png_error_ptr warn_fn) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 457 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 458 | #ifdef PNG_USER_MEM_SUPPORTED |
| 459 | return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 460 | warn_fn, NULL, NULL, NULL)); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* Alternate initialize png_ptr structure, and allocate any memory needed */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 464 | png_structp PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 465 | 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] | 466 | png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, |
| 467 | png_malloc_ptr malloc_fn, png_free_ptr free_fn) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 468 | { |
| 469 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 62ca98e | 2009-12-20 15:14:57 -0600 | [diff] [blame] | 470 | volatile int png_cleanup_needed = 0; |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 471 | #ifdef PNG_SETJMP_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 472 | volatile |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 473 | #endif |
Glenn Randers-Pehrson | f0a8fe0 | 2009-04-14 08:28:15 -0500 | [diff] [blame] | 474 | png_structp png_ptr; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 475 | #ifdef PNG_SETJMP_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 476 | #ifdef USE_FAR_KEYWORD |
Glenn Randers-Pehrson | 95ca51b | 2010-02-19 14:09:09 -0600 | [diff] [blame] | 477 | jmp_buf png_jmpbuf; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 478 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 479 | #endif |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 480 | int i; |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 481 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 482 | png_debug(1, "in png_create_write_struct"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 483 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 484 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 485 | png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 486 | (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 487 | #else |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 488 | png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 489 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 490 | if (png_ptr == NULL) |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 491 | return (NULL); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 492 | |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 493 | /* Added at libpng-1.2.6 */ |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 494 | #ifdef PNG_SET_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | 614b91d | 2009-10-17 19:00:18 -0500 | [diff] [blame] | 495 | png_ptr->user_width_max = PNG_USER_WIDTH_MAX; |
| 496 | png_ptr->user_height_max = PNG_USER_HEIGHT_MAX; |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 497 | #endif |
| 498 | |
Glenn Randers-Pehrson | f0a8fe0 | 2009-04-14 08:28:15 -0500 | [diff] [blame] | 499 | #ifdef PNG_SETJMP_SUPPORTED |
| 500 | /* Applications that neglect to set up their own setjmp() and then |
| 501 | encounter a png_error() will longjmp here. Since the jmpbuf is |
| 502 | then meaningless we abort instead of returning. */ |
| 503 | #ifdef USE_FAR_KEYWORD |
Glenn Randers-Pehrson | 95ca51b | 2010-02-19 14:09:09 -0600 | [diff] [blame] | 504 | if (setjmp(png_jmpbuf)) |
Glenn Randers-Pehrson | f0a8fe0 | 2009-04-14 08:28:15 -0500 | [diff] [blame] | 505 | #else |
Glenn Randers-Pehrson | f4ea224 | 2009-11-20 21:38:24 -0600 | [diff] [blame] | 506 | if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */ |
| 507 | #endif |
| 508 | #ifdef USE_FAR_KEYWORD |
Glenn Randers-Pehrson | 95ca51b | 2010-02-19 14:09:09 -0600 | [diff] [blame] | 509 | png_memcpy(png_jmpbuf(png_ptr), png_jmpbuf, png_sizeof(jmp_buf)); |
Glenn Randers-Pehrson | f0a8fe0 | 2009-04-14 08:28:15 -0500 | [diff] [blame] | 510 | #endif |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 511 | PNG_ABORT(); |
Glenn Randers-Pehrson | f0a8fe0 | 2009-04-14 08:28:15 -0500 | [diff] [blame] | 512 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 513 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 514 | #ifdef PNG_USER_MEM_SUPPORTED |
| 515 | png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn); |
| 516 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 517 | png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 518 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 519 | if (user_png_ver) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 520 | { |
Glenn Randers-Pehrson | 614b91d | 2009-10-17 19:00:18 -0500 | [diff] [blame] | 521 | i = 0; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 522 | do |
| 523 | { |
| 524 | if (user_png_ver[i] != png_libpng_ver[i]) |
| 525 | png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; |
| 526 | } while (png_libpng_ver[i++]); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 527 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 528 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 529 | if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) |
Glenn Randers-Pehrson | 98c9d73 | 2000-05-03 21:06:11 -0500 | [diff] [blame] | 530 | { |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 531 | /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so |
| 532 | * we must recompile any applications that use any older library version. |
| 533 | * For versions after libpng 1.0, we will be compatible, so we need |
| 534 | * only check the first digit. |
| 535 | */ |
| 536 | if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] || |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 537 | (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) || |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 538 | (user_png_ver[0] == '0' && user_png_ver[2] < '9')) |
| 539 | { |
Glenn Randers-Pehrson | 6a9e480 | 2010-02-19 09:47:43 -0600 | [diff] [blame] | 540 | #ifdef PNG_CONSOLE_IO_SUPPORTED |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 541 | char msg[80]; |
| 542 | if (user_png_ver) |
| 543 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 544 | png_snprintf(msg, 80, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 545 | "Application was compiled with png.h from libpng-%.20s", |
| 546 | user_png_ver); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 547 | png_warning(png_ptr, msg); |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 548 | } |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 549 | png_snprintf(msg, 80, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 550 | "Application is running with png.c from libpng-%.20s", |
| 551 | png_libpng_ver); |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 552 | png_warning(png_ptr, msg); |
| 553 | #endif |
| 554 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
Glenn Randers-Pehrson | 614b91d | 2009-10-17 19:00:18 -0500 | [diff] [blame] | 555 | png_ptr->flags = 0; |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 556 | #endif |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 557 | png_warning(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 558 | "Incompatible libpng version in application and library"); |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 559 | png_cleanup_needed = 1; |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 560 | } |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 561 | } |
Glenn Randers-Pehrson | 98c9d73 | 2000-05-03 21:06:11 -0500 | [diff] [blame] | 562 | |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 563 | /* Initialize zbuf - compression buffer */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 564 | png_ptr->zbuf_size = PNG_ZBUF_SIZE; |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 565 | if (!png_cleanup_needed) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 566 | { |
| 567 | png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 568 | png_ptr->zbuf_size); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 569 | if (png_ptr->zbuf == NULL) |
| 570 | png_cleanup_needed = 1; |
| 571 | } |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 572 | if (png_cleanup_needed) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 573 | { |
| 574 | /* Clean up PNG structure and deallocate any memory. */ |
| 575 | png_free(png_ptr, png_ptr->zbuf); |
| 576 | png_ptr->zbuf = NULL; |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 577 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 578 | png_destroy_struct_2((png_voidp)png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 579 | (png_free_ptr)free_fn, (png_voidp)mem_ptr); |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 580 | #else |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 581 | png_destroy_struct((png_voidp)png_ptr); |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 582 | #endif |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 583 | return (NULL); |
| 584 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 585 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 586 | png_set_write_fn(png_ptr, NULL, NULL, NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 587 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 588 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 589 | png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 590 | 1, NULL, NULL); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 591 | #endif |
| 592 | |
Glenn Randers-Pehrson | 03008a0 | 2002-04-27 10:11:25 -0500 | [diff] [blame] | 593 | return (png_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 594 | } |
| 595 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 596 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 597 | /* Write a few rows of image data. If the image is interlaced, |
| 598 | * either you will have to write the 7 sub images, or, if you |
| 599 | * have called png_set_interlace_handling(), you will have to |
| 600 | * "write" the image seven times. |
| 601 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 602 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 603 | png_write_rows(png_structp png_ptr, png_bytepp row, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 604 | png_uint_32 num_rows) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 605 | { |
| 606 | png_uint_32 i; /* row counter */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 607 | png_bytepp rp; /* row pointer */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 608 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 609 | png_debug(1, "in png_write_rows"); |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 610 | |
| 611 | if (png_ptr == NULL) |
| 612 | return; |
| 613 | |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 614 | /* Loop through the rows */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 615 | for (i = 0, rp = row; i < num_rows; i++, rp++) |
| 616 | { |
| 617 | png_write_row(png_ptr, *rp); |
| 618 | } |
| 619 | } |
| 620 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 621 | /* Write the image. You only need to call this function once, even |
| 622 | * if you are writing an interlaced image. |
| 623 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 624 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 625 | png_write_image(png_structp png_ptr, png_bytepp image) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 626 | { |
| 627 | png_uint_32 i; /* row index */ |
| 628 | int pass, num_pass; /* pass variables */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 629 | png_bytepp rp; /* points to current row */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 630 | |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 631 | if (png_ptr == NULL) |
| 632 | return; |
| 633 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 634 | png_debug(1, "in png_write_image"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 635 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 636 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 637 | /* Initialize interlace handling. If image is not interlaced, |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 638 | * this will set pass to 1 |
| 639 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 640 | num_pass = png_set_interlace_handling(png_ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 641 | #else |
| 642 | num_pass = 1; |
| 643 | #endif |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 644 | /* Loop through passes */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 645 | for (pass = 0; pass < num_pass; pass++) |
| 646 | { |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 647 | /* Loop through image */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 648 | for (i = 0, rp = image; i < png_ptr->height; i++, rp++) |
| 649 | { |
| 650 | png_write_row(png_ptr, *rp); |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 655 | /* Called by user to write a row of image data */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 656 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 657 | png_write_row(png_structp png_ptr, png_bytep row) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 658 | { |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 659 | if (png_ptr == NULL) |
| 660 | return; |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 661 | |
Glenn Randers-Pehrson | 632a84e | 2010-03-09 22:28:33 -0600 | [diff] [blame^] | 662 | png_debug2(1, "in png_write_row (row %u, pass %d)", |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 663 | png_ptr->row_number, png_ptr->pass); |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 664 | |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 665 | /* Initialize transformations and other stuff if first time */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 666 | if (png_ptr->row_number == 0 && png_ptr->pass == 0) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 667 | { |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 668 | /* Make sure we wrote the header info */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 669 | if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE)) |
| 670 | png_error(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 671 | "png_write_info was never called before png_write_row"); |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 672 | |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 673 | /* Check for transforms that have been set but were defined out */ |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 674 | #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] | 675 | if (png_ptr->transformations & PNG_INVERT_MONO) |
| 676 | png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 677 | #endif |
| 678 | #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] | 679 | if (png_ptr->transformations & PNG_FILLER) |
| 680 | png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 681 | #endif |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 682 | #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ |
| 683 | defined(PNG_READ_PACKSWAP_SUPPORTED) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 684 | if (png_ptr->transformations & PNG_PACKSWAP) |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 685 | png_warning(png_ptr, |
| 686 | "PNG_WRITE_PACKSWAP_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 687 | #endif |
| 688 | #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] | 689 | if (png_ptr->transformations & PNG_PACK) |
| 690 | png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 691 | #endif |
| 692 | #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] | 693 | if (png_ptr->transformations & PNG_SHIFT) |
| 694 | png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 695 | #endif |
| 696 | #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] | 697 | if (png_ptr->transformations & PNG_BGR) |
| 698 | png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 699 | #endif |
| 700 | #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] | 701 | if (png_ptr->transformations & PNG_SWAP_BYTES) |
| 702 | png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 703 | #endif |
| 704 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 705 | png_write_start_row(png_ptr); |
| 706 | } |
| 707 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 708 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 709 | /* If interlaced and not interested in row, return */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 710 | if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) |
| 711 | { |
| 712 | switch (png_ptr->pass) |
| 713 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 714 | case 0: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 715 | if (png_ptr->row_number & 0x07) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 716 | { |
| 717 | png_write_finish_row(png_ptr); |
| 718 | return; |
| 719 | } |
| 720 | break; |
| 721 | case 1: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 722 | if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 723 | { |
| 724 | png_write_finish_row(png_ptr); |
| 725 | return; |
| 726 | } |
| 727 | break; |
| 728 | case 2: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 729 | if ((png_ptr->row_number & 0x07) != 4) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 730 | { |
| 731 | png_write_finish_row(png_ptr); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 732 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 733 | } |
| 734 | break; |
| 735 | case 3: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 736 | if ((png_ptr->row_number & 0x03) || png_ptr->width < 3) |
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; |
| 742 | case 4: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 743 | if ((png_ptr->row_number & 0x03) != 2) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 744 | { |
| 745 | png_write_finish_row(png_ptr); |
| 746 | return; |
| 747 | } |
| 748 | break; |
| 749 | case 5: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 750 | if ((png_ptr->row_number & 0x01) || png_ptr->width < 2) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 751 | { |
| 752 | png_write_finish_row(png_ptr); |
| 753 | return; |
| 754 | } |
| 755 | break; |
| 756 | case 6: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 757 | if (!(png_ptr->row_number & 0x01)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 758 | { |
| 759 | png_write_finish_row(png_ptr); |
| 760 | return; |
| 761 | } |
| 762 | break; |
| 763 | } |
| 764 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 765 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 766 | |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 767 | /* Set up row info for transformations */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 768 | png_ptr->row_info.color_type = png_ptr->color_type; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 769 | png_ptr->row_info.width = png_ptr->usr_width; |
| 770 | png_ptr->row_info.channels = png_ptr->usr_channels; |
| 771 | png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 772 | png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth * |
| 773 | png_ptr->row_info.channels); |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 774 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 775 | png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, |
| 776 | png_ptr->row_info.width); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 777 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 778 | png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type); |
Glenn Randers-Pehrson | 632a84e | 2010-03-09 22:28:33 -0600 | [diff] [blame^] | 779 | png_debug1(3, "row_info->width = %u", png_ptr->row_info.width); |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 780 | png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels); |
| 781 | png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth); |
| 782 | png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth); |
Glenn Randers-Pehrson | 632a84e | 2010-03-09 22:28:33 -0600 | [diff] [blame^] | 783 | png_debug1(3, "row_info->rowbytes = %u", png_ptr->row_info.rowbytes); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 784 | |
| 785 | /* Copy user's row into buffer, leaving room for filter byte. */ |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 786 | png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 787 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 788 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 789 | /* Handle interlacing */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 790 | if (png_ptr->interlaced && png_ptr->pass < 6 && |
| 791 | (png_ptr->transformations & PNG_INTERLACE)) |
| 792 | { |
| 793 | png_do_write_interlace(&(png_ptr->row_info), |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 794 | png_ptr->row_buf + 1, png_ptr->pass); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 795 | /* This should always get caught above, but still ... */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 796 | if (!(png_ptr->row_info.width)) |
| 797 | { |
| 798 | png_write_finish_row(png_ptr); |
| 799 | return; |
| 800 | } |
| 801 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 802 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 803 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 804 | /* Handle other transformations */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 805 | if (png_ptr->transformations) |
| 806 | png_do_write_transformations(png_ptr); |
| 807 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 808 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
Glenn Randers-Pehrson | 408b421 | 2000-12-18 09:33:57 -0600 | [diff] [blame] | 809 | /* Write filter_method 64 (intrapixel differencing) only if |
| 810 | * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and |
| 811 | * 2. Libpng did not write a PNG signature (this filter_method is only |
| 812 | * used in PNG datastreams that are embedded in MNG datastreams) and |
| 813 | * 3. The application called png_permit_mng_features with a mask that |
| 814 | * included PNG_FLAG_MNG_FILTER_64 and |
| 815 | * 4. The filter_method is 64 and |
| 816 | * 5. The color_type is RGB or RGBA |
| 817 | */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 818 | if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 819 | (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) |
| 820 | { |
| 821 | /* Intrapixel differencing */ |
| 822 | png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 823 | } |
| 824 | #endif |
| 825 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 826 | /* Find a filter if necessary, filter the row and write it out. */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 827 | png_write_find_filter(png_ptr, &(png_ptr->row_info)); |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 828 | |
| 829 | if (png_ptr->write_row_fn != NULL) |
| 830 | (*(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] | 831 | } |
| 832 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 833 | #ifdef PNG_WRITE_FLUSH_SUPPORTED |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 834 | /* Set the automatic flush interval or 0 to turn flushing off */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 835 | void PNGAPI |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 836 | png_set_flush(png_structp png_ptr, int nrows) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 837 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 838 | png_debug(1, "in png_set_flush"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 839 | |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 840 | if (png_ptr == NULL) |
| 841 | return; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 842 | png_ptr->flush_dist = (nrows < 0 ? 0 : nrows); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 843 | } |
| 844 | |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 845 | /* Flush the current output buffers now */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 846 | void PNGAPI |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 847 | png_write_flush(png_structp png_ptr) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 848 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 849 | int wrote_IDAT; |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 850 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 851 | png_debug(1, "in png_write_flush"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 852 | |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 853 | if (png_ptr == NULL) |
| 854 | return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 855 | /* We have already written out all of the data */ |
| 856 | 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] | 857 | return; |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 858 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 859 | do |
| 860 | { |
| 861 | int ret; |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 862 | |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 863 | /* Compress the data */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 864 | ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 865 | wrote_IDAT = 0; |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 866 | |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 867 | /* Check for compression errors */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 868 | if (ret != Z_OK) |
| 869 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 870 | if (png_ptr->zstream.msg != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 871 | png_error(png_ptr, png_ptr->zstream.msg); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 872 | else |
| 873 | png_error(png_ptr, "zlib error"); |
| 874 | } |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 875 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 876 | if (!(png_ptr->zstream.avail_out)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 877 | { |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 878 | /* Write the IDAT and reset the zlib output buffer */ |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 879 | png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 880 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 881 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 882 | wrote_IDAT = 1; |
| 883 | } |
| 884 | } while(wrote_IDAT == 1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 885 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 886 | /* If there is any data left to be output, write it into a new IDAT */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 887 | if (png_ptr->zbuf_size != png_ptr->zstream.avail_out) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 888 | { |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 889 | /* Write the IDAT and reset the zlib output buffer */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 890 | png_write_IDAT(png_ptr, png_ptr->zbuf, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 891 | png_ptr->zbuf_size - png_ptr->zstream.avail_out); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 892 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 893 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 894 | } |
| 895 | png_ptr->flush_rows = 0; |
| 896 | png_flush(png_ptr); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 897 | } |
| 898 | #endif /* PNG_WRITE_FLUSH_SUPPORTED */ |
| 899 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 900 | /* Free all memory used by the write */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 901 | void PNGAPI |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 902 | png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 903 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 904 | png_structp png_ptr = NULL; |
| 905 | png_infop info_ptr = NULL; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 906 | #ifdef PNG_USER_MEM_SUPPORTED |
| 907 | png_free_ptr free_fn = NULL; |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 908 | png_voidp mem_ptr = NULL; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 909 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 910 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 911 | png_debug(1, "in png_destroy_write_struct"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 912 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 913 | if (png_ptr_ptr != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 914 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 915 | png_ptr = *png_ptr_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 916 | #ifdef PNG_USER_MEM_SUPPORTED |
| 917 | free_fn = png_ptr->free_fn; |
Glenn Randers-Pehrson | fcbd787 | 2002-04-07 16:35:38 -0500 | [diff] [blame] | 918 | mem_ptr = png_ptr->mem_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 919 | #endif |
| 920 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 921 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 922 | #ifdef PNG_USER_MEM_SUPPORTED |
| 923 | if (png_ptr != NULL) |
| 924 | { |
| 925 | free_fn = png_ptr->free_fn; |
| 926 | mem_ptr = png_ptr->mem_ptr; |
| 927 | } |
| 928 | #endif |
| 929 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 930 | if (info_ptr_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 931 | info_ptr = *info_ptr_ptr; |
| 932 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 933 | if (info_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 934 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 935 | if (png_ptr != NULL) |
| 936 | { |
| 937 | png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 938 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 939 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 940 | if (png_ptr->num_chunk_list) |
| 941 | { |
| 942 | png_free(png_ptr, png_ptr->chunk_list); |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 943 | png_ptr->num_chunk_list = 0; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 944 | } |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 945 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 946 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 947 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 948 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 949 | png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn, |
| 950 | (png_voidp)mem_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 951 | #else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 952 | png_destroy_struct((png_voidp)info_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 953 | #endif |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 954 | *info_ptr_ptr = NULL; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 955 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 956 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 957 | if (png_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 958 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 959 | png_write_destroy(png_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 960 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 961 | png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 962 | (png_voidp)mem_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 963 | #else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 964 | png_destroy_struct((png_voidp)png_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 965 | #endif |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 966 | *png_ptr_ptr = NULL; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
| 970 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 971 | /* Free any memory used in png_ptr struct (old method) */ |
Glenn Randers-Pehrson | f64a06f | 2001-04-11 07:38:00 -0500 | [diff] [blame] | 972 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 973 | png_write_destroy(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 974 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 975 | #ifdef PNG_SETJMP_SUPPORTED |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 976 | jmp_buf tmp_jmp; /* Save jump buffer */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 977 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 978 | png_error_ptr error_fn; |
| 979 | png_error_ptr warning_fn; |
| 980 | png_voidp error_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 981 | #ifdef PNG_USER_MEM_SUPPORTED |
| 982 | png_free_ptr free_fn; |
| 983 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 984 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 985 | png_debug(1, "in png_write_destroy"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 986 | |
| 987 | /* Free any memory zlib uses */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 988 | deflateEnd(&png_ptr->zstream); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 989 | |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 990 | /* Free our memory. png_free checks NULL for us. */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 991 | png_free(png_ptr, png_ptr->zbuf); |
| 992 | png_free(png_ptr, png_ptr->row_buf); |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 993 | #ifdef PNG_WRITE_FILTER_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 994 | png_free(png_ptr, png_ptr->prev_row); |
| 995 | png_free(png_ptr, png_ptr->sub_row); |
| 996 | png_free(png_ptr, png_ptr->up_row); |
| 997 | png_free(png_ptr, png_ptr->avg_row); |
| 998 | png_free(png_ptr, png_ptr->paeth_row); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 999 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1000 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1001 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1002 | png_free(png_ptr, png_ptr->time_buffer); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1003 | #endif |
| 1004 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1005 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1006 | png_free(png_ptr, png_ptr->prev_filters); |
| 1007 | png_free(png_ptr, png_ptr->filter_weights); |
| 1008 | png_free(png_ptr, png_ptr->inv_filter_weights); |
| 1009 | png_free(png_ptr, png_ptr->filter_costs); |
| 1010 | png_free(png_ptr, png_ptr->inv_filter_costs); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1011 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1012 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1013 | #ifdef PNG_SETJMP_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1014 | /* Reset structure */ |
Glenn Randers-Pehrson | 95ca51b | 2010-02-19 14:09:09 -0600 | [diff] [blame] | 1015 | png_memcpy(tmp_jmp, png_ptr->png_jmpbuf, png_sizeof(jmp_buf)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1016 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1017 | |
| 1018 | error_fn = png_ptr->error_fn; |
| 1019 | warning_fn = png_ptr->warning_fn; |
| 1020 | error_ptr = png_ptr->error_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1021 | #ifdef PNG_USER_MEM_SUPPORTED |
| 1022 | free_fn = png_ptr->free_fn; |
| 1023 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1024 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1025 | png_memset(png_ptr, 0, png_sizeof(png_struct)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1026 | |
| 1027 | png_ptr->error_fn = error_fn; |
| 1028 | png_ptr->warning_fn = warning_fn; |
| 1029 | png_ptr->error_ptr = error_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1030 | #ifdef PNG_USER_MEM_SUPPORTED |
| 1031 | png_ptr->free_fn = free_fn; |
| 1032 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1033 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1034 | #ifdef PNG_SETJMP_SUPPORTED |
Glenn Randers-Pehrson | 95ca51b | 2010-02-19 14:09:09 -0600 | [diff] [blame] | 1035 | png_memcpy(png_ptr->png_jmpbuf, tmp_jmp, png_sizeof(jmp_buf)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1036 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1037 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1038 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1039 | /* 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] | 1040 | void PNGAPI |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1041 | png_set_filter(png_structp png_ptr, int method, int filters) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1042 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1043 | png_debug(1, "in png_set_filter"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 1044 | |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 1045 | if (png_ptr == NULL) |
| 1046 | return; |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1047 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1048 | if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1049 | (method == PNG_INTRAPIXEL_DIFFERENCING)) |
| 1050 | method = PNG_FILTER_TYPE_BASE; |
Glenn Randers-Pehrson | 408b421 | 2000-12-18 09:33:57 -0600 | [diff] [blame] | 1051 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1052 | if (method == PNG_FILTER_TYPE_BASE) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1053 | { |
| 1054 | switch (filters & (PNG_ALL_FILTERS | 0x07)) |
| 1055 | { |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 1056 | #ifdef PNG_WRITE_FILTER_SUPPORTED |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1057 | case 5: |
| 1058 | case 6: |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1059 | 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] | 1060 | #endif /* PNG_WRITE_FILTER_SUPPORTED */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1061 | case PNG_FILTER_VALUE_NONE: |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1062 | png_ptr->do_filter = PNG_FILTER_NONE; break; |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 1063 | #ifdef PNG_WRITE_FILTER_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1064 | case PNG_FILTER_VALUE_SUB: |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1065 | png_ptr->do_filter = PNG_FILTER_SUB; break; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1066 | case PNG_FILTER_VALUE_UP: |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1067 | png_ptr->do_filter = PNG_FILTER_UP; break; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1068 | case PNG_FILTER_VALUE_AVG: |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1069 | png_ptr->do_filter = PNG_FILTER_AVG; break; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1070 | case PNG_FILTER_VALUE_PAETH: |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1071 | png_ptr->do_filter = PNG_FILTER_PAETH; break; |
| 1072 | default: |
| 1073 | png_ptr->do_filter = (png_byte)filters; break; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1074 | #else |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1075 | default: |
| 1076 | png_warning(png_ptr, "Unknown row filter for method 0"); |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 1077 | #endif /* PNG_WRITE_FILTER_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1078 | } |
| 1079 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1080 | /* If we have allocated the row_buf, this means we have already started |
| 1081 | * with the image and we should have allocated all of the filter buffers |
| 1082 | * that have been selected. If prev_row isn't already allocated, then |
| 1083 | * it is too late to start using the filters that need it, since we |
| 1084 | * will be missing the data in the previous row. If an application |
| 1085 | * wants to start and stop using particular filters during compression, |
| 1086 | * it should start out with all of the filters, and then add and |
| 1087 | * remove them after the start of compression. |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1088 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1089 | if (png_ptr->row_buf != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1090 | { |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 1091 | #ifdef PNG_WRITE_FILTER_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1092 | 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] | 1093 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1094 | png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1095 | (png_ptr->rowbytes + 1)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1096 | png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1097 | } |
| 1098 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1099 | 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] | 1100 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1101 | if (png_ptr->prev_row == NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1102 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1103 | png_warning(png_ptr, "Can't add Up filter after starting"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1104 | png_ptr->do_filter &= ~PNG_FILTER_UP; |
| 1105 | } |
| 1106 | else |
| 1107 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1108 | png_ptr->up_row = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1109 | (png_ptr->rowbytes + 1)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1110 | png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1111 | } |
| 1112 | } |
| 1113 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1114 | 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] | 1115 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1116 | if (png_ptr->prev_row == NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1117 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1118 | png_warning(png_ptr, "Can't add Average filter after starting"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1119 | png_ptr->do_filter &= ~PNG_FILTER_AVG; |
| 1120 | } |
| 1121 | else |
| 1122 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1123 | png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1124 | (png_ptr->rowbytes + 1)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1125 | png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1129 | if ((png_ptr->do_filter & PNG_FILTER_PAETH) && |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1130 | png_ptr->paeth_row == NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1131 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1132 | if (png_ptr->prev_row == NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1133 | { |
| 1134 | png_warning(png_ptr, "Can't add Paeth filter after starting"); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1135 | png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1136 | } |
| 1137 | else |
| 1138 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 1139 | png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1140 | (png_ptr->rowbytes + 1)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1141 | png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | if (png_ptr->do_filter == PNG_NO_FILTERS) |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 1146 | #endif /* PNG_WRITE_FILTER_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1147 | png_ptr->do_filter = PNG_FILTER_NONE; |
| 1148 | } |
| 1149 | } |
| 1150 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1151 | png_error(png_ptr, "Unknown custom filter method"); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1152 | } |
| 1153 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1154 | /* This allows us to influence the way in which libpng chooses the "best" |
| 1155 | * filter for the current scanline. While the "minimum-sum-of-absolute- |
| 1156 | * differences metric is relatively fast and effective, there is some |
| 1157 | * question as to whether it can be improved upon by trying to keep the |
| 1158 | * filtered data going to zlib more consistent, hopefully resulting in |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1159 | * better compression. |
| 1160 | */ |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1161 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1162 | void PNGAPI |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1163 | png_set_filter_heuristics(png_structp png_ptr, int heuristic_method, |
| 1164 | int num_weights, png_doublep filter_weights, |
| 1165 | png_doublep filter_costs) |
| 1166 | { |
| 1167 | int i; |
| 1168 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1169 | png_debug(1, "in png_set_filter_heuristics"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 1170 | |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 1171 | if (png_ptr == NULL) |
| 1172 | return; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1173 | if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST) |
| 1174 | { |
| 1175 | png_warning(png_ptr, "Unknown filter heuristic method"); |
| 1176 | return; |
| 1177 | } |
| 1178 | |
| 1179 | if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT) |
| 1180 | { |
| 1181 | heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED; |
| 1182 | } |
| 1183 | |
| 1184 | if (num_weights < 0 || filter_weights == NULL || |
| 1185 | heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED) |
| 1186 | { |
| 1187 | num_weights = 0; |
| 1188 | } |
| 1189 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1190 | png_ptr->num_prev_filters = (png_byte)num_weights; |
| 1191 | png_ptr->heuristic_method = (png_byte)heuristic_method; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1192 | |
| 1193 | if (num_weights > 0) |
| 1194 | { |
| 1195 | if (png_ptr->prev_filters == NULL) |
| 1196 | { |
| 1197 | png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1198 | (png_uint_32)(png_sizeof(png_byte) * num_weights)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1199 | |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 1200 | /* To make sure that the weighting starts out fairly */ |
| 1201 | for (i = 0; i < num_weights; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1202 | { |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 1203 | png_ptr->prev_filters[i] = 255; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | if (png_ptr->filter_weights == NULL) |
| 1208 | { |
Glenn Randers-Pehrson | 87c6bc9 | 2001-04-03 22:43:19 -0500 | [diff] [blame] | 1209 | png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1210 | (png_uint_32)(png_sizeof(png_uint_16) * num_weights)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1211 | |
Glenn Randers-Pehrson | 87c6bc9 | 2001-04-03 22:43:19 -0500 | [diff] [blame] | 1212 | png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1213 | (png_uint_32)(png_sizeof(png_uint_16) * num_weights)); |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 1214 | for (i = 0; i < num_weights; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1215 | { |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 1216 | png_ptr->inv_filter_weights[i] = |
| 1217 | png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | for (i = 0; i < num_weights; i++) |
| 1222 | { |
| 1223 | if (filter_weights[i] < 0.0) |
| 1224 | { |
| 1225 | png_ptr->inv_filter_weights[i] = |
| 1226 | png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; |
| 1227 | } |
| 1228 | else |
| 1229 | { |
| 1230 | png_ptr->inv_filter_weights[i] = |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1231 | (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1232 | png_ptr->filter_weights[i] = |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1233 | (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1234 | } |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | /* If, in the future, there are other filter methods, this would |
| 1239 | * need to be based on png_ptr->filter. |
| 1240 | */ |
| 1241 | if (png_ptr->filter_costs == NULL) |
| 1242 | { |
Glenn Randers-Pehrson | 87c6bc9 | 2001-04-03 22:43:19 -0500 | [diff] [blame] | 1243 | png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1244 | (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1245 | |
Glenn Randers-Pehrson | 87c6bc9 | 2001-04-03 22:43:19 -0500 | [diff] [blame] | 1246 | png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1247 | (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1248 | |
| 1249 | for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) |
| 1250 | { |
| 1251 | png_ptr->inv_filter_costs[i] = |
| 1252 | png_ptr->filter_costs[i] = PNG_COST_FACTOR; |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | /* Here is where we set the relative costs of the different filters. We |
| 1257 | * should take the desired compression level into account when setting |
| 1258 | * the costs, so that Paeth, for instance, has a high relative cost at low |
| 1259 | * compression levels, while it has a lower relative cost at higher |
| 1260 | * compression settings. The filter types are in order of increasing |
| 1261 | * relative cost, so it would be possible to do this with an algorithm. |
| 1262 | */ |
| 1263 | for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) |
| 1264 | { |
| 1265 | if (filter_costs == NULL || filter_costs[i] < 0.0) |
| 1266 | { |
| 1267 | png_ptr->inv_filter_costs[i] = |
| 1268 | png_ptr->filter_costs[i] = PNG_COST_FACTOR; |
| 1269 | } |
| 1270 | else if (filter_costs[i] >= 1.0) |
| 1271 | { |
| 1272 | png_ptr->inv_filter_costs[i] = |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1273 | (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1274 | png_ptr->filter_costs[i] = |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1275 | (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */ |
| 1280 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1281 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1282 | png_set_compression_level(png_structp png_ptr, int level) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1283 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1284 | png_debug(1, "in png_set_compression_level"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 1285 | |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 1286 | if (png_ptr == NULL) |
| 1287 | return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1288 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1289 | png_ptr->zlib_level = level; |
| 1290 | } |
| 1291 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1292 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1293 | png_set_compression_mem_level(png_structp png_ptr, int mem_level) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1294 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1295 | png_debug(1, "in png_set_compression_mem_level"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 1296 | |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 1297 | if (png_ptr == NULL) |
| 1298 | return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1299 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1300 | png_ptr->zlib_mem_level = mem_level; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1301 | } |
| 1302 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1303 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1304 | png_set_compression_strategy(png_structp png_ptr, int strategy) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1305 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1306 | png_debug(1, "in png_set_compression_strategy"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 1307 | |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 1308 | if (png_ptr == NULL) |
| 1309 | return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1310 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1311 | png_ptr->zlib_strategy = strategy; |
| 1312 | } |
| 1313 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1314 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1315 | png_set_compression_window_bits(png_structp png_ptr, int window_bits) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1316 | { |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 1317 | if (png_ptr == NULL) |
| 1318 | return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1319 | if (window_bits > 15) |
| 1320 | png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1321 | else if (window_bits < 8) |
| 1322 | png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1323 | #ifndef WBITS_8_OK |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 1324 | /* Avoid libpng bug with 256-byte windows */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1325 | if (window_bits == 8) |
| 1326 | { |
| 1327 | png_warning(png_ptr, "Compression window is being reset to 512"); |
Glenn Randers-Pehrson | 614b91d | 2009-10-17 19:00:18 -0500 | [diff] [blame] | 1328 | window_bits = 9; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1329 | } |
| 1330 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1331 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1332 | png_ptr->zlib_window_bits = window_bits; |
| 1333 | } |
| 1334 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1335 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1336 | png_set_compression_method(png_structp png_ptr, int method) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1337 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1338 | png_debug(1, "in png_set_compression_method"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 1339 | |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 1340 | if (png_ptr == NULL) |
| 1341 | return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1342 | if (method != 8) |
| 1343 | png_warning(png_ptr, "Only compression method 8 is supported by PNG"); |
| 1344 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1345 | png_ptr->zlib_method = method; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1346 | } |
| 1347 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1348 | void PNGAPI |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1349 | png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn) |
| 1350 | { |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 1351 | if (png_ptr == NULL) |
| 1352 | return; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1353 | png_ptr->write_row_fn = write_row_fn; |
| 1354 | } |
| 1355 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1356 | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1357 | void PNGAPI |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1358 | png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1359 | write_user_transform_fn) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1360 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1361 | png_debug(1, "in png_set_write_user_transform_fn"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 1362 | |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 1363 | if (png_ptr == NULL) |
| 1364 | return; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1365 | png_ptr->transformations |= PNG_USER_TRANSFORM; |
| 1366 | png_ptr->write_user_transform_fn = write_user_transform_fn; |
| 1367 | } |
| 1368 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1369 | |
| 1370 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1371 | #ifdef PNG_INFO_IMAGE_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1372 | void PNGAPI |
| 1373 | png_write_png(png_structp png_ptr, png_infop info_ptr, |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 1374 | int transforms, voidp params) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1375 | { |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 1376 | if (png_ptr == NULL || info_ptr == NULL) |
| 1377 | return; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1378 | |
| 1379 | /* Write the file header information. */ |
| 1380 | png_write_info(png_ptr, info_ptr); |
| 1381 | |
| 1382 | /* ------ these transformations don't touch the info structure ------- */ |
| 1383 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1384 | #ifdef PNG_WRITE_INVERT_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1385 | /* Invert monochrome pixels */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1386 | if (transforms & PNG_TRANSFORM_INVERT_MONO) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1387 | png_set_invert_mono(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1388 | #endif |
| 1389 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1390 | #ifdef PNG_WRITE_SHIFT_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1391 | /* Shift the pixels up to a legal bit depth and fill in |
| 1392 | * as appropriate to correctly scale the image. |
| 1393 | */ |
| 1394 | if ((transforms & PNG_TRANSFORM_SHIFT) |
Glenn Randers-Pehrson | 45624d6 | 2010-03-03 11:40:43 -0600 | [diff] [blame] | 1395 | && (info_ptr->valid & PNG_INFO_sBIT)) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1396 | png_set_shift(png_ptr, &info_ptr->sig_bit); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1397 | #endif |
| 1398 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1399 | #ifdef PNG_WRITE_PACK_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1400 | /* Pack pixels into bytes */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1401 | if (transforms & PNG_TRANSFORM_PACKING) |
| 1402 | png_set_packing(png_ptr); |
| 1403 | #endif |
| 1404 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1405 | #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1406 | /* Swap location of alpha bytes from ARGB to RGBA */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1407 | if (transforms & PNG_TRANSFORM_SWAP_ALPHA) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1408 | png_set_swap_alpha(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1409 | #endif |
| 1410 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1411 | #ifdef PNG_WRITE_FILLER_SUPPORTED |
Glenn Randers-Pehrson | 4a82d69 | 2008-12-15 16:25:05 -0600 | [diff] [blame] | 1412 | /* 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] | 1413 | if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) |
Glenn Randers-Pehrson | 1eb14e9 | 2008-12-10 07:14:45 -0600 | [diff] [blame] | 1414 | png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1415 | else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) |
Glenn Randers-Pehrson | 1eb14e9 | 2008-12-10 07:14:45 -0600 | [diff] [blame] | 1416 | png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1417 | #endif |
| 1418 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1419 | #ifdef PNG_WRITE_BGR_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1420 | /* Flip BGR pixels to RGB */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1421 | if (transforms & PNG_TRANSFORM_BGR) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1422 | png_set_bgr(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1423 | #endif |
| 1424 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1425 | #ifdef PNG_WRITE_SWAP_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1426 | /* Swap bytes of 16-bit files to most significant byte first */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1427 | if (transforms & PNG_TRANSFORM_SWAP_ENDIAN) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1428 | png_set_swap(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1429 | #endif |
| 1430 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1431 | #ifdef PNG_WRITE_PACKSWAP_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1432 | /* Swap bits of 1, 2, 4 bit packed pixel formats */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1433 | if (transforms & PNG_TRANSFORM_PACKSWAP) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1434 | png_set_packswap(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1435 | #endif |
| 1436 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1437 | #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED |
Glenn Randers-Pehrson | 6878eb6 | 2009-06-29 16:45:53 -0500 | [diff] [blame] | 1438 | /* Invert the alpha channel from opacity to transparency */ |
| 1439 | if (transforms & PNG_TRANSFORM_INVERT_ALPHA) |
| 1440 | png_set_invert_alpha(png_ptr); |
| 1441 | #endif |
| 1442 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1443 | /* ----------------------- end of transformations ------------------- */ |
| 1444 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1445 | /* Write the bits */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1446 | if (info_ptr->valid & PNG_INFO_IDAT) |
| 1447 | png_write_image(png_ptr, info_ptr->row_pointers); |
| 1448 | |
| 1449 | /* It is REQUIRED to call this to finish writing the rest of the file */ |
| 1450 | png_write_end(png_ptr, info_ptr); |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1451 | |
Glenn Randers-Pehrson | 3dfe937 | 2009-08-22 08:44:23 -0500 | [diff] [blame] | 1452 | transforms = transforms; /* Quiet compiler warnings */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1453 | params = params; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1454 | } |
| 1455 | #endif |
Glenn Randers-Pehrson | 1909560 | 2001-03-14 07:08:39 -0600 | [diff] [blame] | 1456 | #endif /* PNG_WRITE_SUPPORTED */ |