< prev index next >

src/java.desktop/share/native/libsplashscreen/libpng/png.c

Print this page




  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /* png.c - location for general purpose libpng functions
  26  *
  27  * This file is available under and governed by the GNU General Public
  28  * License version 2 only, as published by the Free Software Foundation.
  29  * However, the following notice accompanied the original version of this
  30  * file and, per its terms, should not be removed:
  31  *
  32  * Last changed in libpng 1.6.19 [November 12, 2015]
  33  * Copyright (c) 1998-2002,2004,2006-2015 Glenn Randers-Pehrson
  34  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  35  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  36  *
  37  * This code is released under the libpng license.
  38  * For conditions of distribution and use, see the disclaimer
  39  * and license in png.h
  40  */
  41 
  42 #include "pngpriv.h"
  43 
  44 /* Generate a compiler error if there is an old png.h in the search path. */
  45 typedef png_libpng_version_1_6_23 Your_png_h_is_not_version_1_6_23;
  46 
  47 /* Tells libpng that we have already handled the first "num_bytes" bytes
  48  * of the PNG file signature.  If the PNG data is embedded into another
  49  * stream we can set num_bytes = 8 so that libpng will not attempt to read
  50  * or write any of the magic bytes before it starts on the IHDR.
  51  */
  52 
  53 #ifdef PNG_READ_SUPPORTED
  54 void PNGAPI
  55 png_set_sig_bytes(png_structrp png_ptr, int num_bytes)
  56 {
  57    unsigned int nb = (unsigned int)num_bytes;
  58 
  59    png_debug(1, "in png_set_sig_bytes");
  60 
  61    if (png_ptr == NULL)
  62       return;
  63 
  64    if (num_bytes < 0)
  65       nb = 0;


 469       info_ptr->free_me |= mask;
 470 
 471    else if (freer == PNG_USER_WILL_FREE_DATA)
 472       info_ptr->free_me &= ~mask;
 473 
 474    else
 475       png_error(png_ptr, "Unknown freer parameter in png_data_freer");
 476 }
 477 
 478 void PNGAPI
 479 png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask,
 480    int num)
 481 {
 482    png_debug(1, "in png_free_data");
 483 
 484    if (png_ptr == NULL || info_ptr == NULL)
 485       return;
 486 
 487 #ifdef PNG_TEXT_SUPPORTED
 488    /* Free text item num or (if num == -1) all text items */
 489    if (info_ptr->text != 0 &&
 490        ((mask & PNG_FREE_TEXT) & info_ptr->free_me) != 0)
 491    {
 492       if (num != -1)
 493       {
 494          png_free(png_ptr, info_ptr->text[num].key);
 495          info_ptr->text[num].key = NULL;
 496       }
 497 
 498       else
 499       {
 500          int i;
 501 
 502          for (i = 0; i < info_ptr->num_text; i++)
 503             png_free(png_ptr, info_ptr->text[i].key);
 504 
 505          png_free(png_ptr, info_ptr->text);
 506          info_ptr->text = NULL;
 507          info_ptr->num_text = 0;

 508       }
 509    }
 510 #endif
 511 
 512 #ifdef PNG_tRNS_SUPPORTED
 513    /* Free any tRNS entry */
 514    if (((mask & PNG_FREE_TRNS) & info_ptr->free_me) != 0)
 515    {
 516       info_ptr->valid &= ~PNG_INFO_tRNS;
 517       png_free(png_ptr, info_ptr->trans_alpha);
 518       info_ptr->trans_alpha = NULL;
 519       info_ptr->num_trans = 0;
 520    }
 521 #endif
 522 
 523 #ifdef PNG_sCAL_SUPPORTED
 524    /* Free any sCAL entry */
 525    if (((mask & PNG_FREE_SCAL) & info_ptr->free_me) != 0)
 526    {
 527       png_free(png_ptr, info_ptr->scal_s_width);


 552             info_ptr->pcal_params = NULL;
 553          }
 554       info_ptr->valid &= ~PNG_INFO_pCAL;
 555    }
 556 #endif
 557 
 558 #ifdef PNG_iCCP_SUPPORTED
 559    /* Free any profile entry */
 560    if (((mask & PNG_FREE_ICCP) & info_ptr->free_me) != 0)
 561    {
 562       png_free(png_ptr, info_ptr->iccp_name);
 563       png_free(png_ptr, info_ptr->iccp_profile);
 564       info_ptr->iccp_name = NULL;
 565       info_ptr->iccp_profile = NULL;
 566       info_ptr->valid &= ~PNG_INFO_iCCP;
 567    }
 568 #endif
 569 
 570 #ifdef PNG_sPLT_SUPPORTED
 571    /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
 572    if (info_ptr->splt_palettes != 0 &&
 573        ((mask & PNG_FREE_SPLT) & info_ptr->free_me) != 0)
 574    {
 575       if (num != -1)
 576       {
 577          png_free(png_ptr, info_ptr->splt_palettes[num].name);
 578          png_free(png_ptr, info_ptr->splt_palettes[num].entries);
 579          info_ptr->splt_palettes[num].name = NULL;
 580          info_ptr->splt_palettes[num].entries = NULL;
 581       }
 582 
 583       else
 584       {
 585          int i;
 586 
 587          for (i = 0; i < info_ptr->splt_palettes_num; i++)
 588          {
 589             png_free(png_ptr, info_ptr->splt_palettes[i].name);
 590             png_free(png_ptr, info_ptr->splt_palettes[i].entries);
 591          }
 592 
 593          png_free(png_ptr, info_ptr->splt_palettes);
 594          info_ptr->splt_palettes = NULL;
 595          info_ptr->splt_palettes_num = 0;
 596          info_ptr->valid &= ~PNG_INFO_sPLT;
 597       }
 598    }
 599 #endif
 600 
 601 #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
 602    if (info_ptr->unknown_chunks != 0 &&
 603        ((mask & PNG_FREE_UNKN) & info_ptr->free_me) != 0)
 604    {
 605       if (num != -1)
 606       {
 607           png_free(png_ptr, info_ptr->unknown_chunks[num].data);
 608           info_ptr->unknown_chunks[num].data = NULL;
 609       }
 610 
 611       else
 612       {
 613          int i;
 614 
 615          for (i = 0; i < info_ptr->unknown_chunks_num; i++)
 616             png_free(png_ptr, info_ptr->unknown_chunks[i].data);
 617 
 618          png_free(png_ptr, info_ptr->unknown_chunks);
 619          info_ptr->unknown_chunks = NULL;
 620          info_ptr->unknown_chunks_num = 0;
 621       }
 622    }


 628    {
 629       png_free(png_ptr, info_ptr->hist);
 630       info_ptr->hist = NULL;
 631       info_ptr->valid &= ~PNG_INFO_hIST;
 632    }
 633 #endif
 634 
 635    /* Free any PLTE entry that was internally allocated */
 636    if (((mask & PNG_FREE_PLTE) & info_ptr->free_me) != 0)
 637    {
 638       png_free(png_ptr, info_ptr->palette);
 639       info_ptr->palette = NULL;
 640       info_ptr->valid &= ~PNG_INFO_PLTE;
 641       info_ptr->num_palette = 0;
 642    }
 643 
 644 #ifdef PNG_INFO_IMAGE_SUPPORTED
 645    /* Free any image bits attached to the info structure */
 646    if (((mask & PNG_FREE_ROWS) & info_ptr->free_me) != 0)
 647    {
 648       if (info_ptr->row_pointers != 0)
 649       {
 650          png_uint_32 row;
 651          for (row = 0; row < info_ptr->height; row++)
 652             png_free(png_ptr, info_ptr->row_pointers[row]);
 653 
 654          png_free(png_ptr, info_ptr->row_pointers);
 655          info_ptr->row_pointers = NULL;
 656       }
 657       info_ptr->valid &= ~PNG_INFO_IDAT;
 658    }
 659 #endif
 660 
 661    if (num != -1)
 662       mask &= ~PNG_FREE_MUL;
 663 
 664    info_ptr->free_me &= ~mask;
 665 }
 666 #endif /* READ || WRITE */
 667 
 668 /* This function returns a pointer to the io_ptr associated with the user


 695       return;
 696 
 697    png_ptr->io_ptr = (png_voidp)fp;
 698 }
 699 #  endif
 700 
 701 #  ifdef PNG_SAVE_INT_32_SUPPORTED
 702 /* PNG signed integers are saved in 32-bit 2's complement format.  ANSI C-90
 703  * defines a cast of a signed integer to an unsigned integer either to preserve
 704  * the value, if it is positive, or to calculate:
 705  *
 706  *     (UNSIGNED_MAX+1) + integer
 707  *
 708  * Where UNSIGNED_MAX is the appropriate maximum unsigned value, so when the
 709  * negative integral value is added the result will be an unsigned value
 710  * correspnding to the 2's complement representation.
 711  */
 712 void PNGAPI
 713 png_save_int_32(png_bytep buf, png_int_32 i)
 714 {
 715    png_save_uint_32(buf, i);
 716 }
 717 #  endif
 718 
 719 #  ifdef PNG_TIME_RFC1123_SUPPORTED
 720 /* Convert the supplied time into an RFC 1123 string suitable for use in
 721  * a "Creation Time" or other text-based time string.
 722  */
 723 int PNGAPI
 724 png_convert_to_rfc1123_buffer(char out[29], png_const_timep ptime)
 725 {
 726    static PNG_CONST char short_months[12][4] =
 727         {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
 728          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
 729 
 730    if (out == NULL)
 731       return 0;
 732 
 733    if (ptime->year > 9999 /* RFC1123 limitation */ ||
 734        ptime->month == 0    ||  ptime->month > 12  ||
 735        ptime->day   == 0    ||  ptime->day   > 31  ||


 786       else
 787          return png_ptr->time_buffer;
 788    }
 789 
 790    return NULL;
 791 }
 792 #    endif /* LIBPNG_VER < 10700 */
 793 #  endif /* TIME_RFC1123 */
 794 
 795 #endif /* READ || WRITE */
 796 
 797 png_const_charp PNGAPI
 798 png_get_copyright(png_const_structrp png_ptr)
 799 {
 800    PNG_UNUSED(png_ptr)  /* Silence compiler warning about unused png_ptr */
 801 #ifdef PNG_STRING_COPYRIGHT
 802    return PNG_STRING_COPYRIGHT
 803 #else
 804 #  ifdef __STDC__
 805    return PNG_STRING_NEWLINE \
 806       "libpng version 1.6.23 - June 9, 2016" PNG_STRING_NEWLINE \
 807       "Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson" \
 808       PNG_STRING_NEWLINE \
 809       "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
 810       "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
 811       PNG_STRING_NEWLINE;
 812 #  else
 813    return "libpng version 1.6.23 - June 9, 2016\
 814       Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson\
 815       Copyright (c) 1996-1997 Andreas Dilger\
 816       Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.";
 817 #  endif
 818 #endif
 819 }
 820 
 821 /* The following return the library version as a short string in the
 822  * format 1.0.0 through 99.99.99zz.  To get the version of *.h files
 823  * used with your application, print out PNG_LIBPNG_VER_STRING, which
 824  * is defined in png.h.
 825  * Note: now there is no difference between png_get_libpng_ver() and
 826  * png_get_header_ver().  Due to the version_nn_nn_nn typedef guard,
 827  * it is guaranteed that png.c uses the correct version of png.h.
 828  */
 829 png_const_charp PNGAPI
 830 png_get_libpng_ver(png_const_structrp png_ptr)
 831 {
 832    /* Version of *.c files used when building libpng */
 833    return png_get_header_ver(png_ptr);
 834 }


1942    colorspace->gamma = PNG_GAMMA_sRGB_INVERSE;
1943    colorspace->flags |= PNG_COLORSPACE_HAVE_GAMMA;
1944 
1945    /* Finally record that we have an sRGB profile */
1946    colorspace->flags |=
1947       (PNG_COLORSPACE_MATCHES_sRGB|PNG_COLORSPACE_FROM_sRGB);
1948 
1949    return 1; /* set */
1950 }
1951 #endif /* sRGB */
1952 
1953 #ifdef PNG_iCCP_SUPPORTED
1954 /* Encoded value of D50 as an ICC XYZNumber.  From the ICC 2010 spec the value
1955  * is XYZ(0.9642,1.0,0.8249), which scales to:
1956  *
1957  *    (63189.8112, 65536, 54060.6464)
1958  */
1959 static const png_byte D50_nCIEXYZ[12] =
1960    { 0x00, 0x00, 0xf6, 0xd6, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d };
1961 
1962 int /* PRIVATE */
1963 png_icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace,
1964    png_const_charp name, png_uint_32 profile_length)
1965 {
1966    if (profile_length < 132)
1967       return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
1968          "too short");
1969 
1970    return 1;
1971 }
1972 


































