< prev index next >

src/java.desktop/share/native/libsplashscreen/libpng/pngread.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 /* 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. */
  53 PNG_FUNCTION(png_structp,PNGAPI


 186 #endif
 187       else if (chunk_name == png_PLTE)
 188          png_handle_PLTE(png_ptr, info_ptr, length);
 189 
 190       else if (chunk_name == png_IDAT)
 191       {
 192          png_ptr->idat_size = length;
 193          break;
 194       }
 195 
 196 #ifdef PNG_READ_bKGD_SUPPORTED
 197       else if (chunk_name == png_bKGD)
 198          png_handle_bKGD(png_ptr, info_ptr, length);
 199 #endif
 200 
 201 #ifdef PNG_READ_cHRM_SUPPORTED
 202       else if (chunk_name == png_cHRM)
 203          png_handle_cHRM(png_ptr, info_ptr, length);
 204 #endif
 205 





 206 #ifdef PNG_READ_gAMA_SUPPORTED
 207       else if (chunk_name == png_gAMA)
 208          png_handle_gAMA(png_ptr, info_ptr, length);
 209 #endif
 210 
 211 #ifdef PNG_READ_hIST_SUPPORTED
 212       else if (chunk_name == png_hIST)
 213          png_handle_hIST(png_ptr, info_ptr, length);
 214 #endif
 215 
 216 #ifdef PNG_READ_oFFs_SUPPORTED
 217       else if (chunk_name == png_oFFs)
 218          png_handle_oFFs(png_ptr, info_ptr, length);
 219 #endif
 220 
 221 #ifdef PNG_READ_pCAL_SUPPORTED
 222       else if (chunk_name == png_pCAL)
 223          png_handle_pCAL(png_ptr, info_ptr, length);
 224 #endif
 225 


 545                return;
 546             }
 547             break;
 548 
 549          default:
 550          case 6:
 551             if ((png_ptr->row_number & 1) == 0)
 552             {
 553                png_read_finish_row(png_ptr);
 554                return;
 555             }
 556             break;
 557       }
 558    }
 559 #endif
 560 
 561    if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
 562       png_error(png_ptr, "Invalid attempt to read row data");
 563 
 564    /* Fill the row with IDAT data: */

 565    png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
 566 
 567    if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
 568    {
 569       if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
 570          png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
 571              png_ptr->prev_row + 1, png_ptr->row_buf[0]);
 572       else
 573          png_error(png_ptr, "bad adaptive filter value");
 574    }
 575 
 576    /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
 577     * 1.5.6, while the buffer really is this big in current versions of libpng
 578     * it may not be in the future, so this was changed just to copy the
 579     * interlaced count:
 580     */
 581    memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
 582 
 583 #ifdef PNG_MNG_FEATURES_SUPPORTED
 584    if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&


 853           */
 854          if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
 855              || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
 856             png_benign_error(png_ptr, "..Too many IDATs found");
 857 
 858          png_crc_finish(png_ptr, length);
 859       }
 860       else if (chunk_name == png_PLTE)
 861          png_handle_PLTE(png_ptr, info_ptr, length);
 862 
 863 #ifdef PNG_READ_bKGD_SUPPORTED
 864       else if (chunk_name == png_bKGD)
 865          png_handle_bKGD(png_ptr, info_ptr, length);
 866 #endif
 867 
 868 #ifdef PNG_READ_cHRM_SUPPORTED
 869       else if (chunk_name == png_cHRM)
 870          png_handle_cHRM(png_ptr, info_ptr, length);
 871 #endif
 872 





 873 #ifdef PNG_READ_gAMA_SUPPORTED
 874       else if (chunk_name == png_gAMA)
 875          png_handle_gAMA(png_ptr, info_ptr, length);
 876 #endif
 877 
 878 #ifdef PNG_READ_hIST_SUPPORTED
 879       else if (chunk_name == png_hIST)
 880          png_handle_hIST(png_ptr, info_ptr, length);
 881 #endif
 882 
 883 #ifdef PNG_READ_oFFs_SUPPORTED
 884       else if (chunk_name == png_oFFs)
 885          png_handle_oFFs(png_ptr, info_ptr, length);
 886 #endif
 887 
 888 #ifdef PNG_READ_pCAL_SUPPORTED
 889       else if (chunk_name == png_pCAL)
 890          png_handle_pCAL(png_ptr, info_ptr, length);
 891 #endif
 892 


1894 #     ifdef PNG_FORMAT_BGR_SUPPORTED
1895          const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
1896 #     else
1897 #        define bgr 0
1898 #     endif
1899 
1900       if (output_encoding == P_LINEAR)
1901       {
1902          png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
1903 
1904          entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1905 
1906          /* The linear 16-bit values must be pre-multiplied by the alpha channel
1907           * value, if less than 65535 (this is, effectively, composite on black
1908           * if the alpha channel is removed.)
1909           */
1910          switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1911          {
1912             case 4:
1913                entry[afirst ? 0 : 3] = (png_uint_16)alpha;
1914                /* FALL THROUGH */
1915 
1916             case 3:
1917                if (alpha < 65535)
1918                {
1919                   if (alpha > 0)
1920                   {
1921                      blue = (blue * alpha + 32767U)/65535U;
1922                      green = (green * alpha + 32767U)/65535U;
1923                      red = (red * alpha + 32767U)/65535U;
1924                   }
1925 
1926                   else
1927                      red = green = blue = 0;
1928                }
1929                entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
1930                entry[afirst + 1] = (png_uint_16)green;
1931                entry[afirst + bgr] = (png_uint_16)red;
1932                break;
1933 
1934             case 2:
1935                entry[1 ^ afirst] = (png_uint_16)alpha;
1936                /* FALL THROUGH */
1937 
1938             case 1:
1939                if (alpha < 65535)
1940                {
1941                   if (alpha > 0)
1942                      green = (green * alpha + 32767U)/65535U;
1943 
1944                   else
1945                      green = 0;
1946                }
1947                entry[afirst] = (png_uint_16)green;
1948                break;
1949 
1950             default:
1951                break;
1952          }
1953       }
1954 
1955       else /* output encoding is P_sRGB */
1956       {
1957          png_bytep entry = png_voidcast(png_bytep, display->colormap);
1958 
1959          entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1960 
1961          switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1962          {
1963             case 4:
1964                entry[afirst ? 0 : 3] = (png_byte)alpha;

1965             case 3:
1966                entry[afirst + (2 ^ bgr)] = (png_byte)blue;
1967                entry[afirst + 1] = (png_byte)green;
1968                entry[afirst + bgr] = (png_byte)red;
1969                break;
1970 
1971             case 2:
1972                entry[1 ^ afirst] = (png_byte)alpha;

1973             case 1:
1974                entry[afirst] = (png_byte)green;
1975                break;
1976 
1977             default:
1978                break;
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)


2872             if (png_ptr->bit_depth < 8)
2873                png_set_packing(png_ptr);
2874          }
2875          break;
2876 
2877       default:
2878          png_error(png_ptr, "invalid PNG color type");
2879          /*NOT REACHED*/
2880    }
2881 
2882    /* Now deal with the output processing */
2883    if (expand_tRNS != 0 && png_ptr->num_trans > 0 &&
2884        (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
2885       png_set_tRNS_to_alpha(png_ptr);
2886 
2887    switch (data_encoding)
2888    {
2889       case P_sRGB:
2890          /* Change to 8-bit sRGB */
2891          png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
2892          /* FALL THROUGH */
2893 
2894       case P_FILE:
2895          if (png_ptr->bit_depth > 8)
2896             png_set_scale_16(png_ptr);
2897          break;
2898 
2899 #ifdef __GNUC__
2900       default:
2901          png_error(png_ptr, "bad data option (internal error)");
2902 #endif
2903    }
2904 
2905    if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
2906       png_error(png_ptr, "color map overflow (BAD internal error)");
2907 
2908    image->colormap_entries = cmap_entries;
2909 
2910    /* Double check using the recorded background index */
2911    switch (output_processing)
2912    {


3190          goto bad_output;
3191 
3192       case PNG_CMAP_RGB:
3193          /* Output must be 8-bit sRGB encoded RGB */
3194          if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
3195             info_ptr->bit_depth == 8 &&
3196             png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3197             image->colormap_entries == 216)
3198             break;
3199 
3200          goto bad_output;
3201 
3202       case PNG_CMAP_RGB_ALPHA:
3203          /* Output must be 8-bit sRGB encoded RGBA */
3204          if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
3205             info_ptr->bit_depth == 8 &&
3206             png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3207             image->colormap_entries == 244 /* 216 + 1 + 27 */)
3208             break;
3209 
3210          /* goto bad_output; */
3211          /* FALL THROUGH */
3212 
3213       default:
3214       bad_output:
3215          png_error(png_ptr, "bad color-map processing (internal error)");
3216    }
3217 
3218    /* Now read the rows.  Do this here if it is possible to read directly into
3219     * the output buffer, otherwise allocate a local row buffer of the maximum
3220     * size libpng requires and call the relevant processing routine safely.
3221     */
3222    {
3223       png_voidp first_row = display->buffer;
3224       ptrdiff_t row_bytes = display->row_stride;
3225 
3226       /* The following expression is designed to work correctly whether it gives
3227        * a signed or an unsigned result.
3228        */
3229       if (row_bytes < 0)
3230       {
3231          char *ptr = png_voidcast(char*, first_row);


3759       if (linear != 0)
3760       {
3761          /* If there *is* an alpha channel in the input it must be multiplied
3762           * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3763           */
3764          if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3765             mode = PNG_ALPHA_STANDARD; /* associated alpha */
3766 
3767          else
3768             mode = PNG_ALPHA_PNG;
3769 
3770          output_gamma = PNG_GAMMA_LINEAR;
3771       }
3772 
3773       else
3774       {
3775          mode = PNG_ALPHA_PNG;
3776          output_gamma = PNG_DEFAULT_sRGB;
3777       }
3778 






3779       /* If 'do_local_background' is set check for the presence of gamma
3780        * correction; this is part of the work-round for the libpng bug
3781        * described above.
3782        *
3783        * TODO: fix libpng and remove this.
3784        */
3785       if (do_local_background != 0)
3786       {
3787          png_fixed_point gtest;
3788 
3789          /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3790           * gamma correction, the screen gamma hasn't been set on png_struct
3791           * yet; it's set below.  png_struct::gamma, however, is set to the
3792           * final value.
3793           */
3794          if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
3795              PNG_FP_1) != 0 && png_gamma_significant(gtest) == 0)
3796             do_local_background = 0;
3797 
3798          else if (mode == PNG_ALPHA_STANDARD)


3983    {
3984       png_uint_32 info_format = 0;
3985 
3986       if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
3987          info_format |= PNG_FORMAT_FLAG_COLOR;
3988 
3989       if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
3990       {
3991          /* do_local_compose removes this channel below. */
3992          if (do_local_compose == 0)
3993          {
3994             /* do_local_background does the same if required. */
3995             if (do_local_background != 2 ||
3996                (format & PNG_FORMAT_FLAG_ALPHA) != 0)
3997                info_format |= PNG_FORMAT_FLAG_ALPHA;
3998          }
3999       }
4000 
4001       else if (do_local_compose != 0) /* internal error */
4002          png_error(png_ptr, "png_image_read: alpha channel lost");




4003 
4004       if (info_ptr->bit_depth == 16)
4005          info_format |= PNG_FORMAT_FLAG_LINEAR;
4006 
4007 #ifdef PNG_FORMAT_BGR_SUPPORTED
4008       if ((png_ptr->transformations & PNG_BGR) != 0)
4009          info_format |= PNG_FORMAT_FLAG_BGR;
4010 #endif
4011 
4012 #ifdef PNG_FORMAT_AFIRST_SUPPORTED
4013          if (do_local_background == 2)
4014          {
4015             if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
4016                info_format |= PNG_FORMAT_FLAG_AFIRST;
4017          }
4018 
4019          if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
4020             ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
4021             (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
4022          {




  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.33 [September 28, 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  * 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. */
  53 PNG_FUNCTION(png_structp,PNGAPI


 186 #endif
 187       else if (chunk_name == png_PLTE)
 188          png_handle_PLTE(png_ptr, info_ptr, length);
 189 
 190       else if (chunk_name == png_IDAT)
 191       {
 192          png_ptr->idat_size = length;
 193          break;
 194       }
 195 
 196 #ifdef PNG_READ_bKGD_SUPPORTED
 197       else if (chunk_name == png_bKGD)
 198          png_handle_bKGD(png_ptr, info_ptr, length);
 199 #endif
 200 
 201 #ifdef PNG_READ_cHRM_SUPPORTED
 202       else if (chunk_name == png_cHRM)
 203          png_handle_cHRM(png_ptr, info_ptr, length);
 204 #endif
 205 
 206 #ifdef PNG_READ_eXIf_SUPPORTED
 207       else if (chunk_name == png_eXIf)
 208          png_handle_eXIf(png_ptr, info_ptr, length);
 209 #endif
 210 
 211 #ifdef PNG_READ_gAMA_SUPPORTED
 212       else if (chunk_name == png_gAMA)
 213          png_handle_gAMA(png_ptr, info_ptr, length);
 214 #endif
 215 
 216 #ifdef PNG_READ_hIST_SUPPORTED
 217       else if (chunk_name == png_hIST)
 218          png_handle_hIST(png_ptr, info_ptr, length);
 219 #endif
 220 
 221 #ifdef PNG_READ_oFFs_SUPPORTED
 222       else if (chunk_name == png_oFFs)
 223          png_handle_oFFs(png_ptr, info_ptr, length);
 224 #endif
 225 
 226 #ifdef PNG_READ_pCAL_SUPPORTED
 227       else if (chunk_name == png_pCAL)
 228          png_handle_pCAL(png_ptr, info_ptr, length);
 229 #endif
 230 


 550                return;
 551             }
 552             break;
 553 
 554          default:
 555          case 6:
 556             if ((png_ptr->row_number & 1) == 0)
 557             {
 558                png_read_finish_row(png_ptr);
 559                return;
 560             }
 561             break;
 562       }
 563    }
 564 #endif
 565 
 566    if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
 567       png_error(png_ptr, "Invalid attempt to read row data");
 568 
 569    /* Fill the row with IDAT data: */
 570    png_ptr->row_buf[0]=255; /* to force error if no data was found */
 571    png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
 572 
 573    if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
 574    {
 575       if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
 576          png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
 577              png_ptr->prev_row + 1, png_ptr->row_buf[0]);
 578       else
 579          png_error(png_ptr, "bad adaptive filter value");
 580    }
 581 
 582    /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
 583     * 1.5.6, while the buffer really is this big in current versions of libpng
 584     * it may not be in the future, so this was changed just to copy the
 585     * interlaced count:
 586     */
 587    memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
 588 
 589 #ifdef PNG_MNG_FEATURES_SUPPORTED
 590    if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&


 859           */
 860          if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
 861              || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
 862             png_benign_error(png_ptr, "..Too many IDATs found");
 863 
 864          png_crc_finish(png_ptr, length);
 865       }
 866       else if (chunk_name == png_PLTE)
 867          png_handle_PLTE(png_ptr, info_ptr, length);
 868 
 869 #ifdef PNG_READ_bKGD_SUPPORTED
 870       else if (chunk_name == png_bKGD)
 871          png_handle_bKGD(png_ptr, info_ptr, length);
 872 #endif
 873 
 874 #ifdef PNG_READ_cHRM_SUPPORTED
 875       else if (chunk_name == png_cHRM)
 876          png_handle_cHRM(png_ptr, info_ptr, length);
 877 #endif
 878 
 879 #ifdef PNG_READ_eXIf_SUPPORTED
 880       else if (chunk_name == png_eXIf)
 881          png_handle_eXIf(png_ptr, info_ptr, length);
 882 #endif
 883 
 884 #ifdef PNG_READ_gAMA_SUPPORTED
 885       else if (chunk_name == png_gAMA)
 886          png_handle_gAMA(png_ptr, info_ptr, length);
 887 #endif
 888 
 889 #ifdef PNG_READ_hIST_SUPPORTED
 890       else if (chunk_name == png_hIST)
 891          png_handle_hIST(png_ptr, info_ptr, length);
 892 #endif
 893 
 894 #ifdef PNG_READ_oFFs_SUPPORTED
 895       else if (chunk_name == png_oFFs)
 896          png_handle_oFFs(png_ptr, info_ptr, length);
 897 #endif
 898 
 899 #ifdef PNG_READ_pCAL_SUPPORTED
 900       else if (chunk_name == png_pCAL)
 901          png_handle_pCAL(png_ptr, info_ptr, length);
 902 #endif
 903 


1905 #     ifdef PNG_FORMAT_BGR_SUPPORTED
1906          const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
1907 #     else
1908 #        define bgr 0
1909 #     endif
1910 
1911       if (output_encoding == P_LINEAR)
1912       {
1913          png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
1914 
1915          entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1916 
1917          /* The linear 16-bit values must be pre-multiplied by the alpha channel
1918           * value, if less than 65535 (this is, effectively, composite on black
1919           * if the alpha channel is removed.)
1920           */
1921          switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1922          {
1923             case 4:
1924                entry[afirst ? 0 : 3] = (png_uint_16)alpha;
1925                /* FALLTHROUGH */
1926 
1927             case 3:
1928                if (alpha < 65535)
1929                {
1930                   if (alpha > 0)
1931                   {
1932                      blue = (blue * alpha + 32767U)/65535U;
1933                      green = (green * alpha + 32767U)/65535U;
1934                      red = (red * alpha + 32767U)/65535U;
1935                   }
1936 
1937                   else
1938                      red = green = blue = 0;
1939                }
1940                entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
1941                entry[afirst + 1] = (png_uint_16)green;
1942                entry[afirst + bgr] = (png_uint_16)red;
1943                break;
1944 
1945             case 2:
1946                entry[1 ^ afirst] = (png_uint_16)alpha;
1947                /* FALLTHROUGH */
1948 
1949             case 1:
1950                if (alpha < 65535)
1951                {
1952                   if (alpha > 0)
1953                      green = (green * alpha + 32767U)/65535U;
1954 
1955                   else
1956                      green = 0;
1957                }
1958                entry[afirst] = (png_uint_16)green;
1959                break;
1960 
1961             default:
1962                break;
1963          }
1964       }
1965 
1966       else /* output encoding is P_sRGB */
1967       {
1968          png_bytep entry = png_voidcast(png_bytep, display->colormap);
1969 
1970          entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1971 
1972          switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1973          {
1974             case 4:
1975                entry[afirst ? 0 : 3] = (png_byte)alpha;
1976                /* FALLTHROUGH */
1977             case 3:
1978                entry[afirst + (2 ^ bgr)] = (png_byte)blue;
1979                entry[afirst + 1] = (png_byte)green;
1980                entry[afirst + bgr] = (png_byte)red;
1981                break;
1982 
1983             case 2:
1984                entry[1 ^ afirst] = (png_byte)alpha;
1985                /* FALLTHROUGH */
1986             case 1:
1987                entry[afirst] = (png_byte)green;
1988                break;
1989 
1990             default:
1991                break;
1992          }
1993       }
1994 
1995 #     ifdef afirst
1996 #        undef afirst
1997 #     endif
1998 #     ifdef bgr
1999 #        undef bgr
2000 #     endif
2001    }
2002 }
2003 
2004 static int
2005 make_gray_file_colormap(png_image_read_control *display)


2885             if (png_ptr->bit_depth < 8)
2886                png_set_packing(png_ptr);
2887          }
2888          break;
2889 
2890       default:
2891          png_error(png_ptr, "invalid PNG color type");
2892          /*NOT REACHED*/
2893    }
2894 
2895    /* Now deal with the output processing */
2896    if (expand_tRNS != 0 && png_ptr->num_trans > 0 &&
2897        (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
2898       png_set_tRNS_to_alpha(png_ptr);
2899 
2900    switch (data_encoding)
2901    {
2902       case P_sRGB:
2903          /* Change to 8-bit sRGB */
2904          png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
2905          /* FALLTHROUGH */
2906 
2907       case P_FILE:
2908          if (png_ptr->bit_depth > 8)
2909             png_set_scale_16(png_ptr);
2910          break;
2911 
2912 #ifdef __GNUC__
2913       default:
2914          png_error(png_ptr, "bad data option (internal error)");
2915 #endif
2916    }
2917 
2918    if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
2919       png_error(png_ptr, "color map overflow (BAD internal error)");
2920 
2921    image->colormap_entries = cmap_entries;
2922 
2923    /* Double check using the recorded background index */
2924    switch (output_processing)
2925    {


3203          goto bad_output;
3204 
3205       case PNG_CMAP_RGB:
3206          /* Output must be 8-bit sRGB encoded RGB */
3207          if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
3208             info_ptr->bit_depth == 8 &&
3209             png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3210             image->colormap_entries == 216)
3211             break;
3212 
3213          goto bad_output;
3214 
3215       case PNG_CMAP_RGB_ALPHA:
3216          /* Output must be 8-bit sRGB encoded RGBA */
3217          if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
3218             info_ptr->bit_depth == 8 &&
3219             png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3220             image->colormap_entries == 244 /* 216 + 1 + 27 */)
3221             break;
3222 
3223          goto bad_output;

3224 
3225       default:
3226       bad_output:
3227          png_error(png_ptr, "bad color-map processing (internal error)");
3228    }
3229 
3230    /* Now read the rows.  Do this here if it is possible to read directly into
3231     * the output buffer, otherwise allocate a local row buffer of the maximum
3232     * size libpng requires and call the relevant processing routine safely.
3233     */
3234    {
3235       png_voidp first_row = display->buffer;
3236       ptrdiff_t row_bytes = display->row_stride;
3237 
3238       /* The following expression is designed to work correctly whether it gives
3239        * a signed or an unsigned result.
3240        */
3241       if (row_bytes < 0)
3242       {
3243          char *ptr = png_voidcast(char*, first_row);


3771       if (linear != 0)
3772       {
3773          /* If there *is* an alpha channel in the input it must be multiplied
3774           * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3775           */
3776          if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3777             mode = PNG_ALPHA_STANDARD; /* associated alpha */
3778 
3779          else
3780             mode = PNG_ALPHA_PNG;
3781 
3782          output_gamma = PNG_GAMMA_LINEAR;
3783       }
3784 
3785       else
3786       {
3787          mode = PNG_ALPHA_PNG;
3788          output_gamma = PNG_DEFAULT_sRGB;
3789       }
3790 
3791       if ((change & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0)
3792       {
3793          mode = PNG_ALPHA_OPTIMIZED;
3794          change &= ~PNG_FORMAT_FLAG_ASSOCIATED_ALPHA;
3795       }
3796 
3797       /* If 'do_local_background' is set check for the presence of gamma
3798        * correction; this is part of the work-round for the libpng bug
3799        * described above.
3800        *
3801        * TODO: fix libpng and remove this.
3802        */
3803       if (do_local_background != 0)
3804       {
3805          png_fixed_point gtest;
3806 
3807          /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3808           * gamma correction, the screen gamma hasn't been set on png_struct
3809           * yet; it's set below.  png_struct::gamma, however, is set to the
3810           * final value.
3811           */
3812          if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
3813              PNG_FP_1) != 0 && png_gamma_significant(gtest) == 0)
3814             do_local_background = 0;
3815 
3816          else if (mode == PNG_ALPHA_STANDARD)


4001    {
4002       png_uint_32 info_format = 0;
4003 
4004       if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
4005          info_format |= PNG_FORMAT_FLAG_COLOR;
4006 
4007       if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
4008       {
4009          /* do_local_compose removes this channel below. */
4010          if (do_local_compose == 0)
4011          {
4012             /* do_local_background does the same if required. */
4013             if (do_local_background != 2 ||
4014                (format & PNG_FORMAT_FLAG_ALPHA) != 0)
4015                info_format |= PNG_FORMAT_FLAG_ALPHA;
4016          }
4017       }
4018 
4019       else if (do_local_compose != 0) /* internal error */
4020          png_error(png_ptr, "png_image_read: alpha channel lost");
4021 
4022       if ((format & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0) {
4023          info_format |= PNG_FORMAT_FLAG_ASSOCIATED_ALPHA;
4024       }
4025 
4026       if (info_ptr->bit_depth == 16)
4027          info_format |= PNG_FORMAT_FLAG_LINEAR;
4028 
4029 #ifdef PNG_FORMAT_BGR_SUPPORTED
4030       if ((png_ptr->transformations & PNG_BGR) != 0)
4031          info_format |= PNG_FORMAT_FLAG_BGR;
4032 #endif
4033 
4034 #ifdef PNG_FORMAT_AFIRST_SUPPORTED
4035          if (do_local_background == 2)
4036          {
4037             if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
4038                info_format |= PNG_FORMAT_FLAG_AFIRST;
4039          }
4040 
4041          if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
4042             ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
4043             (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
4044          {


< prev index next >