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