annotate lib/libpng/pngwutil.c @ 2551:4008b2f34be0

MainMenu rendering moved from event loop to respective window class
author a.parshin
date Tue, 12 May 2015 12:03:30 +0200
parents 6e178010fc29
children
rev   line source
2296
Ritor1
parents:
diff changeset
1
Ritor1
parents:
diff changeset
2 /* pngwutil.c - utilities to write a PNG file
Ritor1
parents:
diff changeset
3 *
Ritor1
parents:
diff changeset
4 * Last changed in libpng 1.6.2 [April 25, 2013]
Ritor1
parents:
diff changeset
5 * Copyright (c) 1998-2013 Glenn Randers-Pehrson
Ritor1
parents:
diff changeset
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
Ritor1
parents:
diff changeset
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Ritor1
parents:
diff changeset
8 *
Ritor1
parents:
diff changeset
9 * This code is released under the libpng license.
Ritor1
parents:
diff changeset
10 * For conditions of distribution and use, see the disclaimer
Ritor1
parents:
diff changeset
11 * and license in png.h
Ritor1
parents:
diff changeset
12 */
Ritor1
parents:
diff changeset
13
Ritor1
parents:
diff changeset
14 #include "pngpriv.h"
Ritor1
parents:
diff changeset
15
Ritor1
parents:
diff changeset
16 #ifdef PNG_WRITE_SUPPORTED
Ritor1
parents:
diff changeset
17
Ritor1
parents:
diff changeset
18 #ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED
Ritor1
parents:
diff changeset
19 /* Place a 32-bit number into a buffer in PNG byte order. We work
Ritor1
parents:
diff changeset
20 * with unsigned numbers for convenience, although one supported
Ritor1
parents:
diff changeset
21 * ancillary chunk uses signed (two's complement) numbers.
Ritor1
parents:
diff changeset
22 */
Ritor1
parents:
diff changeset
23 void PNGAPI
Ritor1
parents:
diff changeset
24 png_save_uint_32(png_bytep buf, png_uint_32 i)
Ritor1
parents:
diff changeset
25 {
Ritor1
parents:
diff changeset
26 buf[0] = (png_byte)((i >> 24) & 0xff);
Ritor1
parents:
diff changeset
27 buf[1] = (png_byte)((i >> 16) & 0xff);
Ritor1
parents:
diff changeset
28 buf[2] = (png_byte)((i >> 8) & 0xff);
Ritor1
parents:
diff changeset
29 buf[3] = (png_byte)(i & 0xff);
Ritor1
parents:
diff changeset
30 }
Ritor1
parents:
diff changeset
31
Ritor1
parents:
diff changeset
32 /* Place a 16-bit number into a buffer in PNG byte order.
Ritor1
parents:
diff changeset
33 * The parameter is declared unsigned int, not png_uint_16,
Ritor1
parents:
diff changeset
34 * just to avoid potential problems on pre-ANSI C compilers.
Ritor1
parents:
diff changeset
35 */
Ritor1
parents:
diff changeset
36 void PNGAPI
Ritor1
parents:
diff changeset
37 png_save_uint_16(png_bytep buf, unsigned int i)
Ritor1
parents:
diff changeset
38 {
Ritor1
parents:
diff changeset
39 buf[0] = (png_byte)((i >> 8) & 0xff);
Ritor1
parents:
diff changeset
40 buf[1] = (png_byte)(i & 0xff);
Ritor1
parents:
diff changeset
41 }
Ritor1
parents:
diff changeset
42 #endif
Ritor1
parents:
diff changeset
43
Ritor1
parents:
diff changeset
44 /* Simple function to write the signature. If we have already written
Ritor1
parents:
diff changeset
45 * the magic bytes of the signature, or more likely, the PNG stream is
Ritor1
parents:
diff changeset
46 * being embedded into another stream and doesn't need its own signature,
Ritor1
parents:
diff changeset
47 * we should call png_set_sig_bytes() to tell libpng how many of the
Ritor1
parents:
diff changeset
48 * bytes have already been written.
Ritor1
parents:
diff changeset
49 */
Ritor1
parents:
diff changeset
50 void PNGAPI
Ritor1
parents:
diff changeset
51 png_write_sig(png_structrp png_ptr)
Ritor1
parents:
diff changeset
52 {
Ritor1
parents:
diff changeset
53 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Ritor1
parents:
diff changeset
54
Ritor1
parents:
diff changeset
55 #ifdef PNG_IO_STATE_SUPPORTED
Ritor1
parents:
diff changeset
56 /* Inform the I/O callback that the signature is being written */
Ritor1
parents:
diff changeset
57 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
Ritor1
parents:
diff changeset
58 #endif
Ritor1
parents:
diff changeset
59
Ritor1
parents:
diff changeset
60 /* Write the rest of the 8 byte signature */
Ritor1
parents:
diff changeset
61 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Ritor1
parents:
diff changeset
62 (png_size_t)(8 - png_ptr->sig_bytes));
Ritor1
parents:
diff changeset
63
Ritor1
parents:
diff changeset
64 if (png_ptr->sig_bytes < 3)
Ritor1
parents:
diff changeset
65 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Ritor1
parents:
diff changeset
66 }
Ritor1
parents:
diff changeset
67
Ritor1
parents:
diff changeset
68 /* Write the start of a PNG chunk. The type is the chunk type.
Ritor1
parents:
diff changeset
69 * The total_length is the sum of the lengths of all the data you will be
Ritor1
parents:
diff changeset
70 * passing in png_write_chunk_data().
Ritor1
parents:
diff changeset
71 */
Ritor1
parents:
diff changeset
72 static void
Ritor1
parents:
diff changeset
73 png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name,
Ritor1
parents:
diff changeset
74 png_uint_32 length)
Ritor1
parents:
diff changeset
75 {
Ritor1
parents:
diff changeset
76 png_byte buf[8];
Ritor1
parents:
diff changeset
77
Ritor1
parents:
diff changeset
78 #if defined(PNG_DEBUG) && (PNG_DEBUG > 0)
Ritor1
parents:
diff changeset
79 PNG_CSTRING_FROM_CHUNK(buf, chunk_name);
Ritor1
parents:
diff changeset
80 png_debug2(0, "Writing %s chunk, length = %lu", buf, (unsigned long)length);
Ritor1
parents:
diff changeset
81 #endif
Ritor1
parents:
diff changeset
82
Ritor1
parents:
diff changeset
83 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
84 return;
Ritor1
parents:
diff changeset
85
Ritor1
parents:
diff changeset
86 #ifdef PNG_IO_STATE_SUPPORTED
Ritor1
parents:
diff changeset
87 /* Inform the I/O callback that the chunk header is being written.
Ritor1
parents:
diff changeset
88 * PNG_IO_CHUNK_HDR requires a single I/O call.
Ritor1
parents:
diff changeset
89 */
Ritor1
parents:
diff changeset
90 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
Ritor1
parents:
diff changeset
91 #endif
Ritor1
parents:
diff changeset
92
Ritor1
parents:
diff changeset
93 /* Write the length and the chunk name */
Ritor1
parents:
diff changeset
94 png_save_uint_32(buf, length);
Ritor1
parents:
diff changeset
95 png_save_uint_32(buf + 4, chunk_name);
Ritor1
parents:
diff changeset
96 png_write_data(png_ptr, buf, 8);
Ritor1
parents:
diff changeset
97
Ritor1
parents:
diff changeset
98 /* Put the chunk name into png_ptr->chunk_name */
Ritor1
parents:
diff changeset
99 png_ptr->chunk_name = chunk_name;
Ritor1
parents:
diff changeset
100
Ritor1
parents:
diff changeset
101 /* Reset the crc and run it over the chunk name */
Ritor1
parents:
diff changeset
102 png_reset_crc(png_ptr);
Ritor1
parents:
diff changeset
103
Ritor1
parents:
diff changeset
104 png_calculate_crc(png_ptr, buf + 4, 4);
Ritor1
parents:
diff changeset
105
Ritor1
parents:
diff changeset
106 #ifdef PNG_IO_STATE_SUPPORTED
Ritor1
parents:
diff changeset
107 /* Inform the I/O callback that chunk data will (possibly) be written.
Ritor1
parents:
diff changeset
108 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
Ritor1
parents:
diff changeset
109 */
Ritor1
parents:
diff changeset
110 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
Ritor1
parents:
diff changeset
111 #endif
Ritor1
parents:
diff changeset
112 }
Ritor1
parents:
diff changeset
113
Ritor1
parents:
diff changeset
114 void PNGAPI
Ritor1
parents:
diff changeset
115 png_write_chunk_start(png_structrp png_ptr, png_const_bytep chunk_string,
Ritor1
parents:
diff changeset
116 png_uint_32 length)
Ritor1
parents:
diff changeset
117 {
Ritor1
parents:
diff changeset
118 png_write_chunk_header(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), length);
Ritor1
parents:
diff changeset
119 }
Ritor1
parents:
diff changeset
120
Ritor1
parents:
diff changeset
121 /* Write the data of a PNG chunk started with png_write_chunk_header().
Ritor1
parents:
diff changeset
122 * Note that multiple calls to this function are allowed, and that the
Ritor1
parents:
diff changeset
123 * sum of the lengths from these calls *must* add up to the total_length
Ritor1
parents:
diff changeset
124 * given to png_write_chunk_header().
Ritor1
parents:
diff changeset
125 */
Ritor1
parents:
diff changeset
126 void PNGAPI
Ritor1
parents:
diff changeset
127 png_write_chunk_data(png_structrp png_ptr, png_const_bytep data,
Ritor1
parents:
diff changeset
128 png_size_t length)
Ritor1
parents:
diff changeset
129 {
Ritor1
parents:
diff changeset
130 /* Write the data, and run the CRC over it */
Ritor1
parents:
diff changeset
131 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
132 return;
Ritor1
parents:
diff changeset
133
Ritor1
parents:
diff changeset
134 if (data != NULL && length > 0)
Ritor1
parents:
diff changeset
135 {
Ritor1
parents:
diff changeset
136 png_write_data(png_ptr, data, length);
Ritor1
parents:
diff changeset
137
Ritor1
parents:
diff changeset
138 /* Update the CRC after writing the data,
Ritor1
parents:
diff changeset
139 * in case that the user I/O routine alters it.
Ritor1
parents:
diff changeset
140 */
Ritor1
parents:
diff changeset
141 png_calculate_crc(png_ptr, data, length);
Ritor1
parents:
diff changeset
142 }
Ritor1
parents:
diff changeset
143 }
Ritor1
parents:
diff changeset
144
Ritor1
parents:
diff changeset
145 /* Finish a chunk started with png_write_chunk_header(). */
Ritor1
parents:
diff changeset
146 void PNGAPI
Ritor1
parents:
diff changeset
147 png_write_chunk_end(png_structrp png_ptr)
Ritor1
parents:
diff changeset
148 {
Ritor1
parents:
diff changeset
149 png_byte buf[4];
Ritor1
parents:
diff changeset
150
Ritor1
parents:
diff changeset
151 if (png_ptr == NULL) return;
Ritor1
parents:
diff changeset
152
Ritor1
parents:
diff changeset
153 #ifdef PNG_IO_STATE_SUPPORTED
Ritor1
parents:
diff changeset
154 /* Inform the I/O callback that the chunk CRC is being written.
Ritor1
parents:
diff changeset
155 * PNG_IO_CHUNK_CRC requires a single I/O function call.
Ritor1
parents:
diff changeset
156 */
Ritor1
parents:
diff changeset
157 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
Ritor1
parents:
diff changeset
158 #endif
Ritor1
parents:
diff changeset
159
Ritor1
parents:
diff changeset
160 /* Write the crc in a single operation */
Ritor1
parents:
diff changeset
161 png_save_uint_32(buf, png_ptr->crc);
Ritor1
parents:
diff changeset
162
Ritor1
parents:
diff changeset
163 png_write_data(png_ptr, buf, (png_size_t)4);
Ritor1
parents:
diff changeset
164 }
Ritor1
parents:
diff changeset
165
Ritor1
parents:
diff changeset
166 /* Write a PNG chunk all at once. The type is an array of ASCII characters
Ritor1
parents:
diff changeset
167 * representing the chunk name. The array must be at least 4 bytes in
Ritor1
parents:
diff changeset
168 * length, and does not need to be null terminated. To be safe, pass the
Ritor1
parents:
diff changeset
169 * pre-defined chunk names here, and if you need a new one, define it
Ritor1
parents:
diff changeset
170 * where the others are defined. The length is the length of the data.
Ritor1
parents:
diff changeset
171 * All the data must be present. If that is not possible, use the
Ritor1
parents:
diff changeset
172 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
Ritor1
parents:
diff changeset
173 * functions instead.
Ritor1
parents:
diff changeset
174 */
Ritor1
parents:
diff changeset
175 static void
Ritor1
parents:
diff changeset
176 png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name,
Ritor1
parents:
diff changeset
177 png_const_bytep data, png_size_t length)
Ritor1
parents:
diff changeset
178 {
Ritor1
parents:
diff changeset
179 if (png_ptr == NULL)
Ritor1
parents:
diff changeset
180 return;
Ritor1
parents:
diff changeset
181
Ritor1
parents:
diff changeset
182 /* On 64 bit architectures 'length' may not fit in a png_uint_32. */
Ritor1
parents:
diff changeset
183 if (length > PNG_UINT_31_MAX)
Ritor1
parents:
diff changeset
184 png_error(png_ptr, "length exceeds PNG maxima");
Ritor1
parents:
diff changeset
185
Ritor1
parents:
diff changeset
186 png_write_chunk_header(png_ptr, chunk_name, (png_uint_32)length);
Ritor1
parents:
diff changeset
187 png_write_chunk_data(png_ptr, data, length);
Ritor1
parents:
diff changeset
188 png_write_chunk_end(png_ptr);
Ritor1
parents:
diff changeset
189 }
Ritor1
parents:
diff changeset
190
Ritor1
parents:
diff changeset
191 /* This is the API that calls the internal function above. */
Ritor1
parents:
diff changeset
192 void PNGAPI
Ritor1
parents:
diff changeset
193 png_write_chunk(png_structrp png_ptr, png_const_bytep chunk_string,
Ritor1
parents:
diff changeset
194 png_const_bytep data, png_size_t length)
Ritor1
parents:
diff changeset
195 {
Ritor1
parents:
diff changeset
196 png_write_complete_chunk(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), data,
Ritor1
parents:
diff changeset
197 length);
Ritor1
parents:
diff changeset
198 }
Ritor1
parents:
diff changeset
199
Ritor1
parents:
diff changeset
200 /* This is used below to find the size of an image to pass to png_deflate_claim,
Ritor1
parents:
diff changeset
201 * so it only needs to be accurate if the size is less than 16384 bytes (the
Ritor1
parents:
diff changeset
202 * point at which a lower LZ window size can be used.)
Ritor1
parents:
diff changeset
203 */
Ritor1
parents:
diff changeset
204 static png_alloc_size_t
Ritor1
parents:
diff changeset
205 png_image_size(png_structrp png_ptr)
Ritor1
parents:
diff changeset
206 {
Ritor1
parents:
diff changeset
207 /* Only return sizes up to the maximum of a png_uint_32, do this by limiting
Ritor1
parents:
diff changeset
208 * the width and height used to 15 bits.
Ritor1
parents:
diff changeset
209 */
Ritor1
parents:
diff changeset
210 png_uint_32 h = png_ptr->height;
Ritor1
parents:
diff changeset
211
Ritor1
parents:
diff changeset
212 if (png_ptr->rowbytes < 32768 && h < 32768)
Ritor1
parents:
diff changeset
213 {
Ritor1
parents:
diff changeset
214 if (png_ptr->interlaced)
Ritor1
parents:
diff changeset
215 {
Ritor1
parents:
diff changeset
216 /* Interlacing makes the image larger because of the replication of
Ritor1
parents:
diff changeset
217 * both the filter byte and the padding to a byte boundary.
Ritor1
parents:
diff changeset
218 */
Ritor1
parents:
diff changeset
219 png_uint_32 w = png_ptr->width;
Ritor1
parents:
diff changeset
220 unsigned int pd = png_ptr->pixel_depth;
Ritor1
parents:
diff changeset
221 png_alloc_size_t cb_base;
Ritor1
parents:
diff changeset
222 int pass;
Ritor1
parents:
diff changeset
223
Ritor1
parents:
diff changeset
224 for (cb_base=0, pass=0; pass<=6; ++pass)
Ritor1
parents:
diff changeset
225 {
Ritor1
parents:
diff changeset
226 png_uint_32 pw = PNG_PASS_COLS(w, pass);
Ritor1
parents:
diff changeset
227
Ritor1
parents:
diff changeset
228 if (pw > 0)
Ritor1
parents:
diff changeset
229 cb_base += (PNG_ROWBYTES(pd, pw)+1) * PNG_PASS_ROWS(h, pass);
Ritor1
parents:
diff changeset
230 }
Ritor1
parents:
diff changeset
231
Ritor1
parents:
diff changeset
232 return cb_base;
Ritor1
parents:
diff changeset
233 }
Ritor1
parents:
diff changeset
234
Ritor1
parents:
diff changeset
235 else
Ritor1
parents:
diff changeset
236 return (png_ptr->rowbytes+1) * h;
Ritor1
parents:
diff changeset
237 }
Ritor1
parents:
diff changeset
238
Ritor1
parents:
diff changeset
239 else
Ritor1
parents:
diff changeset
240 return 0xffffffffU;
Ritor1
parents:
diff changeset
241 }
Ritor1
parents:
diff changeset
242
Ritor1
parents:
diff changeset
243 #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
Ritor1
parents:
diff changeset
244 /* This is the code to hack the first two bytes of the deflate stream (the
Ritor1
parents:
diff changeset
245 * deflate header) to correct the windowBits value to match the actual data
Ritor1
parents:
diff changeset
246 * size. Note that the second argument is the *uncompressed* size but the
Ritor1
parents:
diff changeset
247 * first argument is the *compressed* data (and it must be deflate
Ritor1
parents:
diff changeset
248 * compressed.)
Ritor1
parents:
diff changeset
249 */
Ritor1
parents:
diff changeset
250 static void
Ritor1
parents:
diff changeset
251 optimize_cmf(png_bytep data, png_alloc_size_t data_size)
Ritor1
parents:
diff changeset
252 {
Ritor1
parents:
diff changeset
253 /* Optimize the CMF field in the zlib stream. The resultant zlib stream is
Ritor1
parents:
diff changeset
254 * still compliant to the stream specification.
Ritor1
parents:
diff changeset
255 */
Ritor1
parents:
diff changeset
256 if (data_size <= 16384) /* else windowBits must be 15 */
Ritor1
parents:
diff changeset
257 {
Ritor1
parents:
diff changeset
258 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
Ritor1
parents:
diff changeset
259
Ritor1
parents:
diff changeset
260 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
Ritor1
parents:
diff changeset
261 {
Ritor1
parents:
diff changeset
262 unsigned int z_cinfo;
Ritor1
parents:
diff changeset
263 unsigned int half_z_window_size;
Ritor1
parents:
diff changeset
264
Ritor1
parents:
diff changeset
265 z_cinfo = z_cmf >> 4;
Ritor1
parents:
diff changeset
266 half_z_window_size = 1U << (z_cinfo + 7);
Ritor1
parents:
diff changeset
267
Ritor1
parents:
diff changeset
268 if (data_size <= half_z_window_size) /* else no change */
Ritor1
parents:
diff changeset
269 {
Ritor1
parents:
diff changeset
270 unsigned int tmp;
Ritor1
parents:
diff changeset
271
Ritor1
parents:
diff changeset
272 do
Ritor1
parents:
diff changeset
273 {
Ritor1
parents:
diff changeset
274 half_z_window_size >>= 1;
Ritor1
parents:
diff changeset
275 --z_cinfo;
Ritor1
parents:
diff changeset
276 }
Ritor1
parents:
diff changeset
277 while (z_cinfo > 0 && data_size <= half_z_window_size);
Ritor1
parents:
diff changeset
278
Ritor1
parents:
diff changeset
279 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
Ritor1
parents:
diff changeset
280
Ritor1
parents:
diff changeset
281 data[0] = (png_byte)z_cmf;
Ritor1
parents:
diff changeset
282 tmp = data[1] & 0xe0;
Ritor1
parents:
diff changeset
283 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
Ritor1
parents:
diff changeset
284 data[1] = (png_byte)tmp;
Ritor1
parents:
diff changeset
285 }
Ritor1
parents:
diff changeset
286 }
Ritor1
parents:
diff changeset
287 }
Ritor1
parents:
diff changeset
288 }
Ritor1
parents:
diff changeset
289 #else
Ritor1
parents:
diff changeset
290 # define optimize_cmf(dp,dl) ((void)0)
Ritor1
parents:
diff changeset
291 #endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */
Ritor1
parents:
diff changeset
292
Ritor1
parents:
diff changeset
293 /* Initialize the compressor for the appropriate type of compression. */
Ritor1
parents:
diff changeset
294 static int
Ritor1
parents:
diff changeset
295 png_deflate_claim(png_structrp png_ptr, png_uint_32 owner,
Ritor1
parents:
diff changeset
296 png_alloc_size_t data_size)
Ritor1
parents:
diff changeset
297 {
Ritor1
parents:
diff changeset
298 if (png_ptr->zowner != 0)
Ritor1
parents:
diff changeset
299 {
Ritor1
parents:
diff changeset
300 char msg[64];
Ritor1
parents:
diff changeset
301
Ritor1
parents:
diff changeset
302 PNG_STRING_FROM_CHUNK(msg, owner);
Ritor1
parents:
diff changeset
303 msg[4] = ':';
Ritor1
parents:
diff changeset
304 msg[5] = ' ';
Ritor1
parents:
diff changeset
305 PNG_STRING_FROM_CHUNK(msg+6, png_ptr->zowner);
Ritor1
parents:
diff changeset
306 /* So the message that results is "<chunk> using zstream"; this is an
Ritor1
parents:
diff changeset
307 * internal error, but is very useful for debugging. i18n requirements
Ritor1
parents:
diff changeset
308 * are minimal.
Ritor1
parents:
diff changeset
309 */
Ritor1
parents:
diff changeset
310 (void)png_safecat(msg, (sizeof msg), 10, " using zstream");
Ritor1
parents:
diff changeset
311 # if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
Ritor1
parents:
diff changeset
312 png_warning(png_ptr, msg);
Ritor1
parents:
diff changeset
313
Ritor1
parents:
diff changeset
314 /* Attempt sane error recovery */
Ritor1
parents:
diff changeset
315 if (png_ptr->zowner == png_IDAT) /* don't steal from IDAT */
Ritor1
parents:
diff changeset
316 {
Ritor1
parents:
diff changeset
317 png_ptr->zstream.msg = PNGZ_MSG_CAST("in use by IDAT");
Ritor1
parents:
diff changeset
318 return Z_STREAM_ERROR;
Ritor1
parents:
diff changeset
319 }
Ritor1
parents:
diff changeset
320
Ritor1
parents:
diff changeset
321 png_ptr->zowner = 0;
Ritor1
parents:
diff changeset
322 # else
Ritor1
parents:
diff changeset
323 png_error(png_ptr, msg);
Ritor1
parents:
diff changeset
324 # endif
Ritor1
parents:
diff changeset
325 }
Ritor1
parents:
diff changeset
326
Ritor1
parents:
diff changeset
327 {
Ritor1
parents:
diff changeset
328 int level = png_ptr->zlib_level;
Ritor1
parents:
diff changeset
329 int method = png_ptr->zlib_method;
Ritor1
parents:
diff changeset
330 int windowBits = png_ptr->zlib_window_bits;
Ritor1
parents:
diff changeset
331 int memLevel = png_ptr->zlib_mem_level;
Ritor1
parents:
diff changeset
332 int strategy; /* set below */
Ritor1
parents:
diff changeset
333 int ret; /* zlib return code */
Ritor1
parents:
diff changeset
334
Ritor1
parents:
diff changeset
335 if (owner == png_IDAT)
Ritor1
parents:
diff changeset
336 {
Ritor1
parents:
diff changeset
337 if (png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY)
Ritor1
parents:
diff changeset
338 strategy = png_ptr->zlib_strategy;
Ritor1
parents:
diff changeset
339
Ritor1
parents:
diff changeset
340 else if (png_ptr->do_filter != PNG_FILTER_NONE)
Ritor1
parents:
diff changeset
341 strategy = PNG_Z_DEFAULT_STRATEGY;
Ritor1
parents:
diff changeset
342
Ritor1
parents:
diff changeset
343 else
Ritor1
parents:
diff changeset
344 strategy = PNG_Z_DEFAULT_NOFILTER_STRATEGY;
Ritor1
parents:
diff changeset
345 }
Ritor1
parents:
diff changeset
346
Ritor1
parents:
diff changeset
347 else
Ritor1
parents:
diff changeset
348 {
Ritor1
parents:
diff changeset
349 # ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
Ritor1
parents:
diff changeset
350 level = png_ptr->zlib_text_level;
Ritor1
parents:
diff changeset
351 method = png_ptr->zlib_text_method;
Ritor1
parents:
diff changeset
352 windowBits = png_ptr->zlib_text_window_bits;
Ritor1
parents:
diff changeset
353 memLevel = png_ptr->zlib_text_mem_level;
Ritor1
parents:
diff changeset
354 strategy = png_ptr->zlib_text_strategy;
Ritor1
parents:
diff changeset
355 # else
Ritor1
parents:
diff changeset
356 /* If customization is not supported the values all come from the
Ritor1
parents:
diff changeset
357 * IDAT values except for the strategy, which is fixed to the
Ritor1
parents:
diff changeset
358 * default. (This is the pre-1.6.0 behavior too, although it was
Ritor1
parents:
diff changeset
359 * implemented in a very different way.)
Ritor1
parents:
diff changeset
360 */
Ritor1
parents:
diff changeset
361 strategy = Z_DEFAULT_STRATEGY;
Ritor1
parents:
diff changeset
362 # endif
Ritor1
parents:
diff changeset
363 }
Ritor1
parents:
diff changeset
364
Ritor1
parents:
diff changeset
365 /* Adjust 'windowBits' down if larger than 'data_size'; to stop this
Ritor1
parents:
diff changeset
366 * happening just pass 32768 as the data_size parameter. Notice that zlib
Ritor1
parents:
diff changeset
367 * requires an extra 262 bytes in the window in addition to the data to be
Ritor1
parents:
diff changeset
368 * able to see the whole of the data, so if data_size+262 takes us to the
Ritor1
parents:
diff changeset
369 * next windowBits size we need to fix up the value later. (Because even
Ritor1
parents:
diff changeset
370 * though deflate needs the extra window, inflate does not!)
Ritor1
parents:
diff changeset
371 */
Ritor1
parents:
diff changeset
372 if (data_size <= 16384)
Ritor1
parents:
diff changeset
373 {
Ritor1
parents:
diff changeset
374 /* IMPLEMENTATION NOTE: this 'half_window_size' stuff is only here to
Ritor1
parents:
diff changeset
375 * work round a Microsoft Visual C misbehavior which, contrary to C-90,
Ritor1
parents:
diff changeset
376 * widens the result of the following shift to 64-bits if (and,
Ritor1
parents:
diff changeset
377 * apparently, only if) it is used in a test.
Ritor1
parents:
diff changeset
378 */
Ritor1
parents:
diff changeset
379 unsigned int half_window_size = 1U << (windowBits-1);
Ritor1
parents:
diff changeset
380
Ritor1
parents:
diff changeset
381 while (data_size + 262 <= half_window_size)
Ritor1
parents:
diff changeset
382 {
Ritor1
parents:
diff changeset
383 half_window_size >>= 1;
Ritor1
parents:
diff changeset
384 --windowBits;
Ritor1
parents:
diff changeset
385 }
Ritor1
parents:
diff changeset
386 }
Ritor1
parents:
diff changeset
387
Ritor1
parents:
diff changeset
388 /* Check against the previous initialized values, if any. */
Ritor1
parents:
diff changeset
389 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) &&
Ritor1
parents:
diff changeset
390 (png_ptr->zlib_set_level != level ||
Ritor1
parents:
diff changeset
391 png_ptr->zlib_set_method != method ||
Ritor1
parents:
diff changeset
392 png_ptr->zlib_set_window_bits != windowBits ||
Ritor1
parents:
diff changeset
393 png_ptr->zlib_set_mem_level != memLevel ||
Ritor1
parents:
diff changeset
394 png_ptr->zlib_set_strategy != strategy))
Ritor1
parents:
diff changeset
395 {
Ritor1
parents:
diff changeset
396 if (deflateEnd(&png_ptr->zstream) != Z_OK)
Ritor1
parents:
diff changeset
397 png_warning(png_ptr, "deflateEnd failed (ignored)");
Ritor1
parents:
diff changeset
398
Ritor1
parents:
diff changeset
399 png_ptr->flags &= ~PNG_FLAG_ZSTREAM_INITIALIZED;
Ritor1
parents:
diff changeset
400 }
Ritor1
parents:
diff changeset
401
Ritor1
parents:
diff changeset
402 /* For safety clear out the input and output pointers (currently zlib
Ritor1
parents:
diff changeset
403 * doesn't use them on Init, but it might in the future).
Ritor1
parents:
diff changeset
404 */
Ritor1
parents:
diff changeset
405 png_ptr->zstream.next_in = NULL;
Ritor1
parents:
diff changeset
406 png_ptr->zstream.avail_in = 0;
Ritor1
parents:
diff changeset
407 png_ptr->zstream.next_out = NULL;
Ritor1
parents:
diff changeset
408 png_ptr->zstream.avail_out = 0;
Ritor1
parents:
diff changeset
409
Ritor1
parents:
diff changeset
410 /* Now initialize if required, setting the new parameters, otherwise just
Ritor1
parents:
diff changeset
411 * to a simple reset to the previous parameters.
Ritor1
parents:
diff changeset
412 */
Ritor1
parents:
diff changeset
413 if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED)
Ritor1
parents:
diff changeset
414 ret = deflateReset(&png_ptr->zstream);
Ritor1
parents:
diff changeset
415
Ritor1
parents:
diff changeset
416 else
Ritor1
parents:
diff changeset
417 {
Ritor1
parents:
diff changeset
418 ret = deflateInit2(&png_ptr->zstream, level, method, windowBits,
Ritor1
parents:
diff changeset
419 memLevel, strategy);
Ritor1
parents:
diff changeset
420
Ritor1
parents:
diff changeset
421 if (ret == Z_OK)
Ritor1
parents:
diff changeset
422 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
Ritor1
parents:
diff changeset
423 }
Ritor1
parents:
diff changeset
424
Ritor1
parents:
diff changeset
425 /* The return code is from either deflateReset or deflateInit2; they have
Ritor1
parents:
diff changeset
426 * pretty much the same set of error codes.
Ritor1
parents:
diff changeset
427 */
Ritor1
parents:
diff changeset
428 if (ret == Z_OK)
Ritor1
parents:
diff changeset
429 png_ptr->zowner = owner;
Ritor1
parents:
diff changeset
430
Ritor1
parents:
diff changeset
431 else
Ritor1
parents:
diff changeset
432 png_zstream_error(png_ptr, ret);
Ritor1
parents:
diff changeset
433
Ritor1
parents:
diff changeset
434 return ret;
Ritor1
parents:
diff changeset
435 }
Ritor1
parents:
diff changeset
436 }
Ritor1
parents:
diff changeset
437
Ritor1
parents:
diff changeset
438 /* Clean up (or trim) a linked list of compression buffers. */
Ritor1
parents:
diff changeset
439 void /* PRIVATE */
Ritor1
parents:
diff changeset
440 png_free_buffer_list(png_structrp png_ptr, png_compression_bufferp *listp)
Ritor1
parents:
diff changeset
441 {
Ritor1
parents:
diff changeset
442 png_compression_bufferp list = *listp;
Ritor1
parents:
diff changeset
443
Ritor1
parents:
diff changeset
444 if (list != NULL)
Ritor1
parents:
diff changeset
445 {
Ritor1
parents:
diff changeset
446 *listp = NULL;
Ritor1
parents:
diff changeset
447
Ritor1
parents:
diff changeset
448 do
Ritor1
parents:
diff changeset
449 {
Ritor1
parents:
diff changeset
450 png_compression_bufferp next = list->next;
Ritor1
parents:
diff changeset
451
Ritor1
parents:
diff changeset
452 png_free(png_ptr, list);
Ritor1
parents:
diff changeset
453 list = next;
Ritor1
parents:
diff changeset
454 }
Ritor1
parents:
diff changeset
455 while (list != NULL);
Ritor1
parents:
diff changeset
456 }
Ritor1
parents:
diff changeset
457 }
Ritor1
parents:
diff changeset
458
Ritor1
parents:
diff changeset
459 #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
Ritor1
parents:
diff changeset
460 /* This pair of functions encapsulates the operation of (a) compressing a
Ritor1
parents:
diff changeset
461 * text string, and (b) issuing it later as a series of chunk data writes.
Ritor1
parents:
diff changeset
462 * The compression_state structure is shared context for these functions
Ritor1
parents:
diff changeset
463 * set up by the caller to allow access to the relevant local variables.
Ritor1
parents:
diff changeset
464 *
Ritor1
parents:
diff changeset
465 * compression_buffer (new in 1.6.0) is just a linked list of zbuffer_size
Ritor1
parents:
diff changeset
466 * temporary buffers. From 1.6.0 it is retained in png_struct so that it will
Ritor1
parents:
diff changeset
467 * be correctly freed in the event of a write error (previous implementations
Ritor1
parents:
diff changeset
468 * just leaked memory.)
Ritor1
parents:
diff changeset
469 */
Ritor1
parents:
diff changeset
470 typedef struct
Ritor1
parents:
diff changeset
471 {
Ritor1
parents:
diff changeset
472 png_const_bytep input; /* The uncompressed input data */
Ritor1
parents:
diff changeset
473 png_alloc_size_t input_len; /* Its length */
Ritor1
parents:
diff changeset
474 png_uint_32 output_len; /* Final compressed length */
Ritor1
parents:
diff changeset
475 png_byte output[1024]; /* First block of output */
Ritor1
parents:
diff changeset
476 } compression_state;
Ritor1
parents:
diff changeset
477
Ritor1
parents:
diff changeset
478 static void
Ritor1
parents:
diff changeset
479 png_text_compress_init(compression_state *comp, png_const_bytep input,
Ritor1
parents:
diff changeset
480 png_alloc_size_t input_len)
Ritor1
parents:
diff changeset
481 {
Ritor1
parents:
diff changeset
482 comp->input = input;
Ritor1
parents:
diff changeset
483 comp->input_len = input_len;
Ritor1
parents:
diff changeset
484 comp->output_len = 0;
Ritor1
parents:
diff changeset
485 }
Ritor1
parents:
diff changeset
486
Ritor1
parents:
diff changeset
487 /* Compress the data in the compression state input */
Ritor1
parents:
diff changeset
488 static int
Ritor1
parents:
diff changeset
489 png_text_compress(png_structrp png_ptr, png_uint_32 chunk_name,
Ritor1
parents:
diff changeset
490 compression_state *comp, png_uint_32 prefix_len)
Ritor1
parents:
diff changeset
491 {
Ritor1
parents:
diff changeset
492 int ret;
Ritor1
parents:
diff changeset
493
Ritor1
parents:
diff changeset
494 /* To find the length of the output it is necessary to first compress the
Ritor1
parents:
diff changeset
495 * input, the result is buffered rather than using the two-pass algorithm
Ritor1
parents:
diff changeset
496 * that is used on the inflate side; deflate is assumed to be slower and a
Ritor1
parents:
diff changeset
497 * PNG writer is assumed to have more memory available than a PNG reader.
Ritor1
parents:
diff changeset
498 *
Ritor1
parents:
diff changeset
499 * IMPLEMENTATION NOTE: the zlib API deflateBound() can be used to find an
Ritor1
parents:
diff changeset
500 * upper limit on the output size, but it is always bigger than the input
Ritor1
parents:
diff changeset
501 * size so it is likely to be more efficient to use this linked-list
Ritor1
parents:
diff changeset
502 * approach.
Ritor1
parents:
diff changeset
503 */
Ritor1
parents:
diff changeset
504 ret = png_deflate_claim(png_ptr, chunk_name, comp->input_len);
Ritor1
parents:
diff changeset
505
Ritor1
parents:
diff changeset
506 if (ret != Z_OK)
Ritor1
parents:
diff changeset
507 return ret;
Ritor1
parents:
diff changeset
508
Ritor1
parents:
diff changeset
509 /* Set up the compression buffers, we need a loop here to avoid overflowing a
Ritor1
parents:
diff changeset
510 * uInt. Use ZLIB_IO_MAX to limit the input. The output is always limited
Ritor1
parents:
diff changeset
511 * by the output buffer size, so there is no need to check that. Since this
Ritor1
parents:
diff changeset
512 * is ANSI-C we know that an 'int', hence a uInt, is always at least 16 bits
Ritor1
parents:
diff changeset
513 * in size.
Ritor1
parents:
diff changeset
514 */
Ritor1
parents:
diff changeset
515 {
Ritor1
parents:
diff changeset
516 png_compression_bufferp *end = &png_ptr->zbuffer_list;
Ritor1
parents:
diff changeset
517 png_alloc_size_t input_len = comp->input_len; /* may be zero! */
Ritor1
parents:
diff changeset
518 png_uint_32 output_len;
Ritor1
parents:
diff changeset
519
Ritor1
parents:
diff changeset
520 /* zlib updates these for us: */
Ritor1
parents:
diff changeset
521 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(comp->input);
Ritor1
parents:
diff changeset
522 png_ptr->zstream.avail_in = 0; /* Set below */
Ritor1
parents:
diff changeset
523 png_ptr->zstream.next_out = comp->output;
Ritor1
parents:
diff changeset
524 png_ptr->zstream.avail_out = (sizeof comp->output);
Ritor1
parents:
diff changeset
525
Ritor1
parents:
diff changeset
526 output_len = png_ptr->zstream.avail_out;
Ritor1
parents:
diff changeset
527
Ritor1
parents:
diff changeset
528 do
Ritor1
parents:
diff changeset
529 {
Ritor1
parents:
diff changeset
530 uInt avail_in = ZLIB_IO_MAX;
Ritor1
parents:
diff changeset
531
Ritor1
parents:
diff changeset
532 if (avail_in > input_len)
Ritor1
parents:
diff changeset
533 avail_in = (uInt)input_len;
Ritor1
parents:
diff changeset
534
Ritor1
parents:
diff changeset
535 input_len -= avail_in;
Ritor1
parents:
diff changeset
536
Ritor1
parents:
diff changeset
537 png_ptr->zstream.avail_in = avail_in;
Ritor1
parents:
diff changeset
538
Ritor1
parents:
diff changeset
539 if (png_ptr->zstream.avail_out == 0)
Ritor1
parents:
diff changeset
540 {
Ritor1
parents:
diff changeset
541 png_compression_buffer *next;
Ritor1
parents:
diff changeset
542
Ritor1
parents:
diff changeset
543 /* Chunk data is limited to 2^31 bytes in length, so the prefix
Ritor1
parents:
diff changeset
544 * length must be counted here.
Ritor1
parents:
diff changeset
545 */
Ritor1
parents:
diff changeset
546 if (output_len + prefix_len > PNG_UINT_31_MAX)
Ritor1
parents:
diff changeset
547 {
Ritor1
parents:
diff changeset
548 ret = Z_MEM_ERROR;
Ritor1
parents:
diff changeset
549 break;
Ritor1
parents:
diff changeset
550 }
Ritor1
parents:
diff changeset
551
Ritor1
parents:
diff changeset
552 /* Need a new (malloc'ed) buffer, but there may be one present
Ritor1
parents:
diff changeset
553 * already.
Ritor1
parents:
diff changeset
554 */
Ritor1
parents:
diff changeset
555 next = *end;
Ritor1
parents:
diff changeset
556 if (next == NULL)
Ritor1
parents:
diff changeset
557 {
Ritor1
parents:
diff changeset
558 next = png_voidcast(png_compression_bufferp, png_malloc_base
Ritor1
parents:
diff changeset
559 (png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr)));
Ritor1
parents:
diff changeset
560
Ritor1
parents:
diff changeset
561 if (next == NULL)
Ritor1
parents:
diff changeset
562 {
Ritor1
parents:
diff changeset
563 ret = Z_MEM_ERROR;
Ritor1
parents:
diff changeset
564 break;
Ritor1
parents:
diff changeset
565 }
Ritor1
parents:
diff changeset
566
Ritor1
parents:
diff changeset
567 /* Link in this buffer (so that it will be freed later) */
Ritor1
parents:
diff changeset
568 next->next = NULL;
Ritor1
parents:
diff changeset
569 *end = next;
Ritor1
parents:
diff changeset
570 }
Ritor1
parents:
diff changeset
571
Ritor1
parents:
diff changeset
572 png_ptr->zstream.next_out = next->output;
Ritor1
parents:
diff changeset
573 png_ptr->zstream.avail_out = png_ptr->zbuffer_size;
Ritor1
parents:
diff changeset
574 output_len += png_ptr->zstream.avail_out;
Ritor1
parents:
diff changeset
575
Ritor1
parents:
diff changeset
576 /* Move 'end' to the next buffer pointer. */
Ritor1
parents:
diff changeset
577 end = &next->next;
Ritor1
parents:
diff changeset
578 }
Ritor1
parents:
diff changeset
579
Ritor1
parents:
diff changeset
580 /* Compress the data */
Ritor1
parents:
diff changeset
581 ret = deflate(&png_ptr->zstream,
Ritor1
parents:
diff changeset
582 input_len > 0 ? Z_NO_FLUSH : Z_FINISH);
Ritor1
parents:
diff changeset
583
Ritor1
parents:
diff changeset
584 /* Claw back input data that was not consumed (because avail_in is
Ritor1
parents:
diff changeset
585 * reset above every time round the loop).
Ritor1
parents:
diff changeset
586 */
Ritor1
parents:
diff changeset
587 input_len += png_ptr->zstream.avail_in;
Ritor1
parents:
diff changeset
588 png_ptr->zstream.avail_in = 0; /* safety */
Ritor1
parents:
diff changeset
589 }
Ritor1
parents:
diff changeset
590 while (ret == Z_OK);
Ritor1
parents:
diff changeset
591
Ritor1
parents:
diff changeset
592 /* There may be some space left in the last output buffer, this needs to
Ritor1
parents:
diff changeset
593 * be subtracted from output_len.
Ritor1
parents:
diff changeset
594 */
Ritor1
parents:
diff changeset
595 output_len -= png_ptr->zstream.avail_out;
Ritor1
parents:
diff changeset
596 png_ptr->zstream.avail_out = 0; /* safety */
Ritor1
parents:
diff changeset
597 comp->output_len = output_len;
Ritor1
parents:
diff changeset
598
Ritor1
parents:
diff changeset
599 /* Now double check the output length, put in a custom message if it is
Ritor1
parents:
diff changeset
600 * too long. Otherwise ensure the z_stream::msg pointer is set to
Ritor1
parents:
diff changeset
601 * something.
Ritor1
parents:
diff changeset
602 */
Ritor1
parents:
diff changeset
603 if (output_len + prefix_len >= PNG_UINT_31_MAX)
Ritor1
parents:
diff changeset
604 {
Ritor1
parents:
diff changeset
605 png_ptr->zstream.msg = PNGZ_MSG_CAST("compressed data too long");
Ritor1
parents:
diff changeset
606 ret = Z_MEM_ERROR;
Ritor1
parents:
diff changeset
607 }
Ritor1
parents:
diff changeset
608
Ritor1
parents:
diff changeset
609 else
Ritor1
parents:
diff changeset
610 png_zstream_error(png_ptr, ret);
Ritor1
parents:
diff changeset
611
Ritor1
parents:
diff changeset
612 /* Reset zlib for another zTXt/iTXt or image data */
Ritor1
parents:
diff changeset
613 png_ptr->zowner = 0;
Ritor1
parents:
diff changeset
614
Ritor1
parents:
diff changeset
615 /* The only success case is Z_STREAM_END, input_len must be 0, if not this
Ritor1
parents:
diff changeset
616 * is an internal error.
Ritor1
parents:
diff changeset
617 */
Ritor1
parents:
diff changeset
618 if (ret == Z_STREAM_END && input_len == 0)
Ritor1
parents:
diff changeset
619 {
Ritor1
parents:
diff changeset
620 /* Fix up the deflate header, if required */
Ritor1
parents:
diff changeset
621 optimize_cmf(comp->output, comp->input_len);
Ritor1
parents:
diff changeset
622
Ritor1
parents:
diff changeset
623 /* But Z_OK is returned, not Z_STREAM_END; this allows the claim
Ritor1
parents:
diff changeset
624 * function above to return Z_STREAM_END on an error (though it never
Ritor1
parents:
diff changeset
625 * does in the current versions of zlib.)
Ritor1
parents:
diff changeset
626 */
Ritor1
parents:
diff changeset
627 return Z_OK;
Ritor1
parents:
diff changeset
628 }
Ritor1
parents:
diff changeset
629
Ritor1
parents:
diff changeset
630 else
Ritor1
parents:
diff changeset
631 return ret;
Ritor1
parents:
diff changeset
632 }
Ritor1
parents:
diff changeset
633 }
Ritor1
parents:
diff changeset
634
Ritor1
parents:
diff changeset
635 /* Ship the compressed text out via chunk writes */
Ritor1
parents:
diff changeset
636 static void
Ritor1
parents:
diff changeset
637 png_write_compressed_data_out(png_structrp png_ptr, compression_state *comp)
Ritor1
parents:
diff changeset
638 {
Ritor1
parents:
diff changeset
639 png_uint_32 output_len = comp->output_len;
Ritor1
parents:
diff changeset
640 png_const_bytep output = comp->output;
Ritor1
parents:
diff changeset
641 png_uint_32 avail = (sizeof comp->output);
Ritor1
parents:
diff changeset
642 png_compression_buffer *next = png_ptr->zbuffer_list;
Ritor1
parents:
diff changeset
643
Ritor1
parents:
diff changeset
644 for (;;)
Ritor1
parents:
diff changeset
645 {
Ritor1
parents:
diff changeset
646 if (avail > output_len)
Ritor1
parents:
diff changeset
647 avail = output_len;
Ritor1
parents:
diff changeset
648
Ritor1
parents:
diff changeset
649 png_write_chunk_data(png_ptr, output, avail);
Ritor1
parents:
diff changeset
650
Ritor1
parents:
diff changeset
651 output_len -= avail;
Ritor1
parents:
diff changeset
652
Ritor1
parents:
diff changeset
653 if (output_len == 0 || next == NULL)
Ritor1
parents:
diff changeset
654 break;
Ritor1
parents:
diff changeset
655
Ritor1
parents:
diff changeset
656 avail = png_ptr->zbuffer_size;
Ritor1
parents:
diff changeset
657 output = next->output;
Ritor1
parents:
diff changeset
658 next = next->next;
Ritor1
parents:
diff changeset
659 }
Ritor1
parents:
diff changeset
660
Ritor1
parents:
diff changeset
661 /* This is an internal error; 'next' must have been NULL! */
Ritor1
parents:
diff changeset
662 if (output_len > 0)
Ritor1
parents:
diff changeset
663 png_error(png_ptr, "error writing ancillary chunked compressed data");
Ritor1
parents:
diff changeset
664 }
Ritor1
parents:
diff changeset
665 #endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
Ritor1
parents:
diff changeset
666
Ritor1
parents:
diff changeset
667 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
Ritor1
parents:
diff changeset
668 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Ritor1
parents:
diff changeset
669 /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
Ritor1
parents:
diff changeset
670 * and if invalid, correct the keyword rather than discarding the entire
Ritor1
parents:
diff changeset
671 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
Ritor1
parents:
diff changeset
672 * length, forbids leading or trailing whitespace, multiple internal spaces,
Ritor1
parents:
diff changeset
673 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Ritor1
parents:
diff changeset
674 *
Ritor1
parents:
diff changeset
675 * The 'new_key' buffer must be 80 characters in size (for the keyword plus a
Ritor1
parents:
diff changeset
676 * trailing '\0'). If this routine returns 0 then there was no keyword, or a
Ritor1
parents:
diff changeset
677 * valid one could not be generated, and the caller must png_error.
Ritor1
parents:
diff changeset
678 */
Ritor1
parents:
diff changeset
679 static png_uint_32
Ritor1
parents:
diff changeset
680 png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
Ritor1
parents:
diff changeset
681 {
Ritor1
parents:
diff changeset
682 png_const_charp orig_key = key;
Ritor1
parents:
diff changeset
683 png_uint_32 key_len = 0;
Ritor1
parents:
diff changeset
684 int bad_character = 0;
Ritor1
parents:
diff changeset
685 int space = 1;
Ritor1
parents:
diff changeset
686
Ritor1
parents:
diff changeset
687 png_debug(1, "in png_check_keyword");
Ritor1
parents:
diff changeset
688
Ritor1
parents:
diff changeset
689 if (key == NULL)
Ritor1
parents:
diff changeset
690 {
Ritor1
parents:
diff changeset
691 *new_key = 0;
Ritor1
parents:
diff changeset
692 return 0;
Ritor1
parents:
diff changeset
693 }
Ritor1
parents:
diff changeset
694
Ritor1
parents:
diff changeset
695 while (*key && key_len < 79)
Ritor1
parents:
diff changeset
696 {
Ritor1
parents:
diff changeset
697 png_byte ch = (png_byte)(0xff & *key++);
Ritor1
parents:
diff changeset
698
Ritor1
parents:
diff changeset
699 if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))
Ritor1
parents:
diff changeset
700 *new_key++ = ch, ++key_len, space = 0;
Ritor1
parents:
diff changeset
701
Ritor1
parents:
diff changeset
702 else if (!space)
Ritor1
parents:
diff changeset
703 {
Ritor1
parents:
diff changeset
704 /* A space or an invalid character when one wasn't seen immediately
Ritor1
parents:
diff changeset
705 * before; output just a space.
Ritor1
parents:
diff changeset
706 */
Ritor1
parents:
diff changeset
707 *new_key++ = 32, ++key_len, space = 1;
Ritor1
parents:
diff changeset
708
Ritor1
parents:
diff changeset
709 /* If the character was not a space then it is invalid. */
Ritor1
parents:
diff changeset
710 if (ch != 32)
Ritor1
parents:
diff changeset
711 bad_character = ch;
Ritor1
parents:
diff changeset
712 }
Ritor1
parents:
diff changeset
713
Ritor1
parents:
diff changeset
714 else if (!bad_character)
Ritor1
parents:
diff changeset
715 bad_character = ch; /* just skip it, record the first error */
Ritor1
parents:
diff changeset
716 }
Ritor1
parents:
diff changeset
717
Ritor1
parents:
diff changeset
718 if (key_len > 0 && space) /* trailing space */
Ritor1
parents:
diff changeset
719 {
Ritor1
parents:
diff changeset
720 --key_len, --new_key;
Ritor1
parents:
diff changeset
721 if (!bad_character)
Ritor1
parents:
diff changeset
722 bad_character = 32;
Ritor1
parents:
diff changeset
723 }
Ritor1
parents:
diff changeset
724
Ritor1
parents:
diff changeset
725 /* Terminate the keyword */
Ritor1
parents:
diff changeset
726 *new_key = 0;
Ritor1
parents:
diff changeset
727
Ritor1
parents:
diff changeset
728 if (key_len == 0)
Ritor1
parents:
diff changeset
729 return 0;
Ritor1
parents:
diff changeset
730
Ritor1
parents:
diff changeset
731 /* Try to only output one warning per keyword: */
Ritor1
parents:
diff changeset
732 if (*key) /* keyword too long */
Ritor1
parents:
diff changeset
733 png_warning(png_ptr, "keyword truncated");
Ritor1
parents:
diff changeset
734
Ritor1
parents:
diff changeset
735 else if (bad_character)
Ritor1
parents:
diff changeset
736 {
Ritor1
parents:
diff changeset
737 PNG_WARNING_PARAMETERS(p)
Ritor1
parents:
diff changeset
738
Ritor1
parents:
diff changeset
739 png_warning_parameter(p, 1, orig_key);
Ritor1
parents:
diff changeset
740 png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);
Ritor1
parents:
diff changeset
741
Ritor1
parents:
diff changeset
742 png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");
Ritor1
parents:
diff changeset
743 }
Ritor1
parents:
diff changeset
744
Ritor1
parents:
diff changeset
745 return key_len;
Ritor1
parents:
diff changeset
746 }
Ritor1
parents:
diff changeset
747 #endif
Ritor1
parents:
diff changeset
748
Ritor1
parents:
diff changeset
749 /* Write the IHDR chunk, and update the png_struct with the necessary
Ritor1
parents:
diff changeset
750 * information. Note that the rest of this code depends upon this
Ritor1
parents:
diff changeset
751 * information being correct.
Ritor1
parents:
diff changeset
752 */
Ritor1
parents:
diff changeset
753 void /* PRIVATE */
Ritor1
parents:
diff changeset
754 png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height,
Ritor1
parents:
diff changeset
755 int bit_depth, int color_type, int compression_type, int filter_type,
Ritor1
parents:
diff changeset
756 int interlace_type)
Ritor1
parents:
diff changeset
757 {
Ritor1
parents:
diff changeset
758 png_byte buf[13]; /* Buffer to store the IHDR info */
Ritor1
parents:
diff changeset
759
Ritor1
parents:
diff changeset
760 png_debug(1, "in png_write_IHDR");
Ritor1
parents:
diff changeset
761
Ritor1
parents:
diff changeset
762 /* Check that we have valid input data from the application info */
Ritor1
parents:
diff changeset
763 switch (color_type)
Ritor1
parents:
diff changeset
764 {
Ritor1
parents:
diff changeset
765 case PNG_COLOR_TYPE_GRAY:
Ritor1
parents:
diff changeset
766 switch (bit_depth)
Ritor1
parents:
diff changeset
767 {
Ritor1
parents:
diff changeset
768 case 1:
Ritor1
parents:
diff changeset
769 case 2:
Ritor1
parents:
diff changeset
770 case 4:
Ritor1
parents:
diff changeset
771 case 8:
Ritor1
parents:
diff changeset
772 #ifdef PNG_WRITE_16BIT_SUPPORTED
Ritor1
parents:
diff changeset
773 case 16:
Ritor1
parents:
diff changeset
774 #endif
Ritor1
parents:
diff changeset
775 png_ptr->channels = 1; break;
Ritor1
parents:
diff changeset
776
Ritor1
parents:
diff changeset
777 default:
Ritor1
parents:
diff changeset
778 png_error(png_ptr,
Ritor1
parents:
diff changeset
779 "Invalid bit depth for grayscale image");
Ritor1
parents:
diff changeset
780 }
Ritor1
parents:
diff changeset
781 break;
Ritor1
parents:
diff changeset
782
Ritor1
parents:
diff changeset
783 case PNG_COLOR_TYPE_RGB:
Ritor1
parents:
diff changeset
784 #ifdef PNG_WRITE_16BIT_SUPPORTED
Ritor1
parents:
diff changeset
785 if (bit_depth != 8 && bit_depth != 16)
Ritor1
parents:
diff changeset
786 #else
Ritor1
parents:
diff changeset
787 if (bit_depth != 8)
Ritor1
parents:
diff changeset
788 #endif
Ritor1
parents:
diff changeset
789 png_error(png_ptr, "Invalid bit depth for RGB image");
Ritor1
parents:
diff changeset
790
Ritor1
parents:
diff changeset
791 png_ptr->channels = 3;
Ritor1
parents:
diff changeset
792 break;
Ritor1
parents:
diff changeset
793
Ritor1
parents:
diff changeset
794 case PNG_COLOR_TYPE_PALETTE:
Ritor1
parents:
diff changeset
795 switch (bit_depth)
Ritor1
parents:
diff changeset
796 {
Ritor1
parents:
diff changeset
797 case 1:
Ritor1
parents:
diff changeset
798 case 2:
Ritor1
parents:
diff changeset
799 case 4:
Ritor1
parents:
diff changeset
800 case 8:
Ritor1
parents:
diff changeset
801 png_ptr->channels = 1;
Ritor1
parents:
diff changeset
802 break;
Ritor1
parents:
diff changeset
803
Ritor1
parents:
diff changeset
804 default:
Ritor1
parents:
diff changeset
805 png_error(png_ptr, "Invalid bit depth for paletted image");
Ritor1
parents:
diff changeset
806 }
Ritor1
parents:
diff changeset
807 break;
Ritor1
parents:
diff changeset
808
Ritor1
parents:
diff changeset
809 case PNG_COLOR_TYPE_GRAY_ALPHA:
Ritor1
parents:
diff changeset
810 if (bit_depth != 8 && bit_depth != 16)
Ritor1
parents:
diff changeset
811 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
Ritor1
parents:
diff changeset
812
Ritor1
parents:
diff changeset
813 png_ptr->channels = 2;
Ritor1
parents:
diff changeset
814 break;
Ritor1
parents:
diff changeset
815
Ritor1
parents:
diff changeset
816 case PNG_COLOR_TYPE_RGB_ALPHA:
Ritor1
parents:
diff changeset
817 #ifdef PNG_WRITE_16BIT_SUPPORTED
Ritor1
parents:
diff changeset
818 if (bit_depth != 8 && bit_depth != 16)
Ritor1
parents:
diff changeset
819 #else
Ritor1
parents:
diff changeset
820 if (bit_depth != 8)
Ritor1
parents:
diff changeset
821 #endif
Ritor1
parents:
diff changeset
822 png_error(png_ptr, "Invalid bit depth for RGBA image");
Ritor1
parents:
diff changeset
823
Ritor1
parents:
diff changeset
824 png_ptr->channels = 4;
Ritor1
parents:
diff changeset
825 break;
Ritor1
parents:
diff changeset
826
Ritor1
parents:
diff changeset
827 default:
Ritor1
parents:
diff changeset
828 png_error(png_ptr, "Invalid image color type specified");
Ritor1
parents:
diff changeset
829 }
Ritor1
parents:
diff changeset
830
Ritor1
parents:
diff changeset
831 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Ritor1
parents:
diff changeset
832 {
Ritor1
parents:
diff changeset
833 png_warning(png_ptr, "Invalid compression type specified");
Ritor1
parents:
diff changeset
834 compression_type = PNG_COMPRESSION_TYPE_BASE;
Ritor1
parents:
diff changeset
835 }
Ritor1
parents:
diff changeset
836
Ritor1
parents:
diff changeset
837 /* Write filter_method 64 (intrapixel differencing) only if
Ritor1
parents:
diff changeset
838 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
Ritor1
parents:
diff changeset
839 * 2. Libpng did not write a PNG signature (this filter_method is only
Ritor1
parents:
diff changeset
840 * used in PNG datastreams that are embedded in MNG datastreams) and
Ritor1
parents:
diff changeset
841 * 3. The application called png_permit_mng_features with a mask that
Ritor1
parents:
diff changeset
842 * included PNG_FLAG_MNG_FILTER_64 and
Ritor1
parents:
diff changeset
843 * 4. The filter_method is 64 and
Ritor1
parents:
diff changeset
844 * 5. The color_type is RGB or RGBA
Ritor1
parents:
diff changeset
845 */
Ritor1
parents:
diff changeset
846 if (
Ritor1
parents:
diff changeset
847 #ifdef PNG_MNG_FEATURES_SUPPORTED
Ritor1
parents:
diff changeset
848 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Ritor1
parents:
diff changeset
849 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Ritor1
parents:
diff changeset
850 (color_type == PNG_COLOR_TYPE_RGB ||
Ritor1
parents:
diff changeset
851 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Ritor1
parents:
diff changeset
852 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
Ritor1
parents:
diff changeset
853 #endif
Ritor1
parents:
diff changeset
854 filter_type != PNG_FILTER_TYPE_BASE)
Ritor1
parents:
diff changeset
855 {
Ritor1
parents:
diff changeset
856 png_warning(png_ptr, "Invalid filter type specified");
Ritor1
parents:
diff changeset
857 filter_type = PNG_FILTER_TYPE_BASE;
Ritor1
parents:
diff changeset
858 }
Ritor1
parents:
diff changeset
859
Ritor1
parents:
diff changeset
860 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
Ritor1
parents:
diff changeset
861 if (interlace_type != PNG_INTERLACE_NONE &&
Ritor1
parents:
diff changeset
862 interlace_type != PNG_INTERLACE_ADAM7)
Ritor1
parents:
diff changeset
863 {
Ritor1
parents:
diff changeset
864 png_warning(png_ptr, "Invalid interlace type specified");
Ritor1
parents:
diff changeset
865 interlace_type = PNG_INTERLACE_ADAM7;
Ritor1
parents:
diff changeset
866 }
Ritor1
parents:
diff changeset
867 #else
Ritor1
parents:
diff changeset
868 interlace_type=PNG_INTERLACE_NONE;
Ritor1
parents:
diff changeset
869 #endif
Ritor1
parents:
diff changeset
870
Ritor1
parents:
diff changeset
871 /* Save the relevent information */
Ritor1
parents:
diff changeset
872 png_ptr->bit_depth = (png_byte)bit_depth;
Ritor1
parents:
diff changeset
873 png_ptr->color_type = (png_byte)color_type;
Ritor1
parents:
diff changeset
874 png_ptr->interlaced = (png_byte)interlace_type;
Ritor1
parents:
diff changeset
875 #ifdef PNG_MNG_FEATURES_SUPPORTED
Ritor1
parents:
diff changeset
876 png_ptr->filter_type = (png_byte)filter_type;
Ritor1
parents:
diff changeset
877 #endif
Ritor1
parents:
diff changeset
878 png_ptr->compression_type = (png_byte)compression_type;
Ritor1
parents:
diff changeset
879 png_ptr->width = width;
Ritor1
parents:
diff changeset
880 png_ptr->height = height;
Ritor1
parents:
diff changeset
881
Ritor1
parents:
diff changeset
882 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Ritor1
parents:
diff changeset
883 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Ritor1
parents:
diff changeset
884 /* Set the usr info, so any transformations can modify it */
Ritor1
parents:
diff changeset
885 png_ptr->usr_width = png_ptr->width;
Ritor1
parents:
diff changeset
886 png_ptr->usr_bit_depth = png_ptr->bit_depth;
Ritor1
parents:
diff changeset
887 png_ptr->usr_channels = png_ptr->channels;
Ritor1
parents:
diff changeset
888
Ritor1
parents:
diff changeset
889 /* Pack the header information into the buffer */
Ritor1
parents:
diff changeset
890 png_save_uint_32(buf, width);
Ritor1
parents:
diff changeset
891 png_save_uint_32(buf + 4, height);
Ritor1
parents:
diff changeset
892 buf[8] = (png_byte)bit_depth;
Ritor1
parents:
diff changeset
893 buf[9] = (png_byte)color_type;
Ritor1
parents:
diff changeset
894 buf[10] = (png_byte)compression_type;
Ritor1
parents:
diff changeset
895 buf[11] = (png_byte)filter_type;
Ritor1
parents:
diff changeset
896 buf[12] = (png_byte)interlace_type;
Ritor1
parents:
diff changeset
897
Ritor1
parents:
diff changeset
898 /* Write the chunk */
Ritor1
parents:
diff changeset
899 png_write_complete_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
Ritor1
parents:
diff changeset
900
Ritor1
parents:
diff changeset
901 if (!(png_ptr->do_filter))
Ritor1
parents:
diff changeset
902 {
Ritor1
parents:
diff changeset
903 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
Ritor1
parents:
diff changeset
904 png_ptr->bit_depth < 8)
Ritor1
parents:
diff changeset
905 png_ptr->do_filter = PNG_FILTER_NONE;
Ritor1
parents:
diff changeset
906
Ritor1
parents:
diff changeset
907 else
Ritor1
parents:
diff changeset
908 png_ptr->do_filter = PNG_ALL_FILTERS;
Ritor1
parents:
diff changeset
909 }
Ritor1
parents:
diff changeset
910
Ritor1
parents:
diff changeset
911 png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */
Ritor1
parents:
diff changeset
912 }
Ritor1
parents:
diff changeset
913
Ritor1
parents:
diff changeset
914 /* Write the palette. We are careful not to trust png_color to be in the
Ritor1
parents:
diff changeset
915 * correct order for PNG, so people can redefine it to any convenient
Ritor1
parents:
diff changeset
916 * structure.
Ritor1
parents:
diff changeset
917 */
Ritor1
parents:
diff changeset
918 void /* PRIVATE */
Ritor1
parents:
diff changeset
919 png_write_PLTE(png_structrp png_ptr, png_const_colorp palette,
Ritor1
parents:
diff changeset
920 png_uint_32 num_pal)
Ritor1
parents:
diff changeset
921 {
Ritor1
parents:
diff changeset
922 png_uint_32 i;
Ritor1
parents:
diff changeset
923 png_const_colorp pal_ptr;
Ritor1
parents:
diff changeset
924 png_byte buf[3];
Ritor1
parents:
diff changeset
925
Ritor1
parents:
diff changeset
926 png_debug(1, "in png_write_PLTE");
Ritor1
parents:
diff changeset
927
Ritor1
parents:
diff changeset
928 if ((
Ritor1
parents:
diff changeset
929 #ifdef PNG_MNG_FEATURES_SUPPORTED
Ritor1
parents:
diff changeset
930 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Ritor1
parents:
diff changeset
931 #endif
Ritor1
parents:
diff changeset
932 num_pal == 0) || num_pal > 256)
Ritor1
parents:
diff changeset
933 {
Ritor1
parents:
diff changeset
934 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Ritor1
parents:
diff changeset
935 {
Ritor1
parents:
diff changeset
936 png_error(png_ptr, "Invalid number of colors in palette");
Ritor1
parents:
diff changeset
937 }
Ritor1
parents:
diff changeset
938
Ritor1
parents:
diff changeset
939 else
Ritor1
parents:
diff changeset
940 {
Ritor1
parents:
diff changeset
941 png_warning(png_ptr, "Invalid number of colors in palette");
Ritor1
parents:
diff changeset
942 return;
Ritor1
parents:
diff changeset
943 }
Ritor1
parents:
diff changeset
944 }
Ritor1
parents:
diff changeset
945
Ritor1
parents:
diff changeset
946 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
Ritor1
parents:
diff changeset
947 {
Ritor1
parents:
diff changeset
948 png_warning(png_ptr,
Ritor1
parents:
diff changeset
949 "Ignoring request to write a PLTE chunk in grayscale PNG");
Ritor1
parents:
diff changeset
950
Ritor1
parents:
diff changeset
951 return;
Ritor1
parents:
diff changeset
952 }
Ritor1
parents:
diff changeset
953
Ritor1
parents:
diff changeset
954 png_ptr->num_palette = (png_uint_16)num_pal;
Ritor1
parents:
diff changeset
955 png_debug1(3, "num_palette = %d", png_ptr->num_palette);
Ritor1
parents:
diff changeset
956
Ritor1
parents:
diff changeset
957 png_write_chunk_header(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3));
Ritor1
parents:
diff changeset
958 #ifdef PNG_POINTER_INDEXING_SUPPORTED
Ritor1
parents:
diff changeset
959
Ritor1
parents:
diff changeset
960 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Ritor1
parents:
diff changeset
961 {
Ritor1
parents:
diff changeset
962 buf[0] = pal_ptr->red;
Ritor1
parents:
diff changeset
963 buf[1] = pal_ptr->green;
Ritor1
parents:
diff changeset
964 buf[2] = pal_ptr->blue;
Ritor1
parents:
diff changeset
965 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Ritor1
parents:
diff changeset
966 }
Ritor1
parents:
diff changeset
967
Ritor1
parents:
diff changeset
968 #else
Ritor1
parents:
diff changeset
969 /* This is a little slower but some buggy compilers need to do this
Ritor1
parents:
diff changeset
970 * instead
Ritor1
parents:
diff changeset
971 */
Ritor1
parents:
diff changeset
972 pal_ptr=palette;
Ritor1
parents:
diff changeset
973
Ritor1
parents:
diff changeset
974 for (i = 0; i < num_pal; i++)
Ritor1
parents:
diff changeset
975 {
Ritor1
parents:
diff changeset
976 buf[0] = pal_ptr[i].red;
Ritor1
parents:
diff changeset
977 buf[1] = pal_ptr[i].green;
Ritor1
parents:
diff changeset
978 buf[2] = pal_ptr[i].blue;
Ritor1
parents:
diff changeset
979 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Ritor1
parents:
diff changeset
980 }
Ritor1
parents:
diff changeset
981
Ritor1
parents:
diff changeset
982 #endif
Ritor1
parents:
diff changeset
983 png_write_chunk_end(png_ptr);
Ritor1
parents:
diff changeset
984 png_ptr->mode |= PNG_HAVE_PLTE;
Ritor1
parents:
diff changeset
985 }
Ritor1
parents:
diff changeset
986
Ritor1
parents:
diff changeset
987 /* This is similar to png_text_compress, above, except that it does not require
Ritor1
parents:
diff changeset
988 * all of the data at once and, instead of buffering the compressed result,
Ritor1
parents:
diff changeset
989 * writes it as IDAT chunks. Unlike png_text_compress it *can* png_error out
Ritor1
parents:
diff changeset
990 * because it calls the write interface. As a result it does its own error
Ritor1
parents:
diff changeset
991 * reporting and does not return an error code. In the event of error it will
Ritor1
parents:
diff changeset
992 * just call png_error. The input data length may exceed 32-bits. The 'flush'
Ritor1
parents:
diff changeset
993 * parameter is exactly the same as that to deflate, with the following
Ritor1
parents:
diff changeset
994 * meanings:
Ritor1
parents:
diff changeset
995 *
Ritor1
parents:
diff changeset
996 * Z_NO_FLUSH: normal incremental output of compressed data
Ritor1
parents:
diff changeset
997 * Z_SYNC_FLUSH: do a SYNC_FLUSH, used by png_write_flush
Ritor1
parents:
diff changeset
998 * Z_FINISH: this is the end of the input, do a Z_FINISH and clean up
Ritor1
parents:
diff changeset
999 *
Ritor1
parents:
diff changeset
1000 * The routine manages the acquire and release of the png_ptr->zstream by
Ritor1
parents:
diff changeset
1001 * checking and (at the end) clearing png_ptr->zowner, it does some sanity
Ritor1
parents:
diff changeset
1002 * checks on the 'mode' flags while doing this.
Ritor1
parents:
diff changeset
1003 */
Ritor1
parents:
diff changeset
1004 void /* PRIVATE */
Ritor1
parents:
diff changeset
1005 png_compress_IDAT(png_structrp png_ptr, png_const_bytep input,
Ritor1
parents:
diff changeset
1006 png_alloc_size_t input_len, int flush)
Ritor1
parents:
diff changeset
1007 {
Ritor1
parents:
diff changeset
1008 if (png_ptr->zowner != png_IDAT)
Ritor1
parents:
diff changeset
1009 {
Ritor1
parents:
diff changeset
1010 /* First time. Ensure we have a temporary buffer for compression and
Ritor1
parents:
diff changeset
1011 * trim the buffer list if it has more than one entry to free memory.
Ritor1
parents:
diff changeset
1012 * If 'WRITE_COMPRESSED_TEXT' is not set the list will never have been
Ritor1
parents:
diff changeset
1013 * created at this point, but the check here is quick and safe.
Ritor1
parents:
diff changeset
1014 */
Ritor1
parents:
diff changeset
1015 if (png_ptr->zbuffer_list == NULL)
Ritor1
parents:
diff changeset
1016 {
Ritor1
parents:
diff changeset
1017 png_ptr->zbuffer_list = png_voidcast(png_compression_bufferp,
Ritor1
parents:
diff changeset
1018 png_malloc(png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr)));
Ritor1
parents:
diff changeset
1019 png_ptr->zbuffer_list->next = NULL;
Ritor1
parents:
diff changeset
1020 }
Ritor1
parents:
diff changeset
1021
Ritor1
parents:
diff changeset
1022 else
Ritor1
parents:
diff changeset
1023 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list->next);
Ritor1
parents:
diff changeset
1024
Ritor1
parents:
diff changeset
1025 /* It is a terminal error if we can't claim the zstream. */
Ritor1
parents:
diff changeset
1026 if (png_deflate_claim(png_ptr, png_IDAT, png_image_size(png_ptr)) != Z_OK)
Ritor1
parents:
diff changeset
1027 png_error(png_ptr, png_ptr->zstream.msg);
Ritor1
parents:
diff changeset
1028
Ritor1
parents:
diff changeset
1029 /* The output state is maintained in png_ptr->zstream, so it must be
Ritor1
parents:
diff changeset
1030 * initialized here after the claim.
Ritor1
parents:
diff changeset
1031 */
Ritor1
parents:
diff changeset
1032 png_ptr->zstream.next_out = png_ptr->zbuffer_list->output;
Ritor1
parents:
diff changeset
1033 png_ptr->zstream.avail_out = png_ptr->zbuffer_size;
Ritor1
parents:
diff changeset
1034 }
Ritor1
parents:
diff changeset
1035
Ritor1
parents:
diff changeset
1036 /* Now loop reading and writing until all the input is consumed or an error
Ritor1
parents:
diff changeset
1037 * terminates the operation. The _out values are maintained across calls to
Ritor1
parents:
diff changeset
1038 * this function, but the input must be reset each time.
Ritor1
parents:
diff changeset
1039 */
Ritor1
parents:
diff changeset
1040 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
Ritor1
parents:
diff changeset
1041 png_ptr->zstream.avail_in = 0; /* set below */
Ritor1
parents:
diff changeset
1042 for (;;)
Ritor1
parents:
diff changeset
1043 {
Ritor1
parents:
diff changeset
1044 int ret;
Ritor1
parents:
diff changeset
1045
Ritor1
parents:
diff changeset
1046 /* INPUT: from the row data */
Ritor1
parents:
diff changeset
1047 uInt avail = ZLIB_IO_MAX;
Ritor1
parents:
diff changeset
1048
Ritor1
parents:
diff changeset
1049 if (avail > input_len)
Ritor1
parents:
diff changeset
1050 avail = (uInt)input_len; /* safe because of the check */
Ritor1
parents:
diff changeset
1051
Ritor1
parents:
diff changeset
1052 png_ptr->zstream.avail_in = avail;
Ritor1
parents:
diff changeset
1053 input_len -= avail;
Ritor1
parents:
diff changeset
1054
Ritor1
parents:
diff changeset
1055 ret = deflate(&png_ptr->zstream, input_len > 0 ? Z_NO_FLUSH : flush);
Ritor1
parents:
diff changeset
1056
Ritor1
parents:
diff changeset
1057 /* Include as-yet unconsumed input */
Ritor1
parents:
diff changeset
1058 input_len += png_ptr->zstream.avail_in;
Ritor1
parents:
diff changeset
1059 png_ptr->zstream.avail_in = 0;
Ritor1
parents:
diff changeset
1060
Ritor1
parents:
diff changeset
1061 /* OUTPUT: write complete IDAT chunks when avail_out drops to zero, note
Ritor1
parents:
diff changeset
1062 * that these two zstream fields are preserved across the calls, therefore
Ritor1
parents:
diff changeset
1063 * there is no need to set these up on entry to the loop.
Ritor1
parents:
diff changeset
1064 */
Ritor1
parents:
diff changeset
1065 if (png_ptr->zstream.avail_out == 0)
Ritor1
parents:
diff changeset
1066 {
Ritor1
parents:
diff changeset
1067 png_bytep data = png_ptr->zbuffer_list->output;
Ritor1
parents:
diff changeset
1068 uInt size = png_ptr->zbuffer_size;
Ritor1
parents:
diff changeset
1069
Ritor1
parents:
diff changeset
1070 /* Write an IDAT containing the data then reset the buffer. The
Ritor1
parents:
diff changeset
1071 * first IDAT may need deflate header optimization.
Ritor1
parents:
diff changeset
1072 */
Ritor1
parents:
diff changeset
1073 # ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
Ritor1
parents:
diff changeset
1074 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
Ritor1
parents:
diff changeset
1075 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
Ritor1
parents:
diff changeset
1076 optimize_cmf(data, png_image_size(png_ptr));
Ritor1
parents:
diff changeset
1077 # endif
Ritor1
parents:
diff changeset
1078
Ritor1
parents:
diff changeset
1079 png_write_complete_chunk(png_ptr, png_IDAT, data, size);
Ritor1
parents:
diff changeset
1080 png_ptr->mode |= PNG_HAVE_IDAT;
Ritor1
parents:
diff changeset
1081
Ritor1
parents:
diff changeset
1082 png_ptr->zstream.next_out = data;
Ritor1
parents:
diff changeset
1083 png_ptr->zstream.avail_out = size;
Ritor1
parents:
diff changeset
1084
Ritor1
parents:
diff changeset
1085 /* For SYNC_FLUSH or FINISH it is essential to keep calling zlib with
Ritor1
parents:
diff changeset
1086 * the same flush parameter until it has finished output, for NO_FLUSH
Ritor1
parents:
diff changeset
1087 * it doesn't matter.
Ritor1
parents:
diff changeset
1088 */
Ritor1
parents:
diff changeset
1089 if (ret == Z_OK && flush != Z_NO_FLUSH)
Ritor1
parents:
diff changeset
1090 continue;
Ritor1
parents:
diff changeset
1091 }
Ritor1
parents:
diff changeset
1092
Ritor1
parents:
diff changeset
1093 /* The order of these checks doesn't matter much; it just effect which
Ritor1
parents:
diff changeset
1094 * possible error might be detected if multiple things go wrong at once.
Ritor1
parents:
diff changeset
1095 */
Ritor1
parents:
diff changeset
1096 if (ret == Z_OK) /* most likely return code! */
Ritor1
parents:
diff changeset
1097 {
Ritor1
parents:
diff changeset
1098 /* If all the input has been consumed then just return. If Z_FINISH
Ritor1
parents:
diff changeset
1099 * was used as the flush parameter something has gone wrong if we get
Ritor1
parents:
diff changeset
1100 * here.
Ritor1
parents:
diff changeset
1101 */
Ritor1
parents:
diff changeset
1102 if (input_len == 0)
Ritor1
parents:
diff changeset
1103 {
Ritor1
parents:
diff changeset
1104 if (flush == Z_FINISH)
Ritor1
parents:
diff changeset
1105 png_error(png_ptr, "Z_OK on Z_FINISH with output space");
Ritor1
parents:
diff changeset
1106
Ritor1
parents:
diff changeset
1107 return;
Ritor1
parents:
diff changeset
1108 }
Ritor1
parents:
diff changeset
1109 }
Ritor1
parents:
diff changeset
1110
Ritor1
parents:
diff changeset
1111 else if (ret == Z_STREAM_END && flush == Z_FINISH)
Ritor1
parents:
diff changeset
1112 {
Ritor1
parents:
diff changeset
1113 /* This is the end of the IDAT data; any pending output must be
Ritor1
parents:
diff changeset
1114 * flushed. For small PNG files we may still be at the beginning.
Ritor1
parents:
diff changeset
1115 */
Ritor1
parents:
diff changeset
1116 png_bytep data = png_ptr->zbuffer_list->output;
Ritor1
parents:
diff changeset
1117 uInt size = png_ptr->zbuffer_size - png_ptr->zstream.avail_out;
Ritor1
parents:
diff changeset
1118
Ritor1
parents:
diff changeset
1119 # ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
Ritor1
parents:
diff changeset
1120 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
Ritor1
parents:
diff changeset
1121 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
Ritor1
parents:
diff changeset
1122 optimize_cmf(data, png_image_size(png_ptr));
Ritor1
parents:
diff changeset
1123 # endif
Ritor1
parents:
diff changeset
1124
Ritor1
parents:
diff changeset
1125 png_write_complete_chunk(png_ptr, png_IDAT, data, size);
Ritor1
parents:
diff changeset
1126 png_ptr->zstream.avail_out = 0;
Ritor1
parents:
diff changeset
1127 png_ptr->zstream.next_out = NULL;
Ritor1
parents:
diff changeset
1128 png_ptr->mode |= PNG_HAVE_IDAT | PNG_AFTER_IDAT;
Ritor1
parents:
diff changeset
1129
Ritor1
parents:
diff changeset
1130 png_ptr->zowner = 0; /* Release the stream */
Ritor1
parents:
diff changeset
1131 return;
Ritor1
parents:
diff changeset
1132 }
Ritor1
parents:
diff changeset
1133
Ritor1
parents:
diff changeset
1134 else
Ritor1
parents:
diff changeset
1135 {
Ritor1
parents:
diff changeset
1136 /* This is an error condition. */
Ritor1
parents:
diff changeset
1137 png_zstream_error(png_ptr, ret);
Ritor1
parents:
diff changeset
1138 png_error(png_ptr, png_ptr->zstream.msg);
Ritor1
parents:
diff changeset
1139 }
Ritor1
parents:
diff changeset
1140 }
Ritor1
parents:
diff changeset
1141 }
Ritor1
parents:
diff changeset
1142
Ritor1
parents:
diff changeset
1143 /* Write an IEND chunk */
Ritor1
parents:
diff changeset
1144 void /* PRIVATE */
Ritor1
parents:
diff changeset
1145 png_write_IEND(png_structrp png_ptr)
Ritor1
parents:
diff changeset
1146 {
Ritor1
parents:
diff changeset
1147 png_debug(1, "in png_write_IEND");
Ritor1
parents:
diff changeset
1148
Ritor1
parents:
diff changeset
1149 png_write_complete_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
Ritor1
parents:
diff changeset
1150 png_ptr->mode |= PNG_HAVE_IEND;
Ritor1
parents:
diff changeset
1151 }
Ritor1
parents:
diff changeset
1152
Ritor1
parents:
diff changeset
1153 #ifdef PNG_WRITE_gAMA_SUPPORTED
Ritor1
parents:
diff changeset
1154 /* Write a gAMA chunk */
Ritor1
parents:
diff changeset
1155 void /* PRIVATE */
Ritor1
parents:
diff changeset
1156 png_write_gAMA_fixed(png_structrp png_ptr, png_fixed_point file_gamma)
Ritor1
parents:
diff changeset
1157 {
Ritor1
parents:
diff changeset
1158 png_byte buf[4];
Ritor1
parents:
diff changeset
1159
Ritor1
parents:
diff changeset
1160 png_debug(1, "in png_write_gAMA");
Ritor1
parents:
diff changeset
1161
Ritor1
parents:
diff changeset
1162 /* file_gamma is saved in 1/100,000ths */
Ritor1
parents:
diff changeset
1163 png_save_uint_32(buf, (png_uint_32)file_gamma);
Ritor1
parents:
diff changeset
1164 png_write_complete_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
Ritor1
parents:
diff changeset
1165 }
Ritor1
parents:
diff changeset
1166 #endif
Ritor1
parents:
diff changeset
1167
Ritor1
parents:
diff changeset
1168 #ifdef PNG_WRITE_sRGB_SUPPORTED
Ritor1
parents:
diff changeset
1169 /* Write a sRGB chunk */
Ritor1
parents:
diff changeset
1170 void /* PRIVATE */
Ritor1
parents:
diff changeset
1171 png_write_sRGB(png_structrp png_ptr, int srgb_intent)
Ritor1
parents:
diff changeset
1172 {
Ritor1
parents:
diff changeset
1173 png_byte buf[1];
Ritor1
parents:
diff changeset
1174
Ritor1
parents:
diff changeset
1175 png_debug(1, "in png_write_sRGB");
Ritor1
parents:
diff changeset
1176
Ritor1
parents:
diff changeset
1177 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
Ritor1
parents:
diff changeset
1178 png_warning(png_ptr,
Ritor1
parents:
diff changeset
1179 "Invalid sRGB rendering intent specified");
Ritor1
parents:
diff changeset
1180
Ritor1
parents:
diff changeset
1181 buf[0]=(png_byte)srgb_intent;
Ritor1
parents:
diff changeset
1182 png_write_complete_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
Ritor1
parents:
diff changeset
1183 }
Ritor1
parents:
diff changeset
1184 #endif
Ritor1
parents:
diff changeset
1185
Ritor1
parents:
diff changeset
1186 #ifdef PNG_WRITE_iCCP_SUPPORTED
Ritor1
parents:
diff changeset
1187 /* Write an iCCP chunk */
Ritor1
parents:
diff changeset
1188 void /* PRIVATE */
Ritor1
parents:
diff changeset
1189 png_write_iCCP(png_structrp png_ptr, png_const_charp name,
Ritor1
parents:
diff changeset
1190 png_const_bytep profile)
Ritor1
parents:
diff changeset
1191 {
Ritor1
parents:
diff changeset
1192 png_uint_32 name_len;
Ritor1
parents:
diff changeset
1193 png_uint_32 profile_len;
Ritor1
parents:
diff changeset
1194 png_byte new_name[81]; /* 1 byte for the compression byte */
Ritor1
parents:
diff changeset
1195 compression_state comp;
Ritor1
parents:
diff changeset
1196
Ritor1
parents:
diff changeset
1197 png_debug(1, "in png_write_iCCP");
Ritor1
parents:
diff changeset
1198
Ritor1
parents:
diff changeset
1199 /* These are all internal problems: the profile should have been checked
Ritor1
parents:
diff changeset
1200 * before when it was stored.
Ritor1
parents:
diff changeset
1201 */
Ritor1
parents:
diff changeset
1202 if (profile == NULL)
Ritor1
parents:
diff changeset
1203 png_error(png_ptr, "No profile for iCCP chunk"); /* internal error */
Ritor1
parents:
diff changeset
1204
Ritor1
parents:
diff changeset
1205 profile_len = png_get_uint_32(profile);
Ritor1
parents:
diff changeset
1206
Ritor1
parents:
diff changeset
1207 if (profile_len < 132)
Ritor1
parents:
diff changeset
1208 png_error(png_ptr, "ICC profile too short");
Ritor1
parents:
diff changeset
1209
Ritor1
parents:
diff changeset
1210 if (profile_len & 0x03)
Ritor1
parents:
diff changeset
1211 png_error(png_ptr, "ICC profile length invalid (not a multiple of 4)");
Ritor1
parents:
diff changeset
1212
Ritor1
parents:
diff changeset
1213 {
Ritor1
parents:
diff changeset
1214 png_uint_32 embedded_profile_len = png_get_uint_32(profile);
Ritor1
parents:
diff changeset
1215
Ritor1
parents:
diff changeset
1216 if (profile_len != embedded_profile_len)
Ritor1
parents:
diff changeset
1217 png_error(png_ptr, "Profile length does not match profile");
Ritor1
parents:
diff changeset
1218 }
Ritor1
parents:
diff changeset
1219
Ritor1
parents:
diff changeset
1220 name_len = png_check_keyword(png_ptr, name, new_name);
Ritor1
parents:
diff changeset
1221
Ritor1
parents:
diff changeset
1222 if (name_len == 0)
Ritor1
parents:
diff changeset
1223 png_error(png_ptr, "iCCP: invalid keyword");
Ritor1
parents:
diff changeset
1224
Ritor1
parents:
diff changeset
1225 new_name[++name_len] = PNG_COMPRESSION_TYPE_BASE;
Ritor1
parents:
diff changeset
1226
Ritor1
parents:
diff changeset
1227 /* Make sure we include the NULL after the name and the compression type */
Ritor1
parents:
diff changeset
1228 ++name_len;
Ritor1
parents:
diff changeset
1229
Ritor1
parents:
diff changeset
1230 png_text_compress_init(&comp, profile, profile_len);
Ritor1
parents:
diff changeset
1231
Ritor1
parents:
diff changeset
1232 /* Allow for keyword terminator and compression byte */
Ritor1
parents:
diff changeset
1233 if (png_text_compress(png_ptr, png_iCCP, &comp, name_len) != Z_OK)
Ritor1
parents:
diff changeset
1234 png_error(png_ptr, png_ptr->zstream.msg);
Ritor1
parents:
diff changeset
1235
Ritor1
parents:
diff changeset
1236 png_write_chunk_header(png_ptr, png_iCCP, name_len + comp.output_len);
Ritor1
parents:
diff changeset
1237
Ritor1
parents:
diff changeset
1238 png_write_chunk_data(png_ptr, new_name, name_len);
Ritor1
parents:
diff changeset
1239
Ritor1
parents:
diff changeset
1240 png_write_compressed_data_out(png_ptr, &comp);
Ritor1
parents:
diff changeset
1241
Ritor1
parents:
diff changeset
1242 png_write_chunk_end(png_ptr);
Ritor1
parents:
diff changeset
1243 }
Ritor1
parents:
diff changeset
1244 #endif
Ritor1
parents:
diff changeset
1245
Ritor1
parents:
diff changeset
1246 #ifdef PNG_WRITE_sPLT_SUPPORTED
Ritor1
parents:
diff changeset
1247 /* Write a sPLT chunk */
Ritor1
parents:
diff changeset
1248 void /* PRIVATE */
Ritor1
parents:
diff changeset
1249 png_write_sPLT(png_structrp png_ptr, png_const_sPLT_tp spalette)
Ritor1
parents:
diff changeset
1250 {
Ritor1
parents:
diff changeset
1251 png_uint_32 name_len;
Ritor1
parents:
diff changeset
1252 png_byte new_name[80];
Ritor1
parents:
diff changeset
1253 png_byte entrybuf[10];
Ritor1
parents:
diff changeset
1254 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
Ritor1
parents:
diff changeset
1255 png_size_t palette_size = entry_size * spalette->nentries;
Ritor1
parents:
diff changeset
1256 png_sPLT_entryp ep;
Ritor1
parents:
diff changeset
1257 #ifndef PNG_POINTER_INDEXING_SUPPORTED
Ritor1
parents:
diff changeset
1258 int i;
Ritor1
parents:
diff changeset
1259 #endif
Ritor1
parents:
diff changeset
1260
Ritor1
parents:
diff changeset
1261 png_debug(1, "in png_write_sPLT");
Ritor1
parents:
diff changeset
1262
Ritor1
parents:
diff changeset
1263 name_len = png_check_keyword(png_ptr, spalette->name, new_name);
Ritor1
parents:
diff changeset
1264
Ritor1
parents:
diff changeset
1265 if (name_len == 0)
Ritor1
parents:
diff changeset
1266 png_error(png_ptr, "sPLT: invalid keyword");
Ritor1
parents:
diff changeset
1267
Ritor1
parents:
diff changeset
1268 /* Make sure we include the NULL after the name */
Ritor1
parents:
diff changeset
1269 png_write_chunk_header(png_ptr, png_sPLT,
Ritor1
parents:
diff changeset
1270 (png_uint_32)(name_len + 2 + palette_size));
Ritor1
parents:
diff changeset
1271
Ritor1
parents:
diff changeset
1272 png_write_chunk_data(png_ptr, (png_bytep)new_name,
Ritor1
parents:
diff changeset
1273 (png_size_t)(name_len + 1));
Ritor1
parents:
diff changeset
1274
Ritor1
parents:
diff changeset
1275 png_write_chunk_data(png_ptr, &spalette->depth, (png_size_t)1);
Ritor1
parents:
diff changeset
1276
Ritor1
parents:
diff changeset
1277 /* Loop through each palette entry, writing appropriately */
Ritor1
parents:
diff changeset
1278 #ifdef PNG_POINTER_INDEXING_SUPPORTED
Ritor1
parents:
diff changeset
1279 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
Ritor1
parents:
diff changeset
1280 {
Ritor1
parents:
diff changeset
1281 if (spalette->depth == 8)
Ritor1
parents:
diff changeset
1282 {
Ritor1
parents:
diff changeset
1283 entrybuf[0] = (png_byte)ep->red;
Ritor1
parents:
diff changeset
1284 entrybuf[1] = (png_byte)ep->green;
Ritor1
parents:
diff changeset
1285 entrybuf[2] = (png_byte)ep->blue;
Ritor1
parents:
diff changeset
1286 entrybuf[3] = (png_byte)ep->alpha;
Ritor1
parents:
diff changeset
1287 png_save_uint_16(entrybuf + 4, ep->frequency);
Ritor1
parents:
diff changeset
1288 }
Ritor1
parents:
diff changeset
1289
Ritor1
parents:
diff changeset
1290 else
Ritor1
parents:
diff changeset
1291 {
Ritor1
parents:
diff changeset
1292 png_save_uint_16(entrybuf + 0, ep->red);
Ritor1
parents:
diff changeset
1293 png_save_uint_16(entrybuf + 2, ep->green);
Ritor1
parents:
diff changeset
1294 png_save_uint_16(entrybuf + 4, ep->blue);
Ritor1
parents:
diff changeset
1295 png_save_uint_16(entrybuf + 6, ep->alpha);
Ritor1
parents:
diff changeset
1296 png_save_uint_16(entrybuf + 8, ep->frequency);
Ritor1
parents:
diff changeset
1297 }
Ritor1
parents:
diff changeset
1298
Ritor1
parents:
diff changeset
1299 png_write_chunk_data(png_ptr, entrybuf, entry_size);
Ritor1
parents:
diff changeset
1300 }
Ritor1
parents:
diff changeset
1301 #else
Ritor1
parents:
diff changeset
1302 ep=spalette->entries;
Ritor1
parents:
diff changeset
1303 for (i = 0; i>spalette->nentries; i++)
Ritor1
parents:
diff changeset
1304 {
Ritor1
parents:
diff changeset
1305 if (spalette->depth == 8)
Ritor1
parents:
diff changeset
1306 {
Ritor1
parents:
diff changeset
1307 entrybuf[0] = (png_byte)ep[i].red;
Ritor1
parents:
diff changeset
1308 entrybuf[1] = (png_byte)ep[i].green;
Ritor1
parents:
diff changeset
1309 entrybuf[2] = (png_byte)ep[i].blue;
Ritor1
parents:
diff changeset
1310 entrybuf[3] = (png_byte)ep[i].alpha;
Ritor1
parents:
diff changeset
1311 png_save_uint_16(entrybuf + 4, ep[i].frequency);
Ritor1
parents:
diff changeset
1312 }
Ritor1
parents:
diff changeset
1313
Ritor1
parents:
diff changeset
1314 else
Ritor1
parents:
diff changeset
1315 {
Ritor1
parents:
diff changeset
1316 png_save_uint_16(entrybuf + 0, ep[i].red);
Ritor1
parents:
diff changeset
1317 png_save_uint_16(entrybuf + 2, ep[i].green);
Ritor1
parents:
diff changeset
1318 png_save_uint_16(entrybuf + 4, ep[i].blue);
Ritor1
parents:
diff changeset
1319 png_save_uint_16(entrybuf + 6, ep[i].alpha);
Ritor1
parents:
diff changeset
1320 png_save_uint_16(entrybuf + 8, ep[i].frequency);
Ritor1
parents:
diff changeset
1321 }
Ritor1
parents:
diff changeset
1322
Ritor1
parents:
diff changeset
1323 png_write_chunk_data(png_ptr, entrybuf, entry_size);
Ritor1
parents:
diff changeset
1324 }
Ritor1
parents:
diff changeset
1325 #endif
Ritor1
parents:
diff changeset
1326
Ritor1
parents:
diff changeset
1327 png_write_chunk_end(png_ptr);
Ritor1
parents:
diff changeset
1328 }
Ritor1
parents:
diff changeset
1329 #endif
Ritor1
parents:
diff changeset
1330
Ritor1
parents:
diff changeset
1331 #ifdef PNG_WRITE_sBIT_SUPPORTED
Ritor1
parents:
diff changeset
1332 /* Write the sBIT chunk */
Ritor1
parents:
diff changeset
1333 void /* PRIVATE */
Ritor1
parents:
diff changeset
1334 png_write_sBIT(png_structrp png_ptr, png_const_color_8p sbit, int color_type)
Ritor1
parents:
diff changeset
1335 {
Ritor1
parents:
diff changeset
1336 png_byte buf[4];
Ritor1
parents:
diff changeset
1337 png_size_t size;
Ritor1
parents:
diff changeset
1338
Ritor1
parents:
diff changeset
1339 png_debug(1, "in png_write_sBIT");
Ritor1
parents:
diff changeset
1340
Ritor1
parents:
diff changeset
1341 /* Make sure we don't depend upon the order of PNG_COLOR_8 */
Ritor1
parents:
diff changeset
1342 if (color_type & PNG_COLOR_MASK_COLOR)
Ritor1
parents:
diff changeset
1343 {
Ritor1
parents:
diff changeset
1344 png_byte maxbits;
Ritor1
parents:
diff changeset
1345
Ritor1
parents:
diff changeset
1346 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
Ritor1
parents:
diff changeset
1347 png_ptr->usr_bit_depth);
Ritor1
parents:
diff changeset
1348
Ritor1
parents:
diff changeset
1349 if (sbit->red == 0 || sbit->red > maxbits ||
Ritor1
parents:
diff changeset
1350 sbit->green == 0 || sbit->green > maxbits ||
Ritor1
parents:
diff changeset
1351 sbit->blue == 0 || sbit->blue > maxbits)
Ritor1
parents:
diff changeset
1352 {
Ritor1
parents:
diff changeset
1353 png_warning(png_ptr, "Invalid sBIT depth specified");
Ritor1
parents:
diff changeset
1354 return;
Ritor1
parents:
diff changeset
1355 }
Ritor1
parents:
diff changeset
1356
Ritor1
parents:
diff changeset
1357 buf[0] = sbit->red;
Ritor1
parents:
diff changeset
1358 buf[1] = sbit->green;
Ritor1
parents:
diff changeset
1359 buf[2] = sbit->blue;
Ritor1
parents:
diff changeset
1360 size = 3;
Ritor1
parents:
diff changeset
1361 }
Ritor1
parents:
diff changeset
1362
Ritor1
parents:
diff changeset
1363 else
Ritor1
parents:
diff changeset
1364 {
Ritor1
parents:
diff changeset
1365 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
Ritor1
parents:
diff changeset
1366 {
Ritor1
parents:
diff changeset
1367 png_warning(png_ptr, "Invalid sBIT depth specified");
Ritor1
parents:
diff changeset
1368 return;
Ritor1
parents:
diff changeset
1369 }
Ritor1
parents:
diff changeset
1370
Ritor1
parents:
diff changeset
1371 buf[0] = sbit->gray;
Ritor1
parents:
diff changeset
1372 size = 1;
Ritor1
parents:
diff changeset
1373 }
Ritor1
parents:
diff changeset
1374
Ritor1
parents:
diff changeset
1375 if (color_type & PNG_COLOR_MASK_ALPHA)
Ritor1
parents:
diff changeset
1376 {
Ritor1
parents:
diff changeset
1377 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
Ritor1
parents:
diff changeset
1378 {
Ritor1
parents:
diff changeset
1379 png_warning(png_ptr, "Invalid sBIT depth specified");
Ritor1
parents:
diff changeset
1380 return;
Ritor1
parents:
diff changeset
1381 }
Ritor1
parents:
diff changeset
1382
Ritor1
parents:
diff changeset
1383 buf[size++] = sbit->alpha;
Ritor1
parents:
diff changeset
1384 }
Ritor1
parents:
diff changeset
1385
Ritor1
parents:
diff changeset
1386 png_write_complete_chunk(png_ptr, png_sBIT, buf, size);
Ritor1
parents:
diff changeset
1387 }
Ritor1
parents:
diff changeset
1388 #endif
Ritor1
parents:
diff changeset
1389
Ritor1
parents:
diff changeset
1390 #ifdef PNG_WRITE_cHRM_SUPPORTED
Ritor1
parents:
diff changeset
1391 /* Write the cHRM chunk */
Ritor1
parents:
diff changeset
1392 void /* PRIVATE */
Ritor1
parents:
diff changeset
1393 png_write_cHRM_fixed(png_structrp png_ptr, const png_xy *xy)
Ritor1
parents:
diff changeset
1394 {
Ritor1
parents:
diff changeset
1395 png_byte buf[32];
Ritor1
parents:
diff changeset
1396
Ritor1
parents:
diff changeset
1397 png_debug(1, "in png_write_cHRM");
Ritor1
parents:
diff changeset
1398
Ritor1
parents:
diff changeset
1399 /* Each value is saved in 1/100,000ths */
Ritor1
parents:
diff changeset
1400 png_save_int_32(buf, xy->whitex);
Ritor1
parents:
diff changeset
1401 png_save_int_32(buf + 4, xy->whitey);
Ritor1
parents:
diff changeset
1402
Ritor1
parents:
diff changeset
1403 png_save_int_32(buf + 8, xy->redx);
Ritor1
parents:
diff changeset
1404 png_save_int_32(buf + 12, xy->redy);
Ritor1
parents:
diff changeset
1405
Ritor1
parents:
diff changeset
1406 png_save_int_32(buf + 16, xy->greenx);
Ritor1
parents:
diff changeset
1407 png_save_int_32(buf + 20, xy->greeny);
Ritor1
parents:
diff changeset
1408
Ritor1
parents:
diff changeset
1409 png_save_int_32(buf + 24, xy->bluex);
Ritor1
parents:
diff changeset
1410 png_save_int_32(buf + 28, xy->bluey);
Ritor1
parents:
diff changeset
1411
Ritor1
parents:
diff changeset
1412 png_write_complete_chunk(png_ptr, png_cHRM, buf, 32);
Ritor1
parents:
diff changeset
1413 }
Ritor1
parents:
diff changeset
1414 #endif
Ritor1
parents:
diff changeset
1415
Ritor1
parents:
diff changeset
1416 #ifdef PNG_WRITE_tRNS_SUPPORTED
Ritor1
parents:
diff changeset
1417 /* Write the tRNS chunk */
Ritor1
parents:
diff changeset
1418 void /* PRIVATE */
Ritor1
parents:
diff changeset
1419 png_write_tRNS(png_structrp png_ptr, png_const_bytep trans_alpha,
Ritor1
parents:
diff changeset
1420 png_const_color_16p tran, int num_trans, int color_type)
Ritor1
parents:
diff changeset
1421 {
Ritor1
parents:
diff changeset
1422 png_byte buf[6];
Ritor1
parents:
diff changeset
1423
Ritor1
parents:
diff changeset
1424 png_debug(1, "in png_write_tRNS");
Ritor1
parents:
diff changeset
1425
Ritor1
parents:
diff changeset
1426 if (color_type == PNG_COLOR_TYPE_PALETTE)
Ritor1
parents:
diff changeset
1427 {
Ritor1
parents:
diff changeset
1428 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Ritor1
parents:
diff changeset
1429 {
Ritor1
parents:
diff changeset
1430 png_app_warning(png_ptr,
Ritor1
parents:
diff changeset
1431 "Invalid number of transparent colors specified");
Ritor1
parents:
diff changeset
1432 return;
Ritor1
parents:
diff changeset
1433 }
Ritor1
parents:
diff changeset
1434
Ritor1
parents:
diff changeset
1435 /* Write the chunk out as it is */
Ritor1
parents:
diff changeset
1436 png_write_complete_chunk(png_ptr, png_tRNS, trans_alpha,
Ritor1
parents:
diff changeset
1437 (png_size_t)num_trans);
Ritor1
parents:
diff changeset
1438 }
Ritor1
parents:
diff changeset
1439
Ritor1
parents:
diff changeset
1440 else if (color_type == PNG_COLOR_TYPE_GRAY)
Ritor1
parents:
diff changeset
1441 {
Ritor1
parents:
diff changeset
1442 /* One 16 bit value */
Ritor1
parents:
diff changeset
1443 if (tran->gray >= (1 << png_ptr->bit_depth))
Ritor1
parents:
diff changeset
1444 {
Ritor1
parents:
diff changeset
1445 png_app_warning(png_ptr,
Ritor1
parents:
diff changeset
1446 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
Ritor1
parents:
diff changeset
1447
Ritor1
parents:
diff changeset
1448 return;
Ritor1
parents:
diff changeset
1449 }
Ritor1
parents:
diff changeset
1450
Ritor1
parents:
diff changeset
1451 png_save_uint_16(buf, tran->gray);
Ritor1
parents:
diff changeset
1452 png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
Ritor1
parents:
diff changeset
1453 }
Ritor1
parents:
diff changeset
1454
Ritor1
parents:
diff changeset
1455 else if (color_type == PNG_COLOR_TYPE_RGB)
Ritor1
parents:
diff changeset
1456 {
Ritor1
parents:
diff changeset
1457 /* Three 16 bit values */
Ritor1
parents:
diff changeset
1458 png_save_uint_16(buf, tran->red);
Ritor1
parents:
diff changeset
1459 png_save_uint_16(buf + 2, tran->green);
Ritor1
parents:
diff changeset
1460 png_save_uint_16(buf + 4, tran->blue);
Ritor1
parents:
diff changeset
1461 #ifdef PNG_WRITE_16BIT_SUPPORTED
Ritor1
parents:
diff changeset
1462 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Ritor1
parents:
diff changeset
1463 #else
Ritor1
parents:
diff changeset
1464 if (buf[0] | buf[2] | buf[4])
Ritor1
parents:
diff changeset
1465 #endif
Ritor1
parents:
diff changeset
1466 {
Ritor1
parents:
diff changeset
1467 png_app_warning(png_ptr,
Ritor1
parents:
diff changeset
1468 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
Ritor1
parents:
diff changeset
1469 return;
Ritor1
parents:
diff changeset
1470 }
Ritor1
parents:
diff changeset
1471
Ritor1
parents:
diff changeset
1472 png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
Ritor1
parents:
diff changeset
1473 }
Ritor1
parents:
diff changeset
1474
Ritor1
parents:
diff changeset
1475 else
Ritor1
parents:
diff changeset
1476 {
Ritor1
parents:
diff changeset
1477 png_app_warning(png_ptr, "Can't write tRNS with an alpha channel");
Ritor1
parents:
diff changeset
1478 }
Ritor1
parents:
diff changeset
1479 }
Ritor1
parents:
diff changeset
1480 #endif
Ritor1
parents:
diff changeset
1481
Ritor1
parents:
diff changeset
1482 #ifdef PNG_WRITE_bKGD_SUPPORTED
Ritor1
parents:
diff changeset
1483 /* Write the background chunk */
Ritor1
parents:
diff changeset
1484 void /* PRIVATE */
Ritor1
parents:
diff changeset
1485 png_write_bKGD(png_structrp png_ptr, png_const_color_16p back, int color_type)
Ritor1
parents:
diff changeset
1486 {
Ritor1
parents:
diff changeset
1487 png_byte buf[6];
Ritor1
parents:
diff changeset
1488
Ritor1
parents:
diff changeset
1489 png_debug(1, "in png_write_bKGD");
Ritor1
parents:
diff changeset
1490
Ritor1
parents:
diff changeset
1491 if (color_type == PNG_COLOR_TYPE_PALETTE)
Ritor1
parents:
diff changeset
1492 {
Ritor1
parents:
diff changeset
1493 if (
Ritor1
parents:
diff changeset
1494 #ifdef PNG_MNG_FEATURES_SUPPORTED
Ritor1
parents:
diff changeset
1495 (png_ptr->num_palette ||
Ritor1
parents:
diff changeset
1496 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Ritor1
parents:
diff changeset
1497 #endif
Ritor1
parents:
diff changeset
1498 back->index >= png_ptr->num_palette)
Ritor1
parents:
diff changeset
1499 {
Ritor1
parents:
diff changeset
1500 png_warning(png_ptr, "Invalid background palette index");
Ritor1
parents:
diff changeset
1501 return;
Ritor1
parents:
diff changeset
1502 }
Ritor1
parents:
diff changeset
1503
Ritor1
parents:
diff changeset
1504 buf[0] = back->index;
Ritor1
parents:
diff changeset
1505 png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
Ritor1
parents:
diff changeset
1506 }
Ritor1
parents:
diff changeset
1507
Ritor1
parents:
diff changeset
1508 else if (color_type & PNG_COLOR_MASK_COLOR)
Ritor1
parents:
diff changeset
1509 {
Ritor1
parents:
diff changeset
1510 png_save_uint_16(buf, back->red);
Ritor1
parents:
diff changeset
1511 png_save_uint_16(buf + 2, back->green);
Ritor1
parents:
diff changeset
1512 png_save_uint_16(buf + 4, back->blue);
Ritor1
parents:
diff changeset
1513 #ifdef PNG_WRITE_16BIT_SUPPORTED
Ritor1
parents:
diff changeset
1514 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Ritor1
parents:
diff changeset
1515 #else
Ritor1
parents:
diff changeset
1516 if (buf[0] | buf[2] | buf[4])
Ritor1
parents:
diff changeset
1517 #endif
Ritor1
parents:
diff changeset
1518 {
Ritor1
parents:
diff changeset
1519 png_warning(png_ptr,
Ritor1
parents:
diff changeset
1520 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
Ritor1
parents:
diff changeset
1521
Ritor1
parents:
diff changeset
1522 return;
Ritor1
parents:
diff changeset
1523 }
Ritor1
parents:
diff changeset
1524
Ritor1
parents:
diff changeset
1525 png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
Ritor1
parents:
diff changeset
1526 }
Ritor1
parents:
diff changeset
1527
Ritor1
parents:
diff changeset
1528 else
Ritor1
parents:
diff changeset
1529 {
Ritor1
parents:
diff changeset
1530 if (back->gray >= (1 << png_ptr->bit_depth))
Ritor1
parents:
diff changeset
1531 {
Ritor1
parents:
diff changeset
1532 png_warning(png_ptr,
Ritor1
parents:
diff changeset
1533 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
Ritor1
parents:
diff changeset
1534
Ritor1
parents:
diff changeset
1535 return;
Ritor1
parents:
diff changeset
1536 }
Ritor1
parents:
diff changeset
1537
Ritor1
parents:
diff changeset
1538 png_save_uint_16(buf, back->gray);
Ritor1
parents:
diff changeset
1539 png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
Ritor1
parents:
diff changeset
1540 }
Ritor1
parents:
diff changeset
1541 }
Ritor1
parents:
diff changeset
1542 #endif
Ritor1
parents:
diff changeset
1543
Ritor1
parents:
diff changeset
1544 #ifdef PNG_WRITE_hIST_SUPPORTED
Ritor1
parents:
diff changeset
1545 /* Write the histogram */
Ritor1
parents:
diff changeset
1546 void /* PRIVATE */
Ritor1
parents:
diff changeset
1547 png_write_hIST(png_structrp png_ptr, png_const_uint_16p hist, int num_hist)
Ritor1
parents:
diff changeset
1548 {
Ritor1
parents:
diff changeset
1549 int i;
Ritor1
parents:
diff changeset
1550 png_byte buf[3];
Ritor1
parents:
diff changeset
1551
Ritor1
parents:
diff changeset
1552 png_debug(1, "in png_write_hIST");
Ritor1
parents:
diff changeset
1553
Ritor1
parents:
diff changeset
1554 if (num_hist > (int)png_ptr->num_palette)
Ritor1
parents:
diff changeset
1555 {
Ritor1
parents:
diff changeset
1556 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
Ritor1
parents:
diff changeset
1557 png_ptr->num_palette);
Ritor1
parents:
diff changeset
1558
Ritor1
parents:
diff changeset
1559 png_warning(png_ptr, "Invalid number of histogram entries specified");
Ritor1
parents:
diff changeset
1560 return;
Ritor1
parents:
diff changeset
1561 }
Ritor1
parents:
diff changeset
1562
Ritor1
parents:
diff changeset
1563 png_write_chunk_header(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
Ritor1
parents:
diff changeset
1564
Ritor1
parents:
diff changeset
1565 for (i = 0; i < num_hist; i++)
Ritor1
parents:
diff changeset
1566 {
Ritor1
parents:
diff changeset
1567 png_save_uint_16(buf, hist[i]);
Ritor1
parents:
diff changeset
1568 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Ritor1
parents:
diff changeset
1569 }
Ritor1
parents:
diff changeset
1570
Ritor1
parents:
diff changeset
1571 png_write_chunk_end(png_ptr);
Ritor1
parents:
diff changeset
1572 }
Ritor1
parents:
diff changeset
1573 #endif
Ritor1
parents:
diff changeset
1574
Ritor1
parents:
diff changeset
1575 #ifdef PNG_WRITE_tEXt_SUPPORTED
Ritor1
parents:
diff changeset
1576 /* Write a tEXt chunk */
Ritor1
parents:
diff changeset
1577 void /* PRIVATE */
Ritor1
parents:
diff changeset
1578 png_write_tEXt(png_structrp png_ptr, png_const_charp key, png_const_charp text,
Ritor1
parents:
diff changeset
1579 png_size_t text_len)
Ritor1
parents:
diff changeset
1580 {
Ritor1
parents:
diff changeset
1581 png_uint_32 key_len;
Ritor1
parents:
diff changeset
1582 png_byte new_key[80];
Ritor1
parents:
diff changeset
1583
Ritor1
parents:
diff changeset
1584 png_debug(1, "in png_write_tEXt");
Ritor1
parents:
diff changeset
1585
Ritor1
parents:
diff changeset
1586 key_len = png_check_keyword(png_ptr, key, new_key);
Ritor1
parents:
diff changeset
1587
Ritor1
parents:
diff changeset
1588 if (key_len == 0)
Ritor1
parents:
diff changeset
1589 png_error(png_ptr, "tEXt: invalid keyword");
Ritor1
parents:
diff changeset
1590
Ritor1
parents:
diff changeset
1591 if (text == NULL || *text == '\0')
Ritor1
parents:
diff changeset
1592 text_len = 0;
Ritor1
parents:
diff changeset
1593
Ritor1
parents:
diff changeset
1594 else
Ritor1
parents:
diff changeset
1595 text_len = strlen(text);
Ritor1
parents:
diff changeset
1596
Ritor1
parents:
diff changeset
1597 if (text_len > PNG_UINT_31_MAX - (key_len+1))
Ritor1
parents:
diff changeset
1598 png_error(png_ptr, "tEXt: text too long");
Ritor1
parents:
diff changeset
1599
Ritor1
parents:
diff changeset
1600 /* Make sure we include the 0 after the key */
Ritor1
parents:
diff changeset
1601 png_write_chunk_header(png_ptr, png_tEXt,
Ritor1
parents:
diff changeset
1602 (png_uint_32)/*checked above*/(key_len + text_len + 1));
Ritor1
parents:
diff changeset
1603 /*
Ritor1
parents:
diff changeset
1604 * We leave it to the application to meet PNG-1.0 requirements on the
Ritor1
parents:
diff changeset
1605 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
Ritor1
parents:
diff changeset
1606 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Ritor1
parents:
diff changeset
1607 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Ritor1
parents:
diff changeset
1608 */
Ritor1
parents:
diff changeset
1609 png_write_chunk_data(png_ptr, new_key, key_len + 1);
Ritor1
parents:
diff changeset
1610
Ritor1
parents:
diff changeset
1611 if (text_len)
Ritor1
parents:
diff changeset
1612 png_write_chunk_data(png_ptr, (png_const_bytep)text, text_len);
Ritor1
parents:
diff changeset
1613
Ritor1
parents:
diff changeset
1614 png_write_chunk_end(png_ptr);
Ritor1
parents:
diff changeset
1615 }
Ritor1
parents:
diff changeset
1616 #endif
Ritor1
parents:
diff changeset
1617
Ritor1
parents:
diff changeset
1618 #ifdef PNG_WRITE_zTXt_SUPPORTED
Ritor1
parents:
diff changeset
1619 /* Write a compressed text chunk */
Ritor1
parents:
diff changeset
1620 void /* PRIVATE */
Ritor1
parents:
diff changeset
1621 png_write_zTXt(png_structrp png_ptr, png_const_charp key, png_const_charp text,
Ritor1
parents:
diff changeset
1622 png_size_t text_len, int compression)
Ritor1
parents:
diff changeset
1623 {
Ritor1
parents:
diff changeset
1624 png_uint_32 key_len;
Ritor1
parents:
diff changeset
1625 png_byte new_key[81];
Ritor1
parents:
diff changeset
1626 compression_state comp;
Ritor1
parents:
diff changeset
1627
Ritor1
parents:
diff changeset
1628 png_debug(1, "in png_write_zTXt");
Ritor1
parents:
diff changeset
1629 PNG_UNUSED(text_len) /* Always use strlen */
Ritor1
parents:
diff changeset
1630
Ritor1
parents:
diff changeset
1631 if (compression == PNG_TEXT_COMPRESSION_NONE)
Ritor1
parents:
diff changeset
1632 {
Ritor1
parents:
diff changeset
1633 png_write_tEXt(png_ptr, key, text, 0);
Ritor1
parents:
diff changeset
1634 return;
Ritor1
parents:
diff changeset
1635 }
Ritor1
parents:
diff changeset
1636
Ritor1
parents:
diff changeset
1637 if (compression != PNG_TEXT_COMPRESSION_zTXt)
Ritor1
parents:
diff changeset
1638 png_error(png_ptr, "zTXt: invalid compression type");
Ritor1
parents:
diff changeset
1639
Ritor1
parents:
diff changeset
1640 key_len = png_check_keyword(png_ptr, key, new_key);
Ritor1
parents:
diff changeset
1641
Ritor1
parents:
diff changeset
1642 if (key_len == 0)
Ritor1
parents:
diff changeset
1643 png_error(png_ptr, "zTXt: invalid keyword");
Ritor1
parents:
diff changeset
1644
Ritor1
parents:
diff changeset
1645 /* Add the compression method and 1 for the keyword separator. */
Ritor1
parents:
diff changeset
1646 new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE;
Ritor1
parents:
diff changeset
1647 ++key_len;
Ritor1
parents:
diff changeset
1648
Ritor1
parents:
diff changeset
1649 /* Compute the compressed data; do it now for the length */
Ritor1
parents:
diff changeset
1650 png_text_compress_init(&comp, (png_const_bytep)text,
Ritor1
parents:
diff changeset
1651 text == NULL ? 0 : strlen(text));
Ritor1
parents:
diff changeset
1652
Ritor1
parents:
diff changeset
1653 if (png_text_compress(png_ptr, png_zTXt, &comp, key_len) != Z_OK)
Ritor1
parents:
diff changeset
1654 png_error(png_ptr, png_ptr->zstream.msg);
Ritor1
parents:
diff changeset
1655
Ritor1
parents:
diff changeset
1656 /* Write start of chunk */
Ritor1
parents:
diff changeset
1657 png_write_chunk_header(png_ptr, png_zTXt, key_len + comp.output_len);
Ritor1
parents:
diff changeset
1658
Ritor1
parents:
diff changeset
1659 /* Write key */
Ritor1
parents:
diff changeset
1660 png_write_chunk_data(png_ptr, new_key, key_len);
Ritor1
parents:
diff changeset
1661
Ritor1
parents:
diff changeset
1662 /* Write the compressed data */
Ritor1
parents:
diff changeset
1663 png_write_compressed_data_out(png_ptr, &comp);
Ritor1
parents:
diff changeset
1664
Ritor1
parents:
diff changeset
1665 /* Close the chunk */
Ritor1
parents:
diff changeset
1666 png_write_chunk_end(png_ptr);
Ritor1
parents:
diff changeset
1667 }
Ritor1
parents:
diff changeset
1668 #endif
Ritor1
parents:
diff changeset
1669
Ritor1
parents:
diff changeset
1670 #ifdef PNG_WRITE_iTXt_SUPPORTED
Ritor1
parents:
diff changeset
1671 /* Write an iTXt chunk */
Ritor1
parents:
diff changeset
1672 void /* PRIVATE */
Ritor1
parents:
diff changeset
1673 png_write_iTXt(png_structrp png_ptr, int compression, png_const_charp key,
Ritor1
parents:
diff changeset
1674 png_const_charp lang, png_const_charp lang_key, png_const_charp text)
Ritor1
parents:
diff changeset
1675 {
Ritor1
parents:
diff changeset
1676 png_uint_32 key_len, prefix_len;
Ritor1
parents:
diff changeset
1677 png_size_t lang_len, lang_key_len;
Ritor1
parents:
diff changeset
1678 png_byte new_key[82];
Ritor1
parents:
diff changeset
1679 compression_state comp;
Ritor1
parents:
diff changeset
1680
Ritor1
parents:
diff changeset
1681 png_debug(1, "in png_write_iTXt");
Ritor1
parents:
diff changeset
1682
Ritor1
parents:
diff changeset
1683 key_len = png_check_keyword(png_ptr, key, new_key);
Ritor1
parents:
diff changeset
1684
Ritor1
parents:
diff changeset
1685 if (key_len == 0)
Ritor1
parents:
diff changeset
1686 png_error(png_ptr, "iTXt: invalid keyword");
Ritor1
parents:
diff changeset
1687
Ritor1
parents:
diff changeset
1688 /* Set the compression flag */
Ritor1
parents:
diff changeset
1689 switch (compression)
Ritor1
parents:
diff changeset
1690 {
Ritor1
parents:
diff changeset
1691 case PNG_ITXT_COMPRESSION_NONE:
Ritor1
parents:
diff changeset
1692 case PNG_TEXT_COMPRESSION_NONE:
Ritor1
parents:
diff changeset
1693 compression = new_key[++key_len] = 0; /* no compression */
Ritor1
parents:
diff changeset
1694 break;
Ritor1
parents:
diff changeset
1695
Ritor1
parents:
diff changeset
1696 case PNG_TEXT_COMPRESSION_zTXt:
Ritor1
parents:
diff changeset
1697 case PNG_ITXT_COMPRESSION_zTXt:
Ritor1
parents:
diff changeset
1698 compression = new_key[++key_len] = 1; /* compressed */
Ritor1
parents:
diff changeset
1699 break;
Ritor1
parents:
diff changeset
1700
Ritor1
parents:
diff changeset
1701 default:
Ritor1
parents:
diff changeset
1702 png_error(png_ptr, "iTXt: invalid compression");
Ritor1
parents:
diff changeset
1703 }
Ritor1
parents:
diff changeset
1704
Ritor1
parents:
diff changeset
1705 new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE;
Ritor1
parents:
diff changeset
1706 ++key_len; /* for the keywod separator */
Ritor1
parents:
diff changeset
1707
Ritor1
parents:
diff changeset
1708 /* We leave it to the application to meet PNG-1.0 requirements on the
Ritor1
parents:
diff changeset
1709 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
Ritor1
parents:
diff changeset
1710 * any non-Latin-1 characters except for NEWLINE. ISO PNG, however,
Ritor1
parents:
diff changeset
1711 * specifies that the text is UTF-8 and this really doesn't require any
Ritor1
parents:
diff changeset
1712 * checking.
Ritor1
parents:
diff changeset
1713 *
Ritor1
parents:
diff changeset
1714 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Ritor1
parents:
diff changeset
1715 *
Ritor1
parents:
diff changeset
1716 * TODO: validate the language tag correctly (see the spec.)
Ritor1
parents:
diff changeset
1717 */
Ritor1
parents:
diff changeset
1718 if (lang == NULL) lang = ""; /* empty language is valid */
Ritor1
parents:
diff changeset
1719 lang_len = strlen(lang)+1;
Ritor1
parents:
diff changeset
1720 if (lang_key == NULL) lang_key = ""; /* may be empty */
Ritor1
parents:
diff changeset
1721 lang_key_len = strlen(lang_key)+1;
Ritor1
parents:
diff changeset
1722 if (text == NULL) text = ""; /* may be empty */
Ritor1
parents:
diff changeset
1723
Ritor1
parents:
diff changeset
1724 prefix_len = key_len;
Ritor1
parents:
diff changeset
1725 if (lang_len > PNG_UINT_31_MAX-prefix_len)
Ritor1
parents:
diff changeset
1726 prefix_len = PNG_UINT_31_MAX;
Ritor1
parents:
diff changeset
1727 else
Ritor1
parents:
diff changeset
1728 prefix_len = (png_uint_32)(prefix_len + lang_len);
Ritor1
parents:
diff changeset
1729
Ritor1
parents:
diff changeset
1730 if (lang_key_len > PNG_UINT_31_MAX-prefix_len)
Ritor1
parents:
diff changeset
1731 prefix_len = PNG_UINT_31_MAX;
Ritor1
parents:
diff changeset
1732 else
Ritor1
parents:
diff changeset
1733 prefix_len = (png_uint_32)(prefix_len + lang_key_len);
Ritor1
parents:
diff changeset
1734
Ritor1
parents:
diff changeset
1735 png_text_compress_init(&comp, (png_const_bytep)text, strlen(text));
Ritor1
parents:
diff changeset
1736
Ritor1
parents:
diff changeset
1737 if (compression)
Ritor1
parents:
diff changeset
1738 {
Ritor1
parents:
diff changeset
1739 if (png_text_compress(png_ptr, png_iTXt, &comp, prefix_len) != Z_OK)
Ritor1
parents:
diff changeset
1740 png_error(png_ptr, png_ptr->zstream.msg);
Ritor1
parents:
diff changeset
1741 }
Ritor1
parents:
diff changeset
1742
Ritor1
parents:
diff changeset
1743 else
Ritor1
parents:
diff changeset
1744 {
Ritor1
parents:
diff changeset
1745 if (comp.input_len > PNG_UINT_31_MAX-prefix_len)
Ritor1
parents:
diff changeset
1746 png_error(png_ptr, "iTXt: uncompressed text too long");
Ritor1
parents:
diff changeset
1747
Ritor1
parents:
diff changeset
1748 /* So the string will fit in a chunk: */
Ritor1
parents:
diff changeset
1749 comp.output_len = (png_uint_32)/*SAFE*/comp.input_len;
Ritor1
parents:
diff changeset
1750 }
Ritor1
parents:
diff changeset
1751
Ritor1
parents:
diff changeset
1752 png_write_chunk_header(png_ptr, png_iTXt, comp.output_len + prefix_len);
Ritor1
parents:
diff changeset
1753
Ritor1
parents:
diff changeset
1754 png_write_chunk_data(png_ptr, new_key, key_len);
Ritor1
parents:
diff changeset
1755
Ritor1
parents:
diff changeset
1756 png_write_chunk_data(png_ptr, (png_const_bytep)lang, lang_len);
Ritor1
parents:
diff changeset
1757
Ritor1
parents:
diff changeset
1758 png_write_chunk_data(png_ptr, (png_const_bytep)lang_key, lang_key_len);
Ritor1
parents:
diff changeset
1759
Ritor1
parents:
diff changeset
1760 if (compression)
Ritor1
parents:
diff changeset
1761 png_write_compressed_data_out(png_ptr, &comp);
Ritor1
parents:
diff changeset
1762
Ritor1
parents:
diff changeset
1763 else
Ritor1
parents:
diff changeset
1764 png_write_chunk_data(png_ptr, (png_const_bytep)text, comp.input_len);
Ritor1
parents:
diff changeset
1765
Ritor1
parents:
diff changeset
1766 png_write_chunk_end(png_ptr);
Ritor1
parents:
diff changeset
1767 }
Ritor1
parents:
diff changeset
1768 #endif
Ritor1
parents:
diff changeset
1769
Ritor1
parents:
diff changeset
1770 #ifdef PNG_WRITE_oFFs_SUPPORTED
Ritor1
parents:
diff changeset
1771 /* Write the oFFs chunk */
Ritor1
parents:
diff changeset
1772 void /* PRIVATE */
Ritor1
parents:
diff changeset
1773 png_write_oFFs(png_structrp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Ritor1
parents:
diff changeset
1774 int unit_type)
Ritor1
parents:
diff changeset
1775 {
Ritor1
parents:
diff changeset
1776 png_byte buf[9];
Ritor1
parents:
diff changeset
1777
Ritor1
parents:
diff changeset
1778 png_debug(1, "in png_write_oFFs");
Ritor1
parents:
diff changeset
1779
Ritor1
parents:
diff changeset
1780 if (unit_type >= PNG_OFFSET_LAST)
Ritor1
parents:
diff changeset
1781 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
Ritor1
parents:
diff changeset
1782
Ritor1
parents:
diff changeset
1783 png_save_int_32(buf, x_offset);
Ritor1
parents:
diff changeset
1784 png_save_int_32(buf + 4, y_offset);
Ritor1
parents:
diff changeset
1785 buf[8] = (png_byte)unit_type;
Ritor1
parents:
diff changeset
1786
Ritor1
parents:
diff changeset
1787 png_write_complete_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
Ritor1
parents:
diff changeset
1788 }
Ritor1
parents:
diff changeset
1789 #endif
Ritor1
parents:
diff changeset
1790 #ifdef PNG_WRITE_pCAL_SUPPORTED
Ritor1
parents:
diff changeset
1791 /* Write the pCAL chunk (described in the PNG extensions document) */
Ritor1
parents:
diff changeset
1792 void /* PRIVATE */
Ritor1
parents:
diff changeset
1793 png_write_pCAL(png_structrp png_ptr, png_charp purpose, png_int_32 X0,
Ritor1
parents:
diff changeset
1794 png_int_32 X1, int type, int nparams, png_const_charp units,
Ritor1
parents:
diff changeset
1795 png_charpp params)
Ritor1
parents:
diff changeset
1796 {
Ritor1
parents:
diff changeset
1797 png_uint_32 purpose_len;
Ritor1
parents:
diff changeset
1798 png_size_t units_len, total_len;
Ritor1
parents:
diff changeset
1799 png_size_tp params_len;
Ritor1
parents:
diff changeset
1800 png_byte buf[10];
Ritor1
parents:
diff changeset
1801 png_byte new_purpose[80];
Ritor1
parents:
diff changeset
1802 int i;
Ritor1
parents:
diff changeset
1803
Ritor1
parents:
diff changeset
1804 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
Ritor1
parents:
diff changeset
1805
Ritor1
parents:
diff changeset
1806 if (type >= PNG_EQUATION_LAST)
Ritor1
parents:
diff changeset
1807 png_error(png_ptr, "Unrecognized equation type for pCAL chunk");
Ritor1
parents:
diff changeset
1808
Ritor1
parents:
diff changeset
1809 purpose_len = png_check_keyword(png_ptr, purpose, new_purpose);
Ritor1
parents:
diff changeset
1810
Ritor1
parents:
diff changeset
1811 if (purpose_len == 0)
Ritor1
parents:
diff changeset
1812 png_error(png_ptr, "pCAL: invalid keyword");
Ritor1
parents:
diff changeset
1813
Ritor1
parents:
diff changeset
1814 ++purpose_len; /* terminator */
Ritor1
parents:
diff changeset
1815
Ritor1
parents:
diff changeset
1816 png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
Ritor1
parents:
diff changeset
1817 units_len = strlen(units) + (nparams == 0 ? 0 : 1);
Ritor1
parents:
diff changeset
1818 png_debug1(3, "pCAL units length = %d", (int)units_len);
Ritor1
parents:
diff changeset
1819 total_len = purpose_len + units_len + 10;
Ritor1
parents:
diff changeset
1820
Ritor1
parents:
diff changeset
1821 params_len = (png_size_tp)png_malloc(png_ptr,
Ritor1
parents:
diff changeset
1822 (png_alloc_size_t)(nparams * (sizeof (png_size_t))));
Ritor1
parents:
diff changeset
1823
Ritor1
parents:
diff changeset
1824 /* Find the length of each parameter, making sure we don't count the
Ritor1
parents:
diff changeset
1825 * null terminator for the last parameter.
Ritor1
parents:
diff changeset
1826 */
Ritor1
parents:
diff changeset
1827 for (i = 0; i < nparams; i++)
Ritor1
parents:
diff changeset
1828 {
Ritor1
parents:
diff changeset
1829 params_len[i] = strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Ritor1
parents:
diff changeset
1830 png_debug2(3, "pCAL parameter %d length = %lu", i,
Ritor1
parents:
diff changeset
1831 (unsigned long)params_len[i]);
Ritor1
parents:
diff changeset
1832 total_len += params_len[i];
Ritor1
parents:
diff changeset
1833 }
Ritor1
parents:
diff changeset
1834
Ritor1
parents:
diff changeset
1835 png_debug1(3, "pCAL total length = %d", (int)total_len);
Ritor1
parents:
diff changeset
1836 png_write_chunk_header(png_ptr, png_pCAL, (png_uint_32)total_len);
Ritor1
parents:
diff changeset
1837 png_write_chunk_data(png_ptr, new_purpose, purpose_len);
Ritor1
parents:
diff changeset
1838 png_save_int_32(buf, X0);
Ritor1
parents:
diff changeset
1839 png_save_int_32(buf + 4, X1);
Ritor1
parents:
diff changeset
1840 buf[8] = (png_byte)type;
Ritor1
parents:
diff changeset
1841 buf[9] = (png_byte)nparams;
Ritor1
parents:
diff changeset
1842 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
Ritor1
parents:
diff changeset
1843 png_write_chunk_data(png_ptr, (png_const_bytep)units, (png_size_t)units_len);
Ritor1
parents:
diff changeset
1844
Ritor1
parents:
diff changeset
1845 for (i = 0; i < nparams; i++)
Ritor1
parents:
diff changeset
1846 {
Ritor1
parents:
diff changeset
1847 png_write_chunk_data(png_ptr, (png_const_bytep)params[i], params_len[i]);
Ritor1
parents:
diff changeset
1848 }
Ritor1
parents:
diff changeset
1849
Ritor1
parents:
diff changeset
1850 png_free(png_ptr, params_len);
Ritor1
parents:
diff changeset
1851 png_write_chunk_end(png_ptr);
Ritor1
parents:
diff changeset
1852 }
Ritor1
parents:
diff changeset
1853 #endif
Ritor1
parents:
diff changeset
1854
Ritor1
parents:
diff changeset
1855 #ifdef PNG_WRITE_sCAL_SUPPORTED
Ritor1
parents:
diff changeset
1856 /* Write the sCAL chunk */
Ritor1
parents:
diff changeset
1857 void /* PRIVATE */
Ritor1
parents:
diff changeset
1858 png_write_sCAL_s(png_structrp png_ptr, int unit, png_const_charp width,
Ritor1
parents:
diff changeset
1859 png_const_charp height)
Ritor1
parents:
diff changeset
1860 {
Ritor1
parents:
diff changeset
1861 png_byte buf[64];
Ritor1
parents:
diff changeset
1862 png_size_t wlen, hlen, total_len;
Ritor1
parents:
diff changeset
1863
Ritor1
parents:
diff changeset
1864 png_debug(1, "in png_write_sCAL_s");
Ritor1
parents:
diff changeset
1865
Ritor1
parents:
diff changeset
1866 wlen = strlen(width);
Ritor1
parents:
diff changeset
1867 hlen = strlen(height);
Ritor1
parents:
diff changeset
1868 total_len = wlen + hlen + 2;
Ritor1
parents:
diff changeset
1869
Ritor1
parents:
diff changeset
1870 if (total_len > 64)
Ritor1
parents:
diff changeset
1871 {
Ritor1
parents:
diff changeset
1872 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
Ritor1
parents:
diff changeset
1873 return;
Ritor1
parents:
diff changeset
1874 }
Ritor1
parents:
diff changeset
1875
Ritor1
parents:
diff changeset
1876 buf[0] = (png_byte)unit;
Ritor1
parents:
diff changeset
1877 memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */
Ritor1
parents:
diff changeset
1878 memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */
Ritor1
parents:
diff changeset
1879
Ritor1
parents:
diff changeset
1880 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Ritor1
parents:
diff changeset
1881 png_write_complete_chunk(png_ptr, png_sCAL, buf, total_len);
Ritor1
parents:
diff changeset
1882 }
Ritor1
parents:
diff changeset
1883 #endif
Ritor1
parents:
diff changeset
1884
Ritor1
parents:
diff changeset
1885 #ifdef PNG_WRITE_pHYs_SUPPORTED
Ritor1
parents:
diff changeset
1886 /* Write the pHYs chunk */
Ritor1
parents:
diff changeset
1887 void /* PRIVATE */
Ritor1
parents:
diff changeset
1888 png_write_pHYs(png_structrp png_ptr, png_uint_32 x_pixels_per_unit,
Ritor1
parents:
diff changeset
1889 png_uint_32 y_pixels_per_unit,
Ritor1
parents:
diff changeset
1890 int unit_type)
Ritor1
parents:
diff changeset
1891 {
Ritor1
parents:
diff changeset
1892 png_byte buf[9];
Ritor1
parents:
diff changeset
1893
Ritor1
parents:
diff changeset
1894 png_debug(1, "in png_write_pHYs");
Ritor1
parents:
diff changeset
1895
Ritor1
parents:
diff changeset
1896 if (unit_type >= PNG_RESOLUTION_LAST)
Ritor1
parents:
diff changeset
1897 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
Ritor1
parents:
diff changeset
1898
Ritor1
parents:
diff changeset
1899 png_save_uint_32(buf, x_pixels_per_unit);
Ritor1
parents:
diff changeset
1900 png_save_uint_32(buf + 4, y_pixels_per_unit);
Ritor1
parents:
diff changeset
1901 buf[8] = (png_byte)unit_type;
Ritor1
parents:
diff changeset
1902
Ritor1
parents:
diff changeset
1903 png_write_complete_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
Ritor1
parents:
diff changeset
1904 }
Ritor1
parents:
diff changeset
1905 #endif
Ritor1
parents:
diff changeset
1906
Ritor1
parents:
diff changeset
1907 #ifdef PNG_WRITE_tIME_SUPPORTED
Ritor1
parents:
diff changeset
1908 /* Write the tIME chunk. Use either png_convert_from_struct_tm()
Ritor1
parents:
diff changeset
1909 * or png_convert_from_time_t(), or fill in the structure yourself.
Ritor1
parents:
diff changeset
1910 */
Ritor1
parents:
diff changeset
1911 void /* PRIVATE */
Ritor1
parents:
diff changeset
1912 png_write_tIME(png_structrp png_ptr, png_const_timep mod_time)
Ritor1
parents:
diff changeset
1913 {
Ritor1
parents:
diff changeset
1914 png_byte buf[7];
Ritor1
parents:
diff changeset
1915
Ritor1
parents:
diff changeset
1916 png_debug(1, "in png_write_tIME");
Ritor1
parents:
diff changeset
1917
Ritor1
parents:
diff changeset
1918 if (mod_time->month > 12 || mod_time->month < 1 ||
Ritor1
parents:
diff changeset
1919 mod_time->day > 31 || mod_time->day < 1 ||
Ritor1
parents:
diff changeset
1920 mod_time->hour > 23 || mod_time->second > 60)
Ritor1
parents:
diff changeset
1921 {
Ritor1
parents:
diff changeset
1922 png_warning(png_ptr, "Invalid time specified for tIME chunk");
Ritor1
parents:
diff changeset
1923 return;
Ritor1
parents:
diff changeset
1924 }
Ritor1
parents:
diff changeset
1925
Ritor1
parents:
diff changeset
1926 png_save_uint_16(buf, mod_time->year);
Ritor1
parents:
diff changeset
1927 buf[2] = mod_time->month;
Ritor1
parents:
diff changeset
1928 buf[3] = mod_time->day;
Ritor1
parents:
diff changeset
1929 buf[4] = mod_time->hour;
Ritor1
parents:
diff changeset
1930 buf[5] = mod_time->minute;
Ritor1
parents:
diff changeset
1931 buf[6] = mod_time->second;
Ritor1
parents:
diff changeset
1932
Ritor1
parents:
diff changeset
1933 png_write_complete_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
Ritor1
parents:
diff changeset
1934 }
Ritor1
parents:
diff changeset
1935 #endif
Ritor1
parents:
diff changeset
1936
Ritor1
parents:
diff changeset
1937 /* Initializes the row writing capability of libpng */
Ritor1
parents:
diff changeset
1938 void /* PRIVATE */
Ritor1
parents:
diff changeset
1939 png_write_start_row(png_structrp png_ptr)
Ritor1
parents:
diff changeset
1940 {
Ritor1
parents:
diff changeset
1941 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
Ritor1
parents:
diff changeset
1942 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Ritor1
parents:
diff changeset
1943
Ritor1
parents:
diff changeset
1944 /* Start of interlace block */
Ritor1
parents:
diff changeset
1945 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Ritor1
parents:
diff changeset
1946
Ritor1
parents:
diff changeset
1947 /* Offset to next interlace block */
Ritor1
parents:
diff changeset
1948 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Ritor1
parents:
diff changeset
1949
Ritor1
parents:
diff changeset
1950 /* Start of interlace block in the y direction */
Ritor1
parents:
diff changeset
1951 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Ritor1
parents:
diff changeset
1952
Ritor1
parents:
diff changeset
1953 /* Offset to next interlace block in the y direction */
Ritor1
parents:
diff changeset
1954 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Ritor1
parents:
diff changeset
1955 #endif
Ritor1
parents:
diff changeset
1956
Ritor1
parents:
diff changeset
1957 png_alloc_size_t buf_size;
Ritor1
parents:
diff changeset
1958 int usr_pixel_depth;
Ritor1
parents:
diff changeset
1959
Ritor1
parents:
diff changeset
1960 png_debug(1, "in png_write_start_row");
Ritor1
parents:
diff changeset
1961
Ritor1
parents:
diff changeset
1962 usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth;
Ritor1
parents:
diff changeset
1963 buf_size = PNG_ROWBYTES(usr_pixel_depth, png_ptr->width) + 1;
Ritor1
parents:
diff changeset
1964
Ritor1
parents:
diff changeset
1965 /* 1.5.6: added to allow checking in the row write code. */
Ritor1
parents:
diff changeset
1966 png_ptr->transformed_pixel_depth = png_ptr->pixel_depth;
Ritor1
parents:
diff changeset
1967 png_ptr->maximum_pixel_depth = (png_byte)usr_pixel_depth;
Ritor1
parents:
diff changeset
1968
Ritor1
parents:
diff changeset
1969 /* Set up row buffer */
Ritor1
parents:
diff changeset
1970 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size);
Ritor1
parents:
diff changeset
1971
Ritor1
parents:
diff changeset
1972 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Ritor1
parents:
diff changeset
1973
Ritor1
parents:
diff changeset
1974 #ifdef PNG_WRITE_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
1975 /* Set up filtering buffer, if using this filter */
Ritor1
parents:
diff changeset
1976 if (png_ptr->do_filter & PNG_FILTER_SUB)
Ritor1
parents:
diff changeset
1977 {
Ritor1
parents:
diff changeset
1978 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
Ritor1
parents:
diff changeset
1979
Ritor1
parents:
diff changeset
1980 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Ritor1
parents:
diff changeset
1981 }
Ritor1
parents:
diff changeset
1982
Ritor1
parents:
diff changeset
1983 /* We only need to keep the previous row if we are using one of these. */
Ritor1
parents:
diff changeset
1984 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
Ritor1
parents:
diff changeset
1985 {
Ritor1
parents:
diff changeset
1986 /* Set up previous row buffer */
Ritor1
parents:
diff changeset
1987 png_ptr->prev_row = (png_bytep)png_calloc(png_ptr, buf_size);
Ritor1
parents:
diff changeset
1988
Ritor1
parents:
diff changeset
1989 if (png_ptr->do_filter & PNG_FILTER_UP)
Ritor1
parents:
diff changeset
1990 {
Ritor1
parents:
diff changeset
1991 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Ritor1
parents:
diff changeset
1992 png_ptr->rowbytes + 1);
Ritor1
parents:
diff changeset
1993
Ritor1
parents:
diff changeset
1994 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Ritor1
parents:
diff changeset
1995 }
Ritor1
parents:
diff changeset
1996
Ritor1
parents:
diff changeset
1997 if (png_ptr->do_filter & PNG_FILTER_AVG)
Ritor1
parents:
diff changeset
1998 {
Ritor1
parents:
diff changeset
1999 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Ritor1
parents:
diff changeset
2000 png_ptr->rowbytes + 1);
Ritor1
parents:
diff changeset
2001
Ritor1
parents:
diff changeset
2002 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Ritor1
parents:
diff changeset
2003 }
Ritor1
parents:
diff changeset
2004
Ritor1
parents:
diff changeset
2005 if (png_ptr->do_filter & PNG_FILTER_PAETH)
Ritor1
parents:
diff changeset
2006 {
Ritor1
parents:
diff changeset
2007 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Ritor1
parents:
diff changeset
2008 png_ptr->rowbytes + 1);
Ritor1
parents:
diff changeset
2009
Ritor1
parents:
diff changeset
2010 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Ritor1
parents:
diff changeset
2011 }
Ritor1
parents:
diff changeset
2012 }
Ritor1
parents:
diff changeset
2013 #endif /* PNG_WRITE_FILTER_SUPPORTED */
Ritor1
parents:
diff changeset
2014
Ritor1
parents:
diff changeset
2015 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
Ritor1
parents:
diff changeset
2016 /* If interlaced, we need to set up width and height of pass */
Ritor1
parents:
diff changeset
2017 if (png_ptr->interlaced)
Ritor1
parents:
diff changeset
2018 {
Ritor1
parents:
diff changeset
2019 if (!(png_ptr->transformations & PNG_INTERLACE))
Ritor1
parents:
diff changeset
2020 {
Ritor1
parents:
diff changeset
2021 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Ritor1
parents:
diff changeset
2022 png_pass_ystart[0]) / png_pass_yinc[0];
Ritor1
parents:
diff changeset
2023
Ritor1
parents:
diff changeset
2024 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
Ritor1
parents:
diff changeset
2025 png_pass_start[0]) / png_pass_inc[0];
Ritor1
parents:
diff changeset
2026 }
Ritor1
parents:
diff changeset
2027
Ritor1
parents:
diff changeset
2028 else
Ritor1
parents:
diff changeset
2029 {
Ritor1
parents:
diff changeset
2030 png_ptr->num_rows = png_ptr->height;
Ritor1
parents:
diff changeset
2031 png_ptr->usr_width = png_ptr->width;
Ritor1
parents:
diff changeset
2032 }
Ritor1
parents:
diff changeset
2033 }
Ritor1
parents:
diff changeset
2034
Ritor1
parents:
diff changeset
2035 else
Ritor1
parents:
diff changeset
2036 #endif
Ritor1
parents:
diff changeset
2037 {
Ritor1
parents:
diff changeset
2038 png_ptr->num_rows = png_ptr->height;
Ritor1
parents:
diff changeset
2039 png_ptr->usr_width = png_ptr->width;
Ritor1
parents:
diff changeset
2040 }
Ritor1
parents:
diff changeset
2041 }
Ritor1
parents:
diff changeset
2042
Ritor1
parents:
diff changeset
2043 /* Internal use only. Called when finished processing a row of data. */
Ritor1
parents:
diff changeset
2044 void /* PRIVATE */
Ritor1
parents:
diff changeset
2045 png_write_finish_row(png_structrp png_ptr)
Ritor1
parents:
diff changeset
2046 {
Ritor1
parents:
diff changeset
2047 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
Ritor1
parents:
diff changeset
2048 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Ritor1
parents:
diff changeset
2049
Ritor1
parents:
diff changeset
2050 /* Start of interlace block */
Ritor1
parents:
diff changeset
2051 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Ritor1
parents:
diff changeset
2052
Ritor1
parents:
diff changeset
2053 /* Offset to next interlace block */
Ritor1
parents:
diff changeset
2054 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Ritor1
parents:
diff changeset
2055
Ritor1
parents:
diff changeset
2056 /* Start of interlace block in the y direction */
Ritor1
parents:
diff changeset
2057 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Ritor1
parents:
diff changeset
2058
Ritor1
parents:
diff changeset
2059 /* Offset to next interlace block in the y direction */
Ritor1
parents:
diff changeset
2060 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Ritor1
parents:
diff changeset
2061 #endif
Ritor1
parents:
diff changeset
2062
Ritor1
parents:
diff changeset
2063 png_debug(1, "in png_write_finish_row");
Ritor1
parents:
diff changeset
2064
Ritor1
parents:
diff changeset
2065 /* Next row */
Ritor1
parents:
diff changeset
2066 png_ptr->row_number++;
Ritor1
parents:
diff changeset
2067
Ritor1
parents:
diff changeset
2068 /* See if we are done */
Ritor1
parents:
diff changeset
2069 if (png_ptr->row_number < png_ptr->num_rows)
Ritor1
parents:
diff changeset
2070 return;
Ritor1
parents:
diff changeset
2071
Ritor1
parents:
diff changeset
2072 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
Ritor1
parents:
diff changeset
2073 /* If interlaced, go to next pass */
Ritor1
parents:
diff changeset
2074 if (png_ptr->interlaced)
Ritor1
parents:
diff changeset
2075 {
Ritor1
parents:
diff changeset
2076 png_ptr->row_number = 0;
Ritor1
parents:
diff changeset
2077 if (png_ptr->transformations & PNG_INTERLACE)
Ritor1
parents:
diff changeset
2078 {
Ritor1
parents:
diff changeset
2079 png_ptr->pass++;
Ritor1
parents:
diff changeset
2080 }
Ritor1
parents:
diff changeset
2081
Ritor1
parents:
diff changeset
2082 else
Ritor1
parents:
diff changeset
2083 {
Ritor1
parents:
diff changeset
2084 /* Loop until we find a non-zero width or height pass */
Ritor1
parents:
diff changeset
2085 do
Ritor1
parents:
diff changeset
2086 {
Ritor1
parents:
diff changeset
2087 png_ptr->pass++;
Ritor1
parents:
diff changeset
2088
Ritor1
parents:
diff changeset
2089 if (png_ptr->pass >= 7)
Ritor1
parents:
diff changeset
2090 break;
Ritor1
parents:
diff changeset
2091
Ritor1
parents:
diff changeset
2092 png_ptr->usr_width = (png_ptr->width +
Ritor1
parents:
diff changeset
2093 png_pass_inc[png_ptr->pass] - 1 -
Ritor1
parents:
diff changeset
2094 png_pass_start[png_ptr->pass]) /
Ritor1
parents:
diff changeset
2095 png_pass_inc[png_ptr->pass];
Ritor1
parents:
diff changeset
2096
Ritor1
parents:
diff changeset
2097 png_ptr->num_rows = (png_ptr->height +
Ritor1
parents:
diff changeset
2098 png_pass_yinc[png_ptr->pass] - 1 -
Ritor1
parents:
diff changeset
2099 png_pass_ystart[png_ptr->pass]) /
Ritor1
parents:
diff changeset
2100 png_pass_yinc[png_ptr->pass];
Ritor1
parents:
diff changeset
2101
Ritor1
parents:
diff changeset
2102 if (png_ptr->transformations & PNG_INTERLACE)
Ritor1
parents:
diff changeset
2103 break;
Ritor1
parents:
diff changeset
2104
Ritor1
parents:
diff changeset
2105 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
Ritor1
parents:
diff changeset
2106
Ritor1
parents:
diff changeset
2107 }
Ritor1
parents:
diff changeset
2108
Ritor1
parents:
diff changeset
2109 /* Reset the row above the image for the next pass */
Ritor1
parents:
diff changeset
2110 if (png_ptr->pass < 7)
Ritor1
parents:
diff changeset
2111 {
Ritor1
parents:
diff changeset
2112 if (png_ptr->prev_row != NULL)
Ritor1
parents:
diff changeset
2113 memset(png_ptr->prev_row, 0,
Ritor1
parents:
diff changeset
2114 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
Ritor1
parents:
diff changeset
2115 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Ritor1
parents:
diff changeset
2116
Ritor1
parents:
diff changeset
2117 return;
Ritor1
parents:
diff changeset
2118 }
Ritor1
parents:
diff changeset
2119 }
Ritor1
parents:
diff changeset
2120 #endif
Ritor1
parents:
diff changeset
2121
Ritor1
parents:
diff changeset
2122 /* If we get here, we've just written the last row, so we need
Ritor1
parents:
diff changeset
2123 to flush the compressor */
Ritor1
parents:
diff changeset
2124 png_compress_IDAT(png_ptr, NULL, 0, Z_FINISH);
Ritor1
parents:
diff changeset
2125 }
Ritor1
parents:
diff changeset
2126
Ritor1
parents:
diff changeset
2127 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
Ritor1
parents:
diff changeset
2128 /* Pick out the correct pixels for the interlace pass.
Ritor1
parents:
diff changeset
2129 * The basic idea here is to go through the row with a source
Ritor1
parents:
diff changeset
2130 * pointer and a destination pointer (sp and dp), and copy the
Ritor1
parents:
diff changeset
2131 * correct pixels for the pass. As the row gets compacted,
Ritor1
parents:
diff changeset
2132 * sp will always be >= dp, so we should never overwrite anything.
Ritor1
parents:
diff changeset
2133 * See the default: case for the easiest code to understand.
Ritor1
parents:
diff changeset
2134 */
Ritor1
parents:
diff changeset
2135 void /* PRIVATE */
Ritor1
parents:
diff changeset
2136 png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Ritor1
parents:
diff changeset
2137 {
Ritor1
parents:
diff changeset
2138 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Ritor1
parents:
diff changeset
2139
Ritor1
parents:
diff changeset
2140 /* Start of interlace block */
Ritor1
parents:
diff changeset
2141 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Ritor1
parents:
diff changeset
2142
Ritor1
parents:
diff changeset
2143 /* Offset to next interlace block */
Ritor1
parents:
diff changeset
2144 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Ritor1
parents:
diff changeset
2145
Ritor1
parents:
diff changeset
2146 png_debug(1, "in png_do_write_interlace");
Ritor1
parents:
diff changeset
2147
Ritor1
parents:
diff changeset
2148 /* We don't have to do anything on the last pass (6) */
Ritor1
parents:
diff changeset
2149 if (pass < 6)
Ritor1
parents:
diff changeset
2150 {
Ritor1
parents:
diff changeset
2151 /* Each pixel depth is handled separately */
Ritor1
parents:
diff changeset
2152 switch (row_info->pixel_depth)
Ritor1
parents:
diff changeset
2153 {
Ritor1
parents:
diff changeset
2154 case 1:
Ritor1
parents:
diff changeset
2155 {
Ritor1
parents:
diff changeset
2156 png_bytep sp;
Ritor1
parents:
diff changeset
2157 png_bytep dp;
Ritor1
parents:
diff changeset
2158 int shift;
Ritor1
parents:
diff changeset
2159 int d;
Ritor1
parents:
diff changeset
2160 int value;
Ritor1
parents:
diff changeset
2161 png_uint_32 i;
Ritor1
parents:
diff changeset
2162 png_uint_32 row_width = row_info->width;
Ritor1
parents:
diff changeset
2163
Ritor1
parents:
diff changeset
2164 dp = row;
Ritor1
parents:
diff changeset
2165 d = 0;
Ritor1
parents:
diff changeset
2166 shift = 7;
Ritor1
parents:
diff changeset
2167
Ritor1
parents:
diff changeset
2168 for (i = png_pass_start[pass]; i < row_width;
Ritor1
parents:
diff changeset
2169 i += png_pass_inc[pass])
Ritor1
parents:
diff changeset
2170 {
Ritor1
parents:
diff changeset
2171 sp = row + (png_size_t)(i >> 3);
Ritor1
parents:
diff changeset
2172 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Ritor1
parents:
diff changeset
2173 d |= (value << shift);
Ritor1
parents:
diff changeset
2174
Ritor1
parents:
diff changeset
2175 if (shift == 0)
Ritor1
parents:
diff changeset
2176 {
Ritor1
parents:
diff changeset
2177 shift = 7;
Ritor1
parents:
diff changeset
2178 *dp++ = (png_byte)d;
Ritor1
parents:
diff changeset
2179 d = 0;
Ritor1
parents:
diff changeset
2180 }
Ritor1
parents:
diff changeset
2181
Ritor1
parents:
diff changeset
2182 else
Ritor1
parents:
diff changeset
2183 shift--;
Ritor1
parents:
diff changeset
2184
Ritor1
parents:
diff changeset
2185 }
Ritor1
parents:
diff changeset
2186 if (shift != 7)
Ritor1
parents:
diff changeset
2187 *dp = (png_byte)d;
Ritor1
parents:
diff changeset
2188
Ritor1
parents:
diff changeset
2189 break;
Ritor1
parents:
diff changeset
2190 }
Ritor1
parents:
diff changeset
2191
Ritor1
parents:
diff changeset
2192 case 2:
Ritor1
parents:
diff changeset
2193 {
Ritor1
parents:
diff changeset
2194 png_bytep sp;
Ritor1
parents:
diff changeset
2195 png_bytep dp;
Ritor1
parents:
diff changeset
2196 int shift;
Ritor1
parents:
diff changeset
2197 int d;
Ritor1
parents:
diff changeset
2198 int value;
Ritor1
parents:
diff changeset
2199 png_uint_32 i;
Ritor1
parents:
diff changeset
2200 png_uint_32 row_width = row_info->width;
Ritor1
parents:
diff changeset
2201
Ritor1
parents:
diff changeset
2202 dp = row;
Ritor1
parents:
diff changeset
2203 shift = 6;
Ritor1
parents:
diff changeset
2204 d = 0;
Ritor1
parents:
diff changeset
2205
Ritor1
parents:
diff changeset
2206 for (i = png_pass_start[pass]; i < row_width;
Ritor1
parents:
diff changeset
2207 i += png_pass_inc[pass])
Ritor1
parents:
diff changeset
2208 {
Ritor1
parents:
diff changeset
2209 sp = row + (png_size_t)(i >> 2);
Ritor1
parents:
diff changeset
2210 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Ritor1
parents:
diff changeset
2211 d |= (value << shift);
Ritor1
parents:
diff changeset
2212
Ritor1
parents:
diff changeset
2213 if (shift == 0)
Ritor1
parents:
diff changeset
2214 {
Ritor1
parents:
diff changeset
2215 shift = 6;
Ritor1
parents:
diff changeset
2216 *dp++ = (png_byte)d;
Ritor1
parents:
diff changeset
2217 d = 0;
Ritor1
parents:
diff changeset
2218 }
Ritor1
parents:
diff changeset
2219
Ritor1
parents:
diff changeset
2220 else
Ritor1
parents:
diff changeset
2221 shift -= 2;
Ritor1
parents:
diff changeset
2222 }
Ritor1
parents:
diff changeset
2223 if (shift != 6)
Ritor1
parents:
diff changeset
2224 *dp = (png_byte)d;
Ritor1
parents:
diff changeset
2225
Ritor1
parents:
diff changeset
2226 break;
Ritor1
parents:
diff changeset
2227 }
Ritor1
parents:
diff changeset
2228
Ritor1
parents:
diff changeset
2229 case 4:
Ritor1
parents:
diff changeset
2230 {
Ritor1
parents:
diff changeset
2231 png_bytep sp;
Ritor1
parents:
diff changeset
2232 png_bytep dp;
Ritor1
parents:
diff changeset
2233 int shift;
Ritor1
parents:
diff changeset
2234 int d;
Ritor1
parents:
diff changeset
2235 int value;
Ritor1
parents:
diff changeset
2236 png_uint_32 i;
Ritor1
parents:
diff changeset
2237 png_uint_32 row_width = row_info->width;
Ritor1
parents:
diff changeset
2238
Ritor1
parents:
diff changeset
2239 dp = row;
Ritor1
parents:
diff changeset
2240 shift = 4;
Ritor1
parents:
diff changeset
2241 d = 0;
Ritor1
parents:
diff changeset
2242 for (i = png_pass_start[pass]; i < row_width;
Ritor1
parents:
diff changeset
2243 i += png_pass_inc[pass])
Ritor1
parents:
diff changeset
2244 {
Ritor1
parents:
diff changeset
2245 sp = row + (png_size_t)(i >> 1);
Ritor1
parents:
diff changeset
2246 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Ritor1
parents:
diff changeset
2247 d |= (value << shift);
Ritor1
parents:
diff changeset
2248
Ritor1
parents:
diff changeset
2249 if (shift == 0)
Ritor1
parents:
diff changeset
2250 {
Ritor1
parents:
diff changeset
2251 shift = 4;
Ritor1
parents:
diff changeset
2252 *dp++ = (png_byte)d;
Ritor1
parents:
diff changeset
2253 d = 0;
Ritor1
parents:
diff changeset
2254 }
Ritor1
parents:
diff changeset
2255
Ritor1
parents:
diff changeset
2256 else
Ritor1
parents:
diff changeset
2257 shift -= 4;
Ritor1
parents:
diff changeset
2258 }
Ritor1
parents:
diff changeset
2259 if (shift != 4)
Ritor1
parents:
diff changeset
2260 *dp = (png_byte)d;
Ritor1
parents:
diff changeset
2261
Ritor1
parents:
diff changeset
2262 break;
Ritor1
parents:
diff changeset
2263 }
Ritor1
parents:
diff changeset
2264
Ritor1
parents:
diff changeset
2265 default:
Ritor1
parents:
diff changeset
2266 {
Ritor1
parents:
diff changeset
2267 png_bytep sp;
Ritor1
parents:
diff changeset
2268 png_bytep dp;
Ritor1
parents:
diff changeset
2269 png_uint_32 i;
Ritor1
parents:
diff changeset
2270 png_uint_32 row_width = row_info->width;
Ritor1
parents:
diff changeset
2271 png_size_t pixel_bytes;
Ritor1
parents:
diff changeset
2272
Ritor1
parents:
diff changeset
2273 /* Start at the beginning */
Ritor1
parents:
diff changeset
2274 dp = row;
Ritor1
parents:
diff changeset
2275
Ritor1
parents:
diff changeset
2276 /* Find out how many bytes each pixel takes up */
Ritor1
parents:
diff changeset
2277 pixel_bytes = (row_info->pixel_depth >> 3);
Ritor1
parents:
diff changeset
2278
Ritor1
parents:
diff changeset
2279 /* Loop through the row, only looking at the pixels that matter */
Ritor1
parents:
diff changeset
2280 for (i = png_pass_start[pass]; i < row_width;
Ritor1
parents:
diff changeset
2281 i += png_pass_inc[pass])
Ritor1
parents:
diff changeset
2282 {
Ritor1
parents:
diff changeset
2283 /* Find out where the original pixel is */
Ritor1
parents:
diff changeset
2284 sp = row + (png_size_t)i * pixel_bytes;
Ritor1
parents:
diff changeset
2285
Ritor1
parents:
diff changeset
2286 /* Move the pixel */
Ritor1
parents:
diff changeset
2287 if (dp != sp)
Ritor1
parents:
diff changeset
2288 memcpy(dp, sp, pixel_bytes);
Ritor1
parents:
diff changeset
2289
Ritor1
parents:
diff changeset
2290 /* Next pixel */
Ritor1
parents:
diff changeset
2291 dp += pixel_bytes;
Ritor1
parents:
diff changeset
2292 }
Ritor1
parents:
diff changeset
2293 break;
Ritor1
parents:
diff changeset
2294 }
Ritor1
parents:
diff changeset
2295 }
Ritor1
parents:
diff changeset
2296 /* Set new row width */
Ritor1
parents:
diff changeset
2297 row_info->width = (row_info->width +
Ritor1
parents:
diff changeset
2298 png_pass_inc[pass] - 1 -
Ritor1
parents:
diff changeset
2299 png_pass_start[pass]) /
Ritor1
parents:
diff changeset
2300 png_pass_inc[pass];
Ritor1
parents:
diff changeset
2301
Ritor1
parents:
diff changeset
2302 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
Ritor1
parents:
diff changeset
2303 row_info->width);
Ritor1
parents:
diff changeset
2304 }
Ritor1
parents:
diff changeset
2305 }
Ritor1
parents:
diff changeset
2306 #endif
Ritor1
parents:
diff changeset
2307
Ritor1
parents:
diff changeset
2308 /* This filters the row, chooses which filter to use, if it has not already
Ritor1
parents:
diff changeset
2309 * been specified by the application, and then writes the row out with the
Ritor1
parents:
diff changeset
2310 * chosen filter.
Ritor1
parents:
diff changeset
2311 */
Ritor1
parents:
diff changeset
2312 static void
Ritor1
parents:
diff changeset
2313 png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row,
Ritor1
parents:
diff changeset
2314 png_size_t row_bytes);
Ritor1
parents:
diff changeset
2315
Ritor1
parents:
diff changeset
2316 #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Ritor1
parents:
diff changeset
2317 #define PNG_HISHIFT 10
Ritor1
parents:
diff changeset
2318 #define PNG_LOMASK ((png_uint_32)0xffffL)
Ritor1
parents:
diff changeset
2319 #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Ritor1
parents:
diff changeset
2320 void /* PRIVATE */
Ritor1
parents:
diff changeset
2321 png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
Ritor1
parents:
diff changeset
2322 {
Ritor1
parents:
diff changeset
2323 png_bytep best_row;
Ritor1
parents:
diff changeset
2324 #ifdef PNG_WRITE_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2325 png_bytep prev_row, row_buf;
Ritor1
parents:
diff changeset
2326 png_uint_32 mins, bpp;
Ritor1
parents:
diff changeset
2327 png_byte filter_to_do = png_ptr->do_filter;
Ritor1
parents:
diff changeset
2328 png_size_t row_bytes = row_info->rowbytes;
Ritor1
parents:
diff changeset
2329 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2330 int num_p_filters = png_ptr->num_prev_filters;
Ritor1
parents:
diff changeset
2331 #endif
Ritor1
parents:
diff changeset
2332
Ritor1
parents:
diff changeset
2333 png_debug(1, "in png_write_find_filter");
Ritor1
parents:
diff changeset
2334
Ritor1
parents:
diff changeset
2335 #ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2336 if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
Ritor1
parents:
diff changeset
2337 {
Ritor1
parents:
diff changeset
2338 /* These will never be selected so we need not test them. */
Ritor1
parents:
diff changeset
2339 filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
Ritor1
parents:
diff changeset
2340 }
Ritor1
parents:
diff changeset
2341 #endif
Ritor1
parents:
diff changeset
2342
Ritor1
parents:
diff changeset
2343 /* Find out how many bytes offset each pixel is */
Ritor1
parents:
diff changeset
2344 bpp = (row_info->pixel_depth + 7) >> 3;
Ritor1
parents:
diff changeset
2345
Ritor1
parents:
diff changeset
2346 prev_row = png_ptr->prev_row;
Ritor1
parents:
diff changeset
2347 #endif
Ritor1
parents:
diff changeset
2348 best_row = png_ptr->row_buf;
Ritor1
parents:
diff changeset
2349 #ifdef PNG_WRITE_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2350 row_buf = best_row;
Ritor1
parents:
diff changeset
2351 mins = PNG_MAXSUM;
Ritor1
parents:
diff changeset
2352
Ritor1
parents:
diff changeset
2353 /* The prediction method we use is to find which method provides the
Ritor1
parents:
diff changeset
2354 * smallest value when summing the absolute values of the distances
Ritor1
parents:
diff changeset
2355 * from zero, using anything >= 128 as negative numbers. This is known
Ritor1
parents:
diff changeset
2356 * as the "minimum sum of absolute differences" heuristic. Other
Ritor1
parents:
diff changeset
2357 * heuristics are the "weighted minimum sum of absolute differences"
Ritor1
parents:
diff changeset
2358 * (experimental and can in theory improve compression), and the "zlib
Ritor1
parents:
diff changeset
2359 * predictive" method (not implemented yet), which does test compressions
Ritor1
parents:
diff changeset
2360 * of lines using different filter methods, and then chooses the
Ritor1
parents:
diff changeset
2361 * (series of) filter(s) that give minimum compressed data size (VERY
Ritor1
parents:
diff changeset
2362 * computationally expensive).
Ritor1
parents:
diff changeset
2363 *
Ritor1
parents:
diff changeset
2364 * GRR 980525: consider also
Ritor1
parents:
diff changeset
2365 *
Ritor1
parents:
diff changeset
2366 * (1) minimum sum of absolute differences from running average (i.e.,
Ritor1
parents:
diff changeset
2367 * keep running sum of non-absolute differences & count of bytes)
Ritor1
parents:
diff changeset
2368 * [track dispersion, too? restart average if dispersion too large?]
Ritor1
parents:
diff changeset
2369 *
Ritor1
parents:
diff changeset
2370 * (1b) minimum sum of absolute differences from sliding average, probably
Ritor1
parents:
diff changeset
2371 * with window size <= deflate window (usually 32K)
Ritor1
parents:
diff changeset
2372 *
Ritor1
parents:
diff changeset
2373 * (2) minimum sum of squared differences from zero or running average
Ritor1
parents:
diff changeset
2374 * (i.e., ~ root-mean-square approach)
Ritor1
parents:
diff changeset
2375 */
Ritor1
parents:
diff changeset
2376
Ritor1
parents:
diff changeset
2377
Ritor1
parents:
diff changeset
2378 /* We don't need to test the 'no filter' case if this is the only filter
Ritor1
parents:
diff changeset
2379 * that has been chosen, as it doesn't actually do anything to the data.
Ritor1
parents:
diff changeset
2380 */
Ritor1
parents:
diff changeset
2381 if ((filter_to_do & PNG_FILTER_NONE) && filter_to_do != PNG_FILTER_NONE)
Ritor1
parents:
diff changeset
2382 {
Ritor1
parents:
diff changeset
2383 png_bytep rp;
Ritor1
parents:
diff changeset
2384 png_uint_32 sum = 0;
Ritor1
parents:
diff changeset
2385 png_size_t i;
Ritor1
parents:
diff changeset
2386 int v;
Ritor1
parents:
diff changeset
2387
Ritor1
parents:
diff changeset
2388 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Ritor1
parents:
diff changeset
2389 {
Ritor1
parents:
diff changeset
2390 v = *rp;
Ritor1
parents:
diff changeset
2391 sum += (v < 128) ? v : 256 - v;
Ritor1
parents:
diff changeset
2392 }
Ritor1
parents:
diff changeset
2393
Ritor1
parents:
diff changeset
2394 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2395 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Ritor1
parents:
diff changeset
2396 {
Ritor1
parents:
diff changeset
2397 png_uint_32 sumhi, sumlo;
Ritor1
parents:
diff changeset
2398 int j;
Ritor1
parents:
diff changeset
2399 sumlo = sum & PNG_LOMASK;
Ritor1
parents:
diff changeset
2400 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
Ritor1
parents:
diff changeset
2401
Ritor1
parents:
diff changeset
2402 /* Reduce the sum if we match any of the previous rows */
Ritor1
parents:
diff changeset
2403 for (j = 0; j < num_p_filters; j++)
Ritor1
parents:
diff changeset
2404 {
Ritor1
parents:
diff changeset
2405 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Ritor1
parents:
diff changeset
2406 {
Ritor1
parents:
diff changeset
2407 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Ritor1
parents:
diff changeset
2408 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2409
Ritor1
parents:
diff changeset
2410 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Ritor1
parents:
diff changeset
2411 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2412 }
Ritor1
parents:
diff changeset
2413 }
Ritor1
parents:
diff changeset
2414
Ritor1
parents:
diff changeset
2415 /* Factor in the cost of this filter (this is here for completeness,
Ritor1
parents:
diff changeset
2416 * but it makes no sense to have a "cost" for the NONE filter, as
Ritor1
parents:
diff changeset
2417 * it has the minimum possible computational cost - none).
Ritor1
parents:
diff changeset
2418 */
Ritor1
parents:
diff changeset
2419 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
Ritor1
parents:
diff changeset
2420 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2421
Ritor1
parents:
diff changeset
2422 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
Ritor1
parents:
diff changeset
2423 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2424
Ritor1
parents:
diff changeset
2425 if (sumhi > PNG_HIMASK)
Ritor1
parents:
diff changeset
2426 sum = PNG_MAXSUM;
Ritor1
parents:
diff changeset
2427
Ritor1
parents:
diff changeset
2428 else
Ritor1
parents:
diff changeset
2429 sum = (sumhi << PNG_HISHIFT) + sumlo;
Ritor1
parents:
diff changeset
2430 }
Ritor1
parents:
diff changeset
2431 #endif
Ritor1
parents:
diff changeset
2432 mins = sum;
Ritor1
parents:
diff changeset
2433 }
Ritor1
parents:
diff changeset
2434
Ritor1
parents:
diff changeset
2435 /* Sub filter */
Ritor1
parents:
diff changeset
2436 if (filter_to_do == PNG_FILTER_SUB)
Ritor1
parents:
diff changeset
2437 /* It's the only filter so no testing is needed */
Ritor1
parents:
diff changeset
2438 {
Ritor1
parents:
diff changeset
2439 png_bytep rp, lp, dp;
Ritor1
parents:
diff changeset
2440 png_size_t i;
Ritor1
parents:
diff changeset
2441
Ritor1
parents:
diff changeset
2442 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
Ritor1
parents:
diff changeset
2443 i++, rp++, dp++)
Ritor1
parents:
diff changeset
2444 {
Ritor1
parents:
diff changeset
2445 *dp = *rp;
Ritor1
parents:
diff changeset
2446 }
Ritor1
parents:
diff changeset
2447
Ritor1
parents:
diff changeset
2448 for (lp = row_buf + 1; i < row_bytes;
Ritor1
parents:
diff changeset
2449 i++, rp++, lp++, dp++)
Ritor1
parents:
diff changeset
2450 {
Ritor1
parents:
diff changeset
2451 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Ritor1
parents:
diff changeset
2452 }
Ritor1
parents:
diff changeset
2453
Ritor1
parents:
diff changeset
2454 best_row = png_ptr->sub_row;
Ritor1
parents:
diff changeset
2455 }
Ritor1
parents:
diff changeset
2456
Ritor1
parents:
diff changeset
2457 else if (filter_to_do & PNG_FILTER_SUB)
Ritor1
parents:
diff changeset
2458 {
Ritor1
parents:
diff changeset
2459 png_bytep rp, dp, lp;
Ritor1
parents:
diff changeset
2460 png_uint_32 sum = 0, lmins = mins;
Ritor1
parents:
diff changeset
2461 png_size_t i;
Ritor1
parents:
diff changeset
2462 int v;
Ritor1
parents:
diff changeset
2463
Ritor1
parents:
diff changeset
2464 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2465 /* We temporarily increase the "minimum sum" by the factor we
Ritor1
parents:
diff changeset
2466 * would reduce the sum of this filter, so that we can do the
Ritor1
parents:
diff changeset
2467 * early exit comparison without scaling the sum each time.
Ritor1
parents:
diff changeset
2468 */
Ritor1
parents:
diff changeset
2469 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Ritor1
parents:
diff changeset
2470 {
Ritor1
parents:
diff changeset
2471 int j;
Ritor1
parents:
diff changeset
2472 png_uint_32 lmhi, lmlo;
Ritor1
parents:
diff changeset
2473 lmlo = lmins & PNG_LOMASK;
Ritor1
parents:
diff changeset
2474 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
Ritor1
parents:
diff changeset
2475
Ritor1
parents:
diff changeset
2476 for (j = 0; j < num_p_filters; j++)
Ritor1
parents:
diff changeset
2477 {
Ritor1
parents:
diff changeset
2478 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Ritor1
parents:
diff changeset
2479 {
Ritor1
parents:
diff changeset
2480 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Ritor1
parents:
diff changeset
2481 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2482
Ritor1
parents:
diff changeset
2483 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Ritor1
parents:
diff changeset
2484 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2485 }
Ritor1
parents:
diff changeset
2486 }
Ritor1
parents:
diff changeset
2487
Ritor1
parents:
diff changeset
2488 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Ritor1
parents:
diff changeset
2489 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2490
Ritor1
parents:
diff changeset
2491 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Ritor1
parents:
diff changeset
2492 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2493
Ritor1
parents:
diff changeset
2494 if (lmhi > PNG_HIMASK)
Ritor1
parents:
diff changeset
2495 lmins = PNG_MAXSUM;
Ritor1
parents:
diff changeset
2496
Ritor1
parents:
diff changeset
2497 else
Ritor1
parents:
diff changeset
2498 lmins = (lmhi << PNG_HISHIFT) + lmlo;
Ritor1
parents:
diff changeset
2499 }
Ritor1
parents:
diff changeset
2500 #endif
Ritor1
parents:
diff changeset
2501
Ritor1
parents:
diff changeset
2502 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
Ritor1
parents:
diff changeset
2503 i++, rp++, dp++)
Ritor1
parents:
diff changeset
2504 {
Ritor1
parents:
diff changeset
2505 v = *dp = *rp;
Ritor1
parents:
diff changeset
2506
Ritor1
parents:
diff changeset
2507 sum += (v < 128) ? v : 256 - v;
Ritor1
parents:
diff changeset
2508 }
Ritor1
parents:
diff changeset
2509
Ritor1
parents:
diff changeset
2510 for (lp = row_buf + 1; i < row_bytes;
Ritor1
parents:
diff changeset
2511 i++, rp++, lp++, dp++)
Ritor1
parents:
diff changeset
2512 {
Ritor1
parents:
diff changeset
2513 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Ritor1
parents:
diff changeset
2514
Ritor1
parents:
diff changeset
2515 sum += (v < 128) ? v : 256 - v;
Ritor1
parents:
diff changeset
2516
Ritor1
parents:
diff changeset
2517 if (sum > lmins) /* We are already worse, don't continue. */
Ritor1
parents:
diff changeset
2518 break;
Ritor1
parents:
diff changeset
2519 }
Ritor1
parents:
diff changeset
2520
Ritor1
parents:
diff changeset
2521 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2522 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Ritor1
parents:
diff changeset
2523 {
Ritor1
parents:
diff changeset
2524 int j;
Ritor1
parents:
diff changeset
2525 png_uint_32 sumhi, sumlo;
Ritor1
parents:
diff changeset
2526 sumlo = sum & PNG_LOMASK;
Ritor1
parents:
diff changeset
2527 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
Ritor1
parents:
diff changeset
2528
Ritor1
parents:
diff changeset
2529 for (j = 0; j < num_p_filters; j++)
Ritor1
parents:
diff changeset
2530 {
Ritor1
parents:
diff changeset
2531 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Ritor1
parents:
diff changeset
2532 {
Ritor1
parents:
diff changeset
2533 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Ritor1
parents:
diff changeset
2534 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2535
Ritor1
parents:
diff changeset
2536 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Ritor1
parents:
diff changeset
2537 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2538 }
Ritor1
parents:
diff changeset
2539 }
Ritor1
parents:
diff changeset
2540
Ritor1
parents:
diff changeset
2541 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Ritor1
parents:
diff changeset
2542 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2543
Ritor1
parents:
diff changeset
2544 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Ritor1
parents:
diff changeset
2545 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2546
Ritor1
parents:
diff changeset
2547 if (sumhi > PNG_HIMASK)
Ritor1
parents:
diff changeset
2548 sum = PNG_MAXSUM;
Ritor1
parents:
diff changeset
2549
Ritor1
parents:
diff changeset
2550 else
Ritor1
parents:
diff changeset
2551 sum = (sumhi << PNG_HISHIFT) + sumlo;
Ritor1
parents:
diff changeset
2552 }
Ritor1
parents:
diff changeset
2553 #endif
Ritor1
parents:
diff changeset
2554
Ritor1
parents:
diff changeset
2555 if (sum < mins)
Ritor1
parents:
diff changeset
2556 {
Ritor1
parents:
diff changeset
2557 mins = sum;
Ritor1
parents:
diff changeset
2558 best_row = png_ptr->sub_row;
Ritor1
parents:
diff changeset
2559 }
Ritor1
parents:
diff changeset
2560 }
Ritor1
parents:
diff changeset
2561
Ritor1
parents:
diff changeset
2562 /* Up filter */
Ritor1
parents:
diff changeset
2563 if (filter_to_do == PNG_FILTER_UP)
Ritor1
parents:
diff changeset
2564 {
Ritor1
parents:
diff changeset
2565 png_bytep rp, dp, pp;
Ritor1
parents:
diff changeset
2566 png_size_t i;
Ritor1
parents:
diff changeset
2567
Ritor1
parents:
diff changeset
2568 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Ritor1
parents:
diff changeset
2569 pp = prev_row + 1; i < row_bytes;
Ritor1
parents:
diff changeset
2570 i++, rp++, pp++, dp++)
Ritor1
parents:
diff changeset
2571 {
Ritor1
parents:
diff changeset
2572 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
Ritor1
parents:
diff changeset
2573 }
Ritor1
parents:
diff changeset
2574
Ritor1
parents:
diff changeset
2575 best_row = png_ptr->up_row;
Ritor1
parents:
diff changeset
2576 }
Ritor1
parents:
diff changeset
2577
Ritor1
parents:
diff changeset
2578 else if (filter_to_do & PNG_FILTER_UP)
Ritor1
parents:
diff changeset
2579 {
Ritor1
parents:
diff changeset
2580 png_bytep rp, dp, pp;
Ritor1
parents:
diff changeset
2581 png_uint_32 sum = 0, lmins = mins;
Ritor1
parents:
diff changeset
2582 png_size_t i;
Ritor1
parents:
diff changeset
2583 int v;
Ritor1
parents:
diff changeset
2584
Ritor1
parents:
diff changeset
2585
Ritor1
parents:
diff changeset
2586 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2587 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Ritor1
parents:
diff changeset
2588 {
Ritor1
parents:
diff changeset
2589 int j;
Ritor1
parents:
diff changeset
2590 png_uint_32 lmhi, lmlo;
Ritor1
parents:
diff changeset
2591 lmlo = lmins & PNG_LOMASK;
Ritor1
parents:
diff changeset
2592 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
Ritor1
parents:
diff changeset
2593
Ritor1
parents:
diff changeset
2594 for (j = 0; j < num_p_filters; j++)
Ritor1
parents:
diff changeset
2595 {
Ritor1
parents:
diff changeset
2596 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Ritor1
parents:
diff changeset
2597 {
Ritor1
parents:
diff changeset
2598 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Ritor1
parents:
diff changeset
2599 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2600
Ritor1
parents:
diff changeset
2601 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Ritor1
parents:
diff changeset
2602 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2603 }
Ritor1
parents:
diff changeset
2604 }
Ritor1
parents:
diff changeset
2605
Ritor1
parents:
diff changeset
2606 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
Ritor1
parents:
diff changeset
2607 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2608
Ritor1
parents:
diff changeset
2609 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
Ritor1
parents:
diff changeset
2610 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2611
Ritor1
parents:
diff changeset
2612 if (lmhi > PNG_HIMASK)
Ritor1
parents:
diff changeset
2613 lmins = PNG_MAXSUM;
Ritor1
parents:
diff changeset
2614
Ritor1
parents:
diff changeset
2615 else
Ritor1
parents:
diff changeset
2616 lmins = (lmhi << PNG_HISHIFT) + lmlo;
Ritor1
parents:
diff changeset
2617 }
Ritor1
parents:
diff changeset
2618 #endif
Ritor1
parents:
diff changeset
2619
Ritor1
parents:
diff changeset
2620 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Ritor1
parents:
diff changeset
2621 pp = prev_row + 1; i < row_bytes; i++)
Ritor1
parents:
diff changeset
2622 {
Ritor1
parents:
diff changeset
2623 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Ritor1
parents:
diff changeset
2624
Ritor1
parents:
diff changeset
2625 sum += (v < 128) ? v : 256 - v;
Ritor1
parents:
diff changeset
2626
Ritor1
parents:
diff changeset
2627 if (sum > lmins) /* We are already worse, don't continue. */
Ritor1
parents:
diff changeset
2628 break;
Ritor1
parents:
diff changeset
2629 }
Ritor1
parents:
diff changeset
2630
Ritor1
parents:
diff changeset
2631 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2632 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Ritor1
parents:
diff changeset
2633 {
Ritor1
parents:
diff changeset
2634 int j;
Ritor1
parents:
diff changeset
2635 png_uint_32 sumhi, sumlo;
Ritor1
parents:
diff changeset
2636 sumlo = sum & PNG_LOMASK;
Ritor1
parents:
diff changeset
2637 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
Ritor1
parents:
diff changeset
2638
Ritor1
parents:
diff changeset
2639 for (j = 0; j < num_p_filters; j++)
Ritor1
parents:
diff changeset
2640 {
Ritor1
parents:
diff changeset
2641 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Ritor1
parents:
diff changeset
2642 {
Ritor1
parents:
diff changeset
2643 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Ritor1
parents:
diff changeset
2644 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2645
Ritor1
parents:
diff changeset
2646 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Ritor1
parents:
diff changeset
2647 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2648 }
Ritor1
parents:
diff changeset
2649 }
Ritor1
parents:
diff changeset
2650
Ritor1
parents:
diff changeset
2651 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
Ritor1
parents:
diff changeset
2652 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2653
Ritor1
parents:
diff changeset
2654 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
Ritor1
parents:
diff changeset
2655 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2656
Ritor1
parents:
diff changeset
2657 if (sumhi > PNG_HIMASK)
Ritor1
parents:
diff changeset
2658 sum = PNG_MAXSUM;
Ritor1
parents:
diff changeset
2659
Ritor1
parents:
diff changeset
2660 else
Ritor1
parents:
diff changeset
2661 sum = (sumhi << PNG_HISHIFT) + sumlo;
Ritor1
parents:
diff changeset
2662 }
Ritor1
parents:
diff changeset
2663 #endif
Ritor1
parents:
diff changeset
2664
Ritor1
parents:
diff changeset
2665 if (sum < mins)
Ritor1
parents:
diff changeset
2666 {
Ritor1
parents:
diff changeset
2667 mins = sum;
Ritor1
parents:
diff changeset
2668 best_row = png_ptr->up_row;
Ritor1
parents:
diff changeset
2669 }
Ritor1
parents:
diff changeset
2670 }
Ritor1
parents:
diff changeset
2671
Ritor1
parents:
diff changeset
2672 /* Avg filter */
Ritor1
parents:
diff changeset
2673 if (filter_to_do == PNG_FILTER_AVG)
Ritor1
parents:
diff changeset
2674 {
Ritor1
parents:
diff changeset
2675 png_bytep rp, dp, pp, lp;
Ritor1
parents:
diff changeset
2676 png_uint_32 i;
Ritor1
parents:
diff changeset
2677
Ritor1
parents:
diff changeset
2678 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Ritor1
parents:
diff changeset
2679 pp = prev_row + 1; i < bpp; i++)
Ritor1
parents:
diff changeset
2680 {
Ritor1
parents:
diff changeset
2681 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Ritor1
parents:
diff changeset
2682 }
Ritor1
parents:
diff changeset
2683
Ritor1
parents:
diff changeset
2684 for (lp = row_buf + 1; i < row_bytes; i++)
Ritor1
parents:
diff changeset
2685 {
Ritor1
parents:
diff changeset
2686 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
Ritor1
parents:
diff changeset
2687 & 0xff);
Ritor1
parents:
diff changeset
2688 }
Ritor1
parents:
diff changeset
2689 best_row = png_ptr->avg_row;
Ritor1
parents:
diff changeset
2690 }
Ritor1
parents:
diff changeset
2691
Ritor1
parents:
diff changeset
2692 else if (filter_to_do & PNG_FILTER_AVG)
Ritor1
parents:
diff changeset
2693 {
Ritor1
parents:
diff changeset
2694 png_bytep rp, dp, pp, lp;
Ritor1
parents:
diff changeset
2695 png_uint_32 sum = 0, lmins = mins;
Ritor1
parents:
diff changeset
2696 png_size_t i;
Ritor1
parents:
diff changeset
2697 int v;
Ritor1
parents:
diff changeset
2698
Ritor1
parents:
diff changeset
2699 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2700 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Ritor1
parents:
diff changeset
2701 {
Ritor1
parents:
diff changeset
2702 int j;
Ritor1
parents:
diff changeset
2703 png_uint_32 lmhi, lmlo;
Ritor1
parents:
diff changeset
2704 lmlo = lmins & PNG_LOMASK;
Ritor1
parents:
diff changeset
2705 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
Ritor1
parents:
diff changeset
2706
Ritor1
parents:
diff changeset
2707 for (j = 0; j < num_p_filters; j++)
Ritor1
parents:
diff changeset
2708 {
Ritor1
parents:
diff changeset
2709 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Ritor1
parents:
diff changeset
2710 {
Ritor1
parents:
diff changeset
2711 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Ritor1
parents:
diff changeset
2712 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2713
Ritor1
parents:
diff changeset
2714 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Ritor1
parents:
diff changeset
2715 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2716 }
Ritor1
parents:
diff changeset
2717 }
Ritor1
parents:
diff changeset
2718
Ritor1
parents:
diff changeset
2719 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
Ritor1
parents:
diff changeset
2720 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2721
Ritor1
parents:
diff changeset
2722 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
Ritor1
parents:
diff changeset
2723 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2724
Ritor1
parents:
diff changeset
2725 if (lmhi > PNG_HIMASK)
Ritor1
parents:
diff changeset
2726 lmins = PNG_MAXSUM;
Ritor1
parents:
diff changeset
2727
Ritor1
parents:
diff changeset
2728 else
Ritor1
parents:
diff changeset
2729 lmins = (lmhi << PNG_HISHIFT) + lmlo;
Ritor1
parents:
diff changeset
2730 }
Ritor1
parents:
diff changeset
2731 #endif
Ritor1
parents:
diff changeset
2732
Ritor1
parents:
diff changeset
2733 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Ritor1
parents:
diff changeset
2734 pp = prev_row + 1; i < bpp; i++)
Ritor1
parents:
diff changeset
2735 {
Ritor1
parents:
diff changeset
2736 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Ritor1
parents:
diff changeset
2737
Ritor1
parents:
diff changeset
2738 sum += (v < 128) ? v : 256 - v;
Ritor1
parents:
diff changeset
2739 }
Ritor1
parents:
diff changeset
2740
Ritor1
parents:
diff changeset
2741 for (lp = row_buf + 1; i < row_bytes; i++)
Ritor1
parents:
diff changeset
2742 {
Ritor1
parents:
diff changeset
2743 v = *dp++ =
Ritor1
parents:
diff changeset
2744 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Ritor1
parents:
diff changeset
2745
Ritor1
parents:
diff changeset
2746 sum += (v < 128) ? v : 256 - v;
Ritor1
parents:
diff changeset
2747
Ritor1
parents:
diff changeset
2748 if (sum > lmins) /* We are already worse, don't continue. */
Ritor1
parents:
diff changeset
2749 break;
Ritor1
parents:
diff changeset
2750 }
Ritor1
parents:
diff changeset
2751
Ritor1
parents:
diff changeset
2752 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2753 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Ritor1
parents:
diff changeset
2754 {
Ritor1
parents:
diff changeset
2755 int j;
Ritor1
parents:
diff changeset
2756 png_uint_32 sumhi, sumlo;
Ritor1
parents:
diff changeset
2757 sumlo = sum & PNG_LOMASK;
Ritor1
parents:
diff changeset
2758 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
Ritor1
parents:
diff changeset
2759
Ritor1
parents:
diff changeset
2760 for (j = 0; j < num_p_filters; j++)
Ritor1
parents:
diff changeset
2761 {
Ritor1
parents:
diff changeset
2762 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Ritor1
parents:
diff changeset
2763 {
Ritor1
parents:
diff changeset
2764 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Ritor1
parents:
diff changeset
2765 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2766
Ritor1
parents:
diff changeset
2767 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Ritor1
parents:
diff changeset
2768 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2769 }
Ritor1
parents:
diff changeset
2770 }
Ritor1
parents:
diff changeset
2771
Ritor1
parents:
diff changeset
2772 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
Ritor1
parents:
diff changeset
2773 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2774
Ritor1
parents:
diff changeset
2775 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
Ritor1
parents:
diff changeset
2776 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2777
Ritor1
parents:
diff changeset
2778 if (sumhi > PNG_HIMASK)
Ritor1
parents:
diff changeset
2779 sum = PNG_MAXSUM;
Ritor1
parents:
diff changeset
2780
Ritor1
parents:
diff changeset
2781 else
Ritor1
parents:
diff changeset
2782 sum = (sumhi << PNG_HISHIFT) + sumlo;
Ritor1
parents:
diff changeset
2783 }
Ritor1
parents:
diff changeset
2784 #endif
Ritor1
parents:
diff changeset
2785
Ritor1
parents:
diff changeset
2786 if (sum < mins)
Ritor1
parents:
diff changeset
2787 {
Ritor1
parents:
diff changeset
2788 mins = sum;
Ritor1
parents:
diff changeset
2789 best_row = png_ptr->avg_row;
Ritor1
parents:
diff changeset
2790 }
Ritor1
parents:
diff changeset
2791 }
Ritor1
parents:
diff changeset
2792
Ritor1
parents:
diff changeset
2793 /* Paeth filter */
Ritor1
parents:
diff changeset
2794 if (filter_to_do == PNG_FILTER_PAETH)
Ritor1
parents:
diff changeset
2795 {
Ritor1
parents:
diff changeset
2796 png_bytep rp, dp, pp, cp, lp;
Ritor1
parents:
diff changeset
2797 png_size_t i;
Ritor1
parents:
diff changeset
2798
Ritor1
parents:
diff changeset
2799 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Ritor1
parents:
diff changeset
2800 pp = prev_row + 1; i < bpp; i++)
Ritor1
parents:
diff changeset
2801 {
Ritor1
parents:
diff changeset
2802 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Ritor1
parents:
diff changeset
2803 }
Ritor1
parents:
diff changeset
2804
Ritor1
parents:
diff changeset
2805 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Ritor1
parents:
diff changeset
2806 {
Ritor1
parents:
diff changeset
2807 int a, b, c, pa, pb, pc, p;
Ritor1
parents:
diff changeset
2808
Ritor1
parents:
diff changeset
2809 b = *pp++;
Ritor1
parents:
diff changeset
2810 c = *cp++;
Ritor1
parents:
diff changeset
2811 a = *lp++;
Ritor1
parents:
diff changeset
2812
Ritor1
parents:
diff changeset
2813 p = b - c;
Ritor1
parents:
diff changeset
2814 pc = a - c;
Ritor1
parents:
diff changeset
2815
Ritor1
parents:
diff changeset
2816 #ifdef PNG_USE_ABS
Ritor1
parents:
diff changeset
2817 pa = abs(p);
Ritor1
parents:
diff changeset
2818 pb = abs(pc);
Ritor1
parents:
diff changeset
2819 pc = abs(p + pc);
Ritor1
parents:
diff changeset
2820 #else
Ritor1
parents:
diff changeset
2821 pa = p < 0 ? -p : p;
Ritor1
parents:
diff changeset
2822 pb = pc < 0 ? -pc : pc;
Ritor1
parents:
diff changeset
2823 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Ritor1
parents:
diff changeset
2824 #endif
Ritor1
parents:
diff changeset
2825
Ritor1
parents:
diff changeset
2826 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
Ritor1
parents:
diff changeset
2827
Ritor1
parents:
diff changeset
2828 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Ritor1
parents:
diff changeset
2829 }
Ritor1
parents:
diff changeset
2830 best_row = png_ptr->paeth_row;
Ritor1
parents:
diff changeset
2831 }
Ritor1
parents:
diff changeset
2832
Ritor1
parents:
diff changeset
2833 else if (filter_to_do & PNG_FILTER_PAETH)
Ritor1
parents:
diff changeset
2834 {
Ritor1
parents:
diff changeset
2835 png_bytep rp, dp, pp, cp, lp;
Ritor1
parents:
diff changeset
2836 png_uint_32 sum = 0, lmins = mins;
Ritor1
parents:
diff changeset
2837 png_size_t i;
Ritor1
parents:
diff changeset
2838 int v;
Ritor1
parents:
diff changeset
2839
Ritor1
parents:
diff changeset
2840 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2841 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Ritor1
parents:
diff changeset
2842 {
Ritor1
parents:
diff changeset
2843 int j;
Ritor1
parents:
diff changeset
2844 png_uint_32 lmhi, lmlo;
Ritor1
parents:
diff changeset
2845 lmlo = lmins & PNG_LOMASK;
Ritor1
parents:
diff changeset
2846 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
Ritor1
parents:
diff changeset
2847
Ritor1
parents:
diff changeset
2848 for (j = 0; j < num_p_filters; j++)
Ritor1
parents:
diff changeset
2849 {
Ritor1
parents:
diff changeset
2850 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Ritor1
parents:
diff changeset
2851 {
Ritor1
parents:
diff changeset
2852 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Ritor1
parents:
diff changeset
2853 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2854
Ritor1
parents:
diff changeset
2855 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Ritor1
parents:
diff changeset
2856 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2857 }
Ritor1
parents:
diff changeset
2858 }
Ritor1
parents:
diff changeset
2859
Ritor1
parents:
diff changeset
2860 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Ritor1
parents:
diff changeset
2861 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2862
Ritor1
parents:
diff changeset
2863 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Ritor1
parents:
diff changeset
2864 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2865
Ritor1
parents:
diff changeset
2866 if (lmhi > PNG_HIMASK)
Ritor1
parents:
diff changeset
2867 lmins = PNG_MAXSUM;
Ritor1
parents:
diff changeset
2868
Ritor1
parents:
diff changeset
2869 else
Ritor1
parents:
diff changeset
2870 lmins = (lmhi << PNG_HISHIFT) + lmlo;
Ritor1
parents:
diff changeset
2871 }
Ritor1
parents:
diff changeset
2872 #endif
Ritor1
parents:
diff changeset
2873
Ritor1
parents:
diff changeset
2874 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Ritor1
parents:
diff changeset
2875 pp = prev_row + 1; i < bpp; i++)
Ritor1
parents:
diff changeset
2876 {
Ritor1
parents:
diff changeset
2877 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Ritor1
parents:
diff changeset
2878
Ritor1
parents:
diff changeset
2879 sum += (v < 128) ? v : 256 - v;
Ritor1
parents:
diff changeset
2880 }
Ritor1
parents:
diff changeset
2881
Ritor1
parents:
diff changeset
2882 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Ritor1
parents:
diff changeset
2883 {
Ritor1
parents:
diff changeset
2884 int a, b, c, pa, pb, pc, p;
Ritor1
parents:
diff changeset
2885
Ritor1
parents:
diff changeset
2886 b = *pp++;
Ritor1
parents:
diff changeset
2887 c = *cp++;
Ritor1
parents:
diff changeset
2888 a = *lp++;
Ritor1
parents:
diff changeset
2889
Ritor1
parents:
diff changeset
2890 #ifndef PNG_SLOW_PAETH
Ritor1
parents:
diff changeset
2891 p = b - c;
Ritor1
parents:
diff changeset
2892 pc = a - c;
Ritor1
parents:
diff changeset
2893 #ifdef PNG_USE_ABS
Ritor1
parents:
diff changeset
2894 pa = abs(p);
Ritor1
parents:
diff changeset
2895 pb = abs(pc);
Ritor1
parents:
diff changeset
2896 pc = abs(p + pc);
Ritor1
parents:
diff changeset
2897 #else
Ritor1
parents:
diff changeset
2898 pa = p < 0 ? -p : p;
Ritor1
parents:
diff changeset
2899 pb = pc < 0 ? -pc : pc;
Ritor1
parents:
diff changeset
2900 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Ritor1
parents:
diff changeset
2901 #endif
Ritor1
parents:
diff changeset
2902 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
Ritor1
parents:
diff changeset
2903 #else /* PNG_SLOW_PAETH */
Ritor1
parents:
diff changeset
2904 p = a + b - c;
Ritor1
parents:
diff changeset
2905 pa = abs(p - a);
Ritor1
parents:
diff changeset
2906 pb = abs(p - b);
Ritor1
parents:
diff changeset
2907 pc = abs(p - c);
Ritor1
parents:
diff changeset
2908
Ritor1
parents:
diff changeset
2909 if (pa <= pb && pa <= pc)
Ritor1
parents:
diff changeset
2910 p = a;
Ritor1
parents:
diff changeset
2911
Ritor1
parents:
diff changeset
2912 else if (pb <= pc)
Ritor1
parents:
diff changeset
2913 p = b;
Ritor1
parents:
diff changeset
2914
Ritor1
parents:
diff changeset
2915 else
Ritor1
parents:
diff changeset
2916 p = c;
Ritor1
parents:
diff changeset
2917 #endif /* PNG_SLOW_PAETH */
Ritor1
parents:
diff changeset
2918
Ritor1
parents:
diff changeset
2919 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Ritor1
parents:
diff changeset
2920
Ritor1
parents:
diff changeset
2921 sum += (v < 128) ? v : 256 - v;
Ritor1
parents:
diff changeset
2922
Ritor1
parents:
diff changeset
2923 if (sum > lmins) /* We are already worse, don't continue. */
Ritor1
parents:
diff changeset
2924 break;
Ritor1
parents:
diff changeset
2925 }
Ritor1
parents:
diff changeset
2926
Ritor1
parents:
diff changeset
2927 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2928 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Ritor1
parents:
diff changeset
2929 {
Ritor1
parents:
diff changeset
2930 int j;
Ritor1
parents:
diff changeset
2931 png_uint_32 sumhi, sumlo;
Ritor1
parents:
diff changeset
2932 sumlo = sum & PNG_LOMASK;
Ritor1
parents:
diff changeset
2933 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
Ritor1
parents:
diff changeset
2934
Ritor1
parents:
diff changeset
2935 for (j = 0; j < num_p_filters; j++)
Ritor1
parents:
diff changeset
2936 {
Ritor1
parents:
diff changeset
2937 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Ritor1
parents:
diff changeset
2938 {
Ritor1
parents:
diff changeset
2939 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Ritor1
parents:
diff changeset
2940 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2941
Ritor1
parents:
diff changeset
2942 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Ritor1
parents:
diff changeset
2943 PNG_WEIGHT_SHIFT;
Ritor1
parents:
diff changeset
2944 }
Ritor1
parents:
diff changeset
2945 }
Ritor1
parents:
diff changeset
2946
Ritor1
parents:
diff changeset
2947 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Ritor1
parents:
diff changeset
2948 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2949
Ritor1
parents:
diff changeset
2950 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Ritor1
parents:
diff changeset
2951 PNG_COST_SHIFT;
Ritor1
parents:
diff changeset
2952
Ritor1
parents:
diff changeset
2953 if (sumhi > PNG_HIMASK)
Ritor1
parents:
diff changeset
2954 sum = PNG_MAXSUM;
Ritor1
parents:
diff changeset
2955
Ritor1
parents:
diff changeset
2956 else
Ritor1
parents:
diff changeset
2957 sum = (sumhi << PNG_HISHIFT) + sumlo;
Ritor1
parents:
diff changeset
2958 }
Ritor1
parents:
diff changeset
2959 #endif
Ritor1
parents:
diff changeset
2960
Ritor1
parents:
diff changeset
2961 if (sum < mins)
Ritor1
parents:
diff changeset
2962 {
Ritor1
parents:
diff changeset
2963 best_row = png_ptr->paeth_row;
Ritor1
parents:
diff changeset
2964 }
Ritor1
parents:
diff changeset
2965 }
Ritor1
parents:
diff changeset
2966 #endif /* PNG_WRITE_FILTER_SUPPORTED */
Ritor1
parents:
diff changeset
2967
Ritor1
parents:
diff changeset
2968 /* Do the actual writing of the filtered row data from the chosen filter. */
Ritor1
parents:
diff changeset
2969 png_write_filtered_row(png_ptr, best_row, row_info->rowbytes+1);
Ritor1
parents:
diff changeset
2970
Ritor1
parents:
diff changeset
2971 #ifdef PNG_WRITE_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2972 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Ritor1
parents:
diff changeset
2973 /* Save the type of filter we picked this time for future calculations */
Ritor1
parents:
diff changeset
2974 if (png_ptr->num_prev_filters > 0)
Ritor1
parents:
diff changeset
2975 {
Ritor1
parents:
diff changeset
2976 int j;
Ritor1
parents:
diff changeset
2977
Ritor1
parents:
diff changeset
2978 for (j = 1; j < num_p_filters; j++)
Ritor1
parents:
diff changeset
2979 {
Ritor1
parents:
diff changeset
2980 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Ritor1
parents:
diff changeset
2981 }
Ritor1
parents:
diff changeset
2982
Ritor1
parents:
diff changeset
2983 png_ptr->prev_filters[j] = best_row[0];
Ritor1
parents:
diff changeset
2984 }
Ritor1
parents:
diff changeset
2985 #endif
Ritor1
parents:
diff changeset
2986 #endif /* PNG_WRITE_FILTER_SUPPORTED */
Ritor1
parents:
diff changeset
2987 }
Ritor1
parents:
diff changeset
2988
Ritor1
parents:
diff changeset
2989
Ritor1
parents:
diff changeset
2990 /* Do the actual writing of a previously filtered row. */
Ritor1
parents:
diff changeset
2991 static void
Ritor1
parents:
diff changeset
2992 png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row,
Ritor1
parents:
diff changeset
2993 png_size_t full_row_length/*includes filter byte*/)
Ritor1
parents:
diff changeset
2994 {
Ritor1
parents:
diff changeset
2995 png_debug(1, "in png_write_filtered_row");
Ritor1
parents:
diff changeset
2996
Ritor1
parents:
diff changeset
2997 png_debug1(2, "filter = %d", filtered_row[0]);
Ritor1
parents:
diff changeset
2998
Ritor1
parents:
diff changeset
2999 png_compress_IDAT(png_ptr, filtered_row, full_row_length, Z_NO_FLUSH);
Ritor1
parents:
diff changeset
3000
Ritor1
parents:
diff changeset
3001 /* Swap the current and previous rows */
Ritor1
parents:
diff changeset
3002 if (png_ptr->prev_row != NULL)
Ritor1
parents:
diff changeset
3003 {
Ritor1
parents:
diff changeset
3004 png_bytep tptr;
Ritor1
parents:
diff changeset
3005
Ritor1
parents:
diff changeset
3006 tptr = png_ptr->prev_row;
Ritor1
parents:
diff changeset
3007 png_ptr->prev_row = png_ptr->row_buf;
Ritor1
parents:
diff changeset
3008 png_ptr->row_buf = tptr;
Ritor1
parents:
diff changeset
3009 }
Ritor1
parents:
diff changeset
3010
Ritor1
parents:
diff changeset
3011 /* Finish row - updates counters and flushes zlib if last row */
Ritor1
parents:
diff changeset
3012 png_write_finish_row(png_ptr);
Ritor1
parents:
diff changeset
3013
Ritor1
parents:
diff changeset
3014 #ifdef PNG_WRITE_FLUSH_SUPPORTED
Ritor1
parents:
diff changeset
3015 png_ptr->flush_rows++;
Ritor1
parents:
diff changeset
3016
Ritor1
parents:
diff changeset
3017 if (png_ptr->flush_dist > 0 &&
Ritor1
parents:
diff changeset
3018 png_ptr->flush_rows >= png_ptr->flush_dist)
Ritor1
parents:
diff changeset
3019 {
Ritor1
parents:
diff changeset
3020 png_write_flush(png_ptr);
Ritor1
parents:
diff changeset
3021 }
Ritor1
parents:
diff changeset
3022 #endif
Ritor1
parents:
diff changeset
3023 }
Ritor1
parents:
diff changeset
3024 #endif /* PNG_WRITE_SUPPORTED */