fukasawa e60969
fukasawa e60969
/* pngwutil.c - utilities to write a PNG file
fukasawa e60969
 *
fukasawa e60969
 * Last changed in libpng 1.6.21 [January 15, 2016]
fukasawa e60969
 * Copyright (c) 1998-2002,2004,2006-2015 Glenn Randers-Pehrson
fukasawa e60969
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
fukasawa e60969
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
fukasawa e60969
 *
fukasawa e60969
 * This code is released under the libpng license.
fukasawa e60969
 * For conditions of distribution and use, see the disclaimer
fukasawa e60969
 * and license in png.h
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
#include "pngpriv.h"
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED
fukasawa e60969
/* Place a 32-bit number into a buffer in PNG byte order.  We work
fukasawa e60969
 * with unsigned numbers for convenience, although one supported
fukasawa e60969
 * ancillary chunk uses signed (two's complement) numbers.
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_save_uint_32(png_bytep buf, png_uint_32 i)
fukasawa e60969
{
fukasawa e60969
   buf[0] = (png_byte)(i >> 24);
fukasawa e60969
   buf[1] = (png_byte)(i >> 16);
fukasawa e60969
   buf[2] = (png_byte)(i >> 8);
fukasawa e60969
   buf[3] = (png_byte)(i     );
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Place a 16-bit number into a buffer in PNG byte order.
fukasawa e60969
 * The parameter is declared unsigned int, not png_uint_16,
fukasawa e60969
 * just to avoid potential problems on pre-ANSI C compilers.
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_save_uint_16(png_bytep buf, unsigned int i)
fukasawa e60969
{
fukasawa e60969
   buf[0] = (png_byte)(i >> 8);
fukasawa e60969
   buf[1] = (png_byte)(i     );
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Simple function to write the signature.  If we have already written
fukasawa e60969
 * the magic bytes of the signature, or more likely, the PNG stream is
fukasawa e60969
 * being embedded into another stream and doesn't need its own signature,
fukasawa e60969
 * we should call png_set_sig_bytes() to tell libpng how many of the
fukasawa e60969
 * bytes have already been written.
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_write_sig(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
fukasawa e60969
fukasawa e60969
#ifdef PNG_IO_STATE_SUPPORTED
fukasawa e60969
   /* Inform the I/O callback that the signature is being written */
fukasawa e60969
   png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Write the rest of the 8 byte signature */
fukasawa e60969
   png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
fukasawa e60969
      (png_size_t)(8 - png_ptr->sig_bytes));
fukasawa e60969
fukasawa e60969
   if (png_ptr->sig_bytes < 3)
fukasawa e60969
      png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Write the start of a PNG chunk.  The type is the chunk type.
fukasawa e60969
 * The total_length is the sum of the lengths of all the data you will be
fukasawa e60969
 * passing in png_write_chunk_data().
fukasawa e60969
 */
fukasawa e60969
static void
fukasawa e60969
png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name,
fukasawa e60969
    png_uint_32 length)
