Skip to content

Commit c17171b

Browse files
kennethmyhraawesomekling
authored andcommitted
LibWeb: Add ImageData constructor with data
1 parent 900a889 commit c17171b

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

Tests/LibWeb/Text/expected/HTML/ImageData-create-with-size.txt

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

Tests/LibWeb/Text/input/HTML/ImageData-create-with-size.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,17 @@
55
let imageData = new ImageData(100, 100);
66
println(imageData.data);
77
println(imageData.data.length);
8+
9+
const arr = new Uint8ClampedArray(40_000);
10+
// Fill the array with the same RGBA values
11+
for (let i = 0; i < arr.length; i += 4) {
12+
arr[i + 0] = 0; // R value
13+
arr[i + 1] = 190; // G value
14+
arr[i + 2] = 0; // B value
15+
arr[i + 3] = 255; // A value
16+
}
17+
imageData = new ImageData(arr, 200);
18+
println(imageData.data);
19+
println(imageData.data.length);
820
});
921
</script>

Userland/Libraries/LibWeb/HTML/ImageData.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <LibJS/Runtime/TypedArray.h>
1010
#include <LibWeb/Bindings/Intrinsics.h>
1111
#include <LibWeb/HTML/ImageData.h>
12+
#include <LibWeb/WebIDL/Buffers.h>
1213
#include <LibWeb/WebIDL/DOMException.h>
1314
#include <LibWeb/WebIDL/ExceptionOr.h>
1415

@@ -37,6 +38,50 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> ImageData::construct_impl(JS::R
3738
return ImageData::create(realm, sw, sh, settings);
3839
}
3940

41+
// https://html.spec.whatwg.org/multipage/canvas.html#dom-imagedata-with-data
42+
WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> ImageData::create(JS::Realm& realm, JS::Handle<WebIDL::BufferSource> const& data, u32 sw, Optional<u32> sh, Optional<ImageDataSettings> const&)
43+
{
44+
auto& vm = realm.vm();
45+
46+
if (!is<JS::Uint8ClampedArray>(*data->raw_object()))
47+
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Uint8ClampedArray");
48+
49+
auto& uint8_clamped_array_data = static_cast<JS::Uint8ClampedArray&>(*data->raw_object());
50+
51+
// 1. Let length be the number of bytes in data.
52+
auto length = uint8_clamped_array_data.byte_length().length();
53+
54+
// 2. If length is not a nonzero integral multiple of four, then throw an "InvalidStateError" DOMException.
55+
if (length == 0 || length % 4 != 0)
56+
return WebIDL::InvalidStateError::create(realm, "Source data must have a non-sero length that is a multiple of four."_fly_string);
57+
58+
// 3. Let length be length divided by four.
59+
length = length / 4;
60+
61+
// 4. If length is not an integral multiple of sw, then throw an "IndexSizeError" DOMException.
62+
// NOTE: At this step, the length is guaranteed to be greater than zero (otherwise the second step above would have aborted the steps),
63+
// so if sw is zero, this step will throw the exception and return.
64+
if (sw == 0 || length % sw != 0)
65+
return WebIDL::IndexSizeError::create(realm, "Source width must be a multiple of source data's length."_fly_string);
66+
67+
// 5. Let height be length divided by sw.
68+
auto height = length / sw;
69+
70+
// 6. If sh was given and its value is not equal to height, then throw an "IndexSizeError" DOMException.
71+
if (sh.has_value() && sh.value() != height)
72+
return WebIDL::IndexSizeError::create(realm, "Source height must be equal to the calculated height of the data."_fly_string);
73+
74+
// 7. Initialize this given sw, sh, settings set to settings, and source set to data.
75+
auto bitmap = TRY_OR_THROW_OOM(vm, Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::RGBA8888, Gfx::IntSize(sw, sw), 1, sw * sizeof(u32), uint8_clamped_array_data.data().data()));
76+
77+
return realm.heap().allocate<ImageData>(realm, realm, bitmap, uint8_clamped_array_data);
78+
}
79+
80+
WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> ImageData::construct_impl(JS::Realm& realm, JS::Handle<WebIDL::BufferSource> const& data, u32 sw, Optional<u32> sh, Optional<ImageDataSettings> const& settings)
81+
{
82+
return ImageData::create(realm, data, sw, move(sh), settings);
83+
}
84+
4085
ImageData::ImageData(JS::Realm& realm, NonnullRefPtr<Gfx::Bitmap> bitmap, JS::NonnullGCPtr<JS::Uint8ClampedArray> data)
4186
: PlatformObject(realm)
4287
, m_bitmap(move(bitmap))

Userland/Libraries/LibWeb/HTML/ImageData.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ class ImageData final : public Bindings::PlatformObject {
2323

2424
public:
2525
[[nodiscard]] static WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> create(JS::Realm&, u32 sw, u32 sh, Optional<ImageDataSettings> const& settings = {});
26+
[[nodiscard]] static WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> create(JS::Realm&, JS::Handle<WebIDL::BufferSource> const& data, u32 sw, Optional<u32> sh = {}, Optional<ImageDataSettings> const& settings = {});
27+
2628
[[nodiscard]] static WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> construct_impl(JS::Realm&, u32 sw, u32 sh, Optional<ImageDataSettings> const& settings = {});
29+
[[nodiscard]] static WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> construct_impl(JS::Realm&, JS::Handle<WebIDL::BufferSource> const& data, u32 sw, Optional<u32> sh = {}, Optional<ImageDataSettings> const& settings = {});
2730

2831
virtual ~ImageData() override;
2932

Userland/Libraries/LibWeb/HTML/ImageData.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dictionary ImageDataSettings {
99
[Exposed=(Window,Worker), Serializable]
1010
interface ImageData {
1111
constructor(unsigned long sw, unsigned long sh, optional ImageDataSettings settings = {});
12-
// FIXME: constructor(Uint8ClampedArray data, unsigned long sw, optional unsigned long sh, optional ImageDataSettings settings = {});
12+
constructor(Uint8ClampedArray data, unsigned long sw, optional unsigned long sh, optional ImageDataSettings settings = {});
1313
readonly attribute unsigned long width;
1414
readonly attribute unsigned long height;
1515
readonly attribute Uint8ClampedArray data;

0 commit comments

Comments
 (0)