jdk/src/java.desktop/share/native/libsplashscreen/libpng/pngwrite.c
author azvegint
Mon, 19 Jan 2015 20:02:31 +0300
changeset 29913 95258013e132
parent 25859 3317bb8137f4
child 35296 659e92aaf7f4
permissions -rw-r--r--
8069198: Upgrade image library Reviewed-by: ahgross, bae, mschoene, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/* pngwrite.c - general routines to write a PNG file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * file and, per its terms, should not be removed:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    32
 * Last changed in libpng 1.6.15 [November 20, 2014]
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    33
 * Copyright (c) 1998-2014 Glenn Randers-Pehrson
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    36
 *
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    37
 * This code is released under the libpng license.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    38
 * For conditions of distribution and use, see the disclaimer
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    39
 * and license in png.h
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    42
#include "pngpriv.h"
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    43
#if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    44
#  include <errno.h>
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    45
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    46
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#ifdef PNG_WRITE_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    49
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    50
/* Write out all the unknown chunks for the current given location */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    51
static void
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    52
write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    53
   unsigned int where)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    54
{
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    55
   if (info_ptr->unknown_chunks_num != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    56
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    57
      png_const_unknown_chunkp up;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    58
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    59
      png_debug(5, "writing extra chunks");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    60
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    61
      for (up = info_ptr->unknown_chunks;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    62
           up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    63
           ++up)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    64
         if ((up->location & where) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    65
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    66
         /* If per-chunk unknown chunk handling is enabled use it, otherwise
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    67
          * just write the chunks the application has set.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    68
          */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    69
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    70
         int keep = png_handle_as_unknown(png_ptr, up->name);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    71
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    72
         /* NOTE: this code is radically different from the read side in the
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    73
          * matter of handling an ancillary unknown chunk.  In the read side
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    74
          * the default behavior is to discard it, in the code below the default
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    75
          * behavior is to write it.  Critical chunks are, however, only
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    76
          * written if explicitly listed or if the default is set to write all
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    77
          * unknown chunks.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    78
          *
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    79
          * The default handling is also slightly weird - it is not possible to
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    80
          * stop the writing of all unsafe-to-copy chunks!
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    81
          *
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    82
          * TODO: REVIEW: this would seem to be a bug.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    83
          */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    84
         if (keep != PNG_HANDLE_CHUNK_NEVER &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    85
             ((up->name[3] & 0x20) /* safe-to-copy overrides everything */ ||
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    86
              keep == PNG_HANDLE_CHUNK_ALWAYS ||
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    87
              (keep == PNG_HANDLE_CHUNK_AS_DEFAULT &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    88
               png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS)))
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    89
#endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    90
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    91
            /* TODO: review, what is wrong with a zero length unknown chunk? */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    92
            if (up->size == 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    93
               png_warning(png_ptr, "Writing zero-length unknown chunk");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    94
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    95
            png_write_chunk(png_ptr, up->name, up->data, up->size);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    96
         }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    97
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    98
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
    99
}
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   100
#endif /* WRITE_UNKNOWN_CHUNKS */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   101
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
/* Writes all the PNG information.  This is the suggested way to use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * library.  If you have a new chunk to add, make a function to write it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * and put it in the correct location here.  If you want the chunk written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * after the image data, put it in png_write_end().  I strongly encourage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * the chunk, as that will keep the code from breaking if you want to just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * write a plain PNG file.  If you have long comments, I suggest writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * them in png_write_end(), and compressing them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   112
png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   114
   png_debug(1, "in png_write_info_before_PLTE");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   115
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
   if (png_ptr == NULL || info_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   118
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   119
   if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   121
   /* Write PNG signature */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   122
   png_write_sig(png_ptr);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   123
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   124
#ifdef PNG_MNG_FEATURES_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   125
   if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 && \
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   126
       png_ptr->mng_features_permitted != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   128
      png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   129
      png_ptr->mng_features_permitted = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   132
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   133
   /* Write IHDR information. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
   png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   135
       info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   136
       info_ptr->filter_type,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   137
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   138
       info_ptr->interlace_type
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
#else
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   140
       0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
#endif
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   142
      );
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   143
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   144
   /* The rest of these check to see if the valid field has the appropriate
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   145
    * flag set, and if it does, writes the chunk.
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   146
    *
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   147
    * 1.6.0: COLORSPACE support controls the writing of these chunks too, and
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   148
    * the chunks will be written if the WRITE routine is there and information
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   149
    * is available in the COLORSPACE.  (See png_colorspace_sync_info in png.c
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   150
    * for where the valid flags get set.)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   151
    *
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   152
    * Under certain circumstances the colorspace can be invalidated without
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   153
    * syncing the info_struct 'valid' flags; this happens if libpng detects and
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   154
    * error and calls png_error while the color space is being set, yet the
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   155
    * application continues writing the PNG.  So check the 'invalid' flag here
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   156
    * too.
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   157
    */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   158
#ifdef PNG_GAMMA_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   159
#  ifdef PNG_WRITE_gAMA_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   160
      if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   161
          (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) != 0 &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   162
          (info_ptr->valid & PNG_INFO_gAMA) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   163
         png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   164
#  endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   166
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   167
#ifdef PNG_COLORSPACE_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   168
   /* Write only one of sRGB or an ICC profile.  If a profile was supplied
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   169
    * and it matches one of the known sRGB ones issue a warning.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   170
    */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   171
#  ifdef PNG_WRITE_iCCP_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   172
      if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   173
          (info_ptr->valid & PNG_INFO_iCCP) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   174
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   175
#        ifdef PNG_WRITE_sRGB_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   176
            if ((info_ptr->valid & PNG_INFO_sRGB) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   177
               png_app_warning(png_ptr,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   178
                  "profile matches sRGB but writing iCCP instead");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   179
#        endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   180
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   181
         png_write_iCCP(png_ptr, info_ptr->iccp_name,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   182
            info_ptr->iccp_profile);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   183
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   184
#     ifdef PNG_WRITE_sRGB_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   185
         else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   186
#     endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   187
#  endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   188
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   189
#  ifdef PNG_WRITE_sRGB_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   190
      if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   191
          (info_ptr->valid & PNG_INFO_sRGB) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   192
         png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   193
#  endif /* WRITE_sRGB */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   194
#endif /* COLORSPACE */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   195
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   196
#ifdef PNG_WRITE_sBIT_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   197
   if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
      png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
#endif
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   200
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   201
#ifdef PNG_COLORSPACE_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   202
#  ifdef PNG_WRITE_cHRM_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   203
      if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   204
         (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0 &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   205
         (info_ptr->valid & PNG_INFO_cHRM) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   206
         png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   207
#  endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   209
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   210
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   211
      write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   212
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
      png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   219
png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
   int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   225
   png_debug(1, "in png_write_info");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
   if (png_ptr == NULL || info_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
   png_write_info_before_PLTE(png_ptr, info_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   232
   if ((info_ptr->valid & PNG_INFO_PLTE) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
      png_write_PLTE(png_ptr, info_ptr->palette,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   234
          (png_uint_32)info_ptr->num_palette);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   235
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   236
   else if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) !=0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
      png_error(png_ptr, "Valid palette required for paletted images");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   239
#ifdef PNG_WRITE_tRNS_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   240
   if ((info_ptr->valid & PNG_INFO_tRNS) !=0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   241
   {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   242
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   243
      /* Invert the alpha channel (in tRNS) */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   244
      if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0 &&
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   245
          info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   247
         int j;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   248
         for (j = 0; j<(int)info_ptr->num_trans; j++)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   249
            info_ptr->trans_alpha[j] =
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   250
               (png_byte)(255 - info_ptr->trans_alpha[j]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   253
      png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   254
          info_ptr->num_trans, info_ptr->color_type);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   255
   }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   256
#endif
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   257
#ifdef PNG_WRITE_bKGD_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   258
   if ((info_ptr->valid & PNG_INFO_bKGD) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
      png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   261
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   262
#ifdef PNG_WRITE_hIST_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   263
   if ((info_ptr->valid & PNG_INFO_hIST) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
      png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   266
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   267
#ifdef PNG_WRITE_oFFs_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   268
   if ((info_ptr->valid & PNG_INFO_oFFs) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
      png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   270
          info_ptr->offset_unit_type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   272
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   273
#ifdef PNG_WRITE_pCAL_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   274
   if ((info_ptr->valid & PNG_INFO_pCAL) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
      png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   276
          info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   277
          info_ptr->pcal_units, info_ptr->pcal_params);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   279
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   280
#ifdef PNG_WRITE_sCAL_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   281
   if ((info_ptr->valid & PNG_INFO_sCAL) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
      png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
          info_ptr->scal_s_width, info_ptr->scal_s_height);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   284
#endif /* sCAL */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   285
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   286
#ifdef PNG_WRITE_pHYs_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   287
   if ((info_ptr->valid & PNG_INFO_pHYs) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
      png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   289
          info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   290
#endif /* pHYs */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   291
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   292
#ifdef PNG_WRITE_tIME_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   293
   if ((info_ptr->valid & PNG_INFO_tIME) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
      png_write_tIME(png_ptr, &(info_ptr->mod_time));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
      png_ptr->mode |= PNG_WROTE_tIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   298
#endif /* tIME */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   299
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   300
#ifdef PNG_WRITE_sPLT_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   301
   if ((info_ptr->valid & PNG_INFO_sPLT) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   302
      for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   303
         png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   304
#endif /* sPLT */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   305
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   306
#ifdef PNG_WRITE_TEXT_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
   /* Check to see if we need to write text chunks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
   for (i = 0; i < info_ptr->num_text; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   310
      png_debug2(2, "Writing header text chunk %d, type %d", i,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   311
          info_ptr->text[i].compression);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   312
      /* An internationalized chunk? */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
      if (info_ptr->text[i].compression > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   315
#ifdef PNG_WRITE_iTXt_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   316
         /* Write international chunk */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   317
         png_write_iTXt(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   318
             info_ptr->text[i].compression,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   319
             info_ptr->text[i].key,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   320
             info_ptr->text[i].lang,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   321
             info_ptr->text[i].lang_key,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   322
             info_ptr->text[i].text);
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   323
         /* Mark this chunk as written */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   324
         if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   325
            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   326
         else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   327
            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
#else
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   329
         png_warning(png_ptr, "Unable to write international text");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   332
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
      /* If we want a compressed text chunk */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   336
#ifdef PNG_WRITE_zTXt_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   337
         /* Write compressed chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         png_write_zTXt(png_ptr, info_ptr->text[i].key,
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   339
             info_ptr->text[i].text, info_ptr->text[i].compression);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   340
         /* Mark this chunk as written */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   341
         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         png_warning(png_ptr, "Unable to write compressed text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   346
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
      else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   349
#ifdef PNG_WRITE_tEXt_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   350
         /* Write uncompressed chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         png_write_tEXt(png_ptr, info_ptr->text[i].key,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   352
             info_ptr->text[i].text,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   353
             0);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   354
         /* Mark this chunk as written */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   355
         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
#else
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   357
         /* Can't get here */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         png_warning(png_ptr, "Unable to write uncompressed text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   362
#endif /* tEXt */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   363
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   364
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   365
   write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
/* Writes the end of the PNG file.  If you don't want to write comments or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
 * time information, you can pass NULL for info.  If you already wrote these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
 * in png_write_info(), do not write them again here.  If you have long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
 * comments, I suggest writing them here, and compressing them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   375
png_write_end(png_structrp png_ptr, png_inforp info_ptr)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   377
   png_debug(1, "in png_write_end");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   378
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   381
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   382
   if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
      png_error(png_ptr, "No IDATs written into file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   385
#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   386
   if (png_ptr->num_palette_max > png_ptr->num_palette)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   387
      png_benign_error(png_ptr, "Wrote palette index exceeding num_palette");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   388
#endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   389
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   390
   /* See if user wants us to write information chunks */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
   if (info_ptr != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   393
#ifdef PNG_WRITE_TEXT_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
      int i; /* local index variable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   396
#ifdef PNG_WRITE_tIME_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   397
      /* Check to see if user has supplied a time chunk */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   398
      if ((info_ptr->valid & PNG_INFO_tIME) != 0 &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   399
          (png_ptr->mode & PNG_WROTE_tIME) == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         png_write_tIME(png_ptr, &(info_ptr->mod_time));
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   401
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   403
#ifdef PNG_WRITE_TEXT_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   404
      /* Loop through comment chunks */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
      for (i = 0; i < info_ptr->num_text; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   407
         png_debug2(2, "Writing trailer text chunk %d, type %d", i,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            info_ptr->text[i].compression);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   409
         /* An internationalized chunk? */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         if (info_ptr->text[i].compression > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   412
#ifdef PNG_WRITE_iTXt_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   413
            /* Write international chunk */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   414
            png_write_iTXt(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   415
                info_ptr->text[i].compression,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   416
                info_ptr->text[i].key,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   417
                info_ptr->text[i].lang,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   418
                info_ptr->text[i].lang_key,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   419
                info_ptr->text[i].text);
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   420
            /* Mark this chunk as written */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   421
            if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   422
               info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   423
            else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   424
               info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
#else
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   426
            png_warning(png_ptr, "Unable to write international text");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   429
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   432
#ifdef PNG_WRITE_zTXt_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   433
            /* Write compressed chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            png_write_zTXt(png_ptr, info_ptr->text[i].key,
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   435
                info_ptr->text[i].text, info_ptr->text[i].compression);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   436
            /* Mark this chunk as written */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   437
            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            png_warning(png_ptr, "Unable to write compressed text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   442
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   445
#ifdef PNG_WRITE_tEXt_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   446
            /* Write uncompressed chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            png_write_tEXt(png_ptr, info_ptr->text[i].key,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   448
                info_ptr->text[i].text, 0);
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   449
            /* Mark this chunk as written */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   450
            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            png_warning(png_ptr, "Unable to write uncompressed text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   457
#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   458
      write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
   png_ptr->mode |= PNG_AFTER_IDAT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   464
   /* Write end of PNG file */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
   png_write_IEND(png_ptr);
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   466
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   467
   /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   468
    * and restored again in libpng-1.2.30, may cause some applications that
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   469
    * do not set png_ptr->output_flush_fn to crash.  If your application
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   470
    * experiences a problem, please try building libpng with
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   471
    * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   472
    * png-mng-implement at lists.sf.net .
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   473
    */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   474
#ifdef PNG_WRITE_FLUSH_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   475
#  ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
   png_flush(png_ptr);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   477
#  endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   481
#ifdef PNG_CONVERT_tIME_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   483
png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm * ttime)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   485
   png_debug(1, "in png_convert_from_struct_tm");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   486
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
   ptime->year = (png_uint_16)(1900 + ttime->tm_year);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
   ptime->month = (png_byte)(ttime->tm_mon + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
   ptime->day = (png_byte)ttime->tm_mday;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
   ptime->hour = (png_byte)ttime->tm_hour;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
   ptime->minute = (png_byte)ttime->tm_min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
   ptime->second = (png_byte)ttime->tm_sec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
void PNGAPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
png_convert_from_time_t(png_timep ptime, time_t ttime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
   struct tm *tbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   500
   png_debug(1, "in png_convert_from_time_t");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   501
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
   tbuf = gmtime(&ttime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
   png_convert_from_struct_tm(ptime, tbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
/* Initialize png_ptr structure, and allocate any memory needed */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   508
PNG_FUNCTION(png_structp,PNGAPI
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   509
png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   510
    png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
{
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   512
#ifndef PNG_USER_MEM_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   513
   png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   514
       error_fn, warn_fn, NULL, NULL, NULL);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   515
#else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   516
   return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   517
       warn_fn, NULL, NULL, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
/* Alternate initialize png_ptr structure, and allocate any memory needed */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   521
PNG_FUNCTION(png_structp,PNGAPI
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   522
png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   523
    png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   524
    png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
{
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   526
   png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   527
       error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   528
#endif /* USER_MEM */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   529
   if (png_ptr != NULL)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   530
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   531
      /* Set the zlib control values to defaults; they can be overridden by the
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   532
       * application after the struct has been created.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   533
       */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   534
      png_ptr->zbuffer_size = PNG_ZBUF_SIZE;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   535
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   536
      /* The 'zlib_strategy' setting is irrelevant because png_default_claim in
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   537
       * pngwutil.c defaults it according to whether or not filters will be
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   538
       * used, and ignores this setting.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   539
       */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   540
      png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   541
      png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   542
      png_ptr->zlib_mem_level = 8;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   543
      png_ptr->zlib_window_bits = 15;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   544
      png_ptr->zlib_method = 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   546
#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   547
      png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   548
      png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   549
      png_ptr->zlib_text_mem_level = 8;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   550
      png_ptr->zlib_text_window_bits = 15;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   551
      png_ptr->zlib_text_method = 8;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   552
#endif /* WRITE_COMPRESSED_TEXT */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   553
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   554
      /* This is a highly dubious configuration option; by default it is off,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   555
       * but it may be appropriate for private builds that are testing
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   556
       * extensions not conformant to the current specification, or of
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   557
       * applications that must not fail to write at all costs!
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   558
       */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   559
#ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   560
      /* In stable builds only warn if an application error can be completely
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   561
       * handled.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   562
       */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   563
      png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   566
      /* App warnings are warnings in release (or release candidate) builds but
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   567
       * are errors during development.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   568
       */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   569
#if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   570
      png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   573
      /* TODO: delay this, it can be done in png_init_io() (if the app doesn't
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   574
       * do it itself) avoiding setting the default function if it is not
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   575
       * required.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   576
       */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   577
      png_set_write_fn(png_ptr, NULL, NULL, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   580
   return png_ptr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
/* Write a few rows of image data.  If the image is interlaced,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
 * either you will have to write the 7 sub images, or, if you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
 * have called png_set_interlace_handling(), you will have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
 * "write" the image seven times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   590
png_write_rows(png_structrp png_ptr, png_bytepp row,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   591
    png_uint_32 num_rows)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
   png_uint_32 i; /* row counter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
   png_bytepp rp; /* row pointer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   596
   png_debug(1, "in png_write_rows");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   601
   /* Loop through the rows */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
   for (i = 0, rp = row; i < num_rows; i++, rp++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
      png_write_row(png_ptr, *rp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
/* Write the image.  You only need to call this function once, even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
 * if you are writing an interlaced image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   612
png_write_image(png_structrp png_ptr, png_bytepp image)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
   png_uint_32 i; /* row index */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
   int pass, num_pass; /* pass variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
   png_bytepp rp; /* points to current row */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   621
   png_debug(1, "in png_write_image");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   622
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   623
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   624
   /* Initialize interlace handling.  If image is not interlaced,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   625
    * this will set pass to 1
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   626
    */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
   num_pass = png_set_interlace_handling(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
   num_pass = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   631
   /* Loop through passes */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
   for (pass = 0; pass < num_pass; pass++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   634
      /* Loop through image */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
      for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
         png_write_row(png_ptr, *rp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   642
#ifdef PNG_MNG_FEATURES_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   643
/* Performs intrapixel differencing  */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   644
static void
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   645
png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   646
{
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   647
   png_debug(1, "in png_do_write_intrapixel");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   648
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   649
   if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   650
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   651
      int bytes_per_pixel;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   652
      png_uint_32 row_width = row_info->width;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   653
      if (row_info->bit_depth == 8)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   654
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   655
         png_bytep rp;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   656
         png_uint_32 i;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   657
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   658
         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   659
            bytes_per_pixel = 3;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   660
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   661
         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   662
            bytes_per_pixel = 4;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   663
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   664
         else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   665
            return;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   666
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   667
         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   668
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   669
            *(rp)     = (png_byte)((*rp       - *(rp + 1)) & 0xff);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   670
            *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   671
         }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   672
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   673
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   674
#ifdef PNG_WRITE_16BIT_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   675
      else if (row_info->bit_depth == 16)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   676
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   677
         png_bytep rp;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   678
         png_uint_32 i;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   679
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   680
         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   681
            bytes_per_pixel = 6;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   682
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   683
         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   684
            bytes_per_pixel = 8;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   685
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   686
         else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   687
            return;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   688
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   689
         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   690
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   691
            png_uint_32 s0   = (*(rp    ) << 8) | *(rp + 1);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   692
            png_uint_32 s1   = (*(rp + 2) << 8) | *(rp + 3);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   693
            png_uint_32 s2   = (*(rp + 4) << 8) | *(rp + 5);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   694
            png_uint_32 red  = (png_uint_32)((s0 - s1) & 0xffffL);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   695
            png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   696
            *(rp    ) = (png_byte)((red >> 8) & 0xff);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   697
            *(rp + 1) = (png_byte)(red & 0xff);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   698
            *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   699
            *(rp + 5) = (png_byte)(blue & 0xff);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   700
         }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   701
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   702
#endif /* WRITE_16BIT */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   703
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   704
}
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   705
#endif /* MNG_FEATURES */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   706
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   707
/* Called by user to write a row of image data */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   709
png_write_row(png_structrp png_ptr, png_const_bytep row)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
{
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   711
   /* 1.5.6: moved from png_struct to be a local structure: */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   712
   png_row_info row_info;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   713
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   716
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   717
   png_debug2(1, "in png_write_row (row %u, pass %d)",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
      png_ptr->row_number, png_ptr->pass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   720
   /* Initialize transformations and other stuff if first time */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   723
      /* Make sure we wrote the header info */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   724
      if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   725
         png_error(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   726
             "png_write_info was never called before png_write_row");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   728
      /* Check for transforms that have been set but were defined out */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   730
      if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   731
         png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   733
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   735
      if ((png_ptr->transformations & PNG_FILLER) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   736
         png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   738
#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   739
    defined(PNG_READ_PACKSWAP_SUPPORTED)
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   740
      if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   741
         png_warning(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   742
             "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   744
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   746
      if ((png_ptr->transformations & PNG_PACK) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   747
         png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   749
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   751
      if ((png_ptr->transformations & PNG_SHIFT) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   752
         png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   754
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   756
      if ((png_ptr->transformations & PNG_BGR) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   757
         png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   759
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   761
      if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   762
         png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
      png_write_start_row(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   768
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   769
   /* If interlaced and not interested in row, return */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   770
   if (png_ptr->interlaced != 0 &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   771
       (png_ptr->transformations & PNG_INTERLACE) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
      switch (png_ptr->pass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
         case 0:
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   776
            if ((png_ptr->row_number & 0x07) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
               png_write_finish_row(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
               return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   782
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         case 1:
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   784
            if ((png_ptr->row_number & 0x07) != 0 || png_ptr->width < 5)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
               png_write_finish_row(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
               return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   790
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
         case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            if ((png_ptr->row_number & 0x07) != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
               png_write_finish_row(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
               return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   798
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
         case 3:
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   800
            if ((png_ptr->row_number & 0x03) != 0 || png_ptr->width < 3)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
               png_write_finish_row(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
               return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   806
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
         case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            if ((png_ptr->row_number & 0x03) != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
               png_write_finish_row(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
               return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   814
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
         case 5:
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   816
            if ((png_ptr->row_number & 0x01) != 0 || png_ptr->width < 2)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
               png_write_finish_row(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
               return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   822
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
         case 6:
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   824
            if ((png_ptr->row_number & 0x01) == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
               png_write_finish_row(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
               return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   830
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   831
         default: /* error: ignore it */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   832
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   837
   /* Set up row info for transformations */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   838
   row_info.color_type = png_ptr->color_type;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   839
   row_info.width = png_ptr->usr_width;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   840
   row_info.channels = png_ptr->usr_channels;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   841
   row_info.bit_depth = png_ptr->usr_bit_depth;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   842
   row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   843
   row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   845
   png_debug1(3, "row_info->color_type = %d", row_info.color_type);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   846
   png_debug1(3, "row_info->width = %u", row_info.width);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   847
   png_debug1(3, "row_info->channels = %d", row_info.channels);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   848
   png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   849
   png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   850
   png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
   /* Copy user's row into buffer, leaving room for filter byte. */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   853
   memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   855
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   856
   /* Handle interlacing */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
   if (png_ptr->interlaced && png_ptr->pass < 6 &&
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   858
       (png_ptr->transformations & PNG_INTERLACE) != 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
   {
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   860
      png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   861
      /* This should always get caught above, but still ... */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   862
      if (row_info.width == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
         png_write_finish_row(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
         return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   870
#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   871
   /* Handle other transformations */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   872
   if (png_ptr->transformations != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   873
      png_do_write_transformations(png_ptr, &row_info);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   874
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   876
   /* At this point the row_info pixel depth must match the 'transformed' depth,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   877
    * which is also the output depth.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   878
    */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   879
   if (row_info.pixel_depth != png_ptr->pixel_depth ||
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   880
      row_info.pixel_depth != png_ptr->transformed_pixel_depth)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   881
      png_error(png_ptr, "internal write transform logic error");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   882
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   883
#ifdef PNG_MNG_FEATURES_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
   /* Write filter_method 64 (intrapixel differencing) only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    * 2. Libpng did not write a PNG signature (this filter_method is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    *    used in PNG datastreams that are embedded in MNG datastreams) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    * 3. The application called png_permit_mng_features with a mask that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    *    included PNG_FLAG_MNG_FILTER_64 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    * 4. The filter_method is 64 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    * 5. The color_type is RGB or RGBA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   893
   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   894
       (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
      /* Intrapixel differencing */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   897
      png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   901
/* Added at libpng-1.5.10 */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   902
#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   903
   /* Check for out-of-range palette index */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   904
   if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   905
       png_ptr->num_palette_max >= 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   906
      png_do_check_palette_indexes(png_ptr, &row_info);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   907
#endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   908
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
   /* Find a filter if necessary, filter the row and write it out. */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   910
   png_write_find_filter(png_ptr, &row_info);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
   if (png_ptr->write_row_fn != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
      (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   916
#ifdef PNG_WRITE_FLUSH_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
/* Set the automatic flush interval or 0 to turn flushing off */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   919
png_set_flush(png_structrp png_ptr, int nrows)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   921
   png_debug(1, "in png_set_flush");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   922
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   925
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
   png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   929
/* Flush the current output buffers now */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   931
png_write_flush(png_structrp png_ptr)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   933
   png_debug(1, "in png_write_flush");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   934
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   937
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
   /* We have already written out all of the data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
   if (png_ptr->row_number >= png_ptr->num_rows)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   940
      return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   942
   png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
   png_ptr->flush_rows = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
   png_flush(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
}
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   946
#endif /* WRITE_FLUSH */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   947
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   948
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   949
static void png_reset_filter_heuristics(png_structrp png_ptr);/* forward decl */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   950
#endif
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   951
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   952
/* Free any memory used in png_ptr struct without freeing the struct itself. */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   953
static void
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   954
png_write_destroy(png_structrp png_ptr)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   956
   png_debug(1, "in png_write_destroy");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   958
   /* Free any memory zlib uses */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   959
   if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   960
      deflateEnd(&png_ptr->zstream);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   961
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   962
   /* Free our memory.  png_free checks NULL for us. */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   963
   png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
   png_free(png_ptr, png_ptr->row_buf);
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   965
   png_ptr->row_buf = NULL;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   966
#ifdef PNG_WRITE_FILTER_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
   png_free(png_ptr, png_ptr->prev_row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
   png_free(png_ptr, png_ptr->sub_row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
   png_free(png_ptr, png_ptr->up_row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
   png_free(png_ptr, png_ptr->avg_row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
   png_free(png_ptr, png_ptr->paeth_row);
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   972
   png_ptr->prev_row = NULL;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   973
   png_ptr->sub_row = NULL;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   974
   png_ptr->up_row = NULL;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   975
   png_ptr->avg_row = NULL;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   976
   png_ptr->paeth_row = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   979
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   980
   /* Use this to save a little code space, it doesn't free the filter_costs */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   981
   png_reset_filter_heuristics(png_ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
   png_free(png_ptr, png_ptr->filter_costs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
   png_free(png_ptr, png_ptr->inv_filter_costs);
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   984
   png_ptr->filter_costs = NULL;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   985
   png_ptr->inv_filter_costs = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   988
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   989
   png_free(png_ptr, png_ptr->chunk_list);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   990
   png_ptr->chunk_list = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   993
   /* The error handling and memory handling information is left intact at this
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   994
    * point: the jmp_buf may still have to be freed.  See png_destroy_png_struct
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   995
    * for how this happens.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   996
    */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   997
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
   999
/* Free all memory used by the write.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1000
 * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1001
 * *png_ptr_ptr.  Prior to 1.6.0 it would accept such a value and it would free
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1002
 * the passed in info_structs but it would quietly fail to free any of the data
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1003
 * inside them.  In 1.6.0 it quietly does nothing (it has to be quiet because it
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1004
 * has no png_ptr.)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1005
 */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1006
void PNGAPI
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1007
png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1008
{
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1009
   png_debug(1, "in png_destroy_write_struct");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1011
   if (png_ptr_ptr != NULL)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1012
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1013
      png_structrp png_ptr = *png_ptr_ptr;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1014
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1015
      if (png_ptr != NULL) /* added in libpng 1.6.0 */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1016
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1017
         png_destroy_info_struct(png_ptr, info_ptr_ptr);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1018
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1019
         *png_ptr_ptr = NULL;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1020
         png_write_destroy(png_ptr);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1021
         png_destroy_png_struct(png_ptr);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1022
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1023
   }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
/* Allow the application to select one or more row filters to use. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1028
png_set_filter(png_structrp png_ptr, int method, int filters)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1030
   png_debug(1, "in png_set_filter");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1031
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1034
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1035
#ifdef PNG_MNG_FEATURES_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1036
   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1037
       (method == PNG_INTRAPIXEL_DIFFERENCING))
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1038
      method = PNG_FILTER_TYPE_BASE;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1039
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
   if (method == PNG_FILTER_TYPE_BASE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
      switch (filters & (PNG_ALL_FILTERS | 0x07))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1045
#ifdef PNG_WRITE_FILTER_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
         case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
         case 6:
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1048
         case 7: png_app_error(png_ptr, "Unknown row filter for method 0");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1049
            /* FALL THROUGH */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1050
#endif /* WRITE_FILTER */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1051
         case PNG_FILTER_VALUE_NONE:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1052
            png_ptr->do_filter = PNG_FILTER_NONE; break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1053
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1054
#ifdef PNG_WRITE_FILTER_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1055
         case PNG_FILTER_VALUE_SUB:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1056
            png_ptr->do_filter = PNG_FILTER_SUB; break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1057
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1058
         case PNG_FILTER_VALUE_UP:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1059
            png_ptr->do_filter = PNG_FILTER_UP; break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1060
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1061
         case PNG_FILTER_VALUE_AVG:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1062
            png_ptr->do_filter = PNG_FILTER_AVG; break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1063
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1064
         case PNG_FILTER_VALUE_PAETH:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1065
            png_ptr->do_filter = PNG_FILTER_PAETH; break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1066
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1067
         default:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1068
            png_ptr->do_filter = (png_byte)filters; break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1069
#else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1070
         default:
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1071
            png_app_error(png_ptr, "Unknown row filter for method 0");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1072
#endif /* WRITE_FILTER */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
      /* If we have allocated the row_buf, this means we have already started
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
       * with the image and we should have allocated all of the filter buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
       * that have been selected.  If prev_row isn't already allocated, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
       * it is too late to start using the filters that need it, since we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
       * will be missing the data in the previous row.  If an application
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
       * wants to start and stop using particular filters during compression,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
       * it should start out with all of the filters, and then add and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
       * remove them after the start of compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
      if (png_ptr->row_buf != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1086
#ifdef PNG_WRITE_FILTER_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1087
         if ((png_ptr->do_filter & PNG_FILTER_SUB) != 0 &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1088
             png_ptr->sub_row == NULL)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1091
                (png_ptr->rowbytes + 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1095
         if ((png_ptr->do_filter & PNG_FILTER_UP) != 0 &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1096
              png_ptr->up_row == NULL)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            if (png_ptr->prev_row == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
               png_warning(png_ptr, "Can't add Up filter after starting");
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1101
               png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1102
                   ~PNG_FILTER_UP);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1104
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
               png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1108
                   (png_ptr->rowbytes + 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
               png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1113
         if ((png_ptr->do_filter & PNG_FILTER_AVG) != 0 &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1114
              png_ptr->avg_row == NULL)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            if (png_ptr->prev_row == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
               png_warning(png_ptr, "Can't add Average filter after starting");
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1119
               png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1120
                   ~PNG_FILTER_AVG);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1122
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
               png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1126
                   (png_ptr->rowbytes + 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
               png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1131
         if ((png_ptr->do_filter & PNG_FILTER_PAETH) != 0 &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
             png_ptr->paeth_row == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            if (png_ptr->prev_row == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
               png_warning(png_ptr, "Can't add Paeth filter after starting");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
               png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1139
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
               png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1143
                   (png_ptr->rowbytes + 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
               png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
         if (png_ptr->do_filter == PNG_NO_FILTERS)
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1149
#endif /* WRITE_FILTER */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            png_ptr->do_filter = PNG_FILTER_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
   else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
      png_error(png_ptr, "Unknown custom filter method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
/* This allows us to influence the way in which libpng chooses the "best"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
 * filter for the current scanline.  While the "minimum-sum-of-absolute-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
 * differences metric is relatively fast and effective, there is some
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
 * question as to whether it can be improved upon by trying to keep the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
 * filtered data going to zlib more consistent, hopefully resulting in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
 * better compression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
 */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1164
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED      /* GRR 970116 */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1165
/* Convenience reset API. */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1166
static void
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1167
png_reset_filter_heuristics(png_structrp png_ptr)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1169
   /* Clear out any old values in the 'weights' - this must be done because if
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1170
    * the app calls set_filter_heuristics multiple times with different
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1171
    * 'num_weights' values we would otherwise potentially have wrong sized
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1172
    * arrays.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1173
    */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1174
   png_ptr->num_prev_filters = 0;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1175
   png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1176
   if (png_ptr->prev_filters != NULL)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1178
      png_bytep old = png_ptr->prev_filters;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1179
      png_ptr->prev_filters = NULL;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1180
      png_free(png_ptr, old);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1181
   }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1182
   if (png_ptr->filter_weights != NULL)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1183
   {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1184
      png_uint_16p old = png_ptr->filter_weights;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1185
      png_ptr->filter_weights = NULL;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1186
      png_free(png_ptr, old);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1189
   if (png_ptr->inv_filter_weights != NULL)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1191
      png_uint_16p old = png_ptr->inv_filter_weights;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1192
      png_ptr->inv_filter_weights = NULL;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1193
      png_free(png_ptr, old);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1196
   /* Leave the filter_costs - this array is fixed size. */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1197
}
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1198
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1199
static int
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1200
png_init_filter_heuristics(png_structrp png_ptr, int heuristic_method,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1201
   int num_weights)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1202
{
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1203
   if (png_ptr == NULL)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1204
      return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1206
   /* Clear out the arrays */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1207
   png_reset_filter_heuristics(png_ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1209
   /* Check arguments; the 'reset' function makes the correct settings for the
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1210
    * unweighted case, but we must handle the weight case by initializing the
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1211
    * arrays for the caller.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1212
    */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1213
   if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1215
      int i;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1216
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1217
      if (num_weights > 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
         png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1220
             (png_uint_32)((sizeof (png_byte)) * num_weights));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
         /* To make sure that the weighting starts out fairly */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
         for (i = 0; i < num_weights; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            png_ptr->prev_filters[i] = 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
         png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1229
             (png_uint_32)((sizeof (png_uint_16)) * num_weights));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
         png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1232
             (png_uint_32)((sizeof (png_uint_16)) * num_weights));
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1233
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
         for (i = 0; i < num_weights; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            png_ptr->inv_filter_weights[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
         }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1239
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1240
         /* Safe to set this now */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1241
         png_ptr->num_prev_filters = (png_byte)num_weights;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1244
      /* If, in the future, there are other filter methods, this would
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1245
       * need to be based on png_ptr->filter.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1246
       */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1247
      if (png_ptr->filter_costs == NULL)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1249
         png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1250
             (png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST));
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1251
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1252
         png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1253
             (png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
      for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
         png_ptr->inv_filter_costs[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
         png_ptr->filter_costs[i] = PNG_COST_FACTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1261
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1262
      /* All the arrays are inited, safe to set this: */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1263
      png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1264
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1265
      /* Return the 'ok' code. */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1266
      return 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1268
   else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1269
      heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1270
   {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1271
      return 1;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1272
   }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1273
   else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1274
   {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1275
      png_warning(png_ptr, "Unknown filter heuristic method");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1276
      return 0;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1277
   }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1278
}
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1279
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1280
/* Provide floating and fixed point APIs */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1281
#ifdef PNG_FLOATING_POINT_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1282
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1283
png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1284
    int num_weights, png_const_doublep filter_weights,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1285
    png_const_doublep filter_costs)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1286
{
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1287
   png_debug(1, "in png_set_filter_heuristics");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1289
   /* The internal API allocates all the arrays and ensures that the elements of
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1290
    * those arrays are set to the default value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1292
   if (png_init_filter_heuristics(png_ptr, heuristic_method, num_weights) == 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1293
      return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1294
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1295
   /* If using the weighted method copy in the weights. */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1296
   if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1298
      int i;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1299
      for (i = 0; i < num_weights; i++)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1300
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1301
         if (filter_weights[i] <= 0.0)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1302
         {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1303
            png_ptr->inv_filter_weights[i] =
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1304
            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1305
         }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1306
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1307
         else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1308
         {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1309
            png_ptr->inv_filter_weights[i] =
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1310
                (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1311
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1312
            png_ptr->filter_weights[i] =
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1313
                (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1314
         }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1315
      }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1316
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1317
      /* Here is where we set the relative costs of the different filters.  We
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1318
       * should take the desired compression level into account when setting
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1319
       * the costs, so that Paeth, for instance, has a high relative cost at low
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1320
       * compression levels, while it has a lower relative cost at higher
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1321
       * compression settings.  The filter types are in order of increasing
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1322
       * relative cost, so it would be possible to do this with an algorithm.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1323
       */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1324
      for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
         png_ptr->inv_filter_costs[i] =
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1327
             (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1328
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
         png_ptr->filter_costs[i] =
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1330
             (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
}
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1334
#endif /* FLOATING_POINT */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1335
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1336
#ifdef PNG_FIXED_POINT_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1337
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1338
png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1339
    int num_weights, png_const_fixed_point_p filter_weights,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1340
    png_const_fixed_point_p filter_costs)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1341
{
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1342
   png_debug(1, "in png_set_filter_heuristics_fixed");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1343
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1344
   /* The internal API allocates all the arrays and ensures that the elements of
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1345
    * those arrays are set to the default value.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1346
    */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1347
   if (png_init_filter_heuristics(png_ptr, heuristic_method, num_weights) == 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1348
      return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1349
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1350
   /* If using the weighted method copy in the weights. */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1351
   if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1352
   {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1353
      int i;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1354
      for (i = 0; i < num_weights; i++)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1355
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1356
         if (filter_weights[i] <= 0)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1357
         {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1358
            png_ptr->inv_filter_weights[i] =
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1359
            png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1360
         }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1361
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1362
         else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1363
         {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1364
            png_ptr->inv_filter_weights[i] = (png_uint_16)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1365
               ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1366
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1367
            png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1368
               PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1369
         }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1370
      }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1371
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1372
      /* Here is where we set the relative costs of the different filters.  We
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1373
       * should take the desired compression level into account when setting
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1374
       * the costs, so that Paeth, for instance, has a high relative cost at low
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1375
       * compression levels, while it has a lower relative cost at higher
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1376
       * compression settings.  The filter types are in order of increasing
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1377
       * relative cost, so it would be possible to do this with an algorithm.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1378
       */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1379
      for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1380
         if (filter_costs[i] >= PNG_FP_1)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1381
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1382
         png_uint_32 tmp;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1383
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1384
         /* Use a 32 bit unsigned temporary here because otherwise the
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1385
          * intermediate value will be a 32 bit *signed* integer (ANSI rules)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1386
          * and this will get the wrong answer on division.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1387
          */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1388
         tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1389
         tmp /= filter_costs[i];
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1390
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1391
         png_ptr->inv_filter_costs[i] = (png_uint_16)tmp;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1392
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1393
         tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1394
         tmp /= PNG_FP_1;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1395
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1396
         png_ptr->filter_costs[i] = (png_uint_16)tmp;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1397
      }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1398
   }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1399
}
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1400
#endif /* FIXED_POINT */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1401
#endif /* WRITE_WEIGHTED_FILTER */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1404
png_set_compression_level(png_structrp png_ptr, int level)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1406
   png_debug(1, "in png_set_compression_level");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1407
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1410
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
   png_ptr->zlib_level = level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1415
png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1417
   png_debug(1, "in png_set_compression_mem_level");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1418
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1421
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
   png_ptr->zlib_mem_level = mem_level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1426
png_set_compression_strategy(png_structrp png_ptr, int strategy)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1428
   png_debug(1, "in png_set_compression_strategy");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1429
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1432
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1433
   /* The flag setting here prevents the libpng dynamic selection of strategy.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1434
    */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
   png_ptr->zlib_strategy = strategy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1439
/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1440
 * smaller value of window_bits if it can do so safely.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1441
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1443
png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1447
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1448
   /* Prior to 1.6.0 this would warn but then set the window_bits value, this
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1449
    * meant that negative window bits values could be selected which would cause
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1450
    * libpng to write a non-standard PNG file with raw deflate or gzip
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1451
    * compressed IDAT or ancillary chunks.  Such files can be read and there is
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1452
    * no warning on read, so this seems like a very bad idea.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1453
    */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
   if (window_bits > 15)
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1455
   {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
      png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1457
      window_bits = 15;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1458
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1459
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
   else if (window_bits < 8)
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1461
   {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
      png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1463
      window_bits = 8;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1464
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1465
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
   png_ptr->zlib_window_bits = window_bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1470
png_set_compression_method(png_structrp png_ptr, int method)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1472
   png_debug(1, "in png_set_compression_method");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1473
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1476
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1477
   /* This would produce an invalid PNG file if it worked, but it doesn't and
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1478
    * deflate will fault it, so it is harmless to just warn here.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1479
    */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
   if (method != 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
      png_warning(png_ptr, "Only compression method 8 is supported by PNG");
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1482
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
   png_ptr->zlib_method = method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1486
/* The following were added to libpng-1.5.4 */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1487
#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1488
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1489
png_set_text_compression_level(png_structrp png_ptr, int level)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1490
{
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1491
   png_debug(1, "in png_set_text_compression_level");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1492
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1493
   if (png_ptr == NULL)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1494
      return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1495
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1496
   png_ptr->zlib_text_level = level;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1497
}
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1498
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1499
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1500
png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1501
{
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1502
   png_debug(1, "in png_set_text_compression_mem_level");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1503
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1504
   if (png_ptr == NULL)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1505
      return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1506
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1507
   png_ptr->zlib_text_mem_level = mem_level;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1508
}
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1509
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1510
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1511
png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1512
{
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1513
   png_debug(1, "in png_set_text_compression_strategy");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1514
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1515
   if (png_ptr == NULL)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1516
      return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1517
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1518
   png_ptr->zlib_text_strategy = strategy;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1519
}
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1520
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1521
/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1522
 * smaller value of window_bits if it can do so safely.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1523
 */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1524
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1525
png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1526
{
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1527
   if (png_ptr == NULL)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1528
      return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1529
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1530
   if (window_bits > 15)
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1531
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1532
      png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1533
      window_bits = 15;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1534
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1535
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1536
   else if (window_bits < 8)
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1537
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1538
      png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1539
      window_bits = 8;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1540
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1541
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1542
   png_ptr->zlib_text_window_bits = window_bits;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1543
}
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1544
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1545
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1546
png_set_text_compression_method(png_structrp png_ptr, int method)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1547
{
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1548
   png_debug(1, "in png_set_text_compression_method");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1549
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1550
   if (png_ptr == NULL)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1551
      return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1552
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1553
   if (method != 8)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1554
      png_warning(png_ptr, "Only compression method 8 is supported by PNG");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1555
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1556
   png_ptr->zlib_text_method = method;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1557
}
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1558
#endif /* WRITE_CUSTOMIZE_ZTXT_COMPRESSION */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1559
/* end of API added to libpng-1.5.4 */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1560
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1562
png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1566
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
   png_ptr->write_row_fn = write_row_fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1570
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1572
png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1573
    write_user_transform_fn)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1575
   png_debug(1, "in png_set_write_user_transform_fn");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1576
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
   if (png_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1579
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
   png_ptr->transformations |= PNG_USER_TRANSFORM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
   png_ptr->write_user_transform_fn = write_user_transform_fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1586
#ifdef PNG_INFO_IMAGE_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
void PNGAPI
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1588
png_write_png(png_structrp png_ptr, png_inforp info_ptr,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1589
    int transforms, voidp params)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
   if (png_ptr == NULL || info_ptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1594
   if ((info_ptr->valid & PNG_INFO_IDAT) == 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1595
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1596
      png_app_error(png_ptr, "no rows for png_write_image to write");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1597
      return;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1598
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1599
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
   /* Write the file header information. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
   png_write_info(png_ptr, info_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
   /* ------ these transformations don't touch the info structure ------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1605
   /* Invert monochrome pixels */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1606
   if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1607
#ifdef PNG_WRITE_INVERT_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1608
      png_set_invert_mono(png_ptr);
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1609
#else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1610
      png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
   /* Shift the pixels up to a legal bit depth and fill in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    * as appropriate to correctly scale the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
    */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1616
   if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1617
#ifdef PNG_WRITE_SHIFT_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1618
      if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1619
         png_set_shift(png_ptr, &info_ptr->sig_bit);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1620
#else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1621
      png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1624
   /* Pack pixels into bytes */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1625
   if ((transforms & PNG_TRANSFORM_PACKING) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1626
#ifdef PNG_WRITE_PACK_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1627
      png_set_packing(png_ptr);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1628
#else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1629
      png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1632
   /* Swap location of alpha bytes from ARGB to RGBA */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1633
   if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1634
#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1635
      png_set_swap_alpha(png_ptr);
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1636
#else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1637
      png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1640
   /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1641
    * RGB, note that the code expects the input color type to be G or RGB; no
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1642
    * alpha channel.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1643
    */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1644
   if ((transforms & (PNG_TRANSFORM_STRIP_FILLER_AFTER|
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1645
      PNG_TRANSFORM_STRIP_FILLER_BEFORE)) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1646
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1647
#ifdef PNG_WRITE_FILLER_SUPPORTED
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1648
      if ((transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1649
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1650
         if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1651
            png_app_error(png_ptr,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1652
               "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported");
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1653
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1654
         /* Continue if ignored - this is the pre-1.6.10 behavior */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1655
         png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1656
      }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1658
      else if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1659
         png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1660
#else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1661
      png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1662
#endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1663
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1664
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1665
   /* Flip BGR pixels to RGB */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1666
   if ((transforms & PNG_TRANSFORM_BGR) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1667
#ifdef PNG_WRITE_BGR_SUPPORTED
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1668
      png_set_bgr(png_ptr);
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1669
#else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1670
      png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1673
   /* Swap bytes of 16-bit files to most significant byte first */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1674
   if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1675
#ifdef PNG_WRITE_SWAP_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1676
      png_set_swap(png_ptr);
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1677
#else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1678
      png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1681
   /* Swap bits of 1, 2, 4 bit packed pixel formats */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1682
   if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1683
#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1684
      png_set_packswap(png_ptr);
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1685
#else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1686
      png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1687
#endif
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1688
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1689
   /* Invert the alpha channel from opacity to transparency */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1690
   if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1691
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1692
      png_set_invert_alpha(png_ptr);
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1693
#else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1694
      png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
   /* ----------------------- end of transformations ------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1699
   /* Write the bits */
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1700
   png_write_image(png_ptr, info_ptr->row_pointers);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
   /* It is REQUIRED to call this to finish writing the rest of the file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
   png_write_end(png_ptr, info_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1705
   PNG_UNUSED(params)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
#endif
29913
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1708
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1709
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1710
#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1711
#ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1712
/* Initialize the write structure - general purpose utility. */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1713
static int
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1714
png_image_write_init(png_imagep image)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1715
{
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1716
   png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1717
          png_safe_error, png_safe_warning);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1718
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1719
   if (png_ptr != NULL)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1720
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1721
      png_infop info_ptr = png_create_info_struct(png_ptr);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1722
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1723
      if (info_ptr != NULL)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1724
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1725
         png_controlp control = png_voidcast(png_controlp,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1726
            png_malloc_warn(png_ptr, (sizeof *control)));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1727
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1728
         if (control != NULL)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1729
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1730
            memset(control, 0, (sizeof *control));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1731
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1732
            control->png_ptr = png_ptr;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1733
            control->info_ptr = info_ptr;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1734
            control->for_write = 1;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1735
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1736
            image->opaque = control;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1737
            return 1;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1738
         }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1739
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1740
         /* Error clean up */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1741
         png_destroy_info_struct(png_ptr, &info_ptr);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1742
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1743
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1744
      png_destroy_write_struct(&png_ptr, NULL);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1745
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1746
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1747
   return png_image_error(image, "png_image_write_: out of memory");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1748
}
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1749
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1750
/* Arguments to png_image_write_main: */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1751
typedef struct
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1752
{
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1753
   /* Arguments: */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1754
   png_imagep      image;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1755
   png_const_voidp buffer;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1756
   png_int_32      row_stride;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1757
   png_const_voidp colormap;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1758
   int             convert_to_8bit;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1759
   /* Local variables: */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1760
   png_const_voidp first_row;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1761
   ptrdiff_t       row_bytes;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1762
   png_voidp       local_row;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1763
} png_image_write_control;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1764
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1765
/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1766
 * do any necessary byte swapping.  The component order is defined by the
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1767
 * png_image format value.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1768
 */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1769
static int
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1770
png_write_image_16bit(png_voidp argument)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1771
{
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1772
   png_image_write_control *display = png_voidcast(png_image_write_control*,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1773
      argument);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1774
   png_imagep image = display->image;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1775
   png_structrp png_ptr = image->opaque->png_ptr;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1776
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1777
   png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1778
      display->first_row);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1779
   png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1780
   png_uint_16p row_end;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1781
   const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1782
   int aindex = 0;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1783
   png_uint_32 y = image->height;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1784
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1785
   if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1786
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1787
#     ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1788
         if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1789
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1790
            aindex = -1;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1791
            ++input_row; /* To point to the first component */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1792
            ++output_row;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1793
         }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1794
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1795
         else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1796
#     endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1797
         aindex = channels;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1798
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1799
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1800
   else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1801
      png_error(png_ptr, "png_write_image: internal call error");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1802
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1803
   /* Work out the output row end and count over this, note that the increment
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1804
    * above to 'row' means that row_end can actually be beyond the end of the
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1805
    * row; this is correct.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1806
    */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1807
   row_end = output_row + image->width * (channels+1);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1808
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1809
   while (y-- > 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1810
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1811
      png_const_uint_16p in_ptr = input_row;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1812
      png_uint_16p out_ptr = output_row;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1813
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1814
      while (out_ptr < row_end)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1815
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1816
         const png_uint_16 alpha = in_ptr[aindex];
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1817
         png_uint_32 reciprocal = 0;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1818
         int c;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1819
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1820
         out_ptr[aindex] = alpha;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1821
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1822
         /* Calculate a reciprocal.  The correct calculation is simply
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1823
          * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1824
          * allows correct rounding by adding .5 before the shift.  'reciprocal'
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1825
          * is only initialized when required.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1826
          */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1827
         if (alpha > 0 && alpha < 65535)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1828
            reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1829
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1830
         c = channels;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1831
         do /* always at least one channel */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1832
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1833
            png_uint_16 component = *in_ptr++;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1834
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1835
            /* The following gives 65535 for an alpha of 0, which is fine,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1836
             * otherwise if 0/0 is represented as some other value there is more
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1837
             * likely to be a discontinuity which will probably damage
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1838
             * compression when moving from a fully transparent area to a
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1839
             * nearly transparent one.  (The assumption here is that opaque
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1840
             * areas tend not to be 0 intensity.)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1841
             */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1842
            if (component >= alpha)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1843
               component = 65535;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1844
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1845
            /* component<alpha, so component/alpha is less than one and
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1846
             * component*reciprocal is less than 2^31.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1847
             */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1848
            else if (component > 0 && alpha < 65535)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1849
            {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1850
               png_uint_32 calc = component * reciprocal;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1851
               calc += 16384; /* round to nearest */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1852
               component = (png_uint_16)(calc >> 15);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1853
            }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1854
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1855
            *out_ptr++ = component;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1856
         }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1857
         while (--c > 0);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1858
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1859
         /* Skip to next component (skip the intervening alpha channel) */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1860
         ++in_ptr;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1861
         ++out_ptr;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1862
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1863
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1864
      png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1865
      input_row += display->row_bytes/(sizeof (png_uint_16));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1866
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1867
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1868
   return 1;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1869
}
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1870
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1871
/* Given 16-bit input (1 to 4 channels) write 8-bit output.  If an alpha channel
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1872
 * is present it must be removed from the components, the components are then
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1873
 * written in sRGB encoding.  No components are added or removed.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1874
 *
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1875
 * Calculate an alpha reciprocal to reverse pre-multiplication.  As above the
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1876
 * calculation can be done to 15 bits of accuracy; however, the output needs to
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1877
 * be scaled in the range 0..255*65535, so include that scaling here.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1878
 */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1879
#define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+(alpha>>1))/alpha)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1880
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1881
static png_byte
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1882
png_unpremultiply(png_uint_32 component, png_uint_32 alpha,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1883
   png_uint_32 reciprocal/*from the above macro*/)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1884
{
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1885
   /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1886
    * is represented as some other value there is more likely to be a
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1887
    * discontinuity which will probably damage compression when moving from a
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1888
    * fully transparent area to a nearly transparent one.  (The assumption here
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1889
    * is that opaque areas tend not to be 0 intensity.)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1890
    *
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1891
    * There is a rounding problem here; if alpha is less than 128 it will end up
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1892
    * as 0 when scaled to 8 bits.  To avoid introducing spurious colors into the
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1893
    * output change for this too.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1894
    */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1895
   if (component >= alpha || alpha < 128)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1896
      return 255;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1897
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1898
   /* component<alpha, so component/alpha is less than one and
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1899
    * component*reciprocal is less than 2^31.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1900
    */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1901
   else if (component > 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1902
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1903
      /* The test is that alpha/257 (rounded) is less than 255, the first value
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1904
       * that becomes 255 is 65407.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1905
       * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1906
       * be exact!)  [Could also test reciprocal != 0]
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1907
       */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1908
      if (alpha < 65407)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1909
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1910
         component *= reciprocal;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1911
         component += 64; /* round to nearest */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1912
         component >>= 7;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1913
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1914
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1915
      else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1916
         component *= 255;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1917
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1918
      /* Convert the component to sRGB. */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1919
      return (png_byte)PNG_sRGB_FROM_LINEAR(component);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1920
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1921
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1922
   else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1923
      return 0;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1924
}
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1925
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1926
static int
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1927
png_write_image_8bit(png_voidp argument)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1928
{
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1929
   png_image_write_control *display = png_voidcast(png_image_write_control*,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1930
      argument);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1931
   png_imagep image = display->image;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1932
   png_structrp png_ptr = image->opaque->png_ptr;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1933
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1934
   png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1935
      display->first_row);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1936
   png_bytep output_row = png_voidcast(png_bytep, display->local_row);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1937
   png_uint_32 y = image->height;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1938
   const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1939
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1940
   if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1941
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1942
      png_bytep row_end;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1943
      int aindex;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1944
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1945
#     ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1946
         if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1947
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1948
            aindex = -1;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1949
            ++input_row; /* To point to the first component */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1950
            ++output_row;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1951
         }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1952
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1953
         else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1954
#     endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1955
         aindex = channels;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1956
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1957
      /* Use row_end in place of a loop counter: */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1958
      row_end = output_row + image->width * (channels+1);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1959
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1960
      while (y-- > 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1961
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1962
         png_const_uint_16p in_ptr = input_row;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1963
         png_bytep out_ptr = output_row;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1964
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1965
         while (out_ptr < row_end)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1966
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1967
            png_uint_16 alpha = in_ptr[aindex];
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1968
            png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1969
            png_uint_32 reciprocal = 0;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1970
            int c;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1971
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1972
            /* Scale and write the alpha channel. */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1973
            out_ptr[aindex] = alphabyte;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1974
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1975
            if (alphabyte > 0 && alphabyte < 255)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1976
               reciprocal = UNP_RECIPROCAL(alpha);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1977
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1978
            c = channels;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1979
            do /* always at least one channel */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1980
               *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1981
            while (--c > 0);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1982
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1983
            /* Skip to next component (skip the intervening alpha channel) */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1984
            ++in_ptr;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1985
            ++out_ptr;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1986
         } /* while out_ptr < row_end */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1987
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1988
         png_write_row(png_ptr, png_voidcast(png_const_bytep,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1989
            display->local_row));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1990
         input_row += display->row_bytes/(sizeof (png_uint_16));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1991
      } /* while y */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1992
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1993
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1994
   else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1995
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1996
      /* No alpha channel, so the row_end really is the end of the row and it
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1997
       * is sufficient to loop over the components one by one.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1998
       */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  1999
      png_bytep row_end = output_row + image->width * channels;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2000
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2001
      while (y-- > 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2002
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2003
         png_const_uint_16p in_ptr = input_row;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2004
         png_bytep out_ptr = output_row;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2005
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2006
         while (out_ptr < row_end)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2007
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2008
            png_uint_32 component = *in_ptr++;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2009
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2010
            component *= 255;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2011
            *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2012
         }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2013
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2014
         png_write_row(png_ptr, output_row);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2015
         input_row += display->row_bytes/(sizeof (png_uint_16));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2016
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2017
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2018
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2019
   return 1;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2020
}
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2021
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2022
static void
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2023
png_image_set_PLTE(png_image_write_control *display)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2024
{
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2025
   const png_imagep image = display->image;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2026
   const void *cmap = display->colormap;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2027
   const int entries = image->colormap_entries > 256 ? 256 :
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2028
      (int)image->colormap_entries;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2029
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2030
   /* NOTE: the caller must check for cmap != NULL and entries != 0 */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2031
   const png_uint_32 format = image->format;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2032
   const int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2033
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2034
#  if defined(PNG_FORMAT_BGR_SUPPORTED) &&\
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2035
      defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2036
      const int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2037
         (format & PNG_FORMAT_FLAG_ALPHA) != 0;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2038
#  else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2039
#     define afirst 0
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2040
#  endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2041
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2042
#  ifdef PNG_FORMAT_BGR_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2043
      const int bgr = (format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2044
#  else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2045
#     define bgr 0
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2046
#  endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2047
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2048
   int i, num_trans;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2049
   png_color palette[256];
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2050
   png_byte tRNS[256];
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2051
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2052
   memset(tRNS, 255, (sizeof tRNS));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2053
   memset(palette, 0, (sizeof palette));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2054
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2055
   for (i=num_trans=0; i<entries; ++i)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2056
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2057
      /* This gets automatically converted to sRGB with reversal of the
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2058
       * pre-multiplication if the color-map has an alpha channel.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2059
       */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2060
      if ((format & PNG_FORMAT_FLAG_LINEAR) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2061
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2062
         png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2063
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2064
         entry += i * channels;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2065
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2066
         if ((channels & 1) != 0) /* no alpha */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2067
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2068
            if (channels >= 3) /* RGB */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2069
            {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2070
               palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2071
                  entry[(2 ^ bgr)]);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2072
               palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2073
                  entry[1]);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2074
               palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2075
                  entry[bgr]);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2076
            }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2077
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2078
            else /* Gray */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2079
               palette[i].blue = palette[i].red = palette[i].green =
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2080
                  (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2081
         }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2082
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2083
         else /* alpha */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2084
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2085
            png_uint_16 alpha = entry[afirst ? 0 : channels-1];
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2086
            png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2087
            png_uint_32 reciprocal = 0;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2088
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2089
            /* Calculate a reciprocal, as in the png_write_image_8bit code above
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2090
             * this is designed to produce a value scaled to 255*65535 when
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2091
             * divided by 128 (i.e. asr 7).
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2092
             */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2093
            if (alphabyte > 0 && alphabyte < 255)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2094
               reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2095
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2096
            tRNS[i] = alphabyte;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2097
            if (alphabyte < 255)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2098
               num_trans = i+1;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2099
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2100
            if (channels >= 3) /* RGB */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2101
            {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2102
               palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2103
                  alpha, reciprocal);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2104
               palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2105
                  reciprocal);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2106
               palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2107
                  reciprocal);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2108
            }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2109
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2110
            else /* gray */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2111
               palette[i].blue = palette[i].red = palette[i].green =
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2112
                  png_unpremultiply(entry[afirst], alpha, reciprocal);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2113
         }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2114
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2115
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2116
      else /* Color-map has sRGB values */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2117
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2118
         png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2119
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2120
         entry += i * channels;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2121
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2122
         switch (channels)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2123
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2124
            case 4:
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2125
               tRNS[i] = entry[afirst ? 0 : 3];
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2126
               if (tRNS[i] < 255)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2127
                  num_trans = i+1;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2128
               /* FALL THROUGH */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2129
            case 3:
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2130
               palette[i].blue = entry[afirst + (2 ^ bgr)];
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2131
               palette[i].green = entry[afirst + 1];
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2132
               palette[i].red = entry[afirst + bgr];
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2133
               break;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2134
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2135
            case 2:
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2136
               tRNS[i] = entry[1 ^ afirst];
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2137
               if (tRNS[i] < 255)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2138
                  num_trans = i+1;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2139
               /* FALL THROUGH */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2140
            case 1:
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2141
               palette[i].blue = palette[i].red = palette[i].green =
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2142
                  entry[afirst];
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2143
               break;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2144
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2145
            default:
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2146
               break;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2147
         }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2148
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2149
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2150
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2151
#  ifdef afirst
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2152
#     undef afirst
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2153
#  endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2154
#  ifdef bgr
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2155
#     undef bgr
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2156
#  endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2157
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2158
   png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2159
      entries);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2160
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2161
   if (num_trans > 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2162
      png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2163
         num_trans, NULL);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2164
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2165
   image->colormap_entries = entries;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2166
}
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2167
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2168
static int
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2169
png_image_write_main(png_voidp argument)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2170
{
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2171
   png_image_write_control *display = png_voidcast(png_image_write_control*,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2172
      argument);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2173
   png_imagep image = display->image;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2174
   png_structrp png_ptr = image->opaque->png_ptr;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2175
   png_inforp info_ptr = image->opaque->info_ptr;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2176
   png_uint_32 format = image->format;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2177
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2178
   /* The following four ints are actually booleans */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2179
   int colormap = (format & PNG_FORMAT_FLAG_COLORMAP);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2180
   int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR); /* input */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2181
   int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2182
   int write_16bit = linear && !colormap && (display->convert_to_8bit == 0);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2183
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2184
#  ifdef PNG_BENIGN_ERRORS_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2185
      /* Make sure we error out on any bad situation */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2186
      png_set_benign_errors(png_ptr, 0/*error*/);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2187
#  endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2188
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2189
   /* Default the 'row_stride' parameter if required. */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2190
   if (display->row_stride == 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2191
      display->row_stride = PNG_IMAGE_ROW_STRIDE(*image);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2192
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2193
   /* Set the required transforms then write the rows in the correct order. */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2194
   if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2195
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2196
      if (display->colormap != NULL && image->colormap_entries > 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2197
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2198
         png_uint_32 entries = image->colormap_entries;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2199
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2200
         png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2201
            entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2202
            PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2203
            PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2204
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2205
         png_image_set_PLTE(display);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2206
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2207
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2208
      else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2209
         png_error(image->opaque->png_ptr,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2210
            "no color-map for color-mapped image");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2211
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2212
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2213
   else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2214
      png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2215
         write_16bit ? 16 : 8,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2216
         ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2217
         ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2218
         PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2219
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2220
   /* Counter-intuitively the data transformations must be called *after*
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2221
    * png_write_info, not before as in the read code, but the 'set' functions
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2222
    * must still be called before.  Just set the color space information, never
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2223
    * write an interlaced image.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2224
    */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2225
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2226
   if (write_16bit != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2227
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2228
      /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2229
      png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2230
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2231
      if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2232
         png_set_cHRM_fixed(png_ptr, info_ptr,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2233
            /* color      x       y */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2234
            /* white */ 31270, 32900,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2235
            /* red   */ 64000, 33000,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2236
            /* green */ 30000, 60000,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2237
            /* blue  */ 15000,  6000
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2238
         );
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2239
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2240
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2241
   else if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2242
      png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2243
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2244
   /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2245
    * space must still be gamma encoded.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2246
    */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2247
   else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2248
      png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2249
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2250
   /* Write the file header. */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2251
   png_write_info(png_ptr, info_ptr);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2252
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2253
   /* Now set up the data transformations (*after* the header is written),
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2254
    * remove the handled transformations from the 'format' flags for checking.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2255
    *
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2256
    * First check for a little endian system if writing 16 bit files.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2257
    */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2258
   if (write_16bit != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2259
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2260
      PNG_CONST png_uint_16 le = 0x0001;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2261
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2262
      if ((*(png_const_bytep) & le) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2263
         png_set_swap(png_ptr);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2264
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2265
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2266
#  ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2267
      if ((format & PNG_FORMAT_FLAG_BGR) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2268
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2269
         if (colormap == 0 && (format & PNG_FORMAT_FLAG_COLOR) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2270
            png_set_bgr(png_ptr);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2271
         format &= ~PNG_FORMAT_FLAG_BGR;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2272
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2273
#  endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2274
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2275
#  ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2276
      if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2277
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2278
         if (colormap == 0 && (format & PNG_FORMAT_FLAG_ALPHA) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2279
            png_set_swap_alpha(png_ptr);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2280
         format &= ~PNG_FORMAT_FLAG_AFIRST;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2281
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2282
#  endif
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2283
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2284
   /* If there are 16 or fewer color-map entries we wrote a lower bit depth
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2285
    * above, but the application data is still byte packed.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2286
    */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2287
   if (colormap != 0 && image->colormap_entries <= 16)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2288
      png_set_packing(png_ptr);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2289
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2290
   /* That should have handled all (both) the transforms. */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2291
   if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2292
         PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2293
      png_error(png_ptr, "png_write_image: unsupported transformation");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2294
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2295
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2296
      png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2297
      ptrdiff_t row_bytes = display->row_stride;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2298
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2299
      if (linear != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2300
         row_bytes *= (sizeof (png_uint_16));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2301
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2302
      if (row_bytes < 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2303
         row += (image->height-1) * (-row_bytes);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2304
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2305
      display->first_row = row;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2306
      display->row_bytes = row_bytes;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2307
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2308
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2309
   /* Apply 'fast' options if the flag is set. */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2310
   if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2311
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2312
      png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2313
      /* NOTE: determined by experiment using pngstest, this reflects some
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2314
       * balance between the time to write the image once and the time to read
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2315
       * it about 50 times.  The speed-up in pngstest was about 10-20% of the
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2316
       * total (user) time on a heavily loaded system.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2317
       */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2318
      png_set_compression_level(png_ptr, 3);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2319
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2320
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2321
   /* Check for the cases that currently require a pre-transform on the row
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2322
    * before it is written.  This only applies when the input is 16-bit and
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2323
    * either there is an alpha channel or it is converted to 8-bit.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2324
    */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2325
   if ((linear != 0 && alpha != 0 ) ||
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2326
       (colormap == 0 && display->convert_to_8bit != 0))
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2327
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2328
      png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2329
         png_get_rowbytes(png_ptr, info_ptr)));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2330
      int result;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2331
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2332
      display->local_row = row;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2333
      if (write_16bit != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2334
         result = png_safe_execute(image, png_write_image_16bit, display);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2335
      else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2336
         result = png_safe_execute(image, png_write_image_8bit, display);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2337
      display->local_row = NULL;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2338
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2339
      png_free(png_ptr, row);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2340
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2341
      /* Skip the 'write_end' on error: */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2342
      if (result == 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2343
         return 0;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2344
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2345
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2346
   /* Otherwise this is the case where the input is in a format currently
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2347
    * supported by the rest of the libpng write code; call it directly.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2348
    */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2349
   else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2350
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2351
      png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2352
      ptrdiff_t row_bytes = display->row_bytes;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2353
      png_uint_32 y = image->height;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2354
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2355
      while (y-- > 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2356
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2357
         png_write_row(png_ptr, row);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2358
         row += row_bytes;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2359
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2360
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2361
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2362
   png_write_end(png_ptr, info_ptr);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2363
   return 1;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2364
}
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2365
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2366
int PNGAPI
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2367
png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2368
   const void *buffer, png_int_32 row_stride, const void *colormap)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2369
{
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2370
   /* Write the image to the given (FILE*). */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2371
   if (image != NULL && image->version == PNG_IMAGE_VERSION)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2372
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2373
      if (file != NULL)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2374
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2375
         if (png_image_write_init(image) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2376
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2377
            png_image_write_control display;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2378
            int result;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2379
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2380
            /* This is slightly evil, but png_init_io doesn't do anything other
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2381
             * than this and we haven't changed the standard IO functions so
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2382
             * this saves a 'safe' function.
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2383
             */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2384
            image->opaque->png_ptr->io_ptr = file;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2385
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2386
            memset(&display, 0, (sizeof display));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2387
            display.image = image;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2388
            display.buffer = buffer;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2389
            display.row_stride = row_stride;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2390
            display.colormap = colormap;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2391
            display.convert_to_8bit = convert_to_8bit;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2392
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2393
            result = png_safe_execute(image, png_image_write_main, &display);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2394
            png_image_free(image);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2395
            return result;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2396
         }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2397
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2398
         else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2399
            return 0;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2400
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2401
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2402
      else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2403
         return png_image_error(image,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2404
            "png_image_write_to_stdio: invalid argument");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2405
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2406
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2407
   else if (image != NULL)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2408
      return png_image_error(image,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2409
         "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2410
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2411
   else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2412
      return 0;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2413
}
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2414
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2415
int PNGAPI
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2416
png_image_write_to_file(png_imagep image, const char *file_name,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2417
   int convert_to_8bit, const void *buffer, png_int_32 row_stride,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2418
   const void *colormap)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2419
{
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2420
   /* Write the image to the named file. */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2421
   if (image != NULL && image->version == PNG_IMAGE_VERSION)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2422
   {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2423
      if (file_name != NULL)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2424
      {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2425
         FILE *fp = fopen(file_name, "wb");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2426
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2427
         if (fp != NULL)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2428
         {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2429
            if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2430
               row_stride, colormap) != 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2431
            {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2432
               int error; /* from fflush/fclose */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2433
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2434
               /* Make sure the file is flushed correctly. */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2435
               if (fflush(fp) == 0 && ferror(fp) == 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2436
               {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2437
                  if (fclose(fp) == 0)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2438
                     return 1;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2439
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2440
                  error = errno; /* from fclose */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2441
               }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2442
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2443
               else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2444
               {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2445
                  error = errno; /* from fflush or ferror */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2446
                  (void)fclose(fp);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2447
               }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2448
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2449
               (void)remove(file_name);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2450
               /* The image has already been cleaned up; this is just used to
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2451
                * set the error (because the original write succeeded).
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2452
                */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2453
               return png_image_error(image, strerror(error));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2454
            }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2455
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2456
            else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2457
            {
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2458
               /* Clean up: just the opened file. */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2459
               (void)fclose(fp);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2460
               (void)remove(file_name);
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2461
               return 0;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2462
            }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2463
         }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2464
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2465
         else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2466
            return png_image_error(image, strerror(errno));
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2467
      }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2468
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2469
      else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2470
         return png_image_error(image,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2471
            "png_image_write_to_file: invalid argument");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2472
   }
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2473
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2474
   else if (image != NULL)
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2475
      return png_image_error(image,
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2476
         "png_image_write_to_file: incorrect PNG_IMAGE_VERSION");
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2477
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2478
   else
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2479
      return 0;
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2480
}
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2481
#endif /* STDIO */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2482
#endif /* SIMPLIFIED_WRITE */
95258013e132 8069198: Upgrade image library
azvegint
parents: 25859
diff changeset
  2483
#endif /* WRITE */