<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[215179] trunk/Tools</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/215179">215179</a></dd>
<dt>Author</dt> <dd>carlosgc@webkit.org</dd>
<dt>Date</dt> <dd>2017-04-10 04:48:22 -0700 (Mon, 10 Apr 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>[GTK] Remove the GDK dependency from ImageDiff
https://bugs.webkit.org/show_bug.cgi?id=85299

Reviewed by Žan Doberšek.

Add a cairo only implementation of ImageDiff. We currently have 3 different implementations of ImageDiff: the
Gdk based one used by GTK+ port, the cairo based one used by WinCairo and the CG based one. Most of the code is
actually common in all of them, but it's duplicated. So, I've taken advantage of this patch to refactor the
ImageDiff code to share the common implementation. This patch adds the cross-platform code, and the cairo
implementation and enables it in the GTK+ port. In follow up patches we can move WinCairo to this implementation
and also add CG implementation.

* ImageDiff/CMakeLists.txt:
* ImageDiff/Cairo.cmake: Added.
* ImageDiff/ImageDiff.cpp: Added.
(main):
* ImageDiff/PlatformGTK.cmake:
* ImageDiff/PlatformImage.cpp: Added.
(ImageDiff::PlatformImage::isCompatible):
(ImageDiff::PlatformImage::difference):
* ImageDiff/PlatformImage.h: Added.
* ImageDiff/cairo/PlatformImageCairo.cpp: Added.
(ImageDiff::PlatformImage::createFromStdin):
(ImageDiff::PlatformImage::createFromDiffData):
(ImageDiff::PlatformImage::PlatformImage):
(ImageDiff::PlatformImage::~PlatformImage):
(ImageDiff::PlatformImage::width):
(ImageDiff::PlatformImage::height):
(ImageDiff::PlatformImage::rowBytes):
(ImageDiff::PlatformImage::hasAlpha):
(ImageDiff::PlatformImage::pixels):
(ImageDiff::PlatformImage::writeAsPNGToStdout):
* ImageDiff/gtk/ImageDiff.cpp: Removed.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsImageDiffCMakeListstxt">trunk/Tools/ImageDiff/CMakeLists.txt</a></li>
<li><a href="#trunkToolsImageDiffPlatformGTKcmake">trunk/Tools/ImageDiff/PlatformGTK.cmake</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkToolsImageDiffCairocmake">trunk/Tools/ImageDiff/Cairo.cmake</a></li>
<li><a href="#trunkToolsImageDiffImageDiffcpp">trunk/Tools/ImageDiff/ImageDiff.cpp</a></li>
<li><a href="#trunkToolsImageDiffPlatformImagecpp">trunk/Tools/ImageDiff/PlatformImage.cpp</a></li>
<li><a href="#trunkToolsImageDiffPlatformImageh">trunk/Tools/ImageDiff/PlatformImage.h</a></li>
<li>trunk/Tools/ImageDiff/cairo/</li>
<li><a href="#trunkToolsImageDiffcairoPlatformImageCairocpp">trunk/Tools/ImageDiff/cairo/PlatformImageCairo.cpp</a></li>
</ul>

<h3>Removed Paths</h3>
<ul>
<li>trunk/Tools/ImageDiff/gtk/</li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (215178 => 215179)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog        2017-04-10 11:44:21 UTC (rev 215178)
+++ trunk/Tools/ChangeLog        2017-04-10 11:48:22 UTC (rev 215179)
</span><span class="lines">@@ -1,5 +1,41 @@
</span><span class="cx"> 2017-04-10  Carlos Garcia Campos  &lt;cgarcia@igalia.com&gt;
</span><span class="cx"> 
</span><ins>+        [GTK] Remove the GDK dependency from ImageDiff
+        https://bugs.webkit.org/show_bug.cgi?id=85299
+
+        Reviewed by Žan Doberšek.
+
+        Add a cairo only implementation of ImageDiff. We currently have 3 different implementations of ImageDiff: the
+        Gdk based one used by GTK+ port, the cairo based one used by WinCairo and the CG based one. Most of the code is
+        actually common in all of them, but it's duplicated. So, I've taken advantage of this patch to refactor the
+        ImageDiff code to share the common implementation. This patch adds the cross-platform code, and the cairo
+        implementation and enables it in the GTK+ port. In follow up patches we can move WinCairo to this implementation
+        and also add CG implementation.
+
+        * ImageDiff/CMakeLists.txt:
+        * ImageDiff/Cairo.cmake: Added.
+        * ImageDiff/ImageDiff.cpp: Added.
+        (main):
+        * ImageDiff/PlatformGTK.cmake:
+        * ImageDiff/PlatformImage.cpp: Added.
+        (ImageDiff::PlatformImage::isCompatible):
+        (ImageDiff::PlatformImage::difference):
+        * ImageDiff/PlatformImage.h: Added.
+        * ImageDiff/cairo/PlatformImageCairo.cpp: Added.
+        (ImageDiff::PlatformImage::createFromStdin):
+        (ImageDiff::PlatformImage::createFromDiffData):
+        (ImageDiff::PlatformImage::PlatformImage):
+        (ImageDiff::PlatformImage::~PlatformImage):
+        (ImageDiff::PlatformImage::width):
+        (ImageDiff::PlatformImage::height):
+        (ImageDiff::PlatformImage::rowBytes):
+        (ImageDiff::PlatformImage::hasAlpha):
+        (ImageDiff::PlatformImage::pixels):
+        (ImageDiff::PlatformImage::writeAsPNGToStdout):
+        * ImageDiff/gtk/ImageDiff.cpp: Removed.
+
+2017-04-10  Carlos Garcia Campos  &lt;cgarcia@igalia.com&gt;
+
</ins><span class="cx">         REGRESSION(r214426): [GTK] Test media/video-click-dblckick-standalone.html started to fail in the bots after r214426
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=170667
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkToolsImageDiffCMakeListstxt"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ImageDiff/CMakeLists.txt (215178 => 215179)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ImageDiff/CMakeLists.txt        2017-04-10 11:44:21 UTC (rev 215178)
+++ trunk/Tools/ImageDiff/CMakeLists.txt        2017-04-10 11:48:22 UTC (rev 215179)
</span><span class="lines">@@ -2,6 +2,15 @@
</span><span class="cx"> 
</span><span class="cx"> set(IMAGE_DIFF_SYSTEM_INCLUDE_DIRECTORIES &quot;&quot;)
</span><span class="cx"> 
</span><ins>+set(IMAGE_DIFF_SOURCES
+    ${IMAGE_DIFF_DIR}/ImageDiff.cpp
+    ${IMAGE_DIFF_DIR}/PlatformImage.cpp
+)
+
+list(APPEND IMAGE_DIFF_INCLUDE_DIRECTORIES
+    ${IMAGE_DIFF_DIR}
+)
+
</ins><span class="cx"> set(IMAGE_DIFF_LIBRARIES
</span><span class="cx">     WTF
</span><span class="cx"> )
</span></span></pre></div>
<a id="trunkToolsImageDiffCairocmake"></a>
<div class="addfile"><h4>Added: trunk/Tools/ImageDiff/Cairo.cmake (0 => 215179)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ImageDiff/Cairo.cmake                                (rev 0)
+++ trunk/Tools/ImageDiff/Cairo.cmake        2017-04-10 11:48:22 UTC (rev 215179)
</span><span class="lines">@@ -0,0 +1,11 @@
</span><ins>+list(APPEND IMAGE_DIFF_SOURCES
+    ${IMAGE_DIFF_DIR}/cairo/PlatformImageCairo.cpp
+)
+
+list(APPEND IMAGE_DIFF_SYSTEM_INCLUDE_DIRECTORIES
+    ${CAIRO_INCLUDE_DIRS}
+)
+
+list(APPEND IMAGE_DIFF_LIBRARIES
+    ${CAIRO_LIBRARIES}
+)
</ins></span></pre></div>
<a id="trunkToolsImageDiffImageDiffcpp"></a>
<div class="addfile"><h4>Added: trunk/Tools/ImageDiff/ImageDiff.cpp (0 => 215179)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ImageDiff/ImageDiff.cpp                                (rev 0)
+++ trunk/Tools/ImageDiff/ImageDiff.cpp        2017-04-10 11:48:22 UTC (rev 215179)
</span><span class="lines">@@ -0,0 +1,131 @@
</span><ins>+/*
+ * Copyright (C) 2017 Igalia S.L.
+ * Copyright (C) 2005, 2007, 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2005 Ben La Monica &lt;ben.lamonica@gmail.com&gt;.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include &quot;config.h&quot;
+#include &quot;PlatformImage.h&quot;
+#include &lt;algorithm&gt;
+#include &lt;cstdlib&gt;
+#include &lt;stdio.h&gt;
+#include &lt;string.h&gt;
+
+using namespace ImageDiff;
+
+#if OS(WINDOWS)
+#define FORMAT_SIZE_T &quot;Iu&quot;
+#else
+#define FORMAT_SIZE_T &quot;zu&quot;
+#endif
+
+int main(int argc, const char* argv[])
+{
+#if PLATFORM(WIN)
+    _setmode(0, _O_BINARY);
+    _setmode(1, _O_BINARY);
+#endif
+
+    float tolerance = 0.0f;
+
+    for (int i = 1; i &lt; argc; ++i) {
+        if (!strcmp(argv[i], &quot;-t&quot;) || !strcmp(argv[i], &quot;--tolerance&quot;)) {
+            if (i &gt;= argc - 1)
+                exit(1);
+            tolerance = strtof(argv[i + 1], 0);
+            ++i;
+            continue;
+        }
+    }
+
+    char buffer[2048];
+    std::unique_ptr&lt;PlatformImage&gt; actualImage;
+    std::unique_ptr&lt;PlatformImage&gt; baselineImage;
+
+    while (fgets(buffer, sizeof(buffer), stdin)) {
+        // Convert the first newline into a NUL character so that strtok doesn't produce it.
+        char* newLineCharacter = strchr(buffer, '\n');
+        if (newLineCharacter)
+            *newLineCharacter = '\0';
+
+        if (!strncmp(&quot;Content-Length: &quot;, buffer, 16)) {
+            strtok(buffer, &quot; &quot;);
+
+            int imageSize = strtol(strtok(0, &quot; &quot;), 0, 10);
+            if (imageSize &lt;= 0) {
+                fprintf(stderr, &quot;Error: image size must be specified.\n&quot;);
+                return EXIT_FAILURE;
+            }
+
+            if (!actualImage) {
+                if (!(actualImage = PlatformImage::createFromStdin(imageSize))) {
+                    fprintf(stderr, &quot;Error: could not read actual image.\n&quot;);
+                    return EXIT_FAILURE;
+                }
+            } else if (!baselineImage) {
+                if (!(baselineImage = PlatformImage::createFromStdin(imageSize))) {
+                    fprintf(stderr, &quot;Error: could not read baseline image.\n&quot;);
+                    return EXIT_FAILURE;
+                }
+            }
+        }
+
+        if (actualImage &amp;&amp; baselineImage) {
+            if (!actualImage-&gt;isCompatible(*baselineImage)) {
+                if (actualImage-&gt;width() != baselineImage-&gt;width() || actualImage-&gt;height() != baselineImage-&gt;height()) {
+                    fprintf(stderr, &quot;Error: test and reference images have different sizes. Test image is %&quot; FORMAT_SIZE_T &quot;x%&quot; FORMAT_SIZE_T &quot;, reference image is %&quot; FORMAT_SIZE_T &quot;x%&quot; FORMAT_SIZE_T &quot;\n&quot;,
+                        actualImage-&gt;width(), actualImage-&gt;height(), baselineImage-&gt;width(), baselineImage-&gt;height());
+                } else if (actualImage-&gt;hasAlpha() != baselineImage-&gt;hasAlpha()) {
+                    fprintf(stderr, &quot;Error: test and reference images differ in alpha. Test image %s alpha, reference image %s alpha.\n&quot;,
+                        actualImage-&gt;hasAlpha() ? &quot;has&quot; : &quot;does not have&quot;, baselineImage-&gt;hasAlpha() ? &quot;has&quot; : &quot;does not have&quot;);
+                }
+
+                return EXIT_FAILURE;
+            }
+
+            float difference = 100.0f;
+            std::unique_ptr&lt;PlatformImage&gt; diffImage = actualImage-&gt;difference(*baselineImage, difference);
+            if (difference &lt;= tolerance)
+                difference = 0.0f;
+            else {
+                difference = roundf(difference * 100.0f) / 100.0f;
+                difference = std::max&lt;float&gt;(difference, 0.01f); // round to 2 decimal places
+            }
+
+            if (difference &gt; 0.0f) {
+                if (diffImage)
+                    diffImage-&gt;writeAsPNGToStdout();
+                fprintf(stdout, &quot;diff: %01.2f%% failed\n&quot;, difference);
+            } else
+                fprintf(stdout, &quot;diff: %01.2f%% passed\n&quot;, difference);
+
+            actualImage = nullptr;
+            baselineImage = nullptr;
+        }
+
+        fflush(stdout);
+    }
+
+    return EXIT_SUCCESS;
+}
</ins></span></pre></div>
<a id="trunkToolsImageDiffPlatformGTKcmake"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ImageDiff/PlatformGTK.cmake (215178 => 215179)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ImageDiff/PlatformGTK.cmake        2017-04-10 11:44:21 UTC (rev 215178)
+++ trunk/Tools/ImageDiff/PlatformGTK.cmake        2017-04-10 11:48:22 UTC (rev 215179)
</span><span class="lines">@@ -1,11 +1,2 @@
</span><del>-set(IMAGE_DIFF_SOURCES
-    ${IMAGE_DIFF_DIR}/gtk/ImageDiff.cpp
-)
</del><ins>+include(Cairo.cmake)
</ins><span class="cx"> 
</span><del>-list(APPEND IMAGE_DIFF_SYSTEM_INCLUDE_DIRECTORIES
-    ${GTK_INCLUDE_DIRS}
-)
-
-list(APPEND IMAGE_DIFF_LIBRARIES
-    ${GTK_LIBRARIES}
-)
</del></span></pre></div>
<a id="trunkToolsImageDiffPlatformImagecpp"></a>
<div class="addfile"><h4>Added: trunk/Tools/ImageDiff/PlatformImage.cpp (0 => 215179)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ImageDiff/PlatformImage.cpp                                (rev 0)
+++ trunk/Tools/ImageDiff/PlatformImage.cpp        2017-04-10 11:48:22 UTC (rev 215179)
</span><span class="lines">@@ -0,0 +1,99 @@
</span><ins>+/*
+ * Copyright (C) 2005, 2007, 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2005 Ben La Monica &lt;ben.lamonica@gmail.com&gt;.  All rights reserved.
+ * Copyright (C) 2011 Brent Fulgham. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include &quot;config.h&quot;
+#include &quot;PlatformImage.h&quot;
+
+#include &lt;algorithm&gt;
+
+namespace ImageDiff {
+
+bool PlatformImage::isCompatible(const PlatformImage&amp; other) const
+{
+    return width() == other.width()
+        &amp;&amp; height() == other.height()
+        &amp;&amp; rowBytes() == other.rowBytes()
+        &amp;&amp; hasAlpha() == other.hasAlpha();
+}
+
+std::unique_ptr&lt;PlatformImage&gt; PlatformImage::difference(const PlatformImage&amp; other, float&amp; percentageDifference)
+{
+    size_t width = this-&gt;width();
+    size_t height = this-&gt;height();
+
+    // Compare the content of the 2 bitmaps
+    void* diffBuffer = malloc(width * height);
+    float count = 0.0f;
+    float sum = 0.0f;
+    float maxDistance = 0.0f;
+    unsigned char* basePixel = this-&gt;pixels();
+    unsigned char* pixel = other.pixels();
+    unsigned char* diffPixel = reinterpret_cast&lt;unsigned char*&gt;(diffBuffer);
+    for (size_t y = 0; y &lt; height; ++y) {
+        for (size_t x = 0; x &lt; width; ++x) {
+            float red = (pixel[0] - basePixel[0]) / std::max&lt;float&gt;(255 - basePixel[0], basePixel[0]);
+            float green = (pixel[1] - basePixel[1]) / std::max&lt;float&gt;(255 - basePixel[1], basePixel[1]);
+            float blue = (pixel[2] - basePixel[2]) / std::max&lt;float&gt;(255 - basePixel[2], basePixel[2]);
+            float alpha = (pixel[3] - basePixel[3]) / std::max&lt;float&gt;(255 - basePixel[3], basePixel[3]);
+            float distance = sqrtf(red * red + green * green + blue * blue + alpha * alpha) / 2.0f;
+
+            *diffPixel++ = static_cast&lt;unsigned char&gt;(distance * 255.0f);
+
+            if (distance &gt;= 1.0f / 255.0f) {
+                count += 1.0f;
+                sum += distance;
+                if (distance &gt; maxDistance)
+                    maxDistance = distance;
+            }
+
+            basePixel += 4;
+            pixel += 4;
+        }
+    }
+
+    // Compute the difference as a percentage combining both the number of different pixels and their difference amount i.e. the average distance over the entire image
+    if (count &gt; 0.0f)
+        percentageDifference = 100.0f * sum / (height * width);
+    else
+        percentageDifference = 0.0f;
+
+    if (!percentageDifference) {
+        free(diffBuffer);
+        return nullptr;
+    }
+
+    // Generate a normalized diff image if there is any difference
+    if (maxDistance &lt; 1.0f) {
+        diffPixel = reinterpret_cast&lt;unsigned char*&gt;(diffBuffer);
+        for (size_t p = 0; p &lt; height * width; ++p)
+            diffPixel[p] /= maxDistance;
+    }
+
+    return PlatformImage::createFromDiffData(diffBuffer, width, height);
+}
+
+} // namespace ImageDiff
</ins></span></pre></div>
<a id="trunkToolsImageDiffPlatformImageh"></a>
<div class="addfile"><h4>Added: trunk/Tools/ImageDiff/PlatformImage.h (0 => 215179)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ImageDiff/PlatformImage.h                                (rev 0)
+++ trunk/Tools/ImageDiff/PlatformImage.h        2017-04-10 11:48:22 UTC (rev 215179)
</span><span class="lines">@@ -0,0 +1,61 @@
</span><ins>+/*
+ * Copyright (C) 2017 Igalia S.L.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include &lt;cmath&gt;
+#include &lt;memory&gt;
+#include &lt;wtf/Platform.h&gt;
+
+#if USE(CAIRO)
+typedef struct _cairo_surface cairo_surface_t;
+#endif
+
+namespace ImageDiff {
+
+class PlatformImage {
+public:
+    static std::unique_ptr&lt;PlatformImage&gt; createFromStdin(size_t);
+    static std::unique_ptr&lt;PlatformImage&gt; createFromDiffData(void*, size_t width, size_t height);
+
+#if USE(CAIRO)
+    PlatformImage(cairo_surface_t*);
+#endif
+    ~PlatformImage();
+
+    size_t width() const;
+    size_t height() const;
+    size_t rowBytes() const;
+    bool hasAlpha() const;
+    unsigned char* pixels() const;
+    bool isCompatible(const PlatformImage&amp;) const;
+    std::unique_ptr&lt;PlatformImage&gt; difference(const PlatformImage&amp;, float&amp; percentageDifference);
+    void writeAsPNGToStdout();
+
+private:
+#if USE(CAIRO)
+    cairo_surface_t* m_image;
+#endif
+};
+
+} // namespace ImageDiff
</ins></span></pre></div>
<a id="trunkToolsImageDiffcairoPlatformImageCairocpp"></a>
<div class="addfile"><h4>Added: trunk/Tools/ImageDiff/cairo/PlatformImageCairo.cpp (0 => 215179)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ImageDiff/cairo/PlatformImageCairo.cpp                                (rev 0)
+++ trunk/Tools/ImageDiff/cairo/PlatformImageCairo.cpp        2017-04-10 11:48:22 UTC (rev 215179)
</span><span class="lines">@@ -0,0 +1,133 @@
</span><ins>+/*
+ * Copyright (C) 2017 Igalia S.L.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include &quot;config.h&quot;
+#include &quot;PlatformImage.h&quot;
+
+#include &lt;cairo.h&gt;
+#include &lt;stdio.h&gt;
+
+#if OS(WINDOWS)
+#define FORMAT_SIZE_T &quot;Iu&quot;
+#else
+#define FORMAT_SIZE_T &quot;zu&quot;
+#endif
+
+namespace ImageDiff {
+
+std::unique_ptr&lt;PlatformImage&gt; PlatformImage::createFromStdin(size_t imageSize)
+{
+    struct ReadContext {
+        char buffer[2048];
+        unsigned long incomingBytes;
+        unsigned long readBytes;
+    } context { { }, imageSize, 0 };
+
+    cairo_surface_t* surface = cairo_image_surface_create_from_png_stream(
+        [](void* closure, unsigned char* data, unsigned length) -&gt; cairo_status_t {
+            auto&amp; context = *static_cast&lt;ReadContext*&gt;(closure);
+            context.readBytes += length;
+
+            fread(data, 1, length, stdin);
+            return CAIRO_STATUS_SUCCESS;
+        }, &amp;context);
+
+    if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
+        cairo_surface_destroy(surface);
+        return nullptr;
+    }
+
+    return std::make_unique&lt;PlatformImage&gt;(surface);
+}
+
+std::unique_ptr&lt;PlatformImage&gt; PlatformImage::createFromDiffData(void* data, size_t width, size_t height)
+{
+    cairo_surface_t* surface = cairo_image_surface_create_for_data(reinterpret_cast&lt;unsigned char*&gt;(data), CAIRO_FORMAT_A8,
+        width, height, cairo_format_stride_for_width(CAIRO_FORMAT_A8, width));
+    static cairo_user_data_key_t imageDataKey;
+    cairo_surface_set_user_data(surface, &amp;imageDataKey, data, [](void* data) { free(data); });
+    return std::make_unique&lt;PlatformImage&gt;(surface);
+}
+
+PlatformImage::PlatformImage(cairo_surface_t* surface)
+    : m_image(surface)
+{
+}
+
+PlatformImage::~PlatformImage()
+{
+    cairo_surface_destroy(m_image);
+}
+
+size_t PlatformImage::width() const
+{
+    return cairo_image_surface_get_width(m_image);
+}
+
+size_t PlatformImage::height() const
+{
+    return cairo_image_surface_get_height(m_image);
+}
+
+size_t PlatformImage::rowBytes() const
+{
+    return cairo_image_surface_get_stride(m_image);
+}
+
+bool PlatformImage::hasAlpha() const
+{
+    // What matters here is whether the image data has an alpha channel. In cairo, both
+    // CAIRO_FORMAT_ARGB32 and CAIRO_FORMAT_RGB24 have an alpha channel even if it's
+    // always 0 in the CAIRO_FORMAT_RGB24 case.
+    return cairo_image_surface_get_format(m_image) == CAIRO_FORMAT_ARGB32 || cairo_image_surface_get_format(m_image) == CAIRO_FORMAT_RGB24;
+}
+
+unsigned char* PlatformImage::pixels() const
+{
+    return cairo_image_surface_get_data(m_image);
+}
+
+void PlatformImage::writeAsPNGToStdout()
+{
+    struct WriteContext {
+        unsigned long writtenBytes { 0 };
+    } context;
+
+    // First we sum up the bytes that are to be written.
+    cairo_surface_write_to_png_stream(m_image,
+        [](void* closure, const unsigned char*, unsigned length) -&gt; cairo_status_t {
+            auto&amp; context = *static_cast&lt;WriteContext*&gt;(closure);
+            context.writtenBytes += length;
+            return CAIRO_STATUS_SUCCESS;
+        }, &amp;context);
+    fprintf(stdout, &quot;Content-Length: %&quot; FORMAT_SIZE_T &quot;\n&quot;, context.writtenBytes);
+    cairo_surface_write_to_png_stream(m_image,
+        [](void*, const unsigned char* data, unsigned length) -&gt; cairo_status_t {
+            fwrite(data, 1, length, stdout);
+            return CAIRO_STATUS_SUCCESS;
+        }, nullptr);
+}
+
+} // namespace ImageDiff
</ins></span></pre>
</div>
</div>

</body>
</html>