fukasawa e60969
{
fukasawa e60969
   png_byte buf[8];
fukasawa e60969
fukasawa e60969
#if defined(PNG_DEBUG) && (PNG_DEBUG > 0)
fukasawa e60969
   PNG_CSTRING_FROM_CHUNK(buf, chunk_name);
fukasawa e60969
   png_debug2(0, "Writing %s chunk, length = %lu", buf, (unsigned long)length);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
#ifdef PNG_IO_STATE_SUPPORTED
fukasawa e60969
   /* Inform the I/O callback that the chunk header is being written.
fukasawa e60969
    * PNG_IO_CHUNK_HDR requires a single I/O call.
fukasawa e60969
    */
fukasawa e60969
   png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Write the length and the chunk name */
fukasawa e60969
   png_save_uint_32(buf, length);
fukasawa e60969
   png_save_uint_32(buf + 4, chunk_name);
fukasawa e60969
   png_write_data(png_ptr, buf, 8);
fukasawa e60969
fukasawa e60969
   /* Put the chunk name into png_ptr->chunk_name */
fukasawa e60969
   png_ptr->chunk_name = chunk_name;
fukasawa e60969
fukasawa e60969
   /* Reset the crc and run it over the chunk name */
fukasawa e60969
   png_reset_crc(png_ptr);
fukasawa e60969
fukasawa e60969
   png_calculate_crc(png_ptr, buf + 4, 4);
fukasawa e60969
fukasawa e60969
#ifdef PNG_IO_STATE_SUPPORTED
fukasawa e60969
   /* Inform the I/O callback that chunk data will (possibly) be written.
fukasawa e60969
    * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
fukasawa e60969
    */
fukasawa e60969
   png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
fukasawa e60969
#endif
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void PNGAPI
fukasawa e60969
png_write_chunk_start(png_structrp png_ptr, png_const_bytep chunk_string,
fukasawa e60969
    png_uint_32 length)
fukasawa e60969
{
fukasawa e60969
   png_write_chunk_header(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), length);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Write the data of a PNG chunk started with png_write_chunk_header().
fukasawa e60969
 * Note that multiple calls to this function are allowed, and that the
fukasawa e60969
 * sum of the lengths from these calls *must* add up to the total_length
fukasawa e60969
 * given to png_write_chunk_header().
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_write_chunk_data(png_structrp png_ptr, png_const_bytep data,
fukasawa e60969
    png_size_t length)
fukasawa e60969
{
fukasawa e60969
   /* Write the data, and run the CRC over it */
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   if (data != NULL && length > 0)
fukasawa e60969
   {
fukasawa e60969
      png_write_data(png_ptr, data, length);
fukasawa e60969
fukasawa e60969
      /* Update the CRC after writing the data,
fukasawa e60969
       * in case the user I/O routine alters it.
fukasawa e60969
       */
fukasawa e60969
      png_calculate_crc(png_ptr, data, length);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Finish a chunk started with png_write_chunk_header(). */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_write_chunk_end(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   png_byte buf[4];
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL) return;
fukasawa e60969
fukasawa e60969
#ifdef PNG_IO_STATE_SUPPORTED
fukasawa e60969
   /* Inform the I/O callback that the chunk CRC is being written.
fukasawa e60969
    * PNG_IO_CHUNK_CRC requires a single I/O function call.
fukasawa e60969
    */
fukasawa e60969
   png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Write the crc in a single operation */
fukasawa e60969
   png_save_uint_32(buf, png_ptr->crc);
fukasawa e60969
fukasawa e60969
   png_write_data(png_ptr, buf, (png_size_t)4);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Write a PNG chunk all at once.  The type is an array of ASCII characters
fukasawa e60969
 * representing the chunk name.  The array must be at least 4 bytes in
fukasawa e60969
 * length, and does not need to be null terminated.  To be safe, pass the
fukasawa e60969
 * pre-defined chunk names here, and if you need a new one, define it
fukasawa e60969
 * where the others are defined.  The length is the length of the data.
fukasawa e60969
 * All the data must be present.  If that is not possible, use the
fukasawa e60969
 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
fukasawa e60969
 * functions instead.
fukasawa e60969
 */
fukasawa e60969
static void
fukasawa e60969
png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name,
fukasawa e60969
   png_const_bytep data, png_size_t length)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   /* On 64-bit architectures 'length' may not fit in a png_uint_32. */
fukasawa e60969
   if (length > PNG_UINT_31_MAX)
fukasawa e60969
      png_error(png_ptr, "length exceeds PNG maximum");
fukasawa e60969
fukasawa e60969
   png_write_chunk_header(png_ptr, chunk_name, (png_uint_32)length);
fukasawa e60969
   png_write_chunk_data(png_ptr, data, length);
fukasawa e60969
   png_write_chunk_end(png_ptr);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* This is the API that calls the internal function above. */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_write_chunk(png_structrp png_ptr, png_const_bytep chunk_string,
fukasawa e60969
   png_const_bytep data, png_size_t length)
fukasawa e60969
{
fukasawa e60969
   png_write_complete_chunk(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), data,
fukasawa e60969
      length);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* This is used below to find the size of an image to pass to png_deflate_claim,
fukasawa e60969
 * so it only needs to be accurate if the size is less than 16384 bytes (the
fukasawa e60969
 * point at which a lower LZ window size can be used.)
fukasawa e60969
 */
fukasawa e60969
static png_alloc_size_t
fukasawa e60969
png_image_size(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   /* Only return sizes up to the maximum of a png_uint_32; do this by limiting
fukasawa e60969
    * the width and height used to 15 bits.
fukasawa e60969
    */
fukasawa e60969
   png_uint_32 h = png_ptr->height;
fukasawa e60969
fukasawa e60969
   if (png_ptr->rowbytes < 32768 && h < 32768)
fukasawa e60969
   {
fukasawa e60969
      if (png_ptr->interlaced != 0)
fukasawa e60969
      {
fukasawa e60969
         /* Interlacing makes the image larger because of the replication of
fukasawa e60969
          * both the filter byte and the padding to a byte boundary.
fukasawa e60969
          */
fukasawa e60969
         png_uint_32 w = png_ptr->width;
fukasawa e60969
         unsigned int pd = png_ptr->pixel_depth;
fukasawa e60969
         png_alloc_size_t cb_base;
fukasawa e60969
         int pass;
fukasawa e60969
fukasawa e60969
         for (cb_base=0, pass=0; pass<=6; ++pass)
fukasawa e60969
         {
fukasawa e60969
            png_uint_32 pw = PNG_PASS_COLS(w, pass);
fukasawa e60969
fukasawa e60969
            if (pw > 0)
fukasawa e60969
               cb_base += (PNG_ROWBYTES(pd, pw)+1) * PNG_PASS_ROWS(h, pass);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         return cb_base;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         return (png_ptr->rowbytes+1) * h;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      return 0xffffffffU;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
fukasawa e60969
   /* This is the code to hack the first two bytes of the deflate stream (the
fukasawa e60969
    * deflate header) to correct the windowBits value to match the actual data
fukasawa e60969
    * size.  Note that the second argument is the *uncompressed* size but the
fukasawa e60969
    * first argument is the *compressed* data (and it must be deflate
fukasawa e60969
    * compressed.)
fukasawa e60969
    */
fukasawa e60969
static void
fukasawa e60969
optimize_cmf(png_bytep data, png_alloc_size_t data_size)
fukasawa e60969
{
fukasawa e60969
   /* Optimize the CMF field in the zlib stream.  The resultant zlib stream is
fukasawa e60969
    * still compliant to the stream specification.
fukasawa e60969
    */
fukasawa e60969
   if (data_size <= 16384) /* else windowBits must be 15 */
fukasawa e60969
   {
fukasawa e60969
      unsigned int z_cmf = data[0];  /* zlib compression method and flags */
fukasawa e60969
fukasawa e60969
      if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
fukasawa e60969
      {
fukasawa e60969
         unsigned int z_cinfo;
fukasawa e60969
         unsigned int half_z_window_size;
fukasawa e60969
fukasawa e60969
         z_cinfo = z_cmf >> 4;
fukasawa e60969
         half_z_window_size = 1U << (z_cinfo + 7);
fukasawa e60969
fukasawa e60969
         if (data_size <= half_z_window_size) /* else no change */
fukasawa e60969
         {
fukasawa e60969
            unsigned int tmp;
fukasawa e60969
fukasawa e60969
            do
fukasawa e60969
            {
fukasawa e60969
               half_z_window_size >>= 1;
fukasawa e60969
               --z_cinfo;
fukasawa e60969
            }
fukasawa e60969
            while (z_cinfo > 0 && data_size <= half_z_window_size);
fukasawa e60969
fukasawa e60969
            z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
fukasawa e60969
fukasawa e60969
            data[0] = (png_byte)z_cmf;
fukasawa e60969
            tmp = data[1] & 0xe0;
fukasawa e60969
            tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
fukasawa e60969
            data[1] = (png_byte)tmp;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif /* WRITE_OPTIMIZE_CMF */
fukasawa e60969
fukasawa e60969
/* Initialize the compressor for the appropriate type of compression. */
fukasawa e60969
static int
fukasawa e60969
png_deflate_claim(png_structrp png_ptr, png_uint_32 owner,
fukasawa e60969
   png_alloc_size_t data_size)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr->zowner != 0)
fukasawa e60969
   {
fukasawa e60969
#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)
fukasawa e60969
      char msg[64];
fukasawa e60969
fukasawa e60969
      PNG_STRING_FROM_CHUNK(msg, owner);
fukasawa e60969
      msg[4] = ':';
fukasawa e60969
      msg[5] = ' ';
fukasawa e60969
      PNG_STRING_FROM_CHUNK(msg+6, png_ptr->zowner);
fukasawa e60969
      /* So the message that results is "<chunk> using zstream"; this is an</chunk>
fukasawa e60969
       * internal error, but is very useful for debugging.  i18n requirements
fukasawa e60969
       * are minimal.
fukasawa e60969
       */
fukasawa e60969
      (void)png_safecat(msg, (sizeof msg), 10, " using zstream");
fukasawa e60969
#endif
fukasawa e60969
#if PNG_RELEASE_BUILD
fukasawa e60969
         png_warning(png_ptr, msg);
fukasawa e60969
fukasawa e60969
         /* Attempt sane error recovery */
fukasawa e60969
         if (png_ptr->zowner == png_IDAT) /* don't steal from IDAT */
fukasawa e60969
         {
fukasawa e60969
            png_ptr->zstream.msg = PNGZ_MSG_CAST("in use by IDAT");
fukasawa e60969
            return Z_STREAM_ERROR;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         png_ptr->zowner = 0;
fukasawa e60969
#else
fukasawa e60969
         png_error(png_ptr, msg);
fukasawa e60969
#endif
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   {
fukasawa e60969
      int level = png_ptr->zlib_level;
fukasawa e60969
      int method = png_ptr->zlib_method;
fukasawa e60969
      int windowBits = png_ptr->zlib_window_bits;
fukasawa e60969
      int memLevel = png_ptr->zlib_mem_level;
fukasawa e60969
      int strategy; /* set below */
fukasawa e60969
      int ret; /* zlib return code */
fukasawa e60969
fukasawa e60969
      if (owner == png_IDAT)
fukasawa e60969
      {
fukasawa e60969
         if ((png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY) != 0)
fukasawa e60969
            strategy = png_ptr->zlib_strategy;
fukasawa e60969
fukasawa e60969
         else if (png_ptr->do_filter != PNG_FILTER_NONE)
fukasawa e60969
            strategy = PNG_Z_DEFAULT_STRATEGY;
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            strategy = PNG_Z_DEFAULT_NOFILTER_STRATEGY;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
fukasawa e60969
            level = png_ptr->zlib_text_level;
fukasawa e60969
            method = png_ptr->zlib_text_method;
fukasawa e60969
            windowBits = png_ptr->zlib_text_window_bits;
fukasawa e60969
            memLevel = png_ptr->zlib_text_mem_level;
fukasawa e60969
            strategy = png_ptr->zlib_text_strategy;
fukasawa e60969
#else
fukasawa e60969
            /* If customization is not supported the values all come from the
fukasawa e60969
             * IDAT values except for the strategy, which is fixed to the
fukasawa e60969
             * default.  (This is the pre-1.6.0 behavior too, although it was
fukasawa e60969
             * implemented in a very different way.)
fukasawa e60969
             */
fukasawa e60969
            strategy = Z_DEFAULT_STRATEGY;
fukasawa e60969
#endif
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Adjust 'windowBits' down if larger than 'data_size'; to stop this
fukasawa e60969
       * happening just pass 32768 as the data_size parameter.  Notice that zlib
fukasawa e60969
       * requires an extra 262 bytes in the window in addition to the data to be
fukasawa e60969
       * able to see the whole of the data, so if data_size+262 takes us to the
fukasawa e60969
       * next windowBits size we need to fix up the value later.  (Because even
fukasawa e60969
       * though deflate needs the extra window, inflate does not!)
fukasawa e60969
       */
fukasawa e60969
      if (data_size <= 16384)
fukasawa e60969
      {
fukasawa e60969
         /* IMPLEMENTATION NOTE: this 'half_window_size' stuff is only here to
fukasawa e60969
          * work round a Microsoft Visual C misbehavior which, contrary to C-90,
fukasawa e60969
          * widens the result of the following shift to 64-bits if (and,
fukasawa e60969
          * apparently, only if) it is used in a test.
fukasawa e60969
          */
fukasawa e60969
         unsigned int half_window_size = 1U << (windowBits-1);
fukasawa e60969
fukasawa e60969
         while (data_size + 262 <= half_window_size)
fukasawa e60969
         {
fukasawa e60969
            half_window_size >>= 1;
fukasawa e60969
            --windowBits;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Check against the previous initialized values, if any. */
fukasawa e60969
      if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0 &&
fukasawa e60969
         (png_ptr->zlib_set_level != level ||
fukasawa e60969
         png_ptr->zlib_set_method != method ||
fukasawa e60969
         png_ptr->zlib_set_window_bits != windowBits ||
fukasawa e60969
         png_ptr->zlib_set_mem_level != memLevel ||
fukasawa e60969
         png_ptr->zlib_set_strategy != strategy))
fukasawa e60969
      {
fukasawa e60969
         if (deflateEnd(&png_ptr->zstream) != Z_OK)
fukasawa e60969
            png_warning(png_ptr, "deflateEnd failed (ignored)");
fukasawa e60969
fukasawa e60969
         png_ptr->flags &= ~PNG_FLAG_ZSTREAM_INITIALIZED;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* For safety clear out the input and output pointers (currently zlib
fukasawa e60969
       * doesn't use them on Init, but it might in the future).
fukasawa e60969
       */
fukasawa e60969
      png_ptr->zstream.next_in = NULL;
fukasawa e60969
      png_ptr->zstream.avail_in = 0;
fukasawa e60969
      png_ptr->zstream.next_out = NULL;
fukasawa e60969
      png_ptr->zstream.avail_out = 0;
fukasawa e60969
fukasawa e60969
      /* Now initialize if required, setting the new parameters, otherwise just
fukasawa e60969
       * to a simple reset to the previous parameters.
fukasawa e60969
       */
fukasawa e60969
      if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
fukasawa e60969
         ret = deflateReset(&png_ptr->zstream);
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         ret = deflateInit2(&png_ptr->zstream, level, method, windowBits,
fukasawa e60969
            memLevel, strategy);
fukasawa e60969
fukasawa e60969
         if (ret == Z_OK)
fukasawa e60969
            png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* The return code is from either deflateReset or deflateInit2; they have
fukasawa e60969
       * pretty much the same set of error codes.
fukasawa e60969
       */
fukasawa e60969
      if (ret == Z_OK)
fukasawa e60969
         png_ptr->zowner = owner;
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         png_zstream_error(png_ptr, ret);
fukasawa e60969
fukasawa e60969
      return ret;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Clean up (or trim) a linked list of compression buffers. */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_free_buffer_list(png_structrp png_ptr, png_compression_bufferp *listp)
fukasawa e60969
{
fukasawa e60969
   png_compression_bufferp list = *listp;
fukasawa e60969
fukasawa e60969
   if (list != NULL)
fukasawa e60969
   {
fukasawa e60969
      *listp = NULL;
fukasawa e60969
fukasawa e60969
      do
fukasawa e60969
      {
fukasawa e60969
         png_compression_bufferp next = list->next;
fukasawa e60969
fukasawa e60969
         png_free(png_ptr, list);
fukasawa e60969
         list = next;
fukasawa e60969
      }
fukasawa e60969
      while (list != NULL);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
fukasawa e60969
/* This pair of functions encapsulates the operation of (a) compressing a
fukasawa e60969
 * text string, and (b) issuing it later as a series of chunk data writes.
fukasawa e60969
 * The compression_state structure is shared context for these functions
fukasawa e60969
 * set up by the caller to allow access to the relevant local variables.
fukasawa e60969
 *
fukasawa e60969
 * compression_buffer (new in 1.6.0) is just a linked list of zbuffer_size
fukasawa e60969
 * temporary buffers.  From 1.6.0 it is retained in png_struct so that it will
fukasawa e60969
 * be correctly freed in the event of a write error (previous implementations
fukasawa e60969
 * just leaked memory.)
fukasawa e60969
 */
fukasawa e60969
typedef struct
fukasawa e60969
{
fukasawa e60969
   png_const_bytep      input;        /* The uncompressed input data */
fukasawa e60969
   png_alloc_size_t     input_len;    /* Its length */
fukasawa e60969
   png_uint_32          output_len;   /* Final compressed length */
fukasawa e60969
   png_byte             output[1024]; /* First block of output */
fukasawa e60969
} compression_state;
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
png_text_compress_init(compression_state *comp, png_const_bytep input,
fukasawa e60969
   png_alloc_size_t input_len)
fukasawa e60969
{
fukasawa e60969
   comp->input = input;
fukasawa e60969
   comp->input_len = input_len;
fukasawa e60969
   comp->output_len = 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Compress the data in the compression state input */
fukasawa e60969
static int
fukasawa e60969
png_text_compress(png_structrp png_ptr, png_uint_32 chunk_name,
fukasawa e60969
   compression_state *comp, png_uint_32 prefix_len)
fukasawa e60969
{
fukasawa e60969
   int ret;
fukasawa e60969
fukasawa e60969
   /* To find the length of the output it is necessary to first compress the
fukasawa e60969
    * input. The result is buffered rather than using the two-pass algorithm
fukasawa e60969
    * that is used on the inflate side; deflate is assumed to be slower and a
fukasawa e60969
    * PNG writer is assumed to have more memory available than a PNG reader.
fukasawa e60969
    *
fukasawa e60969
    * IMPLEMENTATION NOTE: the zlib API deflateBound() can be used to find an
fukasawa e60969
    * upper limit on the output size, but it is always bigger than the input
fukasawa e60969
    * size so it is likely to be more efficient to use this linked-list
fukasawa e60969
    * approach.
fukasawa e60969
    */
fukasawa e60969
   ret = png_deflate_claim(png_ptr, chunk_name, comp->input_len);
fukasawa e60969
fukasawa e60969
   if (ret != Z_OK)
fukasawa e60969
      return ret;
fukasawa e60969
fukasawa e60969
   /* Set up the compression buffers, we need a loop here to avoid overflowing a
fukasawa e60969
    * uInt.  Use ZLIB_IO_MAX to limit the input.  The output is always limited
fukasawa e60969
    * by the output buffer size, so there is no need to check that.  Since this
fukasawa e60969
    * is ANSI-C we know that an 'int', hence a uInt, is always at least 16 bits
fukasawa e60969
    * in size.
fukasawa e60969
    */
fukasawa e60969
   {
fukasawa e60969
      png_compression_bufferp *end = &png_ptr->zbuffer_list;
fukasawa e60969
      png_alloc_size_t input_len = comp->input_len; /* may be zero! */
fukasawa e60969
      png_uint_32 output_len;
fukasawa e60969
fukasawa e60969
      /* zlib updates these for us: */
fukasawa e60969
      png_ptr->zstream.next_in = PNGZ_INPUT_CAST(comp->input);
fukasawa e60969
      png_ptr->zstream.avail_in = 0; /* Set below */
fukasawa e60969
      png_ptr->zstream.next_out = comp->output;
fukasawa e60969
      png_ptr->zstream.avail_out = (sizeof comp->output);
fukasawa e60969
fukasawa e60969
      output_len = png_ptr->zstream.avail_out;
fukasawa e60969
fukasawa e60969
      do
fukasawa e60969
      {
fukasawa e60969
         uInt avail_in = ZLIB_IO_MAX;
fukasawa e60969
fukasawa e60969
         if (avail_in > input_len)
fukasawa e60969
            avail_in = (uInt)input_len;
fukasawa e60969
fukasawa e60969
         input_len -= avail_in;
fukasawa e60969
fukasawa e60969
         png_ptr->zstream.avail_in = avail_in;
fukasawa e60969
fukasawa e60969
         if (png_ptr->zstream.avail_out == 0)
fukasawa e60969
         {
fukasawa e60969
            png_compression_buffer *next;
fukasawa e60969
fukasawa e60969
            /* Chunk data is limited to 2^31 bytes in length, so the prefix
fukasawa e60969
             * length must be counted here.
fukasawa e60969
             */
fukasawa e60969
            if (output_len + prefix_len > PNG_UINT_31_MAX)
fukasawa e60969
            {
fukasawa e60969
               ret = Z_MEM_ERROR;
fukasawa e60969
               break;
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            /* Need a new (malloc'ed) buffer, but there may be one present
fukasawa e60969
             * already.
fukasawa e60969
             */
fukasawa e60969
            next = *end;
fukasawa e60969
            if (next == NULL)
fukasawa e60969
            {
fukasawa e60969
               next = png_voidcast(png_compression_bufferp, png_malloc_base
fukasawa e60969
                  (png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr)));
fukasawa e60969
fukasawa e60969
               if (next == NULL)
fukasawa e60969
               {
fukasawa e60969
                  ret = Z_MEM_ERROR;
fukasawa e60969
                  break;
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               /* Link in this buffer (so that it will be freed later) */
fukasawa e60969
               next->next = NULL;
fukasawa e60969
               *end = next;
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            png_ptr->zstream.next_out = next->output;
fukasawa e60969
            png_ptr->zstream.avail_out = png_ptr->zbuffer_size;
fukasawa e60969
            output_len += png_ptr->zstream.avail_out;
fukasawa e60969
fukasawa e60969
            /* Move 'end' to the next buffer pointer. */
fukasawa e60969
            end = &next->next;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         /* Compress the data */
fukasawa e60969
         ret = deflate(&png_ptr->zstream,
fukasawa e60969
            input_len > 0 ? Z_NO_FLUSH : Z_FINISH);
fukasawa e60969
fukasawa e60969
         /* Claw back input data that was not consumed (because avail_in is
fukasawa e60969
          * reset above every time round the loop).
fukasawa e60969
          */
fukasawa e60969
         input_len += png_ptr->zstream.avail_in;
fukasawa e60969
         png_ptr->zstream.avail_in = 0; /* safety */
fukasawa e60969
      }
fukasawa e60969
      while (ret == Z_OK);
fukasawa e60969
fukasawa e60969
      /* There may be some space left in the last output buffer. This needs to
fukasawa e60969
       * be subtracted from output_len.
fukasawa e60969
       */
fukasawa e60969
      output_len -= png_ptr->zstream.avail_out;
fukasawa e60969
      png_ptr->zstream.avail_out = 0; /* safety */
fukasawa e60969
      comp->output_len = output_len;
fukasawa e60969
fukasawa e60969
      /* Now double check the output length, put in a custom message if it is
fukasawa e60969
       * too long.  Otherwise ensure the z_stream::msg pointer is set to
fukasawa e60969
       * something.
fukasawa e60969
       */
fukasawa e60969
      if (output_len + prefix_len >= PNG_UINT_31_MAX)
fukasawa e60969
      {
fukasawa e60969
         png_ptr->zstream.msg = PNGZ_MSG_CAST("compressed data too long");
fukasawa e60969
         ret = Z_MEM_ERROR;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         png_zstream_error(png_ptr, ret);
fukasawa e60969
fukasawa e60969
      /* Reset zlib for another zTXt/iTXt or image data */
fukasawa e60969
      png_ptr->zowner = 0;
fukasawa e60969
fukasawa e60969
      /* The only success case is Z_STREAM_END, input_len must be 0; if not this
fukasawa e60969
       * is an internal error.
fukasawa e60969
       */
fukasawa e60969
      if (ret == Z_STREAM_END && input_len == 0)
fukasawa e60969
      {
fukasawa e60969
#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
fukasawa e60969
         /* Fix up the deflate header, if required */
fukasawa e60969
         optimize_cmf(comp->output, comp->input_len);
fukasawa e60969
#endif
fukasawa e60969
         /* But Z_OK is returned, not Z_STREAM_END; this allows the claim
fukasawa e60969
          * function above to return Z_STREAM_END on an error (though it never
fukasawa e60969
          * does in the current versions of zlib.)
fukasawa e60969
          */
fukasawa e60969
         return Z_OK;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         return ret;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Ship the compressed text out via chunk writes */
fukasawa e60969
static void
fukasawa e60969
png_write_compressed_data_out(png_structrp png_ptr, compression_state *comp)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 output_len = comp->output_len;
fukasawa e60969
   png_const_bytep output = comp->output;
fukasawa e60969
   png_uint_32 avail = (sizeof comp->output);
fukasawa e60969
   png_compression_buffer *next = png_ptr->zbuffer_list;
fukasawa e60969
fukasawa e60969
   for (;;)
fukasawa e60969
   {
fukasawa e60969
      if (avail > output_len)
fukasawa e60969
         avail = output_len;
fukasawa e60969
fukasawa e60969
      png_write_chunk_data(png_ptr, output, avail);
fukasawa e60969
fukasawa e60969
      output_len -= avail;
fukasawa e60969
fukasawa e60969
      if (output_len == 0 || next == NULL)
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      avail = png_ptr->zbuffer_size;
fukasawa e60969
      output = next->output;
fukasawa e60969
      next = next->next;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* This is an internal error; 'next' must have been NULL! */
fukasawa e60969
   if (output_len > 0)
fukasawa e60969
      png_error(png_ptr, "error writing ancillary chunked compressed data");
fukasawa e60969
}
fukasawa e60969
#endif /* WRITE_COMPRESSED_TEXT */
fukasawa e60969
fukasawa e60969
/* Write the IHDR chunk, and update the png_struct with the necessary
fukasawa e60969
 * information.  Note that the rest of this code depends upon this
fukasawa e60969
 * information being correct.
fukasawa e60969
 */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height,
fukasawa e60969
    int bit_depth, int color_type, int compression_type, int filter_type,
fukasawa e60969
    int interlace_type)
fukasawa e60969
{
fukasawa e60969
   png_byte buf[13]; /* Buffer to store the IHDR info */
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_IHDR");
fukasawa e60969
fukasawa e60969
   /* Check that we have valid input data from the application info */
fukasawa e60969
   switch (color_type)
fukasawa e60969
   {
fukasawa e60969
      case PNG_COLOR_TYPE_GRAY:
fukasawa e60969
         switch (bit_depth)
fukasawa e60969
         {
fukasawa e60969
            case 1:
fukasawa e60969
            case 2:
fukasawa e60969
            case 4:
fukasawa e60969
            case 8:
fukasawa e60969
#ifdef PNG_WRITE_16BIT_SUPPORTED
fukasawa e60969
            case 16:
fukasawa e60969
#endif
fukasawa e60969
               png_ptr->channels = 1; break;
fukasawa e60969
fukasawa e60969
            default:
fukasawa e60969
               png_error(png_ptr,
fukasawa e60969
                   "Invalid bit depth for grayscale image");
fukasawa e60969
         }
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case PNG_COLOR_TYPE_RGB:
fukasawa e60969
#ifdef PNG_WRITE_16BIT_SUPPORTED
fukasawa e60969
         if (bit_depth != 8 && bit_depth != 16)
fukasawa e60969
#else
fukasawa e60969
         if (bit_depth != 8)
fukasawa e60969
#endif
fukasawa e60969
            png_error(png_ptr, "Invalid bit depth for RGB image");
fukasawa e60969
fukasawa e60969
         png_ptr->channels = 3;
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case PNG_COLOR_TYPE_PALETTE:
fukasawa e60969
         switch (bit_depth)
fukasawa e60969
         {
fukasawa e60969
            case 1:
fukasawa e60969
            case 2:
fukasawa e60969
            case 4:
fukasawa e60969
            case 8:
fukasawa e60969
               png_ptr->channels = 1;
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            default:
fukasawa e60969
               png_error(png_ptr, "Invalid bit depth for paletted image");
fukasawa e60969
         }
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case PNG_COLOR_TYPE_GRAY_ALPHA:
fukasawa e60969
         if (bit_depth != 8 && bit_depth != 16)
fukasawa e60969
            png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
fukasawa e60969
fukasawa e60969
         png_ptr->channels = 2;
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case PNG_COLOR_TYPE_RGB_ALPHA:
fukasawa e60969
#ifdef PNG_WRITE_16BIT_SUPPORTED
fukasawa e60969
         if (bit_depth != 8 && bit_depth != 16)
fukasawa e60969
#else
fukasawa e60969
         if (bit_depth != 8)
fukasawa e60969
#endif
fukasawa e60969
            png_error(png_ptr, "Invalid bit depth for RGBA image");
fukasawa e60969
fukasawa e60969
         png_ptr->channels = 4;
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      default:
fukasawa e60969
         png_error(png_ptr, "Invalid image color type specified");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
fukasawa e60969
   {
fukasawa e60969
      png_warning(png_ptr, "Invalid compression type specified");
fukasawa e60969
      compression_type = PNG_COMPRESSION_TYPE_BASE;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Write filter_method 64 (intrapixel differencing) only if
fukasawa e60969
    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
fukasawa e60969
    * 2. Libpng did not write a PNG signature (this filter_method is only
fukasawa e60969
    *    used in PNG datastreams that are embedded in MNG datastreams) and
fukasawa e60969
    * 3. The application called png_permit_mng_features with a mask that
fukasawa e60969
    *    included PNG_FLAG_MNG_FILTER_64 and
fukasawa e60969
    * 4. The filter_method is 64 and
fukasawa e60969
    * 5. The color_type is RGB or RGBA
fukasawa e60969
    */
fukasawa e60969
   if (
fukasawa e60969
#ifdef PNG_MNG_FEATURES_SUPPORTED
fukasawa e60969
       !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
fukasawa e60969
       ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
fukasawa e60969
       (color_type == PNG_COLOR_TYPE_RGB ||
fukasawa e60969
        color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
fukasawa e60969
       (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
fukasawa e60969
#endif
fukasawa e60969
       filter_type != PNG_FILTER_TYPE_BASE)
fukasawa e60969
   {
fukasawa e60969
      png_warning(png_ptr, "Invalid filter type specified");
fukasawa e60969
      filter_type = PNG_FILTER_TYPE_BASE;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
fukasawa e60969
   if (interlace_type != PNG_INTERLACE_NONE &&
fukasawa e60969
       interlace_type != PNG_INTERLACE_ADAM7)
fukasawa e60969
   {
fukasawa e60969
      png_warning(png_ptr, "Invalid interlace type specified");
fukasawa e60969
      interlace_type = PNG_INTERLACE_ADAM7;
fukasawa e60969
   }
fukasawa e60969
#else
fukasawa e60969
   interlace_type=PNG_INTERLACE_NONE;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Save the relevant information */
fukasawa e60969
   png_ptr->bit_depth = (png_byte)bit_depth;
fukasawa e60969
   png_ptr->color_type = (png_byte)color_type;
fukasawa e60969
   png_ptr->interlaced = (png_byte)interlace_type;
fukasawa e60969
#ifdef PNG_MNG_FEATURES_SUPPORTED
fukasawa e60969
   png_ptr->filter_type = (png_byte)filter_type;
fukasawa e60969
#endif
fukasawa e60969
   png_ptr->compression_type = (png_byte)compression_type;
fukasawa e60969
   png_ptr->width = width;
fukasawa e60969
   png_ptr->height = height;
fukasawa e60969
fukasawa e60969
   png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
fukasawa e60969
   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
fukasawa e60969
   /* Set the usr info, so any transformations can modify it */
fukasawa e60969
   png_ptr->usr_width = png_ptr->width;
fukasawa e60969
   png_ptr->usr_bit_depth = png_ptr->bit_depth;
fukasawa e60969
   png_ptr->usr_channels = png_ptr->channels;
fukasawa e60969
fukasawa e60969
   /* Pack the header information into the buffer */
fukasawa e60969
   png_save_uint_32(buf, width);
fukasawa e60969
   png_save_uint_32(buf + 4, height);
fukasawa e60969
   buf[8] = (png_byte)bit_depth;
fukasawa e60969
   buf[9] = (png_byte)color_type;
fukasawa e60969
   buf[10] = (png_byte)compression_type;
fukasawa e60969
   buf[11] = (png_byte)filter_type;
fukasawa e60969
   buf[12] = (png_byte)interlace_type;
fukasawa e60969
fukasawa e60969
   /* Write the chunk */
fukasawa e60969
   png_write_complete_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
fukasawa e60969
fukasawa e60969
   if ((png_ptr->do_filter) == PNG_NO_FILTERS)
fukasawa e60969
   {
fukasawa e60969
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
fukasawa e60969
          png_ptr->bit_depth < 8)
fukasawa e60969
         png_ptr->do_filter = PNG_FILTER_NONE;
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         png_ptr->do_filter = PNG_ALL_FILTERS;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Write the palette.  We are careful not to trust png_color to be in the
fukasawa e60969
 * correct order for PNG, so people can redefine it to any convenient
fukasawa e60969
 * structure.
fukasawa e60969
 */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_PLTE(png_structrp png_ptr, png_const_colorp palette,
fukasawa e60969
    png_uint_32 num_pal)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 max_palette_length, i;
fukasawa e60969
   png_const_colorp pal_ptr;
fukasawa e60969
   png_byte buf[3];
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_PLTE");
fukasawa e60969
fukasawa e60969
   max_palette_length = (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
fukasawa e60969
      (1 << png_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
fukasawa e60969
fukasawa e60969
   if ((
fukasawa e60969
#ifdef PNG_MNG_FEATURES_SUPPORTED
fukasawa e60969
       (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0 &&
fukasawa e60969
#endif
fukasawa e60969
       num_pal == 0) || num_pal > max_palette_length)
fukasawa e60969
   {
fukasawa e60969
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
fukasawa e60969
      {
fukasawa e60969
         png_error(png_ptr, "Invalid number of colors in palette");
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         png_warning(png_ptr, "Invalid number of colors in palette");
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
fukasawa e60969
   {
fukasawa e60969
      png_warning(png_ptr,
fukasawa e60969
          "Ignoring request to write a PLTE chunk in grayscale PNG");
fukasawa e60969
fukasawa e60969
      return;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_ptr->num_palette = (png_uint_16)num_pal;
fukasawa e60969
   png_debug1(3, "num_palette = %d", png_ptr->num_palette);
fukasawa e60969
fukasawa e60969
   png_write_chunk_header(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3));
fukasawa e60969
#ifdef PNG_POINTER_INDEXING_SUPPORTED
fukasawa e60969
fukasawa e60969
   for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
fukasawa e60969
   {
fukasawa e60969
      buf[0] = pal_ptr->red;
fukasawa e60969
      buf[1] = pal_ptr->green;
fukasawa e60969
      buf[2] = pal_ptr->blue;
fukasawa e60969
      png_write_chunk_data(png_ptr, buf, (png_size_t)3);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#else
fukasawa e60969
   /* This is a little slower but some buggy compilers need to do this
fukasawa e60969
    * instead
fukasawa e60969
    */
fukasawa e60969
   pal_ptr=palette;
fukasawa e60969
fukasawa e60969
   for (i = 0; i < num_pal; i++)
fukasawa e60969
   {
fukasawa e60969
      buf[0] = pal_ptr[i].red;
fukasawa e60969
      buf[1] = pal_ptr[i].green;
fukasawa e60969
      buf[2] = pal_ptr[i].blue;
fukasawa e60969
      png_write_chunk_data(png_ptr, buf, (png_size_t)3);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
   png_write_chunk_end(png_ptr);
fukasawa e60969
   png_ptr->mode |= PNG_HAVE_PLTE;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* This is similar to png_text_compress, above, except that it does not require
fukasawa e60969
 * all of the data at once and, instead of buffering the compressed result,
fukasawa e60969
 * writes it as IDAT chunks.  Unlike png_text_compress it *can* png_error out
fukasawa e60969
 * because it calls the write interface.  As a result it does its own error
fukasawa e60969
 * reporting and does not return an error code.  In the event of error it will
fukasawa e60969
 * just call png_error.  The input data length may exceed 32-bits.  The 'flush'
fukasawa e60969
 * parameter is exactly the same as that to deflate, with the following
fukasawa e60969
 * meanings:
fukasawa e60969
 *
fukasawa e60969
 * Z_NO_FLUSH: normal incremental output of compressed data
fukasawa e60969
 * Z_SYNC_FLUSH: do a SYNC_FLUSH, used by png_write_flush
fukasawa e60969
 * Z_FINISH: this is the end of the input, do a Z_FINISH and clean up
fukasawa e60969
 *
fukasawa e60969
 * The routine manages the acquire and release of the png_ptr->zstream by
fukasawa e60969
 * checking and (at the end) clearing png_ptr->zowner; it does some sanity
fukasawa e60969
 * checks on the 'mode' flags while doing this.
fukasawa e60969
 */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_compress_IDAT(png_structrp png_ptr, png_const_bytep input,
fukasawa e60969
   png_alloc_size_t input_len, int flush)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr->zowner != png_IDAT)
fukasawa e60969
   {
fukasawa e60969
      /* First time.   Ensure we have a temporary buffer for compression and
fukasawa e60969
       * trim the buffer list if it has more than one entry to free memory.
fukasawa e60969
       * If 'WRITE_COMPRESSED_TEXT' is not set the list will never have been
fukasawa e60969
       * created at this point, but the check here is quick and safe.
fukasawa e60969
       */
fukasawa e60969
      if (png_ptr->zbuffer_list == NULL)
fukasawa e60969
      {
fukasawa e60969
         png_ptr->zbuffer_list = png_voidcast(png_compression_bufferp,
fukasawa e60969
            png_malloc(png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr)));
fukasawa e60969
         png_ptr->zbuffer_list->next = NULL;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list->next);
fukasawa e60969
fukasawa e60969
      /* It is a terminal error if we can't claim the zstream. */
fukasawa e60969
      if (png_deflate_claim(png_ptr, png_IDAT, png_image_size(png_ptr)) != Z_OK)
fukasawa e60969
         png_error(png_ptr, png_ptr->zstream.msg);
fukasawa e60969
fukasawa e60969
      /* The output state is maintained in png_ptr->zstream, so it must be
fukasawa e60969
       * initialized here after the claim.
fukasawa e60969
       */
fukasawa e60969
      png_ptr->zstream.next_out = png_ptr->zbuffer_list->output;
fukasawa e60969
      png_ptr->zstream.avail_out = png_ptr->zbuffer_size;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Now loop reading and writing until all the input is consumed or an error
fukasawa e60969
    * terminates the operation.  The _out values are maintained across calls to
fukasawa e60969
    * this function, but the input must be reset each time.
fukasawa e60969
    */
fukasawa e60969
   png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
fukasawa e60969
   png_ptr->zstream.avail_in = 0; /* set below */
fukasawa e60969
   for (;;)
fukasawa e60969
   {
fukasawa e60969
      int ret;
fukasawa e60969
fukasawa e60969
      /* INPUT: from the row data */
fukasawa e60969
      uInt avail = ZLIB_IO_MAX;
fukasawa e60969
fukasawa e60969
      if (avail > input_len)
fukasawa e60969
         avail = (uInt)input_len; /* safe because of the check */
fukasawa e60969
fukasawa e60969
      png_ptr->zstream.avail_in = avail;
fukasawa e60969
      input_len -= avail;
fukasawa e60969
fukasawa e60969
      ret = deflate(&png_ptr->zstream, input_len > 0 ? Z_NO_FLUSH : flush);
fukasawa e60969
fukasawa e60969
      /* Include as-yet unconsumed input */
fukasawa e60969
      input_len += png_ptr->zstream.avail_in;
fukasawa e60969
      png_ptr->zstream.avail_in = 0;
fukasawa e60969
fukasawa e60969
      /* OUTPUT: write complete IDAT chunks when avail_out drops to zero. Note
fukasawa e60969
       * that these two zstream fields are preserved across the calls, therefore
fukasawa e60969
       * there is no need to set these up on entry to the loop.
fukasawa e60969
       */
fukasawa e60969
      if (png_ptr->zstream.avail_out == 0)
fukasawa e60969
      {
fukasawa e60969
         png_bytep data = png_ptr->zbuffer_list->output;
fukasawa e60969
         uInt size = png_ptr->zbuffer_size;
fukasawa e60969
fukasawa e60969
         /* Write an IDAT containing the data then reset the buffer.  The
fukasawa e60969
          * first IDAT may need deflate header optimization.
fukasawa e60969
          */
fukasawa e60969
#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
fukasawa e60969
            if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 &&
fukasawa e60969
                png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
fukasawa e60969
               optimize_cmf(data, png_image_size(png_ptr));
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
         png_write_complete_chunk(png_ptr, png_IDAT, data, size);
fukasawa e60969
         png_ptr->mode |= PNG_HAVE_IDAT;
fukasawa e60969
fukasawa e60969
         png_ptr->zstream.next_out = data;
fukasawa e60969
         png_ptr->zstream.avail_out = size;
fukasawa e60969
fukasawa e60969
         /* For SYNC_FLUSH or FINISH it is essential to keep calling zlib with
fukasawa e60969
          * the same flush parameter until it has finished output, for NO_FLUSH
fukasawa e60969
          * it doesn't matter.
fukasawa e60969
          */
fukasawa e60969
         if (ret == Z_OK && flush != Z_NO_FLUSH)
fukasawa e60969
            continue;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* The order of these checks doesn't matter much; it just affects which
fukasawa e60969
       * possible error might be detected if multiple things go wrong at once.
fukasawa e60969
       */
fukasawa e60969
      if (ret == Z_OK) /* most likely return code! */
fukasawa e60969
      {
fukasawa e60969
         /* If all the input has been consumed then just return.  If Z_FINISH
fukasawa e60969
          * was used as the flush parameter something has gone wrong if we get
fukasawa e60969
          * here.
fukasawa e60969
          */
fukasawa e60969
         if (input_len == 0)
fukasawa e60969
         {
fukasawa e60969
            if (flush == Z_FINISH)
fukasawa e60969
               png_error(png_ptr, "Z_OK on Z_FINISH with output space");
fukasawa e60969
fukasawa e60969
            return;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (ret == Z_STREAM_END && flush == Z_FINISH)
fukasawa e60969
      {
fukasawa e60969
         /* This is the end of the IDAT data; any pending output must be
fukasawa e60969
          * flushed.  For small PNG files we may still be at the beginning.
fukasawa e60969
          */
fukasawa e60969
         png_bytep data = png_ptr->zbuffer_list->output;
fukasawa e60969
         uInt size = png_ptr->zbuffer_size - png_ptr->zstream.avail_out;
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
fukasawa e60969
         if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 &&
fukasawa e60969
             png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
fukasawa e60969
            optimize_cmf(data, png_image_size(png_ptr));
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
         png_write_complete_chunk(png_ptr, png_IDAT, data, size);
fukasawa e60969
         png_ptr->zstream.avail_out = 0;
fukasawa e60969
         png_ptr->zstream.next_out = NULL;
fukasawa e60969
         png_ptr->mode |= PNG_HAVE_IDAT | PNG_AFTER_IDAT;
fukasawa e60969
fukasawa e60969
         png_ptr->zowner = 0; /* Release the stream */
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         /* This is an error condition. */
fukasawa e60969
         png_zstream_error(png_ptr, ret);
fukasawa e60969
         png_error(png_ptr, png_ptr->zstream.msg);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Write an IEND chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_IEND(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_write_IEND");
fukasawa e60969
fukasawa e60969
   png_write_complete_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
fukasawa e60969
   png_ptr->mode |= PNG_HAVE_IEND;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_gAMA_SUPPORTED
fukasawa e60969
/* Write a gAMA chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_gAMA_fixed(png_structrp png_ptr, png_fixed_point file_gamma)
fukasawa e60969
{
fukasawa e60969
   png_byte buf[4];
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_gAMA");
fukasawa e60969
fukasawa e60969
   /* file_gamma is saved in 1/100,000ths */
fukasawa e60969
   png_save_uint_32(buf, (png_uint_32)file_gamma);
fukasawa e60969
   png_write_complete_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_sRGB_SUPPORTED
fukasawa e60969
/* Write a sRGB chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_sRGB(png_structrp png_ptr, int srgb_intent)
fukasawa e60969
{
fukasawa e60969
   png_byte buf[1];
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_sRGB");
fukasawa e60969
fukasawa e60969
   if (srgb_intent >= PNG_sRGB_INTENT_LAST)
fukasawa e60969
      png_warning(png_ptr,
fukasawa e60969
          "Invalid sRGB rendering intent specified");
fukasawa e60969
fukasawa e60969
   buf[0]=(png_byte)srgb_intent;
fukasawa e60969
   png_write_complete_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_iCCP_SUPPORTED
fukasawa e60969
/* Write an iCCP chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_iCCP(png_structrp png_ptr, png_const_charp name,
fukasawa e60969
    png_const_bytep profile)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 name_len;
fukasawa e60969
   png_uint_32 profile_len;
fukasawa e60969
   png_byte new_name[81]; /* 1 byte for the compression byte */
fukasawa e60969
   compression_state comp;
fukasawa e60969
   png_uint_32 temp;
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_iCCP");
fukasawa e60969
fukasawa e60969
   /* These are all internal problems: the profile should have been checked
fukasawa e60969
    * before when it was stored.
fukasawa e60969
    */
fukasawa e60969
   if (profile == NULL)
fukasawa e60969
      png_error(png_ptr, "No profile for iCCP chunk"); /* internal error */
fukasawa e60969
fukasawa e60969
   profile_len = png_get_uint_32(profile);
fukasawa e60969
fukasawa e60969
   if (profile_len < 132)
fukasawa e60969
      png_error(png_ptr, "ICC profile too short");
fukasawa e60969
fukasawa e60969
   temp = (png_uint_32) (*(profile+8));
fukasawa e60969
   if (temp > 3 && (profile_len & 0x03))
fukasawa e60969
      png_error(png_ptr, "ICC profile length invalid (not a multiple of 4)");
fukasawa e60969
fukasawa e60969
   {
fukasawa e60969
      png_uint_32 embedded_profile_len = png_get_uint_32(profile);
fukasawa e60969
fukasawa e60969
      if (profile_len != embedded_profile_len)
fukasawa e60969
         png_error(png_ptr, "Profile length does not match profile");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   name_len = png_check_keyword(png_ptr, name, new_name);
fukasawa e60969
fukasawa e60969
   if (name_len == 0)
fukasawa e60969
      png_error(png_ptr, "iCCP: invalid keyword");
fukasawa e60969
fukasawa e60969
   new_name[++name_len] = PNG_COMPRESSION_TYPE_BASE;
fukasawa e60969
fukasawa e60969
   /* Make sure we include the NULL after the name and the compression type */
fukasawa e60969
   ++name_len;
fukasawa e60969
fukasawa e60969
   png_text_compress_init(&comp, profile, profile_len);
fukasawa e60969
fukasawa e60969
   /* Allow for keyword terminator and compression byte */
fukasawa e60969
   if (png_text_compress(png_ptr, png_iCCP, &comp, name_len) != Z_OK)
fukasawa e60969
      png_error(png_ptr, png_ptr->zstream.msg);
fukasawa e60969
fukasawa e60969
   png_write_chunk_header(png_ptr, png_iCCP, name_len + comp.output_len);
fukasawa e60969
fukasawa e60969
   png_write_chunk_data(png_ptr, new_name, name_len);
fukasawa e60969
fukasawa e60969
   png_write_compressed_data_out(png_ptr, &comp);
fukasawa e60969
fukasawa e60969
   png_write_chunk_end(png_ptr);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_sPLT_SUPPORTED
fukasawa e60969
/* Write a sPLT chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_sPLT(png_structrp png_ptr, png_const_sPLT_tp spalette)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 name_len;
fukasawa e60969
   png_byte new_name[80];
fukasawa e60969
   png_byte entrybuf[10];
fukasawa e60969
   png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
fukasawa e60969
   png_size_t palette_size = entry_size * spalette->nentries;
fukasawa e60969
   png_sPLT_entryp ep;
fukasawa e60969
#ifndef PNG_POINTER_INDEXING_SUPPORTED
fukasawa e60969
   int i;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_sPLT");
fukasawa e60969
fukasawa e60969
   name_len = png_check_keyword(png_ptr, spalette->name, new_name);
fukasawa e60969
fukasawa e60969
   if (name_len == 0)
fukasawa e60969
      png_error(png_ptr, "sPLT: invalid keyword");
fukasawa e60969
fukasawa e60969
   /* Make sure we include the NULL after the name */
fukasawa e60969
   png_write_chunk_header(png_ptr, png_sPLT,
fukasawa e60969
       (png_uint_32)(name_len + 2 + palette_size));
fukasawa e60969
fukasawa e60969
   png_write_chunk_data(png_ptr, (png_bytep)new_name,
fukasawa e60969
       (png_size_t)(name_len + 1));
fukasawa e60969
fukasawa e60969
   png_write_chunk_data(png_ptr, &spalette->depth, (png_size_t)1);
fukasawa e60969
fukasawa e60969
   /* Loop through each palette entry, writing appropriately */
fukasawa e60969
#ifdef PNG_POINTER_INDEXING_SUPPORTED
fukasawa e60969
   for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)</spalette->
fukasawa e60969
   {
fukasawa e60969
      if (spalette->depth == 8)
fukasawa e60969
      {
fukasawa e60969
         entrybuf[0] = (png_byte)ep->red;
fukasawa e60969
         entrybuf[1] = (png_byte)ep->green;
fukasawa e60969
         entrybuf[2] = (png_byte)ep->blue;
fukasawa e60969
         entrybuf[3] = (png_byte)ep->alpha;
fukasawa e60969
         png_save_uint_16(entrybuf + 4, ep->frequency);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         png_save_uint_16(entrybuf + 0, ep->red);
fukasawa e60969
         png_save_uint_16(entrybuf + 2, ep->green);
fukasawa e60969
         png_save_uint_16(entrybuf + 4, ep->blue);
fukasawa e60969
         png_save_uint_16(entrybuf + 6, ep->alpha);
fukasawa e60969
         png_save_uint_16(entrybuf + 8, ep->frequency);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      png_write_chunk_data(png_ptr, entrybuf, entry_size);
fukasawa e60969
   }
fukasawa e60969
#else
fukasawa e60969
   ep=spalette->entries;
fukasawa e60969
   for (i = 0; i>spalette->nentries; i++)
fukasawa e60969
   {
fukasawa e60969
      if (spalette->depth == 8)
fukasawa e60969
      {
fukasawa e60969
         entrybuf[0] = (png_byte)ep[i].red;
fukasawa e60969
         entrybuf[1] = (png_byte)ep[i].green;
fukasawa e60969
         entrybuf[2] = (png_byte)ep[i].blue;
fukasawa e60969
         entrybuf[3] = (png_byte)ep[i].alpha;
fukasawa e60969
         png_save_uint_16(entrybuf + 4, ep[i].frequency);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         png_save_uint_16(entrybuf + 0, ep[i].red);
fukasawa e60969
         png_save_uint_16(entrybuf + 2, ep[i].green);
fukasawa e60969
         png_save_uint_16(entrybuf + 4, ep[i].blue);
fukasawa e60969
         png_save_uint_16(entrybuf + 6, ep[i].alpha);
fukasawa e60969
         png_save_uint_16(entrybuf + 8, ep[i].frequency);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      png_write_chunk_data(png_ptr, entrybuf, entry_size);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   png_write_chunk_end(png_ptr);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_sBIT_SUPPORTED
fukasawa e60969
/* Write the sBIT chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_sBIT(png_structrp png_ptr, png_const_color_8p sbit, int color_type)
fukasawa e60969
{
fukasawa e60969
   png_byte buf[4];
fukasawa e60969
   png_size_t size;
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_sBIT");
fukasawa e60969
fukasawa e60969
   /* Make sure we don't depend upon the order of PNG_COLOR_8 */
fukasawa e60969
   if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
fukasawa e60969
   {
fukasawa e60969
      png_byte maxbits;
fukasawa e60969
fukasawa e60969
      maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
fukasawa e60969
          png_ptr->usr_bit_depth);
fukasawa e60969
fukasawa e60969
      if (sbit->red == 0 || sbit->red > maxbits ||
fukasawa e60969
          sbit->green == 0 || sbit->green > maxbits ||
fukasawa e60969
          sbit->blue == 0 || sbit->blue > maxbits)
fukasawa e60969
      {
fukasawa e60969
         png_warning(png_ptr, "Invalid sBIT depth specified");
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      buf[0] = sbit->red;
fukasawa e60969
      buf[1] = sbit->green;
fukasawa e60969
      buf[2] = sbit->blue;
fukasawa e60969
      size = 3;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
fukasawa e60969
      {
fukasawa e60969
         png_warning(png_ptr, "Invalid sBIT depth specified");
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      buf[0] = sbit->gray;
fukasawa e60969
      size = 1;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if ((color_type & PNG_COLOR_MASK_ALPHA) != 0)
fukasawa e60969
   {
fukasawa e60969
      if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
fukasawa e60969
      {
fukasawa e60969
         png_warning(png_ptr, "Invalid sBIT depth specified");
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      buf[size++] = sbit->alpha;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_write_complete_chunk(png_ptr, png_sBIT, buf, size);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_cHRM_SUPPORTED
fukasawa e60969
/* Write the cHRM chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_cHRM_fixed(png_structrp png_ptr, const png_xy *xy)
fukasawa e60969
{
fukasawa e60969
   png_byte buf[32];
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_cHRM");
fukasawa e60969
fukasawa e60969
   /* Each value is saved in 1/100,000ths */
fukasawa e60969
   png_save_int_32(buf,      xy->whitex);
fukasawa e60969
   png_save_int_32(buf +  4, xy->whitey);
fukasawa e60969
fukasawa e60969
   png_save_int_32(buf +  8, xy->redx);
fukasawa e60969
   png_save_int_32(buf + 12, xy->redy);
fukasawa e60969
fukasawa e60969
   png_save_int_32(buf + 16, xy->greenx);
fukasawa e60969
   png_save_int_32(buf + 20, xy->greeny);
fukasawa e60969
fukasawa e60969
   png_save_int_32(buf + 24, xy->bluex);
fukasawa e60969
   png_save_int_32(buf + 28, xy->bluey);
fukasawa e60969
fukasawa e60969
   png_write_complete_chunk(png_ptr, png_cHRM, buf, 32);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_tRNS_SUPPORTED
fukasawa e60969
/* Write the tRNS chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_tRNS(png_structrp png_ptr, png_const_bytep trans_alpha,
fukasawa e60969
    png_const_color_16p tran, int num_trans, int color_type)
fukasawa e60969
{
fukasawa e60969
   png_byte buf[6];
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_tRNS");
fukasawa e60969
fukasawa e60969
   if (color_type == PNG_COLOR_TYPE_PALETTE)
fukasawa e60969
   {
fukasawa e60969
      if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
fukasawa e60969
      {
fukasawa e60969
         png_app_warning(png_ptr,
fukasawa e60969
             "Invalid number of transparent colors specified");
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Write the chunk out as it is */
fukasawa e60969
      png_write_complete_chunk(png_ptr, png_tRNS, trans_alpha,
fukasawa e60969
         (png_size_t)num_trans);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (color_type == PNG_COLOR_TYPE_GRAY)
fukasawa e60969
   {
fukasawa e60969
      /* One 16-bit value */
fukasawa e60969
      if (tran->gray >= (1 << png_ptr->bit_depth))
fukasawa e60969
      {
fukasawa e60969
         png_app_warning(png_ptr,
fukasawa e60969
             "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
fukasawa e60969
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      png_save_uint_16(buf, tran->gray);
fukasawa e60969
      png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (color_type == PNG_COLOR_TYPE_RGB)
fukasawa e60969
   {
fukasawa e60969
      /* Three 16-bit values */
fukasawa e60969
      png_save_uint_16(buf, tran->red);
fukasawa e60969
      png_save_uint_16(buf + 2, tran->green);
fukasawa e60969
      png_save_uint_16(buf + 4, tran->blue);
fukasawa e60969
#ifdef PNG_WRITE_16BIT_SUPPORTED
fukasawa e60969
      if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0)
fukasawa e60969
#else
fukasawa e60969
      if ((buf[0] | buf[2] | buf[4]) != 0)
fukasawa e60969
#endif
fukasawa e60969
      {
fukasawa e60969
         png_app_warning(png_ptr,
fukasawa e60969
           "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      png_app_warning(png_ptr, "Can't write tRNS with an alpha channel");
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_bKGD_SUPPORTED
fukasawa e60969
/* Write the background chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_bKGD(png_structrp png_ptr, png_const_color_16p back, int color_type)
fukasawa e60969
{
fukasawa e60969
   png_byte buf[6];
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_bKGD");
fukasawa e60969
fukasawa e60969
   if (color_type == PNG_COLOR_TYPE_PALETTE)
fukasawa e60969
   {
fukasawa e60969
      if (
fukasawa e60969
#ifdef PNG_MNG_FEATURES_SUPPORTED
fukasawa e60969
          (png_ptr->num_palette != 0 ||
fukasawa e60969
          (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0) &&
fukasawa e60969
#endif
fukasawa e60969
         back->index >= png_ptr->num_palette)
fukasawa e60969
      {
fukasawa e60969
         png_warning(png_ptr, "Invalid background palette index");
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      buf[0] = back->index;
fukasawa e60969
      png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
fukasawa e60969
   {
fukasawa e60969
      png_save_uint_16(buf, back->red);
fukasawa e60969
      png_save_uint_16(buf + 2, back->green);
fukasawa e60969
      png_save_uint_16(buf + 4, back->blue);
fukasawa e60969
#ifdef PNG_WRITE_16BIT_SUPPORTED
fukasawa e60969
      if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0)
fukasawa e60969
#else
fukasawa e60969
      if ((buf[0] | buf[2] | buf[4]) != 0)
fukasawa e60969
#endif
fukasawa e60969
      {
fukasawa e60969
         png_warning(png_ptr,
fukasawa e60969
             "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
fukasawa e60969
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      if (back->gray >= (1 << png_ptr->bit_depth))
fukasawa e60969
      {
fukasawa e60969
         png_warning(png_ptr,
fukasawa e60969
             "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
fukasawa e60969
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      png_save_uint_16(buf, back->gray);
fukasawa e60969
      png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_hIST_SUPPORTED
fukasawa e60969
/* Write the histogram */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_hIST(png_structrp png_ptr, png_const_uint_16p hist, int num_hist)
fukasawa e60969
{
fukasawa e60969
   int i;
fukasawa e60969
   png_byte buf[3];
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_hIST");
fukasawa e60969
fukasawa e60969
   if (num_hist > (int)png_ptr->num_palette)
fukasawa e60969
   {
fukasawa e60969
      png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
fukasawa e60969
          png_ptr->num_palette);
fukasawa e60969
fukasawa e60969
      png_warning(png_ptr, "Invalid number of histogram entries specified");
fukasawa e60969
      return;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_write_chunk_header(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
fukasawa e60969
fukasawa e60969
   for (i = 0; i < num_hist; i++)
fukasawa e60969
   {
fukasawa e60969
      png_save_uint_16(buf, hist[i]);
fukasawa e60969
      png_write_chunk_data(png_ptr, buf, (png_size_t)2);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_write_chunk_end(png_ptr);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_tEXt_SUPPORTED
fukasawa e60969
/* Write a tEXt chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_tEXt(png_structrp png_ptr, png_const_charp key, png_const_charp text,
fukasawa e60969
    png_size_t text_len)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 key_len;
fukasawa e60969
   png_byte new_key[80];
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_tEXt");
fukasawa e60969
fukasawa e60969
   key_len = png_check_keyword(png_ptr, key, new_key);
fukasawa e60969
fukasawa e60969
   if (key_len == 0)
fukasawa e60969
      png_error(png_ptr, "tEXt: invalid keyword");
fukasawa e60969
fukasawa e60969
   if (text == NULL || *text == '\0')
fukasawa e60969
      text_len = 0;
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      text_len = strlen(text);
fukasawa e60969
fukasawa e60969
   if (text_len > PNG_UINT_31_MAX - (key_len+1))
fukasawa e60969
      png_error(png_ptr, "tEXt: text too long");
fukasawa e60969
fukasawa e60969
   /* Make sure we include the 0 after the key */
fukasawa e60969
   png_write_chunk_header(png_ptr, png_tEXt,
fukasawa e60969
       (png_uint_32)/*checked above*/(key_len + text_len + 1));
fukasawa e60969
   /*
fukasawa e60969
    * We leave it to the application to meet PNG-1.0 requirements on the
fukasawa e60969
    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of
fukasawa e60969
    * any non-Latin-1 characters except for NEWLINE.  ISO PNG will forbid them.
fukasawa e60969
    * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
fukasawa e60969
    */
fukasawa e60969
   png_write_chunk_data(png_ptr, new_key, key_len + 1);
fukasawa e60969
fukasawa e60969
   if (text_len != 0)
fukasawa e60969
      png_write_chunk_data(png_ptr, (png_const_bytep)text, text_len);
fukasawa e60969
fukasawa e60969
   png_write_chunk_end(png_ptr);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_zTXt_SUPPORTED
fukasawa e60969
/* Write a compressed text chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_zTXt(png_structrp png_ptr, png_const_charp key, png_const_charp text,
fukasawa e60969
    int compression)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 key_len;
fukasawa e60969
   png_byte new_key[81];
fukasawa e60969
   compression_state comp;
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_zTXt");
fukasawa e60969
fukasawa e60969
   if (compression == PNG_TEXT_COMPRESSION_NONE)
fukasawa e60969
   {
fukasawa e60969
      png_write_tEXt(png_ptr, key, text, 0);
fukasawa e60969
      return;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (compression != PNG_TEXT_COMPRESSION_zTXt)
fukasawa e60969
      png_error(png_ptr, "zTXt: invalid compression type");
fukasawa e60969
fukasawa e60969
   key_len = png_check_keyword(png_ptr, key, new_key);
fukasawa e60969
fukasawa e60969
   if (key_len == 0)
fukasawa e60969
      png_error(png_ptr, "zTXt: invalid keyword");
fukasawa e60969
fukasawa e60969
   /* Add the compression method and 1 for the keyword separator. */
fukasawa e60969
   new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE;
fukasawa e60969
   ++key_len;
fukasawa e60969
fukasawa e60969
   /* Compute the compressed data; do it now for the length */
fukasawa e60969
   png_text_compress_init(&comp, (png_const_bytep)text,
fukasawa e60969
      text == NULL ? 0 : strlen(text));
fukasawa e60969
fukasawa e60969
   if (png_text_compress(png_ptr, png_zTXt, &comp, key_len) != Z_OK)
fukasawa e60969
      png_error(png_ptr, png_ptr->zstream.msg);
fukasawa e60969
fukasawa e60969
   /* Write start of chunk */
fukasawa e60969
   png_write_chunk_header(png_ptr, png_zTXt, key_len + comp.output_len);
fukasawa e60969
fukasawa e60969
   /* Write key */
fukasawa e60969
   png_write_chunk_data(png_ptr, new_key, key_len);
fukasawa e60969
fukasawa e60969
   /* Write the compressed data */
fukasawa e60969
   png_write_compressed_data_out(png_ptr, &comp);
fukasawa e60969
fukasawa e60969
   /* Close the chunk */
fukasawa e60969
   png_write_chunk_end(png_ptr);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_iTXt_SUPPORTED
fukasawa e60969
/* Write an iTXt chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_iTXt(png_structrp png_ptr, int compression, png_const_charp key,
fukasawa e60969
    png_const_charp lang, png_const_charp lang_key, png_const_charp text)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 key_len, prefix_len;
fukasawa e60969
   png_size_t lang_len, lang_key_len;
fukasawa e60969
   png_byte new_key[82];
fukasawa e60969
   compression_state comp;
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_iTXt");
fukasawa e60969
fukasawa e60969
   key_len = png_check_keyword(png_ptr, key, new_key);
fukasawa e60969
fukasawa e60969
   if (key_len == 0)
fukasawa e60969
      png_error(png_ptr, "iTXt: invalid keyword");
fukasawa e60969
fukasawa e60969
   /* Set the compression flag */
fukasawa e60969
   switch (compression)
fukasawa e60969
   {
fukasawa e60969
      case PNG_ITXT_COMPRESSION_NONE:
fukasawa e60969
      case PNG_TEXT_COMPRESSION_NONE:
fukasawa e60969
         compression = new_key[++key_len] = 0; /* no compression */
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case PNG_TEXT_COMPRESSION_zTXt:
fukasawa e60969
      case PNG_ITXT_COMPRESSION_zTXt:
fukasawa e60969
         compression = new_key[++key_len] = 1; /* compressed */
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      default:
fukasawa e60969
         png_error(png_ptr, "iTXt: invalid compression");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE;
fukasawa e60969
   ++key_len; /* for the keywod separator */
fukasawa e60969
fukasawa e60969
   /* We leave it to the application to meet PNG-1.0 requirements on the
fukasawa e60969
    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of
fukasawa e60969
    * any non-Latin-1 characters except for NEWLINE.  ISO PNG, however,
fukasawa e60969
    * specifies that the text is UTF-8 and this really doesn't require any
fukasawa e60969
    * checking.
fukasawa e60969
    *
fukasawa e60969
    * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
fukasawa e60969
    *
fukasawa e60969
    * TODO: validate the language tag correctly (see the spec.)
fukasawa e60969
    */
fukasawa e60969
   if (lang == NULL) lang = ""; /* empty language is valid */
fukasawa e60969
   lang_len = strlen(lang)+1;
fukasawa e60969
   if (lang_key == NULL) lang_key = ""; /* may be empty */
fukasawa e60969
   lang_key_len = strlen(lang_key)+1;
fukasawa e60969
   if (text == NULL) text = ""; /* may be empty */
fukasawa e60969
fukasawa e60969
   prefix_len = key_len;
fukasawa e60969
   if (lang_len > PNG_UINT_31_MAX-prefix_len)
fukasawa e60969
      prefix_len = PNG_UINT_31_MAX;
fukasawa e60969
   else
fukasawa e60969
      prefix_len = (png_uint_32)(prefix_len + lang_len);
fukasawa e60969
fukasawa e60969
   if (lang_key_len > PNG_UINT_31_MAX-prefix_len)
fukasawa e60969
      prefix_len = PNG_UINT_31_MAX;
fukasawa e60969
   else
fukasawa e60969
      prefix_len = (png_uint_32)(prefix_len + lang_key_len);
fukasawa e60969
fukasawa e60969
   png_text_compress_init(&comp, (png_const_bytep)text, strlen(text));
fukasawa e60969
fukasawa e60969
   if (compression != 0)
fukasawa e60969
   {
fukasawa e60969
      if (png_text_compress(png_ptr, png_iTXt, &comp, prefix_len) != Z_OK)
fukasawa e60969
         png_error(png_ptr, png_ptr->zstream.msg);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      if (comp.input_len > PNG_UINT_31_MAX-prefix_len)
fukasawa e60969
         png_error(png_ptr, "iTXt: uncompressed text too long");
fukasawa e60969
fukasawa e60969
      /* So the string will fit in a chunk: */
fukasawa e60969
      comp.output_len = (png_uint_32)/*SAFE*/comp.input_len;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_write_chunk_header(png_ptr, png_iTXt, comp.output_len + prefix_len);
fukasawa e60969
fukasawa e60969
   png_write_chunk_data(png_ptr, new_key, key_len);
fukasawa e60969
fukasawa e60969
   png_write_chunk_data(png_ptr, (png_const_bytep)lang, lang_len);
fukasawa e60969
fukasawa e60969
   png_write_chunk_data(png_ptr, (png_const_bytep)lang_key, lang_key_len);
fukasawa e60969
fukasawa e60969
   if (compression != 0)
fukasawa e60969
      png_write_compressed_data_out(png_ptr, &comp);
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      png_write_chunk_data(png_ptr, (png_const_bytep)text, comp.output_len);
fukasawa e60969
fukasawa e60969
   png_write_chunk_end(png_ptr);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_oFFs_SUPPORTED
fukasawa e60969
/* Write the oFFs chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_oFFs(png_structrp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
fukasawa e60969
    int unit_type)
fukasawa e60969
{
fukasawa e60969
   png_byte buf[9];
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_oFFs");
fukasawa e60969
fukasawa e60969
   if (unit_type >= PNG_OFFSET_LAST)
fukasawa e60969
      png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
fukasawa e60969
fukasawa e60969
   png_save_int_32(buf, x_offset);
fukasawa e60969
   png_save_int_32(buf + 4, y_offset);
fukasawa e60969
   buf[8] = (png_byte)unit_type;
fukasawa e60969
fukasawa e60969
   png_write_complete_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_pCAL_SUPPORTED
fukasawa e60969
/* Write the pCAL chunk (described in the PNG extensions document) */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_pCAL(png_structrp png_ptr, png_charp purpose, png_int_32 X0,
fukasawa e60969
    png_int_32 X1, int type, int nparams, png_const_charp units,
fukasawa e60969
    png_charpp params)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 purpose_len;
fukasawa e60969
   png_size_t units_len, total_len;
fukasawa e60969
   png_size_tp params_len;
fukasawa e60969
   png_byte buf[10];
fukasawa e60969
   png_byte new_purpose[80];
fukasawa e60969
   int i;
fukasawa e60969
fukasawa e60969
   png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
fukasawa e60969
fukasawa e60969
   if (type >= PNG_EQUATION_LAST)
fukasawa e60969
      png_error(png_ptr, "Unrecognized equation type for pCAL chunk");
fukasawa e60969
fukasawa e60969
   purpose_len = png_check_keyword(png_ptr, purpose, new_purpose);
fukasawa e60969
fukasawa e60969
   if (purpose_len == 0)
fukasawa e60969
      png_error(png_ptr, "pCAL: invalid keyword");
fukasawa e60969
fukasawa e60969
   ++purpose_len; /* terminator */
fukasawa e60969
fukasawa e60969
   png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
fukasawa e60969
   units_len = strlen(units) + (nparams == 0 ? 0 : 1);
fukasawa e60969
   png_debug1(3, "pCAL units length = %d", (int)units_len);
fukasawa e60969
   total_len = purpose_len + units_len + 10;
fukasawa e60969
fukasawa e60969
   params_len = (png_size_tp)png_malloc(png_ptr,
fukasawa e60969
       (png_alloc_size_t)(nparams * (sizeof (png_size_t))));
fukasawa e60969
fukasawa e60969
   /* Find the length of each parameter, making sure we don't count the
fukasawa e60969
    * null terminator for the last parameter.
fukasawa e60969
    */
fukasawa e60969
   for (i = 0; i < nparams; i++)
fukasawa e60969
   {
fukasawa e60969
      params_len[i] = strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
fukasawa e60969
      png_debug2(3, "pCAL parameter %d length = %lu", i,
fukasawa e60969
          (unsigned long)params_len[i]);
fukasawa e60969
      total_len += params_len[i];
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_debug1(3, "pCAL total length = %d", (int)total_len);
fukasawa e60969
   png_write_chunk_header(png_ptr, png_pCAL, (png_uint_32)total_len);
fukasawa e60969
   png_write_chunk_data(png_ptr, new_purpose, purpose_len);
fukasawa e60969
   png_save_int_32(buf, X0);
fukasawa e60969
   png_save_int_32(buf + 4, X1);
fukasawa e60969
   buf[8] = (png_byte)type;
fukasawa e60969
   buf[9] = (png_byte)nparams;
fukasawa e60969
   png_write_chunk_data(png_ptr, buf, (png_size_t)10);
fukasawa e60969
   png_write_chunk_data(png_ptr, (png_const_bytep)units, (png_size_t)units_len);
fukasawa e60969
fukasawa e60969
   for (i = 0; i < nparams; i++)
fukasawa e60969
   {
fukasawa e60969
      png_write_chunk_data(png_ptr, (png_const_bytep)params[i], params_len[i]);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_free(png_ptr, params_len);
fukasawa e60969
   png_write_chunk_end(png_ptr);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_sCAL_SUPPORTED
fukasawa e60969
/* Write the sCAL chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_sCAL_s(png_structrp png_ptr, int unit, png_const_charp width,
fukasawa e60969
    png_const_charp height)
fukasawa e60969
{
fukasawa e60969
   png_byte buf[64];
fukasawa e60969
   png_size_t wlen, hlen, total_len;
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_sCAL_s");
fukasawa e60969
fukasawa e60969
   wlen = strlen(width);
fukasawa e60969
   hlen = strlen(height);
fukasawa e60969
   total_len = wlen + hlen + 2;
fukasawa e60969
fukasawa e60969
   if (total_len > 64)
fukasawa e60969
   {
fukasawa e60969
      png_warning(png_ptr, "Can't write sCAL (buffer too small)");
fukasawa e60969
      return;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   buf[0] = (png_byte)unit;
fukasawa e60969
   memcpy(buf + 1, width, wlen + 1);      /* Append the '\0' here */
fukasawa e60969
   memcpy(buf + wlen + 2, height, hlen);  /* Do NOT append the '\0' here */
fukasawa e60969
fukasawa e60969
   png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
fukasawa e60969
   png_write_complete_chunk(png_ptr, png_sCAL, buf, total_len);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_pHYs_SUPPORTED
fukasawa e60969
/* Write the pHYs chunk */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_pHYs(png_structrp png_ptr, png_uint_32 x_pixels_per_unit,
fukasawa e60969
    png_uint_32 y_pixels_per_unit,
fukasawa e60969
    int unit_type)
fukasawa e60969
{
fukasawa e60969
   png_byte buf[9];
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_pHYs");
fukasawa e60969
fukasawa e60969
   if (unit_type >= PNG_RESOLUTION_LAST)
fukasawa e60969
      png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
fukasawa e60969
fukasawa e60969
   png_save_uint_32(buf, x_pixels_per_unit);
fukasawa e60969
   png_save_uint_32(buf + 4, y_pixels_per_unit);
fukasawa e60969
   buf[8] = (png_byte)unit_type;
fukasawa e60969
fukasawa e60969
   png_write_complete_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_tIME_SUPPORTED
fukasawa e60969
/* Write the tIME chunk.  Use either png_convert_from_struct_tm()
fukasawa e60969
 * or png_convert_from_time_t(), or fill in the structure yourself.
fukasawa e60969
 */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_tIME(png_structrp png_ptr, png_const_timep mod_time)
fukasawa e60969
{
fukasawa e60969
   png_byte buf[7];
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_tIME");
fukasawa e60969
fukasawa e60969
   if (mod_time->month  > 12 || mod_time->month  < 1 ||
fukasawa e60969
       mod_time->day    > 31 || mod_time->day    < 1 ||
fukasawa e60969
       mod_time->hour   > 23 || mod_time->second > 60)
fukasawa e60969
   {
fukasawa e60969
      png_warning(png_ptr, "Invalid time specified for tIME chunk");
fukasawa e60969
      return;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_save_uint_16(buf, mod_time->year);
fukasawa e60969
   buf[2] = mod_time->month;
fukasawa e60969
   buf[3] = mod_time->day;
fukasawa e60969
   buf[4] = mod_time->hour;
fukasawa e60969
   buf[5] = mod_time->minute;
fukasawa e60969
   buf[6] = mod_time->second;
fukasawa e60969
fukasawa e60969
   png_write_complete_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Initializes the row writing capability of libpng */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_start_row(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
fukasawa e60969
   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
fukasawa e60969
fukasawa e60969
   /* Start of interlace block */
fukasawa e60969
   static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
fukasawa e60969
fukasawa e60969
   /* Offset to next interlace block */
fukasawa e60969
   static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
fukasawa e60969
fukasawa e60969
   /* Start of interlace block in the y direction */
fukasawa e60969
   static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
fukasawa e60969
fukasawa e60969
   /* Offset to next interlace block in the y direction */
fukasawa e60969
   static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   png_alloc_size_t buf_size;
fukasawa e60969
   int usr_pixel_depth;
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_FILTER_SUPPORTED
fukasawa e60969
   png_byte filters;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_start_row");
fukasawa e60969
fukasawa e60969
   usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth;
fukasawa e60969
   buf_size = PNG_ROWBYTES(usr_pixel_depth, png_ptr->width) + 1;
fukasawa e60969
fukasawa e60969
   /* 1.5.6: added to allow checking in the row write code. */
fukasawa e60969
   png_ptr->transformed_pixel_depth = png_ptr->pixel_depth;
fukasawa e60969
   png_ptr->maximum_pixel_depth = (png_byte)usr_pixel_depth;
fukasawa e60969
fukasawa e60969
   /* Set up row buffer */
fukasawa e60969
   png_ptr->row_buf = png_voidcast(png_bytep, png_malloc(png_ptr, buf_size));
fukasawa e60969
fukasawa e60969
   png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_FILTER_SUPPORTED
fukasawa e60969
   filters = png_ptr->do_filter;
fukasawa e60969
fukasawa e60969
   if (png_ptr->height == 1)
fukasawa e60969
      filters &= 0xff & ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
fukasawa e60969
fukasawa e60969
   if (png_ptr->width == 1)
fukasawa e60969
      filters &= 0xff & ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH);
fukasawa e60969
fukasawa e60969
   if (filters == 0)
fukasawa e60969
      filters = PNG_FILTER_NONE;
fukasawa e60969
fukasawa e60969
   png_ptr->do_filter = filters;
fukasawa e60969
fukasawa e60969
   if (((filters & (PNG_FILTER_SUB | PNG_FILTER_UP | PNG_FILTER_AVG |
fukasawa e60969
       PNG_FILTER_PAETH)) != 0) && png_ptr->try_row == NULL)
fukasawa e60969
   {
fukasawa e60969
      int num_filters = 0;
fukasawa e60969
fukasawa e60969
      png_ptr->try_row = png_voidcast(png_bytep, png_malloc(png_ptr, buf_size));
fukasawa e60969
fukasawa e60969
      if (filters & PNG_FILTER_SUB)
fukasawa e60969
         num_filters++;
fukasawa e60969
fukasawa e60969
      if (filters & PNG_FILTER_UP)
fukasawa e60969
         num_filters++;
fukasawa e60969
fukasawa e60969
      if (filters & PNG_FILTER_AVG)
fukasawa e60969
         num_filters++;
fukasawa e60969
fukasawa e60969
      if (filters & PNG_FILTER_PAETH)
fukasawa e60969
         num_filters++;
fukasawa e60969
fukasawa e60969
      if (num_filters > 1)
fukasawa e60969
         png_ptr->tst_row = png_voidcast(png_bytep, png_malloc(png_ptr,
fukasawa e60969
             buf_size));
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* We only need to keep the previous row if we are using one of the following
fukasawa e60969
    * filters.
fukasawa e60969
    */
fukasawa e60969
   if ((filters & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) != 0)
fukasawa e60969
      png_ptr->prev_row = png_voidcast(png_bytep,
fukasawa e60969
         png_calloc(png_ptr, buf_size));
fukasawa e60969
#endif /* WRITE_FILTER */
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
fukasawa e60969
   /* If interlaced, we need to set up width and height of pass */
fukasawa e60969
   if (png_ptr->interlaced != 0)
fukasawa e60969
   {
fukasawa e60969
      if ((png_ptr->transformations & PNG_INTERLACE) == 0)
fukasawa e60969
      {
fukasawa e60969
         png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
fukasawa e60969
             png_pass_ystart[0]) / png_pass_yinc[0];
fukasawa e60969
fukasawa e60969
         png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
fukasawa e60969
             png_pass_start[0]) / png_pass_inc[0];
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         png_ptr->num_rows = png_ptr->height;
fukasawa e60969
         png_ptr->usr_width = png_ptr->width;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
#endif
fukasawa e60969
   {
fukasawa e60969
      png_ptr->num_rows = png_ptr->height;
fukasawa e60969
      png_ptr->usr_width = png_ptr->width;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Internal use only.  Called when finished processing a row of data. */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_finish_row(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
fukasawa e60969
   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
fukasawa e60969
fukasawa e60969
   /* Start of interlace block */
fukasawa e60969
   static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
fukasawa e60969
fukasawa e60969
   /* Offset to next interlace block */
fukasawa e60969
   static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
fukasawa e60969
fukasawa e60969
   /* Start of interlace block in the y direction */
fukasawa e60969
   static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
fukasawa e60969
fukasawa e60969
   /* Offset to next interlace block in the y direction */
fukasawa e60969
   static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_finish_row");
fukasawa e60969
fukasawa e60969
   /* Next row */
fukasawa e60969
   png_ptr->row_number++;
fukasawa e60969
fukasawa e60969
   /* See if we are done */
fukasawa e60969
   if (png_ptr->row_number < png_ptr->num_rows)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
fukasawa e60969
   /* If interlaced, go to next pass */
fukasawa e60969
   if (png_ptr->interlaced != 0)
fukasawa e60969
   {
fukasawa e60969
      png_ptr->row_number = 0;
fukasawa e60969
      if ((png_ptr->transformations & PNG_INTERLACE) != 0)
fukasawa e60969
      {
fukasawa e60969
         png_ptr->pass++;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         /* Loop until we find a non-zero width or height pass */
fukasawa e60969
         do
fukasawa e60969
         {
fukasawa e60969
            png_ptr->pass++;
fukasawa e60969
fukasawa e60969
            if (png_ptr->pass >= 7)
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            png_ptr->usr_width = (png_ptr->width +
fukasawa e60969
                png_pass_inc[png_ptr->pass] - 1 -
fukasawa e60969
                png_pass_start[png_ptr->pass]) /
fukasawa e60969
                png_pass_inc[png_ptr->pass];
fukasawa e60969
fukasawa e60969
            png_ptr->num_rows = (png_ptr->height +
fukasawa e60969
                png_pass_yinc[png_ptr->pass] - 1 -
fukasawa e60969
                png_pass_ystart[png_ptr->pass]) /
fukasawa e60969
                png_pass_yinc[png_ptr->pass];
fukasawa e60969
fukasawa e60969
            if ((png_ptr->transformations & PNG_INTERLACE) != 0)
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
         } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
fukasawa e60969
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Reset the row above the image for the next pass */
fukasawa e60969
      if (png_ptr->pass < 7)
fukasawa e60969
      {
fukasawa e60969
         if (png_ptr->prev_row != NULL)
fukasawa e60969
            memset(png_ptr->prev_row, 0,
fukasawa e60969
                (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
fukasawa e60969
                png_ptr->usr_bit_depth, png_ptr->width)) + 1);
fukasawa e60969
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* If we get here, we've just written the last row, so we need
fukasawa e60969
      to flush the compressor */
fukasawa e60969
   png_compress_IDAT(png_ptr, NULL, 0, Z_FINISH);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
fukasawa e60969
/* Pick out the correct pixels for the interlace pass.
fukasawa e60969
 * The basic idea here is to go through the row with a source
fukasawa e60969
 * pointer and a destination pointer (sp and dp), and copy the
fukasawa e60969
 * correct pixels for the pass.  As the row gets compacted,
fukasawa e60969
 * sp will always be >= dp, so we should never overwrite anything.
fukasawa e60969
 * See the default: case for the easiest code to understand.
fukasawa e60969
 */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
fukasawa e60969
{
fukasawa e60969
   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
fukasawa e60969
fukasawa e60969
   /* Start of interlace block */
fukasawa e60969
   static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
fukasawa e60969
fukasawa e60969
   /* Offset to next interlace block */
fukasawa e60969
   static PNG_CONST png_byte  png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_do_write_interlace");
fukasawa e60969
fukasawa e60969
   /* We don't have to do anything on the last pass (6) */
fukasawa e60969
   if (pass < 6)
fukasawa e60969
   {
fukasawa e60969
      /* Each pixel depth is handled separately */
fukasawa e60969
      switch (row_info->pixel_depth)
fukasawa e60969
      {
fukasawa e60969
         case 1:
fukasawa e60969
         {
fukasawa e60969
            png_bytep sp;
fukasawa e60969
            png_bytep dp;
fukasawa e60969
            unsigned int shift;
fukasawa e60969
            int d;
fukasawa e60969
            int value;
fukasawa e60969
            png_uint_32 i;
fukasawa e60969
            png_uint_32 row_width = row_info->width;
fukasawa e60969
fukasawa e60969
            dp = row;
fukasawa e60969
            d = 0;
fukasawa e60969
            shift = 7;
fukasawa e60969
fukasawa e60969
            for (i = png_pass_start[pass]; i < row_width;
fukasawa e60969
               i += png_pass_inc[pass])
fukasawa e60969
            {
fukasawa e60969
               sp = row + (png_size_t)(i >> 3);
fukasawa e60969
               value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
fukasawa e60969
               d |= (value << shift);
fukasawa e60969
fukasawa e60969
               if (shift == 0)
fukasawa e60969
               {
fukasawa e60969
                  shift = 7;
fukasawa e60969
                  *dp++ = (png_byte)d;
fukasawa e60969
                  d = 0;
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               else
fukasawa e60969
                  shift--;
fukasawa e60969
fukasawa e60969
            }
fukasawa e60969
            if (shift != 7)
fukasawa e60969
               *dp = (png_byte)d;
fukasawa e60969
fukasawa e60969
            break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         case 2:
fukasawa e60969
         {
fukasawa e60969
            png_bytep sp;
fukasawa e60969
            png_bytep dp;
fukasawa e60969
            unsigned int shift;
fukasawa e60969
            int d;
fukasawa e60969
            int value;
fukasawa e60969
            png_uint_32 i;
fukasawa e60969
            png_uint_32 row_width = row_info->width;
fukasawa e60969
fukasawa e60969
            dp = row;
fukasawa e60969
            shift = 6;
fukasawa e60969
            d = 0;
fukasawa e60969
fukasawa e60969
            for (i = png_pass_start[pass]; i < row_width;
fukasawa e60969
               i += png_pass_inc[pass])
fukasawa e60969
            {
fukasawa e60969
               sp = row + (png_size_t)(i >> 2);
fukasawa e60969
               value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
fukasawa e60969
               d |= (value << shift);
fukasawa e60969
fukasawa e60969
               if (shift == 0)
fukasawa e60969
               {
fukasawa e60969
                  shift = 6;
fukasawa e60969
                  *dp++ = (png_byte)d;
fukasawa e60969
                  d = 0;
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               else
fukasawa e60969
                  shift -= 2;
fukasawa e60969
            }
fukasawa e60969
            if (shift != 6)
fukasawa e60969
               *dp = (png_byte)d;
fukasawa e60969
fukasawa e60969
            break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         case 4:
fukasawa e60969
         {
fukasawa e60969
            png_bytep sp;
fukasawa e60969
            png_bytep dp;
fukasawa e60969
            unsigned int shift;
fukasawa e60969
            int d;
fukasawa e60969
            int value;
fukasawa e60969
            png_uint_32 i;
fukasawa e60969
            png_uint_32 row_width = row_info->width;
fukasawa e60969
fukasawa e60969
            dp = row;
fukasawa e60969
            shift = 4;
fukasawa e60969
            d = 0;
fukasawa e60969
            for (i = png_pass_start[pass]; i < row_width;
fukasawa e60969
                i += png_pass_inc[pass])
fukasawa e60969
            {
fukasawa e60969
               sp = row + (png_size_t)(i >> 1);
fukasawa e60969
               value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
fukasawa e60969
               d |= (value << shift);
fukasawa e60969
fukasawa e60969
               if (shift == 0)
fukasawa e60969
               {
fukasawa e60969
                  shift = 4;
fukasawa e60969
                  *dp++ = (png_byte)d;
fukasawa e60969
                  d = 0;
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               else
fukasawa e60969
                  shift -= 4;
fukasawa e60969
            }
fukasawa e60969
            if (shift != 4)
fukasawa e60969
               *dp = (png_byte)d;
fukasawa e60969
fukasawa e60969
            break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         default:
fukasawa e60969
         {
fukasawa e60969
            png_bytep sp;
fukasawa e60969
            png_bytep dp;
fukasawa e60969
            png_uint_32 i;
fukasawa e60969
            png_uint_32 row_width = row_info->width;
fukasawa e60969
            png_size_t pixel_bytes;
fukasawa e60969
fukasawa e60969
            /* Start at the beginning */
fukasawa e60969
            dp = row;
fukasawa e60969
fukasawa e60969
            /* Find out how many bytes each pixel takes up */
fukasawa e60969
            pixel_bytes = (row_info->pixel_depth >> 3);
fukasawa e60969
fukasawa e60969
            /* Loop through the row, only looking at the pixels that matter */
fukasawa e60969
            for (i = png_pass_start[pass]; i < row_width;
fukasawa e60969
               i += png_pass_inc[pass])
fukasawa e60969
            {
fukasawa e60969
               /* Find out where the original pixel is */
fukasawa e60969
               sp = row + (png_size_t)i * pixel_bytes;
fukasawa e60969
fukasawa e60969
               /* Move the pixel */
fukasawa e60969
               if (dp != sp)
fukasawa e60969
                  memcpy(dp, sp, pixel_bytes);
fukasawa e60969
fukasawa e60969
               /* Next pixel */
fukasawa e60969
               dp += pixel_bytes;
fukasawa e60969
            }
fukasawa e60969
            break;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
      /* Set new row width */
fukasawa e60969
      row_info->width = (row_info->width +
fukasawa e60969
          png_pass_inc[pass] - 1 -
fukasawa e60969
          png_pass_start[pass]) /
fukasawa e60969
          png_pass_inc[pass];
fukasawa e60969
fukasawa e60969
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
fukasawa e60969
          row_info->width);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
fukasawa e60969
/* This filters the row, chooses which filter to use, if it has not already
fukasawa e60969
 * been specified by the application, and then writes the row out with the
fukasawa e60969
 * chosen filter.
fukasawa e60969
 */
fukasawa e60969
static void /* PRIVATE */
fukasawa e60969
png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row,
fukasawa e60969
   png_size_t row_bytes);
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_FILTER_SUPPORTED
fukasawa e60969
static png_size_t /* PRIVATE */
fukasawa e60969
png_setup_sub_row(png_structrp png_ptr, const png_uint_32 bpp,
fukasawa e60969
    const png_size_t row_bytes, const png_size_t lmins)
fukasawa e60969
{
fukasawa e60969
   png_bytep rp, dp, lp;
fukasawa e60969
   png_size_t i;
fukasawa e60969
   png_size_t sum = 0;
fukasawa e60969
   int v;
fukasawa e60969
fukasawa e60969
   png_ptr->try_row[0] = PNG_FILTER_VALUE_SUB;
fukasawa e60969
fukasawa e60969
   for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1; i < bpp;
fukasawa e60969
        i++, rp++, dp++)
fukasawa e60969
   {
fukasawa e60969
      v = *dp = *rp;
fukasawa e60969
      sum += (v < 128) ? v : 256 - v;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   for (lp = png_ptr->row_buf + 1; i < row_bytes;
fukasawa e60969
      i++, rp++, lp++, dp++)
fukasawa e60969
   {
fukasawa e60969
      v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
fukasawa e60969
      sum += (v < 128) ? v : 256 - v;
fukasawa e60969
fukasawa e60969
      if (sum > lmins)  /* We are already worse, don't continue. */
fukasawa e60969
        break;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return (sum);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static png_size_t /* PRIVATE */
fukasawa e60969
png_setup_up_row(png_structrp png_ptr, const png_size_t row_bytes,
fukasawa e60969
    const png_size_t lmins)
fukasawa e60969
{
fukasawa e60969
   png_bytep rp, dp, pp;
fukasawa e60969
   png_size_t i;
fukasawa e60969
   png_size_t sum = 0;
fukasawa e60969
   int v;
fukasawa e60969
fukasawa e60969
   png_ptr->try_row[0] = PNG_FILTER_VALUE_UP;
fukasawa e60969
fukasawa e60969
   for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
fukasawa e60969
       pp = png_ptr->prev_row + 1; i < row_bytes;
fukasawa e60969
       i++, rp++, pp++, dp++)
fukasawa e60969
   {
fukasawa e60969
      v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
fukasawa e60969
      sum += (v < 128) ? v : 256 - v;
fukasawa e60969
fukasawa e60969
      if (sum > lmins)  /* We are already worse, don't continue. */
fukasawa e60969
        break;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return (sum);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static png_size_t /* PRIVATE */
fukasawa e60969
png_setup_avg_row(png_structrp png_ptr, const png_uint_32 bpp,
fukasawa e60969
      const png_size_t row_bytes, const png_size_t lmins)
fukasawa e60969
{
fukasawa e60969
   png_bytep rp, dp, pp, lp;
fukasawa e60969
   png_uint_32 i;
fukasawa e60969
   png_size_t sum = 0;
fukasawa e60969
   int v;
fukasawa e60969
fukasawa e60969
   png_ptr->try_row[0] = PNG_FILTER_VALUE_AVG;
fukasawa e60969
fukasawa e60969
   for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
fukasawa e60969
        pp = png_ptr->prev_row + 1; i < bpp; i++)
fukasawa e60969
   {
fukasawa e60969
      v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
fukasawa e60969
fukasawa e60969
      sum += (v < 128) ? v : 256 - v;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   for (lp = png_ptr->row_buf + 1; i < row_bytes; i++)
fukasawa e60969
   {
fukasawa e60969
      v = *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
fukasawa e60969
          & 0xff);
fukasawa e60969
fukasawa e60969
      sum += (v < 128) ? v : 256 - v;
fukasawa e60969
fukasawa e60969
      if (sum > lmins)  /* We are already worse, don't continue. */
fukasawa e60969
        break;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return (sum);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static png_size_t /* PRIVATE */
fukasawa e60969
png_setup_paeth_row(png_structrp png_ptr, const png_uint_32 bpp,
fukasawa e60969
    const png_size_t row_bytes, const png_size_t lmins)
fukasawa e60969
{
fukasawa e60969
   png_bytep rp, dp, pp, cp, lp;
fukasawa e60969
   png_size_t i;
fukasawa e60969
   png_size_t sum = 0;
fukasawa e60969
   int v;
fukasawa e60969
fukasawa e60969
   png_ptr->try_row[0] = PNG_FILTER_VALUE_PAETH;
fukasawa e60969
fukasawa e60969
   for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
fukasawa e60969
       pp = png_ptr->prev_row + 1; i < bpp; i++)
fukasawa e60969
   {
fukasawa e60969
      v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
fukasawa e60969
fukasawa e60969
      sum += (v < 128) ? v : 256 - v;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   for (lp = png_ptr->row_buf + 1, cp = png_ptr->prev_row + 1; i < row_bytes;
fukasawa e60969
        i++)
fukasawa e60969
   {
fukasawa e60969
      int a, b, c, pa, pb, pc, p;
fukasawa e60969
fukasawa e60969
      b = *pp++;
fukasawa e60969
      c = *cp++;
fukasawa e60969
      a = *lp++;
fukasawa e60969
fukasawa e60969
      p = b - c;
fukasawa e60969
      pc = a - c;
fukasawa e60969
fukasawa e60969
#ifdef PNG_USE_ABS
fukasawa e60969
      pa = abs(p);
fukasawa e60969
      pb = abs(pc);
fukasawa e60969
      pc = abs(p + pc);
fukasawa e60969
#else
fukasawa e60969
      pa = p < 0 ? -p : p;
fukasawa e60969
      pb = pc < 0 ? -pc : pc;
fukasawa e60969
      pc = (p + pc) < 0 ? -(p + pc) : p + pc;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
      p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
fukasawa e60969
fukasawa e60969
      v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
fukasawa e60969
fukasawa e60969
      sum += (v < 128) ? v : 256 - v;
fukasawa e60969
fukasawa e60969
      if (sum > lmins)  /* We are already worse, don't continue. */
fukasawa e60969
        break;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return (sum);
fukasawa e60969
}
fukasawa e60969
#endif /* WRITE_FILTER */
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
fukasawa e60969
{
fukasawa e60969
#ifndef PNG_WRITE_FILTER_SUPPORTED
fukasawa e60969
   png_write_filtered_row(png_ptr, png_ptr->row_buf, row_info->rowbytes+1);
fukasawa e60969
#else
fukasawa e60969
   png_byte filter_to_do = png_ptr->do_filter;
fukasawa e60969
   png_bytep row_buf;
fukasawa e60969
   png_bytep best_row;
fukasawa e60969
   png_uint_32 bpp;
fukasawa e60969
   png_size_t mins;
fukasawa e60969
   png_size_t row_bytes = row_info->rowbytes;
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_find_filter");
fukasawa e60969
fukasawa e60969
   /* Find out how many bytes offset each pixel is */
fukasawa e60969
   bpp = (row_info->pixel_depth + 7) >> 3;
fukasawa e60969
fukasawa e60969
   row_buf = png_ptr->row_buf;
fukasawa e60969
   mins = PNG_SIZE_MAX - 256/* so we can detect potential overflow of the
fukasawa e60969
                               running sum */;
fukasawa e60969
fukasawa e60969
   /* The prediction method we use is to find which method provides the
fukasawa e60969
    * smallest value when summing the absolute values of the distances
fukasawa e60969
    * from zero, using anything >= 128 as negative numbers.  This is known
fukasawa e60969
    * as the "minimum sum of absolute differences" heuristic.  Other
fukasawa e60969
    * heuristics are the "weighted minimum sum of absolute differences"
fukasawa e60969
    * (experimental and can in theory improve compression), and the "zlib
fukasawa e60969
    * predictive" method (not implemented yet), which does test compressions
fukasawa e60969
    * of lines using different filter methods, and then chooses the
fukasawa e60969
    * (series of) filter(s) that give minimum compressed data size (VERY
fukasawa e60969
    * computationally expensive).
fukasawa e60969
    *
fukasawa e60969
    * GRR 980525:  consider also
fukasawa e60969
    *
fukasawa e60969
    *   (1) minimum sum of absolute differences from running average (i.e.,
fukasawa e60969
    *       keep running sum of non-absolute differences & count of bytes)
fukasawa e60969
    *       [track dispersion, too?  restart average if dispersion too large?]
fukasawa e60969
    *
fukasawa e60969
    *  (1b) minimum sum of absolute differences from sliding average, probably
fukasawa e60969
    *       with window size <= deflate window (usually 32K)
fukasawa e60969
    *
fukasawa e60969
    *   (2) minimum sum of squared differences from zero or running average
fukasawa e60969
    *       (i.e., ~ root-mean-square approach)
fukasawa e60969
    */
fukasawa e60969
fukasawa e60969
fukasawa e60969
   /* We don't need to test the 'no filter' case if this is the only filter
fukasawa e60969
    * that has been chosen, as it doesn't actually do anything to the data.
fukasawa e60969
    */
fukasawa e60969
   best_row = png_ptr->row_buf;
fukasawa e60969
fukasawa e60969
fukasawa e60969
   if ((filter_to_do & PNG_FILTER_NONE) != 0 && filter_to_do != PNG_FILTER_NONE)
fukasawa e60969
   {
fukasawa e60969
      png_bytep rp;
fukasawa e60969
      png_size_t sum = 0;
fukasawa e60969
      png_size_t i;
fukasawa e60969
      int v;
fukasawa e60969
fukasawa e60969
      if (PNG_SIZE_MAX/128 <= row_bytes)
fukasawa e60969
      {
fukasawa e60969
         for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
fukasawa e60969
         {
fukasawa e60969
            /* Check for overflow */
fukasawa e60969
            if (sum > PNG_SIZE_MAX/128 - 256)
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            v = *rp;
fukasawa e60969
            sum += (v < 128) ? v : 256 - v;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
      else /* Overflow is not possible */
fukasawa e60969
      {
fukasawa e60969
         for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
fukasawa e60969
         {
fukasawa e60969
            v = *rp;
fukasawa e60969
            sum += (v < 128) ? v : 256 - v;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      mins = sum;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Sub filter */
fukasawa e60969
   if (filter_to_do == PNG_FILTER_SUB)
fukasawa e60969
   /* It's the only filter so no testing is needed */
fukasawa e60969
   {
fukasawa e60969
      (void) png_setup_sub_row(png_ptr, bpp, row_bytes, mins);
fukasawa e60969
      best_row = png_ptr->try_row;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if ((filter_to_do & PNG_FILTER_SUB) != 0)
fukasawa e60969
   {
fukasawa e60969
      png_size_t sum;
fukasawa e60969
      png_size_t lmins = mins;
fukasawa e60969
fukasawa e60969
      sum = png_setup_sub_row(png_ptr, bpp, row_bytes, lmins);
fukasawa e60969
fukasawa e60969
      if (sum < mins)
fukasawa e60969
      {
fukasawa e60969
         mins = sum;
fukasawa e60969
         best_row = png_ptr->try_row;
fukasawa e60969
         if (png_ptr->tst_row != NULL)
fukasawa e60969
         {
fukasawa e60969
            png_ptr->try_row = png_ptr->tst_row;
fukasawa e60969
            png_ptr->tst_row = best_row;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Up filter */
fukasawa e60969
   if (filter_to_do == PNG_FILTER_UP)
fukasawa e60969
   {
fukasawa e60969
      (void) png_setup_up_row(png_ptr, row_bytes, mins);
fukasawa e60969
      best_row = png_ptr->try_row;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if ((filter_to_do & PNG_FILTER_UP) != 0)
fukasawa e60969
   {
fukasawa e60969
      png_size_t sum;
fukasawa e60969
      png_size_t lmins = mins;
fukasawa e60969
fukasawa e60969
      sum = png_setup_up_row(png_ptr, row_bytes, lmins);
fukasawa e60969
fukasawa e60969
      if (sum < mins)
fukasawa e60969
      {
fukasawa e60969
         mins = sum;
fukasawa e60969
         best_row = png_ptr->try_row;
fukasawa e60969
         if (png_ptr->tst_row != NULL)
fukasawa e60969
         {
fukasawa e60969
            png_ptr->try_row = png_ptr->tst_row;
fukasawa e60969
            png_ptr->tst_row = best_row;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Avg filter */
fukasawa e60969
   if (filter_to_do == PNG_FILTER_AVG)
fukasawa e60969
   {
fukasawa e60969
      (void) png_setup_avg_row(png_ptr, bpp, row_bytes, mins);
fukasawa e60969
      best_row = png_ptr->try_row;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if ((filter_to_do & PNG_FILTER_AVG) != 0)
fukasawa e60969
   {
fukasawa e60969
      png_size_t sum;
fukasawa e60969
      png_size_t lmins = mins;
fukasawa e60969
fukasawa e60969
      sum= png_setup_avg_row(png_ptr, bpp, row_bytes, lmins);
fukasawa e60969
fukasawa e60969
      if (sum < mins)
fukasawa e60969
      {
fukasawa e60969
         mins = sum;
fukasawa e60969
         best_row = png_ptr->try_row;
fukasawa e60969
         if (png_ptr->tst_row != NULL)
fukasawa e60969
         {
fukasawa e60969
            png_ptr->try_row = png_ptr->tst_row;
fukasawa e60969
            png_ptr->tst_row = best_row;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Paeth filter */
fukasawa e60969
   if ((filter_to_do == PNG_FILTER_PAETH) != 0)
fukasawa e60969
   {
fukasawa e60969
      (void) png_setup_paeth_row(png_ptr, bpp, row_bytes, mins);
fukasawa e60969
      best_row = png_ptr->try_row;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if ((filter_to_do & PNG_FILTER_PAETH) != 0)
fukasawa e60969
   {
fukasawa e60969
      png_size_t sum;
fukasawa e60969
      png_size_t lmins = mins;
fukasawa e60969
fukasawa e60969
      sum = png_setup_paeth_row(png_ptr, bpp, row_bytes, lmins);
fukasawa e60969
fukasawa e60969
      if (sum < mins)
fukasawa e60969
      {
fukasawa e60969
         best_row = png_ptr->try_row;
fukasawa e60969
         if (png_ptr->tst_row != NULL)
fukasawa e60969
         {
fukasawa e60969
            png_ptr->try_row = png_ptr->tst_row;
fukasawa e60969
            png_ptr->tst_row = best_row;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Do the actual writing of the filtered row data from the chosen filter. */
fukasawa e60969
   png_write_filtered_row(png_ptr, best_row, row_info->rowbytes+1);
fukasawa e60969
fukasawa e60969
#endif /* WRITE_FILTER */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
fukasawa e60969
/* Do the actual writing of a previously filtered row. */
fukasawa e60969
static void
fukasawa e60969
png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row,
fukasawa e60969
   png_size_t full_row_length/*includes filter byte*/)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_write_filtered_row");
fukasawa e60969
fukasawa e60969
   png_debug1(2, "filter = %d", filtered_row[0]);
fukasawa e60969
fukasawa e60969
   png_compress_IDAT(png_ptr, filtered_row, full_row_length, Z_NO_FLUSH);
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_FILTER_SUPPORTED
fukasawa e60969
   /* Swap the current and previous rows */
fukasawa e60969
   if (png_ptr->prev_row != NULL)
fukasawa e60969
   {
fukasawa e60969
      png_bytep tptr;
fukasawa e60969
fukasawa e60969
      tptr = png_ptr->prev_row;
fukasawa e60969
      png_ptr->prev_row = png_ptr->row_buf;
fukasawa e60969
      png_ptr->row_buf = tptr;
fukasawa e60969
   }
fukasawa e60969
#endif /* WRITE_FILTER */
fukasawa e60969
fukasawa e60969
   /* Finish row - updates counters and flushes zlib if last row */
fukasawa e60969
   png_write_finish_row(png_ptr);
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_FLUSH_SUPPORTED
fukasawa e60969
   png_ptr->flush_rows++;
fukasawa e60969
fukasawa e60969
   if (png_ptr->flush_dist > 0 &&
fukasawa e60969
       png_ptr->flush_rows >= png_ptr->flush_dist)
fukasawa e60969
   {
fukasawa e60969
      png_write_flush(png_ptr);
fukasawa e60969
   }
fukasawa e60969
#endif /* WRITE_FLUSH */
fukasawa e60969
}
fukasawa e60969
#endif /* WRITE */