Title: [181553] trunk
Revision
181553
Author
commit-qu...@webkit.org
Date
2015-03-16 07:24:46 -0700 (Mon, 16 Mar 2015)

Log Message

Add APNG support
https://bugs.webkit.org/show_bug.cgi?id=17022

Patch by Max Stepin <maxste...@gmail.com> on 2015-03-16
Reviewed by Carlos Garcia Campos.

Source/WebCore:

Test: fast/images/animated-png.html

* platform/image-decoders/ImageDecoder.h:
(WebCore::ImageFrame::divide255):
(WebCore::ImageFrame::overRGBA):
* platform/image-decoders/png/PNGImageDecoder.cpp:
(WebCore::frameHeader):
(WebCore::readChunks):
(WebCore::PNGImageReader::PNGImageReader):
(WebCore::PNGImageDecoder::PNGImageDecoder):
(WebCore::PNGImageDecoder::frameBufferAtIndex):
(WebCore::PNGImageDecoder::headerAvailable):
(WebCore::PNGImageDecoder::rowAvailable):
(WebCore::PNGImageDecoder::pngComplete):
(WebCore::PNGImageDecoder::readChunks):
(WebCore::PNGImageDecoder::frameHeader):
(WebCore::PNGImageDecoder::init):
(WebCore::PNGImageDecoder::clearFrameBufferCache):
(WebCore::PNGImageDecoder::initFrameBuffer):
(WebCore::PNGImageDecoder::frameComplete):
(WebCore::PNGImageDecoder::processingStart):
(WebCore::PNGImageDecoder::processingFinish):
(WebCore::PNGImageDecoder::fallbackNotAnimated):
* platform/image-decoders/png/PNGImageDecoder.h:
(WebCore::PNGImageDecoder::frameCount):
(WebCore::PNGImageDecoder::repetitionCount):
(WebCore::PNGImageDecoder::isComplete):

Source/WTF:

* wtf/FeatureDefines.h:

LayoutTests:

* fast/images/animated-png-expected.html: Added.
* fast/images/animated-png.html: Added.
* fast/images/resources/apng00-ref.png: Added.
* fast/images/resources/apng00.png: Added.
* fast/images/resources/apng01-ref.png: Added.
* fast/images/resources/apng01.png: Added.
* fast/images/resources/apng02-ref.png: Added.
* fast/images/resources/apng02.png: Added.
* fast/images/resources/apng04-ref.png: Added.
* fast/images/resources/apng04.png: Added.
* fast/images/resources/apng08-ref.png: Added.
* fast/images/resources/apng08.png: Added.
* fast/images/resources/apng10-ref.png: Added.
* fast/images/resources/apng10.png: Added.
* fast/images/resources/apng11-ref.png: Added.
* fast/images/resources/apng11.png: Added.
* fast/images/resources/apng12-ref.png: Added.
* fast/images/resources/apng12.png: Added.
* fast/images/resources/apng14-ref.png: Added.
* fast/images/resources/apng14.png: Added.
* fast/images/resources/apng18-ref.png: Added.
* fast/images/resources/apng18.png: Added.
* fast/images/resources/apng24-ref.png: Added.
* fast/images/resources/apng24.png: Added.
* fast/images/resources/apng26-ref.png: Added.
* fast/images/resources/apng26.png: Added.
* platform/mac/TestExpectations:

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (181552 => 181553)


--- trunk/LayoutTests/ChangeLog	2015-03-16 12:30:43 UTC (rev 181552)
+++ trunk/LayoutTests/ChangeLog	2015-03-16 14:24:46 UTC (rev 181553)
@@ -1,3 +1,38 @@
+2015-03-16  Max Stepin  <maxste...@gmail.com>
+
+        Add APNG support
+        https://bugs.webkit.org/show_bug.cgi?id=17022
+
+        Reviewed by Carlos Garcia Campos.
+
+        * fast/images/animated-png-expected.html: Added.
+        * fast/images/animated-png.html: Added.
+        * fast/images/resources/apng00-ref.png: Added.
+        * fast/images/resources/apng00.png: Added.
+        * fast/images/resources/apng01-ref.png: Added.
+        * fast/images/resources/apng01.png: Added.
+        * fast/images/resources/apng02-ref.png: Added.
+        * fast/images/resources/apng02.png: Added.
+        * fast/images/resources/apng04-ref.png: Added.
+        * fast/images/resources/apng04.png: Added.
+        * fast/images/resources/apng08-ref.png: Added.
+        * fast/images/resources/apng08.png: Added.
+        * fast/images/resources/apng10-ref.png: Added.
+        * fast/images/resources/apng10.png: Added.
+        * fast/images/resources/apng11-ref.png: Added.
+        * fast/images/resources/apng11.png: Added.
+        * fast/images/resources/apng12-ref.png: Added.
+        * fast/images/resources/apng12.png: Added.
+        * fast/images/resources/apng14-ref.png: Added.
+        * fast/images/resources/apng14.png: Added.
+        * fast/images/resources/apng18-ref.png: Added.
+        * fast/images/resources/apng18.png: Added.
+        * fast/images/resources/apng24-ref.png: Added.
+        * fast/images/resources/apng24.png: Added.
+        * fast/images/resources/apng26-ref.png: Added.
+        * fast/images/resources/apng26.png: Added.
+        * platform/mac/TestExpectations:
+
 2015-03-16  Gyuyoung Kim  <gyuyoung....@samsung.com>
 
         [EFL] Unreviewed EFL gardening on 16th Mar.

Added: trunk/LayoutTests/fast/images/animated-png-expected.html (0 => 181553)


