jdk/src/java.desktop/share/native/libsplashscreen/libpng/pngwutil.c
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 10576 jdk/src/share/native/sun/awt/libpng/pngwutil.c@db3409425573
child 29913 95258013e132
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
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
/* pngwutil.c - utilities 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
 *
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    32
 * Last changed in libpng 1.5.4 [July 7, 2011]
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    33
 * Copyright (c) 1998-2011 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"
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    43
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#ifdef PNG_WRITE_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    46
#ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/* Place a 32-bit number into a buffer in PNG byte order.  We work
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * with unsigned numbers for convenience, although one supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * ancillary chunk uses signed (two's complement) numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
void PNGAPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
png_save_uint_32(png_bytep buf, png_uint_32 i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
   buf[0] = (png_byte)((i >> 24) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
   buf[1] = (png_byte)((i >> 16) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
   buf[2] = (png_byte)((i >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
   buf[3] = (png_byte)(i & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    60
#ifdef PNG_SAVE_INT_32_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
/* The png_save_int_32 function assumes integers are stored in two's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * complement format.  If this isn't the case, then this routine needs to
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    63
 * be modified to write data in two's complement format.  Note that,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    64
 * the following works correctly even if png_int_32 has more than 32 bits
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    65
 * (compare the more complex code required on read for sign extention.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
void PNGAPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
png_save_int_32(png_bytep buf, png_int_32 i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
   buf[0] = (png_byte)((i >> 24) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
   buf[1] = (png_byte)((i >> 16) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
   buf[2] = (png_byte)((i >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
   buf[3] = (png_byte)(i & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
}
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    75
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
/* Place a 16-bit number into a buffer in PNG byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * The parameter is declared unsigned int, not png_uint_16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * just to avoid potential problems on pre-ANSI C compilers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
void PNGAPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
png_save_uint_16(png_bytep buf, unsigned int i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
   buf[0] = (png_byte)((i >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
   buf[1] = (png_byte)(i & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
}
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    87
#endif
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    88
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    89
/* Simple function to write the signature.  If we have already written
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    90
 * the magic bytes of the signature, or more likely, the PNG stream is
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    91
 * being embedded into another stream and doesn't need its own signature,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    92
 * we should call png_set_sig_bytes() to tell libpng how many of the
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    93
 * bytes have already been written.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    94
 */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    95
void PNGAPI
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    96
png_write_sig(png_structp png_ptr)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    97
{
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    98
   png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
    99
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   100
#ifdef PNG_IO_STATE_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   101
   /* Inform the I/O callback that the signature is being written */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   102
   png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   103
#endif
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   104
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   105
   /* Write the rest of the 8 byte signature */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   106
   png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   107
      (png_size_t)(8 - png_ptr->sig_bytes));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   108
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   109
   if (png_ptr->sig_bytes < 3)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   110
      png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   111
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
/* Write a PNG chunk all at once.  The type is an array of ASCII characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * representing the chunk name.  The array must be at least 4 bytes in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * length, and does not need to be null terminated.  To be safe, pass the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * pre-defined chunk names here, and if you need a new one, define it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * where the others are defined.  The length is the length of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * All the data must be present.  If that is not possible, use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * functions instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
void PNGAPI
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   123
png_write_chunk(png_structp png_ptr, png_const_bytep chunk_name,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   124
   png_const_bytep data, png_size_t length)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   126
   if (png_ptr == NULL)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   127
      return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   128
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
   png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   130
   png_write_chunk_data(png_ptr, data, (png_size_t)length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
   png_write_chunk_end(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
/* Write the start of a PNG chunk.  The type is the chunk type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * The total_length is the sum of the lengths of all the data you will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * passing in png_write_chunk_data().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
void PNGAPI
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   139
png_write_chunk_start(png_structp png_ptr, png_const_bytep chunk_name,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   140
    png_uint_32 length)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   142
   png_byte buf[8];
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   143
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   144
   png_debug2(0, "Writing %s chunk, length = %lu", chunk_name,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   145
      (unsigned long)length);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   146
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   147
   if (png_ptr == NULL)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   148
      return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   149
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   150
#ifdef PNG_IO_STATE_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   151
   /* Inform the I/O callback that the chunk header is being written.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   152
    * PNG_IO_CHUNK_HDR requires a single I/O call.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   153
    */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   154
   png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   155
