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
This commit is contained in:
Cory Virok 2024-10-13 12:42:39 -07:00 committed by Andreas Kling
parent d71887e48c
commit 01301c374b
Notes: github-actions[bot] 2024-10-14 06:25:28 +00:00

View file

@ -353,6 +353,13 @@ WebIDL::ExceptionOr<JS::GCPtr<ImageData>> 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.