--- trunk/LayoutTests/fast/images/animated-png-expected.html	                        (rev 0)
+++ trunk/LayoutTests/fast/images/animated-png-expected.html	2015-03-16 14:24:46 UTC (rev 181553)
@@ -0,0 +1,13 @@
+<html>
+<head>
+<title>APNG reftest: when animation ends, compare its last frame against the reference static PNG.</title>
+<style>
+img { margin: 1px; }
+</style>
+</head>
+<body style="margin: 1px">
+<img src="" src="" src="" src="" src=""
+<img src="" src="" src="" src="" src=""
+<img src="" src=""
+</body>
+</html>

Added: trunk/LayoutTests/fast/images/animated-png.html (0 => 181553)


--- trunk/LayoutTests/fast/images/animated-png.html	                        (rev 0)
+++ trunk/LayoutTests/fast/images/animated-png.html	2015-03-16 14:24:46 UTC (rev 181553)
@@ -0,0 +1,27 @@
+<html>
+<head>
+<title>APNG reftest: when animation ends, compare its last frame against the reference static PNG.</title>
+<script>
+    if (window.testRunner)
+        testRunner.dumpAsText(true);
+
+    window._onload_ = function() {
+        if (window.testRunner)
+            testRunner.waitUntilDone();
+
+        window.setTimeout(function() {
+            if (window.testRunner)
+                testRunner.notifyDone();
+        }, 300);
+    }
+</script>
+<style>
+img { margin: 1px; }
+</style>
+</head>
+<body style="margin: 1px">
+<img src="" src="" src="" src="" src=""
+<img src="" src="" src="" src="" src=""
+<img src="" src=""
+</body>
+</html>

Added: trunk/LayoutTests/fast/images/resources/apng00-ref.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng00-ref.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng00.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng00.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng01-ref.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng01-ref.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng01.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng01.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng02-ref.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng02-ref.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng02.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng02.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng04-ref.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng04-ref.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng04.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng04.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng08-ref.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng08-ref.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng08.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng08.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng10-ref.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng10-ref.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng10.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng10.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng11-ref.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng11-ref.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng11.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng11.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng12-ref.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng12-ref.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng12.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng12.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng14-ref.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng14-ref.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng14.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng14.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng18-ref.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng18-ref.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng18.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng18.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng24-ref.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng24-ref.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng24.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng24.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng26-ref.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng26-ref.png ___________________________________________________________________

Added: svn:mime-type

Added: trunk/LayoutTests/fast/images/resources/apng26.png


(Binary files differ)
Property changes on: trunk/LayoutTests/fast/images/resources/apng26.png ___________________________________________________________________

Added: svn:mime-type

Modified: trunk/LayoutTests/platform/mac/TestExpectations (181552 => 181553)


--- trunk/LayoutTests/platform/mac/TestExpectations	2015-03-16 12:30:43 UTC (rev 181552)
+++ trunk/LayoutTests/platform/mac/TestExpectations	2015-03-16 14:24:46 UTC (rev 181553)
@@ -1273,3 +1273,6 @@
 
 # Test uses Yosemite blurs
 [ Mavericks ] compositing/media-controls-bar-appearance.html [ Skip ]
+
+# Mavericks and prior do not support APNG.
+webkit.org/b/17022 [ MountainLion Mavericks ] fast/images/animated-png.html [ Skip ]

Modified: trunk/Source/WTF/ChangeLog (181552 => 181553)


--- trunk/Source/WTF/ChangeLog	2015-03-16 12:30:43 UTC (rev 181552)
+++ trunk/Source/WTF/ChangeLog	2015-03-16 14:24:46 UTC (rev 181553)
@@ -1,3 +1,12 @@
+2015-03-16  Max Stepin  <maxste...@gmail.com>
+
+        Add APNG support
+        https://bugs.webkit.org/show_bug.cgi?id=17022
+
+        Reviewed by Carlos Garcia Campos.
+
+        * wtf/FeatureDefines.h:
+
 2015-03-15  Benjamin Poulain  <benja...@webkit.org>
 
         CSS: fix the case-insensitive matching of the attribute selectors Begin, End and Hyphen

Modified: trunk/Source/WTF/wtf/FeatureDefines.h (181552 => 181553)


--- trunk/Source/WTF/wtf/FeatureDefines.h	2015-03-16 12:30:43 UTC (rev 181552)
+++ trunk/Source/WTF/wtf/FeatureDefines.h	2015-03-16 14:24:46 UTC (rev 181553)
@@ -307,6 +307,10 @@
 #define ENABLE_ACCELERATED_OVERFLOW_SCROLLING 0
 #endif
 
+#if !defined(ENABLE_APNG)
+#define ENABLE_APNG 1
+#endif
+
 #if !defined(ENABLE_BATTERY_STATUS)
 #define ENABLE_BATTERY_STATUS 0
 #endif

Modified: trunk/Source/WebCore/ChangeLog (181552 => 181553)


