<!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>[282634] trunk</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/282634">282634</a></dd>
<dt>Author</dt> <dd>heycam@apple.com</dd>
<dt>Date</dt> <dd>2021-09-16 22:32:39 -0700 (Thu, 16 Sep 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>Preserve canvas color space when producing JPEGs from toDataURL/toBlob
https://bugs.webkit.org/show_bug.cgi?id=230209
<rdar://82948457>

Reviewed by Sam Weinig.

LayoutTests/imported/w3c:

* web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas-expected.txt: Added.
* web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html: Copied from LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html.
* web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas-expected.txt:
* web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html:
* web-platform-tests/html/canvas/tools/yaml/element/color_space.yaml:

Source/WebCore:

Test: imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html

* platform/graphics/cg/ImageBufferCGBackend.cpp:
(WebCore::colorSpaceForBitmap): Return an appropriate color space for
performing any graphics operations on ImageBuffers (such as scaling or
cropping, or compositing on top of solid colors) becore encoding.
(WebCore::createBitmapImageAfterScalingIfNeeded):
(WebCore::ImageBufferCGBackend::copyCGImageForEncoding const):

LayoutTests:

* TestExpectations:
* platform/ios-14/TestExpectations:
* platform/ios/TestExpectations:
* platform/mac/TestExpectations:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsTestExpectations">trunk/LayoutTests/TestExpectations</a></li>
<li><a href="#trunkLayoutTestsimportedw3cChangeLog">trunk/LayoutTests/imported/w3c/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsimportedw3cwebplatformtestshtmlcanvaselementwidegamutcanvas2dcolorspacep3toDataURLp3canvasexpectedtxt">trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas-expected.txt</a></li>
<li><a href="#trunkLayoutTestsimportedw3cwebplatformtestshtmlcanvaselementwidegamutcanvas2dcolorspacep3toDataURLp3canvashtml">trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html</a></li>
<li><a href="#trunkLayoutTestsimportedw3cwebplatformtestshtmlcanvastoolsyamlelementcolor_spaceyaml">trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/tools/yaml/element/color_space.yaml</a></li>
<li><a href="#trunkLayoutTestsplatformiosTestExpectations">trunk/LayoutTests/platform/ios/TestExpectations</a></li>
<li><a href="#trunkLayoutTestsplatformios14TestExpectations">trunk/LayoutTests/platform/ios-14/TestExpectations</a></li>
<li><a href="#trunkLayoutTestsplatformmacTestExpectations">trunk/LayoutTests/platform/mac/TestExpectations</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicscgImageBufferCGBackendcpp">trunk/Source/WebCore/platform/graphics/cg/ImageBufferCGBackend.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsimportedw3cwebplatformtestshtmlcanvaselementwidegamutcanvas2dcolorspacep3toDataURLjpegp3canvasexpectedtxt">trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas-expected.txt</a></li>
<li><a href="#trunkLayoutTestsimportedw3cwebplatformtestshtmlcanvaselementwidegamutcanvas2dcolorspacep3toDataURLjpegp3canvashtml">trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (282633 => 282634)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog      2021-09-17 05:15:31 UTC (rev 282633)
+++ trunk/LayoutTests/ChangeLog 2021-09-17 05:32:39 UTC (rev 282634)
</span><span class="lines">@@ -1,3 +1,16 @@
</span><ins>+2021-09-16  Cameron McCormack  <heycam@apple.com>
+
+        Preserve canvas color space when producing JPEGs from toDataURL/toBlob
+        https://bugs.webkit.org/show_bug.cgi?id=230209
+        <rdar://82948457>
+
+        Reviewed by Sam Weinig.
+
+        * TestExpectations:
+        * platform/ios-14/TestExpectations:
+        * platform/ios/TestExpectations:
+        * platform/mac/TestExpectations:
+
</ins><span class="cx"> 2021-09-16  Alex Christensen  <achristensen@webkit.org>
</span><span class="cx"> 
</span><span class="cx">         Regression (r282484): [ Catalina ] 2 imported/w3c/web-platform-tests/content-security-policy/ tests are failing
</span></span></pre></div>
<a id="trunkLayoutTestsTestExpectations"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/TestExpectations (282633 => 282634)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/TestExpectations       2021-09-17 05:15:31 UTC (rev 282633)
+++ trunk/LayoutTests/TestExpectations  2021-09-17 05:32:39 UTC (rev 282634)
</span><span class="lines">@@ -5267,6 +5267,7 @@
</span><span class="cx"> imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.p3.canvas.html [ Failure ]
</span><span class="cx"> imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.with.putImageData.html [ Failure ]
</span><span class="cx"> imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html [ Failure ]
</span><ins>+imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html [ Failure ]
</ins><span class="cx"> imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.with.putImageData.html [ Failure ]
</span><span class="cx"> storage/indexeddb/structured-clone-image-data-display-p3.html [ Failure ]
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestsimportedw3cChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/imported/w3c/ChangeLog (282633 => 282634)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/imported/w3c/ChangeLog 2021-09-17 05:15:31 UTC (rev 282633)
+++ trunk/LayoutTests/imported/w3c/ChangeLog    2021-09-17 05:32:39 UTC (rev 282634)
</span><span class="lines">@@ -1,5 +1,19 @@
</span><span class="cx"> 2021-09-16  Cameron McCormack  <heycam@apple.com>
</span><span class="cx"> 
</span><ins>+        Preserve canvas color space when producing JPEGs from toDataURL/toBlob
+        https://bugs.webkit.org/show_bug.cgi?id=230209
+        <rdar://82948457>
+
+        Reviewed by Sam Weinig.
+
+        * web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas-expected.txt: Added.
+        * web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html: Copied from LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html.
+        * web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas-expected.txt:
+        * web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html:
+        * web-platform-tests/html/canvas/tools/yaml/element/color_space.yaml:
+
+2021-09-16  Cameron McCormack  <heycam@apple.com>
+
</ins><span class="cx">         Support patterns with a wide gamut source painting into a display-p3 canvas
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=229023
</span><span class="cx">         <rdar://problem/81828466>
</span></span></pre></div>
<a id="trunkLayoutTestsimportedw3cwebplatformtestshtmlcanvaselementwidegamutcanvas2dcolorspacep3toDataURLjpegp3canvasexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas-expected.txt (0 => 282634)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas-expected.txt                          (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas-expected.txt     2021-09-17 05:32:39 UTC (rev 282634)
</span><span class="lines">@@ -0,0 +1,6 @@
</span><ins>+2d.color.space.p3.toDataURL.jpeg.p3.canvas
+test if toDataURL('image/jpeg') returns p3 data from canvas with p3 color space
+Actual output:
+
+PASS test if toDataURL('image/jpeg') returns p3 data from canvas with p3 color space
+
</ins></span></pre></div>
<a id="trunkLayoutTestsimportedw3cwebplatformtestshtmlcanvaselementwidegamutcanvas2dcolorspacep3toDataURLjpegp3canvashtmlfromrev282633trunkLayoutTestsimportedw3cwebplatformtestshtmlcanvaselementwidegamutcanvas2dcolorspacep3toDataURLp3canvashtml"></a>
<div class="copfile"><h4>Copied: trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html (from rev 282633, trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html) (0 => 282634)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html                          (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html     2021-09-17 05:32:39 UTC (rev 282634)
</span><span class="lines">@@ -0,0 +1,47 @@
</span><ins>+<!DOCTYPE html>
+<!-- DO NOT EDIT! This test has been generated by /html/canvas/tools/gentest.py. -->
+<title>Canvas test: 2d.color.space.p3.toDataURL.jpeg.p3.canvas</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="/html/canvas/resources/canvas-tests.js"></script>
+<link rel="stylesheet" href="/html/canvas/resources/canvas-tests.css">
+<body class="show_output">
+
+<h1>2d.color.space.p3.toDataURL.jpeg.p3.canvas</h1>
+<p class="desc">test if toDataURL('image/jpeg') returns p3 data from canvas with p3 color space</p>
+
+
+<p class="output">Actual output:</p>
+<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
+
+<ul id="d"></ul>
+<script>
+var t = async_test("test if toDataURL('image/jpeg') returns p3 data from canvas with p3 color space");
+_addTest(function(canvas, ctx) {
+
+ctx.fillStyle = "rgba(155, 27, 27, 1)";
+ctx.fillRect(0, 0, 1, 1);
+ctx.fillStyle = "rgba(27, 155, 27, 0)";
+ctx.fillRect(1, 0, 1, 1);
+ctx.fillStyle = "rgba(27, 27, 155, 0.5)";
+ctx.fillRect(0, 1, 1, 1);
+ctx.fillStyle = "rgba(27, 27, 27, 0.5)";
+ctx.fillRect(1, 1, 1, 1);
+expectedPixels = ctx.getImageData(0, 0, 2, 2, {colorSpace: "display-p3"}).data;
+
+var image = new Image();
+image.onload = t.step_func_done(function() {
+    var dstCanvas = document.createElement("canvas");
+    dstCanvas.width = 2;
+    dstCanvas.height = 2;
+    var ctx = dstCanvas.getContext('2d', {colorSpace: "display-p3"});
+    ctx.drawImage(image, 0, 0);
+    var actualPixels = ctx.getImageData(0, 0, 2, 2, {colorSpace: "display-p3"}).data;
+    assert_array_approx_equals(actualPixels, expectedPixels, 2);
+});
+image.src = canvas.toDataURL("image/jpeg");
+
+
+}, {colorSpace: "display-p3"});
+</script>
+
</ins></span></pre></div>
<a id="trunkLayoutTestsimportedw3cwebplatformtestshtmlcanvaselementwidegamutcanvas2dcolorspacep3toDataURLp3canvasexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas-expected.txt (282633 => 282634)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas-expected.txt       2021-09-17 05:15:31 UTC (rev 282633)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas-expected.txt  2021-09-17 05:32:39 UTC (rev 282634)
</span><span class="lines">@@ -1,6 +1,6 @@
</span><span class="cx"> 2d.color.space.p3.toDataURL.p3.canvas
</span><del>-test if toblob returns p3 data from canvas with p3 color space
</del><ins>+test if toDataURL returns p3 data from canvas with p3 color space
</ins><span class="cx"> Actual output:
</span><span class="cx"> 
</span><del>-PASS test if toblob returns p3 data from canvas with p3 color space
</del><ins>+PASS test if toDataURL returns p3 data from canvas with p3 color space
</ins><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestsimportedw3cwebplatformtestshtmlcanvaselementwidegamutcanvas2dcolorspacep3toDataURLp3canvashtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html (282633 => 282634)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html       2021-09-17 05:15:31 UTC (rev 282633)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html  2021-09-17 05:32:39 UTC (rev 282634)
</span><span class="lines">@@ -8,7 +8,7 @@
</span><span class="cx"> <body class="show_output">
</span><span class="cx"> 
</span><span class="cx"> <h1>2d.color.space.p3.toDataURL.p3.canvas</h1>
</span><del>-<p class="desc">test if toblob returns p3 data from canvas with p3 color space</p>
</del><ins>+<p class="desc">test if toDataURL returns p3 data from canvas with p3 color space</p>
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> <p class="output">Actual output:</p>
</span><span class="lines">@@ -16,7 +16,7 @@
</span><span class="cx"> 
</span><span class="cx"> <ul id="d"></ul>
</span><span class="cx"> <script>
</span><del>-var t = async_test("test if toblob returns p3 data from canvas with p3 color space");
</del><ins>+var t = async_test("test if toDataURL returns p3 data from canvas with p3 color space");
</ins><span class="cx"> _addTest(function(canvas, ctx) {
</span><span class="cx"> 
</span><span class="cx"> ctx.fillStyle = "rgba(155, 27, 27, 1)";
</span></span></pre></div>
<a id="trunkLayoutTestsimportedw3cwebplatformtestshtmlcanvastoolsyamlelementcolor_spaceyaml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/tools/yaml/element/color_space.yaml (282633 => 282634)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/tools/yaml/element/color_space.yaml        2021-09-17 05:15:31 UTC (rev 282633)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/tools/yaml/element/color_space.yaml   2021-09-17 05:32:39 UTC (rev 282634)
</span><span class="lines">@@ -75,7 +75,7 @@
</span><span class="cx"> - name: 2d.color.space.p3.toDataURL.p3.canvas
</span><span class="cx">   testing:
</span><span class="cx">   - 2d.color.space.p3.and.uint8
</span><del>-  desc: test if toblob returns p3 data from canvas with p3 color space
</del><ins>+  desc: test if toDataURL returns p3 data from canvas with p3 color space
</ins><span class="cx">   attributes: |
</span><span class="cx">     {colorSpace: "display-p3"}
</span><span class="cx">   code: |
</span><span class="lines">@@ -101,6 +101,35 @@
</span><span class="cx">     });
</span><span class="cx">     image.src = canvas.toDataURL();
</span><span class="cx"> 
</span><ins>+- name: 2d.color.space.p3.toDataURL.jpeg.p3.canvas
+  testing:
+  - 2d.color.space.p3.and.uint8
+  desc: test if toDataURL('image/jpeg') returns p3 data from canvas with p3 color space
+  attributes: |
+    {colorSpace: "display-p3"}
+  code: |
+    ctx.fillStyle = "rgba(155, 27, 27, 1)";
+    ctx.fillRect(0, 0, 1, 1);
+    ctx.fillStyle = "rgba(27, 155, 27, 0)";
+    ctx.fillRect(1, 0, 1, 1);
+    ctx.fillStyle = "rgba(27, 27, 155, 0.5)";
+    ctx.fillRect(0, 1, 1, 1);
+    ctx.fillStyle = "rgba(27, 27, 27, 0.5)";
+    ctx.fillRect(1, 1, 1, 1);
+    expectedPixels = ctx.getImageData(0, 0, 2, 2, {colorSpace: "display-p3"}).data;
+
+    var image = new Image();
+    image.onload = t.step_func_done(function() {
+        var dstCanvas = document.createElement("canvas");
+        dstCanvas.width = 2;
+        dstCanvas.height = 2;
+        var ctx = dstCanvas.getContext('2d', {colorSpace: "display-p3"});
+        ctx.drawImage(image, 0, 0);
+        var actualPixels = ctx.getImageData(0, 0, 2, 2, {colorSpace: "display-p3"}).data;
+        assert_array_approx_equals(actualPixels, expectedPixels, 2);
+    });
+    image.src = canvas.toDataURL("image/jpeg");
+
</ins><span class="cx"> - name: 2d.color.space.p3.toBlob.with.putImageData
</span><span class="cx">   testing:
</span><span class="cx">   - 2d.color.space.p3.and.uint8
</span></span></pre></div>
<a id="trunkLayoutTestsplatformiosTestExpectations"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/platform/ios/TestExpectations (282633 => 282634)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/platform/ios/TestExpectations  2021-09-17 05:15:31 UTC (rev 282633)
+++ trunk/LayoutTests/platform/ios/TestExpectations     2021-09-17 05:32:39 UTC (rev 282634)
</span><span class="lines">@@ -3500,6 +3500,7 @@
</span><span class="cx"> imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.p3.canvas.html [ Pass ]
</span><span class="cx"> imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.with.putImageData.html [ Pass ]
</span><span class="cx"> imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html [ Pass ]
</span><ins>+imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html [ Pass ]
</ins><span class="cx"> imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.with.putImageData.html [ Pass ]
</span><span class="cx"> storage/indexeddb/structured-clone-image-data-display-p3.html [ Pass ]
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestsplatformios14TestExpectations"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/platform/ios-14/TestExpectations (282633 => 282634)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/platform/ios-14/TestExpectations       2021-09-17 05:15:31 UTC (rev 282633)
+++ trunk/LayoutTests/platform/ios-14/TestExpectations  2021-09-17 05:32:39 UTC (rev 282634)
</span><span class="lines">@@ -84,6 +84,7 @@
</span><span class="cx"> imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.p3.canvas.html [ Failure ]
</span><span class="cx"> imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.with.putImageData.html [ Failure ]
</span><span class="cx"> imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html [ Failure ]
</span><ins>+imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html [ Failure ]
</ins><span class="cx"> imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.with.putImageData.html [ Failure ]
</span><span class="cx"> storage/indexeddb/structured-clone-image-data-display-p3.html [ Failure ]
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestsplatformmacTestExpectations"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/platform/mac/TestExpectations (282633 => 282634)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/platform/mac/TestExpectations  2021-09-17 05:15:31 UTC (rev 282633)
+++ trunk/LayoutTests/platform/mac/TestExpectations     2021-09-17 05:32:39 UTC (rev 282634)
</span><span class="lines">@@ -2359,6 +2359,7 @@
</span><span class="cx"> [ Monterey+ ] imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.p3.canvas.html [ Pass ]
</span><span class="cx"> [ Monterey+ ] imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.with.putImageData.html [ Pass ]
</span><span class="cx"> [ Monterey+ ] imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html [ Pass ]
</span><ins>+[ Monterey+ ] imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html [ Pass ]
</ins><span class="cx"> [ Monterey+ ] imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.with.putImageData.html [ Pass ]
</span><span class="cx"> [ Monterey+ ] storage/indexeddb/structured-clone-image-data-display-p3.html [ Pass ]
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (282633 => 282634)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2021-09-17 05:15:31 UTC (rev 282633)
+++ trunk/Source/WebCore/ChangeLog      2021-09-17 05:32:39 UTC (rev 282634)
</span><span class="lines">@@ -1,3 +1,20 @@
</span><ins>+2021-09-16  Cameron McCormack  <heycam@apple.com>
+
+        Preserve canvas color space when producing JPEGs from toDataURL/toBlob
+        https://bugs.webkit.org/show_bug.cgi?id=230209
+        <rdar://82948457>
+
+        Reviewed by Sam Weinig.
+
+        Test: imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html
+
+        * platform/graphics/cg/ImageBufferCGBackend.cpp:
+        (WebCore::colorSpaceForBitmap): Return an appropriate color space for
+        performing any graphics operations on ImageBuffers (such as scaling or
+        cropping, or compositing on top of solid colors) becore encoding.
+        (WebCore::createBitmapImageAfterScalingIfNeeded):
+        (WebCore::ImageBufferCGBackend::copyCGImageForEncoding const):
+
</ins><span class="cx"> 2021-09-16  Diego Pino Garcia  <dpino@igalia.com>
</span><span class="cx"> 
</span><span class="cx">         Unreviewed build fix for Ubuntu LTS/Debian after r282597
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicscgImageBufferCGBackendcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/cg/ImageBufferCGBackend.cpp (282633 => 282634)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/cg/ImageBufferCGBackend.cpp       2021-09-17 05:15:31 UTC (rev 282633)
+++ trunk/Source/WebCore/platform/graphics/cg/ImageBufferCGBackend.cpp  2021-09-17 05:32:39 UTC (rev 282634)
</span><span class="lines">@@ -93,6 +93,13 @@
</span><span class="cx">     return image;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static CGColorSpaceRef colorSpaceForBitmap(DestinationColorSpace imageBufferColorSpace)
+{
+    if (CGColorSpaceGetModel(imageBufferColorSpace.platformColorSpace()) != kCGColorSpaceModelRGB)
+        return sRGBColorSpaceRef();
+    return imageBufferColorSpace.platformColorSpace();
+}
+
</ins><span class="cx"> static RefPtr<Image> createBitmapImageAfterScalingIfNeeded(RefPtr<NativeImage>&& image, const IntSize& logicalSize, const IntSize& backendSize, float resolutionScale, PreserveResolution preserveResolution)
</span><span class="cx"> {
</span><span class="cx">     if (!image)
</span><span class="lines">@@ -101,7 +108,7 @@
</span><span class="cx">     if (resolutionScale == 1 || preserveResolution == PreserveResolution::Yes)
</span><span class="cx">         image = NativeImage::create(createCroppedImageIfNecessary(image->platformImage().get(), backendSize));
</span><span class="cx">     else {
</span><del>-        auto context = adoptCF(CGBitmapContextCreate(0, logicalSize.width(), logicalSize.height(), 8, 4 * logicalSize.width(), sRGBColorSpaceRef(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
</del><ins>+        auto context = adoptCF(CGBitmapContextCreate(0, logicalSize.width(), logicalSize.height(), 8, 4 * logicalSize.width(), colorSpaceForBitmap(image->colorSpace()), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
</ins><span class="cx">         CGContextSetBlendMode(context.get(), kCGBlendModeCopy);
</span><span class="cx">         CGContextClipToRect(context.get(), FloatRect(FloatPoint::zero(), logicalSize));
</span><span class="cx">         FloatSize imageSizeInUserSpace = logicalSize;
</span><span class="lines">@@ -177,7 +184,7 @@
</span><span class="cx">         // FIXME: Should this be using the same logic as ImageBufferUtilitiesCG?
</span><span class="cx"> 
</span><span class="cx">         // JPEGs don't have an alpha channel, so we have to manually composite on top of black.
</span><del>-        PixelBufferFormat format { AlphaPremultiplication::Premultiplied, PixelFormat::RGBA8, DestinationColorSpace::SRGB() };
</del><ins>+        PixelBufferFormat format { AlphaPremultiplication::Premultiplied, PixelFormat::RGBA8, DestinationColorSpace(colorSpaceForBitmap(colorSpace())) };
</ins><span class="cx">         auto pixelBuffer = getPixelBuffer(format, logicalRect());
</span><span class="cx">         if (!pixelBuffer)
</span><span class="cx">             return nullptr;
</span><span class="lines">@@ -209,7 +216,7 @@
</span><span class="cx">     if (!nativeImage)
</span><span class="cx">         return nullptr;
</span><span class="cx">     auto image = nativeImage->platformImage();
</span><del>-    auto context = adoptCF(CGBitmapContextCreate(0, backendSize().width(), backendSize().height(), 8, 4 * backendSize().width(), sRGBColorSpaceRef(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
</del><ins>+    auto context = adoptCF(CGBitmapContextCreate(0, backendSize().width(), backendSize().height(), 8, 4 * backendSize().width(), colorSpaceForBitmap(colorSpace()), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
</ins><span class="cx">     CGContextSetBlendMode(context.get(), kCGBlendModeCopy);
</span><span class="cx">     CGContextClipToRect(context.get(), CGRectMake(0, 0, backendSize().width(), backendSize().height()));
</span><span class="cx">     CGContextDrawImage(context.get(), CGRectMake(0, 0, backendSize().width(), backendSize().height()), image.get());
</span></span></pre>
</div>
</div>

</body>
</html>