[devel] Added some whitespace for readability.

This commit is contained in:
Glenn Randers-Pehrson 2010-07-29 22:54:34 -05:00
parent f3d53796af
commit 7b81e2e747

85
png.c
View File

@ -918,8 +918,10 @@ png_check_fp_number(png_charp string, png_size_t size, int *statep,
/* First find the type of the next character */ /* First find the type of the next character */
{ {
char ch = string[i]; char ch = string[i];
if (ch >= 48 && ch <= 57) if (ch >= 48 && ch <= 57)
type = PNG_FP_DIGIT; type = PNG_FP_DIGIT;
else switch (ch) else switch (ch)
{ {
case 43: case 45: type = PNG_FP_SIGN; break; case 43: case 45: type = PNG_FP_SIGN; break;
@ -938,34 +940,48 @@ png_check_fp_number(png_charp string, png_size_t size, int *statep,
case PNG_FP_INTEGER + PNG_FP_SIGN: case PNG_FP_INTEGER + PNG_FP_SIGN:
if (state & PNG_FP_SAW_ANY) if (state & PNG_FP_SAW_ANY)
goto PNG_FP_End; /* not a part of the number */ goto PNG_FP_End; /* not a part of the number */
png_fp_add(state, PNG_FP_SAW_SIGN); png_fp_add(state, PNG_FP_SAW_SIGN);
break; break;
case PNG_FP_INTEGER + PNG_FP_DOT: case PNG_FP_INTEGER + PNG_FP_DOT:
/* Ok as trailer, ok as lead of fraction. */ /* Ok as trailer, ok as lead of fraction. */
if (state & PNG_FP_SAW_DOT) /* two dots */ if (state & PNG_FP_SAW_DOT) /* two dots */
goto PNG_FP_End; goto PNG_FP_End;
else if (state & PNG_FP_SAW_DIGIT) /* trailing dot? */ else if (state & PNG_FP_SAW_DIGIT) /* trailing dot? */
png_fp_add(state, PNG_FP_SAW_DOT); png_fp_add(state, PNG_FP_SAW_DOT);
else else
png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT); png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT);
break; break;
case PNG_FP_INTEGER + PNG_FP_DIGIT: case PNG_FP_INTEGER + PNG_FP_DIGIT:
if (state & PNG_FP_SAW_DOT) /* delayed fraction */ if (state & PNG_FP_SAW_DOT) /* delayed fraction */
png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT); png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT);
png_fp_add(state, PNG_FP_SAW_DIGIT + PNG_FP_WAS_VALID); png_fp_add(state, PNG_FP_SAW_DIGIT + PNG_FP_WAS_VALID);
break; break;
case PNG_FP_INTEGER + PNG_FP_E: case PNG_FP_INTEGER + PNG_FP_E:
if ((state & PNG_FP_SAW_DIGIT) == 0) if ((state & PNG_FP_SAW_DIGIT) == 0)
goto PNG_FP_End; goto PNG_FP_End;
png_fp_set(state, PNG_FP_EXPONENT); png_fp_set(state, PNG_FP_EXPONENT);
break; break;
/* case PNG_FP_FRACTION + PNG_FP_SIGN: /* case PNG_FP_FRACTION + PNG_FP_SIGN:
goto PNG_FP_End; ** no sign in exponent */ goto PNG_FP_End; ** no sign in exponent */
/* case PNG_FP_FRACTION + PNG_FP_DOT: /* case PNG_FP_FRACTION + PNG_FP_DOT:
goto PNG_FP_End; ** Because SAW_DOT is always set */ goto PNG_FP_End; ** Because SAW_DOT is always set */
case PNG_FP_FRACTION + PNG_FP_DIGIT: case PNG_FP_FRACTION + PNG_FP_DIGIT:
png_fp_add(state, PNG_FP_SAW_DIGIT + PNG_FP_WAS_VALID); png_fp_add(state, PNG_FP_SAW_DIGIT + PNG_FP_WAS_VALID);
break; break;
case PNG_FP_FRACTION + PNG_FP_E: case PNG_FP_FRACTION + PNG_FP_E:
/* This is correct because the trailing '.' on an /* This is correct because the trailing '.' on an
* integer is handled above - so we can only get here * integer is handled above - so we can only get here
@ -973,20 +989,30 @@ png_check_fp_number(png_charp string, png_size_t size, int *statep,
*/ */
if ((state & PNG_FP_SAW_DIGIT) == 0) if ((state & PNG_FP_SAW_DIGIT) == 0)
goto PNG_FP_End; goto PNG_FP_End;
png_fp_set(state, PNG_FP_EXPONENT); png_fp_set(state, PNG_FP_EXPONENT);
break; break;
case PNG_FP_EXPONENT + PNG_FP_SIGN: case PNG_FP_EXPONENT + PNG_FP_SIGN:
if (state & PNG_FP_SAW_ANY) if (state & PNG_FP_SAW_ANY)
goto PNG_FP_End; /* not a part of the number */ goto PNG_FP_End; /* not a part of the number */
png_fp_add(state, PNG_FP_SAW_SIGN); png_fp_add(state, PNG_FP_SAW_SIGN);
break; break;
/* case PNG_FP_EXPONENT + PNG_FP_DOT: /* case PNG_FP_EXPONENT + PNG_FP_DOT:
goto PNG_FP_End; */ goto PNG_FP_End; */
case PNG_FP_EXPONENT + PNG_FP_DIGIT: case PNG_FP_EXPONENT + PNG_FP_DIGIT:
png_fp_add(state, PNG_FP_SAW_DIGIT + PNG_FP_WAS_VALID); png_fp_add(state, PNG_FP_SAW_DIGIT + PNG_FP_WAS_VALID);
break; break;
/* case PNG_FP_EXPONEXT + PNG_FP_E: /* case PNG_FP_EXPONEXT + PNG_FP_E:
goto PNG_FP_End; */ goto PNG_FP_End; */
default: goto PNG_FP_End; /* I.e. break 2 */ default: goto PNG_FP_End; /* I.e. break 2 */
} }
@ -1027,11 +1053,13 @@ png_pow10(int power)
{ {
int recip = 0; int recip = 0;
double d = 1; double d = 1;
/* Handle negative exponent with a reciprocal at the end because /* Handle negative exponent with a reciprocal at the end because
* 10 is exact whereas .1 is inexact in base 2 * 10 is exact whereas .1 is inexact in base 2
*/ */
if (power < 0) if (power < 0)
recip = 1, power = -power; recip = 1, power = -power;
if (power > 0) if (power > 0)
{ {
/* Decompose power bitwise. */ /* Decompose power bitwise. */
@ -1095,15 +1123,20 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size,
* exponents. * exponents.
*/ */
(void)frexp(fp, &exp); /* exponent to base 2 */ (void)frexp(fp, &exp); /* exponent to base 2 */
exp = (exp * 77) >> 8; /* <= exponent to base 10 */ exp = (exp * 77) >> 8; /* <= exponent to base 10 */
/* Avoid underflow here. */ /* Avoid underflow here. */
base = png_pow10(exp); /* May underflow */ base = png_pow10(exp); /* May underflow */
while (base < DBL_MIN || base < fp) while (base < DBL_MIN || base < fp)
{ {
/* And this may overflow. */ /* And this may overflow. */
double test = png_pow10(exp+1); double test = png_pow10(exp+1);
if (test <= DBL_MAX) if (test <= DBL_MAX)
++exp, base = test; ++exp, base = test;
else else
break; break;
} }
@ -1144,6 +1177,7 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size,
*/ */
clead = czero; /* Count of leading zeros */ clead = czero; /* Count of leading zeros */
cdigits = 0; /* Count of digits in list. */ cdigits = 0; /* Count of digits in list. */
do do
{ {
double d; double d;
@ -1156,6 +1190,7 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size,
*/ */
if (cdigits+czero-clead+1 < (int)precision) if (cdigits+czero-clead+1 < (int)precision)
fp = modf(fp, &d); fp = modf(fp, &d);
else else
{ {
d = floor(fp + .5); d = floor(fp + .5);
@ -1173,8 +1208,10 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size,
while (cdigits > 0 && d > 9) while (cdigits > 0 && d > 9)
{ {
int ch = *--ascii; int ch = *--ascii;
if (exp != (-1)) if (exp != (-1))
++exp; ++exp;
else if (ch == 46) else if (ch == 46)
{ {
ch = *--ascii, ++size; ch = *--ascii, ++size;
@ -1202,8 +1239,10 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size,
* be reentered below. * be reentered below.
*/ */
int ch = *--ascii; int ch = *--ascii;
if (ch == 46) if (ch == 46)
++size, exp = 1; ++size, exp = 1;
/* Else lost a leading zero, so 'exp' is /* Else lost a leading zero, so 'exp' is
* still ok at (-1) * still ok at (-1)
*/ */
@ -1274,7 +1313,9 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size,
* the output count. * the output count.
*/ */
while (--exp >= 0) *ascii++ = 48; while (--exp >= 0) *ascii++ = 48;
*ascii = 0; *ascii = 0;
/* Total buffer requirement (including the '\0') is /* Total buffer requirement (including the '\0') is
* 5+precision - see check at the start. * 5+precision - see check at the start.
*/ */
@ -1297,6 +1338,7 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size,
} }
cdigits = 0; cdigits = 0;
while (exp > 0) while (exp > 0)
{ {
exponent[cdigits++] = 48 + exp % 10; exponent[cdigits++] = 48 + exp % 10;
@ -1309,7 +1351,9 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size,
if ((int)size > cdigits) if ((int)size > cdigits)
{ {
while (cdigits > 0) *ascii++ = exponent[--cdigits]; while (cdigits > 0) *ascii++ = exponent[--cdigits];
*ascii = 0; *ascii = 0;
return; return;
} }
} }
@ -1343,6 +1387,7 @@ png_fixed_point
png_fixed(png_structp png_ptr, double fp, png_const_charp text) png_fixed(png_structp png_ptr, double fp, png_const_charp text)
{ {
double r = floor(100000 * fp + .5); double r = floor(100000 * fp + .5);
if (r <= 2147483647. && r >= -2147483648.) if (r <= 2147483647. && r >= -2147483648.)
return (png_fixed_point)r; return (png_fixed_point)r;
@ -1377,6 +1422,7 @@ png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times,
r *= times; r *= times;
r /= div; r /= div;
r = floor(r+.5); r = floor(r+.5);
/* A png_fixed_point is a 32 bit integer. */ /* A png_fixed_point is a 32 bit integer. */
if (r <= 2147483647. && r >= -2147483648.) if (r <= 2147483647. && r >= -2147483648.)
{ {
@ -1415,7 +1461,9 @@ png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times,
s16 = (s16 & 0xffff) << 16; s16 = (s16 & 0xffff) << 16;
s00 += s16; s00 += s16;
if (s00 < s16) ++s32; /* carry */
if (s00 < s16)
++s32; /* carry */
if (s32 < D) /* else overflow */ if (s32 < D) /* else overflow */
{ {
@ -1432,6 +1480,7 @@ png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times,
if (bitshift > 0) if (bitshift > 0)
d32 = D >> (32-bitshift), d00 = D << bitshift; d32 = D >> (32-bitshift), d00 = D << bitshift;
else else
d32 = 0, d00 = D; d32 = 0, d00 = D;
@ -1440,15 +1489,18 @@ png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times,
if (s00 < d00) --s32; /* carry */ if (s00 < d00) --s32; /* carry */
s32 -= d32, s00 -= d00, result += 1<<bitshift; s32 -= d32, s00 -= d00, result += 1<<bitshift;
} }
else else
if (s32 == d32 && s00 >= d00) if (s32 == d32 && s00 >= d00)
s32 = 0, s00 -= d00, result += 1<<bitshift; s32 = 0, s00 -= d00, result += 1<<bitshift;
} }
/* Handle the rounding. */ /* Handle the rounding. */
if (s00 >= (D >> 1)) ++result; if (s00 >= (D >> 1))
++result;
if (negative) result = -result; if (negative)
result = -result;
/* Check for overflow. */ /* Check for overflow. */
if (negative && result <= 0 || !negative && result >= 0) if (negative && result <= 0 || !negative && result >= 0)
@ -1474,6 +1526,7 @@ png_muldiv_warn(png_structp png_ptr, png_fixed_point a, png_int_32 times,
png_int_32 div) png_int_32 div)
{ {
png_fixed_point result; png_fixed_point result;
if (png_muldiv(&result, a, times, div)) if (png_muldiv(&result, a, times, div))
return result; return result;
@ -1489,10 +1542,12 @@ png_reciprocal(png_fixed_point a)
{ {
#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
double r = floor(1E10/a+.5); double r = floor(1E10/a+.5);
if (r <= 2147483647. && r >= -2147483648.) if (r <= 2147483647. && r >= -2147483648.)
return (png_fixed_point)r; return (png_fixed_point)r;
#else #else
png_fixed_point res; png_fixed_point res;
if (png_muldiv(&res, 100000, 100000, a)) if (png_muldiv(&res, 100000, 100000, a))
return res; return res;
#endif #endif
@ -1509,10 +1564,12 @@ png_product2(png_fixed_point a, png_fixed_point b)
double r = a * 1E-5; double r = a * 1E-5;
r *= b; r *= b;
r = floor(r+.5); r = floor(r+.5);
if (r <= 2147483647. && r >= -2147483648.) if (r <= 2147483647. && r >= -2147483648.)
return (png_fixed_point)r; return (png_fixed_point)r;
#else #else
png_fixed_point res; png_fixed_point res;
if (png_muldiv(&res, a, b, 100000)) if (png_muldiv(&res, a, b, 100000))
return res; return res;
#endif #endif
@ -1529,6 +1586,7 @@ png_reciprocal2(png_fixed_point a, png_fixed_point b)
double r = 1E15/a; double r = 1E15/a;
r /= b; r /= b;
r = floor(r+.5); r = floor(r+.5);
if (r <= 2147483647. && r >= -2147483648.) if (r <= 2147483647. && r >= -2147483648.)
return (png_fixed_point)r; return (png_fixed_point)r;
#else #else
@ -1538,6 +1596,7 @@ png_reciprocal2(png_fixed_point a, png_fixed_point b)
* 1/100000 * 1/100000
*/ */
png_fixed_point res = png_product2(a, b); png_fixed_point res = png_product2(a, b);
if (res != 0) if (res != 0)
return png_reciprocal(res); return png_reciprocal(res);
#endif #endif
@ -1751,8 +1810,10 @@ png_log16bit(png_uint_32 x)
* the overall scaling by 6-12. Round at every step. * the overall scaling by 6-12. Round at every step.
*/ */
x -= 1U << 24; x -= 1U << 24;
if (x <= 65536U) /* <= '257' */ if (x <= 65536U) /* <= '257' */
log += ((23591U * (65536U-x)) + (1U << (16+6-12-1))) >> (16+6-12); log += ((23591U * (65536U-x)) + (1U << (16+6-12-1))) >> (16+6-12);
else else
log -= ((23499U * (x-65536U)) + (1U << (16+6-12-1))) >> (16+6-12); log -= ((23499U * (x-65536U)) + (1U << (16+6-12-1))) >> (16+6-12);
@ -1812,7 +1873,7 @@ png_exp(png_uint_32 x)
/* Incorporate the low 12 bits - these decrease the returned value by /* 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 * 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 * 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 * converge on 45426 and this is used to allow linear interpolation of the
* low bits. * low bits.
*/ */
@ -1881,6 +1942,7 @@ png_gamma_8bit_correct(unsigned value, png_fixed_point gamma)
# else # else
png_uint_32 log = png_log8bit(value); png_uint_32 log = png_log8bit(value);
png_fixed_point res; png_fixed_point res;
if (png_muldiv(&res, gamma, log, PNG_FP_1)) if (png_muldiv(&res, gamma, log, PNG_FP_1))
return png_exp8bit(res); return png_exp8bit(res);
# endif # endif
@ -1899,9 +1961,11 @@ png_gamma_16bit_correct(unsigned value, png_fixed_point gamma)
{ {
# ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
return (png_uint_16)floor(65535*pow(value/65535.,gamma*.00001)+.5); return (png_uint_16)floor(65535*pow(value/65535.,gamma*.00001)+.5);
# else # else
png_uint_32 log = png_log16bit(value); png_uint_32 log = png_log16bit(value);
png_fixed_point res; png_fixed_point res;
if (png_muldiv(&res, gamma, log, PNG_FP_1)) if (png_muldiv(&res, gamma, log, PNG_FP_1))
return png_exp16bit(res); return png_exp16bit(res);
# endif # endif
@ -1923,6 +1987,7 @@ png_gamma_correct(png_structp png_ptr, unsigned value, png_fixed_point gamma)
{ {
if (png_ptr->bit_depth == 8) if (png_ptr->bit_depth == 8)
return png_gamma_8bit_correct(value, gamma); return png_gamma_8bit_correct(value, gamma);
else else
return png_gamma_16bit_correct(value, gamma); return png_gamma_16bit_correct(value, gamma);
} }
@ -1984,9 +2049,11 @@ png_build_16bit_table(png_structp png_ptr, png_uint_16pp *ptable,
/* Inline the 'max' scaling operation: */ /* Inline the 'max' scaling operation: */
sub_table[j] = (png_uint_16)floor(65535*pow(ig/(double)max, sub_table[j] = (png_uint_16)floor(65535*pow(ig/(double)max,
gamma*.00001)+.5); gamma*.00001)+.5);
# else # else
if (shift) if (shift)
ig = (ig * 65535U + max_by_2)/max; ig = (ig * 65535U + max_by_2)/max;
sub_table[j] = png_gamma_16bit_correct(ig, gamma); sub_table[j] = png_gamma_16bit_correct(ig, gamma);
# endif # endif
} }
@ -1995,11 +2062,14 @@ png_build_16bit_table(png_structp png_ptr, png_uint_16pp *ptable,
{ {
/* We must still build a table, but do it the fast way. */ /* We must still build a table, but do it the fast way. */
unsigned j; unsigned j;
for (j = 0; j < 256; j++) for (j = 0; j < 256; j++)
{ {
png_uint_32 ig = (j << (8-shift)) + i; png_uint_32 ig = (j << (8-shift)) + i;
if (shift) if (shift)
ig = (ig * 65535U + max_by_2)/max; ig = (ig * 65535U + max_by_2)/max;
sub_table[j] = ig; sub_table[j] = ig;
} }
} }
@ -2050,8 +2120,10 @@ png_build_16to8_table(png_structp png_ptr, png_uint_16pp *ptable,
{ {
/* Find the corresponding maximum input value */ /* Find the corresponding maximum input value */
png_uint_16 out = i * 257U; /* 16 bit output value */ png_uint_16 out = i * 257U; /* 16 bit output value */
/* Find the boundary value in 16 bits: */ /* Find the boundary value in 16 bits: */
png_uint_16 bound = png_gamma_16bit_correct(out+128U, gamma); png_uint_16 bound = png_gamma_16bit_correct(out+128U, gamma);
/* Adjust (round) to (16-shift) bits: */ /* Adjust (round) to (16-shift) bits: */
bound = (bound * max + 32768)/65535; bound = (bound * max + 32768)/65535;
@ -2083,6 +2155,7 @@ png_build_8bit_table(png_structp png_ptr, png_bytepp ptable,
if (png_gamma_significant(gamma)) for (i=0; i<256; i++) if (png_gamma_significant(gamma)) for (i=0; i<256; i++)
table[i] = png_gamma_8bit_correct(i, gamma); table[i] = png_gamma_8bit_correct(i, gamma);
else for (i=0; i<245; ++i) else for (i=0; i<245; ++i)
table[i] = i; table[i] = i;
} }
@ -2153,6 +2226,7 @@ png_build_gamma_table(png_structp png_ptr, png_byte bit_depth)
*/ */
if (sig_bit > 0) if (sig_bit > 0)
shift = 16U - sig_bit; /* shift == insignificant bits */ shift = 16U - sig_bit; /* shift == insignificant bits */
else else
shift = 0; /* keep all 16 bits */ shift = 0; /* keep all 16 bits */
@ -2175,6 +2249,7 @@ png_build_gamma_table(png_structp png_ptr, png_byte bit_depth)
png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift, png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift,
png_ptr->screen_gamma > 0 ? png_product2(png_ptr->gamma, png_ptr->screen_gamma > 0 ? png_product2(png_ptr->gamma,
png_ptr->screen_gamma) : PNG_FP_1); png_ptr->screen_gamma) : PNG_FP_1);
else else
png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift, png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift,
png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->gamma, png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->gamma,
@ -2188,7 +2263,7 @@ png_build_gamma_table(png_structp png_ptr, png_byte bit_depth)
png_reciprocal(png_ptr->gamma)); png_reciprocal(png_ptr->gamma));
/* Notice that the '16 from 1' table should be full precision, however /* Notice that the '16 from 1' table should be full precision, however
* the lookup on this table still uses gamma_shift, os it can't be. * the lookup on this table still uses gamma_shift, so it can't be.
* TODO: fix this. * TODO: fix this.
*/ */
png_build_16bit_table(png_ptr, &png_ptr->gamma_16_from_1, shift, png_build_16bit_table(png_ptr, &png_ptr->gamma_16_from_1, shift,