1973 int /* PRIVATE */
1974 png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace,
1975    png_const_charp name, png_uint_32 profile_length,
1976    png_const_bytep profile/* first 132 bytes only */, int color_type)
1977 {
1978    png_uint_32 temp;
1979 
1980    /* Length check; this cannot be ignored in this code because profile_length
1981     * is used later to check the tag table, so even if the profile seems over
1982     * long profile_length from the caller must be correct.  The caller can fix
1983     * this up on read or write by just passing in the profile header length.
1984     */
1985    temp = png_get_uint_32(profile);
1986    if (temp != profile_length)
1987       return png_icc_profile_error(png_ptr, colorspace, name, temp,
1988          "length does not match profile");
1989 
1990    temp = (png_uint_32) (*(profile+8));
1991    if (temp > 3 && (profile_length & 3))
1992       return png_icc_profile_error(png_ptr, colorspace, name, profile_length,


2365                   return 1+png_sRGB_checks[i].is_broken;
2366                }
2367             }
2368 
2369 # if PNG_sRGB_PROFILE_CHECKS > 0
2370          /* The signature matched, but the profile had been changed in some
2371           * way.  This probably indicates a data error or uninformed hacking.
2372           * Fall through to "no match".
2373           */
2374          png_chunk_report(png_ptr,
2375              "Not recognizing known sRGB profile that has been edited",
2376              PNG_CHUNK_WARNING);
2377          break;
2378 # endif
2379          }
2380       }
2381    }
2382 
2383    return 0; /* no match */
2384 }
2385 #endif /* PNG_sRGB_PROFILE_CHECKS >= 0 */
2386 
2387 void /* PRIVATE */
2388 png_icc_set_sRGB(png_const_structrp png_ptr,
2389    png_colorspacerp colorspace, png_const_bytep profile, uLong adler)
2390 {
2391    /* Is this profile one of the known ICC sRGB profiles?  If it is, just set
2392     * the sRGB information.
2393     */
2394 #if PNG_sRGB_PROFILE_CHECKS >= 0
2395    if (png_compare_ICC_profile_with_sRGB(png_ptr, profile, adler) != 0)
2396 #endif
2397       (void)png_colorspace_set_sRGB(png_ptr, colorspace,
2398          (int)/*already checked*/png_get_uint_32(profile+64));
2399 }

