fukasawa e60969
fukasawa e60969
/* pngwrite.c - general routines to write a PNG file
fukasawa e60969
 *
fukasawa e60969
 * Last changed in libpng 1.6.19 [November 12, 2015]
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
#if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
fukasawa e60969
#  include <errno.h></errno.h>
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_SUPPORTED
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
/* Write out all the unknown chunks for the current given location */
fukasawa e60969
static void
fukasawa e60969
write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr,
fukasawa e60969
   unsigned int where)
fukasawa e60969
{
fukasawa e60969
   if (info_ptr->unknown_chunks_num != 0)
fukasawa e60969
   {
fukasawa e60969
      png_const_unknown_chunkp up;
fukasawa e60969
fukasawa e60969
      png_debug(5, "writing extra chunks");
fukasawa e60969
fukasawa e60969
      for (up = info_ptr->unknown_chunks;
fukasawa e60969
           up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
fukasawa e60969
           ++up)
fukasawa e60969
         if ((up->location & where) != 0)
fukasawa e60969
      {
fukasawa e60969
         /* If per-chunk unknown chunk handling is enabled use it, otherwise
fukasawa e60969
          * just write the chunks the application has set.
fukasawa e60969
          */
fukasawa e60969
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
         int keep = png_handle_as_unknown(png_ptr, up->name);
fukasawa e60969
fukasawa e60969
         /* NOTE: this code is radically different from the read side in the
fukasawa e60969
          * matter of handling an ancillary unknown chunk.  In the read side
fukasawa e60969
          * the default behavior is to discard it, in the code below the default
fukasawa e60969
          * behavior is to write it.  Critical chunks are, however, only
fukasawa e60969
          * written if explicitly listed or if the default is set to write all
fukasawa e60969
          * unknown chunks.
fukasawa e60969
          *
fukasawa e60969
          * The default handling is also slightly weird - it is not possible to
fukasawa e60969
          * stop the writing of all unsafe-to-copy chunks!
fukasawa e60969
          *
fukasawa e60969
          * TODO: REVIEW: this would seem to be a bug.
fukasawa e60969
          */
fukasawa e60969
         if (keep != PNG_HANDLE_CHUNK_NEVER &&
fukasawa e60969
             ((up->name[3] & 0x20) /* safe-to-copy overrides everything */ ||
fukasawa e60969
              keep == PNG_HANDLE_CHUNK_ALWAYS ||
fukasawa e60969
              (keep == PNG_HANDLE_CHUNK_AS_DEFAULT &&
fukasawa e60969
               png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS)))
fukasawa e60969
#endif
fukasawa e60969
         {
fukasawa e60969
            /* TODO: review, what is wrong with a zero length unknown chunk? */
fukasawa e60969
            if (up->size == 0)
fukasawa e60969
               png_warning(png_ptr, "Writing zero-length unknown chunk");
fukasawa e60969
fukasawa e60969
            png_write_chunk(png_ptr, up->name, up->data, up->size);
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif /* WRITE_UNKNOWN_CHUNKS */
fukasawa e60969
fukasawa e60969
/* Writes all the PNG information.  This is the suggested way to use the
fukasawa e60969
 * library.  If you have a new chunk to add, make a function to write it,
fukasawa e60969
 * and put it in the correct location here.  If you want the chunk written
fukasawa e60969
 * after the image data, put it in png_write_end().  I strongly encourage
fukasawa e60969
 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
fukasawa e60969
 * the chunk, as that will keep the code from breaking if you want to just
fukasawa e60969
 * write a plain PNG file.  If you have long comments, I suggest writing
fukasawa e60969
 * them in png_write_end(), and compressing them.
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_write_info_before_PLTE");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL || info_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
fukasawa e60969
   {
fukasawa e60969
      /* Write PNG signature */
fukasawa e60969
      png_write_sig(png_ptr);
fukasawa e60969
fukasawa e60969
#ifdef PNG_MNG_FEATURES_SUPPORTED
fukasawa e60969
      if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 && \
fukasawa e60969
          png_ptr->mng_features_permitted != 0)
fukasawa e60969
      {
fukasawa e60969
         png_warning(png_ptr,
fukasawa e60969
             "MNG features are not allowed in a PNG datastream");
fukasawa e60969
         png_ptr->mng_features_permitted = 0;
fukasawa e60969
      }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
      /* Write IHDR information. */
fukasawa e60969
      png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
fukasawa e60969
          info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
fukasawa e60969
          info_ptr->filter_type,
fukasawa e60969
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
fukasawa e60969
          info_ptr->interlace_type
fukasawa e60969
#else
fukasawa e60969
          0
fukasawa e60969
#endif
fukasawa e60969
         );
fukasawa e60969
fukasawa e60969
      /* The rest of these check to see if the valid field has the appropriate
fukasawa e60969
       * flag set, and if it does, writes the chunk.
fukasawa e60969
       *
fukasawa e60969
       * 1.6.0: COLORSPACE support controls the writing of these chunks too, and
fukasawa e60969
       * the chunks will be written if the WRITE routine is there and
fukasawa e60969
       * information * is available in the COLORSPACE. (See
fukasawa e60969
       * png_colorspace_sync_info in png.c for where the valid flags get set.)
fukasawa e60969
       *
fukasawa e60969
       * Under certain circumstances the colorspace can be invalidated without
fukasawa e60969
       * syncing the info_struct 'valid' flags; this happens if libpng detects
fukasawa e60969
       * an error and calls png_error while the color space is being set, yet
fukasawa e60969
       * the application continues writing the PNG.  So check the 'invalid'
fukasawa e60969
       * flag here too.
fukasawa e60969
       */
fukasawa e60969
#ifdef PNG_GAMMA_SUPPORTED
fukasawa e60969
#  ifdef PNG_WRITE_gAMA_SUPPORTED
fukasawa e60969
      if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
fukasawa e60969
          (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) != 0 &&
fukasawa e60969
          (info_ptr->valid & PNG_INFO_gAMA) != 0)
fukasawa e60969
         png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma);
fukasawa e60969
#  endif
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_COLORSPACE_SUPPORTED
fukasawa e60969
      /* Write only one of sRGB or an ICC profile.  If a profile was supplied
fukasawa e60969
       * and it matches one of the known sRGB ones issue a warning.
fukasawa e60969
       */
fukasawa e60969
#  ifdef PNG_WRITE_iCCP_SUPPORTED
fukasawa e60969
         if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
fukasawa e60969
             (info_ptr->valid & PNG_INFO_iCCP) != 0)
fukasawa e60969
         {
fukasawa e60969
#    ifdef PNG_WRITE_sRGB_SUPPORTED
fukasawa e60969
               if ((info_ptr->valid & PNG_INFO_sRGB) != 0)
fukasawa e60969
                  png_app_warning(png_ptr,
fukasawa e60969
                     "profile matches sRGB but writing iCCP instead");
fukasawa e60969
#     endif
fukasawa e60969
fukasawa e60969
            png_write_iCCP(png_ptr, info_ptr->iccp_name,
fukasawa e60969
               info_ptr->iccp_profile);
fukasawa e60969
         }
fukasawa e60969
#     ifdef PNG_WRITE_sRGB_SUPPORTED
fukasawa e60969
         else
fukasawa e60969
#     endif
fukasawa e60969
#  endif
fukasawa e60969
fukasawa e60969
#  ifdef PNG_WRITE_sRGB_SUPPORTED
fukasawa e60969
         if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
fukasawa e60969
             (info_ptr->valid & PNG_INFO_sRGB) != 0)
fukasawa e60969
            png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent);
fukasawa e60969
#  endif /* WRITE_sRGB */
fukasawa e60969
#endif /* COLORSPACE */
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_sBIT_SUPPORTED
fukasawa e60969
         if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
fukasawa e60969
            png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_COLORSPACE_SUPPORTED
fukasawa e60969
#  ifdef PNG_WRITE_cHRM_SUPPORTED
fukasawa e60969
         if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
fukasawa e60969
             (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0 &&
fukasawa e60969
             (info_ptr->valid & PNG_INFO_cHRM) != 0)
fukasawa e60969
            png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy);
fukasawa e60969
#  endif
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
         write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
      png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void PNGAPI
fukasawa e60969
png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
fukasawa e60969
{
fukasawa e60969
#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
fukasawa e60969
   int i;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_info");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL || info_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_write_info_before_PLTE(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
   if ((info_ptr->valid & PNG_INFO_PLTE) != 0)
fukasawa e60969
      png_write_PLTE(png_ptr, info_ptr->palette,
fukasawa e60969
          (png_uint_32)info_ptr->num_palette);
fukasawa e60969
fukasawa e60969
   else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
fukasawa e60969
      png_error(png_ptr, "Valid palette required for paletted images");
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_tRNS_SUPPORTED
fukasawa e60969
   if ((info_ptr->valid & PNG_INFO_tRNS) !=0)
fukasawa e60969
   {
fukasawa e60969
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
fukasawa e60969
      /* Invert the alpha channel (in tRNS) */
fukasawa e60969
      if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0 &&
fukasawa e60969
          info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
fukasawa e60969
      {
fukasawa e60969
         int j, jend;
fukasawa e60969
fukasawa e60969
         jend = info_ptr->num_trans;
fukasawa e60969
         if (jend > PNG_MAX_PALETTE_LENGTH)
fukasawa e60969
            jend = PNG_MAX_PALETTE_LENGTH;
fukasawa e60969
fukasawa e60969
         for (j = 0; j
fukasawa e60969
            info_ptr->trans_alpha[j] =
fukasawa e60969
               (png_byte)(255 - info_ptr->trans_alpha[j]);
fukasawa e60969
      }
fukasawa e60969
#endif
fukasawa e60969
      png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
fukasawa e60969
          info_ptr->num_trans, info_ptr->color_type);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_bKGD_SUPPORTED
fukasawa e60969
   if ((info_ptr->valid & PNG_INFO_bKGD) != 0)
fukasawa e60969
      png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_hIST_SUPPORTED
fukasawa e60969
   if ((info_ptr->valid & PNG_INFO_hIST) != 0)
fukasawa e60969
      png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_oFFs_SUPPORTED
fukasawa e60969
   if ((info_ptr->valid & PNG_INFO_oFFs) != 0)
fukasawa e60969
      png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
fukasawa e60969
          info_ptr->offset_unit_type);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_pCAL_SUPPORTED
fukasawa e60969
   if ((info_ptr->valid & PNG_INFO_pCAL) != 0)
fukasawa e60969
      png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
fukasawa e60969
          info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
fukasawa e60969
          info_ptr->pcal_units, info_ptr->pcal_params);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_sCAL_SUPPORTED
