Title: [282634] trunk
Revision
282634
Author
[email protected]
Date
2021-09-16 22:32:39 -0700 (Thu, 16 Sep 2021)

Log Message

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:

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (282633 => 282634)


--- trunk/LayoutTests/ChangeLog	2021-09-17 05:15:31 UTC (rev 282633)
+++ trunk/LayoutTests/ChangeLog	2021-09-17 05:32:39 UTC (rev 282634)
@@ -1,3 +1,16 @@
+2021-09-16  Cameron McCormack  <[email protected]>
+
+        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:
+
 2021-09-16  Alex Christensen  <[email protected]>
 
         Regression (r282484): [ Catalina ] 2 imported/w3c/web-platform-tests/content-security-policy/ tests are failing

Modified: trunk/LayoutTests/TestExpectations (282633 => 282634)


--- trunk/LayoutTests/TestExpectations	2021-09-17 05:15:31 UTC (rev 282633)
+++ trunk/LayoutTests/TestExpectations	2021-09-17 05:32:39 UTC (rev 282634)
@@ -5267,6 +5267,7 @@
 imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.p3.canvas.html [ Failure ]
 imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.with.putImageData.html [ Failure ]
 imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html [ Failure ]
+imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html [ Failure ]
 imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.with.putImageData.html [ Failure ]
 storage/indexeddb/structured-clone-image-data-display-p3.html [ Failure ]
 

Modified: trunk/LayoutTests/imported/w3c/ChangeLog (282633 => 282634)


--- 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)
@@ -1,5 +1,19 @@
 2021-09-16  Cameron McCormack  <[email protected]>
 
+        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  <[email protected]>
+
         Support patterns with a wide gamut source painting into a display-p3 canvas
         https://bugs.webkit.org/show_bug.cgi?id=229023
         <rdar://problem/81828466>

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)


--- 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)
@@ -0,0 +1,6 @@
+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
+

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)


--- 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)
@@ -0,0 +1,47 @@
+<!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=""
+<script src=""
+<script src=""
+<link rel="stylesheet" href=""
+<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 = ""
+
+
+}, {colorSpace: "display-p3"});
+</script>
+

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)


--- 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)
@@ -1,6 +1,6 @@
 2d.color.space.p3.toDataURL.p3.canvas
-test if toblob returns p3 data from canvas with p3 color space
+test if toDataURL returns p3 data from canvas with p3 color space
 Actual output:
 
-PASS test if toblob returns p3 data from canvas with p3 color space
+PASS test if toDataURL returns p3 data from canvas with p3 color space
 

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html (282633 => 282634)


--- 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)
@@ -8,7 +8,7 @@
 <body class="show_output">
 
 <h1>2d.color.space.p3.toDataURL.p3.canvas</h1>
-<p class="desc">test if toblob returns p3 data from canvas with p3 color space</p>
+<p class="desc">test if toDataURL returns p3 data from canvas with p3 color space</p>
 
 
 <p class="output">Actual output:</p>
@@ -16,7 +16,7 @@
 
 <ul id="d"></ul>
 <script>
-var t = async_test("test if toblob returns p3 data from canvas with p3 color space");
+var t = async_test("test if toDataURL returns p3 data from canvas with p3 color space");
 _addTest(function(canvas, ctx) {
 
 ctx.fillStyle = "rgba(155, 27, 27, 1)";

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/html/canvas/tools/yaml/element/color_space.yaml (282633 => 282634)


--- 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)
@@ -75,7 +75,7 @@
 - name: 2d.color.space.p3.toDataURL.p3.canvas
   testing:
   - 2d.color.space.p3.and.uint8
-  desc: test if toblob returns p3 data from canvas with p3 color space
+  desc: test if toDataURL returns p3 data from canvas with p3 color space
   attributes: |
     {colorSpace: "display-p3"}
   code: |
@@ -101,6 +101,35 @@
     });
     image.src = ""
 
+- 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 = ""
+
 - name: 2d.color.space.p3.toBlob.with.putImageData
   testing:
   - 2d.color.space.p3.and.uint8

Modified: trunk/LayoutTests/platform/ios/TestExpectations (282633 => 282634)


--- 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)
@@ -3500,6 +3500,7 @@
 imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.p3.canvas.html [ Pass ]
 imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.with.putImageData.html [ Pass ]
 imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html [ Pass ]
+imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html [ Pass ]
 imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.with.putImageData.html [ Pass ]
 storage/indexeddb/structured-clone-image-data-display-p3.html [ Pass ]
 

Modified: trunk/LayoutTests/platform/ios-14/TestExpectations (282633 => 282634)


