Separate png_compress_IDAT into w/c

I.e. write/compress, also remove some trailing spaces and clean up pnglibconf
stuff.

Signed-off-by: John Bowler <jbowler@acm.org>
This commit is contained in:
John Bowler 2015-11-30 21:00:22 -08:00
parent 8978eba436
commit 8fe2eac47f
5 changed files with 189 additions and 163 deletions

View File

@ -566,7 +566,7 @@ struct png_struct_def
* wherein !(r==g==b).
*/
#endif /* RGB_TO_GRAY */
#endif /* TRANFORM_MECH */
#endif /* TRANSFORM_MECH */
#ifdef PNG_READ_SUPPORTED
/* These, and IDAT_size below, control how much input and output (at most) is
@ -599,12 +599,7 @@ struct png_struct_def
int zlib_set_window_bits;
int zlib_set_mem_level;
int zlib_set_strategy;
unsigned int zbuffer_start; /* Bytes written from start */
png_uint_32 zbuffer_len; /* Length of data in list */
png_compression_bufferp zbuffer_list; /* Created on demand during write */
png_compression_bufferp *zbuffer_end; /* 'next' field of current buffer */
#endif
#endif /* WRITE */
/* ERROR HANDLING */
#ifdef PNG_SETJMP_SUPPORTED
@ -748,14 +743,23 @@ struct png_struct_def
* zlib expects a 'zstream' as the fundamental control structure, it allows
* all the parameters to be passed as one pointer.
*/
png_uint_32 zowner; /* ID (chunk type) of zstream owner, 0 if none */
z_stream zstream; /* decompression structure */
unsigned int zstream_start:1; /* before first byte of stream */
unsigned int zstream_ended:1; /* no more zlib output available */
unsigned int zstream_error:1; /* zlib error message has been output */
png_uint_32 zowner; /* ID (chunk type) of zstream owner, 0 if none */
# ifdef PNG_WRITE_SUPPORTED
png_compression_bufferp zbuffer_list; /* Created on demand during write */
png_compression_bufferp *zbuffer_end; /* 'next' field of current buffer */
png_uint_32 zbuffer_len; /* Length of data in list */
unsigned int zbuffer_start; /* Bytes written from start */
# endif /* WRITE */
# ifdef PNG_READ_SUPPORTED
unsigned int zstream_ended:1; /* no more zlib output available [read] */
unsigned int zstream_error:1; /* zlib error message has been output [read] */
# endif /* READ */
# ifdef PNG_PROGRESSIVE_READ_SUPPORTED
unsigned int zstream_eod :1; /* all the required uncompressed data has been
* received; set by the zstream using code for
* its own purposes. */
* its own purposes. [progressive read] */
# endif /* PROGRESSIVE_READ */
/* MNG SUPPORT */
#ifdef PNG_MNG_FEATURES_SUPPORTED

View File