fukasawa e60969
   if ((info_ptr->valid & PNG_INFO_sCAL) != 0)
fukasawa e60969
      png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
fukasawa e60969
          info_ptr->scal_s_width, info_ptr->scal_s_height);
fukasawa e60969
#endif /* sCAL */
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_pHYs_SUPPORTED
fukasawa e60969
   if ((info_ptr->valid & PNG_INFO_pHYs) != 0)
fukasawa e60969
      png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
fukasawa e60969
          info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
fukasawa e60969
#endif /* pHYs */
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_tIME_SUPPORTED
fukasawa e60969
   if ((info_ptr->valid & PNG_INFO_tIME) != 0)
fukasawa e60969
   {
fukasawa e60969
      png_write_tIME(png_ptr, &(info_ptr->mod_time));
fukasawa e60969
      png_ptr->mode |= PNG_WROTE_tIME;
fukasawa e60969
   }
fukasawa e60969
#endif /* tIME */
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_sPLT_SUPPORTED
fukasawa e60969
   if ((info_ptr->valid & PNG_INFO_sPLT) != 0)
fukasawa e60969
      for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
fukasawa e60969
         png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
fukasawa e60969
#endif /* sPLT */
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_TEXT_SUPPORTED
fukasawa e60969
   /* Check to see if we need to write text chunks */
fukasawa e60969
   for (i = 0; i < info_ptr->num_text; i++)