--- 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)
@@ -84,6 +84,7 @@
 imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.p3.canvas.html [ Failure ]
 imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.with.putImageData.html [ Failure ]
 imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html [ Failure ]
+imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html [ Failure ]
 imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.with.putImageData.html [ Failure ]
 storage/indexeddb/structured-clone-image-data-display-p3.html [ Failure ]
 

Modified: trunk/LayoutTests/platform/mac/TestExpectations (282633 => 282634)


--- 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)
@@ -2359,6 +2359,7 @@
 [ Monterey+ ] imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.p3.canvas.html [ Pass ]
 [ Monterey+ ] imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toBlob.with.putImageData.html [ Pass ]
 [ Monterey+ ] imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.p3.canvas.html [ Pass ]
+[ Monterey+ ] imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.jpeg.p3.canvas.html [ Pass ]
 [ Monterey+ ] imported/w3c/web-platform-tests/html/canvas/element/wide-gamut-canvas/2d.color.space.p3.toDataURL.with.putImageData.html [ Pass ]
 [ Monterey+ ] storage/indexeddb/structured-clone-image-data-display-p3.html [ Pass ]
 

Modified: trunk/Source/WebCore/ChangeLog (282633 => 282634)


--- 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)
@@ -1,3 +1,20 @@
+2021-09-16  Cameron McCormack  <[email protected]>
+
+        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):
+
 2021-09-16  Diego Pino Garcia  <[email protected]>
 
         Unreviewed build fix for Ubuntu LTS/Debian after r282597

Modified: trunk/Source/WebCore/platform/graphics/cg/ImageBufferCGBackend.cpp (282633 => 282634)


--- 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)
@@ -93,6 +93,13 @@
     return image;
 }
 
+static CGColorSpaceRef colorSpaceForBitmap(DestinationColorSpace imageBufferColorSpace)
+{
+    if (CGColorSpaceGetModel(imageBufferColorSpace.platformColorSpace()) != kCGColorSpaceModelRGB)
+        return sRGBColorSpaceRef();
+    return imageBufferColorSpace.platformColorSpace();
+}
+
 static RefPtr<Image> createBitmapImageAfterScalingIfNeeded(RefPtr<NativeImage>&& image, const IntSize& logicalSize, const IntSize& backendSize, float resolutionScale, PreserveResolution preserveResolution)
 {
     if (!image)
@@ -101,7 +108,7 @@
     if (resolutionScale == 1 || preserveResolution == PreserveResolution::Yes)
         image = NativeImage::create(createCroppedImageIfNecessary(image->platformImage().get(), backendSize));
     else {
-        auto context = adoptCF(CGBitmapContextCreate(0, logicalSize.width(), logicalSize.height(), 8, 4 * logicalSize.width(), sRGBColorSpaceRef(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
+        auto context = adoptCF(CGBitmapContextCreate(0, logicalSize.width(), logicalSize.height(), 8, 4 * logicalSize.width(), colorSpaceForBitmap(image->colorSpace()), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
         CGContextSetBlendMode(context.get(), kCGBlendModeCopy);
         CGContextClipToRect(context.get(), FloatRect(FloatPoint::zero(), logicalSize));
         FloatSize imageSizeInUserSpace = logicalSize;
@@ -177,7 +184,7 @@
         // FIXME: Should this be using the same logic as ImageBufferUtilitiesCG?
 
         // JPEGs don't have an alpha channel, so we have to manually composite on top of black.
-        PixelBufferFormat format { AlphaPremultiplication::Premultiplied, PixelFormat::RGBA8, DestinationColorSpace::SRGB() };
+        PixelBufferFormat format { AlphaPremultiplication::Premultiplied, PixelFormat::RGBA8, DestinationColorSpace(colorSpaceForBitmap(colorSpace())) };
         auto pixelBuffer = getPixelBuffer(format, logicalRect());
         if (!pixelBuffer)
             return nullptr;
@@ -209,7 +216,7 @@
     if (!nativeImage)
         return nullptr;
     auto image = nativeImage->platformImage();
-    auto context = adoptCF(CGBitmapContextCreate(0, backendSize().width(), backendSize().height(), 8, 4 * backendSize().width(), sRGBColorSpaceRef(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
+    auto context = adoptCF(CGBitmapContextCreate(0, backendSize().width(), backendSize().height(), 8, 4 * backendSize().width(), colorSpaceForBitmap(colorSpace()), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
     CGContextSetBlendMode(context.get(), kCGBlendModeCopy);
     CGContextClipToRect(context.get(), CGRectMake(0, 0, backendSize().width(), backendSize().height()));
     CGContextDrawImage(context.get(), CGRectMake(0, 0, backendSize().width(), backendSize().height()), image.get());
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to