diff -ru4NwbB libpng-1.5.2/Makefile.am libpng-1.5.3beta02/Makefile.am --- libpng-1.5.2/Makefile.am 2011-03-31 11:23:47.610923954 -0500 +++ libpng-1.5.3beta02/Makefile.am 2011-04-03 06:22:00.295870206 -0500 @@ -161,9 +161,9 @@ rm -f $@ dfn?.out test -z "$(CPPFLAGS)" echo "com @PNGLIB_VERSION@ STANDARD API DEFINITION" |\ $(AWK) -f ${srcdir}/scripts/options.awk out=dfn1.out\ - logunsupported=2 - ${srcdir}/scripts/pnglibconf.dfa 1>&2 + logunsupported=3 - ${srcdir}/scripts/pnglibconf.dfa 1>&2 $(AWK) -f ${srcdir}/scripts/options.awk out=dfn2.out dfn1.out 1>&2 rm dfn1.out mv dfn2.out $@ diff -ru4NwbB libpng-1.5.2/png.h libpng-1.5.3beta02/png.h --- libpng-1.5.2/png.h 2011-03-31 11:23:40.652780723 -0500 +++ libpng-1.5.3beta02/png.h 2011-04-03 06:21:52.165738566 -0500 @@ -149,8 +149,9 @@ * 1.5.1 15 10501 15.so.15.1[.0] * 1.5.2beta01-03 15 10502 15.so.15.2[.0] * 1.5.2rc01-03 15 10502 15.so.15.2[.0] * 1.5.2 15 10502 15.so.15.2[.0] + * 1.5.3beta01-02 15 10503 15.so.15.3[.0] * * Henceforth the source version will match the shared-library major * and minor numbers; the shared-library major version number will be * used for changes in backward compatibility, as it is intended. The @@ -1412,8 +1413,26 @@ PNG_EXPORT(73, void, png_set_compression_method, (png_structp png_ptr, int method)); +#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION +/* Also set zlib parameters for compressing non-IDAT chunks */ +PNG_EXPORT(222, void, png_set_text_compression_level, + (png_structp png_ptr, int level)); + +PNG_EXPORT(223, void, png_set_text_compression_mem_level, (png_structp png_ptr, + int mem_level)); + +PNG_EXPORT(224, void, png_set_text_compression_strategy, (png_structp png_ptr, + int strategy)); + +PNG_EXPORT(225, void, png_set_text_compression_window_bits, (png_structp + png_ptr, int window_bits)); + +PNG_EXPORT(226, void, png_set_text_compression_method, (png_structp png_ptr, + int method)); +#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION */ + /* These next functions are called for input/output, memory, and error * handling. They are in the file pngrio.c, pngwio.c, and pngerror.c, * and call standard C I/O routines such as fread(), fwrite(), and * fprintf(). These functions can be made to use other I/O routines @@ -2296,9 +2315,9 @@ * one to use is one more than this.) Maintainer, remember to add an entry to * scripts/symbols.def as well. */ #ifdef PNG_EXPORT_LAST_ORDINAL - PNG_EXPORT_LAST_ORDINAL(221); + PNG_EXPORT_LAST_ORDINAL(226); #endif #ifdef __cplusplus } diff -ru4NwbB libpng-1.5.2/pngmem.c libpng-1.5.3beta02/pngmem.c --- libpng-1.5.2/pngmem.c 2011-03-31 11:23:40.721711366 -0500 +++ libpng-1.5.3beta02/pngmem.c 2011-04-03 06:21:52.235916796 -0500 @@ -1,8 +1,8 @@ /* pngmem.c - stub functions for memory allocation * - * Last changed in libpng 1.5.1 [February 3, 2011] + * Last changed in libpng 1.5.3 [(PENDING RELEASE)] * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -186,25 +186,34 @@ { int num_blocks; png_uint_32 total_size; png_bytep table; - int i; + int i, mem_level, window_bits; png_byte huge * hptr; + int window_bits if (ret != NULL) { farfree(ret); ret = NULL; } - if (png_ptr->zlib_window_bits > 14) - num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14)); + window_bits = + png_ptr->zlib_window_bits >= png_ptr->zlib_text_window_bits ? + png_ptr->zlib_window_bits : png_ptr->zlib_text_window_bits; + + if (window_bits > 14) + num_blocks = (int)(1 << (window_bits - 14)); else num_blocks = 1; - if (png_ptr->zlib_mem_level >= 7) - num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7)); + mem_level = + png_ptr->zlib_mem_level >= png_ptr->zlib_text_mem_level ? + png_ptr->zlib_mem_level : png_ptr->zlib_text_mem_level; + + if (mem_level >= 7) + num_blocks += (int)(1 << (mem_level - 7)); else num_blocks++; @@ -276,9 +285,9 @@ if (png_ptr->offset_table_count >= png_ptr->offset_table_number) { # ifndef PNG_USER_MEM_SUPPORTED if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) - png_error(png_ptr, "Out of Memory"); /* Note "o" and "M" */ + png_error(png_ptr, "Out of Memory"); /* Note "O" and "M" */ else png_warning(png_ptr, "Out of Memory"); # endif diff -ru4NwbB libpng-1.5.2/pngpriv.h libpng-1.5.3beta02/pngpriv.h --- libpng-1.5.2/pngpriv.h 2011-03-31 11:23:40.667851883 -0500 +++ libpng-1.5.3beta02/pngpriv.h 2011-04-03 06:21:52.181205625 -0500 @@ -5,9 +5,9 @@ * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * - * Last changed in libpng 1.5.2 [March 31, 2011] + * Last changed in libpng 1.5.3 [(PENDING RELEASE)] * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h @@ -276,8 +276,9 @@ #define PNG_WROTE_INFO_BEFORE_PLTE 0x400 #define PNG_BACKGROUND_IS_GRAY 0x800 #define PNG_HAVE_PNG_SIGNATURE 0x1000 #define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000 /* Have another chunk after IDAT */ +#define PNG_ZLIB_READY_FOR_ZTXT 0x4000 /* 1: ready for ZTXT; 0: for IDAT */ /* Flags for the transformations the PNG library does on the image data */ #define PNG_BGR 0x0001 #define PNG_INTERLACE 0x0002 @@ -344,13 +345,13 @@ #define PNG_FLAG_MALLOC_NULL_MEM_OK 0x100000L /* 0x200000L unused */ /* 0x400000L unused */ #define PNG_FLAG_BENIGN_ERRORS_WARN 0x800000L /* Added to libpng-1.4.0 */ - /* 0x1000000L unused */ - /* 0x2000000L unused */ - /* 0x4000000L unused */ - /* 0x8000000L unused */ - /* 0x10000000L unused */ +#define PNG_FLAG_ZTXT_CUSTOM_STRATEGY 0x1000000L /* 5 lines added */ +#define PNG_FLAG_ZTXT_CUSTOM_LEVEL 0x2000000L /* to libpng-1.5.3 */ +#define PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL 0x4000000L +#define PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS 0x8000000L +#define PNG_FLAG_ZTXT_CUSTOM_METHOD 0x10000000L /* 0x20000000L unused */ /* 0x40000000L unused */ #define PNG_FLAG_CRC_ANCILLARY_MASK (PNG_FLAG_CRC_ANCILLARY_USE | \ @@ -539,10 +540,9 @@ PNG_EXTERN void png_crc_read PNGARG((png_structp png_ptr, png_bytep buf, png_size_t length)); /* Decompress data in a chunk that uses compression */ -#if defined(PNG_zTXt_SUPPORTED) || defined(PNG_iTXt_SUPPORTED) || \ - defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) +#if defined(PNG_READ_COMPRESSED_TEXT_SUPPORTED) PNG_EXTERN void png_decompress_chunk PNGARG((png_structp png_ptr, int comp_type, png_size_t chunklength, png_size_t prefix_length, png_size_t *data_length)); #endif @@ -643,8 +643,9 @@ PNG_EXTERN void png_write_hIST PNGARG((png_structp png_ptr, png_const_uint_16p hist, int num_hist)); #endif +/* Chunks that have keywords */ #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) PNG_EXTERN png_size_t png_check_keyword PNGARG((png_structp png_ptr, png_const_charp key, png_charpp new_key)); diff -ru4NwbB libpng-1.5.2/pngrutil.c libpng-1.5.3beta02/pngrutil.c --- libpng-1.5.2/pngrutil.c 2011-03-31 11:23:40.773411012 -0500 +++ libpng-1.5.3beta02/pngrutil.c 2011-04-03 06:21:52.287555850 -0500 @@ -1,8 +1,8 @@ /* pngrutil.c - utilities to read a PNG file * - * Last changed in libpng 1.5.2 [March 31, 2011] + * Last changed in libpng 1.5.3 [(PENDING RELEASE)] * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -277,10 +277,9 @@ else return (0); } -#if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \ - defined(PNG_READ_iCCP_SUPPORTED) +#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED static png_size_t png_inflate(png_structp png_ptr, png_bytep data, png_size_t size, png_bytep output, png_size_t output_size) { @@ -535,9 +534,9 @@ } *newlength = prefix_size; } -#endif +#endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */ /* Read and check the IDHR chunk */ void /* PRIVATE */ png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) diff -ru4NwbB libpng-1.5.2/pngstruct.h libpng-1.5.3beta02/pngstruct.h --- libpng-1.5.2/pngstruct.h 2011-03-31 11:23:40.673577111 -0500 +++ libpng-1.5.3beta02/pngstruct.h 2011-04-03 06:21:52.186866151 -0500 @@ -68,8 +68,18 @@ int zlib_method; /* holds zlib compression method */ int zlib_window_bits; /* holds zlib compression window bits */ int zlib_mem_level; /* holds zlib compression memory level */ int zlib_strategy; /* holds zlib compression strategy */ +/* Added at libpng 1.5.3 */ +#if defined(PNG_WRITE_COMPRESSED_TEXT_SUPPORTED) || \ + defined(PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION) + int zlib_text_level; /* holds zlib compression level */ + int zlib_text_method; /* holds zlib compression method */ + int zlib_text_window_bits; /* holds zlib compression window bits */ + int zlib_text_mem_level; /* holds zlib compression memory level */ + int zlib_text_strategy; /* holds zlib compression strategy */ +#endif +/* End of material added at libpng 1.5.3 */ png_uint_32 width; /* width of image in pixels */ png_uint_32 height; /* height of image in pixels */ png_uint_32 num_rows; /* number of rows in current pass */ diff -ru4NwbB libpng-1.5.2/pngtest.c libpng-1.5.3beta02/pngtest.c --- libpng-1.5.2/pngtest.c 2011-03-31 11:23:40.790873438 -0500 +++ libpng-1.5.3beta02/pngtest.c 2011-04-03 06:21:52.305457066 -0500 @@ -1,8 +1,8 @@ /* pngtest.c - a simple test program to test libpng * - * Last changed in libpng 1.5.0 [January 6, 2011] + * Last changed in libpng 1.5.3 [(PENDING RELEASE)] * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -912,8 +912,16 @@ # endif # endif #endif +#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION + /* Normally one would use Z_DEFAULT_STRATEGY for text compression. + * This is here just to make pngtest replicate the results from libpng + * versions prior to 1.5.3, and to test this new API. + */ + png_set_text_compression_strategy(write_ptr, Z_FILTERED); +#endif + if (status_dots_requested == 1) { #ifdef PNG_WRITE_SUPPORTED png_set_write_status_fn(write_ptr, write_row_callback); diff -ru4NwbB libpng-1.5.2/pngwrite.c libpng-1.5.3beta02/pngwrite.c --- libpng-1.5.2/pngwrite.c 2011-03-31 11:23:40.832722141 -0500 +++ libpng-1.5.3beta02/pngwrite.c 2011-04-03 06:21:52.347580655 -0500 @@ -1,8 +1,8 @@ /* pngwrite.c - general routines to write a PNG file * - * Last changed in libpng 1.5.1 [February 3, 2011] + * Last changed in libpng 1.5.3 [(PENDING RELEASE)] * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -1490,8 +1490,88 @@ png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD; png_ptr->zlib_method = method; } +/* The following were added to libpng-1.5.3 */ +#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION +void PNGAPI +png_set_text_compression_level(png_structp png_ptr, int level) +{ + png_debug(1, "in png_set_text_compression_level"); + + if (png_ptr == NULL) + return; + + png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_LEVEL; + png_ptr->zlib_text_level = level; +} + +void PNGAPI +png_set_text_compression_mem_level(png_structp png_ptr, int mem_level) +{ + png_debug(1, "in png_set_text_compression_mem_level"); + + if (png_ptr == NULL) + return; + + png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL; + png_ptr->zlib_text_mem_level = mem_level; +} + +void PNGAPI +png_set_text_compression_strategy(png_structp png_ptr, int strategy) +{ + png_debug(1, "in png_set_text_compression_strategy"); + + if (png_ptr == NULL) + return; + + png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_STRATEGY; + png_ptr->zlib_text_strategy = strategy; +} + +void PNGAPI +png_set_text_compression_window_bits(png_structp png_ptr, int window_bits) +{ + if (png_ptr == NULL) + return; + + if (window_bits > 15) + png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); + + else if (window_bits < 8) + png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); + +#ifndef WBITS_8_OK + /* Avoid libpng bug with 256-byte windows */ + if (window_bits == 8) + { + png_warning(png_ptr, "Text compression window is being reset to 512"); + window_bits = 9; + } + +#endif + png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS; + png_ptr->zlib_text_window_bits = window_bits; +} + +void PNGAPI +png_set_text_compression_method(png_structp png_ptr, int method) +{ + png_debug(1, "in png_set_text_compression_method"); + + if (png_ptr == NULL) + return; + + if (method != 8) + png_warning(png_ptr, "Only compression method 8 is supported by PNG"); + + png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_METHOD; + png_ptr->zlib_text_method = method; +} +#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION */ +/* end of API added to libpng-1.5.3 */ + void PNGAPI png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn) { if (png_ptr == NULL) diff -ru4NwbB libpng-1.5.2/pngwutil.c libpng-1.5.3beta02/pngwutil.c --- libpng-1.5.2/pngwutil.c 2011-03-31 11:23:40.850911793 -0500 +++ libpng-1.5.3beta02/pngwutil.c 2011-04-03 06:21:52.366429406 -0500 @@ -1,8 +1,8 @@ /* pngwutil.c - utilities to write a PNG file * - * Last changed in libpng 1.5.0 [January 6, 2011] + * Last changed in libpng 1.5.3 [(PENDING RELEASE)] * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -191,9 +191,9 @@ png_write_data(png_ptr, buf, (png_size_t)4); } -#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED) +#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED /* This pair of functions encapsulates the operation of (a) compressing a * text string, and (b) issuing it later as a series of chunk data writes. * The compression_state structure is shared context for these functions * set up by the caller in order to make the whole mess thread-safe. @@ -219,15 +219,14 @@ comp->num_output_ptr = 0; comp->max_output_ptr = 0; comp->output_ptr = NULL; comp->input = NULL; - comp->input_len = 0; + comp->input_len = text_len; /* We may just want to pass the text right through */ if (compression == PNG_TEXT_COMPRESSION_NONE) { comp->input = (png_const_bytep)text; - comp->input_len = text_len; return((int)text_len); } if (compression >= PNG_TEXT_COMPRESSION_LAST) @@ -255,11 +254,43 @@ * data, or if the input string is incredibly large (although this * wouldn't cause a failure, just a slowdown due to swapping). */ + if (!(png_ptr->mode & PNG_ZLIB_READY_FOR_ZTXT)) + { + + /* png_warning(png_ptr, "Initialize compressor for ztxt"); */ + /* Free memory from previously opened zstream */ + deflateEnd(&png_ptr->zstream); + + /* Initialize the compressor for zTXt compression. */ + ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_text_level, + png_ptr->zlib_text_method, png_ptr->zlib_text_window_bits, + png_ptr->zlib_text_mem_level, png_ptr->zlib_text_strategy); + + if (ret != Z_OK) + { + if (ret == Z_VERSION_ERROR) + png_error(png_ptr, + "zlib failed to initialize compressor for text-- version error"); + + if (ret == Z_STREAM_ERROR) + png_error(png_ptr, + "zlib failed to initialize compressor for text-- stream error"); + + if (ret == Z_MEM_ERROR) + png_error(png_ptr, + "zlib failed to initialize compressor for text-- mem error"); + + png_error(png_ptr, "zlib failed to initialize compressor for text"); + } + png_ptr->mode |= PNG_ZLIB_READY_FOR_ZTXT; + } + /* Set up the compression buffers */ /* TODO: the following cast hides a potential overflow problem. */ png_ptr->zstream.avail_in = (uInt)text_len; + /* NOTE: assume zlib doesn't overwrite the input */ png_ptr->zstream.next_in = (Bytef *)text; png_ptr->zstream.avail_out = png_ptr->zbuf_size; png_ptr->zstream.next_out = png_ptr->zbuf; @@ -417,8 +448,71 @@ return; } +#if 1 /* For testing */ + + if (comp->input_len >= 2 && comp->input_len < 16384) + { + unsigned int z_cmf; /* zlib compression method and flags */ + + /* Optimize the CMF field in the zlib stream. This hack of the zlib + * stream is compliant to the stream specification. + */ + + if (comp->num_output_ptr) + z_cmf = comp->output_ptr[0][0]; + else + z_cmf = png_ptr->zbuf[0]; + + if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) + { + unsigned int z_cinfo; + unsigned int half_z_window_size; + png_uint_32 uncompressed_idat_size = comp->input_len; + + z_cinfo = z_cmf >> 4; + half_z_window_size = 1 << (z_cinfo + 7); + + while (uncompressed_idat_size <= half_z_window_size && + half_z_window_size >= 256) + { + z_cinfo--; + half_z_window_size >>= 1; + } + + z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4); + + if (comp->num_output_ptr) + { + + if (comp->output_ptr[0][0] != z_cmf) + { + int tmp; + + comp->output_ptr[0][0] = (png_byte)z_cmf; + tmp = comp->output_ptr[0][1] & 0xe0; + tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f; + comp->output_ptr[0][1] = (png_byte)tmp; + } + } + else + { + int tmp; + + png_ptr->zbuf[0] = (png_byte)z_cmf; + tmp = png_ptr->zbuf[1] & 0xe0; + tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f; + png_ptr->zbuf[1] = (png_byte)tmp; + } + } + + else + png_error(png_ptr, + "Invalid zlib compression method or flags in non-IDAT chunk"); + } +#endif + /* Write saved output buffers, if any */ for (i = 0; i < comp->num_output_ptr; i++) { png_write_chunk_data(png_ptr, comp->output_ptr[i], @@ -436,11 +530,10 @@ (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out)); /* Reset zlib for another zTXt/iTXt or image data */ deflateReset(&png_ptr->zstream); - png_ptr->zstream.data_type = Z_BINARY; } -#endif +#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */ /* Write the IHDR chunk, and update the png_struct with the necessary * information. Note that the rest of this code depends upon this * information being correct. @@ -631,37 +724,39 @@ if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD)) png_ptr->zlib_method = 8; - ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level, - png_ptr->zlib_method, png_ptr->zlib_window_bits, - png_ptr->zlib_mem_level, png_ptr->zlib_strategy); - - if (ret != Z_OK) - { - if (ret == Z_VERSION_ERROR) - png_error(png_ptr, - "zlib failed to initialize compressor -- version error"); +#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED +#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION + if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_STRATEGY)) + png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY; + + if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_LEVEL)) + png_ptr->zlib_text_level = png_ptr->zlib_level; - if (ret == Z_STREAM_ERROR) - png_error(png_ptr, - "zlib failed to initialize compressor -- stream error"); + if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL)) + png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level; - if (ret == Z_MEM_ERROR) - png_error(png_ptr, - "zlib failed to initialize compressor -- mem error"); + if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS)) + png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits; - png_error(png_ptr, "zlib failed to initialize compressor"); - } + if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_METHOD)) + png_ptr->zlib_text_method = png_ptr->zlib_method; +#else + png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY; + png_ptr->zlib_text_level = png_ptr->zlib_level; + png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level; + png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits; + png_ptr->zlib_text_method = png_ptr->zlib_method; +#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION */ +#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */ - png_ptr->zstream.next_out = png_ptr->zbuf; - png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; - /* libpng is not interested in zstream.data_type, so set it - * to a predefined value, to avoid its evaluation inside zlib - */ - png_ptr->zstream.data_type = Z_BINARY; + /* Initialize the zlib compressor */ + ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level, + png_ptr->zlib_method, png_ptr->zlib_window_bits, + png_ptr->zlib_mem_level, png_ptr->zlib_strategy); - png_ptr->mode = PNG_HAVE_IHDR; + png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */ } /* Write the palette. We are careful not to trust png_color to be in the * correct order for PNG, so people can redefine it to any convenient @@ -744,14 +839,58 @@ PNG_IDAT; png_debug(1, "in png_write_IDAT"); - /* Optimize the CMF field in the zlib stream. */ - /* This hack of the zlib stream is compliant to the stream specification. */ if (!(png_ptr->mode & PNG_HAVE_IDAT) && png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) { - unsigned int z_cmf = data[0]; /* zlib compression method and flags */ + unsigned int z_cmf; /* zlib compression method and flags */ + +#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED + int ret; + + /* Reinitialize the compressor if necessary */ + if (png_ptr->mode & PNG_ZLIB_READY_FOR_ZTXT) + { + /* Free memory from previously opened zstream */ + deflateEnd(&png_ptr->zstream); + + ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level, + png_ptr->zlib_method, png_ptr->zlib_window_bits, + png_ptr->zlib_mem_level, png_ptr->zlib_strategy); + + if (ret != Z_OK) + { + if (ret == Z_VERSION_ERROR) + png_error(png_ptr, + "zlib failed to initialize compressor -- version error"); + + if (ret == Z_STREAM_ERROR) + png_error(png_ptr, + "zlib failed to initialize compressor -- stream error"); + + if (ret == Z_MEM_ERROR) + png_error(png_ptr, + "zlib failed to initialize compressor -- mem error"); + + png_error(png_ptr, "zlib failed to initialize compressor"); + } + png_ptr->mode &= ~PNG_ZLIB_READY_FOR_ZTXT; /* Ready for IDAT */ + } +#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */ + + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + /* libpng is not interested in zstream.data_type, so set it + * to a predefined value, to avoid its evaluation inside zlib + */ + png_ptr->zstream.data_type = Z_BINARY; + + /* Optimize the CMF field in the zlib stream. This hack of the zlib + * stream is compliant to the stream specification. + */ + z_cmf = data[0]; + if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) { /* Avoid memory underflows and multiplication overflows. * @@ -760,13 +899,31 @@ */ if (length >= 2 && png_ptr->height < 16384 && png_ptr->width < 16384) { + /* Compute the maximum possible length of the datastream */ + + /* Number of pixels, plus for each row a filter byte and possible + * and possibly a padding byte, so increase the maximum + * size to account for these. + */ + unsigned int z_cinfo; + unsigned int half_z_window_size; png_uint_32 uncompressed_idat_size = png_ptr->height * ((png_ptr->width * png_ptr->channels * png_ptr->bit_depth + 15) >> 3); - unsigned int z_cinfo = z_cmf >> 4; - unsigned int half_z_window_size = 1 << (z_cinfo + 7); + + /* If it's interlaced, each block of 8 rows is sent as up to + * 14 rows, i.e., 6 additional rows, each with a filter byte + * and possibly a padding byte + */ + if (png_ptr->interlaced) + uncompressed_idat_size += ((png_ptr->height + 7)/8) * + (png_ptr->bit_depth < 8 ? 12 : 6); + + z_cinfo = z_cmf >> 4; + half_z_window_size = 1 << (z_cinfo + 7); + while (uncompressed_idat_size <= half_z_window_size && half_z_window_size >= 256) { z_cinfo--; @@ -917,9 +1074,12 @@ png_write_chunk_data(png_ptr, (png_bytep)new_name, (png_size_t)(name_len + 2)); if (profile_len) + { + comp.input_len = profile_len; png_write_compressed_data_out(png_ptr, &comp); + } png_write_chunk_end(png_ptr); png_free(png_ptr, new_name); } @@ -1498,8 +1658,9 @@ /* Write compression */ png_write_chunk_data(png_ptr, &buf, (png_size_t)1); /* Write the compressed data */ + comp.input_len = text_len; png_write_compressed_data_out(png_ptr, &comp); /* Close the chunk */ png_write_chunk_end(png_ptr); diff -ru4NwbB libpng-1.5.2/scripts/pnglibconf.dfa libpng-1.5.3beta02/scripts/pnglibconf.dfa --- libpng-1.5.2/scripts/pnglibconf.dfa 2011-02-12 09:16:01.541296000 -0600 +++ libpng-1.5.3beta02/scripts/pnglibconf.dfa 2011-04-01 14:49:47.719281000 -0500 @@ -385,8 +385,13 @@ # apps implementing the user transforms option USER_TRANSFORM_PTR if READ_USER_TRANSFORM WRITE_USER_TRANSFORM option USER_TRANSFORM_INFO if READ_USER_TRANSFORM WRITE_USER_TRANSFORM +# This enables API to set compression parameters for compressing +# non-IDAT chunks (zTXt, iTXt, iCCP, and unknown chunks). This feature +# was added at libpng-1.5.3. +option WRITE_CUSTOMIZE_ZTXT_COMPRESSION requires WRITE + # Any chunks you are not interested in, you can undef here. The # ones that allocate memory may be expecially important (hIST, # tEXt, zTXt, tRNS, pCAL). Others will just save time and make png_info # a bit smaller. @@ -500,9 +505,9 @@ chunk cHRM chunk gAMA chunk hIST chunk iCCP -chunk iTXt requires TEXT +chunk iTXt chunk oFFs chunk pCAL chunk sCAL chunk pHYs @@ -511,9 +516,9 @@ chunk sRGB chunk tEXt requires TEXT chunk tIME chunk tRNS -chunk zTXt requires TEXT +chunk zTXt # This only affects support of the optional PLTE chunk in RGB and RGBA # images. Notice that READ_ANCILLARY_CHUNKS therefore disables part # of the regular chunk reading too. @@ -540,11 +545,21 @@ option SAVE_INT_32 requires WRITE # png_save_int_32 is required by the ancillary chunks oFFs and pCAL +option READ_iCCP enables READ_COMPRESSED_TEXT +option READ_iTXt enables READ_COMPRESSED_TEXT +option READ_zTXt enables READ_COMPRESSED_TEXT +option READ_COMPRESSED_TEXT enables READ_TEXT + option WRITE_oFFs enables SAVE_INT_32 option WRITE_pCAL enables SAVE_INT_32 +option WRITE_iCCP enables WRITE_COMPRESSED_TEXT +option WRITE_iTXt enables WRITE_COMPRESSED_TEXT +option WRITE_zTXt enables WRITE_COMPRESSED_TEXT +option WRITE_COMPRESSED_TEXT enables WRITE_TEXT + # Turn this off to disable png_read_png() and png_write_png() and # leave the row_pointers member out of the info structure. option INFO_IMAGE diff -ru4NwbB libpng-1.5.2/scripts/pnglibconf.h.prebuilt libpng-1.5.3beta02/scripts/pnglibconf.h.prebuilt --- libpng-1.5.2/scripts/pnglibconf.h.prebuilt 2011-02-12 09:20:50.414618000 -0600 +++ libpng-1.5.3beta02/scripts/pnglibconf.h.prebuilt 2011-04-01 13:06:13.124838000 -0500 @@ -112,8 +112,9 @@ #define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED #define PNG_READ_USER_CHUNKS_SUPPORTED #define PNG_READ_USER_TRANSFORM_SUPPORTED #define PNG_READ_zTXt_SUPPORTED +#define PNG_READ_COMPRESSED_TEXT_SUPPORTED #define PNG_SAVE_INT_32_SUPPORTED #define PNG_sBIT_SUPPORTED #define PNG_sCAL_SUPPORTED #define PNG_SEQUENTIAL_READ_SUPPORTED @@ -150,8 +151,9 @@ #define PNG_WRITE_INTERLACING_SUPPORTED #define PNG_WRITE_INT_FUNCTIONS_SUPPORTED #define PNG_WRITE_INVERT_ALPHA_SUPPORTED #define PNG_WRITE_INVERT_SUPPORTED +#define PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION #define PNG_WRITE_iTXt_SUPPORTED #define PNG_WRITE_oFFs_SUPPORTED #define PNG_WRITE_PACK_SUPPORTED #define PNG_WRITE_PACKSWAP_SUPPORTED @@ -174,8 +176,9 @@ #define PNG_WRITE_USER_TRANSFORM_SUPPORTED #define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED #define PNG_WRITE_zTXt_SUPPORTED #define PNG_zTXt_SUPPORTED +#define PNG_WRITE_COMPRESSED_TEXT_SUPPORTED /*#undef PNG_ERROR_NUMBERS_SUPPORTED*/ /*#undef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED*/ /* end of options */ #endif /* PNGLCONF_H */ diff -ru4NwbB libpng-1.5.2/scripts/symbols.def libpng-1.5.3beta02/scripts/symbols.def --- libpng-1.5.2/scripts/symbols.def 2011-03-31 11:23:42.905754321 -0500 +++ libpng-1.5.3beta02/scripts/symbols.def 2011-04-03 06:21:55.111609819 -0500 @@ -226,4 +226,9 @@ png_get_current_pass_number @218 png_process_data_pause @219 png_process_data_skip @220 png_set_expand_16 @221 + png_set_text_compression_level @222 + png_set_text_compression_mem_level @223 + png_set_text_compression_strategy @224 + png_set_text_compression_window_bits @225 + png_set_text_compression_method @226