0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A/* png.c - location for general purpose libpng functions 0N/A * This file is available under and governed by the GNU General Public 0N/A * License version 2 only, as published by the Free Software Foundation. 0N/A * However, the following notice accompanied the original version of this 0N/A * file and, per its terms, should not be removed: 4418N/A * Last changed in libpng 1.5.4 [July 7, 2011] 4418N/A * Copyright (c) 1998-2011 Glenn Randers-Pehrson 0N/A * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 0N/A * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 4418N/A * This code is released under the libpng license. 4418N/A * For conditions of distribution and use, see the disclaimer 0N/A/* Generate a compiler error if there is an old png.h in the search path. */ 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/* 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#
endif /* PNG_READ_SUPPORTED */ 4418N/A/* Function to allocate memory for zlib */ 4418N/A/* Function to free memory for zlib */ 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/* 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. 4418N/A/* Check a user supplied version number, called from both read and write 4418N/A * functions that create a png_struct 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. 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/* 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/* 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() 4418N/A "Unknown freer parameter in png_data_freer");
4418N/A /* Free text item num or (if num == -1) all text items */ 4418N/A /* Free a given sPLT entry, or (if num == -1) all sPLT entries */ 4418N/A /* Free any PLTE entry that was internally allocated */ 4418N/A /* Free any image bits attached to the info structure */ 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#
endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ 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/* 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/* 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 {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
0N/A "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"};
4418N/A#
endif /* PNG_TIME_RFC1123_SUPPORTED */ 0N/A#
endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ 4418N/A "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
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.";
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 * 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 /* Version of *.c files used when building libpng */ 0N/A /* Version of *.h files used when building libpng */ 0N/A /* Returns longer string containing both version and date */ 4418N/A /* Check chunk_name and return "keep" value if it's on the list, else 0 */ 4418N/A#
endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ 0N/A/* This function, added to libpng-1.0.6g, is untested. */ 4418N/A#
endif /* PNG_READ_SUPPORTED */ 0N/A/* This function was added to libpng-1.0.7 */ 0N/A /* Version of *.c files used when building libpng */ 0N/A/* Added at libpng version 1.2.6 */ 4418N/A/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */ 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 "Ignoring attempt to set negative chromaticity value");
4418N/A /* And (x+y) must be <= PNG_FP_1 (so z is >= 0) */ 4418N/A "Ignoring attempt to set cHRM RGB triangle with zero area");
4418N/A#
endif /* PNG_CHECK_cHRM_SUPPORTED */ 4418N/A /* Check for width and height valid values */ 4418N/A >>
3)
/* 8-byte RGBA pixels */ 4418N/A -
7*
8 /* rounding of width to multiple of 8 pixels */ 4418N/A -
8)
/* extra max_pixel_depth pad */ 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/* Check an ASCII formated floating point value, see the more detailed 4418N/A/* The following is used internally to preserve the sticky flags */ 4418N/A /* First find the type of the next character */ 4418N/A case 49:
case 50:
case 51:
case 52:
4418N/A case 53:
case 54:
case 55:
case 56:
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 /* Ok as trailer, ok as lead of fraction. */ 4418N/A /* case PNG_FP_FRACTION + PNG_FP_SAW_SIGN: 4418N/A goto PNG_FP_End; ** no sign in fraction */ 4418N/A /* case PNG_FP_FRACTION + PNG_FP_SAW_DOT: 4418N/A goto PNG_FP_End; ** Because SAW_DOT is always set */ 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 /* case PNG_FP_EXPONENT + PNG_FP_SAW_DOT: 4418N/A /* case PNG_FP_EXPONEXT + PNG_FP_SAW_E: 4418N/A /* The character seems ok, continue. */ 4418N/A /* Here at the end, update the state and return the correct 4418N/A/* The same but for a complete string. */ 4418N/A/* Utility used below - a simple accurate power of ten from an integral 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 /* Decompose power bitwise. */ 4418N/A /* else power is 0 and d is 1 */ 4418N/A/* Function to format a floating point value in ASCII with a given 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 /* Enforce the limit of the implementation precision too. */ 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 /* Avoid underflow here. */ 4418N/A /* And this may overflow. */ 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 /* 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 /* Allow up to two leading zeros - this will not lengthen 4418N/A * the number compared to using E-n. 4418N/A /* Generate the digit list, stripping trailing zeros and 4418N/A * inserting a '.' before a digit if the exponent is 0. 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 /* Rounding up to 10, handle that here. */ 4418N/A /* Advance exp_b10 to '1', so that the 4418N/A * decimal point happens after the 4418N/A /* Did we reach the beginning? If so adjust the 4418N/A * exponent but take into account the leading 4418N/A if (d >
9)
/* cdigits == 0 */ 4418N/A /* Leading decimal point (plus zeros?), if 4418N/A * we lose the decimal point here it must 4418N/A /* Else lost a leading zero, so 'exp_b10' is 4418N/A /* In all cases we output a '1' */ 4418N/A fp = 0;
/* Guarantees termination below. */ 4418N/A /* Included embedded zeros in the digit count. */ 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 /* The total output count (max) is now 4+precision */ 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 /* 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 /* Total buffer requirement (including the '\0') is 4418N/A * 5+precision - see check at the start. 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 /* Need another size check here for the exponent digits, so 4418N/A * this need not be considered above. 4418N/A /* Here on buffer too small. */ 4418N/A#
endif /* FLOATING_POINT */ 4418N/A/* Function to format a fixed point value in ASCII. 4418N/A /* Require space for 10 decimal digits, a decimal point, a minus sign and a 4418N/A * trailing \0, 13 characters: 4418N/A /* Avoid overflow here on the minimum integer. */ 4418N/A if (
num <=
0x80000000U)
/* else overflowed */ 4418N/A /* Split the low digit off 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 /* 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 /* ndigits may be <5 for small numbers, output leading zeros 4418N/A * then ndigits digits to first: 4418N/A /* Don't output the trailing zeros! */ 4418N/A /* And null terminate the string: */ 4418N/A /* Here on buffer too small. */ 4418N/A if (r >
2147483647. || r < -
2147483648.)
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 /* Return a * times / divisor, rounded. */ 4418N/A /* A png_fixed_point is a 32-bit integer. */ 4418N/A if (r <=
2147483647. && r >= -
2147483648.)
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 /* Can't overflow because the a*times bit is only 30 4418N/A * division, we know that s32 < D, so the maximum 4418N/A#
endif /* READ_GAMMA || INCH_CONVERSIONS */ 4418N/A/* The following is for when the caller doesn't much care about the 4418N/A/* Calculate a reciprocal, return 0 on div-by-zero or overflow. */ 4418N/A if (r <=
2147483647. && r >= -
2147483648.)
4418N/A/* A local convenience routine. */ 4418N/A /* The required result is 1/a * 1/b; the following preserves accuracy. */ 4418N/A if (r <=
2147483647. && r >= -
2147483648.)
4418N/A/* The inverse of the above. */ 4418N/A /* The required result is 1/a * 1/b; the following preserves accuracy. */ 4418N/A if (r <=
2147483647. && r >= -
2147483648.)
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/* Added at libpng version 1.2.34 (Dec 8, 2008) and 1.4.0 (Jan 2, 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 * 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 x = a * d + c * b;
/* AD + CB */ 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 * 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 * 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 for (i=
128;i<
256;++i) { .
5 - l(i/
255)/l(
2)*
65536*
65536; }
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 /* 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 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 /* 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 /* result is at most 19 bits, so this cast is safe: */ 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 * We want log2(value/65535), we have log2(v'/255), where: 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 * 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 * 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 /* As above, but now the input has 16 bits. */ 4418N/A /* Calculate the base logarithm from the top 8 bits as a 28-bit fractional 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 x = ((x <<
16) + (x >>
9)) / (x >>
8);
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 if (x <=
65536U)
/* <= '257' */ 4418N/A lg2 += ((
23591U * (
65536U-x)) + (
1U << (
16+
6-
12-
1))) >> (
16+
6-
12);
4418N/A lg2 -= ((
23499U * (x-
65536U)) + (
1U << (
16+
6-
12-
1))) >> (
16+
6-
12);
4418N/A /* Safe, because the result can't have more than 20 bits: */ 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 * 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 * 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 for (i=0;i<
16;++i) { .
5 + e(-i/
16*l(
2))*
2^
32; }
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/* Adjustment table; provided to explain the numbers in the code below. */ 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 if (x > 0 && x <=
0xfffff)
/* Else overflow or zero (underflow) */ 4418N/A /* Obtain a 4-bit approximation */ 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 e -= (((e >>
16) *
44938U) +
16U) >>
5;
4418N/A e -= (((e >>
16) *
45181U) +
32U) >>
6;
4418N/A e -= (((e >>
16) *
45303U) +
64U) >>
7;
4418N/A e -= (((e >>
16) *
45365U) +
128U) >>
8;
4418N/A e -= (((e >>
16) *
45395U) +
256U) >>
9;
4418N/A e -= (((e >>
16) *
45410U) +
512U) >>
10;
4418N/A /* And handle the low 6 bits in a single block. */ 4418N/A e -= (((e >>
16) *
355U * (x &
0x3fU)) +
256U) >>
9;
4418N/A /* Handle the upper bits of x. */ 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 /* Convert the 32-bit value to 0..65535 by multiplying by 65536-1: */ 4418N/A#
endif /* FLOATING_ARITHMETIC */ 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/* This is the shared test on whether a gamma value is 'significant' - whether 4418N/A * it is worth doing gamma correction. 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 * 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 /* Various values derived from 'shift': */ 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 /* 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 * We want input * 65535/max, rounded, the arithmetic fits in 32 4418N/A * bits (unsigned) so long as max <= 32767. 4418N/A /* Inline the 'max' scaling operation: */ 4418N/A /* We must still build a table, but do it the fast way. */ 4418N/A/* NOTE: this function expects the *inverse* of the overall gamma transformation 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 /* '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 * 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 * 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 for (i = 0; i <
255; ++i)
/* 8-bit output value */ 4418N/A /* Find the corresponding maximum input value */ 4418N/A /* Find the boundary value in 16 bits: */ 4418N/A /* Adjust (round) to (16-shift) bits: */ 4418N/A /* And fill in the final entries. */ 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/* 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#
endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ 4418N/A /* 16-bit gamma code uses this equation: 4418N/A * ov = table[(iv & 0xff) >> gamma_shift][iv >> 8] 4418N/A * Where 'iv' is the input color value and 'ov' is the output value - 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 * table[low bits][high 8 bits] 4418N/A * So the table 'n' corresponds to all those 'iv' of: 4418N/A * <all high 8-bit values><n << gamma_shift>..<(n+1 << gamma_shift)-1> 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 /* 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 /* 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#
endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ 0N/A#
endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */