Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 362637
Collapse All | Expand All

(-)file_not_specified_in_diff (-438 / +376 lines)
Line  Link Here
0
-- a/coders/png.c
0
++ b/coders/png.c
Lines 88-113 Link Here
88
#include "zlib.h"
88
#include "zlib.h"
89
89
90
90
91
/*
91
#if PNG_LIBPNG_VER > 10011
92
 * TO DO: rewrite using png_get_tRNS() instead of direct access to the
93
 * ping and ping_info structs.
94
 */
95
#if PNG_LIBPNG_VER < 10400
96
#    define trans_color  trans_values   /* Changed at libpng-1.4.0beta35 */
97
#    define trans_alpha  trans          /* Changed at libpng-1.4.0beta74 */
98
#else
99
   /* We could parse PNG_LIBPNG_VER_STRING here but it's too much bother..
100
    * Just don't use libpng-1.4.0beta32-34 or beta67-73
101
    */
102
#  ifndef  PNG_USER_CHUNK_CACHE_MAX     /* Added at libpng-1.4.0beta32 */
103
#    define trans_color  trans_values   /* Changed at libpng-1.4.0beta35 */
104
#  endif
105
#  ifndef  PNG_TRANSFORM_GRAY_TO_RGB    /* Added at libpng-1.4.0beta67 */
106
#    define trans_alpha  trans          /* Changed at libpng-1.4.0beta74 */
107
#  endif
108
#endif
109
110
#if PNG_LIBPNG_VER > 95
111
/*
92
/*
112
  Optional declarations. Define or undefine them as you like.
93
  Optional declarations. Define or undefine them as you like.
113
*/
94
*/
Lines 164-173 static SemaphoreInfo Link Here
164
  PNG_MNG_FEATURES_SUPPORTED is disabled by default in libpng-1.0.9 and
145
  PNG_MNG_FEATURES_SUPPORTED is disabled by default in libpng-1.0.9 and
165
  will be enabled by default in libpng-1.2.0.
146
  will be enabled by default in libpng-1.2.0.
166
*/
147
*/
167
#if (PNG_LIBPNG_VER == 10009)  /* work around libpng-1.0.9 bug */
168
#  undef PNG_READ_EMPTY_PLTE_SUPPORTED
169
#  undef PNG_WRITE_EMPTY_PLTE_SUPPORTED
170
#endif
171
#ifdef PNG_MNG_FEATURES_SUPPORTED
148
#ifdef PNG_MNG_FEATURES_SUPPORTED
172
#  ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
149
#  ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
173
#    define PNG_READ_EMPTY_PLTE_SUPPORTED
150
#    define PNG_READ_EMPTY_PLTE_SUPPORTED
Lines 492-498 static const char* PngColorTypeToString( Link Here
492
  return result;
469
  return result;
493
}
470
}
494
471
495
#if PNG_LIBPNG_VER > 95
496
#if defined(PNG_SORT_PALETTE)
472
#if defined(PNG_SORT_PALETTE)
497
/*
473
/*
498
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Lines 785-791 static MagickPassFail CompressColormapTr Link Here
785
  return(MagickPass);
761
  return(MagickPass);
786
}
762
}
787
#endif
763
#endif
788
#endif /* PNG_LIBPNG_VER > 95 */
789
#endif /* HasPNG */
764
#endif /* HasPNG */
790
765
791
/*
766
/*
Lines 907-913 static MagickPassFail IsPNG(const unsign Link Here
907
extern "C" {
882
extern "C" {
908
#endif
883
#endif
909
884
910
#if (PNG_LIBPNG_VER > 95)
885
#if (PNG_LIBPNG_VER > 10011)
911
static size_t WriteBlobMSBULong(Image *image,const unsigned long value)
886
static size_t WriteBlobMSBULong(Image *image,const unsigned long value)
912
{
887
{
913
  unsigned char
888
  unsigned char
Lines 956-968 static void LogPNGChunk(int logging, png Link Here
956
        "  Writing %c%c%c%c chunk, length: %lu",
931
        "  Writing %c%c%c%c chunk, length: %lu",
957
        type[0],type[1],type[2],type[3],length);
932
        type[0],type[1],type[2],type[3],length);
958
}
933
}
959
#endif /* PNG_LIBPNG_VER > 95 */
934
#endif /* PNG_LIBPNG_VER > 10011 */
960
935
961
#if defined(__cplusplus) || defined(c_plusplus)
936
#if defined(__cplusplus) || defined(c_plusplus)
962
}
937
}
963
#endif
938
#endif
964
939
965
#if PNG_LIBPNG_VER > 95
940
#if PNG_LIBPNG_VER > 10011
966
/*
941
/*
967
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
942
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
968
%                                                                             %
943
%                                                                             %
Lines 1351-1357 static void PNGErrorHandler(png_struct * Link Here
1351
                        "  libpng-%.1024s error: %.1024s",
1326
                        "  libpng-%.1024s error: %.1024s",
1352
                        PNG_LIBPNG_VER_STRING, message);
1327
                        PNG_LIBPNG_VER_STRING, message);
1353
  (void) ThrowException2(&image->exception,CoderError,message,image->filename);
1328
  (void) ThrowException2(&image->exception,CoderError,message,image->filename);
1329
#if (PNG_LIBPNG_VER < 10500)
1354
  longjmp(ping->jmpbuf,1);
1330
  longjmp(ping->jmpbuf,1);
1331
#else
1332
  png_longjmp(ping,1);
1333
#endif
1355
}
1334
}
1356
1335
1357
static void PNGWarningHandler(png_struct *ping,png_const_charp message)
1336
static void PNGWarningHandler(png_struct *ping,png_const_charp message)
Lines 1372-1390 static void PNGWarningHandler(png_struct Link Here
1372
#ifdef PNG_USER_MEM_SUPPORTED
1351
#ifdef PNG_USER_MEM_SUPPORTED
1373
static png_voidp png_IM_malloc(png_structp png_ptr,png_uint_32 size)
1352
static png_voidp png_IM_malloc(png_structp png_ptr,png_uint_32 size)
1374
{
1353
{
1375
#if (PNG_LIBPNG_VER < 10011)
1376
  png_voidp
1377
    ret;
1378
  
1379
  png_ptr=png_ptr;
1380
  ret=MagickAllocateMemory(png_voidp,(size_t) size);
1381
  if (ret == NULL)
1382
    png_error("Insufficient memory.");
1383
  return (ret);
1384
#else
1385
  png_ptr=png_ptr;
1354
  png_ptr=png_ptr;
1386
  return MagickAllocateMemory(png_voidp,(size_t) size);
1355
  return MagickAllocateMemory(png_voidp,(size_t) size);
1387
#endif
1388
}
1356
}
1389
1357
1390
/*
1358
/*
Lines 1560-1570 static Image *ReadOnePNGImage(MngInfo *m Link Here
1560
    logging,
1528
    logging,
1561
    num_text,
1529
    num_text,
1562
    num_passes,
1530
    num_passes,
1563
    pass;
1531
    pass,
1532
    ping_info_bit_depth,
1533
    ping_info_color_type,
1534
    ping_info_interlace_method,
1535
    ping_info_compression_method,
1536
    ping_info_filter_method,
1537
    ping_info_num_trans;
1564
1538
1565
  PixelPacket
1539
  PixelPacket
1566
    transparent_color;
1540
    transparent_color;
1567
1541
1542
  png_bytep
1543
     ping_trans_alpha;
1544
1545
  png_color_16p
1546
     ping_background,
1547
     ping_trans_color;
1548
1568
  png_info
1549
  png_info
1569
    *end_info,
1550
    *end_info,
1570
    *ping_info;
1551
    *ping_info;
Lines 1572-1577 static Image *ReadOnePNGImage(MngInfo *m Link Here
1572
  png_struct
1553
  png_struct
1573
    *ping;
1554
    *ping;
1574
1555
1556
  png_uint_32
1557
    ping_rowbytes,
1558
    ping_info_width,
1559
    ping_info_height;
1560
1575
  png_textp
1561
  png_textp
1576
    text;
1562
    text;
1577
1563
Lines 1619-1641 static Image *ReadOnePNGImage(MngInfo *m Link Here
1619
  LockSemaphoreInfo(png_semaphore);
1605
  LockSemaphoreInfo(png_semaphore);
1620
#endif
1606
#endif
1621
1607
1622
#if (PNG_LIBPNG_VER < 10007)
1608
#if (PNG_LIBPNG_VER < 10012)
1623
  if (image_info->verbose)
1609
  if (image_info->verbose)
1624
    printf("Your PNG library (libpng-%s) is rather old.\n",
1610
    printf("Your PNG library (libpng-%s) is rather old.\n",
1625
           PNG_LIBPNG_VER_STRING);
1611
           PNG_LIBPNG_VER_STRING);
1626
#endif
1612
#endif
1627
1613
1628
#if (PNG_LIBPNG_VER >= 10400)
1629
#  ifndef  PNG_TRANSFORM_GRAY_TO_RGB    /* Added at libpng-1.4.0beta67 */
1630
  if (image_info->verbose)
1631
    {
1632
      printf("Your PNG library (libpng-%s) is an old beta version.\n",
1633
           PNG_LIBPNG_VER_STRING);
1634
      printf("Please update it.\n");
1635
    }
1636
#  endif
1637
#endif
1638
1639
  image=mng_info->image;
1614
  image=mng_info->image;
1640
1615
1641
  /*
1616
  /*
Lines 1665-1671 static Image *ReadOnePNGImage(MngInfo *m Link Here
1665
      ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
1640
      ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
1666
    }
1641
    }
1667
  png_pixels=(unsigned char *) NULL;
1642
  png_pixels=(unsigned char *) NULL;
1668
  if (setjmp(ping->jmpbuf))
1643
  if (setjmp(png_jmpbuf(ping)))
1669
    {
1644
    {
1670
      /*
1645
      /*
1671
        PNG image is corrupt.
1646
        PNG image is corrupt.
Lines 1740-1757 static Image *ReadOnePNGImage(MngInfo *m Link Here
1740
1715
1741
  png_read_info(ping,ping_info);
1716
  png_read_info(ping,ping_info);
1742
1717
1718
  (void) png_get_IHDR(ping,ping_info,
1719
                      &ping_info_width,
1720
                      &ping_info_height,
1721
                      &ping_info_bit_depth,
1722
                      &ping_info_color_type,
1723
                      &ping_info_interlace_method,
1724
                      &ping_info_compression_method,
1725
                      &ping_info_filter_method);
1726
1727
  (void) png_get_tRNS(ping, ping_info, &ping_trans_alpha, &ping_info_num_trans,
1728
                      &ping_trans_color);
1729
1730
  (void) png_get_bKGD(ping, ping_info, &ping_background);
1731
1732
  ping_rowbytes=png_get_rowbytes(ping,ping_info);
1733
1743
#if (QuantumDepth == 8)
1734
#if (QuantumDepth == 8)
1744
  image->depth=8;
1735
  image->depth=8;
1745
#else
1736
#else
1746
  if (ping_info->bit_depth > 8)
1737
  if (ping_info_bit_depth > 8)
1747
    image->depth=16;
1738
    image->depth=16;
1748
  else
1739
  else
1749
    image->depth=8;
1740
    image->depth=8;
1750
#endif
1741
#endif
1751
1742
1752
  if (ping_info->bit_depth < 8)
1743
  if (ping_info_bit_depth < 8)
1753
    {
1744
    {
1754
      if ((ping_info->color_type == PNG_COLOR_TYPE_PALETTE))
1745
      if (ping_info_color_type == PNG_COLOR_TYPE_PALETTE)
1755
        {
1746
        {
1756
          png_set_packing(ping);
1747
          png_set_packing(ping);
1757
          image->depth=8;
1748
          image->depth=8;
Lines 1761-1781 static Image *ReadOnePNGImage(MngInfo *m Link Here
1761
    {
1752
    {
1762
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1753
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1763
                            "    PNG width: %lu, height: %lu",
1754
                            "    PNG width: %lu, height: %lu",
1764
                            (unsigned long)ping_info->width,
1755
                            (unsigned long)ping_info_width,
1765
                            (unsigned long)ping_info->height);
1756
                            (unsigned long)ping_info_height);
1766
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1757
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1767
                            "    PNG color_type: %d, bit_depth: %d",
1758
                            "    PNG color_type: %d, bit_depth: %d",
1768
                            ping_info->color_type, ping_info->bit_depth);
1759
                            ping_info_color_type, ping_info_bit_depth);
1769
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1760
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1770
                            "    PNG compression_method: %d",
1761
                            "    PNG compression_method: %d",
1771
                            ping_info->compression_type);
1762
                            ping_info_compression_method);
1772
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1763
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1773
                            "    PNG interlace_method: %d, filter_method: %d",
1764
                            "    PNG interlace_method: %d, filter_method: %d",
1774
                            ping_info->interlace_type,ping_info->filter_type);
1765
                            ping_info_interlace_method,
1766
                            ping_info_filter_method);
1775
    }
1767
    }
1776
1768
1777
#if (PNG_LIBPNG_VER > 10008) && defined(PNG_READ_iCCP_SUPPORTED)
1769
#if defined(PNG_READ_iCCP_SUPPORTED)
1778
  if (ping_info->valid & PNG_INFO_iCCP)
1770
    if (png_get_valid(ping, ping_info, PNG_INFO_iCCP))
1779
    {
1771
    {
1780
      int
1772
      int
1781
        compression;
1773
        compression;
Lines 1803-1817 static Image *ReadOnePNGImage(MngInfo *m Link Here
1803
            }
1795
            }
1804
        }
1796
        }
1805
    }
1797
    }
1806
#endif /* #if (PNG_LIBPNG_VER > 10008) && defined(PNG_READ_iCCP_SUPPORTED) */
1798
#endif /* #if defined(PNG_READ_iCCP_SUPPORTED) */
1807
#if defined(PNG_READ_sRGB_SUPPORTED)
1799
#if defined(PNG_READ_sRGB_SUPPORTED)
1808
  {
1800
  {
1809
    int
1801
    int
1810
      intent;
1802
      intent;
1811
1803
1812
    if (mng_info->have_global_srgb)
1804
    if (!png_get_sRGB(ping,ping_info,&intent))
1813
      image->rendering_intent=(RenderingIntent)
1805
      {
1814
        (mng_info->global_srgb_intent+1);
1806
        if (mng_info->have_global_srgb)
1807
          {
1808
            png_set_sRGB(ping,ping_info,(mng_info->global_srgb_intent+1));
1809
          }
1810
      }
1815
    if (png_get_sRGB(ping,ping_info,&intent))
1811
    if (png_get_sRGB(ping,ping_info,&intent))
1816
      {
1812
      {
1817
        image->rendering_intent=(RenderingIntent) (intent+1);
1813
        image->rendering_intent=(RenderingIntent) (intent+1);
Lines 1827-1834 static Image *ReadOnePNGImage(MngInfo *m Link Here
1827
    double
1823
    double
1828
      file_gamma;
1824
      file_gamma;
1829
1825
1830
    if (mng_info->have_global_gama)
1826
    if (!png_get_gAMA(ping,ping_info,&file_gamma))
1831
      image->gamma=mng_info->global_gamma;
1827
      {
1828
        if (mng_info->have_global_gama)
1829
          png_set_gAMA(ping,ping_info,mng_info->global_gamma);
1830
      }
1832
    if (png_get_gAMA(ping,ping_info,&file_gamma))
1831
    if (png_get_gAMA(ping,ping_info,&file_gamma))
1833
      {
1832
      {
1834
        image->gamma=(float) file_gamma;
1833
        image->gamma=(float) file_gamma;
Lines 1838-1846 static Image *ReadOnePNGImage(MngInfo *m Link Here
1838
                                file_gamma);
1837
                                file_gamma);
1839
      }
1838
      }
1840
  }
1839
  }
1841
  if (mng_info->have_global_chrm)
1840
  if (!png_get_valid(ping, ping_info, PNG_INFO_cHRM))
1842
    image->chromaticity=mng_info->global_chrm;
1841
    {
1843
  if (ping_info->valid & PNG_INFO_cHRM)
1842
      if (mng_info->have_global_chrm)
1843
        (void) png_set_cHRM(ping,ping_info,
1844
                     mng_info->global_chrm.white_point.x,
1845
                     mng_info->global_chrm.white_point.y,
1846
                     mng_info->global_chrm.red_primary.x,
1847
                     mng_info->global_chrm.red_primary.y,
1848
                     mng_info->global_chrm.green_primary.x,
1849
                     mng_info->global_chrm.green_primary.y,
1850
                     mng_info->global_chrm.blue_primary.x,
1851
                     mng_info->global_chrm.blue_primary.y);
1852
    }
1853
  if (png_get_valid(ping, ping_info, PNG_INFO_cHRM))
1844
    {
1854
    {
1845
      (void) png_get_cHRM(ping,ping_info,
1855
      (void) png_get_cHRM(ping,ping_info,
1846
                          &image->chromaticity.white_point.x,
1856
                          &image->chromaticity.white_point.x,
Lines 1867-1878 static Image *ReadOnePNGImage(MngInfo *m Link Here
1867
      image->chromaticity.white_point.x=0.3127f;
1877
      image->chromaticity.white_point.x=0.3127f;
1868
      image->chromaticity.white_point.y=0.3290f;
1878
      image->chromaticity.white_point.y=0.3290f;
1869
    }
1879
    }
1870
  if (mng_info->have_global_gama || image->rendering_intent)
1871
    ping_info->valid|=PNG_INFO_gAMA;
1872
  if (mng_info->have_global_chrm || image->rendering_intent)
1873
    ping_info->valid|=PNG_INFO_cHRM;
1874
#if defined(PNG_oFFs_SUPPORTED)
1880
#if defined(PNG_oFFs_SUPPORTED)
1875
  if (mng_info->mng_type == 0 && (ping_info->valid & PNG_INFO_oFFs))
1881
  if (mng_info->mng_type == 0 && (png_get_valid(ping, ping_info,
1882
                                                PNG_INFO_oFFs)))
1876
    {
1883
    {
1877
      image->page.x=png_get_x_offset_pixels(ping, ping_info);
1884
      image->page.x=png_get_x_offset_pixels(ping, ping_info);
1878
      image->page.y=png_get_y_offset_pixels(ping, ping_info);
1885
      image->page.y=png_get_y_offset_pixels(ping, ping_info);
Lines 1885-1891 static Image *ReadOnePNGImage(MngInfo *m Link Here
1885
    }
1892
    }
1886
#endif
1893
#endif
1887
#if defined(PNG_pHYs_SUPPORTED)
1894
#if defined(PNG_pHYs_SUPPORTED)
1888
  if (ping_info->valid & PNG_INFO_pHYs)
1895
  if (!png_get_valid(ping, ping_info, PNG_INFO_pHYs))
1896
    {
1897
      if (mng_info->have_global_phys)
1898
        {
1899
          png_set_pHYs(ping,ping_info,
1900
                       mng_info->global_x_pixels_per_unit,
1901
                       mng_info->global_y_pixels_per_unit,
1902
                       mng_info->global_phys_unit_type);
1903
        }
1904
    }
1905
  if (png_get_valid(ping, ping_info, PNG_INFO_pHYs))
1889
    {
1906
    {
1890
      int
1907
      int
1891
        unit_type;
1908
        unit_type;
Lines 1915-1939 static Image *ReadOnePNGImage(MngInfo *m Link Here
1915
                              (unsigned long)y_resolution,
1932
                              (unsigned long)y_resolution,
1916
                              unit_type);
1933
                              unit_type);
1917
    }
1934
    }
1918
  else
1919
    {
1920
      if (mng_info->have_global_phys)
1921
        {
1922
          image->x_resolution=(float) mng_info->global_x_pixels_per_unit;
1923
          image->y_resolution=(float) mng_info->global_y_pixels_per_unit;
1924
          if (mng_info->global_phys_unit_type == PNG_RESOLUTION_METER)
1925
            {
1926
              image->units=PixelsPerCentimeterResolution;
1927
              image->x_resolution=(double)
1928
                mng_info->global_x_pixels_per_unit/100.0;
1929
              image->y_resolution=(double)
1930
                mng_info->global_y_pixels_per_unit/100.0;
1931
            }
1932
          ping_info->valid|=PNG_INFO_pHYs;
1933
        }
1934
    }
1935
#endif
1935
#endif
1936
  if (ping_info->valid & PNG_INFO_PLTE)
1936
  if (png_get_valid(ping, ping_info, PNG_INFO_PLTE))
1937
    {
1937
    {
1938
      int
1938
      int
1939
        number_colors;
1939
        number_colors;
Lines 1942-1955 static Image *ReadOnePNGImage(MngInfo *m Link Here
1942
        palette;
1942
        palette;
1943
1943
1944
      (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
1944
      (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
1945
      if (number_colors == 0 && ping_info->color_type ==
1945
      if (number_colors == 0 && ping_info_color_type ==
1946
          PNG_COLOR_TYPE_PALETTE)
1946
          PNG_COLOR_TYPE_PALETTE)
1947
        {
1947
        {
1948
          if (mng_info->global_plte_length)
1948
          if (mng_info->global_plte_length)
1949
            {
1949
            {
1950
              png_set_PLTE(ping,ping_info,mng_info->global_plte,
1950
              png_set_PLTE(ping,ping_info,mng_info->global_plte,
1951
                           (int) mng_info->global_plte_length);
1951
                           (int) mng_info->global_plte_length);
1952
              if (!(ping_info->valid & PNG_INFO_tRNS))
1952
              if (!(png_get_valid(ping, ping_info, PNG_INFO_tRNS)))
1953
                if (mng_info->global_trns_length)
1953
                if (mng_info->global_trns_length)
1954
                  {
1954
                  {
1955
                    if (mng_info->global_trns_length >
1955
                    if (mng_info->global_trns_length >
Lines 1966-1972 static Image *ReadOnePNGImage(MngInfo *m Link Here
1966
#ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
1966
#ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
1967
                  mng_info->have_saved_bkgd_index ||
1967
                  mng_info->have_saved_bkgd_index ||
1968
#endif
1968
#endif
1969
                  ping_info->valid & PNG_INFO_bKGD)
1969
                  png_get_valid(ping, ping_info, PNG_INFO_bKGD))
1970
                {
1970
                {
1971
                  png_color_16
1971
                  png_color_16
1972
                    background;
1972
                    background;
Lines 1974-1982 static Image *ReadOnePNGImage(MngInfo *m Link Here
1974
#ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
1974
#ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
1975
                  if (mng_info->have_saved_bkgd_index)
1975
                  if (mng_info->have_saved_bkgd_index)
1976
                    background.index=mng_info->saved_bkgd_index;
1976
                    background.index=mng_info->saved_bkgd_index;
1977
                  else
1978
#endif
1977
#endif
1979
                    background.index=ping_info->background.index;
1978
                  if (png_get_valid(ping, ping_info, PNG_INFO_bKGD))
1979
                    {
1980
                      background.index=ping_background->index;
1981
                    }
1980
                  background.red=(png_uint_16)
1982
                  background.red=(png_uint_16)
1981
                    mng_info->global_plte[background.index].red;
1983
                    mng_info->global_plte[background.index].red;
1982
                  background.green=(png_uint_16)
1984
                  background.green=(png_uint_16)
Lines 1995-2028 static Image *ReadOnePNGImage(MngInfo *m Link Here
1995
    }
1997
    }
1996
1998
1997
#if defined(PNG_READ_bKGD_SUPPORTED)
1999
#if defined(PNG_READ_bKGD_SUPPORTED)
1998
  if (mng_info->have_global_bkgd && !(ping_info->valid & PNG_INFO_bKGD))
2000
  if (mng_info->have_global_bkgd && 
2001
              !(png_get_valid(ping,ping_info, PNG_INFO_bKGD)))
1999
    image->background_color=mng_info->mng_global_bkgd;
2002
    image->background_color=mng_info->mng_global_bkgd;
2000
  if (ping_info->valid & PNG_INFO_bKGD)
2003
  if (png_get_valid(ping, ping_info, PNG_INFO_bKGD))
2001
    {
2004
    {
2002
      /*
2005
      /*
2003
        Set image background color.
2006
        Set image background color.
2004
      */
2007
      */
2008
2005
      if (logging)
2009
      if (logging)
2006
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2010
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2007
                              "    Reading PNG bKGD chunk.");
2011
                              "    Reading PNG bKGD chunk.");
2008
      if (ping_info->bit_depth <= QuantumDepth)
2012
2013
      if (ping_info_bit_depth <= QuantumDepth)
2009
        {
2014
        {
2010
          image->background_color.red=ping_info->background.red;
2015
          image->background_color.red  = ping_background->red;
2011
          image->background_color.green=ping_info->background.green;
2016
          image->background_color.green= ping_background->green;
2012
          image->background_color.blue=ping_info->background.blue;
2017
          image->background_color.blue = ping_background->blue;
2013
        }
2018
        }
2014
      else
2019
      else
2015
        {
2020
        {
2016
          image->background_color.red=
2021
          image->background_color.red=
2017
            ScaleShortToQuantum(ping_info->background.red);
2022
            ScaleShortToQuantum(ping_background->red);
2018
          image->background_color.green=
2023
          image->background_color.green=
2019
            ScaleShortToQuantum(ping_info->background.green);
2024
            ScaleShortToQuantum(ping_background->green);
2020
          image->background_color.blue=
2025
          image->background_color.blue=
2021
            ScaleShortToQuantum(ping_info->background.blue);
2026
            ScaleShortToQuantum(ping_background->blue);
2022
        }
2027
        }
2023
    }
2028
    }
2024
#endif
2029
#endif
2025
  if (ping_info->valid & PNG_INFO_tRNS)
2030
  if (png_get_valid(ping, ping_info, PNG_INFO_tRNS))
2026
    {
2031
    {
2027
      int
2032
      int
2028
        bit_mask;
2033
        bit_mask;
Lines 2031-2050 static Image *ReadOnePNGImage(MngInfo *m Link Here
2031
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2036
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2032
          "    Reading PNG tRNS chunk.");
2037
          "    Reading PNG tRNS chunk.");
2033
2038
2034
      bit_mask = (1 << ping_info->bit_depth) - 1;
2039
      bit_mask = (1 << ping_info_bit_depth) - 1;
2035
2040
2036
      /*
2041
      /*
2037
        Image has a transparent background.
2042
        Image has a transparent background.
2038
      */
2043
      */
2044
2039
      transparent_color.red=
2045
      transparent_color.red=
2040
        (Quantum)(ping_info->trans_color.red & bit_mask);
2046
        (Quantum)(ping_trans_color->red & bit_mask);
2041
      transparent_color.green=
2047
      transparent_color.green=
2042
        (Quantum) (ping_info->trans_color.green & bit_mask);
2048
        (Quantum) (ping_trans_color->green & bit_mask);
2043
      transparent_color.blue=
2049
      transparent_color.blue=
2044
        (Quantum) (ping_info->trans_color.blue & bit_mask);
2050
        (Quantum) (ping_trans_color->blue & bit_mask);
2045
      transparent_color.opacity=
2051
      transparent_color.opacity=
2046
        (Quantum) (ping_info->trans_color.gray & bit_mask);
2052
        (Quantum) (ping_trans_color->gray & bit_mask);
2047
      if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
2053
      if (ping_info_color_type == PNG_COLOR_TYPE_GRAY)
2048
        {
2054
        {
2049
          transparent_color.red=transparent_color.opacity;
2055
          transparent_color.red=transparent_color.opacity;
2050
          transparent_color.green=transparent_color.opacity;
2056
          transparent_color.green=transparent_color.opacity;
Lines 2052-2065 static Image *ReadOnePNGImage(MngInfo *m Link Here
2052
        }
2058
        }
2053
    }
2059
    }
2054
#if defined(PNG_READ_sBIT_SUPPORTED)
2060
#if defined(PNG_READ_sBIT_SUPPORTED)
2055
  if (mng_info->have_global_sbit)
2061
  if (!png_get_valid(ping, ping_info, PNG_INFO_sBIT))
2056
    {
2062
    if (mng_info->have_global_sbit)
2057
      int
2058
        not_valid;
2059
      not_valid=!ping_info->valid;
2060
      if (not_valid & PNG_INFO_sBIT)
2061
        png_set_sBIT(ping,ping_info,&mng_info->global_sbit);
2063
        png_set_sBIT(ping,ping_info,&mng_info->global_sbit);
2062
    }
2063
#endif
2064
#endif
2064
  num_passes=png_set_interlace_handling(ping);
2065
  num_passes=png_set_interlace_handling(ping);
2065
  png_read_update_info(ping,ping_info);
2066
  png_read_update_info(ping,ping_info);
Lines 2067-2079 static Image *ReadOnePNGImage(MngInfo *m Link Here
2067
    Initialize image structure.
2068
    Initialize image structure.
2068
  */
2069
  */
2069
  mng_info->image_box.left=0;
2070
  mng_info->image_box.left=0;
2070
  mng_info->image_box.right=(long) ping_info->width;
2071
  mng_info->image_box.right=(long) ping_info_width;
2071
  mng_info->image_box.top=0;
2072
  mng_info->image_box.top=0;
2072
  mng_info->image_box.bottom=(long) ping_info->height;
2073
  mng_info->image_box.bottom=(long) ping_info_height;
2073
  if (mng_info->mng_type == 0)
2074
  if (mng_info->mng_type == 0)
2074
    {
2075
    {
2075
      mng_info->mng_width=ping_info->width;
2076
      mng_info->mng_width=ping_info_width;
2076
      mng_info->mng_height=ping_info->height;
2077
      mng_info->mng_height=ping_info_height;
2077
      mng_info->frame=mng_info->image_box;
2078
      mng_info->frame=mng_info->image_box;
2078
      mng_info->clip=mng_info->image_box;
2079
      mng_info->clip=mng_info->image_box;
2079
    }
2080
    }
Lines 2082-2095 static Image *ReadOnePNGImage(MngInfo *m Link Here
2082
      image->page.y=mng_info->y_off[mng_info->object_id];
2083
      image->page.y=mng_info->y_off[mng_info->object_id];
2083
    }
2084
    }
2084
  image->compression=ZipCompression;
2085
  image->compression=ZipCompression;
2085
  image->columns=ping_info->width;
2086
  image->columns=ping_info_width;
2086
  image->rows=ping_info->height;
2087
  image->rows=ping_info_height;
2087
  if ((ping_info->color_type == PNG_COLOR_TYPE_PALETTE) ||
2088
  if ((ping_info_color_type == PNG_COLOR_TYPE_PALETTE) ||
2088
      (ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
2089
      (ping_info_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
2089
      (ping_info->color_type == PNG_COLOR_TYPE_GRAY))
2090
      (ping_info_color_type == PNG_COLOR_TYPE_GRAY))
2090
    {
2091
    {
2091
      image->storage_class=PseudoClass;
2092
      image->storage_class=PseudoClass;
2092
      image->colors=1 << ping_info->bit_depth;
2093
      image->colors=1 << ping_info_bit_depth;
2093
#if (QuantumDepth == 8)
2094
#if (QuantumDepth == 8)
2094
      if (image->colors > 256)
2095
      if (image->colors > 256)
2095
        image->colors=256;
2096
        image->colors=256;
Lines 2097-2103 static Image *ReadOnePNGImage(MngInfo *m Link Here
2097
      if (image->colors > 65536L)
2098
      if (image->colors > 65536L)
2098
        image->colors=65536L;
2099
        image->colors=65536L;
2099
#endif
2100
#endif
2100
      if (ping_info->color_type == PNG_COLOR_TYPE_PALETTE)
2101
      if (ping_info_color_type == PNG_COLOR_TYPE_PALETTE)
2101
        {
2102
        {
2102
          int
2103
          int
2103
            number_colors;
2104
            number_colors;
Lines 2121-2127 static Image *ReadOnePNGImage(MngInfo *m Link Here
2121
      */
2122
      */
2122
      if (!AllocateImageColormap(image,image->colors))
2123
      if (!AllocateImageColormap(image,image->colors))
2123
        ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
2124
        ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
2124
      if (ping_info->color_type == PNG_COLOR_TYPE_PALETTE)
2125
      if (ping_info_color_type == PNG_COLOR_TYPE_PALETTE)
2125
        {
2126
        {
2126
          int
2127
          int
2127
            number_colors;
2128
            number_colors;
Lines 2142-2148 static Image *ReadOnePNGImage(MngInfo *m Link Here
2142
          unsigned long
2143
          unsigned long
2143
            scale;
2144
            scale;
2144
2145
2145
          scale=(MaxRGB/((1 << ping_info->bit_depth)-1));
2146
          scale=(MaxRGB/((1 << ping_info_bit_depth)-1));
2146
          if (scale < 1)
2147
          if (scale < 1)
2147
            scale=1;
2148
            scale=1;
2148
          for (i=0; i < (long) image->colors; i++)
2149
          for (i=0; i < (long) image->colors; i++)
Lines 2182-2191 static Image *ReadOnePNGImage(MngInfo *m Link Here
2182
                          "    Reading PNG IDAT chunk(s)");
2183
                          "    Reading PNG IDAT chunk(s)");
2183
  if (num_passes > 1)
2184
  if (num_passes > 1)
2184
    png_pixels=MagickAllocateMemory(unsigned char *,
2185
    png_pixels=MagickAllocateMemory(unsigned char *,
2185
                                    ping_info->rowbytes*image->rows);
2186
                                    ping_rowbytes*image->rows);
2186
  else
2187
  else
2187
    png_pixels=MagickAllocateMemory(unsigned char *,
2188
    png_pixels=MagickAllocateMemory(unsigned char *, ping_rowbytes);
2188
                                    ping_info->rowbytes);
2189
  if (png_pixels == (unsigned char *) NULL)
2189
  if (png_pixels == (unsigned char *) NULL)
2190
    ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
2190
    ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
2191
2191
Lines 2205-2224 static Image *ReadOnePNGImage(MngInfo *m Link Here
2205
        int
2205
        int
2206
          depth;
2206
          depth;
2207
2207
2208
        depth=(long) ping_info->bit_depth;
2208
        depth=(long) ping_info_bit_depth;
2209
#endif
2209
#endif
2210
        image->matte=((ping_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ||
2210
        image->matte=((ping_info_color_type == PNG_COLOR_TYPE_RGB_ALPHA) ||
2211
                      (ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
2211
                      (ping_info_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
2212
                      (ping_info->valid & PNG_INFO_tRNS));
2212
                      (png_get_valid(ping, ping_info, PNG_INFO_tRNS)));
2213
2213
2214
        for (y=0; y < (long) image->rows; y++)
2214
        for (y=0; y < (long) image->rows; y++)
2215
          {
2215
          {
2216
            if (num_passes > 1)
2216
            if (num_passes > 1)
2217
              row_offset=ping_info->rowbytes*y;
2217
              row_offset=ping_rowbytes*y;
2218
            else
2218
            else
2219
              row_offset=0;
2219
              row_offset=0;
2220
            png_read_row(ping,png_pixels+row_offset,NULL);
2220
            png_read_row(ping,png_pixels+row_offset,NULL);
2221
	    if (!SetImagePixels(image,0,y,image->columns,1))  /* Was GetImagePixels() */
2221
	    if (!SetImagePixels(image,0,y,image->columns,1))
2222
              break;
2222
              break;
2223
#if (QuantumDepth == 8)
2223
#if (QuantumDepth == 8)
2224
            if (depth == 16)
2224
            if (depth == 16)
Lines 2229-2241 static Image *ReadOnePNGImage(MngInfo *m Link Here
2229
2229
2230
                r=png_pixels+row_offset;
2230
                r=png_pixels+row_offset;
2231
                p=r;
2231
                p=r;
2232
                if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
2232
                if (ping_info_color_type == PNG_COLOR_TYPE_GRAY)
2233
                  {
2233
                  {
2234
                    for (x=(long) image->columns; x > 0; x--)
2234
                    for (x=(long) image->columns; x > 0; x--)
2235
                      {
2235
                      {
2236
                        *r++=*p++;
2236
                        *r++=*p++;
2237
                        p++;
2237
                        p++;
2238
                        if ((ping_info->valid & PNG_INFO_tRNS) &&
2238
                        if ((png_get_valid(ping, ping_info, PNG_INFO_tRNS)) &&
2239
                            (((*(p-2) << 8)|*(p-1))
2239
                            (((*(p-2) << 8)|*(p-1))
2240
                            == transparent_color.opacity))
2240
                            == transparent_color.opacity))
2241
                          {
2241
                          {
Lines 2246-2254 static Image *ReadOnePNGImage(MngInfo *m Link Here
2246
                          *r++=OpaqueOpacity;
2246
                          *r++=OpaqueOpacity;
2247
                      }
2247
                      }
2248
                  }
2248
                  }
2249
                else if (ping_info->color_type == PNG_COLOR_TYPE_RGB)
2249
                else if (ping_info_color_type == PNG_COLOR_TYPE_RGB)
2250
                  {
2250
                  {
2251
                    if (ping_info->valid & PNG_INFO_tRNS)
2251
                    if (png_get_valid(ping, ping_info, PNG_INFO_tRNS))
2252
                      for (x=(long) image->columns; x > 0; x--)
2252
                      for (x=(long) image->columns; x > 0; x--)
2253
                        {
2253
                        {
2254
                          *r++=*p++;
2254
                          *r++=*p++;
Lines 2282-2306 static Image *ReadOnePNGImage(MngInfo *m Link Here
2282
                          *r++=OpaqueOpacity;
2282
                          *r++=OpaqueOpacity;
2283
                        }
2283
                        }
2284
                  }
2284
                  }
2285
                else if (ping_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2285
                else if (ping_info_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2286
                  for (x=(long) (4*image->columns); x > 0; x--)
2286
                  for (x=(long) (4*image->columns); x > 0; x--)
2287
                    {
2287
                    {
2288
                      *r++=*p++;
2288
                      *r++=*p++;
2289
                      p++;
2289
                      p++;
2290
                    }
2290
                    }
2291
                else if (ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2291
                else if (ping_info_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2292
                  for (x=(long) (2*image->columns); x > 0; x--)
2292
                  for (x=(long) (2*image->columns); x > 0; x--)
2293
                    {
2293
                    {
2294
                      *r++=*p++;
2294
                      *r++=*p++;
2295
                      p++;
2295
                      p++;
2296
                    }
2296
                    }
2297
              }
2297
              }
2298
            if (depth == 8 && ping_info->color_type == PNG_COLOR_TYPE_GRAY)
2298
            if (depth == 8 && ping_info_color_type == PNG_COLOR_TYPE_GRAY)
2299
              (void) ImportImagePixelArea(image,(QuantumType) GrayQuantum,
2299
              (void) ImportImagePixelArea(image,(QuantumType) GrayQuantum,
2300
                                          image->depth,png_pixels+
2300
                                          image->depth,png_pixels+
2301
                                          row_offset,0,0);
2301
                                          row_offset,0,0);
2302
            if (ping_info->color_type == PNG_COLOR_TYPE_GRAY ||
2302
            if (ping_info_color_type == PNG_COLOR_TYPE_GRAY ||
2303
                ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2303
                ping_info_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2304
              {
2304
              {
2305
                image->depth=8;
2305
                image->depth=8;
2306
                (void) ImportImagePixelArea(image,
2306
                (void) ImportImagePixelArea(image,
Lines 2309-2320 static Image *ReadOnePNGImage(MngInfo *m Link Here
2309
                                            row_offset,0,0);
2309
                                            row_offset,0,0);
2310
                image->depth=depth;
2310
                image->depth=depth;
2311
              }
2311
              }
2312
            else if (depth == 8 && ping_info->color_type == PNG_COLOR_TYPE_RGB)
2312
            else if (depth == 8 && ping_info_color_type == PNG_COLOR_TYPE_RGB)
2313
              (void) ImportImagePixelArea(image,(QuantumType) RGBQuantum,
2313
              (void) ImportImagePixelArea(image,(QuantumType) RGBQuantum,
2314
                                          image->depth,png_pixels+
2314
                                          image->depth,png_pixels+
2315
                                          row_offset,0,0);
2315
                                          row_offset,0,0);
2316
            else if (ping_info->color_type == PNG_COLOR_TYPE_RGB ||
2316
            else if (ping_info_color_type == PNG_COLOR_TYPE_RGB ||
2317
                     ping_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2317
                     ping_info_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2318
              {
2318
              {
2319
                image->depth=8;
2319
                image->depth=8;
2320
                (void) ImportImagePixelArea(image,(QuantumType) RGBAQuantum,
2320
                (void) ImportImagePixelArea(image,(QuantumType) RGBAQuantum,
Lines 2322-2349 static Image *ReadOnePNGImage(MngInfo *m Link Here
2322
                                            row_offset,0,0);
2322
                                            row_offset,0,0);
2323
                image->depth=depth;
2323
                image->depth=depth;
2324
              }
2324
              }
2325
            else if (ping_info->color_type == PNG_COLOR_TYPE_PALETTE)
2325
            else if (ping_info_color_type == PNG_COLOR_TYPE_PALETTE)
2326
              (void) ImportImagePixelArea(image,(QuantumType) IndexQuantum,
2326
              (void) ImportImagePixelArea(image,(QuantumType) IndexQuantum,
2327
                                          ping_info->bit_depth,png_pixels+
2327
                                          ping_info_bit_depth,png_pixels+
2328
                                          row_offset,0,0);
2328
                                          row_offset,0,0);
2329
                                          /* FIXME, sample size ??? */
2329
                                          /* FIXME, sample size ??? */
2330
#else /* (QuantumDepth != 8) */
2330
#else /* (QuantumDepth != 8) */
2331
2331
2332
            if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
2332
            if (ping_info_color_type == PNG_COLOR_TYPE_GRAY)
2333
              (void) ImportImagePixelArea(image,(QuantumType) GrayQuantum,
2333
              (void) ImportImagePixelArea(image,(QuantumType) GrayQuantum,
2334
                                          image->depth,png_pixels+
2334
                                          image->depth,png_pixels+
2335
                                          row_offset,0,0);
2335
                                          row_offset,0,0);
2336
            else if (ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2336
            else if (ping_info_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2337
              (void) ImportImagePixelArea(image,(QuantumType) GrayAlphaQuantum,
2337
              (void) ImportImagePixelArea(image,(QuantumType) GrayAlphaQuantum,
2338
                                          image->depth,png_pixels+
2338
                                          image->depth,png_pixels+
2339
                                          row_offset,0,0);
2339
                                          row_offset,0,0);
2340
            else if (ping_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2340
            else if (ping_info_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2341
              (void) ImportImagePixelArea(image,(QuantumType) RGBAQuantum,
2341
              (void) ImportImagePixelArea(image,(QuantumType) RGBAQuantum,
2342
                                          image->depth,png_pixels+
2342
                                          image->depth,png_pixels+
2343
                                          row_offset,0,0);
2343
                                          row_offset,0,0);
2344
            else if (ping_info->color_type == PNG_COLOR_TYPE_PALETTE)
2344
            else if (ping_info_color_type == PNG_COLOR_TYPE_PALETTE)
2345
              (void) ImportImagePixelArea(image,(QuantumType) IndexQuantum,
2345
              (void) ImportImagePixelArea(image,(QuantumType) IndexQuantum,
2346
                                          ping_info->bit_depth,png_pixels+
2346
                                          ping_info_bit_depth,png_pixels+
2347
                                          row_offset,0,0);
2347
                                          row_offset,0,0);
2348
                                          /* FIXME, sample size ??? */
2348
                                          /* FIXME, sample size ??? */
2349
            else
2349
            else
Lines 2372-2378 static Image *ReadOnePNGImage(MngInfo *m Link Here
2372
        /*
2372
        /*
2373
          Convert grayscale image to PseudoClass pixel packets.
2373
          Convert grayscale image to PseudoClass pixel packets.
2374
        */
2374
        */
2375
        image->matte=ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA;
2375
        image->matte=ping_info_color_type == PNG_COLOR_TYPE_GRAY_ALPHA;
2376
        quantum_scanline=MagickAllocateMemory(Quantum *,
2376
        quantum_scanline=MagickAllocateMemory(Quantum *,
2377
                                              (image->matte ?  2 : 1) *
2377
                                              (image->matte ?  2 : 1) *
2378
                                              image->columns*sizeof(Quantum));
2378
                                              image->columns*sizeof(Quantum));
Lines 2385-2391 static Image *ReadOnePNGImage(MngInfo *m Link Here
2385
              *p;
2385
              *p;
2386
2386
2387
            if (num_passes > 1)
2387
            if (num_passes > 1)
2388
              row_offset=ping_info->rowbytes*y;
2388
              row_offset=ping_rowbytes*y;
2389
            else
2389
            else
2390
              row_offset=0;
2390
              row_offset=0;
2391
            png_read_row(ping,png_pixels+row_offset,NULL);
2391
            png_read_row(ping,png_pixels+row_offset,NULL);
Lines 2395-2401 static Image *ReadOnePNGImage(MngInfo *m Link Here
2395
            indexes=AccessMutableIndexes(image);
2395
            indexes=AccessMutableIndexes(image);
2396
            p=png_pixels+row_offset;
2396
            p=png_pixels+row_offset;
2397
            r=quantum_scanline;
2397
            r=quantum_scanline;
2398
            switch (ping_info->bit_depth)
2398
            switch (ping_info_bit_depth)
2399
              {
2399
              {
2400
              case 1:
2400
              case 1:
2401
                {
2401
                {
Lines 2445-2451 static Image *ReadOnePNGImage(MngInfo *m Link Here
2445
                }
2445
                }
2446
              case 8:
2446
              case 8:
2447
                {
2447
                {
2448
                  if (ping_info->color_type == 4)
2448
                  if (ping_info_color_type == 4)
2449
                    for (x=(long) image->columns; x > 0; x--)
2449
                    for (x=(long) image->columns; x > 0; x--)
2450
                      {
2450
                      {
2451
                        *r++=*p++;
2451
                        *r++=*p++;
Lines 2473-2479 static Image *ReadOnePNGImage(MngInfo *m Link Here
2473
                        *r=0;
2473
                        *r=0;
2474
                      *r|=(*p++);
2474
                      *r|=(*p++);
2475
                      r++;
2475
                      r++;
2476
                      if (ping_info->color_type == 4)
2476
                      if (ping_info_color_type == 4)
2477
                        {
2477
                        {
2478
                          q->opacity=((*p++) << 8);
2478
                          q->opacity=((*p++) << 8);
2479
                          q->opacity|=(*p++);
2479
                          q->opacity|=(*p++);
Lines 2488-2494 static Image *ReadOnePNGImage(MngInfo *m Link Here
2488
                        *r=0;
2488
                        *r=0;
2489
                      *r|=(*p++);
2489
                      *r|=(*p++);
2490
                      r++;
2490
                      r++;
2491
                      if (ping_info->color_type == 4)
2491
                      if (ping_info_color_type == 4)
2492
                        {
2492
                        {
2493
                          q->opacity=((*p++) << 8);
2493
                          q->opacity=((*p++) << 8);
2494
                          q->opacity|=(*p++);
2494
                          q->opacity|=(*p++);
Lines 2499-2505 static Image *ReadOnePNGImage(MngInfo *m Link Here
2499
#else /* QuantumDepth == 8 */
2499
#else /* QuantumDepth == 8 */
2500
                      *r++=(*p++);
2500
                      *r++=(*p++);
2501
                      p++; /* strip low byte */
2501
                      p++; /* strip low byte */
2502
                      if (ping_info->color_type == 4)
2502
                      if (ping_info_color_type == 4)
2503
                        {
2503
                        {
2504
                          q->opacity=(Quantum) (MaxRGB-(*p++));
2504
                          q->opacity=(Quantum) (MaxRGB-(*p++));
2505
                          p++;
2505
                          p++;
Lines 2549-2555 static Image *ReadOnePNGImage(MngInfo *m Link Here
2549
                              "  exit ReadOnePNGImage().");
2549
                              "  exit ReadOnePNGImage().");
2550
      return (image);
2550
      return (image);
2551
    }
2551
    }
2552
  if (ping_info->valid & PNG_INFO_tRNS)
2552
  if (png_get_valid(ping, ping_info, PNG_INFO_tRNS))
2553
    {
2553
    {
2554
      ClassType
2554
      ClassType
2555
        storage_class;
2555
        storage_class;
Lines 2572-2589 static Image *ReadOnePNGImage(MngInfo *m Link Here
2572
              IndexPacket
2572
              IndexPacket
2573
                index;
2573
                index;
2574
2574
2575
              if (ping_info->color_type == PNG_COLOR_TYPE_PALETTE)
2575
              if (ping_info_color_type == PNG_COLOR_TYPE_PALETTE)
2576
                for (x=0; x < (long) image->columns; x++)
2576
                for (x=0; x < (long) image->columns; x++)
2577
                  {
2577
                  {
2578
                    index=indexes[x];
2578
                    index=indexes[x];
2579
                    if (index < ping_info->num_trans)
2579
                    if (index < ping_info_num_trans)
2580
                      q->opacity=
2580
                      q->opacity=
2581
                        ScaleCharToQuantum(255-ping_info->trans_alpha[index]);
2581
                        ScaleCharToQuantum(255-ping_trans_alpha[index]);
2582
		    else
2582
		    else
2583
		      q->opacity=OpaqueOpacity;
2583
		      q->opacity=OpaqueOpacity;
2584
                    q++;
2584
                    q++;
2585
                  }
2585
                  }
2586
              else if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
2586
              else if (ping_info_color_type == PNG_COLOR_TYPE_GRAY)
2587
                for (x=0; x < (long) image->columns; x++)
2587
                for (x=0; x < (long) image->columns; x++)
2588
                  {
2588
                  {
2589
                    index=indexes[x];
2589
                    index=indexes[x];
Lines 2714-2720 static Image *ReadOnePNGImage(MngInfo *m Link Here
2714
          mng_info->ob[object_id]->interlace_method=interlace_method;
2714
          mng_info->ob[object_id]->interlace_method=interlace_method;
2715
          mng_info->ob[object_id]->compression_method=compression_method;
2715
          mng_info->ob[object_id]->compression_method=compression_method;
2716
          mng_info->ob[object_id]->filter_method=filter_method;
2716
          mng_info->ob[object_id]->filter_method=filter_method;
2717
          if (ping_info->valid & PNG_INFO_PLTE)
2717
          if (png_get_valid(ping, ping_info, PNG_INFO_PLTE))
2718
            {
2718
            {
2719
              int
2719
              int
2720
                number_colors;
2720
                number_colors;
Lines 5734-5740 static Image *ReadMNGImage(const ImageIn Link Here
5734
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadMNGImage()");
5734
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadMNGImage()");
5735
  return(image);
5735
  return(image);
5736
}
5736
}
5737
#else /* PNG_LIBPNG_VER > 95 */
5737
#else /* PNG_LIBPNG_VER > 10011 */
5738
static Image *ReadPNGImage(const ImageInfo *image_info,
5738
static Image *ReadPNGImage(const ImageInfo *image_info,
5739
                           ExceptionInfo *exception)
5739
                           ExceptionInfo *exception)
5740
{
5740
{
Lines 5749-5755 static Image *ReadMNGImage(const ImageIn Link Here
5749
{
5749
{
5750
  return (ReadPNGImage(image_info,exception));
5750
  return (ReadPNGImage(image_info,exception));
5751
}
5751
}
5752
#endif /* PNG_LIBPNG_VER > 95 */
5752
#endif /* PNG_LIBPNG_VER > 10011 */
5753
#endif
5753
#endif
5754
5754
5755
/*
5755
/*
Lines 5960-5966 ModuleExport void UnregisterPNGImage(voi Link Here
5960
}
5960
}
5961
5961
5962
#if defined(HasPNG)
5962
#if defined(HasPNG)
5963
#if PNG_LIBPNG_VER > 95
5963
#if PNG_LIBPNG_VER > 10011
5964
/*
5964
/*
5965
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5965
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5966
%                                                                             %
5966
%                                                                             %
Lines 6043-6062 ModuleExport void UnregisterPNGImage(voi Link Here
6043
*/
6043
*/
6044
6044
6045
6045
6046
#if (PNG_LIBPNG_VER > 99 && PNG_LIBPNG_VER < 10007)
6047
/* This function became available in libpng version 1.0.6g. */
6048
static void
6049
png_set_compression_buffer_size(png_structp png_ptr, png_uint_32 size)
6050
{
6051
  if (png_ptr->zbuf)
6052
    png_free(png_ptr, png_ptr->zbuf); png_ptr->zbuf=NULL;
6053
  png_ptr->zbuf_size=(png_size_t) size;
6054
  png_ptr->zbuf=(png_bytep) png_malloc(png_ptr, size);
6055
  if (!png_ptr->zbuf)
6056
    png_error(png_ptr,"Unable to allocate zbuf");
6057
}
6058
#endif
6059
6060
static void
6046
static void
6061
png_write_raw_profile(const ImageInfo *image_info,png_struct *ping,
6047
png_write_raw_profile(const ImageInfo *image_info,png_struct *ping,
6062
                      png_info *ping_info, const char *profile_type,
6048
                      png_info *ping_info, const char *profile_type,
Lines 6064-6070 png_write_raw_profile(const ImageInfo *i Link Here
6064
                      const unsigned char *profile_data,
6050
                      const unsigned char *profile_data,
6065
                      png_uint_32 length)
6051
                      png_uint_32 length)
6066
{
6052
{
6067
#if (PNG_LIBPNG_VER > 10005)
6068
  png_textp
6053
  png_textp
6069
    text;
6054
    text;
6070
6055
Lines 6083-6107 png_write_raw_profile(const ImageInfo *i Link Here
6083
6068
6084
  unsigned char
6069
  unsigned char
6085
    hex[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
6070
    hex[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
6086
#endif
6087
6071
6088
#if (PNG_LIBPNG_VER <= 10005)
6089
  if (image_info->verbose)
6090
    (void) printf("Not ");
6091
  (void) image_info;
6092
  (void) ping;
6093
  (void) ping_info;
6094
  (void) profile_type;
6095
  (void) profile_description;
6096
  (void) profile_data;
6097
  (void) length;
6098
#endif
6099
  if (image_info->verbose)
6072
  if (image_info->verbose)
6100
    {
6073
    {
6101
      (void) printf("writing raw profile: type=%.1024s, length=%lu\n",
6074
      (void) printf("writing raw profile: type=%.1024s, length=%lu\n",
6102
                    profile_type, (unsigned long)length);
6075
                    profile_type, (unsigned long)length);
6103
    }
6076
    }
6104
#if (PNG_LIBPNG_VER > 10005)
6105
  text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text));
6077
  text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text));
6106
  description_length=strlen((const char *) profile_description);
6078
  description_length=strlen((const char *) profile_description);
6107
  allocated_length=(png_uint_32) (length*2 + (length >> 5) + 20
6079
  allocated_length=(png_uint_32) (length*2 + (length >> 5) + 20
Lines 6137-6143 png_write_raw_profile(const ImageInfo *i Link Here
6137
  png_free(ping,text[0].text);
6109
  png_free(ping,text[0].text);
6138
  png_free(ping,text[0].key);
6110
  png_free(ping,text[0].key);
6139
  png_free(ping,text);
6111
  png_free(ping,text);
6140
#endif
6141
}
6112
}
6142
6113
6143
static MagickPassFail WriteOnePNGImage(MngInfo *mng_info,
6114
static MagickPassFail WriteOnePNGImage(MngInfo *mng_info,
Lines 6152-6168 static MagickPassFail WriteOnePNGImage(M Link Here
6152
6123
6153
  int
6124
  int
6154
    num_passes,
6125
    num_passes,
6155
    pass;
6126
    pass,
6127
    ping_info_bit_depth = 0,
6128
    ping_info_color_type = 0,
6129
    ping_info_interlace_method = 0,
6130
    ping_info_compression_method = 0,
6131
    ping_info_filter_method = 0,
6132
    ping_info_num_trans = 0;
6133
6134
  png_bytep
6135
     ping_trans_alpha = NULL;
6156
6136
6157
  png_colorp
6137
  png_colorp
6158
    palette;
6138
    palette;
6159
6139
6140
  png_color_16
6141
    ping_background,
6142
    ping_trans_color;
6143
6160
  png_info
6144
  png_info
6161
    *ping_info;
6145
    *ping_info;
6162
6146
6163
  png_struct
6147
  png_struct
6164
    *ping;
6148
    *ping;
6165
6149
6150
  png_uint_32
6151
    ping_info_width,
6152
    ping_info_height;
6153
6166
  long
6154
  long
6167
    y;
6155
    y;
6168
6156
Lines 6181-6188 static MagickPassFail WriteOnePNGImage(M Link Here
6181
    image_depth,
6169
    image_depth,
6182
    image_matte,
6170
    image_matte,
6183
    logging,
6171
    logging,
6184
    matte,
6172
    matte;
6185
    not_valid;
6186
6173
6187
  unsigned long
6174
  unsigned long
6188
    quantum_size,  /* depth for ExportImage */
6175
    quantum_size,  /* depth for ExportImage */
Lines 6211-6216 static MagickPassFail WriteOnePNGImage(M Link Here
6211
      return MagickFail;
6198
      return MagickFail;
6212
    }
6199
    }
6213
6200
6201
  /* Initialize some stuff */
6202
  ping_background.red = 0;
6203
  ping_background.green = 0;
6204
  ping_background.blue = 0;
6205
  ping_background.gray = 0;
6206
  ping_background.index = 0;
6207
6208
  ping_trans_color.red=0;
6209
  ping_trans_color.green=0;
6210
  ping_trans_color.blue=0;
6211
  ping_trans_color.gray=0;
6212
6214
  image_colors=image->colors;
6213
  image_colors=image->colors;
6215
  image_depth=image->depth;
6214
  image_depth=image->depth;
6216
  image_matte=image->matte;
6215
  image_matte=image->matte;
Lines 6259-6265 static MagickPassFail WriteOnePNGImage(M Link Here
6259
  LockSemaphoreInfo(png_semaphore);
6258
  LockSemaphoreInfo(png_semaphore);
6260
#endif
6259
#endif
6261
6260
6262
  if (setjmp(ping->jmpbuf))
6261
  if (setjmp(png_jmpbuf(ping)))
6263
    {
6262
    {
6264
      /*
6263
      /*
6265
        PNG write failed.
6264
        PNG write failed.
Lines 6288-6303 static MagickPassFail WriteOnePNGImage(M Link Here
6288
# endif
6287
# endif
6289
#endif
6288
#endif
6290
  x=0;
6289
  x=0;
6291
  ping_info->width=image->columns;
6290
  ping_info_width=image->columns;
6292
  ping_info->height=image->rows;
6291
  ping_info_height=image->rows;
6293
  if (logging)
6292
  if (logging)
6294
    {
6293
    {
6295
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6294
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6296
                            "    width=%lu",
6295
                            "    width=%lu",
6297
                            (unsigned long)ping_info->width);
6296
                            (unsigned long)ping_info_width);
6298
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6297
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6299
                            "    height=%lu",
6298
                            "    height=%lu",
6300
                            (unsigned long)ping_info->height);
6299
                            (unsigned long)ping_info_height);
6301
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6300
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6302
                            "    image->depth=%u",image_depth);
6301
                            "    image->depth=%u",image_depth);
6303
    }
6302
    }
Lines 6306-6317 static MagickPassFail WriteOnePNGImage(M Link Here
6306
  quantum_size=(image_depth > 8) ? 16:8;
6305
  quantum_size=(image_depth > 8) ? 16:8;
6307
6306
6308
  save_image_depth=image_depth;
6307
  save_image_depth=image_depth;
6309
  ping_info->bit_depth=(png_byte) save_image_depth;
6308
  ping_info_bit_depth=(png_byte) save_image_depth;
6310
  if (logging)
6309
  if (logging)
6311
    {
6310
    {
6312
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6311
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6313
                            "    ping_info->bit_depth=%u",
6312
                            "    ping_info_bit_depth=%u",
6314
                            ping_info->bit_depth);
6313
                            ping_info_bit_depth);
6315
    }
6314
    }
6316
#if defined(PNG_pHYs_SUPPORTED)
6315
#if defined(PNG_pHYs_SUPPORTED)
6317
  if ((image->x_resolution != 0) && (image->y_resolution != 0) &&
6316
  if ((image->x_resolution != 0) && (image->y_resolution != 0) &&
Lines 6398-6405 static MagickPassFail WriteOnePNGImage(M Link Here
6398
  matte=image_matte;
6397
  matte=image_matte;
6399
  if (mng_info->write_png8)
6398
  if (mng_info->write_png8)
6400
    {
6399
    {
6401
      ping_info->color_type=PNG_COLOR_TYPE_PALETTE;
6400
      ping_info_color_type=PNG_COLOR_TYPE_PALETTE;
6402
      ping_info->bit_depth=8;
6401
      ping_info_bit_depth=8;
6403
      {
6402
      {
6404
        /* TO DO: make this a function cause it's used twice, except
6403
        /* TO DO: make this a function cause it's used twice, except
6405
           for reducing the sample depth from 8. */
6404
           for reducing the sample depth from 8. */
Lines 6425-6436 static MagickPassFail WriteOnePNGImage(M Link Here
6425
                                    number_colors);
6424
                                    number_colors);
6426
          }
6425
          }
6427
        if (matte)
6426
        if (matte)
6428
          ping_info->valid|=PNG_INFO_tRNS;
6427
          png_set_invalid(ping,ping_info,PNG_INFO_tRNS);
6429
        /*
6428
        /*
6430
          Set image palette.
6429
          Set image palette.
6431
        */
6430
        */
6432
        ping_info->color_type=PNG_COLOR_TYPE_PALETTE;
6431
        ping_info_color_type=PNG_COLOR_TYPE_PALETTE;
6433
        ping_info->valid|=PNG_INFO_PLTE;
6432
        png_set_invalid(ping,ping_info,PNG_INFO_PLTE);
6434
#if defined(PNG_SORT_PALETTE)
6433
#if defined(PNG_SORT_PALETTE)
6435
        save_number_colors=image_colors;
6434
        save_number_colors=image_colors;
6436
        if (CompressColormapTransFirst(image) == MagickFail)
6435
        if (CompressColormapTransFirst(image) == MagickFail)
Lines 6465-6484 static MagickPassFail WriteOnePNGImage(M Link Here
6465
6464
6466
          }
6465
          }
6467
        png_set_PLTE(ping,ping_info,palette,(int) number_colors);
6466
        png_set_PLTE(ping,ping_info,palette,(int) number_colors);
6468
#if (PNG_LIBPNG_VER > 10008)
6469
        MagickFreeMemory(palette);
6467
        MagickFreeMemory(palette);
6470
#endif
6471
        /*
6468
        /*
6472
          Identify which colormap entry is transparent.
6469
          Identify which colormap entry is transparent.
6473
        */
6470
        */
6474
        ping_info->trans_alpha=MagickAllocateMemory(unsigned char *,
6471
        ping_trans_alpha=MagickAllocateMemory(unsigned char *, number_colors);
6475
           number_colors);
6472
        if (ping_trans_alpha == (unsigned char *) NULL)
6476
        if (ping_info->trans_alpha == (unsigned char *) NULL)
6477
          ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,
6473
          ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,
6478
                               image);
6474
                               image);
6479
        assert(number_colors <= 256);
6475
        assert(number_colors <= 256);
6480
        for (i=0; i < (long) number_colors; i++)
6476
        for (i=0; i < (long) number_colors; i++)
6481
          ping_info->trans_alpha[i]=255;
6477
          ping_trans_alpha[i]=255;
6482
        for (y=0; y < (long) image->rows; y++)
6478
        for (y=0; y < (long) image->rows; y++)
6483
          {
6479
          {
6484
            register const PixelPacket
6480
            register const PixelPacket
Lines 6498-6526 static MagickPassFail WriteOnePNGImage(M Link Here
6498
6494
6499
                    index=indexes[x];
6495
                    index=indexes[x];
6500
                    assert((unsigned long) index < number_colors);
6496
                    assert((unsigned long) index < number_colors);
6501
                    ping_info->trans_alpha[index]=(png_byte) (255-
6497
                    ping_trans_alpha[index]=(png_byte) (255-
6502
                                            ScaleQuantumToChar(p->opacity));
6498
                                            ScaleQuantumToChar(p->opacity));
6503
                  }
6499
                  }
6504
                p++;
6500
                p++;
6505
              }
6501
              }
6506
          }
6502
          }
6507
        ping_info->num_trans=0;
6503
        ping_info_num_trans=0;
6508
        for (i=0; i < (long) number_colors; i++)
6504
        for (i=0; i < (long) number_colors; i++)
6509
          if (ping_info->trans_alpha[i] != 255)
6505
          if (ping_trans_alpha[i] != 255)
6510
            ping_info->num_trans=(unsigned short) (i+1);
6506
            ping_info_num_trans=(unsigned short) (i+1);
6511
        if (ping_info->num_trans == 0)
6507
        if (ping_info_num_trans == 0)
6512
          ping_info->valid&=(~PNG_INFO_tRNS);
6508
          png_set_invalid(ping,ping_info,PNG_INFO_tRNS);
6513
        if (!(ping_info->valid & PNG_INFO_tRNS))
6509
        if (!(png_get_valid(ping,ping_info,PNG_INFO_tRNS)))
6514
          ping_info->num_trans=0;
6510
          ping_info_num_trans=0;
6515
        if (ping_info->num_trans == 0)
6511
        if (ping_info_num_trans == 0)
6516
          MagickFreeMemory(ping_info->trans_alpha);
6512
          MagickFreeMemory(ping_trans_alpha);
6517
        /*
6513
        /*
6518
          Identify which colormap entry is the background color.
6514
          Identify which colormap entry is the background color.
6519
        */
6515
        */
6520
        for (i=0; i < (long) Max(number_colors-1,1); i++)
6516
        for (i=0; i < (long) Max(number_colors-1,1); i++)
6521
          if (RGBColorMatchExact(ping_info->background,image->colormap[i]))
6517
          if (RGBColorMatchExact(ping_background,image->colormap[i]))
6522
            break;
6518
            break;
6523
        ping_info->background.index=(png_uint_16) i;
6519
        ping_background.index=(png_uint_16) i;
6524
      }
6520
      }
6525
      if (image_matte)
6521
      if (image_matte)
6526
        {
6522
        {
Lines 6530-6608 static MagickPassFail WriteOnePNGImage(M Link Here
6530
  else if (mng_info->write_png24)
6526
  else if (mng_info->write_png24)
6531
    {
6527
    {
6532
      image_matte=MagickFalse;
6528
      image_matte=MagickFalse;
6533
      ping_info->color_type=PNG_COLOR_TYPE_RGB;
6529
      ping_info_color_type=PNG_COLOR_TYPE_RGB;
6534
    }
6530
    }
6535
  else if (mng_info->write_png32)
6531
  else if (mng_info->write_png32)
6536
    {
6532
    {
6537
      image_matte=MagickTrue;
6533
      image_matte=MagickTrue;
6538
      ping_info->color_type=PNG_COLOR_TYPE_RGB_ALPHA;
6534
      ping_info_color_type=PNG_COLOR_TYPE_RGB_ALPHA;
6539
    }
6535
    }
6540
  else
6536
  else
6541
    {
6537
    {
6542
      if (ping_info->bit_depth < 8)
6538
      if (ping_info_bit_depth < 8)
6543
        ping_info->bit_depth=8;
6539
        ping_info_bit_depth=8;
6544
6540
6545
      ping_info->color_type=PNG_COLOR_TYPE_RGB;
6541
      ping_info_color_type=PNG_COLOR_TYPE_RGB;
6546
      if (characteristics.monochrome)
6542
      if (characteristics.monochrome)
6547
        {
6543
        {
6548
          if (characteristics.opaque)
6544
          if (characteristics.opaque)
6549
            {
6545
            {
6550
              ping_info->color_type=PNG_COLOR_TYPE_GRAY;
6546
              ping_info_color_type=PNG_COLOR_TYPE_GRAY;
6551
              ping_info->bit_depth=1;
6547
              ping_info_bit_depth=1;
6552
            }
6548
            }
6553
          else
6549
          else
6554
            {
6550
            {
6555
              ping_info->color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
6551
              ping_info_color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
6556
            }
6552
            }
6557
        }
6553
        }
6558
      else if (characteristics.grayscale)
6554
      else if (characteristics.grayscale)
6559
        {
6555
        {
6560
          if (characteristics.opaque)
6556
          if (characteristics.opaque)
6561
            ping_info->color_type=PNG_COLOR_TYPE_GRAY;
6557
            ping_info_color_type=PNG_COLOR_TYPE_GRAY;
6562
          else
6558
          else
6563
            ping_info->color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
6559
            ping_info_color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
6564
        }
6560
        }
6565
      else if (characteristics.palette && image_colors <= 256)
6561
      else if (characteristics.palette && image_colors <= 256)
6566
        {
6562
        {
6567
          ping_info->color_type=PNG_COLOR_TYPE_PALETTE;
6563
          ping_info_color_type=PNG_COLOR_TYPE_PALETTE;
6568
          ping_info->bit_depth=8;
6564
          ping_info_bit_depth=8;
6569
          mng_info->IsPalette=MagickTrue;
6565
          mng_info->IsPalette=MagickTrue;
6570
        }
6566
        }
6571
      else
6567
      else
6572
        {
6568
        {
6573
          if (characteristics.opaque)
6569
          if (characteristics.opaque)
6574
            ping_info->color_type=PNG_COLOR_TYPE_RGB;
6570
            ping_info_color_type=PNG_COLOR_TYPE_RGB;
6575
          else
6571
          else
6576
            ping_info->color_type=PNG_COLOR_TYPE_RGB_ALPHA;
6572
            ping_info_color_type=PNG_COLOR_TYPE_RGB_ALPHA;
6577
        }
6573
        }
6578
      if (image_info->type == BilevelType)
6574
      if (image_info->type == BilevelType)
6579
        {
6575
        {
6580
          if (characteristics.monochrome)
6576
          if (characteristics.monochrome)
6581
            {
6577
            {
6582
              if (!image_matte)
6578
              if (!image_matte)
6583
                ping_info->bit_depth=1;
6579
                ping_info_bit_depth=1;
6584
            }
6580
            }
6585
        }
6581
        }
6586
      if (image_info->type == GrayscaleType)
6582
      if (image_info->type == GrayscaleType)
6587
        ping_info->color_type=PNG_COLOR_TYPE_GRAY;
6583
        ping_info_color_type=PNG_COLOR_TYPE_GRAY;
6588
      if (image_info->type == GrayscaleMatteType)
6584
      if (image_info->type == GrayscaleMatteType)
6589
        ping_info->color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
6585
        ping_info_color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
6590
      /*       if (!mng_info->optimize && matte) */
6586
      /*       if (!mng_info->optimize && matte) */
6591
      /*         ping_info->color_type=PNG_COLOR_TYPE_RGB_ALPHA; */
6587
      /*         ping_info_color_type=PNG_COLOR_TYPE_RGB_ALPHA; */
6592
6588
6593
      if (logging)
6589
      if (logging)
6594
        {
6590
        {
6595
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6591
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6596
                                "    Tentative PNG color type: %s (%d)",
6592
                                "    Tentative PNG color type: %s (%d)",
6597
                                PngColorTypeToString(ping_info->color_type),
6593
                                PngColorTypeToString(ping_info_color_type),
6598
                                ping_info->color_type);
6594
                                ping_info_color_type);
6599
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6595
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6600
                                "    image_info->type: %d",image_info->type);
6596
                                "    image_info->type: %d",image_info->type);
6601
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6597
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6602
                                "    image->depth: %u",image_depth);
6598
                                "    image->depth: %u",image_depth);
6603
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6599
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6604
                                "    ping_info->bit_depth: %d",
6600
                                "    ping_info_bit_depth: %d",
6605
                                ping_info->bit_depth);
6601
                                ping_info_bit_depth);
6606
        }
6602
        }
6607
6603
6608
      if (matte && (mng_info->optimize || mng_info->IsPalette))
6604
      if (matte && (mng_info->optimize || mng_info->IsPalette))
Lines 6614-6623 static MagickPassFail WriteOnePNGImage(M Link Here
6614
            {
6610
            {
6615
              /*
6611
              /*
6616
                No transparent pixels are present.  Change 4 or 6 to 0 or 2,
6612
                No transparent pixels are present.  Change 4 or 6 to 0 or 2,
6617
                and do not set the PNG_INFO_tRNS flag in ping_info->valid.
6613
                and do not set the PNG_INFO_tRNS flag.
6618
              */
6614
              */
6619
              image_matte=MagickFalse;
6615
              image_matte=MagickFalse;
6620
              ping_info->color_type&=0x03;
6616
              ping_info_color_type&=0x03;
6621
            }
6617
            }
6622
          else
6618
          else
6623
            {
6619
            {
Lines 6625-6637 static MagickPassFail WriteOnePNGImage(M Link Here
6625
                mask;
6621
                mask;
6626
6622
6627
              mask=0xffff;
6623
              mask=0xffff;
6628
              if (ping_info->bit_depth == 8)
6624
              if (ping_info_bit_depth == 8)
6629
                mask=0x00ff;
6625
                mask=0x00ff;
6630
              if (ping_info->bit_depth == 4)
6626
              if (ping_info_bit_depth == 4)
6631
                mask=0x000f;
6627
                mask=0x000f;
6632
              if (ping_info->bit_depth == 2)
6628
              if (ping_info_bit_depth == 2)
6633
                mask=0x0003;
6629
                mask=0x0003;
6634
              if (ping_info->bit_depth == 1)
6630
              if (ping_info_bit_depth == 1)
6635
                mask=0x0001;
6631
                mask=0x0001;
6636
6632
6637
              /*
6633
              /*
Lines 6655-6673 static MagickPassFail WriteOnePNGImage(M Link Here
6655
              if ((p != (const PixelPacket *) NULL) &&
6651
              if ((p != (const PixelPacket *) NULL) &&
6656
                  (p->opacity != OpaqueOpacity))
6652
                  (p->opacity != OpaqueOpacity))
6657
                {
6653
                {
6658
                  ping_info->valid|=PNG_INFO_tRNS;
6654
                  ping_trans_color.red=ScaleQuantumToShort(p->red)&mask;
6659
                  ping_info->trans_color.red=ScaleQuantumToShort(p->red)&mask;
6655
                  ping_trans_color.green=ScaleQuantumToShort(p->green)
6660
                  ping_info->trans_color.green=ScaleQuantumToShort(p->green)
6661
                                                &mask;
6656
                                                &mask;
6662
                  ping_info->trans_color.blue=ScaleQuantumToShort(p->blue)
6657
                  ping_trans_color.blue=ScaleQuantumToShort(p->blue)
6663
                                               &mask;
6658
                                               &mask;
6664
                  ping_info->trans_color.gray=
6659
                  ping_trans_color.gray=
6665
                    (png_uint_16) ScaleQuantumToShort(PixelIntensity(p))&mask;
6660
                    (png_uint_16) ScaleQuantumToShort(PixelIntensity(p))&mask;
6666
                  ping_info->trans_color.index=(unsigned char)
6661
                  ping_trans_color.index=(unsigned char)
6667
                    (ScaleQuantumToChar(MaxRGB-p->opacity));
6662
                    (ScaleQuantumToChar(MaxRGB-p->opacity));
6663
                  (void) png_set_tRNS(ping, ping_info, NULL, 0,
6664
                                      &ping_trans_color);
6668
                }
6665
                }
6669
            }
6666
            }
6670
          if (ping_info->valid & PNG_INFO_tRNS)
6667
          if (png_get_valid(ping, ping_info, PNG_INFO_tRNS))
6671
            {
6668
            {
6672
              /*
6669
              /*
6673
                Determine if there is one and only one transparent color
6670
                Determine if there is one and only one transparent color
Lines 6684-6690 static MagickPassFail WriteOnePNGImage(M Link Here
6684
                    {
6681
                    {
6685
                      if (p->opacity != OpaqueOpacity)
6682
                      if (p->opacity != OpaqueOpacity)
6686
                        {
6683
                        {
6687
                          if (!RGBColorMatchExact(ping_info->trans_color,*p))
6684
                          if (!RGBColorMatchExact(ping_trans_color,*p))
6688
                            {
6685
                            {
6689
                              break;  /* Can't use RGB + tRNS for multiple
6686
                              break;  /* Can't use RGB + tRNS for multiple
6690
                                         transparent colors.  */
6687
                                         transparent colors.  */
Lines 6697-6703 static MagickPassFail WriteOnePNGImage(M Link Here
6697
                        }
6694
                        }
6698
                      else
6695
                      else
6699
                        {
6696
                        {
6700
                          if (RGBColorMatchExact(ping_info->trans_color,*p))
6697
                          if (RGBColorMatchExact(ping_trans_color,*p))
6701
                            break; /* Can't use RGB + tRNS when another pixel
6698
                            break; /* Can't use RGB + tRNS when another pixel
6702
                                      having the same RGB samples is
6699
                                      having the same RGB samples is
6703
                                      transparent. */
6700
                                      transparent. */
Lines 6708-6757 static MagickPassFail WriteOnePNGImage(M Link Here
6708
                    break;
6705
                    break;
6709
                }
6706
                }
6710
              if (x != 0)
6707
              if (x != 0)
6711
                ping_info->valid&=(~PNG_INFO_tRNS);
6708
                png_set_invalid(ping, ping_info, PNG_INFO_tRNS);
6712
            }
6709
            }
6713
          if (ping_info->valid & PNG_INFO_tRNS)
6710
          if (png_get_valid(ping, ping_info, PNG_INFO_tRNS))
6714
            {
6711
            {
6715
              ping_info->color_type &= 0x03;  /* changes 4 or 6 to 0 or 2 */
6712
              ping_info_color_type &= 0x03;  /* changes 4 or 6 to 0 or 2 */
6716
              if (image_depth == 8)
6713
              if (image_depth == 8)
6717
                {
6714
                {
6718
                  ping_info->trans_color.red&=0xff;
6715
                  ping_trans_color.red&=0xff;
6719
                  ping_info->trans_color.green&=0xff;
6716
                  ping_trans_color.green&=0xff;
6720
                  ping_info->trans_color.blue&=0xff;
6717
                  ping_trans_color.blue&=0xff;
6721
                  ping_info->trans_color.gray&=0xff;
6718
                  ping_trans_color.gray&=0xff;
6722
                }
6719
                }
6723
            }
6720
            }
6724
        }
6721
        }
6725
      matte=image_matte;
6722
      matte=image_matte;
6726
      if (ping_info->valid & PNG_INFO_tRNS)
6723
      if (png_get_valid(ping, ping_info, PNG_INFO_tRNS))
6727
        image_matte=MagickFalse;
6724
        image_matte=MagickFalse;
6728
      if ((mng_info->optimize || mng_info->IsPalette) &&
6725
      if ((mng_info->optimize || mng_info->IsPalette) &&
6729
          characteristics.grayscale && (!image_matte || image_depth >= 8))
6726
          characteristics.grayscale && (!image_matte || image_depth >= 8))
6730
        {
6727
        {
6731
          if (image_matte)
6728
          if (image_matte)
6732
            ping_info->color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
6729
            ping_info_color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
6733
          else
6730
          else
6734
            {
6731
            {
6735
              ping_info->color_type=PNG_COLOR_TYPE_GRAY;
6732
              ping_info_color_type=PNG_COLOR_TYPE_GRAY;
6736
              if (save_image_depth == 16 && image_depth == 8)
6733
              if (save_image_depth == 16 && image_depth == 8)
6737
                ping_info->trans_color.gray*=0x0101;
6734
                ping_trans_color.gray*=0x0101;
6738
            }
6735
            }
6739
          if (image_depth > QuantumDepth)
6736
          if (image_depth > QuantumDepth)
6740
            image_depth=QuantumDepth;
6737
            image_depth=QuantumDepth;
6741
          if (image_colors == 0 || image_colors-1 > MaxRGB)
6738
          if (image_colors == 0 || image_colors-1 > MaxRGB)
6742
            image_colors=1 << image_depth;
6739
            image_colors=1 << image_depth;
6743
          if (image_depth > 8)
6740
          if (image_depth > 8)
6744
            ping_info->bit_depth=16;
6741
            ping_info_bit_depth=16;
6745
          else
6742
          else
6746
            {
6743
            {
6747
              if (ping_info->color_type == PNG_COLOR_TYPE_PALETTE)
6744
              if (ping_info_color_type == PNG_COLOR_TYPE_PALETTE)
6748
                {
6745
                {
6749
                  ping_info->bit_depth=1;
6746
                  ping_info_bit_depth=1;
6750
                  while ((int) (1 << ping_info->bit_depth) <
6747
                  while ((int) (1 << ping_info_bit_depth) <
6751
                                (long) image_colors)
6748
                                (long) image_colors)
6752
                    ping_info->bit_depth <<= 1;
6749
                    ping_info_bit_depth <<= 1;
6753
                }
6750
                }
6754
              else if (mng_info->optimize && ping_info->color_type ==
6751
              else if (mng_info->optimize && ping_info_color_type ==
6755
                       PNG_COLOR_TYPE_GRAY && image_colors < 17 &&
6752
                       PNG_COLOR_TYPE_GRAY && image_colors < 17 &&
6756
                       mng_info->IsPalette)
6753
                       mng_info->IsPalette)
6757
                {
6754
                {
Lines 6779-6789 static MagickPassFail WriteOnePNGImage(M Link Here
6779
                        depth_1_ok=MagickFalse;
6776
                        depth_1_ok=MagickFalse;
6780
                    }
6777
                    }
6781
                  if (depth_1_ok)
6778
                  if (depth_1_ok)
6782
                    ping_info->bit_depth=1;
6779
                    ping_info_bit_depth=1;
6783
                  else if (depth_2_ok)
6780
                  else if (depth_2_ok)
6784
                    ping_info->bit_depth=2;
6781
                    ping_info_bit_depth=2;
6785
                  else if (depth_4_ok)
6782
                  else if (depth_4_ok)
6786
                    ping_info->bit_depth=4;
6783
                    ping_info_bit_depth=4;
6787
                }
6784
                }
6788
            }
6785
            }
6789
        }
6786
        }
Lines 6797-6808 static MagickPassFail WriteOnePNGImage(M Link Here
6797
6794
6798
                number_colors=image_colors;
6795
                number_colors=image_colors;
6799
                if (matte)
6796
                if (matte)
6800
                  ping_info->valid|=PNG_INFO_tRNS;
6797
                  png_set_invalid(ping, ping_info, PNG_INFO_tRNS);
6801
                /*
6798
                /*
6802
                  Set image palette.
6799
                  Set image palette.
6803
                */
6800
                */
6804
                ping_info->color_type=PNG_COLOR_TYPE_PALETTE;
6801
                ping_info_color_type=PNG_COLOR_TYPE_PALETTE;
6805
                ping_info->valid|=PNG_INFO_PLTE;
6806
                if (mng_info->have_write_global_plte && !matte)
6802
                if (mng_info->have_write_global_plte && !matte)
6807
                  {
6803
                  {
6808
                    png_set_PLTE(ping,ping_info,NULL,0);
6804
                    png_set_PLTE(ping,ping_info,NULL,0);
Lines 6847-6860 static MagickPassFail WriteOnePNGImage(M Link Here
6847
                           "  Setting up PLTE chunk with %d colors",
6843
                           "  Setting up PLTE chunk with %d colors",
6848
                           (int) number_colors);
6844
                           (int) number_colors);
6849
                    png_set_PLTE(ping,ping_info,palette,(int) number_colors);
6845
                    png_set_PLTE(ping,ping_info,palette,(int) number_colors);
6850
#if (PNG_LIBPNG_VER > 10008)
6851
                    MagickFreeMemory(palette);
6846
                    MagickFreeMemory(palette);
6852
#endif
6853
                  }
6847
                  }
6854
                ping_info->bit_depth=1;
6848
                ping_info_bit_depth=1;
6855
                while ((1UL << ping_info->bit_depth) < number_colors)
6849
                while ((1UL << ping_info_bit_depth) < number_colors)
6856
                  ping_info->bit_depth <<= 1;
6850
                  ping_info_bit_depth <<= 1;
6857
                ping_info->num_trans=0;
6851
                ping_info_num_trans=0;
6858
                if (matte)
6852
                if (matte)
6859
                  {
6853
                  {
6860
                    int
6854
                    int
Lines 6890-6896 static MagickPassFail WriteOnePNGImage(M Link Here
6890
                                    if (trans_alpha[index] != (png_byte) (255-
6884
                                    if (trans_alpha[index] != (png_byte) (255-
6891
                                        ScaleQuantumToChar(p->opacity)))
6885
                                        ScaleQuantumToChar(p->opacity)))
6892
                                      {
6886
                                      {
6893
                                        ping_info->color_type=
6887
                                        ping_info_color_type=
6894
                                          PNG_COLOR_TYPE_RGB_ALPHA;
6888
                                          PNG_COLOR_TYPE_RGB_ALPHA;
6895
                                        break;
6889
                                        break;
6896
                                      }
6890
                                      }
Lines 6900-6910 static MagickPassFail WriteOnePNGImage(M Link Here
6900
                              }
6894
                              }
6901
                            p++;
6895
                            p++;
6902
                          }
6896
                          }
6903
                        if (ping_info->color_type==PNG_COLOR_TYPE_RGB_ALPHA)
6897
                        if (ping_info_color_type==PNG_COLOR_TYPE_RGB_ALPHA)
6904
                          {
6898
                          {
6905
                            ping_info->num_trans=0;
6899
                            ping_info_num_trans=0;
6906
                            ping_info->valid&=(~PNG_INFO_tRNS);
6900
                            png_set_invalid(ping, ping_info, PNG_INFO_tRNS);
6907
                            ping_info->valid&=(~PNG_INFO_PLTE);
6901
                            png_set_invalid(ping, ping_info, PNG_INFO_PLTE);
6908
                            mng_info->IsPalette=MagickFalse;
6902
                            mng_info->IsPalette=MagickFalse;
6909
                            (void) SyncImage(image);
6903
                            (void) SyncImage(image);
6910
                            if (logging)
6904
                            if (logging)
Lines 6916-6955 static MagickPassFail WriteOnePNGImage(M Link Here
6916
                            break;
6910
                            break;
6917
                          }
6911
                          }
6918
                      }
6912
                      }
6919
                    if ((ping_info->valid & PNG_INFO_tRNS))
6913
                    if (png_get_valid(ping, ping_info, PNG_INFO_tRNS))
6920
                      {
6914
                      {
6921
                        for (i=0; i < (long) number_colors; i++)
6915
                        for (i=0; i < (long) number_colors; i++)
6922
                          {
6916
                          {
6923
                            if (trans_alpha[i] == 256)
6917
                            if (trans_alpha[i] == 256)
6924
                              trans_alpha[i]=255;
6918
                              trans_alpha[i]=255;
6925
                            if (trans_alpha[i] != 255)
6919
                            if (trans_alpha[i] != 255)
6926
                              ping_info->num_trans=(unsigned short) (i+1);
6920
                              ping_info_num_trans=(unsigned short) (i+1);
6927
                          }
6921
                          }
6928
                      }
6922
                      }
6929
                    if (ping_info->num_trans == 0)
6923
                    if (ping_info_num_trans == 0)
6930
                      ping_info->valid&=(~PNG_INFO_tRNS);
6924
                       png_set_invalid(ping, ping_info, PNG_INFO_tRNS);
6931
                    if (!(ping_info->valid & PNG_INFO_tRNS))
6925
                    if (!png_get_valid(ping, ping_info, PNG_INFO_tRNS))
6932
                      ping_info->num_trans=0;
6926
                      ping_info_num_trans=0;
6933
                    if (ping_info->num_trans != 0)
6927
                    if (ping_info_num_trans != 0)
6934
                      {
6928
                      {
6935
                        ping_info->trans_alpha=MagickAllocateMemory(
6929
                        for (i=0; i<256; i++)
6936
                                              unsigned char *, number_colors);
6930
                           ping_trans_alpha[i]=(png_byte) trans_alpha[i];
6937
                        if (ping_info->trans_alpha == (unsigned char *) NULL)
6938
                          ThrowWriterException(ResourceLimitError,
6939
                                               MemoryAllocationFailed,image);
6940
                        for (i=0; i < (long) number_colors; i++)
6941
                          ping_info->trans_alpha[i]=(png_byte) trans_alpha[i];
6942
                      }
6931
                      }
6932
6933
                    (void) png_set_tRNS(ping, ping_info,
6934
                                        ping_trans_alpha,
6935
                                        ping_info_num_trans,
6936
                                        &ping_trans_color);
6943
                  }
6937
                  }
6944
6938
6945
                /*
6939
                /*
6946
                  Identify which colormap entry is the background color.
6940
                  Identify which colormap entry is the background color.
6947
                */
6941
                */
6948
                for (i=0; i < (long) Max(number_colors-1,1); i++)
6942
                for (i=0; i < (long) Max(number_colors-1,1); i++)
6949
                  if (RGBColorMatchExact(ping_info->background,
6943
                  if (RGBColorMatchExact(ping_background,
6950
                                         image->colormap[i]))
6944
                                         image->colormap[i]))
6951
                    break;
6945
                    break;
6952
                ping_info->background.index=(png_uint_16) i;
6946
                ping_background.index=(png_uint_16) i;
6953
              }
6947
              }
6954
          }
6948
          }
6955
        else
6949
        else
Lines 6958-6967 static MagickPassFail WriteOnePNGImage(M Link Here
6958
              image_depth=8;
6952
              image_depth=8;
6959
            if ((save_image_depth == 16) && (image_depth == 8))
6953
            if ((save_image_depth == 16) && (image_depth == 8))
6960
              {
6954
              {
6961
                ping_info->trans_color.red*=0x0101;
6955
                ping_trans_color.red*=0x0101;
6962
                ping_info->trans_color.green*=0x0101;
6956
                ping_trans_color.green*=0x0101;
6963
                ping_info->trans_color.blue*=0x0101;
6957
                ping_trans_color.blue*=0x0101;
6964
                ping_info->trans_color.gray*=0x0101;
6958
                ping_trans_color.gray*=0x0101;
6965
              }
6959
              }
6966
          }
6960
          }
6967
6961
Lines 6969-6975 static MagickPassFail WriteOnePNGImage(M Link Here
6969
        Adjust background and transparency samples in sub-8-bit
6963
        Adjust background and transparency samples in sub-8-bit
6970
        grayscale files.
6964
        grayscale files.
6971
      */
6965
      */
6972
      if (ping_info->bit_depth < 8 && ping_info->color_type ==
6966
      if (ping_info_bit_depth < 8 && ping_info_color_type ==
6973
          PNG_COLOR_TYPE_GRAY)
6967
          PNG_COLOR_TYPE_GRAY)
6974
        {
6968
        {
6975
          png_uint_16
6969
          png_uint_16
Lines 6978-6984 static MagickPassFail WriteOnePNGImage(M Link Here
6978
          png_color_16
6972
          png_color_16
6979
            background;
6973
            background;
6980
6974
6981
          maxval=(1 << ping_info->bit_depth)-1;
6975
          maxval=(1 << ping_info_bit_depth)-1;
6982
6976
6983
6977
6984
          background.gray=(png_uint_16)
6978
          background.gray=(png_uint_16)
Lines 6989-7016 static MagickPassFail WriteOnePNGImage(M Link Here
6989
                                  "  Setting up bKGD chunk");
6983
                                  "  Setting up bKGD chunk");
6990
          png_set_bKGD(ping,ping_info,&background);
6984
          png_set_bKGD(ping,ping_info,&background);
6991
6985
6992
          ping_info->trans_color.gray=(png_uint_16)(maxval*
6986
          ping_trans_color.gray=(png_uint_16)(maxval*
6993
                                        ping_info->trans_color.gray/
6987
                                ping_trans_color.gray/
6994
                                        MaxRGB);
6988
                                MaxRGB);
6995
        }
6989
        }
6996
    }
6990
    }
6997
  if (logging)
6991
  if (logging)
6998
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6992
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6999
                          "    PNG color type: %s (%d)",
6993
                          "    PNG color type: %s (%d)",
7000
                          PngColorTypeToString(ping_info->color_type),
6994
                          PngColorTypeToString(ping_info_color_type),
7001
                          ping_info->color_type);
6995
                          ping_info_color_type);
7002
  /*
6996
  /*
7003
    Initialize compression level and filtering.
6997
    Initialize compression level and filtering.
7004
  */
6998
  */
7005
  if (logging)
6999
  if (logging)
7006
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7000
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7007
                          "  Setting up deflate compression");
7001
                          "  Setting up deflate compression");
7008
#if (PNG_LIBPNG_VER > 99)
7009
  if (logging)
7002
  if (logging)
7010
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7003
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7011
                          "    Compression buffer size: 32768");
7004
                          "    Compression buffer size: 32768");
7012
  png_set_compression_buffer_size(ping,32768L);
7005
  png_set_compression_buffer_size(ping,32768L);
7013
#endif
7014
  if (logging)
7006
  if (logging)
7015
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7007
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7016
                          "    Compression mem level: 9");
7008
                          "    Compression mem level: 9");
Lines 7044-7050 static MagickPassFail WriteOnePNGImage(M Link Here
7044
      if (logging)
7036
      if (logging)
7045
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7037
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7046
                              "    Filter_type: PNG_INTRAPIXEL_DIFFERENCING");
7038
                              "    Filter_type: PNG_INTRAPIXEL_DIFFERENCING");
7047
      ping_info->filter_type=PNG_INTRAPIXEL_DIFFERENCING;
7039
      ping_info_filter_method=PNG_INTRAPIXEL_DIFFERENCING;
7048
    }
7040
    }
7049
  else
7041
  else
7050
    if (logging)
7042
    if (logging)
Lines 7061-7068 static MagickPassFail WriteOnePNGImage(M Link Here
7061
      if ((image_info->quality % 10) != 5)
7053
      if ((image_info->quality % 10) != 5)
7062
        base_filter=(int) image_info->quality % 10;
7054
        base_filter=(int) image_info->quality % 10;
7063
      else
7055
      else
7064
        if ((ping_info->color_type == PNG_COLOR_TYPE_GRAY) ||
7056
        if ((ping_info_color_type == PNG_COLOR_TYPE_GRAY) ||
7065
            (ping_info->color_type == PNG_COLOR_TYPE_PALETTE) ||
7057
            (ping_info_color_type == PNG_COLOR_TYPE_PALETTE) ||
7066
            (image_info->quality < 50))
7058
            (image_info->quality < 50))
7067
          base_filter=PNG_NO_FILTERS;
7059
          base_filter=PNG_NO_FILTERS;
7068
        else
7060
        else
Lines 7099-7105 static MagickPassFail WriteOnePNGImage(M Link Here
7099
          {
7091
          {
7100
            if (LocaleCompare(profile_name,"ICM") == 0)
7092
            if (LocaleCompare(profile_name,"ICM") == 0)
7101
              {
7093
              {
7102
#if (PNG_LIBPNG_VER > 10008) && defined(PNG_WRITE_iCCP_SUPPORTED)
7094
#if defined(PNG_WRITE_iCCP_SUPPORTED)
7103
                {
7095
                {
7104
                  if (logging)
7096
                  if (logging)
7105
                    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7097
                    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
Lines 7170-7177 static MagickPassFail WriteOnePNGImage(M Link Here
7170
        (void) png_set_sRGB(ping,ping_info,PerceptualIntent);
7162
        (void) png_set_sRGB(ping,ping_info,PerceptualIntent);
7171
      png_set_gAMA(ping,ping_info,0.45455);
7163
      png_set_gAMA(ping,ping_info,0.45455);
7172
    }
7164
    }
7173
  not_valid=(!ping_info->valid);
7165
  if ((!mng_info->write_mng) || 
7174
  if ((!mng_info->write_mng) || not_valid & PNG_INFO_sRGB)
7166
       !png_get_valid(ping, ping_info, PNG_INFO_sRGB))
7175
#endif
7167
#endif
7176
    {
7168
    {
7177
      if (!mng_info->have_write_global_gama && (image->gamma != 0.0))
7169
      if (!mng_info->have_write_global_gama && (image->gamma != 0.0))
Lines 7210-7216 static MagickPassFail WriteOnePNGImage(M Link Here
7210
                       bp.x,bp.y);
7202
                       bp.x,bp.y);
7211
        }
7203
        }
7212
    }
7204
    }
7213
  ping_info->interlace_type=(image_info->interlace == LineInterlace);
7205
  ping_info_interlace_method=(image_info->interlace == LineInterlace);
7214
7206
7215
  if (mng_info->write_mng)
7207
  if (mng_info->write_mng)
7216
    png_set_sig_bytes(ping,8);
7208
    png_set_sig_bytes(ping,8);
Lines 7219-7224 static MagickPassFail WriteOnePNGImage(M Link Here
7219
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7211
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7220
                          "  Writing PNG header chunks");
7212
                          "  Writing PNG header chunks");
7221
7213
7214
  png_set_IHDR(ping,ping_info,
7215
               ping_info_width,
7216
               ping_info_height,
7217
               ping_info_bit_depth,
7218
               ping_info_color_type,
7219
               ping_info_interlace_method,
7220
               ping_info_compression_method,
7221
               ping_info_filter_method);
7222
7222
  png_write_info(ping,ping_info);
7223
  png_write_info(ping,ping_info);
7223
7224
7224
#if (PNG_LIBPNG_VER == 10206)
7225
#if (PNG_LIBPNG_VER == 10206)
Lines 7313-7319 static MagickPassFail WriteOnePNGImage(M Link Here
7313
      {
7314
      {
7314
        if ((!mng_info->write_png8 && !mng_info->write_png24 &&
7315
        if ((!mng_info->write_png8 && !mng_info->write_png24 &&
7315
             !mng_info->write_png32) &&
7316
             !mng_info->write_png32) &&
7316
            (!image_matte || (ping_info->bit_depth >= QuantumDepth)) &&
7317
            (!image_matte || (ping_info_bit_depth >= QuantumDepth)) &&
7317
            (mng_info->optimize || mng_info->IsPalette) &&
7318
            (mng_info->optimize || mng_info->IsPalette) &&
7318
            IsGrayImage(image,&image->exception))
7319
            IsGrayImage(image,&image->exception))
7319
          {
7320
          {
Lines 7325-7331 static MagickPassFail WriteOnePNGImage(M Link Here
7325
                if (!AcquireImagePixels(image,0,y,image->columns,1,
7326
                if (!AcquireImagePixels(image,0,y,image->columns,1,
7326
                                        &image->exception))
7327
                                        &image->exception))
7327
                  break;
7328
                  break;
7328
                if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
7329
                if (ping_info_color_type == PNG_COLOR_TYPE_GRAY)
7329
                  {
7330
                  {
7330
                    if (mng_info->IsPalette)
7331
                    if (mng_info->IsPalette)
7331
                      (void) ExportImagePixelArea(image,
7332
                      (void) ExportImagePixelArea(image,
Lines 7368-7381 static MagickPassFail WriteOnePNGImage(M Link Here
7368
                                          "  pass %d, Image Is RGB,"
7369
                                          "  pass %d, Image Is RGB,"
7369
                                          " PNG colortype is %s (%d)",pass,
7370
                                          " PNG colortype is %s (%d)",pass,
7370
                                          PngColorTypeToString(
7371
                                          PngColorTypeToString(
7371
                                             ping_info->color_type),
7372
                                             ping_info_color_type),
7372
                                          ping_info->color_type);
7373
                                          ping_info_color_type);
7373
                  for (y=0; y < (long) image->rows; y++)
7374
                  for (y=0; y < (long) image->rows; y++)
7374
                    {
7375
                    {
7375
                      if (!AcquireImagePixels(image,0,y,image->columns,1,
7376
                      if (!AcquireImagePixels(image,0,y,image->columns,1,
7376
                                              &image->exception))
7377
                                              &image->exception))
7377
                        break;
7378
                        break;
7378
                      if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
7379
                      if (ping_info_color_type == PNG_COLOR_TYPE_GRAY)
7379
                        {
7380
                        {
7380
                          if (image->storage_class == DirectClass)
7381
                          if (image->storage_class == DirectClass)
7381
                            (void) ExportImagePixelArea(image,(QuantumType)
7382
                            (void) ExportImagePixelArea(image,(QuantumType)
Lines 7388-7394 static MagickPassFail WriteOnePNGImage(M Link Here
7388
                                                        quantum_size,
7389
                                                        quantum_size,
7389
                                                        png_pixels,0,0);
7390
                                                        png_pixels,0,0);
7390
                        }
7391
                        }
7391
                      else if (ping_info->color_type ==
7392
                      else if (ping_info_color_type ==
7392
                               PNG_COLOR_TYPE_GRAY_ALPHA)
7393
                               PNG_COLOR_TYPE_GRAY_ALPHA)
7393
                        (void) ExportImagePixelArea(image,(QuantumType)
7394
                        (void) ExportImagePixelArea(image,(QuantumType)
7394
                                                    GrayAlphaQuantum,
7395
                                                    GrayAlphaQuantum,
Lines 7433-7444 static MagickPassFail WriteOnePNGImage(M Link Here
7433
                      if (!AcquireImagePixels(image,0,y,image->columns,1,
7434
                      if (!AcquireImagePixels(image,0,y,image->columns,1,
7434
                                              &image->exception))
7435
                                              &image->exception))
7435
                        break;
7436
                        break;
7436
                      if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
7437
                      if (ping_info_color_type == PNG_COLOR_TYPE_GRAY)
7437
                        (void) ExportImagePixelArea(image,(QuantumType)
7438
                        (void) ExportImagePixelArea(image,(QuantumType)
7438
                                                    GrayQuantum,
7439
                                                    GrayQuantum,
7439
                                                    quantum_size,
7440
                                                    quantum_size,
7440
                                                    png_pixels,0,0);
7441
                                                    png_pixels,0,0);
7441
                      else if (ping_info->color_type ==
7442
                      else if (ping_info_color_type ==
7442
                               PNG_COLOR_TYPE_GRAY_ALPHA)
7443
                               PNG_COLOR_TYPE_GRAY_ALPHA)
7443
                        (void) ExportImagePixelArea(image,(QuantumType)
7444
                        (void) ExportImagePixelArea(image,(QuantumType)
7444
                                                    GrayAlphaQuantum,
7445
                                                    GrayAlphaQuantum,
Lines 7471-7508 static MagickPassFail WriteOnePNGImage(M Link Here
7471
                            "  Writing PNG image data");
7472
                            "  Writing PNG image data");
7472
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7473
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7473
                            "    Width: %lu",
7474
                            "    Width: %lu",
7474
                            (unsigned long)ping_info->width);
7475
                            (unsigned long)ping_info_width);
7475
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7476
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7476
                            "    Height: %lu",
7477
                            "    Height: %lu",
7477
                            (unsigned long)ping_info->height);
7478
                            (unsigned long)ping_info_height);
7478
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7479
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7479
                            "    PNG sample depth: %d",ping_info->bit_depth);
7480
                            "    PNG sample depth: %d",ping_info_bit_depth);
7480
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7481
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7481
                            "    PNG color type: %s (%d)",
7482
                            "    PNG color type: %s (%d)",
7482
                            PngColorTypeToString(ping_info->color_type),
7483
                            PngColorTypeToString(ping_info_color_type),
7483
                            ping_info->color_type);
7484
                            ping_info_color_type);
7484
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7485
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7485
                            "    PNG Interlace method: %d",
7486
                            "    PNG Interlace method: %d",
7486
                            ping_info->interlace_type);
7487
                            ping_info_interlace_method);
7487
    }
7488
    }
7488
  /*
7489
  /*
7489
    Generate text chunks.
7490
    Generate text chunks.
7490
  */
7491
  */
7491
#if (PNG_LIBPNG_VER <= 10005)
7492
  ping_info->num_text=0;
7493
#endif
7494
  attribute=GetImageAttribute(image,(char *) NULL);
7492
  attribute=GetImageAttribute(image,(char *) NULL);
7495
  for ( ; attribute != (const ImageAttribute *) NULL;
7493
  for ( ; attribute != (const ImageAttribute *) NULL;
7496
        attribute=attribute->next)
7494
        attribute=attribute->next)
7497
    {
7495
    {
7498
#if (PNG_LIBPNG_VER > 10005)
7499
      png_textp
7496
      png_textp
7500
        text;
7497
        text;
7501
#endif
7502
7498
7503
      if (*attribute->key == '[')
7499
      if (*attribute->key == '[')
7504
        continue;
7500
        continue;
7505
#if (PNG_LIBPNG_VER > 10005)
7506
      text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text));
7501
      text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text));
7507
      text[0].key=attribute->key;
7502
      text[0].key=attribute->key;
7508
      text[0].text=attribute->value;
7503
      text[0].text=attribute->value;
Lines 7519-7558 static MagickPassFail WriteOnePNGImage(M Link Here
7519
        }
7514
        }
7520
      png_set_text(ping,ping_info,text,1);
7515
      png_set_text(ping,ping_info,text,1);
7521
      png_free(ping,text);
7516
      png_free(ping,text);
7522
#else
7523
      /* Work directly with ping_info struct;
7524
       * png_set_text before libpng version
7525
       * 1.0.5a is leaky */
7526
      if (ping_info->num_text == 0)
7527
        {
7528
          ping_info->text=MagickAllocateMemory(png_text *,
7529
                                               256*sizeof(png_text));
7530
          if (ping_info->text == (png_text *) NULL)
7531
            (void) ThrowException(&image->exception,(ExceptionType)
7532
                                  ResourceLimitError,MemoryAllocationFailed,
7533
                                  image->filename);
7534
        }
7535
      i=ping_info->num_text++;
7536
      if (i > 255)
7537
        (void) ThrowException(&image->exception,(ExceptionType)
7538
                              ResourceLimitError,
7539
                              "Cannot write more than 256 PNG text chunks",
7540
                              image->filename);
7541
      ping_info->text[i].key=attribute->key;
7542
      ping_info->text[i].text=attribute->value;
7543
      ping_info->text[i].text_length=strlen(attribute->value);
7544
      ping_info->text[i].compression=
7545
        image_info->compression == NoCompression ||
7546
        (image_info->compression == UndefinedCompression &&
7547
         ping_info->text[i].text_length < 128) ? -1 : 0;
7548
      if (logging)
7549
        {
7550
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7551
                                "  Setting up text chunk");
7552
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7553
                                "    keyword: %s",ping_info->text[i].key);
7554
        }
7555
#endif
7556
    }
7517
    }
7557
  if (logging)
7518
  if (logging)
7558
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7519
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
Lines 7560-7568 static MagickPassFail WriteOnePNGImage(M Link Here
7560
  png_write_end(ping,ping_info);
7521
  png_write_end(ping,ping_info);
7561
  if (mng_info->need_fram && (int) image->dispose == BackgroundDispose)
7522
  if (mng_info->need_fram && (int) image->dispose == BackgroundDispose)
7562
    {
7523
    {
7563
      if (mng_info->page.x || mng_info->page.y || (ping_info->width !=
7524
      if (mng_info->page.x || mng_info->page.y || (ping_info_width !=
7564
                                                   mng_info->page.width) ||
7525
                                                   mng_info->page.width) ||
7565
          (ping_info->height != mng_info->page.height))
7526
          (ping_info_height != mng_info->page.height))
7566
        {
7527
        {
7567
          unsigned char
7528
          unsigned char
7568
            chunk[32];
7529
            chunk[32];
Lines 7583-7592 static MagickPassFail WriteOnePNGImage(M Link Here
7583
          chunk[14]=0; /* clipping boundaries delta type */
7544
          chunk[14]=0; /* clipping boundaries delta type */
7584
          PNGLong(chunk+15,(png_uint_32) (mng_info->page.x)); /* left cb */
7545
          PNGLong(chunk+15,(png_uint_32) (mng_info->page.x)); /* left cb */
7585
          PNGLong(chunk+19,(png_uint_32) (mng_info->page.x +
7546
          PNGLong(chunk+19,(png_uint_32) (mng_info->page.x +
7586
                                          ping_info->width));
7547
                                          ping_info_width));
7587
          PNGLong(chunk+23,(png_uint_32) (mng_info->page.y)); /* top cb */
7548
          PNGLong(chunk+23,(png_uint_32) (mng_info->page.y)); /* top cb */
7588
          PNGLong(chunk+27,(png_uint_32) (mng_info->page.y +
7549
          PNGLong(chunk+27,(png_uint_32) (mng_info->page.y +
7589
                                          ping_info->height));
7550
                                          ping_info_height));
7590
          (void) WriteBlob(image,31,(char *) chunk);
7551
          (void) WriteBlob(image,31,(char *) chunk);
7591
          (void) WriteBlobMSBULong(image,crc32(0,chunk,31));
7552
          (void) WriteBlobMSBULong(image,crc32(0,chunk,31));
7592
          mng_info->old_framing_mode=4;
7553
          mng_info->old_framing_mode=4;
Lines 7605-7611 static MagickPassFail WriteOnePNGImage(M Link Here
7605
7566
7606
  /* Save depth actually written */
7567
  /* Save depth actually written */
7607
7568
7608
  s[0]=(char) ping_info->bit_depth;
7569
  s[0]=(char) ping_info_bit_depth;
7609
  s[1]='\0';
7570
  s[1]='\0';
7610
7571
7611
  (void) SetImageAttribute(image,"[png:bit-depth-written]",s);
7572
  (void) SetImageAttribute(image,"[png:bit-depth-written]",s);
Lines 7613-7630 static MagickPassFail WriteOnePNGImage(M Link Here
7613
  /*
7574
  /*
7614
    Free PNG resources.
7575
    Free PNG resources.
7615
  */
7576
  */
7616
#if (PNG_LIBPNG_VER < 10007)
7617
  if (ping_info->valid & PNG_INFO_PLTE)
7618
    {
7619
      MagickFreeMemory(ping_info->palette);
7620
      ping_info->valid&=(~PNG_INFO_PLTE);
7621
    }
7622
#endif
7623
  if (ping_info->valid & PNG_INFO_tRNS)
7624
    {
7625
      MagickFreeMemory(ping_info->trans_alpha);
7626
      ping_info->valid&=(~PNG_INFO_tRNS);
7627
    }
7628
  png_destroy_write_struct(&ping,&ping_info);
7577
  png_destroy_write_struct(&ping,&ping_info);
7629
7578
7630
  MagickFreeMemory(png_pixels);
7579
  MagickFreeMemory(png_pixels);
Lines 8365-8387 static unsigned int WriteMNGImage(const Link Here
8365
    final_delay=0,
8314
    final_delay=0,
8366
    initial_delay;
8315
    initial_delay;
8367
8316
8368
#if (PNG_LIBPNG_VER < 10007)
8317
#if (PNG_LIBPNG_VER < 10200)
8369
  if (image_info->verbose)
8318
  if (image_info->verbose)
8370
    printf("Your PNG library (libpng-%s) is rather old.\n",
8319
    printf("Your PNG library (libpng-%s) is rather old.\n",
8371
           PNG_LIBPNG_VER_STRING);
8320
           PNG_LIBPNG_VER_STRING);
8372
#endif
8321
#endif
8373
8322
8374
#if (PNG_LIBPNG_VER >= 10400)
8375
#  ifndef  PNG_TRANSFORM_GRAY_TO_RGB    /* Added at libpng-1.4.0beta67 */
8376
  if (image_info->verbose)
8377
    {
8378
      printf("Your PNG library (libpng-%s) is an old beta version.\n",
8379
           PNG_LIBPNG_VER_STRING);
8380
      printf("Please update it.\n");
8381
    }
8382
#  endif
8383
#endif
8384
8385
  /*
8323
  /*
8386
    Open image file.
8324
    Open image file.
8387
  */
8325
  */
Lines 9196-9202 static unsigned int WriteMNGImage(const Link Here
9196
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteMNGImage()");
9134
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteMNGImage()");
9197
  return(MagickPass);
9135
  return(MagickPass);
9198
}
9136
}
9199
#else /* PNG_LIBPNG_VER > 95 */
9137
#else /* PNG_LIBPNG_VER > 10011 */
9200
static unsigned int WritePNGImage(const ImageInfo *image_info,Image *image)
9138
static unsigned int WritePNGImage(const ImageInfo *image_info,Image *image)
9201
{
9139
{
9202
  image=image;
9140
  image=image;
Lines 9208-9212 static unsigned int WriteMNGImage(const Link Here
9208
{
9146
{
9209
  return (WritePNGImage(image_info,image));
9147
  return (WritePNGImage(image_info,image));
9210
}
9148
}
9211
#endif /* PNG_LIBPNG_VER > 95 */
9149
#endif /* PNG_LIBPNG_VER > 10011 */
9212
#endif
9150
#endif

Return to bug 362637