@ -1907,62 +1907,14 @@ png_start_IDAT(png_structrp png_ptr)
}
static void
png_compress_IDAT(png_structrp png_ptr, png_const_voidp input, uInt input_len,
int flush)
{
int ret;
/* The stream must have been claimed: */
affirm(png_ptr->zowner == png_IDAT);
/* z_stream::{next,avail}_out are set by png_compress to point into the
* buffer list. next_in must be set here, avail_in comes from the input_len
* parameter:
*/
{
png_uint_32 output_len = 0U;
png_ptr->zstream.next_in =
PNGZ_INPUT_CAST(png_voidcast(const Bytef*,input));
ret = png_compress(png_ptr, &png_ptr->zstream, &png_ptr->zbuffer_end,
input_len, &output_len, flush);
implies(ret == Z_OK || ret == Z_FINISH, png_ptr->zstream.avail_in == 0U);
png_ptr->zstream.next_in = NULL;
png_ptr->zstream.avail_in = 0U; /* safety */
/* If IDAT_size is set to PNG_UINT_31_MAX the length will be larger, but
* not enough to overflow a png_uint_32.
*/
png_ptr->zbuffer_len += output_len;
}
/* Check the return code. */
if (ret == Z_OK || ret == Z_STREAM_END)
{
/* Z_FINISH should give Z_STREAM_END, everything else should give Z_OK,
* so:
*/
debug((ret == Z_STREAM_END) == (flush == Z_FINISH));
/* At this point png_compress has produced (in total) zbuffer_start +
* zbuffer_len compressed bytes in a list of compression buffers starting
* with zbuffer_list and ending with the buffer containing *zbuffer_end,
* which may be NULL or may point to unused compression buffers.
*
* This code has already written out zbuffer_start bytes from the first
* buffer. Can we write more? If flush is Z_FINISH this is the end of
* the stream and there should be final bytes to write. Otherwise wait
* for IDAT_size bytes before writing a chunk. If nothing is to be
* written this function call is complete.
*/
if (flush == Z_FINISH || png_ptr->zbuffer_len >= png_ptr->IDAT_size)
png_write_IDAT(png_structrp png_ptr, int end_of_image)
{
png_compression_bufferp *listp = &png_ptr->zbuffer_list;
png_compression_bufferp list;
png_uint_32 output_len = png_ptr->zbuffer_len;
png_uint_32 start = png_ptr->zbuffer_start;
png_uint_32 size = png_ptr->IDAT_size;
const png_uint_32 min_size = (flush == Z_FINISH ? 1U : size);
const png_uint_32 min_size = (end_of_image ? 1U : size);
affirm(output_len >= min_size);
@ -2042,7 +1994,7 @@ png_compress_IDAT(png_structrp png_ptr, png_const_voidp input, uInt input_len,
if (output_len > 0U) /* Still got stuff to write */
{
affirm(flush != Z_FINISH && list != NULL);
affirm(!end_of_image && list != NULL);
/* If any compression buffers have been completely written move them
* to the end of the list so that they can be re-used and move
@ -2064,7 +2016,7 @@ png_compress_IDAT(png_structrp png_ptr, png_const_voidp input, uInt input_len,
else /* output_len == 0U; all compressed data has been written */
{
if (flush == Z_FINISH) /* end of data */
if (end_of_image)
{
png_ptr->zowner = 0U; /* release z_stream */
png_ptr->mode |= PNG_AFTER_IDAT;
@ -2082,7 +2034,78 @@ png_compress_IDAT(png_structrp png_ptr, png_const_voidp input, uInt input_len,
png_ptr->zbuffer_len = 0U;
png_ptr->zbuffer_end = &png_ptr->zbuffer_list;
} /* output_len == 0 */
} /* flush == FINISH || png_ptr->zbuffer_len >= png_ptr->IDAT_size */
}
typedef struct
{
png_compression_bufferp *zbuffer_end; /* 'next' field of current buffer */
png_uint_32 zbuffer_len; /* Length of data in list */
} png_IDAT_compression_state;
static int
png_compress_IDAT_test(png_structrp png_ptr, png_IDAT_compression_state *state,
z_stream *zstream, png_const_voidp input, uInt input_len, int flush)
{
png_uint_32 output_len = 0U;
int ret;
/* The stream must have been claimed: */
affirm(png_ptr->zowner == png_IDAT);
/* z_stream::{next,avail}_out are set by png_compress to point into the
* buffer list. next_in must be set here, avail_in comes from the input_len
* parameter:
*/
zstream->next_in = PNGZ_INPUT_CAST(png_voidcast(const Bytef*, input));
ret = png_compress(png_ptr, zstream, &state->zbuffer_end, input_len,
&output_len, flush);
implies(ret == Z_OK || ret == Z_FINISH, zstream->avail_in == 0U);
zstream->next_in = NULL;
zstream->avail_in = 0U; /* safety */
/* If IDAT_size is set to PNG_UINT_31_MAX the length will be larger, but
* not enough to overflow a png_uint_32.
*/
state->zbuffer_len += output_len;
return ret;
}
static void
png_compress_IDAT(png_structp png_ptr, png_const_voidp input, uInt input_len,
int flush)
{
png_IDAT_compression_state state;
int ret;
state.zbuffer_end = png_ptr->zbuffer_end;
state.zbuffer_len = png_ptr->zbuffer_len;
ret = png_compress_IDAT_test(png_ptr, &state, &png_ptr->zstream, input,
input_len, flush);
png_ptr->zbuffer_end = state.zbuffer_end;
png_ptr->zbuffer_len = state.zbuffer_len;
/* Check the return code. */
if (ret == Z_OK || ret == Z_STREAM_END)
{
/* Z_FINISH should give Z_STREAM_END, everything else should give Z_OK,
* so:
*/
debug((ret == Z_STREAM_END) == (flush == Z_FINISH));
/* At this point png_compress has produced (in total) zbuffer_start +
* zbuffer_len compressed bytes in a list of compression buffers starting
* with zbuffer_list and ending with the buffer containing *zbuffer_end,
* which may be NULL or may point to unused compression buffers.
*
* This code has already written out zbuffer_start bytes from the first
* buffer. Can we write more? If flush is Z_FINISH this is the end of
* the stream and there should be final bytes to write. Otherwise wait
* for IDAT_size bytes before writing a chunk. If nothing is to be
* written this function call is complete.
*/
if (flush == Z_FINISH || png_ptr->zbuffer_len >= png_ptr->IDAT_size)
png_write_IDAT(png_ptr, flush == Z_FINISH);
}
else /* ret != Z_OK && ret != Z_STREAM_END */

View File

@ -496,7 +496,7 @@ option READ_16BIT requires READ enables 16BIT
option READ_TRANSFORMS requires READ
= NO_READ_TRANSFORMS READ_TRANSFORMS_NOT_SUPPORTED
option READ_QUANTIZE requires READ_TRANSFORMS enables TRANFORM_MECH
option READ_QUANTIZE requires READ_TRANSFORMS enables TRANSFORM_MECH
# Read gamma handling. Gamma processing is a core part of libpng and many of
# the capabilities are dependent on libpng performing gamma correction.

View File

@ -116,7 +116,6 @@
#define PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
#define PNG_TEXT_SUPPORTED
#define PNG_TIME_RFC1123_SUPPORTED
#define PNG_TRANFORM_MECH_SUPPORTED
#define PNG_TRANSFORM_MECH_SUPPORTED
#define PNG_UNKNOWN_CHUNKS_SUPPORTED
#define PNG_USER_CHUNKS_SUPPORTED
@ -194,7 +193,7 @@
#define PNG_DEFAULT_GAMMA_ACCURACY 665
#define PNG_DEFAULT_READ_MACROS 1
#define PNG_GAMMA_THRESHOLD_FIXED 153
#define PNG_IDAT_READ_SIZE PNG_ZBUF_SIZE
#define PNG_IDAT_READ_SIZE 4096
#define PNG_INFLATE_BUF_SIZE 1024
#define PNG_MAX_GAMMA_8 11
#define PNG_QUANTIZE_BLUE_BITS 5
@ -206,7 +205,7 @@
#define PNG_USER_CHUNK_MALLOC_MAX 8000000
#define PNG_USER_HEIGHT_MAX 1000000
#define PNG_USER_WIDTH_MAX 1000000
#define PNG_ZBUF_SIZE 8192
#define PNG_ZBUF_SIZE 4096
#define PNG_ZLIB_HEADER <zlib.h>
#define PNG_ZLIB_VERNUM 0
#define PNG_Z_DEFAULT_COMPRESSION (-1)