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 /* pngread.c - read a PNG file
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.23 [June 9, 2016]
33 * Copyright (c) 1998-2002,2004,2006-2016 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 * This file contains routines that an application calls directly to
42 * read a PNG file or stream.
43 */
44
45 #include "pngpriv.h"
46 #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
47 # include <errno.h>
48 #endif
49
50 #ifdef PNG_READ_SUPPORTED
51
52 /* Create a PNG structure for reading, and allocate any memory needed. */
370 *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
371 *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
372 }
373 }
374 else if (row_info->bit_depth == 16)
375 {
376 png_bytep rp;
377 png_uint_32 i;
378
379 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
380 bytes_per_pixel = 6;
381
382 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
383 bytes_per_pixel = 8;
384
385 else
386 return;
387
388 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
389 {
390 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
391 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
392 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
393 png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
394 png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
395 *(rp ) = (png_byte)((red >> 8) & 0xff);
396 *(rp + 1) = (png_byte)(red & 0xff);
397 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
398 *(rp + 5) = (png_byte)(blue & 0xff);
399 }
400 }
401 }
402 }
403 #endif /* MNG_FEATURES */
404
405 void PNGAPI
406 png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
407 {
408 png_row_info row_info;
409
410 if (png_ptr == NULL)
411 return;
412
1041
1042 *png_ptr_ptr = NULL;
1043 png_read_destroy(png_ptr);
1044 png_destroy_png_struct(png_ptr);
1045 }
1046
1047 void PNGAPI
1048 png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
1049 {
1050 if (png_ptr == NULL)
1051 return;
1052
1053 png_ptr->read_row_fn = read_row_fn;
1054 }
1055
1056
1057 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1058 #ifdef PNG_INFO_IMAGE_SUPPORTED
1059 void PNGAPI
1060 png_read_png(png_structrp png_ptr, png_inforp info_ptr,
1061 int transforms,
1062 voidp params)
1063 {
1064 if (png_ptr == NULL || info_ptr == NULL)
1065 return;
1066
1067 /* png_read_info() gives us all of the information from the
1068 * PNG file before the first IDAT (image data chunk).
1069 */
1070 png_read_info(png_ptr, info_ptr);
1071 if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
1072 png_error(png_ptr, "Image is too high to process with png_read_png()");
1073
1074 /* -------------- image transformations start here ------------------- */
1075 /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
1076 * is not implemented. This will only happen in de-configured (non-default)
1077 * libpng builds. The results can be unexpected - png_read_png may return
1078 * short or mal-formed rows because the transform is skipped.
1079 */
1080
1081 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
1082 */
1405 if (g == 0)
1406 return 0;
1407
1408 return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
1409 }
1410
1411 return 1;
1412 }
1413
1414 /* Do the main body of a 'png_image_begin_read' function; read the PNG file
1415 * header and fill in all the information. This is executed in a safe context,
1416 * unlike the init routine above.
1417 */
1418 static int
1419 png_image_read_header(png_voidp argument)
1420 {
1421 png_imagep image = png_voidcast(png_imagep, argument);
1422 png_structrp png_ptr = image->opaque->png_ptr;
1423 png_inforp info_ptr = image->opaque->info_ptr;
1424
1425 png_set_benign_errors(png_ptr, 1/*warn*/);
1426 png_read_info(png_ptr, info_ptr);
1427
1428 /* Do this the fast way; just read directly out of png_struct. */
1429 image->width = png_ptr->width;
1430 image->height = png_ptr->height;
1431
1432 {
1433 png_uint_32 format = png_image_format(png_ptr);
1434
1435 image->format = format;
1436
1437 #ifdef PNG_COLORSPACE_SUPPORTED
1438 /* Does the colorspace match sRGB? If there is no color endpoint
1439 * (colorant) information assume yes, otherwise require the
1440 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
1441 * colorspace has been determined to be invalid ignore it.
1442 */
1443 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
1444 & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB|
1445 PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS))
1446 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1447 #endif
1448 }
1449
1450 /* We need the maximum number of entries regardless of the format the
1451 * application sets here.
1452 */
1453 {
1454 png_uint_32 cmap_entries;
1455
1456 switch (png_ptr->color_type)
1457 {
1458 case PNG_COLOR_TYPE_GRAY:
1459 cmap_entries = 1U << png_ptr->bit_depth;
1460 break;
1461
1462 case PNG_COLOR_TYPE_PALETTE:
1463 cmap_entries = png_ptr->num_palette;
1464 break;
1465
1466 default:
1467 cmap_entries = 256;
1468 break;
1469 }
1470
1471 if (cmap_entries > 256)
1472 cmap_entries = 256;
1473
1474 image->colormap_entries = cmap_entries;
1475 }
1476
1477 return 1;
1478 }
1479
1480 #ifdef PNG_STDIO_SUPPORTED
1481 int PNGAPI
1482 png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1483 {
1978 }
1979 }
1980
1981 # ifdef afirst
1982 # undef afirst
1983 # endif
1984 # ifdef bgr
1985 # undef bgr
1986 # endif
1987 }
1988 }
1989
1990 static int
1991 make_gray_file_colormap(png_image_read_control *display)
1992 {
1993 unsigned int i;
1994
1995 for (i=0; i<256; ++i)
1996 png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
1997
1998 return i;
1999 }
2000
2001 static int
2002 make_gray_colormap(png_image_read_control *display)
2003 {
2004 unsigned int i;
2005
2006 for (i=0; i<256; ++i)
2007 png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
2008
2009 return i;
2010 }
2011 #define PNG_GRAY_COLORMAP_ENTRIES 256
2012
2013 static int
2014 make_ga_colormap(png_image_read_control *display)
2015 {
2016 unsigned int i, a;
2017
2018 /* Alpha is retained, the output will be a color-map with entries
2019 * selected by six levels of alpha. One transparent entry, 6 gray
2020 * levels for all the intermediate alpha values, leaving 230 entries
2021 * for the opaque grays. The color-map entries are the six values
2022 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
2023 * relevant entry.
2024 *
2025 * if (alpha > 229) // opaque
2026 * {
2027 * // The 231 entries are selected to make the math below work:
2028 * base = 0;
2029 * entry = (231 * gray + 128) >> 8;
2043 while (i < 231)
2044 {
2045 unsigned int gray = (i * 256 + 115) / 231;
2046 png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
2047 }
2048
2049 /* 255 is used here for the component values for consistency with the code
2050 * that undoes premultiplication in pngwrite.c.
2051 */
2052 png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
2053
2054 for (a=1; a<5; ++a)
2055 {
2056 unsigned int g;
2057
2058 for (g=0; g<6; ++g)
2059 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
2060 P_sRGB);
2061 }
2062
2063 return i;
2064 }
2065
2066 #define PNG_GA_COLORMAP_ENTRIES 256
2067
2068 static int
2069 make_rgb_colormap(png_image_read_control *display)
2070 {
2071 unsigned int i, r;
2072
2073 /* Build a 6x6x6 opaque RGB cube */
2074 for (i=r=0; r<6; ++r)
2075 {
2076 unsigned int g;
2077
2078 for (g=0; g<6; ++g)
2079 {
2080 unsigned int b;
2081
2082 for (b=0; b<6; ++b)
2083 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
2084 P_sRGB);
2085 }
2086 }
2087
2088 return i;
2089 }
2090
2091 #define PNG_RGB_COLORMAP_ENTRIES 216
2092
2093 /* Return a palette index to the above palette given three 8-bit sRGB values. */
2094 #define PNG_RGB_INDEX(r,g,b) \
2095 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
2096
2097 static int
2098 png_image_read_colormap(png_voidp argument)
2099 {
2100 png_image_read_control *display =
2101 png_voidcast(png_image_read_control*, argument);
2102 const png_imagep image = display->image;
2103
2104 const png_structrp png_ptr = image->opaque->png_ptr;
2105 const png_uint_32 output_format = image->format;
2106 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
2107 P_LINEAR : P_sRGB;
2108
2116 unsigned int background_index = 256;
2117 png_uint_32 back_r, back_g, back_b;
2118
2119 /* Flags to accumulate things that need to be done to the input. */
2120 int expand_tRNS = 0;
2121
2122 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
2123 * very difficult to do, the results look awful, and it is difficult to see
2124 * what possible use it is because the application can't control the
2125 * color-map.
2126 */
2127 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
2128 png_ptr->num_trans > 0) /* alpha in input */ &&
2129 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
2130 {
2131 if (output_encoding == P_LINEAR) /* compose on black */
2132 back_b = back_g = back_r = 0;
2133
2134 else if (display->background == NULL /* no way to remove it */)
2135 png_error(png_ptr,
2136 "a background color must be supplied to remove alpha/transparency");
2137
2138 /* Get a copy of the background color (this avoids repeating the checks
2139 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
2140 * output format.
2141 */
2142 else
2143 {
2144 back_g = display->background->green;
2145 if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0)
2146 {
2147 back_r = display->background->red;
2148 back_b = display->background->blue;
2149 }
2150 else
2151 back_b = back_r = back_g;
2152 }
2153 }
2154
2155 else if (output_encoding == P_LINEAR)
2156 back_b = back_r = back_g = 65535;
2261 * encoded values; however, if a tRNS chunk is present 257 color-map
2262 * entries are required. This means that the extra entry requires
2263 * special processing; add an alpha channel, sacrifice gray level
2264 * 254 and convert transparent (alpha==0) entries to that.
2265 *
2266 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2267 * same time to minimize quality loss. If a tRNS chunk is present
2268 * this means libpng must handle it too; otherwise it is impossible
2269 * to do the exact match on the 16-bit value.
2270 *
2271 * If the output has no alpha channel *and* the background color is
2272 * gray then it is possible to let libpng handle the substitution by
2273 * ensuring that the corresponding gray level matches the background
2274 * color exactly.
2275 */
2276 data_encoding = P_sRGB;
2277
2278 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2279 png_error(png_ptr, "gray[16] color-map: too few entries");
2280
2281 cmap_entries = make_gray_colormap(display);
2282
2283 if (png_ptr->num_trans > 0)
2284 {
2285 unsigned int back_alpha;
2286
2287 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2288 back_alpha = 0;
2289
2290 else
2291 {
2292 if (back_r == back_g && back_g == back_b)
2293 {
2294 /* Background is gray; no special processing will be
2295 * required.
2296 */
2297 png_color_16 c;
2298 png_uint_32 gray = back_g;
2299
2300 if (output_encoding == P_LINEAR)
2301 {
2359 break;
2360
2361 case PNG_COLOR_TYPE_GRAY_ALPHA:
2362 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2363 * of 65536 combinations. If, however, the alpha channel is to be
2364 * removed there are only 256 possibilities if the background is gray.
2365 * (Otherwise there is a subset of the 65536 possibilities defined by
2366 * the triangle between black, white and the background color.)
2367 *
2368 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2369 * worry about tRNS matching - tRNS is ignored if there is an alpha
2370 * channel.
2371 */
2372 data_encoding = P_sRGB;
2373
2374 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2375 {
2376 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2377 png_error(png_ptr, "gray+alpha color-map: too few entries");
2378
2379 cmap_entries = make_ga_colormap(display);
2380
2381 background_index = PNG_CMAP_GA_BACKGROUND;
2382 output_processing = PNG_CMAP_GA;
2383 }
2384
2385 else /* alpha is removed */
2386 {
2387 /* Alpha must be removed as the PNG data is processed when the
2388 * background is a color because the G and A channels are
2389 * independent and the vector addition (non-parallel vectors) is a
2390 * 2-D problem.
2391 *
2392 * This can be reduced to the same algorithm as above by making a
2393 * colormap containing gray levels (for the opaque grays), a
2394 * background entry (for a transparent pixel) and a set of four six
2395 * level color values, one set for each intermediate alpha value.
2396 * See the comments in make_ga_colormap for how this works in the
2397 * per-pixel processing.
2398 *
2399 * If the background is gray, however, we only need a 256 entry gray
2400 * level color map. It is sufficient to make the entry generated
2401 * for the background color be exactly the color specified.
2402 */
2403 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
2404 (back_r == back_g && back_g == back_b))
2405 {
2406 /* Background is gray; no special processing will be required. */
2407 png_color_16 c;
2408 png_uint_32 gray = back_g;
2409
2410 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2411 png_error(png_ptr, "gray-alpha color-map: too few entries");
2412
2413 cmap_entries = make_gray_colormap(display);
2414
2415 if (output_encoding == P_LINEAR)
2416 {
2417 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2418
2419 /* And make sure the corresponding palette entry matches. */
2420 png_create_colormap_entry(display, gray, back_g, back_g,
2421 back_g, 65535, P_LINEAR);
2422 }
2423
2424 /* The background passed to libpng, however, must be the sRGB
2425 * value.
2426 */
2427 c.index = 0; /*unused*/
2428 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2429
2430 png_set_background_fixed(png_ptr, &c,
2431 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2432 0/*gamma: not used*/);
2433
2441 /* This is the same as png_make_ga_colormap, above, except that
2442 * the entries are all opaque.
2443 */
2444 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2445 png_error(png_ptr, "ga-alpha color-map: too few entries");
2446
2447 i = 0;
2448 while (i < 231)
2449 {
2450 png_uint_32 gray = (i * 256 + 115) / 231;
2451 png_create_colormap_entry(display, i++, gray, gray, gray,
2452 255, P_sRGB);
2453 }
2454
2455 /* NOTE: this preserves the full precision of the application
2456 * background color.
2457 */
2458 background_index = i;
2459 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
2460 #ifdef __COVERITY__
2461 /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
2462 * here.
2463 */ 255U,
2464 #else
2465 output_encoding == P_LINEAR ? 65535U : 255U,
2466 #endif
2467 output_encoding);
2468
2469 /* For non-opaque input composite on the sRGB background - this
2470 * requires inverting the encoding for each component. The input
2471 * is still converted to the sRGB encoding because this is a
2472 * reasonable approximate to the logarithmic curve of human
2473 * visual sensitivity, at least over the narrow range which PNG
2474 * represents. Consequently 'G' is always sRGB encoded, while
2475 * 'A' is linear. We need the linear background colors.
2476 */
2477 if (output_encoding == P_sRGB) /* else already linear */
2478 {
2479 /* This may produce a value not exactly matching the
2480 * background, but that's ok because these numbers are only
2481 * used when alpha != 0
2482 */
2532 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2533 -1);
2534 data_encoding = P_sRGB;
2535
2536 /* The output will now be one or two 8-bit gray or gray+alpha
2537 * channels. The more complex case arises when the input has alpha.
2538 */
2539 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2540 png_ptr->num_trans > 0) &&
2541 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2542 {
2543 /* Both input and output have an alpha channel, so no background
2544 * processing is required; just map the GA bytes to the right
2545 * color-map entry.
2546 */
2547 expand_tRNS = 1;
2548
2549 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2550 png_error(png_ptr, "rgb[ga] color-map: too few entries");
2551
2552 cmap_entries = make_ga_colormap(display);
2553 background_index = PNG_CMAP_GA_BACKGROUND;
2554 output_processing = PNG_CMAP_GA;
2555 }
2556
2557 else
2558 {
2559 /* Either the input or the output has no alpha channel, so there
2560 * will be no non-opaque pixels in the color-map; it will just be
2561 * grayscale.
2562 */
2563 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2564 png_error(png_ptr, "rgb[gray] color-map: too few entries");
2565
2566 /* Ideally this code would use libpng to do the gamma correction,
2567 * but if an input alpha channel is to be removed we will hit the
2568 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2569 * correction bug). Fix this by dropping the gamma correction in
2570 * this case and doing it in the palette; this will result in
2571 * duplicate palette entries, but that's better than the
2572 * alternative of double gamma correction.
2573 */
2574 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2575 png_ptr->num_trans > 0) &&
2576 png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0)
2577 {
2578 cmap_entries = make_gray_file_colormap(display);
2579 data_encoding = P_FILE;
2580 }
2581
2582 else
2583 cmap_entries = make_gray_colormap(display);
2584
2585 /* But if the input has alpha or transparency it must be removed
2586 */
2587 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2588 png_ptr->num_trans > 0)
2589 {
2590 png_color_16 c;
2591 png_uint_32 gray = back_g;
2592
2593 /* We need to ensure that the application background exists in
2594 * the colormap and that completely transparent pixels map to
2595 * it. Achieve this simply by ensuring that the entry
2596 * selected for the background really is the background color.
2597 */
2598 if (data_encoding == P_FILE) /* from the fixup above */
2599 {
2600 /* The app supplied a gray which is in output_encoding, we
2601 * need to convert it to a value of the input (P_FILE)
2602 * encoding then set this palette entry to the required
2603 * output encoding.
2651 * color or alpha; png_quantize ignores alpha. Easier overall just
2652 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2653 * Consequently we always want libpng to produce sRGB data.
2654 */
2655 data_encoding = P_sRGB;
2656
2657 /* Is there any transparency or alpha? */
2658 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2659 png_ptr->num_trans > 0)
2660 {
2661 /* Is there alpha in the output too? If so all four channels are
2662 * processed into a special RGB cube with alpha support.
2663 */
2664 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2665 {
2666 png_uint_32 r;
2667
2668 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2669 png_error(png_ptr, "rgb+alpha color-map: too few entries");
2670
2671 cmap_entries = make_rgb_colormap(display);
2672
2673 /* Add a transparent entry. */
2674 png_create_colormap_entry(display, cmap_entries, 255, 255,
2675 255, 0, P_sRGB);
2676
2677 /* This is stored as the background index for the processing
2678 * algorithm.
2679 */
2680 background_index = cmap_entries++;
2681
2682 /* Add 27 r,g,b entries each with alpha 0.5. */
2683 for (r=0; r<256; r = (r << 1) | 0x7f)
2684 {
2685 png_uint_32 g;
2686
2687 for (g=0; g<256; g = (g << 1) | 0x7f)
2688 {
2689 png_uint_32 b;
2690
2691 /* This generates components with the values 0, 127 and
2700 expand_tRNS = 1;
2701 output_processing = PNG_CMAP_RGB_ALPHA;
2702 }
2703
2704 else
2705 {
2706 /* Alpha/transparency must be removed. The background must
2707 * exist in the color map (achieved by setting adding it after
2708 * the 666 color-map). If the standard processing code will
2709 * pick up this entry automatically that's all that is
2710 * required; libpng can be called to do the background
2711 * processing.
2712 */
2713 unsigned int sample_size =
2714 PNG_IMAGE_SAMPLE_SIZE(output_format);
2715 png_uint_32 r, g, b; /* sRGB background */
2716
2717 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2718 png_error(png_ptr, "rgb-alpha color-map: too few entries");
2719
2720 cmap_entries = make_rgb_colormap(display);
2721
2722 png_create_colormap_entry(display, cmap_entries, back_r,
2723 back_g, back_b, 0/*unused*/, output_encoding);
2724
2725 if (output_encoding == P_LINEAR)
2726 {
2727 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2728 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2729 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2730 }
2731
2732 else
2733 {
2734 r = back_r;
2735 g = back_g;
2736 b = back_g;
2737 }
2738
2739 /* Compare the newly-created color-map entry with the one the
2740 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2785 c.gray = c.green = (png_uint_16)back_g;
2786 c.blue = (png_uint_16)back_b;
2787
2788 png_set_background_fixed(png_ptr, &c,
2789 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2790 0/*gamma: not used*/);
2791
2792 output_processing = PNG_CMAP_RGB;
2793 }
2794 }
2795 }
2796
2797 else /* no alpha or transparency in the input */
2798 {
2799 /* Alpha in the output is irrelevant, simply map the opaque input
2800 * pixels to the 6x6x6 color-map.
2801 */
2802 if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
2803 png_error(png_ptr, "rgb color-map: too few entries");
2804
2805 cmap_entries = make_rgb_colormap(display);
2806 output_processing = PNG_CMAP_RGB;
2807 }
2808 }
2809 break;
2810
2811 case PNG_COLOR_TYPE_PALETTE:
2812 /* It's already got a color-map. It may be necessary to eliminate the
2813 * tRNS entries though.
2814 */
2815 {
2816 unsigned int num_trans = png_ptr->num_trans;
2817 png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
2818 png_const_colorp colormap = png_ptr->palette;
2819 const int do_background = trans != NULL &&
2820 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2821 unsigned int i;
2822
2823 /* Just in case: */
2824 if (trans == NULL)
2825 num_trans = 0;
2826
2827 output_processing = PNG_CMAP_NONE;
2828 data_encoding = P_FILE; /* Don't change from color-map indices */
2829 cmap_entries = png_ptr->num_palette;
2830 if (cmap_entries > 256)
2831 cmap_entries = 256;
2832
2833 if (cmap_entries > image->colormap_entries)
2834 png_error(png_ptr, "palette color-map: too few entries");
2835
2836 for (i=0; i < cmap_entries; ++i)
2837 {
2838 if (do_background != 0 && i < num_trans && trans[i] < 255)
2839 {
2840 if (trans[i] == 0)
2841 png_create_colormap_entry(display, i, back_r, back_g,
2842 back_b, 0, output_encoding);
2843
2844 else
2845 {
2846 /* Must compose the PNG file color in the color-map entry
2847 * on the sRGB color in 'back'.
2848 */
2849 png_create_colormap_entry(display, i,
2850 png_colormap_compose(display, colormap[i].red, P_FILE,
2851 trans[i], back_r, output_encoding),
2852 png_colormap_compose(display, colormap[i].green, P_FILE,
2853 trans[i], back_g, output_encoding),
2854 png_colormap_compose(display, colormap[i].blue, P_FILE,
2855 trans[i], back_b, output_encoding),
2856 output_encoding == P_LINEAR ? trans[i] * 257U :
2857 trans[i],
2858 output_encoding);
2859 }
2860 }
2861
2862 else
2863 png_create_colormap_entry(display, i, colormap[i].red,
2864 colormap[i].green, colormap[i].blue,
2865 i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
2866 }
2867
2868 /* The PNG data may have indices packed in fewer than 8 bits, it
2869 * must be expanded if so.
2870 */
2871 if (png_ptr->bit_depth < 8)
2872 png_set_packing(png_ptr);
2873 }
2874 break;
2875
2925 goto bad_background;
2926 break;
2927
2928 case PNG_CMAP_RGB:
2929 if (background_index != PNG_CMAP_RGB_BACKGROUND)
2930 goto bad_background;
2931 break;
2932
2933 case PNG_CMAP_RGB_ALPHA:
2934 if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
2935 goto bad_background;
2936 break;
2937
2938 default:
2939 png_error(png_ptr, "bad processing option (internal error)");
2940
2941 bad_background:
2942 png_error(png_ptr, "bad background index (internal error)");
2943 }
2944
2945 display->colormap_processing = output_processing;
2946
2947 return 1/*ok*/;
2948 }
2949
2950 /* The final part of the color-map read called from png_image_finish_read. */
2951 static int
2952 png_image_read_and_map(png_voidp argument)
2953 {
2954 png_image_read_control *display = png_voidcast(png_image_read_control*,
2955 argument);
2956 png_imagep image = display->image;
2957 png_structrp png_ptr = image->opaque->png_ptr;
2958 int passes;
2959
2960 /* Called when the libpng data must be transformed into the color-mapped
2961 * form. There is a local row buffer in display->local and this routine must
2962 * do the interlace handling.
2963 */
2964 switch (png_ptr->interlaced)
2965 {
3234
3235 display->first_row = first_row;
3236 display->row_bytes = row_bytes;
3237 }
3238
3239 if (passes == 0)
3240 {
3241 int result;
3242 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
3243
3244 display->local_row = row;
3245 result = png_safe_execute(image, png_image_read_and_map, display);
3246 display->local_row = NULL;
3247 png_free(png_ptr, row);
3248
3249 return result;
3250 }
3251
3252 else
3253 {
3254 png_alloc_size_t row_bytes = display->row_bytes;
3255
3256 while (--passes >= 0)
3257 {
3258 png_uint_32 y = image->height;
3259 png_bytep row = png_voidcast(png_bytep, display->first_row);
3260
3261 while (y-- > 0)
3262 {
3263 png_read_row(png_ptr, row, NULL);
3264 row += row_bytes;
3265 }
3266 }
3267
3268 return 1;
3269 }
3270 }
3271
3272 /* Just the row reading part of png_image_read. */
3273 static int
3274 png_image_read_composite(png_voidp argument)
3275 {
3276 png_image_read_control *display = png_voidcast(png_image_read_control*,
3277 argument);
3278 png_imagep image = display->image;
3279 png_structrp png_ptr = image->opaque->png_ptr;
3280 int passes;
3281
3444
3445 /* Use direct access to info_ptr here because otherwise the simplified API
3446 * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
3447 * checking the value after libpng expansions, not the original value in the
3448 * PNG.
3449 */
3450 switch (info_ptr->bit_depth)
3451 {
3452 case 8:
3453 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
3454 * to be removed by composing on a background: either the row if
3455 * display->background is NULL or display->background->green if not.
3456 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3457 */
3458 {
3459 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3460 ptrdiff_t step_row = display->row_bytes;
3461
3462 for (pass = 0; pass < passes; ++pass)
3463 {
3464 png_bytep row = png_voidcast(png_bytep,
3465 display->first_row);
3466 unsigned int startx, stepx, stepy;
3467 png_uint_32 y;
3468
3469 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3470 {
3471 /* The row may be empty for a short image: */
3472 if (PNG_PASS_COLS(width, pass) == 0)
3473 continue;
3474
3475 startx = PNG_PASS_START_COL(pass);
3476 stepx = PNG_PASS_COL_OFFSET(pass);
3477 y = PNG_PASS_START_ROW(pass);
3478 stepy = PNG_PASS_ROW_OFFSET(pass);
3479 }
3480
3481 else
3482 {
3483 y = 0;
3484 startx = 0;
3485 stepx = stepy = 1;
3570
3571 row += display->row_bytes;
3572 }
3573 }
3574 }
3575 }
3576 break;
3577
3578 case 16:
3579 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3580 * still be done and, maybe, the alpha channel removed. This code also
3581 * handles the alpha-first option.
3582 */
3583 {
3584 png_uint_16p first_row = png_voidcast(png_uint_16p,
3585 display->first_row);
3586 /* The division by two is safe because the caller passed in a
3587 * stride which was multiplied by 2 (below) to get row_bytes.
3588 */
3589 ptrdiff_t step_row = display->row_bytes / 2;
3590 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
3591 unsigned int outchannels = 1+preserve_alpha;
3592 int swap_alpha = 0;
3593
3594 # ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
3595 if (preserve_alpha != 0 &&
3596 (image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
3597 swap_alpha = 1;
3598 # endif
3599
3600 for (pass = 0; pass < passes; ++pass)
3601 {
3602 unsigned int startx, stepx, stepy;
3603 png_uint_32 y;
3604
3605 /* The 'x' start and step are adjusted to output components here.
3606 */
3607 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3608 {
3609 /* The row may be empty for a short image: */
3610 if (PNG_PASS_COLS(width, pass) == 0)
3611 continue;
4068 png_free(png_ptr, row);
4069
4070 return result;
4071 }
4072
4073 else if (do_local_background == 2)
4074 {
4075 int result;
4076 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
4077
4078 display->local_row = row;
4079 result = png_safe_execute(image, png_image_read_background, display);
4080 display->local_row = NULL;
4081 png_free(png_ptr, row);
4082
4083 return result;
4084 }
4085
4086 else
4087 {
4088 png_alloc_size_t row_bytes = display->row_bytes;
4089
4090 while (--passes >= 0)
4091 {
4092 png_uint_32 y = image->height;
4093 png_bytep row = png_voidcast(png_bytep, display->first_row);
4094
4095 while (y-- > 0)
4096 {
4097 png_read_row(png_ptr, row, NULL);
4098 row += row_bytes;
4099 }
4100 }
4101
4102 return 1;
4103 }
4104 }
4105
4106 int PNGAPI
4107 png_image_finish_read(png_imagep image, png_const_colorp background,
4108 void *buffer, png_int_32 row_stride, void *colormap)
4109 {
4110 if (image != NULL && image->version == PNG_IMAGE_VERSION)
4111 {
4112 /* Check for row_stride overflow. This check is not performed on the
4113 * original PNG format because it may not occur in the output PNG format
4114 * and libpng deals with the issues of reading the original.
4115 */
4116 const unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);
4117
4118 if (image->width <= 0x7FFFFFFFU/channels) /* no overflow */
4119 {
4120 png_uint_32 check;
4121 const png_uint_32 png_row_stride = image->width * channels;
4122
4123 if (row_stride == 0)
4124 row_stride = (png_int_32)/*SAFE*/png_row_stride;
4125
4126 if (row_stride < 0)
4127 check = -row_stride;
4128
4129 else
4130 check = row_stride;
4131
4132 if (image->opaque != NULL && buffer != NULL && check >= png_row_stride)
4133 {
4134 /* Now check for overflow of the image buffer calculation; this
4135 * limits the whole image size to 32 bits for API compatibility with
4136 * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
4137 */
4138 if (image->height <= 0xFFFFFFFF/png_row_stride)
4139 {
4140 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
4141 (image->colormap_entries > 0 && colormap != NULL))
4142 {
4143 int result;
4144 png_image_read_control display;
4145
4146 memset(&display, 0, (sizeof display));
4147 display.image = image;
4148 display.buffer = buffer;
4149 display.row_stride = row_stride;
4150 display.colormap = colormap;
4151 display.background = background;
4152 display.local_row = NULL;
4153
4154 /* Choose the correct 'end' routine; for the color-map case
4155 * all the setup has already been done.
4156 */
4157 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0)
4158 result = png_safe_execute(image,
4159 png_image_read_colormap, &display) &&
4160 png_safe_execute(image,
4161 png_image_read_colormapped, &display);
4162
4163 else
4164 result =
4165 png_safe_execute(image,
4166 png_image_read_direct, &display);
4167
4168 png_image_free(image);
4169 return result;
4170 }
4171
4172 else
4173 return png_image_error(image,
4174 "png_image_finish_read[color-map]: no color-map");
4175 }
4176
4177 else
4178 return png_image_error(image,
|
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 /* pngread.c - read a PNG file
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.26 [October 20, 2016]
33 * Copyright (c) 1998-2002,2004,2006-2016 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 * This file contains routines that an application calls directly to
42 * read a PNG file or stream.
43 */
44
45 #include "pngpriv.h"
46 #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
47 # include <errno.h>
48 #endif
49
50 #ifdef PNG_READ_SUPPORTED
51
52 /* Create a PNG structure for reading, and allocate any memory needed. */
370 *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
371 *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
372 }
373 }
374 else if (row_info->bit_depth == 16)
375 {
376 png_bytep rp;
377 png_uint_32 i;
378
379 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
380 bytes_per_pixel = 6;
381
382 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
383 bytes_per_pixel = 8;
384
385 else
386 return;
387
388 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
389 {
390 png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1);
391 png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3);
392 png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5);
393 png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
394 png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
395 *(rp ) = (png_byte)((red >> 8) & 0xff);
396 *(rp + 1) = (png_byte)(red & 0xff);
397 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
398 *(rp + 5) = (png_byte)(blue & 0xff);
399 }
400 }
401 }
402 }
403 #endif /* MNG_FEATURES */
404
405 void PNGAPI
406 png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
407 {
408 png_row_info row_info;
409
410 if (png_ptr == NULL)
411 return;
412
1041
1042 *png_ptr_ptr = NULL;
1043 png_read_destroy(png_ptr);
1044 png_destroy_png_struct(png_ptr);
1045 }
1046
1047 void PNGAPI
1048 png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
1049 {
1050 if (png_ptr == NULL)
1051 return;
1052
1053 png_ptr->read_row_fn = read_row_fn;
1054 }
1055
1056
1057 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1058 #ifdef PNG_INFO_IMAGE_SUPPORTED
1059 void PNGAPI
1060 png_read_png(png_structrp png_ptr, png_inforp info_ptr,
1061 int transforms, voidp params)
1062 {
1063 if (png_ptr == NULL || info_ptr == NULL)
1064 return;
1065
1066 /* png_read_info() gives us all of the information from the
1067 * PNG file before the first IDAT (image data chunk).
1068 */
1069 png_read_info(png_ptr, info_ptr);
1070 if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
1071 png_error(png_ptr, "Image is too high to process with png_read_png()");
1072
1073 /* -------------- image transformations start here ------------------- */
1074 /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
1075 * is not implemented. This will only happen in de-configured (non-default)
1076 * libpng builds. The results can be unexpected - png_read_png may return
1077 * short or mal-formed rows because the transform is skipped.
1078 */
1079
1080 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
1081 */
1404 if (g == 0)
1405 return 0;
1406
1407 return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
1408 }
1409
1410 return 1;
1411 }
1412
1413 /* Do the main body of a 'png_image_begin_read' function; read the PNG file
1414 * header and fill in all the information. This is executed in a safe context,
1415 * unlike the init routine above.
1416 */
1417 static int
1418 png_image_read_header(png_voidp argument)
1419 {
1420 png_imagep image = png_voidcast(png_imagep, argument);
1421 png_structrp png_ptr = image->opaque->png_ptr;
1422 png_inforp info_ptr = image->opaque->info_ptr;
1423
1424 #ifdef PNG_BENIGN_ERRORS_SUPPORTED
1425 png_set_benign_errors(png_ptr, 1/*warn*/);
1426 #endif
1427 png_read_info(png_ptr, info_ptr);
1428
1429 /* Do this the fast way; just read directly out of png_struct. */
1430 image->width = png_ptr->width;
1431 image->height = png_ptr->height;
1432
1433 {
1434 png_uint_32 format = png_image_format(png_ptr);
1435
1436 image->format = format;
1437
1438 #ifdef PNG_COLORSPACE_SUPPORTED
1439 /* Does the colorspace match sRGB? If there is no color endpoint
1440 * (colorant) information assume yes, otherwise require the
1441 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
1442 * colorspace has been determined to be invalid ignore it.
1443 */
1444 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
1445 & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB|
1446 PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS))
1447 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1448 #endif
1449 }
1450
1451 /* We need the maximum number of entries regardless of the format the
1452 * application sets here.
1453 */
1454 {
1455 png_uint_32 cmap_entries;
1456
1457 switch (png_ptr->color_type)
1458 {
1459 case PNG_COLOR_TYPE_GRAY:
1460 cmap_entries = 1U << png_ptr->bit_depth;
1461 break;
1462
1463 case PNG_COLOR_TYPE_PALETTE:
1464 cmap_entries = (png_uint_32)png_ptr->num_palette;
1465 break;
1466
1467 default:
1468 cmap_entries = 256;
1469 break;
1470 }
1471
1472 if (cmap_entries > 256)
1473 cmap_entries = 256;
1474
1475 image->colormap_entries = cmap_entries;
1476 }
1477
1478 return 1;
1479 }
1480
1481 #ifdef PNG_STDIO_SUPPORTED
1482 int PNGAPI
1483 png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1484 {
1979 }
1980 }
1981
1982 # ifdef afirst
1983 # undef afirst
1984 # endif
1985 # ifdef bgr
1986 # undef bgr
1987 # endif
1988 }
1989 }
1990
1991 static int
1992 make_gray_file_colormap(png_image_read_control *display)
1993 {
1994 unsigned int i;
1995
1996 for (i=0; i<256; ++i)
1997 png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
1998
1999 return (int)i;
2000 }
2001
2002 static int
2003 make_gray_colormap(png_image_read_control *display)
2004 {
2005 unsigned int i;
2006
2007 for (i=0; i<256; ++i)
2008 png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
2009
2010 return (int)i;
2011 }
2012 #define PNG_GRAY_COLORMAP_ENTRIES 256
2013
2014 static int
2015 make_ga_colormap(png_image_read_control *display)
2016 {
2017 unsigned int i, a;
2018
2019 /* Alpha is retained, the output will be a color-map with entries
2020 * selected by six levels of alpha. One transparent entry, 6 gray
2021 * levels for all the intermediate alpha values, leaving 230 entries
2022 * for the opaque grays. The color-map entries are the six values
2023 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
2024 * relevant entry.
2025 *
2026 * if (alpha > 229) // opaque
2027 * {
2028 * // The 231 entries are selected to make the math below work:
2029 * base = 0;
2030 * entry = (231 * gray + 128) >> 8;
2044 while (i < 231)
2045 {
2046 unsigned int gray = (i * 256 + 115) / 231;
2047 png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
2048 }
2049
2050 /* 255 is used here for the component values for consistency with the code
2051 * that undoes premultiplication in pngwrite.c.
2052 */
2053 png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
2054
2055 for (a=1; a<5; ++a)
2056 {
2057 unsigned int g;
2058
2059 for (g=0; g<6; ++g)
2060 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
2061 P_sRGB);
2062 }
2063
2064 return (int)i;
2065 }
2066
2067 #define PNG_GA_COLORMAP_ENTRIES 256
2068
2069 static int
2070 make_rgb_colormap(png_image_read_control *display)
2071 {
2072 unsigned int i, r;
2073
2074 /* Build a 6x6x6 opaque RGB cube */
2075 for (i=r=0; r<6; ++r)
2076 {
2077 unsigned int g;
2078
2079 for (g=0; g<6; ++g)
2080 {
2081 unsigned int b;
2082
2083 for (b=0; b<6; ++b)
2084 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
2085 P_sRGB);
2086 }
2087 }
2088
2089 return (int)i;
2090 }
2091
2092 #define PNG_RGB_COLORMAP_ENTRIES 216
2093
2094 /* Return a palette index to the above palette given three 8-bit sRGB values. */
2095 #define PNG_RGB_INDEX(r,g,b) \
2096 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
2097
2098 static int
2099 png_image_read_colormap(png_voidp argument)
2100 {
2101 png_image_read_control *display =
2102 png_voidcast(png_image_read_control*, argument);
2103 const png_imagep image = display->image;
2104
2105 const png_structrp png_ptr = image->opaque->png_ptr;
2106 const png_uint_32 output_format = image->format;
2107 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
2108 P_LINEAR : P_sRGB;
2109
2117 unsigned int background_index = 256;
2118 png_uint_32 back_r, back_g, back_b;
2119
2120 /* Flags to accumulate things that need to be done to the input. */
2121 int expand_tRNS = 0;
2122
2123 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
2124 * very difficult to do, the results look awful, and it is difficult to see
2125 * what possible use it is because the application can't control the
2126 * color-map.
2127 */
2128 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
2129 png_ptr->num_trans > 0) /* alpha in input */ &&
2130 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
2131 {
2132 if (output_encoding == P_LINEAR) /* compose on black */
2133 back_b = back_g = back_r = 0;
2134
2135 else if (display->background == NULL /* no way to remove it */)
2136 png_error(png_ptr,
2137 "background color must be supplied to remove alpha/transparency");
2138
2139 /* Get a copy of the background color (this avoids repeating the checks
2140 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
2141 * output format.
2142 */
2143 else
2144 {
2145 back_g = display->background->green;
2146 if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0)
2147 {
2148 back_r = display->background->red;
2149 back_b = display->background->blue;
2150 }
2151 else
2152 back_b = back_r = back_g;
2153 }
2154 }
2155
2156 else if (output_encoding == P_LINEAR)
2157 back_b = back_r = back_g = 65535;
2262 * encoded values; however, if a tRNS chunk is present 257 color-map
2263 * entries are required. This means that the extra entry requires
2264 * special processing; add an alpha channel, sacrifice gray level
2265 * 254 and convert transparent (alpha==0) entries to that.
2266 *
2267 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2268 * same time to minimize quality loss. If a tRNS chunk is present
2269 * this means libpng must handle it too; otherwise it is impossible
2270 * to do the exact match on the 16-bit value.
2271 *
2272 * If the output has no alpha channel *and* the background color is
2273 * gray then it is possible to let libpng handle the substitution by
2274 * ensuring that the corresponding gray level matches the background
2275 * color exactly.
2276 */
2277 data_encoding = P_sRGB;
2278
2279 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2280 png_error(png_ptr, "gray[16] color-map: too few entries");
2281
2282 cmap_entries = (unsigned int)make_gray_colormap(display);
2283
2284 if (png_ptr->num_trans > 0)
2285 {
2286 unsigned int back_alpha;
2287
2288 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2289 back_alpha = 0;
2290
2291 else
2292 {
2293 if (back_r == back_g && back_g == back_b)
2294 {
2295 /* Background is gray; no special processing will be
2296 * required.
2297 */
2298 png_color_16 c;
2299 png_uint_32 gray = back_g;
2300
2301 if (output_encoding == P_LINEAR)
2302 {
2360 break;
2361
2362 case PNG_COLOR_TYPE_GRAY_ALPHA:
2363 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2364 * of 65536 combinations. If, however, the alpha channel is to be
2365 * removed there are only 256 possibilities if the background is gray.
2366 * (Otherwise there is a subset of the 65536 possibilities defined by
2367 * the triangle between black, white and the background color.)
2368 *
2369 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2370 * worry about tRNS matching - tRNS is ignored if there is an alpha
2371 * channel.
2372 */
2373 data_encoding = P_sRGB;
2374
2375 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2376 {
2377 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2378 png_error(png_ptr, "gray+alpha color-map: too few entries");
2379
2380 cmap_entries = (unsigned int)make_ga_colormap(display);
2381
2382 background_index = PNG_CMAP_GA_BACKGROUND;
2383 output_processing = PNG_CMAP_GA;
2384 }
2385
2386 else /* alpha is removed */
2387 {
2388 /* Alpha must be removed as the PNG data is processed when the
2389 * background is a color because the G and A channels are
2390 * independent and the vector addition (non-parallel vectors) is a
2391 * 2-D problem.
2392 *
2393 * This can be reduced to the same algorithm as above by making a
2394 * colormap containing gray levels (for the opaque grays), a
2395 * background entry (for a transparent pixel) and a set of four six
2396 * level color values, one set for each intermediate alpha value.
2397 * See the comments in make_ga_colormap for how this works in the
2398 * per-pixel processing.
2399 *
2400 * If the background is gray, however, we only need a 256 entry gray
2401 * level color map. It is sufficient to make the entry generated
2402 * for the background color be exactly the color specified.
2403 */
2404 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
2405 (back_r == back_g && back_g == back_b))
2406 {
2407 /* Background is gray; no special processing will be required. */
2408 png_color_16 c;
2409 png_uint_32 gray = back_g;
2410
2411 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2412 png_error(png_ptr, "gray-alpha color-map: too few entries");
2413
2414 cmap_entries = (unsigned int)make_gray_colormap(display);
2415
2416 if (output_encoding == P_LINEAR)
2417 {
2418 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2419
2420 /* And make sure the corresponding palette entry matches. */
2421 png_create_colormap_entry(display, gray, back_g, back_g,
2422 back_g, 65535, P_LINEAR);
2423 }
2424
2425 /* The background passed to libpng, however, must be the sRGB
2426 * value.
2427 */
2428 c.index = 0; /*unused*/
2429 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2430
2431 png_set_background_fixed(png_ptr, &c,
2432 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2433 0/*gamma: not used*/);
2434
2442 /* This is the same as png_make_ga_colormap, above, except that
2443 * the entries are all opaque.
2444 */
2445 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2446 png_error(png_ptr, "ga-alpha color-map: too few entries");
2447
2448 i = 0;
2449 while (i < 231)
2450 {
2451 png_uint_32 gray = (i * 256 + 115) / 231;
2452 png_create_colormap_entry(display, i++, gray, gray, gray,
2453 255, P_sRGB);
2454 }
2455
2456 /* NOTE: this preserves the full precision of the application
2457 * background color.
2458 */
2459 background_index = i;
2460 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
2461 #ifdef __COVERITY__
2462 /* Coverity claims that output_encoding
2463 * cannot be 2 (P_LINEAR) here.
2464 */ 255U,
2465 #else
2466 output_encoding == P_LINEAR ? 65535U : 255U,
2467 #endif
2468 output_encoding);
2469
2470 /* For non-opaque input composite on the sRGB background - this
2471 * requires inverting the encoding for each component. The input
2472 * is still converted to the sRGB encoding because this is a
2473 * reasonable approximate to the logarithmic curve of human
2474 * visual sensitivity, at least over the narrow range which PNG
2475 * represents. Consequently 'G' is always sRGB encoded, while
2476 * 'A' is linear. We need the linear background colors.
2477 */
2478 if (output_encoding == P_sRGB) /* else already linear */
2479 {
2480 /* This may produce a value not exactly matching the
2481 * background, but that's ok because these numbers are only
2482 * used when alpha != 0
2483 */
2533 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2534 -1);
2535 data_encoding = P_sRGB;
2536
2537 /* The output will now be one or two 8-bit gray or gray+alpha
2538 * channels. The more complex case arises when the input has alpha.
2539 */
2540 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2541 png_ptr->num_trans > 0) &&
2542 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2543 {
2544 /* Both input and output have an alpha channel, so no background
2545 * processing is required; just map the GA bytes to the right
2546 * color-map entry.
2547 */
2548 expand_tRNS = 1;
2549
2550 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2551 png_error(png_ptr, "rgb[ga] color-map: too few entries");
2552
2553 cmap_entries = (unsigned int)make_ga_colormap(display);
2554 background_index = PNG_CMAP_GA_BACKGROUND;
2555 output_processing = PNG_CMAP_GA;
2556 }
2557
2558 else
2559 {
2560 /* Either the input or the output has no alpha channel, so there
2561 * will be no non-opaque pixels in the color-map; it will just be
2562 * grayscale.
2563 */
2564 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2565 png_error(png_ptr, "rgb[gray] color-map: too few entries");
2566
2567 /* Ideally this code would use libpng to do the gamma correction,
2568 * but if an input alpha channel is to be removed we will hit the
2569 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2570 * correction bug). Fix this by dropping the gamma correction in
2571 * this case and doing it in the palette; this will result in
2572 * duplicate palette entries, but that's better than the
2573 * alternative of double gamma correction.
2574 */
2575 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2576 png_ptr->num_trans > 0) &&
2577 png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0)
2578 {
2579 cmap_entries = (unsigned int)make_gray_file_colormap(display);
2580 data_encoding = P_FILE;
2581 }
2582
2583 else
2584 cmap_entries = (unsigned int)make_gray_colormap(display);
2585
2586 /* But if the input has alpha or transparency it must be removed
2587 */
2588 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2589 png_ptr->num_trans > 0)
2590 {
2591 png_color_16 c;
2592 png_uint_32 gray = back_g;
2593
2594 /* We need to ensure that the application background exists in
2595 * the colormap and that completely transparent pixels map to
2596 * it. Achieve this simply by ensuring that the entry
2597 * selected for the background really is the background color.
2598 */
2599 if (data_encoding == P_FILE) /* from the fixup above */
2600 {
2601 /* The app supplied a gray which is in output_encoding, we
2602 * need to convert it to a value of the input (P_FILE)
2603 * encoding then set this palette entry to the required
2604 * output encoding.
2652 * color or alpha; png_quantize ignores alpha. Easier overall just
2653 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2654 * Consequently we always want libpng to produce sRGB data.
2655 */
2656 data_encoding = P_sRGB;
2657
2658 /* Is there any transparency or alpha? */
2659 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2660 png_ptr->num_trans > 0)
2661 {
2662 /* Is there alpha in the output too? If so all four channels are
2663 * processed into a special RGB cube with alpha support.
2664 */
2665 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2666 {
2667 png_uint_32 r;
2668
2669 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2670 png_error(png_ptr, "rgb+alpha color-map: too few entries");
2671
2672 cmap_entries = (unsigned int)make_rgb_colormap(display);
2673
2674 /* Add a transparent entry. */
2675 png_create_colormap_entry(display, cmap_entries, 255, 255,
2676 255, 0, P_sRGB);
2677
2678 /* This is stored as the background index for the processing
2679 * algorithm.
2680 */
2681 background_index = cmap_entries++;
2682
2683 /* Add 27 r,g,b entries each with alpha 0.5. */
2684 for (r=0; r<256; r = (r << 1) | 0x7f)
2685 {
2686 png_uint_32 g;
2687
2688 for (g=0; g<256; g = (g << 1) | 0x7f)
2689 {
2690 png_uint_32 b;
2691
2692 /* This generates components with the values 0, 127 and
2701 expand_tRNS = 1;
2702 output_processing = PNG_CMAP_RGB_ALPHA;
2703 }
2704
2705 else
2706 {
2707 /* Alpha/transparency must be removed. The background must
2708 * exist in the color map (achieved by setting adding it after
2709 * the 666 color-map). If the standard processing code will
2710 * pick up this entry automatically that's all that is
2711 * required; libpng can be called to do the background
2712 * processing.
2713 */
2714 unsigned int sample_size =
2715 PNG_IMAGE_SAMPLE_SIZE(output_format);
2716 png_uint_32 r, g, b; /* sRGB background */
2717
2718 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2719 png_error(png_ptr, "rgb-alpha color-map: too few entries");
2720
2721 cmap_entries = (unsigned int)make_rgb_colormap(display);
2722
2723 png_create_colormap_entry(display, cmap_entries, back_r,
2724 back_g, back_b, 0/*unused*/, output_encoding);
2725
2726 if (output_encoding == P_LINEAR)
2727 {
2728 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2729 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2730 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2731 }
2732
2733 else
2734 {
2735 r = back_r;
2736 g = back_g;
2737 b = back_g;
2738 }
2739
2740 /* Compare the newly-created color-map entry with the one the
2741 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2786 c.gray = c.green = (png_uint_16)back_g;
2787 c.blue = (png_uint_16)back_b;
2788
2789 png_set_background_fixed(png_ptr, &c,
2790 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2791 0/*gamma: not used*/);
2792
2793 output_processing = PNG_CMAP_RGB;
2794 }
2795 }
2796 }
2797
2798 else /* no alpha or transparency in the input */
2799 {
2800 /* Alpha in the output is irrelevant, simply map the opaque input
2801 * pixels to the 6x6x6 color-map.
2802 */
2803 if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
2804 png_error(png_ptr, "rgb color-map: too few entries");
2805
2806 cmap_entries = (unsigned int)make_rgb_colormap(display);
2807 output_processing = PNG_CMAP_RGB;
2808 }
2809 }
2810 break;
2811
2812 case PNG_COLOR_TYPE_PALETTE:
2813 /* It's already got a color-map. It may be necessary to eliminate the
2814 * tRNS entries though.
2815 */
2816 {
2817 unsigned int num_trans = png_ptr->num_trans;
2818 png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
2819 png_const_colorp colormap = png_ptr->palette;
2820 const int do_background = trans != NULL &&
2821 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2822 unsigned int i;
2823
2824 /* Just in case: */
2825 if (trans == NULL)
2826 num_trans = 0;
2827
2828 output_processing = PNG_CMAP_NONE;
2829 data_encoding = P_FILE; /* Don't change from color-map indices */
2830 cmap_entries = (unsigned int)png_ptr->num_palette;
2831 if (cmap_entries > 256)
2832 cmap_entries = 256;
2833
2834 if (cmap_entries > (unsigned int)image->colormap_entries)
2835 png_error(png_ptr, "palette color-map: too few entries");
2836
2837 for (i=0; i < cmap_entries; ++i)
2838 {
2839 if (do_background != 0 && i < num_trans && trans[i] < 255)
2840 {
2841 if (trans[i] == 0)
2842 png_create_colormap_entry(display, i, back_r, back_g,
2843 back_b, 0, output_encoding);
2844
2845 else
2846 {
2847 /* Must compose the PNG file color in the color-map entry
2848 * on the sRGB color in 'back'.
2849 */
2850 png_create_colormap_entry(display, i,
2851 png_colormap_compose(display, colormap[i].red,
2852 P_FILE, trans[i], back_r, output_encoding),
2853 png_colormap_compose(display, colormap[i].green,
2854 P_FILE, trans[i], back_g, output_encoding),
2855 png_colormap_compose(display, colormap[i].blue,
2856 P_FILE, trans[i], back_b, output_encoding),
2857 output_encoding == P_LINEAR ? trans[i] * 257U :
2858 trans[i],
2859 output_encoding);
2860 }
2861 }
2862
2863 else
2864 png_create_colormap_entry(display, i, colormap[i].red,
2865 colormap[i].green, colormap[i].blue,
2866 i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
2867 }
2868
2869 /* The PNG data may have indices packed in fewer than 8 bits, it
2870 * must be expanded if so.
2871 */
2872 if (png_ptr->bit_depth < 8)
2873 png_set_packing(png_ptr);
2874 }
2875 break;
2876
2926 goto bad_background;
2927 break;
2928
2929 case PNG_CMAP_RGB:
2930 if (background_index != PNG_CMAP_RGB_BACKGROUND)
2931 goto bad_background;
2932 break;
2933
2934 case PNG_CMAP_RGB_ALPHA:
2935 if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
2936 goto bad_background;
2937 break;
2938
2939 default:
2940 png_error(png_ptr, "bad processing option (internal error)");
2941
2942 bad_background:
2943 png_error(png_ptr, "bad background index (internal error)");
2944 }
2945
2946 display->colormap_processing = (int)output_processing;
2947
2948 return 1/*ok*/;
2949 }
2950
2951 /* The final part of the color-map read called from png_image_finish_read. */
2952 static int
2953 png_image_read_and_map(png_voidp argument)
2954 {
2955 png_image_read_control *display = png_voidcast(png_image_read_control*,
2956 argument);
2957 png_imagep image = display->image;
2958 png_structrp png_ptr = image->opaque->png_ptr;
2959 int passes;
2960
2961 /* Called when the libpng data must be transformed into the color-mapped
2962 * form. There is a local row buffer in display->local and this routine must
2963 * do the interlace handling.
2964 */
2965 switch (png_ptr->interlaced)
2966 {
3235
3236 display->first_row = first_row;
3237 display->row_bytes = row_bytes;
3238 }
3239
3240 if (passes == 0)
3241 {
3242 int result;
3243 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
3244
3245 display->local_row = row;
3246 result = png_safe_execute(image, png_image_read_and_map, display);
3247 display->local_row = NULL;
3248 png_free(png_ptr, row);
3249
3250 return result;
3251 }
3252
3253 else
3254 {
3255 png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes;
3256
3257 while (--passes >= 0)
3258 {
3259 png_uint_32 y = image->height;
3260 png_bytep row = png_voidcast(png_bytep, display->first_row);
3261
3262 for (; y > 0; --y)
3263 {
3264 png_read_row(png_ptr, row, NULL);
3265 row += row_bytes;
3266 }
3267 }
3268
3269 return 1;
3270 }
3271 }
3272
3273 /* Just the row reading part of png_image_read. */
3274 static int
3275 png_image_read_composite(png_voidp argument)
3276 {
3277 png_image_read_control *display = png_voidcast(png_image_read_control*,
3278 argument);
3279 png_imagep image = display->image;
3280 png_structrp png_ptr = image->opaque->png_ptr;
3281 int passes;
3282
3445
3446 /* Use direct access to info_ptr here because otherwise the simplified API
3447 * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
3448 * checking the value after libpng expansions, not the original value in the
3449 * PNG.
3450 */
3451 switch (info_ptr->bit_depth)
3452 {
3453 case 8:
3454 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
3455 * to be removed by composing on a background: either the row if
3456 * display->background is NULL or display->background->green if not.
3457 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3458 */
3459 {
3460 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3461 ptrdiff_t step_row = display->row_bytes;
3462
3463 for (pass = 0; pass < passes; ++pass)
3464 {
3465 png_bytep row = png_voidcast(png_bytep, display->first_row);
3466 unsigned int startx, stepx, stepy;
3467 png_uint_32 y;
3468
3469 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3470 {
3471 /* The row may be empty for a short image: */
3472 if (PNG_PASS_COLS(width, pass) == 0)
3473 continue;
3474
3475 startx = PNG_PASS_START_COL(pass);
3476 stepx = PNG_PASS_COL_OFFSET(pass);
3477 y = PNG_PASS_START_ROW(pass);
3478 stepy = PNG_PASS_ROW_OFFSET(pass);
3479 }
3480
3481 else
3482 {
3483 y = 0;
3484 startx = 0;
3485 stepx = stepy = 1;
3570
3571 row += display->row_bytes;
3572 }
3573 }
3574 }
3575 }
3576 break;
3577
3578 case 16:
3579 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3580 * still be done and, maybe, the alpha channel removed. This code also
3581 * handles the alpha-first option.
3582 */
3583 {
3584 png_uint_16p first_row = png_voidcast(png_uint_16p,
3585 display->first_row);
3586 /* The division by two is safe because the caller passed in a
3587 * stride which was multiplied by 2 (below) to get row_bytes.
3588 */
3589 ptrdiff_t step_row = display->row_bytes / 2;
3590 unsigned int preserve_alpha = (image->format &
3591 PNG_FORMAT_FLAG_ALPHA) != 0;
3592 unsigned int outchannels = 1U+preserve_alpha;
3593 int swap_alpha = 0;
3594
3595 # ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
3596 if (preserve_alpha != 0 &&
3597 (image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
3598 swap_alpha = 1;
3599 # endif
3600
3601 for (pass = 0; pass < passes; ++pass)
3602 {
3603 unsigned int startx, stepx, stepy;
3604 png_uint_32 y;
3605
3606 /* The 'x' start and step are adjusted to output components here.
3607 */
3608 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3609 {
3610 /* The row may be empty for a short image: */
3611 if (PNG_PASS_COLS(width, pass) == 0)
3612 continue;
4069 png_free(png_ptr, row);
4070
4071 return result;
4072 }
4073
4074 else if (do_local_background == 2)
4075 {
4076 int result;
4077 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
4078
4079 display->local_row = row;
4080 result = png_safe_execute(image, png_image_read_background, display);
4081 display->local_row = NULL;
4082 png_free(png_ptr, row);
4083
4084 return result;
4085 }
4086
4087 else
4088 {
4089 png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes;
4090
4091 while (--passes >= 0)
4092 {
4093 png_uint_32 y = image->height;
4094 png_bytep row = png_voidcast(png_bytep, display->first_row);
4095
4096 for (; y > 0; --y)
4097 {
4098 png_read_row(png_ptr, row, NULL);
4099 row += row_bytes;
4100 }
4101 }
4102
4103 return 1;
4104 }
4105 }
4106
4107 int PNGAPI
4108 png_image_finish_read(png_imagep image, png_const_colorp background,
4109 void *buffer, png_int_32 row_stride, void *colormap)
4110 {
4111 if (image != NULL && image->version == PNG_IMAGE_VERSION)
4112 {
4113 /* Check for row_stride overflow. This check is not performed on the
4114 * original PNG format because it may not occur in the output PNG format
4115 * and libpng deals with the issues of reading the original.
4116 */
4117 const unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);
4118
4119 /* The following checks just the 'row_stride' calculation to ensure it
4120 * fits in a signed 32-bit value. Because channels/components can be
4121 * either 1 or 2 bytes in size the length of a row can still overflow 32
4122 * bits; this is just to verify that the 'row_stride' argument can be
4123 * represented.
4124 */
4125 if (image->width <= 0x7fffffffU/channels) /* no overflow */
4126 {
4127 png_uint_32 check;
4128 const png_uint_32 png_row_stride = image->width * channels;
4129
4130 if (row_stride == 0)
4131 row_stride = (png_int_32)/*SAFE*/png_row_stride;
4132
4133 if (row_stride < 0)
4134 check = (png_uint_32)(-row_stride);
4135
4136 else
4137 check = (png_uint_32)row_stride;
4138
4139 /* This verifies 'check', the absolute value of the actual stride
4140 * passed in and detects overflow in the application calculation (i.e.
4141 * if the app did actually pass in a non-zero 'row_stride'.
4142 */
4143 if (image->opaque != NULL && buffer != NULL && check >= png_row_stride)
4144 {
4145 /* Now check for overflow of the image buffer calculation; this
4146 * limits the whole image size to 32 bits for API compatibility with
4147 * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
4148 *
4149 * The PNG_IMAGE_BUFFER_SIZE macro is:
4150 *
4151 * (PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)*height*(row_stride))
4152 *
4153 * And the component size is always 1 or 2, so make sure that the
4154 * number of *bytes* that the application is saying are available
4155 * does actually fit into a 32-bit number.
4156 *
4157 * NOTE: this will be changed in 1.7 because PNG_IMAGE_BUFFER_SIZE
4158 * will be changed to use png_alloc_size_t; bigger images can be
4159 * accomodated on 64-bit systems.
4160 */
4161 if (image->height <=
4162 0xffffffffU/PNG_IMAGE_PIXEL_COMPONENT_SIZE(image->format)/check)
4163 {
4164 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
4165 (image->colormap_entries > 0 && colormap != NULL))
4166 {
4167 int result;
4168 png_image_read_control display;
4169
4170 memset(&display, 0, (sizeof display));
4171 display.image = image;
4172 display.buffer = buffer;
4173 display.row_stride = row_stride;
4174 display.colormap = colormap;
4175 display.background = background;
4176 display.local_row = NULL;
4177
4178 /* Choose the correct 'end' routine; for the color-map case
4179 * all the setup has already been done.
4180 */
4181 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0)
4182 result =
4183 png_safe_execute(image,
4184 png_image_read_colormap, &display) &&
4185 png_safe_execute(image,
4186 png_image_read_colormapped, &display);
4187
4188 else
4189 result =
4190 png_safe_execute(image,
4191 png_image_read_direct, &display);
4192
4193 png_image_free(image);
4194 return result;
4195 }
4196
4197 else
4198 return png_image_error(image,
4199 "png_image_finish_read[color-map]: no color-map");
4200 }
4201
4202 else
4203 return png_image_error(image,
|