#endif
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   156
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   157
   /* Write the length and the chunk name */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
   png_save_uint_32(buf, length);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   159
   png_memcpy(buf + 4, chunk_name, 4);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   160
   png_write_data(png_ptr, buf, (png_size_t)8);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   161
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   162
   /* Put the chunk name into png_ptr->chunk_name */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   163
   png_memcpy(png_ptr->chunk_name, chunk_name, 4);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   164
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   165
   /* Reset the crc and run it over the chunk name */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
   png_reset_crc(png_ptr);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   167
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   168
   png_calculate_crc(png_ptr, chunk_name, 4);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   169
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   170
#ifdef PNG_IO_STATE_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   171
   /* Inform the I/O callback that chunk data will (possibly) be written.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   172
    * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   173
    */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   174
   png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   175
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
/* Write the data of a PNG chunk started with png_write_chunk_start().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * Note that multiple calls to this function are allowed, and that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * sum of the lengths from these calls *must* add up to the total_length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * given to png_write_chunk_start().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
void PNGAPI
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   184
png_write_chunk_data(png_structp png_ptr, png_const_bytep data,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   185
    png_size_t length)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   187
   /* Write the data, and run the CRC over it */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   188
   if (png_ptr == NULL)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   189
      return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   190
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
   if (data != NULL && length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   193
      png_write_data(png_ptr, data, length);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   194
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   195
      /* Update the CRC after writing the data,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   196
       * in case that the user I/O routine alters it.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   197
       */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
      png_calculate_crc(png_ptr, data, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
/* Finish a chunk started with png_write_chunk_start(). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
void PNGAPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
png_write_chunk_end(png_structp png_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
   png_byte buf[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   208
   if (png_ptr == NULL) return;
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_IO_STATE_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   211
   /* Inform the I/O callback that the chunk CRC is being written.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   212
    * PNG_IO_CHUNK_CRC requires a single I/O function call.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   213
    */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   214
   png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   215
#endif
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   216
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   217
   /* Write the crc in a single operation */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
   png_save_uint_32(buf, png_ptr->crc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
   png_write_data(png_ptr, buf, (png_size_t)4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   223
/* Initialize the compressor for the appropriate type of compression. */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   224
static void
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   225
png_zlib_claim(png_structp png_ptr, png_uint_32 state)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   227
   if (!(png_ptr->zlib_state & PNG_ZLIB_IN_USE))
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   228
   {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   229
      /* If already initialized for 'state' do not re-init. */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   230
      if (png_ptr->zlib_state != state)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   231
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   232
         int ret = Z_OK;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   233
         png_const_charp who = "-";
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   234
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   235
         /* If actually initialized for another state do a deflateEnd. */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   236
         if (png_ptr->zlib_state != PNG_ZLIB_UNINITIALIZED)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   237
         {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   238
            ret = deflateEnd(&png_ptr->zstream);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   239
            who = "end";
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   240
            png_ptr->zlib_state = PNG_ZLIB_UNINITIALIZED;
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
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   243
         /* zlib itself detects an incomplete state on deflateEnd */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   244
         if (ret == Z_OK) switch (state)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   245
         {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   246
#           ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   247
               case PNG_ZLIB_FOR_TEXT:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   248
                  ret = deflateInit2(&png_ptr->zstream,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   249
                     png_ptr->zlib_text_level, png_ptr->zlib_text_method,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   250
                     png_ptr->zlib_text_window_bits,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   251
                     png_ptr->zlib_text_mem_level, png_ptr->zlib_text_strategy);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   252
                  who = "text";
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   253
                  break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   254
#           endif
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
            case PNG_ZLIB_FOR_IDAT:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   257
               ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   258
                   png_ptr->zlib_method, png_ptr->zlib_window_bits,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   259
                   png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   260
               who = "IDAT";
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   261
               break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   262
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   263
            default:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   264
               png_error(png_ptr, "invalid zlib state");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   265
         }
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
         if (ret == Z_OK)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   268
            png_ptr->zlib_state = state;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   269
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   270
         else /* an error in deflateEnd or deflateInit2 */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   271
         {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   272
            size_t pos = 0;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   273
            char msg[64];
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   274
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   275
            pos = png_safecat(msg, sizeof msg, pos,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   276
               "zlib failed to initialize compressor (");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   277
            pos = png_safecat(msg, sizeof msg, pos, who);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   278
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   279
            switch (ret)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   280
            {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   281
               case Z_VERSION_ERROR:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   282
                  pos = png_safecat(msg, sizeof msg, pos, ") version error");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   283
                  break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   284
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   285
               case Z_STREAM_ERROR:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   286
                  pos = png_safecat(msg, sizeof msg, pos, ") stream error");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   287
                  break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   288
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   289
               case Z_MEM_ERROR:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   290
                  pos = png_safecat(msg, sizeof msg, pos, ") memory error");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   291
                  break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   292
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   293
               default:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   294
                  pos = png_safecat(msg, sizeof msg, pos, ") unknown error");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   295
                  break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   296
            }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   297
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   298
            png_error(png_ptr, msg);
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
      }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   301
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   302
      /* Here on success, claim the zstream: */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   303
      png_ptr->zlib_state |= PNG_ZLIB_IN_USE;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   304
   }
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
   else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   307
      png_error(png_ptr, "zstream already in use (internal error)");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   310
/* The opposite: release the stream.  It is also reset, this API will warn on
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   311
 * error but will not fail.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   312
 */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   313
static void
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   314
png_zlib_release(png_structp png_ptr)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   315
{
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   316
   if (png_ptr->zlib_state & PNG_ZLIB_IN_USE)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   317
   {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   318
      int ret = deflateReset(&png_ptr->zstream);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   319
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   320
      png_ptr->zlib_state &= ~PNG_ZLIB_IN_USE;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   321
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   322
      if (ret != Z_OK)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   323
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   324
         png_const_charp err;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   325
         PNG_WARNING_PARAMETERS(p)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   326
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   327
         switch (ret)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   328
         {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   329
            case Z_VERSION_ERROR:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   330
               err = "version";
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   331
               break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   332
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   333
            case Z_STREAM_ERROR:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   334
               err = "stream";
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   335
               break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   336
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   337
            case Z_MEM_ERROR:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   338
               err = "memory";
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   339
               break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   340
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   341
            default:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   342
               err = "unknown";
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   343
               break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   344
         }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   345
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   346
         png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_d, ret);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   347
         png_warning_parameter(p, 2, err);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   348
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   349
         if (png_ptr->zstream.msg)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   350
            err = png_ptr->zstream.msg;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   351
         else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   352
            err = "[no zlib message]";
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   353
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   354
         png_warning_parameter(p, 3, err);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   355
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   356
         png_formatted_warning(png_ptr, p,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   357
            "zlib failed to reset compressor: @1(@2): @3");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   358
      }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   359
   }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   360
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   361
   else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   362
      png_warning(png_ptr, "zstream not in use (internal error)");
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
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   365
#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   366
/* This pair of functions encapsulates the operation of (a) compressing a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
 * text string, and (b) issuing it later as a series of chunk data writes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
 * The compression_state structure is shared context for these functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
 * set up by the caller in order to make the whole mess thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
typedef struct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   374
   png_const_bytep input;   /* The uncompressed input data */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   375
   png_size_t input_len;    /* Its length */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   376
   int num_output_ptr;      /* Number of output pointers used */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   377
   int max_output_ptr;      /* Size of output_ptr */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   378
   png_bytep *output_ptr;   /* Array of pointers to output */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
} compression_state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   381
/* Compress given text into storage in the png_ptr structure */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
static int /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
png_text_compress(png_structp png_ptr,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   384
    png_const_charp text, png_size_t text_len, int compression,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   385
    compression_state *comp)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
   int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
   comp->num_output_ptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
   comp->max_output_ptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
   comp->output_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
   comp->input = NULL;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   393
   comp->input_len = text_len;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   394
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   395
   /* We may just want to pass the text right through */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
   if (compression == PNG_TEXT_COMPRESSION_NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   398
      comp->input = (png_const_bytep)text;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   399
      return((int)text_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
   if (compression >= PNG_TEXT_COMPRESSION_LAST)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   404
      PNG_WARNING_PARAMETERS(p)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   405
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   406
      png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_d,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   407
         compression);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   408
      png_formatted_warning(png_ptr, p, "Unknown compression type @1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
   /* We can't write the chunk until we find out how much data we have,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    * which means we need to run the compressor first and save the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    * output.  This shouldn't be a problem, as the vast majority of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    * comments should be reasonable, but we will set up an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    * malloc'd pointers to be sure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    * If we knew the application was well behaved, we could simplify this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    * greatly by assuming we can always malloc an output buffer large
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    * and malloc this directly.  The only time this would be a bad idea is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    * if we can't malloc more than 64K and we have 64K of random input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    * data, or if the input string is incredibly large (although this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    * wouldn't cause a failure, just a slowdown due to swapping).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   425
   png_zlib_claim(png_ptr, PNG_ZLIB_FOR_TEXT);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   426
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   427
   /* Set up the compression buffers */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   428
   /* TODO: the following cast hides a potential overflow problem. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
   png_ptr->zstream.avail_in = (uInt)text_len;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   430
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   431
   /* NOTE: assume zlib doesn't overwrite the input */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
   png_ptr->zstream.next_in = (Bytef *)text;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   433
   png_ptr->zstream.avail_out = png_ptr->zbuf_size;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   434
   png_ptr->zstream.next_out = png_ptr->zbuf;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   435
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   436
   /* This is the same compression loop as in png_write_row() */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
   do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   439
      /* Compress the data */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
      ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   441
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
      if (ret != Z_OK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   444
         /* Error */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         if (png_ptr->zstream.msg != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            png_error(png_ptr, png_ptr->zstream.msg);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   447
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            png_error(png_ptr, "zlib error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   451
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   452
      /* Check to see if we need more room */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
      if (!(png_ptr->zstream.avail_out))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   455
         /* Make sure the output array has room */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         if (comp->num_output_ptr >= comp->max_output_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            int old_max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            old_max = comp->max_output_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            comp->max_output_ptr = comp->num_output_ptr + 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            if (comp->output_ptr != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   464
               png_bytepp old_ptr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
               old_ptr = comp->output_ptr;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   467
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   468
               comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   469
                   (png_alloc_size_t)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   470
                   (comp->max_output_ptr * png_sizeof(png_charpp)));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   471
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
               png_memcpy(comp->output_ptr, old_ptr, old_max
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   473
                   * png_sizeof(png_charp));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   474
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
               png_free(png_ptr, old_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            else
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   478
               comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   479
                   (png_alloc_size_t)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   480
                   (comp->max_output_ptr * png_sizeof(png_charp)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   483
         /* Save the data */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   484
         comp->output_ptr[comp->num_output_ptr] =
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   485
             (png_bytep)png_malloc(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   486
             (png_alloc_size_t)png_ptr->zbuf_size);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   487
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   489
             png_ptr->zbuf_size);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   490
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         comp->num_output_ptr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         /* and reset the buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         png_ptr->zstream.next_out = png_ptr->zbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   497
   /* Continue until we don't have any more to compress */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
   } while (png_ptr->zstream.avail_in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   500
   /* Finish the compression */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
   do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   503
      /* Tell zlib we are finished */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
      ret = deflate(&png_ptr->zstream, Z_FINISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
      if (ret == Z_OK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   508
         /* Check to see if we need more room */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         if (!(png_ptr->zstream.avail_out))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
         {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   511
            /* Check to make sure our output array has room */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            if (comp->num_output_ptr >= comp->max_output_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
               int old_max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
               old_max = comp->max_output_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
               comp->max_output_ptr = comp->num_output_ptr + 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
               if (comp->output_ptr != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
               {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   520
                  png_bytepp old_ptr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                  old_ptr = comp->output_ptr;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   523
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                  /* This could be optimized to realloc() */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   525
                  comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   526
                      (png_alloc_size_t)(comp->max_output_ptr *
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   527
                      png_sizeof(png_charp)));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   528
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                  png_memcpy(comp->output_ptr, old_ptr,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   530
                      old_max * png_sizeof(png_charp));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   531
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                  png_free(png_ptr, old_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
               }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   534
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
               else
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   536
                  comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   537
                      (png_alloc_size_t)(comp->max_output_ptr *
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   538
                      png_sizeof(png_charp)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   541
            /* Save the data */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            comp->output_ptr[comp->num_output_ptr] =
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   543
                (png_bytep)png_malloc(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   544
                (png_alloc_size_t)png_ptr->zbuf_size);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   545
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   547
                png_ptr->zbuf_size);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   548
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            comp->num_output_ptr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            /* and reset the buffer pointers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            png_ptr->zstream.next_out = png_ptr->zbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
      else if (ret != Z_STREAM_END)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   558
         /* We got an error */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
         if (png_ptr->zstream.msg != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            png_error(png_ptr, png_ptr->zstream.msg);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   561
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            png_error(png_ptr, "zlib error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
   } while (ret != Z_STREAM_END);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   567
   /* Text length is number of buffers plus last buffer */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
   text_len = png_ptr->zbuf_size * comp->num_output_ptr;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   569
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
   if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
      text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
   return((int)text_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   576
/* Ship the compressed text out via chunk writes */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
static void /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
   int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   582
   /* Handle the no-compression case */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
   if (comp->input)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   585
      png_write_chunk_data(png_ptr, comp->input, comp->input_len);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   586
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   587
      return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   590
#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   591
   if (comp->input_len >= 2 && comp->input_len < 16384)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   592
   {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   593
      unsigned int z_cmf;  /* zlib compression method and flags */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   594
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   595
      /* Optimize the CMF field in the zlib stream.  This hack of the zlib
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   596
       * stream is compliant to the stream specification.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   597
       */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   598
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   599
      if (comp->num_output_ptr)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   600
        z_cmf = comp->output_ptr[0][0];
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   601
      else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   602
        z_cmf = png_ptr->zbuf[0];
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   603
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   604
      if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   605
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   606
         unsigned int z_cinfo;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   607
         unsigned int half_z_window_size;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   608
         png_size_t uncompressed_text_size = comp->input_len;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   609
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   610
         z_cinfo = z_cmf >> 4;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   611
         half_z_window_size = 1 << (z_cinfo + 7);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   612
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   613
         while (uncompressed_text_size <= half_z_window_size &&
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   614
             half_z_window_size >= 256)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   615
         {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   616
            z_cinfo--;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   617
            half_z_window_size >>= 1;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   618
         }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   619
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   620
         z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   621
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   622
         if (comp->num_output_ptr)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   623
         {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   624
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   625
           if (comp->output_ptr[0][0] != z_cmf)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   626
           {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   627
              int tmp;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   628
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   629
              comp->output_ptr[0][0] = (png_byte)z_cmf;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   630
              tmp = comp->output_ptr[0][1] & 0xe0;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   631
              tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   632
              comp->output_ptr[0][1] = (png_byte)tmp;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   633
           }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   634
         }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   635
         else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   636
         {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   637
            int tmp;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   638
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   639
            png_ptr->zbuf[0] = (png_byte)z_cmf;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   640
            tmp = png_ptr->zbuf[1] & 0xe0;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   641
            tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   642
            png_ptr->zbuf[1] = (png_byte)tmp;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   643
         }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   644
      }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   645
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   646
      else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   647
         png_error(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   648
             "Invalid zlib compression method or flags in non-IDAT chunk");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   649
   }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   650
#endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   651
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   652
   /* Write saved output buffers, if any */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
   for (i = 0; i < comp->num_output_ptr; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   655
      png_write_chunk_data(png_ptr, comp->output_ptr[i],
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   656
          (png_size_t)png_ptr->zbuf_size);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   657
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
      png_free(png_ptr, comp->output_ptr[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   660
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
   if (comp->max_output_ptr != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
      png_free(png_ptr, comp->output_ptr);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   663
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   664
   /* Write anything left in zbuf */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
   if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
      png_write_chunk_data(png_ptr, png_ptr->zbuf,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   667
          (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   668
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   669
   /* Reset zlib for another zTXt/iTXt or image data */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   670
   png_zlib_release(png_ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
}
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   672
#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
/* Write the IHDR chunk, and update the png_struct with the necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
 * information.  Note that the rest of this code depends upon this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
 * information being correct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
void /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   680
    int bit_depth, int color_type, int compression_type, int filter_type,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   681
    int interlace_type)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
   PNG_IHDR;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   684
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   685
   png_byte buf[13]; /* Buffer to store the IHDR info */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   686
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   687
   png_debug(1, "in png_write_IHDR");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   688
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
   /* Check that we have valid input data from the application info */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
   switch (color_type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
      case PNG_COLOR_TYPE_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
         switch (bit_depth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            case 8:
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   699
#ifdef PNG_WRITE_16BIT_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   700
            case 16:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   701
#endif
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   702
               png_ptr->channels = 1; break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   703
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   704
            default:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   705
               png_error(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   706
                   "Invalid bit depth for grayscale image");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
         break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   709
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
      case PNG_COLOR_TYPE_RGB:
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   711
#ifdef PNG_WRITE_16BIT_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
         if (bit_depth != 8 && bit_depth != 16)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   713
#else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   714
         if (bit_depth != 8)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   715
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            png_error(png_ptr, "Invalid bit depth for RGB image");
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   717
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
         png_ptr->channels = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
         break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   720
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
      case PNG_COLOR_TYPE_PALETTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
         switch (bit_depth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            case 4:
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   727
            case 8:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   728
               png_ptr->channels = 1;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   729
               break;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   730
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   731
            default:
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   732
               png_error(png_ptr, "Invalid bit depth for paletted image");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
         break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   735
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
      case PNG_COLOR_TYPE_GRAY_ALPHA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
         if (bit_depth != 8 && bit_depth != 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   739
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
         png_ptr->channels = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
         break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   742
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
      case PNG_COLOR_TYPE_RGB_ALPHA:
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   744
#ifdef PNG_WRITE_16BIT_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
         if (bit_depth != 8 && bit_depth != 16)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   746
#else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   747
         if (bit_depth != 8)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   748
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            png_error(png_ptr, "Invalid bit depth for RGBA image");
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   750
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         png_ptr->channels = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   753
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
      default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
         png_error(png_ptr, "Invalid image color type specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
      png_warning(png_ptr, "Invalid compression type specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
      compression_type = PNG_COMPRESSION_TYPE_BASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
   /* Write filter_method 64 (intrapixel differencing) only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    * 2. Libpng did not write a PNG signature (this filter_method is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    *    used in PNG datastreams that are embedded in MNG datastreams) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    * 3. The application called png_permit_mng_features with a mask that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    *    included PNG_FLAG_MNG_FILTER_64 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    * 4. The filter_method is 64 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    * 5. The color_type is RGB or RGBA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
   if (
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   774
#ifdef PNG_MNG_FEATURES_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   775
       !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   776
       ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   777
       (color_type == PNG_COLOR_TYPE_RGB ||
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   778
        color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   779
       (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   781
       filter_type != PNG_FILTER_TYPE_BASE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
      png_warning(png_ptr, "Invalid filter type specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
      filter_type = PNG_FILTER_TYPE_BASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
   if (interlace_type != PNG_INTERLACE_NONE &&
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   789
       interlace_type != PNG_INTERLACE_ADAM7)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
      png_warning(png_ptr, "Invalid interlace type specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
      interlace_type = PNG_INTERLACE_ADAM7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
   interlace_type=PNG_INTERLACE_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   798
   /* Save the relevent information */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
   png_ptr->bit_depth = (png_byte)bit_depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
   png_ptr->color_type = (png_byte)color_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
   png_ptr->interlaced = (png_byte)interlace_type;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   802
#ifdef PNG_MNG_FEATURES_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
   png_ptr->filter_type = (png_byte)filter_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
   png_ptr->compression_type = (png_byte)compression_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
   png_ptr->width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
   png_ptr->height = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
   png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
   png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   811
   /* Set the usr info, so any transformations can modify it */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
   png_ptr->usr_width = png_ptr->width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
   png_ptr->usr_bit_depth = png_ptr->bit_depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
   png_ptr->usr_channels = png_ptr->channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   816
   /* Pack the header information into the buffer */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
   png_save_uint_32(buf, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
   png_save_uint_32(buf + 4, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
   buf[8] = (png_byte)bit_depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
   buf[9] = (png_byte)color_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
   buf[10] = (png_byte)compression_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
   buf[11] = (png_byte)filter_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
   buf[12] = (png_byte)interlace_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   825
   /* Write the chunk */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   826
   png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   827
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   828
   /* Initialize zlib with PNG info */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
   png_ptr->zstream.zalloc = png_zalloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
   png_ptr->zstream.zfree = png_zfree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
   png_ptr->zstream.opaque = (voidpf)png_ptr;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   832
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
   if (!(png_ptr->do_filter))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   836
          png_ptr->bit_depth < 8)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         png_ptr->do_filter = PNG_FILTER_NONE;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   838
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
         png_ptr->do_filter = PNG_ALL_FILTERS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   842
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
      if (png_ptr->do_filter != PNG_FILTER_NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
         png_ptr->zlib_strategy = Z_FILTERED;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   847
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
         png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   851
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
      png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   854
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
      png_ptr->zlib_mem_level = 8;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   857
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
      png_ptr->zlib_window_bits = 15;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   860
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
   if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
      png_ptr->zlib_method = 8;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   863
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   864
#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   865
#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   866
   if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_STRATEGY))
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   867
      png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   868
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   869
   if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_LEVEL))
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   870
      png_ptr->zlib_text_level = png_ptr->zlib_level;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   871
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   872
   if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL))
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   873
      png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   874
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   875
   if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS))
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   876
      png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   877
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   878
   if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_METHOD))
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   879
      png_ptr->zlib_text_method = png_ptr->zlib_method;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   880
#else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   881
   png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   882
   png_ptr->zlib_text_level = png_ptr->zlib_level;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   883
   png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   884
   png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   885
   png_ptr->zlib_text_method = png_ptr->zlib_method;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   886
#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   887
#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   888
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   889
   /* Record that the compressor has not yet been initialized. */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   890
   png_ptr->zlib_state = PNG_ZLIB_UNINITIALIZED;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   891
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   892
   png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   895
/* Write the palette.  We are careful not to trust png_color to be in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
 * correct order for PNG, so people can redefine it to any convenient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
 * structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
void /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   900
png_write_PLTE(png_structp png_ptr, png_const_colorp palette,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   901
    png_uint_32 num_pal)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
   PNG_PLTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
   png_uint_32 i;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   905
   png_const_colorp pal_ptr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
   png_byte buf[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   908
   png_debug(1, "in png_write_PLTE");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   909
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
   if ((
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   911
#ifdef PNG_MNG_FEATURES_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   912
       !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   914
       num_pal == 0) || num_pal > 256)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   916
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   917
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   918
         png_error(png_ptr, "Invalid number of colors in palette");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   919
      }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   920
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   921
      else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   922
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   923
         png_warning(png_ptr, "Invalid number of colors in palette");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   924
         return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   925
      }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
   if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
      png_warning(png_ptr,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   931
          "Ignoring request to write a PLTE chunk in grayscale PNG");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   932
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
   png_ptr->num_palette = (png_uint_16)num_pal;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   937
   png_debug1(3, "num_palette = %d", png_ptr->num_palette);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   938
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   939
   png_write_chunk_start(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   940
#ifdef PNG_POINTER_INDEXING_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   941
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
   for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
      buf[0] = pal_ptr->red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
      buf[1] = pal_ptr->green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
      buf[2] = pal_ptr->blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
      png_write_chunk_data(png_ptr, buf, (png_size_t)3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   949
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
#else
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   951
   /* This is a little slower but some buggy compilers need to do this
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   952
    * instead
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   953
    */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
   pal_ptr=palette;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   955
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
   for (i = 0; i < num_pal; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
      buf[0] = pal_ptr[i].red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
      buf[1] = pal_ptr[i].green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
      buf[2] = pal_ptr[i].blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
      png_write_chunk_data(png_ptr, buf, (png_size_t)3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   963
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
   png_write_chunk_end(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
   png_ptr->mode |= PNG_HAVE_PLTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   969
/* Write an IDAT chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
void /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
   PNG_IDAT;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   974
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   975
   png_debug(1, "in png_write_IDAT");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   976
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   977
#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
   if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
       png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   981
      /* Optimize the CMF field in the zlib stream.  This hack of the zlib
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   982
       * stream is compliant to the stream specification.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   983
       */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
      unsigned int z_cmf = data[0];  /* zlib compression method and flags */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   985
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
      if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   988
         /* Avoid memory underflows and multiplication overflows.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   989
          *
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   990
          * The conditions below are practically always satisfied;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   991
          * however, they still must be checked.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   992
          */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
         if (length >= 2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
             png_ptr->height < 16384 && png_ptr->width < 16384)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
         {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   996
            /* Compute the maximum possible length of the datastream */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   997
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   998
            /* Number of pixels, plus for each row a filter byte
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
   999
             * and possibly a padding byte, so increase the maximum
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1000
             * size to account for these.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1001
             */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1002
            unsigned int z_cinfo;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1003
            unsigned int half_z_window_size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            png_uint_32 uncompressed_idat_size = png_ptr->height *
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1005
                ((png_ptr->width *
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1006
                png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1007
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1008
            /* If it's interlaced, each block of 8 rows is sent as up to
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1009
             * 14 rows, i.e., 6 additional rows, each with a filter byte
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1010
             * and possibly a padding byte
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1011
             */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1012
            if (png_ptr->interlaced)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1013
               uncompressed_idat_size += ((png_ptr->height + 7)/8) *
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1014
                   (png_ptr->bit_depth < 8 ? 12 : 6);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1015
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1016
            z_cinfo = z_cmf >> 4;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1017
            half_z_window_size = 1 << (z_cinfo + 7);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1018
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            while (uncompressed_idat_size <= half_z_window_size &&
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1020
                half_z_window_size >= 256)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
               z_cinfo--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
               half_z_window_size >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1025
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1027
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1028
            if (data[0] != z_cmf)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1030
               int tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
               data[0] = (png_byte)z_cmf;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1032
               tmp = data[1] & 0xe0;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1033
               tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1034
               data[1] = (png_byte)tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1038
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
         png_error(png_ptr,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1041
             "Invalid zlib compression method or flags in IDAT");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1043
#endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1044
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1045
   png_write_chunk(png_ptr, png_IDAT, data, length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
   png_ptr->mode |= PNG_HAVE_IDAT;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1047
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1048
   /* Prior to 1.5.4 this code was replicated in every caller (except at the
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1049
    * end, where it isn't technically necessary).  Since this function has
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1050
    * flushed the data we can safely reset the zlib output buffer here.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1051
    */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1052
   png_ptr->zstream.next_out = png_ptr->zbuf;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1053
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1056
/* Write an IEND chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
void /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
png_write_IEND(png_structp png_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
   PNG_IEND;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1061
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1062
   png_debug(1, "in png_write_IEND");
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
   png_write_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
   png_ptr->mode |= PNG_HAVE_IEND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1068
#ifdef PNG_WRITE_gAMA_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1069
/* Write a gAMA chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
void /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
   PNG_gAMA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
   png_byte buf[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1076
   png_debug(1, "in png_write_gAMA");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1077
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
   /* file_gamma is saved in 1/100,000ths */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
   png_save_uint_32(buf, (png_uint_32)file_gamma);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1080
   png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1083
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1084
#ifdef PNG_WRITE_sRGB_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1085
/* Write a sRGB chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
void /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
png_write_sRGB(png_structp png_ptr, int srgb_intent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
   PNG_sRGB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
   png_byte buf[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1092
   png_debug(1, "in png_write_sRGB");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1093
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1094
   if (srgb_intent >= PNG_sRGB_INTENT_LAST)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1095
      png_warning(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1096
          "Invalid sRGB rendering intent specified");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1097
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
   buf[0]=(png_byte)srgb_intent;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1099
   png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1103
#ifdef PNG_WRITE_iCCP_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1104
/* Write an iCCP chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
void /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1106
png_write_iCCP(png_structp png_ptr, png_const_charp name, int compression_type,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1107
    png_const_charp profile, int profile_len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
   PNG_iCCP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
   png_size_t name_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
   png_charp new_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
   compression_state comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
   int embedded_profile_len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1115
   png_debug(1, "in png_write_iCCP");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
   comp.num_output_ptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
   comp.max_output_ptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
   comp.output_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
   comp.input = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
   comp.input_len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1123
   if ((name_len = png_check_keyword(png_ptr, name, &new_name)) == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
      png_warning(png_ptr, "Unknown compression type in iCCP chunk");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
   if (profile == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
      profile_len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
   if (profile_len > 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
      embedded_profile_len =
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1134
          ((*( (png_const_bytep)profile    ))<<24) |
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1135
          ((*( (png_const_bytep)profile + 1))<<16) |
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1136
          ((*( (png_const_bytep)profile + 2))<< 8) |
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1137
          ((*( (png_const_bytep)profile + 3))    );
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1138
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1139
   if (embedded_profile_len < 0)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1140
   {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1141
      png_warning(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1142
          "Embedded profile length in iCCP chunk is negative");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1143
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1144
      png_free(png_ptr, new_name);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1145
      return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1146
   }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
   if (profile_len < embedded_profile_len)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1149
   {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1150
      png_warning(png_ptr,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
          "Embedded profile length too large in iCCP chunk");
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1152
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1153
      png_free(png_ptr, new_name);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1154
      return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1155
   }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
   if (profile_len > embedded_profile_len)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1158
   {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1159
      png_warning(png_ptr,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
          "Truncating profile to actual length in iCCP chunk");
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1161
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1162
      profile_len = embedded_profile_len;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1163
   }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
   if (profile_len)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1166
      profile_len = png_text_compress(png_ptr, profile,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1167
          (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1168
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1169
   /* Make sure we include the NULL after the name and the compression type */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1170
   png_write_chunk_start(png_ptr, png_iCCP,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1171
       (png_uint_32)(name_len + profile_len + 2));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1172
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1173
   new_name[name_len + 1] = 0x00;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1174
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1175
   png_write_chunk_data(png_ptr, (png_bytep)new_name,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1176
       (png_size_t)(name_len + 2));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
   if (profile_len)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1179
   {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1180
      comp.input_len = profile_len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
      png_write_compressed_data_out(png_ptr, &comp);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1182
   }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
   png_write_chunk_end(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
   png_free(png_ptr, new_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1189
#ifdef PNG_WRITE_sPLT_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1190
/* Write a sPLT chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
void /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1192
png_write_sPLT(png_structp png_ptr, png_const_sPLT_tp spalette)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
   PNG_sPLT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
   png_size_t name_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
   png_charp new_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
   png_byte entrybuf[10];
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1198
   png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1199
   png_size_t palette_size = entry_size * spalette->nentries;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
   png_sPLT_entryp ep;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1201
#ifndef PNG_POINTER_INDEXING_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
   int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1205
   png_debug(1, "in png_write_sPLT");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1206
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1207
   if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1209
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1210
   /* Make sure we include the NULL after the name */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1211
   png_write_chunk_start(png_ptr, png_sPLT,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1212
       (png_uint_32)(name_len + 2 + palette_size));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1213
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1214
   png_write_chunk_data(png_ptr, (png_bytep)new_name,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1215
       (png_size_t)(name_len + 1));
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
   png_write_chunk_data(png_ptr, &spalette->depth, (png_size_t)1);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1218
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1219
   /* Loop through each palette entry, writing appropriately */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1220
#ifdef PNG_POINTER_INDEXING_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1221
   for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1223
      if (spalette->depth == 8)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1224
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1225
         entrybuf[0] = (png_byte)ep->red;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1226
         entrybuf[1] = (png_byte)ep->green;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1227
         entrybuf[2] = (png_byte)ep->blue;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1228
         entrybuf[3] = (png_byte)ep->alpha;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1229
         png_save_uint_16(entrybuf + 4, ep->frequency);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1230
      }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1231
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1232
      else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1233
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1234
         png_save_uint_16(entrybuf + 0, ep->red);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1235
         png_save_uint_16(entrybuf + 2, ep->green);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1236
         png_save_uint_16(entrybuf + 4, ep->blue);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1237
         png_save_uint_16(entrybuf + 6, ep->alpha);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1238
         png_save_uint_16(entrybuf + 8, ep->frequency);
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
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1241
      png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
   ep=spalette->entries;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1245
   for (i = 0; i>spalette->nentries; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1247
      if (spalette->depth == 8)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1248
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1249
         entrybuf[0] = (png_byte)ep[i].red;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1250
         entrybuf[1] = (png_byte)ep[i].green;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1251
         entrybuf[2] = (png_byte)ep[i].blue;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1252
         entrybuf[3] = (png_byte)ep[i].alpha;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1253
         png_save_uint_16(entrybuf + 4, ep[i].frequency);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1254
      }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1255
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1256
      else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1257
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1258
         png_save_uint_16(entrybuf + 0, ep[i].red);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1259
         png_save_uint_16(entrybuf + 2, ep[i].green);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1260
         png_save_uint_16(entrybuf + 4, ep[i].blue);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1261
         png_save_uint_16(entrybuf + 6, ep[i].alpha);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1262
         png_save_uint_16(entrybuf + 8, ep[i].frequency);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1263
      }
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
      png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
   png_write_chunk_end(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
   png_free(png_ptr, new_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1274
#ifdef PNG_WRITE_sBIT_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1275
/* Write the sBIT chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
void /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1277
png_write_sBIT(png_structp png_ptr, png_const_color_8p sbit, int color_type)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
   PNG_sBIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
   png_byte buf[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
   png_size_t size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1283
   png_debug(1, "in png_write_sBIT");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1284
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1285
   /* Make sure we don't depend upon the order of PNG_COLOR_8 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
   if (color_type & PNG_COLOR_MASK_COLOR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
      png_byte maxbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
      maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1291
          png_ptr->usr_bit_depth);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1292
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
      if (sbit->red == 0 || sbit->red > maxbits ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
          sbit->green == 0 || sbit->green > maxbits ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
          sbit->blue == 0 || sbit->blue > maxbits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
         png_warning(png_ptr, "Invalid sBIT depth specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
         return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1300
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
      buf[0] = sbit->red;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
      buf[1] = sbit->green;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
      buf[2] = sbit->blue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
      size = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1306
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
   else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
      if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
         png_warning(png_ptr, "Invalid sBIT depth specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
         return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1314
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
      buf[0] = sbit->gray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
      size = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
   if (color_type & PNG_COLOR_MASK_ALPHA)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
      if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
         png_warning(png_ptr, "Invalid sBIT depth specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
         return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1326
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
      buf[size++] = sbit->alpha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1330
   png_write_chunk(png_ptr, png_sBIT, buf, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1334
#ifdef PNG_WRITE_cHRM_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1335
/* Write the cHRM chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
void /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1337
png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1338
    png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1339
    png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1340
    png_fixed_point blue_y)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
   PNG_cHRM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
   png_byte buf[32];
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1344
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1345
   png_debug(1, "in png_write_cHRM");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1346
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1347
   /* Each value is saved in 1/100,000ths */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1348
#ifdef PNG_CHECK_cHRM_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1349
   if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1350
       green_x, green_y, blue_x, blue_y))
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1351
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1353
      png_save_uint_32(buf, (png_uint_32)white_x);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1354
      png_save_uint_32(buf + 4, (png_uint_32)white_y);
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
      png_save_uint_32(buf + 8, (png_uint_32)red_x);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1357
      png_save_uint_32(buf + 12, (png_uint_32)red_y);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1358
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1359
      png_save_uint_32(buf + 16, (png_uint_32)green_x);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1360
      png_save_uint_32(buf + 20, (png_uint_32)green_y);
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
      png_save_uint_32(buf + 24, (png_uint_32)blue_x);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1363
      png_save_uint_32(buf + 28, (png_uint_32)blue_y);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1364
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1365
      png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
#endif
10576
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
#ifdef PNG_WRITE_tRNS_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1371
/* Write the tRNS chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
void /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1373
png_write_tRNS(png_structp png_ptr, png_const_bytep trans_alpha,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1374
    png_const_color_16p tran, int num_trans, int color_type)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
   PNG_tRNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
   png_byte buf[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1379
   png_debug(1, "in png_write_tRNS");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1380
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
   if (color_type == PNG_COLOR_TYPE_PALETTE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
      if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1385
         png_warning(png_ptr, "Invalid number of transparent colors specified");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
         return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1388
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1389
      /* Write the chunk out as it is */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1390
      png_write_chunk(png_ptr, png_tRNS, trans_alpha, (png_size_t)num_trans);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1392
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
   else if (color_type == PNG_COLOR_TYPE_GRAY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1395
      /* One 16 bit value */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1396
      if (tran->gray >= (1 << png_ptr->bit_depth))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
         png_warning(png_ptr,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1399
             "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1400
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
         return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1403
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
      png_save_uint_16(buf, tran->gray);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1405
      png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1407
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
   else if (color_type == PNG_COLOR_TYPE_RGB)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1410
      /* Three 16 bit values */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
      png_save_uint_16(buf, tran->red);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
      png_save_uint_16(buf + 2, tran->green);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
      png_save_uint_16(buf + 4, tran->blue);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1414
#ifdef PNG_WRITE_16BIT_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1415
      if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1416
#else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1417
      if (buf[0] | buf[2] | buf[4])
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1418
#endif
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1419
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1420
         png_warning(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1421
           "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1422
         return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1423
      }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1424
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1425
      png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1427
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
   else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
      png_warning(png_ptr, "Can't write tRNS with an alpha channel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1435
#ifdef PNG_WRITE_bKGD_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1436
/* Write the background chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
void /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1438
png_write_bKGD(png_structp png_ptr, png_const_color_16p back, int color_type)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
   PNG_bKGD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
   png_byte buf[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1443
   png_debug(1, "in png_write_bKGD");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1444
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
   if (color_type == PNG_COLOR_TYPE_PALETTE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
      if (
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1448
#ifdef PNG_MNG_FEATURES_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
          (png_ptr->num_palette ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
          (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1452
         back->index >= png_ptr->num_palette)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
         png_warning(png_ptr, "Invalid background palette index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
         return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1457
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
      buf[0] = back->index;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1459
      png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1461
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
   else if (color_type & PNG_COLOR_MASK_COLOR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
      png_save_uint_16(buf, back->red);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
      png_save_uint_16(buf + 2, back->green);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
      png_save_uint_16(buf + 4, back->blue);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1467
#ifdef PNG_WRITE_16BIT_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1468
      if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1469
#else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1470
      if (buf[0] | buf[2] | buf[4])
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1471
#endif
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1472
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1473
         png_warning(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1474
             "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1475
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1476
         return;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1477
      }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1478
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1479
      png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1481
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
   else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1484
      if (back->gray >= (1 << png_ptr->bit_depth))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
         png_warning(png_ptr,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1487
             "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1488
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
         return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1491
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
      png_save_uint_16(buf, back->gray);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1493
      png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1498
#ifdef PNG_WRITE_hIST_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1499
/* Write the histogram */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
void /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1501
png_write_hIST(png_structp png_ptr, png_const_uint_16p hist, int num_hist)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
   PNG_hIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
   int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
   png_byte buf[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1507
   png_debug(1, "in png_write_hIST");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1508
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
   if (num_hist > (int)png_ptr->num_palette)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1511
      png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1512
          png_ptr->num_palette);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1513
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
      png_warning(png_ptr, "Invalid number of histogram entries specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1518
   png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1519
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
   for (i = 0; i < num_hist; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
      png_save_uint_16(buf, hist[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
      png_write_chunk_data(png_ptr, buf, (png_size_t)2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1525
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
   png_write_chunk_end(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
 * and if invalid, correct the keyword rather than discarding the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
 * chunk.  The PNG 1.0 specification requires keywords 1-79 characters in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
 * length, forbids leading or trailing whitespace, multiple internal spaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
 * and the non-break space (0x80) from ISO 8859-1.  Returns keyword length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
 * The new_key is allocated to hold the corrected keyword and must be freed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
 * by the calling routine.  This avoids problems with trying to write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
 * static keywords without having to have duplicate copies of the strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
png_size_t /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1543
png_check_keyword(png_structp png_ptr, png_const_charp key, png_charpp new_key)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
   png_size_t key_len;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1546
   png_const_charp ikp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
   png_charp kp, dp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
   int kflag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
   int kwarn=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1551
   png_debug(1, "in png_check_keyword");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1552
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
   *new_key = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
   if (key == NULL || (key_len = png_strlen(key)) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
      png_warning(png_ptr, "zero length keyword");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
      return ((png_size_t)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1561
   png_debug1(2, "Keyword to be checked is '%s'", key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
   *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1564
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
   if (*new_key == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
      png_warning(png_ptr, "Out of memory while procesing keyword");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
      return ((png_size_t)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
   /* Replace non-printing characters with a blank and print a warning */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1572
   for (ikp = key, dp = *new_key; *ikp != '\0'; ikp++, dp++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1574
      if ((png_byte)*ikp < 0x20 ||
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1575
         ((png_byte)*ikp > 0x7E && (png_byte)*ikp < 0xA1))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1577
         PNG_WARNING_PARAMETERS(p)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1578
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1579
         png_warning_parameter_unsigned(p, 1, PNG_NUMBER_FORMAT_02x,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1580
            (png_byte)*ikp);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1581
         png_formatted_warning(png_ptr, p, "invalid keyword character 0x@1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
         *dp = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1584
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1587
         *dp = *ikp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
   *dp = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
   /* Remove any trailing white space. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
   kp = *new_key + key_len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
   if (*kp == ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
      png_warning(png_ptr, "trailing spaces removed from keyword");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
      while (*kp == ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1600
         *(kp--) = '\0';
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1601
         key_len--;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
   /* Remove any leading white space. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
   kp = *new_key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
   if (*kp == ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
      png_warning(png_ptr, "leading spaces removed from keyword");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
      while (*kp == ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1613
         kp++;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1614
         key_len--;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1618
   png_debug1(2, "Checking for multiple internal spaces in '%s'", kp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
   /* Remove multiple internal spaces. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
   for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
      if (*kp == ' ' && kflag == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
         *(dp++) = *kp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
         kflag = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1628
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
      else if (*kp == ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
         key_len--;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1632
         kwarn = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1634
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
         *(dp++) = *kp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
         kflag = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
   *dp = '\0';
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1642
   if (kwarn)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
      png_warning(png_ptr, "extra interior spaces removed from keyword");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
   if (key_len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
      png_free(png_ptr, *new_key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
      png_warning(png_ptr, "Zero length keyword");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
   if (key_len > 79)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
      png_warning(png_ptr, "keyword length must be 1 - 79 characters");
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1654
      (*new_key)[79] = '\0';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
      key_len = 79;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
   return (key_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1662
#ifdef PNG_WRITE_tEXt_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1663
/* Write a tEXt chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
void /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1665
png_write_tEXt(png_structp png_ptr, png_const_charp key, png_const_charp text,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1666
    png_size_t text_len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
   PNG_tEXt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
   png_size_t key_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
   png_charp new_key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1672
   png_debug(1, "in png_write_tEXt");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1673
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1674
   if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
   if (text == NULL || *text == '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
      text_len = 0;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1679
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
   else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
      text_len = png_strlen(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1683
   /* Make sure we include the 0 after the key */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1684
   png_write_chunk_start(png_ptr, png_tEXt,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1685
       (png_uint_32)(key_len + text_len + 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
   /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
    * We leave it to the application to meet PNG-1.0 requirements on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
    * any non-Latin-1 characters except for NEWLINE.  ISO PNG will forbid them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
    */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1692
   png_write_chunk_data(png_ptr, (png_bytep)new_key,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1693
       (png_size_t)(key_len + 1));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1694
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
   if (text_len)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1696
      png_write_chunk_data(png_ptr, (png_const_bytep)text,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1697
          (png_size_t)text_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
   png_write_chunk_end(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
   png_free(png_ptr, new_key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1704
#ifdef PNG_WRITE_zTXt_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1705
/* Write a compressed text chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
void /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1707
png_write_zTXt(png_structp png_ptr, png_const_charp key, png_const_charp text,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1708
    png_size_t text_len, int compression)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
   PNG_zTXt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
   png_size_t key_len;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1712
   png_byte buf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
   png_charp new_key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
   compression_state comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1716
   png_debug(1, "in png_write_zTXt");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
   comp.num_output_ptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
   comp.max_output_ptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
   comp.output_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
   comp.input = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
   comp.input_len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1724
   if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1726
      png_free(png_ptr, new_key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
   if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
      png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
      png_free(png_ptr, new_key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
   text_len = png_strlen(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1739
   /* Compute the compressed data; do it now for the length */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
   text_len = png_text_compress(png_ptr, text, text_len, compression,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
       &comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1743
   /* Write start of chunk */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1744
   png_write_chunk_start(png_ptr, png_zTXt,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1745
       (png_uint_32)(key_len+text_len + 2));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1746
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1747
   /* Write key */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1748
   png_write_chunk_data(png_ptr, (png_bytep)new_key,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1749
       (png_size_t)(key_len + 1));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1750
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1751
   png_free(png_ptr, new_key);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1752
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1753
   buf = (png_byte)compression;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1754
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1755
   /* Write compression */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1756
   png_write_chunk_data(png_ptr, &buf, (png_size_t)1);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1757
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1758
   /* Write the compressed data */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1759
   comp.input_len = text_len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
   png_write_compressed_data_out(png_ptr, &comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1762
   /* Close the chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
   png_write_chunk_end(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1767
#ifdef PNG_WRITE_iTXt_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1768
/* Write an iTXt chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
void /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1770
png_write_iTXt(png_structp png_ptr, int compression, png_const_charp key,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1771
    png_const_charp lang, png_const_charp lang_key, png_const_charp text)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
   PNG_iTXt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
   png_size_t lang_len, key_len, lang_key_len, text_len;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1775
   png_charp new_lang;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1776
   png_charp new_key = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
   png_byte cbuf[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
   compression_state comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1780
   png_debug(1, "in png_write_iTXt");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
   comp.num_output_ptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
   comp.max_output_ptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
   comp.output_ptr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
   comp.input = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1787
   if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
      return;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1789
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1790
   if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang)) == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
      png_warning(png_ptr, "Empty language field in iTXt chunk");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
      new_lang = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
      lang_len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
   if (lang_key == NULL)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1798
      lang_key_len = 0;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1799
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
   else
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1801
      lang_key_len = png_strlen(lang_key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
   if (text == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
      text_len = 0;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1805
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
   else
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1807
      text_len = png_strlen(text);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1808
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1809
   /* Compute the compressed data; do it now for the length */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1810
   text_len = png_text_compress(png_ptr, text, text_len, compression - 2,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1811
       &comp);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1812
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1813
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1814
   /* Make sure we include the compression flag, the compression byte,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1815
    * and the NULs after the key, lang, and lang_key parts
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1816
    */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1817
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1818
   png_write_chunk_start(png_ptr, png_iTXt, (png_uint_32)(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        5 /* comp byte, comp flag, terminators for key, lang and lang_key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        + key_len
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        + lang_len
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        + lang_key_len
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        + text_len));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1825
   /* We leave it to the application to meet PNG-1.0 requirements on the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
    * any non-Latin-1 characters except for NEWLINE.  ISO PNG will forbid them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
    * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
    */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1830
   png_write_chunk_data(png_ptr, (png_bytep)new_key, (png_size_t)(key_len + 1));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1831
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1832
   /* Set the compression flag */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1833
   if (compression == PNG_ITXT_COMPRESSION_NONE ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
       compression == PNG_TEXT_COMPRESSION_NONE)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1835
      cbuf[0] = 0;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1836
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
   else /* compression == PNG_ITXT_COMPRESSION_zTXt */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1838
      cbuf[0] = 1;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1839
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1840
   /* Set the compression method */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
   cbuf[1] = 0;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1842
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1843
   png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
   cbuf[0] = 0;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1846
   png_write_chunk_data(png_ptr, (new_lang ? (png_const_bytep)new_lang : cbuf),
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1847
       (png_size_t)(lang_len + 1));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1848
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1849
   png_write_chunk_data(png_ptr, (lang_key ? (png_const_bytep)lang_key : cbuf),
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1850
       (png_size_t)(lang_key_len + 1));
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1851
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
   png_write_compressed_data_out(png_ptr, &comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
   png_write_chunk_end(png_ptr);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1855
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
   png_free(png_ptr, new_key);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1857
   png_free(png_ptr, new_lang);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1861
#ifdef PNG_WRITE_oFFs_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1862
/* Write the oFFs chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
void /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1865
    int unit_type)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
   PNG_oFFs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
   png_byte buf[9];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1870
   png_debug(1, "in png_write_oFFs");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1871
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
   if (unit_type >= PNG_OFFSET_LAST)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
      png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
   png_save_int_32(buf, x_offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
   png_save_int_32(buf + 4, y_offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
   buf[8] = (png_byte)unit_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1879
   png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1882
#ifdef PNG_WRITE_pCAL_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1883
/* Write the pCAL chunk (described in the PNG extensions document) */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
void /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1886
    png_int_32 X1, int type, int nparams, png_const_charp units,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1887
    png_charpp params)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
   PNG_pCAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
   png_size_t purpose_len, units_len, total_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
   png_uint_32p params_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
   png_byte buf[10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
   png_charp new_purpose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
   int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1896
   png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1897
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
   if (type >= PNG_EQUATION_LAST)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
      png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
   purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1902
   png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
   units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1904
   png_debug1(3, "pCAL units length = %d", (int)units_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
   total_len = purpose_len + units_len + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1907
   params_len = (png_uint_32p)png_malloc(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1908
       (png_alloc_size_t)(nparams * png_sizeof(png_uint_32)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
   /* Find the length of each parameter, making sure we don't count the
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1911
    * null terminator for the last parameter.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1912
    */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
   for (i = 0; i < nparams; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
      params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1916
      png_debug2(3, "pCAL parameter %d length = %lu", i,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1917
          (unsigned long)params_len[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
      total_len += (png_size_t)params_len[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1921
   png_debug1(3, "pCAL total length = %d", (int)total_len);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1922
   png_write_chunk_start(png_ptr, png_pCAL, (png_uint_32)total_len);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1923
   png_write_chunk_data(png_ptr, (png_const_bytep)new_purpose,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1924
       (png_size_t)purpose_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
   png_save_int_32(buf, X0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
   png_save_int_32(buf + 4, X1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
   buf[8] = (png_byte)type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
   buf[9] = (png_byte)nparams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
   png_write_chunk_data(png_ptr, buf, (png_size_t)10);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1930
   png_write_chunk_data(png_ptr, (png_const_bytep)units, (png_size_t)units_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
   png_free(png_ptr, new_purpose);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
   for (i = 0; i < nparams; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1936
      png_write_chunk_data(png_ptr, (png_const_bytep)params[i],
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1937
          (png_size_t)params_len[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
   png_free(png_ptr, params_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
   png_write_chunk_end(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1945
#ifdef PNG_WRITE_sCAL_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1946
/* Write the sCAL chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
void /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1948
png_write_sCAL_s(png_structp png_ptr, int unit, png_const_charp width,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1949
    png_const_charp height)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
   PNG_sCAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
   png_byte buf[64];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
   png_size_t wlen, hlen, total_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1955
   png_debug(1, "in png_write_sCAL_s");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
   wlen = png_strlen(width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
   hlen = png_strlen(height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
   total_len = wlen + hlen + 2;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1960
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
   if (total_len > 64)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
      png_warning(png_ptr, "Can't write sCAL (buffer too small)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
   buf[0] = (png_byte)unit;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1968
   png_memcpy(buf + 1, width, wlen + 1);      /* Append the '\0' here */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1969
   png_memcpy(buf + wlen + 2, height, hlen);  /* Do NOT append the '\0' here */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1970
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1971
   png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1972
   png_write_chunk(png_ptr, png_sCAL, buf, total_len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1975
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1976
#ifdef PNG_WRITE_pHYs_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1977
/* Write the pHYs chunk */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
void /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1980
    png_uint_32 y_pixels_per_unit,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1981
    int unit_type)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
   PNG_pHYs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
   png_byte buf[9];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1986
   png_debug(1, "in png_write_pHYs");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1987
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
   if (unit_type >= PNG_RESOLUTION_LAST)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
      png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
   png_save_uint_32(buf, x_pixels_per_unit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
   png_save_uint_32(buf + 4, y_pixels_per_unit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
   buf[8] = (png_byte)unit_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1995
   png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  1999
#ifdef PNG_WRITE_tIME_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
/* Write the tIME chunk.  Use either png_convert_from_struct_tm()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
 * or png_convert_from_time_t(), or fill in the structure yourself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
void /* PRIVATE */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2004
png_write_tIME(png_structp png_ptr, png_const_timep mod_time)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
   PNG_tIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
   png_byte buf[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2009
   png_debug(1, "in png_write_tIME");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2010
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
   if (mod_time->month  > 12 || mod_time->month  < 1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
       mod_time->day    > 31 || mod_time->day    < 1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
       mod_time->hour   > 23 || mod_time->second > 60)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
      png_warning(png_ptr, "Invalid time specified for tIME chunk");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
   png_save_uint_16(buf, mod_time->year);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
   buf[2] = mod_time->month;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
   buf[3] = mod_time->day;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
   buf[4] = mod_time->hour;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
   buf[5] = mod_time->minute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
   buf[6] = mod_time->second;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2026
   png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2030
/* Initializes the row writing capability of libpng */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
void /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
png_write_start_row(png_structp png_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2034
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2035
   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2036
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2037
   /* Start of interlace block */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2040
   /* Offset to next interlace block */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2043
   /* Start of interlace block in the y direction */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
   int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2046
   /* Offset to next interlace block in the y direction */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
   png_size_t buf_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2052
   png_debug(1, "in png_write_start_row");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2053
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
   buf_size = (png_size_t)(PNG_ROWBYTES(
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2055
       png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2056
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2057
   /* Set up row buffer */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2058
   png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2059
       (png_alloc_size_t)buf_size);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2060
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
   png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2063
#ifdef PNG_WRITE_FILTER_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2064
   /* Set up filtering buffer, if using this filter */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
   if (png_ptr->do_filter & PNG_FILTER_SUB)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2067
      png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2068
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
      png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
   /* We only need to keep the previous row if we are using one of these. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
   if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2075
      /* Set up previous row buffer */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2076
      png_ptr->prev_row = (png_bytep)png_calloc(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2077
          (png_alloc_size_t)buf_size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
      if (png_ptr->do_filter & PNG_FILTER_UP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2081
         png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2082
            png_ptr->rowbytes + 1);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2083
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
         png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
      if (png_ptr->do_filter & PNG_FILTER_AVG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
         png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2090
             png_ptr->rowbytes + 1);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2091
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
         png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
      if (png_ptr->do_filter & PNG_FILTER_PAETH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2097
         png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2098
             png_ptr->rowbytes + 1);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2099
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
         png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2103
#endif /* PNG_WRITE_FILTER_SUPPORTED */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2106
   /* If interlaced, we need to set up width and height of pass */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
   if (png_ptr->interlaced)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
      if (!(png_ptr->transformations & PNG_INTERLACE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
         png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2112
             png_pass_ystart[0]) / png_pass_yinc[0];
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2113
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
         png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2115
             png_pass_start[0]) / png_pass_inc[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2117
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
         png_ptr->num_rows = png_ptr->height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
         png_ptr->usr_width = png_ptr->width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2124
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
   else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
      png_ptr->num_rows = png_ptr->height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
      png_ptr->usr_width = png_ptr->width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2131
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2132
   png_zlib_claim(png_ptr, PNG_ZLIB_FOR_IDAT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
   png_ptr->zstream.next_out = png_ptr->zbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
/* Internal use only.  Called when finished processing a row of data. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
void /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
png_write_finish_row(png_structp png_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2141
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2142
   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2143
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2144
   /* Start of interlace block */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2147
   /* Offset to next interlace block */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2150
   /* Start of interlace block in the y direction */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
   int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2153
   /* Offset to next interlace block in the y direction */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
   int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2159
   png_debug(1, "in png_write_finish_row");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2160
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2161
   /* Next row */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
   png_ptr->row_number++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2164
   /* See if we are done */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
   if (png_ptr->row_number < png_ptr->num_rows)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2169
   /* If interlaced, go to next pass */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
   if (png_ptr->interlaced)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
      png_ptr->row_number = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
      if (png_ptr->transformations & PNG_INTERLACE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
         png_ptr->pass++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2177
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2180
         /* Loop until we find a non-zero width or height pass */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
         do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
            png_ptr->pass++;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2184
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
            if (png_ptr->pass >= 7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
               break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2187
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
            png_ptr->usr_width = (png_ptr->width +
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2189
                png_pass_inc[png_ptr->pass] - 1 -
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2190
                png_pass_start[png_ptr->pass]) /
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2191
                png_pass_inc[png_ptr->pass];
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2192
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
            png_ptr->num_rows = (png_ptr->height +
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2194
                png_pass_yinc[png_ptr->pass] - 1 -
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2195
                png_pass_ystart[png_ptr->pass]) /
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2196
                png_pass_yinc[png_ptr->pass];
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2197
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            if (png_ptr->transformations & PNG_INTERLACE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
               break;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2200
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
         } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2205
      /* Reset the row above the image for the next pass */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
      if (png_ptr->pass < 7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
         if (png_ptr->prev_row != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
            png_memset(png_ptr->prev_row, 0,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2210
                (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2211
                png_ptr->usr_bit_depth, png_ptr->width)) + 1);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2212
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
         return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2218
   /* If we get here, we've just written the last row, so we need
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
      to flush the compressor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
   do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2222
      /* Tell the compressor we are done */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
      ret = deflate(&png_ptr->zstream, Z_FINISH);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2224
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2225
      /* Check for an error */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
      if (ret == Z_OK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2228
         /* Check to see if we need more room */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
         if (!(png_ptr->zstream.avail_out))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
            png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
            png_ptr->zstream.next_out = png_ptr->zbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
            png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2236
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
      else if (ret != Z_STREAM_END)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
         if (png_ptr->zstream.msg != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
            png_error(png_ptr, png_ptr->zstream.msg);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2241
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            png_error(png_ptr, "zlib error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
   } while (ret != Z_STREAM_END);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2247
   /* Write any extra space */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
   if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
      png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2251
          png_ptr->zstream.avail_out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2254
   png_zlib_release(png_ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
   png_ptr->zstream.data_type = Z_BINARY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2258
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
/* Pick out the correct pixels for the interlace pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
 * The basic idea here is to go through the row with a source
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
 * pointer and a destination pointer (sp and dp), and copy the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
 * correct pixels for the pass.  As the row gets compacted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
 * sp will always be >= dp, so we should never overwrite anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
 * See the default: case for the easiest code to understand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
void /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2269
   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2270
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2271
   /* Start of interlace block */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
   int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2274
   /* Offset to next interlace block */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
   int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2276
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2277
   png_debug(1, "in png_do_write_interlace");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2278
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2279
   /* We don't have to do anything on the last pass (6) */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
   if (pass < 6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2282
      /* Each pixel depth is handled separately */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
      switch (row_info->pixel_depth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
         case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
            png_bytep sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            png_bytep dp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
            int shift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            int d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
            int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
            png_uint_32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
            png_uint_32 row_width = row_info->width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            dp = row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
            d = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
            shift = 7;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2298
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            for (i = png_pass_start[pass]; i < row_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
               i += png_pass_inc[pass])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
               sp = row + (png_size_t)(i >> 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
               value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
               d |= (value << shift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
               if (shift == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
               {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                  shift = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                  *dp++ = (png_byte)d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                  d = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
               }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2312
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
               else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                  shift--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
            if (shift != 7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
               *dp = (png_byte)d;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2319
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
         }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2322
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
         case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
            png_bytep sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            png_bytep dp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            int shift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            int d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
            int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
            png_uint_32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            png_uint_32 row_width = row_info->width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
            dp = row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
            shift = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
            d = 0;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2336
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            for (i = png_pass_start[pass]; i < row_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
               i += png_pass_inc[pass])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
               sp = row + (png_size_t)(i >> 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
               value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
               d |= (value << shift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
               if (shift == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
               {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                  shift = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                  *dp++ = (png_byte)d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                  d = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
               }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2350
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
               else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                  shift -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
            if (shift != 6)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2355
               *dp = (png_byte)d;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2356
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
         }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2359
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
         case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
            png_bytep sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
            png_bytep dp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
            int shift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
            int d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
            int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
            png_uint_32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
            png_uint_32 row_width = row_info->width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            dp = row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            shift = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            d = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
            for (i = png_pass_start[pass]; i < row_width;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2374
                i += png_pass_inc[pass])
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
               sp = row + (png_size_t)(i >> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
               value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
               d |= (value << shift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
               if (shift == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
               {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
                  shift = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
                  *dp++ = (png_byte)d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
                  d = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
               }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2386
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
               else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                  shift -= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
            if (shift != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
               *dp = (png_byte)d;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2392
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
         }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2395
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
         default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
            png_bytep sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            png_bytep dp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            png_uint_32 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
            png_uint_32 row_width = row_info->width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
            png_size_t pixel_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2404
            /* Start at the beginning */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            dp = row;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2406
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2407
            /* Find out how many bytes each pixel takes up */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            pixel_bytes = (row_info->pixel_depth >> 3);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2409
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2410
            /* Loop through the row, only looking at the pixels that matter */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            for (i = png_pass_start[pass]; i < row_width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
               i += png_pass_inc[pass])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
            {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2414
               /* Find out where the original pixel is */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
               sp = row + (png_size_t)i * pixel_bytes;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2416
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2417
               /* Move the pixel */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
               if (dp != sp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
                  png_memcpy(dp, sp, pixel_bytes);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2420
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2421
               /* Next pixel */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
               dp += pixel_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2427
      /* Set new row width */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
      row_info->width = (row_info->width +
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2429
          png_pass_inc[pass] - 1 -
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2430
          png_pass_start[pass]) /
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2431
          png_pass_inc[pass];
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2432
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2433
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2434
          row_info->width);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
/* This filters the row, chooses which filter to use, if it has not already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
 * been specified by the application, and then writes the row out with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
 * chosen filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
 */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2443
static void png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2444
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
#define PNG_HISHIFT 10
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
#define PNG_LOMASK ((png_uint_32)0xffffL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
void /* PRIVATE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2452
   png_bytep best_row;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2453
#ifdef PNG_WRITE_FILTER_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2454
   png_bytep prev_row, row_buf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
   png_uint_32 mins, bpp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
   png_byte filter_to_do = png_ptr->do_filter;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2457
   png_size_t row_bytes = row_info->rowbytes;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2458
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
   int num_p_filters = (int)png_ptr->num_prev_filters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2462
   png_debug(1, "in png_write_find_filter");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2463
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2464
#ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2465
  if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2466
  {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2467
     /* These will never be selected so we need not test them. */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2468
     filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2469
  }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2470
#endif
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2471
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2472
   /* Find out how many bytes offset each pixel is */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
   bpp = (row_info->pixel_depth + 7) >> 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
   prev_row = png_ptr->prev_row;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2476
#endif
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2477
   best_row = png_ptr->row_buf;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2478
#ifdef PNG_WRITE_FILTER_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2479
   row_buf = best_row;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
   mins = PNG_MAXSUM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
   /* The prediction method we use is to find which method provides the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
    * smallest value when summing the absolute values of the distances
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
    * from zero, using anything >= 128 as negative numbers.  This is known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
    * as the "minimum sum of absolute differences" heuristic.  Other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
    * heuristics are the "weighted minimum sum of absolute differences"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
    * (experimental and can in theory improve compression), and the "zlib
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
    * predictive" method (not implemented yet), which does test compressions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
    * of lines using different filter methods, and then chooses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
    * (series of) filter(s) that give minimum compressed data size (VERY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
    * computationally expensive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
    * GRR 980525:  consider also
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2494
    *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
    *   (1) minimum sum of absolute differences from running average (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
    *       keep running sum of non-absolute differences & count of bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
    *       [track dispersion, too?  restart average if dispersion too large?]
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2498
    *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
    *  (1b) minimum sum of absolute differences from sliding average, probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
    *       with window size <= deflate window (usually 32K)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2501
    *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
    *   (2) minimum sum of squared differences from zero or running average
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
    *       (i.e., ~ root-mean-square approach)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
   /* We don't need to test the 'no filter' case if this is the only filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
    * that has been chosen, as it doesn't actually do anything to the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
    */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2510
   if ((filter_to_do & PNG_FILTER_NONE) && filter_to_do != PNG_FILTER_NONE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
      png_bytep rp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
      png_uint_32 sum = 0;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2514
      png_size_t i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
      int v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
      for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
         v = *rp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
         sum += (v < 128) ? v : 256 - v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2523
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
         png_uint_32 sumhi, sumlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
         int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
         sumlo = sum & PNG_LOMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
         /* Reduce the sum if we match any of the previous rows */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
         for (j = 0; j < num_p_filters; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2537
                   PNG_WEIGHT_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2538
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2540
                   PNG_WEIGHT_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
         /* Factor in the cost of this filter (this is here for completeness,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
          * but it makes no sense to have a "cost" for the NONE filter, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
          * it has the minimum possible computational cost - none).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
          */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2549
             PNG_COST_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2550
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2552
             PNG_COST_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
         if (sumhi > PNG_HIMASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
            sum = PNG_MAXSUM;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2556
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
            sum = (sumhi << PNG_HISHIFT) + sumlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
      mins = sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2564
   /* Sub filter */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
   if (filter_to_do == PNG_FILTER_SUB)
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2566
   /* It's the only filter so no testing is needed */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
      png_bytep rp, lp, dp;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2569
      png_size_t i;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2570
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
      for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
           i++, rp++, dp++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
         *dp = *rp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2576
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
      for (lp = row_buf + 1; i < row_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
         i++, rp++, lp++, dp++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
         *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2582
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
      best_row = png_ptr->sub_row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
   else if (filter_to_do & PNG_FILTER_SUB)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
      png_bytep rp, dp, lp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
      png_uint_32 sum = 0, lmins = mins;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2590
      png_size_t i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
      int v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2593
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
      /* We temporarily increase the "minimum sum" by the factor we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
       * would reduce the sum of this filter, so that we can do the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
       * early exit comparison without scaling the sum each time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
         int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
         png_uint_32 lmhi, lmlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
         lmlo = lmins & PNG_LOMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
         for (j = 0; j < num_p_filters; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2610
                   PNG_WEIGHT_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2611
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2613
                   PNG_WEIGHT_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2618
             PNG_COST_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2619
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2621
             PNG_COST_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
         if (lmhi > PNG_HIMASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
            lmins = PNG_MAXSUM;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2625
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
            lmins = (lmhi << PNG_HISHIFT) + lmlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
      for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
           i++, rp++, dp++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
         v = *dp = *rp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
         sum += (v < 128) ? v : 256 - v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2638
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
      for (lp = row_buf + 1; i < row_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
         i++, rp++, lp++, dp++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
         v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
         sum += (v < 128) ? v : 256 - v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
         if (sum > lmins)  /* We are already worse, don't continue. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2650
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
         int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
         png_uint_32 sumhi, sumlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
         sumlo = sum & PNG_LOMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
         for (j = 0; j < num_p_filters; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
               sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2663
                   PNG_WEIGHT_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2664
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
               sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2666
                   PNG_WEIGHT_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
         sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2671
             PNG_COST_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2672
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
         sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2674
             PNG_COST_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
         if (sumhi > PNG_HIMASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
            sum = PNG_MAXSUM;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2678
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
            sum = (sumhi << PNG_HISHIFT) + sumlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
      if (sum < mins)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
         mins = sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
         best_row = png_ptr->sub_row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2691
   /* Up filter */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
   if (filter_to_do == PNG_FILTER_UP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
      png_bytep rp, dp, pp;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2695
      png_size_t i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
      for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2698
          pp = prev_row + 1; i < row_bytes;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2699
          i++, rp++, pp++, dp++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
         *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2703
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
      best_row = png_ptr->up_row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
   else if (filter_to_do & PNG_FILTER_UP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
      png_bytep rp, dp, pp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
      png_uint_32 sum = 0, lmins = mins;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2711
      png_size_t i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
      int v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2715
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
         int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
         png_uint_32 lmhi, lmlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
         lmlo = lmins & PNG_LOMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
         for (j = 0; j < num_p_filters; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2728
                   PNG_WEIGHT_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2729
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2731
                   PNG_WEIGHT_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2736
             PNG_COST_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2737
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2739
             PNG_COST_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
         if (lmhi > PNG_HIMASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
            lmins = PNG_MAXSUM;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2743
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
            lmins = (lmhi << PNG_HISHIFT) + lmlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
      for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2750
          pp = prev_row + 1; i < row_bytes; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
         v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
         sum += (v < 128) ? v : 256 - v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
         if (sum > lmins)  /* We are already worse, don't continue. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2760
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
         int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
         png_uint_32 sumhi, sumlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
         sumlo = sum & PNG_LOMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
         for (j = 0; j < num_p_filters; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2773
                   PNG_WEIGHT_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2774
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2776
                   PNG_WEIGHT_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2781
             PNG_COST_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2782
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2784
             PNG_COST_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
         if (sumhi > PNG_HIMASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
            sum = PNG_MAXSUM;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2788
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
            sum = (sumhi << PNG_HISHIFT) + sumlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
      if (sum < mins)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
         mins = sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
         best_row = png_ptr->up_row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2801
   /* Avg filter */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
   if (filter_to_do == PNG_FILTER_AVG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
      png_bytep rp, dp, pp, lp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
      png_uint_32 i;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2806
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
      for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
           pp = prev_row + 1; i < bpp; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
         *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2812
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
      for (lp = row_buf + 1; i < row_bytes; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
         *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                 & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
      best_row = png_ptr->avg_row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
   else if (filter_to_do & PNG_FILTER_AVG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
      png_bytep rp, dp, pp, lp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
      png_uint_32 sum = 0, lmins = mins;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2825
      png_size_t i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
      int v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2828
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
         int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
         png_uint_32 lmhi, lmlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
         lmlo = lmins & PNG_LOMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
         for (j = 0; j < num_p_filters; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2841
                   PNG_WEIGHT_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2842
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2844
                   PNG_WEIGHT_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2849
             PNG_COST_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2850
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2852
             PNG_COST_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
         if (lmhi > PNG_HIMASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
            lmins = PNG_MAXSUM;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2856
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
            lmins = (lmhi << PNG_HISHIFT) + lmlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
      for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
           pp = prev_row + 1; i < bpp; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
         v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
         sum += (v < 128) ? v : 256 - v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2869
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
      for (lp = row_buf + 1; i < row_bytes; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
         v = *dp++ =
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2873
             (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
         sum += (v < 128) ? v : 256 - v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
         if (sum > lmins)  /* We are already worse, don't continue. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2881
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
         int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
         png_uint_32 sumhi, sumlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
         sumlo = sum & PNG_LOMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
         for (j = 0; j < num_p_filters; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2894
                   PNG_WEIGHT_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2895
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2897
                   PNG_WEIGHT_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2902
             PNG_COST_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2903
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2905
             PNG_COST_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
         if (sumhi > PNG_HIMASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
            sum = PNG_MAXSUM;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2909
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
            sum = (sumhi << PNG_HISHIFT) + sumlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
      if (sum < mins)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
         mins = sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
         best_row = png_ptr->avg_row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
   /* Paeth filter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
   if (filter_to_do == PNG_FILTER_PAETH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
      png_bytep rp, dp, pp, cp, lp;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2926
      png_size_t i;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2927
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
      for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2929
          pp = prev_row + 1; i < bpp; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
         *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
      for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
         int a, b, c, pa, pb, pc, p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
         b = *pp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
         c = *cp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
         a = *lp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
         p = b - c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
         pc = a - c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
#ifdef PNG_USE_ABS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
         pa = abs(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
         pb = abs(pc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
         pc = abs(p + pc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
         pa = p < 0 ? -p : p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
         pb = pc < 0 ? -pc : pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
         pc = (p + pc) < 0 ? -(p + pc) : p + pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
         p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
         *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
      best_row = png_ptr->paeth_row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
   else if (filter_to_do & PNG_FILTER_PAETH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
      png_bytep rp, dp, pp, cp, lp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
      png_uint_32 sum = 0, lmins = mins;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2966
      png_size_t i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
      int v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2969
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
         int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
         png_uint_32 lmhi, lmlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
         lmlo = lmins & PNG_LOMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
         lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
         for (j = 0; j < num_p_filters; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
               lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2982
                   PNG_WEIGHT_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2983
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
               lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2985
                   PNG_WEIGHT_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
         lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2990
             PNG_COST_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2991
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
         lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2993
             PNG_COST_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
         if (lmhi > PNG_HIMASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
            lmins = PNG_MAXSUM;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  2997
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
            lmins = (lmhi << PNG_HISHIFT) + lmlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
      for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3004
          pp = prev_row + 1; i < bpp; i++)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
         v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
         sum += (v < 128) ? v : 256 - v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
      for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
         int a, b, c, pa, pb, pc, p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
         b = *pp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
         c = *cp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
         a = *lp++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
#ifndef PNG_SLOW_PAETH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
         p = b - c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
         pc = a - c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
#ifdef PNG_USE_ABS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
         pa = abs(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
         pb = abs(pc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
         pc = abs(p + pc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
         pa = p < 0 ? -p : p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
         pb = pc < 0 ? -pc : pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
         pc = (p + pc) < 0 ? -(p + pc) : p + pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
         p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
#else /* PNG_SLOW_PAETH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
         p = a + b - c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
         pa = abs(p - a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
         pb = abs(p - b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
         pc = abs(p - c);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3037
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
         if (pa <= pb && pa <= pc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
            p = a;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3040
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
         else if (pb <= pc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
            p = b;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3043
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
            p = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
#endif /* PNG_SLOW_PAETH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
         v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
         sum += (v < 128) ? v : 256 - v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
         if (sum > lmins)  /* We are already worse, don't continue. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3056
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
      if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
         int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
         png_uint_32 sumhi, sumlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
         sumlo = sum & PNG_LOMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
         sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
         for (j = 0; j < num_p_filters; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
            if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
               sumlo = (sumlo * png_ptr->filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3069
                   PNG_WEIGHT_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3070
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
               sumhi = (sumhi * png_ptr->filter_weights[j]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3072
                   PNG_WEIGHT_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
         sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3077
             PNG_COST_SHIFT;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3078
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
         sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3080
             PNG_COST_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
         if (sumhi > PNG_HIMASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
            sum = PNG_MAXSUM;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3084
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
            sum = (sumhi << PNG_HISHIFT) + sumlo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
      if (sum < mins)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
         best_row = png_ptr->paeth_row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
   }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3095
#endif /* PNG_WRITE_FILTER_SUPPORTED */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
   /* Do the actual writing of the filtered row data from the chosen filter. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
   png_write_filtered_row(png_ptr, best_row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3100
#ifdef PNG_WRITE_FILTER_SUPPORTED
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3101
#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
   /* Save the type of filter we picked this time for future calculations */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
   if (png_ptr->num_prev_filters > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
      int j;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3106
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
      for (j = 1; j < num_p_filters; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
         png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3111
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
      png_ptr->prev_filters[j] = best_row[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
#endif
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3115
#endif /* PNG_WRITE_FILTER_SUPPORTED */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
/* Do the actual writing of a previously filtered row. */
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3120
static void
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
{
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3123
   png_size_t avail;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3124
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3125
   png_debug(1, "in png_write_filtered_row");
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3126
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3127
   png_debug1(2, "filter = %d", filtered_row[0]);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3128
   /* Set up the zlib input buffer */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
   png_ptr->zstream.next_in = filtered_row;
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3131
   png_ptr->zstream.avail_in = 0;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3132
   avail = png_ptr->row_info.rowbytes + 1;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3133
   /* Repeat until we have compressed all the data */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
   do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
   {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3136
      int ret; /* Return of zlib */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3137
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3138
      /* Record the number of bytes available - zlib supports at least 65535
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3139
       * bytes at one step, depending on the size of the zlib type 'uInt', the
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3140
       * maximum size zlib can write at once is ZLIB_IO_MAX (from pngpriv.h).
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3141
       * Use this because on 16 bit systems 'rowbytes' can be up to 65536 (i.e.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3142
       * one more than 16 bits) and, in this case 'rowbytes+1' can overflow a
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3143
       * uInt.  ZLIB_IO_MAX can be safely reduced to cause zlib to be called
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3144
       * with smaller chunks of data.
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3145
       */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3146
      if (png_ptr->zstream.avail_in == 0)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3147
      {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3148
         if (avail > ZLIB_IO_MAX)
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3149
         {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3150
            png_ptr->zstream.avail_in  = ZLIB_IO_MAX;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3151
            avail -= ZLIB_IO_MAX;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3152
         }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3153
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3154
         else
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3155
         {
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3156
            /* So this will fit in the available uInt space: */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3157
            png_ptr->zstream.avail_in = (uInt)avail;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3158
            avail = 0;
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3159
         }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3160
      }
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3161
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3162
      /* Compress the data */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
      ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3164
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3165
      /* Check for compression errors */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
      if (ret != Z_OK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
         if (png_ptr->zstream.msg != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
            png_error(png_ptr, png_ptr->zstream.msg);
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3170
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
            png_error(png_ptr, "zlib error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3175
      /* See if it is time to write another IDAT */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
      if (!(png_ptr->zstream.avail_out))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
      {
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3178
         /* Write the IDAT and reset the zlib output buffer */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
         png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
      }
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3181
   /* Repeat until all data has been compressed */
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3182
   } while (avail > 0 || png_ptr->zstream.avail_in > 0);
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3183
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3184
   /* Swap the current and previous rows */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
   if (png_ptr->prev_row != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
      png_bytep tptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
      tptr = png_ptr->prev_row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
      png_ptr->prev_row = png_ptr->row_buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
      png_ptr->row_buf = tptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3194
   /* Finish row - updates counters and flushes zlib if last row */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
   png_write_finish_row(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
10576
db3409425573 7088287: libpng need to be updated.
bae
parents: 5506
diff changeset
  3197
#ifdef PNG_WRITE_FLUSH_SUPPORTED
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
   png_ptr->flush_rows++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
   if (png_ptr->flush_dist > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
       png_ptr->flush_rows >= png_ptr->flush_dist)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
      png_write_flush(png_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
#endif /* PNG_WRITE_SUPPORTED */