0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
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 *
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 *
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.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* png.c - location for general purpose libpng functions
0N/A *
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:
0N/A *
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 *
4418N/A * This code is released under the libpng license.
4418N/A * For conditions of distribution and use, see the disclaimer
4418N/A * and license in png.h
0N/A */
0N/A
4418N/A#include "pngpriv.h"
0N/A
0N/A/* Generate a compiler error if there is an old png.h in the search path. */
4418N/Atypedef png_libpng_version_1_5_4 Your_png_h_is_not_version_1_5_4;
0N/A
0N/A/* Tells libpng that we have already handled the first "num_bytes" bytes
0N/A * of the PNG file signature. If the PNG data is embedded into another
0N/A * stream we can set num_bytes = 8 so that libpng will not attempt to read
0N/A * or write any of the magic bytes before it starts on the IHDR.
0N/A */
0N/A
0N/A#ifdef PNG_READ_SUPPORTED
0N/Avoid PNGAPI
0N/Apng_set_sig_bytes(png_structp png_ptr, int num_bytes)
0N/A{
4418N/A png_debug(1, "in png_set_sig_bytes");
4418N/A
4418N/A if (png_ptr == NULL)
4418N/A return;
4418N/A
0N/A if (num_bytes > 8)
4418N/A png_error(png_ptr, "Too many bytes for PNG signature");
0N/A
0N/A png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
0N/A}
0N/A
0N/A/* Checks whether the supplied bytes match the PNG signature. We allow
0N/A * checking less than the full 8-byte signature so that those apps that
0N/A * already read the first few bytes of a file to determine the file type
0N/A * can simply check the remaining bytes for extra assurance. Returns
0N/A * an integer less than, equal to, or greater than zero if sig is found,
0N/A * respectively, to be less than, to match, or be greater than the correct
0N/A * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
0N/A */
0N/Aint PNGAPI
4418N/Apng_sig_cmp(png_const_bytep sig, png_size_t start, png_size_t num_to_check)
0N/A{
0N/A png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
4418N/A
0N/A if (num_to_check > 8)
0N/A num_to_check = 8;
4418N/A
0N/A else if (num_to_check < 1)
0N/A return (-1);
0N/A
0N/A if (start > 7)
0N/A return (-1);
0N/A
0N/A if (start + num_to_check > 8)
0N/A num_to_check = 8 - start;
0N/A
0N/A return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
0N/A}
0N/A
0N/A#endif /* PNG_READ_SUPPORTED */
0N/A
0N/A#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
4418N/A/* Function to allocate memory for zlib */
4418N/APNG_FUNCTION(voidpf /* PRIVATE */,
4418N/Apng_zalloc,(voidpf png_ptr, uInt items, uInt size),PNG_ALLOCATED)
0N/A{
0N/A png_voidp ptr;
0N/A png_structp p=(png_structp)png_ptr;
0N/A png_uint_32 save_flags=p->flags;
4418N/A png_alloc_size_t num_bytes;
0N/A
4418N/A if (png_ptr == NULL)
4418N/A return (NULL);
4418N/A
0N/A if (items > PNG_UINT_32_MAX/size)
0N/A {
0N/A png_warning (p, "Potential overflow in png_zalloc()");
0N/A return (NULL);
0N/A }
4418N/A num_bytes = (png_alloc_size_t)items * size;
0N/A
0N/A p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
0N/A ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
0N/A p->flags=save_flags;
0N/A
0N/A return ((voidpf)ptr);
0N/A}
0N/A
4418N/A/* Function to free memory for zlib */
4418N/Avoid /* PRIVATE */
0N/Apng_zfree(voidpf png_ptr, voidpf ptr)
0N/A{
0N/A png_free((png_structp)png_ptr, (png_voidp)ptr);
0N/A}
0N/A
0N/A/* Reset the CRC variable to 32 bits of 1's. Care must be taken
0N/A * in case CRC is > 32 bits to leave the top bits 0.
0N/A */
0N/Avoid /* PRIVATE */
0N/Apng_reset_crc(png_structp png_ptr)
0N/A{
0N/A png_ptr->crc = crc32(0, Z_NULL, 0);
0N/A}
0N/A
0N/A/* Calculate the CRC over a section of data. We can only pass as
0N/A * much data to this routine as the largest single buffer size. We
0N/A * also check that this data will actually be used before going to the
0N/A * trouble of calculating it.
0N/A */
0N/Avoid /* PRIVATE */
4418N/Apng_calculate_crc(png_structp png_ptr, png_const_bytep ptr, png_size_t length)
0N/A{
0N/A int need_crc = 1;
0N/A
0N/A if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
0N/A {
0N/A if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
0N/A (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
0N/A need_crc = 0;
0N/A }
4418N/A
0N/A else /* critical */
0N/A {
0N/A if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
0N/A need_crc = 0;
0N/A }
0N/A
0N/A if (need_crc)
0N/A png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
0N/A}
0N/A
4418N/A/* Check a user supplied version number, called from both read and write
4418N/A * functions that create a png_struct
4418N/A */
4418N/Aint
4418N/Apng_user_version_check(png_structp png_ptr, png_const_charp user_png_ver)
4418N/A{
4418N/A if (user_png_ver)
4418N/A {
4418N/A int i = 0;
4418N/A
4418N/A do
4418N/A {
4418N/A if (user_png_ver[i] != png_libpng_ver[i])
4418N/A png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
4418N/A } while (png_libpng_ver[i++]);
4418N/A }
4418N/A
4418N/A else
4418N/A png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
4418N/A
4418N/A if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
4418N/A {
4418N/A /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
4418N/A * we must recompile any applications that use any older library version.
4418N/A * For versions after libpng 1.0, we will be compatible, so we need
4418N/A * only check the first digit.
4418N/A */
4418N/A if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
4418N/A (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
4418N/A (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
4418N/A {
4418N/A#ifdef PNG_WARNINGS_SUPPORTED
4418N/A size_t pos = 0;
4418N/A char m[128];
4418N/A
4418N/A pos = png_safecat(m, sizeof m, pos, "Application built with libpng-");
4418N/A pos = png_safecat(m, sizeof m, pos, user_png_ver);
4418N/A pos = png_safecat(m, sizeof m, pos, " but running with ");
4418N/A pos = png_safecat(m, sizeof m, pos, png_libpng_ver);
4418N/A
4418N/A png_warning(png_ptr, m);
4418N/A#endif
4418N/A
4418N/A#ifdef PNG_ERROR_NUMBERS_SUPPORTED
4418N/A png_ptr->flags = 0;
4418N/A#endif
4418N/A
4418N/A return 0;
4418N/A }
4418N/A }
4418N/A
4418N/A /* Success return. */
4418N/A return 1;
4418N/A}
4418N/A
0N/A/* Allocate the memory for an info_struct for the application. We don't
0N/A * really need the png_ptr, but it could potentially be useful in the
0N/A * future. This should be used in favour of malloc(png_sizeof(png_info))
0N/A * and png_info_init() so that applications that want to use a shared
0N/A * libpng don't have to be recompiled if png_info changes size.
0N/A */
4418N/APNG_FUNCTION(png_infop,PNGAPI
4418N/Apng_create_info_struct,(png_structp png_ptr),PNG_ALLOCATED)
0N/A{
0N/A png_infop info_ptr;
0N/A
4418N/A png_debug(1, "in png_create_info_struct");
4418N/A
4418N/A if (png_ptr == NULL)
4418N/A return (NULL);
4418N/A
0N/A#ifdef PNG_USER_MEM_SUPPORTED
0N/A info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
0N/A png_ptr->malloc_fn, png_ptr->mem_ptr);
0N/A#else
0N/A info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
0N/A#endif
0N/A if (info_ptr != NULL)
0N/A png_info_init_3(&info_ptr, png_sizeof(png_info));
0N/A
0N/A return (info_ptr);
0N/A}
0N/A
0N/A/* This function frees the memory associated with a single info struct.
0N/A * Normally, one would use either png_destroy_read_struct() or
0N/A * png_destroy_write_struct() to free an info struct, but this may be
0N/A * useful for some applications.
0N/A */
0N/Avoid PNGAPI
0N/Apng_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
0N/A{
0N/A png_infop info_ptr = NULL;
4418N/A
4418N/A png_debug(1, "in png_destroy_info_struct");
0N/A
4418N/A if (png_ptr == NULL)
4418N/A return;
4418N/A
0N/A if (info_ptr_ptr != NULL)
0N/A info_ptr = *info_ptr_ptr;
0N/A
0N/A if (info_ptr != NULL)
0N/A {
0N/A png_info_destroy(png_ptr, info_ptr);
0N/A
0N/A#ifdef PNG_USER_MEM_SUPPORTED
0N/A png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
0N/A png_ptr->mem_ptr);
0N/A#else
0N/A png_destroy_struct((png_voidp)info_ptr);
0N/A#endif
0N/A *info_ptr_ptr = NULL;
0N/A }
0N/A}
0N/A
0N/A/* Initialize the info structure. This is now an internal function (0.89)
0N/A * and applications using it are urged to use png_create_info_struct()
0N/A * instead.
0N/A */
0N/A
0N/Avoid PNGAPI
0N/Apng_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
0N/A{
0N/A png_infop info_ptr = *ptr_ptr;
0N/A
4418N/A png_debug(1, "in png_info_init_3");
0N/A
4418N/A if (info_ptr == NULL)
4418N/A return;
0N/A
4418N/A if (png_sizeof(png_info) > png_info_struct_size)
4418N/A {
4418N/A png_destroy_struct(info_ptr);
4418N/A info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
4418N/A *ptr_ptr = info_ptr;
4418N/A }
0N/A
4418N/A /* Set everything to 0 */
4418N/A png_memset(info_ptr, 0, png_sizeof(png_info));
0N/A}
0N/A
0N/Avoid PNGAPI
0N/Apng_data_freer(png_structp png_ptr, png_infop info_ptr,
0N/A int freer, png_uint_32 mask)
0N/A{
4418N/A png_debug(1, "in png_data_freer");
4418N/A
0N/A if (png_ptr == NULL || info_ptr == NULL)
0N/A return;
4418N/A
4418N/A if (freer == PNG_DESTROY_WILL_FREE_DATA)
0N/A info_ptr->free_me |= mask;
4418N/A
4418N/A else if (freer == PNG_USER_WILL_FREE_DATA)
0N/A info_ptr->free_me &= ~mask;
4418N/A
0N/A else
0N/A png_warning(png_ptr,
4418N/A "Unknown freer parameter in png_data_freer");
0N/A}
0N/A
0N/Avoid PNGAPI
0N/Apng_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
0N/A int num)
0N/A{
4418N/A png_debug(1, "in png_free_data");
4418N/A
0N/A if (png_ptr == NULL || info_ptr == NULL)
0N/A return;
0N/A
4418N/A#ifdef PNG_TEXT_SUPPORTED
4418N/A /* Free text item num or (if num == -1) all text items */
4418N/A if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
0N/A {
4418N/A if (num != -1)
4418N/A {
4418N/A if (info_ptr->text && info_ptr->text[num].key)
4418N/A {
4418N/A png_free(png_ptr, info_ptr->text[num].key);
4418N/A info_ptr->text[num].key = NULL;
4418N/A }
4418N/A }
4418N/A
4418N/A else
4418N/A {
4418N/A int i;
4418N/A for (i = 0; i < info_ptr->num_text; i++)
4418N/A png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
4418N/A png_free(png_ptr, info_ptr->text);
4418N/A info_ptr->text = NULL;
4418N/A info_ptr->num_text=0;
4418N/A }
0N/A }
0N/A#endif
0N/A
4418N/A#ifdef PNG_tRNS_SUPPORTED
4418N/A /* Free any tRNS entry */
4418N/A if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
4418N/A {
4418N/A png_free(png_ptr, info_ptr->trans_alpha);
4418N/A info_ptr->trans_alpha = NULL;
4418N/A info_ptr->valid &= ~PNG_INFO_tRNS;
4418N/A }
0N/A#endif
0N/A
4418N/A#ifdef PNG_sCAL_SUPPORTED
4418N/A /* Free any sCAL entry */
4418N/A if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
4418N/A {
4418N/A png_free(png_ptr, info_ptr->scal_s_width);
4418N/A png_free(png_ptr, info_ptr->scal_s_height);
4418N/A info_ptr->scal_s_width = NULL;
4418N/A info_ptr->scal_s_height = NULL;
4418N/A info_ptr->valid &= ~PNG_INFO_sCAL;
4418N/A }
0N/A#endif
0N/A
4418N/A#ifdef PNG_pCAL_SUPPORTED
4418N/A /* Free any pCAL entry */
4418N/A if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
4418N/A {
4418N/A png_free(png_ptr, info_ptr->pcal_purpose);
4418N/A png_free(png_ptr, info_ptr->pcal_units);
4418N/A info_ptr->pcal_purpose = NULL;
4418N/A info_ptr->pcal_units = NULL;
4418N/A if (info_ptr->pcal_params != NULL)
4418N/A {
4418N/A int i;
4418N/A for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
4418N/A {
4418N/A png_free(png_ptr, info_ptr->pcal_params[i]);
4418N/A info_ptr->pcal_params[i] = NULL;
4418N/A }
4418N/A png_free(png_ptr, info_ptr->pcal_params);
4418N/A info_ptr->pcal_params = NULL;
4418N/A }
4418N/A info_ptr->valid &= ~PNG_INFO_pCAL;
4418N/A }
0N/A#endif
0N/A
4418N/A#ifdef PNG_iCCP_SUPPORTED
4418N/A /* Free any iCCP entry */
4418N/A if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
4418N/A {
4418N/A png_free(png_ptr, info_ptr->iccp_name);
4418N/A png_free(png_ptr, info_ptr->iccp_profile);
4418N/A info_ptr->iccp_name = NULL;
4418N/A info_ptr->iccp_profile = NULL;
4418N/A info_ptr->valid &= ~PNG_INFO_iCCP;
4418N/A }
0N/A#endif
0N/A
4418N/A#ifdef PNG_sPLT_SUPPORTED
4418N/A /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
4418N/A if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
0N/A {
4418N/A if (num != -1)
0N/A {
4418N/A if (info_ptr->splt_palettes)
4418N/A {
4418N/A png_free(png_ptr, info_ptr->splt_palettes[num].name);
4418N/A png_free(png_ptr, info_ptr->splt_palettes[num].entries);
4418N/A info_ptr->splt_palettes[num].name = NULL;
4418N/A info_ptr->splt_palettes[num].entries = NULL;
4418N/A }
4418N/A }
4418N/A
4418N/A else
4418N/A {
4418N/A if (info_ptr->splt_palettes_num)
4418N/A {
4418N/A int i;
4418N/A for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
4418N/A png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
4418N/A
4418N/A png_free(png_ptr, info_ptr->splt_palettes);
4418N/A info_ptr->splt_palettes = NULL;
4418N/A info_ptr->splt_palettes_num = 0;
4418N/A }
4418N/A info_ptr->valid &= ~PNG_INFO_sPLT;
0N/A }
0N/A }
0N/A#endif
0N/A
4418N/A#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
4418N/A if (png_ptr->unknown_chunk.data)
4418N/A {
4418N/A png_free(png_ptr, png_ptr->unknown_chunk.data);
4418N/A png_ptr->unknown_chunk.data = NULL;
4418N/A }
4418N/A
4418N/A if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
0N/A {
4418N/A if (num != -1)
4418N/A {
4418N/A if (info_ptr->unknown_chunks)
4418N/A {
4418N/A png_free(png_ptr, info_ptr->unknown_chunks[num].data);
4418N/A info_ptr->unknown_chunks[num].data = NULL;
4418N/A }
4418N/A }
0N/A
4418N/A else
4418N/A {
4418N/A int i;
0N/A
4418N/A if (info_ptr->unknown_chunks_num)
4418N/A {
4418N/A for (i = 0; i < info_ptr->unknown_chunks_num; i++)
4418N/A png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
4418N/A
4418N/A png_free(png_ptr, info_ptr->unknown_chunks);
4418N/A info_ptr->unknown_chunks = NULL;
4418N/A info_ptr->unknown_chunks_num = 0;
4418N/A }
4418N/A }
0N/A }
0N/A#endif
0N/A
4418N/A#ifdef PNG_hIST_SUPPORTED
4418N/A /* Free any hIST entry */
4418N/A if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
4418N/A {
4418N/A png_free(png_ptr, info_ptr->hist);
4418N/A info_ptr->hist = NULL;
4418N/A info_ptr->valid &= ~PNG_INFO_hIST;
4418N/A }
0N/A#endif
0N/A
4418N/A /* Free any PLTE entry that was internally allocated */
4418N/A if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
4418N/A {
4418N/A png_zfree(png_ptr, info_ptr->palette);
4418N/A info_ptr->palette = NULL;
4418N/A info_ptr->valid &= ~PNG_INFO_PLTE;
4418N/A info_ptr->num_palette = 0;
4418N/A }
0N/A
4418N/A#ifdef PNG_INFO_IMAGE_SUPPORTED
4418N/A /* Free any image bits attached to the info structure */
4418N/A if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
4418N/A {
4418N/A if (info_ptr->row_pointers)
4418N/A {
4418N/A int row;
4418N/A for (row = 0; row < (int)info_ptr->height; row++)
4418N/A {
4418N/A png_free(png_ptr, info_ptr->row_pointers[row]);
4418N/A info_ptr->row_pointers[row] = NULL;
4418N/A }
4418N/A png_free(png_ptr, info_ptr->row_pointers);
4418N/A info_ptr->row_pointers = NULL;
4418N/A }
4418N/A info_ptr->valid &= ~PNG_INFO_IDAT;
4418N/A }
0N/A#endif
0N/A
4418N/A if (num != -1)
4418N/A mask &= ~PNG_FREE_MUL;
4418N/A
4418N/A info_ptr->free_me &= ~mask;
0N/A}
0N/A
0N/A/* This is an internal routine to free any memory that the info struct is
0N/A * pointing to before re-using it or freeing the struct itself. Recall
0N/A * that png_free() checks for NULL pointers for us.
0N/A */
0N/Avoid /* PRIVATE */
0N/Apng_info_destroy(png_structp png_ptr, png_infop info_ptr)
0N/A{
4418N/A png_debug(1, "in png_info_destroy");
0N/A
0N/A png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
0N/A
4418N/A#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
0N/A if (png_ptr->num_chunk_list)
0N/A {
4418N/A png_free(png_ptr, png_ptr->chunk_list);
4418N/A png_ptr->chunk_list = NULL;
4418N/A png_ptr->num_chunk_list = 0;
0N/A }
0N/A#endif
0N/A
0N/A png_info_init_3(&info_ptr, png_sizeof(png_info));
0N/A}
0N/A#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
0N/A
0N/A/* This function returns a pointer to the io_ptr associated with the user
0N/A * functions. The application should free any memory associated with this
0N/A * pointer before png_write_destroy() or png_read_destroy() are called.
0N/A */
0N/Apng_voidp PNGAPI
0N/Apng_get_io_ptr(png_structp png_ptr)
0N/A{
4418N/A if (png_ptr == NULL)
4418N/A return (NULL);
4418N/A
0N/A return (png_ptr->io_ptr);
0N/A}
0N/A
0N/A#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
4418N/A# ifdef PNG_STDIO_SUPPORTED
0N/A/* Initialize the default input/output functions for the PNG file. If you
0N/A * use your own read or write routines, you can call either png_set_read_fn()
0N/A * or png_set_write_fn() instead of png_init_io(). If you have defined
0N/A * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
0N/A * necessarily available.
0N/A */
0N/Avoid PNGAPI
0N/Apng_init_io(png_structp png_ptr, png_FILE_p fp)
0N/A{
4418N/A png_debug(1, "in png_init_io");
4418N/A
4418N/A if (png_ptr == NULL)
4418N/A return;
4418N/A
0N/A png_ptr->io_ptr = (png_voidp)fp;
0N/A}
4418N/A# endif
0N/A
4418N/A# ifdef PNG_TIME_RFC1123_SUPPORTED
0N/A/* Convert the supplied time into an RFC 1123 string suitable for use in
0N/A * a "Creation Time" or other text-based time string.
0N/A */
4418N/Apng_const_charp PNGAPI
4418N/Apng_convert_to_rfc1123(png_structp png_ptr, png_const_timep ptime)
0N/A{
0N/A static PNG_CONST char short_months[12][4] =
0N/A {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
0N/A "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
0N/A
4418N/A if (png_ptr == NULL)
4418N/A return (NULL);
4418N/A
0N/A {
4418N/A size_t pos = 0;
4418N/A char number_buf[5]; /* enough for a four digit year */
4418N/A
4418N/A# define APPEND_STRING(string)\
4418N/A pos = png_safecat(png_ptr->time_buffer, sizeof png_ptr->time_buffer,\
4418N/A pos, (string))
4418N/A# define APPEND_NUMBER(format, value)\
4418N/A APPEND_STRING(PNG_FORMAT_NUMBER(number_buf, format, (value)))
4418N/A# define APPEND(ch)\
4418N/A if (pos < (sizeof png_ptr->time_buffer)-1)\
4418N/A png_ptr->time_buffer[pos++] = (ch)
4418N/A
4418N/A APPEND_NUMBER(PNG_NUMBER_FORMAT_u, (unsigned)ptime->day % 32);
4418N/A APPEND(' ');
4418N/A APPEND_STRING(short_months[(ptime->month - 1) % 12]);
4418N/A APPEND(' ');
4418N/A APPEND_NUMBER(PNG_NUMBER_FORMAT_u, ptime->year);
4418N/A APPEND(' ');
4418N/A APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->hour % 24);
4418N/A APPEND(':');
4418N/A APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->minute % 60);
4418N/A APPEND(':');
4418N/A APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->second % 61);
4418N/A APPEND_STRING(" +0000"); /* This reliably terminates the buffer */
4418N/A
4418N/A# undef APPEND
4418N/A# undef APPEND_NUMBER
4418N/A# undef APPEND_STRING
0N/A }
0N/A
4418N/A return png_ptr->time_buffer;
0N/A}
4418N/A# endif /* PNG_TIME_RFC1123_SUPPORTED */
0N/A
0N/A#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
0N/A
4418N/Apng_const_charp PNGAPI
4418N/Apng_get_copyright(png_const_structp png_ptr)
0N/A{
4418N/A PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
4418N/A#ifdef PNG_STRING_COPYRIGHT
4418N/A return PNG_STRING_COPYRIGHT
4418N/A#else
4418N/A# ifdef __STDC__
4418N/A return PNG_STRING_NEWLINE \
4418N/A "libpng version 1.5.4 - July 7, 2011" PNG_STRING_NEWLINE \
4418N/A "Copyright (c) 1998-2011 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
4418N/A "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
4418N/A "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
4418N/A PNG_STRING_NEWLINE;
4418N/A# else
4418N/A return "libpng version 1.5.4 - July 7, 2011\
4418N/A Copyright (c) 1998-2011 Glenn Randers-Pehrson\
4418N/A Copyright (c) 1996-1997 Andreas Dilger\
4418N/A Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.";
4418N/A# endif
4418N/A#endif
0N/A}
0N/A
0N/A/* The following return the library version as a short string in the
0N/A * format 1.0.0 through 99.99.99zz. To get the version of *.h files
0N/A * used with your application, print out PNG_LIBPNG_VER_STRING, which
0N/A * is defined in png.h.
0N/A * Note: now there is no difference between png_get_libpng_ver() and
0N/A * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
0N/A * it is guaranteed that png.c uses the correct version of png.h.
0N/A */
4418N/Apng_const_charp PNGAPI
4418N/Apng_get_libpng_ver(png_const_structp png_ptr)
0N/A{
0N/A /* Version of *.c files used when building libpng */
4418N/A return png_get_header_ver(png_ptr);
0N/A}
0N/A
4418N/Apng_const_charp PNGAPI
4418N/Apng_get_header_ver(png_const_structp png_ptr)
0N/A{
0N/A /* Version of *.h files used when building libpng */
4418N/A PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
4418N/A return PNG_LIBPNG_VER_STRING;
0N/A}
0N/A
4418N/Apng_const_charp PNGAPI
4418N/Apng_get_header_version(png_const_structp png_ptr)
0N/A{
0N/A /* Returns longer string containing both version and date */
4418N/A PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
4418N/A#ifdef __STDC__
4418N/A return PNG_HEADER_VERSION_STRING
4418N/A# ifndef PNG_READ_SUPPORTED
4418N/A " (NO READ SUPPORT)"
4418N/A# endif
4418N/A PNG_STRING_NEWLINE;
4418N/A#else
4418N/A return PNG_HEADER_VERSION_STRING;
4418N/A#endif
0N/A}
0N/A
0N/A#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
4418N/A# ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
0N/Aint PNGAPI
4418N/Apng_handle_as_unknown(png_structp png_ptr, png_const_bytep chunk_name)
0N/A{
4418N/A /* Check chunk_name and return "keep" value if it's on the list, else 0 */
0N/A int i;
0N/A png_bytep p;
4418N/A if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
0N/A return 0;
4418N/A
4418N/A p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
4418N/A for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
0N/A if (!png_memcmp(chunk_name, p, 4))
4418N/A return ((int)*(p + 4));
0N/A return 0;
0N/A}
4418N/A# endif
4418N/A#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
0N/A
4418N/A#ifdef PNG_READ_SUPPORTED
0N/A/* This function, added to libpng-1.0.6g, is untested. */
0N/Aint PNGAPI
0N/Apng_reset_zstream(png_structp png_ptr)
0N/A{
4418N/A if (png_ptr == NULL)
4418N/A return Z_STREAM_ERROR;
4418N/A
0N/A return (inflateReset(&png_ptr->zstream));
0N/A}
4418N/A#endif /* PNG_READ_SUPPORTED */
0N/A
0N/A/* This function was added to libpng-1.0.7 */
0N/Apng_uint_32 PNGAPI
0N/Apng_access_version_number(void)
0N/A{
0N/A /* Version of *.c files used when building libpng */
4418N/A return((png_uint_32)PNG_LIBPNG_VER);
0N/A}
0N/A
0N/A
0N/A
0N/A#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
4418N/A# ifdef PNG_SIZE_T
0N/A/* Added at libpng version 1.2.6 */
0N/A PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
0N/Apng_size_t PNGAPI
0N/Apng_convert_size(size_t size)
0N/A{
4418N/A if (size > (png_size_t)-1)
4418N/A PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
4418N/A
4418N/A return ((png_size_t)size);
4418N/A}
4418N/A# endif /* PNG_SIZE_T */
4418N/A
4418N/A/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
4418N/A# ifdef PNG_CHECK_cHRM_SUPPORTED
4418N/A
4418N/Aint /* PRIVATE */
4418N/Apng_check_cHRM_fixed(png_structp png_ptr,
4418N/A png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
4418N/A png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
4418N/A png_fixed_point blue_x, png_fixed_point blue_y)
4418N/A{
4418N/A int ret = 1;
4418N/A unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
4418N/A
4418N/A png_debug(1, "in function png_check_cHRM_fixed");
4418N/A
4418N/A if (png_ptr == NULL)
4418N/A return 0;
4418N/A
4418N/A /* (x,y,z) values are first limited to 0..100000 (PNG_FP_1), the white
4418N/A * y must also be greater than 0. To test for the upper limit calculate
4418N/A * (PNG_FP_1-y) - x must be <= to this for z to be >= 0 (and the expression
4418N/A * cannot overflow.) At this point we know x and y are >= 0 and (x+y) is
4418N/A * <= PNG_FP_1. The previous test on PNG_MAX_UINT_31 is removed because it
4418N/A * pointless (and it produces compiler warnings!)
4418N/A */
4418N/A if (white_x < 0 || white_y <= 0 ||
4418N/A red_x < 0 || red_y < 0 ||
4418N/A green_x < 0 || green_y < 0 ||
4418N/A blue_x < 0 || blue_y < 0)
4418N/A {
4418N/A png_warning(png_ptr,
4418N/A "Ignoring attempt to set negative chromaticity value");
4418N/A ret = 0;
4418N/A }
4418N/A /* And (x+y) must be <= PNG_FP_1 (so z is >= 0) */
4418N/A if (white_x > PNG_FP_1 - white_y)
4418N/A {
4418N/A png_warning(png_ptr, "Invalid cHRM white point");
4418N/A ret = 0;
4418N/A }
4418N/A
4418N/A if (red_x > PNG_FP_1 - red_y)
4418N/A {
4418N/A png_warning(png_ptr, "Invalid cHRM red point");
4418N/A ret = 0;
4418N/A }
4418N/A
4418N/A if (green_x > PNG_FP_1 - green_y)
4418N/A {
4418N/A png_warning(png_ptr, "Invalid cHRM green point");
4418N/A ret = 0;
4418N/A }
4418N/A
4418N/A if (blue_x > PNG_FP_1 - blue_y)
4418N/A {
4418N/A png_warning(png_ptr, "Invalid cHRM blue point");
4418N/A ret = 0;
4418N/A }
4418N/A
4418N/A png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
4418N/A png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
4418N/A
4418N/A if (xy_hi == yx_hi && xy_lo == yx_lo)
4418N/A {
4418N/A png_warning(png_ptr,
4418N/A "Ignoring attempt to set cHRM RGB triangle with zero area");
4418N/A ret = 0;
4418N/A }
4418N/A
4418N/A return ret;
4418N/A}
4418N/A# endif /* PNG_CHECK_cHRM_SUPPORTED */
4418N/A
4418N/Avoid /* PRIVATE */
4418N/Apng_check_IHDR(png_structp png_ptr,
4418N/A png_uint_32 width, png_uint_32 height, int bit_depth,
4418N/A int color_type, int interlace_type, int compression_type,
4418N/A int filter_type)
4418N/A{
4418N/A int error = 0;
4418N/A
4418N/A /* Check for width and height valid values */
4418N/A if (width == 0)
4418N/A {
4418N/A png_warning(png_ptr, "Image width is zero in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A
4418N/A if (height == 0)
4418N/A {
4418N/A png_warning(png_ptr, "Image height is zero in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A
4418N/A# ifdef PNG_SET_USER_LIMITS_SUPPORTED
4418N/A if (width > png_ptr->user_width_max)
4418N/A
4418N/A# else
4418N/A if (width > PNG_USER_WIDTH_MAX)
4418N/A# endif
4418N/A {
4418N/A png_warning(png_ptr, "Image width exceeds user limit in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A
4418N/A# ifdef PNG_SET_USER_LIMITS_SUPPORTED
4418N/A if (height > png_ptr->user_height_max)
4418N/A# else
4418N/A if (height > PNG_USER_HEIGHT_MAX)
4418N/A# endif
4418N/A {
4418N/A png_warning(png_ptr, "Image height exceeds user limit in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A
4418N/A if (width > PNG_UINT_31_MAX)
4418N/A {
4418N/A png_warning(png_ptr, "Invalid image width in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A
4418N/A if (height > PNG_UINT_31_MAX)
4418N/A {
4418N/A png_warning(png_ptr, "Invalid image height in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A
4418N/A if (width > (PNG_UINT_32_MAX
4418N/A >> 3) /* 8-byte RGBA pixels */
4418N/A - 48 /* bigrowbuf hack */
4418N/A - 1 /* filter byte */
4418N/A - 7*8 /* rounding of width to multiple of 8 pixels */
4418N/A - 8) /* extra max_pixel_depth pad */
4418N/A png_warning(png_ptr, "Width is too large for libpng to process pixels");
4418N/A
4418N/A /* Check other values */
4418N/A if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
4418N/A bit_depth != 8 && bit_depth != 16)
4418N/A {
4418N/A png_warning(png_ptr, "Invalid bit depth in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A
4418N/A if (color_type < 0 || color_type == 1 ||
4418N/A color_type == 5 || color_type > 6)
4418N/A {
4418N/A png_warning(png_ptr, "Invalid color type in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A
4418N/A if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
4418N/A ((color_type == PNG_COLOR_TYPE_RGB ||
4418N/A color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
4418N/A color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
4418N/A {
4418N/A png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A
4418N/A if (interlace_type >= PNG_INTERLACE_LAST)
4418N/A {
4418N/A png_warning(png_ptr, "Unknown interlace method in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A
4418N/A if (compression_type != PNG_COMPRESSION_TYPE_BASE)
4418N/A {
4418N/A png_warning(png_ptr, "Unknown compression method in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A
4418N/A# ifdef PNG_MNG_FEATURES_SUPPORTED
4418N/A /* Accept filter_method 64 (intrapixel differencing) only if
4418N/A * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
4418N/A * 2. Libpng did not read a PNG signature (this filter_method is only
4418N/A * used in PNG datastreams that are embedded in MNG datastreams) and
4418N/A * 3. The application called png_permit_mng_features with a mask that
4418N/A * included PNG_FLAG_MNG_FILTER_64 and
4418N/A * 4. The filter_method is 64 and
4418N/A * 5. The color_type is RGB or RGBA
4418N/A */
4418N/A if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) &&
4418N/A png_ptr->mng_features_permitted)
4418N/A png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
4418N/A
4418N/A if (filter_type != PNG_FILTER_TYPE_BASE)
4418N/A {
4418N/A if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
4418N/A (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
4418N/A ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
4418N/A (color_type == PNG_COLOR_TYPE_RGB ||
4418N/A color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
4418N/A {
4418N/A png_warning(png_ptr, "Unknown filter method in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A
4418N/A if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE)
4418N/A {
4418N/A png_warning(png_ptr, "Invalid filter method in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A }
4418N/A
4418N/A# else
4418N/A if (filter_type != PNG_FILTER_TYPE_BASE)
4418N/A {
4418N/A png_warning(png_ptr, "Unknown filter method in IHDR");
4418N/A error = 1;
4418N/A }
4418N/A# endif
4418N/A
4418N/A if (error == 1)
4418N/A png_error(png_ptr, "Invalid IHDR data");
4418N/A}
4418N/A
4418N/A#if defined(PNG_sCAL_SUPPORTED) || defined(PNG_pCAL_SUPPORTED)
4418N/A/* ASCII to fp functions */
4418N/A/* Check an ASCII formated floating point value, see the more detailed
4418N/A * comments in pngpriv.h
4418N/A */
4418N/A/* The following is used internally to preserve the sticky flags */
4418N/A#define png_fp_add(state, flags) ((state) |= (flags))
4418N/A#define png_fp_set(state, value) ((state) = (value) | ((state) & PNG_FP_STICKY))
4418N/A
4418N/Aint /* PRIVATE */
4418N/Apng_check_fp_number(png_const_charp string, png_size_t size, int *statep,
4418N/A png_size_tp whereami)
4418N/A{
4418N/A int state = *statep;
4418N/A png_size_t i = *whereami;
4418N/A
4418N/A while (i < size)
4418N/A {
4418N/A int type;
4418N/A /* First find the type of the next character */
4418N/A switch (string[i])
4418N/A {
4418N/A case 43: type = PNG_FP_SAW_SIGN; break;
4418N/A case 45: type = PNG_FP_SAW_SIGN + PNG_FP_NEGATIVE; break;
4418N/A case 46: type = PNG_FP_SAW_DOT; break;
4418N/A case 48: type = PNG_FP_SAW_DIGIT; break;
4418N/A case 49: case 50: case 51: case 52:
4418N/A case 53: case 54: case 55: case 56:
4418N/A case 57: type = PNG_FP_SAW_DIGIT + PNG_FP_NONZERO; break;
4418N/A case 69:
4418N/A case 101: type = PNG_FP_SAW_E; break;
4418N/A default: goto PNG_FP_End;
4418N/A }
4418N/A
4418N/A /* Now deal with this type according to the current
4418N/A * state, the type is arranged to not overlap the
4418N/A * bits of the PNG_FP_STATE.
4418N/A */
4418N/A switch ((state & PNG_FP_STATE) + (type & PNG_FP_SAW_ANY))
4418N/A {
4418N/A case PNG_FP_INTEGER + PNG_FP_SAW_SIGN:
4418N/A if (state & PNG_FP_SAW_ANY)
4418N/A goto PNG_FP_End; /* not a part of the number */
4418N/A
4418N/A png_fp_add(state, type);
4418N/A break;
4418N/A
4418N/A case PNG_FP_INTEGER + PNG_FP_SAW_DOT:
4418N/A /* Ok as trailer, ok as lead of fraction. */
4418N/A if (state & PNG_FP_SAW_DOT) /* two dots */
4418N/A goto PNG_FP_End;
4418N/A
4418N/A else if (state & PNG_FP_SAW_DIGIT) /* trailing dot? */
4418N/A png_fp_add(state, type);
4418N/A
4418N/A else
4418N/A png_fp_set(state, PNG_FP_FRACTION | type);
4418N/A
4418N/A break;
4418N/A
4418N/A case PNG_FP_INTEGER + PNG_FP_SAW_DIGIT:
4418N/A if (state & PNG_FP_SAW_DOT) /* delayed fraction */
4418N/A png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT);
4418N/A
4418N/A png_fp_add(state, type | PNG_FP_WAS_VALID);
4418N/A
4418N/A break;
4418N/A
4418N/A case PNG_FP_INTEGER + PNG_FP_SAW_E:
4418N/A if ((state & PNG_FP_SAW_DIGIT) == 0)
4418N/A goto PNG_FP_End;
4418N/A
4418N/A png_fp_set(state, PNG_FP_EXPONENT);
4418N/A
4418N/A break;
4418N/A
4418N/A /* case PNG_FP_FRACTION + PNG_FP_SAW_SIGN:
4418N/A goto PNG_FP_End; ** no sign in fraction */
4418N/A
4418N/A /* case PNG_FP_FRACTION + PNG_FP_SAW_DOT:
4418N/A goto PNG_FP_End; ** Because SAW_DOT is always set */
4418N/A
4418N/A case PNG_FP_FRACTION + PNG_FP_SAW_DIGIT:
4418N/A png_fp_add(state, type | PNG_FP_WAS_VALID);
4418N/A break;
4418N/A
4418N/A case PNG_FP_FRACTION + PNG_FP_SAW_E:
4418N/A /* This is correct because the trailing '.' on an
4418N/A * integer is handled above - so we can only get here
4418N/A * with the sequence ".E" (with no preceding digits).
4418N/A */
4418N/A if ((state & PNG_FP_SAW_DIGIT) == 0)
4418N/A goto PNG_FP_End;
4418N/A
4418N/A png_fp_set(state, PNG_FP_EXPONENT);
4418N/A
4418N/A break;
4418N/A
4418N/A case PNG_FP_EXPONENT + PNG_FP_SAW_SIGN:
4418N/A if (state & PNG_FP_SAW_ANY)
4418N/A goto PNG_FP_End; /* not a part of the number */
4418N/A
4418N/A png_fp_add(state, PNG_FP_SAW_SIGN);
4418N/A
4418N/A break;
4418N/A
4418N/A /* case PNG_FP_EXPONENT + PNG_FP_SAW_DOT:
4418N/A goto PNG_FP_End; */
4418N/A
4418N/A case PNG_FP_EXPONENT + PNG_FP_SAW_DIGIT:
4418N/A png_fp_add(state, PNG_FP_SAW_DIGIT | PNG_FP_WAS_VALID);
4418N/A
4418N/A break;
4418N/A
4418N/A /* case PNG_FP_EXPONEXT + PNG_FP_SAW_E:
4418N/A goto PNG_FP_End; */
4418N/A
4418N/A default: goto PNG_FP_End; /* I.e. break 2 */
4418N/A }
4418N/A
4418N/A /* The character seems ok, continue. */
4418N/A ++i;
4418N/A }
4418N/A
4418N/APNG_FP_End:
4418N/A /* Here at the end, update the state and return the correct
4418N/A * return code.
4418N/A */
4418N/A *statep = state;
4418N/A *whereami = i;
4418N/A
4418N/A return (state & PNG_FP_SAW_DIGIT) != 0;
4418N/A}
4418N/A
4418N/A
4418N/A/* The same but for a complete string. */
4418N/Aint
4418N/Apng_check_fp_string(png_const_charp string, png_size_t size)
4418N/A{
4418N/A int state=0;
4418N/A png_size_t char_index=0;
4418N/A
4418N/A if (png_check_fp_number(string, size, &state, &char_index) &&
4418N/A (char_index == size || string[char_index] == 0))
4418N/A return state /* must be non-zero - see above */;
4418N/A
4418N/A return 0; /* i.e. fail */
4418N/A}
4418N/A#endif /* pCAL or sCAL */
4418N/A
4418N/A#ifdef PNG_READ_sCAL_SUPPORTED
4418N/A# ifdef PNG_FLOATING_POINT_SUPPORTED
4418N/A/* Utility used below - a simple accurate power of ten from an integral
4418N/A * exponent.
4418N/A */
4418N/Astatic double
4418N/Apng_pow10(int power)
4418N/A{
4418N/A int recip = 0;
4418N/A double d = 1;
4418N/A
4418N/A /* Handle negative exponent with a reciprocal at the end because
4418N/A * 10 is exact whereas .1 is inexact in base 2
4418N/A */
4418N/A if (power < 0)
4418N/A {
4418N/A if (power < DBL_MIN_10_EXP) return 0;
4418N/A recip = 1, power = -power;
4418N/A }
4418N/A
4418N/A if (power > 0)
4418N/A {
4418N/A /* Decompose power bitwise. */
4418N/A double mult = 10;
4418N/A do
4418N/A {
4418N/A if (power & 1) d *= mult;
4418N/A mult *= mult;
4418N/A power >>= 1;
4418N/A }
4418N/A while (power > 0);
4418N/A
4418N/A if (recip) d = 1/d;
4418N/A }
4418N/A /* else power is 0 and d is 1 */
4418N/A
4418N/A return d;
4418N/A}
4418N/A
4418N/A/* Function to format a floating point value in ASCII with a given
4418N/A * precision.
4418N/A */
4418N/Avoid /* PRIVATE */
4418N/Apng_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size,
4418N/A double fp, unsigned int precision)
4418N/A{
4418N/A /* We use standard functions from math.h, but not printf because
4418N/A * that would require stdio. The caller must supply a buffer of
4418N/A * sufficient size or we will png_error. The tests on size and
4418N/A * the space in ascii[] consumed are indicated below.
4418N/A */
4418N/A if (precision < 1)
4418N/A precision = DBL_DIG;
4418N/A
4418N/A /* Enforce the limit of the implementation precision too. */
4418N/A if (precision > DBL_DIG+1)
4418N/A precision = DBL_DIG+1;
4418N/A
4418N/A /* Basic sanity checks */
4418N/A if (size >= precision+5) /* See the requirements below. */
4418N/A {
4418N/A if (fp < 0)
4418N/A {
4418N/A fp = -fp;
4418N/A *ascii++ = 45; /* '-' PLUS 1 TOTAL 1 */
4418N/A --size;
4418N/A }
4418N/A
4418N/A if (fp >= DBL_MIN && fp <= DBL_MAX)
4418N/A {
4418N/A int exp_b10; /* A base 10 exponent */
4418N/A double base; /* 10^exp_b10 */
4418N/A
4418N/A /* First extract a base 10 exponent of the number,
4418N/A * the calculation below rounds down when converting
4418N/A * from base 2 to base 10 (multiply by log10(2) -
4418N/A * 0.3010, but 77/256 is 0.3008, so exp_b10 needs to
4418N/A * be increased. Note that the arithmetic shift
4418N/A * performs a floor() unlike C arithmetic - using a
4418N/A * C multiply would break the following for negative
4418N/A * exponents.
4418N/A */
4418N/A (void)frexp(fp, &exp_b10); /* exponent to base 2 */
4418N/A
4418N/A exp_b10 = (exp_b10 * 77) >> 8; /* <= exponent to base 10 */
4418N/A
4418N/A /* Avoid underflow here. */
4418N/A base = png_pow10(exp_b10); /* May underflow */
4418N/A
4418N/A while (base < DBL_MIN || base < fp)
4418N/A {
4418N/A /* And this may overflow. */
4418N/A double test = png_pow10(exp_b10+1);
4418N/A
4418N/A if (test <= DBL_MAX)
4418N/A ++exp_b10, base = test;
4418N/A
4418N/A else
4418N/A break;
4418N/A }
4418N/A
4418N/A /* Normalize fp and correct exp_b10, after this fp is in the
4418N/A * range [.1,1) and exp_b10 is both the exponent and the digit
4418N/A * *before* which the decimal point should be inserted
4418N/A * (starting with 0 for the first digit). Note that this
4418N/A * works even if 10^exp_b10 is out of range because of the
4418N/A * test on DBL_MAX above.
4418N/A */
4418N/A fp /= base;
4418N/A while (fp >= 1) fp /= 10, ++exp_b10;
4418N/A
4418N/A /* Because of the code above fp may, at this point, be
4418N/A * less than .1, this is ok because the code below can
4418N/A * handle the leading zeros this generates, so no attempt
4418N/A * is made to correct that here.
4418N/A */
4418N/A
4418N/A {
4418N/A int czero, clead, cdigits;
4418N/A char exponent[10];
4418N/A
4418N/A /* Allow up to two leading zeros - this will not lengthen
4418N/A * the number compared to using E-n.
4418N/A */
4418N/A if (exp_b10 < 0 && exp_b10 > -3) /* PLUS 3 TOTAL 4 */
4418N/A {
4418N/A czero = -exp_b10; /* PLUS 2 digits: TOTAL 3 */
4418N/A exp_b10 = 0; /* Dot added below before first output. */
4418N/A }
4418N/A else
4418N/A czero = 0; /* No zeros to add */
4418N/A
4418N/A /* Generate the digit list, stripping trailing zeros and
4418N/A * inserting a '.' before a digit if the exponent is 0.
4418N/A */
4418N/A clead = czero; /* Count of leading zeros */
4418N/A cdigits = 0; /* Count of digits in list. */
4418N/A
4418N/A do
4418N/A {
4418N/A double d;
4418N/A
4418N/A fp *= 10;
4418N/A /* Use modf here, not floor and subtract, so that
4418N/A * the separation is done in one step. At the end
4418N/A * of the loop don't break the number into parts so
4418N/A * that the final digit is rounded.
4418N/A */
4418N/A if (cdigits+czero-clead+1 < (int)precision)
4418N/A fp = modf(fp, &d);
4418N/A
4418N/A else
4418N/A {
4418N/A d = floor(fp + .5);
4418N/A
4418N/A if (d > 9)
4418N/A {
4418N/A /* Rounding up to 10, handle that here. */
4418N/A if (czero > 0)
4418N/A {
4418N/A --czero, d = 1;
4418N/A if (cdigits == 0) --clead;
4418N/A }
4418N/A else
4418N/A {
4418N/A while (cdigits > 0 && d > 9)
4418N/A {
4418N/A int ch = *--ascii;
4418N/A
4418N/A if (exp_b10 != (-1))
4418N/A ++exp_b10;
4418N/A
4418N/A else if (ch == 46)
4418N/A {
4418N/A ch = *--ascii, ++size;
4418N/A /* Advance exp_b10 to '1', so that the
4418N/A * decimal point happens after the
4418N/A * previous digit.
4418N/A */
4418N/A exp_b10 = 1;
4418N/A }
4418N/A
4418N/A --cdigits;
4418N/A d = ch - 47; /* I.e. 1+(ch-48) */
4418N/A }
4418N/A
4418N/A /* Did we reach the beginning? If so adjust the
4418N/A * exponent but take into account the leading
4418N/A * decimal point.
4418N/A */
4418N/A if (d > 9) /* cdigits == 0 */
4418N/A {
4418N/A if (exp_b10 == (-1))
4418N/A {
4418N/A /* Leading decimal point (plus zeros?), if
4418N/A * we lose the decimal point here it must
4418N/A * be reentered below.
4418N/A */
4418N/A int ch = *--ascii;
4418N/A
4418N/A if (ch == 46)
4418N/A ++size, exp_b10 = 1;
4418N/A
4418N/A /* Else lost a leading zero, so 'exp_b10' is
4418N/A * still ok at (-1)
4418N/A */
4418N/A }
4418N/A else
4418N/A ++exp_b10;
4418N/A
4418N/A /* In all cases we output a '1' */
4418N/A d = 1;
4418N/A }
4418N/A }
4418N/A }
4418N/A fp = 0; /* Guarantees termination below. */
4418N/A }
4418N/A
4418N/A if (d == 0)
4418N/A {
4418N/A ++czero;
4418N/A if (cdigits == 0) ++clead;
4418N/A }
4418N/A else
4418N/A {
4418N/A /* Included embedded zeros in the digit count. */
4418N/A cdigits += czero - clead;
4418N/A clead = 0;
4418N/A
4418N/A while (czero > 0)
4418N/A {
4418N/A /* exp_b10 == (-1) means we just output the decimal
4418N/A * place - after the DP don't adjust 'exp_b10' any
4418N/A * more!
4418N/A */
4418N/A if (exp_b10 != (-1))
4418N/A {
4418N/A if (exp_b10 == 0) *ascii++ = 46, --size;
4418N/A /* PLUS 1: TOTAL 4 */
4418N/A --exp_b10;
4418N/A }
4418N/A *ascii++ = 48, --czero;
4418N/A }
4418N/A
4418N/A if (exp_b10 != (-1))
4418N/A {
4418N/A if (exp_b10 == 0) *ascii++ = 46, --size; /* counted
4418N/A above */
4418N/A --exp_b10;
4418N/A }
4418N/A *ascii++ = (char)(48 + (int)d), ++cdigits;
4418N/A }
4418N/A }
4418N/A while (cdigits+czero-clead < (int)precision && fp > DBL_MIN);
4418N/A
4418N/A /* The total output count (max) is now 4+precision */
4418N/A
4418N/A /* Check for an exponent, if we don't need one we are
4418N/A * done and just need to terminate the string. At
4418N/A * this point exp_b10==(-1) is effectively if flag - it got
4418N/A * to '-1' because of the decrement after outputing
4418N/A * the decimal point above (the exponent required is
4418N/A * *not* -1!)
4418N/A */
4418N/A if (exp_b10 >= (-1) && exp_b10 <= 2)
4418N/A {
4418N/A /* The following only happens if we didn't output the
4418N/A * leading zeros above for negative exponent, so this
4418N/A * doest add to the digit requirement. Note that the
4418N/A * two zeros here can only be output if the two leading
4418N/A * zeros were *not* output, so this doesn't increase
4418N/A * the output count.
4418N/A */
4418N/A while (--exp_b10 >= 0) *ascii++ = 48;
4418N/A
4418N/A *ascii = 0;
4418N/A
4418N/A /* Total buffer requirement (including the '\0') is
4418N/A * 5+precision - see check at the start.
4418N/A */
4418N/A return;
4418N/A }
4418N/A
4418N/A /* Here if an exponent is required, adjust size for
4418N/A * the digits we output but did not count. The total
4418N/A * digit output here so far is at most 1+precision - no
4418N/A * decimal point and no leading or trailing zeros have
4418N/A * been output.
4418N/A */
4418N/A size -= cdigits;
4418N/A
4418N/A *ascii++ = 69, --size; /* 'E': PLUS 1 TOTAL 2+precision */
4418N/A if (exp_b10 < 0)
4418N/A {
4418N/A *ascii++ = 45, --size; /* '-': PLUS 1 TOTAL 3+precision */
4418N/A exp_b10 = -exp_b10;
4418N/A }
4418N/A
4418N/A cdigits = 0;
4418N/A
4418N/A while (exp_b10 > 0)
4418N/A {
4418N/A exponent[cdigits++] = (char)(48 + exp_b10 % 10);
4418N/A exp_b10 /= 10;
4418N/A }
4418N/A
4418N/A /* Need another size check here for the exponent digits, so
4418N/A * this need not be considered above.
4418N/A */
4418N/A if ((int)size > cdigits)
4418N/A {
4418N/A while (cdigits > 0) *ascii++ = exponent[--cdigits];
4418N/A
4418N/A *ascii = 0;
4418N/A
4418N/A return;
4418N/A }
4418N/A }
4418N/A }
4418N/A else if (!(fp >= DBL_MIN))
4418N/A {
4418N/A *ascii++ = 48; /* '0' */
4418N/A *ascii = 0;
4418N/A return;
4418N/A }
4418N/A else
4418N/A {
4418N/A *ascii++ = 105; /* 'i' */
4418N/A *ascii++ = 110; /* 'n' */
4418N/A *ascii++ = 102; /* 'f' */
4418N/A *ascii = 0;
4418N/A return;
4418N/A }
4418N/A }
4418N/A
4418N/A /* Here on buffer too small. */
4418N/A png_error(png_ptr, "ASCII conversion buffer too small");
4418N/A}
4418N/A
4418N/A# endif /* FLOATING_POINT */
4418N/A
4418N/A# ifdef PNG_FIXED_POINT_SUPPORTED
4418N/A/* Function to format a fixed point value in ASCII.
4418N/A */
4418N/Avoid /* PRIVATE */
4418N/Apng_ascii_from_fixed(png_structp png_ptr, png_charp ascii, png_size_t size,
4418N/A png_fixed_point fp)
4418N/A{
4418N/A /* Require space for 10 decimal digits, a decimal point, a minus sign and a
4418N/A * trailing \0, 13 characters:
4418N/A */
4418N/A if (size > 12)
4418N/A {
4418N/A png_uint_32 num;
4418N/A
4418N/A /* Avoid overflow here on the minimum integer. */
4418N/A if (fp < 0)
4418N/A *ascii++ = 45, --size, num = -fp;
4418N/A else
4418N/A num = fp;
4418N/A
4418N/A if (num <= 0x80000000U) /* else overflowed */
4418N/A {
4418N/A unsigned int ndigits = 0, first = 16 /* flag value */;
4418N/A char digits[10];
4418N/A
4418N/A while (num)
4418N/A {
4418N/A /* Split the low digit off num: */
4418N/A unsigned int tmp = num/10;
4418N/A num -= tmp*10;
4418N/A digits[ndigits++] = (char)(48 + num);
4418N/A /* Record the first non-zero digit, note that this is a number
4418N/A * starting at 1, it's not actually the array index.
4418N/A */
4418N/A if (first == 16 && num > 0)
4418N/A first = ndigits;
4418N/A num = tmp;
4418N/A }
4418N/A
4418N/A if (ndigits > 0)
4418N/A {
4418N/A while (ndigits > 5) *ascii++ = digits[--ndigits];
4418N/A /* The remaining digits are fractional digits, ndigits is '5' or
4418N/A * smaller at this point. It is certainly not zero. Check for a
4418N/A * non-zero fractional digit:
4418N/A */
4418N/A if (first <= 5)
4418N/A {
4418N/A unsigned int i;
4418N/A *ascii++ = 46; /* decimal point */
4418N/A /* ndigits may be <5 for small numbers, output leading zeros
4418N/A * then ndigits digits to first:
4418N/A */
4418N/A i = 5;
4418N/A while (ndigits < i) *ascii++ = 48, --i;
4418N/A while (ndigits >= first) *ascii++ = digits[--ndigits];
4418N/A /* Don't output the trailing zeros! */
4418N/A }
4418N/A }
4418N/A else
4418N/A *ascii++ = 48;
4418N/A
4418N/A /* And null terminate the string: */
4418N/A *ascii = 0;
4418N/A return;
4418N/A }
4418N/A }
4418N/A
4418N/A /* Here on buffer too small. */
4418N/A png_error(png_ptr, "ASCII conversion buffer too small");
4418N/A}
4418N/A# endif /* FIXED_POINT */
4418N/A#endif /* READ_SCAL */
4418N/A
4418N/A#if defined(PNG_FLOATING_POINT_SUPPORTED) && \
4418N/A !defined(PNG_FIXED_POINT_MACRO_SUPPORTED)
4418N/Apng_fixed_point
4418N/Apng_fixed(png_structp png_ptr, double fp, png_const_charp text)
4418N/A{
4418N/A double r = floor(100000 * fp + .5);
4418N/A
4418N/A if (r > 2147483647. || r < -2147483648.)
4418N/A png_fixed_error(png_ptr, text);
4418N/A
4418N/A return (png_fixed_point)r;
0N/A}
4418N/A#endif
4418N/A
4418N/A#if defined(PNG_READ_GAMMA_SUPPORTED) || \
4418N/A defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG__READ_pHYs_SUPPORTED)
4418N/A/* muldiv functions */
4418N/A/* This API takes signed arguments and rounds the result to the nearest
4418N/A * integer (or, for a fixed point number - the standard argument - to
4418N/A * the nearest .00001). Overflow and divide by zero are signalled in
4418N/A * the result, a boolean - true on success, false on overflow.
4418N/A */
4418N/Aint
4418N/Apng_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times,
4418N/A png_int_32 divisor)
4418N/A{
4418N/A /* Return a * times / divisor, rounded. */
4418N/A if (divisor != 0)
4418N/A {
4418N/A if (a == 0 || times == 0)
4418N/A {
4418N/A *res = 0;
4418N/A return 1;
4418N/A }
4418N/A else
4418N/A {
4418N/A#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
4418N/A double r = a;
4418N/A r *= times;
4418N/A r /= divisor;
4418N/A r = floor(r+.5);
4418N/A
4418N/A /* A png_fixed_point is a 32-bit integer. */
4418N/A if (r <= 2147483647. && r >= -2147483648.)
4418N/A {
4418N/A *res = (png_fixed_point)r;
4418N/A return 1;
4418N/A }
4418N/A#else
4418N/A int negative = 0;
4418N/A png_uint_32 A, T, D;
4418N/A png_uint_32 s16, s32, s00;
4418N/A
4418N/A if (a < 0)
4418N/A negative = 1, A = -a;
4418N/A else
4418N/A A = a;
4418N/A
4418N/A if (times < 0)
4418N/A negative = !negative, T = -times;
4418N/A else
4418N/A T = times;
4418N/A
4418N/A if (divisor < 0)
4418N/A negative = !negative, D = -divisor;
4418N/A else
4418N/A D = divisor;
4418N/A
4418N/A /* Following can't overflow because the arguments only
4418N/A * have 31 bits each, however the result may be 32 bits.
4418N/A */
4418N/A s16 = (A >> 16) * (T & 0xffff) +
4418N/A (A & 0xffff) * (T >> 16);
4418N/A /* Can't overflow because the a*times bit is only 30
4418N/A * bits at most.
4418N/A */
4418N/A s32 = (A >> 16) * (T >> 16) + (s16 >> 16);
4418N/A s00 = (A & 0xffff) * (T & 0xffff);
4418N/A
4418N/A s16 = (s16 & 0xffff) << 16;
4418N/A s00 += s16;
4418N/A
4418N/A if (s00 < s16)
4418N/A ++s32; /* carry */
4418N/A
4418N/A if (s32 < D) /* else overflow */
4418N/A {
4418N/A /* s32.s00 is now the 64-bit product, do a standard
4418N/A * division, we know that s32 < D, so the maximum
4418N/A * required shift is 31.
4418N/A */
4418N/A int bitshift = 32;
4418N/A png_fixed_point result = 0; /* NOTE: signed */
4418N/A
4418N/A while (--bitshift >= 0)
4418N/A {
4418N/A png_uint_32 d32, d00;
4418N/A
4418N/A if (bitshift > 0)
4418N/A d32 = D >> (32-bitshift), d00 = D << bitshift;
4418N/A
4418N/A else
4418N/A d32 = 0, d00 = D;
4418N/A
4418N/A if (s32 > d32)
4418N/A {
4418N/A if (s00 < d00) --s32; /* carry */
4418N/A s32 -= d32, s00 -= d00, result += 1<<bitshift;
4418N/A }
4418N/A
4418N/A else
4418N/A if (s32 == d32 && s00 >= d00)
4418N/A s32 = 0, s00 -= d00, result += 1<<bitshift;
4418N/A }
4418N/A
4418N/A /* Handle the rounding. */
4418N/A if (s00 >= (D >> 1))
4418N/A ++result;
4418N/A
4418N/A if (negative)
4418N/A result = -result;
4418N/A
4418N/A /* Check for overflow. */
4418N/A if ((negative && result <= 0) || (!negative && result >= 0))
4418N/A {
4418N/A *res = result;
4418N/A return 1;
4418N/A }
4418N/A }
4418N/A#endif
4418N/A }
4418N/A }
4418N/A
4418N/A return 0;
4418N/A}
4418N/A#endif /* READ_GAMMA || INCH_CONVERSIONS */
4418N/A
4418N/A#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_INCH_CONVERSIONS_SUPPORTED)
4418N/A/* The following is for when the caller doesn't much care about the
4418N/A * result.
4418N/A */
4418N/Apng_fixed_point
4418N/Apng_muldiv_warn(png_structp png_ptr, png_fixed_point a, png_int_32 times,
4418N/A png_int_32 divisor)
4418N/A{
4418N/A png_fixed_point result;
4418N/A
4418N/A if (png_muldiv(&result, a, times, divisor))
4418N/A return result;
4418N/A
4418N/A png_warning(png_ptr, "fixed point overflow ignored");
4418N/A return 0;
4418N/A}
4418N/A#endif
4418N/A
4418N/A#ifdef PNG_READ_GAMMA_SUPPORTED /* more fixed point functions for gammma */
4418N/A/* Calculate a reciprocal, return 0 on div-by-zero or overflow. */
4418N/Apng_fixed_point
4418N/Apng_reciprocal(png_fixed_point a)
4418N/A{
4418N/A#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
4418N/A double r = floor(1E10/a+.5);
4418N/A
4418N/A if (r <= 2147483647. && r >= -2147483648.)
4418N/A return (png_fixed_point)r;
4418N/A#else
4418N/A png_fixed_point res;
4418N/A
4418N/A if (png_muldiv(&res, 100000, 100000, a))
4418N/A return res;
4418N/A#endif
4418N/A
4418N/A return 0; /* error/overflow */
4418N/A}
4418N/A
4418N/A/* A local convenience routine. */
4418N/Astatic png_fixed_point
4418N/Apng_product2(png_fixed_point a, png_fixed_point b)
4418N/A{
4418N/A /* The required result is 1/a * 1/b; the following preserves accuracy. */
4418N/A#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
4418N/A double r = a * 1E-5;
4418N/A r *= b;
4418N/A r = floor(r+.5);
4418N/A
4418N/A if (r <= 2147483647. && r >= -2147483648.)
4418N/A return (png_fixed_point)r;
4418N/A#else
4418N/A png_fixed_point res;
4418N/A
4418N/A if (png_muldiv(&res, a, b, 100000))
4418N/A return res;
4418N/A#endif
4418N/A
4418N/A return 0; /* overflow */
4418N/A}
4418N/A
4418N/A/* The inverse of the above. */
4418N/Apng_fixed_point
4418N/Apng_reciprocal2(png_fixed_point a, png_fixed_point b)
4418N/A{
4418N/A /* The required result is 1/a * 1/b; the following preserves accuracy. */
4418N/A#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
4418N/A double r = 1E15/a;
4418N/A r /= b;
4418N/A r = floor(r+.5);
4418N/A
4418N/A if (r <= 2147483647. && r >= -2147483648.)
4418N/A return (png_fixed_point)r;
4418N/A#else
4418N/A /* This may overflow because the range of png_fixed_point isn't symmetric,
4418N/A * but this API is only used for the product of file and screen gamma so it
4418N/A * doesn't matter that the smallest number it can produce is 1/21474, not
4418N/A * 1/100000
4418N/A */
4418N/A png_fixed_point res = png_product2(a, b);
4418N/A
4418N/A if (res != 0)
4418N/A return png_reciprocal(res);
4418N/A#endif
4418N/A
4418N/A return 0; /* overflow */
4418N/A}
4418N/A#endif /* READ_GAMMA */
4418N/A
4418N/A#ifdef PNG_CHECK_cHRM_SUPPORTED
4418N/A/* Added at libpng version 1.2.34 (Dec 8, 2008) and 1.4.0 (Jan 2,
4418N/A * 2010: moved from pngset.c) */
4418N/A/*
4418N/A * Multiply two 32-bit numbers, V1 and V2, using 32-bit
4418N/A * arithmetic, to produce a 64-bit result in the HI/LO words.
4418N/A *
4418N/A * A B
4418N/A * x C D
4418N/A * ------
4418N/A * AD || BD
4418N/A * AC || CB || 0
4418N/A *
4418N/A * where A and B are the high and low 16-bit words of V1,
4418N/A * C and D are the 16-bit words of V2, AD is the product of
4418N/A * A and D, and X || Y is (X << 16) + Y.
4418N/A*/
4418N/A
4418N/Avoid /* PRIVATE */
4418N/Apng_64bit_product (long v1, long v2, unsigned long *hi_product,
4418N/A unsigned long *lo_product)
4418N/A{
4418N/A int a, b, c, d;
4418N/A long lo, hi, x, y;
4418N/A
4418N/A a = (v1 >> 16) & 0xffff;
4418N/A b = v1 & 0xffff;
4418N/A c = (v2 >> 16) & 0xffff;
4418N/A d = v2 & 0xffff;
4418N/A
4418N/A lo = b * d; /* BD */
4418N/A x = a * d + c * b; /* AD + CB */
4418N/A y = ((lo >> 16) & 0xffff) + x;
4418N/A
4418N/A lo = (lo & 0xffff) | ((y & 0xffff) << 16);
4418N/A hi = (y >> 16) & 0xffff;
4418N/A
4418N/A hi += a * c; /* AC */
4418N/A
4418N/A *hi_product = (unsigned long)hi;
4418N/A *lo_product = (unsigned long)lo;
4418N/A}
4418N/A#endif /* CHECK_cHRM */
4418N/A
4418N/A#ifdef PNG_READ_GAMMA_SUPPORTED /* gamma table code */
4418N/A#ifndef PNG_FLOATING_ARITHMETIC_SUPPORTED
4418N/A/* Fixed point gamma.
4418N/A *
4418N/A * To calculate gamma this code implements fast log() and exp() calls using only
4418N/A * fixed point arithmetic. This code has sufficient precision for either 8-bit
4418N/A * or 16-bit sample values.
4418N/A *
4418N/A * The tables used here were calculated using simple 'bc' programs, but C double
4418N/A * precision floating point arithmetic would work fine. The programs are given
4418N/A * at the head of each table.
4418N/A *
4418N/A * 8-bit log table
4418N/A * This is a table of -log(value/255)/log(2) for 'value' in the range 128 to
4418N/A * 255, so it's the base 2 logarithm of a normalized 8-bit floating point
4418N/A * mantissa. The numbers are 32-bit fractions.
4418N/A */
4418N/Astatic png_uint_32
4418N/Apng_8bit_l2[128] =
4418N/A{
4418N/A# if PNG_DO_BC
4418N/A for (i=128;i<256;++i) { .5 - l(i/255)/l(2)*65536*65536; }
4418N/A# endif
4418N/A 4270715492U, 4222494797U, 4174646467U, 4127164793U, 4080044201U, 4033279239U,
4418N/A 3986864580U, 3940795015U, 3895065449U, 3849670902U, 3804606499U, 3759867474U,
4418N/A 3715449162U, 3671346997U, 3627556511U, 3584073329U, 3540893168U, 3498011834U,
4418N/A 3455425220U, 3413129301U, 3371120137U, 3329393864U, 3287946700U, 3246774933U,
4418N/A 3205874930U, 3165243125U, 3124876025U, 3084770202U, 3044922296U, 3005329011U,
4418N/A 2965987113U, 2926893432U, 2888044853U, 2849438323U, 2811070844U, 2772939474U,
4418N/A 2735041326U, 2697373562U, 2659933400U, 2622718104U, 2585724991U, 2548951424U,
4418N/A 2512394810U, 2476052606U, 2439922311U, 2404001468U, 2368287663U, 2332778523U,
4418N/A 2297471715U, 2262364947U, 2227455964U, 2192742551U, 2158222529U, 2123893754U,
4418N/A 2089754119U, 2055801552U, 2022034013U, 1988449497U, 1955046031U, 1921821672U,
4418N/A 1888774511U, 1855902668U, 1823204291U, 1790677560U, 1758320682U, 1726131893U,
4418N/A 1694109454U, 1662251657U, 1630556815U, 1599023271U, 1567649391U, 1536433567U,
4418N/A 1505374214U, 1474469770U, 1443718700U, 1413119487U, 1382670639U, 1352370686U,
4418N/A 1322218179U, 1292211689U, 1262349810U, 1232631153U, 1203054352U, 1173618059U,
4418N/A 1144320946U, 1115161701U, 1086139034U, 1057251672U, 1028498358U, 999877854U,
4418N/A 971388940U, 943030410U, 914801076U, 886699767U, 858725327U, 830876614U,
4418N/A 803152505U, 775551890U, 748073672U, 720716771U, 693480120U, 666362667U,
4418N/A 639363374U, 612481215U, 585715177U, 559064263U, 532527486U, 506103872U,
4418N/A 479792461U, 453592303U, 427502463U, 401522014U, 375650043U, 349885648U,
4418N/A 324227938U, 298676034U, 273229066U, 247886176U, 222646516U, 197509248U,
4418N/A 172473545U, 147538590U, 122703574U, 97967701U, 73330182U, 48790236U,
4418N/A 24347096U, 0U
4418N/A#if 0
4418N/A /* The following are the values for 16-bit tables - these work fine for the
4418N/A * 8-bit conversions but produce very slightly larger errors in the 16-bit
4418N/A * log (about 1.2 as opposed to 0.7 absolute error in the final value). To
4418N/A * use these all the shifts below must be adjusted appropriately.
4418N/A */
4418N/A 65166, 64430, 63700, 62976, 62257, 61543, 60835, 60132, 59434, 58741, 58054,
4418N/A 57371, 56693, 56020, 55352, 54689, 54030, 53375, 52726, 52080, 51439, 50803,
4418N/A 50170, 49542, 48918, 48298, 47682, 47070, 46462, 45858, 45257, 44661, 44068,
4418N/A 43479, 42894, 42312, 41733, 41159, 40587, 40020, 39455, 38894, 38336, 37782,
4418N/A 37230, 36682, 36137, 35595, 35057, 34521, 33988, 33459, 32932, 32408, 31887,
4418N/A 31369, 30854, 30341, 29832, 29325, 28820, 28319, 27820, 27324, 26830, 26339,
4418N/A 25850, 25364, 24880, 24399, 23920, 23444, 22970, 22499, 22029, 21562, 21098,
4418N/A 20636, 20175, 19718, 19262, 18808, 18357, 17908, 17461, 17016, 16573, 16132,
4418N/A 15694, 15257, 14822, 14390, 13959, 13530, 13103, 12678, 12255, 11834, 11415,
4418N/A 10997, 10582, 10168, 9756, 9346, 8937, 8531, 8126, 7723, 7321, 6921, 6523,
4418N/A 6127, 5732, 5339, 4947, 4557, 4169, 3782, 3397, 3014, 2632, 2251, 1872, 1495,
4418N/A 1119, 744, 372
4418N/A#endif
4418N/A};
4418N/A
4418N/APNG_STATIC png_int_32
4418N/Apng_log8bit(unsigned int x)
4418N/A{
4418N/A unsigned int lg2 = 0;
4418N/A /* Each time 'x' is multiplied by 2, 1 must be subtracted off the final log,
4418N/A * because the log is actually negate that means adding 1. The final
4418N/A * returned value thus has the range 0 (for 255 input) to 7.994 (for 1
4418N/A * input), return 7.99998 for the overflow (log 0) case - so the result is
4418N/A * always at most 19 bits.
4418N/A */
4418N/A if ((x &= 0xff) == 0)
4418N/A return 0xffffffff;
4418N/A
4418N/A if ((x & 0xf0) == 0)
4418N/A lg2 = 4, x <<= 4;
4418N/A
4418N/A if ((x & 0xc0) == 0)
4418N/A lg2 += 2, x <<= 2;
4418N/A
4418N/A if ((x & 0x80) == 0)
4418N/A lg2 += 1, x <<= 1;
4418N/A
4418N/A /* result is at most 19 bits, so this cast is safe: */
4418N/A return (png_int_32)((lg2 << 16) + ((png_8bit_l2[x-128]+32768)>>16));
4418N/A}
4418N/A
4418N/A/* The above gives exact (to 16 binary places) log2 values for 8-bit images,
4418N/A * for 16-bit images we use the most significant 8 bits of the 16-bit value to
4418N/A * get an approximation then multiply the approximation by a correction factor
4418N/A * determined by the remaining up to 8 bits. This requires an additional step
4418N/A * in the 16-bit case.
4418N/A *
4418N/A * We want log2(value/65535), we have log2(v'/255), where:
4418N/A *
4418N/A * value = v' * 256 + v''
4418N/A * = v' * f
4418N/A *
4418N/A * So f is value/v', which is equal to (256+v''/v') since v' is in the range 128
4418N/A * to 255 and v'' is in the range 0 to 255 f will be in the range 256 to less
4418N/A * than 258. The final factor also needs to correct for the fact that our 8-bit
4418N/A * value is scaled by 255, whereas the 16-bit values must be scaled by 65535.
4418N/A *
4418N/A * This gives a final formula using a calculated value 'x' which is value/v' and
4418N/A * scaling by 65536 to match the above table:
4418N/A *
4418N/A * log2(x/257) * 65536
4418N/A *
4418N/A * Since these numbers are so close to '1' we can use simple linear
4418N/A * interpolation between the two end values 256/257 (result -368.61) and 258/257
4418N/A * (result 367.179). The values used below are scaled by a further 64 to give
4418N/A * 16-bit precision in the interpolation:
4418N/A *
4418N/A * Start (256): -23591
4418N/A * Zero (257): 0
4418N/A * End (258): 23499
4418N/A */
4418N/APNG_STATIC png_int_32
4418N/Apng_log16bit(png_uint_32 x)
4418N/A{
4418N/A unsigned int lg2 = 0;
4418N/A
4418N/A /* As above, but now the input has 16 bits. */
4418N/A if ((x &= 0xffff) == 0)
4418N/A return 0xffffffff;
4418N/A
4418N/A if ((x & 0xff00) == 0)
4418N/A lg2 = 8, x <<= 8;
4418N/A
4418N/A if ((x & 0xf000) == 0)
4418N/A lg2 += 4, x <<= 4;
4418N/A
4418N/A if ((x & 0xc000) == 0)
4418N/A lg2 += 2, x <<= 2;
4418N/A
4418N/A if ((x & 0x8000) == 0)
4418N/A lg2 += 1, x <<= 1;
4418N/A
4418N/A /* Calculate the base logarithm from the top 8 bits as a 28-bit fractional
4418N/A * value.
4418N/A */
4418N/A lg2 <<= 28;
4418N/A lg2 += (png_8bit_l2[(x>>8)-128]+8) >> 4;
4418N/A
4418N/A /* Now we need to interpolate the factor, this requires a division by the top
4418N/A * 8 bits. Do this with maximum precision.
4418N/A */
4418N/A x = ((x << 16) + (x >> 9)) / (x >> 8);
4418N/A
4418N/A /* Since we divided by the top 8 bits of 'x' there will be a '1' at 1<<24,
4418N/A * the value at 1<<16 (ignoring this) will be 0 or 1; this gives us exactly
4418N/A * 16 bits to interpolate to get the low bits of the result. Round the
4418N/A * answer. Note that the end point values are scaled by 64 to retain overall
4418N/A * precision and that 'lg2' is current scaled by an extra 12 bits, so adjust
4418N/A * the overall scaling by 6-12. Round at every step.
4418N/A */
4418N/A x -= 1U << 24;
4418N/A
4418N/A if (x <= 65536U) /* <= '257' */
4418N/A lg2 += ((23591U * (65536U-x)) + (1U << (16+6-12-1))) >> (16+6-12);
4418N/A
4418N/A else
4418N/A lg2 -= ((23499U * (x-65536U)) + (1U << (16+6-12-1))) >> (16+6-12);
4418N/A
4418N/A /* Safe, because the result can't have more than 20 bits: */
4418N/A return (png_int_32)((lg2 + 2048) >> 12);
4418N/A}
4418N/A
4418N/A/* The 'exp()' case must invert the above, taking a 20-bit fixed point
4418N/A * logarithmic value and returning a 16 or 8-bit number as appropriate. In
4418N/A * each case only the low 16 bits are relevant - the fraction - since the
4418N/A * integer bits (the top 4) simply determine a shift.
4418N/A *
4418N/A * The worst case is the 16-bit distinction between 65535 and 65534, this
4418N/A * requires perhaps spurious accuracty in the decoding of the logarithm to
4418N/A * distinguish log2(65535/65534.5) - 10^-5 or 17 bits. There is little chance
4418N/A * of getting this accuracy in practice.
4418N/A *
4418N/A * To deal with this the following exp() function works out the exponent of the
4418N/A * frational part of the logarithm by using an accurate 32-bit value from the
4418N/A * top four fractional bits then multiplying in the remaining bits.
4418N/A */
4418N/Astatic png_uint_32
4418N/Apng_32bit_exp[16] =
4418N/A{
4418N/A# if PNG_DO_BC
4418N/A for (i=0;i<16;++i) { .5 + e(-i/16*l(2))*2^32; }
4418N/A# endif
4418N/A /* NOTE: the first entry is deliberately set to the maximum 32-bit value. */
4418N/A 4294967295U, 4112874773U, 3938502376U, 3771522796U, 3611622603U, 3458501653U,
4418N/A 3311872529U, 3171459999U, 3037000500U, 2908241642U, 2784941738U, 2666869345U,
4418N/A 2553802834U, 2445529972U, 2341847524U, 2242560872U
4418N/A};
4418N/A
4418N/A/* Adjustment table; provided to explain the numbers in the code below. */
4418N/A#if PNG_DO_BC
4418N/Afor (i=11;i>=0;--i){ print i, " ", (1 - e(-(2^i)/65536*l(2))) * 2^(32-i), "\n"}
4418N/A 11 44937.64284865548751208448
4418N/A 10 45180.98734845585101160448
4418N/A 9 45303.31936980687359311872
4418N/A 8 45364.65110595323018870784
4418N/A 7 45395.35850361789624614912
4418N/A 6 45410.72259715102037508096
4418N/A 5 45418.40724413220722311168
4418N/A 4 45422.25021786898173001728
4418N/A 3 45424.17186732298419044352
4418N/A 2 45425.13273269940811464704
4418N/A 1 45425.61317555035558641664
4418N/A 0 45425.85339951654943850496
4418N/A#endif
4418N/A
4418N/APNG_STATIC png_uint_32
4418N/Apng_exp(png_fixed_point x)
4418N/A{
4418N/A if (x > 0 && x <= 0xfffff) /* Else overflow or zero (underflow) */
4418N/A {
4418N/A /* Obtain a 4-bit approximation */
4418N/A png_uint_32 e = png_32bit_exp[(x >> 12) & 0xf];
4418N/A
4418N/A /* Incorporate the low 12 bits - these decrease the returned value by
4418N/A * multiplying by a number less than 1 if the bit is set. The multiplier
4418N/A * is determined by the above table and the shift. Notice that the values
4418N/A * converge on 45426 and this is used to allow linear interpolation of the
4418N/A * low bits.
4418N/A */
4418N/A if (x & 0x800)
4418N/A e -= (((e >> 16) * 44938U) + 16U) >> 5;
4418N/A
4418N/A if (x & 0x400)
4418N/A e -= (((e >> 16) * 45181U) + 32U) >> 6;
4418N/A
4418N/A if (x & 0x200)
4418N/A e -= (((e >> 16) * 45303U) + 64U) >> 7;
4418N/A
4418N/A if (x & 0x100)
4418N/A e -= (((e >> 16) * 45365U) + 128U) >> 8;
4418N/A
4418N/A if (x & 0x080)
4418N/A e -= (((e >> 16) * 45395U) + 256U) >> 9;
4418N/A
4418N/A if (x & 0x040)
4418N/A e -= (((e >> 16) * 45410U) + 512U) >> 10;
4418N/A
4418N/A /* And handle the low 6 bits in a single block. */
4418N/A e -= (((e >> 16) * 355U * (x & 0x3fU)) + 256U) >> 9;
4418N/A
4418N/A /* Handle the upper bits of x. */
4418N/A e >>= x >> 16;
4418N/A return e;
4418N/A }
4418N/A
4418N/A /* Check for overflow */
4418N/A if (x <= 0)
4418N/A return png_32bit_exp[0];
4418N/A
4418N/A /* Else underflow */
4418N/A return 0;
4418N/A}
4418N/A
4418N/APNG_STATIC png_byte
4418N/Apng_exp8bit(png_fixed_point lg2)
4418N/A{
4418N/A /* Get a 32-bit value: */
4418N/A png_uint_32 x = png_exp(lg2);
4418N/A
4418N/A /* Convert the 32-bit value to 0..255 by multiplying by 256-1, note that the
4418N/A * second, rounding, step can't overflow because of the first, subtraction,
4418N/A * step.
4418N/A */
4418N/A x -= x >> 8;
4418N/A return (png_byte)((x + 0x7fffffU) >> 24);
4418N/A}
4418N/A
4418N/APNG_STATIC png_uint_16
4418N/Apng_exp16bit(png_fixed_point lg2)
4418N/A{
4418N/A /* Get a 32-bit value: */
4418N/A png_uint_32 x = png_exp(lg2);
4418N/A
4418N/A /* Convert the 32-bit value to 0..65535 by multiplying by 65536-1: */
4418N/A x -= x >> 16;
4418N/A return (png_uint_16)((x + 32767U) >> 16);
4418N/A}
4418N/A#endif /* FLOATING_ARITHMETIC */
4418N/A
4418N/Apng_byte
4418N/Apng_gamma_8bit_correct(unsigned int value, png_fixed_point gamma_val)
4418N/A{
4418N/A if (value > 0 && value < 255)
4418N/A {
4418N/A# ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
4418N/A double r = floor(255*pow(value/255.,gamma_val*.00001)+.5);
4418N/A return (png_byte)r;
4418N/A# else
4418N/A png_int_32 lg2 = png_log8bit(value);
4418N/A png_fixed_point res;
4418N/A
4418N/A if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1))
4418N/A return png_exp8bit(res);
4418N/A
4418N/A /* Overflow. */
4418N/A value = 0;
4418N/A# endif
4418N/A }
4418N/A
4418N/A return (png_byte)value;
4418N/A}
4418N/A
4418N/Apng_uint_16
4418N/Apng_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val)
4418N/A{
4418N/A if (value > 0 && value < 65535)
4418N/A {
4418N/A# ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
4418N/A double r = floor(65535*pow(value/65535.,gamma_val*.00001)+.5);
4418N/A return (png_uint_16)r;
4418N/A# else
4418N/A png_int_32 lg2 = png_log16bit(value);
4418N/A png_fixed_point res;
4418N/A
4418N/A if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1))
4418N/A return png_exp16bit(res);
4418N/A
4418N/A /* Overflow. */
4418N/A value = 0;
4418N/A# endif
4418N/A }
4418N/A
4418N/A return (png_uint_16)value;
4418N/A}
4418N/A
4418N/A/* This does the right thing based on the bit_depth field of the
4418N/A * png_struct, interpreting values as 8-bit or 16-bit. While the result
4418N/A * is nominally a 16-bit value if bit depth is 8 then the result is
4418N/A * 8-bit (as are the arguments.)
4418N/A */
4418N/Apng_uint_16 /* PRIVATE */
4418N/Apng_gamma_correct(png_structp png_ptr, unsigned int value,
4418N/A png_fixed_point gamma_val)
4418N/A{
4418N/A if (png_ptr->bit_depth == 8)
4418N/A return png_gamma_8bit_correct(value, gamma_val);
4418N/A
4418N/A else
4418N/A return png_gamma_16bit_correct(value, gamma_val);
4418N/A}
4418N/A
4418N/A/* This is the shared test on whether a gamma value is 'significant' - whether
4418N/A * it is worth doing gamma correction.
4418N/A */
4418N/Aint /* PRIVATE */
4418N/Apng_gamma_significant(png_fixed_point gamma_val)
4418N/A{
4418N/A return gamma_val < PNG_FP_1 - PNG_GAMMA_THRESHOLD_FIXED ||
4418N/A gamma_val > PNG_FP_1 + PNG_GAMMA_THRESHOLD_FIXED;
4418N/A}
4418N/A
4418N/A/* Internal function to build a single 16-bit table - the table consists of
4418N/A * 'num' 256 entry subtables, where 'num' is determined by 'shift' - the amount
4418N/A * to shift the input values right (or 16-number_of_signifiant_bits).
4418N/A *
4418N/A * The caller is responsible for ensuring that the table gets cleaned up on
4418N/A * png_error (i.e. if one of the mallocs below fails) - i.e. the *table argument
4418N/A * should be somewhere that will be cleaned.
4418N/A */
4418N/Astatic void
4418N/Apng_build_16bit_table(png_structp png_ptr, png_uint_16pp *ptable,
4418N/A PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val)
4418N/A{
4418N/A /* Various values derived from 'shift': */
4418N/A PNG_CONST unsigned int num = 1U << (8U - shift);
4418N/A PNG_CONST unsigned int max = (1U << (16U - shift))-1U;
4418N/A PNG_CONST unsigned int max_by_2 = 1U << (15U-shift);
4418N/A unsigned int i;
4418N/A
4418N/A png_uint_16pp table = *ptable =
4418N/A (png_uint_16pp)png_calloc(png_ptr, num * png_sizeof(png_uint_16p));
4418N/A
4418N/A for (i = 0; i < num; i++)
4418N/A {
4418N/A png_uint_16p sub_table = table[i] =
4418N/A (png_uint_16p)png_malloc(png_ptr, 256 * png_sizeof(png_uint_16));
4418N/A
4418N/A /* The 'threshold' test is repeated here because it can arise for one of
4418N/A * the 16-bit tables even if the others don't hit it.
4418N/A */
4418N/A if (png_gamma_significant(gamma_val))
4418N/A {
4418N/A /* The old code would overflow at the end and this would cause the
4418N/A * 'pow' function to return a result >1, resulting in an
4418N/A * arithmetic error. This code follows the spec exactly; ig is
4418N/A * the recovered input sample, it always has 8-16 bits.
4418N/A *
4418N/A * We want input * 65535/max, rounded, the arithmetic fits in 32
4418N/A * bits (unsigned) so long as max <= 32767.
4418N/A */
4418N/A unsigned int j;
4418N/A for (j = 0; j < 256; j++)
4418N/A {
4418N/A png_uint_32 ig = (j << (8-shift)) + i;
4418N/A# ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
4418N/A /* Inline the 'max' scaling operation: */
4418N/A double d = floor(65535*pow(ig/(double)max, gamma_val*.00001)+.5);
4418N/A sub_table[j] = (png_uint_16)d;
4418N/A# else
4418N/A if (shift)
4418N/A ig = (ig * 65535U + max_by_2)/max;
4418N/A
4418N/A sub_table[j] = png_gamma_16bit_correct(ig, gamma_val);
4418N/A# endif
4418N/A }
4418N/A }
4418N/A else
4418N/A {
4418N/A /* We must still build a table, but do it the fast way. */
4418N/A unsigned int j;
4418N/A
4418N/A for (j = 0; j < 256; j++)
4418N/A {
4418N/A png_uint_32 ig = (j << (8-shift)) + i;
4418N/A
4418N/A if (shift)
4418N/A ig = (ig * 65535U + max_by_2)/max;
4418N/A
4418N/A sub_table[j] = (png_uint_16)ig;
4418N/A }
4418N/A }
4418N/A }
4418N/A}
4418N/A
4418N/A/* NOTE: this function expects the *inverse* of the overall gamma transformation
4418N/A * required.
4418N/A */
4418N/Astatic void
4418N/Apng_build_16to8_table(png_structp png_ptr, png_uint_16pp *ptable,
4418N/A PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val)
4418N/A{
4418N/A PNG_CONST unsigned int num = 1U << (8U - shift);
4418N/A PNG_CONST unsigned int max = (1U << (16U - shift))-1U;
4418N/A unsigned int i;
4418N/A png_uint_32 last;
4418N/A
4418N/A png_uint_16pp table = *ptable =
4418N/A (png_uint_16pp)png_calloc(png_ptr, num * png_sizeof(png_uint_16p));
4418N/A
4418N/A /* 'num' is the number of tables and also the number of low bits of low
4418N/A * bits of the input 16-bit value used to select a table. Each table is
4418N/A * itself index by the high 8 bits of the value.
4418N/A */
4418N/A for (i = 0; i < num; i++)
4418N/A table[i] = (png_uint_16p)png_malloc(png_ptr,
4418N/A 256 * png_sizeof(png_uint_16));
4418N/A
4418N/A /* 'gamma_val' is set to the reciprocal of the value calculated above, so
4418N/A * pow(out,g) is an *input* value. 'last' is the last input value set.
4418N/A *
4418N/A * In the loop 'i' is used to find output values. Since the output is
4418N/A * 8-bit there are only 256 possible values. The tables are set up to
4418N/A * select the closest possible output value for each input by finding
4418N/A * the input value at the boundary between each pair of output values
4418N/A * and filling the table up to that boundary with the lower output
4418N/A * value.
4418N/A *
4418N/A * The boundary values are 0.5,1.5..253.5,254.5. Since these are 9-bit
4418N/A * values the code below uses a 16-bit value in i; the values start at
4418N/A * 128.5 (for 0.5) and step by 257, for a total of 254 values (the last
4418N/A * entries are filled with 255). Start i at 128 and fill all 'last'
4418N/A * table entries <= 'max'
4418N/A */
4418N/A last = 0;
4418N/A for (i = 0; i < 255; ++i) /* 8-bit output value */
4418N/A {
4418N/A /* Find the corresponding maximum input value */
4418N/A png_uint_16 out = (png_uint_16)(i * 257U); /* 16-bit output value */
4418N/A
4418N/A /* Find the boundary value in 16 bits: */
4418N/A png_uint_32 bound = png_gamma_16bit_correct(out+128U, gamma_val);
4418N/A
4418N/A /* Adjust (round) to (16-shift) bits: */
4418N/A bound = (bound * max + 32768U)/65535U + 1U;
4418N/A
4418N/A while (last < bound)
4418N/A {
4418N/A table[last & (0xffU >> shift)][last >> (8U - shift)] = out;
4418N/A last++;
4418N/A }
4418N/A }
4418N/A
4418N/A /* And fill in the final entries. */
4418N/A while (last < (num << 8))
4418N/A {
4418N/A table[last & (0xff >> shift)][last >> (8U - shift)] = 65535U;
4418N/A last++;
4418N/A }
4418N/A}
4418N/A
4418N/A/* Build a single 8-bit table: same as the 16-bit case but much simpler (and
4418N/A * typically much faster). Note that libpng currently does no sBIT processing
4418N/A * (apparently contrary to the spec) so a 256 entry table is always generated.
4418N/A */
4418N/Astatic void
4418N/Apng_build_8bit_table(png_structp png_ptr, png_bytepp ptable,
4418N/A PNG_CONST png_fixed_point gamma_val)
4418N/A{
4418N/A unsigned int i;
4418N/A png_bytep table = *ptable = (png_bytep)png_malloc(png_ptr, 256);
4418N/A
4418N/A if (png_gamma_significant(gamma_val)) for (i=0; i<256; i++)
4418N/A table[i] = png_gamma_8bit_correct(i, gamma_val);
4418N/A
4418N/A else for (i=0; i<256; ++i)
4418N/A table[i] = (png_byte)i;
4418N/A}
4418N/A
4418N/A/* We build the 8- or 16-bit gamma tables here. Note that for 16-bit
4418N/A * tables, we don't make a full table if we are reducing to 8-bit in
4418N/A * the future. Note also how the gamma_16 tables are segmented so that
4418N/A * we don't need to allocate > 64K chunks for a full 16-bit table.
4418N/A */
4418N/Avoid /* PRIVATE */
4418N/Apng_build_gamma_table(png_structp png_ptr, int bit_depth)
4418N/A{
4418N/A png_debug(1, "in png_build_gamma_table");
4418N/A
4418N/A if (bit_depth <= 8)
4418N/A {
4418N/A png_build_8bit_table(png_ptr, &png_ptr->gamma_table,
4418N/A png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->gamma,
4418N/A png_ptr->screen_gamma) : PNG_FP_1);
4418N/A
4418N/A#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4418N/A defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
4418N/A defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4418N/A if (png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY))
4418N/A {
4418N/A png_build_8bit_table(png_ptr, &png_ptr->gamma_to_1,
4418N/A png_reciprocal(png_ptr->gamma));
4418N/A
4418N/A png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1,
4418N/A png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) :
4418N/A png_ptr->gamma/* Probably doing rgb_to_gray */);
4418N/A }
4418N/A#endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
4418N/A }
4418N/A else
4418N/A {
4418N/A png_byte shift, sig_bit;
4418N/A
4418N/A if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
4418N/A {
4418N/A sig_bit = png_ptr->sig_bit.red;
4418N/A
4418N/A if (png_ptr->sig_bit.green > sig_bit)
4418N/A sig_bit = png_ptr->sig_bit.green;
4418N/A
4418N/A if (png_ptr->sig_bit.blue > sig_bit)
4418N/A sig_bit = png_ptr->sig_bit.blue;
4418N/A }
4418N/A else
4418N/A sig_bit = png_ptr->sig_bit.gray;
4418N/A
4418N/A /* 16-bit gamma code uses this equation:
4418N/A *
4418N/A * ov = table[(iv & 0xff) >> gamma_shift][iv >> 8]
4418N/A *
4418N/A * Where 'iv' is the input color value and 'ov' is the output value -
4418N/A * pow(iv, gamma).
4418N/A *
4418N/A * Thus the gamma table consists of up to 256 256 entry tables. The table
4418N/A * is selected by the (8-gamma_shift) most significant of the low 8 bits of
4418N/A * the color value then indexed by the upper 8 bits:
4418N/A *
4418N/A * table[low bits][high 8 bits]
4418N/A *
4418N/A * So the table 'n' corresponds to all those 'iv' of:
4418N/A *
4418N/A * <all high 8-bit values><n << gamma_shift>..<(n+1 << gamma_shift)-1>
4418N/A *
4418N/A */
4418N/A if (sig_bit > 0 && sig_bit < 16U)
4418N/A shift = (png_byte)(16U - sig_bit); /* shift == insignificant bits */
4418N/A
4418N/A else
4418N/A shift = 0; /* keep all 16 bits */
4418N/A
4418N/A if (png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8))
4418N/A {
4418N/A /* PNG_MAX_GAMMA_8 is the number of bits to keep - effectively
4418N/A * the significant bits in the *input* when the output will
4418N/A * eventually be 8 bits. By default it is 11.
4418N/A */
4418N/A if (shift < (16U - PNG_MAX_GAMMA_8))
4418N/A shift = (16U - PNG_MAX_GAMMA_8);
4418N/A }
4418N/A
4418N/A if (shift > 8U)
4418N/A shift = 8U; /* Guarantees at least one table! */
4418N/A
4418N/A png_ptr->gamma_shift = shift;
4418N/A
4418N/A#ifdef PNG_16BIT_SUPPORTED
4418N/A /* NOTE: prior to 1.5.4 this test used to include PNG_BACKGROUND (now
4418N/A * PNG_COMPOSE). This effectively smashed the background calculation for
4418N/A * 16-bit output because the 8-bit table assumes the result will be reduced
4418N/A * to 8 bits.
4418N/A */
4418N/A if (png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8))
4418N/A#endif
4418N/A png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift,
4418N/A png_ptr->screen_gamma > 0 ? png_product2(png_ptr->gamma,
4418N/A png_ptr->screen_gamma) : PNG_FP_1);
4418N/A
4418N/A#ifdef PNG_16BIT_SUPPORTED
4418N/A else
4418N/A png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift,
4418N/A png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->gamma,
4418N/A png_ptr->screen_gamma) : PNG_FP_1);
4418N/A#endif
4418N/A
4418N/A#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4418N/A defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
4418N/A defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4418N/A if (png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY))
4418N/A {
4418N/A png_build_16bit_table(png_ptr, &png_ptr->gamma_16_to_1, shift,
4418N/A png_reciprocal(png_ptr->gamma));
4418N/A
4418N/A /* Notice that the '16 from 1' table should be full precision, however
4418N/A * the lookup on this table still uses gamma_shift, so it can't be.
4418N/A * TODO: fix this.
4418N/A */
4418N/A png_build_16bit_table(png_ptr, &png_ptr->gamma_16_from_1, shift,
4418N/A png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) :
4418N/A png_ptr->gamma/* Probably doing rgb_to_gray */);
4418N/A }
4418N/A#endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
4418N/A }
4418N/A}
4418N/A#endif /* READ_GAMMA */
0N/A#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */