0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* pngwutil.c - utilities to write a PNG file
0N/A *
0N/A * This file is available under and governed by the GNU General Public
0N/A * License version 2 only, as published by the Free Software Foundation.
0N/A * However, the following notice accompanied the original version of this
0N/A * file and, per its terms, should not be removed:
0N/A *
4418N/A * Last changed in libpng 1.5.4 [July 7, 2011]
4418N/A * Copyright (c) 1998-2011 Glenn Randers-Pehrson
0N/A * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
0N/A * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
4418N/A *
4418N/A * This code is released under the libpng license.
4418N/A * For conditions of distribution and use, see the disclaimer
4418N/A * and license in png.h
0N/A */
0N/A
4418N/A#include "pngpriv.h"
4418N/A
0N/A#ifdef PNG_WRITE_SUPPORTED
0N/A
4418N/A#ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED
0N/A/* Place a 32-bit number into a buffer in PNG byte order. We work
0N/A * with unsigned numbers for convenience, although one supported
0N/A * ancillary chunk uses signed (two's complement) numbers.
0N/A */
0N/Avoid PNGAPI
0N/Apng_save_uint_32(png_bytep buf, png_uint_32 i)
0N/A{
0N/A buf[0] = (png_byte)((i >> 24) & 0xff);
0N/A buf[1] = (png_byte)((i >> 16) & 0xff);
0N/A buf[2] = (png_byte)((i >> 8) & 0xff);
0N/A buf[3] = (png_byte)(i & 0xff);
0N/A}
0N/A
4418N/A#ifdef PNG_SAVE_INT_32_SUPPORTED
0N/A/* The png_save_int_32 function assumes integers are stored in two's
0N/A * complement format. If this isn't the case, then this routine needs to
4418N/A * be modified to write data in two's complement format. Note that,
4418N/A * the following works correctly even if png_int_32 has more than 32 bits
4418N/A * (compare the more complex code required on read for sign extention.)
0N/A */
0N/Avoid PNGAPI
0N/Apng_save_int_32(png_bytep buf, png_int_32 i)
0N/A{
0N/A buf[0] = (png_byte)((i >> 24) & 0xff);
0N/A buf[1] = (png_byte)((i >> 16) & 0xff);
0N/A buf[2] = (png_byte)((i >> 8) & 0xff);
0N/A buf[3] = (png_byte)(i & 0xff);
0N/A}
4418N/A#endif
0N/A
0N/A/* Place a 16-bit number into a buffer in PNG byte order.
0N/A * The parameter is declared unsigned int, not png_uint_16,
0N/A * just to avoid potential problems on pre-ANSI C compilers.
0N/A */
0N/Avoid PNGAPI
0N/Apng_save_uint_16(png_bytep buf, unsigned int i)
0N/A{
0N/A buf[0] = (png_byte)((i >> 8) & 0xff);
0N/A buf[1] = (png_byte)(i & 0xff);
0N/A}
4418N/A#endif
4418N/A
4418N/A/* Simple function to write the signature. If we have already written
4418N/A * the magic bytes of the signature, or more likely, the PNG stream is
4418N/A * being embedded into another stream and doesn't need its own signature,
4418N/A * we should call png_set_sig_bytes() to tell libpng how many of the
4418N/A * bytes have already been written.
4418N/A */
4418N/Avoid PNGAPI
4418N/Apng_write_sig(png_structp png_ptr)
4418N/A{
4418N/A png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
4418N/A
4418N/A#ifdef PNG_IO_STATE_SUPPORTED
4418N/A /* Inform the I/O callback that the signature is being written */
4418N/A png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
4418N/A#endif
4418N/A
4418N/A /* Write the rest of the 8 byte signature */
4418N/A png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
4418N/A (png_size_t)(8 - png_ptr->sig_bytes));
4418N/A
4418N/A if (png_ptr->sig_bytes < 3)
4418N/A png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
4418N/A}
0N/A
0N/A/* Write a PNG chunk all at once. The type is an array of ASCII characters
0N/A * representing the chunk name. The array must be at least 4 bytes in
0N/A * length, and does not need to be null terminated. To be safe, pass the
0N/A * pre-defined chunk names here, and if you need a new one, define it
0N/A * where the others are defined. The length is the length of the data.
0N/A * All the data must be present. If that is not possible, use the
0N/A * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
0N/A * functions instead.
0N/A */
0N/Avoid PNGAPI
4418N/Apng_write_chunk(png_structp png_ptr, png_const_bytep chunk_name,
4418N/A png_const_bytep data, png_size_t length)
0N/A{
4418N/A if (png_ptr == NULL)
4418N/A return;
4418N/A
0N/A png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
4418N/A png_write_chunk_data(png_ptr, data, (png_size_t)length);
0N/A png_write_chunk_end(png_ptr);
0N/A}
0N/A
0N/A/* Write the start of a PNG chunk. The type is the chunk type.
0N/A * The total_length is the sum of the lengths of all the data you will be
0N/A * passing in png_write_chunk_data().
0N/A */
0N/Avoid PNGAPI
4418N/Apng_write_chunk_start(png_structp png_ptr, png_const_bytep chunk_name,
4418N/A png_uint_32 length)
0N/A{
4418N/A png_byte buf[8];
4418N/A
4418N/A png_debug2(0, "Writing %s chunk, length = %lu", chunk_name,
4418N/A (unsigned long)length);
4418N/A
4418N/A if (png_ptr == NULL)
4418N/A return;
4418N/A
4418N/A#ifdef PNG_IO_STATE_SUPPORTED
4418N/A /* Inform the I/O callback that the chunk header is being written.
4418N/A * PNG_IO_CHUNK_HDR requires a single I/O call.
4418N/A */
4418N/A png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
4418N/A#endif
4418N/A
4418N/A /* Write the length and the chunk name */
0N/A png_save_uint_32(buf, length);
4418N/A png_memcpy(buf + 4, chunk_name, 4);
4418N/A png_write_data(png_ptr, buf, (png_size_t)8);
4418N/A
4418N/A /* Put the chunk name into png_ptr->chunk_name */
4418N/A png_memcpy(png_ptr->chunk_name, chunk_name, 4);
4418N/A
4418N/A /* Reset the crc and run it over the chunk name */
0N/A png_reset_crc(png_ptr);
4418N/A
4418N/A png_calculate_crc(png_ptr, chunk_name, 4);
4418N/A
4418N/A#ifdef PNG_IO_STATE_SUPPORTED
4418N/A /* Inform the I/O callback that chunk data will (possibly) be written.
4418N/A * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
4418N/A */
4418N/A png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
4418N/A#endif
0N/A}
0N/A
0N/A/* Write the data of a PNG chunk started with png_write_chunk_start().
0N/A * Note that multiple calls to this function are allowed, and that the
0N/A * sum of the lengths from these calls *must* add up to the total_length
0N/A * given to png_write_chunk_start().
0N/A */
0N/Avoid PNGAPI
4418N/Apng_write_chunk_data(png_structp png_ptr, png_const_bytep data,
4418N/A png_size_t length)
0N/A{
4418N/A /* Write the data, and run the CRC over it */
4418N/A if (png_ptr == NULL)
4418N/A return;
4418N/A
0N/A if (data != NULL && length > 0)
0N/A {
4418N/A png_write_data(png_ptr, data, length);
4418N/A
4418N/A /* Update the CRC after writing the data,
4418N/A * in case that the user I/O routine alters it.
4418N/A */
0N/A png_calculate_crc(png_ptr, data, length);
0N/A }
0N/A}
0N/A
0N/A/* Finish a chunk started with png_write_chunk_start(). */
0N/Avoid PNGAPI
0N/Apng_write_chunk_end(png_structp png_ptr)
0N/A{
0N/A png_byte buf[4];
0N/A
4418N/A if (png_ptr == NULL) return;
4418N/A
4418N/A#ifdef PNG_IO_STATE_SUPPORTED
4418N/A /* Inform the I/O callback that the chunk CRC is being written.
4418N/A * PNG_IO_CHUNK_CRC requires a single I/O function call.
4418N/A */
4418N/A png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
4418N/A#endif
4418N/A
4418N/A /* Write the crc in a single operation */
0N/A png_save_uint_32(buf, png_ptr->crc);
0N/A
0N/A png_write_data(png_ptr, buf, (png_size_t)4);
0N/A}
0N/A
4418N/A/* Initialize the compressor for the appropriate type of compression. */
4418N/Astatic void
4418N/Apng_zlib_claim(png_structp png_ptr, png_uint_32 state)
0N/A{
4418N/A if (!(png_ptr->zlib_state & PNG_ZLIB_IN_USE))
4418N/A {
4418N/A /* If already initialized for 'state' do not re-init. */
4418N/A if (png_ptr->zlib_state != state)
4418N/A {
4418N/A int ret = Z_OK;
4418N/A png_const_charp who = "-";
4418N/A
4418N/A /* If actually initialized for another state do a deflateEnd. */
4418N/A if (png_ptr->zlib_state != PNG_ZLIB_UNINITIALIZED)
4418N/A {
4418N/A ret = deflateEnd(&png_ptr->zstream);
4418N/A who = "end";
4418N/A png_ptr->zlib_state = PNG_ZLIB_UNINITIALIZED;
4418N/A }
4418N/A
4418N/A /* zlib itself detects an incomplete state on deflateEnd */
4418N/A if (ret == Z_OK) switch (state)
4418N/A {
4418N/A# ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
4418N/A case PNG_ZLIB_FOR_TEXT:
4418N/A ret = deflateInit2(&png_ptr->zstream,
4418N/A png_ptr->zlib_text_level, png_ptr->zlib_text_method,
4418N/A png_ptr->zlib_text_window_bits,
4418N/A png_ptr->zlib_text_mem_level, png_ptr->zlib_text_strategy);
4418N/A who = "text";
4418N/A break;
4418N/A# endif
4418N/A
4418N/A case PNG_ZLIB_FOR_IDAT:
4418N/A ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
4418N/A png_ptr->zlib_method, png_ptr->zlib_window_bits,
4418N/A png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
4418N/A who = "IDAT";
4418N/A break;
4418N/A
4418N/A default:
4418N/A png_error(png_ptr, "invalid zlib state");
4418N/A }
4418N/A
4418N/A if (ret == Z_OK)
4418N/A png_ptr->zlib_state = state;
4418N/A
4418N/A else /* an error in deflateEnd or deflateInit2 */
4418N/A {
4418N/A size_t pos = 0;
4418N/A char msg[64];
4418N/A
4418N/A pos = png_safecat(msg, sizeof msg, pos,
4418N/A "zlib failed to initialize compressor (");
4418N/A pos = png_safecat(msg, sizeof msg, pos, who);
4418N/A
4418N/A switch (ret)
4418N/A {
4418N/A case Z_VERSION_ERROR:
4418N/A pos = png_safecat(msg, sizeof msg, pos, ") version error");
4418N/A break;
4418N/A
4418N/A case Z_STREAM_ERROR:
4418N/A pos = png_safecat(msg, sizeof msg, pos, ") stream error");
4418N/A break;
4418N/A
4418N/A case Z_MEM_ERROR:
4418N/A pos = png_safecat(msg, sizeof msg, pos, ") memory error");
4418N/A break;
4418N/A
4418N/A default:
4418N/A pos = png_safecat(msg, sizeof msg, pos, ") unknown error");
4418N/A break;
4418N/A }
4418N/A
4418N/A png_error(png_ptr, msg);
4418N/A }
4418N/A }
4418N/A
4418N/A /* Here on success, claim the zstream: */
4418N/A png_ptr->zlib_state |= PNG_ZLIB_IN_USE;
4418N/A }
4418N/A
4418N/A else
4418N/A png_error(png_ptr, "zstream already in use (internal error)");
0N/A}
0N/A
4418N/A/* The opposite: release the stream. It is also reset, this API will warn on
4418N/A * error but will not fail.
4418N/A */
4418N/Astatic void
4418N/Apng_zlib_release(png_structp png_ptr)
4418N/A{
4418N/A if (png_ptr->zlib_state & PNG_ZLIB_IN_USE)
4418N/A {
4418N/A int ret = deflateReset(&png_ptr->zstream);
4418N/A
4418N/A png_ptr->zlib_state &= ~PNG_ZLIB_IN_USE;
4418N/A
4418N/A if (ret != Z_OK)
4418N/A {
4418N/A png_const_charp err;
4418N/A PNG_WARNING_PARAMETERS(p)
4418N/A
4418N/A switch (ret)
4418N/A {
4418N/A case Z_VERSION_ERROR:
4418N/A err = "version";
4418N/A break;
4418N/A
4418N/A case Z_STREAM_ERROR:
4418N/A err = "stream";
4418N/A break;
4418N/A
4418N/A case Z_MEM_ERROR:
4418N/A err = "memory";
4418N/A break;
4418N/A
4418N/A default:
4418N/A err = "unknown";
4418N/A break;
4418N/A }
4418N/A
4418N/A png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_d, ret);
4418N/A png_warning_parameter(p, 2, err);
4418N/A
4418N/A if (png_ptr->zstream.msg)
4418N/A err = png_ptr->zstream.msg;
4418N/A else
4418N/A err = "[no zlib message]";
4418N/A
4418N/A png_warning_parameter(p, 3, err);
4418N/A
4418N/A png_formatted_warning(png_ptr, p,
4418N/A "zlib failed to reset compressor: @1(@2): @3");
4418N/A }
4418N/A }
4418N/A
4418N/A else
4418N/A png_warning(png_ptr, "zstream not in use (internal error)");
4418N/A}
4418N/A
4418N/A#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
4418N/A/* This pair of functions encapsulates the operation of (a) compressing a
0N/A * text string, and (b) issuing it later as a series of chunk data writes.
0N/A * The compression_state structure is shared context for these functions
0N/A * set up by the caller in order to make the whole mess thread-safe.
0N/A */
0N/A
0N/Atypedef struct
0N/A{
4418N/A png_const_bytep input; /* The uncompressed input data */
4418N/A png_size_t input_len; /* Its length */
4418N/A int num_output_ptr; /* Number of output pointers used */
4418N/A int max_output_ptr; /* Size of output_ptr */
4418N/A png_bytep *output_ptr; /* Array of pointers to output */
0N/A} compression_state;
0N/A
4418N/A/* Compress given text into storage in the png_ptr structure */
0N/Astatic int /* PRIVATE */
0N/Apng_text_compress(png_structp png_ptr,
4418N/A png_const_charp text, png_size_t text_len, int compression,
4418N/A compression_state *comp)
0N/A{
0N/A int ret;
0N/A
0N/A comp->num_output_ptr = 0;
0N/A comp->max_output_ptr = 0;
0N/A comp->output_ptr = NULL;
0N/A comp->input = NULL;
4418N/A comp->input_len = text_len;
4418N/A
4418N/A /* We may just want to pass the text right through */
0N/A if (compression == PNG_TEXT_COMPRESSION_NONE)
0N/A {
4418N/A comp->input = (png_const_bytep)text;
4418N/A return((int)text_len);
0N/A }
0N/A
0N/A if (compression >= PNG_TEXT_COMPRESSION_LAST)
0N/A {
4418N/A PNG_WARNING_PARAMETERS(p)
4418N/A
4418N/A png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_d,
4418N/A compression);
4418N/A png_formatted_warning(png_ptr, p, "Unknown compression type @1");
0N/A }
0N/A
0N/A /* We can't write the chunk until we find out how much data we have,
0N/A * which means we need to run the compressor first and save the
0N/A * output. This shouldn't be a problem, as the vast majority of
0N/A * comments should be reasonable, but we will set up an array of
0N/A * malloc'd pointers to be sure.
0N/A *
0N/A * If we knew the application was well behaved, we could simplify this
0N/A * greatly by assuming we can always malloc an output buffer large
0N/A * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
0N/A * and malloc this directly. The only time this would be a bad idea is
0N/A * if we can't malloc more than 64K and we have 64K of random input
0N/A * data, or if the input string is incredibly large (although this
0N/A * wouldn't cause a failure, just a slowdown due to swapping).
0N/A */
4418N/A png_zlib_claim(png_ptr, PNG_ZLIB_FOR_TEXT);
4418N/A
4418N/A /* Set up the compression buffers */
4418N/A /* TODO: the following cast hides a potential overflow problem. */
0N/A png_ptr->zstream.avail_in = (uInt)text_len;
4418N/A
4418N/A /* NOTE: assume zlib doesn't overwrite the input */
0N/A png_ptr->zstream.next_in = (Bytef *)text;
4418N/A png_ptr->zstream.avail_out = png_ptr->zbuf_size;
4418N/A png_ptr->zstream.next_out = png_ptr->zbuf;
4418N/A
4418N/A /* This is the same compression loop as in png_write_row() */
0N/A do
0N/A {
4418N/A /* Compress the data */
0N/A ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
4418N/A
0N/A if (ret != Z_OK)
0N/A {
4418N/A /* Error */
0N/A if (png_ptr->zstream.msg != NULL)
0N/A png_error(png_ptr, png_ptr->zstream.msg);
4418N/A
0N/A else
0N/A png_error(png_ptr, "zlib error");
0N/A }
4418N/A
4418N/A /* Check to see if we need more room */
0N/A if (!(png_ptr->zstream.avail_out))
0N/A {
4418N/A /* Make sure the output array has room */
0N/A if (comp->num_output_ptr >= comp->max_output_ptr)
0N/A {
0N/A int old_max;
0N/A
0N/A old_max = comp->max_output_ptr;
0N/A comp->max_output_ptr = comp->num_output_ptr + 4;
0N/A if (comp->output_ptr != NULL)
0N/A {
4418N/A png_bytepp old_ptr;
0N/A
0N/A old_ptr = comp->output_ptr;
4418N/A
4418N/A comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
4418N/A (png_alloc_size_t)
4418N/A (comp->max_output_ptr * png_sizeof(png_charpp)));
4418N/A
0N/A png_memcpy(comp->output_ptr, old_ptr, old_max
4418N/A * png_sizeof(png_charp));
4418N/A
0N/A png_free(png_ptr, old_ptr);
0N/A }
0N/A else
4418N/A comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
4418N/A (png_alloc_size_t)
4418N/A (comp->max_output_ptr * png_sizeof(png_charp)));
0N/A }
0N/A
4418N/A /* Save the data */
4418N/A comp->output_ptr[comp->num_output_ptr] =
4418N/A (png_bytep)png_malloc(png_ptr,
4418N/A (png_alloc_size_t)png_ptr->zbuf_size);
4418N/A
0N/A png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
4418N/A png_ptr->zbuf_size);
4418N/A
0N/A comp->num_output_ptr++;
0N/A
0N/A /* and reset the buffer */
0N/A png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
0N/A png_ptr->zstream.next_out = png_ptr->zbuf;
0N/A }
4418N/A /* Continue until we don't have any more to compress */
0N/A } while (png_ptr->zstream.avail_in);
0N/A
4418N/A /* Finish the compression */
0N/A do
0N/A {
4418N/A /* Tell zlib we are finished */
0N/A ret = deflate(&png_ptr->zstream, Z_FINISH);
0N/A
0N/A if (ret == Z_OK)
0N/A {
4418N/A /* Check to see if we need more room */
0N/A if (!(png_ptr->zstream.avail_out))
0N/A {
4418N/A /* Check to make sure our output array has room */
0N/A if (comp->num_output_ptr >= comp->max_output_ptr)
0N/A {
0N/A int old_max;
0N/A
0N/A old_max = comp->max_output_ptr;
0N/A comp->max_output_ptr = comp->num_output_ptr + 4;
0N/A if (comp->output_ptr != NULL)
0N/A {
4418N/A png_bytepp old_ptr;
0N/A
0N/A old_ptr = comp->output_ptr;
4418N/A
0N/A /* This could be optimized to realloc() */
4418N/A comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
4418N/A (png_alloc_size_t)(comp->max_output_ptr *
4418N/A png_sizeof(png_charp)));
4418N/A
0N/A png_memcpy(comp->output_ptr, old_ptr,
4418N/A old_max * png_sizeof(png_charp));
4418N/A
0N/A png_free(png_ptr, old_ptr);
0N/A }
4418N/A
0N/A else
4418N/A comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
4418N/A (png_alloc_size_t)(comp->max_output_ptr *
4418N/A png_sizeof(png_charp)));
0N/A }
0N/A
4418N/A /* Save the data */
0N/A comp->output_ptr[comp->num_output_ptr] =
4418N/A (png_bytep)png_malloc(png_ptr,
4418N/A (png_alloc_size_t)png_ptr->zbuf_size);
4418N/A
0N/A png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
4418N/A png_ptr->zbuf_size);
4418N/A
0N/A comp->num_output_ptr++;
0N/A
0N/A /* and reset the buffer pointers */
0N/A png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
0N/A png_ptr->zstream.next_out = png_ptr->zbuf;
0N/A }
0N/A }
0N/A else if (ret != Z_STREAM_END)
0N/A {
4418N/A /* We got an error */
0N/A if (png_ptr->zstream.msg != NULL)
0N/A png_error(png_ptr, png_ptr->zstream.msg);
4418N/A
0N/A else
0N/A png_error(png_ptr, "zlib error");
0N/A }
0N/A } while (ret != Z_STREAM_END);
0N/A
4418N/A /* Text length is number of buffers plus last buffer */
0N/A text_len = png_ptr->zbuf_size * comp->num_output_ptr;
4418N/A
0N/A if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
0N/A text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
0N/A
0N/A return((int)text_len);
0N/A}
0N/A
4418N/A/* Ship the compressed text out via chunk writes */
0N/Astatic void /* PRIVATE */
0N/Apng_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
0N/A{
0N/A int i;
0N/A
4418N/A /* Handle the no-compression case */
0N/A if (comp->input)
0N/A {
4418N/A png_write_chunk_data(png_ptr, comp->input, comp->input_len);
4418N/A
4418N/A return;
0N/A }
0N/A
4418N/A#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
4418N/A if (comp->input_len >= 2 && comp->input_len < 16384)
4418N/A {
4418N/A unsigned int z_cmf; /* zlib compression method and flags */
4418N/A
4418N/A /* Optimize the CMF field in the zlib stream. This hack of the zlib
4418N/A * stream is compliant to the stream specification.
4418N/A */
4418N/A
4418N/A if (comp->num_output_ptr)
4418N/A z_cmf = comp->output_ptr[0][0];
4418N/A else
4418N/A z_cmf = png_ptr->zbuf[0];
4418N/A
4418N/A if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
4418N/A {
4418N/A unsigned int z_cinfo;
4418N/A unsigned int half_z_window_size;
4418N/A png_size_t uncompressed_text_size = comp->input_len;
4418N/A
4418N/A z_cinfo = z_cmf >> 4;
4418N/A half_z_window_size = 1 << (z_cinfo + 7);
4418N/A
4418N/A while (uncompressed_text_size <= half_z_window_size &&
4418N/A half_z_window_size >= 256)
4418N/A {
4418N/A z_cinfo--;
4418N/A half_z_window_size >>= 1;
4418N/A }
4418N/A
4418N/A z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
4418N/A
4418N/A if (comp->num_output_ptr)
4418N/A {
4418N/A
4418N/A if (comp->output_ptr[0][0] != z_cmf)
4418N/A {
4418N/A int tmp;
4418N/A
4418N/A comp->output_ptr[0][0] = (png_byte)z_cmf;
4418N/A tmp = comp->output_ptr[0][1] & 0xe0;
4418N/A tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
4418N/A comp->output_ptr[0][1] = (png_byte)tmp;
4418N/A }
4418N/A }
4418N/A else
4418N/A {
4418N/A int tmp;
4418N/A
4418N/A png_ptr->zbuf[0] = (png_byte)z_cmf;
4418N/A tmp = png_ptr->zbuf[1] & 0xe0;
4418N/A tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
4418N/A png_ptr->zbuf[1] = (png_byte)tmp;
4418N/A }
4418N/A }
4418N/A
4418N/A else
4418N/A png_error(png_ptr,
4418N/A "Invalid zlib compression method or flags in non-IDAT chunk");
4418N/A }
4418N/A#endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */
4418N/A
4418N/A /* Write saved output buffers, if any */
0N/A for (i = 0; i < comp->num_output_ptr; i++)
0N/A {
4418N/A png_write_chunk_data(png_ptr, comp->output_ptr[i],
4418N/A (png_size_t)png_ptr->zbuf_size);
4418N/A
0N/A png_free(png_ptr, comp->output_ptr[i]);
0N/A }
4418N/A
0N/A if (comp->max_output_ptr != 0)
0N/A png_free(png_ptr, comp->output_ptr);
4418N/A
4418N/A /* Write anything left in zbuf */
0N/A if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
0N/A png_write_chunk_data(png_ptr, png_ptr->zbuf,
4418N/A (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
4418N/A
4418N/A /* Reset zlib for another zTXt/iTXt or image data */
4418N/A png_zlib_release(png_ptr);
0N/A}
4418N/A#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
0N/A
0N/A/* Write the IHDR chunk, and update the png_struct with the necessary
0N/A * information. Note that the rest of this code depends upon this
0N/A * information being correct.
0N/A */
0N/Avoid /* PRIVATE */
0N/Apng_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
4418N/A int bit_depth, int color_type, int compression_type, int filter_type,
4418N/A int interlace_type)
0N/A{
0N/A PNG_IHDR;
4418N/A
4418N/A png_byte buf[13]; /* Buffer to store the IHDR info */
4418N/A
4418N/A png_debug(1, "in png_write_IHDR");
4418N/A
0N/A /* Check that we have valid input data from the application info */
0N/A switch (color_type)
0N/A {
0N/A case PNG_COLOR_TYPE_GRAY:
0N/A switch (bit_depth)
0N/A {
0N/A case 1:
0N/A case 2:
0N/A case 4:
0N/A case 8:
4418N/A#ifdef PNG_WRITE_16BIT_SUPPORTED
4418N/A case 16:
4418N/A#endif
4418N/A png_ptr->channels = 1; break;
4418N/A
4418N/A default:
4418N/A png_error(png_ptr,
4418N/A "Invalid bit depth for grayscale image");
0N/A }
0N/A break;
4418N/A
0N/A case PNG_COLOR_TYPE_RGB:
4418N/A#ifdef PNG_WRITE_16BIT_SUPPORTED
0N/A if (bit_depth != 8 && bit_depth != 16)
4418N/A#else
4418N/A if (bit_depth != 8)
4418N/A#endif
0N/A png_error(png_ptr, "Invalid bit depth for RGB image");
4418N/A
0N/A png_ptr->channels = 3;
0N/A break;
4418N/A
0N/A case PNG_COLOR_TYPE_PALETTE:
0N/A switch (bit_depth)
0N/A {
0N/A case 1:
0N/A case 2:
0N/A case 4:
4418N/A case 8:
4418N/A png_ptr->channels = 1;
4418N/A break;
4418N/A
4418N/A default:
4418N/A png_error(png_ptr, "Invalid bit depth for paletted image");
0N/A }
0N/A break;
4418N/A
0N/A case PNG_COLOR_TYPE_GRAY_ALPHA:
0N/A if (bit_depth != 8 && bit_depth != 16)
0N/A png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
4418N/A
0N/A png_ptr->channels = 2;
0N/A break;
4418N/A
0N/A case PNG_COLOR_TYPE_RGB_ALPHA:
4418N/A#ifdef PNG_WRITE_16BIT_SUPPORTED
0N/A if (bit_depth != 8 && bit_depth != 16)
4418N/A#else
4418N/A if (bit_depth != 8)
4418N/A#endif
0N/A png_error(png_ptr, "Invalid bit depth for RGBA image");
4418N/A
0N/A png_ptr->channels = 4;
0N/A break;
4418N/A
0N/A default:
0N/A png_error(png_ptr, "Invalid image color type specified");
0N/A }
0N/A
0N/A if (compression_type != PNG_COMPRESSION_TYPE_BASE)
0N/A {
0N/A png_warning(png_ptr, "Invalid compression type specified");
0N/A compression_type = PNG_COMPRESSION_TYPE_BASE;
0N/A }
0N/A
0N/A /* Write filter_method 64 (intrapixel differencing) only if
0N/A * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
0N/A * 2. Libpng did not write a PNG signature (this filter_method is only
0N/A * used in PNG datastreams that are embedded in MNG datastreams) and
0N/A * 3. The application called png_permit_mng_features with a mask that
0N/A * included PNG_FLAG_MNG_FILTER_64 and
0N/A * 4. The filter_method is 64 and
0N/A * 5. The color_type is RGB or RGBA
0N/A */
0N/A if (
4418N/A#ifdef PNG_MNG_FEATURES_SUPPORTED
4418N/A !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
4418N/A ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
4418N/A (color_type == PNG_COLOR_TYPE_RGB ||
4418N/A color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
4418N/A (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
0N/A#endif
4418N/A filter_type != PNG_FILTER_TYPE_BASE)
0N/A {
0N/A png_warning(png_ptr, "Invalid filter type specified");
0N/A filter_type = PNG_FILTER_TYPE_BASE;
0N/A }
0N/A
0N/A#ifdef PNG_WRITE_INTERLACING_SUPPORTED
0N/A if (interlace_type != PNG_INTERLACE_NONE &&
4418N/A interlace_type != PNG_INTERLACE_ADAM7)
0N/A {
0N/A png_warning(png_ptr, "Invalid interlace type specified");
0N/A interlace_type = PNG_INTERLACE_ADAM7;
0N/A }
0N/A#else
0N/A interlace_type=PNG_INTERLACE_NONE;
0N/A#endif
0N/A
4418N/A /* Save the relevent information */
0N/A png_ptr->bit_depth = (png_byte)bit_depth;
0N/A png_ptr->color_type = (png_byte)color_type;
0N/A png_ptr->interlaced = (png_byte)interlace_type;
4418N/A#ifdef PNG_MNG_FEATURES_SUPPORTED
0N/A png_ptr->filter_type = (png_byte)filter_type;
0N/A#endif
0N/A png_ptr->compression_type = (png_byte)compression_type;
0N/A png_ptr->width = width;
0N/A png_ptr->height = height;
0N/A
0N/A png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
0N/A png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
4418N/A /* Set the usr info, so any transformations can modify it */
0N/A png_ptr->usr_width = png_ptr->width;
0N/A png_ptr->usr_bit_depth = png_ptr->bit_depth;
0N/A png_ptr->usr_channels = png_ptr->channels;
0N/A
4418N/A /* Pack the header information into the buffer */
0N/A png_save_uint_32(buf, width);
0N/A png_save_uint_32(buf + 4, height);
0N/A buf[8] = (png_byte)bit_depth;
0N/A buf[9] = (png_byte)color_type;
0N/A buf[10] = (png_byte)compression_type;
0N/A buf[11] = (png_byte)filter_type;
0N/A buf[12] = (png_byte)interlace_type;
0N/A
4418N/A /* Write the chunk */
4418N/A png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
4418N/A
4418N/A /* Initialize zlib with PNG info */
0N/A png_ptr->zstream.zalloc = png_zalloc;
0N/A png_ptr->zstream.zfree = png_zfree;
0N/A png_ptr->zstream.opaque = (voidpf)png_ptr;
4418N/A
0N/A if (!(png_ptr->do_filter))
0N/A {
0N/A if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
4418N/A png_ptr->bit_depth < 8)
0N/A png_ptr->do_filter = PNG_FILTER_NONE;
4418N/A
0N/A else
0N/A png_ptr->do_filter = PNG_ALL_FILTERS;
0N/A }
4418N/A
0N/A if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
0N/A {
0N/A if (png_ptr->do_filter != PNG_FILTER_NONE)
0N/A png_ptr->zlib_strategy = Z_FILTERED;
4418N/A
0N/A else
0N/A png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
0N/A }
4418N/A
0N/A if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
0N/A png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
4418N/A
0N/A if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
0N/A png_ptr->zlib_mem_level = 8;
4418N/A
0N/A if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
0N/A png_ptr->zlib_window_bits = 15;
4418N/A
0N/A if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
0N/A png_ptr->zlib_method = 8;
4418N/A
4418N/A#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
4418N/A#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
4418N/A if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_STRATEGY))
4418N/A png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
4418N/A
4418N/A if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_LEVEL))
4418N/A png_ptr->zlib_text_level = png_ptr->zlib_level;
4418N/A
4418N/A if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL))
4418N/A png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
4418N/A
4418N/A if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS))
4418N/A png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
4418N/A
4418N/A if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_METHOD))
4418N/A png_ptr->zlib_text_method = png_ptr->zlib_method;
4418N/A#else
4418N/A png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
4418N/A png_ptr->zlib_text_level = png_ptr->zlib_level;
4418N/A png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
4418N/A png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
4418N/A png_ptr->zlib_text_method = png_ptr->zlib_method;
4418N/A#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
4418N/A#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
4418N/A
4418N/A /* Record that the compressor has not yet been initialized. */
4418N/A png_ptr->zlib_state = PNG_ZLIB_UNINITIALIZED;
4418N/A
4418N/A png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */
0N/A}
0N/A
4418N/A/* Write the palette. We are careful not to trust png_color to be in the
0N/A * correct order for PNG, so people can redefine it to any convenient
0N/A * structure.
0N/A */
0N/Avoid /* PRIVATE */
4418N/Apng_write_PLTE(png_structp png_ptr, png_const_colorp palette,
4418N/A png_uint_32 num_pal)
0N/A{
0N/A PNG_PLTE;
0N/A png_uint_32 i;
4418N/A png_const_colorp pal_ptr;
0N/A png_byte buf[3];
0N/A
4418N/A png_debug(1, "in png_write_PLTE");
4418N/A
0N/A if ((
4418N/A#ifdef PNG_MNG_FEATURES_SUPPORTED
4418N/A !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
0N/A#endif
4418N/A num_pal == 0) || num_pal > 256)
0N/A {
4418N/A if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4418N/A {
4418N/A png_error(png_ptr, "Invalid number of colors in palette");
4418N/A }
4418N/A
4418N/A else
4418N/A {
4418N/A png_warning(png_ptr, "Invalid number of colors in palette");
4418N/A return;
4418N/A }
0N/A }
0N/A
0N/A if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
0N/A {
0N/A png_warning(png_ptr,
4418N/A "Ignoring request to write a PLTE chunk in grayscale PNG");
4418N/A
0N/A return;
0N/A }
0N/A
0N/A png_ptr->num_palette = (png_uint_16)num_pal;
4418N/A png_debug1(3, "num_palette = %d", png_ptr->num_palette);
4418N/A
4418N/A png_write_chunk_start(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3));
4418N/A#ifdef PNG_POINTER_INDEXING_SUPPORTED
4418N/A
0N/A for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
0N/A {
0N/A buf[0] = pal_ptr->red;
0N/A buf[1] = pal_ptr->green;
0N/A buf[2] = pal_ptr->blue;
0N/A png_write_chunk_data(png_ptr, buf, (png_size_t)3);
0N/A }
4418N/A
0N/A#else
4418N/A /* This is a little slower but some buggy compilers need to do this
4418N/A * instead
4418N/A */
0N/A pal_ptr=palette;
4418N/A
0N/A for (i = 0; i < num_pal; i++)
0N/A {
0N/A buf[0] = pal_ptr[i].red;
0N/A buf[1] = pal_ptr[i].green;
0N/A buf[2] = pal_ptr[i].blue;
0N/A png_write_chunk_data(png_ptr, buf, (png_size_t)3);
0N/A }
4418N/A
0N/A#endif
0N/A png_write_chunk_end(png_ptr);
0N/A png_ptr->mode |= PNG_HAVE_PLTE;
0N/A}
0N/A
4418N/A/* Write an IDAT chunk */
0N/Avoid /* PRIVATE */
0N/Apng_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
0N/A{
0N/A PNG_IDAT;
4418N/A
4418N/A png_debug(1, "in png_write_IDAT");
4418N/A
4418N/A#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
0N/A if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
0N/A png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
0N/A {
4418N/A /* Optimize the CMF field in the zlib stream. This hack of the zlib
4418N/A * stream is compliant to the stream specification.
4418N/A */
0N/A unsigned int z_cmf = data[0]; /* zlib compression method and flags */
4418N/A
0N/A if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
0N/A {
4418N/A /* Avoid memory underflows and multiplication overflows.
4418N/A *
4418N/A * The conditions below are practically always satisfied;
4418N/A * however, they still must be checked.
4418N/A */
0N/A if (length >= 2 &&
0N/A png_ptr->height < 16384 && png_ptr->width < 16384)
0N/A {
4418N/A /* Compute the maximum possible length of the datastream */
4418N/A
4418N/A /* Number of pixels, plus for each row a filter byte
4418N/A * and possibly a padding byte, so increase the maximum
4418N/A * size to account for these.
4418N/A */
4418N/A unsigned int z_cinfo;
4418N/A unsigned int half_z_window_size;
0N/A png_uint_32 uncompressed_idat_size = png_ptr->height *
4418N/A ((png_ptr->width *
4418N/A png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
4418N/A
4418N/A /* If it's interlaced, each block of 8 rows is sent as up to
4418N/A * 14 rows, i.e., 6 additional rows, each with a filter byte
4418N/A * and possibly a padding byte
4418N/A */
4418N/A if (png_ptr->interlaced)
4418N/A uncompressed_idat_size += ((png_ptr->height + 7)/8) *
4418N/A (png_ptr->bit_depth < 8 ? 12 : 6);
4418N/A
4418N/A z_cinfo = z_cmf >> 4;
4418N/A half_z_window_size = 1 << (z_cinfo + 7);
4418N/A
0N/A while (uncompressed_idat_size <= half_z_window_size &&
4418N/A half_z_window_size >= 256)
0N/A {
0N/A z_cinfo--;
0N/A half_z_window_size >>= 1;
0N/A }
4418N/A
0N/A z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
4418N/A
4418N/A if (data[0] != z_cmf)
0N/A {
4418N/A int tmp;
0N/A data[0] = (png_byte)z_cmf;
4418N/A tmp = data[1] & 0xe0;
4418N/A tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
4418N/A data[1] = (png_byte)tmp;
0N/A }
0N/A }
0N/A }
4418N/A
0N/A else
0N/A png_error(png_ptr,
4418N/A "Invalid zlib compression method or flags in IDAT");
0N/A }
4418N/A#endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */
4418N/A
4418N/A png_write_chunk(png_ptr, png_IDAT, data, length);
0N/A png_ptr->mode |= PNG_HAVE_IDAT;
4418N/A
4418N/A /* Prior to 1.5.4 this code was replicated in every caller (except at the
4418N/A * end, where it isn't technically necessary). Since this function has
4418N/A * flushed the data we can safely reset the zlib output buffer here.
4418N/A */
4418N/A png_ptr->zstream.next_out = png_ptr->zbuf;
4418N/A png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
0N/A}
0N/A
4418N/A/* Write an IEND chunk */
0N/Avoid /* PRIVATE */
0N/Apng_write_IEND(png_structp png_ptr)
0N/A{
0N/A PNG_IEND;
4418N/A
4418N/A png_debug(1, "in png_write_IEND");
4418N/A
4418N/A png_write_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
0N/A png_ptr->mode |= PNG_HAVE_IEND;
0N/A}
0N/A
4418N/A#ifdef PNG_WRITE_gAMA_SUPPORTED
4418N/A/* Write a gAMA chunk */
0N/Avoid /* PRIVATE */
0N/Apng_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
0N/A{
0N/A PNG_gAMA;
0N/A png_byte buf[4];
0N/A
4418N/A png_debug(1, "in png_write_gAMA");
4418N/A
0N/A /* file_gamma is saved in 1/100,000ths */
0N/A png_save_uint_32(buf, (png_uint_32)file_gamma);
4418N/A png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
0N/A}
0N/A#endif
4418N/A
4418N/A#ifdef PNG_WRITE_sRGB_SUPPORTED
4418N/A/* Write a sRGB chunk */
0N/Avoid /* PRIVATE */
0N/Apng_write_sRGB(png_structp png_ptr, int srgb_intent)
0N/A{
0N/A PNG_sRGB;
0N/A png_byte buf[1];
0N/A
4418N/A png_debug(1, "in png_write_sRGB");
4418N/A
4418N/A if (srgb_intent >= PNG_sRGB_INTENT_LAST)
4418N/A png_warning(png_ptr,
4418N/A "Invalid sRGB rendering intent specified");
4418N/A
0N/A buf[0]=(png_byte)srgb_intent;
4418N/A png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
0N/A}
0N/A#endif
0N/A
4418N/A#ifdef PNG_WRITE_iCCP_SUPPORTED
4418N/A/* Write an iCCP chunk */
0N/Avoid /* PRIVATE */
4418N/Apng_write_iCCP(png_structp png_ptr, png_const_charp name, int compression_type,
4418N/A png_const_charp profile, int profile_len)
0N/A{
0N/A PNG_iCCP;
0N/A png_size_t name_len;
0N/A png_charp new_name;
0N/A compression_state comp;
0N/A int embedded_profile_len = 0;
0N/A
4418N/A png_debug(1, "in png_write_iCCP");
0N/A
0N/A comp.num_output_ptr = 0;
0N/A comp.max_output_ptr = 0;
0N/A comp.output_ptr = NULL;
0N/A comp.input = NULL;
0N/A comp.input_len = 0;
0N/A
4418N/A if ((name_len = png_check_keyword(png_ptr, name, &new_name)) == 0)
0N/A return;
0N/A
0N/A if (compression_type != PNG_COMPRESSION_TYPE_BASE)
0N/A png_warning(png_ptr, "Unknown compression type in iCCP chunk");
0N/A
0N/A if (profile == NULL)
0N/A profile_len = 0;
0N/A
0N/A if (profile_len > 3)
0N/A embedded_profile_len =
4418N/A ((*( (png_const_bytep)profile ))<<24) |
4418N/A ((*( (png_const_bytep)profile + 1))<<16) |
4418N/A ((*( (png_const_bytep)profile + 2))<< 8) |
4418N/A ((*( (png_const_bytep)profile + 3)) );
4418N/A
4418N/A if (embedded_profile_len < 0)
4418N/A {
4418N/A png_warning(png_ptr,
4418N/A "Embedded profile length in iCCP chunk is negative");
4418N/A
4418N/A png_free(png_ptr, new_name);
4418N/A return;
4418N/A }
0N/A
0N/A if (profile_len < embedded_profile_len)
4418N/A {
4418N/A png_warning(png_ptr,
0N/A "Embedded profile length too large in iCCP chunk");
4418N/A
4418N/A png_free(png_ptr, new_name);
4418N/A return;
4418N/A }
0N/A
0N/A if (profile_len > embedded_profile_len)
4418N/A {
4418N/A png_warning(png_ptr,
0N/A "Truncating profile to actual length in iCCP chunk");
4418N/A
4418N/A profile_len = embedded_profile_len;
4418N/A }
0N/A
0N/A if (profile_len)
4418N/A profile_len = png_text_compress(png_ptr, profile,
4418N/A (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
4418N/A
4418N/A /* Make sure we include the NULL after the name and the compression type */
4418N/A png_write_chunk_start(png_ptr, png_iCCP,
4418N/A (png_uint_32)(name_len + profile_len + 2));
4418N/A
4418N/A new_name[name_len + 1] = 0x00;
4418N/A
4418N/A png_write_chunk_data(png_ptr, (png_bytep)new_name,
4418N/A (png_size_t)(name_len + 2));
0N/A
0N/A if (profile_len)
4418N/A {
4418N/A comp.input_len = profile_len;
0N/A png_write_compressed_data_out(png_ptr, &comp);
4418N/A }
0N/A
0N/A png_write_chunk_end(png_ptr);
0N/A png_free(png_ptr, new_name);
0N/A}
0N/A#endif
0N/A
4418N/A#ifdef PNG_WRITE_sPLT_SUPPORTED
4418N/A/* Write a sPLT chunk */
0N/Avoid /* PRIVATE */
4418N/Apng_write_sPLT(png_structp png_ptr, png_const_sPLT_tp spalette)
0N/A{
0N/A PNG_sPLT;
0N/A png_size_t name_len;
0N/A png_charp new_name;
0N/A png_byte entrybuf[10];
4418N/A png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
4418N/A png_size_t palette_size = entry_size * spalette->nentries;
0N/A png_sPLT_entryp ep;
4418N/A#ifndef PNG_POINTER_INDEXING_SUPPORTED
0N/A int i;
0N/A#endif
0N/A
4418N/A png_debug(1, "in png_write_sPLT");
4418N/A
4418N/A if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0)
0N/A return;
4418N/A
4418N/A /* Make sure we include the NULL after the name */
4418N/A png_write_chunk_start(png_ptr, png_sPLT,
4418N/A (png_uint_32)(name_len + 2 + palette_size));
4418N/A
4418N/A png_write_chunk_data(png_ptr, (png_bytep)new_name,
4418N/A (png_size_t)(name_len + 1));
4418N/A
4418N/A png_write_chunk_data(png_ptr, &spalette->depth, (png_size_t)1);
4418N/A
4418N/A /* Loop through each palette entry, writing appropriately */
4418N/A#ifdef PNG_POINTER_INDEXING_SUPPORTED
4418N/A for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
0N/A {
4418N/A if (spalette->depth == 8)
4418N/A {
4418N/A entrybuf[0] = (png_byte)ep->red;
4418N/A entrybuf[1] = (png_byte)ep->green;
4418N/A entrybuf[2] = (png_byte)ep->blue;
4418N/A entrybuf[3] = (png_byte)ep->alpha;
4418N/A png_save_uint_16(entrybuf + 4, ep->frequency);
4418N/A }
4418N/A
4418N/A else
4418N/A {
4418N/A png_save_uint_16(entrybuf + 0, ep->red);
4418N/A png_save_uint_16(entrybuf + 2, ep->green);
4418N/A png_save_uint_16(entrybuf + 4, ep->blue);
4418N/A png_save_uint_16(entrybuf + 6, ep->alpha);
4418N/A png_save_uint_16(entrybuf + 8, ep->frequency);
4418N/A }
4418N/A
4418N/A png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
0N/A }
0N/A#else
0N/A ep=spalette->entries;
4418N/A for (i = 0; i>spalette->nentries; i++)
0N/A {
4418N/A if (spalette->depth == 8)
4418N/A {
4418N/A entrybuf[0] = (png_byte)ep[i].red;
4418N/A entrybuf[1] = (png_byte)ep[i].green;
4418N/A entrybuf[2] = (png_byte)ep[i].blue;
4418N/A entrybuf[3] = (png_byte)ep[i].alpha;
4418N/A png_save_uint_16(entrybuf + 4, ep[i].frequency);
4418N/A }
4418N/A
4418N/A else
4418N/A {
4418N/A png_save_uint_16(entrybuf + 0, ep[i].red);
4418N/A png_save_uint_16(entrybuf + 2, ep[i].green);
4418N/A png_save_uint_16(entrybuf + 4, ep[i].blue);
4418N/A png_save_uint_16(entrybuf + 6, ep[i].alpha);
4418N/A png_save_uint_16(entrybuf + 8, ep[i].frequency);
4418N/A }
4418N/A
4418N/A png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
0N/A }
0N/A#endif
0N/A
0N/A png_write_chunk_end(png_ptr);
0N/A png_free(png_ptr, new_name);
0N/A}
0N/A#endif
0N/A
4418N/A#ifdef PNG_WRITE_sBIT_SUPPORTED
4418N/A/* Write the sBIT chunk */
0N/Avoid /* PRIVATE */
4418N/Apng_write_sBIT(png_structp png_ptr, png_const_color_8p sbit, int color_type)
0N/A{
0N/A PNG_sBIT;
0N/A png_byte buf[4];
0N/A png_size_t size;
0N/A
4418N/A png_debug(1, "in png_write_sBIT");
4418N/A
4418N/A /* Make sure we don't depend upon the order of PNG_COLOR_8 */
0N/A if (color_type & PNG_COLOR_MASK_COLOR)
0N/A {
0N/A png_byte maxbits;
0N/A
0N/A maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
4418N/A png_ptr->usr_bit_depth);
4418N/A
0N/A if (sbit->red == 0 || sbit->red > maxbits ||
0N/A sbit->green == 0 || sbit->green > maxbits ||
0N/A sbit->blue == 0 || sbit->blue > maxbits)
0N/A {
0N/A png_warning(png_ptr, "Invalid sBIT depth specified");
0N/A return;
0N/A }
4418N/A
0N/A buf[0] = sbit->red;
0N/A buf[1] = sbit->green;
0N/A buf[2] = sbit->blue;
0N/A size = 3;
0N/A }
4418N/A
0N/A else
0N/A {
0N/A if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
0N/A {
0N/A png_warning(png_ptr, "Invalid sBIT depth specified");
0N/A return;
0N/A }
4418N/A
0N/A buf[0] = sbit->gray;
0N/A size = 1;
0N/A }
0N/A
0N/A if (color_type & PNG_COLOR_MASK_ALPHA)
0N/A {
0N/A if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
0N/A {
0N/A png_warning(png_ptr, "Invalid sBIT depth specified");
0N/A return;
0N/A }
4418N/A
0N/A buf[size++] = sbit->alpha;
0N/A }
0N/A
4418N/A png_write_chunk(png_ptr, png_sBIT, buf, size);
0N/A}
0N/A#endif
0N/A
4418N/A#ifdef PNG_WRITE_cHRM_SUPPORTED
4418N/A/* Write the cHRM chunk */
0N/Avoid /* PRIVATE */
4418N/Apng_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
4418N/A png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
4418N/A png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
4418N/A png_fixed_point blue_y)
0N/A{
0N/A PNG_cHRM;
0N/A png_byte buf[32];
4418N/A
4418N/A png_debug(1, "in png_write_cHRM");
4418N/A
4418N/A /* Each value is saved in 1/100,000ths */
4418N/A#ifdef PNG_CHECK_cHRM_SUPPORTED
4418N/A if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y,
4418N/A green_x, green_y, blue_x, blue_y))
4418N/A#endif
0N/A {
4418N/A png_save_uint_32(buf, (png_uint_32)white_x);
4418N/A png_save_uint_32(buf + 4, (png_uint_32)white_y);
4418N/A
4418N/A png_save_uint_32(buf + 8, (png_uint_32)red_x);
4418N/A png_save_uint_32(buf + 12, (png_uint_32)red_y);
4418N/A
4418N/A png_save_uint_32(buf + 16, (png_uint_32)green_x);
4418N/A png_save_uint_32(buf + 20, (png_uint_32)green_y);
4418N/A
4418N/A png_save_uint_32(buf + 24, (png_uint_32)blue_x);
4418N/A png_save_uint_32(buf + 28, (png_uint_32)blue_y);
4418N/A
4418N/A png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
0N/A }
0N/A}
0N/A#endif
4418N/A
4418N/A#ifdef PNG_WRITE_tRNS_SUPPORTED
4418N/A/* Write the tRNS chunk */
0N/Avoid /* PRIVATE */
4418N/Apng_write_tRNS(png_structp png_ptr, png_const_bytep trans_alpha,
4418N/A png_const_color_16p tran, int num_trans, int color_type)
0N/A{
0N/A PNG_tRNS;
0N/A png_byte buf[6];
0N/A
4418N/A png_debug(1, "in png_write_tRNS");
4418N/A
0N/A if (color_type == PNG_COLOR_TYPE_PALETTE)
0N/A {
0N/A if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
0N/A {
4418N/A png_warning(png_ptr, "Invalid number of transparent colors specified");
0N/A return;
0N/A }
4418N/A
4418N/A /* Write the chunk out as it is */
4418N/A png_write_chunk(png_ptr, png_tRNS, trans_alpha, (png_size_t)num_trans);
0N/A }
4418N/A
0N/A else if (color_type == PNG_COLOR_TYPE_GRAY)
0N/A {
4418N/A /* One 16 bit value */
4418N/A if (tran->gray >= (1 << png_ptr->bit_depth))
0N/A {
0N/A png_warning(png_ptr,
4418N/A "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
4418N/A
0N/A return;
0N/A }
4418N/A
0N/A png_save_uint_16(buf, tran->gray);
4418N/A png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
0N/A }
4418N/A
0N/A else if (color_type == PNG_COLOR_TYPE_RGB)
0N/A {
4418N/A /* Three 16 bit values */
0N/A png_save_uint_16(buf, tran->red);
0N/A png_save_uint_16(buf + 2, tran->green);
0N/A png_save_uint_16(buf + 4, tran->blue);
4418N/A#ifdef PNG_WRITE_16BIT_SUPPORTED
4418N/A if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
4418N/A#else
4418N/A if (buf[0] | buf[2] | buf[4])
4418N/A#endif
4418N/A {
4418N/A png_warning(png_ptr,
4418N/A "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
4418N/A return;
4418N/A }
4418N/A
4418N/A png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
0N/A }
4418N/A
0N/A else
0N/A {
0N/A png_warning(png_ptr, "Can't write tRNS with an alpha channel");
0N/A }
0N/A}
0N/A#endif
0N/A
4418N/A#ifdef PNG_WRITE_bKGD_SUPPORTED
4418N/A/* Write the background chunk */
0N/Avoid /* PRIVATE */
4418N/Apng_write_bKGD(png_structp png_ptr, png_const_color_16p back, int color_type)
0N/A{
0N/A PNG_bKGD;
0N/A png_byte buf[6];
0N/A
4418N/A png_debug(1, "in png_write_bKGD");
4418N/A
0N/A if (color_type == PNG_COLOR_TYPE_PALETTE)
0N/A {
0N/A if (
4418N/A#ifdef PNG_MNG_FEATURES_SUPPORTED
0N/A (png_ptr->num_palette ||
0N/A (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
0N/A#endif
4418N/A back->index >= png_ptr->num_palette)
0N/A {
0N/A png_warning(png_ptr, "Invalid background palette index");
0N/A return;
0N/A }
4418N/A
0N/A buf[0] = back->index;
4418N/A png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
0N/A }
4418N/A
0N/A else if (color_type & PNG_COLOR_MASK_COLOR)
0N/A {
0N/A png_save_uint_16(buf, back->red);
0N/A png_save_uint_16(buf + 2, back->green);
0N/A png_save_uint_16(buf + 4, back->blue);
4418N/A#ifdef PNG_WRITE_16BIT_SUPPORTED
4418N/A if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
4418N/A#else
4418N/A if (buf[0] | buf[2] | buf[4])
4418N/A#endif
4418N/A {
4418N/A png_warning(png_ptr,
4418N/A "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
4418N/A
4418N/A return;
4418N/A }
4418N/A
4418N/A png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
0N/A }
4418N/A
0N/A else
0N/A {
4418N/A if (back->gray >= (1 << png_ptr->bit_depth))
0N/A {
0N/A png_warning(png_ptr,
4418N/A "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
4418N/A
0N/A return;
0N/A }
4418N/A
0N/A png_save_uint_16(buf, back->gray);
4418N/A png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
0N/A }
0N/A}
0N/A#endif
0N/A
4418N/A#ifdef PNG_WRITE_hIST_SUPPORTED
4418N/A/* Write the histogram */
0N/Avoid /* PRIVATE */
4418N/Apng_write_hIST(png_structp png_ptr, png_const_uint_16p hist, int num_hist)
0N/A{
0N/A PNG_hIST;
0N/A int i;
0N/A png_byte buf[3];
0N/A
4418N/A png_debug(1, "in png_write_hIST");
4418N/A
0N/A if (num_hist > (int)png_ptr->num_palette)
0N/A {
4418N/A png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
4418N/A png_ptr->num_palette);
4418N/A
0N/A png_warning(png_ptr, "Invalid number of histogram entries specified");
0N/A return;
0N/A }
0N/A
4418N/A png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
4418N/A
0N/A for (i = 0; i < num_hist; i++)
0N/A {
0N/A png_save_uint_16(buf, hist[i]);
0N/A png_write_chunk_data(png_ptr, buf, (png_size_t)2);
0N/A }
4418N/A
0N/A png_write_chunk_end(png_ptr);
0N/A}
0N/A#endif
0N/A
0N/A#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
0N/A defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
0N/A/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
0N/A * and if invalid, correct the keyword rather than discarding the entire
0N/A * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
0N/A * length, forbids leading or trailing whitespace, multiple internal spaces,
0N/A * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
0N/A *
0N/A * The new_key is allocated to hold the corrected keyword and must be freed
0N/A * by the calling routine. This avoids problems with trying to write to
0N/A * static keywords without having to have duplicate copies of the strings.
0N/A */
0N/Apng_size_t /* PRIVATE */
4418N/Apng_check_keyword(png_structp png_ptr, png_const_charp key, png_charpp new_key)
0N/A{
0N/A png_size_t key_len;
4418N/A png_const_charp ikp;
0N/A png_charp kp, dp;
0N/A int kflag;
0N/A int kwarn=0;
0N/A
4418N/A png_debug(1, "in png_check_keyword");
4418N/A
0N/A *new_key = NULL;
0N/A
0N/A if (key == NULL || (key_len = png_strlen(key)) == 0)
0N/A {
0N/A png_warning(png_ptr, "zero length keyword");
0N/A return ((png_size_t)0);
0N/A }
0N/A
4418N/A png_debug1(2, "Keyword to be checked is '%s'", key);
0N/A
0N/A *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
4418N/A
0N/A if (*new_key == NULL)
0N/A {
0N/A png_warning(png_ptr, "Out of memory while procesing keyword");
0N/A return ((png_size_t)0);
0N/A }
0N/A
0N/A /* Replace non-printing characters with a blank and print a warning */
4418N/A for (ikp = key, dp = *new_key; *ikp != '\0'; ikp++, dp++)
0N/A {
4418N/A if ((png_byte)*ikp < 0x20 ||
4418N/A ((png_byte)*ikp > 0x7E && (png_byte)*ikp < 0xA1))
0N/A {
4418N/A PNG_WARNING_PARAMETERS(p)
4418N/A
4418N/A png_warning_parameter_unsigned(p, 1, PNG_NUMBER_FORMAT_02x,
4418N/A (png_byte)*ikp);
4418N/A png_formatted_warning(png_ptr, p, "invalid keyword character 0x@1");
0N/A *dp = ' ';
0N/A }
4418N/A
0N/A else
0N/A {
4418N/A *dp = *ikp;
0N/A }
0N/A }
0N/A *dp = '\0';
0N/A
0N/A /* Remove any trailing white space. */
0N/A kp = *new_key + key_len - 1;
0N/A if (*kp == ' ')
0N/A {
0N/A png_warning(png_ptr, "trailing spaces removed from keyword");
0N/A
0N/A while (*kp == ' ')
0N/A {
4418N/A *(kp--) = '\0';
4418N/A key_len--;
0N/A }
0N/A }
0N/A
0N/A /* Remove any leading white space. */
0N/A kp = *new_key;
0N/A if (*kp == ' ')
0N/A {
0N/A png_warning(png_ptr, "leading spaces removed from keyword");
0N/A
0N/A while (*kp == ' ')
0N/A {
4418N/A kp++;
4418N/A key_len--;
0N/A }
0N/A }
0N/A
4418N/A png_debug1(2, "Checking for multiple internal spaces in '%s'", kp);
0N/A
0N/A /* Remove multiple internal spaces. */
0N/A for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
0N/A {
0N/A if (*kp == ' ' && kflag == 0)
0N/A {
0N/A *(dp++) = *kp;
0N/A kflag = 1;
0N/A }
4418N/A
0N/A else if (*kp == ' ')
0N/A {
0N/A key_len--;
4418N/A kwarn = 1;
0N/A }
4418N/A
0N/A else
0N/A {
0N/A *(dp++) = *kp;
0N/A kflag = 0;
0N/A }
0N/A }
0N/A *dp = '\0';
4418N/A if (kwarn)
0N/A png_warning(png_ptr, "extra interior spaces removed from keyword");
0N/A
0N/A if (key_len == 0)
0N/A {
0N/A png_free(png_ptr, *new_key);
0N/A png_warning(png_ptr, "Zero length keyword");
0N/A }
0N/A
0N/A if (key_len > 79)
0N/A {
0N/A png_warning(png_ptr, "keyword length must be 1 - 79 characters");
4418N/A (*new_key)[79] = '\0';
0N/A key_len = 79;
0N/A }
0N/A
0N/A return (key_len);
0N/A}
0N/A#endif
0N/A
4418N/A#ifdef PNG_WRITE_tEXt_SUPPORTED
4418N/A/* Write a tEXt chunk */
0N/Avoid /* PRIVATE */
4418N/Apng_write_tEXt(png_structp png_ptr, png_const_charp key, png_const_charp text,
4418N/A png_size_t text_len)
0N/A{
0N/A PNG_tEXt;
0N/A png_size_t key_len;
0N/A png_charp new_key;
0N/A
4418N/A png_debug(1, "in png_write_tEXt");
4418N/A
4418N/A if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
0N/A return;
0N/A
0N/A if (text == NULL || *text == '\0')
0N/A text_len = 0;
4418N/A
0N/A else
0N/A text_len = png_strlen(text);
0N/A
4418N/A /* Make sure we include the 0 after the key */
4418N/A png_write_chunk_start(png_ptr, png_tEXt,
4418N/A (png_uint_32)(key_len + text_len + 1));
0N/A /*
0N/A * We leave it to the application to meet PNG-1.0 requirements on the
0N/A * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
0N/A * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
0N/A * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
0N/A */
4418N/A png_write_chunk_data(png_ptr, (png_bytep)new_key,
4418N/A (png_size_t)(key_len + 1));
4418N/A
0N/A if (text_len)
4418N/A png_write_chunk_data(png_ptr, (png_const_bytep)text,
4418N/A (png_size_t)text_len);
0N/A
0N/A png_write_chunk_end(png_ptr);
0N/A png_free(png_ptr, new_key);
0N/A}
0N/A#endif
0N/A
4418N/A#ifdef PNG_WRITE_zTXt_SUPPORTED
4418N/A/* Write a compressed text chunk */
0N/Avoid /* PRIVATE */
4418N/Apng_write_zTXt(png_structp png_ptr, png_const_charp key, png_const_charp text,
4418N/A png_size_t text_len, int compression)
0N/A{
0N/A PNG_zTXt;
0N/A png_size_t key_len;
4418N/A png_byte buf;
0N/A png_charp new_key;
0N/A compression_state comp;
0N/A
4418N/A png_debug(1, "in png_write_zTXt");
0N/A
0N/A comp.num_output_ptr = 0;
0N/A comp.max_output_ptr = 0;
0N/A comp.output_ptr = NULL;
0N/A comp.input = NULL;
0N/A comp.input_len = 0;
0N/A
4418N/A if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
0N/A {
4418N/A png_free(png_ptr, new_key);
0N/A return;
0N/A }
0N/A
0N/A if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
0N/A {
0N/A png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
0N/A png_free(png_ptr, new_key);
0N/A return;
0N/A }
0N/A
0N/A text_len = png_strlen(text);
0N/A
4418N/A /* Compute the compressed data; do it now for the length */
0N/A text_len = png_text_compress(png_ptr, text, text_len, compression,
0N/A &comp);
0N/A
4418N/A /* Write start of chunk */
4418N/A png_write_chunk_start(png_ptr, png_zTXt,
4418N/A (png_uint_32)(key_len+text_len + 2));
4418N/A
4418N/A /* Write key */
4418N/A png_write_chunk_data(png_ptr, (png_bytep)new_key,
4418N/A (png_size_t)(key_len + 1));
4418N/A
4418N/A png_free(png_ptr, new_key);
4418N/A
4418N/A buf = (png_byte)compression;
4418N/A
4418N/A /* Write compression */
4418N/A png_write_chunk_data(png_ptr, &buf, (png_size_t)1);
4418N/A
4418N/A /* Write the compressed data */
4418N/A comp.input_len = text_len;
0N/A png_write_compressed_data_out(png_ptr, &comp);
0N/A
4418N/A /* Close the chunk */
0N/A png_write_chunk_end(png_ptr);
0N/A}
0N/A#endif
0N/A
4418N/A#ifdef PNG_WRITE_iTXt_SUPPORTED
4418N/A/* Write an iTXt chunk */
0N/Avoid /* PRIVATE */
4418N/Apng_write_iTXt(png_structp png_ptr, int compression, png_const_charp key,
4418N/A png_const_charp lang, png_const_charp lang_key, png_const_charp text)
0N/A{
0N/A PNG_iTXt;
0N/A png_size_t lang_len, key_len, lang_key_len, text_len;
4418N/A png_charp new_lang;
4418N/A png_charp new_key = NULL;
0N/A png_byte cbuf[2];
0N/A compression_state comp;
0N/A
4418N/A png_debug(1, "in png_write_iTXt");
0N/A
0N/A comp.num_output_ptr = 0;
0N/A comp.max_output_ptr = 0;
0N/A comp.output_ptr = NULL;
0N/A comp.input = NULL;
0N/A
4418N/A if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
0N/A return;
4418N/A
4418N/A if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang)) == 0)
0N/A {
0N/A png_warning(png_ptr, "Empty language field in iTXt chunk");
0N/A new_lang = NULL;
0N/A lang_len = 0;
0N/A }
0N/A
0N/A if (lang_key == NULL)
4418N/A lang_key_len = 0;
4418N/A
0N/A else
4418N/A lang_key_len = png_strlen(lang_key);
0N/A
0N/A if (text == NULL)
0N/A text_len = 0;
4418N/A
0N/A else
4418N/A text_len = png_strlen(text);
4418N/A
4418N/A /* Compute the compressed data; do it now for the length */
4418N/A text_len = png_text_compress(png_ptr, text, text_len, compression - 2,
4418N/A &comp);
4418N/A
4418N/A
4418N/A /* Make sure we include the compression flag, the compression byte,
4418N/A * and the NULs after the key, lang, and lang_key parts
4418N/A */
4418N/A
4418N/A png_write_chunk_start(png_ptr, png_iTXt, (png_uint_32)(
0N/A 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
0N/A + key_len
0N/A + lang_len
0N/A + lang_key_len
0N/A + text_len));
0N/A
4418N/A /* We leave it to the application to meet PNG-1.0 requirements on the
0N/A * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
0N/A * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
0N/A * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
0N/A */
4418N/A png_write_chunk_data(png_ptr, (png_bytep)new_key, (png_size_t)(key_len + 1));
4418N/A
4418N/A /* Set the compression flag */
4418N/A if (compression == PNG_ITXT_COMPRESSION_NONE ||
0N/A compression == PNG_TEXT_COMPRESSION_NONE)
4418N/A cbuf[0] = 0;
4418N/A
0N/A else /* compression == PNG_ITXT_COMPRESSION_zTXt */
4418N/A cbuf[0] = 1;
4418N/A
4418N/A /* Set the compression method */
0N/A cbuf[1] = 0;
4418N/A
4418N/A png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
0N/A
0N/A cbuf[0] = 0;
4418N/A png_write_chunk_data(png_ptr, (new_lang ? (png_const_bytep)new_lang : cbuf),
4418N/A (png_size_t)(lang_len + 1));
4418N/A
4418N/A png_write_chunk_data(png_ptr, (lang_key ? (png_const_bytep)lang_key : cbuf),
4418N/A (png_size_t)(lang_key_len + 1));
4418N/A
0N/A png_write_compressed_data_out(png_ptr, &comp);
0N/A
0N/A png_write_chunk_end(png_ptr);
4418N/A
0N/A png_free(png_ptr, new_key);
4418N/A png_free(png_ptr, new_lang);
0N/A}
0N/A#endif
0N/A
4418N/A#ifdef PNG_WRITE_oFFs_SUPPORTED
4418N/A/* Write the oFFs chunk */
0N/Avoid /* PRIVATE */
0N/Apng_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
4418N/A int unit_type)
0N/A{
0N/A PNG_oFFs;
0N/A png_byte buf[9];
0N/A
4418N/A png_debug(1, "in png_write_oFFs");
4418N/A
0N/A if (unit_type >= PNG_OFFSET_LAST)
0N/A png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
0N/A
0N/A png_save_int_32(buf, x_offset);
0N/A png_save_int_32(buf + 4, y_offset);
0N/A buf[8] = (png_byte)unit_type;
0N/A
4418N/A png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
0N/A}
0N/A#endif
4418N/A#ifdef PNG_WRITE_pCAL_SUPPORTED
4418N/A/* Write the pCAL chunk (described in the PNG extensions document) */
0N/Avoid /* PRIVATE */
0N/Apng_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
4418N/A png_int_32 X1, int type, int nparams, png_const_charp units,
4418N/A png_charpp params)
0N/A{
0N/A PNG_pCAL;
0N/A png_size_t purpose_len, units_len, total_len;
0N/A png_uint_32p params_len;
0N/A png_byte buf[10];
0N/A png_charp new_purpose;
0N/A int i;
0N/A
4418N/A png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
4418N/A
0N/A if (type >= PNG_EQUATION_LAST)
0N/A png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
0N/A
0N/A purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
4418N/A png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
0N/A units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
4418N/A png_debug1(3, "pCAL units length = %d", (int)units_len);
0N/A total_len = purpose_len + units_len + 10;
0N/A
4418N/A params_len = (png_uint_32p)png_malloc(png_ptr,
4418N/A (png_alloc_size_t)(nparams * png_sizeof(png_uint_32)));
0N/A
0N/A /* Find the length of each parameter, making sure we don't count the
4418N/A * null terminator for the last parameter.
4418N/A */
0N/A for (i = 0; i < nparams; i++)
0N/A {
0N/A params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
4418N/A png_debug2(3, "pCAL parameter %d length = %lu", i,
4418N/A (unsigned long)params_len[i]);
0N/A total_len += (png_size_t)params_len[i];
0N/A }
0N/A
4418N/A png_debug1(3, "pCAL total length = %d", (int)total_len);
4418N/A png_write_chunk_start(png_ptr, png_pCAL, (png_uint_32)total_len);
4418N/A png_write_chunk_data(png_ptr, (png_const_bytep)new_purpose,
4418N/A (png_size_t)purpose_len);
0N/A png_save_int_32(buf, X0);
0N/A png_save_int_32(buf + 4, X1);
0N/A buf[8] = (png_byte)type;
0N/A buf[9] = (png_byte)nparams;
0N/A png_write_chunk_data(png_ptr, buf, (png_size_t)10);
4418N/A png_write_chunk_data(png_ptr, (png_const_bytep)units, (png_size_t)units_len);
0N/A
0N/A png_free(png_ptr, new_purpose);
0N/A
0N/A for (i = 0; i < nparams; i++)
0N/A {
4418N/A png_write_chunk_data(png_ptr, (png_const_bytep)params[i],
4418N/A (png_size_t)params_len[i]);
0N/A }
0N/A
0N/A png_free(png_ptr, params_len);
0N/A png_write_chunk_end(png_ptr);
0N/A}
0N/A#endif
0N/A
4418N/A#ifdef PNG_WRITE_sCAL_SUPPORTED
4418N/A/* Write the sCAL chunk */
0N/Avoid /* PRIVATE */
4418N/Apng_write_sCAL_s(png_structp png_ptr, int unit, png_const_charp width,
4418N/A png_const_charp height)
0N/A{
0N/A PNG_sCAL;
0N/A png_byte buf[64];
0N/A png_size_t wlen, hlen, total_len;
0N/A
4418N/A png_debug(1, "in png_write_sCAL_s");
0N/A
0N/A wlen = png_strlen(width);
0N/A hlen = png_strlen(height);
0N/A total_len = wlen + hlen + 2;
4418N/A
0N/A if (total_len > 64)
0N/A {
0N/A png_warning(png_ptr, "Can't write sCAL (buffer too small)");
0N/A return;
0N/A }
0N/A
0N/A buf[0] = (png_byte)unit;
4418N/A png_memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */
4418N/A png_memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */
4418N/A
4418N/A png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
4418N/A png_write_chunk(png_ptr, png_sCAL, buf, total_len);
0N/A}
0N/A#endif
4418N/A
4418N/A#ifdef PNG_WRITE_pHYs_SUPPORTED
4418N/A/* Write the pHYs chunk */
0N/Avoid /* PRIVATE */
0N/Apng_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
4418N/A png_uint_32 y_pixels_per_unit,
4418N/A int unit_type)
0N/A{
0N/A PNG_pHYs;
0N/A png_byte buf[9];
0N/A
4418N/A png_debug(1, "in png_write_pHYs");
4418N/A
0N/A if (unit_type >= PNG_RESOLUTION_LAST)
0N/A png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
0N/A
0N/A png_save_uint_32(buf, x_pixels_per_unit);
0N/A png_save_uint_32(buf + 4, y_pixels_per_unit);
0N/A buf[8] = (png_byte)unit_type;
0N/A
4418N/A png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
0N/A}
0N/A#endif
0N/A
4418N/A#ifdef PNG_WRITE_tIME_SUPPORTED
0N/A/* Write the tIME chunk. Use either png_convert_from_struct_tm()
0N/A * or png_convert_from_time_t(), or fill in the structure yourself.
0N/A */
0N/Avoid /* PRIVATE */
4418N/Apng_write_tIME(png_structp png_ptr, png_const_timep mod_time)
0N/A{
0N/A PNG_tIME;
0N/A png_byte buf[7];
0N/A
4418N/A png_debug(1, "in png_write_tIME");
4418N/A
0N/A if (mod_time->month > 12 || mod_time->month < 1 ||
0N/A mod_time->day > 31 || mod_time->day < 1 ||
0N/A mod_time->hour > 23 || mod_time->second > 60)
0N/A {
0N/A png_warning(png_ptr, "Invalid time specified for tIME chunk");
0N/A return;
0N/A }
0N/A
0N/A png_save_uint_16(buf, mod_time->year);
0N/A buf[2] = mod_time->month;
0N/A buf[3] = mod_time->day;
0N/A buf[4] = mod_time->hour;
0N/A buf[5] = mod_time->minute;
0N/A buf[6] = mod_time->second;
0N/A
4418N/A png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
0N/A}
0N/A#endif
0N/A
4418N/A/* Initializes the row writing capability of libpng */
0N/Avoid /* PRIVATE */
0N/Apng_write_start_row(png_structp png_ptr)
0N/A{
4418N/A#ifdef PNG_WRITE_INTERLACING_SUPPORTED
4418N/A /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4418N/A
4418N/A /* Start of interlace block */
0N/A int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
0N/A
4418N/A /* Offset to next interlace block */
0N/A int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
0N/A
4418N/A /* Start of interlace block in the y direction */
0N/A int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
0N/A
4418N/A /* Offset to next interlace block in the y direction */
0N/A int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
0N/A#endif
0N/A
0N/A png_size_t buf_size;
0N/A
4418N/A png_debug(1, "in png_write_start_row");
4418N/A
0N/A buf_size = (png_size_t)(PNG_ROWBYTES(
4418N/A png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
4418N/A
4418N/A /* Set up row buffer */
4418N/A png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
4418N/A (png_alloc_size_t)buf_size);
4418N/A
0N/A png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
0N/A
4418N/A#ifdef PNG_WRITE_FILTER_SUPPORTED
4418N/A /* Set up filtering buffer, if using this filter */
0N/A if (png_ptr->do_filter & PNG_FILTER_SUB)
0N/A {
4418N/A png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
4418N/A
0N/A png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
0N/A }
0N/A
0N/A /* We only need to keep the previous row if we are using one of these. */
0N/A if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
0N/A {
4418N/A /* Set up previous row buffer */
4418N/A png_ptr->prev_row = (png_bytep)png_calloc(png_ptr,
4418N/A (png_alloc_size_t)buf_size);
0N/A
0N/A if (png_ptr->do_filter & PNG_FILTER_UP)
0N/A {
4418N/A png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
4418N/A png_ptr->rowbytes + 1);
4418N/A
0N/A png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
0N/A }
0N/A
0N/A if (png_ptr->do_filter & PNG_FILTER_AVG)
0N/A {
0N/A png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
4418N/A png_ptr->rowbytes + 1);
4418N/A
0N/A png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
0N/A }
0N/A
0N/A if (png_ptr->do_filter & PNG_FILTER_PAETH)
0N/A {
4418N/A png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
4418N/A png_ptr->rowbytes + 1);
4418N/A
0N/A png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
0N/A }
0N/A }
4418N/A#endif /* PNG_WRITE_FILTER_SUPPORTED */
0N/A
0N/A#ifdef PNG_WRITE_INTERLACING_SUPPORTED
4418N/A /* If interlaced, we need to set up width and height of pass */
0N/A if (png_ptr->interlaced)
0N/A {
0N/A if (!(png_ptr->transformations & PNG_INTERLACE))
0N/A {
0N/A png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
4418N/A png_pass_ystart[0]) / png_pass_yinc[0];
4418N/A
0N/A png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
4418N/A png_pass_start[0]) / png_pass_inc[0];
0N/A }
4418N/A
0N/A else
0N/A {
0N/A png_ptr->num_rows = png_ptr->height;
0N/A png_ptr->usr_width = png_ptr->width;
0N/A }
0N/A }
4418N/A
0N/A else
0N/A#endif
0N/A {
0N/A png_ptr->num_rows = png_ptr->height;
0N/A png_ptr->usr_width = png_ptr->width;
0N/A }
4418N/A
4418N/A png_zlib_claim(png_ptr, PNG_ZLIB_FOR_IDAT);
0N/A png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
0N/A png_ptr->zstream.next_out = png_ptr->zbuf;
0N/A}
0N/A
0N/A/* Internal use only. Called when finished processing a row of data. */
0N/Avoid /* PRIVATE */
0N/Apng_write_finish_row(png_structp png_ptr)
0N/A{
4418N/A#ifdef PNG_WRITE_INTERLACING_SUPPORTED
4418N/A /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4418N/A
4418N/A /* Start of interlace block */
0N/A int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
0N/A
4418N/A /* Offset to next interlace block */
0N/A int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
0N/A
4418N/A /* Start of interlace block in the y direction */
0N/A int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
0N/A
4418N/A /* Offset to next interlace block in the y direction */
0N/A int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
0N/A#endif
0N/A
0N/A int ret;
0N/A
4418N/A png_debug(1, "in png_write_finish_row");
4418N/A
4418N/A /* Next row */
0N/A png_ptr->row_number++;
0N/A
4418N/A /* See if we are done */
0N/A if (png_ptr->row_number < png_ptr->num_rows)
0N/A return;
0N/A
0N/A#ifdef PNG_WRITE_INTERLACING_SUPPORTED
4418N/A /* If interlaced, go to next pass */
0N/A if (png_ptr->interlaced)
0N/A {
0N/A png_ptr->row_number = 0;
0N/A if (png_ptr->transformations & PNG_INTERLACE)
0N/A {
0N/A png_ptr->pass++;
0N/A }
4418N/A
0N/A else
0N/A {
4418N/A /* Loop until we find a non-zero width or height pass */
0N/A do
0N/A {
0N/A png_ptr->pass++;
4418N/A
0N/A if (png_ptr->pass >= 7)
0N/A break;
4418N/A
0N/A png_ptr->usr_width = (png_ptr->width +
4418N/A png_pass_inc[png_ptr->pass] - 1 -
4418N/A png_pass_start[png_ptr->pass]) /
4418N/A png_pass_inc[png_ptr->pass];
4418N/A
0N/A png_ptr->num_rows = (png_ptr->height +
4418N/A png_pass_yinc[png_ptr->pass] - 1 -
4418N/A png_pass_ystart[png_ptr->pass]) /
4418N/A png_pass_yinc[png_ptr->pass];
4418N/A
0N/A if (png_ptr->transformations & PNG_INTERLACE)
0N/A break;
4418N/A
0N/A } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
0N/A
0N/A }
0N/A
4418N/A /* Reset the row above the image for the next pass */
0N/A if (png_ptr->pass < 7)
0N/A {
0N/A if (png_ptr->prev_row != NULL)
0N/A png_memset(png_ptr->prev_row, 0,
4418N/A (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
4418N/A png_ptr->usr_bit_depth, png_ptr->width)) + 1);
4418N/A
0N/A return;
0N/A }
0N/A }
0N/A#endif
0N/A
4418N/A /* If we get here, we've just written the last row, so we need
0N/A to flush the compressor */
0N/A do
0N/A {
4418N/A /* Tell the compressor we are done */
0N/A ret = deflate(&png_ptr->zstream, Z_FINISH);
4418N/A
4418N/A /* Check for an error */
0N/A if (ret == Z_OK)
0N/A {
4418N/A /* Check to see if we need more room */
0N/A if (!(png_ptr->zstream.avail_out))
0N/A {
0N/A png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
0N/A png_ptr->zstream.next_out = png_ptr->zbuf;
0N/A png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
0N/A }
0N/A }
4418N/A
0N/A else if (ret != Z_STREAM_END)
0N/A {
0N/A if (png_ptr->zstream.msg != NULL)
0N/A png_error(png_ptr, png_ptr->zstream.msg);
4418N/A
0N/A else
0N/A png_error(png_ptr, "zlib error");
0N/A }
0N/A } while (ret != Z_STREAM_END);
0N/A
4418N/A /* Write any extra space */
0N/A if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
0N/A {
0N/A png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
4418N/A png_ptr->zstream.avail_out);
0N/A }
0N/A
4418N/A png_zlib_release(png_ptr);
0N/A png_ptr->zstream.data_type = Z_BINARY;
0N/A}
0N/A
4418N/A#ifdef PNG_WRITE_INTERLACING_SUPPORTED
0N/A/* Pick out the correct pixels for the interlace pass.
0N/A * The basic idea here is to go through the row with a source
0N/A * pointer and a destination pointer (sp and dp), and copy the
0N/A * correct pixels for the pass. As the row gets compacted,
0N/A * sp will always be >= dp, so we should never overwrite anything.
0N/A * See the default: case for the easiest code to understand.
0N/A */
0N/Avoid /* PRIVATE */
0N/Apng_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
0N/A{
4418N/A /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4418N/A
4418N/A /* Start of interlace block */
0N/A int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
0N/A
4418N/A /* Offset to next interlace block */
0N/A int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4418N/A
4418N/A png_debug(1, "in png_do_write_interlace");
4418N/A
4418N/A /* We don't have to do anything on the last pass (6) */
0N/A if (pass < 6)
0N/A {
4418N/A /* Each pixel depth is handled separately */
0N/A switch (row_info->pixel_depth)
0N/A {
0N/A case 1:
0N/A {
0N/A png_bytep sp;
0N/A png_bytep dp;
0N/A int shift;
0N/A int d;
0N/A int value;
0N/A png_uint_32 i;
0N/A png_uint_32 row_width = row_info->width;
0N/A
0N/A dp = row;
0N/A d = 0;
0N/A shift = 7;
4418N/A
0N/A for (i = png_pass_start[pass]; i < row_width;
0N/A i += png_pass_inc[pass])
0N/A {
0N/A sp = row + (png_size_t)(i >> 3);
0N/A value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
0N/A d |= (value << shift);
0N/A
0N/A if (shift == 0)
0N/A {
0N/A shift = 7;
0N/A *dp++ = (png_byte)d;
0N/A d = 0;
0N/A }
4418N/A
0N/A else
0N/A shift--;
0N/A
0N/A }
0N/A if (shift != 7)
0N/A *dp = (png_byte)d;
4418N/A
0N/A break;
0N/A }
4418N/A
0N/A case 2:
0N/A {
0N/A png_bytep sp;
0N/A png_bytep dp;
0N/A int shift;
0N/A int d;
0N/A int value;
0N/A png_uint_32 i;
0N/A png_uint_32 row_width = row_info->width;
0N/A
0N/A dp = row;
0N/A shift = 6;
0N/A d = 0;
4418N/A
0N/A for (i = png_pass_start[pass]; i < row_width;
0N/A i += png_pass_inc[pass])
0N/A {
0N/A sp = row + (png_size_t)(i >> 2);
0N/A value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
0N/A d |= (value << shift);
0N/A
0N/A if (shift == 0)
0N/A {
0N/A shift = 6;
0N/A *dp++ = (png_byte)d;
0N/A d = 0;
0N/A }
4418N/A
0N/A else
0N/A shift -= 2;
0N/A }
0N/A if (shift != 6)
4418N/A *dp = (png_byte)d;
4418N/A
0N/A break;
0N/A }
4418N/A
0N/A case 4:
0N/A {
0N/A png_bytep sp;
0N/A png_bytep dp;
0N/A int shift;
0N/A int d;
0N/A int value;
0N/A png_uint_32 i;
0N/A png_uint_32 row_width = row_info->width;
0N/A
0N/A dp = row;
0N/A shift = 4;
0N/A d = 0;
0N/A for (i = png_pass_start[pass]; i < row_width;
4418N/A i += png_pass_inc[pass])
0N/A {
0N/A sp = row + (png_size_t)(i >> 1);
0N/A value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
0N/A d |= (value << shift);
0N/A
0N/A if (shift == 0)
0N/A {
0N/A shift = 4;
0N/A *dp++ = (png_byte)d;
0N/A d = 0;
0N/A }
4418N/A
0N/A else
0N/A shift -= 4;
0N/A }
0N/A if (shift != 4)
0N/A *dp = (png_byte)d;
4418N/A
0N/A break;
0N/A }
4418N/A
0N/A default:
0N/A {
0N/A png_bytep sp;
0N/A png_bytep dp;
0N/A png_uint_32 i;
0N/A png_uint_32 row_width = row_info->width;
0N/A png_size_t pixel_bytes;
0N/A
4418N/A /* Start at the beginning */
0N/A dp = row;
4418N/A
4418N/A /* Find out how many bytes each pixel takes up */
0N/A pixel_bytes = (row_info->pixel_depth >> 3);
4418N/A
4418N/A /* Loop through the row, only looking at the pixels that matter */
0N/A for (i = png_pass_start[pass]; i < row_width;
0N/A i += png_pass_inc[pass])
0N/A {
4418N/A /* Find out where the original pixel is */
0N/A sp = row + (png_size_t)i * pixel_bytes;
4418N/A
4418N/A /* Move the pixel */
0N/A if (dp != sp)
0N/A png_memcpy(dp, sp, pixel_bytes);
4418N/A
4418N/A /* Next pixel */
0N/A dp += pixel_bytes;
0N/A }
0N/A break;
0N/A }
0N/A }
4418N/A /* Set new row width */
0N/A row_info->width = (row_info->width +
4418N/A png_pass_inc[pass] - 1 -
4418N/A png_pass_start[pass]) /
4418N/A png_pass_inc[pass];
4418N/A
4418N/A row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
4418N/A row_info->width);
0N/A }
0N/A}
0N/A#endif
0N/A
0N/A/* This filters the row, chooses which filter to use, if it has not already
0N/A * been specified by the application, and then writes the row out with the
0N/A * chosen filter.
0N/A */
4418N/Astatic void png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row);
4418N/A
0N/A#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
0N/A#define PNG_HISHIFT 10
0N/A#define PNG_LOMASK ((png_uint_32)0xffffL)
0N/A#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
0N/Avoid /* PRIVATE */
0N/Apng_write_find_filter(png_structp png_ptr, png_row_infop row_info)
0N/A{
4418N/A png_bytep best_row;
4418N/A#ifdef PNG_WRITE_FILTER_SUPPORTED
4418N/A png_bytep prev_row, row_buf;
0N/A png_uint_32 mins, bpp;
0N/A png_byte filter_to_do = png_ptr->do_filter;
4418N/A png_size_t row_bytes = row_info->rowbytes;
4418N/A#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
0N/A int num_p_filters = (int)png_ptr->num_prev_filters;
0N/A#endif
0N/A
4418N/A png_debug(1, "in png_write_find_filter");
4418N/A
4418N/A#ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
4418N/A if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
4418N/A {
4418N/A /* These will never be selected so we need not test them. */
4418N/A filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
4418N/A }
4418N/A#endif
4418N/A
4418N/A /* Find out how many bytes offset each pixel is */
0N/A bpp = (row_info->pixel_depth + 7) >> 3;
0N/A
0N/A prev_row = png_ptr->prev_row;
4418N/A#endif
4418N/A best_row = png_ptr->row_buf;
4418N/A#ifdef PNG_WRITE_FILTER_SUPPORTED
4418N/A row_buf = best_row;
0N/A mins = PNG_MAXSUM;
0N/A
0N/A /* The prediction method we use is to find which method provides the
0N/A * smallest value when summing the absolute values of the distances
0N/A * from zero, using anything >= 128 as negative numbers. This is known
0N/A * as the "minimum sum of absolute differences" heuristic. Other
0N/A * heuristics are the "weighted minimum sum of absolute differences"
0N/A * (experimental and can in theory improve compression), and the "zlib
0N/A * predictive" method (not implemented yet), which does test compressions
0N/A * of lines using different filter methods, and then chooses the
0N/A * (series of) filter(s) that give minimum compressed data size (VERY
0N/A * computationally expensive).
0N/A *
0N/A * GRR 980525: consider also
4418N/A *
0N/A * (1) minimum sum of absolute differences from running average (i.e.,
0N/A * keep running sum of non-absolute differences & count of bytes)
0N/A * [track dispersion, too? restart average if dispersion too large?]
4418N/A *
0N/A * (1b) minimum sum of absolute differences from sliding average, probably
0N/A * with window size <= deflate window (usually 32K)
4418N/A *
0N/A * (2) minimum sum of squared differences from zero or running average
0N/A * (i.e., ~ root-mean-square approach)
0N/A */
0N/A
0N/A
0N/A /* We don't need to test the 'no filter' case if this is the only filter
0N/A * that has been chosen, as it doesn't actually do anything to the data.
0N/A */
4418N/A if ((filter_to_do & PNG_FILTER_NONE) && filter_to_do != PNG_FILTER_NONE)
0N/A {
0N/A png_bytep rp;
0N/A png_uint_32 sum = 0;
4418N/A png_size_t i;
0N/A int v;
0N/A
0N/A for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
0N/A {
0N/A v = *rp;
0N/A sum += (v < 128) ? v : 256 - v;
0N/A }
0N/A
4418N/A#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
0N/A if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
0N/A {
0N/A png_uint_32 sumhi, sumlo;
0N/A int j;
0N/A sumlo = sum & PNG_LOMASK;
0N/A sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
0N/A
0N/A /* Reduce the sum if we match any of the previous rows */
0N/A for (j = 0; j < num_p_filters; j++)
0N/A {
0N/A if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
0N/A {
0N/A sumlo = (sumlo * png_ptr->filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
4418N/A
0N/A sumhi = (sumhi * png_ptr->filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
0N/A }
0N/A }
0N/A
0N/A /* Factor in the cost of this filter (this is here for completeness,
0N/A * but it makes no sense to have a "cost" for the NONE filter, as
0N/A * it has the minimum possible computational cost - none).
0N/A */
0N/A sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
4418N/A PNG_COST_SHIFT;
4418N/A
0N/A sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
4418N/A PNG_COST_SHIFT;
0N/A
0N/A if (sumhi > PNG_HIMASK)
0N/A sum = PNG_MAXSUM;
4418N/A
0N/A else
0N/A sum = (sumhi << PNG_HISHIFT) + sumlo;
0N/A }
0N/A#endif
0N/A mins = sum;
0N/A }
0N/A
4418N/A /* Sub filter */
0N/A if (filter_to_do == PNG_FILTER_SUB)
4418N/A /* It's the only filter so no testing is needed */
0N/A {
0N/A png_bytep rp, lp, dp;
4418N/A png_size_t i;
4418N/A
0N/A for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
0N/A i++, rp++, dp++)
0N/A {
0N/A *dp = *rp;
0N/A }
4418N/A
0N/A for (lp = row_buf + 1; i < row_bytes;
0N/A i++, rp++, lp++, dp++)
0N/A {
0N/A *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
0N/A }
4418N/A
0N/A best_row = png_ptr->sub_row;
0N/A }
0N/A
0N/A else if (filter_to_do & PNG_FILTER_SUB)
0N/A {
0N/A png_bytep rp, dp, lp;
0N/A png_uint_32 sum = 0, lmins = mins;
4418N/A png_size_t i;
0N/A int v;
0N/A
4418N/A#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
0N/A /* We temporarily increase the "minimum sum" by the factor we
0N/A * would reduce the sum of this filter, so that we can do the
0N/A * early exit comparison without scaling the sum each time.
0N/A */
0N/A if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
0N/A {
0N/A int j;
0N/A png_uint_32 lmhi, lmlo;
0N/A lmlo = lmins & PNG_LOMASK;
0N/A lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
0N/A
0N/A for (j = 0; j < num_p_filters; j++)
0N/A {
0N/A if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
0N/A {
0N/A lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
4418N/A
0N/A lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
0N/A }
0N/A }
0N/A
0N/A lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
4418N/A PNG_COST_SHIFT;
4418N/A
0N/A lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
4418N/A PNG_COST_SHIFT;
0N/A
0N/A if (lmhi > PNG_HIMASK)
0N/A lmins = PNG_MAXSUM;
4418N/A
0N/A else
0N/A lmins = (lmhi << PNG_HISHIFT) + lmlo;
0N/A }
0N/A#endif
0N/A
0N/A for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
0N/A i++, rp++, dp++)
0N/A {
0N/A v = *dp = *rp;
0N/A
0N/A sum += (v < 128) ? v : 256 - v;
0N/A }
4418N/A
0N/A for (lp = row_buf + 1; i < row_bytes;
0N/A i++, rp++, lp++, dp++)
0N/A {
0N/A v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
0N/A
0N/A sum += (v < 128) ? v : 256 - v;
0N/A
0N/A if (sum > lmins) /* We are already worse, don't continue. */
0N/A break;
0N/A }
0N/A
4418N/A#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
0N/A if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
0N/A {
0N/A int j;
0N/A png_uint_32 sumhi, sumlo;
0N/A sumlo = sum & PNG_LOMASK;
0N/A sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
0N/A
0N/A for (j = 0; j < num_p_filters; j++)
0N/A {
0N/A if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
0N/A {
0N/A sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
4418N/A
0N/A sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
0N/A }
0N/A }
0N/A
0N/A sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
4418N/A PNG_COST_SHIFT;
4418N/A
0N/A sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
4418N/A PNG_COST_SHIFT;
0N/A
0N/A if (sumhi > PNG_HIMASK)
0N/A sum = PNG_MAXSUM;
4418N/A
0N/A else
0N/A sum = (sumhi << PNG_HISHIFT) + sumlo;
0N/A }
0N/A#endif
0N/A
0N/A if (sum < mins)
0N/A {
0N/A mins = sum;
0N/A best_row = png_ptr->sub_row;
0N/A }
0N/A }
0N/A
4418N/A /* Up filter */
0N/A if (filter_to_do == PNG_FILTER_UP)
0N/A {
0N/A png_bytep rp, dp, pp;
4418N/A png_size_t i;
0N/A
0N/A for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
4418N/A pp = prev_row + 1; i < row_bytes;
4418N/A i++, rp++, pp++, dp++)
0N/A {
0N/A *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
0N/A }
4418N/A
0N/A best_row = png_ptr->up_row;
0N/A }
0N/A
0N/A else if (filter_to_do & PNG_FILTER_UP)
0N/A {
0N/A png_bytep rp, dp, pp;
0N/A png_uint_32 sum = 0, lmins = mins;
4418N/A png_size_t i;
0N/A int v;
0N/A
0N/A
4418N/A#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
0N/A if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
0N/A {
0N/A int j;
0N/A png_uint_32 lmhi, lmlo;
0N/A lmlo = lmins & PNG_LOMASK;
0N/A lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
0N/A
0N/A for (j = 0; j < num_p_filters; j++)
0N/A {
0N/A if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
0N/A {
0N/A lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
4418N/A
0N/A lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
0N/A }
0N/A }
0N/A
0N/A lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
4418N/A PNG_COST_SHIFT;
4418N/A
0N/A lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
4418N/A PNG_COST_SHIFT;
0N/A
0N/A if (lmhi > PNG_HIMASK)
0N/A lmins = PNG_MAXSUM;
4418N/A
0N/A else
0N/A lmins = (lmhi << PNG_HISHIFT) + lmlo;
0N/A }
0N/A#endif
0N/A
0N/A for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
4418N/A pp = prev_row + 1; i < row_bytes; i++)
0N/A {
0N/A v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
0N/A
0N/A sum += (v < 128) ? v : 256 - v;
0N/A
0N/A if (sum > lmins) /* We are already worse, don't continue. */
0N/A break;
0N/A }
0N/A
4418N/A#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
0N/A if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
0N/A {
0N/A int j;
0N/A png_uint_32 sumhi, sumlo;
0N/A sumlo = sum & PNG_LOMASK;
0N/A sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
0N/A
0N/A for (j = 0; j < num_p_filters; j++)
0N/A {
0N/A if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
0N/A {
0N/A sumlo = (sumlo * png_ptr->filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
4418N/A
0N/A sumhi = (sumhi * png_ptr->filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
0N/A }
0N/A }
0N/A
0N/A sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
4418N/A PNG_COST_SHIFT;
4418N/A
0N/A sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
4418N/A PNG_COST_SHIFT;
0N/A
0N/A if (sumhi > PNG_HIMASK)
0N/A sum = PNG_MAXSUM;
4418N/A
0N/A else
0N/A sum = (sumhi << PNG_HISHIFT) + sumlo;
0N/A }
0N/A#endif
0N/A
0N/A if (sum < mins)
0N/A {
0N/A mins = sum;
0N/A best_row = png_ptr->up_row;
0N/A }
0N/A }
0N/A
4418N/A /* Avg filter */
0N/A if (filter_to_do == PNG_FILTER_AVG)
0N/A {
0N/A png_bytep rp, dp, pp, lp;
0N/A png_uint_32 i;
4418N/A
0N/A for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
0N/A pp = prev_row + 1; i < bpp; i++)
0N/A {
0N/A *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
0N/A }
4418N/A
0N/A for (lp = row_buf + 1; i < row_bytes; i++)
0N/A {
0N/A *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
0N/A & 0xff);
0N/A }
0N/A best_row = png_ptr->avg_row;
0N/A }
0N/A
0N/A else if (filter_to_do & PNG_FILTER_AVG)
0N/A {
0N/A png_bytep rp, dp, pp, lp;
0N/A png_uint_32 sum = 0, lmins = mins;
4418N/A png_size_t i;
0N/A int v;
0N/A
4418N/A#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
0N/A if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
0N/A {
0N/A int j;
0N/A png_uint_32 lmhi, lmlo;
0N/A lmlo = lmins & PNG_LOMASK;
0N/A lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
0N/A
0N/A for (j = 0; j < num_p_filters; j++)
0N/A {
0N/A if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
0N/A {
0N/A lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
4418N/A
0N/A lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
0N/A }
0N/A }
0N/A
0N/A lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
4418N/A PNG_COST_SHIFT;
4418N/A
0N/A lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
4418N/A PNG_COST_SHIFT;
0N/A
0N/A if (lmhi > PNG_HIMASK)
0N/A lmins = PNG_MAXSUM;
4418N/A
0N/A else
0N/A lmins = (lmhi << PNG_HISHIFT) + lmlo;
0N/A }
0N/A#endif
0N/A
0N/A for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
0N/A pp = prev_row + 1; i < bpp; i++)
0N/A {
0N/A v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
0N/A
0N/A sum += (v < 128) ? v : 256 - v;
0N/A }
4418N/A
0N/A for (lp = row_buf + 1; i < row_bytes; i++)
0N/A {
0N/A v = *dp++ =
4418N/A (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
0N/A
0N/A sum += (v < 128) ? v : 256 - v;
0N/A
0N/A if (sum > lmins) /* We are already worse, don't continue. */
0N/A break;
0N/A }
0N/A
4418N/A#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
0N/A if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
0N/A {
0N/A int j;
0N/A png_uint_32 sumhi, sumlo;
0N/A sumlo = sum & PNG_LOMASK;
0N/A sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
0N/A
0N/A for (j = 0; j < num_p_filters; j++)
0N/A {
0N/A if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
0N/A {
0N/A sumlo = (sumlo * png_ptr->filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
4418N/A
0N/A sumhi = (sumhi * png_ptr->filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
0N/A }
0N/A }
0N/A
0N/A sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
4418N/A PNG_COST_SHIFT;
4418N/A
0N/A sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
4418N/A PNG_COST_SHIFT;
0N/A
0N/A if (sumhi > PNG_HIMASK)
0N/A sum = PNG_MAXSUM;
4418N/A
0N/A else
0N/A sum = (sumhi << PNG_HISHIFT) + sumlo;
0N/A }
0N/A#endif
0N/A
0N/A if (sum < mins)
0N/A {
0N/A mins = sum;
0N/A best_row = png_ptr->avg_row;
0N/A }
0N/A }
0N/A
0N/A /* Paeth filter */
0N/A if (filter_to_do == PNG_FILTER_PAETH)
0N/A {
0N/A png_bytep rp, dp, pp, cp, lp;
4418N/A png_size_t i;
4418N/A
0N/A for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
4418N/A pp = prev_row + 1; i < bpp; i++)
0N/A {
0N/A *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
0N/A }
0N/A
0N/A for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
0N/A {
0N/A int a, b, c, pa, pb, pc, p;
0N/A
0N/A b = *pp++;
0N/A c = *cp++;
0N/A a = *lp++;
0N/A
0N/A p = b - c;
0N/A pc = a - c;
0N/A
0N/A#ifdef PNG_USE_ABS
0N/A pa = abs(p);
0N/A pb = abs(pc);
0N/A pc = abs(p + pc);
0N/A#else
0N/A pa = p < 0 ? -p : p;
0N/A pb = pc < 0 ? -pc : pc;
0N/A pc = (p + pc) < 0 ? -(p + pc) : p + pc;
0N/A#endif
0N/A
0N/A p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
0N/A
0N/A *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
0N/A }
0N/A best_row = png_ptr->paeth_row;
0N/A }
0N/A
0N/A else if (filter_to_do & PNG_FILTER_PAETH)
0N/A {
0N/A png_bytep rp, dp, pp, cp, lp;
0N/A png_uint_32 sum = 0, lmins = mins;
4418N/A png_size_t i;
0N/A int v;
0N/A
4418N/A#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
0N/A if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
0N/A {
0N/A int j;
0N/A png_uint_32 lmhi, lmlo;
0N/A lmlo = lmins & PNG_LOMASK;
0N/A lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
0N/A
0N/A for (j = 0; j < num_p_filters; j++)
0N/A {
0N/A if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
0N/A {
0N/A lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
4418N/A
0N/A lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
0N/A }
0N/A }
0N/A
0N/A lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
4418N/A PNG_COST_SHIFT;
4418N/A
0N/A lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
4418N/A PNG_COST_SHIFT;
0N/A
0N/A if (lmhi > PNG_HIMASK)
0N/A lmins = PNG_MAXSUM;
4418N/A
0N/A else
0N/A lmins = (lmhi << PNG_HISHIFT) + lmlo;
0N/A }
0N/A#endif
0N/A
0N/A for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
4418N/A pp = prev_row + 1; i < bpp; i++)
0N/A {
0N/A v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
0N/A
0N/A sum += (v < 128) ? v : 256 - v;
0N/A }
0N/A
0N/A for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
0N/A {
0N/A int a, b, c, pa, pb, pc, p;
0N/A
0N/A b = *pp++;
0N/A c = *cp++;
0N/A a = *lp++;
0N/A
0N/A#ifndef PNG_SLOW_PAETH
0N/A p = b - c;
0N/A pc = a - c;
0N/A#ifdef PNG_USE_ABS
0N/A pa = abs(p);
0N/A pb = abs(pc);
0N/A pc = abs(p + pc);
0N/A#else
0N/A pa = p < 0 ? -p : p;
0N/A pb = pc < 0 ? -pc : pc;
0N/A pc = (p + pc) < 0 ? -(p + pc) : p + pc;
0N/A#endif
0N/A p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
0N/A#else /* PNG_SLOW_PAETH */
0N/A p = a + b - c;
0N/A pa = abs(p - a);
0N/A pb = abs(p - b);
0N/A pc = abs(p - c);
4418N/A
0N/A if (pa <= pb && pa <= pc)
0N/A p = a;
4418N/A
0N/A else if (pb <= pc)
0N/A p = b;
4418N/A
0N/A else
0N/A p = c;
0N/A#endif /* PNG_SLOW_PAETH */
0N/A
0N/A v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
0N/A
0N/A sum += (v < 128) ? v : 256 - v;
0N/A
0N/A if (sum > lmins) /* We are already worse, don't continue. */
0N/A break;
0N/A }
0N/A
4418N/A#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
0N/A if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
0N/A {
0N/A int j;
0N/A png_uint_32 sumhi, sumlo;
0N/A sumlo = sum & PNG_LOMASK;
0N/A sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
0N/A
0N/A for (j = 0; j < num_p_filters; j++)
0N/A {
0N/A if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
0N/A {
0N/A sumlo = (sumlo * png_ptr->filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
4418N/A
0N/A sumhi = (sumhi * png_ptr->filter_weights[j]) >>
4418N/A PNG_WEIGHT_SHIFT;
0N/A }
0N/A }
0N/A
0N/A sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
4418N/A PNG_COST_SHIFT;
4418N/A
0N/A sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
4418N/A PNG_COST_SHIFT;
0N/A
0N/A if (sumhi > PNG_HIMASK)
0N/A sum = PNG_MAXSUM;
4418N/A
0N/A else
0N/A sum = (sumhi << PNG_HISHIFT) + sumlo;
0N/A }
0N/A#endif
0N/A
0N/A if (sum < mins)
0N/A {
0N/A best_row = png_ptr->paeth_row;
0N/A }
0N/A }
4418N/A#endif /* PNG_WRITE_FILTER_SUPPORTED */
0N/A /* Do the actual writing of the filtered row data from the chosen filter. */
0N/A
0N/A png_write_filtered_row(png_ptr, best_row);
0N/A
4418N/A#ifdef PNG_WRITE_FILTER_SUPPORTED
4418N/A#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
0N/A /* Save the type of filter we picked this time for future calculations */
0N/A if (png_ptr->num_prev_filters > 0)
0N/A {
0N/A int j;
4418N/A
0N/A for (j = 1; j < num_p_filters; j++)
0N/A {
0N/A png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
0N/A }
4418N/A
0N/A png_ptr->prev_filters[j] = best_row[0];
0N/A }
0N/A#endif
4418N/A#endif /* PNG_WRITE_FILTER_SUPPORTED */
0N/A}
0N/A
0N/A
0N/A/* Do the actual writing of a previously filtered row. */
4418N/Astatic void
0N/Apng_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
0N/A{
4418N/A png_size_t avail;
4418N/A
4418N/A png_debug(1, "in png_write_filtered_row");
4418N/A
4418N/A png_debug1(2, "filter = %d", filtered_row[0]);
4418N/A /* Set up the zlib input buffer */
0N/A
0N/A png_ptr->zstream.next_in = filtered_row;
4418N/A png_ptr->zstream.avail_in = 0;
4418N/A avail = png_ptr->row_info.rowbytes + 1;
4418N/A /* Repeat until we have compressed all the data */
0N/A do
0N/A {
4418N/A int ret; /* Return of zlib */
4418N/A
4418N/A /* Record the number of bytes available - zlib supports at least 65535
4418N/A * bytes at one step, depending on the size of the zlib type 'uInt', the
4418N/A * maximum size zlib can write at once is ZLIB_IO_MAX (from pngpriv.h).
4418N/A * Use this because on 16 bit systems 'rowbytes' can be up to 65536 (i.e.
4418N/A * one more than 16 bits) and, in this case 'rowbytes+1' can overflow a
4418N/A * uInt. ZLIB_IO_MAX can be safely reduced to cause zlib to be called
4418N/A * with smaller chunks of data.
4418N/A */
4418N/A if (png_ptr->zstream.avail_in == 0)
4418N/A {
4418N/A if (avail > ZLIB_IO_MAX)
4418N/A {
4418N/A png_ptr->zstream.avail_in = ZLIB_IO_MAX;
4418N/A avail -= ZLIB_IO_MAX;
4418N/A }
4418N/A
4418N/A else
4418N/A {
4418N/A /* So this will fit in the available uInt space: */
4418N/A png_ptr->zstream.avail_in = (uInt)avail;
4418N/A avail = 0;
4418N/A }
4418N/A }
4418N/A
4418N/A /* Compress the data */
0N/A ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
4418N/A
4418N/A /* Check for compression errors */
0N/A if (ret != Z_OK)
0N/A {
0N/A if (png_ptr->zstream.msg != NULL)
0N/A png_error(png_ptr, png_ptr->zstream.msg);
4418N/A
0N/A else
0N/A png_error(png_ptr, "zlib error");
0N/A }
0N/A
4418N/A /* See if it is time to write another IDAT */
0N/A if (!(png_ptr->zstream.avail_out))
0N/A {
4418N/A /* Write the IDAT and reset the zlib output buffer */
0N/A png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
0N/A }
4418N/A /* Repeat until all data has been compressed */
4418N/A } while (avail > 0 || png_ptr->zstream.avail_in > 0);
4418N/A
4418N/A /* Swap the current and previous rows */
0N/A if (png_ptr->prev_row != NULL)
0N/A {
0N/A png_bytep tptr;
0N/A
0N/A tptr = png_ptr->prev_row;
0N/A png_ptr->prev_row = png_ptr->row_buf;
0N/A png_ptr->row_buf = tptr;
0N/A }
0N/A
4418N/A /* Finish row - updates counters and flushes zlib if last row */
0N/A png_write_finish_row(png_ptr);
0N/A
4418N/A#ifdef PNG_WRITE_FLUSH_SUPPORTED
0N/A png_ptr->flush_rows++;
0N/A
0N/A if (png_ptr->flush_dist > 0 &&
0N/A png_ptr->flush_rows >= png_ptr->flush_dist)
0N/A {
0N/A png_write_flush(png_ptr);
0N/A }
0N/A#endif
0N/A}
0N/A#endif /* PNG_WRITE_SUPPORTED */