/* png.c - location for general purpose libpng functions * * Copyright (c) 2018-2024 Cosmin Truta * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson * Copyright (c) 1996-1997 Andreas Dilger * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h */ #include "pngpriv.h" /* Generate a compiler error if there is an old png.h in the search path. */ Your_png_h_is_not_version_1_6_43; /* Tells libpng that we have already handled the first "num_bytes" bytes * of the PNG file signature. If the PNG data is embedded into another * stream we can set num_bytes = 8 so that libpng will not attempt to read * or write any of the magic bytes before it starts on the IHDR. */ #ifdef PNG_READ_SUPPORTED void PNGAPI png_set_sig_bytes(png_structrp png_ptr, int num_bytes) { … } /* Checks whether the supplied bytes match the PNG signature. We allow * checking less than the full 8-byte signature so that those apps that * already read the first few bytes of a file to determine the file type * can simply check the remaining bytes for extra assurance. Returns * an integer less than, equal to, or greater than zero if sig is found, * respectively, to be less than, to match, or be greater than the correct * PNG signature (this is the same behavior as strcmp, memcmp, etc). */ int PNGAPI png_sig_cmp(png_const_bytep sig, size_t start, size_t num_to_check) { … } #endif /* READ */ #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) /* Function to allocate memory for zlib */ PNG_FUNCTION(voidpf /* PRIVATE */, png_zalloc,(voidpf png_ptr, uInt items, uInt size),PNG_ALLOCATED) { … } /* Function to free memory for zlib */ void /* PRIVATE */ png_zfree(voidpf png_ptr, voidpf ptr) { … } /* Reset the CRC variable to 32 bits of 1's. Care must be taken * in case CRC is > 32 bits to leave the top bits 0. */ void /* PRIVATE */ png_reset_crc(png_structrp png_ptr) { … } /* Calculate the CRC over a section of data. We can only pass as * much data to this routine as the largest single buffer size. We * also check that this data will actually be used before going to the * trouble of calculating it. */ void /* PRIVATE */ png_calculate_crc(png_structrp png_ptr, png_const_bytep ptr, size_t length) { … } /* Check a user supplied version number, called from both read and write * functions that create a png_struct. */ int png_user_version_check(png_structrp png_ptr, png_const_charp user_png_ver) { … } /* Generic function to create a png_struct for either read or write - this * contains the common initialization. */ PNG_FUNCTION(png_structp /* PRIVATE */, png_create_png_struct,(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) { … } /* Allocate the memory for an info_struct for the application. */ PNG_FUNCTION(png_infop,PNGAPI png_create_info_struct,(png_const_structrp png_ptr),PNG_ALLOCATED) { … } /* This function frees the memory associated with a single info struct. * Normally, one would use either png_destroy_read_struct() or * png_destroy_write_struct() to free an info struct, but this may be * useful for some applications. From libpng 1.6.0 this function is also used * internally to implement the png_info release part of the 'struct' destroy * APIs. This ensures that all possible approaches free the same data (all of * it). */ void PNGAPI png_destroy_info_struct(png_const_structrp png_ptr, png_infopp info_ptr_ptr) { … } /* Initialize the info structure. This is now an internal function (0.89) * and applications using it are urged to use png_create_info_struct() * instead. Use deprecated in 1.6.0, internal use removed (used internally it * is just a memset). * * NOTE: it is almost inconceivable that this API is used because it bypasses * the user-memory mechanism and the user error handling/warning mechanisms in * those cases where it does anything other than a memset. */ PNG_FUNCTION(void,PNGAPI png_info_init_3,(png_infopp ptr_ptr, size_t png_info_struct_size), PNG_DEPRECATED) { … } void PNGAPI png_data_freer(png_const_structrp png_ptr, png_inforp info_ptr, int freer, png_uint_32 mask) { … } void PNGAPI png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask, int num) { … } #endif /* READ || WRITE */ /* This function returns a pointer to the io_ptr associated with the user * functions. The application should free any memory associated with this * pointer before png_write_destroy() or png_read_destroy() are called. */ png_voidp PNGAPI png_get_io_ptr(png_const_structrp png_ptr) { … } #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) # ifdef PNG_STDIO_SUPPORTED /* Initialize the default input/output functions for the PNG file. If you * use your own read or write routines, you can call either png_set_read_fn() * or png_set_write_fn() instead of png_init_io(). If you have defined * PNG_NO_STDIO or otherwise disabled PNG_STDIO_SUPPORTED, you must use a * function of your own because "FILE *" isn't necessarily available. */ void PNGAPI png_init_io(png_structrp png_ptr, png_FILE_p fp) { … } # endif # ifdef PNG_SAVE_INT_32_SUPPORTED /* PNG signed integers are saved in 32-bit 2's complement format. ANSI C-90 * defines a cast of a signed integer to an unsigned integer either to preserve * the value, if it is positive, or to calculate: * * (UNSIGNED_MAX+1) + integer * * Where UNSIGNED_MAX is the appropriate maximum unsigned value, so when the * negative integral value is added the result will be an unsigned value * corresponding to the 2's complement representation. */ void PNGAPI png_save_int_32(png_bytep buf, png_int_32 i) { … } # endif # ifdef PNG_TIME_RFC1123_SUPPORTED /* Convert the supplied time into an RFC 1123 string suitable for use in * a "Creation Time" or other text-based time string. */ int PNGAPI png_convert_to_rfc1123_buffer(char out[29], png_const_timep ptime) { … } # if PNG_LIBPNG_VER < 10700 /* To do: remove the following from libpng-1.7 */ /* Original API that uses a private buffer in png_struct. * Deprecated because it causes png_struct to carry a spurious temporary * buffer (png_struct::time_buffer), better to have the caller pass this in. */ png_const_charp PNGAPI png_convert_to_rfc1123(png_structrp png_ptr, png_const_timep ptime) { … } # endif /* LIBPNG_VER < 10700 */ # endif /* TIME_RFC1123 */ #endif /* READ || WRITE */ png_const_charp PNGAPI png_get_copyright(png_const_structrp png_ptr) { … } /* The following return the library version as a short string in the * format 1.0.0 through 99.99.99zz. To get the version of *.h files * used with your application, print out PNG_LIBPNG_VER_STRING, which * is defined in png.h. * Note: now there is no difference between png_get_libpng_ver() and * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard, * it is guaranteed that png.c uses the correct version of png.h. */ png_const_charp PNGAPI png_get_libpng_ver(png_const_structrp png_ptr) { … } png_const_charp PNGAPI png_get_header_ver(png_const_structrp png_ptr) { … } png_const_charp PNGAPI png_get_header_version(png_const_structrp png_ptr) { … } #ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED /* NOTE: this routine is not used internally! */ /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth * large of png_color. This lets grayscale images be treated as * paletted. Most useful for gamma correction and simplification * of code. This API is not used internally. */ void PNGAPI png_build_grayscale_palette(int bit_depth, png_colorp palette) { … } #endif #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED int PNGAPI png_handle_as_unknown(png_const_structrp png_ptr, png_const_bytep chunk_name) { … } #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) ||\ defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED) int /* PRIVATE */ png_chunk_unknown_handling(png_const_structrp png_ptr, png_uint_32 chunk_name) { … } #endif /* READ_UNKNOWN_CHUNKS || HANDLE_AS_UNKNOWN */ #endif /* SET_UNKNOWN_CHUNKS */ #ifdef PNG_READ_SUPPORTED /* This function, added to libpng-1.0.6g, is untested. */ int PNGAPI png_reset_zstream(png_structrp png_ptr) { … } #endif /* READ */ /* This function was added to libpng-1.0.7 */ png_uint_32 PNGAPI png_access_version_number(void) { … } #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) /* Ensure that png_ptr->zstream.msg holds some appropriate error message string. * If it doesn't 'ret' is used to set it to something appropriate, even in cases * like Z_OK or Z_STREAM_END where the error code is apparently a success code. */ void /* PRIVATE */ png_zstream_error(png_structrp png_ptr, int ret) { … } /* png_convert_size: a PNGAPI but no longer in png.h, so deleted * at libpng 1.5.5! */ /* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */ #ifdef PNG_GAMMA_SUPPORTED /* always set if COLORSPACE */ static int png_colorspace_check_gamma(png_const_structrp png_ptr, png_colorspacerp colorspace, png_fixed_point gAMA, int from) /* This is called to check a new gamma value against an existing one. The * routine returns false if the new gamma value should not be written. * * 'from' says where the new gamma value comes from: * * 0: the new gamma value is the libpng estimate for an ICC profile * 1: the new gamma value comes from a gAMA chunk * 2: the new gamma value comes from an sRGB chunk */ { … } void /* PRIVATE */ png_colorspace_set_gamma(png_const_structrp png_ptr, png_colorspacerp colorspace, png_fixed_point gAMA) { … } void /* PRIVATE */ png_colorspace_sync_info(png_const_structrp png_ptr, png_inforp info_ptr) { … } #ifdef PNG_READ_SUPPORTED void /* PRIVATE */ png_colorspace_sync(png_const_structrp png_ptr, png_inforp info_ptr) { … } #endif #endif /* GAMMA */ #ifdef PNG_COLORSPACE_SUPPORTED /* Added at libpng-1.5.5 to support read and write of true CIEXYZ values for * cHRM, as opposed to using chromaticities. These internal APIs return * non-zero on a parameter error. The X, Y and Z values are required to be * positive and less than 1.0. */ static int png_xy_from_XYZ(png_xy *xy, const png_XYZ *XYZ) { … } static int png_XYZ_from_xy(png_XYZ *XYZ, const png_xy *xy) { … } static int png_XYZ_normalize(png_XYZ *XYZ) { … } static int png_colorspace_endpoints_match(const png_xy *xy1, const png_xy *xy2, int delta) { … } /* Added in libpng-1.6.0, a different check for the validity of a set of cHRM * chunk chromaticities. Earlier checks used to simply look for the overflow * condition (where the determinant of the matrix to solve for XYZ ends up zero * because the chromaticity values are not all distinct.) Despite this it is * theoretically possible to produce chromaticities that are apparently valid * but that rapidly degrade to invalid, potentially crashing, sets because of * arithmetic inaccuracies when calculations are performed on them. The new * check is to round-trip xy -> XYZ -> xy and then check that the result is * within a small percentage of the original. */ static int png_colorspace_check_xy(png_XYZ *XYZ, const png_xy *xy) { … } /* This is the check going the other way. The XYZ is modified to normalize it * (another side-effect) and the xy chromaticities are returned. */ static int png_colorspace_check_XYZ(png_xy *xy, png_XYZ *XYZ) { … } /* Used to check for an endpoint match against sRGB */ static const png_xy sRGB_xy = …/* From ITU-R BT.709-3 */ { … }; static int png_colorspace_set_xy_and_XYZ(png_const_structrp png_ptr, png_colorspacerp colorspace, const png_xy *xy, const png_XYZ *XYZ, int preferred) { … } int /* PRIVATE */ png_colorspace_set_chromaticities(png_const_structrp png_ptr, png_colorspacerp colorspace, const png_xy *xy, int preferred) { … } int /* PRIVATE */ png_colorspace_set_endpoints(png_const_structrp png_ptr, png_colorspacerp colorspace, const png_XYZ *XYZ_in, int preferred) { … } #if defined(PNG_sRGB_SUPPORTED) || defined(PNG_iCCP_SUPPORTED) /* Error message generation */ static char png_icc_tag_char(png_uint_32 byte) { … } static void png_icc_tag_name(char *name, png_uint_32 tag) { … } static int is_ICC_signature_char(png_alloc_size_t it) { … } static int is_ICC_signature(png_alloc_size_t it) { … } static int png_icc_profile_error(png_const_structrp png_ptr, png_colorspacerp colorspace, png_const_charp name, png_alloc_size_t value, png_const_charp reason) { … } #endif /* sRGB || iCCP */ #ifdef PNG_sRGB_SUPPORTED int /* PRIVATE */ png_colorspace_set_sRGB(png_const_structrp png_ptr, png_colorspacerp colorspace, int intent) { … } #endif /* sRGB */ #ifdef PNG_iCCP_SUPPORTED /* Encoded value of D50 as an ICC XYZNumber. From the ICC 2010 spec the value * is XYZ(0.9642,1.0,0.8249), which scales to: * * (63189.8112, 65536, 54060.6464) */ static const png_byte D50_nCIEXYZ[12] = …; static int /* bool */ icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace, png_const_charp name, png_uint_32 profile_length) { … } #ifdef PNG_READ_iCCP_SUPPORTED int /* PRIVATE */ png_icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace, png_const_charp name, png_uint_32 profile_length) { … } #endif /* READ_iCCP */ int /* PRIVATE */ png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace, png_const_charp name, png_uint_32 profile_length, png_const_bytep profile/* first 132 bytes only */, int color_type) { … } int /* PRIVATE */ png_icc_check_tag_table(png_const_structrp png_ptr, png_colorspacerp colorspace, png_const_charp name, png_uint_32 profile_length, png_const_bytep profile /* header plus whole tag table */) { … } #ifdef PNG_sRGB_SUPPORTED #if PNG_sRGB_PROFILE_CHECKS >= 0 /* Information about the known ICC sRGB profiles */ static const struct { … } png_sRGB_checks[] = …; static int png_compare_ICC_profile_with_sRGB(png_const_structrp png_ptr, png_const_bytep profile, uLong adler) { … } void /* PRIVATE */ png_icc_set_sRGB(png_const_structrp png_ptr, png_colorspacerp colorspace, png_const_bytep profile, uLong adler) { … } #endif /* PNG_sRGB_PROFILE_CHECKS >= 0 */ #endif /* sRGB */ int /* PRIVATE */ png_colorspace_set_ICC(png_const_structrp png_ptr, png_colorspacerp colorspace, png_const_charp name, png_uint_32 profile_length, png_const_bytep profile, int color_type) { … } #endif /* iCCP */ #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED void /* PRIVATE */ png_colorspace_set_rgb_coefficients(png_structrp png_ptr) { … } #endif /* READ_RGB_TO_GRAY */ #endif /* COLORSPACE */ void /* PRIVATE */ png_check_IHDR(png_const_structrp png_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_type, int compression_type, int filter_type) { … } #if defined(PNG_sCAL_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) /* ASCII to fp functions */ /* Check an ASCII formatted floating point value, see the more detailed * comments in pngpriv.h */ /* The following is used internally to preserve the sticky flags */ #define png_fp_add(state, flags) … #define png_fp_set(state, value) … int /* PRIVATE */ png_check_fp_number(png_const_charp string, size_t size, int *statep, size_t *whereami) { … } /* The same but for a complete string. */ int png_check_fp_string(png_const_charp string, size_t size) { … } #endif /* pCAL || sCAL */ #ifdef PNG_sCAL_SUPPORTED # ifdef PNG_FLOATING_POINT_SUPPORTED /* Utility used below - a simple accurate power of ten from an integral * exponent. */ static double png_pow10(int power) { … } /* Function to format a floating point value in ASCII with a given * precision. */ void /* PRIVATE */ png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, size_t size, double fp, unsigned int precision) { … } # endif /* FLOATING_POINT */ # ifdef PNG_FIXED_POINT_SUPPORTED /* Function to format a fixed point value in ASCII. */ void /* PRIVATE */ png_ascii_from_fixed(png_const_structrp png_ptr, png_charp ascii, size_t size, png_fixed_point fp) { … } # endif /* FIXED_POINT */ #endif /* SCAL */ #if defined(PNG_FLOATING_POINT_SUPPORTED) && \ !defined(PNG_FIXED_POINT_MACRO_SUPPORTED) && \ (defined(PNG_gAMA_SUPPORTED) || defined(PNG_cHRM_SUPPORTED) || \ defined(PNG_sCAL_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) || \ defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)) || \ (defined(PNG_sCAL_SUPPORTED) && \ defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)) png_fixed_point png_fixed(png_const_structrp png_ptr, double fp, png_const_charp text) { … } #endif #if defined(PNG_GAMMA_SUPPORTED) || defined(PNG_COLORSPACE_SUPPORTED) ||\ defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG_READ_pHYs_SUPPORTED) /* muldiv functions */ /* This API takes signed arguments and rounds the result to the nearest * integer (or, for a fixed point number - the standard argument - to * the nearest .00001). Overflow and divide by zero are signalled in * the result, a boolean - true on success, false on overflow. */ int png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, png_int_32 divisor) { … } #endif /* READ_GAMMA || INCH_CONVERSIONS */ #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_INCH_CONVERSIONS_SUPPORTED) /* The following is for when the caller doesn't much care about the * result. */ png_fixed_point png_muldiv_warn(png_const_structrp png_ptr, png_fixed_point a, png_int_32 times, png_int_32 divisor) { … } #endif #ifdef PNG_GAMMA_SUPPORTED /* more fixed point functions for gamma */ /* Calculate a reciprocal, return 0 on div-by-zero or overflow. */ png_fixed_point png_reciprocal(png_fixed_point a) { … } /* This is the shared test on whether a gamma value is 'significant' - whether * it is worth doing gamma correction. */ int /* PRIVATE */ png_gamma_significant(png_fixed_point gamma_val) { … } #endif #ifdef PNG_READ_GAMMA_SUPPORTED #ifdef PNG_16BIT_SUPPORTED /* A local convenience routine. */ static png_fixed_point png_product2(png_fixed_point a, png_fixed_point b) { … } #endif /* 16BIT */ /* The inverse of the above. */ png_fixed_point png_reciprocal2(png_fixed_point a, png_fixed_point b) { … } #endif /* READ_GAMMA */ #ifdef PNG_READ_GAMMA_SUPPORTED /* gamma table code */ #ifndef PNG_FLOATING_ARITHMETIC_SUPPORTED /* Fixed point gamma. * * The code to calculate the tables used below can be found in the shell script * contrib/tools/intgamma.sh * * To calculate gamma this code implements fast log() and exp() calls using only * fixed point arithmetic. This code has sufficient precision for either 8-bit * or 16-bit sample values. * * The tables used here were calculated using simple 'bc' programs, but C double * precision floating point arithmetic would work fine. * * 8-bit log table * This is a table of -log(value/255)/log(2) for 'value' in the range 128 to * 255, so it's the base 2 logarithm of a normalized 8-bit floating point * mantissa. The numbers are 32-bit fractions. */ static const png_uint_32 png_8bit_l2[128] = { 4270715492U, 4222494797U, 4174646467U, 4127164793U, 4080044201U, 4033279239U, 3986864580U, 3940795015U, 3895065449U, 3849670902U, 3804606499U, 3759867474U, 3715449162U, 3671346997U, 3627556511U, 3584073329U, 3540893168U, 3498011834U, 3455425220U, 3413129301U, 3371120137U, 3329393864U, 3287946700U, 3246774933U, 3205874930U, 3165243125U, 3124876025U, 3084770202U, 3044922296U, 3005329011U, 2965987113U, 2926893432U, 2888044853U, 2849438323U, 2811070844U, 2772939474U, 2735041326U, 2697373562U, 2659933400U, 2622718104U, 2585724991U, 2548951424U, 2512394810U, 2476052606U, 2439922311U, 2404001468U, 2368287663U, 2332778523U, 2297471715U, 2262364947U, 2227455964U, 2192742551U, 2158222529U, 2123893754U, 2089754119U, 2055801552U, 2022034013U, 1988449497U, 1955046031U, 1921821672U, 1888774511U, 1855902668U, 1823204291U, 1790677560U, 1758320682U, 1726131893U, 1694109454U, 1662251657U, 1630556815U, 1599023271U, 1567649391U, 1536433567U, 1505374214U, 1474469770U, 1443718700U, 1413119487U, 1382670639U, 1352370686U, 1322218179U, 1292211689U, 1262349810U, 1232631153U, 1203054352U, 1173618059U, 1144320946U, 1115161701U, 1086139034U, 1057251672U, 1028498358U, 999877854U, 971388940U, 943030410U, 914801076U, 886699767U, 858725327U, 830876614U, 803152505U, 775551890U, 748073672U, 720716771U, 693480120U, 666362667U, 639363374U, 612481215U, 585715177U, 559064263U, 532527486U, 506103872U, 479792461U, 453592303U, 427502463U, 401522014U, 375650043U, 349885648U, 324227938U, 298676034U, 273229066U, 247886176U, 222646516U, 197509248U, 172473545U, 147538590U, 122703574U, 97967701U, 73330182U, 48790236U, 24347096U, 0U #if 0 /* The following are the values for 16-bit tables - these work fine for the * 8-bit conversions but produce very slightly larger errors in the 16-bit * log (about 1.2 as opposed to 0.7 absolute error in the final value). To * use these all the shifts below must be adjusted appropriately. */ 65166, 64430, 63700, 62976, 62257, 61543, 60835, 60132, 59434, 58741, 58054, 57371, 56693, 56020, 55352, 54689, 54030, 53375, 52726, 52080, 51439, 50803, 50170, 49542, 48918, 48298, 47682, 47070, 46462, 45858, 45257, 44661, 44068, 43479, 42894, 42312, 41733, 41159, 40587, 40020, 39455, 38894, 38336, 37782, 37230, 36682, 36137, 35595, 35057, 34521, 33988, 33459, 32932, 32408, 31887, 31369, 30854, 30341, 29832, 29325, 28820, 28319, 27820, 27324, 26830, 26339, 25850, 25364, 24880, 24399, 23920, 23444, 22970, 22499, 22029, 21562, 21098, 20636, 20175, 19718, 19262, 18808, 18357, 17908, 17461, 17016, 16573, 16132, 15694, 15257, 14822, 14390, 13959, 13530, 13103, 12678, 12255, 11834, 11415, 10997, 10582, 10168, 9756, 9346, 8937, 8531, 8126, 7723, 7321, 6921, 6523, 6127, 5732, 5339, 4947, 4557, 4169, 3782, 3397, 3014, 2632, 2251, 1872, 1495, 1119, 744, 372 #endif }; static png_int_32 png_log8bit(unsigned int x) { unsigned int lg2 = 0; /* Each time 'x' is multiplied by 2, 1 must be subtracted off the final log, * because the log is actually negate that means adding 1. The final * returned value thus has the range 0 (for 255 input) to 7.994 (for 1 * input), return -1 for the overflow (log 0) case, - so the result is * always at most 19 bits. */ if ((x &= 0xff) == 0) return -1; if ((x & 0xf0) == 0) lg2 = 4, x <<= 4; if ((x & 0xc0) == 0) lg2 += 2, x <<= 2; if ((x & 0x80) == 0) lg2 += 1, x <<= 1; /* result is at most 19 bits, so this cast is safe: */ return (png_int_32)((lg2 << 16) + ((png_8bit_l2[x-128]+32768)>>16)); } /* The above gives exact (to 16 binary places) log2 values for 8-bit images, * for 16-bit images we use the most significant 8 bits of the 16-bit value to * get an approximation then multiply the approximation by a correction factor * determined by the remaining up to 8 bits. This requires an additional step * in the 16-bit case. * * We want log2(value/65535), we have log2(v'/255), where: * * value = v' * 256 + v'' * = v' * f * * So f is value/v', which is equal to (256+v''/v') since v' is in the range 128 * to 255 and v'' is in the range 0 to 255 f will be in the range 256 to less * than 258. The final factor also needs to correct for the fact that our 8-bit * value is scaled by 255, whereas the 16-bit values must be scaled by 65535. * * This gives a final formula using a calculated value 'x' which is value/v' and * scaling by 65536 to match the above table: * * log2(x/257) * 65536 * * Since these numbers are so close to '1' we can use simple linear * interpolation between the two end values 256/257 (result -368.61) and 258/257 * (result 367.179). The values used below are scaled by a further 64 to give * 16-bit precision in the interpolation: * * Start (256): -23591 * Zero (257): 0 * End (258): 23499 */ #ifdef PNG_16BIT_SUPPORTED static png_int_32 png_log16bit(png_uint_32 x) { unsigned int lg2 = 0; /* As above, but now the input has 16 bits. */ if ((x &= 0xffff) == 0) return -1; if ((x & 0xff00) == 0) lg2 = 8, x <<= 8; if ((x & 0xf000) == 0) lg2 += 4, x <<= 4; if ((x & 0xc000) == 0) lg2 += 2, x <<= 2; if ((x & 0x8000) == 0) lg2 += 1, x <<= 1; /* Calculate the base logarithm from the top 8 bits as a 28-bit fractional * value. */ lg2 <<= 28; lg2 += (png_8bit_l2[(x>>8)-128]+8) >> 4; /* Now we need to interpolate the factor, this requires a division by the top * 8 bits. Do this with maximum precision. */ x = ((x << 16) + (x >> 9)) / (x >> 8); /* Since we divided by the top 8 bits of 'x' there will be a '1' at 1<<24, * the value at 1<<16 (ignoring this) will be 0 or 1; this gives us exactly * 16 bits to interpolate to get the low bits of the result. Round the * answer. Note that the end point values are scaled by 64 to retain overall * precision and that 'lg2' is current scaled by an extra 12 bits, so adjust * the overall scaling by 6-12. Round at every step. */ x -= 1U << 24; if (x <= 65536U) /* <= '257' */ lg2 += ((23591U * (65536U-x)) + (1U << (16+6-12-1))) >> (16+6-12); else lg2 -= ((23499U * (x-65536U)) + (1U << (16+6-12-1))) >> (16+6-12); /* Safe, because the result can't have more than 20 bits: */ return (png_int_32)((lg2 + 2048) >> 12); } #endif /* 16BIT */ /* The 'exp()' case must invert the above, taking a 20-bit fixed point * logarithmic value and returning a 16 or 8-bit number as appropriate. In * each case only the low 16 bits are relevant - the fraction - since the * integer bits (the top 4) simply determine a shift. * * The worst case is the 16-bit distinction between 65535 and 65534. This * requires perhaps spurious accuracy in the decoding of the logarithm to * distinguish log2(65535/65534.5) - 10^-5 or 17 bits. There is little chance * of getting this accuracy in practice. * * To deal with this the following exp() function works out the exponent of the * fractional part of the logarithm by using an accurate 32-bit value from the * top four fractional bits then multiplying in the remaining bits. */ static const png_uint_32 png_32bit_exp[16] = { /* NOTE: the first entry is deliberately set to the maximum 32-bit value. */ 4294967295U, 4112874773U, 3938502376U, 3771522796U, 3611622603U, 3458501653U, 3311872529U, 3171459999U, 3037000500U, 2908241642U, 2784941738U, 2666869345U, 2553802834U, 2445529972U, 2341847524U, 2242560872U }; /* Adjustment table; provided to explain the numbers in the code below. */ #if 0 for (i=11;i>=0;--i){ print i, " ", (1 - e(-(2^i)/65536*l(2))) * 2^(32-i), "\n"} 11 44937.64284865548751208448 10 45180.98734845585101160448 9 45303.31936980687359311872 8 45364.65110595323018870784 7 45395.35850361789624614912 6 45410.72259715102037508096 5 45418.40724413220722311168 4 45422.25021786898173001728 3 45424.17186732298419044352 2 45425.13273269940811464704 1 45425.61317555035558641664 0 45425.85339951654943850496 #endif static png_uint_32 png_exp(png_fixed_point x) { if (x > 0 && x <= 0xfffff) /* Else overflow or zero (underflow) */ { /* Obtain a 4-bit approximation */ png_uint_32 e = png_32bit_exp[(x >> 12) & 0x0f]; /* Incorporate the low 12 bits - these decrease the returned value by * multiplying by a number less than 1 if the bit is set. The multiplier * is determined by the above table and the shift. Notice that the values * converge on 45426 and this is used to allow linear interpolation of the * low bits. */ if (x & 0x800) e -= (((e >> 16) * 44938U) + 16U) >> 5; if (x & 0x400) e -= (((e >> 16) * 45181U) + 32U) >> 6; if (x & 0x200) e -= (((e >> 16) * 45303U) + 64U) >> 7; if (x & 0x100) e -= (((e >> 16) * 45365U) + 128U) >> 8; if (x & 0x080) e -= (((e >> 16) * 45395U) + 256U) >> 9; if (x & 0x040) e -= (((e >> 16) * 45410U) + 512U) >> 10; /* And handle the low 6 bits in a single block. */ e -= (((e >> 16) * 355U * (x & 0x3fU)) + 256U) >> 9; /* Handle the upper bits of x. */ e >>= x >> 16; return e; } /* Check for overflow */ if (x <= 0) return png_32bit_exp[0]; /* Else underflow */ return 0; } static png_byte png_exp8bit(png_fixed_point lg2) { /* Get a 32-bit value: */ png_uint_32 x = png_exp(lg2); /* Convert the 32-bit value to 0..255 by multiplying by 256-1. Note that the * second, rounding, step can't overflow because of the first, subtraction, * step. */ x -= x >> 8; return (png_byte)(((x + 0x7fffffU) >> 24) & 0xff); } #ifdef PNG_16BIT_SUPPORTED static png_uint_16 png_exp16bit(png_fixed_point lg2) { /* Get a 32-bit value: */ png_uint_32 x = png_exp(lg2); /* Convert the 32-bit value to 0..65535 by multiplying by 65536-1: */ x -= x >> 16; return (png_uint_16)((x + 32767U) >> 16); } #endif /* 16BIT */ #endif /* FLOATING_ARITHMETIC */ png_byte png_gamma_8bit_correct(unsigned int value, png_fixed_point gamma_val) { … } #ifdef PNG_16BIT_SUPPORTED png_uint_16 png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val) { … } #endif /* 16BIT */ /* This does the right thing based on the bit_depth field of the * png_struct, interpreting values as 8-bit or 16-bit. While the result * is nominally a 16-bit value if bit depth is 8 then the result is * 8-bit (as are the arguments.) */ png_uint_16 /* PRIVATE */ png_gamma_correct(png_structrp png_ptr, unsigned int value, png_fixed_point gamma_val) { … } #ifdef PNG_16BIT_SUPPORTED /* Internal function to build a single 16-bit table - the table consists of * 'num' 256 entry subtables, where 'num' is determined by 'shift' - the amount * to shift the input values right (or 16-number_of_signifiant_bits). * * The caller is responsible for ensuring that the table gets cleaned up on * png_error (i.e. if one of the mallocs below fails) - i.e. the *table argument * should be somewhere that will be cleaned. */ static void png_build_16bit_table(png_structrp png_ptr, png_uint_16pp *ptable, unsigned int shift, png_fixed_point gamma_val) { … } /* NOTE: this function expects the *inverse* of the overall gamma transformation * required. */ static void png_build_16to8_table(png_structrp png_ptr, png_uint_16pp *ptable, unsigned int shift, png_fixed_point gamma_val) { … } #endif /* 16BIT */ /* Build a single 8-bit table: same as the 16-bit case but much simpler (and * typically much faster). Note that libpng currently does no sBIT processing * (apparently contrary to the spec) so a 256-entry table is always generated. */ static void png_build_8bit_table(png_structrp png_ptr, png_bytepp ptable, png_fixed_point gamma_val) { … } /* Used from png_read_destroy and below to release the memory used by the gamma * tables. */ void /* PRIVATE */ png_destroy_gamma_table(png_structrp png_ptr) { … } /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit * tables, we don't make a full table if we are reducing to 8-bit in * the future. Note also how the gamma_16 tables are segmented so that * we don't need to allocate > 64K chunks for a full 16-bit table. */ void /* PRIVATE */ png_build_gamma_table(png_structrp png_ptr, int bit_depth) { … } #endif /* READ_GAMMA */ /* HARDWARE OR SOFTWARE OPTION SUPPORT */ #ifdef PNG_SET_OPTION_SUPPORTED int PNGAPI png_set_option(png_structrp png_ptr, int option, int onoff) { … } #endif /* sRGB support */ #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) /* sRGB conversion tables; these are machine generated with the code in * contrib/tools/makesRGB.c. The actual sRGB transfer curve defined in the * specification (see the article at https://en.wikipedia.org/wiki/SRGB) * is used, not the gamma=1/2.2 approximation use elsewhere in libpng. * The sRGB to linear table is exact (to the nearest 16-bit linear fraction). * The inverse (linear to sRGB) table has accuracies as follows: * * For all possible (255*65535+1) input values: * * error: -0.515566 - 0.625971, 79441 (0.475369%) of readings inexact * * For the input values corresponding to the 65536 16-bit values: * * error: -0.513727 - 0.607759, 308 (0.469978%) of readings inexact * * In all cases the inexact readings are only off by one. */ #ifdef PNG_SIMPLIFIED_READ_SUPPORTED /* The convert-to-sRGB table is only currently required for read. */ const png_uint_16 png_sRGB_table[256] = …; #endif /* SIMPLIFIED_READ */ /* The base/delta tables are required for both read and write (but currently * only the simplified versions.) */ const png_uint_16 png_sRGB_base[512] = …; const png_byte png_sRGB_delta[512] = …; #endif /* SIMPLIFIED READ/WRITE sRGB support */ /* SIMPLIFIED READ/WRITE SUPPORT */ #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) static int png_image_free_function(png_voidp argument) { … } void PNGAPI png_image_free(png_imagep image) { … } int /* PRIVATE */ png_image_error(png_imagep image, png_const_charp error_message) { … } #endif /* SIMPLIFIED READ/WRITE */ #endif /* READ || WRITE */