Browse Source

LibWeb: Fix CRC2D.getImageData() when width != height

We were mistakenly using the width as both width and height when getting
ImageData from a 2D canvas.
Andreas Kling 1 year ago
parent
commit
134c4b6335

+ 2 - 0
Tests/LibWeb/Text/expected/HTML/canvas-getImageData-oblong.txt

@@ -0,0 +1,2 @@
+[object ImageData]
+PASS: Didn't crash

+ 11 - 0
Tests/LibWeb/Text/input/HTML/canvas-getImageData-oblong.html

@@ -0,0 +1,11 @@
+<script src="../include.js"></script>
+<body><canvas id=c width=1 height=2></div>
+<script>
+    test(() => {
+        let x = c.getContext("2d");
+        x.clearRect(0, 0, 1, 1);
+        let d = x.getImageData(0, 0, 1, 2);
+        println(d);
+        println("PASS: Didn't crash");
+    });
+</script>

+ 1 - 1
Userland/Libraries/LibWeb/HTML/ImageData.cpp

@@ -29,7 +29,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> ImageData::create(JS::Realm& re
     // 2. Initialize this given sw, sh, and settings set to settings.
     // 2. Initialize this given sw, sh, and settings set to settings.
     // 3. Initialize the image data of this to transparent black.
     // 3. Initialize the image data of this to transparent black.
     auto data = TRY(JS::Uint8ClampedArray::create(realm, sw * sh * 4));
     auto data = TRY(JS::Uint8ClampedArray::create(realm, sw * sh * 4));
-    auto bitmap = TRY_OR_THROW_OOM(vm, Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::RGBA8888, Gfx::IntSize(sw, sw), 1, sw * sizeof(u32), data->data().data()));
+    auto bitmap = TRY_OR_THROW_OOM(vm, Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::RGBA8888, Gfx::IntSize(sw, sh), 1, sw * sizeof(u32), data->data().data()));
 
 
     return realm.heap().allocate<ImageData>(realm, realm, bitmap, data);
     return realm.heap().allocate<ImageData>(realm, realm, bitmap, data);
 }
 }