--- trunk/Source/WebCore/ChangeLog	2015-03-16 12:30:43 UTC (rev 181552)
+++ trunk/Source/WebCore/ChangeLog	2015-03-16 14:24:46 UTC (rev 181553)
@@ -1,3 +1,38 @@
+2015-03-16  Max Stepin  <maxste...@gmail.com>
+
+        Add APNG support
+        https://bugs.webkit.org/show_bug.cgi?id=17022
+
+        Reviewed by Carlos Garcia Campos.
+
+        Test: fast/images/animated-png.html
+
+        * platform/image-decoders/ImageDecoder.h:
+        (WebCore::ImageFrame::divide255):
+        (WebCore::ImageFrame::overRGBA):
+        * platform/image-decoders/png/PNGImageDecoder.cpp:
+        (WebCore::frameHeader):
+        (WebCore::readChunks):
+        (WebCore::PNGImageReader::PNGImageReader):
+        (WebCore::PNGImageDecoder::PNGImageDecoder):
+        (WebCore::PNGImageDecoder::frameBufferAtIndex):
+        (WebCore::PNGImageDecoder::headerAvailable):
+        (WebCore::PNGImageDecoder::rowAvailable):
+        (WebCore::PNGImageDecoder::pngComplete):
+        (WebCore::PNGImageDecoder::readChunks):
+        (WebCore::PNGImageDecoder::frameHeader):
+        (WebCore::PNGImageDecoder::init):
+        (WebCore::PNGImageDecoder::clearFrameBufferCache):
+        (WebCore::PNGImageDecoder::initFrameBuffer):
+        (WebCore::PNGImageDecoder::frameComplete):
+        (WebCore::PNGImageDecoder::processingStart):
+        (WebCore::PNGImageDecoder::processingFinish):
+        (WebCore::PNGImageDecoder::fallbackNotAnimated):
+        * platform/image-decoders/png/PNGImageDecoder.h:
+        (WebCore::PNGImageDecoder::frameCount):
+        (WebCore::PNGImageDecoder::repetitionCount):
+        (WebCore::PNGImageDecoder::isComplete):
+
 2015-03-15  Benjamin Poulain  <benja...@webkit.org>
 
         CSS: fix the case-insensitive matching of the attribute selectors Begin, End and Hyphen

Modified: trunk/Source/WebCore/platform/image-decoders/ImageDecoder.h (181552 => 181553)


--- trunk/Source/WebCore/platform/image-decoders/ImageDecoder.h	2015-03-16 12:30:43 UTC (rev 181552)
+++ trunk/Source/WebCore/platform/image-decoders/ImageDecoder.h	2015-03-16 14:24:46 UTC (rev 181553)
@@ -165,6 +165,48 @@
             *dest = (a << 24 | r << 16 | g << 8 | b);
         }
 
+#if ENABLE(APNG)
+        static inline unsigned divide255(unsigned a)
+        {
+            return (a + (a >> 8) + 1) >> 8;
+        }
+
+        inline void overRGBA(PixelData* dest, unsigned r, unsigned g, unsigned b, unsigned a)
+        {
+            if (!a)
+                return;
+
+            if (a < 255) {
+                unsigned aDest = ((*dest) >> 24) & 255;
+                if (aDest) {
+                    unsigned rDest = ((*dest) >> 16) & 255;
+                    unsigned gDest = ((*dest) >> 8) & 255;
+                    unsigned bDest = (*dest) & 255;
+                    unsigned aAux = 255 - a;
+                    if (!m_premultiplyAlpha) {
+                        rDest = divide255(rDest * aDest);
+                        gDest = divide255(gDest * aDest);
+                        bDest = divide255(bDest * aDest);
+                    }
+                    r = divide255(r * a + rDest * aAux);
+                    g = divide255(g * a + gDest * aAux);
+                    b = divide255(b * a + bDest * aAux);
+                    a += divide255(aDest * aAux);
+                    if (!m_premultiplyAlpha) {
+                        r = (r * 255 + a - 1) / a;
+                        g = (g * 255 + a - 1) / a;
+                        b = (b * 255 + a - 1) / a;
+                    }
+                } else if (m_premultiplyAlpha) {
+                    r = divide255(r * a);
+                    g = divide255(g * a);
+                    b = divide255(b * a);
+                }
+            }
+            *dest = (a << 24 | r << 16 | g << 8 | b);
+        }
+#endif
+
     private:
         int width() const
         {

Modified: trunk/Source/WebCore/platform/image-decoders/png/PNGImageDecoder.cpp (181552 => 181553)


--- trunk/Source/WebCore/platform/image-decoders/png/PNGImageDecoder.cpp	2015-03-16 12:30:43 UTC (rev 181552)
+++ trunk/Source/WebCore/platform/image-decoders/png/PNGImageDecoder.cpp	2015-03-16 14:24:46 UTC (rev 181553)
@@ -7,6 +7,7 @@
  *
  * Other contributors:
  *   Stuart Parmenter <stu...@mozilla.com>
+ *   Max Stepin <maxste...@gmail.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -100,6 +101,21 @@
     static_cast<PNGImageDecoder*>(png_get_progressive_ptr(png))->pngComplete();
 }
 
+#if ENABLE(APNG)
+// Called when we have the frame header.
+static void PNGAPI frameHeader(png_structp png, png_infop)
+{
+    static_cast<PNGImageDecoder*>(png_get_progressive_ptr(png))->frameHeader();
+}
+
+// Called when we found user chunks.
+static int PNGAPI readChunks(png_structp png, png_unknown_chunkp chunk)
+{
+    static_cast<PNGImageDecoder*>(png_get_user_chunk_ptr(png))->readChunks(chunk);
+    return 1;
+}
+#endif
+
 class PNGImageReader {
     WTF_MAKE_FAST_ALLOCATED;
 public:
@@ -117,6 +133,12 @@
         m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, decodingFailed, decodingWarning);
         m_info = png_create_info_struct(m_png);
         png_set_progressive_read_fn(m_png, decoder, headerAvailable, rowAvailable, pngComplete);
