From 0522e514a92e6534311a33890780b877b6353979 Mon Sep 17 00:00:00 2001 From: Taylor Mapes Date: Sun, 13 Oct 2024 16:12:27 -0500 Subject: [PATCH] LibWeb: Implement CanvasRenderingContext2D.createImageData(ImageData) Fixes at least one WPT test. https://wpt.live/html/canvas/element/pixel-manipulation/2d.imageData.create1.basic.html --- .../expected/canvas/2d-canvas-create-imagedata.txt | 1 + .../input/canvas/2d-canvas-create-imagedata.html | 14 ++++++++++++++ .../Libraries/LibWeb/HTML/Canvas/CanvasImageData.h | 1 + .../LibWeb/HTML/Canvas/CanvasImageData.idl | 2 +- .../LibWeb/HTML/CanvasRenderingContext2D.cpp | 12 ++++++++++++ .../LibWeb/HTML/CanvasRenderingContext2D.h | 1 + 6 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 Tests/LibWeb/Text/expected/canvas/2d-canvas-create-imagedata.txt create mode 100644 Tests/LibWeb/Text/input/canvas/2d-canvas-create-imagedata.html diff --git a/Tests/LibWeb/Text/expected/canvas/2d-canvas-create-imagedata.txt b/Tests/LibWeb/Text/expected/canvas/2d-canvas-create-imagedata.txt new file mode 100644 index 00000000000..7ef22e9a431 --- /dev/null +++ b/Tests/LibWeb/Text/expected/canvas/2d-canvas-create-imagedata.txt @@ -0,0 +1 @@ +PASS diff --git a/Tests/LibWeb/Text/input/canvas/2d-canvas-create-imagedata.html b/Tests/LibWeb/Text/input/canvas/2d-canvas-create-imagedata.html new file mode 100644 index 00000000000..783456b22ff --- /dev/null +++ b/Tests/LibWeb/Text/input/canvas/2d-canvas-create-imagedata.html @@ -0,0 +1,14 @@ + + diff --git a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasImageData.h b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasImageData.h index d19f904f7d9..e365220e024 100644 --- a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasImageData.h +++ b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasImageData.h @@ -16,6 +16,7 @@ public: virtual ~CanvasImageData() = default; virtual WebIDL::ExceptionOr> create_image_data(int width, int height, Optional const& settings = {}) const = 0; + virtual WebIDL::ExceptionOr> create_image_data(ImageData const&) const = 0; virtual WebIDL::ExceptionOr> get_image_data(int x, int y, int width, int height, Optional const& settings = {}) const = 0; virtual void put_image_data(ImageData const&, float x, float y) = 0; diff --git a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasImageData.idl b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasImageData.idl index 4709ff43310..d5af08612e4 100644 --- a/Userland/Libraries/LibWeb/HTML/Canvas/CanvasImageData.idl +++ b/Userland/Libraries/LibWeb/HTML/Canvas/CanvasImageData.idl @@ -3,7 +3,7 @@ // https://html.spec.whatwg.org/multipage/canvas.html#canvasimagedata interface mixin CanvasImageData { ImageData createImageData([EnforceRange] long sw, [EnforceRange] long sh, optional ImageDataSettings settings = {}); - [FIXME] ImageData createImageData(ImageData imagedata); + ImageData createImageData(ImageData imagedata); ImageData getImageData([EnforceRange] long sx, [EnforceRange] long sy, [EnforceRange] long sw, [EnforceRange] long sh, optional ImageDataSettings settings = {}); diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index 31b82b65150..278cdd14fed 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -342,6 +342,18 @@ WebIDL::ExceptionOr> CanvasRenderingContext2D::creat return image_data; } +// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createimagedata-imagedata +WebIDL::ExceptionOr> CanvasRenderingContext2D::create_image_data(ImageData const& image_data) const +{ + // 1. Let newImageData be a new ImageData object. + // 2. Initialize newImageData given the value of imagedata's width attribute, the value of imagedata's height attribute, and defaultColorSpace set to the value of imagedata's colorSpace attribute. + // FIXME: Set defaultColorSpace to the value of image_data's colorSpace attribute + // 3. Initialize the image data of newImageData to transparent black. + // NOTE: No-op, already done during creation. + // 4. Return newImageData. + return TRY(ImageData::create(realm(), image_data.width(), image_data.height())); +} + // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-getimagedata WebIDL::ExceptionOr> CanvasRenderingContext2D::get_image_data(int x, int y, int width, int height, Optional const& settings) const { diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h index 2ce3223a274..8ad9732388c 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h @@ -75,6 +75,7 @@ public: virtual void fill(Path2D& path, StringView fill_rule) override; virtual WebIDL::ExceptionOr> create_image_data(int width, int height, Optional const& settings = {}) const override; + virtual WebIDL::ExceptionOr> create_image_data(ImageData const& image_data) const override; virtual WebIDL::ExceptionOr> get_image_data(int x, int y, int width, int height, Optional const& settings = {}) const override; virtual void put_image_data(ImageData const&, float x, float y) override;