Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1 | |
| 2 | /* pngwrite.c - general routines to write a PNG file |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame^] | 3 | * |
| 4 | * libpng 1.00.97 |
| 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 |
| 8 | * May 28, 1997 |
| 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 |
| 18 | * after the image data, put it in png_write_end(). I strongly encurage |
| 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 |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 25 | png_write_info(png_structp png_ptr, png_infop info_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 26 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 27 | int i; |
| 28 | |
| 29 | png_debug(1, "in png_write_info\n"); |
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, |
| 34 | info_ptr->filter_type, info_ptr->interlace_type); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 35 | /* the rest of these check to see if the valid field has the appropriate |
| 36 | flag set, and if it does, writes the chunk. */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 37 | #if defined(PNG_WRITE_gAMA_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 38 | if (info_ptr->valid & PNG_INFO_gAMA) |
| 39 | png_write_gAMA(png_ptr, info_ptr->gamma); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 40 | #endif |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame^] | 41 | #if defined(PNG_WRITE_sRGB_SUPPORTED) |
| 42 | if (info_ptr->valid & PNG_INFO_sRGB) |
| 43 | png_write_sRGB(png_ptr, info_ptr->srgb_intent); |
| 44 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 45 | #if defined(PNG_WRITE_sBIT_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 46 | if (info_ptr->valid & PNG_INFO_sBIT) |
| 47 | 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] | 48 | #endif |
| 49 | #if defined(PNG_WRITE_cHRM_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 50 | if (info_ptr->valid & PNG_INFO_cHRM) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 51 | png_write_cHRM(png_ptr, |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 52 | info_ptr->x_white, info_ptr->y_white, |
| 53 | info_ptr->x_red, info_ptr->y_red, |
| 54 | info_ptr->x_green, info_ptr->y_green, |
| 55 | info_ptr->x_blue, info_ptr->y_blue); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 56 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 57 | if (info_ptr->valid & PNG_INFO_PLTE) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 58 | png_write_PLTE(png_ptr, info_ptr->palette, |
| 59 | (png_uint_32)info_ptr->num_palette); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 60 | else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 61 | png_error(png_ptr, "Valid palette required for paletted images\n"); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 62 | #if defined(PNG_WRITE_tRNS_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 63 | if (info_ptr->valid & PNG_INFO_tRNS) |
| 64 | png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values), |
| 65 | info_ptr->num_trans, info_ptr->color_type); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 66 | #endif |
| 67 | #if defined(PNG_WRITE_bKGD_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 68 | if (info_ptr->valid & PNG_INFO_bKGD) |
| 69 | png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 70 | #endif |
| 71 | #if defined(PNG_WRITE_hIST_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 72 | if (info_ptr->valid & PNG_INFO_hIST) |
| 73 | png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 74 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 75 | #if defined(PNG_WRITE_oFFs_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 76 | if (info_ptr->valid & PNG_INFO_oFFs) |
| 77 | png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset, |
| 78 | info_ptr->offset_unit_type); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 79 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 80 | #if defined(PNG_WRITE_pCAL_SUPPORTED) |
| 81 | if (info_ptr->valid & PNG_INFO_pCAL) |
| 82 | png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0, |
| 83 | info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams, |
| 84 | info_ptr->pcal_units, info_ptr->pcal_params); |
| 85 | #endif |
| 86 | #if defined(PNG_WRITE_pHYs_SUPPORTED) |
| 87 | if (info_ptr->valid & PNG_INFO_pHYs) |
| 88 | png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit, |
| 89 | info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type); |
| 90 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 91 | #if defined(PNG_WRITE_tIME_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 92 | if (info_ptr->valid & PNG_INFO_tIME) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 93 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 94 | png_write_tIME(png_ptr, &(info_ptr->mod_time)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 95 | png_ptr->flags |= PNG_FLAG_WROTE_tIME; |
| 96 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 97 | #endif |
| 98 | #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 99 | /* Check to see if we need to write text chunks */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 100 | for (i = 0; i < info_ptr->num_text; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 101 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 102 | png_debug2(2, "Writing header text chunk %d, type %d\n", i, |
| 103 | info_ptr->text[i].compression); |
| 104 | /* If we want a compressed text chunk */ |
| 105 | if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt) |
| 106 | { |
| 107 | #if defined(PNG_WRITE_zTXt_SUPPORTED) |
| 108 | /* write compressed chunk */ |
| 109 | png_write_zTXt(png_ptr, info_ptr->text[i].key, |
| 110 | info_ptr->text[i].text, info_ptr->text[i].text_length, |
| 111 | info_ptr->text[i].compression); |
| 112 | #else |
| 113 | png_warning(png_ptr, "Unable to write compressed text\n"); |
| 114 | #endif |
| 115 | /* Mark this chunk as written */ |
| 116 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; |
| 117 | } |
| 118 | else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) |
| 119 | { |
| 120 | #if defined(PNG_WRITE_tEXt_SUPPORTED) |
| 121 | /* write uncompressed chunk */ |
| 122 | png_write_tEXt(png_ptr, info_ptr->text[i].key, |
| 123 | info_ptr->text[i].text, info_ptr->text[i].text_length); |
| 124 | #else |
| 125 | png_warning(png_ptr, "Unable to write uncompressed text\n"); |
| 126 | #endif |
| 127 | /* Mark this chunk as written */ |
| 128 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; |
| 129 | } |
| 130 | } |
| 131 | #endif |
| 132 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 133 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 134 | /* 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^] | 135 | * time information, you can pass NULL for info. If you already wrote these |
| 136 | * in png_write_info(), do not write them again here. If you have long |
| 137 | * comments, I suggest writing them here, and compressing them. |
| 138 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 139 | void |
| 140 | png_write_end(png_structp png_ptr, png_infop info_ptr) |
| 141 | { |
| 142 | png_debug(1, "in png_write_end\n"); |
| 143 | if (!(png_ptr->mode & PNG_HAVE_IDAT)) |
| 144 | png_error(png_ptr, "No IDATs written into file"); |
| 145 | |
| 146 | /* see if user wants us to write information chunks */ |
| 147 | if (info_ptr != NULL) |
| 148 | { |
| 149 | int i; /* local index variable */ |
| 150 | #if defined(PNG_WRITE_tIME_SUPPORTED) |
| 151 | /* check to see if user has supplied a time chunk */ |
| 152 | if (info_ptr->valid & PNG_INFO_tIME && |
| 153 | !(png_ptr->flags & PNG_FLAG_WROTE_tIME)) |
| 154 | png_write_tIME(png_ptr, &(info_ptr->mod_time)); |
| 155 | #endif |
| 156 | #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED) |
| 157 | /* loop through comment chunks */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 158 | for (i = 0; i < info_ptr->num_text; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 159 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 160 | png_debug2(2, "Writing trailer text chunk %d, type %d\n", i, |
| 161 | info_ptr->text[i].compression); |
| 162 | if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 163 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 164 | #if defined(PNG_WRITE_zTXt_SUPPORTED) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 165 | /* write compressed chunk */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 166 | png_write_zTXt(png_ptr, info_ptr->text[i].key, |
| 167 | info_ptr->text[i].text, info_ptr->text[i].text_length, |
| 168 | info_ptr->text[i].compression); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 169 | #else |
| 170 | png_warning(png_ptr, "Unable to write compressed text\n"); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 171 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 172 | /* Mark this chunk as written */ |
| 173 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 174 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 175 | else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 176 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 177 | #if defined(PNG_WRITE_tEXt_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 178 | /* write uncompressed chunk */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 179 | png_write_tEXt(png_ptr, info_ptr->text[i].key, |
| 180 | info_ptr->text[i].text, info_ptr->text[i].text_length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 181 | #else |
| 182 | png_warning(png_ptr, "Unable to write uncompressed text\n"); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 183 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 184 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 185 | /* Mark this chunk as written */ |
| 186 | info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 187 | } |
| 188 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 189 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 190 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 191 | |
| 192 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 193 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 194 | /* write end of PNG file */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 195 | png_write_IEND(png_ptr); |
| 196 | } |
| 197 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame^] | 198 | #if defined(PNG_TIME_RFC1152_SUPPORTED) |
| 199 | /* Convert the supplied time into an RFC 1152 string suitable for use in |
| 200 | * a "Creation Time" or other text-based time string. |
| 201 | */ |
| 202 | png_charp |
| 203 | png_convert_to_rfc1152(png_structp png_ptr, png_timep ptime) |
| 204 | { |
| 205 | const char *short_months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 206 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
| 207 | |
| 208 | if (png_ptr->time_buffer == NULL) |
| 209 | { |
| 210 | png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, 29*sizeof(char)); |
| 211 | } |
| 212 | |
| 213 | sprintf(png_ptr->time_buffer, "%d %s %d %02d:%02d:%02d +0000", |
| 214 | ptime->day % 31, short_months[ptime->month], ptime->year, |
| 215 | ptime->hour % 24, ptime->minute % 60, ptime->second % 61); |
| 216 | return png_ptr->time_buffer; |
| 217 | } |
| 218 | #endif /* PNG_TIME_RFC1152_SUPPORTED */ |
| 219 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 220 | #if defined(PNG_WRITE_tIME_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 221 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 222 | png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 223 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 224 | png_debug(1, "in png_convert_from_struct_tm\n"); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 225 | ptime->year = (png_uint_16)(1900 + ttime->tm_year); |
| 226 | ptime->month = (png_byte)(ttime->tm_mon + 1); |
| 227 | ptime->day = (png_byte)ttime->tm_mday; |
| 228 | ptime->hour = (png_byte)ttime->tm_hour; |
| 229 | ptime->minute = (png_byte)ttime->tm_min; |
| 230 | ptime->second = (png_byte)ttime->tm_sec; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 234 | png_convert_from_time_t(png_timep ptime, time_t ttime) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 235 | { |
| 236 | struct tm *tbuf; |
| 237 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 238 | png_debug(1, "in png_convert_from_time_t\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 239 | tbuf = gmtime(&ttime); |
| 240 | png_convert_from_struct_tm(ptime, tbuf); |
| 241 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 242 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 243 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 244 | /* Initialize png_ptr structure, and allocate any memory needed */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 245 | png_structp |
| 246 | png_create_write_struct(png_const_charp user_png_ver, voidp error_ptr, |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 247 | png_error_ptr error_fn, png_error_ptr warn_fn) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 248 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 249 | png_structp png_ptr; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 250 | #ifdef USE_FAR_KEYWORD |
| 251 | jmp_buf jmpbuf; |
| 252 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 253 | png_debug(1, "in png_create_write_struct\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 254 | if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL) |
| 255 | { |
| 256 | return (png_structp)NULL; |
| 257 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 258 | #ifdef USE_FAR_KEYWORD |
| 259 | if (setjmp(jmpbuf)) |
| 260 | #else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 261 | if (setjmp(png_ptr->jmpbuf)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 262 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 263 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 264 | png_free(png_ptr, png_ptr->zbuf); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 265 | png_destroy_struct(png_ptr); |
| 266 | return (png_structp)NULL; |
| 267 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 268 | #ifdef USE_FAR_KEYWORD |
| 269 | png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf)); |
| 270 | #endif |
| 271 | png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 272 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 273 | /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so |
| 274 | * we must recompile any applications that use any older library version. |
| 275 | * For versions after libpng 1.0, we will be compatible, so we need |
| 276 | * only check the first digit. |
| 277 | */ |
| 278 | if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] || |
| 279 | (png_libpng_ver[0] == '0' && user_png_ver[2] < '9')) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 280 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 281 | png_error(png_ptr, |
| 282 | "Incompatible libpng version in application and library"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 283 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 284 | |
| 285 | /* initialize zbuf - compression buffer */ |
| 286 | png_ptr->zbuf_size = PNG_ZBUF_SIZE; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 287 | png_ptr->zbuf = png_malloc(png_ptr, png_ptr->zbuf_size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 288 | |
| 289 | png_set_write_fn(png_ptr, NULL, NULL, NULL); |
| 290 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame^] | 291 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) |
| 292 | png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT, |
| 293 | 1, NULL, NULL); |
| 294 | #endif |
| 295 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 296 | return (png_ptr); |
| 297 | } |
| 298 | |
| 299 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 300 | /* Initialize png_ptr structure, and allocate any memory needed */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 301 | void |
| 302 | png_write_init(png_structp png_ptr) |
| 303 | { |
| 304 | jmp_buf tmp_jmp; /* to save current jump buffer */ |
| 305 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 306 | png_debug(1, "in png_write_init\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 307 | /* save jump buffer and error functions */ |
| 308 | png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf)); |
| 309 | |
| 310 | /* reset all variables to 0 */ |
| 311 | png_memset(png_ptr, 0, sizeof (png_struct)); |
| 312 | |
| 313 | /* restore jump buffer */ |
| 314 | png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf)); |
| 315 | |
| 316 | /* initialize zbuf - compression buffer */ |
| 317 | png_ptr->zbuf_size = PNG_ZBUF_SIZE; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 318 | png_ptr->zbuf = png_malloc(png_ptr, png_ptr->zbuf_size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 319 | png_set_write_fn(png_ptr, NULL, NULL, NULL); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 320 | |
| 321 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) |
| 322 | png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT, |
| 323 | 1, NULL, NULL); |
| 324 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 325 | } |
| 326 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame^] | 327 | /* Write a few rows of image data. If the image is interlaced, |
| 328 | * either you will have to write the 7 sub images, or, if you |
| 329 | * have called png_set_interlace_handling(), you will have to |
| 330 | * "write" the image seven times. |
| 331 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 332 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 333 | png_write_rows(png_structp png_ptr, png_bytepp row, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 334 | png_uint_32 num_rows) |
| 335 | { |
| 336 | png_uint_32 i; /* row counter */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 337 | png_bytepp rp; /* row pointer */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 338 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 339 | png_debug(1, "in png_write_rows\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 340 | /* loop through the rows */ |
| 341 | for (i = 0, rp = row; i < num_rows; i++, rp++) |
| 342 | { |
| 343 | png_write_row(png_ptr, *rp); |
| 344 | } |
| 345 | } |
| 346 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame^] | 347 | /* Write the image. You only need to call this function once, even |
| 348 | * if you are writing an interlaced image. |
| 349 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 350 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 351 | png_write_image(png_structp png_ptr, png_bytepp image) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 352 | { |
| 353 | png_uint_32 i; /* row index */ |
| 354 | int pass, num_pass; /* pass variables */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 355 | png_bytepp rp; /* points to current row */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 356 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 357 | png_debug(1, "in png_write_image\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 358 | /* intialize interlace handling. If image is not interlaced, |
| 359 | this will set pass to 1 */ |
| 360 | num_pass = png_set_interlace_handling(png_ptr); |
| 361 | /* loop through passes */ |
| 362 | for (pass = 0; pass < num_pass; pass++) |
| 363 | { |
| 364 | /* loop through image */ |
| 365 | for (i = 0, rp = image; i < png_ptr->height; i++, rp++) |
| 366 | { |
| 367 | png_write_row(png_ptr, *rp); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 372 | /* called by user to write a row of image data */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 373 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 374 | png_write_row(png_structp png_ptr, png_bytep row) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 375 | { |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame^] | 376 | png_debug2(1, "in png_write_row (row %ld, pass %d)\n", |
| 377 | png_ptr->row_number, png_ptr->pass); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 378 | /* initialize transformations and other stuff if first time */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 379 | if (png_ptr->row_number == 0 && png_ptr->pass == 0) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 380 | { |
| 381 | png_write_start_row(png_ptr); |
| 382 | } |
| 383 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 384 | #if defined(PNG_WRITE_INTERLACING_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 385 | /* if interlaced and not interested in row, return */ |
| 386 | if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) |
| 387 | { |
| 388 | switch (png_ptr->pass) |
| 389 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 390 | case 0: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 391 | if (png_ptr->row_number & 7) |
| 392 | { |
| 393 | png_write_finish_row(png_ptr); |
| 394 | return; |
| 395 | } |
| 396 | break; |
| 397 | case 1: |
| 398 | if ((png_ptr->row_number & 7) || png_ptr->width < 5) |
| 399 | { |
| 400 | png_write_finish_row(png_ptr); |
| 401 | return; |
| 402 | } |
| 403 | break; |
| 404 | case 2: |
| 405 | if ((png_ptr->row_number & 7) != 4) |
| 406 | { |
| 407 | png_write_finish_row(png_ptr); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 408 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 409 | } |
| 410 | break; |
| 411 | case 3: |
| 412 | if ((png_ptr->row_number & 3) || png_ptr->width < 3) |
| 413 | { |
| 414 | png_write_finish_row(png_ptr); |
| 415 | return; |
| 416 | } |
| 417 | break; |
| 418 | case 4: |
| 419 | if ((png_ptr->row_number & 3) != 2) |
| 420 | { |
| 421 | png_write_finish_row(png_ptr); |
| 422 | return; |
| 423 | } |
| 424 | break; |
| 425 | case 5: |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 426 | if ((png_ptr->row_number & 1) || png_ptr->width < 2) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 427 | { |
| 428 | png_write_finish_row(png_ptr); |
| 429 | return; |
| 430 | } |
| 431 | break; |
| 432 | case 6: |
| 433 | if (!(png_ptr->row_number & 1)) |
| 434 | { |
| 435 | png_write_finish_row(png_ptr); |
| 436 | return; |
| 437 | } |
| 438 | break; |
| 439 | } |
| 440 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 441 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 442 | |
| 443 | /* set up row info for transformations */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 444 | png_ptr->row_info.color_type = png_ptr->color_type; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 445 | png_ptr->row_info.width = png_ptr->usr_width; |
| 446 | png_ptr->row_info.channels = png_ptr->usr_channels; |
| 447 | png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 448 | png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth * |
| 449 | png_ptr->row_info.channels); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 450 | png_ptr->row_info.rowbytes = ((png_ptr->row_info.width * |
| 451 | (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3); |
| 452 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame^] | 453 | png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type); |
| 454 | png_debug1(3, "row_info->width = %d\n", png_ptr->row_info.width); |
| 455 | png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels); |
| 456 | png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth); |
| 457 | png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth); |
| 458 | 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] | 459 | |
| 460 | /* Copy user's row into buffer, leaving room for filter byte. */ |
| 461 | 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] | 462 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 463 | #if defined(PNG_WRITE_INTERLACING_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 464 | /* handle interlacing */ |
| 465 | if (png_ptr->interlaced && png_ptr->pass < 6 && |
| 466 | (png_ptr->transformations & PNG_INTERLACE)) |
| 467 | { |
| 468 | png_do_write_interlace(&(png_ptr->row_info), |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 469 | png_ptr->row_buf + 1, png_ptr->pass); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 470 | /* this should always get caught above, but still ... */ |
| 471 | if (!(png_ptr->row_info.width)) |
| 472 | { |
| 473 | png_write_finish_row(png_ptr); |
| 474 | return; |
| 475 | } |
| 476 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 477 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 478 | |
| 479 | /* handle other transformations */ |
| 480 | if (png_ptr->transformations) |
| 481 | png_do_write_transformations(png_ptr); |
| 482 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 483 | /* Find a filter if necessary, filter the row and write it out. */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 484 | png_write_find_filter(png_ptr, &(png_ptr->row_info)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 485 | } |
| 486 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 487 | #if defined(PNG_WRITE_FLUSH_SUPPORTED) |
| 488 | /* Set the automatic flush interval or 0 to turn flushing off */ |
| 489 | void |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 490 | png_set_flush(png_structp png_ptr, int nrows) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 491 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 492 | png_debug(1, "in png_set_flush\n"); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 493 | png_ptr->flush_dist = (nrows < 0 ? 0 : nrows); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /* flush the current output buffers now */ |
| 497 | void |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 498 | png_write_flush(png_structp png_ptr) |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 499 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 500 | int wrote_IDAT; |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 501 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 502 | png_debug(1, "in png_write_flush\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 503 | /* We have already written out all of the data */ |
| 504 | if (png_ptr->row_number >= png_ptr->num_rows) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 505 | return; |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 506 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 507 | do |
| 508 | { |
| 509 | int ret; |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 510 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 511 | /* compress the data */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 512 | ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 513 | wrote_IDAT = 0; |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 514 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 515 | /* check for compression errors */ |
| 516 | if (ret != Z_OK) |
| 517 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 518 | if (png_ptr->zstream.msg != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 519 | png_error(png_ptr, png_ptr->zstream.msg); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 520 | else |
| 521 | png_error(png_ptr, "zlib error"); |
| 522 | } |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 523 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 524 | if (!(png_ptr->zstream.avail_out)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 525 | { |
| 526 | /* write the IDAT and reset the zlib output buffer */ |
| 527 | png_write_IDAT(png_ptr, png_ptr->zbuf, |
| 528 | png_ptr->zbuf_size); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 529 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 530 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 531 | wrote_IDAT = 1; |
| 532 | } |
| 533 | } while(wrote_IDAT == 1); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 534 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 535 | /* 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] | 536 | if (png_ptr->zbuf_size != png_ptr->zstream.avail_out) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 537 | { |
| 538 | /* write the IDAT and reset the zlib output buffer */ |
| 539 | png_write_IDAT(png_ptr, png_ptr->zbuf, |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 540 | png_ptr->zbuf_size - png_ptr->zstream.avail_out); |
| 541 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 542 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 543 | } |
| 544 | png_ptr->flush_rows = 0; |
| 545 | png_flush(png_ptr); |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 546 | } |
| 547 | #endif /* PNG_WRITE_FLUSH_SUPPORTED */ |
| 548 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 549 | /* free all memory used by the write */ |
| 550 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 551 | 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] | 552 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 553 | png_structp png_ptr = NULL; |
| 554 | png_infop info_ptr = NULL; |
| 555 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 556 | png_debug(1, "in png_destroy_write_struct\n"); |
| 557 | if (png_ptr_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 558 | png_ptr = *png_ptr_ptr; |
| 559 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 560 | if (info_ptr_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 561 | info_ptr = *info_ptr_ptr; |
| 562 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 563 | if (info_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 564 | { |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame^] | 565 | #ifdef PNG_WRITE_tEXt_SUPPORTED |
| 566 | png_free(png_ptr, info_ptr->text); |
| 567 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 568 | png_destroy_struct((png_voidp)info_ptr); |
| 569 | *info_ptr_ptr = (png_infop)NULL; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 570 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 571 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 572 | if (png_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 573 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 574 | png_write_destroy(png_ptr); |
| 575 | png_destroy_struct((png_voidp)png_ptr); |
| 576 | *png_ptr_ptr = (png_structp)NULL; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
| 580 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 581 | /* Free any memory used in png_ptr struct (old method) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 582 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 583 | png_write_destroy(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 584 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 585 | jmp_buf tmp_jmp; /* save jump buffer */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 586 | png_error_ptr error_fn; |
| 587 | png_error_ptr warning_fn; |
| 588 | png_voidp error_ptr; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 589 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 590 | png_debug(1, "in png_write_destroy\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 591 | /* free any memory zlib uses */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 592 | deflateEnd(&png_ptr->zstream); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 593 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 594 | /* free our memory. png_free checks NULL for us. */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 595 | png_free(png_ptr, png_ptr->zbuf); |
| 596 | png_free(png_ptr, png_ptr->row_buf); |
| 597 | png_free(png_ptr, png_ptr->prev_row); |
| 598 | png_free(png_ptr, png_ptr->sub_row); |
| 599 | png_free(png_ptr, png_ptr->up_row); |
| 600 | png_free(png_ptr, png_ptr->avg_row); |
| 601 | png_free(png_ptr, png_ptr->paeth_row); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame^] | 602 | #if defined(PNG_TIME_RFC1152_SUPPORTED) |
| 603 | png_free(png_ptr, png_ptr->time_buffer); |
| 604 | #endif /* PNG_TIME_RFC1152_SUPPORTED */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 605 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) |
| 606 | png_free(png_ptr, png_ptr->prev_filters); |
| 607 | png_free(png_ptr, png_ptr->filter_weights); |
| 608 | png_free(png_ptr, png_ptr->inv_filter_weights); |
| 609 | png_free(png_ptr, png_ptr->filter_costs); |
| 610 | png_free(png_ptr, png_ptr->inv_filter_costs); |
| 611 | #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 612 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 613 | /* reset structure */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 614 | png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 615 | |
| 616 | error_fn = png_ptr->error_fn; |
| 617 | warning_fn = png_ptr->warning_fn; |
| 618 | error_ptr = png_ptr->error_ptr; |
| 619 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 620 | png_memset(png_ptr, 0, sizeof (png_struct)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 621 | |
| 622 | png_ptr->error_fn = error_fn; |
| 623 | png_ptr->warning_fn = warning_fn; |
| 624 | png_ptr->error_ptr = error_ptr; |
| 625 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 626 | png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf)); |
| 627 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 628 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 629 | /* Allow the application to select one or more row filters to use. */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 630 | void |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 631 | png_set_filter(png_structp png_ptr, int method, int filters) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 632 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 633 | png_debug(1, "in png_set_filter\n"); |
| 634 | /* We allow 'method' only for future expansion of the base filter method. */ |
| 635 | if (method == PNG_FILTER_TYPE_BASE) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 636 | { |
| 637 | switch (filters & (PNG_ALL_FILTERS | 0x07)) |
| 638 | { |
| 639 | case 5: |
| 640 | case 6: |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 641 | case 7: png_warning(png_ptr, "Unknown row filter for method 0"); |
| 642 | case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break; |
| 643 | case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break; |
| 644 | case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break; |
| 645 | case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break; |
| 646 | 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] | 647 | default: png_ptr->do_filter = (png_byte)filters; break; |
| 648 | } |
| 649 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 650 | /* If we have allocated the row_buf, this means we have already started |
| 651 | * with the image and we should have allocated all of the filter buffers |
| 652 | * that have been selected. If prev_row isn't already allocated, then |
| 653 | * it is too late to start using the filters that need it, since we |
| 654 | * will be missing the data in the previous row. If an application |
| 655 | * wants to start and stop using particular filters during compression, |
| 656 | * it should start out with all of the filters, and then add and |
| 657 | * remove them after the start of compression. |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 658 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 659 | if (png_ptr->row_buf != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 660 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 661 | 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] | 662 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 663 | png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 664 | png_ptr->rowbytes + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 665 | png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 666 | } |
| 667 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 668 | 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] | 669 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 670 | if (png_ptr->prev_row == NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 671 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 672 | png_warning(png_ptr, "Can't add Up filter after starting"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 673 | png_ptr->do_filter &= ~PNG_FILTER_UP; |
| 674 | } |
| 675 | else |
| 676 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 677 | png_ptr->up_row = (png_bytep)png_malloc(png_ptr, |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 678 | png_ptr->rowbytes + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 679 | png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 683 | 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] | 684 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 685 | if (png_ptr->prev_row == NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 686 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 687 | png_warning(png_ptr, "Can't add Average filter after starting"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 688 | png_ptr->do_filter &= ~PNG_FILTER_AVG; |
| 689 | } |
| 690 | else |
| 691 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 692 | png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 693 | png_ptr->rowbytes + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 694 | png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 698 | if (png_ptr->do_filter & PNG_FILTER_PAETH && |
| 699 | png_ptr->paeth_row == NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 700 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 701 | if (png_ptr->prev_row == NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 702 | { |
| 703 | png_warning(png_ptr, "Can't add Paeth filter after starting"); |
| 704 | png_ptr->do_filter &= ~PNG_FILTER_PAETH; |
| 705 | } |
| 706 | else |
| 707 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 708 | png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr, |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 709 | png_ptr->rowbytes + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 710 | png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
| 714 | if (png_ptr->do_filter == PNG_NO_FILTERS) |
| 715 | png_ptr->do_filter = PNG_FILTER_NONE; |
| 716 | } |
| 717 | } |
| 718 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 719 | png_error(png_ptr, "Unknown custom filter method"); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 720 | } |
| 721 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 722 | /* This allows us to influence the way in which libpng chooses the "best" |
| 723 | * filter for the current scanline. While the "minimum-sum-of-absolute- |
| 724 | * differences metric is relatively fast and effective, there is some |
| 725 | * question as to whether it can be improved upon by trying to keep the |
| 726 | * filtered data going to zlib more consistent, hopefully resulting in |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame^] | 727 | * better compression. |
| 728 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 729 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */ |
| 730 | void |
| 731 | png_set_filter_heuristics(png_structp png_ptr, int heuristic_method, |
| 732 | int num_weights, png_doublep filter_weights, |
| 733 | png_doublep filter_costs) |
| 734 | { |
| 735 | int i; |
| 736 | |
| 737 | png_debug(1, "in png_set_filter_heuristics\n"); |
| 738 | if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST) |
| 739 | { |
| 740 | png_warning(png_ptr, "Unknown filter heuristic method"); |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT) |
| 745 | { |
| 746 | heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED; |
| 747 | } |
| 748 | |
| 749 | if (num_weights < 0 || filter_weights == NULL || |
| 750 | heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED) |
| 751 | { |
| 752 | num_weights = 0; |
| 753 | } |
| 754 | |
| 755 | png_ptr->num_prev_filters = num_weights; |
| 756 | png_ptr->heuristic_method = heuristic_method; |
| 757 | |
| 758 | if (num_weights > 0) |
| 759 | { |
| 760 | if (png_ptr->prev_filters == NULL) |
| 761 | { |
| 762 | png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr, |
| 763 | sizeof(png_byte) * num_weights); |
| 764 | |
| 765 | /* To make sure that the weighting starts out fairly */ |
| 766 | for (i = 0; i < num_weights; i++) |
| 767 | { |
| 768 | png_ptr->prev_filters[i] = 255; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | if (png_ptr->filter_weights == NULL) |
| 773 | { |
| 774 | png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr, |
| 775 | sizeof(png_uint_16) * num_weights); |
| 776 | |
| 777 | png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr, |
| 778 | sizeof(png_uint_16) * num_weights); |
| 779 | |
| 780 | for (i = 0; i < num_weights; i++) |
| 781 | { |
| 782 | png_ptr->inv_filter_weights[i] = |
| 783 | png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | for (i = 0; i < num_weights; i++) |
| 788 | { |
| 789 | if (filter_weights[i] < 0.0) |
| 790 | { |
| 791 | png_ptr->inv_filter_weights[i] = |
| 792 | png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; |
| 793 | } |
| 794 | else |
| 795 | { |
| 796 | png_ptr->inv_filter_weights[i] = |
| 797 | (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5); |
| 798 | png_ptr->filter_weights[i] = |
| 799 | (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5); |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /* If, in the future, there are other filter methods, this would |
| 805 | * need to be based on png_ptr->filter. |
| 806 | */ |
| 807 | if (png_ptr->filter_costs == NULL) |
| 808 | { |
| 809 | png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr, |
| 810 | sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST); |
| 811 | |
| 812 | png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr, |
| 813 | sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST); |
| 814 | |
| 815 | for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) |
| 816 | { |
| 817 | png_ptr->inv_filter_costs[i] = |
| 818 | png_ptr->filter_costs[i] = PNG_COST_FACTOR; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | /* Here is where we set the relative costs of the different filters. We |
| 823 | * should take the desired compression level into account when setting |
| 824 | * the costs, so that Paeth, for instance, has a high relative cost at low |
| 825 | * compression levels, while it has a lower relative cost at higher |
| 826 | * compression settings. The filter types are in order of increasing |
| 827 | * relative cost, so it would be possible to do this with an algorithm. |
| 828 | */ |
| 829 | for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) |
| 830 | { |
| 831 | if (filter_costs == NULL || filter_costs[i] < 0.0) |
| 832 | { |
| 833 | png_ptr->inv_filter_costs[i] = |
| 834 | png_ptr->filter_costs[i] = PNG_COST_FACTOR; |
| 835 | } |
| 836 | else if (filter_costs[i] >= 1.0) |
| 837 | { |
| 838 | png_ptr->inv_filter_costs[i] = |
| 839 | (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5); |
| 840 | png_ptr->filter_costs[i] = |
| 841 | (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5); |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */ |
| 846 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 847 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 848 | png_set_compression_level(png_structp png_ptr, int level) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 849 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 850 | png_debug(1, "in png_set_compression_level\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 851 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 852 | png_ptr->zlib_level = level; |
| 853 | } |
| 854 | |
| 855 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 856 | png_set_compression_mem_level(png_structp png_ptr, int mem_level) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 857 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 858 | png_debug(1, "in png_set_compression_mem_level\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 859 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 860 | png_ptr->zlib_mem_level = mem_level; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 864 | png_set_compression_strategy(png_structp png_ptr, int strategy) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 865 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 866 | png_debug(1, "in png_set_compression_strategy\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 867 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 868 | png_ptr->zlib_strategy = strategy; |
| 869 | } |
| 870 | |
| 871 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 872 | png_set_compression_window_bits(png_structp png_ptr, int window_bits) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 873 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 874 | if (window_bits > 15) |
| 875 | png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); |
| 876 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 877 | png_ptr->zlib_window_bits = window_bits; |
| 878 | } |
| 879 | |
| 880 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 881 | png_set_compression_method(png_structp png_ptr, int method) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 882 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 883 | png_debug(1, "in png_set_compression_method\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 884 | if (method != 8) |
| 885 | png_warning(png_ptr, "Only compression method 8 is supported by PNG"); |
| 886 | png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 887 | png_ptr->zlib_method = method; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 888 | } |
| 889 | |