LibWeb: Implement CanvasRenderingContext2D.createImageData(ImageData)

Fixes at least one WPT test.
https://wpt.live/html/canvas/element/pixel-manipulation/2d.imageData.create1.basic.html
This commit is contained in:
Taylor Mapes 2024-10-13 16:12:27 -05:00 committed by Sam Atkins
parent 6ffe83c9da
commit 0522e514a9
Notes: github-actions[bot] 2024-10-14 08:34:45 +00:00
6 changed files with 30 additions and 1 deletions

View file

@ -0,0 +1 @@
PASS

View file

@ -0,0 +1,14 @@
<script src="../include.js"></script>
<script>
test(() => {
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
const existing_image_data = context.createImageData(75, 75);
const image_data = context.createImageData(existing_image_data);
if (image_data.width === 75 && image_data.height === 75) {
println('PASS')
} else {
println('FAIL')
}
});
</script>

View file

@ -16,6 +16,7 @@ public:
virtual ~CanvasImageData() = default;
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> create_image_data(int width, int height, Optional<ImageDataSettings> const& settings = {}) const = 0;
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> create_image_data(ImageData const&) const = 0;
virtual WebIDL::ExceptionOr<JS::GCPtr<ImageData>> get_image_data(int x, int y, int width, int height, Optional<ImageDataSettings> const& settings = {}) const = 0;
virtual void put_image_data(ImageData const&, float x, float y) = 0;

View file

@ -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 = {});

View file

@ -342,6 +342,18 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> CanvasRenderingContext2D::creat
return image_data;
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createimagedata-imagedata
WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> 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<JS::GCPtr<ImageData>> CanvasRenderingContext2D::get_image_data(int x, int y, int width, int height, Optional<ImageDataSettings> const& settings) const
{

View file

@ -75,6 +75,7 @@ public:
virtual void fill(Path2D& path, StringView fill_rule) override;
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> create_image_data(int width, int height, Optional<ImageDataSettings> const& settings = {}) const override;
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> create_image_data(ImageData const& image_data) const override;
virtual WebIDL::ExceptionOr<JS::GCPtr<ImageData>> get_image_data(int x, int y, int width, int height, Optional<ImageDataSettings> const& settings = {}) const override;
virtual void put_image_data(ImageData const&, float x, float y) override;