media/libpng/pngwrite.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libpng/pngwrite.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,2483 @@
     1.4 +
     1.5 +/* pngwrite.c - general routines to write a PNG file
     1.6 + *
     1.7 + * Last changed in libpng 1.6.10 [March 6, 2014]
     1.8 + * Copyright (c) 1998-2014 Glenn Randers-Pehrson
     1.9 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
    1.10 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
    1.11 + *
    1.12 + * This code is released under the libpng license.
    1.13 + * For conditions of distribution and use, see the disclaimer
    1.14 + * and license in png.h
    1.15 + */
    1.16 +
    1.17 +#include "pngpriv.h"
    1.18 +#if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
    1.19 +#  include <errno.h>
    1.20 +#endif
    1.21 +
    1.22 +#ifdef PNG_WRITE_SUPPORTED
    1.23 +
    1.24 +#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
    1.25 +/* Write out all the unknown chunks for the current given location */
    1.26 +static void
    1.27 +write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr,
    1.28 +   unsigned int where)
    1.29 +{
    1.30 +   if (info_ptr->unknown_chunks_num)
    1.31 +   {
    1.32 +      png_const_unknown_chunkp up;
    1.33 +
    1.34 +      png_debug(5, "writing extra chunks");
    1.35 +
    1.36 +      for (up = info_ptr->unknown_chunks;
    1.37 +           up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
    1.38 +           ++up)
    1.39 +         if (up->location & where)
    1.40 +      {
    1.41 +         /* If per-chunk unknown chunk handling is enabled use it, otherwise
    1.42 +          * just write the chunks the application has set.
    1.43 +          */
    1.44 +#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
    1.45 +         int keep = png_handle_as_unknown(png_ptr, up->name);
    1.46 +
    1.47 +         /* NOTE: this code is radically different from the read side in the
    1.48 +          * matter of handling an ancillary unknown chunk.  In the read side
    1.49 +          * the default behavior is to discard it, in the code below the default
    1.50 +          * behavior is to write it.  Critical chunks are, however, only
    1.51 +          * written if explicitly listed or if the default is set to write all
    1.52 +          * unknown chunks.
    1.53 +          *
    1.54 +          * The default handling is also slightly weird - it is not possible to
    1.55 +          * stop the writing of all unsafe-to-copy chunks!
    1.56 +          *
    1.57 +          * TODO: REVIEW: this would seem to be a bug.
    1.58 +          */
    1.59 +         if (keep != PNG_HANDLE_CHUNK_NEVER &&
    1.60 +             ((up->name[3] & 0x20) /* safe-to-copy overrides everything */ ||
    1.61 +              keep == PNG_HANDLE_CHUNK_ALWAYS ||
    1.62 +              (keep == PNG_HANDLE_CHUNK_AS_DEFAULT &&
    1.63 +               png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS)))
    1.64 +#endif
    1.65 +         {
    1.66 +            /* TODO: review, what is wrong with a zero length unknown chunk? */
    1.67 +            if (up->size == 0)
    1.68 +               png_warning(png_ptr, "Writing zero-length unknown chunk");
    1.69 +
    1.70 +            png_write_chunk(png_ptr, up->name, up->data, up->size);
    1.71 +         }
    1.72 +      }
    1.73 +   }
    1.74 +}
    1.75 +#endif /* PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED */
    1.76 +
    1.77 +/* Writes all the PNG information.  This is the suggested way to use the
    1.78 + * library.  If you have a new chunk to add, make a function to write it,
    1.79 + * and put it in the correct location here.  If you want the chunk written
    1.80 + * after the image data, put it in png_write_end().  I strongly encourage
    1.81 + * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
    1.82 + * the chunk, as that will keep the code from breaking if you want to just
    1.83 + * write a plain PNG file.  If you have long comments, I suggest writing
    1.84 + * them in png_write_end(), and compressing them.
    1.85 + */
    1.86 +void PNGAPI
    1.87 +png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)
    1.88 +{
    1.89 +   png_debug(1, "in png_write_info_before_PLTE");
    1.90 +
    1.91 +   if (png_ptr == NULL || info_ptr == NULL)
    1.92 +      return;
    1.93 +
    1.94 +   if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
    1.95 +   {
    1.96 +   /* Write PNG signature */
    1.97 +   png_write_sig(png_ptr);
    1.98 +
    1.99 +#ifdef PNG_MNG_FEATURES_SUPPORTED
   1.100 +   if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
   1.101 +       (png_ptr->mng_features_permitted))
   1.102 +   {
   1.103 +      png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
   1.104 +      png_ptr->mng_features_permitted = 0;
   1.105 +   }
   1.106 +#endif
   1.107 +
   1.108 +   /* Write IHDR information. */
   1.109 +   png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
   1.110 +       info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
   1.111 +       info_ptr->filter_type,
   1.112 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
   1.113 +       info_ptr->interlace_type
   1.114 +#else
   1.115 +       0
   1.116 +#endif
   1.117 +      );
   1.118 +
   1.119 +   /* The rest of these check to see if the valid field has the appropriate
   1.120 +    * flag set, and if it does, writes the chunk.
   1.121 +    *
   1.122 +    * 1.6.0: COLORSPACE support controls the writing of these chunks too, and
   1.123 +    * the chunks will be written if the WRITE routine is there and information
   1.124 +    * is available in the COLORSPACE.  (See png_colorspace_sync_info in png.c
   1.125 +    * for where the valid flags get set.)
   1.126 +    *
   1.127 +    * Under certain circumstances the colorspace can be invalidated without
   1.128 +    * syncing the info_struct 'valid' flags; this happens if libpng detects and
   1.129 +    * error and calls png_error while the color space is being set, yet the
   1.130 +    * application continues writing the PNG.  So check the 'invalid' flag here
   1.131 +    * too.
   1.132 +    */
   1.133 +#ifdef PNG_WRITE_APNG_SUPPORTED
   1.134 +   if (info_ptr->valid & PNG_INFO_acTL)
   1.135 +      png_write_acTL(png_ptr, info_ptr->num_frames, info_ptr->num_plays);
   1.136 +#endif
   1.137 +#ifdef PNG_GAMMA_SUPPORTED
   1.138 +#  ifdef PNG_WRITE_gAMA_SUPPORTED
   1.139 +      if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
   1.140 +         (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) &&
   1.141 +         (info_ptr->valid & PNG_INFO_gAMA))
   1.142 +         png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma);
   1.143 +#  endif
   1.144 +#endif
   1.145 +
   1.146 +#ifdef PNG_COLORSPACE_SUPPORTED
   1.147 +   /* Write only one of sRGB or an ICC profile.  If a profile was supplied
   1.148 +    * and it matches one of the known sRGB ones issue a warning.
   1.149 +    */
   1.150 +#  ifdef PNG_WRITE_iCCP_SUPPORTED
   1.151 +      if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
   1.152 +         (info_ptr->valid & PNG_INFO_iCCP))
   1.153 +      {
   1.154 +#        ifdef PNG_WRITE_sRGB_SUPPORTED
   1.155 +            if (info_ptr->valid & PNG_INFO_sRGB)
   1.156 +               png_app_warning(png_ptr,
   1.157 +                  "profile matches sRGB but writing iCCP instead");
   1.158 +#        endif
   1.159 +
   1.160 +         png_write_iCCP(png_ptr, info_ptr->iccp_name,
   1.161 +            info_ptr->iccp_profile);
   1.162 +      }
   1.163 +#     ifdef PNG_WRITE_sRGB_SUPPORTED
   1.164 +         else
   1.165 +#     endif
   1.166 +#  endif
   1.167 +
   1.168 +#  ifdef PNG_WRITE_sRGB_SUPPORTED
   1.169 +      if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
   1.170 +         (info_ptr->valid & PNG_INFO_sRGB))
   1.171 +         png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent);
   1.172 +#  endif /* WRITE_sRGB */
   1.173 +#endif /* COLORSPACE */
   1.174 +
   1.175 +#ifdef PNG_WRITE_sBIT_SUPPORTED
   1.176 +   if (info_ptr->valid & PNG_INFO_sBIT)
   1.177 +      png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
   1.178 +#endif
   1.179 +
   1.180 +#ifdef PNG_COLORSPACE_SUPPORTED
   1.181 +#  ifdef PNG_WRITE_cHRM_SUPPORTED
   1.182 +      if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
   1.183 +         (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) &&
   1.184 +         (info_ptr->valid & PNG_INFO_cHRM))
   1.185 +         png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy);
   1.186 +#  endif
   1.187 +#endif
   1.188 +
   1.189 +#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
   1.190 +      write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR);
   1.191 +#endif
   1.192 +
   1.193 +      png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
   1.194 +   }
   1.195 +}
   1.196 +
   1.197 +void PNGAPI
   1.198 +png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
   1.199 +{
   1.200 +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
   1.201 +   int i;
   1.202 +#endif
   1.203 +
   1.204 +   png_debug(1, "in png_write_info");
   1.205 +
   1.206 +   if (png_ptr == NULL || info_ptr == NULL)
   1.207 +      return;
   1.208 +
   1.209 +   png_write_info_before_PLTE(png_ptr, info_ptr);
   1.210 +
   1.211 +   if (info_ptr->valid & PNG_INFO_PLTE)
   1.212 +      png_write_PLTE(png_ptr, info_ptr->palette,
   1.213 +          (png_uint_32)info_ptr->num_palette);
   1.214 +
   1.215 +   else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
   1.216 +      png_error(png_ptr, "Valid palette required for paletted images");
   1.217 +
   1.218 +#ifdef PNG_WRITE_tRNS_SUPPORTED
   1.219 +   if (info_ptr->valid & PNG_INFO_tRNS)
   1.220 +   {
   1.221 +#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
   1.222 +      /* Invert the alpha channel (in tRNS) */
   1.223 +      if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
   1.224 +          info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
   1.225 +      {
   1.226 +         int j;
   1.227 +         for (j = 0; j<(int)info_ptr->num_trans; j++)
   1.228 +            info_ptr->trans_alpha[j] =
   1.229 +               (png_byte)(255 - info_ptr->trans_alpha[j]);
   1.230 +      }
   1.231 +#endif
   1.232 +      png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
   1.233 +          info_ptr->num_trans, info_ptr->color_type);
   1.234 +   }
   1.235 +#endif
   1.236 +#ifdef PNG_WRITE_bKGD_SUPPORTED
   1.237 +   if (info_ptr->valid & PNG_INFO_bKGD)
   1.238 +      png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
   1.239 +#endif
   1.240 +
   1.241 +#ifdef PNG_WRITE_hIST_SUPPORTED
   1.242 +   if (info_ptr->valid & PNG_INFO_hIST)
   1.243 +      png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
   1.244 +#endif
   1.245 +
   1.246 +#ifdef PNG_WRITE_oFFs_SUPPORTED
   1.247 +   if (info_ptr->valid & PNG_INFO_oFFs)
   1.248 +      png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
   1.249 +          info_ptr->offset_unit_type);
   1.250 +#endif
   1.251 +
   1.252 +#ifdef PNG_WRITE_pCAL_SUPPORTED
   1.253 +   if (info_ptr->valid & PNG_INFO_pCAL)
   1.254 +      png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
   1.255 +          info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
   1.256 +          info_ptr->pcal_units, info_ptr->pcal_params);
   1.257 +#endif
   1.258 +
   1.259 +#ifdef PNG_WRITE_sCAL_SUPPORTED
   1.260 +   if (info_ptr->valid & PNG_INFO_sCAL)
   1.261 +      png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
   1.262 +          info_ptr->scal_s_width, info_ptr->scal_s_height);
   1.263 +#endif /* sCAL */
   1.264 +
   1.265 +#ifdef PNG_WRITE_pHYs_SUPPORTED
   1.266 +   if (info_ptr->valid & PNG_INFO_pHYs)
   1.267 +      png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
   1.268 +          info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
   1.269 +#endif /* pHYs */
   1.270 +
   1.271 +#ifdef PNG_WRITE_tIME_SUPPORTED
   1.272 +   if (info_ptr->valid & PNG_INFO_tIME)
   1.273 +   {
   1.274 +      png_write_tIME(png_ptr, &(info_ptr->mod_time));
   1.275 +      png_ptr->mode |= PNG_WROTE_tIME;
   1.276 +   }
   1.277 +#endif /* tIME */
   1.278 +
   1.279 +#ifdef PNG_WRITE_sPLT_SUPPORTED
   1.280 +   if (info_ptr->valid & PNG_INFO_sPLT)
   1.281 +      for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
   1.282 +         png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
   1.283 +#endif /* sPLT */
   1.284 +
   1.285 +#ifdef PNG_WRITE_TEXT_SUPPORTED
   1.286 +   /* Check to see if we need to write text chunks */
   1.287 +   for (i = 0; i < info_ptr->num_text; i++)
   1.288 +   {
   1.289 +      png_debug2(2, "Writing header text chunk %d, type %d", i,
   1.290 +          info_ptr->text[i].compression);
   1.291 +      /* An internationalized chunk? */
   1.292 +      if (info_ptr->text[i].compression > 0)
   1.293 +      {
   1.294 +#ifdef PNG_WRITE_iTXt_SUPPORTED
   1.295 +         /* Write international chunk */
   1.296 +         png_write_iTXt(png_ptr,
   1.297 +             info_ptr->text[i].compression,
   1.298 +             info_ptr->text[i].key,
   1.299 +             info_ptr->text[i].lang,
   1.300 +             info_ptr->text[i].lang_key,
   1.301 +             info_ptr->text[i].text);
   1.302 +#else
   1.303 +          png_warning(png_ptr, "Unable to write international text");
   1.304 +#endif
   1.305 +          /* Mark this chunk as written */
   1.306 +          info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
   1.307 +      }
   1.308 +
   1.309 +      /* If we want a compressed text chunk */
   1.310 +      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
   1.311 +      {
   1.312 +#ifdef PNG_WRITE_zTXt_SUPPORTED
   1.313 +         /* Write compressed chunk */
   1.314 +         png_write_zTXt(png_ptr, info_ptr->text[i].key,
   1.315 +             info_ptr->text[i].text, 0,
   1.316 +             info_ptr->text[i].compression);
   1.317 +#else
   1.318 +         png_warning(png_ptr, "Unable to write compressed text");
   1.319 +#endif
   1.320 +         /* Mark this chunk as written */
   1.321 +         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
   1.322 +      }
   1.323 +
   1.324 +      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
   1.325 +      {
   1.326 +#ifdef PNG_WRITE_tEXt_SUPPORTED
   1.327 +         /* Write uncompressed chunk */
   1.328 +         png_write_tEXt(png_ptr, info_ptr->text[i].key,
   1.329 +             info_ptr->text[i].text,
   1.330 +             0);
   1.331 +         /* Mark this chunk as written */
   1.332 +         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
   1.333 +#else
   1.334 +         /* Can't get here */
   1.335 +         png_warning(png_ptr, "Unable to write uncompressed text");
   1.336 +#endif
   1.337 +      }
   1.338 +   }
   1.339 +#endif /* tEXt */
   1.340 +
   1.341 +#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
   1.342 +   write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE);
   1.343 +#endif
   1.344 +}
   1.345 +
   1.346 +/* Writes the end of the PNG file.  If you don't want to write comments or
   1.347 + * time information, you can pass NULL for info.  If you already wrote these
   1.348 + * in png_write_info(), do not write them again here.  If you have long
   1.349 + * comments, I suggest writing them here, and compressing them.
   1.350 + */
   1.351 +void PNGAPI
   1.352 +png_write_end(png_structrp png_ptr, png_inforp info_ptr)
   1.353 +{
   1.354 +   png_debug(1, "in png_write_end");
   1.355 +
   1.356 +   if (png_ptr == NULL)
   1.357 +      return;
   1.358 +
   1.359 +   if (!(png_ptr->mode & PNG_HAVE_IDAT))
   1.360 +      png_error(png_ptr, "No IDATs written into file");
   1.361 +
   1.362 +#ifdef PNG_WRITE_APNG_SUPPORTED
   1.363 +   if (png_ptr->num_frames_written != png_ptr->num_frames_to_write)
   1.364 +      png_error(png_ptr, "Not enough frames written");
   1.365 +#endif
   1.366 +
   1.367 +#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
   1.368 +   if (png_ptr->num_palette_max > png_ptr->num_palette)
   1.369 +      png_benign_error(png_ptr, "Wrote palette index exceeding num_palette");
   1.370 +#endif
   1.371 +
   1.372 +   /* See if user wants us to write information chunks */
   1.373 +   if (info_ptr != NULL)
   1.374 +   {
   1.375 +#ifdef PNG_WRITE_TEXT_SUPPORTED
   1.376 +      int i; /* local index variable */
   1.377 +#endif
   1.378 +#ifdef PNG_WRITE_tIME_SUPPORTED
   1.379 +      /* Check to see if user has supplied a time chunk */
   1.380 +      if ((info_ptr->valid & PNG_INFO_tIME) &&
   1.381 +          !(png_ptr->mode & PNG_WROTE_tIME))
   1.382 +         png_write_tIME(png_ptr, &(info_ptr->mod_time));
   1.383 +
   1.384 +#endif
   1.385 +#ifdef PNG_WRITE_TEXT_SUPPORTED
   1.386 +      /* Loop through comment chunks */
   1.387 +      for (i = 0; i < info_ptr->num_text; i++)
   1.388 +      {
   1.389 +         png_debug2(2, "Writing trailer text chunk %d, type %d", i,
   1.390 +            info_ptr->text[i].compression);
   1.391 +         /* An internationalized chunk? */
   1.392 +         if (info_ptr->text[i].compression > 0)
   1.393 +         {
   1.394 +#ifdef PNG_WRITE_iTXt_SUPPORTED
   1.395 +            /* Write international chunk */
   1.396 +            png_write_iTXt(png_ptr,
   1.397 +                info_ptr->text[i].compression,
   1.398 +                info_ptr->text[i].key,
   1.399 +                info_ptr->text[i].lang,
   1.400 +                info_ptr->text[i].lang_key,
   1.401 +                info_ptr->text[i].text);
   1.402 +#else
   1.403 +            png_warning(png_ptr, "Unable to write international text");
   1.404 +#endif
   1.405 +            /* Mark this chunk as written */
   1.406 +            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
   1.407 +         }
   1.408 +
   1.409 +         else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
   1.410 +         {
   1.411 +#ifdef PNG_WRITE_zTXt_SUPPORTED
   1.412 +            /* Write compressed chunk */
   1.413 +            png_write_zTXt(png_ptr, info_ptr->text[i].key,
   1.414 +                info_ptr->text[i].text, 0,
   1.415 +                info_ptr->text[i].compression);
   1.416 +#else
   1.417 +            png_warning(png_ptr, "Unable to write compressed text");
   1.418 +#endif
   1.419 +            /* Mark this chunk as written */
   1.420 +            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
   1.421 +         }
   1.422 +
   1.423 +         else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
   1.424 +         {
   1.425 +#ifdef PNG_WRITE_tEXt_SUPPORTED
   1.426 +            /* Write uncompressed chunk */
   1.427 +            png_write_tEXt(png_ptr, info_ptr->text[i].key,
   1.428 +                info_ptr->text[i].text, 0);
   1.429 +#else
   1.430 +            png_warning(png_ptr, "Unable to write uncompressed text");
   1.431 +#endif
   1.432 +
   1.433 +            /* Mark this chunk as written */
   1.434 +            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
   1.435 +         }
   1.436 +      }
   1.437 +#endif
   1.438 +#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
   1.439 +      write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT);
   1.440 +#endif
   1.441 +   }
   1.442 +
   1.443 +   png_ptr->mode |= PNG_AFTER_IDAT;
   1.444 +
   1.445 +   /* Write end of PNG file */
   1.446 +   png_write_IEND(png_ptr);
   1.447 +   /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
   1.448 +    * and restored again in libpng-1.2.30, may cause some applications that
   1.449 +    * do not set png_ptr->output_flush_fn to crash.  If your application
   1.450 +    * experiences a problem, please try building libpng with
   1.451 +    * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
   1.452 +    * png-mng-implement at lists.sf.net .
   1.453 +    */
   1.454 +#ifdef PNG_WRITE_FLUSH_SUPPORTED
   1.455 +#  ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
   1.456 +   png_flush(png_ptr);
   1.457 +#  endif
   1.458 +#endif
   1.459 +}
   1.460 +
   1.461 +#ifdef PNG_CONVERT_tIME_SUPPORTED
   1.462 +void PNGAPI
   1.463 +png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm * ttime)
   1.464 +{
   1.465 +   png_debug(1, "in png_convert_from_struct_tm");
   1.466 +
   1.467 +   ptime->year = (png_uint_16)(1900 + ttime->tm_year);
   1.468 +   ptime->month = (png_byte)(ttime->tm_mon + 1);
   1.469 +   ptime->day = (png_byte)ttime->tm_mday;
   1.470 +   ptime->hour = (png_byte)ttime->tm_hour;
   1.471 +   ptime->minute = (png_byte)ttime->tm_min;
   1.472 +   ptime->second = (png_byte)ttime->tm_sec;
   1.473 +}
   1.474 +
   1.475 +void PNGAPI
   1.476 +png_convert_from_time_t(png_timep ptime, time_t ttime)
   1.477 +{
   1.478 +   struct tm *tbuf;
   1.479 +
   1.480 +   png_debug(1, "in png_convert_from_time_t");
   1.481 +
   1.482 +   tbuf = gmtime(&ttime);
   1.483 +   png_convert_from_struct_tm(ptime, tbuf);
   1.484 +}
   1.485 +#endif
   1.486 +
   1.487 +/* Initialize png_ptr structure, and allocate any memory needed */
   1.488 +PNG_FUNCTION(png_structp,PNGAPI
   1.489 +png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
   1.490 +    png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
   1.491 +{
   1.492 +#ifndef PNG_USER_MEM_SUPPORTED
   1.493 +   png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
   1.494 +      error_fn, warn_fn, NULL, NULL, NULL);
   1.495 +#else
   1.496 +   return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
   1.497 +       warn_fn, NULL, NULL, NULL);
   1.498 +}
   1.499 +
   1.500 +/* Alternate initialize png_ptr structure, and allocate any memory needed */
   1.501 +PNG_FUNCTION(png_structp,PNGAPI
   1.502 +png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
   1.503 +    png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
   1.504 +    png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
   1.505 +{
   1.506 +   png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
   1.507 +      error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
   1.508 +#endif /* PNG_USER_MEM_SUPPORTED */
   1.509 +   if (png_ptr != NULL)
   1.510 +   {
   1.511 +      /* Set the zlib control values to defaults; they can be overridden by the
   1.512 +       * application after the struct has been created.
   1.513 +       */
   1.514 +      png_ptr->zbuffer_size = PNG_ZBUF_SIZE;
   1.515 +
   1.516 +      /* The 'zlib_strategy' setting is irrelevant because png_default_claim in
   1.517 +       * pngwutil.c defaults it according to whether or not filters will be
   1.518 +       * used, and ignores this setting.
   1.519 +       */
   1.520 +      png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY;
   1.521 +      png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION;
   1.522 +      png_ptr->zlib_mem_level = 8;
   1.523 +      png_ptr->zlib_window_bits = 15;
   1.524 +      png_ptr->zlib_method = 8;
   1.525 +
   1.526 +#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
   1.527 +      png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY;
   1.528 +      png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION;
   1.529 +      png_ptr->zlib_text_mem_level = 8;
   1.530 +      png_ptr->zlib_text_window_bits = 15;
   1.531 +      png_ptr->zlib_text_method = 8;
   1.532 +#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
   1.533 +
   1.534 +      /* This is a highly dubious configuration option; by default it is off,
   1.535 +       * but it may be appropriate for private builds that are testing
   1.536 +       * extensions not conformant to the current specification, or of
   1.537 +       * applications that must not fail to write at all costs!
   1.538 +       */
   1.539 +#ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED
   1.540 +      png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
   1.541 +      /* In stable builds only warn if an application error can be completely
   1.542 +       * handled.
   1.543 +       */
   1.544 +#endif
   1.545 +
   1.546 +      /* App warnings are warnings in release (or release candidate) builds but
   1.547 +       * are errors during development.
   1.548 +       */
   1.549 +#if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
   1.550 +      png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
   1.551 +#endif
   1.552 +
   1.553 +      /* TODO: delay this, it can be done in png_init_io() (if the app doesn't
   1.554 +       * do it itself) avoiding setting the default function if it is not
   1.555 +       * required.
   1.556 +       */
   1.557 +      png_set_write_fn(png_ptr, NULL, NULL, NULL);
   1.558 +   }
   1.559 +
   1.560 +   return png_ptr;
   1.561 +}
   1.562 +
   1.563 +
   1.564 +/* Write a few rows of image data.  If the image is interlaced,
   1.565 + * either you will have to write the 7 sub images, or, if you
   1.566 + * have called png_set_interlace_handling(), you will have to
   1.567 + * "write" the image seven times.
   1.568 + */
   1.569 +void PNGAPI
   1.570 +png_write_rows(png_structrp png_ptr, png_bytepp row,
   1.571 +    png_uint_32 num_rows)
   1.572 +{
   1.573 +   png_uint_32 i; /* row counter */
   1.574 +   png_bytepp rp; /* row pointer */
   1.575 +
   1.576 +   png_debug(1, "in png_write_rows");
   1.577 +
   1.578 +   if (png_ptr == NULL)
   1.579 +      return;
   1.580 +
   1.581 +   /* Loop through the rows */
   1.582 +   for (i = 0, rp = row; i < num_rows; i++, rp++)
   1.583 +   {
   1.584 +      png_write_row(png_ptr, *rp);
   1.585 +   }
   1.586 +}
   1.587 +
   1.588 +/* Write the image.  You only need to call this function once, even
   1.589 + * if you are writing an interlaced image.
   1.590 + */
   1.591 +void PNGAPI
   1.592 +png_write_image(png_structrp png_ptr, png_bytepp image)
   1.593 +{
   1.594 +   png_uint_32 i; /* row index */
   1.595 +   int pass, num_pass; /* pass variables */
   1.596 +   png_bytepp rp; /* points to current row */
   1.597 +
   1.598 +   if (png_ptr == NULL)
   1.599 +      return;
   1.600 +
   1.601 +   png_debug(1, "in png_write_image");
   1.602 +
   1.603 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
   1.604 +   /* Initialize interlace handling.  If image is not interlaced,
   1.605 +    * this will set pass to 1
   1.606 +    */
   1.607 +   num_pass = png_set_interlace_handling(png_ptr);
   1.608 +#else
   1.609 +   num_pass = 1;
   1.610 +#endif
   1.611 +   /* Loop through passes */
   1.612 +   for (pass = 0; pass < num_pass; pass++)
   1.613 +   {
   1.614 +      /* Loop through image */
   1.615 +      for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
   1.616 +      {
   1.617 +         png_write_row(png_ptr, *rp);
   1.618 +      }
   1.619 +   }
   1.620 +}
   1.621 +
   1.622 +#ifdef PNG_MNG_FEATURES_SUPPORTED
   1.623 +/* Performs intrapixel differencing  */
   1.624 +static void
   1.625 +png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
   1.626 +{
   1.627 +   png_debug(1, "in png_do_write_intrapixel");
   1.628 +
   1.629 +   if ((row_info->color_type & PNG_COLOR_MASK_COLOR))
   1.630 +   {
   1.631 +      int bytes_per_pixel;
   1.632 +      png_uint_32 row_width = row_info->width;
   1.633 +      if (row_info->bit_depth == 8)
   1.634 +      {
   1.635 +         png_bytep rp;
   1.636 +         png_uint_32 i;
   1.637 +
   1.638 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
   1.639 +            bytes_per_pixel = 3;
   1.640 +
   1.641 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
   1.642 +            bytes_per_pixel = 4;
   1.643 +
   1.644 +         else
   1.645 +            return;
   1.646 +
   1.647 +         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
   1.648 +         {
   1.649 +            *(rp)     = (png_byte)((*rp       - *(rp + 1)) & 0xff);
   1.650 +            *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
   1.651 +         }
   1.652 +      }
   1.653 +
   1.654 +#ifdef PNG_WRITE_16BIT_SUPPORTED
   1.655 +      else if (row_info->bit_depth == 16)
   1.656 +      {
   1.657 +         png_bytep rp;
   1.658 +         png_uint_32 i;
   1.659 +
   1.660 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
   1.661 +            bytes_per_pixel = 6;
   1.662 +
   1.663 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
   1.664 +            bytes_per_pixel = 8;
   1.665 +
   1.666 +         else
   1.667 +            return;
   1.668 +
   1.669 +         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
   1.670 +         {
   1.671 +            png_uint_32 s0   = (*(rp    ) << 8) | *(rp + 1);
   1.672 +            png_uint_32 s1   = (*(rp + 2) << 8) | *(rp + 3);
   1.673 +            png_uint_32 s2   = (*(rp + 4) << 8) | *(rp + 5);
   1.674 +            png_uint_32 red  = (png_uint_32)((s0 - s1) & 0xffffL);
   1.675 +            png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
   1.676 +            *(rp    ) = (png_byte)((red >> 8) & 0xff);
   1.677 +            *(rp + 1) = (png_byte)(red & 0xff);
   1.678 +            *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
   1.679 +            *(rp + 5) = (png_byte)(blue & 0xff);
   1.680 +         }
   1.681 +      }
   1.682 +#endif /* PNG_WRITE_16BIT_SUPPORTED */
   1.683 +   }
   1.684 +}
   1.685 +#endif /* PNG_MNG_FEATURES_SUPPORTED */
   1.686 +
   1.687 +/* Called by user to write a row of image data */
   1.688 +void PNGAPI
   1.689 +png_write_row(png_structrp png_ptr, png_const_bytep row)
   1.690 +{
   1.691 +   /* 1.5.6: moved from png_struct to be a local structure: */
   1.692 +   png_row_info row_info;
   1.693 +
   1.694 +   if (png_ptr == NULL)
   1.695 +      return;
   1.696 +
   1.697 +   png_debug2(1, "in png_write_row (row %u, pass %d)",
   1.698 +      png_ptr->row_number, png_ptr->pass);
   1.699 +
   1.700 +   /* Initialize transformations and other stuff if first time */
   1.701 +   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
   1.702 +   {
   1.703 +      /* Make sure we wrote the header info */
   1.704 +      if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
   1.705 +         png_error(png_ptr,
   1.706 +             "png_write_info was never called before png_write_row");
   1.707 +
   1.708 +      /* Check for transforms that have been set but were defined out */
   1.709 +#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
   1.710 +      if (png_ptr->transformations & PNG_INVERT_MONO)
   1.711 +         png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
   1.712 +#endif
   1.713 +
   1.714 +#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
   1.715 +      if (png_ptr->transformations & PNG_FILLER)
   1.716 +         png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
   1.717 +#endif
   1.718 +#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
   1.719 +    defined(PNG_READ_PACKSWAP_SUPPORTED)
   1.720 +      if (png_ptr->transformations & PNG_PACKSWAP)
   1.721 +         png_warning(png_ptr,
   1.722 +             "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
   1.723 +#endif
   1.724 +
   1.725 +#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
   1.726 +      if (png_ptr->transformations & PNG_PACK)
   1.727 +         png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
   1.728 +#endif
   1.729 +
   1.730 +#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
   1.731 +      if (png_ptr->transformations & PNG_SHIFT)
   1.732 +         png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
   1.733 +#endif
   1.734 +
   1.735 +#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
   1.736 +      if (png_ptr->transformations & PNG_BGR)
   1.737 +         png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
   1.738 +#endif
   1.739 +
   1.740 +#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
   1.741 +      if (png_ptr->transformations & PNG_SWAP_BYTES)
   1.742 +         png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
   1.743 +#endif
   1.744 +
   1.745 +      png_write_start_row(png_ptr);
   1.746 +   }
   1.747 +
   1.748 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
   1.749 +   /* If interlaced and not interested in row, return */
   1.750 +   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
   1.751 +   {
   1.752 +      switch (png_ptr->pass)
   1.753 +      {
   1.754 +         case 0:
   1.755 +            if (png_ptr->row_number & 0x07)
   1.756 +            {
   1.757 +               png_write_finish_row(png_ptr);
   1.758 +               return;
   1.759 +            }
   1.760 +            break;
   1.761 +
   1.762 +         case 1:
   1.763 +            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
   1.764 +            {
   1.765 +               png_write_finish_row(png_ptr);
   1.766 +               return;
   1.767 +            }
   1.768 +            break;
   1.769 +
   1.770 +         case 2:
   1.771 +            if ((png_ptr->row_number & 0x07) != 4)
   1.772 +            {
   1.773 +               png_write_finish_row(png_ptr);
   1.774 +               return;
   1.775 +            }
   1.776 +            break;
   1.777 +
   1.778 +         case 3:
   1.779 +            if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
   1.780 +            {
   1.781 +               png_write_finish_row(png_ptr);
   1.782 +               return;
   1.783 +            }
   1.784 +            break;
   1.785 +
   1.786 +         case 4:
   1.787 +            if ((png_ptr->row_number & 0x03) != 2)
   1.788 +            {
   1.789 +               png_write_finish_row(png_ptr);
   1.790 +               return;
   1.791 +            }
   1.792 +            break;
   1.793 +
   1.794 +         case 5:
   1.795 +            if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
   1.796 +            {
   1.797 +               png_write_finish_row(png_ptr);
   1.798 +               return;
   1.799 +            }
   1.800 +            break;
   1.801 +
   1.802 +         case 6:
   1.803 +            if (!(png_ptr->row_number & 0x01))
   1.804 +            {
   1.805 +               png_write_finish_row(png_ptr);
   1.806 +               return;
   1.807 +            }
   1.808 +            break;
   1.809 +
   1.810 +         default: /* error: ignore it */
   1.811 +            break;
   1.812 +      }
   1.813 +   }
   1.814 +#endif
   1.815 +
   1.816 +   /* Set up row info for transformations */
   1.817 +   row_info.color_type = png_ptr->color_type;
   1.818 +   row_info.width = png_ptr->usr_width;
   1.819 +   row_info.channels = png_ptr->usr_channels;
   1.820 +   row_info.bit_depth = png_ptr->usr_bit_depth;
   1.821 +   row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
   1.822 +   row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
   1.823 +
   1.824 +   png_debug1(3, "row_info->color_type = %d", row_info.color_type);
   1.825 +   png_debug1(3, "row_info->width = %u", row_info.width);
   1.826 +   png_debug1(3, "row_info->channels = %d", row_info.channels);
   1.827 +   png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
   1.828 +   png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
   1.829 +   png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
   1.830 +
   1.831 +   /* Copy user's row into buffer, leaving room for filter byte. */
   1.832 +   memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
   1.833 +
   1.834 +#ifdef PNG_WRITE_INTERLACING_SUPPORTED
   1.835 +   /* Handle interlacing */
   1.836 +   if (png_ptr->interlaced && png_ptr->pass < 6 &&
   1.837 +       (png_ptr->transformations & PNG_INTERLACE))
   1.838 +   {
   1.839 +      png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);
   1.840 +      /* This should always get caught above, but still ... */
   1.841 +      if (!(row_info.width))
   1.842 +      {
   1.843 +         png_write_finish_row(png_ptr);
   1.844 +         return;
   1.845 +      }
   1.846 +   }
   1.847 +#endif
   1.848 +
   1.849 +#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
   1.850 +   /* Handle other transformations */
   1.851 +   if (png_ptr->transformations)
   1.852 +      png_do_write_transformations(png_ptr, &row_info);
   1.853 +#endif
   1.854 +
   1.855 +   /* At this point the row_info pixel depth must match the 'transformed' depth,
   1.856 +    * which is also the output depth.
   1.857 +    */
   1.858 +   if (row_info.pixel_depth != png_ptr->pixel_depth ||
   1.859 +      row_info.pixel_depth != png_ptr->transformed_pixel_depth)
   1.860 +      png_error(png_ptr, "internal write transform logic error");
   1.861 +
   1.862 +#ifdef PNG_MNG_FEATURES_SUPPORTED
   1.863 +   /* Write filter_method 64 (intrapixel differencing) only if
   1.864 +    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
   1.865 +    * 2. Libpng did not write a PNG signature (this filter_method is only
   1.866 +    *    used in PNG datastreams that are embedded in MNG datastreams) and
   1.867 +    * 3. The application called png_permit_mng_features with a mask that
   1.868 +    *    included PNG_FLAG_MNG_FILTER_64 and
   1.869 +    * 4. The filter_method is 64 and
   1.870 +    * 5. The color_type is RGB or RGBA
   1.871 +    */
   1.872 +   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
   1.873 +       (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
   1.874 +   {
   1.875 +      /* Intrapixel differencing */
   1.876 +      png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
   1.877 +   }
   1.878 +#endif
   1.879 +
   1.880 +/* Added at libpng-1.5.10 */
   1.881 +#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
   1.882 +   /* Check for out-of-range palette index */
   1.883 +   if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&
   1.884 +       png_ptr->num_palette_max >= 0)
   1.885 +      png_do_check_palette_indexes(png_ptr, &row_info);
   1.886 +#endif
   1.887 +
   1.888 +   /* Find a filter if necessary, filter the row and write it out. */
   1.889 +   png_write_find_filter(png_ptr, &row_info);
   1.890 +
   1.891 +   if (png_ptr->write_row_fn != NULL)
   1.892 +      (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
   1.893 +}
   1.894 +
   1.895 +#ifdef PNG_WRITE_FLUSH_SUPPORTED
   1.896 +/* Set the automatic flush interval or 0 to turn flushing off */
   1.897 +void PNGAPI
   1.898 +png_set_flush(png_structrp png_ptr, int nrows)
   1.899 +{
   1.900 +   png_debug(1, "in png_set_flush");
   1.901 +
   1.902 +   if (png_ptr == NULL)
   1.903 +      return;
   1.904 +
   1.905 +   png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
   1.906 +}
   1.907 +
   1.908 +/* Flush the current output buffers now */
   1.909 +void PNGAPI
   1.910 +png_write_flush(png_structrp png_ptr)
   1.911 +{
   1.912 +   png_debug(1, "in png_write_flush");
   1.913 +
   1.914 +   if (png_ptr == NULL)
   1.915 +      return;
   1.916 +
   1.917 +   /* We have already written out all of the data */
   1.918 +   if (png_ptr->row_number >= png_ptr->num_rows)
   1.919 +      return;
   1.920 +
   1.921 +   png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);
   1.922 +   png_ptr->flush_rows = 0;
   1.923 +   png_flush(png_ptr);
   1.924 +}
   1.925 +#endif /* PNG_WRITE_FLUSH_SUPPORTED */
   1.926 +
   1.927 +#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
   1.928 +static void png_reset_filter_heuristics(png_structrp png_ptr);/* forward decl */
   1.929 +#endif
   1.930 +
   1.931 +/* Free any memory used in png_ptr struct without freeing the struct itself. */
   1.932 +static void
   1.933 +png_write_destroy(png_structrp png_ptr)
   1.934 +{
   1.935 +   png_debug(1, "in png_write_destroy");
   1.936 +
   1.937 +   /* Free any memory zlib uses */
   1.938 +   if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED)
   1.939 +      deflateEnd(&png_ptr->zstream);
   1.940 +
   1.941 +   /* Free our memory.  png_free checks NULL for us. */
   1.942 +   png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
   1.943 +   png_free(png_ptr, png_ptr->row_buf);
   1.944 +#ifdef PNG_WRITE_FILTER_SUPPORTED
   1.945 +   png_free(png_ptr, png_ptr->prev_row);
   1.946 +   png_free(png_ptr, png_ptr->sub_row);
   1.947 +   png_free(png_ptr, png_ptr->up_row);
   1.948 +   png_free(png_ptr, png_ptr->avg_row);
   1.949 +   png_free(png_ptr, png_ptr->paeth_row);
   1.950 +#endif
   1.951 +
   1.952 +#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
   1.953 +   /* Use this to save a little code space, it doesn't free the filter_costs */
   1.954 +   png_reset_filter_heuristics(png_ptr);
   1.955 +   png_free(png_ptr, png_ptr->filter_costs);
   1.956 +   png_free(png_ptr, png_ptr->inv_filter_costs);
   1.957 +#endif
   1.958 +
   1.959 +#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
   1.960 +   png_free(png_ptr, png_ptr->chunk_list);
   1.961 +#endif
   1.962 +
   1.963 +   /* The error handling and memory handling information is left intact at this
   1.964 +    * point: the jmp_buf may still have to be freed.  See png_destroy_png_struct
   1.965 +    * for how this happens.
   1.966 +    */
   1.967 +}
   1.968 +
   1.969 +/* Free all memory used by the write.
   1.970 + * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
   1.971 + * *png_ptr_ptr.  Prior to 1.6.0 it would accept such a value and it would free
   1.972 + * the passed in info_structs but it would quietly fail to free any of the data
   1.973 + * inside them.  In 1.6.0 it quietly does nothing (it has to be quiet because it
   1.974 + * has no png_ptr.)
   1.975 + */
   1.976 +void PNGAPI
   1.977 +png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
   1.978 +{
   1.979 +   png_debug(1, "in png_destroy_write_struct");
   1.980 +
   1.981 +   if (png_ptr_ptr != NULL)
   1.982 +   {
   1.983 +      png_structrp png_ptr = *png_ptr_ptr;
   1.984 +
   1.985 +      if (png_ptr != NULL) /* added in libpng 1.6.0 */
   1.986 +      {
   1.987 +         png_destroy_info_struct(png_ptr, info_ptr_ptr);
   1.988 +
   1.989 +         *png_ptr_ptr = NULL;
   1.990 +         png_write_destroy(png_ptr);
   1.991 +         png_destroy_png_struct(png_ptr);
   1.992 +      }
   1.993 +   }
   1.994 +}
   1.995 +
   1.996 +/* Allow the application to select one or more row filters to use. */
   1.997 +void PNGAPI
   1.998 +png_set_filter(png_structrp png_ptr, int method, int filters)
   1.999 +{
  1.1000 +   png_debug(1, "in png_set_filter");
  1.1001 +
  1.1002 +   if (png_ptr == NULL)
  1.1003 +      return;
  1.1004 +
  1.1005 +#ifdef PNG_MNG_FEATURES_SUPPORTED
  1.1006 +   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  1.1007 +       (method == PNG_INTRAPIXEL_DIFFERENCING))
  1.1008 +      method = PNG_FILTER_TYPE_BASE;
  1.1009 +
  1.1010 +#endif
  1.1011 +   if (method == PNG_FILTER_TYPE_BASE)
  1.1012 +   {
  1.1013 +      switch (filters & (PNG_ALL_FILTERS | 0x07))
  1.1014 +      {
  1.1015 +#ifdef PNG_WRITE_FILTER_SUPPORTED
  1.1016 +         case 5:
  1.1017 +         case 6:
  1.1018 +         case 7: png_app_error(png_ptr, "Unknown row filter for method 0");
  1.1019 +            /* FALL THROUGH */
  1.1020 +#endif /* PNG_WRITE_FILTER_SUPPORTED */
  1.1021 +         case PNG_FILTER_VALUE_NONE:
  1.1022 +            png_ptr->do_filter = PNG_FILTER_NONE; break;
  1.1023 +
  1.1024 +#ifdef PNG_WRITE_FILTER_SUPPORTED
  1.1025 +         case PNG_FILTER_VALUE_SUB:
  1.1026 +            png_ptr->do_filter = PNG_FILTER_SUB; break;
  1.1027 +
  1.1028 +         case PNG_FILTER_VALUE_UP:
  1.1029 +            png_ptr->do_filter = PNG_FILTER_UP; break;
  1.1030 +
  1.1031 +         case PNG_FILTER_VALUE_AVG:
  1.1032 +            png_ptr->do_filter = PNG_FILTER_AVG; break;
  1.1033 +
  1.1034 +         case PNG_FILTER_VALUE_PAETH:
  1.1035 +            png_ptr->do_filter = PNG_FILTER_PAETH; break;
  1.1036 +
  1.1037 +         default:
  1.1038 +            png_ptr->do_filter = (png_byte)filters; break;
  1.1039 +#else
  1.1040 +         default:
  1.1041 +            png_app_error(png_ptr, "Unknown row filter for method 0");
  1.1042 +#endif /* PNG_WRITE_FILTER_SUPPORTED */
  1.1043 +      }
  1.1044 +
  1.1045 +      /* If we have allocated the row_buf, this means we have already started
  1.1046 +       * with the image and we should have allocated all of the filter buffers
  1.1047 +       * that have been selected.  If prev_row isn't already allocated, then
  1.1048 +       * it is too late to start using the filters that need it, since we
  1.1049 +       * will be missing the data in the previous row.  If an application
  1.1050 +       * wants to start and stop using particular filters during compression,
  1.1051 +       * it should start out with all of the filters, and then add and
  1.1052 +       * remove them after the start of compression.
  1.1053 +       */
  1.1054 +      if (png_ptr->row_buf != NULL)
  1.1055 +      {
  1.1056 +#ifdef PNG_WRITE_FILTER_SUPPORTED
  1.1057 +         if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
  1.1058 +         {
  1.1059 +            png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
  1.1060 +                (png_ptr->rowbytes + 1));
  1.1061 +            png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
  1.1062 +         }
  1.1063 +
  1.1064 +         if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
  1.1065 +         {
  1.1066 +            if (png_ptr->prev_row == NULL)
  1.1067 +            {
  1.1068 +               png_warning(png_ptr, "Can't add Up filter after starting");
  1.1069 +               png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
  1.1070 +                   ~PNG_FILTER_UP);
  1.1071 +            }
  1.1072 +
  1.1073 +            else
  1.1074 +            {
  1.1075 +               png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
  1.1076 +                   (png_ptr->rowbytes + 1));
  1.1077 +               png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
  1.1078 +            }
  1.1079 +         }
  1.1080 +
  1.1081 +         if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
  1.1082 +         {
  1.1083 +            if (png_ptr->prev_row == NULL)
  1.1084 +            {
  1.1085 +               png_warning(png_ptr, "Can't add Average filter after starting");
  1.1086 +               png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
  1.1087 +                   ~PNG_FILTER_AVG);
  1.1088 +            }
  1.1089 +
  1.1090 +            else
  1.1091 +            {
  1.1092 +               png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
  1.1093 +                   (png_ptr->rowbytes + 1));
  1.1094 +               png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
  1.1095 +            }
  1.1096 +         }
  1.1097 +
  1.1098 +         if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
  1.1099 +             png_ptr->paeth_row == NULL)
  1.1100 +         {
  1.1101 +            if (png_ptr->prev_row == NULL)
  1.1102 +            {
  1.1103 +               png_warning(png_ptr, "Can't add Paeth filter after starting");
  1.1104 +               png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
  1.1105 +            }
  1.1106 +
  1.1107 +            else
  1.1108 +            {
  1.1109 +               png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
  1.1110 +                   (png_ptr->rowbytes + 1));
  1.1111 +               png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
  1.1112 +            }
  1.1113 +         }
  1.1114 +
  1.1115 +         if (png_ptr->do_filter == PNG_NO_FILTERS)
  1.1116 +#endif /* PNG_WRITE_FILTER_SUPPORTED */
  1.1117 +            png_ptr->do_filter = PNG_FILTER_NONE;
  1.1118 +      }
  1.1119 +   }
  1.1120 +   else
  1.1121 +      png_error(png_ptr, "Unknown custom filter method");
  1.1122 +}
  1.1123 +
  1.1124 +/* This allows us to influence the way in which libpng chooses the "best"
  1.1125 + * filter for the current scanline.  While the "minimum-sum-of-absolute-
  1.1126 + * differences metric is relatively fast and effective, there is some
  1.1127 + * question as to whether it can be improved upon by trying to keep the
  1.1128 + * filtered data going to zlib more consistent, hopefully resulting in
  1.1129 + * better compression.
  1.1130 + */
  1.1131 +#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED      /* GRR 970116 */
  1.1132 +/* Convenience reset API. */
  1.1133 +static void
  1.1134 +png_reset_filter_heuristics(png_structrp png_ptr)
  1.1135 +{
  1.1136 +   /* Clear out any old values in the 'weights' - this must be done because if
  1.1137 +    * the app calls set_filter_heuristics multiple times with different
  1.1138 +    * 'num_weights' values we would otherwise potentially have wrong sized
  1.1139 +    * arrays.
  1.1140 +    */
  1.1141 +   png_ptr->num_prev_filters = 0;
  1.1142 +   png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
  1.1143 +   if (png_ptr->prev_filters != NULL)
  1.1144 +   {
  1.1145 +      png_bytep old = png_ptr->prev_filters;
  1.1146 +      png_ptr->prev_filters = NULL;
  1.1147 +      png_free(png_ptr, old);
  1.1148 +   }
  1.1149 +   if (png_ptr->filter_weights != NULL)
  1.1150 +   {
  1.1151 +      png_uint_16p old = png_ptr->filter_weights;
  1.1152 +      png_ptr->filter_weights = NULL;
  1.1153 +      png_free(png_ptr, old);
  1.1154 +   }
  1.1155 +
  1.1156 +   if (png_ptr->inv_filter_weights != NULL)
  1.1157 +   {
  1.1158 +      png_uint_16p old = png_ptr->inv_filter_weights;
  1.1159 +      png_ptr->inv_filter_weights = NULL;
  1.1160 +      png_free(png_ptr, old);
  1.1161 +   }
  1.1162 +
  1.1163 +   /* Leave the filter_costs - this array is fixed size. */
  1.1164 +}
  1.1165 +
  1.1166 +static int
  1.1167 +png_init_filter_heuristics(png_structrp png_ptr, int heuristic_method,
  1.1168 +   int num_weights)
  1.1169 +{
  1.1170 +   if (png_ptr == NULL)
  1.1171 +      return 0;
  1.1172 +
  1.1173 +   /* Clear out the arrays */
  1.1174 +   png_reset_filter_heuristics(png_ptr);
  1.1175 +
  1.1176 +   /* Check arguments; the 'reset' function makes the correct settings for the
  1.1177 +    * unweighted case, but we must handle the weight case by initializing the
  1.1178 +    * arrays for the caller.
  1.1179 +    */
  1.1180 +   if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  1.1181 +   {
  1.1182 +      int i;
  1.1183 +
  1.1184 +      if (num_weights > 0)
  1.1185 +      {
  1.1186 +         png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
  1.1187 +             (png_uint_32)((sizeof (png_byte)) * num_weights));
  1.1188 +
  1.1189 +         /* To make sure that the weighting starts out fairly */
  1.1190 +         for (i = 0; i < num_weights; i++)
  1.1191 +         {
  1.1192 +            png_ptr->prev_filters[i] = 255;
  1.1193 +         }
  1.1194 +
  1.1195 +         png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
  1.1196 +             (png_uint_32)((sizeof (png_uint_16)) * num_weights));
  1.1197 +
  1.1198 +         png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
  1.1199 +             (png_uint_32)((sizeof (png_uint_16)) * num_weights));
  1.1200 +
  1.1201 +         for (i = 0; i < num_weights; i++)
  1.1202 +         {
  1.1203 +            png_ptr->inv_filter_weights[i] =
  1.1204 +            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
  1.1205 +         }
  1.1206 +
  1.1207 +         /* Safe to set this now */
  1.1208 +         png_ptr->num_prev_filters = (png_byte)num_weights;
  1.1209 +      }
  1.1210 +
  1.1211 +      /* If, in the future, there are other filter methods, this would
  1.1212 +       * need to be based on png_ptr->filter.
  1.1213 +       */
  1.1214 +      if (png_ptr->filter_costs == NULL)
  1.1215 +      {
  1.1216 +         png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
  1.1217 +             (png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST));
  1.1218 +
  1.1219 +         png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
  1.1220 +             (png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST));
  1.1221 +      }
  1.1222 +
  1.1223 +      for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
  1.1224 +      {
  1.1225 +         png_ptr->inv_filter_costs[i] =
  1.1226 +         png_ptr->filter_costs[i] = PNG_COST_FACTOR;
  1.1227 +      }
  1.1228 +
  1.1229 +      /* All the arrays are inited, safe to set this: */
  1.1230 +      png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
  1.1231 +
  1.1232 +      /* Return the 'ok' code. */
  1.1233 +      return 1;
  1.1234 +   }
  1.1235 +   else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
  1.1236 +      heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
  1.1237 +   {
  1.1238 +      return 1;
  1.1239 +   }
  1.1240 +   else
  1.1241 +   {
  1.1242 +      png_warning(png_ptr, "Unknown filter heuristic method");
  1.1243 +      return 0;
  1.1244 +   }
  1.1245 +}
  1.1246 +
  1.1247 +/* Provide floating and fixed point APIs */
  1.1248 +#ifdef PNG_FLOATING_POINT_SUPPORTED
  1.1249 +void PNGAPI
  1.1250 +png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
  1.1251 +    int num_weights, png_const_doublep filter_weights,
  1.1252 +    png_const_doublep filter_costs)
  1.1253 +{
  1.1254 +   png_debug(1, "in png_set_filter_heuristics");
  1.1255 +
  1.1256 +   /* The internal API allocates all the arrays and ensures that the elements of
  1.1257 +    * those arrays are set to the default value.
  1.1258 +    */
  1.1259 +   if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
  1.1260 +      return;
  1.1261 +
  1.1262 +   /* If using the weighted method copy in the weights. */
  1.1263 +   if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  1.1264 +   {
  1.1265 +      int i;
  1.1266 +      for (i = 0; i < num_weights; i++)
  1.1267 +      {
  1.1268 +         if (filter_weights[i] <= 0.0)
  1.1269 +         {
  1.1270 +            png_ptr->inv_filter_weights[i] =
  1.1271 +            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
  1.1272 +         }
  1.1273 +
  1.1274 +         else
  1.1275 +         {
  1.1276 +            png_ptr->inv_filter_weights[i] =
  1.1277 +                (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
  1.1278 +
  1.1279 +            png_ptr->filter_weights[i] =
  1.1280 +                (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
  1.1281 +         }
  1.1282 +      }
  1.1283 +
  1.1284 +      /* Here is where we set the relative costs of the different filters.  We
  1.1285 +       * should take the desired compression level into account when setting
  1.1286 +       * the costs, so that Paeth, for instance, has a high relative cost at low
  1.1287 +       * compression levels, while it has a lower relative cost at higher
  1.1288 +       * compression settings.  The filter types are in order of increasing
  1.1289 +       * relative cost, so it would be possible to do this with an algorithm.
  1.1290 +       */
  1.1291 +      for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
  1.1292 +      {
  1.1293 +         png_ptr->inv_filter_costs[i] =
  1.1294 +             (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
  1.1295 +
  1.1296 +         png_ptr->filter_costs[i] =
  1.1297 +             (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
  1.1298 +      }
  1.1299 +   }
  1.1300 +}
  1.1301 +#endif /* FLOATING_POINT */
  1.1302 +
  1.1303 +#ifdef PNG_FIXED_POINT_SUPPORTED
  1.1304 +void PNGAPI
  1.1305 +png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
  1.1306 +    int num_weights, png_const_fixed_point_p filter_weights,
  1.1307 +    png_const_fixed_point_p filter_costs)
  1.1308 +{
  1.1309 +   png_debug(1, "in png_set_filter_heuristics_fixed");
  1.1310 +
  1.1311 +   /* The internal API allocates all the arrays and ensures that the elements of
  1.1312 +    * those arrays are set to the default value.
  1.1313 +    */
  1.1314 +   if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
  1.1315 +      return;
  1.1316 +
  1.1317 +   /* If using the weighted method copy in the weights. */
  1.1318 +   if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  1.1319 +   {
  1.1320 +      int i;
  1.1321 +      for (i = 0; i < num_weights; i++)
  1.1322 +      {
  1.1323 +         if (filter_weights[i] <= 0)
  1.1324 +         {
  1.1325 +            png_ptr->inv_filter_weights[i] =
  1.1326 +            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
  1.1327 +         }
  1.1328 +
  1.1329 +         else
  1.1330 +         {
  1.1331 +            png_ptr->inv_filter_weights[i] = (png_uint_16)
  1.1332 +               ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
  1.1333 +
  1.1334 +            png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
  1.1335 +               PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
  1.1336 +         }
  1.1337 +      }
  1.1338 +
  1.1339 +      /* Here is where we set the relative costs of the different filters.  We
  1.1340 +       * should take the desired compression level into account when setting
  1.1341 +       * the costs, so that Paeth, for instance, has a high relative cost at low
  1.1342 +       * compression levels, while it has a lower relative cost at higher
  1.1343 +       * compression settings.  The filter types are in order of increasing
  1.1344 +       * relative cost, so it would be possible to do this with an algorithm.
  1.1345 +       */
  1.1346 +      for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
  1.1347 +         if (filter_costs[i] >= PNG_FP_1)
  1.1348 +      {
  1.1349 +         png_uint_32 tmp;
  1.1350 +
  1.1351 +         /* Use a 32 bit unsigned temporary here because otherwise the
  1.1352 +          * intermediate value will be a 32 bit *signed* integer (ANSI rules)
  1.1353 +          * and this will get the wrong answer on division.
  1.1354 +          */
  1.1355 +         tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2);
  1.1356 +         tmp /= filter_costs[i];
  1.1357 +
  1.1358 +         png_ptr->inv_filter_costs[i] = (png_uint_16)tmp;
  1.1359 +
  1.1360 +         tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF;
  1.1361 +         tmp /= PNG_FP_1;
  1.1362 +
  1.1363 +         png_ptr->filter_costs[i] = (png_uint_16)tmp;
  1.1364 +      }
  1.1365 +   }
  1.1366 +}
  1.1367 +#endif /* FIXED_POINT */
  1.1368 +#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
  1.1369 +
  1.1370 +void PNGAPI
  1.1371 +png_set_compression_level(png_structrp png_ptr, int level)
  1.1372 +{
  1.1373 +   png_debug(1, "in png_set_compression_level");
  1.1374 +
  1.1375 +   if (png_ptr == NULL)
  1.1376 +      return;
  1.1377 +
  1.1378 +   png_ptr->zlib_level = level;
  1.1379 +}
  1.1380 +
  1.1381 +void PNGAPI
  1.1382 +png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
  1.1383 +{
  1.1384 +   png_debug(1, "in png_set_compression_mem_level");
  1.1385 +
  1.1386 +   if (png_ptr == NULL)
  1.1387 +      return;
  1.1388 +
  1.1389 +   png_ptr->zlib_mem_level = mem_level;
  1.1390 +}
  1.1391 +
  1.1392 +void PNGAPI
  1.1393 +png_set_compression_strategy(png_structrp png_ptr, int strategy)
  1.1394 +{
  1.1395 +   png_debug(1, "in png_set_compression_strategy");
  1.1396 +
  1.1397 +   if (png_ptr == NULL)
  1.1398 +      return;
  1.1399 +
  1.1400 +   /* The flag setting here prevents the libpng dynamic selection of strategy.
  1.1401 +    */
  1.1402 +   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
  1.1403 +   png_ptr->zlib_strategy = strategy;
  1.1404 +}
  1.1405 +
  1.1406 +/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
  1.1407 + * smaller value of window_bits if it can do so safely.
  1.1408 + */
  1.1409 +void PNGAPI
  1.1410 +png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
  1.1411 +{
  1.1412 +   if (png_ptr == NULL)
  1.1413 +      return;
  1.1414 +
  1.1415 +   /* Prior to 1.6.0 this would warn but then set the window_bits value, this
  1.1416 +    * meant that negative window bits values could be selected which would cause
  1.1417 +    * libpng to write a non-standard PNG file with raw deflate or gzip
  1.1418 +    * compressed IDAT or ancillary chunks.  Such files can be read and there is
  1.1419 +    * no warning on read, so this seems like a very bad idea.
  1.1420 +    */
  1.1421 +   if (window_bits > 15)
  1.1422 +   {
  1.1423 +      png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
  1.1424 +      window_bits = 15;
  1.1425 +   }
  1.1426 +
  1.1427 +   else if (window_bits < 8)
  1.1428 +   {
  1.1429 +      png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
  1.1430 +      window_bits = 8;
  1.1431 +   }
  1.1432 +
  1.1433 +   png_ptr->zlib_window_bits = window_bits;
  1.1434 +}
  1.1435 +
  1.1436 +void PNGAPI
  1.1437 +png_set_compression_method(png_structrp png_ptr, int method)
  1.1438 +{
  1.1439 +   png_debug(1, "in png_set_compression_method");
  1.1440 +
  1.1441 +   if (png_ptr == NULL)
  1.1442 +      return;
  1.1443 +
  1.1444 +   /* This would produce an invalid PNG file if it worked, but it doesn't and
  1.1445 +    * deflate will fault it, so it is harmless to just warn here.
  1.1446 +    */
  1.1447 +   if (method != 8)
  1.1448 +      png_warning(png_ptr, "Only compression method 8 is supported by PNG");
  1.1449 +
  1.1450 +   png_ptr->zlib_method = method;
  1.1451 +}
  1.1452 +
  1.1453 +/* The following were added to libpng-1.5.4 */
  1.1454 +#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
  1.1455 +void PNGAPI
  1.1456 +png_set_text_compression_level(png_structrp png_ptr, int level)
  1.1457 +{
  1.1458 +   png_debug(1, "in png_set_text_compression_level");
  1.1459 +
  1.1460 +   if (png_ptr == NULL)
  1.1461 +      return;
  1.1462 +
  1.1463 +   png_ptr->zlib_text_level = level;
  1.1464 +}
  1.1465 +
  1.1466 +void PNGAPI
  1.1467 +png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)
  1.1468 +{
  1.1469 +   png_debug(1, "in png_set_text_compression_mem_level");
  1.1470 +
  1.1471 +   if (png_ptr == NULL)
  1.1472 +      return;
  1.1473 +
  1.1474 +   png_ptr->zlib_text_mem_level = mem_level;
  1.1475 +}
  1.1476 +
  1.1477 +void PNGAPI
  1.1478 +png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
  1.1479 +{
  1.1480 +   png_debug(1, "in png_set_text_compression_strategy");
  1.1481 +
  1.1482 +   if (png_ptr == NULL)
  1.1483 +      return;
  1.1484 +
  1.1485 +   png_ptr->zlib_text_strategy = strategy;
  1.1486 +}
  1.1487 +
  1.1488 +/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
  1.1489 + * smaller value of window_bits if it can do so safely.
  1.1490 + */
  1.1491 +void PNGAPI
  1.1492 +png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
  1.1493 +{
  1.1494 +   if (png_ptr == NULL)
  1.1495 +      return;
  1.1496 +
  1.1497 +   if (window_bits > 15)
  1.1498 +   {
  1.1499 +      png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
  1.1500 +      window_bits = 15;
  1.1501 +   }
  1.1502 +
  1.1503 +   else if (window_bits < 8)
  1.1504 +   {
  1.1505 +      png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
  1.1506 +      window_bits = 8;
  1.1507 +   }
  1.1508 +
  1.1509 +   png_ptr->zlib_text_window_bits = window_bits;
  1.1510 +}
  1.1511 +
  1.1512 +void PNGAPI
  1.1513 +png_set_text_compression_method(png_structrp png_ptr, int method)
  1.1514 +{
  1.1515 +   png_debug(1, "in png_set_text_compression_method");
  1.1516 +
  1.1517 +   if (png_ptr == NULL)
  1.1518 +      return;
  1.1519 +
  1.1520 +   if (method != 8)
  1.1521 +      png_warning(png_ptr, "Only compression method 8 is supported by PNG");
  1.1522 +
  1.1523 +   png_ptr->zlib_text_method = method;
  1.1524 +}
  1.1525 +#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
  1.1526 +/* end of API added to libpng-1.5.4 */
  1.1527 +
  1.1528 +void PNGAPI
  1.1529 +png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
  1.1530 +{
  1.1531 +   if (png_ptr == NULL)
  1.1532 +      return;
  1.1533 +
  1.1534 +   png_ptr->write_row_fn = write_row_fn;
  1.1535 +}
  1.1536 +
  1.1537 +#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
  1.1538 +void PNGAPI
  1.1539 +png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
  1.1540 +    write_user_transform_fn)
  1.1541 +{
  1.1542 +   png_debug(1, "in png_set_write_user_transform_fn");
  1.1543 +
  1.1544 +   if (png_ptr == NULL)
  1.1545 +      return;
  1.1546 +
  1.1547 +   png_ptr->transformations |= PNG_USER_TRANSFORM;
  1.1548 +   png_ptr->write_user_transform_fn = write_user_transform_fn;
  1.1549 +}
  1.1550 +#endif
  1.1551 +
  1.1552 +
  1.1553 +#ifdef PNG_INFO_IMAGE_SUPPORTED
  1.1554 +void PNGAPI
  1.1555 +png_write_png(png_structrp png_ptr, png_inforp info_ptr,
  1.1556 +    int transforms, voidp params)
  1.1557 +{
  1.1558 +   if (png_ptr == NULL || info_ptr == NULL)
  1.1559 +      return;
  1.1560 +
  1.1561 +   if ((info_ptr->valid & PNG_INFO_IDAT) == 0)
  1.1562 +   {
  1.1563 +      png_app_error(png_ptr, "no rows for png_write_image to write");
  1.1564 +      return;
  1.1565 +   }
  1.1566 +
  1.1567 +   /* Write the file header information. */
  1.1568 +   png_write_info(png_ptr, info_ptr);
  1.1569 +
  1.1570 +   /* ------ these transformations don't touch the info structure ------- */
  1.1571 +
  1.1572 +   /* Invert monochrome pixels */
  1.1573 +   if (transforms & PNG_TRANSFORM_INVERT_MONO)
  1.1574 +#ifdef PNG_WRITE_INVERT_SUPPORTED
  1.1575 +      png_set_invert_mono(png_ptr);
  1.1576 +#else
  1.1577 +      png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
  1.1578 +#endif
  1.1579 +
  1.1580 +   /* Shift the pixels up to a legal bit depth and fill in
  1.1581 +    * as appropriate to correctly scale the image.
  1.1582 +    */
  1.1583 +   if (transforms & PNG_TRANSFORM_SHIFT)
  1.1584 +#ifdef PNG_WRITE_SHIFT_SUPPORTED
  1.1585 +      if (info_ptr->valid & PNG_INFO_sBIT)
  1.1586 +         png_set_shift(png_ptr, &info_ptr->sig_bit);
  1.1587 +#else
  1.1588 +      png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
  1.1589 +#endif
  1.1590 +
  1.1591 +   /* Pack pixels into bytes */
  1.1592 +   if (transforms & PNG_TRANSFORM_PACKING)
  1.1593 +#ifdef PNG_WRITE_PACK_SUPPORTED
  1.1594 +      png_set_packing(png_ptr);
  1.1595 +#else
  1.1596 +      png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
  1.1597 +#endif
  1.1598 +
  1.1599 +   /* Swap location of alpha bytes from ARGB to RGBA */
  1.1600 +   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
  1.1601 +#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
  1.1602 +      png_set_swap_alpha(png_ptr);
  1.1603 +#else
  1.1604 +      png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
  1.1605 +#endif
  1.1606 +
  1.1607 +   /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into
  1.1608 +    * RGB, note that the code expects the input color type to be G or RGB; no
  1.1609 +    * alpha channel.
  1.1610 +    */
  1.1611 +   if (transforms &
  1.1612 +      (PNG_TRANSFORM_STRIP_FILLER_AFTER|PNG_TRANSFORM_STRIP_FILLER_BEFORE))
  1.1613 +   {
  1.1614 +#ifdef PNG_WRITE_FILLER_SUPPORTED
  1.1615 +      if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
  1.1616 +      {
  1.1617 +         if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
  1.1618 +            png_app_error(png_ptr,
  1.1619 +               "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported");
  1.1620 +
  1.1621 +         /* Continue if ignored - this is the pre-1.6.10 behavior */
  1.1622 +         png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
  1.1623 +      }
  1.1624 +
  1.1625 +      else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
  1.1626 +         png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
  1.1627 +#else
  1.1628 +      png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported");
  1.1629 +#endif
  1.1630 +   }
  1.1631 +
  1.1632 +   /* Flip BGR pixels to RGB */
  1.1633 +   if (transforms & PNG_TRANSFORM_BGR)
  1.1634 +#ifdef PNG_WRITE_BGR_SUPPORTED
  1.1635 +      png_set_bgr(png_ptr);
  1.1636 +#else
  1.1637 +      png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
  1.1638 +#endif
  1.1639 +
  1.1640 +   /* Swap bytes of 16-bit files to most significant byte first */
  1.1641 +   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
  1.1642 +#ifdef PNG_WRITE_SWAP_SUPPORTED
  1.1643 +      png_set_swap(png_ptr);
  1.1644 +#else
  1.1645 +      png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
  1.1646 +#endif
  1.1647 +
  1.1648 +   /* Swap bits of 1, 2, 4 bit packed pixel formats */
  1.1649 +   if (transforms & PNG_TRANSFORM_PACKSWAP)
  1.1650 +#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
  1.1651 +      png_set_packswap(png_ptr);
  1.1652 +#else
  1.1653 +      png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
  1.1654 +#endif
  1.1655 +
  1.1656 +   /* Invert the alpha channel from opacity to transparency */
  1.1657 +   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
  1.1658 +#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
  1.1659 +      png_set_invert_alpha(png_ptr);
  1.1660 +#else
  1.1661 +      png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
  1.1662 +#endif
  1.1663 +
  1.1664 +   /* ----------------------- end of transformations ------------------- */
  1.1665 +
  1.1666 +   /* Write the bits */
  1.1667 +   png_write_image(png_ptr, info_ptr->row_pointers);
  1.1668 +
  1.1669 +   /* It is REQUIRED to call this to finish writing the rest of the file */
  1.1670 +   png_write_end(png_ptr, info_ptr);
  1.1671 +
  1.1672 +   PNG_UNUSED(params)
  1.1673 +}
  1.1674 +#endif
  1.1675 +
  1.1676 +
  1.1677 +#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
  1.1678 +#ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */
  1.1679 +/* Initialize the write structure - general purpose utility. */
  1.1680 +static int
  1.1681 +png_image_write_init(png_imagep image)
  1.1682 +{
  1.1683 +   png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
  1.1684 +          png_safe_error, png_safe_warning);
  1.1685 +
  1.1686 +   if (png_ptr != NULL)
  1.1687 +   {
  1.1688 +      png_infop info_ptr = png_create_info_struct(png_ptr);
  1.1689 +
  1.1690 +      if (info_ptr != NULL)
  1.1691 +      {
  1.1692 +         png_controlp control = png_voidcast(png_controlp,
  1.1693 +            png_malloc_warn(png_ptr, (sizeof *control)));
  1.1694 +
  1.1695 +         if (control != NULL)
  1.1696 +         {
  1.1697 +            memset(control, 0, (sizeof *control));
  1.1698 +
  1.1699 +            control->png_ptr = png_ptr;
  1.1700 +            control->info_ptr = info_ptr;
  1.1701 +            control->for_write = 1;
  1.1702 +
  1.1703 +            image->opaque = control;
  1.1704 +            return 1;
  1.1705 +         }
  1.1706 +
  1.1707 +         /* Error clean up */
  1.1708 +         png_destroy_info_struct(png_ptr, &info_ptr);
  1.1709 +      }
  1.1710 +
  1.1711 +      png_destroy_write_struct(&png_ptr, NULL);
  1.1712 +   }
  1.1713 +
  1.1714 +   return png_image_error(image, "png_image_write_: out of memory");
  1.1715 +}
  1.1716 +
  1.1717 +/* Arguments to png_image_write_main: */
  1.1718 +typedef struct
  1.1719 +{
  1.1720 +   /* Arguments: */
  1.1721 +   png_imagep      image;
  1.1722 +   png_const_voidp buffer;
  1.1723 +   png_int_32      row_stride;
  1.1724 +   png_const_voidp colormap;
  1.1725 +   int             convert_to_8bit;
  1.1726 +   /* Local variables: */
  1.1727 +   png_const_voidp first_row;
  1.1728 +   ptrdiff_t       row_bytes;
  1.1729 +   png_voidp       local_row;
  1.1730 +} png_image_write_control;
  1.1731 +
  1.1732 +/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
  1.1733 + * do any necessary byte swapping.  The component order is defined by the
  1.1734 + * png_image format value.
  1.1735 + */
  1.1736 +static int
  1.1737 +png_write_image_16bit(png_voidp argument)
  1.1738 +{
  1.1739 +   png_image_write_control *display = png_voidcast(png_image_write_control*,
  1.1740 +      argument);
  1.1741 +   png_imagep image = display->image;
  1.1742 +   png_structrp png_ptr = image->opaque->png_ptr;
  1.1743 +
  1.1744 +   png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
  1.1745 +      display->first_row);
  1.1746 +   png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
  1.1747 +   png_uint_16p row_end;
  1.1748 +   const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
  1.1749 +   int aindex = 0;
  1.1750 +   png_uint_32 y = image->height;
  1.1751 +
  1.1752 +   if (image->format & PNG_FORMAT_FLAG_ALPHA)
  1.1753 +   {
  1.1754 +#     ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
  1.1755 +         if (image->format & PNG_FORMAT_FLAG_AFIRST)
  1.1756 +         {
  1.1757 +            aindex = -1;
  1.1758 +            ++input_row; /* To point to the first component */
  1.1759 +            ++output_row;
  1.1760 +         }
  1.1761 +
  1.1762 +         else
  1.1763 +#     endif
  1.1764 +         aindex = channels;
  1.1765 +   }
  1.1766 +
  1.1767 +   else
  1.1768 +      png_error(png_ptr, "png_write_image: internal call error");
  1.1769 +
  1.1770 +   /* Work out the output row end and count over this, note that the increment
  1.1771 +    * above to 'row' means that row_end can actually be beyond the end of the
  1.1772 +    * row; this is correct.
  1.1773 +    */
  1.1774 +   row_end = output_row + image->width * (channels+1);
  1.1775 +
  1.1776 +   while (y-- > 0)
  1.1777 +   {
  1.1778 +      png_const_uint_16p in_ptr = input_row;
  1.1779 +      png_uint_16p out_ptr = output_row;
  1.1780 +
  1.1781 +      while (out_ptr < row_end)
  1.1782 +      {
  1.1783 +         const png_uint_16 alpha = in_ptr[aindex];
  1.1784 +         png_uint_32 reciprocal = 0;
  1.1785 +         int c;
  1.1786 +
  1.1787 +         out_ptr[aindex] = alpha;
  1.1788 +
  1.1789 +         /* Calculate a reciprocal.  The correct calculation is simply
  1.1790 +          * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
  1.1791 +          * allows correct rounding by adding .5 before the shift.  'reciprocal'
  1.1792 +          * is only initialized when required.
  1.1793 +          */
  1.1794 +         if (alpha > 0 && alpha < 65535)
  1.1795 +            reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
  1.1796 +
  1.1797 +         c = channels;
  1.1798 +         do /* always at least one channel */
  1.1799 +         {
  1.1800 +            png_uint_16 component = *in_ptr++;
  1.1801 +
  1.1802 +            /* The following gives 65535 for an alpha of 0, which is fine,
  1.1803 +             * otherwise if 0/0 is represented as some other value there is more
  1.1804 +             * likely to be a discontinuity which will probably damage
  1.1805 +             * compression when moving from a fully transparent area to a
  1.1806 +             * nearly transparent one.  (The assumption here is that opaque
  1.1807 +             * areas tend not to be 0 intensity.)
  1.1808 +             */
  1.1809 +            if (component >= alpha)
  1.1810 +               component = 65535;
  1.1811 +
  1.1812 +            /* component<alpha, so component/alpha is less than one and
  1.1813 +             * component*reciprocal is less than 2^31.
  1.1814 +             */
  1.1815 +            else if (component > 0 && alpha < 65535)
  1.1816 +            {
  1.1817 +               png_uint_32 calc = component * reciprocal;
  1.1818 +               calc += 16384; /* round to nearest */
  1.1819 +               component = (png_uint_16)(calc >> 15);
  1.1820 +            }
  1.1821 +
  1.1822 +            *out_ptr++ = component;
  1.1823 +         }
  1.1824 +         while (--c > 0);
  1.1825 +
  1.1826 +         /* Skip to next component (skip the intervening alpha channel) */
  1.1827 +         ++in_ptr;
  1.1828 +         ++out_ptr;
  1.1829 +      }
  1.1830 +
  1.1831 +      png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
  1.1832 +      input_row += display->row_bytes/(sizeof (png_uint_16));
  1.1833 +   }
  1.1834 +
  1.1835 +   return 1;
  1.1836 +}
  1.1837 +
  1.1838 +/* Given 16-bit input (1 to 4 channels) write 8-bit output.  If an alpha channel
  1.1839 + * is present it must be removed from the components, the components are then
  1.1840 + * written in sRGB encoding.  No components are added or removed.
  1.1841 + *
  1.1842 + * Calculate an alpha reciprocal to reverse pre-multiplication.  As above the
  1.1843 + * calculation can be done to 15 bits of accuracy; however, the output needs to
  1.1844 + * be scaled in the range 0..255*65535, so include that scaling here.
  1.1845 + */
  1.1846 +#define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+(alpha>>1))/alpha)
  1.1847 +
  1.1848 +static png_byte
  1.1849 +png_unpremultiply(png_uint_32 component, png_uint_32 alpha,
  1.1850 +   png_uint_32 reciprocal/*from the above macro*/)
  1.1851 +{
  1.1852 +   /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
  1.1853 +    * is represented as some other value there is more likely to be a
  1.1854 +    * discontinuity which will probably damage compression when moving from a
  1.1855 +    * fully transparent area to a nearly transparent one.  (The assumption here
  1.1856 +    * is that opaque areas tend not to be 0 intensity.)
  1.1857 +    *
  1.1858 +    * There is a rounding problem here; if alpha is less than 128 it will end up
  1.1859 +    * as 0 when scaled to 8 bits.  To avoid introducing spurious colors into the
  1.1860 +    * output change for this too.
  1.1861 +    */
  1.1862 +   if (component >= alpha || alpha < 128)
  1.1863 +      return 255;
  1.1864 +
  1.1865 +   /* component<alpha, so component/alpha is less than one and
  1.1866 +    * component*reciprocal is less than 2^31.
  1.1867 +    */
  1.1868 +   else if (component > 0)
  1.1869 +   {
  1.1870 +      /* The test is that alpha/257 (rounded) is less than 255, the first value
  1.1871 +       * that becomes 255 is 65407.
  1.1872 +       * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
  1.1873 +       * be exact!)  [Could also test reciprocal != 0]
  1.1874 +       */
  1.1875 +      if (alpha < 65407)
  1.1876 +      {
  1.1877 +         component *= reciprocal;
  1.1878 +         component += 64; /* round to nearest */
  1.1879 +         component >>= 7;
  1.1880 +      }
  1.1881 +
  1.1882 +      else
  1.1883 +         component *= 255;
  1.1884 +
  1.1885 +      /* Convert the component to sRGB. */
  1.1886 +      return (png_byte)PNG_sRGB_FROM_LINEAR(component);
  1.1887 +   }
  1.1888 +
  1.1889 +   else
  1.1890 +      return 0;
  1.1891 +}
  1.1892 +
  1.1893 +static int
  1.1894 +png_write_image_8bit(png_voidp argument)
  1.1895 +{
  1.1896 +   png_image_write_control *display = png_voidcast(png_image_write_control*,
  1.1897 +      argument);
  1.1898 +   png_imagep image = display->image;
  1.1899 +   png_structrp png_ptr = image->opaque->png_ptr;
  1.1900 +
  1.1901 +   png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
  1.1902 +      display->first_row);
  1.1903 +   png_bytep output_row = png_voidcast(png_bytep, display->local_row);
  1.1904 +   png_uint_32 y = image->height;
  1.1905 +   const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
  1.1906 +
  1.1907 +   if (image->format & PNG_FORMAT_FLAG_ALPHA)
  1.1908 +   {
  1.1909 +      png_bytep row_end;
  1.1910 +      int aindex;
  1.1911 +
  1.1912 +#     ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
  1.1913 +         if (image->format & PNG_FORMAT_FLAG_AFIRST)
  1.1914 +         {
  1.1915 +            aindex = -1;
  1.1916 +            ++input_row; /* To point to the first component */
  1.1917 +            ++output_row;
  1.1918 +         }
  1.1919 +
  1.1920 +         else
  1.1921 +#     endif
  1.1922 +         aindex = channels;
  1.1923 +
  1.1924 +      /* Use row_end in place of a loop counter: */
  1.1925 +      row_end = output_row + image->width * (channels+1);
  1.1926 +
  1.1927 +      while (y-- > 0)
  1.1928 +      {
  1.1929 +         png_const_uint_16p in_ptr = input_row;
  1.1930 +         png_bytep out_ptr = output_row;
  1.1931 +
  1.1932 +         while (out_ptr < row_end)
  1.1933 +         {
  1.1934 +            png_uint_16 alpha = in_ptr[aindex];
  1.1935 +            png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
  1.1936 +            png_uint_32 reciprocal = 0;
  1.1937 +            int c;
  1.1938 +
  1.1939 +            /* Scale and write the alpha channel. */
  1.1940 +            out_ptr[aindex] = alphabyte;
  1.1941 +
  1.1942 +            if (alphabyte > 0 && alphabyte < 255)
  1.1943 +               reciprocal = UNP_RECIPROCAL(alpha);
  1.1944 +
  1.1945 +            c = channels;
  1.1946 +            do /* always at least one channel */
  1.1947 +               *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
  1.1948 +            while (--c > 0);
  1.1949 +
  1.1950 +            /* Skip to next component (skip the intervening alpha channel) */
  1.1951 +            ++in_ptr;
  1.1952 +            ++out_ptr;
  1.1953 +         } /* while out_ptr < row_end */
  1.1954 +
  1.1955 +         png_write_row(png_ptr, png_voidcast(png_const_bytep,
  1.1956 +            display->local_row));
  1.1957 +         input_row += display->row_bytes/(sizeof (png_uint_16));
  1.1958 +      } /* while y */
  1.1959 +   }
  1.1960 +
  1.1961 +   else
  1.1962 +   {
  1.1963 +      /* No alpha channel, so the row_end really is the end of the row and it
  1.1964 +       * is sufficient to loop over the components one by one.
  1.1965 +       */
  1.1966 +      png_bytep row_end = output_row + image->width * channels;
  1.1967 +
  1.1968 +      while (y-- > 0)
  1.1969 +      {
  1.1970 +         png_const_uint_16p in_ptr = input_row;
  1.1971 +         png_bytep out_ptr = output_row;
  1.1972 +
  1.1973 +         while (out_ptr < row_end)
  1.1974 +         {
  1.1975 +            png_uint_32 component = *in_ptr++;
  1.1976 +
  1.1977 +            component *= 255;
  1.1978 +            *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
  1.1979 +         }
  1.1980 +
  1.1981 +         png_write_row(png_ptr, output_row);
  1.1982 +         input_row += display->row_bytes/(sizeof (png_uint_16));
  1.1983 +      }
  1.1984 +   }
  1.1985 +
  1.1986 +   return 1;
  1.1987 +}
  1.1988 +
  1.1989 +static void
  1.1990 +png_image_set_PLTE(png_image_write_control *display)
  1.1991 +{
  1.1992 +   const png_imagep image = display->image;
  1.1993 +   const void *cmap = display->colormap;
  1.1994 +   const int entries = image->colormap_entries > 256 ? 256 :
  1.1995 +      (int)image->colormap_entries;
  1.1996 +
  1.1997 +   /* NOTE: the caller must check for cmap != NULL and entries != 0 */
  1.1998 +   const png_uint_32 format = image->format;
  1.1999 +   const int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
  1.2000 +
  1.2001 +#  if defined(PNG_FORMAT_BGR_SUPPORTED) &&\
  1.2002 +      defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)
  1.2003 +      const int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
  1.2004 +         (format & PNG_FORMAT_FLAG_ALPHA) != 0;
  1.2005 +#  else
  1.2006 +#     define afirst 0
  1.2007 +#  endif
  1.2008 +
  1.2009 +#  ifdef PNG_FORMAT_BGR_SUPPORTED
  1.2010 +      const int bgr = (format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
  1.2011 +#  else
  1.2012 +#     define bgr 0
  1.2013 +#  endif
  1.2014 +
  1.2015 +   int i, num_trans;
  1.2016 +   png_color palette[256];
  1.2017 +   png_byte tRNS[256];
  1.2018 +
  1.2019 +   memset(tRNS, 255, (sizeof tRNS));
  1.2020 +   memset(palette, 0, (sizeof palette));
  1.2021 +
  1.2022 +   for (i=num_trans=0; i<entries; ++i)
  1.2023 +   {
  1.2024 +      /* This gets automatically converted to sRGB with reversal of the
  1.2025 +       * pre-multiplication if the color-map has an alpha channel.
  1.2026 +       */
  1.2027 +      if (format & PNG_FORMAT_FLAG_LINEAR)
  1.2028 +      {
  1.2029 +         png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
  1.2030 +
  1.2031 +         entry += i * channels;
  1.2032 +
  1.2033 +         if (channels & 1) /* no alpha */
  1.2034 +         {
  1.2035 +            if (channels >= 3) /* RGB */
  1.2036 +            {
  1.2037 +               palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
  1.2038 +                  entry[(2 ^ bgr)]);
  1.2039 +               palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
  1.2040 +                  entry[1]);
  1.2041 +               palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
  1.2042 +                  entry[bgr]);
  1.2043 +            }
  1.2044 +
  1.2045 +            else /* Gray */
  1.2046 +               palette[i].blue = palette[i].red = palette[i].green =
  1.2047 +                  (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);
  1.2048 +         }
  1.2049 +
  1.2050 +         else /* alpha */
  1.2051 +         {
  1.2052 +            png_uint_16 alpha = entry[afirst ? 0 : channels-1];
  1.2053 +            png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
  1.2054 +            png_uint_32 reciprocal = 0;
  1.2055 +
  1.2056 +            /* Calculate a reciprocal, as in the png_write_image_8bit code above
  1.2057 +             * this is designed to produce a value scaled to 255*65535 when
  1.2058 +             * divided by 128 (i.e. asr 7).
  1.2059 +             */
  1.2060 +            if (alphabyte > 0 && alphabyte < 255)
  1.2061 +               reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
  1.2062 +
  1.2063 +            tRNS[i] = alphabyte;
  1.2064 +            if (alphabyte < 255)
  1.2065 +               num_trans = i+1;
  1.2066 +
  1.2067 +            if (channels >= 3) /* RGB */
  1.2068 +            {
  1.2069 +               palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],
  1.2070 +                  alpha, reciprocal);
  1.2071 +               palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,
  1.2072 +                  reciprocal);
  1.2073 +               palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,
  1.2074 +                  reciprocal);
  1.2075 +            }
  1.2076 +
  1.2077 +            else /* gray */
  1.2078 +               palette[i].blue = palette[i].red = palette[i].green =
  1.2079 +                  png_unpremultiply(entry[afirst], alpha, reciprocal);
  1.2080 +         }
  1.2081 +      }
  1.2082 +
  1.2083 +      else /* Color-map has sRGB values */
  1.2084 +      {
  1.2085 +         png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
  1.2086 +
  1.2087 +         entry += i * channels;
  1.2088 +
  1.2089 +         switch (channels)
  1.2090 +         {
  1.2091 +            case 4:
  1.2092 +               tRNS[i] = entry[afirst ? 0 : 3];
  1.2093 +               if (tRNS[i] < 255)
  1.2094 +                  num_trans = i+1;
  1.2095 +               /* FALL THROUGH */
  1.2096 +            case 3:
  1.2097 +               palette[i].blue = entry[afirst + (2 ^ bgr)];
  1.2098 +               palette[i].green = entry[afirst + 1];
  1.2099 +               palette[i].red = entry[afirst + bgr];
  1.2100 +               break;
  1.2101 +
  1.2102 +            case 2:
  1.2103 +               tRNS[i] = entry[1 ^ afirst];
  1.2104 +               if (tRNS[i] < 255)
  1.2105 +                  num_trans = i+1;
  1.2106 +               /* FALL THROUGH */
  1.2107 +            case 1:
  1.2108 +               palette[i].blue = palette[i].red = palette[i].green =
  1.2109 +                  entry[afirst];
  1.2110 +               break;
  1.2111 +
  1.2112 +            default:
  1.2113 +               break;
  1.2114 +         }
  1.2115 +      }
  1.2116 +   }
  1.2117 +
  1.2118 +#  ifdef afirst
  1.2119 +#     undef afirst
  1.2120 +#  endif
  1.2121 +#  ifdef bgr
  1.2122 +#     undef bgr
  1.2123 +#  endif
  1.2124 +
  1.2125 +   png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,
  1.2126 +      entries);
  1.2127 +
  1.2128 +   if (num_trans > 0)
  1.2129 +      png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
  1.2130 +         num_trans, NULL);
  1.2131 +
  1.2132 +   image->colormap_entries = entries;
  1.2133 +}
  1.2134 +
  1.2135 +static int
  1.2136 +png_image_write_main(png_voidp argument)
  1.2137 +{
  1.2138 +   png_image_write_control *display = png_voidcast(png_image_write_control*,
  1.2139 +      argument);
  1.2140 +   png_imagep image = display->image;
  1.2141 +   png_structrp png_ptr = image->opaque->png_ptr;
  1.2142 +   png_inforp info_ptr = image->opaque->info_ptr;
  1.2143 +   png_uint_32 format = image->format;
  1.2144 +
  1.2145 +   int colormap = (format & PNG_FORMAT_FLAG_COLORMAP) != 0;
  1.2146 +   int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR) != 0; /* input */
  1.2147 +   int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA) != 0;
  1.2148 +   int write_16bit = linear && !colormap && !display->convert_to_8bit;
  1.2149 +
  1.2150 +#  ifdef PNG_BENIGN_ERRORS_SUPPORTED
  1.2151 +      /* Make sure we error out on any bad situation */
  1.2152 +      png_set_benign_errors(png_ptr, 0/*error*/);
  1.2153 +#  endif
  1.2154 +
  1.2155 +   /* Default the 'row_stride' parameter if required. */
  1.2156 +   if (display->row_stride == 0)
  1.2157 +      display->row_stride = PNG_IMAGE_ROW_STRIDE(*image);
  1.2158 +
  1.2159 +   /* Set the required transforms then write the rows in the correct order. */
  1.2160 +   if (format & PNG_FORMAT_FLAG_COLORMAP)
  1.2161 +   {
  1.2162 +      if (display->colormap != NULL && image->colormap_entries > 0)
  1.2163 +      {
  1.2164 +         png_uint_32 entries = image->colormap_entries;
  1.2165 +
  1.2166 +         png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
  1.2167 +            entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),
  1.2168 +            PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
  1.2169 +            PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  1.2170 +
  1.2171 +         png_image_set_PLTE(display);
  1.2172 +      }
  1.2173 +
  1.2174 +      else
  1.2175 +         png_error(image->opaque->png_ptr,
  1.2176 +            "no color-map for color-mapped image");
  1.2177 +   }
  1.2178 +
  1.2179 +   else
  1.2180 +      png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
  1.2181 +         write_16bit ? 16 : 8,
  1.2182 +         ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
  1.2183 +         ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
  1.2184 +         PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  1.2185 +
  1.2186 +   /* Counter-intuitively the data transformations must be called *after*
  1.2187 +    * png_write_info, not before as in the read code, but the 'set' functions
  1.2188 +    * must still be called before.  Just set the color space information, never
  1.2189 +    * write an interlaced image.
  1.2190 +    */
  1.2191 +
  1.2192 +   if (write_16bit)
  1.2193 +   {
  1.2194 +      /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
  1.2195 +      png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
  1.2196 +
  1.2197 +      if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB))
  1.2198 +         png_set_cHRM_fixed(png_ptr, info_ptr,
  1.2199 +            /* color      x       y */
  1.2200 +            /* white */ 31270, 32900,
  1.2201 +            /* red   */ 64000, 33000,
  1.2202 +            /* green */ 30000, 60000,
  1.2203 +            /* blue  */ 15000,  6000
  1.2204 +         );
  1.2205 +   }
  1.2206 +
  1.2207 +   else if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB))
  1.2208 +      png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
  1.2209 +
  1.2210 +   /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
  1.2211 +    * space must still be gamma encoded.
  1.2212 +    */
  1.2213 +   else
  1.2214 +      png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
  1.2215 +
  1.2216 +   /* Write the file header. */
  1.2217 +   png_write_info(png_ptr, info_ptr);
  1.2218 +
  1.2219 +   /* Now set up the data transformations (*after* the header is written),
  1.2220 +    * remove the handled transformations from the 'format' flags for checking.
  1.2221 +    *
  1.2222 +    * First check for a little endian system if writing 16 bit files.
  1.2223 +    */
  1.2224 +   if (write_16bit)
  1.2225 +   {
  1.2226 +      PNG_CONST png_uint_16 le = 0x0001;
  1.2227 +
  1.2228 +      if (*(png_const_bytep)&le)
  1.2229 +         png_set_swap(png_ptr);
  1.2230 +   }
  1.2231 +
  1.2232 +#  ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
  1.2233 +      if (format & PNG_FORMAT_FLAG_BGR)
  1.2234 +      {
  1.2235 +         if (!colormap && (format & PNG_FORMAT_FLAG_COLOR) != 0)
  1.2236 +            png_set_bgr(png_ptr);
  1.2237 +         format &= ~PNG_FORMAT_FLAG_BGR;
  1.2238 +      }
  1.2239 +#  endif
  1.2240 +
  1.2241 +#  ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
  1.2242 +      if (format & PNG_FORMAT_FLAG_AFIRST)
  1.2243 +      {
  1.2244 +         if (!colormap && (format & PNG_FORMAT_FLAG_ALPHA) != 0)
  1.2245 +            png_set_swap_alpha(png_ptr);
  1.2246 +         format &= ~PNG_FORMAT_FLAG_AFIRST;
  1.2247 +      }
  1.2248 +#  endif
  1.2249 +
  1.2250 +   /* If there are 16 or fewer color-map entries we wrote a lower bit depth
  1.2251 +    * above, but the application data is still byte packed.
  1.2252 +    */
  1.2253 +   if (colormap && image->colormap_entries <= 16)
  1.2254 +      png_set_packing(png_ptr);
  1.2255 +
  1.2256 +   /* That should have handled all (both) the transforms. */
  1.2257 +   if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
  1.2258 +         PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)
  1.2259 +      png_error(png_ptr, "png_write_image: unsupported transformation");
  1.2260 +
  1.2261 +   {
  1.2262 +      png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
  1.2263 +      ptrdiff_t row_bytes = display->row_stride;
  1.2264 +
  1.2265 +      if (linear)
  1.2266 +         row_bytes *= (sizeof (png_uint_16));
  1.2267 +
  1.2268 +      if (row_bytes < 0)
  1.2269 +         row += (image->height-1) * (-row_bytes);
  1.2270 +
  1.2271 +      display->first_row = row;
  1.2272 +      display->row_bytes = row_bytes;
  1.2273 +   }
  1.2274 +
  1.2275 +   /* Apply 'fast' options if the flag is set. */
  1.2276 +   if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)
  1.2277 +   {
  1.2278 +      png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);
  1.2279 +      /* NOTE: determined by experiment using pngstest, this reflects some
  1.2280 +       * balance between the time to write the image once and the time to read
  1.2281 +       * it about 50 times.  The speed-up in pngstest was about 10-20% of the
  1.2282 +       * total (user) time on a heavily loaded system.
  1.2283 +       */
  1.2284 +      png_set_compression_level(png_ptr, 3);
  1.2285 +   }
  1.2286 +
  1.2287 +   /* Check for the cases that currently require a pre-transform on the row
  1.2288 +    * before it is written.  This only applies when the input is 16-bit and
  1.2289 +    * either there is an alpha channel or it is converted to 8-bit.
  1.2290 +    */
  1.2291 +   if ((linear && alpha) || (!colormap && display->convert_to_8bit))
  1.2292 +   {
  1.2293 +      png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
  1.2294 +         png_get_rowbytes(png_ptr, info_ptr)));
  1.2295 +      int result;
  1.2296 +
  1.2297 +      display->local_row = row;
  1.2298 +      if (write_16bit)
  1.2299 +         result = png_safe_execute(image, png_write_image_16bit, display);
  1.2300 +      else
  1.2301 +         result = png_safe_execute(image, png_write_image_8bit, display);
  1.2302 +      display->local_row = NULL;
  1.2303 +
  1.2304 +      png_free(png_ptr, row);
  1.2305 +
  1.2306 +      /* Skip the 'write_end' on error: */
  1.2307 +      if (!result)
  1.2308 +         return 0;
  1.2309 +   }
  1.2310 +
  1.2311 +   /* Otherwise this is the case where the input is in a format currently
  1.2312 +    * supported by the rest of the libpng write code; call it directly.
  1.2313 +    */
  1.2314 +   else
  1.2315 +   {
  1.2316 +      png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
  1.2317 +      ptrdiff_t row_bytes = display->row_bytes;
  1.2318 +      png_uint_32 y = image->height;
  1.2319 +
  1.2320 +      while (y-- > 0)
  1.2321 +      {
  1.2322 +         png_write_row(png_ptr, row);
  1.2323 +         row += row_bytes;
  1.2324 +      }
  1.2325 +   }
  1.2326 +
  1.2327 +   png_write_end(png_ptr, info_ptr);
  1.2328 +   return 1;
  1.2329 +}
  1.2330 +
  1.2331 +int PNGAPI
  1.2332 +png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
  1.2333 +   const void *buffer, png_int_32 row_stride, const void *colormap)
  1.2334 +{
  1.2335 +   /* Write the image to the given (FILE*). */
  1.2336 +   if (image != NULL && image->version == PNG_IMAGE_VERSION)
  1.2337 +   {
  1.2338 +      if (file != NULL)
  1.2339 +      {
  1.2340 +         if (png_image_write_init(image))
  1.2341 +         {
  1.2342 +            png_image_write_control display;
  1.2343 +            int result;
  1.2344 +
  1.2345 +            /* This is slightly evil, but png_init_io doesn't do anything other
  1.2346 +             * than this and we haven't changed the standard IO functions so
  1.2347 +             * this saves a 'safe' function.
  1.2348 +             */
  1.2349 +            image->opaque->png_ptr->io_ptr = file;
  1.2350 +
  1.2351 +            memset(&display, 0, (sizeof display));
  1.2352 +            display.image = image;
  1.2353 +            display.buffer = buffer;
  1.2354 +            display.row_stride = row_stride;
  1.2355 +            display.colormap = colormap;
  1.2356 +            display.convert_to_8bit = convert_to_8bit;
  1.2357 +
  1.2358 +            result = png_safe_execute(image, png_image_write_main, &display);
  1.2359 +            png_image_free(image);
  1.2360 +            return result;
  1.2361 +         }
  1.2362 +
  1.2363 +         else
  1.2364 +            return 0;
  1.2365 +      }
  1.2366 +
  1.2367 +      else
  1.2368 +         return png_image_error(image,
  1.2369 +            "png_image_write_to_stdio: invalid argument");
  1.2370 +   }
  1.2371 +
  1.2372 +   else if (image != NULL)
  1.2373 +      return png_image_error(image,
  1.2374 +         "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION");
  1.2375 +
  1.2376 +   else
  1.2377 +      return 0;
  1.2378 +}
  1.2379 +
  1.2380 +int PNGAPI
  1.2381 +png_image_write_to_file(png_imagep image, const char *file_name,
  1.2382 +   int convert_to_8bit, const void *buffer, png_int_32 row_stride,
  1.2383 +   const void *colormap)
  1.2384 +{
  1.2385 +   /* Write the image to the named file. */
  1.2386 +   if (image != NULL && image->version == PNG_IMAGE_VERSION)
  1.2387 +   {
  1.2388 +      if (file_name != NULL)
  1.2389 +      {
  1.2390 +         FILE *fp = fopen(file_name, "wb");
  1.2391 +
  1.2392 +         if (fp != NULL)
  1.2393 +         {
  1.2394 +            if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
  1.2395 +               row_stride, colormap))
  1.2396 +            {
  1.2397 +               int error; /* from fflush/fclose */
  1.2398 +
  1.2399 +               /* Make sure the file is flushed correctly. */
  1.2400 +               if (fflush(fp) == 0 && ferror(fp) == 0)
  1.2401 +               {
  1.2402 +                  if (fclose(fp) == 0)
  1.2403 +                     return 1;
  1.2404 +
  1.2405 +                  error = errno; /* from fclose */
  1.2406 +               }
  1.2407 +
  1.2408 +               else
  1.2409 +               {
  1.2410 +                  error = errno; /* from fflush or ferror */
  1.2411 +                  (void)fclose(fp);
  1.2412 +               }
  1.2413 +
  1.2414 +               (void)remove(file_name);
  1.2415 +               /* The image has already been cleaned up; this is just used to
  1.2416 +                * set the error (because the original write succeeded).
  1.2417 +                */
  1.2418 +               return png_image_error(image, strerror(error));
  1.2419 +            }
  1.2420 +
  1.2421 +            else
  1.2422 +            {
  1.2423 +               /* Clean up: just the opened file. */
  1.2424 +               (void)fclose(fp);
  1.2425 +               (void)remove(file_name);
  1.2426 +               return 0;
  1.2427 +            }
  1.2428 +         }
  1.2429 +
  1.2430 +         else
  1.2431 +            return png_image_error(image, strerror(errno));
  1.2432 +      }
  1.2433 +
  1.2434 +      else
  1.2435 +         return png_image_error(image,
  1.2436 +            "png_image_write_to_file: invalid argument");
  1.2437 +   }
  1.2438 +
  1.2439 +   else if (image != NULL)
  1.2440 +      return png_image_error(image,
  1.2441 +         "png_image_write_to_file: incorrect PNG_IMAGE_VERSION");
  1.2442 +
  1.2443 +   else
  1.2444 +      return 0;
  1.2445 +}
  1.2446 +#endif /* PNG_STDIO_SUPPORTED */
  1.2447 +#endif /* SIMPLIFIED_WRITE */
  1.2448 +
  1.2449 +#ifdef PNG_WRITE_APNG_SUPPORTED
  1.2450 +void PNGAPI
  1.2451 +png_write_frame_head(png_structp png_ptr, png_infop info_ptr,
  1.2452 +    png_bytepp row_pointers, png_uint_32 width, png_uint_32 height,
  1.2453 +    png_uint_32 x_offset, png_uint_32 y_offset,
  1.2454 +    png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
  1.2455 +    png_byte blend_op)
  1.2456 +{
  1.2457 +    png_debug(1, "in png_write_frame_head");
  1.2458 +
  1.2459 +    /* there is a chance this has been set after png_write_info was called,
  1.2460 +    * so it would be set but not written. is there a way to be sure? */
  1.2461 +    if (!(info_ptr->valid & PNG_INFO_acTL))
  1.2462 +        png_error(png_ptr, "png_write_frame_head(): acTL not set");
  1.2463 +
  1.2464 +    png_write_reset(png_ptr);
  1.2465 +
  1.2466 +    png_write_reinit(png_ptr, info_ptr, width, height);
  1.2467 +
  1.2468 +    if ( !(png_ptr->num_frames_written == 0 &&
  1.2469 +           (png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN) ) )
  1.2470 +        png_write_fcTL(png_ptr, width, height, x_offset, y_offset,
  1.2471 +                       delay_num, delay_den, dispose_op, blend_op);
  1.2472 +
  1.2473 +    PNG_UNUSED(row_pointers)
  1.2474 +}
  1.2475 +
  1.2476 +void PNGAPI
  1.2477 +png_write_frame_tail(png_structp png_ptr, png_infop info_ptr)
  1.2478 +{
  1.2479 +    png_debug(1, "in png_write_frame_tail");
  1.2480 +
  1.2481 +    png_ptr->num_frames_written++;
  1.2482 +
  1.2483 +    PNG_UNUSED(info_ptr)
  1.2484 +}
  1.2485 +#endif /* PNG_WRITE_APNG_SUPPORTED */
  1.2486 +#endif /* PNG_WRITE_SUPPORTED */

mercurial