fukasawa e60969
   {
fukasawa e60969
      png_debug2(2, "Writing header text chunk %d, type %d", i,
fukasawa e60969
          info_ptr->text[i].compression);
fukasawa e60969
      /* An internationalized chunk? */
fukasawa e60969
      if (info_ptr->text[i].compression > 0)
fukasawa e60969
      {
fukasawa e60969
#ifdef PNG_WRITE_iTXt_SUPPORTED
fukasawa e60969
         /* Write international chunk */
fukasawa e60969
         png_write_iTXt(png_ptr,
fukasawa e60969
             info_ptr->text[i].compression,
fukasawa e60969
             info_ptr->text[i].key,
fukasawa e60969
             info_ptr->text[i].lang,
fukasawa e60969
             info_ptr->text[i].lang_key,
fukasawa e60969
             info_ptr->text[i].text);
fukasawa e60969
         /* Mark this chunk as written */
fukasawa e60969
         if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
fukasawa e60969
            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
fukasawa e60969
         else
fukasawa e60969
            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
fukasawa e60969
#else
fukasawa e60969
         png_warning(png_ptr, "Unable to write international text");
fukasawa e60969
#endif
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* If we want a compressed text chunk */
fukasawa e60969
      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
fukasawa e60969
      {
fukasawa e60969
#ifdef PNG_WRITE_zTXt_SUPPORTED
fukasawa e60969
         /* Write compressed chunk */
fukasawa e60969
         png_write_zTXt(png_ptr, info_ptr->text[i].key,
fukasawa e60969
             info_ptr->text[i].text, info_ptr->text[i].compression);
fukasawa e60969
         /* Mark this chunk as written */
fukasawa e60969
         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
fukasawa e60969
#else
fukasawa e60969
         png_warning(png_ptr, "Unable to write compressed text");
fukasawa e60969
#endif
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
fukasawa e60969
      {
fukasawa e60969
#ifdef PNG_WRITE_tEXt_SUPPORTED
fukasawa e60969
         /* Write uncompressed chunk */
fukasawa e60969
         png_write_tEXt(png_ptr, info_ptr->text[i].key,
fukasawa e60969
             info_ptr->text[i].text,
fukasawa e60969
             0);
fukasawa e60969
         /* Mark this chunk as written */
fukasawa e60969
         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
fukasawa e60969
#else
fukasawa e60969
         /* Can't get here */
fukasawa e60969
         png_warning(png_ptr, "Unable to write uncompressed text");
fukasawa e60969
#endif
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif /* tEXt */
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
   write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE);
fukasawa e60969
#endif
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Writes the end of the PNG file.  If you don't want to write comments or
fukasawa e60969
 * time information, you can pass NULL for info.  If you already wrote these
fukasawa e60969
 * in png_write_info(), do not write them again here.  If you have long
fukasawa e60969
 * comments, I suggest writing them here, and compressing them.
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_write_end(png_structrp png_ptr, png_inforp info_ptr)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_write_end");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
fukasawa e60969
      png_error(png_ptr, "No IDATs written into file");
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
fukasawa e60969
   if (png_ptr->num_palette_max > png_ptr->num_palette)
fukasawa e60969
      png_benign_error(png_ptr, "Wrote palette index exceeding num_palette");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* See if user wants us to write information chunks */
fukasawa e60969
   if (info_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
#ifdef PNG_WRITE_TEXT_SUPPORTED
fukasawa e60969
      int i; /* local index variable */
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_tIME_SUPPORTED
fukasawa e60969
      /* Check to see if user has supplied a time chunk */
fukasawa e60969
      if ((info_ptr->valid & PNG_INFO_tIME) != 0 &&
fukasawa e60969
          (png_ptr->mode & PNG_WROTE_tIME) == 0)
fukasawa e60969
         png_write_tIME(png_ptr, &(info_ptr->mod_time));
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_TEXT_SUPPORTED
fukasawa e60969
      /* Loop through comment chunks */
fukasawa e60969
      for (i = 0; i < info_ptr->num_text; i++)
fukasawa e60969
      {
fukasawa e60969
         png_debug2(2, "Writing trailer text chunk %d, type %d", i,
fukasawa e60969
            info_ptr->text[i].compression);
fukasawa e60969
         /* An internationalized chunk? */
fukasawa e60969
         if (info_ptr->text[i].compression > 0)
fukasawa e60969
         {
fukasawa e60969
#ifdef PNG_WRITE_iTXt_SUPPORTED
fukasawa e60969
            /* Write international chunk */
fukasawa e60969
            png_write_iTXt(png_ptr,
fukasawa e60969
                info_ptr->text[i].compression,
fukasawa e60969
                info_ptr->text[i].key,
fukasawa e60969
                info_ptr->text[i].lang,
fukasawa e60969
                info_ptr->text[i].lang_key,
fukasawa e60969
                info_ptr->text[i].text);
fukasawa e60969
            /* Mark this chunk as written */
fukasawa e60969
            if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
fukasawa e60969
               info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
fukasawa e60969
            else
fukasawa e60969
               info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
fukasawa e60969
#else
fukasawa e60969
            png_warning(png_ptr, "Unable to write international text");
fukasawa e60969
#endif
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
fukasawa e60969
         {
fukasawa e60969
#ifdef PNG_WRITE_zTXt_SUPPORTED
fukasawa e60969
            /* Write compressed chunk */
fukasawa e60969
            png_write_zTXt(png_ptr, info_ptr->text[i].key,
fukasawa e60969
                info_ptr->text[i].text, info_ptr->text[i].compression);
fukasawa e60969
            /* Mark this chunk as written */
fukasawa e60969
            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
fukasawa e60969
#else
fukasawa e60969
            png_warning(png_ptr, "Unable to write compressed text");
fukasawa e60969
#endif
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
fukasawa e60969
         {
fukasawa e60969
#ifdef PNG_WRITE_tEXt_SUPPORTED
fukasawa e60969
            /* Write uncompressed chunk */
fukasawa e60969
            png_write_tEXt(png_ptr, info_ptr->text[i].key,
fukasawa e60969
                info_ptr->text[i].text, 0);
fukasawa e60969
            /* Mark this chunk as written */
fukasawa e60969
            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
fukasawa e60969
#else
fukasawa e60969
            png_warning(png_ptr, "Unable to write uncompressed text");
fukasawa e60969
#endif
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
      write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT);
fukasawa e60969
#endif
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_ptr->mode |= PNG_AFTER_IDAT;
fukasawa e60969
fukasawa e60969
   /* Write end of PNG file */
fukasawa e60969
   png_write_IEND(png_ptr);
fukasawa e60969
fukasawa e60969
   /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
fukasawa e60969
    * and restored again in libpng-1.2.30, may cause some applications that
fukasawa e60969
    * do not set png_ptr->output_flush_fn to crash.  If your application
fukasawa e60969
    * experiences a problem, please try building libpng with
fukasawa e60969
    * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
fukasawa e60969
    * png-mng-implement at lists.sf.net .
fukasawa e60969
    */
fukasawa e60969
#ifdef PNG_WRITE_FLUSH_SUPPORTED
fukasawa e60969
#  ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
fukasawa e60969
   png_flush(png_ptr);
fukasawa e60969
#  endif
fukasawa e60969
#endif
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_CONVERT_tIME_SUPPORTED
fukasawa e60969
void PNGAPI
fukasawa e60969
png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm * ttime)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_convert_from_struct_tm");
fukasawa e60969
fukasawa e60969
   ptime->year = (png_uint_16)(1900 + ttime->tm_year);
fukasawa e60969
   ptime->month = (png_byte)(ttime->tm_mon + 1);
fukasawa e60969
   ptime->day = (png_byte)ttime->tm_mday;
fukasawa e60969
   ptime->hour = (png_byte)ttime->tm_hour;
fukasawa e60969
   ptime->minute = (png_byte)ttime->tm_min;
fukasawa e60969
   ptime->second = (png_byte)ttime->tm_sec;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void PNGAPI
fukasawa e60969
png_convert_from_time_t(png_timep ptime, time_t ttime)
fukasawa e60969
{
fukasawa e60969
   struct tm *tbuf;
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_convert_from_time_t");
fukasawa e60969
fukasawa e60969
   tbuf = gmtime(&ttime);
fukasawa e60969
   png_convert_from_struct_tm(ptime, tbuf);
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Initialize png_ptr structure, and allocate any memory needed */
fukasawa e60969
PNG_FUNCTION(png_structp,PNGAPI
fukasawa e60969
png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
fukasawa e60969
    png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
fukasawa e60969
{
fukasawa e60969
#ifndef PNG_USER_MEM_SUPPORTED
fukasawa e60969
   png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
fukasawa e60969
       error_fn, warn_fn, NULL, NULL, NULL);
fukasawa e60969
#else
fukasawa e60969
   return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
fukasawa e60969
       warn_fn, NULL, NULL, NULL);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Alternate initialize png_ptr structure, and allocate any memory needed */
fukasawa e60969
PNG_FUNCTION(png_structp,PNGAPI
fukasawa e60969
png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
fukasawa e60969
    png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
fukasawa e60969
    png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
fukasawa e60969
{
fukasawa e60969
   png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
fukasawa e60969
       error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
fukasawa e60969
#endif /* USER_MEM */
fukasawa e60969
   if (png_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
      /* Set the zlib control values to defaults; they can be overridden by the
fukasawa e60969
       * application after the struct has been created.
fukasawa e60969
       */
fukasawa e60969
      png_ptr->zbuffer_size = PNG_ZBUF_SIZE;
fukasawa e60969
fukasawa e60969
      /* The 'zlib_strategy' setting is irrelevant because png_default_claim in
fukasawa e60969
       * pngwutil.c defaults it according to whether or not filters will be
fukasawa e60969
       * used, and ignores this setting.
fukasawa e60969
       */
fukasawa e60969
      png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY;
fukasawa e60969
      png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION;
fukasawa e60969
      png_ptr->zlib_mem_level = 8;
fukasawa e60969
      png_ptr->zlib_window_bits = 15;
fukasawa e60969
      png_ptr->zlib_method = 8;
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
fukasawa e60969
      png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY;
fukasawa e60969
      png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION;
fukasawa e60969
      png_ptr->zlib_text_mem_level = 8;
fukasawa e60969
      png_ptr->zlib_text_window_bits = 15;
fukasawa e60969
      png_ptr->zlib_text_method = 8;
fukasawa e60969
#endif /* WRITE_COMPRESSED_TEXT */
fukasawa e60969
fukasawa e60969
      /* This is a highly dubious configuration option; by default it is off,
fukasawa e60969
       * but it may be appropriate for private builds that are testing
fukasawa e60969
       * extensions not conformant to the current specification, or of
fukasawa e60969
       * applications that must not fail to write at all costs!
fukasawa e60969
       */
fukasawa e60969
#ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED
fukasawa e60969
      /* In stable builds only warn if an application error can be completely
fukasawa e60969
       * handled.
fukasawa e60969
       */
fukasawa e60969
      png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
      /* App warnings are warnings in release (or release candidate) builds but
fukasawa e60969
       * are errors during development.
fukasawa e60969
       */
fukasawa e60969
#if PNG_RELEASE_BUILD
fukasawa e60969
      png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
      /* TODO: delay this, it can be done in png_init_io() (if the app doesn't
fukasawa e60969
       * do it itself) avoiding setting the default function if it is not
fukasawa e60969
       * required.
fukasawa e60969
       */
fukasawa e60969
      png_set_write_fn(png_ptr, NULL, NULL, NULL);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return png_ptr;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
fukasawa e60969
/* Write a few rows of image data.  If the image is interlaced,
fukasawa e60969
 * either you will have to write the 7 sub images, or, if you
fukasawa e60969
 * have called png_set_interlace_handling(), you will have to
fukasawa e60969
 * "write" the image seven times.
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_write_rows(png_structrp png_ptr, png_bytepp row,
fukasawa e60969
    png_uint_32 num_rows)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 i; /* row counter */
fukasawa e60969
   png_bytepp rp; /* row pointer */
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_rows");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   /* Loop through the rows */
fukasawa e60969
   for (i = 0, rp = row; i < num_rows; i++, rp++)
fukasawa e60969
   {
fukasawa e60969
      png_write_row(png_ptr, *rp);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Write the image.  You only need to call this function once, even
fukasawa e60969
 * if you are writing an interlaced image.
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_write_image(png_structrp png_ptr, png_bytepp image)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 i; /* row index */
fukasawa e60969
   int pass, num_pass; /* pass variables */
fukasawa e60969
   png_bytepp rp; /* points to current row */
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_debug(1, "in png_write_image");
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
fukasawa e60969
   /* Initialize interlace handling.  If image is not interlaced,
fukasawa e60969
    * this will set pass to 1
fukasawa e60969
    */
fukasawa e60969
   num_pass = png_set_interlace_handling(png_ptr);
fukasawa e60969
#else
fukasawa e60969
   num_pass = 1;
fukasawa e60969
#endif
fukasawa e60969
   /* Loop through passes */
fukasawa e60969
   for (pass = 0; pass < num_pass; pass++)
fukasawa e60969
   {
fukasawa e60969
      /* Loop through image */
fukasawa e60969
      for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
fukasawa e60969
      {
fukasawa e60969
         png_write_row(png_ptr, *rp);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_MNG_FEATURES_SUPPORTED
fukasawa e60969
/* Performs intrapixel differencing  */
fukasawa e60969
static void
fukasawa e60969
png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_do_write_intrapixel");
fukasawa e60969
fukasawa e60969
   if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
fukasawa e60969
   {
fukasawa e60969
      int bytes_per_pixel;
fukasawa e60969
      png_uint_32 row_width = row_info->width;
fukasawa e60969
      if (row_info->bit_depth == 8)
fukasawa e60969
      {
fukasawa e60969
         png_bytep rp;
fukasawa e60969
         png_uint_32 i;
fukasawa e60969
fukasawa e60969
         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
fukasawa e60969
            bytes_per_pixel = 3;
fukasawa e60969
fukasawa e60969
         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
fukasawa e60969
            bytes_per_pixel = 4;
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            return;
fukasawa e60969
fukasawa e60969
         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
fukasawa e60969
         {
fukasawa e60969
            *(rp)     = (png_byte)(*rp       - *(rp + 1));
fukasawa e60969
            *(rp + 2) = (png_byte)(*(rp + 2) - *(rp + 1));
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_16BIT_SUPPORTED
fukasawa e60969
      else if (row_info->bit_depth == 16)
fukasawa e60969
      {
fukasawa e60969
         png_bytep rp;
fukasawa e60969
         png_uint_32 i;
fukasawa e60969
fukasawa e60969
         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
fukasawa e60969
            bytes_per_pixel = 6;
fukasawa e60969
fukasawa e60969
         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
fukasawa e60969
            bytes_per_pixel = 8;
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            return;
fukasawa e60969
fukasawa e60969
         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
fukasawa e60969
         {
fukasawa e60969
            png_uint_32 s0   = (*(rp    ) << 8) | *(rp + 1);
fukasawa e60969
            png_uint_32 s1   = (*(rp + 2) << 8) | *(rp + 3);
fukasawa e60969
            png_uint_32 s2   = (*(rp + 4) << 8) | *(rp + 5);
fukasawa e60969
            png_uint_32 red  = (png_uint_32)((s0 - s1) & 0xffffL);
fukasawa e60969
            png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
fukasawa e60969
            *(rp    ) = (png_byte)(red >> 8);
fukasawa e60969
            *(rp + 1) = (png_byte)red;
fukasawa e60969
            *(rp + 4) = (png_byte)(blue >> 8);
fukasawa e60969
            *(rp + 5) = (png_byte)blue;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
#endif /* WRITE_16BIT */
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
#endif /* MNG_FEATURES */
fukasawa e60969
fukasawa e60969
/* Called by user to write a row of image data */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_write_row(png_structrp png_ptr, png_const_bytep row)
fukasawa e60969
{
fukasawa e60969
   /* 1.5.6: moved from png_struct to be a local structure: */
fukasawa e60969
   png_row_info row_info;
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_debug2(1, "in png_write_row (row %u, pass %d)",
fukasawa e60969
      png_ptr->row_number, png_ptr->pass);
fukasawa e60969
fukasawa e60969
   /* Initialize transformations and other stuff if first time */
fukasawa e60969
   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
fukasawa e60969
   {
fukasawa e60969
      /* Make sure we wrote the header info */
fukasawa e60969
      if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
fukasawa e60969
         png_error(png_ptr,
fukasawa e60969
             "png_write_info was never called before png_write_row");
fukasawa e60969
fukasawa e60969
      /* Check for transforms that have been set but were defined out */
fukasawa e60969
#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
fukasawa e60969
      if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
fukasawa e60969
         png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
fukasawa e60969
      if ((png_ptr->transformations & PNG_FILLER) != 0)
fukasawa e60969
         png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
fukasawa e60969
#endif
fukasawa e60969
#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
fukasawa e60969
    defined(PNG_READ_PACKSWAP_SUPPORTED)
fukasawa e60969
      if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
fukasawa e60969
         png_warning(png_ptr,
fukasawa e60969
             "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
fukasawa e60969
      if ((png_ptr->transformations & PNG_PACK) != 0)
fukasawa e60969
         png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
fukasawa e60969
      if ((png_ptr->transformations & PNG_SHIFT) != 0)
fukasawa e60969
         png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
fukasawa e60969
      if ((png_ptr->transformations & PNG_BGR) != 0)
fukasawa e60969
         png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
fukasawa e60969
      if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
fukasawa e60969
         png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
      png_write_start_row(png_ptr);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
fukasawa e60969
   /* If interlaced and not interested in row, return */
fukasawa e60969
   if (png_ptr->interlaced != 0 &&
fukasawa e60969
       (png_ptr->transformations & PNG_INTERLACE) != 0)
fukasawa e60969
   {
fukasawa e60969
      switch (png_ptr->pass)
fukasawa e60969
      {
fukasawa e60969
         case 0:
fukasawa e60969
            if ((png_ptr->row_number & 0x07) != 0)
fukasawa e60969
            {
fukasawa e60969
               png_write_finish_row(png_ptr);
fukasawa e60969
               return;
fukasawa e60969
            }
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 1:
fukasawa e60969
            if ((png_ptr->row_number & 0x07) != 0 || png_ptr->width < 5)
fukasawa e60969
            {
fukasawa e60969
               png_write_finish_row(png_ptr);
fukasawa e60969
               return;
fukasawa e60969
            }
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 2:
fukasawa e60969
            if ((png_ptr->row_number & 0x07) != 4)
fukasawa e60969
            {
fukasawa e60969
               png_write_finish_row(png_ptr);
fukasawa e60969
               return;
fukasawa e60969
            }
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 3:
fukasawa e60969
            if ((png_ptr->row_number & 0x03) != 0 || png_ptr->width < 3)
fukasawa e60969
            {
fukasawa e60969
               png_write_finish_row(png_ptr);
fukasawa e60969
               return;
fukasawa e60969
            }
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 4:
fukasawa e60969
            if ((png_ptr->row_number & 0x03) != 2)
fukasawa e60969
            {
fukasawa e60969
               png_write_finish_row(png_ptr);
fukasawa e60969
               return;
fukasawa e60969
            }
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 5:
fukasawa e60969
            if ((png_ptr->row_number & 0x01) != 0 || png_ptr->width < 2)
fukasawa e60969
            {
fukasawa e60969
               png_write_finish_row(png_ptr);
fukasawa e60969
               return;
fukasawa e60969
            }
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         case 6:
fukasawa e60969
            if ((png_ptr->row_number & 0x01) == 0)
fukasawa e60969
            {
fukasawa e60969
               png_write_finish_row(png_ptr);
fukasawa e60969
               return;
fukasawa e60969
            }
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         default: /* error: ignore it */
fukasawa e60969
            break;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Set up row info for transformations */
fukasawa e60969
   row_info.color_type = png_ptr->color_type;
fukasawa e60969
   row_info.width = png_ptr->usr_width;
fukasawa e60969
   row_info.channels = png_ptr->usr_channels;
fukasawa e60969
   row_info.bit_depth = png_ptr->usr_bit_depth;
fukasawa e60969
   row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
fukasawa e60969
   row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
fukasawa e60969
fukasawa e60969
   png_debug1(3, "row_info->color_type = %d", row_info.color_type);
fukasawa e60969
   png_debug1(3, "row_info->width = %u", row_info.width);
fukasawa e60969
   png_debug1(3, "row_info->channels = %d", row_info.channels);
fukasawa e60969
   png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
fukasawa e60969
   png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
fukasawa e60969
   png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
fukasawa e60969
fukasawa e60969
   /* Copy user's row into buffer, leaving room for filter byte. */
fukasawa e60969
   memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
fukasawa e60969
   /* Handle interlacing */
fukasawa e60969
   if (png_ptr->interlaced && png_ptr->pass < 6 &&
fukasawa e60969
       (png_ptr->transformations & PNG_INTERLACE) != 0)
fukasawa e60969
   {
fukasawa e60969
      png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);
fukasawa e60969
      /* This should always get caught above, but still ... */
fukasawa e60969
      if (row_info.width == 0)
fukasawa e60969
      {
fukasawa e60969
         png_write_finish_row(png_ptr);
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
fukasawa e60969
   /* Handle other transformations */
fukasawa e60969
   if (png_ptr->transformations != 0)
fukasawa e60969
      png_do_write_transformations(png_ptr, &row_info);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* At this point the row_info pixel depth must match the 'transformed' depth,
fukasawa e60969
    * which is also the output depth.
fukasawa e60969
    */
fukasawa e60969
   if (row_info.pixel_depth != png_ptr->pixel_depth ||
fukasawa e60969
       row_info.pixel_depth != png_ptr->transformed_pixel_depth)
fukasawa e60969
      png_error(png_ptr, "internal write transform logic error");
fukasawa e60969
fukasawa e60969
#ifdef PNG_MNG_FEATURES_SUPPORTED
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 ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
fukasawa e60969
       (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
fukasawa e60969
   {
fukasawa e60969
      /* Intrapixel differencing */
fukasawa e60969
      png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Added at libpng-1.5.10 */
fukasawa e60969
#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
fukasawa e60969
   /* Check for out-of-range palette index */
fukasawa e60969
   if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&
fukasawa e60969
       png_ptr->num_palette_max >= 0)
fukasawa e60969
      png_do_check_palette_indexes(png_ptr, &row_info);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Find a filter if necessary, filter the row and write it out. */
fukasawa e60969
   png_write_find_filter(png_ptr, &row_info);
fukasawa e60969
fukasawa e60969
   if (png_ptr->write_row_fn != NULL)
fukasawa e60969
      (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_FLUSH_SUPPORTED
fukasawa e60969
/* Set the automatic flush interval or 0 to turn flushing off */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_flush(png_structrp png_ptr, int nrows)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_set_flush");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Flush the current output buffers now */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_write_flush(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_write_flush");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   /* We have already written out all of the data */
fukasawa e60969
   if (png_ptr->row_number >= png_ptr->num_rows)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);
fukasawa e60969
   png_ptr->flush_rows = 0;
fukasawa e60969
   png_flush(png_ptr);
fukasawa e60969
}
fukasawa e60969
#endif /* WRITE_FLUSH */
fukasawa e60969
fukasawa e60969
/* Free any memory used in png_ptr struct without freeing the struct itself. */
fukasawa e60969
static void
fukasawa e60969
png_write_destroy(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_write_destroy");
fukasawa e60969
fukasawa e60969
   /* Free any memory zlib uses */
fukasawa e60969
   if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
fukasawa e60969
      deflateEnd(&png_ptr->zstream);
fukasawa e60969
fukasawa e60969
   /* Free our memory.  png_free checks NULL for us. */
fukasawa e60969
   png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
fukasawa e60969
   png_free(png_ptr, png_ptr->row_buf);
fukasawa e60969
   png_ptr->row_buf = NULL;
fukasawa e60969
#ifdef PNG_WRITE_FILTER_SUPPORTED
fukasawa e60969
   png_free(png_ptr, png_ptr->prev_row);
fukasawa e60969
   png_free(png_ptr, png_ptr->try_row);
fukasawa e60969
   png_free(png_ptr, png_ptr->tst_row);
fukasawa e60969
   png_ptr->prev_row = NULL;
fukasawa e60969
   png_ptr->try_row = NULL;
fukasawa e60969
   png_ptr->tst_row = NULL;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
   png_free(png_ptr, png_ptr->chunk_list);
fukasawa e60969
   png_ptr->chunk_list = NULL;
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* The error handling and memory handling information is left intact at this
fukasawa e60969
    * point: the jmp_buf may still have to be freed.  See png_destroy_png_struct
fukasawa e60969
    * for how this happens.
fukasawa e60969
    */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Free all memory used by the write.
fukasawa e60969
 * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
fukasawa e60969
 * *png_ptr_ptr.  Prior to 1.6.0 it would accept such a value and it would free
fukasawa e60969
 * the passed in info_structs but it would quietly fail to free any of the data
fukasawa e60969
 * inside them.  In 1.6.0 it quietly does nothing (it has to be quiet because it
fukasawa e60969
 * has no png_ptr.)
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_destroy_write_struct");
fukasawa e60969
fukasawa e60969
   if (png_ptr_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
      png_structrp png_ptr = *png_ptr_ptr;
fukasawa e60969
fukasawa e60969
      if (png_ptr != NULL) /* added in libpng 1.6.0 */
fukasawa e60969
      {
fukasawa e60969
         png_destroy_info_struct(png_ptr, info_ptr_ptr);
fukasawa e60969
fukasawa e60969
         *png_ptr_ptr = NULL;
fukasawa e60969
         png_write_destroy(png_ptr);
fukasawa e60969
         png_destroy_png_struct(png_ptr);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Allow the application to select one or more row filters to use. */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_filter(png_structrp png_ptr, int method, int filters)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_set_filter");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
#ifdef PNG_MNG_FEATURES_SUPPORTED
fukasawa e60969
   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
fukasawa e60969
       (method == PNG_INTRAPIXEL_DIFFERENCING))
fukasawa e60969
      method = PNG_FILTER_TYPE_BASE;
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
   if (method == PNG_FILTER_TYPE_BASE)
fukasawa e60969
   {
fukasawa e60969
      switch (filters & (PNG_ALL_FILTERS | 0x07))
fukasawa e60969
      {
fukasawa e60969
#ifdef PNG_WRITE_FILTER_SUPPORTED
fukasawa e60969
         case 5:
fukasawa e60969
         case 6:
fukasawa e60969
         case 7: png_app_error(png_ptr, "Unknown row filter for method 0");
fukasawa e60969
            /* FALL THROUGH */
fukasawa e60969
#endif /* WRITE_FILTER */
fukasawa e60969
         case PNG_FILTER_VALUE_NONE:
fukasawa e60969
            png_ptr->do_filter = PNG_FILTER_NONE; break;
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_FILTER_SUPPORTED
fukasawa e60969
         case PNG_FILTER_VALUE_SUB:
fukasawa e60969
            png_ptr->do_filter = PNG_FILTER_SUB; break;
fukasawa e60969
fukasawa e60969
         case PNG_FILTER_VALUE_UP:
fukasawa e60969
            png_ptr->do_filter = PNG_FILTER_UP; break;
fukasawa e60969
fukasawa e60969
         case PNG_FILTER_VALUE_AVG:
fukasawa e60969
            png_ptr->do_filter = PNG_FILTER_AVG; break;
fukasawa e60969
fukasawa e60969
         case PNG_FILTER_VALUE_PAETH:
fukasawa e60969
            png_ptr->do_filter = PNG_FILTER_PAETH; break;
fukasawa e60969
fukasawa e60969
         default:
fukasawa e60969
            png_ptr->do_filter = (png_byte)filters; break;
fukasawa e60969
#else
fukasawa e60969
         default:
fukasawa e60969
            png_app_error(png_ptr, "Unknown row filter for method 0");
fukasawa e60969
#endif /* WRITE_FILTER */
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_FILTER_SUPPORTED
fukasawa e60969
      /* If we have allocated the row_buf, this means we have already started
fukasawa e60969
       * with the image and we should have allocated all of the filter buffers
fukasawa e60969
       * that have been selected.  If prev_row isn't already allocated, then
fukasawa e60969
       * it is too late to start using the filters that need it, since we
fukasawa e60969
       * will be missing the data in the previous row.  If an application
fukasawa e60969
       * wants to start and stop using particular filters during compression,
fukasawa e60969
       * it should start out with all of the filters, and then remove them
fukasawa e60969
       * or add them back after the start of compression.
fukasawa e60969
       *
fukasawa e60969
       * NOTE: this is a nasty constraint on the code, because it means that the
fukasawa e60969
       * prev_row buffer must be maintained even if there are currently no
fukasawa e60969
       * 'prev_row' requiring filters active.
fukasawa e60969
       */
fukasawa e60969
      if (png_ptr->row_buf != NULL)
fukasawa e60969
      {
fukasawa e60969
         int num_filters;
fukasawa e60969
         png_alloc_size_t buf_size;
fukasawa e60969
fukasawa e60969
         /* Repeat the checks in png_write_start_row; 1 pixel high or wide
fukasawa e60969
          * images cannot benefit from certain filters.  If this isn't done here
fukasawa e60969
          * the check below will fire on 1 pixel high images.
fukasawa e60969
          */
fukasawa e60969
         if (png_ptr->height == 1)
fukasawa e60969
            filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
fukasawa e60969
fukasawa e60969
         if (png_ptr->width == 1)
fukasawa e60969
            filters &= ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH);
fukasawa e60969
fukasawa e60969
         if ((filters & (PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH)) != 0
fukasawa e60969
            && png_ptr->prev_row == NULL)
fukasawa e60969
         {
fukasawa e60969
            /* This is the error case, however it is benign - the previous row
fukasawa e60969
             * is not available so the filter can't be used.  Just warn here.
fukasawa e60969
             */
fukasawa e60969
            png_app_warning(png_ptr,
fukasawa e60969
               "png_set_filter: UP/AVG/PAETH cannot be added after start");
fukasawa e60969
            filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         num_filters = 0;
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
         /* Allocate needed row buffers if they have not already been
fukasawa e60969
          * allocated.
fukasawa e60969
          */
fukasawa e60969
         buf_size = PNG_ROWBYTES(png_ptr->usr_channels * png_ptr->usr_bit_depth,
fukasawa e60969
             png_ptr->width) + 1;
fukasawa e60969
fukasawa e60969
         if (png_ptr->try_row == NULL)
fukasawa e60969
            png_ptr->try_row = png_voidcast(png_bytep,
fukasawa e60969
               png_malloc(png_ptr, buf_size));
fukasawa e60969
fukasawa e60969
         if (num_filters > 1)
fukasawa e60969
         {
fukasawa e60969
            if (png_ptr->tst_row == NULL)
fukasawa e60969
               png_ptr->tst_row = png_voidcast(png_bytep,
fukasawa e60969
                  png_malloc(png_ptr, buf_size));
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
      png_ptr->do_filter = (png_byte)filters;
fukasawa e60969
#endif
fukasawa e60969
   }
fukasawa e60969
   else
fukasawa e60969
      png_error(png_ptr, "Unknown custom filter method");
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* DEPRECATED */
fukasawa e60969
/* Provide floating and fixed point APIs */
fukasawa e60969
#ifdef PNG_FLOATING_POINT_SUPPORTED
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
fukasawa e60969
    int num_weights, png_const_doublep filter_weights,
fukasawa e60969
    png_const_doublep filter_costs)
fukasawa e60969
{
fukasawa e60969
   PNG_UNUSED(png_ptr)
fukasawa e60969
   PNG_UNUSED(heuristic_method)
fukasawa e60969
   PNG_UNUSED(num_weights)
fukasawa e60969
   PNG_UNUSED(filter_weights)
fukasawa e60969
   PNG_UNUSED(filter_costs)
fukasawa e60969
}
fukasawa e60969
#endif /* FLOATING_POINT */
fukasawa e60969
fukasawa e60969
#ifdef PNG_FIXED_POINT_SUPPORTED
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
fukasawa e60969
    int num_weights, png_const_fixed_point_p filter_weights,
fukasawa e60969
    png_const_fixed_point_p filter_costs)
fukasawa e60969
{
fukasawa e60969
   PNG_UNUSED(png_ptr)
fukasawa e60969
   PNG_UNUSED(heuristic_method)
fukasawa e60969
   PNG_UNUSED(num_weights)
fukasawa e60969
   PNG_UNUSED(filter_weights)
fukasawa e60969
   PNG_UNUSED(filter_costs)
fukasawa e60969
}
fukasawa e60969
#endif /* FIXED_POINT */
fukasawa e60969
#endif /* WRITE_WEIGHTED_FILTER */
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_compression_level(png_structrp png_ptr, int level)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_set_compression_level");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_ptr->zlib_level = level;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_set_compression_mem_level");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_ptr->zlib_mem_level = mem_level;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_compression_strategy(png_structrp png_ptr, int strategy)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_set_compression_strategy");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   /* The flag setting here prevents the libpng dynamic selection of strategy.
fukasawa e60969
    */
fukasawa e60969
   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
fukasawa e60969
   png_ptr->zlib_strategy = strategy;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
fukasawa e60969
 * smaller value of window_bits if it can do so safely.
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   /* Prior to 1.6.0 this would warn but then set the window_bits value. This
fukasawa e60969
    * meant that negative window bits values could be selected that would cause
fukasawa e60969
    * libpng to write a non-standard PNG file with raw deflate or gzip
fukasawa e60969
    * compressed IDAT or ancillary chunks.  Such files can be read and there is
fukasawa e60969
    * no warning on read, so this seems like a very bad idea.
fukasawa e60969
    */
fukasawa e60969
   if (window_bits > 15)
fukasawa e60969
   {
fukasawa e60969
      png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
fukasawa e60969
      window_bits = 15;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (window_bits < 8)
fukasawa e60969
   {
fukasawa e60969
      png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
fukasawa e60969
      window_bits = 8;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_ptr->zlib_window_bits = window_bits;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_compression_method(png_structrp png_ptr, int method)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_set_compression_method");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   /* This would produce an invalid PNG file if it worked, but it doesn't and
fukasawa e60969
    * deflate will fault it, so it is harmless to just warn here.
fukasawa e60969
    */
fukasawa e60969
   if (method != 8)
fukasawa e60969
      png_warning(png_ptr, "Only compression method 8 is supported by PNG");
fukasawa e60969
fukasawa e60969
   png_ptr->zlib_method = method;
fukasawa e60969
}
fukasawa e60969
#endif /* WRITE_CUSTOMIZE_COMPRESSION */
fukasawa e60969
fukasawa e60969
/* The following were added to libpng-1.5.4 */
fukasawa e60969
#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_text_compression_level(png_structrp png_ptr, int level)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_set_text_compression_level");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_ptr->zlib_text_level = level;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_set_text_compression_mem_level");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_ptr->zlib_text_mem_level = mem_level;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_set_text_compression_strategy");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_ptr->zlib_text_strategy = strategy;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
fukasawa e60969
 * smaller value of window_bits if it can do so safely.
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   if (window_bits > 15)
fukasawa e60969
   {
fukasawa e60969
      png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
fukasawa e60969
      window_bits = 15;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (window_bits < 8)
fukasawa e60969
   {
fukasawa e60969
      png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
fukasawa e60969
      window_bits = 8;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_ptr->zlib_text_window_bits = window_bits;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_text_compression_method(png_structrp png_ptr, int method)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_set_text_compression_method");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   if (method != 8)
fukasawa e60969
      png_warning(png_ptr, "Only compression method 8 is supported by PNG");
fukasawa e60969
fukasawa e60969
   png_ptr->zlib_text_method = method;
fukasawa e60969
}
fukasawa e60969
#endif /* WRITE_CUSTOMIZE_ZTXT_COMPRESSION */
fukasawa e60969
/* end of API added to libpng-1.5.4 */
fukasawa e60969
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_ptr->write_row_fn = write_row_fn;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
fukasawa e60969
    write_user_transform_fn)
fukasawa e60969
{
fukasawa e60969
   png_debug(1, "in png_set_write_user_transform_fn");
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_ptr->transformations |= PNG_USER_TRANSFORM;
fukasawa e60969
   png_ptr->write_user_transform_fn = write_user_transform_fn;
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
fukasawa e60969
#ifdef PNG_INFO_IMAGE_SUPPORTED
fukasawa e60969
void PNGAPI
fukasawa e60969
png_write_png(png_structrp png_ptr, png_inforp info_ptr,
fukasawa e60969
    int transforms, voidp params)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL || info_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   if ((info_ptr->valid & PNG_INFO_IDAT) == 0)
fukasawa e60969
   {
fukasawa e60969
      png_app_error(png_ptr, "no rows for png_write_image to write");
fukasawa e60969
      return;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Write the file header information. */
fukasawa e60969
   png_write_info(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
   /* ------ these transformations don't touch the info structure ------- */
fukasawa e60969
fukasawa e60969
   /* Invert monochrome pixels */
fukasawa e60969
   if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
fukasawa e60969
#ifdef PNG_WRITE_INVERT_SUPPORTED
fukasawa e60969
      png_set_invert_mono(png_ptr);
fukasawa e60969
#else
fukasawa e60969
      png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Shift the pixels up to a legal bit depth and fill in
fukasawa e60969
    * as appropriate to correctly scale the image.
fukasawa e60969
    */
fukasawa e60969
   if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
fukasawa e60969
#ifdef PNG_WRITE_SHIFT_SUPPORTED
fukasawa e60969
      if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
fukasawa e60969
         png_set_shift(png_ptr, &info_ptr->sig_bit);
fukasawa e60969
#else
fukasawa e60969
      png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Pack pixels into bytes */
fukasawa e60969
   if ((transforms & PNG_TRANSFORM_PACKING) != 0)
fukasawa e60969
#ifdef PNG_WRITE_PACK_SUPPORTED
fukasawa e60969
      png_set_packing(png_ptr);
fukasawa e60969
#else
fukasawa e60969
      png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Swap location of alpha bytes from ARGB to RGBA */
fukasawa e60969
   if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
fukasawa e60969
#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
fukasawa e60969
      png_set_swap_alpha(png_ptr);
fukasawa e60969
#else
fukasawa e60969
      png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into
fukasawa e60969
    * RGB, note that the code expects the input color type to be G or RGB; no
fukasawa e60969
    * alpha channel.
fukasawa e60969
    */
fukasawa e60969
   if ((transforms & (PNG_TRANSFORM_STRIP_FILLER_AFTER|
fukasawa e60969
       PNG_TRANSFORM_STRIP_FILLER_BEFORE)) != 0)
fukasawa e60969
   {
fukasawa e60969
#ifdef PNG_WRITE_FILLER_SUPPORTED
fukasawa e60969
      if ((transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) != 0)
fukasawa e60969
      {
fukasawa e60969
         if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
fukasawa e60969
            png_app_error(png_ptr,
fukasawa e60969
                "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported");
fukasawa e60969
fukasawa e60969
         /* Continue if ignored - this is the pre-1.6.10 behavior */
fukasawa e60969
         png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
fukasawa e60969
         png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
fukasawa e60969
#else
fukasawa e60969
      png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported");
fukasawa e60969
#endif
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Flip BGR pixels to RGB */
fukasawa e60969
   if ((transforms & PNG_TRANSFORM_BGR) != 0)
fukasawa e60969
#ifdef PNG_WRITE_BGR_SUPPORTED
fukasawa e60969
      png_set_bgr(png_ptr);
fukasawa e60969
#else
fukasawa e60969
      png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Swap bytes of 16-bit files to most significant byte first */
fukasawa e60969
   if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
fukasawa e60969
#ifdef PNG_WRITE_SWAP_SUPPORTED
fukasawa e60969
      png_set_swap(png_ptr);
fukasawa e60969
#else
fukasawa e60969
      png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Swap bits of 1-bit, 2-bit, 4-bit packed pixel formats */
fukasawa e60969
   if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
fukasawa e60969
#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
fukasawa e60969
      png_set_packswap(png_ptr);
fukasawa e60969
#else
fukasawa e60969
      png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Invert the alpha channel from opacity to transparency */
fukasawa e60969
   if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
fukasawa e60969
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
fukasawa e60969
      png_set_invert_alpha(png_ptr);
fukasawa e60969
#else
fukasawa e60969
      png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* ----------------------- end of transformations ------------------- */
fukasawa e60969
fukasawa e60969
   /* Write the bits */
fukasawa e60969
   png_write_image(png_ptr, info_ptr->row_pointers);
fukasawa e60969
fukasawa e60969
   /* It is REQUIRED to call this to finish writing the rest of the file */
fukasawa e60969
   png_write_end(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
   PNG_UNUSED(params)
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
fukasawa e60969
#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
fukasawa e60969
# ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */
fukasawa e60969
/* Initialize the write structure - general purpose utility. */
fukasawa e60969
static int
fukasawa e60969
png_image_write_init(png_imagep image)
fukasawa e60969
{
fukasawa e60969
   png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
fukasawa e60969
       png_safe_error, png_safe_warning);
fukasawa e60969
fukasawa e60969
   if (png_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
      png_infop info_ptr = png_create_info_struct(png_ptr);
fukasawa e60969
fukasawa e60969
      if (info_ptr != NULL)
fukasawa e60969
      {
fukasawa e60969
         png_controlp control = png_voidcast(png_controlp,
fukasawa e60969
             png_malloc_warn(png_ptr, (sizeof *control)));
fukasawa e60969
fukasawa e60969
         if (control != NULL)
fukasawa e60969
         {
fukasawa e60969
            memset(control, 0, (sizeof *control));
fukasawa e60969
fukasawa e60969
            control->png_ptr = png_ptr;
fukasawa e60969
            control->info_ptr = info_ptr;
fukasawa e60969
            control->for_write = 1;
fukasawa e60969
fukasawa e60969
            image->opaque = control;
fukasawa e60969
            return 1;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         /* Error clean up */
fukasawa e60969
         png_destroy_info_struct(png_ptr, &info_ptr);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      png_destroy_write_struct(&png_ptr, NULL);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return png_image_error(image, "png_image_write_: out of memory");
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Arguments to png_image_write_main: */
fukasawa e60969
typedef struct
fukasawa e60969
{
fukasawa e60969
   /* Arguments: */
fukasawa e60969
   png_imagep      image;
fukasawa e60969
   png_const_voidp buffer;
fukasawa e60969
   png_int_32      row_stride;
fukasawa e60969
   png_const_voidp colormap;
fukasawa e60969
   int             convert_to_8bit;
fukasawa e60969
   /* Local variables: */
fukasawa e60969
   png_const_voidp first_row;
fukasawa e60969
   ptrdiff_t       row_bytes;
fukasawa e60969
   png_voidp       local_row;
fukasawa e60969
} png_image_write_control;
fukasawa e60969
fukasawa e60969
/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
fukasawa e60969
 * do any necessary byte swapping.  The component order is defined by the
fukasawa e60969
 * png_image format value.
fukasawa e60969
 */
fukasawa e60969
static int
fukasawa e60969
png_write_image_16bit(png_voidp argument)
fukasawa e60969
{
fukasawa e60969
   png_image_write_control *display = png_voidcast(png_image_write_control*,
fukasawa e60969
       argument);
fukasawa e60969
   png_imagep image = display->image;
fukasawa e60969
   png_structrp png_ptr = image->opaque->png_ptr;
fukasawa e60969
fukasawa e60969
   png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
fukasawa e60969
       display->first_row);
fukasawa e60969
   png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
fukasawa e60969
   png_uint_16p row_end;
fukasawa e60969
   const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
fukasawa e60969
   int aindex = 0;
fukasawa e60969
   png_uint_32 y = image->height;
fukasawa e60969
fukasawa e60969
   if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
fukasawa e60969
   {
fukasawa e60969
#   ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
fukasawa e60969
      if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
fukasawa e60969
      {
fukasawa e60969
         aindex = -1;
fukasawa e60969
         ++input_row; /* To point to the first component */
fukasawa e60969
         ++output_row;
fukasawa e60969
      }
fukasawa e60969
         else
fukasawa e60969
            aindex = channels;
fukasawa e60969
#     else
fukasawa e60969
         aindex = channels;
fukasawa e60969
#     endif
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      png_error(png_ptr, "png_write_image: internal call error");
fukasawa e60969
fukasawa e60969
   /* Work out the output row end and count over this, note that the increment
fukasawa e60969
    * above to 'row' means that row_end can actually be beyond the end of the
fukasawa e60969
    * row; this is correct.
fukasawa e60969
    */
fukasawa e60969
   row_end = output_row + image->width * (channels+1);
fukasawa e60969
fukasawa e60969
   while (y-- > 0)
fukasawa e60969
   {
fukasawa e60969
      png_const_uint_16p in_ptr = input_row;
fukasawa e60969
      png_uint_16p out_ptr = output_row;
fukasawa e60969
fukasawa e60969
      while (out_ptr < row_end)
fukasawa e60969
      {
fukasawa e60969
         const png_uint_16 alpha = in_ptr[aindex];
fukasawa e60969
         png_uint_32 reciprocal = 0;
fukasawa e60969
         int c;
fukasawa e60969
fukasawa e60969
         out_ptr[aindex] = alpha;
fukasawa e60969
fukasawa e60969
         /* Calculate a reciprocal.  The correct calculation is simply
fukasawa e60969
          * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
fukasawa e60969
          * allows correct rounding by adding .5 before the shift.  'reciprocal'
fukasawa e60969
          * is only initialized when required.
fukasawa e60969
          */
fukasawa e60969
         if (alpha > 0 && alpha < 65535)
fukasawa e60969
            reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
fukasawa e60969
fukasawa e60969
         c = channels;
fukasawa e60969
         do /* always at least one channel */
fukasawa e60969
         {
fukasawa e60969
            png_uint_16 component = *in_ptr++;
fukasawa e60969
fukasawa e60969
            /* The following gives 65535 for an alpha of 0, which is fine,
fukasawa e60969
             * otherwise if 0/0 is represented as some other value there is more
fukasawa e60969
             * likely to be a discontinuity which will probably damage
fukasawa e60969
             * compression when moving from a fully transparent area to a
fukasawa e60969
             * nearly transparent one.  (The assumption here is that opaque
fukasawa e60969
             * areas tend not to be 0 intensity.)
fukasawa e60969
             */
fukasawa e60969
            if (component >= alpha)
fukasawa e60969
               component = 65535;
fukasawa e60969
fukasawa e60969
            /* component
fukasawa e60969
             * component*reciprocal is less than 2^31.
fukasawa e60969
             */
fukasawa e60969
            else if (component > 0 && alpha < 65535)
fukasawa e60969
            {
fukasawa e60969
               png_uint_32 calc = component * reciprocal;
fukasawa e60969
               calc += 16384; /* round to nearest */
fukasawa e60969
               component = (png_uint_16)(calc >> 15);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            *out_ptr++ = component;
fukasawa e60969
         }
fukasawa e60969
         while (--c > 0);
fukasawa e60969
fukasawa e60969
         /* Skip to next component (skip the intervening alpha channel) */
fukasawa e60969
         ++in_ptr;
fukasawa e60969
         ++out_ptr;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
fukasawa e60969
      input_row += display->row_bytes/(sizeof (png_uint_16));
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 1;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Given 16-bit input (1 to 4 channels) write 8-bit output.  If an alpha channel
fukasawa e60969
 * is present it must be removed from the components, the components are then
fukasawa e60969
 * written in sRGB encoding.  No components are added or removed.
fukasawa e60969
 *
fukasawa e60969
 * Calculate an alpha reciprocal to reverse pre-multiplication.  As above the
fukasawa e60969
 * calculation can be done to 15 bits of accuracy; however, the output needs to
fukasawa e60969
 * be scaled in the range 0..255*65535, so include that scaling here.
fukasawa e60969
 */
fukasawa e60969
#   define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+(alpha>>1))/alpha)
fukasawa e60969
fukasawa e60969
static png_byte
fukasawa e60969
png_unpremultiply(png_uint_32 component, png_uint_32 alpha,
fukasawa e60969
   png_uint_32 reciprocal/*from the above macro*/)
fukasawa e60969
{
fukasawa e60969
   /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
fukasawa e60969
    * is represented as some other value there is more likely to be a
fukasawa e60969
    * discontinuity which will probably damage compression when moving from a
fukasawa e60969
    * fully transparent area to a nearly transparent one.  (The assumption here
fukasawa e60969
    * is that opaque areas tend not to be 0 intensity.)
fukasawa e60969
    *
fukasawa e60969
    * There is a rounding problem here; if alpha is less than 128 it will end up
fukasawa e60969
    * as 0 when scaled to 8 bits.  To avoid introducing spurious colors into the
fukasawa e60969
    * output change for this too.
fukasawa e60969
    */
fukasawa e60969
   if (component >= alpha || alpha < 128)
fukasawa e60969
      return 255;
fukasawa e60969
fukasawa e60969
   /* component
fukasawa e60969
    * component*reciprocal is less than 2^31.
fukasawa e60969
    */
fukasawa e60969
   else if (component > 0)
fukasawa e60969
   {
fukasawa e60969
      /* The test is that alpha/257 (rounded) is less than 255, the first value
fukasawa e60969
       * that becomes 255 is 65407.
fukasawa e60969
       * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
fukasawa e60969
       * be exact!)  [Could also test reciprocal != 0]
fukasawa e60969
       */
fukasawa e60969
      if (alpha < 65407)
fukasawa e60969
      {
fukasawa e60969
         component *= reciprocal;
fukasawa e60969
         component += 64; /* round to nearest */
fukasawa e60969
         component >>= 7;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         component *= 255;
fukasawa e60969
fukasawa e60969
      /* Convert the component to sRGB. */
fukasawa e60969
      return (png_byte)PNG_sRGB_FROM_LINEAR(component);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      return 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
png_write_image_8bit(png_voidp argument)
fukasawa e60969
{
fukasawa e60969
   png_image_write_control *display = png_voidcast(png_image_write_control*,
fukasawa e60969
       argument);
fukasawa e60969
   png_imagep image = display->image;
fukasawa e60969
   png_structrp png_ptr = image->opaque->png_ptr;
fukasawa e60969
fukasawa e60969
   png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
fukasawa e60969
       display->first_row);
fukasawa e60969
   png_bytep output_row = png_voidcast(png_bytep, display->local_row);
fukasawa e60969
   png_uint_32 y = image->height;
fukasawa e60969
   const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
fukasawa e60969
fukasawa e60969
   if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
fukasawa e60969
   {
fukasawa e60969
      png_bytep row_end;
fukasawa e60969
      int aindex;
fukasawa e60969
fukasawa e60969
#   ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
fukasawa e60969
      if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
fukasawa e60969
      {
fukasawa e60969
         aindex = -1;
fukasawa e60969
         ++input_row; /* To point to the first component */
fukasawa e60969
         ++output_row;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
#   endif
fukasawa e60969
      aindex = channels;
fukasawa e60969
fukasawa e60969
      /* Use row_end in place of a loop counter: */
fukasawa e60969
      row_end = output_row + image->width * (channels+1);
fukasawa e60969
fukasawa e60969
      while (y-- > 0)
fukasawa e60969
      {
fukasawa e60969
         png_const_uint_16p in_ptr = input_row;
fukasawa e60969
         png_bytep out_ptr = output_row;
fukasawa e60969
fukasawa e60969
         while (out_ptr < row_end)
fukasawa e60969
         {
fukasawa e60969
            png_uint_16 alpha = in_ptr[aindex];
fukasawa e60969
            png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
fukasawa e60969
            png_uint_32 reciprocal = 0;
fukasawa e60969
            int c;
fukasawa e60969
fukasawa e60969
            /* Scale and write the alpha channel. */
fukasawa e60969
            out_ptr[aindex] = alphabyte;
fukasawa e60969
fukasawa e60969
            if (alphabyte > 0 && alphabyte < 255)
fukasawa e60969
               reciprocal = UNP_RECIPROCAL(alpha);
fukasawa e60969
fukasawa e60969
            c = channels;
fukasawa e60969
            do /* always at least one channel */
fukasawa e60969
               *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
fukasawa e60969
            while (--c > 0);
fukasawa e60969
fukasawa e60969
            /* Skip to next component (skip the intervening alpha channel) */
fukasawa e60969
            ++in_ptr;
fukasawa e60969
            ++out_ptr;
fukasawa e60969
         } /* while out_ptr < row_end */
fukasawa e60969
fukasawa e60969
         png_write_row(png_ptr, png_voidcast(png_const_bytep,
fukasawa e60969
             display->local_row));
fukasawa e60969
         input_row += display->row_bytes/(sizeof (png_uint_16));
fukasawa e60969
      } /* while y */
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      /* No alpha channel, so the row_end really is the end of the row and it
fukasawa e60969
       * is sufficient to loop over the components one by one.
fukasawa e60969
       */
fukasawa e60969
      png_bytep row_end = output_row + image->width * channels;
fukasawa e60969
fukasawa e60969
      while (y-- > 0)
fukasawa e60969
      {
fukasawa e60969
         png_const_uint_16p in_ptr = input_row;
fukasawa e60969
         png_bytep out_ptr = output_row;
fukasawa e60969
fukasawa e60969
         while (out_ptr < row_end)
fukasawa e60969
         {
fukasawa e60969
            png_uint_32 component = *in_ptr++;
fukasawa e60969
fukasawa e60969
            component *= 255;
fukasawa e60969
            *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         png_write_row(png_ptr, output_row);
fukasawa e60969
         input_row += display->row_bytes/(sizeof (png_uint_16));
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 1;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
png_image_set_PLTE(png_image_write_control *display)
fukasawa e60969
{
fukasawa e60969
   const png_imagep image = display->image;
fukasawa e60969
   const void *cmap = display->colormap;
fukasawa e60969
   const int entries = image->colormap_entries > 256 ? 256 :
fukasawa e60969
       (int)image->colormap_entries;
fukasawa e60969
fukasawa e60969
   /* NOTE: the caller must check for cmap != NULL and entries != 0 */
fukasawa e60969
   const png_uint_32 format = image->format;
fukasawa e60969
   const int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
fukasawa e60969
fukasawa e60969
#   if defined(PNG_FORMAT_BGR_SUPPORTED) &&\
fukasawa e60969
      defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)
fukasawa e60969
      const int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
fukasawa e60969
          (format & PNG_FORMAT_FLAG_ALPHA) != 0;
fukasawa e60969
#   else
fukasawa e60969
#     define afirst 0
fukasawa e60969
#   endif
fukasawa e60969
fukasawa e60969
#   ifdef PNG_FORMAT_BGR_SUPPORTED
fukasawa e60969
      const int bgr = (format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
fukasawa e60969
#   else
fukasawa e60969
#     define bgr 0
fukasawa e60969
#   endif
fukasawa e60969
fukasawa e60969
   int i, num_trans;
fukasawa e60969
   png_color palette[256];
fukasawa e60969
   png_byte tRNS[256];
fukasawa e60969
fukasawa e60969
   memset(tRNS, 255, (sizeof tRNS));
fukasawa e60969
   memset(palette, 0, (sizeof palette));
fukasawa e60969
fukasawa e60969
   for (i=num_trans=0; i
fukasawa e60969
   {
fukasawa e60969
      /* This gets automatically converted to sRGB with reversal of the
fukasawa e60969
       * pre-multiplication if the color-map has an alpha channel.
fukasawa e60969
       */
fukasawa e60969
      if ((format & PNG_FORMAT_FLAG_LINEAR) != 0)
fukasawa e60969
      {
fukasawa e60969
         png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
fukasawa e60969
fukasawa e60969
         entry += i * channels;
fukasawa e60969
fukasawa e60969
         if ((channels & 1) != 0) /* no alpha */
fukasawa e60969
         {
fukasawa e60969
            if (channels >= 3) /* RGB */
fukasawa e60969
            {
fukasawa e60969
               palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
fukasawa e60969
                   entry[(2 ^ bgr)]);
fukasawa e60969
               palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
fukasawa e60969
                   entry[1]);
fukasawa e60969
               palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
fukasawa e60969
                   entry[bgr]);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            else /* Gray */
fukasawa e60969
               palette[i].blue = palette[i].red = palette[i].green =
fukasawa e60969
                  (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else /* alpha */
fukasawa e60969
         {
fukasawa e60969
            png_uint_16 alpha = entry[afirst ? 0 : channels-1];
fukasawa e60969
            png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
fukasawa e60969
            png_uint_32 reciprocal = 0;
fukasawa e60969
fukasawa e60969
            /* Calculate a reciprocal, as in the png_write_image_8bit code above
fukasawa e60969
             * this is designed to produce a value scaled to 255*65535 when
fukasawa e60969
             * divided by 128 (i.e. asr 7).
fukasawa e60969
             */
fukasawa e60969
            if (alphabyte > 0 && alphabyte < 255)
fukasawa e60969
               reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
fukasawa e60969
fukasawa e60969
            tRNS[i] = alphabyte;
fukasawa e60969
            if (alphabyte < 255)
fukasawa e60969
               num_trans = i+1;
fukasawa e60969
fukasawa e60969
            if (channels >= 3) /* RGB */
fukasawa e60969
            {
fukasawa e60969
               palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],
fukasawa e60969
                  alpha, reciprocal);
fukasawa e60969
               palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,
fukasawa e60969
                  reciprocal);
fukasawa e60969
               palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,
fukasawa e60969
                  reciprocal);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            else /* gray */
fukasawa e60969
               palette[i].blue = palette[i].red = palette[i].green =
fukasawa e60969
                  png_unpremultiply(entry[afirst], alpha, reciprocal);
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else /* Color-map has sRGB values */
fukasawa e60969
      {
fukasawa e60969
         png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
fukasawa e60969
fukasawa e60969
         entry += i * channels;
fukasawa e60969
fukasawa e60969
         switch (channels)
fukasawa e60969
         {
fukasawa e60969
            case 4:
fukasawa e60969
               tRNS[i] = entry[afirst ? 0 : 3];
fukasawa e60969
               if (tRNS[i] < 255)
fukasawa e60969
                  num_trans = i+1;
fukasawa e60969
               /* FALL THROUGH */
fukasawa e60969
            case 3:
fukasawa e60969
               palette[i].blue = entry[afirst + (2 ^ bgr)];
fukasawa e60969
               palette[i].green = entry[afirst + 1];
fukasawa e60969
               palette[i].red = entry[afirst + bgr];
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case 2:
fukasawa e60969
               tRNS[i] = entry[1 ^ afirst];
fukasawa e60969
               if (tRNS[i] < 255)
fukasawa e60969
                  num_trans = i+1;
fukasawa e60969
               /* FALL THROUGH */
fukasawa e60969
            case 1:
fukasawa e60969
               palette[i].blue = palette[i].red = palette[i].green =
fukasawa e60969
                  entry[afirst];
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            default:
fukasawa e60969
               break;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#   ifdef afirst
fukasawa e60969
#     undef afirst
fukasawa e60969
#   endif
fukasawa e60969
#   ifdef bgr
fukasawa e60969
#     undef bgr
fukasawa e60969
#   endif
fukasawa e60969
fukasawa e60969
   png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,
fukasawa e60969
      entries);
fukasawa e60969
fukasawa e60969
   if (num_trans > 0)
fukasawa e60969
      png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
fukasawa e60969
         num_trans, NULL);
fukasawa e60969
fukasawa e60969
   image->colormap_entries = entries;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
png_image_write_main(png_voidp argument)
fukasawa e60969
{
fukasawa e60969
   png_image_write_control *display = png_voidcast(png_image_write_control*,
fukasawa e60969
      argument);
fukasawa e60969
   png_imagep image = display->image;
fukasawa e60969
   png_structrp png_ptr = image->opaque->png_ptr;
fukasawa e60969
   png_inforp info_ptr = image->opaque->info_ptr;
fukasawa e60969
   png_uint_32 format = image->format;
fukasawa e60969
fukasawa e60969
   /* The following four ints are actually booleans */
fukasawa e60969
   int colormap = (format & PNG_FORMAT_FLAG_COLORMAP);
fukasawa e60969
   int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR); /* input */
fukasawa e60969
   int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA);
fukasawa e60969
   int write_16bit = linear && !colormap && (display->convert_to_8bit == 0);
fukasawa e60969
fukasawa e60969
#   ifdef PNG_BENIGN_ERRORS_SUPPORTED
fukasawa e60969
      /* Make sure we error out on any bad situation */
fukasawa e60969
      png_set_benign_errors(png_ptr, 0/*error*/);
fukasawa e60969
#   endif
fukasawa e60969
fukasawa e60969
   /* Default the 'row_stride' parameter if required. */
fukasawa e60969
   if (display->row_stride == 0)
fukasawa e60969
      display->row_stride = PNG_IMAGE_ROW_STRIDE(*image);
fukasawa e60969
fukasawa e60969
   /* Set the required transforms then write the rows in the correct order. */
fukasawa e60969
   if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0)
fukasawa e60969
   {
fukasawa e60969
      if (display->colormap != NULL && image->colormap_entries > 0)
fukasawa e60969
      {
fukasawa e60969
         png_uint_32 entries = image->colormap_entries;
fukasawa e60969
fukasawa e60969
         png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
fukasawa e60969
            entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),
fukasawa e60969
            PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
fukasawa e60969
            PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
fukasawa e60969
fukasawa e60969
         png_image_set_PLTE(display);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         png_error(image->opaque->png_ptr,
fukasawa e60969
            "no color-map for color-mapped image");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
fukasawa e60969
         write_16bit ? 16 : 8,
fukasawa e60969
         ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
fukasawa e60969
         ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
fukasawa e60969
         PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
fukasawa e60969
fukasawa e60969
   /* Counter-intuitively the data transformations must be called *after*
fukasawa e60969
    * png_write_info, not before as in the read code, but the 'set' functions
fukasawa e60969
    * must still be called before.  Just set the color space information, never
fukasawa e60969
    * write an interlaced image.
fukasawa e60969
    */
fukasawa e60969
fukasawa e60969
   if (write_16bit != 0)
fukasawa e60969
   {
fukasawa e60969
      /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
fukasawa e60969
      png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
fukasawa e60969
fukasawa e60969
      if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
fukasawa e60969
         png_set_cHRM_fixed(png_ptr, info_ptr,
fukasawa e60969
            /* color      x       y */
fukasawa e60969
            /* white */ 31270, 32900,
fukasawa e60969
            /* red   */ 64000, 33000,
fukasawa e60969
            /* green */ 30000, 60000,
fukasawa e60969
            /* blue  */ 15000,  6000
fukasawa e60969
         );
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
fukasawa e60969
      png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
fukasawa e60969
fukasawa e60969
   /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
fukasawa e60969
    * space must still be gamma encoded.
fukasawa e60969
    */
fukasawa e60969
   else
fukasawa e60969
      png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
fukasawa e60969
fukasawa e60969
   /* Write the file header. */
fukasawa e60969
   png_write_info(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
   /* Now set up the data transformations (*after* the header is written),
fukasawa e60969
    * remove the handled transformations from the 'format' flags for checking.
fukasawa e60969
    *
fukasawa e60969
    * First check for a little endian system if writing 16-bit files.
fukasawa e60969
    */
fukasawa e60969
   if (write_16bit != 0)
fukasawa e60969
   {
fukasawa e60969
      PNG_CONST png_uint_16 le = 0x0001;
fukasawa e60969
fukasawa e60969
      if ((*(png_const_bytep) & le) != 0)
fukasawa e60969
         png_set_swap(png_ptr);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#   ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
fukasawa e60969
      if ((format & PNG_FORMAT_FLAG_BGR) != 0)
fukasawa e60969
      {
fukasawa e60969
         if (colormap == 0 && (format & PNG_FORMAT_FLAG_COLOR) != 0)
fukasawa e60969
            png_set_bgr(png_ptr);
fukasawa e60969
         format &= ~PNG_FORMAT_FLAG_BGR;
fukasawa e60969
      }
fukasawa e60969
#   endif
fukasawa e60969
fukasawa e60969
#   ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
fukasawa e60969
      if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
fukasawa e60969
      {
fukasawa e60969
         if (colormap == 0 && (format & PNG_FORMAT_FLAG_ALPHA) != 0)
fukasawa e60969
            png_set_swap_alpha(png_ptr);
fukasawa e60969
         format &= ~PNG_FORMAT_FLAG_AFIRST;
fukasawa e60969
      }
fukasawa e60969
#   endif
fukasawa e60969
fukasawa e60969
   /* If there are 16 or fewer color-map entries we wrote a lower bit depth
fukasawa e60969
    * above, but the application data is still byte packed.
fukasawa e60969
    */
fukasawa e60969
   if (colormap != 0 && image->colormap_entries <= 16)
fukasawa e60969
      png_set_packing(png_ptr);
fukasawa e60969
fukasawa e60969
   /* That should have handled all (both) the transforms. */
fukasawa e60969
   if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
fukasawa e60969
         PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)
fukasawa e60969
      png_error(png_ptr, "png_write_image: unsupported transformation");
fukasawa e60969
fukasawa e60969
   {
fukasawa e60969
      png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
fukasawa e60969
      ptrdiff_t row_bytes = display->row_stride;
fukasawa e60969
fukasawa e60969
      if (linear != 0)
fukasawa e60969
         row_bytes *= (sizeof (png_uint_16));
fukasawa e60969
fukasawa e60969
      if (row_bytes < 0)
fukasawa e60969
         row += (image->height-1) * (-row_bytes);
fukasawa e60969
fukasawa e60969
      display->first_row = row;
fukasawa e60969
      display->row_bytes = row_bytes;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Apply 'fast' options if the flag is set. */
fukasawa e60969
   if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)
fukasawa e60969
   {
fukasawa e60969
      png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);
fukasawa e60969
      /* NOTE: determined by experiment using pngstest, this reflects some
fukasawa e60969
       * balance between the time to write the image once and the time to read
fukasawa e60969
       * it about 50 times.  The speed-up in pngstest was about 10-20% of the
fukasawa e60969
       * total (user) time on a heavily loaded system.
fukasawa e60969
       */
fukasawa e60969
#   ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
fukasawa e60969
      png_set_compression_level(png_ptr, 3);
fukasawa e60969
#   endif
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Check for the cases that currently require a pre-transform on the row
fukasawa e60969
    * before it is written.  This only applies when the input is 16-bit and
fukasawa e60969
    * either there is an alpha channel or it is converted to 8-bit.
fukasawa e60969
    */
fukasawa e60969
   if ((linear != 0 && alpha != 0 ) ||
fukasawa e60969
       (colormap == 0 && display->convert_to_8bit != 0))
fukasawa e60969
   {
fukasawa e60969
      png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
fukasawa e60969
         png_get_rowbytes(png_ptr, info_ptr)));
fukasawa e60969
      int result;
fukasawa e60969
fukasawa e60969
      display->local_row = row;
fukasawa e60969
      if (write_16bit != 0)
fukasawa e60969
         result = png_safe_execute(image, png_write_image_16bit, display);
fukasawa e60969
      else
fukasawa e60969
         result = png_safe_execute(image, png_write_image_8bit, display);
fukasawa e60969
      display->local_row = NULL;
fukasawa e60969
fukasawa e60969
      png_free(png_ptr, row);
fukasawa e60969
fukasawa e60969
      /* Skip the 'write_end' on error: */
fukasawa e60969
      if (result == 0)
fukasawa e60969
         return 0;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Otherwise this is the case where the input is in a format currently
fukasawa e60969
    * supported by the rest of the libpng write code; call it directly.
fukasawa e60969
    */
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
fukasawa e60969
      ptrdiff_t row_bytes = display->row_bytes;
fukasawa e60969
      png_uint_32 y = image->height;
fukasawa e60969
fukasawa e60969
      while (y-- > 0)
fukasawa e60969
      {
fukasawa e60969
         png_write_row(png_ptr, row);
fukasawa e60969
         row += row_bytes;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_write_end(png_ptr, info_ptr);
fukasawa e60969
   return 1;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
int PNGAPI
fukasawa e60969
png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
fukasawa e60969
   const void *buffer, png_int_32 row_stride, const void *colormap)
fukasawa e60969
{
fukasawa e60969
   /* Write the image to the given (FILE*). */
fukasawa e60969
   if (image != NULL && image->version == PNG_IMAGE_VERSION)
fukasawa e60969
   {
fukasawa e60969
      if (file != NULL)
fukasawa e60969
      {
fukasawa e60969
         if (png_image_write_init(image) != 0)
fukasawa e60969
         {
fukasawa e60969
            png_image_write_control display;
fukasawa e60969
            int result;
fukasawa e60969
fukasawa e60969
            /* This is slightly evil, but png_init_io doesn't do anything other
fukasawa e60969
             * than this and we haven't changed the standard IO functions so
fukasawa e60969
             * this saves a 'safe' function.
fukasawa e60969
             */
fukasawa e60969
            image->opaque->png_ptr->io_ptr = file;
fukasawa e60969
fukasawa e60969
            memset(&display, 0, (sizeof display));
fukasawa e60969
            display.image = image;
fukasawa e60969
            display.buffer = buffer;
fukasawa e60969
            display.row_stride = row_stride;
fukasawa e60969
            display.colormap = colormap;
fukasawa e60969
            display.convert_to_8bit = convert_to_8bit;
fukasawa e60969
fukasawa e60969
            result = png_safe_execute(image, png_image_write_main, &display);
fukasawa e60969
            png_image_free(image);
fukasawa e60969
            return result;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            return 0;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         return png_image_error(image,
fukasawa e60969
            "png_image_write_to_stdio: invalid argument");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (image != NULL)
fukasawa e60969
      return png_image_error(image,
fukasawa e60969
         "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION");
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      return 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
int PNGAPI
fukasawa e60969
png_image_write_to_file(png_imagep image, const char *file_name,
fukasawa e60969
   int convert_to_8bit, const void *buffer, png_int_32 row_stride,
fukasawa e60969
   const void *colormap)
fukasawa e60969
{
fukasawa e60969
   /* Write the image to the named file. */
fukasawa e60969
   if (image != NULL && image->version == PNG_IMAGE_VERSION)
fukasawa e60969
   {
fukasawa e60969
      if (file_name != NULL)
fukasawa e60969
      {
fukasawa e60969
         FILE *fp = fopen(file_name, "wb");
fukasawa e60969
fukasawa e60969
         if (fp != NULL)
fukasawa e60969
         {
fukasawa e60969
            if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
fukasawa e60969
               row_stride, colormap) != 0)
fukasawa e60969
            {
fukasawa e60969
               int error; /* from fflush/fclose */
fukasawa e60969
fukasawa e60969
               /* Make sure the file is flushed correctly. */
fukasawa e60969
               if (fflush(fp) == 0 && ferror(fp) == 0)
fukasawa e60969
               {
fukasawa e60969
                  if (fclose(fp) == 0)
fukasawa e60969
                     return 1;
fukasawa e60969
fukasawa e60969
                  error = errno; /* from fclose */
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               else
fukasawa e60969
               {
fukasawa e60969
                  error = errno; /* from fflush or ferror */
fukasawa e60969
                  (void)fclose(fp);
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               (void)remove(file_name);
fukasawa e60969
               /* The image has already been cleaned up; this is just used to
fukasawa e60969
                * set the error (because the original write succeeded).
fukasawa e60969
                */
fukasawa e60969
               return png_image_error(image, strerror(error));
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            else
fukasawa e60969
            {
fukasawa e60969
               /* Clean up: just the opened file. */
fukasawa e60969
               (void)fclose(fp);
fukasawa e60969
               (void)remove(file_name);
fukasawa e60969
               return 0;
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            return png_image_error(image, strerror(errno));
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         return png_image_error(image,
fukasawa e60969
            "png_image_write_to_file: invalid argument");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (image != NULL)
fukasawa e60969
      return png_image_error(image,
fukasawa e60969
         "png_image_write_to_file: incorrect PNG_IMAGE_VERSION");
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      return 0;
fukasawa e60969
}
fukasawa e60969
# endif /* STDIO */
fukasawa e60969
#endif /* SIMPLIFIED_WRITE */
fukasawa e60969
#endif /* WRITE */