0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A * This file is available under and governed by the GNU General Public 0N/A * License version 2 only, as published by the Free Software Foundation. 0N/A * However, the following notice accompanied the original version of this 0N/A * file and, per its terms, should not be removed: 4418N/A * Last changed in libpng 1.5.4 [July 7, 2011] 4418N/A * Copyright (c) 1998-2011 Glenn Randers-Pehrson 0N/A * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 0N/A * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 4418N/A * This code is released under the libpng license. 4418N/A * For conditions of distribution and use, see the disclaimer 0N/A/* Place a 32-bit number into a buffer in PNG byte order. We work 0N/A * with unsigned numbers for convenience, although one supported 0N/A * ancillary chunk uses signed (two's complement) numbers. 0N/A/* The png_save_int_32 function assumes integers are stored in two's 0N/A * complement format. If this isn't the case, then this routine needs to 4418N/A * be modified to write data in two's complement format. Note that, 4418N/A * the following works correctly even if png_int_32 has more than 32 bits 4418N/A * (compare the more complex code required on read for sign extention.) 0N/A/* Place a 16-bit number into a buffer in PNG byte order. 0N/A * The parameter is declared unsigned int, not png_uint_16, 0N/A * just to avoid potential problems on pre-ANSI C compilers. 4418N/A/* Simple function to write the signature. If we have already written 4418N/A * the magic bytes of the signature, or more likely, the PNG stream is 4418N/A * being embedded into another stream and doesn't need its own signature, 4418N/A * we should call png_set_sig_bytes() to tell libpng how many of the 4418N/A * bytes have already been written. 4418N/A /* Inform the I/O callback that the signature is being written */ 4418N/A /* Write the rest of the 8 byte signature */ 0N/A/* Write a PNG chunk all at once. The type is an array of ASCII characters 0N/A * representing the chunk name. The array must be at least 4 bytes in 0N/A * length, and does not need to be null terminated. To be safe, pass the 0N/A * pre-defined chunk names here, and if you need a new one, define it 0N/A * where the others are defined. The length is the length of the data. 0N/A * All the data must be present. If that is not possible, use the 0N/A * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end() 0N/A * functions instead. 0N/A/* Write the start of a PNG chunk. The type is the chunk type. 0N/A * The total_length is the sum of the lengths of all the data you will be 0N/A * passing in png_write_chunk_data(). 4418N/A /* Inform the I/O callback that the chunk header is being written. 4418N/A * PNG_IO_CHUNK_HDR requires a single I/O call. 4418N/A /* Write the length and the chunk name */ 4418N/A /* Put the chunk name into png_ptr->chunk_name */ 4418N/A /* Reset the crc and run it over the chunk name */ 4418N/A /* Inform the I/O callback that chunk data will (possibly) be written. 4418N/A * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls. 0N/A/* Write the data of a PNG chunk started with png_write_chunk_start(). 0N/A * Note that multiple calls to this function are allowed, and that the 0N/A * sum of the lengths from these calls *must* add up to the total_length 0N/A * given to png_write_chunk_start(). 4418N/A /* Write the data, and run the CRC over it */ 4418N/A /* Update the CRC after writing the data, 4418N/A * in case that the user I/O routine alters it. 0N/A/* Finish a chunk started with png_write_chunk_start(). */ 4418N/A /* Inform the I/O callback that the chunk CRC is being written. 4418N/A * PNG_IO_CHUNK_CRC requires a single I/O function call. 4418N/A /* Write the crc in a single operation */ 4418N/A/* Initialize the compressor for the appropriate type of compression. */ 4418N/A /* If already initialized for 'state' do not re-init. */ 4418N/A /* If actually initialized for another state do a deflateEnd. */ 4418N/A /* zlib itself detects an incomplete state on deflateEnd */ 4418N/A else /* an error in deflateEnd or deflateInit2 */ 4418N/A "zlib failed to initialize compressor (");
4418N/A /* Here on success, claim the zstream: */ 4418N/A/* The opposite: release the stream. It is also reset, this API will warn on 4418N/A "zlib failed to reset compressor: @1(@2): @3");
4418N/A/* This pair of functions encapsulates the operation of (a) compressing a 0N/A * text string, and (b) issuing it later as a series of chunk data writes. 0N/A * The compression_state structure is shared context for these functions 0N/A * set up by the caller in order to make the whole mess thread-safe. 4418N/A/* Compress given text into storage in the png_ptr structure */ 0N/Astatic int /* PRIVATE */ 4418N/A /* We may just want to pass the text right through */ 0N/A /* We can't write the chunk until we find out how much data we have, 0N/A * which means we need to run the compressor first and save the 0N/A * output. This shouldn't be a problem, as the vast majority of 0N/A * comments should be reasonable, but we will set up an array of 0N/A * malloc'd pointers to be sure. 0N/A * If we knew the application was well behaved, we could simplify this 0N/A * greatly by assuming we can always malloc an output buffer large 0N/A * enough to hold the compressed text ((1001 * text_len / 1000) + 12) 0N/A * and malloc this directly. The only time this would be a bad idea is 0N/A * if we can't malloc more than 64K and we have 64K of random input 0N/A * data, or if the input string is incredibly large (although this 0N/A * wouldn't cause a failure, just a slowdown due to swapping). 4418N/A /* Set up the compression buffers */ 4418N/A /* TODO: the following cast hides a potential overflow problem. */ 4418N/A /* NOTE: assume zlib doesn't overwrite the input */ 4418N/A /* This is the same compression loop as in png_write_row() */ 4418N/A /* Check to see if we need more room */ 4418N/A /* Make sure the output array has room */ 0N/A /* and reset the buffer */ 4418N/A /* Continue until we don't have any more to compress */ 4418N/A /* Finish the compression */ 4418N/A /* Tell zlib we are finished */ 4418N/A /* Check to see if we need more room */ 4418N/A /* Check to make sure our output array has room */ 0N/A /* This could be optimized to realloc() */ 0N/A /* and reset the buffer pointers */ 4418N/A /* Text length is number of buffers plus last buffer */ 4418N/A/* Ship the compressed text out via chunk writes */ 0N/Astatic void /* PRIVATE */ 4418N/A /* Handle the no-compression case */ 4418N/A unsigned int z_cmf;
/* zlib compression method and flags */ 4418N/A /* Optimize the CMF field in the zlib stream. This hack of the zlib 4418N/A * stream is compliant to the stream specification. 4418N/A "Invalid zlib compression method or flags in non-IDAT chunk");
4418N/A#
endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */ 4418N/A /* Write saved output buffers, if any */ 4418N/A /* Write anything left in zbuf */ 4418N/A#
endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */ 0N/A/* Write the IHDR chunk, and update the png_struct with the necessary 0N/A * information. Note that the rest of this code depends upon this 0N/A * information being correct. 0N/A /* Check that we have valid input data from the application info */ 4418N/A "Invalid bit depth for grayscale image");
0N/A /* Write filter_method 64 (intrapixel differencing) only if 0N/A * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and 0N/A * 2. Libpng did not write a PNG signature (this filter_method is only 0N/A * used in PNG datastreams that are embedded in MNG datastreams) and 0N/A * 3. The application called png_permit_mng_features with a mask that 0N/A * included PNG_FLAG_MNG_FILTER_64 and 0N/A * 4. The filter_method is 64 and 0N/A * 5. The color_type is RGB or RGBA 4418N/A /* Save the relevent information */ 4418N/A /* Set the usr info, so any transformations can modify it */ 4418N/A /* Pack the header information into the buffer */ 4418N/A /* Initialize zlib with PNG info */ 4418N/A#
endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */ 4418N/A#
endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */ 4418N/A /* Record that the compressor has not yet been initialized. */ 4418N/A/* Write the palette. We are careful not to trust png_color to be in the 0N/A * correct order for PNG, so people can redefine it to any convenient 4418N/A "Ignoring request to write a PLTE chunk in grayscale PNG");
4418N/A /* This is a little slower but some buggy compilers need to do this 4418N/A /* Optimize the CMF field in the zlib stream. This hack of the zlib 4418N/A * stream is compliant to the stream specification. 0N/A unsigned int z_cmf =
data[0];
/* zlib compression method and flags */ 4418N/A /* Avoid memory underflows and multiplication overflows. 4418N/A * The conditions below are practically always satisfied; 4418N/A * however, they still must be checked. 4418N/A /* Compute the maximum possible length of the datastream */ 4418N/A /* Number of pixels, plus for each row a filter byte 4418N/A * and possibly a padding byte, so increase the maximum 4418N/A * size to account for these. 4418N/A /* If it's interlaced, each block of 8 rows is sent as up to 4418N/A * 14 rows, i.e., 6 additional rows, each with a filter byte 4418N/A * and possibly a padding byte 4418N/A "Invalid zlib compression method or flags in IDAT");
4418N/A#
endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */ 4418N/A /* Prior to 1.5.4 this code was replicated in every caller (except at the 4418N/A * end, where it isn't technically necessary). Since this function has 4418N/A * flushed the data we can safely reset the zlib output buffer here. 0N/A /* file_gamma is saved in 1/100,000ths */ 4418N/A "Invalid sRGB rendering intent specified");
4418N/A "Embedded profile length in iCCP chunk is negative");
0N/A "Embedded profile length too large in iCCP chunk");
0N/A "Truncating profile to actual length in iCCP chunk");
4418N/A /* Make sure we include the NULL after the name and the compression type */ 4418N/A /* Make sure we include the NULL after the name */ 4418N/A /* Loop through each palette entry, writing appropriately */ 4418N/A /* Make sure we don't depend upon the order of PNG_COLOR_8 */ 4418N/A /* Each value is saved in 1/100,000ths */ 4418N/A /* Write the chunk out as it is */ 4418N/A "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
4418N/A "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
4418N/A/* Write the background chunk */ 4418N/A "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
4418N/A "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
0N/A/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, 0N/A * and if invalid, correct the keyword rather than discarding the entire 0N/A * chunk. The PNG 1.0 specification requires keywords 1-79 characters in 0N/A * length, forbids leading or trailing whitespace, multiple internal spaces, 0N/A * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. 0N/A * The new_key is allocated to hold the corrected keyword and must be freed 0N/A * by the calling routine. This avoids problems with trying to write to 0N/A * static keywords without having to have duplicate copies of the strings. 0N/A /* Replace non-printing characters with a blank and print a warning */ 0N/A /* Remove any trailing white space. */ 0N/A /* Remove any leading white space. */ 0N/A /* Remove multiple internal spaces. */ 4418N/A /* Make sure we include the 0 after the key */ 0N/A * We leave it to the application to meet PNG-1.0 requirements on the 0N/A * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of 0N/A * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. 0N/A * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. 4418N/A/* Write a compressed text chunk */ 4418N/A /* Compute the compressed data; do it now for the length */ 4418N/A /* Write the compressed data */ 4418N/A /* Compute the compressed data; do it now for the length */ 4418N/A /* Make sure we include the compression flag, the compression byte, 4418N/A * and the NULs after the key, lang, and lang_key parts 0N/A 5 /* comp byte, comp flag, terminators for key, lang and lang_key */ 4418N/A /* We leave it to the application to meet PNG-1.0 requirements on the 0N/A * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of 0N/A * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. 0N/A * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. 4418N/A /* Set the compression flag */ 0N/A else /* compression == PNG_ITXT_COMPRESSION_zTXt */ 4418N/A /* Set the compression method */ 4418N/A/* Write the pCAL chunk (described in the PNG extensions document) */ 0N/A /* Find the length of each parameter, making sure we don't count the 4418N/A * null terminator for the last parameter. 0N/A/* Write the tIME chunk. Use either png_convert_from_struct_tm() 0N/A * or png_convert_from_time_t(), or fill in the structure yourself. 4418N/A/* Initializes the row writing capability of libpng */ 4418N/A /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 4418N/A /* Start of interlace block */ 4418N/A /* Offset to next interlace block */ 4418N/A /* Start of interlace block in the y direction */ 4418N/A /* Offset to next interlace block in the y direction */ 4418N/A /* Set up filtering buffer, if using this filter */ 0N/A /* We only need to keep the previous row if we are using one of these. */ 4418N/A /* Set up previous row buffer */ 4418N/A#
endif /* PNG_WRITE_FILTER_SUPPORTED */ 4418N/A /* If interlaced, we need to set up width and height of pass */ 0N/A/* Internal use only. Called when finished processing a row of data. */ 4418N/A /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 4418N/A /* Start of interlace block */ 4418N/A /* Offset to next interlace block */ 4418N/A /* Start of interlace block in the y direction */ 4418N/A /* Offset to next interlace block in the y direction */ 4418N/A /* If interlaced, go to next pass */ 4418N/A /* Loop until we find a non-zero width or height pass */ 4418N/A /* Reset the row above the image for the next pass */ 4418N/A /* If we get here, we've just written the last row, so we need 0N/A to flush the compressor */ 4418N/A /* Tell the compressor we are done */ 4418N/A /* Check to see if we need more room */ 4418N/A /* Write any extra space */ 0N/A/* Pick out the correct pixels for the interlace pass. 0N/A * The basic idea here is to go through the row with a source 0N/A * pointer and a destination pointer (sp and dp), and copy the 0N/A * correct pixels for the pass. As the row gets compacted, 0N/A * sp will always be >= dp, so we should never overwrite anything. 0N/A * See the default: case for the easiest code to understand. 4418N/A /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 4418N/A /* Start of interlace block */ 4418N/A /* Offset to next interlace block */ 4418N/A /* We don't have to do anything on the last pass (6) */ 4418N/A /* Each pixel depth is handled separately */ 0N/A value = (
int)(*
sp >> (
7 - (
int)(i &
0x07))) &
0x01;
0N/A value = (*
sp >> ((
3 - (
int)(i &
0x03)) <<
1)) &
0x03;
0N/A value = (*
sp >> ((
1 - (
int)(i &
0x01)) <<
2)) &
0x0f;
4418N/A /* Start at the beginning */ 4418N/A /* Find out how many bytes each pixel takes up */ 4418N/A /* Loop through the row, only looking at the pixels that matter */ 4418N/A /* Find out where the original pixel is */ 0N/A/* This filters the row, chooses which filter to use, if it has not already 0N/A * been specified by the application, and then writes the row out with the 4418N/A /* These will never be selected so we need not test them. */ 4418N/A /* Find out how many bytes offset each pixel is */ 0N/A /* The prediction method we use is to find which method provides the 0N/A * smallest value when summing the absolute values of the distances 0N/A * from zero, using anything >= 128 as negative numbers. This is known 0N/A * as the "minimum sum of absolute differences" heuristic. Other 0N/A * heuristics are the "weighted minimum sum of absolute differences" 0N/A * (experimental and can in theory improve compression), and the "zlib 0N/A * predictive" method (not implemented yet), which does test compressions 0N/A * of lines using different filter methods, and then chooses the 0N/A * (series of) filter(s) that give minimum compressed data size (VERY 0N/A * computationally expensive). 0N/A * GRR 980525: consider also 0N/A * (1) minimum sum of absolute differences from running average (i.e., 0N/A * keep running sum of non-absolute differences & count of bytes) 0N/A * [track dispersion, too? restart average if dispersion too large?] 0N/A * (1b) minimum sum of absolute differences from sliding average, probably 0N/A * with window size <= deflate window (usually 32K) 0N/A * (2) minimum sum of squared differences from zero or running average 0N/A * (i.e., ~ root-mean-square approach) 0N/A /* We don't need to test the 'no filter' case if this is the only filter 0N/A * that has been chosen, as it doesn't actually do anything to the data. 0N/A sum += (v <
128) ? v :
256 - v;
0N/A /* Reduce the sum if we match any of the previous rows */ 0N/A /* Factor in the cost of this filter (this is here for completeness, 0N/A * but it makes no sense to have a "cost" for the NONE filter, as 0N/A * it has the minimum possible computational cost - none). 4418N/A /* It's the only filter so no testing is needed */ 0N/A /* We temporarily increase the "minimum sum" by the factor we 0N/A * would reduce the sum of this filter, so that we can do the 0N/A * early exit comparison without scaling the sum each time. 0N/A sum += (v <
128) ? v :
256 - v;
0N/A sum += (v <
128) ? v :
256 - v;
0N/A if (
sum >
lmins)
/* We are already worse, don't continue. */ 0N/A sum += (v <
128) ? v :
256 - v;
0N/A if (
sum >
lmins)
/* We are already worse, don't continue. */ 0N/A sum += (v <
128) ? v :
256 - v;
0N/A sum += (v <
128) ? v :
256 - v;
0N/A if (
sum >
lmins)
/* We are already worse, don't continue. */ 0N/A sum += (v <
128) ? v :
256 - v;
0N/A#
else /* PNG_SLOW_PAETH */ 0N/A#
endif /* PNG_SLOW_PAETH */ 0N/A sum += (v <
128) ? v :
256 - v;
0N/A if (
sum >
lmins)
/* We are already worse, don't continue. */ 4418N/A#
endif /* PNG_WRITE_FILTER_SUPPORTED */ 0N/A /* Do the actual writing of the filtered row data from the chosen filter. */ 0N/A /* Save the type of filter we picked this time for future calculations */ 4418N/A#
endif /* PNG_WRITE_FILTER_SUPPORTED */ 0N/A/* Do the actual writing of a previously filtered row. */ 4418N/A /* Set up the zlib input buffer */ 4418N/A /* Repeat until we have compressed all the data */ 4418N/A /* Record the number of bytes available - zlib supports at least 65535 4418N/A * bytes at one step, depending on the size of the zlib type 'uInt', the 4418N/A * Use this because on 16 bit systems 'rowbytes' can be up to 65536 (i.e. 4418N/A * one more than 16 bits) and, in this case 'rowbytes+1' can overflow a 4418N/A * uInt. ZLIB_IO_MAX can be safely reduced to cause zlib to be called 4418N/A * with smaller chunks of data. 4418N/A /* So this will fit in the available uInt space: */ 4418N/A /* Check for compression errors */ 4418N/A /* See if it is time to write another IDAT */ 4418N/A /* Write the IDAT and reset the zlib output buffer */ 4418N/A /* Repeat until all data has been compressed */ 4418N/A /* Swap the current and previous rows */ 4418N/A /* Finish row - updates counters and flushes zlib if last row */ 0N/A#
endif /* PNG_WRITE_SUPPORTED */