<!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>[284878] 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/284878">284878</a></dd>
<dt>Author</dt> <dd>simon.fraser@apple.com</dd>
<dt>Date</dt> <dd>2021-10-26 10:39:19 -0700 (Tue, 26 Oct 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>The script should decide when an image diff cases, not ImageDiff
https://bugs.webkit.org/show_bug.cgi?id=232225

Reviewed by Martin Robinson.

Rather than have ImageDiff decide if the comparison passes or fails (with some built-in
tolerance), have it just print the percentage difference, and have the script compare it
against the tolerance.

Code to prettify diff_percent is moved into the script (but should eventually
move closer to display time).

* ImageDiff/ImageDiff.cpp:
(processImages):
* Scripts/webkitpy/port/image_diff.py:
(ImageDiffer._read):
* Scripts/webkitpy/port/port_testcase.py:
(PortTestCase.test_diff_image.make_proc):
(PortTestCase.test_diff_image_passed):
(PortTestCase):
(PortTestCase.test_diff_image_passed_with_tolerance):
(PortTestCase.test_diff_image_failed_with_rounded_diff):
(PortTestCase.test_diff_image_failed):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsImageDiffImageDiffcpp">trunk/Tools/ImageDiff/ImageDiff.cpp</a></li>
<li><a href="#trunkToolsScriptswebkitpyportimage_diffpy">trunk/Tools/Scripts/webkitpy/port/image_diff.py</a></li>
<li><a href="#trunkToolsScriptswebkitpyportport_testcasepy">trunk/Tools/Scripts/webkitpy/port/port_testcase.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (284877 => 284878)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog    2021-10-26 17:28:34 UTC (rev 284877)
+++ trunk/Tools/ChangeLog       2021-10-26 17:39:19 UTC (rev 284878)
</span><span class="lines">@@ -1,3 +1,29 @@
</span><ins>+2021-10-26  Simon Fraser  <simon.fraser@apple.com>
+
+        The script should decide when an image diff cases, not ImageDiff
+        https://bugs.webkit.org/show_bug.cgi?id=232225
+
+        Reviewed by Martin Robinson.
+
+        Rather than have ImageDiff decide if the comparison passes or fails (with some built-in
+        tolerance), have it just print the percentage difference, and have the script compare it
+        against the tolerance.
+
+        Code to prettify diff_percent is moved into the script (but should eventually
+        move closer to display time).
+
+        * ImageDiff/ImageDiff.cpp:
+        (processImages):
+        * Scripts/webkitpy/port/image_diff.py:
+        (ImageDiffer._read):
+        * Scripts/webkitpy/port/port_testcase.py:
+        (PortTestCase.test_diff_image.make_proc):
+        (PortTestCase.test_diff_image_passed):
+        (PortTestCase):
+        (PortTestCase.test_diff_image_passed_with_tolerance):
+        (PortTestCase.test_diff_image_failed_with_rounded_diff):
+        (PortTestCase.test_diff_image_failed):
+
</ins><span class="cx"> 2021-10-26  Kate Cheney  <katherine_cheney@apple.com>
</span><span class="cx"> 
</span><span class="cx">         [ App Privacy Report ] Restoring a session after clearing the cache results in app initiated loads in Safari
</span></span></pre></div>
<a id="trunkToolsImageDiffImageDiffcpp"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ImageDiff/ImageDiff.cpp (284877 => 284878)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ImageDiff/ImageDiff.cpp      2021-10-26 17:28:34 UTC (rev 284877)
+++ trunk/Tools/ImageDiff/ImageDiff.cpp 2021-10-26 17:39:19 UTC (rev 284878)
</span><span class="lines">@@ -65,21 +65,10 @@
</span><span class="cx"> 
</span><span class="cx">     PlatformImage::Difference differenceData = { 100, 0, 0 };
</span><span class="cx">     auto diffImage = actualImage->difference(*baselineImage, differenceData);
</span><del>-    float legacyDifference = differenceData.percentageDifference;
-    if (legacyDifference <= tolerance)
-        legacyDifference = 0.0f;
-    else {
-        legacyDifference = roundf(legacyDifference * 100.0f) / 100.0f;
-        legacyDifference = std::max<float>(legacyDifference, 0.01f); // round to 2 decimal places
-    }
-
</del><span class="cx">     if (diffImage)
</span><span class="cx">         diffImage->writeAsPNGToStdout();
</span><span class="cx"> 
</span><del>-    if (legacyDifference > 0.0f) {
-        fprintf(stdout, "diff: %01.2f%% failed\n", legacyDifference);
-    } else
-        fprintf(stdout, "diff: %01.2f%% passed\n", legacyDifference);
</del><ins>+    fprintf(stdout, "diff: %01.8f%%\n", differenceData.percentageDifference);
</ins><span class="cx"> 
</span><span class="cx">     if (printDifference)
</span><span class="cx">         fprintf(stdout, "maxDifference=%u; totalPixels=%lu\n", differenceData.maxDifference, differenceData.totalPixels);
</span></span></pre></div>
<a id="trunkToolsScriptswebkitpyportimage_diffpy"></a>
<div class="modfile"><h4>Modified: trunk/Tools/Scripts/webkitpy/port/image_diff.py (284877 => 284878)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/Scripts/webkitpy/port/image_diff.py  2021-10-26 17:28:34 UTC (rev 284877)
+++ trunk/Tools/Scripts/webkitpy/port/image_diff.py     2021-10-26 17:39:19 UTC (rev 284878)
</span><span class="lines">@@ -129,15 +129,23 @@
</span><span class="cx">         if self._process.has_crashed():
</span><span class="cx">             err_str += "ImageDiff crashed\n"
</span><span class="cx"> 
</span><del>-        diff_percent = 0
-        if diff_output:
-            m = re.match(b'diff: (.+)% (passed|failed)', diff_output)
-            if m.group(2) == b'passed':
-                return ImageDiffResult(passed=True, diff_image=output_image, difference=0)
-            diff_percent = float(string_utils.decode(m.group(1), target_type=str))
</del><ins>+        if not diff_output:
+            return ImageDiffResult(passed=False, diff_image=None, difference=0, tolerance=self._tolerance, error_string=err_str or "Failed to read ImageDiff output")
</ins><span class="cx"> 
</span><del>-        return ImageDiffResult(passed=False, diff_image=output_image, difference=diff_percent, tolerance=self._tolerance, error_string=err_str or None)
</del><ins>+        m = re.match(b'diff: (.+)%', diff_output)
+        if not m:
+            return ImageDiffResult(passed=False, diff_image=None, difference=0, tolerance=self._tolerance, error_string=err_str or "Failed to match ImageDiff output %s" % diff_output)
</ins><span class="cx"> 
</span><ins>+        diff_percent = float(string_utils.decode(m.group(1), target_type=str))
+
+        passed = diff_percent <= self._tolerance
+        if not passed:
+            # FIXME: This prettification should happen at display time.
+            diff_percent = round(diff_percent * 100) / 100
+            diff_percent = max(diff_percent, 0.01)
+
+        return ImageDiffResult(passed=passed, diff_image=output_image, difference=diff_percent, tolerance=self._tolerance, error_string=err_str or None)
+
</ins><span class="cx">     def stop(self):
</span><span class="cx">         if self._process:
</span><span class="cx">             self._process.stop()
</span></span></pre></div>
<a id="trunkToolsScriptswebkitpyportport_testcasepy"></a>
<div class="modfile"><h4>Modified: trunk/Tools/Scripts/webkitpy/port/port_testcase.py (284877 => 284878)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/Scripts/webkitpy/port/port_testcase.py       2021-10-26 17:28:34 UTC (rev 284877)
+++ trunk/Tools/Scripts/webkitpy/port/port_testcase.py  2021-10-26 17:39:19 UTC (rev 284878)
</span><span class="lines">@@ -284,7 +284,7 @@
</span><span class="cx">         self.proc = None
</span><span class="cx"> 
</span><span class="cx">         def make_proc(port, nm, cmd, env, crash_message=None):
</span><del>-            self.proc = MockServerProcess(port, nm, cmd, env, lines=['Content-Length: 6\n', 'image1', 'diff: 90% failed\n', '#EOF\n', 'Content-Length: 6\n', 'image2', 'diff: 100% failed\n', '#EOF\n'])
</del><ins>+            self.proc = MockServerProcess(port, nm, cmd, env, lines=['Content-Length: 6\n', 'image1', 'diff: 90%\n', '#EOF\n', 'Content-Length: 6\n', 'image2', 'diff: 100%\n', '#EOF\n'])
</ins><span class="cx">             return self.proc
</span><span class="cx"> 
</span><span class="cx">         # FIXME: Can't pretend to run setup for some ports, so just skip this test.
</span><span class="lines">@@ -325,15 +325,27 @@
</span><span class="cx"> 
</span><span class="cx">     def test_diff_image_passed(self):
</span><span class="cx">         port = self.make_port()
</span><del>-        port._server_process_constructor = lambda port, nm, cmd, env, crash_message=None: MockServerProcess(lines=['diff: 0% passed\n', '#EOF\n'])
</del><ins>+        port._server_process_constructor = lambda port, nm, cmd, env, crash_message=None: MockServerProcess(lines=['diff: 0%\n', '#EOF\n'])
</ins><span class="cx">         image_differ = ImageDiffer(port)
</span><del>-        self.assertEqual(image_differ.diff_image(b'foo', b'bar', tolerance=0.1), ImageDiffResult(passed=True, diff_image=None, difference=0))
</del><ins>+        self.assertEqual(image_differ.diff_image(b'foo', b'foo', tolerance=0), ImageDiffResult(passed=True, diff_image=None, difference=0, tolerance=0))
</ins><span class="cx"> 
</span><ins>+    def test_diff_image_passed_with_tolerance(self):
+        port = self.make_port()
+        port._server_process_constructor = lambda port, nm, cmd, env, crash_message=None: MockServerProcess(lines=['Content-Length: 4\n', 'test', 'diff: 0.05%\n', '#EOF\n'])
+        image_differ = ImageDiffer(port)
+        self.assertEqual(image_differ.diff_image(b'foo', b'bar', tolerance=0.1), ImageDiffResult(passed=True, diff_image=b'test', difference=0.05, tolerance=0.1))
+
+    def test_diff_image_failed_with_rounded_diff(self):
+        port = self.make_port()
+        port._server_process_constructor = lambda port, nm, cmd, env, crash_message=None: MockServerProcess(lines=['Content-Length: 4\n', 'test', 'diff: 0.101234%\n', '#EOF\n'])
+        image_differ = ImageDiffer(port)
+        self.assertEqual(image_differ.diff_image(b'foo', b'bar', tolerance=0.1), ImageDiffResult(passed=False, diff_image=b'test', difference=0.1, tolerance=0.1))
+
</ins><span class="cx">     def test_diff_image_failed(self):
</span><span class="cx">         port = self.make_port()
</span><del>-        port._server_process_constructor = lambda port, nm, cmd, env, crash_message=None: MockServerProcess(lines=['Content-Length: 4\n', 'test', 'diff: 100% failed\n', '#EOF\n'])
</del><ins>+        port._server_process_constructor = lambda port, nm, cmd, env, crash_message=None: MockServerProcess(lines=['Content-Length: 4\n', 'test', 'diff: 10%\n', '#EOF\n'])
</ins><span class="cx">         image_differ = ImageDiffer(port)
</span><del>-        self.assertEqual(image_differ.diff_image(b'foo', b'bar', tolerance=0.1), ImageDiffResult(passed=False, diff_image=b'test', difference=100.0, tolerance=0.1))
</del><ins>+        self.assertEqual(image_differ.diff_image(b'foo', b'bar', tolerance=0.1), ImageDiffResult(passed=False, diff_image=b'test', difference=10, tolerance=0.1))
</ins><span class="cx"> 
</span><span class="cx">     def test_diff_image_crashed(self):
</span><span class="cx">         port = self.make_port()
</span></span></pre>
</div>
</div>

</body>
</html>