+#if ENABLE(APNG)
+        png_byte apngChunks[]= {"acTL\0fcTL\0fdAT\0"};
+        png_set_keep_unknown_chunks(m_png, 1, apngChunks, 3);
+        png_set_read_user_chunk_fn(m_png, static_cast<png_voidp>(decoder), readChunks);
+        decoder->init();
+#endif
     }
 
     ~PNGImageReader()
@@ -215,10 +237,33 @@
 #endif
 };
 
-PNGImageDecoder::PNGImageDecoder(ImageSource::AlphaOption alphaOption,
-                                 ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)
+PNGImageDecoder::PNGImageDecoder(ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)
     : ImageDecoder(alphaOption, gammaAndColorProfileOption)
     , m_doNothingOnFailure(false)
+    , m_currentFrame(0)
+#if ENABLE(APNG)
+    , m_png(nullptr)
+    , m_info(nullptr)
+    , m_isAnimated(false)
+    , m_frameInfo(false)
+    , m_frameIsHidden(false)
+    , m_hasInfo(false)
+    , m_gamma(45455)
+    , m_frameCount(1)
+    , m_playCount(0)
+    , m_totalFrames(0)
+    , m_sizePLTE(0)
+    , m_sizetRNS(0)
+    , m_sequenceNumber(0)
+    , m_width(0)
+    , m_height(0)
+    , m_xOffset(0)
+    , m_yOffset(0)
+    , m_delayNumerator(1)
+    , m_delayDenominator(1)
+    , m_dispose(0)
+    , m_blend(0)
+#endif
 {
 }
 
@@ -245,15 +290,23 @@
 
 ImageFrame* PNGImageDecoder::frameBufferAtIndex(size_t index)
 {
+#if ENABLE(APNG)
+    if (!isSizeAvailable())
+        return nullptr;
+
+    if (index >= frameCount())
+        index = frameCount() - 1;
+#else
     if (index)
-        return 0;
+        return nullptr;
+#endif
 
     if (m_frameBufferCache.isEmpty()) {
         m_frameBufferCache.resize(1);
         m_frameBufferCache[0].setPremultiplyAlpha(m_premultiplyAlpha);
     }
 
-    ImageFrame& frame = m_frameBufferCache[0];
+    ImageFrame& frame = m_frameBufferCache[index];
     if (frame.status() != ImageFrame::FrameComplete)
         decode(false);
     return &frame;
@@ -329,14 +382,64 @@
 
     // The options we set here match what Mozilla does.
 
+#if ENABLE(APNG)
+    m_hasInfo = true;
+    if (m_isAnimated) {
+        png_save_uint_32(m_dataIHDR, 13);
+        memcpy(m_dataIHDR + 4, "IHDR", 4);
+        png_save_uint_32(m_dataIHDR + 8, width);
+        png_save_uint_32(m_dataIHDR + 12, height);
+        m_dataIHDR[16] = bitDepth;
+        m_dataIHDR[17] = colorType;
+        m_dataIHDR[18] = compressionType;
+        m_dataIHDR[19] = filterType;
+        m_dataIHDR[20] = interlaceType;
+    }
+#endif
+
     // Expand to ensure we use 24-bit for RGB and 32-bit for RGBA.
-    if (colorType == PNG_COLOR_TYPE_PALETTE || (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8))
+    if (colorType == PNG_COLOR_TYPE_PALETTE) {
+#if ENABLE(APNG)
+        if (m_isAnimated) {
+            png_colorp palette;
+            int paletteSize = 0;
+            png_get_PLTE(png, info, &palette, &paletteSize);
+            paletteSize *= 3;
+            png_save_uint_32(m_dataPLTE, paletteSize);
+            memcpy(m_dataPLTE + 4, "PLTE", 4);
+            memcpy(m_dataPLTE + 8, palette, paletteSize);
+            m_sizePLTE = paletteSize + 12;
+        }
+#endif
         png_set_expand(png);
+    }
 
+    if (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8)
+        png_set_expand(png);
+
     png_bytep trns = 0;
     int trnsCount = 0;
+    png_color_16p transValues;
     if (png_get_valid(png, info, PNG_INFO_tRNS)) {
-        png_get_tRNS(png, info, &trns, &trnsCount, 0);
+        png_get_tRNS(png, info, &trns, &trnsCount, &transValues);
+#if ENABLE(APNG)
+        if (m_isAnimated) {
+            if (colorType == PNG_COLOR_TYPE_RGB) {
+                png_save_uint_16(m_datatRNS + 8, transValues->red);
+                png_save_uint_16(m_datatRNS + 10, transValues->green);
+                png_save_uint_16(m_datatRNS + 12, transValues->blue);
+                trnsCount = 6;
+            } else if (colorType == PNG_COLOR_TYPE_GRAY) {
+                png_save_uint_16(m_datatRNS + 8, transValues->gray);
+                trnsCount = 2;
+            } else if (colorType == PNG_COLOR_TYPE_PALETTE)
+                memcpy(m_datatRNS + 8, trns, trnsCount);
+
+            png_save_uint_32(m_datatRNS, trnsCount);
+            memcpy(m_datatRNS + 4, "tRNS", 4);
+            m_sizetRNS = trnsCount + 12;
+        }
+#endif
         png_set_expand(png);
     }
 
@@ -369,6 +472,9 @@
             png_set_gAMA(png, info, gamma);
         }
         png_set_gamma(png, cDefaultGamma, gamma);
+#if ENABLE(APNG)
+        m_gamma = static_cast<int>(gamma * 100000);
+#endif
     } else
         png_set_gamma(png, cDefaultGamma, cInverseGamma);
 
