From 01301c374b295578740a3a9ebf19468a870c763e Mon Sep 17 00:00:00 2001 From: Cory Virok Date: Sun, 13 Oct 2024 12:42:39 -0700 Subject: [PATCH] LibWeb: Wrap negative dims for getImageData() Given negative width or height values for CanvasRenderingContext2D getImageData(), translate the source rect. I wasn't able to find this in the spec, but WPT tests for it and MDN defines this behavior. Fixes this WPT test: https://wpt.live/html/canvas/element/pixel-manipulation/2d.imageData.get.source.negative.html Described in MDN here: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getImageData#sw getImageData() spec: https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-getimagedata --- .../Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index 30952836dfb..aa7736adb88 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -353,6 +353,13 @@ WebIDL::ExceptionOr> CanvasRenderingContext2D::get_image_da // 5. Let the source rectangle be the rectangle whose corners are the four points (sx, sy), (sx+sw, sy), (sx+sw, sy+sh), (sx, sy+sh). auto source_rect = Gfx::Rect { x, y, abs_width, abs_height }; + + // NOTE: The spec doesn't seem to define this behavior, but MDN does and the WPT tests + // assume it works this way. + // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getImageData#sw + if (width < 0 || height < 0) { + source_rect = source_rect.translated(min(width, 0), min(height, 0)); + } auto source_rect_intersected = source_rect.intersected(bitmap.rect()); // 6. Set the pixel values of imageData to be the pixels of this's output bitmap in the area specified by the source rectangle in the bitmap's coordinate space units, converted from this's color space to imageData's colorSpace using 'relative-colorimetric' rendering intent.