2400 #endif /* sRGB */
2401 
2402 int /* PRIVATE */
2403 png_colorspace_set_ICC(png_const_structrp png_ptr, png_colorspacerp colorspace,
2404    png_const_charp name, png_uint_32 profile_length, png_const_bytep profile,
2405    int color_type)
2406 {
2407    if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
2408       return 0;
2409 
2410    if (png_icc_check_length(png_ptr, colorspace, name, profile_length) != 0 &&
2411        png_icc_check_header(png_ptr, colorspace, name, profile_length, profile,
2412           color_type) != 0 &&
2413        png_icc_check_tag_table(png_ptr, colorspace, name, profile_length,
2414           profile) != 0)
2415    {
2416 #     ifdef PNG_sRGB_SUPPORTED
2417          /* If no sRGB support, don't try storing sRGB information */
2418          png_icc_set_sRGB(png_ptr, colorspace, profile, 0);
2419 #     endif
2420       return 1;
2421    }
2422 
2423    /* Failure case */
2424    return 0;
2425 }
2426 #endif /* iCCP */
2427 
2428 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2429 void /* PRIVATE */
2430 png_colorspace_set_rgb_coefficients(png_structrp png_ptr)
2431 {
2432    /* Set the rgb_to_gray coefficients from the colorspace. */
2433    if (png_ptr->rgb_to_gray_coefficients_set == 0 &&
2434       (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)
2435    {
2436       /* png_set_background has not been called, get the coefficients from the Y


2508 png_check_IHDR(png_const_structrp png_ptr,
2509    png_uint_32 width, png_uint_32 height, int bit_depth,
2510    int color_type, int interlace_type, int compression_type,
2511    int filter_type)
2512 {
2513    int error = 0;
2514 
2515    /* Check for width and height valid values */
2516    if (width == 0)
2517    {
2518       png_warning(png_ptr, "Image width is zero in IHDR");
2519       error = 1;
2520    }
2521 
2522    if (width > PNG_UINT_31_MAX)
2523    {
2524       png_warning(png_ptr, "Invalid image width in IHDR");
2525       error = 1;
2526    }
2527 
2528    if (png_gt(((width + 7) & (~7)),
2529        ((PNG_SIZE_MAX
2530            - 48        /* big_row_buf hack */
2531            - 1)        /* filter byte */
2532            / 8)        /* 8-byte RGBA pixels */
2533            - 1))       /* extra max_pixel_depth pad */
2534    {
2535       /* The size of the row must be within the limits of this architecture.
2536        * Because the read code can perform arbitrary transformations the
2537        * maximum size is checked here.  Because the code in png_read_start_row
2538        * adds extra space "for safety's sake" in several places a conservative
2539        * limit is used here.
2540        *
2541        * NOTE: it would be far better to check the size that is actually used,
2542        * but the effect in the real world is minor and the changes are more
2543        * extensive, therefore much more dangerous and much more difficult to
2544        * write in a way that avoids compiler warnings.
2545        */
2546       png_warning(png_ptr, "Image width is too large for this architecture");
2547       error = 1;
2548    }


2919           * test on DBL_MAX above.
2920           */
2921          fp /= base;
2922          while (fp >= 1) fp /= 10, ++exp_b10;
2923 
2924          /* Because of the code above fp may, at this point, be
2925           * less than .1, this is ok because the code below can
2926           * handle the leading zeros this generates, so no attempt
2927           * is made to correct that here.
2928           */
2929 
2930          {
2931             unsigned int czero, clead, cdigits;
2932             char exponent[10];
2933 
2934             /* Allow up to two leading zeros - this will not lengthen
2935              * the number compared to using E-n.
2936              */
2937             if (exp_b10 < 0 && exp_b10 > -3) /* PLUS 3 TOTAL 4 */
2938             {
2939                czero = -exp_b10; /* PLUS 2 digits: TOTAL 3 */
2940                exp_b10 = 0;      /* Dot added below before first output. */
2941             }
2942             else
2943                czero = 0;    /* No zeros to add */
2944 
2945             /* Generate the digit list, stripping trailing zeros and
2946              * inserting a '.' before a digit if the exponent is 0.
2947              */
2948             clead = czero; /* Count of leading zeros */
2949             cdigits = 0;   /* Count of digits in list. */
2950 
2951             do
2952             {
2953                double d;
2954 
2955                fp *= 10;
2956                /* Use modf here, not floor and subtract, so that
2957                 * the separation is done in one step.  At the end
2958                 * of the loop don't break the number into parts so
2959                 * that the final digit is rounded.


3097             /* Here if an exponent is required, adjust size for
3098              * the digits we output but did not count.  The total
3099              * digit output here so far is at most 1+precision - no
3100              * decimal point and no leading or trailing zeros have
3101              * been output.
3102              */
3103             size -= cdigits;
3104 
3105             *ascii++ = 69, --size;    /* 'E': PLUS 1 TOTAL 2+precision */
3106 
3107             /* The following use of an unsigned temporary avoids ambiguities in
3108              * the signed arithmetic on exp_b10 and permits GCC at least to do
3109              * better optimization.
3110              */
3111             {
3112                unsigned int uexp_b10;
3113 
3114                if (exp_b10 < 0)
3115                {
3116                   *ascii++ = 45, --size; /* '-': PLUS 1 TOTAL 3+precision */
3117                   uexp_b10 = -exp_b10;
3118                }
3119 
3120                else
3121                   uexp_b10 = exp_b10;
3122 
3123                cdigits = 0;
3124 
3125                while (uexp_b10 > 0)
3126                {
3127                   exponent[cdigits++] = (char)(48 + uexp_b10 % 10);
3128                   uexp_b10 /= 10;
3129                }
3130             }
3131 
3132             /* Need another size check here for the exponent digits, so
3133              * this need not be considered above.
3134              */
3135             if (size > cdigits)
3136             {
3137                while (cdigits > 0) *ascii++ = exponent[--cdigits];
3138 
3139                *ascii = 0;
3140 
3141                return;


3163 }
3164 
3165 #  endif /* FLOATING_POINT */
3166 
3167 #  ifdef PNG_FIXED_POINT_SUPPORTED
3168 /* Function to format a fixed point value in ASCII.
3169  */
3170 void /* PRIVATE */
3171 png_ascii_from_fixed(png_const_structrp png_ptr, png_charp ascii,
3172     png_size_t size, png_fixed_point fp)
3173 {
3174    /* Require space for 10 decimal digits, a decimal point, a minus sign and a
3175     * trailing \0, 13 characters:
3176     */
3177    if (size > 12)
3178    {
3179       png_uint_32 num;
3180 
3181       /* Avoid overflow here on the minimum integer. */
3182       if (fp < 0)
3183          *ascii++ = 45, num = -fp;
3184       else
3185          num = fp;
3186 
3187       if (num <= 0x80000000) /* else overflowed */
3188       {
3189          unsigned int ndigits = 0, first = 16 /* flag value */;
3190          char digits[10];
3191 
3192          while (num)
3193          {
3194             /* Split the low digit off num: */
3195             unsigned int tmp = num/10;
3196             num -= tmp*10;
3197             digits[ndigits++] = (char)(48 + num);
3198             /* Record the first non-zero digit, note that this is a number
3199              * starting at 1, it's not actually the array index.
3200              */
3201             if (first == 16 && num > 0)
3202                first = ndigits;
3203             num = tmp;
3204          }
3205 


4103       }
4104    png_free(png_ptr, png_ptr->gamma_16_to_1);
4105    png_ptr->gamma_16_to_1 = NULL;
4106    }
4107 #endif /* 16BIT */
4108 #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
4109 }
4110 
4111 /* We build the 8- or 16-bit gamma tables here.  Note that for 16-bit
4112  * tables, we don't make a full table if we are reducing to 8-bit in
4113  * the future.  Note also how the gamma_16 tables are segmented so that
4114  * we don't need to allocate > 64K chunks for a full 16-bit table.
4115  */
4116 void /* PRIVATE */
4117 png_build_gamma_table(png_structrp png_ptr, int bit_depth)
4118 {
4119   png_debug(1, "in png_build_gamma_table");
4120 
4121   /* Remove any existing table; this copes with multiple calls to
4122    * png_read_update_info.  The warning is because building the gamma tables
4123    * multiple times is a performance hit - it's harmless but the ability to call
4124    * png_read_update_info() multiple times is new in 1.5.6 so it seems sensible
4125    * to warn if the app introduces such a hit.
4126    */
4127   if (png_ptr->gamma_table != NULL || png_ptr->gamma_16_table != NULL)
4128   {
4129     png_warning(png_ptr, "gamma table being rebuilt");
4130     png_destroy_gamma_table(png_ptr);
4131   }
4132 
4133   if (bit_depth <= 8)
4134   {
4135      png_build_8bit_table(png_ptr, &png_ptr->gamma_table,
4136          png_ptr->screen_gamma > 0 ?  png_reciprocal2(png_ptr->colorspace.gamma,

4137          png_ptr->screen_gamma) : PNG_FP_1);
4138 
4139 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4140    defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
4141    defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4142      if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0)
4143      {
4144         png_build_8bit_table(png_ptr, &png_ptr->gamma_to_1,
4145             png_reciprocal(png_ptr->colorspace.gamma));
4146 
4147         png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1,
4148             png_ptr->screen_gamma > 0 ?  png_reciprocal(png_ptr->screen_gamma) :

4149             png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */);
4150      }
4151 #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
4152   }
4153 #ifdef PNG_16BIT_SUPPORTED
4154   else
4155   {
4156      png_byte shift, sig_bit;
4157 
4158      if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
4159      {
4160         sig_bit = png_ptr->sig_bit.red;
4161 
4162         if (png_ptr->sig_bit.green > sig_bit)
4163            sig_bit = png_ptr->sig_bit.green;
4164 
4165         if (png_ptr->sig_bit.blue > sig_bit)
4166            sig_bit = png_ptr->sig_bit.blue;
4167      }
4168      else
4169         sig_bit = png_ptr->sig_bit.gray;
4170 
4171      /* 16-bit gamma code uses this equation:
4172       *
4173       *   ov = table[(iv & 0xff) >> gamma_shift][iv >> 8]
4174       *
4175       * Where 'iv' is the input color value and 'ov' is the output value -
4176       * pow(iv, gamma).
4177       *
4178       * Thus the gamma table consists of up to 256 256-entry tables.  The table
4179       * is selected by the (8-gamma_shift) most significant of the low 8 bits of
4180       * the color value then indexed by the upper 8 bits:
4181       *
4182       *   table[low bits][high 8 bits]
4183       *
4184       * So the table 'n' corresponds to all those 'iv' of:
4185       *
4186       *   <all high 8-bit values><n << gamma_shift>..<(n+1 << gamma_shift)-1>
4187       *
4188       */
4189      if (sig_bit > 0 && sig_bit < 16U)
4190         /* shift == insignificant bits */
4191         shift = (png_byte)((16U - sig_bit) & 0xff);
4192 
4193      else
4194         shift = 0; /* keep all 16 bits */
4195 
4196      if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0)
4197      {
4198         /* PNG_MAX_GAMMA_8 is the number of bits to keep - effectively
4199          * the significant bits in the *input* when the output will
4200          * eventually be 8 bits.  By default it is 11.
4201          */
4202         if (shift < (16U - PNG_MAX_GAMMA_8))
4203            shift = (16U - PNG_MAX_GAMMA_8);
4204      }
4205 
4206      if (shift > 8U)
4207         shift = 8U; /* Guarantees at least one table! */
4208 
4209      png_ptr->gamma_shift = shift;
4210 
4211      /* NOTE: prior to 1.5.4 this test used to include PNG_BACKGROUND (now
4212       * PNG_COMPOSE).  This effectively smashed the background calculation for
4213       * 16-bit output because the 8-bit table assumes the result will be reduced
4214       * to 8 bits.
4215       */
4216      if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0)
4217          png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift,
4218          png_ptr->screen_gamma > 0 ? png_product2(png_ptr->colorspace.gamma,
4219          png_ptr->screen_gamma) : PNG_FP_1);
4220 
4221      else
4222          png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift,
4223          png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma,
4224          png_ptr->screen_gamma) : PNG_FP_1);
4225 
4226 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4227    defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
4228    defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4229      if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0)
4230      {
4231         png_build_16bit_table(png_ptr, &png_ptr->gamma_16_to_1, shift,
4232             png_reciprocal(png_ptr->colorspace.gamma));
4233 
4234         /* Notice that the '16 from 1' table should be full precision, however


4236          * TODO: fix this.
4237          */
4238         png_build_16bit_table(png_ptr, &png_ptr->gamma_16_from_1, shift,
4239             png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) :
4240             png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */);
4241      }
4242 #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
4243   }
4244 #endif /* 16BIT */
4245 }
4246 #endif /* READ_GAMMA */
4247 
4248 /* HARDWARE OR SOFTWARE OPTION SUPPORT */
4249 #ifdef PNG_SET_OPTION_SUPPORTED
4250 int PNGAPI
4251 png_set_option(png_structrp png_ptr, int option, int onoff)
4252 {
4253    if (png_ptr != NULL && option >= 0 && option < PNG_OPTION_NEXT &&
4254       (option & 1) == 0)
4255    {
4256       int mask = 3 << option;
4257       int setting = (2 + (onoff != 0)) << option;
4258       int current = png_ptr->options;
4259 
4260       png_ptr->options = (png_byte)(((current & ~mask) | setting) & 0xff);
4261 
4262       return (current & mask) >> option;
4263    }
4264 
4265    return PNG_OPTION_INVALID;
4266 }
4267 #endif
4268 
4269 /* sRGB support */
4270 #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
4271    defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
4272 /* sRGB conversion tables; these are machine generated with the code in
4273  * contrib/tools/makesRGB.c.  The actual sRGB transfer curve defined in the
4274  * specification (see the article at http://en.wikipedia.org/wiki/SRGB)
4275  * is used, not the gamma=1/2.2 approximation use elsewhere in libpng.
4276  * The sRGB to linear table is exact (to the nearest 16-bit linear fraction).
4277  * The inverse (linear to sRGB) table has accuracies as follows:
4278  *
4279  * For all possible (255*65535+1) input values:
4280  *




  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /* png.c - location for general purpose libpng functions
  26  *
  27  * This file is available under and governed by the GNU General Public
  28  * License version 2 only, as published by the Free Software Foundation.
  29  * However, the following notice accompanied the original version of this
  30  * file and, per its terms, should not be removed:
  31  *
  32  * Last changed in libpng 1.6.28 [January 5, 2017]
  33  * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
  34  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  35  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  36  *
  37  * This code is released under the libpng license.
  38  * For conditions of distribution and use, see the disclaimer
  39  * and license in png.h
  40  */
  41 
  42 #include "pngpriv.h"
  43 
  44 /* Generate a compiler error if there is an old png.h in the search path. */
  45 typedef png_libpng_version_1_6_28 Your_png_h_is_not_version_1_6_28;
  46 
  47 /* Tells libpng that we have already handled the first "num_bytes" bytes
  48  * of the PNG file signature.  If the PNG data is embedded into another
  49  * stream we can set num_bytes = 8 so that libpng will not attempt to read
  50  * or write any of the magic bytes before it starts on the IHDR.
  51  */
  52 
  53 #ifdef PNG_READ_SUPPORTED
  54 void PNGAPI
  55 png_set_sig_bytes(png_structrp png_ptr, int num_bytes)
  56 {
  57    unsigned int nb = (unsigned int)num_bytes;
  58 
  59    png_debug(1, "in png_set_sig_bytes");
  60 
  61    if (png_ptr == NULL)
  62       return;
  63 
  64    if (num_bytes < 0)
  65       nb = 0;


 469       info_ptr->free_me |= mask;
 470 
 471    else if (freer == PNG_USER_WILL_FREE_DATA)
 472       info_ptr->free_me &= ~mask;
 473 
 474    else
 475       png_error(png_ptr, "Unknown freer parameter in png_data_freer");
 476 }
 477 
 478 void PNGAPI
 479 png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask,
 480     int num)
 481 {
 482    png_debug(1, "in png_free_data");
 483 
 484    if (png_ptr == NULL || info_ptr == NULL)
 485       return;
 486 
 487 #ifdef PNG_TEXT_SUPPORTED
 488    /* Free text item num or (if num == -1) all text items */
 489    if (info_ptr->text != NULL &&
 490        ((mask & PNG_FREE_TEXT) & info_ptr->free_me) != 0)
 491    {
 492       if (num != -1)
 493       {
 494          png_free(png_ptr, info_ptr->text[num].key);
 495          info_ptr->text[num].key = NULL;
 496       }
 497 
 498       else
 499       {
 500          int i;
 501 
 502          for (i = 0; i < info_ptr->num_text; i++)
 503             png_free(png_ptr, info_ptr->text[i].key);
 504 
 505          png_free(png_ptr, info_ptr->text);
 506          info_ptr->text = NULL;
 507          info_ptr->num_text = 0;
 508          info_ptr->max_text = 0;
 509       }
 510    }
 511 #endif
 512 
 513 #ifdef PNG_tRNS_SUPPORTED
 514    /* Free any tRNS entry */
 515    if (((mask & PNG_FREE_TRNS) & info_ptr->free_me) != 0)
 516    {
 517       info_ptr->valid &= ~PNG_INFO_tRNS;
 518       png_free(png_ptr, info_ptr->trans_alpha);
 519       info_ptr->trans_alpha = NULL;
 520       info_ptr->num_trans = 0;
 521    }
 522 #endif
 523 
 524 #ifdef PNG_sCAL_SUPPORTED
 525    /* Free any sCAL entry */
 526    if (((mask & PNG_FREE_SCAL) & info_ptr->free_me) != 0)
 527    {
 528       png_free(png_ptr, info_ptr->scal_s_width);


 553             info_ptr->pcal_params = NULL;
 554          }
 555       info_ptr->valid &= ~PNG_INFO_pCAL;
 556    }
 557 #endif
 558 
 559 #ifdef PNG_iCCP_SUPPORTED
 560    /* Free any profile entry */
 561    if (((mask & PNG_FREE_ICCP) & info_ptr->free_me) != 0)
 562    {
 563       png_free(png_ptr, info_ptr->iccp_name);
 564       png_free(png_ptr, info_ptr->iccp_profile);
 565       info_ptr->iccp_name = NULL;
 566       info_ptr->iccp_profile = NULL;
 567       info_ptr->valid &= ~PNG_INFO_iCCP;
 568    }
 569 #endif
 570 
 571 #ifdef PNG_sPLT_SUPPORTED
 572    /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
 573    if (info_ptr->splt_palettes != NULL &&
 574        ((mask & PNG_FREE_SPLT) & info_ptr->free_me) != 0)
 575    {
 576       if (num != -1)
 577       {
 578          png_free(png_ptr, info_ptr->splt_palettes[num].name);
 579          png_free(png_ptr, info_ptr->splt_palettes[num].entries);
 580          info_ptr->splt_palettes[num].name = NULL;
 581          info_ptr->splt_palettes[num].entries = NULL;
 582       }
 583 
 584       else
 585       {
 586          int i;
 587 
 588          for (i = 0; i < info_ptr->splt_palettes_num; i++)
 589          {
 590             png_free(png_ptr, info_ptr->splt_palettes[i].name);
 591             png_free(png_ptr, info_ptr->splt_palettes[i].entries);
 592          }
 593 
 594          png_free(png_ptr, info_ptr->splt_palettes);
 595          info_ptr->splt_palettes = NULL;
 596          info_ptr->splt_palettes_num = 0;
 597          info_ptr->valid &= ~PNG_INFO_sPLT;
 598       }
 599    }
 600 #endif
 601 
 602 #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
 603    if (info_ptr->unknown_chunks != NULL &&
 604        ((mask & PNG_FREE_UNKN) & info_ptr->free_me) != 0)
 605    {
 606       if (num != -1)
 607       {
 608           png_free(png_ptr, info_ptr->unknown_chunks[num].data);
 609           info_ptr->unknown_chunks[num].data = NULL;
 610       }
 611 
 612       else
 613       {
 614          int i;
 615 
 616          for (i = 0; i < info_ptr->unknown_chunks_num; i++)
 617             png_free(png_ptr, info_ptr->unknown_chunks[i].data);
 618 
 619          png_free(png_ptr, info_ptr->unknown_chunks);
 620          info_ptr->unknown_chunks = NULL;
 621          info_ptr->unknown_chunks_num = 0;
 622       }
 623    }


 629    {
 630       png_free(png_ptr, info_ptr->hist);
 631       info_ptr->hist = NULL;
 632       info_ptr->valid &= ~PNG_INFO_hIST;
 633    }
 634 #endif
 635 
 636    /* Free any PLTE entry that was internally allocated */
 637    if (((mask & PNG_FREE_PLTE) & info_ptr->free_me) != 0)
 638    {
 639       png_free(png_ptr, info_ptr->palette);
 640       info_ptr->palette = NULL;
 641       info_ptr->valid &= ~PNG_INFO_PLTE;
 642       info_ptr->num_palette = 0;
 643    }
 644 
 645 #ifdef PNG_INFO_IMAGE_SUPPORTED
 646    /* Free any image bits attached to the info structure */
 647    if (((mask & PNG_FREE_ROWS) & info_ptr->free_me) != 0)
 648    {
 649       if (info_ptr->row_pointers != NULL)
 650       {
 651          png_uint_32 row;
 652          for (row = 0; row < info_ptr->height; row++)
 653             png_free(png_ptr, info_ptr->row_pointers[row]);
 654 
 655          png_free(png_ptr, info_ptr->row_pointers);
 656          info_ptr->row_pointers = NULL;
 657       }
 658       info_ptr->valid &= ~PNG_INFO_IDAT;
 659    }
 660 #endif
 661 
 662    if (num != -1)
 663       mask &= ~PNG_FREE_MUL;
 664 
 665    info_ptr->free_me &= ~mask;
 666 }
 667 #endif /* READ || WRITE */
 668 
 669 /* This function returns a pointer to the io_ptr associated with the user


 696       return;
 697 
 698    png_ptr->io_ptr = (png_voidp)fp;
 699 }
 700 #  endif
 701 
 702 #  ifdef PNG_SAVE_INT_32_SUPPORTED
 703 /* PNG signed integers are saved in 32-bit 2's complement format.  ANSI C-90
 704  * defines a cast of a signed integer to an unsigned integer either to preserve
 705  * the value, if it is positive, or to calculate:
 706  *
 707  *     (UNSIGNED_MAX+1) + integer
 708  *
 709  * Where UNSIGNED_MAX is the appropriate maximum unsigned value, so when the
 710  * negative integral value is added the result will be an unsigned value
 711  * correspnding to the 2's complement representation.
 712  */
 713 void PNGAPI
 714 png_save_int_32(png_bytep buf, png_int_32 i)
 715 {
 716    png_save_uint_32(buf, (png_uint_32)i);
 717 }
 718 #  endif
 719 
 720 #  ifdef PNG_TIME_RFC1123_SUPPORTED
 721 /* Convert the supplied time into an RFC 1123 string suitable for use in
 722  * a "Creation Time" or other text-based time string.
 723  */
 724 int PNGAPI
 725 png_convert_to_rfc1123_buffer(char out[29], png_const_timep ptime)
 726 {
 727    static PNG_CONST char short_months[12][4] =
 728         {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
 729          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
 730 
 731    if (out == NULL)
 732       return 0;
 733 
 734    if (ptime->year > 9999 /* RFC1123 limitation */ ||
 735        ptime->month == 0    ||  ptime->month > 12  ||
 736        ptime->day   == 0    ||  ptime->day   > 31  ||


 787       else
 788          return png_ptr->time_buffer;
 789    }
 790 
 791    return NULL;
 792 }
 793 #    endif /* LIBPNG_VER < 10700 */
 794 #  endif /* TIME_RFC1123 */
 795 
 796 #endif /* READ || WRITE */
 797 
 798 png_const_charp PNGAPI
 799 png_get_copyright(png_const_structrp png_ptr)
 800 {
 801    PNG_UNUSED(png_ptr)  /* Silence compiler warning about unused png_ptr */
 802 #ifdef PNG_STRING_COPYRIGHT
 803    return PNG_STRING_COPYRIGHT
 804 #else
 805 #  ifdef __STDC__
 806    return PNG_STRING_NEWLINE \
 807       "libpng version 1.6.28 - January 5, 2017" PNG_STRING_NEWLINE \
 808       "Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson" \
 809       PNG_STRING_NEWLINE \
 810       "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
 811       "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
 812       PNG_STRING_NEWLINE;
 813 #  else
 814    return "libpng version 1.6.28 - January 5, 2017\
 815       Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson\
 816       Copyright (c) 1996-1997 Andreas Dilger\
 817       Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.";
 818 #  endif
 819 #endif
 820 }
 821 
 822 /* The following return the library version as a short string in the
 823  * format 1.0.0 through 99.99.99zz.  To get the version of *.h files
 824  * used with your application, print out PNG_LIBPNG_VER_STRING, which
 825  * is defined in png.h.
 826  * Note: now there is no difference between png_get_libpng_ver() and
 827  * png_get_header_ver().  Due to the version_nn_nn_nn typedef guard,
 828  * it is guaranteed that png.c uses the correct version of png.h.
 829  */
 830 png_const_charp PNGAPI
 831 png_get_libpng_ver(png_const_structrp png_ptr)
 832 {
 833    /* Version of *.c files used when building libpng */
 834    return png_get_header_ver(png_ptr);
 835 }


1943    colorspace->gamma = PNG_GAMMA_sRGB_INVERSE;
1944    colorspace->flags |= PNG_COLORSPACE_HAVE_GAMMA;
1945 
1946    /* Finally record that we have an sRGB profile */
1947    colorspace->flags |=
1948       (PNG_COLORSPACE_MATCHES_sRGB|PNG_COLORSPACE_FROM_sRGB);
1949 
1950    return 1; /* set */
1951 }
1952 #endif /* sRGB */
1953 
1954 #ifdef PNG_iCCP_SUPPORTED
1955 /* Encoded value of D50 as an ICC XYZNumber.  From the ICC 2010 spec the value
1956  * is XYZ(0.9642,1.0,0.8249), which scales to:
1957  *
1958  *    (63189.8112, 65536, 54060.6464)
1959  */
1960 static const png_byte D50_nCIEXYZ[12] =
1961    { 0x00, 0x00, 0xf6, 0xd6, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d };
1962 
1963 static int /* bool */
1964 icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace,
1965     png_const_charp name, png_uint_32 profile_length)
1966 {
1967    if (profile_length < 132)
1968       return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
1969           "too short");
1970 
1971    return 1;
1972 }
1973 
1974 #ifdef PNG_READ_iCCP_SUPPORTED
1975 int /* PRIVATE */
1976 png_icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace,
1977     png_const_charp name, png_uint_32 profile_length)
1978 {
1979    if (!icc_check_length(png_ptr, colorspace, name, profile_length))
1980       return 0;
1981 
1982    /* This needs to be here because the 'normal' check is in
1983     * png_decompress_chunk, yet this happens after the attempt to
1984     * png_malloc_base the required data.  We only need this on read; on write
1985     * the caller supplies the profile buffer so libpng doesn't allocate it.  See
1986     * the call to icc_check_length below (the write case).
1987     */
1988 #  ifdef PNG_SET_USER_LIMITS_SUPPORTED
1989       else if (png_ptr->user_chunk_malloc_max > 0 &&
1990                png_ptr->user_chunk_malloc_max < profile_length)
1991          return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
1992              "exceeds application limits");
1993 #  elif PNG_USER_CHUNK_MALLOC_MAX > 0
1994       else if (PNG_USER_CHUNK_MALLOC_MAX < profile_length)
1995          return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
1996              "exceeds libpng limits");
1997 #  else /* !SET_USER_LIMITS */
1998       /* This will get compiled out on all 32-bit and better systems. */
1999       else if (PNG_SIZE_MAX < profile_length)
2000          return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
2001              "exceeds system limits");
2002 #  endif /* !SET_USER_LIMITS */
2003 
2004    return 1;
2005 }
2006 #endif /* READ_iCCP */
2007 
2008 int /* PRIVATE */
2009 png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace,
2010     png_const_charp name, png_uint_32 profile_length,
2011     png_const_bytep profile/* first 132 bytes only */, int color_type)
2012 {
2013    png_uint_32 temp;
2014 
2015    /* Length check; this cannot be ignored in this code because profile_length
2016     * is used later to check the tag table, so even if the profile seems over
2017     * long profile_length from the caller must be correct.  The caller can fix
2018     * this up on read or write by just passing in the profile header length.
2019     */
2020    temp = png_get_uint_32(profile);
2021    if (temp != profile_length)
2022       return png_icc_profile_error(png_ptr, colorspace, name, temp,
2023           "length does not match profile");
2024 
2025    temp = (png_uint_32) (*(profile+8));
2026    if (temp > 3 && (profile_length & 3))
2027       return png_icc_profile_error(png_ptr, colorspace, name, profile_length,


2400                   return 1+png_sRGB_checks[i].is_broken;
2401                }
2402             }
2403 
2404 # if PNG_sRGB_PROFILE_CHECKS > 0
2405          /* The signature matched, but the profile had been changed in some
2406           * way.  This probably indicates a data error or uninformed hacking.
2407           * Fall through to "no match".
2408           */
2409          png_chunk_report(png_ptr,
2410              "Not recognizing known sRGB profile that has been edited",
2411              PNG_CHUNK_WARNING);
2412          break;
2413 # endif
2414          }
2415       }
2416    }
2417 
2418    return 0; /* no match */
2419 }

