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