OLD | NEW |
1 | 1 |
2 /* pngwutil.c - utilities to write a PNG file | 2 /* pngwutil.c - utilities to write a PNG file |
3 * | 3 * |
4 * Last changed in libpng 1.6.2 [April 25, 2013] | 4 * Last changed in libpng 1.6.19 [November 12, 2015] |
5 * Copyright (c) 1998-2013 Glenn Randers-Pehrson | 5 * Copyright (c) 1998-2015 Glenn Randers-Pehrson |
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) | 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) | 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
8 * | 8 * |
9 * This code is released under the libpng license. | 9 * This code is released under the libpng license. |
10 * For conditions of distribution and use, see the disclaimer | 10 * For conditions of distribution and use, see the disclaimer |
11 * and license in png.h | 11 * and license in png.h |
12 */ | 12 */ |
| 13 |
13 #include "pngpriv.h" | 14 #include "pngpriv.h" |
14 | 15 |
15 #ifdef PNG_WRITE_SUPPORTED | 16 #ifdef PNG_WRITE_SUPPORTED |
16 | 17 |
17 #ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED | 18 #ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED |
18 /* Place a 32-bit number into a buffer in PNG byte order. We work | 19 /* Place a 32-bit number into a buffer in PNG byte order. We work |
19 * with unsigned numbers for convenience, although one supported | 20 * with unsigned numbers for convenience, although one supported |
20 * ancillary chunk uses signed (two's complement) numbers. | 21 * ancillary chunk uses signed (two's complement) numbers. |
21 */ | 22 */ |
22 void PNGAPI | 23 void PNGAPI |
23 png_save_uint_32(png_bytep buf, png_uint_32 i) | 24 png_save_uint_32(png_bytep buf, png_uint_32 i) |
24 { | 25 { |
25 buf[0] = (png_byte)((i >> 24) & 0xff); | 26 buf[0] = (png_byte)(i >> 24); |
26 buf[1] = (png_byte)((i >> 16) & 0xff); | 27 buf[1] = (png_byte)(i >> 16); |
27 buf[2] = (png_byte)((i >> 8) & 0xff); | 28 buf[2] = (png_byte)(i >> 8); |
28 buf[3] = (png_byte)(i & 0xff); | 29 buf[3] = (png_byte)(i ); |
29 } | 30 } |
30 | 31 |
31 /* Place a 16-bit number into a buffer in PNG byte order. | 32 /* Place a 16-bit number into a buffer in PNG byte order. |
32 * The parameter is declared unsigned int, not png_uint_16, | 33 * The parameter is declared unsigned int, not png_uint_16, |
33 * just to avoid potential problems on pre-ANSI C compilers. | 34 * just to avoid potential problems on pre-ANSI C compilers. |
34 */ | 35 */ |
35 void PNGAPI | 36 void PNGAPI |
36 png_save_uint_16(png_bytep buf, unsigned int i) | 37 png_save_uint_16(png_bytep buf, unsigned int i) |
37 { | 38 { |
38 buf[0] = (png_byte)((i >> 8) & 0xff); | 39 buf[0] = (png_byte)(i >> 8); |
39 buf[1] = (png_byte)(i & 0xff); | 40 buf[1] = (png_byte)(i ); |
40 } | 41 } |
41 #endif | 42 #endif |
42 | 43 |
43 /* Simple function to write the signature. If we have already written | 44 /* Simple function to write the signature. If we have already written |
44 * the magic bytes of the signature, or more likely, the PNG stream is | 45 * the magic bytes of the signature, or more likely, the PNG stream is |
45 * being embedded into another stream and doesn't need its own signature, | 46 * being embedded into another stream and doesn't need its own signature, |
46 * we should call png_set_sig_bytes() to tell libpng how many of the | 47 * we should call png_set_sig_bytes() to tell libpng how many of the |
47 * bytes have already been written. | 48 * bytes have already been written. |
48 */ | 49 */ |
49 void PNGAPI | 50 void PNGAPI |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 { | 129 { |
129 /* Write the data, and run the CRC over it */ | 130 /* Write the data, and run the CRC over it */ |
130 if (png_ptr == NULL) | 131 if (png_ptr == NULL) |
131 return; | 132 return; |
132 | 133 |
133 if (data != NULL && length > 0) | 134 if (data != NULL && length > 0) |
134 { | 135 { |
135 png_write_data(png_ptr, data, length); | 136 png_write_data(png_ptr, data, length); |
136 | 137 |
137 /* Update the CRC after writing the data, | 138 /* Update the CRC after writing the data, |
138 * in case that the user I/O routine alters it. | 139 * in case the user I/O routine alters it. |
139 */ | 140 */ |
140 png_calculate_crc(png_ptr, data, length); | 141 png_calculate_crc(png_ptr, data, length); |
141 } | 142 } |
142 } | 143 } |
143 | 144 |
144 /* Finish a chunk started with png_write_chunk_header(). */ | 145 /* Finish a chunk started with png_write_chunk_header(). */ |
145 void PNGAPI | 146 void PNGAPI |
146 png_write_chunk_end(png_structrp png_ptr) | 147 png_write_chunk_end(png_structrp png_ptr) |
147 { | 148 { |
148 png_byte buf[4]; | 149 png_byte buf[4]; |
(...skipping 22 matching lines...) Expand all Loading... |
171 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end() | 172 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end() |
172 * functions instead. | 173 * functions instead. |
173 */ | 174 */ |
174 static void | 175 static void |
175 png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, | 176 png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, |
176 png_const_bytep data, png_size_t length) | 177 png_const_bytep data, png_size_t length) |
177 { | 178 { |
178 if (png_ptr == NULL) | 179 if (png_ptr == NULL) |
179 return; | 180 return; |
180 | 181 |
181 /* On 64 bit architectures 'length' may not fit in a png_uint_32. */ | 182 /* On 64-bit architectures 'length' may not fit in a png_uint_32. */ |
182 if (length > PNG_UINT_31_MAX) | 183 if (length > PNG_UINT_31_MAX) |
183 png_error(png_ptr, "length exceeds PNG maxima"); | 184 png_error(png_ptr, "length exceeds PNG maximum"); |
184 | 185 |
185 png_write_chunk_header(png_ptr, chunk_name, (png_uint_32)length); | 186 png_write_chunk_header(png_ptr, chunk_name, (png_uint_32)length); |
186 png_write_chunk_data(png_ptr, data, length); | 187 png_write_chunk_data(png_ptr, data, length); |
187 png_write_chunk_end(png_ptr); | 188 png_write_chunk_end(png_ptr); |
188 } | 189 } |
189 | 190 |
190 /* This is the API that calls the internal function above. */ | 191 /* This is the API that calls the internal function above. */ |
191 void PNGAPI | 192 void PNGAPI |
192 png_write_chunk(png_structrp png_ptr, png_const_bytep chunk_string, | 193 png_write_chunk(png_structrp png_ptr, png_const_bytep chunk_string, |
193 png_const_bytep data, png_size_t length) | 194 png_const_bytep data, png_size_t length) |
194 { | 195 { |
195 png_write_complete_chunk(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), data, | 196 png_write_complete_chunk(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), data, |
196 length); | 197 length); |
197 } | 198 } |
198 | 199 |
199 /* This is used below to find the size of an image to pass to png_deflate_claim, | 200 /* This is used below to find the size of an image to pass to png_deflate_claim, |
200 * so it only needs to be accurate if the size is less than 16384 bytes (the | 201 * so it only needs to be accurate if the size is less than 16384 bytes (the |
201 * point at which a lower LZ window size can be used.) | 202 * point at which a lower LZ window size can be used.) |
202 */ | 203 */ |
203 static png_alloc_size_t | 204 static png_alloc_size_t |
204 png_image_size(png_structrp png_ptr) | 205 png_image_size(png_structrp png_ptr) |
205 { | 206 { |
206 /* Only return sizes up to the maximum of a png_uint_32, do this by limiting | 207 /* Only return sizes up to the maximum of a png_uint_32; do this by limiting |
207 * the width and height used to 15 bits. | 208 * the width and height used to 15 bits. |
208 */ | 209 */ |
209 png_uint_32 h = png_ptr->height; | 210 png_uint_32 h = png_ptr->height; |
210 | 211 |
211 if (png_ptr->rowbytes < 32768 && h < 32768) | 212 if (png_ptr->rowbytes < 32768 && h < 32768) |
212 { | 213 { |
213 if (png_ptr->interlaced) | 214 if (png_ptr->interlaced != 0) |
214 { | 215 { |
215 /* Interlacing makes the image larger because of the replication of | 216 /* Interlacing makes the image larger because of the replication of |
216 * both the filter byte and the padding to a byte boundary. | 217 * both the filter byte and the padding to a byte boundary. |
217 */ | 218 */ |
218 png_uint_32 w = png_ptr->width; | 219 png_uint_32 w = png_ptr->width; |
219 unsigned int pd = png_ptr->pixel_depth; | 220 unsigned int pd = png_ptr->pixel_depth; |
220 png_alloc_size_t cb_base; | 221 png_alloc_size_t cb_base; |
221 int pass; | 222 int pass; |
222 | 223 |
223 for (cb_base=0, pass=0; pass<=6; ++pass) | 224 for (cb_base=0, pass=0; pass<=6; ++pass) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4); | 279 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4); |
279 | 280 |
280 data[0] = (png_byte)z_cmf; | 281 data[0] = (png_byte)z_cmf; |
281 tmp = data[1] & 0xe0; | 282 tmp = data[1] & 0xe0; |
282 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f; | 283 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f; |
283 data[1] = (png_byte)tmp; | 284 data[1] = (png_byte)tmp; |
284 } | 285 } |
285 } | 286 } |
286 } | 287 } |
287 } | 288 } |
288 #else | 289 #endif /* WRITE_OPTIMIZE_CMF */ |
289 # define optimize_cmf(dp,dl) ((void)0) | |
290 #endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */ | |
291 | 290 |
292 /* Initialize the compressor for the appropriate type of compression. */ | 291 /* Initialize the compressor for the appropriate type of compression. */ |
293 static int | 292 static int |
294 png_deflate_claim(png_structrp png_ptr, png_uint_32 owner, | 293 png_deflate_claim(png_structrp png_ptr, png_uint_32 owner, |
295 png_alloc_size_t data_size) | 294 png_alloc_size_t data_size) |
296 { | 295 { |
297 if (png_ptr->zowner != 0) | 296 if (png_ptr->zowner != 0) |
298 { | 297 { |
| 298 #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED) |
299 char msg[64]; | 299 char msg[64]; |
300 | 300 |
301 PNG_STRING_FROM_CHUNK(msg, owner); | 301 PNG_STRING_FROM_CHUNK(msg, owner); |
302 msg[4] = ':'; | 302 msg[4] = ':'; |
303 msg[5] = ' '; | 303 msg[5] = ' '; |
304 PNG_STRING_FROM_CHUNK(msg+6, png_ptr->zowner); | 304 PNG_STRING_FROM_CHUNK(msg+6, png_ptr->zowner); |
305 /* So the message that results is "<chunk> using zstream"; this is an | 305 /* So the message that results is "<chunk> using zstream"; this is an |
306 * internal error, but is very useful for debugging. i18n requirements | 306 * internal error, but is very useful for debugging. i18n requirements |
307 * are minimal. | 307 * are minimal. |
308 */ | 308 */ |
309 (void)png_safecat(msg, (sizeof msg), 10, " using zstream"); | 309 (void)png_safecat(msg, (sizeof msg), 10, " using zstream"); |
310 # if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC | 310 #endif |
| 311 #if PNG_RELEASE_BUILD |
311 png_warning(png_ptr, msg); | 312 png_warning(png_ptr, msg); |
312 | 313 |
313 /* Attempt sane error recovery */ | 314 /* Attempt sane error recovery */ |
314 if (png_ptr->zowner == png_IDAT) /* don't steal from IDAT */ | 315 if (png_ptr->zowner == png_IDAT) /* don't steal from IDAT */ |
315 { | 316 { |
316 png_ptr->zstream.msg = PNGZ_MSG_CAST("in use by IDAT"); | 317 png_ptr->zstream.msg = PNGZ_MSG_CAST("in use by IDAT"); |
317 return Z_STREAM_ERROR; | 318 return Z_STREAM_ERROR; |
318 } | 319 } |
319 | 320 |
320 png_ptr->zowner = 0; | 321 png_ptr->zowner = 0; |
321 # else | 322 #else |
322 png_error(png_ptr, msg); | 323 png_error(png_ptr, msg); |
323 # endif | 324 #endif |
324 } | 325 } |
325 | 326 |
326 { | 327 { |
327 int level = png_ptr->zlib_level; | 328 int level = png_ptr->zlib_level; |
328 int method = png_ptr->zlib_method; | 329 int method = png_ptr->zlib_method; |
329 int windowBits = png_ptr->zlib_window_bits; | 330 int windowBits = png_ptr->zlib_window_bits; |
330 int memLevel = png_ptr->zlib_mem_level; | 331 int memLevel = png_ptr->zlib_mem_level; |
331 int strategy; /* set below */ | 332 int strategy; /* set below */ |
332 int ret; /* zlib return code */ | 333 int ret; /* zlib return code */ |
333 | 334 |
334 if (owner == png_IDAT) | 335 if (owner == png_IDAT) |
335 { | 336 { |
336 if (png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY) | 337 if ((png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY) != 0) |
337 strategy = png_ptr->zlib_strategy; | 338 strategy = png_ptr->zlib_strategy; |
338 | 339 |
339 else if (png_ptr->do_filter != PNG_FILTER_NONE) | 340 else if (png_ptr->do_filter != PNG_FILTER_NONE) |
340 strategy = PNG_Z_DEFAULT_STRATEGY; | 341 strategy = PNG_Z_DEFAULT_STRATEGY; |
341 | 342 |
342 else | 343 else |
343 strategy = PNG_Z_DEFAULT_NOFILTER_STRATEGY; | 344 strategy = PNG_Z_DEFAULT_NOFILTER_STRATEGY; |
344 } | 345 } |
345 | 346 |
346 else | 347 else |
347 { | 348 { |
348 # ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED | 349 #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED |
349 level = png_ptr->zlib_text_level; | 350 level = png_ptr->zlib_text_level; |
350 method = png_ptr->zlib_text_method; | 351 method = png_ptr->zlib_text_method; |
351 windowBits = png_ptr->zlib_text_window_bits; | 352 windowBits = png_ptr->zlib_text_window_bits; |
352 memLevel = png_ptr->zlib_text_mem_level; | 353 memLevel = png_ptr->zlib_text_mem_level; |
353 strategy = png_ptr->zlib_text_strategy; | 354 strategy = png_ptr->zlib_text_strategy; |
354 # else | 355 #else |
355 /* If customization is not supported the values all come from the | 356 /* If customization is not supported the values all come from the |
356 * IDAT values except for the strategy, which is fixed to the | 357 * IDAT values except for the strategy, which is fixed to the |
357 * default. (This is the pre-1.6.0 behavior too, although it was | 358 * default. (This is the pre-1.6.0 behavior too, although it was |
358 * implemented in a very different way.) | 359 * implemented in a very different way.) |
359 */ | 360 */ |
360 strategy = Z_DEFAULT_STRATEGY; | 361 strategy = Z_DEFAULT_STRATEGY; |
361 # endif | 362 #endif |
362 } | 363 } |
363 | 364 |
364 /* Adjust 'windowBits' down if larger than 'data_size'; to stop this | 365 /* Adjust 'windowBits' down if larger than 'data_size'; to stop this |
365 * happening just pass 32768 as the data_size parameter. Notice that zlib | 366 * happening just pass 32768 as the data_size parameter. Notice that zlib |
366 * requires an extra 262 bytes in the window in addition to the data to be | 367 * requires an extra 262 bytes in the window in addition to the data to be |
367 * able to see the whole of the data, so if data_size+262 takes us to the | 368 * able to see the whole of the data, so if data_size+262 takes us to the |
368 * next windowBits size we need to fix up the value later. (Because even | 369 * next windowBits size we need to fix up the value later. (Because even |
369 * though deflate needs the extra window, inflate does not!) | 370 * though deflate needs the extra window, inflate does not!) |
370 */ | 371 */ |
371 if (data_size <= 16384) | 372 if (data_size <= 16384) |
372 { | 373 { |
373 /* IMPLEMENTATION NOTE: this 'half_window_size' stuff is only here to | 374 /* IMPLEMENTATION NOTE: this 'half_window_size' stuff is only here to |
374 * work round a Microsoft Visual C misbehavior which, contrary to C-90, | 375 * work round a Microsoft Visual C misbehavior which, contrary to C-90, |
375 * widens the result of the following shift to 64-bits if (and, | 376 * widens the result of the following shift to 64-bits if (and, |
376 * apparently, only if) it is used in a test. | 377 * apparently, only if) it is used in a test. |
377 */ | 378 */ |
378 unsigned int half_window_size = 1U << (windowBits-1); | 379 unsigned int half_window_size = 1U << (windowBits-1); |
379 | 380 |
380 while (data_size + 262 <= half_window_size) | 381 while (data_size + 262 <= half_window_size) |
381 { | 382 { |
382 half_window_size >>= 1; | 383 half_window_size >>= 1; |
383 --windowBits; | 384 --windowBits; |
384 } | 385 } |
385 } | 386 } |
386 | 387 |
387 /* Check against the previous initialized values, if any. */ | 388 /* Check against the previous initialized values, if any. */ |
388 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) && | 389 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0 && |
389 (png_ptr->zlib_set_level != level || | 390 (png_ptr->zlib_set_level != level || |
390 png_ptr->zlib_set_method != method || | 391 png_ptr->zlib_set_method != method || |
391 png_ptr->zlib_set_window_bits != windowBits || | 392 png_ptr->zlib_set_window_bits != windowBits || |
392 png_ptr->zlib_set_mem_level != memLevel || | 393 png_ptr->zlib_set_mem_level != memLevel || |
393 png_ptr->zlib_set_strategy != strategy)) | 394 png_ptr->zlib_set_strategy != strategy)) |
394 { | 395 { |
395 if (deflateEnd(&png_ptr->zstream) != Z_OK) | 396 if (deflateEnd(&png_ptr->zstream) != Z_OK) |
396 png_warning(png_ptr, "deflateEnd failed (ignored)"); | 397 png_warning(png_ptr, "deflateEnd failed (ignored)"); |
397 | 398 |
398 png_ptr->flags &= ~PNG_FLAG_ZSTREAM_INITIALIZED; | 399 png_ptr->flags &= ~PNG_FLAG_ZSTREAM_INITIALIZED; |
399 } | 400 } |
400 | 401 |
401 /* For safety clear out the input and output pointers (currently zlib | 402 /* For safety clear out the input and output pointers (currently zlib |
402 * doesn't use them on Init, but it might in the future). | 403 * doesn't use them on Init, but it might in the future). |
403 */ | 404 */ |
404 png_ptr->zstream.next_in = NULL; | 405 png_ptr->zstream.next_in = NULL; |
405 png_ptr->zstream.avail_in = 0; | 406 png_ptr->zstream.avail_in = 0; |
406 png_ptr->zstream.next_out = NULL; | 407 png_ptr->zstream.next_out = NULL; |
407 png_ptr->zstream.avail_out = 0; | 408 png_ptr->zstream.avail_out = 0; |
408 | 409 |
409 /* Now initialize if required, setting the new parameters, otherwise just | 410 /* Now initialize if required, setting the new parameters, otherwise just |
410 * to a simple reset to the previous parameters. | 411 * to a simple reset to the previous parameters. |
411 */ | 412 */ |
412 if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) | 413 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0) |
413 ret = deflateReset(&png_ptr->zstream); | 414 ret = deflateReset(&png_ptr->zstream); |
414 | 415 |
415 else | 416 else |
416 { | 417 { |
417 ret = deflateInit2(&png_ptr->zstream, level, method, windowBits, | 418 ret = deflateInit2(&png_ptr->zstream, level, method, windowBits, |
418 memLevel, strategy); | 419 memLevel, strategy); |
419 | 420 |
420 if (ret == Z_OK) | 421 if (ret == Z_OK) |
421 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED; | 422 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED; |
422 } | 423 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 } | 485 } |
485 | 486 |
486 /* Compress the data in the compression state input */ | 487 /* Compress the data in the compression state input */ |
487 static int | 488 static int |
488 png_text_compress(png_structrp png_ptr, png_uint_32 chunk_name, | 489 png_text_compress(png_structrp png_ptr, png_uint_32 chunk_name, |
489 compression_state *comp, png_uint_32 prefix_len) | 490 compression_state *comp, png_uint_32 prefix_len) |
490 { | 491 { |
491 int ret; | 492 int ret; |
492 | 493 |
493 /* To find the length of the output it is necessary to first compress the | 494 /* To find the length of the output it is necessary to first compress the |
494 * input, the result is buffered rather than using the two-pass algorithm | 495 * input. The result is buffered rather than using the two-pass algorithm |
495 * that is used on the inflate side; deflate is assumed to be slower and a | 496 * that is used on the inflate side; deflate is assumed to be slower and a |
496 * PNG writer is assumed to have more memory available than a PNG reader. | 497 * PNG writer is assumed to have more memory available than a PNG reader. |
497 * | 498 * |
498 * IMPLEMENTATION NOTE: the zlib API deflateBound() can be used to find an | 499 * IMPLEMENTATION NOTE: the zlib API deflateBound() can be used to find an |
499 * upper limit on the output size, but it is always bigger than the input | 500 * upper limit on the output size, but it is always bigger than the input |
500 * size so it is likely to be more efficient to use this linked-list | 501 * size so it is likely to be more efficient to use this linked-list |
501 * approach. | 502 * approach. |
502 */ | 503 */ |
503 ret = png_deflate_claim(png_ptr, chunk_name, comp->input_len); | 504 ret = png_deflate_claim(png_ptr, chunk_name, comp->input_len); |
504 | 505 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
581 input_len > 0 ? Z_NO_FLUSH : Z_FINISH); | 582 input_len > 0 ? Z_NO_FLUSH : Z_FINISH); |
582 | 583 |
583 /* Claw back input data that was not consumed (because avail_in is | 584 /* Claw back input data that was not consumed (because avail_in is |
584 * reset above every time round the loop). | 585 * reset above every time round the loop). |
585 */ | 586 */ |
586 input_len += png_ptr->zstream.avail_in; | 587 input_len += png_ptr->zstream.avail_in; |
587 png_ptr->zstream.avail_in = 0; /* safety */ | 588 png_ptr->zstream.avail_in = 0; /* safety */ |
588 } | 589 } |
589 while (ret == Z_OK); | 590 while (ret == Z_OK); |
590 | 591 |
591 /* There may be some space left in the last output buffer, this needs to | 592 /* There may be some space left in the last output buffer. This needs to |
592 * be subtracted from output_len. | 593 * be subtracted from output_len. |
593 */ | 594 */ |
594 output_len -= png_ptr->zstream.avail_out; | 595 output_len -= png_ptr->zstream.avail_out; |
595 png_ptr->zstream.avail_out = 0; /* safety */ | 596 png_ptr->zstream.avail_out = 0; /* safety */ |
596 comp->output_len = output_len; | 597 comp->output_len = output_len; |
597 | 598 |
598 /* Now double check the output length, put in a custom message if it is | 599 /* Now double check the output length, put in a custom message if it is |
599 * too long. Otherwise ensure the z_stream::msg pointer is set to | 600 * too long. Otherwise ensure the z_stream::msg pointer is set to |
600 * something. | 601 * something. |
601 */ | 602 */ |
602 if (output_len + prefix_len >= PNG_UINT_31_MAX) | 603 if (output_len + prefix_len >= PNG_UINT_31_MAX) |
603 { | 604 { |
604 png_ptr->zstream.msg = PNGZ_MSG_CAST("compressed data too long"); | 605 png_ptr->zstream.msg = PNGZ_MSG_CAST("compressed data too long"); |
605 ret = Z_MEM_ERROR; | 606 ret = Z_MEM_ERROR; |
606 } | 607 } |
607 | 608 |
608 else | 609 else |
609 png_zstream_error(png_ptr, ret); | 610 png_zstream_error(png_ptr, ret); |
610 | 611 |
611 /* Reset zlib for another zTXt/iTXt or image data */ | 612 /* Reset zlib for another zTXt/iTXt or image data */ |
612 png_ptr->zowner = 0; | 613 png_ptr->zowner = 0; |
613 | 614 |
614 /* The only success case is Z_STREAM_END, input_len must be 0, if not this | 615 /* The only success case is Z_STREAM_END, input_len must be 0; if not this |
615 * is an internal error. | 616 * is an internal error. |
616 */ | 617 */ |
617 if (ret == Z_STREAM_END && input_len == 0) | 618 if (ret == Z_STREAM_END && input_len == 0) |
618 { | 619 { |
| 620 #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED |
619 /* Fix up the deflate header, if required */ | 621 /* Fix up the deflate header, if required */ |
620 optimize_cmf(comp->output, comp->input_len); | 622 optimize_cmf(comp->output, comp->input_len); |
621 | 623 #endif |
622 /* But Z_OK is returned, not Z_STREAM_END; this allows the claim | 624 /* But Z_OK is returned, not Z_STREAM_END; this allows the claim |
623 * function above to return Z_STREAM_END on an error (though it never | 625 * function above to return Z_STREAM_END on an error (though it never |
624 * does in the current versions of zlib.) | 626 * does in the current versions of zlib.) |
625 */ | 627 */ |
626 return Z_OK; | 628 return Z_OK; |
627 } | 629 } |
628 | 630 |
629 else | 631 else |
630 return ret; | 632 return ret; |
631 } | 633 } |
(...skipping 22 matching lines...) Expand all Loading... |
654 | 656 |
655 avail = png_ptr->zbuffer_size; | 657 avail = png_ptr->zbuffer_size; |
656 output = next->output; | 658 output = next->output; |
657 next = next->next; | 659 next = next->next; |
658 } | 660 } |
659 | 661 |
660 /* This is an internal error; 'next' must have been NULL! */ | 662 /* This is an internal error; 'next' must have been NULL! */ |
661 if (output_len > 0) | 663 if (output_len > 0) |
662 png_error(png_ptr, "error writing ancillary chunked compressed data"); | 664 png_error(png_ptr, "error writing ancillary chunked compressed data"); |
663 } | 665 } |
664 #endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */ | 666 #endif /* WRITE_COMPRESSED_TEXT */ |
665 | 667 |
666 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ | 668 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ |
667 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) | 669 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) |
668 /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, | 670 /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, |
669 * and if invalid, correct the keyword rather than discarding the entire | 671 * and if invalid, correct the keyword rather than discarding the entire |
670 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in | 672 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in |
671 * length, forbids leading or trailing whitespace, multiple internal spaces, | 673 * length, forbids leading or trailing whitespace, multiple internal spaces, |
672 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. | 674 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. |
673 * | 675 * |
674 * The 'new_key' buffer must be 80 characters in size (for the keyword plus a | 676 * The 'new_key' buffer must be 80 characters in size (for the keyword plus a |
(...skipping 11 matching lines...) Expand all Loading... |
686 png_debug(1, "in png_check_keyword"); | 688 png_debug(1, "in png_check_keyword"); |
687 | 689 |
688 if (key == NULL) | 690 if (key == NULL) |
689 { | 691 { |
690 *new_key = 0; | 692 *new_key = 0; |
691 return 0; | 693 return 0; |
692 } | 694 } |
693 | 695 |
694 while (*key && key_len < 79) | 696 while (*key && key_len < 79) |
695 { | 697 { |
696 png_byte ch = (png_byte)(0xff & *key++); | 698 png_byte ch = (png_byte)*key++; |
697 | 699 |
698 if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/)) | 700 if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/)) |
699 *new_key++ = ch, ++key_len, space = 0; | 701 *new_key++ = ch, ++key_len, space = 0; |
700 | 702 |
701 else if (!space) | 703 else if (space == 0) |
702 { | 704 { |
703 /* A space or an invalid character when one wasn't seen immediately | 705 /* A space or an invalid character when one wasn't seen immediately |
704 * before; output just a space. | 706 * before; output just a space. |
705 */ | 707 */ |
706 *new_key++ = 32, ++key_len, space = 1; | 708 *new_key++ = 32, ++key_len, space = 1; |
707 | 709 |
708 /* If the character was not a space then it is invalid. */ | 710 /* If the character was not a space then it is invalid. */ |
709 if (ch != 32) | 711 if (ch != 32) |
710 bad_character = ch; | 712 bad_character = ch; |
711 } | 713 } |
712 | 714 |
713 else if (!bad_character) | 715 else if (bad_character == 0) |
714 bad_character = ch; /* just skip it, record the first error */ | 716 bad_character = ch; /* just skip it, record the first error */ |
715 } | 717 } |
716 | 718 |
717 if (key_len > 0 && space) /* trailing space */ | 719 if (key_len > 0 && space != 0) /* trailing space */ |
718 { | 720 { |
719 --key_len, --new_key; | 721 --key_len, --new_key; |
720 if (!bad_character) | 722 if (bad_character == 0) |
721 bad_character = 32; | 723 bad_character = 32; |
722 } | 724 } |
723 | 725 |
724 /* Terminate the keyword */ | 726 /* Terminate the keyword */ |
725 *new_key = 0; | 727 *new_key = 0; |
726 | 728 |
727 if (key_len == 0) | 729 if (key_len == 0) |
728 return 0; | 730 return 0; |
729 | 731 |
| 732 #ifdef PNG_WARNINGS_SUPPORTED |
730 /* Try to only output one warning per keyword: */ | 733 /* Try to only output one warning per keyword: */ |
731 if (*key) /* keyword too long */ | 734 if (*key != 0) /* keyword too long */ |
732 png_warning(png_ptr, "keyword truncated"); | 735 png_warning(png_ptr, "keyword truncated"); |
733 | 736 |
734 else if (bad_character) | 737 else if (bad_character != 0) |
735 { | 738 { |
736 PNG_WARNING_PARAMETERS(p) | 739 PNG_WARNING_PARAMETERS(p) |
737 | 740 |
738 png_warning_parameter(p, 1, orig_key); | 741 png_warning_parameter(p, 1, orig_key); |
739 png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character); | 742 png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character); |
740 | 743 |
741 png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'"); | 744 png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'"); |
742 } | 745 } |
| 746 #endif /* WARNINGS */ |
743 | 747 |
744 return key_len; | 748 return key_len; |
745 } | 749 } |
746 #endif | 750 #endif /* WRITE_TEXT || WRITE_pCAL || WRITE_iCCP || WRITE_sPLT */ |
747 | 751 |
748 /* Write the IHDR chunk, and update the png_struct with the necessary | 752 /* Write the IHDR chunk, and update the png_struct with the necessary |
749 * information. Note that the rest of this code depends upon this | 753 * information. Note that the rest of this code depends upon this |
750 * information being correct. | 754 * information being correct. |
751 */ | 755 */ |
752 void /* PRIVATE */ | 756 void /* PRIVATE */ |
753 png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height, | 757 png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height, |
754 int bit_depth, int color_type, int compression_type, int filter_type, | 758 int bit_depth, int color_type, int compression_type, int filter_type, |
755 int interlace_type) | 759 int interlace_type) |
756 { | 760 { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
837 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and | 841 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and |
838 * 2. Libpng did not write a PNG signature (this filter_method is only | 842 * 2. Libpng did not write a PNG signature (this filter_method is only |
839 * used in PNG datastreams that are embedded in MNG datastreams) and | 843 * used in PNG datastreams that are embedded in MNG datastreams) and |
840 * 3. The application called png_permit_mng_features with a mask that | 844 * 3. The application called png_permit_mng_features with a mask that |
841 * included PNG_FLAG_MNG_FILTER_64 and | 845 * included PNG_FLAG_MNG_FILTER_64 and |
842 * 4. The filter_method is 64 and | 846 * 4. The filter_method is 64 and |
843 * 5. The color_type is RGB or RGBA | 847 * 5. The color_type is RGB or RGBA |
844 */ | 848 */ |
845 if ( | 849 if ( |
846 #ifdef PNG_MNG_FEATURES_SUPPORTED | 850 #ifdef PNG_MNG_FEATURES_SUPPORTED |
847 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && | 851 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && |
848 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) && | 852 ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) && |
849 (color_type == PNG_COLOR_TYPE_RGB || | 853 (color_type == PNG_COLOR_TYPE_RGB || |
850 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && | 854 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && |
851 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) && | 855 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) && |
852 #endif | 856 #endif |
853 filter_type != PNG_FILTER_TYPE_BASE) | 857 filter_type != PNG_FILTER_TYPE_BASE) |
854 { | 858 { |
855 png_warning(png_ptr, "Invalid filter type specified"); | 859 png_warning(png_ptr, "Invalid filter type specified"); |
856 filter_type = PNG_FILTER_TYPE_BASE; | 860 filter_type = PNG_FILTER_TYPE_BASE; |
857 } | 861 } |
858 | 862 |
859 #ifdef PNG_WRITE_INTERLACING_SUPPORTED | 863 #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
860 if (interlace_type != PNG_INTERLACE_NONE && | 864 if (interlace_type != PNG_INTERLACE_NONE && |
861 interlace_type != PNG_INTERLACE_ADAM7) | 865 interlace_type != PNG_INTERLACE_ADAM7) |
862 { | 866 { |
863 png_warning(png_ptr, "Invalid interlace type specified"); | 867 png_warning(png_ptr, "Invalid interlace type specified"); |
864 interlace_type = PNG_INTERLACE_ADAM7; | 868 interlace_type = PNG_INTERLACE_ADAM7; |
865 } | 869 } |
866 #else | 870 #else |
867 interlace_type=PNG_INTERLACE_NONE; | 871 interlace_type=PNG_INTERLACE_NONE; |
868 #endif | 872 #endif |
869 | 873 |
870 /* Save the relevent information */ | 874 /* Save the relevant information */ |
871 png_ptr->bit_depth = (png_byte)bit_depth; | 875 png_ptr->bit_depth = (png_byte)bit_depth; |
872 png_ptr->color_type = (png_byte)color_type; | 876 png_ptr->color_type = (png_byte)color_type; |
873 png_ptr->interlaced = (png_byte)interlace_type; | 877 png_ptr->interlaced = (png_byte)interlace_type; |
874 #ifdef PNG_MNG_FEATURES_SUPPORTED | 878 #ifdef PNG_MNG_FEATURES_SUPPORTED |
875 png_ptr->filter_type = (png_byte)filter_type; | 879 png_ptr->filter_type = (png_byte)filter_type; |
876 #endif | 880 #endif |
877 png_ptr->compression_type = (png_byte)compression_type; | 881 png_ptr->compression_type = (png_byte)compression_type; |
878 png_ptr->width = width; | 882 png_ptr->width = width; |
879 png_ptr->height = height; | 883 png_ptr->height = height; |
880 | 884 |
881 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels); | 885 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels); |
882 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width); | 886 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width); |
883 /* Set the usr info, so any transformations can modify it */ | 887 /* Set the usr info, so any transformations can modify it */ |
884 png_ptr->usr_width = png_ptr->width; | 888 png_ptr->usr_width = png_ptr->width; |
885 png_ptr->usr_bit_depth = png_ptr->bit_depth; | 889 png_ptr->usr_bit_depth = png_ptr->bit_depth; |
886 png_ptr->usr_channels = png_ptr->channels; | 890 png_ptr->usr_channels = png_ptr->channels; |
887 | 891 |
888 /* Pack the header information into the buffer */ | 892 /* Pack the header information into the buffer */ |
889 png_save_uint_32(buf, width); | 893 png_save_uint_32(buf, width); |
890 png_save_uint_32(buf + 4, height); | 894 png_save_uint_32(buf + 4, height); |
891 buf[8] = (png_byte)bit_depth; | 895 buf[8] = (png_byte)bit_depth; |
892 buf[9] = (png_byte)color_type; | 896 buf[9] = (png_byte)color_type; |
893 buf[10] = (png_byte)compression_type; | 897 buf[10] = (png_byte)compression_type; |
894 buf[11] = (png_byte)filter_type; | 898 buf[11] = (png_byte)filter_type; |
895 buf[12] = (png_byte)interlace_type; | 899 buf[12] = (png_byte)interlace_type; |
896 | 900 |
897 /* Write the chunk */ | 901 /* Write the chunk */ |
898 png_write_complete_chunk(png_ptr, png_IHDR, buf, (png_size_t)13); | 902 png_write_complete_chunk(png_ptr, png_IHDR, buf, (png_size_t)13); |
899 | 903 |
900 if (!(png_ptr->do_filter)) | 904 if ((png_ptr->do_filter) == PNG_NO_FILTERS) |
901 { | 905 { |
902 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE || | 906 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE || |
903 png_ptr->bit_depth < 8) | 907 png_ptr->bit_depth < 8) |
904 png_ptr->do_filter = PNG_FILTER_NONE; | 908 png_ptr->do_filter = PNG_FILTER_NONE; |
905 | 909 |
906 else | 910 else |
907 png_ptr->do_filter = PNG_ALL_FILTERS; | 911 png_ptr->do_filter = PNG_ALL_FILTERS; |
908 } | 912 } |
909 | 913 |
910 png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */ | 914 png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */ |
911 } | 915 } |
912 | 916 |
913 /* Write the palette. We are careful not to trust png_color to be in the | 917 /* Write the palette. We are careful not to trust png_color to be in the |
914 * correct order for PNG, so people can redefine it to any convenient | 918 * correct order for PNG, so people can redefine it to any convenient |
915 * structure. | 919 * structure. |
916 */ | 920 */ |
917 void /* PRIVATE */ | 921 void /* PRIVATE */ |
918 png_write_PLTE(png_structrp png_ptr, png_const_colorp palette, | 922 png_write_PLTE(png_structrp png_ptr, png_const_colorp palette, |
919 png_uint_32 num_pal) | 923 png_uint_32 num_pal) |
920 { | 924 { |
921 png_uint_32 i; | 925 png_uint_32 max_palette_length, i; |
922 png_const_colorp pal_ptr; | 926 png_const_colorp pal_ptr; |
923 png_byte buf[3]; | 927 png_byte buf[3]; |
924 | 928 |
925 png_debug(1, "in png_write_PLTE"); | 929 png_debug(1, "in png_write_PLTE"); |
926 | 930 |
| 931 max_palette_length = (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ? |
| 932 (1 << png_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH; |
| 933 |
927 if (( | 934 if (( |
928 #ifdef PNG_MNG_FEATURES_SUPPORTED | 935 #ifdef PNG_MNG_FEATURES_SUPPORTED |
929 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) && | 936 (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0 && |
930 #endif | 937 #endif |
931 num_pal == 0) || num_pal > 256) | 938 num_pal == 0) || num_pal > max_palette_length) |
932 { | 939 { |
933 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) | 940 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
934 { | 941 { |
935 png_error(png_ptr, "Invalid number of colors in palette"); | 942 png_error(png_ptr, "Invalid number of colors in palette"); |
936 } | 943 } |
937 | 944 |
938 else | 945 else |
939 { | 946 { |
940 png_warning(png_ptr, "Invalid number of colors in palette"); | 947 png_warning(png_ptr, "Invalid number of colors in palette"); |
941 return; | 948 return; |
942 } | 949 } |
943 } | 950 } |
944 | 951 |
945 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR)) | 952 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) |
946 { | 953 { |
947 png_warning(png_ptr, | 954 png_warning(png_ptr, |
948 "Ignoring request to write a PLTE chunk in grayscale PNG"); | 955 "Ignoring request to write a PLTE chunk in grayscale PNG"); |
949 | 956 |
950 return; | 957 return; |
951 } | 958 } |
952 | 959 |
953 png_ptr->num_palette = (png_uint_16)num_pal; | 960 png_ptr->num_palette = (png_uint_16)num_pal; |
954 png_debug1(3, "num_palette = %d", png_ptr->num_palette); | 961 png_debug1(3, "num_palette = %d", png_ptr->num_palette); |
955 | 962 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
990 * reporting and does not return an error code. In the event of error it will | 997 * reporting and does not return an error code. In the event of error it will |
991 * just call png_error. The input data length may exceed 32-bits. The 'flush' | 998 * just call png_error. The input data length may exceed 32-bits. The 'flush' |
992 * parameter is exactly the same as that to deflate, with the following | 999 * parameter is exactly the same as that to deflate, with the following |
993 * meanings: | 1000 * meanings: |
994 * | 1001 * |
995 * Z_NO_FLUSH: normal incremental output of compressed data | 1002 * Z_NO_FLUSH: normal incremental output of compressed data |
996 * Z_SYNC_FLUSH: do a SYNC_FLUSH, used by png_write_flush | 1003 * Z_SYNC_FLUSH: do a SYNC_FLUSH, used by png_write_flush |
997 * Z_FINISH: this is the end of the input, do a Z_FINISH and clean up | 1004 * Z_FINISH: this is the end of the input, do a Z_FINISH and clean up |
998 * | 1005 * |
999 * The routine manages the acquire and release of the png_ptr->zstream by | 1006 * The routine manages the acquire and release of the png_ptr->zstream by |
1000 * checking and (at the end) clearing png_ptr->zowner, it does some sanity | 1007 * checking and (at the end) clearing png_ptr->zowner; it does some sanity |
1001 * checks on the 'mode' flags while doing this. | 1008 * checks on the 'mode' flags while doing this. |
1002 */ | 1009 */ |
1003 void /* PRIVATE */ | 1010 void /* PRIVATE */ |
1004 png_compress_IDAT(png_structrp png_ptr, png_const_bytep input, | 1011 png_compress_IDAT(png_structrp png_ptr, png_const_bytep input, |
1005 png_alloc_size_t input_len, int flush) | 1012 png_alloc_size_t input_len, int flush) |
1006 { | 1013 { |
1007 if (png_ptr->zowner != png_IDAT) | 1014 if (png_ptr->zowner != png_IDAT) |
1008 { | 1015 { |
1009 /* First time. Ensure we have a temporary buffer for compression and | 1016 /* First time. Ensure we have a temporary buffer for compression and |
1010 * trim the buffer list if it has more than one entry to free memory. | 1017 * trim the buffer list if it has more than one entry to free memory. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1050 | 1057 |
1051 png_ptr->zstream.avail_in = avail; | 1058 png_ptr->zstream.avail_in = avail; |
1052 input_len -= avail; | 1059 input_len -= avail; |
1053 | 1060 |
1054 ret = deflate(&png_ptr->zstream, input_len > 0 ? Z_NO_FLUSH : flush); | 1061 ret = deflate(&png_ptr->zstream, input_len > 0 ? Z_NO_FLUSH : flush); |
1055 | 1062 |
1056 /* Include as-yet unconsumed input */ | 1063 /* Include as-yet unconsumed input */ |
1057 input_len += png_ptr->zstream.avail_in; | 1064 input_len += png_ptr->zstream.avail_in; |
1058 png_ptr->zstream.avail_in = 0; | 1065 png_ptr->zstream.avail_in = 0; |
1059 | 1066 |
1060 /* OUTPUT: write complete IDAT chunks when avail_out drops to zero, note | 1067 /* OUTPUT: write complete IDAT chunks when avail_out drops to zero. Note |
1061 * that these two zstream fields are preserved across the calls, therefore | 1068 * that these two zstream fields are preserved across the calls, therefore |
1062 * there is no need to set these up on entry to the loop. | 1069 * there is no need to set these up on entry to the loop. |
1063 */ | 1070 */ |
1064 if (png_ptr->zstream.avail_out == 0) | 1071 if (png_ptr->zstream.avail_out == 0) |
1065 { | 1072 { |
1066 png_bytep data = png_ptr->zbuffer_list->output; | 1073 png_bytep data = png_ptr->zbuffer_list->output; |
1067 uInt size = png_ptr->zbuffer_size; | 1074 uInt size = png_ptr->zbuffer_size; |
1068 | 1075 |
1069 /* Write an IDAT containing the data then reset the buffer. The | 1076 /* Write an IDAT containing the data then reset the buffer. The |
1070 * first IDAT may need deflate header optimization. | 1077 * first IDAT may need deflate header optimization. |
1071 */ | 1078 */ |
1072 # ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED | 1079 #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED |
1073 if (!(png_ptr->mode & PNG_HAVE_IDAT) && | 1080 if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 && |
1074 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) | 1081 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) |
1075 optimize_cmf(data, png_image_size(png_ptr)); | 1082 optimize_cmf(data, png_image_size(png_ptr)); |
1076 # endif | 1083 #endif |
1077 | 1084 |
1078 png_write_complete_chunk(png_ptr, png_IDAT, data, size); | 1085 png_write_complete_chunk(png_ptr, png_IDAT, data, size); |
1079 png_ptr->mode |= PNG_HAVE_IDAT; | 1086 png_ptr->mode |= PNG_HAVE_IDAT; |
1080 | 1087 |
1081 png_ptr->zstream.next_out = data; | 1088 png_ptr->zstream.next_out = data; |
1082 png_ptr->zstream.avail_out = size; | 1089 png_ptr->zstream.avail_out = size; |
1083 | 1090 |
1084 /* For SYNC_FLUSH or FINISH it is essential to keep calling zlib with | 1091 /* For SYNC_FLUSH or FINISH it is essential to keep calling zlib with |
1085 * the same flush parameter until it has finished output, for NO_FLUSH | 1092 * the same flush parameter until it has finished output, for NO_FLUSH |
1086 * it doesn't matter. | 1093 * it doesn't matter. |
1087 */ | 1094 */ |
1088 if (ret == Z_OK && flush != Z_NO_FLUSH) | 1095 if (ret == Z_OK && flush != Z_NO_FLUSH) |
1089 continue; | 1096 continue; |
1090 } | 1097 } |
1091 | 1098 |
1092 /* The order of these checks doesn't matter much; it just effect which | 1099 /* The order of these checks doesn't matter much; it just affects which |
1093 * possible error might be detected if multiple things go wrong at once. | 1100 * possible error might be detected if multiple things go wrong at once. |
1094 */ | 1101 */ |
1095 if (ret == Z_OK) /* most likely return code! */ | 1102 if (ret == Z_OK) /* most likely return code! */ |
1096 { | 1103 { |
1097 /* If all the input has been consumed then just return. If Z_FINISH | 1104 /* If all the input has been consumed then just return. If Z_FINISH |
1098 * was used as the flush parameter something has gone wrong if we get | 1105 * was used as the flush parameter something has gone wrong if we get |
1099 * here. | 1106 * here. |
1100 */ | 1107 */ |
1101 if (input_len == 0) | 1108 if (input_len == 0) |
1102 { | 1109 { |
1103 if (flush == Z_FINISH) | 1110 if (flush == Z_FINISH) |
1104 png_error(png_ptr, "Z_OK on Z_FINISH with output space"); | 1111 png_error(png_ptr, "Z_OK on Z_FINISH with output space"); |
1105 | 1112 |
1106 return; | 1113 return; |
1107 } | 1114 } |
1108 } | 1115 } |
1109 | 1116 |
1110 else if (ret == Z_STREAM_END && flush == Z_FINISH) | 1117 else if (ret == Z_STREAM_END && flush == Z_FINISH) |
1111 { | 1118 { |
1112 /* This is the end of the IDAT data; any pending output must be | 1119 /* This is the end of the IDAT data; any pending output must be |
1113 * flushed. For small PNG files we may still be at the beginning. | 1120 * flushed. For small PNG files we may still be at the beginning. |
1114 */ | 1121 */ |
1115 png_bytep data = png_ptr->zbuffer_list->output; | 1122 png_bytep data = png_ptr->zbuffer_list->output; |
1116 uInt size = png_ptr->zbuffer_size - png_ptr->zstream.avail_out; | 1123 uInt size = png_ptr->zbuffer_size - png_ptr->zstream.avail_out; |
1117 | 1124 |
1118 # ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED | 1125 #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED |
1119 if (!(png_ptr->mode & PNG_HAVE_IDAT) && | 1126 if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 && |
1120 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) | 1127 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) |
1121 optimize_cmf(data, png_image_size(png_ptr)); | 1128 optimize_cmf(data, png_image_size(png_ptr)); |
1122 # endif | 1129 #endif |
1123 | 1130 |
1124 png_write_complete_chunk(png_ptr, png_IDAT, data, size); | 1131 png_write_complete_chunk(png_ptr, png_IDAT, data, size); |
1125 png_ptr->zstream.avail_out = 0; | 1132 png_ptr->zstream.avail_out = 0; |
1126 png_ptr->zstream.next_out = NULL; | 1133 png_ptr->zstream.next_out = NULL; |
1127 png_ptr->mode |= PNG_HAVE_IDAT | PNG_AFTER_IDAT; | 1134 png_ptr->mode |= PNG_HAVE_IDAT | PNG_AFTER_IDAT; |
1128 | 1135 |
1129 png_ptr->zowner = 0; /* Release the stream */ | 1136 png_ptr->zowner = 0; /* Release the stream */ |
1130 return; | 1137 return; |
1131 } | 1138 } |
1132 | 1139 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1185 #ifdef PNG_WRITE_iCCP_SUPPORTED | 1192 #ifdef PNG_WRITE_iCCP_SUPPORTED |
1186 /* Write an iCCP chunk */ | 1193 /* Write an iCCP chunk */ |
1187 void /* PRIVATE */ | 1194 void /* PRIVATE */ |
1188 png_write_iCCP(png_structrp png_ptr, png_const_charp name, | 1195 png_write_iCCP(png_structrp png_ptr, png_const_charp name, |
1189 png_const_bytep profile) | 1196 png_const_bytep profile) |
1190 { | 1197 { |
1191 png_uint_32 name_len; | 1198 png_uint_32 name_len; |
1192 png_uint_32 profile_len; | 1199 png_uint_32 profile_len; |
1193 png_byte new_name[81]; /* 1 byte for the compression byte */ | 1200 png_byte new_name[81]; /* 1 byte for the compression byte */ |
1194 compression_state comp; | 1201 compression_state comp; |
| 1202 png_uint_32 temp; |
1195 | 1203 |
1196 png_debug(1, "in png_write_iCCP"); | 1204 png_debug(1, "in png_write_iCCP"); |
1197 | 1205 |
1198 /* These are all internal problems: the profile should have been checked | 1206 /* These are all internal problems: the profile should have been checked |
1199 * before when it was stored. | 1207 * before when it was stored. |
1200 */ | 1208 */ |
1201 if (profile == NULL) | 1209 if (profile == NULL) |
1202 png_error(png_ptr, "No profile for iCCP chunk"); /* internal error */ | 1210 png_error(png_ptr, "No profile for iCCP chunk"); /* internal error */ |
1203 | 1211 |
1204 profile_len = png_get_uint_32(profile); | 1212 profile_len = png_get_uint_32(profile); |
1205 | 1213 |
1206 if (profile_len < 132) | 1214 if (profile_len < 132) |
1207 png_error(png_ptr, "ICC profile too short"); | 1215 png_error(png_ptr, "ICC profile too short"); |
1208 | 1216 |
1209 if (profile_len & 0x03) | 1217 temp = (png_uint_32) (*(profile+8)); |
| 1218 if (temp > 3 && (profile_len & 0x03)) |
1210 png_error(png_ptr, "ICC profile length invalid (not a multiple of 4)"); | 1219 png_error(png_ptr, "ICC profile length invalid (not a multiple of 4)"); |
1211 | 1220 |
1212 { | 1221 { |
1213 png_uint_32 embedded_profile_len = png_get_uint_32(profile); | 1222 png_uint_32 embedded_profile_len = png_get_uint_32(profile); |
1214 | 1223 |
1215 if (profile_len != embedded_profile_len) | 1224 if (profile_len != embedded_profile_len) |
1216 png_error(png_ptr, "Profile length does not match profile"); | 1225 png_error(png_ptr, "Profile length does not match profile"); |
1217 } | 1226 } |
1218 | 1227 |
1219 name_len = png_check_keyword(png_ptr, name, new_name); | 1228 name_len = png_check_keyword(png_ptr, name, new_name); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1331 /* Write the sBIT chunk */ | 1340 /* Write the sBIT chunk */ |
1332 void /* PRIVATE */ | 1341 void /* PRIVATE */ |
1333 png_write_sBIT(png_structrp png_ptr, png_const_color_8p sbit, int color_type) | 1342 png_write_sBIT(png_structrp png_ptr, png_const_color_8p sbit, int color_type) |
1334 { | 1343 { |
1335 png_byte buf[4]; | 1344 png_byte buf[4]; |
1336 png_size_t size; | 1345 png_size_t size; |
1337 | 1346 |
1338 png_debug(1, "in png_write_sBIT"); | 1347 png_debug(1, "in png_write_sBIT"); |
1339 | 1348 |
1340 /* Make sure we don't depend upon the order of PNG_COLOR_8 */ | 1349 /* Make sure we don't depend upon the order of PNG_COLOR_8 */ |
1341 if (color_type & PNG_COLOR_MASK_COLOR) | 1350 if ((color_type & PNG_COLOR_MASK_COLOR) != 0) |
1342 { | 1351 { |
1343 png_byte maxbits; | 1352 png_byte maxbits; |
1344 | 1353 |
1345 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 : | 1354 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 : |
1346 png_ptr->usr_bit_depth); | 1355 png_ptr->usr_bit_depth); |
1347 | 1356 |
1348 if (sbit->red == 0 || sbit->red > maxbits || | 1357 if (sbit->red == 0 || sbit->red > maxbits || |
1349 sbit->green == 0 || sbit->green > maxbits || | 1358 sbit->green == 0 || sbit->green > maxbits || |
1350 sbit->blue == 0 || sbit->blue > maxbits) | 1359 sbit->blue == 0 || sbit->blue > maxbits) |
1351 { | 1360 { |
(...skipping 12 matching lines...) Expand all Loading... |
1364 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth) | 1373 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth) |
1365 { | 1374 { |
1366 png_warning(png_ptr, "Invalid sBIT depth specified"); | 1375 png_warning(png_ptr, "Invalid sBIT depth specified"); |
1367 return; | 1376 return; |
1368 } | 1377 } |
1369 | 1378 |
1370 buf[0] = sbit->gray; | 1379 buf[0] = sbit->gray; |
1371 size = 1; | 1380 size = 1; |
1372 } | 1381 } |
1373 | 1382 |
1374 if (color_type & PNG_COLOR_MASK_ALPHA) | 1383 if ((color_type & PNG_COLOR_MASK_ALPHA) != 0) |
1375 { | 1384 { |
1376 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth) | 1385 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth) |
1377 { | 1386 { |
1378 png_warning(png_ptr, "Invalid sBIT depth specified"); | 1387 png_warning(png_ptr, "Invalid sBIT depth specified"); |
1379 return; | 1388 return; |
1380 } | 1389 } |
1381 | 1390 |
1382 buf[size++] = sbit->alpha; | 1391 buf[size++] = sbit->alpha; |
1383 } | 1392 } |
1384 | 1393 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1431 return; | 1440 return; |
1432 } | 1441 } |
1433 | 1442 |
1434 /* Write the chunk out as it is */ | 1443 /* Write the chunk out as it is */ |
1435 png_write_complete_chunk(png_ptr, png_tRNS, trans_alpha, | 1444 png_write_complete_chunk(png_ptr, png_tRNS, trans_alpha, |
1436 (png_size_t)num_trans); | 1445 (png_size_t)num_trans); |
1437 } | 1446 } |
1438 | 1447 |
1439 else if (color_type == PNG_COLOR_TYPE_GRAY) | 1448 else if (color_type == PNG_COLOR_TYPE_GRAY) |
1440 { | 1449 { |
1441 /* One 16 bit value */ | 1450 /* One 16-bit value */ |
1442 if (tran->gray >= (1 << png_ptr->bit_depth)) | 1451 if (tran->gray >= (1 << png_ptr->bit_depth)) |
1443 { | 1452 { |
1444 png_app_warning(png_ptr, | 1453 png_app_warning(png_ptr, |
1445 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth"); | 1454 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth"); |
1446 | 1455 |
1447 return; | 1456 return; |
1448 } | 1457 } |
1449 | 1458 |
1450 png_save_uint_16(buf, tran->gray); | 1459 png_save_uint_16(buf, tran->gray); |
1451 png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)2); | 1460 png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)2); |
1452 } | 1461 } |
1453 | 1462 |
1454 else if (color_type == PNG_COLOR_TYPE_RGB) | 1463 else if (color_type == PNG_COLOR_TYPE_RGB) |
1455 { | 1464 { |
1456 /* Three 16 bit values */ | 1465 /* Three 16-bit values */ |
1457 png_save_uint_16(buf, tran->red); | 1466 png_save_uint_16(buf, tran->red); |
1458 png_save_uint_16(buf + 2, tran->green); | 1467 png_save_uint_16(buf + 2, tran->green); |
1459 png_save_uint_16(buf + 4, tran->blue); | 1468 png_save_uint_16(buf + 4, tran->blue); |
1460 #ifdef PNG_WRITE_16BIT_SUPPORTED | 1469 #ifdef PNG_WRITE_16BIT_SUPPORTED |
1461 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) | 1470 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0) |
1462 #else | 1471 #else |
1463 if (buf[0] | buf[2] | buf[4]) | 1472 if ((buf[0] | buf[2] | buf[4]) != 0) |
1464 #endif | 1473 #endif |
1465 { | 1474 { |
1466 png_app_warning(png_ptr, | 1475 png_app_warning(png_ptr, |
1467 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8"); | 1476 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8"); |
1468 return; | 1477 return; |
1469 } | 1478 } |
1470 | 1479 |
1471 png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)6); | 1480 png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)6); |
1472 } | 1481 } |
1473 | 1482 |
(...skipping 10 matching lines...) Expand all Loading... |
1484 png_write_bKGD(png_structrp png_ptr, png_const_color_16p back, int color_type) | 1493 png_write_bKGD(png_structrp png_ptr, png_const_color_16p back, int color_type) |
1485 { | 1494 { |
1486 png_byte buf[6]; | 1495 png_byte buf[6]; |
1487 | 1496 |
1488 png_debug(1, "in png_write_bKGD"); | 1497 png_debug(1, "in png_write_bKGD"); |
1489 | 1498 |
1490 if (color_type == PNG_COLOR_TYPE_PALETTE) | 1499 if (color_type == PNG_COLOR_TYPE_PALETTE) |
1491 { | 1500 { |
1492 if ( | 1501 if ( |
1493 #ifdef PNG_MNG_FEATURES_SUPPORTED | 1502 #ifdef PNG_MNG_FEATURES_SUPPORTED |
1494 (png_ptr->num_palette || | 1503 (png_ptr->num_palette != 0 || |
1495 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) && | 1504 (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0) && |
1496 #endif | 1505 #endif |
1497 back->index >= png_ptr->num_palette) | 1506 back->index >= png_ptr->num_palette) |
1498 { | 1507 { |
1499 png_warning(png_ptr, "Invalid background palette index"); | 1508 png_warning(png_ptr, "Invalid background palette index"); |
1500 return; | 1509 return; |
1501 } | 1510 } |
1502 | 1511 |
1503 buf[0] = back->index; | 1512 buf[0] = back->index; |
1504 png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)1); | 1513 png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)1); |
1505 } | 1514 } |
1506 | 1515 |
1507 else if (color_type & PNG_COLOR_MASK_COLOR) | 1516 else if ((color_type & PNG_COLOR_MASK_COLOR) != 0) |
1508 { | 1517 { |
1509 png_save_uint_16(buf, back->red); | 1518 png_save_uint_16(buf, back->red); |
1510 png_save_uint_16(buf + 2, back->green); | 1519 png_save_uint_16(buf + 2, back->green); |
1511 png_save_uint_16(buf + 4, back->blue); | 1520 png_save_uint_16(buf + 4, back->blue); |
1512 #ifdef PNG_WRITE_16BIT_SUPPORTED | 1521 #ifdef PNG_WRITE_16BIT_SUPPORTED |
1513 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) | 1522 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0) |
1514 #else | 1523 #else |
1515 if (buf[0] | buf[2] | buf[4]) | 1524 if ((buf[0] | buf[2] | buf[4]) != 0) |
1516 #endif | 1525 #endif |
1517 { | 1526 { |
1518 png_warning(png_ptr, | 1527 png_warning(png_ptr, |
1519 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8"); | 1528 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8"); |
1520 | 1529 |
1521 return; | 1530 return; |
1522 } | 1531 } |
1523 | 1532 |
1524 png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)6); | 1533 png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)6); |
1525 } | 1534 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1600 png_write_chunk_header(png_ptr, png_tEXt, | 1609 png_write_chunk_header(png_ptr, png_tEXt, |
1601 (png_uint_32)/*checked above*/(key_len + text_len + 1)); | 1610 (png_uint_32)/*checked above*/(key_len + text_len + 1)); |
1602 /* | 1611 /* |
1603 * We leave it to the application to meet PNG-1.0 requirements on the | 1612 * We leave it to the application to meet PNG-1.0 requirements on the |
1604 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of | 1613 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of |
1605 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. | 1614 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. |
1606 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. | 1615 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. |
1607 */ | 1616 */ |
1608 png_write_chunk_data(png_ptr, new_key, key_len + 1); | 1617 png_write_chunk_data(png_ptr, new_key, key_len + 1); |
1609 | 1618 |
1610 if (text_len) | 1619 if (text_len != 0) |
1611 png_write_chunk_data(png_ptr, (png_const_bytep)text, text_len); | 1620 png_write_chunk_data(png_ptr, (png_const_bytep)text, text_len); |
1612 | 1621 |
1613 png_write_chunk_end(png_ptr); | 1622 png_write_chunk_end(png_ptr); |
1614 } | 1623 } |
1615 #endif | 1624 #endif |
1616 | 1625 |
1617 #ifdef PNG_WRITE_zTXt_SUPPORTED | 1626 #ifdef PNG_WRITE_zTXt_SUPPORTED |
1618 /* Write a compressed text chunk */ | 1627 /* Write a compressed text chunk */ |
1619 void /* PRIVATE */ | 1628 void /* PRIVATE */ |
1620 png_write_zTXt(png_structrp png_ptr, png_const_charp key, png_const_charp text, | 1629 png_write_zTXt(png_structrp png_ptr, png_const_charp key, png_const_charp text, |
1621 png_size_t text_len, int compression) | 1630 int compression) |
1622 { | 1631 { |
1623 png_uint_32 key_len; | 1632 png_uint_32 key_len; |
1624 png_byte new_key[81]; | 1633 png_byte new_key[81]; |
1625 compression_state comp; | 1634 compression_state comp; |
1626 | 1635 |
1627 png_debug(1, "in png_write_zTXt"); | 1636 png_debug(1, "in png_write_zTXt"); |
1628 PNG_UNUSED(text_len) /* Always use strlen */ | |
1629 | 1637 |
1630 if (compression == PNG_TEXT_COMPRESSION_NONE) | 1638 if (compression == PNG_TEXT_COMPRESSION_NONE) |
1631 { | 1639 { |
1632 png_write_tEXt(png_ptr, key, text, 0); | 1640 png_write_tEXt(png_ptr, key, text, 0); |
1633 return; | 1641 return; |
1634 } | 1642 } |
1635 | 1643 |
1636 if (compression != PNG_TEXT_COMPRESSION_zTXt) | 1644 if (compression != PNG_TEXT_COMPRESSION_zTXt) |
1637 png_error(png_ptr, "zTXt: invalid compression type"); | 1645 png_error(png_ptr, "zTXt: invalid compression type"); |
1638 | 1646 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1726 else | 1734 else |
1727 prefix_len = (png_uint_32)(prefix_len + lang_len); | 1735 prefix_len = (png_uint_32)(prefix_len + lang_len); |
1728 | 1736 |
1729 if (lang_key_len > PNG_UINT_31_MAX-prefix_len) | 1737 if (lang_key_len > PNG_UINT_31_MAX-prefix_len) |
1730 prefix_len = PNG_UINT_31_MAX; | 1738 prefix_len = PNG_UINT_31_MAX; |
1731 else | 1739 else |
1732 prefix_len = (png_uint_32)(prefix_len + lang_key_len); | 1740 prefix_len = (png_uint_32)(prefix_len + lang_key_len); |
1733 | 1741 |
1734 png_text_compress_init(&comp, (png_const_bytep)text, strlen(text)); | 1742 png_text_compress_init(&comp, (png_const_bytep)text, strlen(text)); |
1735 | 1743 |
1736 if (compression) | 1744 if (compression != 0) |
1737 { | 1745 { |
1738 if (png_text_compress(png_ptr, png_iTXt, &comp, prefix_len) != Z_OK) | 1746 if (png_text_compress(png_ptr, png_iTXt, &comp, prefix_len) != Z_OK) |
1739 png_error(png_ptr, png_ptr->zstream.msg); | 1747 png_error(png_ptr, png_ptr->zstream.msg); |
1740 } | 1748 } |
1741 | 1749 |
1742 else | 1750 else |
1743 { | 1751 { |
1744 if (comp.input_len > PNG_UINT_31_MAX-prefix_len) | 1752 if (comp.input_len > PNG_UINT_31_MAX-prefix_len) |
1745 png_error(png_ptr, "iTXt: uncompressed text too long"); | 1753 png_error(png_ptr, "iTXt: uncompressed text too long"); |
1746 | 1754 |
1747 /* So the string will fit in a chunk: */ | 1755 /* So the string will fit in a chunk: */ |
1748 comp.output_len = (png_uint_32)/*SAFE*/comp.input_len; | 1756 comp.output_len = (png_uint_32)/*SAFE*/comp.input_len; |
1749 } | 1757 } |
1750 | 1758 |
1751 png_write_chunk_header(png_ptr, png_iTXt, comp.output_len + prefix_len); | 1759 png_write_chunk_header(png_ptr, png_iTXt, comp.output_len + prefix_len); |
1752 | 1760 |
1753 png_write_chunk_data(png_ptr, new_key, key_len); | 1761 png_write_chunk_data(png_ptr, new_key, key_len); |
1754 | 1762 |
1755 png_write_chunk_data(png_ptr, (png_const_bytep)lang, lang_len); | 1763 png_write_chunk_data(png_ptr, (png_const_bytep)lang, lang_len); |
1756 | 1764 |
1757 png_write_chunk_data(png_ptr, (png_const_bytep)lang_key, lang_key_len); | 1765 png_write_chunk_data(png_ptr, (png_const_bytep)lang_key, lang_key_len); |
1758 | 1766 |
1759 if (compression) | 1767 if (compression != 0) |
1760 png_write_compressed_data_out(png_ptr, &comp); | 1768 png_write_compressed_data_out(png_ptr, &comp); |
1761 | 1769 |
1762 else | 1770 else |
1763 png_write_chunk_data(png_ptr, (png_const_bytep)text, comp.input_len); | 1771 png_write_chunk_data(png_ptr, (png_const_bytep)text, comp.output_len); |
1764 | 1772 |
1765 png_write_chunk_end(png_ptr); | 1773 png_write_chunk_end(png_ptr); |
1766 } | 1774 } |
1767 #endif | 1775 #endif |
1768 | 1776 |
1769 #ifdef PNG_WRITE_oFFs_SUPPORTED | 1777 #ifdef PNG_WRITE_oFFs_SUPPORTED |
1770 /* Write the oFFs chunk */ | 1778 /* Write the oFFs chunk */ |
1771 void /* PRIVATE */ | 1779 void /* PRIVATE */ |
1772 png_write_oFFs(png_structrp png_ptr, png_int_32 x_offset, png_int_32 y_offset, | 1780 png_write_oFFs(png_structrp png_ptr, png_int_32 x_offset, png_int_32 y_offset, |
1773 int unit_type) | 1781 int unit_type) |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1949 /* Start of interlace block in the y direction */ | 1957 /* Start of interlace block in the y direction */ |
1950 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; | 1958 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; |
1951 | 1959 |
1952 /* Offset to next interlace block in the y direction */ | 1960 /* Offset to next interlace block in the y direction */ |
1953 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; | 1961 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; |
1954 #endif | 1962 #endif |
1955 | 1963 |
1956 png_alloc_size_t buf_size; | 1964 png_alloc_size_t buf_size; |
1957 int usr_pixel_depth; | 1965 int usr_pixel_depth; |
1958 | 1966 |
| 1967 #ifdef PNG_WRITE_FILTER_SUPPORTED |
| 1968 png_byte filters; |
| 1969 #endif |
| 1970 |
1959 png_debug(1, "in png_write_start_row"); | 1971 png_debug(1, "in png_write_start_row"); |
1960 | 1972 |
1961 usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth; | 1973 usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth; |
1962 buf_size = PNG_ROWBYTES(usr_pixel_depth, png_ptr->width) + 1; | 1974 buf_size = PNG_ROWBYTES(usr_pixel_depth, png_ptr->width) + 1; |
1963 | 1975 |
1964 /* 1.5.6: added to allow checking in the row write code. */ | 1976 /* 1.5.6: added to allow checking in the row write code. */ |
1965 png_ptr->transformed_pixel_depth = png_ptr->pixel_depth; | 1977 png_ptr->transformed_pixel_depth = png_ptr->pixel_depth; |
1966 png_ptr->maximum_pixel_depth = (png_byte)usr_pixel_depth; | 1978 png_ptr->maximum_pixel_depth = (png_byte)usr_pixel_depth; |
1967 | 1979 |
1968 /* Set up row buffer */ | 1980 /* Set up row buffer */ |
1969 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size); | 1981 png_ptr->row_buf = png_voidcast(png_bytep, png_malloc(png_ptr, buf_size)); |
1970 | 1982 |
1971 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE; | 1983 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE; |
1972 | 1984 |
1973 #ifdef PNG_WRITE_FILTER_SUPPORTED | 1985 #ifdef PNG_WRITE_FILTER_SUPPORTED |
1974 /* Set up filtering buffer, if using this filter */ | 1986 filters = png_ptr->do_filter; |
1975 if (png_ptr->do_filter & PNG_FILTER_SUB) | 1987 |
1976 { | 1988 if (png_ptr->height == 1) |
1977 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1); | 1989 filters &= 0xff & ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH); |
1978 | 1990 |
1979 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; | 1991 if (png_ptr->width == 1) |
1980 } | 1992 filters &= 0xff & ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH); |
1981 | 1993 |
1982 /* We only need to keep the previous row if we are using one of these. */ | 1994 if (filters == 0) |
1983 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) | 1995 filters = PNG_FILTER_NONE; |
1984 { | 1996 |
1985 /* Set up previous row buffer */ | 1997 png_ptr->do_filter = filters; |
1986 png_ptr->prev_row = (png_bytep)png_calloc(png_ptr, buf_size); | 1998 |
1987 | 1999 if (((filters & (PNG_FILTER_SUB | PNG_FILTER_UP | PNG_FILTER_AVG | |
1988 if (png_ptr->do_filter & PNG_FILTER_UP) | 2000 PNG_FILTER_PAETH)) != 0) && png_ptr->try_row == NULL) |
1989 { | 2001 { |
1990 png_ptr->up_row = (png_bytep)png_malloc(png_ptr, | 2002 int num_filters = 0; |
1991 png_ptr->rowbytes + 1); | 2003 |
1992 | 2004 png_ptr->try_row = png_voidcast(png_bytep, png_malloc(png_ptr, buf_size)); |
1993 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; | 2005 |
1994 } | 2006 if (filters & PNG_FILTER_SUB) |
1995 | 2007 num_filters++; |
1996 if (png_ptr->do_filter & PNG_FILTER_AVG) | 2008 |
1997 { | 2009 if (filters & PNG_FILTER_UP) |
1998 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, | 2010 num_filters++; |
1999 png_ptr->rowbytes + 1); | 2011 |
2000 | 2012 if (filters & PNG_FILTER_AVG) |
2001 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; | 2013 num_filters++; |
2002 } | 2014 |
2003 | 2015 if (filters & PNG_FILTER_PAETH) |
2004 if (png_ptr->do_filter & PNG_FILTER_PAETH) | 2016 num_filters++; |
2005 { | 2017 |
2006 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr, | 2018 if (num_filters > 1) |
2007 png_ptr->rowbytes + 1); | 2019 png_ptr->tst_row = png_voidcast(png_bytep, png_malloc(png_ptr, |
2008 | 2020 buf_size)); |
2009 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; | 2021 } |
2010 } | 2022 |
2011 } | 2023 /* We only need to keep the previous row if we are using one of the following |
2012 #endif /* PNG_WRITE_FILTER_SUPPORTED */ | 2024 * filters. |
| 2025 */ |
| 2026 if ((filters & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) != 0) |
| 2027 png_ptr->prev_row = png_voidcast(png_bytep, |
| 2028 png_calloc(png_ptr, buf_size)); |
| 2029 #endif /* WRITE_FILTER */ |
2013 | 2030 |
2014 #ifdef PNG_WRITE_INTERLACING_SUPPORTED | 2031 #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
2015 /* If interlaced, we need to set up width and height of pass */ | 2032 /* If interlaced, we need to set up width and height of pass */ |
2016 if (png_ptr->interlaced) | 2033 if (png_ptr->interlaced != 0) |
2017 { | 2034 { |
2018 if (!(png_ptr->transformations & PNG_INTERLACE)) | 2035 if ((png_ptr->transformations & PNG_INTERLACE) == 0) |
2019 { | 2036 { |
2020 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - | 2037 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - |
2021 png_pass_ystart[0]) / png_pass_yinc[0]; | 2038 png_pass_ystart[0]) / png_pass_yinc[0]; |
2022 | 2039 |
2023 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 - | 2040 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 - |
2024 png_pass_start[0]) / png_pass_inc[0]; | 2041 png_pass_start[0]) / png_pass_inc[0]; |
2025 } | 2042 } |
2026 | 2043 |
2027 else | 2044 else |
2028 { | 2045 { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2063 | 2080 |
2064 /* Next row */ | 2081 /* Next row */ |
2065 png_ptr->row_number++; | 2082 png_ptr->row_number++; |
2066 | 2083 |
2067 /* See if we are done */ | 2084 /* See if we are done */ |
2068 if (png_ptr->row_number < png_ptr->num_rows) | 2085 if (png_ptr->row_number < png_ptr->num_rows) |
2069 return; | 2086 return; |
2070 | 2087 |
2071 #ifdef PNG_WRITE_INTERLACING_SUPPORTED | 2088 #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
2072 /* If interlaced, go to next pass */ | 2089 /* If interlaced, go to next pass */ |
2073 if (png_ptr->interlaced) | 2090 if (png_ptr->interlaced != 0) |
2074 { | 2091 { |
2075 png_ptr->row_number = 0; | 2092 png_ptr->row_number = 0; |
2076 if (png_ptr->transformations & PNG_INTERLACE) | 2093 if ((png_ptr->transformations & PNG_INTERLACE) != 0) |
2077 { | 2094 { |
2078 png_ptr->pass++; | 2095 png_ptr->pass++; |
2079 } | 2096 } |
2080 | 2097 |
2081 else | 2098 else |
2082 { | 2099 { |
2083 /* Loop until we find a non-zero width or height pass */ | 2100 /* Loop until we find a non-zero width or height pass */ |
2084 do | 2101 do |
2085 { | 2102 { |
2086 png_ptr->pass++; | 2103 png_ptr->pass++; |
2087 | 2104 |
2088 if (png_ptr->pass >= 7) | 2105 if (png_ptr->pass >= 7) |
2089 break; | 2106 break; |
2090 | 2107 |
2091 png_ptr->usr_width = (png_ptr->width + | 2108 png_ptr->usr_width = (png_ptr->width + |
2092 png_pass_inc[png_ptr->pass] - 1 - | 2109 png_pass_inc[png_ptr->pass] - 1 - |
2093 png_pass_start[png_ptr->pass]) / | 2110 png_pass_start[png_ptr->pass]) / |
2094 png_pass_inc[png_ptr->pass]; | 2111 png_pass_inc[png_ptr->pass]; |
2095 | 2112 |
2096 png_ptr->num_rows = (png_ptr->height + | 2113 png_ptr->num_rows = (png_ptr->height + |
2097 png_pass_yinc[png_ptr->pass] - 1 - | 2114 png_pass_yinc[png_ptr->pass] - 1 - |
2098 png_pass_ystart[png_ptr->pass]) / | 2115 png_pass_ystart[png_ptr->pass]) / |
2099 png_pass_yinc[png_ptr->pass]; | 2116 png_pass_yinc[png_ptr->pass]; |
2100 | 2117 |
2101 if (png_ptr->transformations & PNG_INTERLACE) | 2118 if ((png_ptr->transformations & PNG_INTERLACE) != 0) |
2102 break; | 2119 break; |
2103 | 2120 |
2104 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0); | 2121 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0); |
2105 | 2122 |
2106 } | 2123 } |
2107 | 2124 |
2108 /* Reset the row above the image for the next pass */ | 2125 /* Reset the row above the image for the next pass */ |
2109 if (png_ptr->pass < 7) | 2126 if (png_ptr->pass < 7) |
2110 { | 2127 { |
2111 if (png_ptr->prev_row != NULL) | 2128 if (png_ptr->prev_row != NULL) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2147 /* We don't have to do anything on the last pass (6) */ | 2164 /* We don't have to do anything on the last pass (6) */ |
2148 if (pass < 6) | 2165 if (pass < 6) |
2149 { | 2166 { |
2150 /* Each pixel depth is handled separately */ | 2167 /* Each pixel depth is handled separately */ |
2151 switch (row_info->pixel_depth) | 2168 switch (row_info->pixel_depth) |
2152 { | 2169 { |
2153 case 1: | 2170 case 1: |
2154 { | 2171 { |
2155 png_bytep sp; | 2172 png_bytep sp; |
2156 png_bytep dp; | 2173 png_bytep dp; |
2157 int shift; | 2174 unsigned int shift; |
2158 int d; | 2175 int d; |
2159 int value; | 2176 int value; |
2160 png_uint_32 i; | 2177 png_uint_32 i; |
2161 png_uint_32 row_width = row_info->width; | 2178 png_uint_32 row_width = row_info->width; |
2162 | 2179 |
2163 dp = row; | 2180 dp = row; |
2164 d = 0; | 2181 d = 0; |
2165 shift = 7; | 2182 shift = 7; |
2166 | 2183 |
2167 for (i = png_pass_start[pass]; i < row_width; | 2184 for (i = png_pass_start[pass]; i < row_width; |
(...skipping 17 matching lines...) Expand all Loading... |
2185 if (shift != 7) | 2202 if (shift != 7) |
2186 *dp = (png_byte)d; | 2203 *dp = (png_byte)d; |
2187 | 2204 |
2188 break; | 2205 break; |
2189 } | 2206 } |
2190 | 2207 |
2191 case 2: | 2208 case 2: |
2192 { | 2209 { |
2193 png_bytep sp; | 2210 png_bytep sp; |
2194 png_bytep dp; | 2211 png_bytep dp; |
2195 int shift; | 2212 unsigned int shift; |
2196 int d; | 2213 int d; |
2197 int value; | 2214 int value; |
2198 png_uint_32 i; | 2215 png_uint_32 i; |
2199 png_uint_32 row_width = row_info->width; | 2216 png_uint_32 row_width = row_info->width; |
2200 | 2217 |
2201 dp = row; | 2218 dp = row; |
2202 shift = 6; | 2219 shift = 6; |
2203 d = 0; | 2220 d = 0; |
2204 | 2221 |
2205 for (i = png_pass_start[pass]; i < row_width; | 2222 for (i = png_pass_start[pass]; i < row_width; |
(...skipping 16 matching lines...) Expand all Loading... |
2222 if (shift != 6) | 2239 if (shift != 6) |
2223 *dp = (png_byte)d; | 2240 *dp = (png_byte)d; |
2224 | 2241 |
2225 break; | 2242 break; |
2226 } | 2243 } |
2227 | 2244 |
2228 case 4: | 2245 case 4: |
2229 { | 2246 { |
2230 png_bytep sp; | 2247 png_bytep sp; |
2231 png_bytep dp; | 2248 png_bytep dp; |
2232 int shift; | 2249 unsigned int shift; |
2233 int d; | 2250 int d; |
2234 int value; | 2251 int value; |
2235 png_uint_32 i; | 2252 png_uint_32 i; |
2236 png_uint_32 row_width = row_info->width; | 2253 png_uint_32 row_width = row_info->width; |
2237 | 2254 |
2238 dp = row; | 2255 dp = row; |
2239 shift = 4; | 2256 shift = 4; |
2240 d = 0; | 2257 d = 0; |
2241 for (i = png_pass_start[pass]; i < row_width; | 2258 for (i = png_pass_start[pass]; i < row_width; |
2242 i += png_pass_inc[pass]) | 2259 i += png_pass_inc[pass]) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2297 png_pass_inc[pass] - 1 - | 2314 png_pass_inc[pass] - 1 - |
2298 png_pass_start[pass]) / | 2315 png_pass_start[pass]) / |
2299 png_pass_inc[pass]; | 2316 png_pass_inc[pass]; |
2300 | 2317 |
2301 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, | 2318 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, |
2302 row_info->width); | 2319 row_info->width); |
2303 } | 2320 } |
2304 } | 2321 } |
2305 #endif | 2322 #endif |
2306 | 2323 |
| 2324 |
2307 /* This filters the row, chooses which filter to use, if it has not already | 2325 /* This filters the row, chooses which filter to use, if it has not already |
2308 * been specified by the application, and then writes the row out with the | 2326 * been specified by the application, and then writes the row out with the |
2309 * chosen filter. | 2327 * chosen filter. |
2310 */ | 2328 */ |
2311 static void png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row, | 2329 static void /* PRIVATE */ |
| 2330 png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row, |
2312 png_size_t row_bytes); | 2331 png_size_t row_bytes); |
2313 | 2332 |
2314 #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1) | 2333 #ifdef PNG_WRITE_FILTER_SUPPORTED |
2315 #define PNG_HISHIFT 10 | 2334 static png_size_t /* PRIVATE */ |
2316 #define PNG_LOMASK ((png_uint_32)0xffffL) | 2335 png_setup_sub_row(png_structrp png_ptr, const png_uint_32 bpp, |
2317 #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT)) | 2336 const png_size_t row_bytes, const png_size_t lmins) |
| 2337 { |
| 2338 png_bytep rp, dp, lp; |
| 2339 png_size_t i; |
| 2340 png_size_t sum = 0; |
| 2341 int v; |
| 2342 |
| 2343 png_ptr->try_row[0] = PNG_FILTER_VALUE_SUB; |
| 2344 |
| 2345 for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1; i < bpp; |
| 2346 i++, rp++, dp++) |
| 2347 { |
| 2348 v = *dp = *rp; |
| 2349 sum += (v < 128) ? v : 256 - v; |
| 2350 } |
| 2351 |
| 2352 for (lp = png_ptr->row_buf + 1; i < row_bytes; |
| 2353 i++, rp++, lp++, dp++) |
| 2354 { |
| 2355 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); |
| 2356 sum += (v < 128) ? v : 256 - v; |
| 2357 |
| 2358 if (sum > lmins) /* We are already worse, don't continue. */ |
| 2359 break; |
| 2360 } |
| 2361 |
| 2362 return (sum); |
| 2363 } |
| 2364 |
| 2365 static png_size_t /* PRIVATE */ |
| 2366 png_setup_up_row(png_structrp png_ptr, const png_size_t row_bytes, |
| 2367 const png_size_t lmins) |
| 2368 { |
| 2369 png_bytep rp, dp, pp; |
| 2370 png_size_t i; |
| 2371 png_size_t sum = 0; |
| 2372 int v; |
| 2373 |
| 2374 png_ptr->try_row[0] = PNG_FILTER_VALUE_UP; |
| 2375 |
| 2376 for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, |
| 2377 pp = png_ptr->prev_row + 1; i < row_bytes; |
| 2378 i++, rp++, pp++, dp++) |
| 2379 { |
| 2380 v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); |
| 2381 sum += (v < 128) ? v : 256 - v; |
| 2382 |
| 2383 if (sum > lmins) /* We are already worse, don't continue. */ |
| 2384 break; |
| 2385 } |
| 2386 |
| 2387 return (sum); |
| 2388 } |
| 2389 |
| 2390 static png_size_t /* PRIVATE */ |
| 2391 png_setup_avg_row(png_structrp png_ptr, const png_uint_32 bpp, |
| 2392 const png_size_t row_bytes, const png_size_t lmins) |
| 2393 { |
| 2394 png_bytep rp, dp, pp, lp; |
| 2395 png_uint_32 i; |
| 2396 png_size_t sum = 0; |
| 2397 int v; |
| 2398 |
| 2399 png_ptr->try_row[0] = PNG_FILTER_VALUE_AVG; |
| 2400 |
| 2401 for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, |
| 2402 pp = png_ptr->prev_row + 1; i < bpp; i++) |
| 2403 { |
| 2404 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); |
| 2405 |
| 2406 sum += (v < 128) ? v : 256 - v; |
| 2407 } |
| 2408 |
| 2409 for (lp = png_ptr->row_buf + 1; i < row_bytes; i++) |
| 2410 { |
| 2411 v = *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) |
| 2412 & 0xff); |
| 2413 |
| 2414 sum += (v < 128) ? v : 256 - v; |
| 2415 |
| 2416 if (sum > lmins) /* We are already worse, don't continue. */ |
| 2417 break; |
| 2418 } |
| 2419 |
| 2420 return (sum); |
| 2421 } |
| 2422 |
| 2423 static png_size_t /* PRIVATE */ |
| 2424 png_setup_paeth_row(png_structrp png_ptr, const png_uint_32 bpp, |
| 2425 const png_size_t row_bytes, const png_size_t lmins) |
| 2426 { |
| 2427 png_bytep rp, dp, pp, cp, lp; |
| 2428 png_size_t i; |
| 2429 png_size_t sum = 0; |
| 2430 int v; |
| 2431 |
| 2432 png_ptr->try_row[0] = PNG_FILTER_VALUE_PAETH; |
| 2433 |
| 2434 for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, |
| 2435 pp = png_ptr->prev_row + 1; i < bpp; i++) |
| 2436 { |
| 2437 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); |
| 2438 |
| 2439 sum += (v < 128) ? v : 256 - v; |
| 2440 } |
| 2441 |
| 2442 for (lp = png_ptr->row_buf + 1, cp = png_ptr->prev_row + 1; i < row_bytes; |
| 2443 i++) |
| 2444 { |
| 2445 int a, b, c, pa, pb, pc, p; |
| 2446 |
| 2447 b = *pp++; |
| 2448 c = *cp++; |
| 2449 a = *lp++; |
| 2450 |
| 2451 p = b - c; |
| 2452 pc = a - c; |
| 2453 |
| 2454 #ifdef PNG_USE_ABS |
| 2455 pa = abs(p); |
| 2456 pb = abs(pc); |
| 2457 pc = abs(p + pc); |
| 2458 #else |
| 2459 pa = p < 0 ? -p : p; |
| 2460 pb = pc < 0 ? -pc : pc; |
| 2461 pc = (p + pc) < 0 ? -(p + pc) : p + pc; |
| 2462 #endif |
| 2463 |
| 2464 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; |
| 2465 |
| 2466 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); |
| 2467 |
| 2468 sum += (v < 128) ? v : 256 - v; |
| 2469 |
| 2470 if (sum > lmins) /* We are already worse, don't continue. */ |
| 2471 break; |
| 2472 } |
| 2473 |
| 2474 return (sum); |
| 2475 } |
| 2476 #endif /* WRITE_FILTER */ |
| 2477 |
2318 void /* PRIVATE */ | 2478 void /* PRIVATE */ |
2319 png_write_find_filter(png_structrp png_ptr, png_row_infop row_info) | 2479 png_write_find_filter(png_structrp png_ptr, png_row_infop row_info) |
2320 { | 2480 { |
| 2481 #ifndef PNG_WRITE_FILTER_SUPPORTED |
| 2482 png_write_filtered_row(png_ptr, png_ptr->row_buf, row_info->rowbytes+1); |
| 2483 #else |
| 2484 png_byte filter_to_do = png_ptr->do_filter; |
| 2485 png_bytep row_buf; |
2321 png_bytep best_row; | 2486 png_bytep best_row; |
2322 #ifdef PNG_WRITE_FILTER_SUPPORTED | 2487 png_uint_32 bpp; |
2323 png_bytep prev_row, row_buf; | 2488 png_size_t mins; |
2324 png_uint_32 mins, bpp; | |
2325 png_byte filter_to_do = png_ptr->do_filter; | |
2326 png_size_t row_bytes = row_info->rowbytes; | 2489 png_size_t row_bytes = row_info->rowbytes; |
2327 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED | |
2328 int num_p_filters = png_ptr->num_prev_filters; | |
2329 #endif | |
2330 | 2490 |
2331 png_debug(1, "in png_write_find_filter"); | 2491 png_debug(1, "in png_write_find_filter"); |
2332 | 2492 |
2333 #ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED | |
2334 if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS) | |
2335 { | |
2336 /* These will never be selected so we need not test them. */ | |
2337 filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH); | |
2338 } | |
2339 #endif | |
2340 | |
2341 /* Find out how many bytes offset each pixel is */ | 2493 /* Find out how many bytes offset each pixel is */ |
2342 bpp = (row_info->pixel_depth + 7) >> 3; | 2494 bpp = (row_info->pixel_depth + 7) >> 3; |
2343 | 2495 |
2344 prev_row = png_ptr->prev_row; | 2496 row_buf = png_ptr->row_buf; |
2345 #endif | 2497 mins = PNG_SIZE_MAX - 256/* so we can detect potential overflow of the |
2346 best_row = png_ptr->row_buf; | 2498 running sum */; |
2347 #ifdef PNG_WRITE_FILTER_SUPPORTED | |
2348 row_buf = best_row; | |
2349 mins = PNG_MAXSUM; | |
2350 | 2499 |
2351 /* The prediction method we use is to find which method provides the | 2500 /* The prediction method we use is to find which method provides the |
2352 * smallest value when summing the absolute values of the distances | 2501 * smallest value when summing the absolute values of the distances |
2353 * from zero, using anything >= 128 as negative numbers. This is known | 2502 * from zero, using anything >= 128 as negative numbers. This is known |
2354 * as the "minimum sum of absolute differences" heuristic. Other | 2503 * as the "minimum sum of absolute differences" heuristic. Other |
2355 * heuristics are the "weighted minimum sum of absolute differences" | 2504 * heuristics are the "weighted minimum sum of absolute differences" |
2356 * (experimental and can in theory improve compression), and the "zlib | 2505 * (experimental and can in theory improve compression), and the "zlib |
2357 * predictive" method (not implemented yet), which does test compressions | 2506 * predictive" method (not implemented yet), which does test compressions |
2358 * of lines using different filter methods, and then chooses the | 2507 * of lines using different filter methods, and then chooses the |
2359 * (series of) filter(s) that give minimum compressed data size (VERY | 2508 * (series of) filter(s) that give minimum compressed data size (VERY |
2360 * computationally expensive). | 2509 * computationally expensive). |
2361 * | 2510 * |
2362 * GRR 980525: consider also | 2511 * GRR 980525: consider also |
2363 * | 2512 * |
2364 * (1) minimum sum of absolute differences from running average (i.e., | 2513 * (1) minimum sum of absolute differences from running average (i.e., |
2365 * keep running sum of non-absolute differences & count of bytes) | 2514 * keep running sum of non-absolute differences & count of bytes) |
2366 * [track dispersion, too? restart average if dispersion too large?] | 2515 * [track dispersion, too? restart average if dispersion too large?] |
2367 * | 2516 * |
2368 * (1b) minimum sum of absolute differences from sliding average, probably | 2517 * (1b) minimum sum of absolute differences from sliding average, probably |
2369 * with window size <= deflate window (usually 32K) | 2518 * with window size <= deflate window (usually 32K) |
2370 * | 2519 * |
2371 * (2) minimum sum of squared differences from zero or running average | 2520 * (2) minimum sum of squared differences from zero or running average |
2372 * (i.e., ~ root-mean-square approach) | 2521 * (i.e., ~ root-mean-square approach) |
2373 */ | 2522 */ |
2374 | 2523 |
2375 | 2524 |
2376 /* We don't need to test the 'no filter' case if this is the only filter | 2525 /* We don't need to test the 'no filter' case if this is the only filter |
2377 * that has been chosen, as it doesn't actually do anything to the data. | 2526 * that has been chosen, as it doesn't actually do anything to the data. |
2378 */ | 2527 */ |
2379 if ((filter_to_do & PNG_FILTER_NONE) && filter_to_do != PNG_FILTER_NONE) | 2528 best_row = png_ptr->row_buf; |
| 2529 |
| 2530 |
| 2531 if ((filter_to_do & PNG_FILTER_NONE) != 0 && filter_to_do != PNG_FILTER_NONE) |
2380 { | 2532 { |
2381 png_bytep rp; | 2533 png_bytep rp; |
2382 png_uint_32 sum = 0; | 2534 png_size_t sum = 0; |
2383 png_size_t i; | 2535 png_size_t i; |
2384 int v; | 2536 int v; |
2385 | 2537 |
2386 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) | 2538 if (PNG_SIZE_MAX/128 <= row_bytes) |
2387 { | 2539 { |
2388 v = *rp; | 2540 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) |
2389 sum += (v < 128) ? v : 256 - v; | |
2390 } | |
2391 | |
2392 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED | |
2393 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) | |
2394 { | |
2395 png_uint_32 sumhi, sumlo; | |
2396 int j; | |
2397 sumlo = sum & PNG_LOMASK; | |
2398 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */ | |
2399 | |
2400 /* Reduce the sum if we match any of the previous rows */ | |
2401 for (j = 0; j < num_p_filters; j++) | |
2402 { | 2541 { |
2403 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) | 2542 /* Check for overflow */ |
2404 { | 2543 if (sum > PNG_SIZE_MAX/128 - 256) |
2405 sumlo = (sumlo * png_ptr->filter_weights[j]) >> | 2544 break; |
2406 PNG_WEIGHT_SHIFT; | 2545 |
2407 | 2546 v = *rp; |
2408 sumhi = (sumhi * png_ptr->filter_weights[j]) >> | 2547 sum += (v < 128) ? v : 256 - v; |
2409 PNG_WEIGHT_SHIFT; | |
2410 } | |
2411 } | 2548 } |
2412 | 2549 } |
2413 /* Factor in the cost of this filter (this is here for completeness, | 2550 else /* Overflow is not possible */ |
2414 * but it makes no sense to have a "cost" for the NONE filter, as | 2551 { |
2415 * it has the minimum possible computational cost - none). | 2552 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) |
2416 */ | 2553 { |
2417 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> | 2554 v = *rp; |
2418 PNG_COST_SHIFT; | 2555 sum += (v < 128) ? v : 256 - v; |
2419 | 2556 } |
2420 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> | 2557 } |
2421 PNG_COST_SHIFT; | 2558 |
2422 | |
2423 if (sumhi > PNG_HIMASK) | |
2424 sum = PNG_MAXSUM; | |
2425 | |
2426 else | |
2427 sum = (sumhi << PNG_HISHIFT) + sumlo; | |
2428 } | |
2429 #endif | |
2430 mins = sum; | 2559 mins = sum; |
2431 } | 2560 } |
2432 | 2561 |
2433 /* Sub filter */ | 2562 /* Sub filter */ |
2434 if (filter_to_do == PNG_FILTER_SUB) | 2563 if (filter_to_do == PNG_FILTER_SUB) |
2435 /* It's the only filter so no testing is needed */ | 2564 /* It's the only filter so no testing is needed */ |
2436 { | 2565 { |
2437 png_bytep rp, lp, dp; | 2566 (void) png_setup_sub_row(png_ptr, bpp, row_bytes, mins); |
2438 png_size_t i; | 2567 best_row = png_ptr->try_row; |
2439 | 2568 } |
2440 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; | 2569 |
2441 i++, rp++, dp++) | 2570 else if ((filter_to_do & PNG_FILTER_SUB) != 0) |
2442 { | 2571 { |
2443 *dp = *rp; | 2572 png_size_t sum; |
2444 } | 2573 png_size_t lmins = mins; |
2445 | 2574 |
2446 for (lp = row_buf + 1; i < row_bytes; | 2575 sum = png_setup_sub_row(png_ptr, bpp, row_bytes, lmins); |
2447 i++, rp++, lp++, dp++) | 2576 |
2448 { | 2577 if (sum < mins) |
2449 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); | 2578 { |
2450 } | 2579 mins = sum; |
2451 | 2580 best_row = png_ptr->try_row; |
2452 best_row = png_ptr->sub_row; | 2581 if (png_ptr->tst_row != NULL) |
2453 } | |
2454 | |
2455 else if (filter_to_do & PNG_FILTER_SUB) | |
2456 { | |
2457 png_bytep rp, dp, lp; | |
2458 png_uint_32 sum = 0, lmins = mins; | |
2459 png_size_t i; | |
2460 int v; | |
2461 | |
2462 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED | |
2463 /* We temporarily increase the "minimum sum" by the factor we | |
2464 * would reduce the sum of this filter, so that we can do the | |
2465 * early exit comparison without scaling the sum each time. | |
2466 */ | |
2467 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) | |
2468 { | |
2469 int j; | |
2470 png_uint_32 lmhi, lmlo; | |
2471 lmlo = lmins & PNG_LOMASK; | |
2472 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; | |
2473 | |
2474 for (j = 0; j < num_p_filters; j++) | |
2475 { | 2582 { |
2476 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) | 2583 png_ptr->try_row = png_ptr->tst_row; |
2477 { | 2584 png_ptr->tst_row = best_row; |
2478 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> | |
2479 PNG_WEIGHT_SHIFT; | |
2480 | |
2481 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> | |
2482 PNG_WEIGHT_SHIFT; | |
2483 } | |
2484 } | 2585 } |
2485 | |
2486 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> | |
2487 PNG_COST_SHIFT; | |
2488 | |
2489 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> | |
2490 PNG_COST_SHIFT; | |
2491 | |
2492 if (lmhi > PNG_HIMASK) | |
2493 lmins = PNG_MAXSUM; | |
2494 | |
2495 else | |
2496 lmins = (lmhi << PNG_HISHIFT) + lmlo; | |
2497 } | |
2498 #endif | |
2499 | |
2500 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; | |
2501 i++, rp++, dp++) | |
2502 { | |
2503 v = *dp = *rp; | |
2504 | |
2505 sum += (v < 128) ? v : 256 - v; | |
2506 } | |
2507 | |
2508 for (lp = row_buf + 1; i < row_bytes; | |
2509 i++, rp++, lp++, dp++) | |
2510 { | |
2511 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); | |
2512 | |
2513 sum += (v < 128) ? v : 256 - v; | |
2514 | |
2515 if (sum > lmins) /* We are already worse, don't continue. */ | |
2516 break; | |
2517 } | |
2518 | |
2519 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED | |
2520 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) | |
2521 { | |
2522 int j; | |
2523 png_uint_32 sumhi, sumlo; | |
2524 sumlo = sum & PNG_LOMASK; | |
2525 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; | |
2526 | |
2527 for (j = 0; j < num_p_filters; j++) | |
2528 { | |
2529 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) | |
2530 { | |
2531 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >> | |
2532 PNG_WEIGHT_SHIFT; | |
2533 | |
2534 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >> | |
2535 PNG_WEIGHT_SHIFT; | |
2536 } | |
2537 } | |
2538 | |
2539 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> | |
2540 PNG_COST_SHIFT; | |
2541 | |
2542 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> | |
2543 PNG_COST_SHIFT; | |
2544 | |
2545 if (sumhi > PNG_HIMASK) | |
2546 sum = PNG_MAXSUM; | |
2547 | |
2548 else | |
2549 sum = (sumhi << PNG_HISHIFT) + sumlo; | |
2550 } | |
2551 #endif | |
2552 | |
2553 if (sum < mins) | |
2554 { | |
2555 mins = sum; | |
2556 best_row = png_ptr->sub_row; | |
2557 } | 2586 } |
2558 } | 2587 } |
2559 | 2588 |
2560 /* Up filter */ | 2589 /* Up filter */ |
2561 if (filter_to_do == PNG_FILTER_UP) | 2590 if (filter_to_do == PNG_FILTER_UP) |
2562 { | 2591 { |
2563 png_bytep rp, dp, pp; | 2592 (void) png_setup_up_row(png_ptr, row_bytes, mins); |
2564 png_size_t i; | 2593 best_row = png_ptr->try_row; |
2565 | 2594 } |
2566 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, | 2595 |
2567 pp = prev_row + 1; i < row_bytes; | 2596 else if ((filter_to_do & PNG_FILTER_UP) != 0) |
2568 i++, rp++, pp++, dp++) | 2597 { |
2569 { | 2598 png_size_t sum; |
2570 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); | 2599 png_size_t lmins = mins; |
2571 } | 2600 |
2572 | 2601 sum = png_setup_up_row(png_ptr, row_bytes, lmins); |
2573 best_row = png_ptr->up_row; | 2602 |
2574 } | 2603 if (sum < mins) |
2575 | 2604 { |
2576 else if (filter_to_do & PNG_FILTER_UP) | 2605 mins = sum; |
2577 { | 2606 best_row = png_ptr->try_row; |
2578 png_bytep rp, dp, pp; | 2607 if (png_ptr->tst_row != NULL) |
2579 png_uint_32 sum = 0, lmins = mins; | |
2580 png_size_t i; | |
2581 int v; | |
2582 | |
2583 | |
2584 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED | |
2585 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) | |
2586 { | |
2587 int j; | |
2588 png_uint_32 lmhi, lmlo; | |
2589 lmlo = lmins & PNG_LOMASK; | |
2590 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; | |
2591 | |
2592 for (j = 0; j < num_p_filters; j++) | |
2593 { | 2608 { |
2594 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) | 2609 png_ptr->try_row = png_ptr->tst_row; |
2595 { | 2610 png_ptr->tst_row = best_row; |
2596 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> | |
2597 PNG_WEIGHT_SHIFT; | |
2598 | |
2599 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> | |
2600 PNG_WEIGHT_SHIFT; | |
2601 } | |
2602 } | 2611 } |
2603 | |
2604 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> | |
2605 PNG_COST_SHIFT; | |
2606 | |
2607 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> | |
2608 PNG_COST_SHIFT; | |
2609 | |
2610 if (lmhi > PNG_HIMASK) | |
2611 lmins = PNG_MAXSUM; | |
2612 | |
2613 else | |
2614 lmins = (lmhi << PNG_HISHIFT) + lmlo; | |
2615 } | |
2616 #endif | |
2617 | |
2618 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, | |
2619 pp = prev_row + 1; i < row_bytes; i++) | |
2620 { | |
2621 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); | |
2622 | |
2623 sum += (v < 128) ? v : 256 - v; | |
2624 | |
2625 if (sum > lmins) /* We are already worse, don't continue. */ | |
2626 break; | |
2627 } | |
2628 | |
2629 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED | |
2630 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) | |
2631 { | |
2632 int j; | |
2633 png_uint_32 sumhi, sumlo; | |
2634 sumlo = sum & PNG_LOMASK; | |
2635 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; | |
2636 | |
2637 for (j = 0; j < num_p_filters; j++) | |
2638 { | |
2639 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) | |
2640 { | |
2641 sumlo = (sumlo * png_ptr->filter_weights[j]) >> | |
2642 PNG_WEIGHT_SHIFT; | |
2643 | |
2644 sumhi = (sumhi * png_ptr->filter_weights[j]) >> | |
2645 PNG_WEIGHT_SHIFT; | |
2646 } | |
2647 } | |
2648 | |
2649 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> | |
2650 PNG_COST_SHIFT; | |
2651 | |
2652 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> | |
2653 PNG_COST_SHIFT; | |
2654 | |
2655 if (sumhi > PNG_HIMASK) | |
2656 sum = PNG_MAXSUM; | |
2657 | |
2658 else | |
2659 sum = (sumhi << PNG_HISHIFT) + sumlo; | |
2660 } | |
2661 #endif | |
2662 | |
2663 if (sum < mins) | |
2664 { | |
2665 mins = sum; | |
2666 best_row = png_ptr->up_row; | |
2667 } | 2612 } |
2668 } | 2613 } |
2669 | 2614 |
2670 /* Avg filter */ | 2615 /* Avg filter */ |
2671 if (filter_to_do == PNG_FILTER_AVG) | 2616 if (filter_to_do == PNG_FILTER_AVG) |
2672 { | 2617 { |
2673 png_bytep rp, dp, pp, lp; | 2618 (void) png_setup_avg_row(png_ptr, bpp, row_bytes, mins); |
2674 png_uint_32 i; | 2619 best_row = png_ptr->try_row; |
2675 | 2620 } |
2676 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, | 2621 |
2677 pp = prev_row + 1; i < bpp; i++) | 2622 else if ((filter_to_do & PNG_FILTER_AVG) != 0) |
2678 { | 2623 { |
2679 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); | 2624 png_size_t sum; |
2680 } | 2625 png_size_t lmins = mins; |
2681 | 2626 |
2682 for (lp = row_buf + 1; i < row_bytes; i++) | 2627 sum= png_setup_avg_row(png_ptr, bpp, row_bytes, lmins); |
2683 { | 2628 |
2684 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) | 2629 if (sum < mins) |
2685 & 0xff); | 2630 { |
2686 } | 2631 mins = sum; |
2687 best_row = png_ptr->avg_row; | 2632 best_row = png_ptr->try_row; |
2688 } | 2633 if (png_ptr->tst_row != NULL) |
2689 | |
2690 else if (filter_to_do & PNG_FILTER_AVG) | |
2691 { | |
2692 png_bytep rp, dp, pp, lp; | |
2693 png_uint_32 sum = 0, lmins = mins; | |
2694 png_size_t i; | |
2695 int v; | |
2696 | |
2697 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED | |
2698 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) | |
2699 { | |
2700 int j; | |
2701 png_uint_32 lmhi, lmlo; | |
2702 lmlo = lmins & PNG_LOMASK; | |
2703 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; | |
2704 | |
2705 for (j = 0; j < num_p_filters; j++) | |
2706 { | 2634 { |
2707 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG) | 2635 png_ptr->try_row = png_ptr->tst_row; |
2708 { | 2636 png_ptr->tst_row = best_row; |
2709 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> | |
2710 PNG_WEIGHT_SHIFT; | |
2711 | |
2712 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> | |
2713 PNG_WEIGHT_SHIFT; | |
2714 } | |
2715 } | 2637 } |
2716 | 2638 } |
2717 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> | 2639 } |
2718 PNG_COST_SHIFT; | 2640 |
2719 | 2641 /* Paeth filter */ |
2720 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> | 2642 if ((filter_to_do == PNG_FILTER_PAETH) != 0) |
2721 PNG_COST_SHIFT; | 2643 { |
2722 | 2644 (void) png_setup_paeth_row(png_ptr, bpp, row_bytes, mins); |
2723 if (lmhi > PNG_HIMASK) | 2645 best_row = png_ptr->try_row; |
2724 lmins = PNG_MAXSUM; | 2646 } |
2725 | 2647 |
2726 else | 2648 else if ((filter_to_do & PNG_FILTER_PAETH) != 0) |
2727 lmins = (lmhi << PNG_HISHIFT) + lmlo; | 2649 { |
2728 } | 2650 png_size_t sum; |
2729 #endif | 2651 png_size_t lmins = mins; |
2730 | 2652 |
2731 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, | 2653 sum = png_setup_paeth_row(png_ptr, bpp, row_bytes, lmins); |
2732 pp = prev_row + 1; i < bpp; i++) | 2654 |
2733 { | 2655 if (sum < mins) |
2734 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); | 2656 { |
2735 | 2657 best_row = png_ptr->try_row; |
2736 sum += (v < 128) ? v : 256 - v; | 2658 if (png_ptr->tst_row != NULL) |
2737 } | |
2738 | |
2739 for (lp = row_buf + 1; i < row_bytes; i++) | |
2740 { | |
2741 v = *dp++ = | |
2742 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff); | |
2743 | |
2744 sum += (v < 128) ? v : 256 - v; | |
2745 | |
2746 if (sum > lmins) /* We are already worse, don't continue. */ | |
2747 break; | |
2748 } | |
2749 | |
2750 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED | |
2751 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) | |
2752 { | |
2753 int j; | |
2754 png_uint_32 sumhi, sumlo; | |
2755 sumlo = sum & PNG_LOMASK; | |
2756 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; | |
2757 | |
2758 for (j = 0; j < num_p_filters; j++) | |
2759 { | 2659 { |
2760 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) | 2660 png_ptr->try_row = png_ptr->tst_row; |
2761 { | 2661 png_ptr->tst_row = best_row; |
2762 sumlo = (sumlo * png_ptr->filter_weights[j]) >> | |
2763 PNG_WEIGHT_SHIFT; | |
2764 | |
2765 sumhi = (sumhi * png_ptr->filter_weights[j]) >> | |
2766 PNG_WEIGHT_SHIFT; | |
2767 } | |
2768 } | 2662 } |
2769 | 2663 } |
2770 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> | 2664 } |
2771 PNG_COST_SHIFT; | |
2772 | |
2773 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> | |
2774 PNG_COST_SHIFT; | |
2775 | |
2776 if (sumhi > PNG_HIMASK) | |
2777 sum = PNG_MAXSUM; | |
2778 | |
2779 else | |
2780 sum = (sumhi << PNG_HISHIFT) + sumlo; | |
2781 } | |
2782 #endif | |
2783 | |
2784 if (sum < mins) | |
2785 { | |
2786 mins = sum; | |
2787 best_row = png_ptr->avg_row; | |
2788 } | |
2789 } | |
2790 | |
2791 /* Paeth filter */ | |
2792 if (filter_to_do == PNG_FILTER_PAETH) | |
2793 { | |
2794 png_bytep rp, dp, pp, cp, lp; | |
2795 png_size_t i; | |
2796 | |
2797 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, | |
2798 pp = prev_row + 1; i < bpp; i++) | |
2799 { | |
2800 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); | |
2801 } | |
2802 | |
2803 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) | |
2804 { | |
2805 int a, b, c, pa, pb, pc, p; | |
2806 | |
2807 b = *pp++; | |
2808 c = *cp++; | |
2809 a = *lp++; | |
2810 | |
2811 p = b - c; | |
2812 pc = a - c; | |
2813 | |
2814 #ifdef PNG_USE_ABS | |
2815 pa = abs(p); | |
2816 pb = abs(pc); | |
2817 pc = abs(p + pc); | |
2818 #else | |
2819 pa = p < 0 ? -p : p; | |
2820 pb = pc < 0 ? -pc : pc; | |
2821 pc = (p + pc) < 0 ? -(p + pc) : p + pc; | |
2822 #endif | |
2823 | |
2824 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; | |
2825 | |
2826 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); | |
2827 } | |
2828 best_row = png_ptr->paeth_row; | |
2829 } | |
2830 | |
2831 else if (filter_to_do & PNG_FILTER_PAETH) | |
2832 { | |
2833 png_bytep rp, dp, pp, cp, lp; | |
2834 png_uint_32 sum = 0, lmins = mins; | |
2835 png_size_t i; | |
2836 int v; | |
2837 | |
2838 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED | |
2839 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) | |
2840 { | |
2841 int j; | |
2842 png_uint_32 lmhi, lmlo; | |
2843 lmlo = lmins & PNG_LOMASK; | |
2844 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; | |
2845 | |
2846 for (j = 0; j < num_p_filters; j++) | |
2847 { | |
2848 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) | |
2849 { | |
2850 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> | |
2851 PNG_WEIGHT_SHIFT; | |
2852 | |
2853 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> | |
2854 PNG_WEIGHT_SHIFT; | |
2855 } | |
2856 } | |
2857 | |
2858 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> | |
2859 PNG_COST_SHIFT; | |
2860 | |
2861 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> | |
2862 PNG_COST_SHIFT; | |
2863 | |
2864 if (lmhi > PNG_HIMASK) | |
2865 lmins = PNG_MAXSUM; | |
2866 | |
2867 else | |
2868 lmins = (lmhi << PNG_HISHIFT) + lmlo; | |
2869 } | |
2870 #endif | |
2871 | |
2872 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, | |
2873 pp = prev_row + 1; i < bpp; i++) | |
2874 { | |
2875 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); | |
2876 | |
2877 sum += (v < 128) ? v : 256 - v; | |
2878 } | |
2879 | |
2880 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) | |
2881 { | |
2882 int a, b, c, pa, pb, pc, p; | |
2883 | |
2884 b = *pp++; | |
2885 c = *cp++; | |
2886 a = *lp++; | |
2887 | |
2888 #ifndef PNG_SLOW_PAETH | |
2889 p = b - c; | |
2890 pc = a - c; | |
2891 #ifdef PNG_USE_ABS | |
2892 pa = abs(p); | |
2893 pb = abs(pc); | |
2894 pc = abs(p + pc); | |
2895 #else | |
2896 pa = p < 0 ? -p : p; | |
2897 pb = pc < 0 ? -pc : pc; | |
2898 pc = (p + pc) < 0 ? -(p + pc) : p + pc; | |
2899 #endif | |
2900 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; | |
2901 #else /* PNG_SLOW_PAETH */ | |
2902 p = a + b - c; | |
2903 pa = abs(p - a); | |
2904 pb = abs(p - b); | |
2905 pc = abs(p - c); | |
2906 | |
2907 if (pa <= pb && pa <= pc) | |
2908 p = a; | |
2909 | |
2910 else if (pb <= pc) | |
2911 p = b; | |
2912 | |
2913 else | |
2914 p = c; | |
2915 #endif /* PNG_SLOW_PAETH */ | |
2916 | |
2917 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); | |
2918 | |
2919 sum += (v < 128) ? v : 256 - v; | |
2920 | |
2921 if (sum > lmins) /* We are already worse, don't continue. */ | |
2922 break; | |
2923 } | |
2924 | |
2925 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED | |
2926 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) | |
2927 { | |
2928 int j; | |
2929 png_uint_32 sumhi, sumlo; | |
2930 sumlo = sum & PNG_LOMASK; | |
2931 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; | |
2932 | |
2933 for (j = 0; j < num_p_filters; j++) | |
2934 { | |
2935 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) | |
2936 { | |
2937 sumlo = (sumlo * png_ptr->filter_weights[j]) >> | |
2938 PNG_WEIGHT_SHIFT; | |
2939 | |
2940 sumhi = (sumhi * png_ptr->filter_weights[j]) >> | |
2941 PNG_WEIGHT_SHIFT; | |
2942 } | |
2943 } | |
2944 | |
2945 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> | |
2946 PNG_COST_SHIFT; | |
2947 | |
2948 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> | |
2949 PNG_COST_SHIFT; | |
2950 | |
2951 if (sumhi > PNG_HIMASK) | |
2952 sum = PNG_MAXSUM; | |
2953 | |
2954 else | |
2955 sum = (sumhi << PNG_HISHIFT) + sumlo; | |
2956 } | |
2957 #endif | |
2958 | |
2959 if (sum < mins) | |
2960 { | |
2961 best_row = png_ptr->paeth_row; | |
2962 } | |
2963 } | |
2964 #endif /* PNG_WRITE_FILTER_SUPPORTED */ | |
2965 | 2665 |
2966 /* Do the actual writing of the filtered row data from the chosen filter. */ | 2666 /* Do the actual writing of the filtered row data from the chosen filter. */ |
2967 png_write_filtered_row(png_ptr, best_row, row_info->rowbytes+1); | 2667 png_write_filtered_row(png_ptr, best_row, row_info->rowbytes+1); |
2968 | 2668 |
2969 #ifdef PNG_WRITE_FILTER_SUPPORTED | 2669 #endif /* WRITE_FILTER */ |
2970 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED | |
2971 /* Save the type of filter we picked this time for future calculations */ | |
2972 if (png_ptr->num_prev_filters > 0) | |
2973 { | |
2974 int j; | |
2975 | |
2976 for (j = 1; j < num_p_filters; j++) | |
2977 { | |
2978 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1]; | |
2979 } | |
2980 | |
2981 png_ptr->prev_filters[j] = best_row[0]; | |
2982 } | |
2983 #endif | |
2984 #endif /* PNG_WRITE_FILTER_SUPPORTED */ | |
2985 } | 2670 } |
2986 | 2671 |
2987 | 2672 |
2988 /* Do the actual writing of a previously filtered row. */ | 2673 /* Do the actual writing of a previously filtered row. */ |
2989 static void | 2674 static void |
2990 png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row, | 2675 png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row, |
2991 png_size_t full_row_length/*includes filter byte*/) | 2676 png_size_t full_row_length/*includes filter byte*/) |
2992 { | 2677 { |
2993 png_debug(1, "in png_write_filtered_row"); | 2678 png_debug(1, "in png_write_filtered_row"); |
2994 | 2679 |
2995 png_debug1(2, "filter = %d", filtered_row[0]); | 2680 png_debug1(2, "filter = %d", filtered_row[0]); |
2996 | 2681 |
2997 png_compress_IDAT(png_ptr, filtered_row, full_row_length, Z_NO_FLUSH); | 2682 png_compress_IDAT(png_ptr, filtered_row, full_row_length, Z_NO_FLUSH); |
2998 | 2683 |
| 2684 #ifdef PNG_WRITE_FILTER_SUPPORTED |
2999 /* Swap the current and previous rows */ | 2685 /* Swap the current and previous rows */ |
3000 if (png_ptr->prev_row != NULL) | 2686 if (png_ptr->prev_row != NULL) |
3001 { | 2687 { |
3002 png_bytep tptr; | 2688 png_bytep tptr; |
3003 | 2689 |
3004 tptr = png_ptr->prev_row; | 2690 tptr = png_ptr->prev_row; |
3005 png_ptr->prev_row = png_ptr->row_buf; | 2691 png_ptr->prev_row = png_ptr->row_buf; |
3006 png_ptr->row_buf = tptr; | 2692 png_ptr->row_buf = tptr; |
3007 } | 2693 } |
| 2694 #endif /* WRITE_FILTER */ |
3008 | 2695 |
3009 /* Finish row - updates counters and flushes zlib if last row */ | 2696 /* Finish row - updates counters and flushes zlib if last row */ |
3010 png_write_finish_row(png_ptr); | 2697 png_write_finish_row(png_ptr); |
3011 | 2698 |
3012 #ifdef PNG_WRITE_FLUSH_SUPPORTED | 2699 #ifdef PNG_WRITE_FLUSH_SUPPORTED |
3013 png_ptr->flush_rows++; | 2700 png_ptr->flush_rows++; |
3014 | 2701 |
3015 if (png_ptr->flush_dist > 0 && | 2702 if (png_ptr->flush_dist > 0 && |
3016 png_ptr->flush_rows >= png_ptr->flush_dist) | 2703 png_ptr->flush_rows >= png_ptr->flush_dist) |
3017 { | 2704 { |
3018 png_write_flush(png_ptr); | 2705 png_write_flush(png_ptr); |
3019 } | 2706 } |
3020 #endif | 2707 #endif /* WRITE_FLUSH */ |
3021 } | 2708 } |
3022 #endif /* PNG_WRITE_SUPPORTED */ | 2709 #endif /* WRITE */ |
OLD | NEW |