2420 
2421 void /* PRIVATE */
2422 png_icc_set_sRGB(png_const_structrp png_ptr,
2423     png_colorspacerp colorspace, png_const_bytep profile, uLong adler)
2424 {
2425    /* Is this profile one of the known ICC sRGB profiles?  If it is, just set
2426     * the sRGB information.
2427     */

2428    if (png_compare_ICC_profile_with_sRGB(png_ptr, profile, adler) != 0)

2429       (void)png_colorspace_set_sRGB(png_ptr, colorspace,
2430          (int)/*already checked*/png_get_uint_32(profile+64));
2431 }
2432 #endif /* PNG_sRGB_PROFILE_CHECKS >= 0 */
2433 #endif /* sRGB */
2434 
2435 int /* PRIVATE */
2436 png_colorspace_set_ICC(png_const_structrp png_ptr, png_colorspacerp colorspace,
2437     png_const_charp name, png_uint_32 profile_length, png_const_bytep profile,
2438     int color_type)
2439 {
2440    if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
2441       return 0;
2442 
2443    if (icc_check_length(png_ptr, colorspace, name, profile_length) != 0 &&
2444        png_icc_check_header(png_ptr, colorspace, name, profile_length, profile,
2445            color_type) != 0 &&
2446        png_icc_check_tag_table(png_ptr, colorspace, name, profile_length,
2447            profile) != 0)
2448    {
2449 #     if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
2450          /* If no sRGB support, don't try storing sRGB information */
2451          png_icc_set_sRGB(png_ptr, colorspace, profile, 0);
2452 #     endif
2453       return 1;
2454    }
2455 
2456    /* Failure case */
2457    return 0;
2458 }
2459 #endif /* iCCP */
2460 
2461 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2462 void /* PRIVATE */
2463 png_colorspace_set_rgb_coefficients(png_structrp png_ptr)
2464 {
2465    /* Set the rgb_to_gray coefficients from the colorspace. */
2466    if (png_ptr->rgb_to_gray_coefficients_set == 0 &&
2467       (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)
2468    {
2469       /* png_set_background has not been called, get the coefficients from the Y


2541 png_check_IHDR(png_const_structrp png_ptr,
2542     png_uint_32 width, png_uint_32 height, int bit_depth,
2543     int color_type, int interlace_type, int compression_type,
2544     int filter_type)
2545 {
2546    int error = 0;
2547 
2548    /* Check for width and height valid values */
2549    if (width == 0)
2550    {
2551       png_warning(png_ptr, "Image width is zero in IHDR");
2552       error = 1;
2553    }
2554 
2555    if (width > PNG_UINT_31_MAX)
2556    {
2557       png_warning(png_ptr, "Invalid image width in IHDR");
2558       error = 1;
2559    }
2560 
2561    if (png_gt(((width + 7) & (~7U)),
2562        ((PNG_SIZE_MAX
2563            - 48        /* big_row_buf hack */
2564            - 1)        /* filter byte */
2565            / 8)        /* 8-byte RGBA pixels */
2566            - 1))       /* extra max_pixel_depth pad */
2567    {
2568       /* The size of the row must be within the limits of this architecture.
2569        * Because the read code can perform arbitrary transformations the
2570        * maximum size is checked here.  Because the code in png_read_start_row
2571        * adds extra space "for safety's sake" in several places a conservative
2572        * limit is used here.
2573        *
2574        * NOTE: it would be far better to check the size that is actually used,
2575        * but the effect in the real world is minor and the changes are more
2576        * extensive, therefore much more dangerous and much more difficult to
2577        * write in a way that avoids compiler warnings.
2578        */
2579       png_warning(png_ptr, "Image width is too large for this architecture");
2580       error = 1;
2581    }


2952           * test on DBL_MAX above.
2953           */
2954          fp /= base;
2955          while (fp >= 1) fp /= 10, ++exp_b10;
2956 
2957          /* Because of the code above fp may, at this point, be
2958           * less than .1, this is ok because the code below can
2959           * handle the leading zeros this generates, so no attempt
2960           * is made to correct that here.
2961           */
2962 
2963          {
2964             unsigned int czero, clead, cdigits;
2965             char exponent[10];
2966 
2967             /* Allow up to two leading zeros - this will not lengthen
2968              * the number compared to using E-n.
2969              */
2970             if (exp_b10 < 0 && exp_b10 > -3) /* PLUS 3 TOTAL 4 */
2971             {
2972                czero = (unsigned int)(-exp_b10); /* PLUS 2 digits: TOTAL 3 */
2973                exp_b10 = 0;      /* Dot added below before first output. */
2974             }
2975             else
2976                czero = 0;    /* No zeros to add */
2977 
2978             /* Generate the digit list, stripping trailing zeros and
2979              * inserting a '.' before a digit if the exponent is 0.
2980              */
2981             clead = czero; /* Count of leading zeros */
2982             cdigits = 0;   /* Count of digits in list. */
2983 
2984             do
2985             {
2986                double d;
2987 
2988                fp *= 10;
2989                /* Use modf here, not floor and subtract, so that
2990                 * the separation is done in one step.  At the end
2991                 * of the loop don't break the number into parts so
2992                 * that the final digit is rounded.


3130             /* Here if an exponent is required, adjust size for
3131              * the digits we output but did not count.  The total
3132              * digit output here so far is at most 1+precision - no
3133              * decimal point and no leading or trailing zeros have
3134              * been output.
3135              */
3136             size -= cdigits;
3137 
3138             *ascii++ = 69, --size;    /* 'E': PLUS 1 TOTAL 2+precision */
3139 
3140             /* The following use of an unsigned temporary avoids ambiguities in
3141              * the signed arithmetic on exp_b10 and permits GCC at least to do
3142              * better optimization.
3143              */
3144             {
3145                unsigned int uexp_b10;
3146 
3147                if (exp_b10 < 0)
3148                {
3149                   *ascii++ = 45, --size; /* '-': PLUS 1 TOTAL 3+precision */
3150                   uexp_b10 = (unsigned int)(-exp_b10);
3151                }
3152 
3153                else
3154                   uexp_b10 = (unsigned int)exp_b10;
3155 
3156                cdigits = 0;
3157 
3158                while (uexp_b10 > 0)
3159                {
3160                   exponent[cdigits++] = (char)(48 + uexp_b10 % 10);
3161                   uexp_b10 /= 10;
3162                }
3163             }
3164 
3165             /* Need another size check here for the exponent digits, so
3166              * this need not be considered above.
3167              */
3168             if (size > cdigits)
3169             {
3170                while (cdigits > 0) *ascii++ = exponent[--cdigits];
3171 
3172                *ascii = 0;
3173 
3174                return;


3196 }
3197 
3198 #  endif /* FLOATING_POINT */
3199 
3200 #  ifdef PNG_FIXED_POINT_SUPPORTED
3201 /* Function to format a fixed point value in ASCII.
3202  */
3203 void /* PRIVATE */
3204 png_ascii_from_fixed(png_const_structrp png_ptr, png_charp ascii,
3205     png_size_t size, png_fixed_point fp)
3206 {
3207    /* Require space for 10 decimal digits, a decimal point, a minus sign and a
3208     * trailing \0, 13 characters:
3209     */
3210    if (size > 12)
3211    {
3212       png_uint_32 num;
3213 
3214       /* Avoid overflow here on the minimum integer. */
3215       if (fp < 0)
3216          *ascii++ = 45, num = (png_uint_32)(-fp);
3217       else
3218          num = (png_uint_32)fp;
3219 
3220       if (num <= 0x80000000) /* else overflowed */
3221       {
3222          unsigned int ndigits = 0, first = 16 /* flag value */;
3223          char digits[10];
3224 
3225          while (num)
3226          {
3227             /* Split the low digit off num: */
3228             unsigned int tmp = num/10;
3229             num -= tmp*10;
3230             digits[ndigits++] = (char)(48 + num);
3231             /* Record the first non-zero digit, note that this is a number
3232              * starting at 1, it's not actually the array index.
3233              */
3234             if (first == 16 && num > 0)
3235                first = ndigits;
3236             num = tmp;
3237          }
3238 


4136       }
4137    png_free(png_ptr, png_ptr->gamma_16_to_1);
4138    png_ptr->gamma_16_to_1 = NULL;
4139    }
4140 #endif /* 16BIT */
4141 #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
4142 }
4143 
4144 /* We build the 8- or 16-bit gamma tables here.  Note that for 16-bit
4145  * tables, we don't make a full table if we are reducing to 8-bit in
4146  * the future.  Note also how the gamma_16 tables are segmented so that
4147  * we don't need to allocate > 64K chunks for a full 16-bit table.
4148  */
4149 void /* PRIVATE */
4150 png_build_gamma_table(png_structrp png_ptr, int bit_depth)
4151 {
4152    png_debug(1, "in png_build_gamma_table");
4153 
4154    /* Remove any existing table; this copes with multiple calls to
4155     * png_read_update_info. The warning is because building the gamma tables
4156     * multiple times is a performance hit - it's harmless but the ability to
4157     * call png_read_update_info() multiple times is new in 1.5.6 so it seems
4158     * sensible to warn if the app introduces such a hit.
4159     */
4160    if (png_ptr->gamma_table != NULL || png_ptr->gamma_16_table != NULL)
4161    {
4162       png_warning(png_ptr, "gamma table being rebuilt");
4163       png_destroy_gamma_table(png_ptr);
4164    }
4165 
4166    if (bit_depth <= 8)
4167    {
4168       png_build_8bit_table(png_ptr, &png_ptr->gamma_table,
4169           png_ptr->screen_gamma > 0 ?
4170           png_reciprocal2(png_ptr->colorspace.gamma,
4171           png_ptr->screen_gamma) : PNG_FP_1);
4172 
4173 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4174    defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
4175    defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4176       if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0)
4177       {
4178          png_build_8bit_table(png_ptr, &png_ptr->gamma_to_1,
4179              png_reciprocal(png_ptr->colorspace.gamma));
4180 
4181          png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1,
4182              png_ptr->screen_gamma > 0 ?
4183              png_reciprocal(png_ptr->screen_gamma) :
4184              png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */);
4185       }
4186 #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
4187    }
4188 #ifdef PNG_16BIT_SUPPORTED
4189    else
4190    {
4191       png_byte shift, sig_bit;
4192 
4193       if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
4194       {
4195          sig_bit = png_ptr->sig_bit.red;
4196 
4197          if (png_ptr->sig_bit.green > sig_bit)
4198             sig_bit = png_ptr->sig_bit.green;
4199 
4200          if (png_ptr->sig_bit.blue > sig_bit)
4201             sig_bit = png_ptr->sig_bit.blue;
4202       }
4203       else
4204          sig_bit = png_ptr->sig_bit.gray;
4205 
4206       /* 16-bit gamma code uses this equation:
4207        *
4208        *   ov = table[(iv & 0xff) >> gamma_shift][iv >> 8]
4209        *
4210        * Where 'iv' is the input color value and 'ov' is the output value -
4211        * pow(iv, gamma).
4212        *
4213        * Thus the gamma table consists of up to 256 256-entry tables.  The table
4214        * is selected by the (8-gamma_shift) most significant of the low 8 bits
4215        * of the color value then indexed by the upper 8 bits:
4216        *
4217        *   table[low bits][high 8 bits]
4218        *
4219        * So the table 'n' corresponds to all those 'iv' of:
4220        *
4221        *   <all high 8-bit values><n << gamma_shift>..<(n+1 << gamma_shift)-1>
4222        *
4223        */
4224       if (sig_bit > 0 && sig_bit < 16U)
4225          /* shift == insignificant bits */
4226          shift = (png_byte)((16U - sig_bit) & 0xff);
4227 
4228       else
4229          shift = 0; /* keep all 16 bits */
4230 
4231       if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0)
4232       {
4233          /* PNG_MAX_GAMMA_8 is the number of bits to keep - effectively
4234           * the significant bits in the *input* when the output will
4235           * eventually be 8 bits.  By default it is 11.
4236           */
4237          if (shift < (16U - PNG_MAX_GAMMA_8))
4238             shift = (16U - PNG_MAX_GAMMA_8);
4239       }
4240 
4241       if (shift > 8U)
4242          shift = 8U; /* Guarantees at least one table! */
4243 
4244       png_ptr->gamma_shift = shift;
4245 
4246       /* NOTE: prior to 1.5.4 this test used to include PNG_BACKGROUND (now
4247        * PNG_COMPOSE).  This effectively smashed the background calculation for
4248        * 16-bit output because the 8-bit table assumes the result will be
4249        * reduced to 8 bits.
4250        */
4251       if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0)
4252           png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift,
4253           png_ptr->screen_gamma > 0 ? png_product2(png_ptr->colorspace.gamma,
4254           png_ptr->screen_gamma) : PNG_FP_1);
4255 
4256       else
4257           png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift,
4258           png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma,
4259           png_ptr->screen_gamma) : PNG_FP_1);
4260 
4261 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4262    defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
4263    defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4264       if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0)
4265       {
4266          png_build_16bit_table(png_ptr, &png_ptr->gamma_16_to_1, shift,
4267              png_reciprocal(png_ptr->colorspace.gamma));
4268 
4269          /* Notice that the '16 from 1' table should be full precision, however


4271           * TODO: fix this.
4272           */
4273          png_build_16bit_table(png_ptr, &png_ptr->gamma_16_from_1, shift,
4274              png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) :
4275              png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */);
4276       }
4277 #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
4278    }
4279 #endif /* 16BIT */
4280 }
4281 #endif /* READ_GAMMA */
4282 
4283 /* HARDWARE OR SOFTWARE OPTION SUPPORT */
4284 #ifdef PNG_SET_OPTION_SUPPORTED
4285 int PNGAPI
4286 png_set_option(png_structrp png_ptr, int option, int onoff)
4287 {
4288    if (png_ptr != NULL && option >= 0 && option < PNG_OPTION_NEXT &&
4289       (option & 1) == 0)
4290    {
4291       png_uint_32 mask = 3 << option;
4292       png_uint_32 setting = (2 + (onoff != 0)) << option;
4293       png_uint_32 current = png_ptr->options;
4294 
4295       png_ptr->options = (png_uint_32)(((current & ~mask) | setting) & 0xff);
4296 
4297       return (current & mask) >> option;
4298    }
4299 
4300    return PNG_OPTION_INVALID;
4301 }
4302 #endif
4303 
4304 /* sRGB support */
4305 #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
4306    defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
4307 /* sRGB conversion tables; these are machine generated with the code in
4308  * contrib/tools/makesRGB.c.  The actual sRGB transfer curve defined in the
4309  * specification (see the article at http://en.wikipedia.org/wiki/SRGB)
4310  * is used, not the gamma=1/2.2 approximation use elsewhere in libpng.
4311  * The sRGB to linear table is exact (to the nearest 16-bit linear fraction).
4312  * The inverse (linear to sRGB) table has accuracies as follows:
4313  *
4314  * For all possible (255*65535+1) input values:
4315  *


< prev index next >