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