@@ -424,7 +530,11 @@
         return;
 
     // Initialize the framebuffer if needed.
-    ImageFrame& buffer = m_frameBufferCache[0];
+#if ENABLE(APNG)
+    if (m_currentFrame >= frameCount())
+        return;
+#endif
+    ImageFrame& buffer = m_frameBufferCache[m_currentFrame];
     if (buffer.status() == ImageFrame::FrameEmpty) {
         png_structp png = m_reader->pngPtr();
         if (!buffer.setSize(scaledSize().width(), scaledSize().height())) {
@@ -433,8 +543,10 @@
         }
 
         unsigned colorChannels = m_reader->hasAlpha() ? 4 : 3;
-        if (PNG_INTERLACE_ADAM7 == png_get_interlace_type(png, m_reader->infoPtr())) {
-            m_reader->createInterlaceBuffer(colorChannels * size().width() * size().height());
+        if (PNG_INTERLACE_ADAM7 == png_get_interlace_type(png, m_reader->infoPtr())
+            || m_currentFrame) {
+            if (!m_reader->interlaceBuffer())
+                m_reader->createInterlaceBuffer(colorChannels * size().width() * size().height());
             if (!m_reader->interlaceBuffer()) {
                 longjmp(JMPBUF(png), 1);
                 return;
@@ -442,7 +554,7 @@
         }
 
 #if USE(QCMSLIB)
-        if (m_reader->colorTransform()) {
+        if (m_reader->colorTransform() && !m_currentFrame) {
             m_reader->createRowBuffer(colorChannels * size().width());
             if (!m_reader->rowBuffer()) {
                 longjmp(JMPBUF(png), 1);
@@ -454,6 +566,11 @@
         buffer.setHasAlpha(false);
         buffer.setColorProfile(m_colorProfile);
 
+#if ENABLE(APNG)
+        if (m_currentFrame)
+            initFrameBuffer(m_currentFrame);
+        else
+#endif
         // For PNGs, the frame always fills the entire image.
         buffer.setOriginalFrameRect(IntRect(IntPoint(), size()));
     }
@@ -504,6 +621,12 @@
 
     if (png_bytep interlaceBuffer = m_reader->interlaceBuffer()) {
         row = interlaceBuffer + (rowIndex * colorChannels * size().width());
+#if ENABLE(APNG)
+        if (m_currentFrame) {
+            png_progressive_combine_row(m_png, row, rowBuffer);
+            return; // Only do incremental image display for the first frame.
+        }
+#endif
         png_progressive_combine_row(m_reader->pngPtr(), row, rowBuffer);
     }
 
@@ -552,6 +675,14 @@
 
 void PNGImageDecoder::pngComplete()
 {
+#if ENABLE(APNG)
+    if (m_isAnimated) {
+        if (!processingFinish() && m_frameCount == m_currentFrame)
+            return;
+
+        fallbackNotAnimated();
+    }
+#endif
     if (!m_frameBufferCache.isEmpty())
         m_frameBufferCache.first().setStatus(ImageFrame::FrameComplete);
 }
@@ -574,4 +705,371 @@
         m_reader = nullptr;
 }
 
+#if ENABLE(APNG)
+void PNGImageDecoder::readChunks(png_unknown_chunkp chunk)
+{
+    if (!memcmp(chunk->name, "acTL", 4) && chunk->size == 8) {
+        if (m_hasInfo || m_isAnimated)
+            return;
+
+        m_frameCount = png_get_uint_32(chunk->data);
+        m_playCount = png_get_uint_32(chunk->data + 4);
+
+        if (!m_frameCount || m_frameCount > PNG_UINT_31_MAX || m_playCount > PNG_UINT_31_MAX) {
+            fallbackNotAnimated();
+            return;
+        }
+
+        m_isAnimated = true;
+        if (!m_frameInfo)
+            m_frameIsHidden = true;
+
+        if (m_frameBufferCache.size() == m_frameCount)
+            return;
+
+        m_frameBufferCache.resize(m_frameCount);
+        for (auto& imageFrame : m_frameBufferCache)
+            imageFrame.setPremultiplyAlpha(m_premultiplyAlpha);
+    } else if (!memcmp(chunk->name, "fcTL", 4) && chunk->size == 26) {
+        if (m_hasInfo && !m_isAnimated)
+            return;
+
+        m_frameInfo = false;
+
+        if (processingFinish()) {
+            fallbackNotAnimated();
+            return;
+        }
+
+        // At this point the old frame is done. Let's start a new one.
+        unsigned sequenceNumber = png_get_uint_32(chunk->data);
+        if (sequenceNumber != m_sequenceNumber++) {
+            fallbackNotAnimated();
+            return;
+        }
+
+        m_width = png_get_uint_32(chunk->data + 4);
+        m_height = png_get_uint_32(chunk->data + 8);
+        m_xOffset = png_get_uint_32(chunk->data + 12);
+        m_yOffset = png_get_uint_32(chunk->data + 16);
+        m_delayNumerator = png_get_uint_16(chunk->data + 20);
+        m_delayDenominator = png_get_uint_16(chunk->data + 22);
+        m_dispose = chunk->data[24];
+        m_blend = chunk->data[25];
+
+        png_structp png = m_reader->pngPtr();
+        png_infop info = m_reader->infoPtr();
+        png_uint_32 width = png_get_image_width(png, info);
+        png_uint_32 height = png_get_image_height(png, info);
+
+        if (m_width > cMaxPNGSize || m_height > cMaxPNGSize
+            || m_xOffset > cMaxPNGSize || m_yOffset > cMaxPNGSize
+            || m_xOffset + m_width > width
+            || m_yOffset + m_height > height
+            || m_dispose > 2 || m_blend > 1) {
+            fallbackNotAnimated();
+            return;
+        }
+
+        m_frameInfo = true;
+        m_frameIsHidden = false;
+
+        if (processingStart(chunk)) {
+            fallbackNotAnimated();
+            return;
+        }
+    } else if (!memcmp(chunk->name, "fdAT", 4) && chunk->size >= 4) {
+        if (!m_frameInfo || !m_isAnimated)
+            return;
+
+        unsigned sequenceNumber = png_get_uint_32(chunk->data);
+        if (sequenceNumber != m_sequenceNumber++) {
+            fallbackNotAnimated();
+            return;
+        }
+
+        if (setjmp(JMPBUF(m_png))) {
+            fallbackNotAnimated();
+            return;
+        }
+
+        png_save_uint_32(chunk->data, chunk->size - 4);
+        png_process_data(m_png, m_info, chunk->data, 4);
+        memcpy(chunk->data, "IDAT", 4);
+        png_process_data(m_png, m_info, chunk->data, chunk->size);
+        png_process_data(m_png, m_info, chunk->data, 4);
+    }
+}
+
+void PNGImageDecoder::frameHeader()
+{
+    int colorType = png_get_color_type(m_png, m_info);
+
+    if (colorType == PNG_COLOR_TYPE_PALETTE)
+        png_set_expand(m_png);
+
+    int bitDepth = png_get_bit_depth(m_png, m_info);
+    if (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8)
+        png_set_expand(m_png);
+
+    if (png_get_valid(m_png, m_info, PNG_INFO_tRNS))
+        png_set_expand(m_png);
+
+    if (bitDepth == 16)
+        png_set_strip_16(m_png);
+
+    if (colorType == PNG_COLOR_TYPE_GRAY || colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
+        png_set_gray_to_rgb(m_png);
+
+    double gamma;
+    if (png_get_gAMA(m_png, m_info, &gamma))
+        png_set_gamma(m_png, cDefaultGamma, gamma);
+
+    png_set_interlace_handling(m_png);
+
+    png_read_update_info(m_png, m_info);
+}
+
+void PNGImageDecoder::init()
+{
+    m_isAnimated = false;
+    m_frameInfo = false;
+    m_frameIsHidden = false;
+    m_hasInfo = false;
+    m_currentFrame = 0;
+    m_totalFrames = 0;
+    m_sequenceNumber = 0;
+}
+
+void PNGImageDecoder::clearFrameBufferCache(size_t clearBeforeFrame)
+{
+    if (m_frameBufferCache.isEmpty())
+        return;
+
+    // See GIFImageDecoder for full explanation.
+    clearBeforeFrame = std::min(clearBeforeFrame, m_frameBufferCache.size() - 1);
+    const Vector<ImageFrame>::iterator end(m_frameBufferCache.begin() + clearBeforeFrame);
+
+    Vector<ImageFrame>::iterator i(end);
+    for (; (i != m_frameBufferCache.begin()) && ((i->status() == ImageFrame::FrameEmpty) || (i->disposalMethod() == ImageFrame::DisposeOverwritePrevious)); --i) {
+        if ((i->status() == ImageFrame::FrameComplete) && (i != end))
+            i->clearPixelData();
+    }
+
+    // Now |i| holds the last frame we need to preserve; clear prior frames.
+    for (Vector<ImageFrame>::iterator j(m_frameBufferCache.begin()); j != i; ++j) {
+        ASSERT(j->status() != ImageFrame::FramePartial);
+        if (j->status() != ImageFrame::FrameEmpty)
+            j->clearPixelData();
+    }
+}
+
+void PNGImageDecoder::initFrameBuffer(size_t frameIndex)
+{
+    if (frameIndex >= frameCount())
+        return;
+
+    IntRect frameRect(m_xOffset, m_yOffset, m_width, m_height);
+
+    // Make sure the frameRect doesn't extend outside the buffer.
+    if (frameRect.maxX() > size().width())
+        frameRect.setWidth(size().width() - m_xOffset);
+    if (frameRect.maxY() > size().height())
+        frameRect.setHeight(size().height() - m_yOffset);
+
+    ImageFrame& buffer = m_frameBufferCache[frameIndex];
+    int left = upperBoundScaledX(frameRect.x());
+    int right = lowerBoundScaledX(frameRect.maxX(), left);
+    int top = upperBoundScaledY(frameRect.y());
+    int bottom = lowerBoundScaledY(frameRect.maxY(), top);
+    buffer.setOriginalFrameRect(IntRect(left, top, right - left, bottom - top));
+
+    // The starting state for this frame depends on the previous frame's
+    // disposal method.
+    //
+    // Frames that use the DisposeOverwritePrevious method are effectively
+    // no-ops in terms of changing the starting state of a frame compared to
+    // the starting state of the previous frame, so skip over them.  (If the
+    // first frame specifies this method, it will get treated like
+    // DisposeOverwriteBgcolor below and reset to a completely empty image.)
+    const ImageFrame* prevBuffer = &m_frameBufferCache[--frameIndex];
+    ImageFrame::FrameDisposalMethod prevMethod = prevBuffer->disposalMethod();
+    while (frameIndex && (prevMethod == ImageFrame::DisposeOverwritePrevious)) {
+        prevBuffer = &m_frameBufferCache[--frameIndex];
+        prevMethod = prevBuffer->disposalMethod();
+    }
+    ASSERT(prevBuffer->status() == ImageFrame::FrameComplete);
+
+    if (prevMethod == ImageFrame::DisposeKeep) {
+        // Preserve the last frame as the starting state for this frame.
+        buffer.copyBitmapData(*prevBuffer);
+    } else {
+        // We want to clear the previous frame to transparent, without
+        // affecting pixels in the image outside of the frame.
+        const IntRect& prevRect = prevBuffer->originalFrameRect();
+        if (!frameIndex || prevRect.contains(IntRect(IntPoint(), scaledSize()))) {
+            // Clearing the first frame, or a frame the size of the whole
+            // image, results in a completely empty image.
+            buffer.zeroFillPixelData();
+        } else {
+            // Copy the whole previous buffer, then clear just its frame.
+            buffer.copyBitmapData(*prevBuffer);
+            buffer.zeroFillFrameRect(prevRect);
+        }
+    }
+}
+
+void PNGImageDecoder::frameComplete()
+{
+    if (m_frameIsHidden || m_currentFrame >= frameCount())
+        return;
+
+    ImageFrame& buffer = m_frameBufferCache[m_currentFrame];
+    buffer.setStatus(ImageFrame::FrameComplete);
+
+    if (!m_delayDenominator)
+        buffer.setDuration(m_delayNumerator * 10);
+    else
+        buffer.setDuration(m_delayNumerator * 1000 / m_delayDenominator);
+
+    if (m_dispose == 2)
+        buffer.setDisposalMethod(ImageFrame::DisposeOverwritePrevious);
+    else if (m_dispose == 1)
+        buffer.setDisposalMethod(ImageFrame::DisposeOverwriteBgcolor);
+    else
+        buffer.setDisposalMethod(ImageFrame::DisposeKeep);
+
+    png_bytep interlaceBuffer = m_reader->interlaceBuffer();
+
+    if (m_currentFrame && interlaceBuffer) {
+        const IntRect& rect = buffer.originalFrameRect();
+        bool hasAlpha = m_reader->hasAlpha();
+        unsigned colorChannels = hasAlpha ? 4 : 3;
+        bool nonTrivialAlpha = false;
+        if (m_blend && !hasAlpha)
+            m_blend = 0;
+
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+        for (int y = 0; y < rect.maxY() - rect.y(); ++y) {
+            png_bytep row = interlaceBuffer + (m_scaled ? m_scaledRows[y] : y) * colorChannels * size().width();
+#if USE(QCMSLIB)
+            if (qcms_transform* transform = m_reader->colorTransform()) {
+                qcms_transform_data(transform, row, m_reader->rowBuffer(), size().width());
+                row = m_reader->rowBuffer();
+            }
+#endif
+            ImageFrame::PixelData* address = buffer.getAddr(rect.x(), y + rect.y());
+            for (int x = 0; x < rect.maxX() - rect.x(); ++x) {
+                png_bytep pixel = row + (m_scaled ? m_scaledColumns[x] : x) * colorChannels;
+                unsigned alpha = hasAlpha ? pixel[3] : 255;
+                nonTrivialAlpha |= alpha < 255;
+                if (!m_blend)
+                    buffer.setRGBA(address++, pixel[0], pixel[1], pixel[2], alpha);
+                else
+                    buffer.overRGBA(address++, pixel[0], pixel[1], pixel[2], alpha);
+            }
+        }
+#else
+        ASSERT(!m_scaled);
+        png_bytep row = interlaceBuffer;
+        for (int y = rect.y(); y < rect.maxY(); ++y, row += colorChannels * size().width()) {
+            png_bytep pixel = row;
+#if USE(QCMSLIB)
+            if (qcms_transform* transform = m_reader->colorTransform()) {
+                qcms_transform_data(transform, row, m_reader->rowBuffer(), size().width());
+                pixel = m_reader->rowBuffer();
+            }
+#endif
+            ImageFrame::PixelData* address = buffer.getAddr(rect.x(), y);
+            for (int x = rect.x(); x < rect.maxX(); ++x, pixel += colorChannels) {
+                unsigned alpha = hasAlpha ? pixel[3] : 255;
+                nonTrivialAlpha |= alpha < 255;
+                if (!m_blend)
+                    buffer.setRGBA(address++, pixel[0], pixel[1], pixel[2], alpha);
+                else
+                    buffer.overRGBA(address++, pixel[0], pixel[1], pixel[2], alpha);
+            }
+        }
+#endif
+
+        if (!nonTrivialAlpha) {
+            if (buffer.originalFrameRect().contains(IntRect(IntPoint(), scaledSize())))
+                buffer.setHasAlpha(false);
+            else {
+                size_t frameIndex = m_currentFrame;
+                const ImageFrame* prevBuffer = &m_frameBufferCache[--frameIndex];
+                while (frameIndex && (prevBuffer->disposalMethod() == ImageFrame::DisposeOverwritePrevious))
+                    prevBuffer = &m_frameBufferCache[--frameIndex];
+                if ((prevBuffer->disposalMethod() == ImageFrame::DisposeOverwriteBgcolor)
+                    && !prevBuffer->hasAlpha() && buffer.originalFrameRect().contains(prevBuffer->originalFrameRect()))
+                    buffer.setHasAlpha(false);
+            }
+        } else if (!m_blend && !buffer.hasAlpha())
+            buffer.setHasAlpha(nonTrivialAlpha);
+    }
+    m_currentFrame++;
+}
+
+int PNGImageDecoder::processingStart(png_unknown_chunkp chunk)
+{
+    static png_byte dataPNG[8] = {137, 80, 78, 71, 13, 10, 26, 10};
+    static png_byte datagAMA[16] = {0, 0, 0, 4, 103, 65, 77, 65};
+
+    if (!m_hasInfo)
+        return 0;
+
+    m_totalFrames++;
+
+    m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, decodingFailed, 0);
+    m_info = png_create_info_struct(m_png);
+    if (setjmp(JMPBUF(m_png)))
+        return 1;
+
+    png_set_crc_action(m_png, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE);
+    png_set_progressive_read_fn(m_png, static_cast<png_voidp>(this),
+        WebCore::frameHeader, WebCore::rowAvailable, 0);
+
+    memcpy(m_dataIHDR + 8, chunk->data + 4, 8);
+    png_save_uint_32(datagAMA + 8, m_gamma);
+
+    png_process_data(m_png, m_info, dataPNG, 8);
+    png_process_data(m_png, m_info, m_dataIHDR, 25);
+    png_process_data(m_png, m_info, datagAMA, 16);
+    if (m_sizePLTE > 0)
+        png_process_data(m_png, m_info, m_dataPLTE, m_sizePLTE);
+    if (m_sizetRNS > 0)
+        png_process_data(m_png, m_info, m_datatRNS, m_sizetRNS);
+
+    return 0;
+}
+
+int PNGImageDecoder::processingFinish()
+{
+    static png_byte dataIEND[12] = {0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130};
+
+    if (!m_hasInfo)
+        return 0;
+
+    if (m_totalFrames) {
+        if (setjmp(JMPBUF(m_png)))
+            return 1;
+
+        png_process_data(m_png, m_info, dataIEND, 12);
+        png_destroy_read_struct(&m_png, &m_info, 0);
+    }
+
+    frameComplete();
+    return 0;
+}
+
+void PNGImageDecoder::fallbackNotAnimated()
+{
+    m_isAnimated = false;
+    m_frameCount = 1;
+    m_playCount = 0;
+    m_currentFrame = 0;
+    m_frameBufferCache.resize(1);
+}
+#endif
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/platform/image-decoders/png/PNGImageDecoder.h (181552 => 181553)


--- trunk/Source/WebCore/platform/image-decoders/png/PNGImageDecoder.h	2015-03-16 12:30:43 UTC (rev 181552)
+++ trunk/Source/WebCore/platform/image-decoders/png/PNGImageDecoder.h	2015-03-16 14:24:46 UTC (rev 181553)
@@ -27,6 +27,9 @@
 #define PNGImageDecoder_h
 
 #include "ImageDecoder.h"
+#if ENABLE(APNG)
+#include <png.h>
+#endif
 
 namespace WebCore {
 
@@ -40,6 +43,10 @@
 
         // ImageDecoder
         virtual String filenameExtension() const { return "png"; }
+#if ENABLE(APNG)
+        virtual size_t frameCount() override { return m_frameCount; }
+        virtual int repetitionCount() const override { return m_playCount-1; }
+#endif
         virtual bool isSizeAvailable();
         virtual bool setSize(unsigned width, unsigned height);
         virtual ImageFrame* frameBufferAtIndex(size_t index);
@@ -52,10 +59,25 @@
         void headerAvailable();
         void rowAvailable(unsigned char* rowBuffer, unsigned rowIndex, int interlacePass);
         void pngComplete();
+#if ENABLE(APNG)
+        void readChunks(png_unknown_chunkp);
+        void frameHeader();
 
+        void init();
+        virtual void clearFrameBufferCache(size_t clearBeforeFrame);
+#endif
+
         bool isComplete() const
         {
-            return !m_frameBufferCache.isEmpty() && (m_frameBufferCache.first().status() == ImageFrame::FrameComplete);
+            if (m_frameBufferCache.isEmpty())
+                return false;
+
+            for (auto& imageFrame : m_frameBufferCache) {
+                if (imageFrame.status() != ImageFrame::FrameComplete)
+                    return false;
+            }
+
+            return true;
         }
 
     private:
@@ -63,9 +85,43 @@
         // calculating the image size.  If decoding fails but there is no more
         // data coming, sets the "decode failure" flag.
         void decode(bool onlySize);
+#if ENABLE(APNG)
+        void initFrameBuffer(size_t frameIndex);
+        void frameComplete();
+        int processingStart(png_unknown_chunkp);
+        int processingFinish();
+        void fallbackNotAnimated();
+#endif
 
         std::unique_ptr<PNGImageReader> m_reader;
         bool m_doNothingOnFailure;
+        unsigned m_currentFrame;
+#if ENABLE(APNG)
+        png_structp m_png;
+        png_infop m_info;
+        bool m_isAnimated;
+        bool m_frameInfo;
+        bool m_frameIsHidden;
+        bool m_hasInfo;
+        int m_gamma;
+        size_t m_frameCount;
+        unsigned m_playCount;
+        unsigned m_totalFrames;
+        unsigned m_sizePLTE;
+        unsigned m_sizetRNS;
+        unsigned m_sequenceNumber;
+        unsigned m_width;
+        unsigned m_height;
+        unsigned m_xOffset;
+        unsigned m_yOffset;
+        unsigned m_delayNumerator;
+        unsigned m_delayDenominator;
+        unsigned m_dispose;
+        unsigned m_blend;
+        png_byte m_dataIHDR[12 + 13];
+        png_byte m_dataPLTE[12 + 256 * 3];
+        png_byte m_datatRNS[12 + 256];
+#endif
     };
 
 } // namespace WebCore
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to