2020-04-21 23:49:51 +02:00
|
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
|
2025-05-04 13:45:51 +02:00
|
|
|
|
* Copyright (c) 2024-2025, Kenneth Myhra <kennethmyhra@serenityos.org>
|
2020-04-21 23:49:51 +02:00
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-21 23:49:51 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibGfx/Bitmap.h>
|
2021-05-22 23:33:26 +04:30
|
|
|
|
#include <LibJS/Runtime/TypedArray.h>
|
2024-04-27 12:09:58 +12:00
|
|
|
|
#include <LibWeb/Bindings/ImageDataPrototype.h>
|
2022-09-25 16:38:21 -06:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2020-07-26 15:08:16 +02:00
|
|
|
|
#include <LibWeb/HTML/ImageData.h>
|
2025-05-04 13:45:51 +02:00
|
|
|
|
#include <LibWeb/HTML/StructuredSerialize.h>
|
2024-03-26 14:31:32 +01:00
|
|
|
|
#include <LibWeb/WebIDL/Buffers.h>
|
2024-03-23 21:45:59 +01:00
|
|
|
|
#include <LibWeb/WebIDL/DOMException.h>
|
|
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2020-04-21 23:49:51 +02:00
|
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
|
namespace Web::HTML {
|
2020-04-21 23:49:51 +02:00
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(ImageData);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
|
2025-05-04 13:45:51 +02:00
|
|
|
|
[[nodiscard]] static auto create_bitmap_backed_by_uint8_clamped_array(u32 const width, u32 const height, JS::Uint8ClampedArray& data)
|
|
|
|
|
{
|
|
|
|
|
return Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::RGBA8888, Gfx::AlphaType::Unpremultiplied, Gfx::IntSize(width, height), width * sizeof(u32), data.data().data());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GC::Ref<ImageData> ImageData::create(JS::Realm& realm)
|
|
|
|
|
{
|
|
|
|
|
return realm.create<ImageData>(realm);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 14:34:11 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/canvas.html#dom-imagedata
|
2025-02-26 11:14:13 -07:00
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<ImageData>> ImageData::create(JS::Realm& realm, u32 sw, u32 sh, Optional<ImageDataSettings> const& settings)
|
2024-03-23 21:45:59 +01:00
|
|
|
|
{
|
|
|
|
|
// 1. If one or both of sw and sh are zero, then throw an "IndexSizeError" DOMException.
|
|
|
|
|
if (sw == 0 || sh == 0)
|
2025-08-07 19:31:52 -04:00
|
|
|
|
return WebIDL::IndexSizeError::create(realm, "The source width and height must be greater than zero."_utf16);
|
2024-03-23 21:45:59 +01:00
|
|
|
|
|
|
|
|
|
// 2. Initialize this given sw, sh, and settings set to settings.
|
|
|
|
|
// 3. Initialize the image data of this to transparent black.
|
2025-02-26 11:14:13 -07:00
|
|
|
|
return initialize(realm, sh, sw, settings);
|
2024-03-23 21:45:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<ImageData>> ImageData::construct_impl(JS::Realm& realm, u32 sw, u32 sh, Optional<ImageDataSettings> const& settings)
|
2020-04-21 23:49:51 +02:00
|
|
|
|
{
|
2024-03-23 21:45:59 +01:00
|
|
|
|
return ImageData::create(realm, sw, sh, settings);
|
|
|
|
|
}
|
2022-08-16 00:20:49 +01:00
|
|
|
|
|
2024-03-26 14:31:32 +01:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/canvas.html#dom-imagedata-with-data
|
2025-02-26 11:14:13 -07:00
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<ImageData>> ImageData::create(JS::Realm& realm, GC::Root<WebIDL::BufferSource> const& data, u32 sw, Optional<u32> sh, Optional<ImageDataSettings> const& settings)
|
2024-03-26 14:31:32 +01:00
|
|
|
|
{
|
|
|
|
|
auto& vm = realm.vm();
|
|
|
|
|
|
|
|
|
|
if (!is<JS::Uint8ClampedArray>(*data->raw_object()))
|
|
|
|
|
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Uint8ClampedArray");
|
|
|
|
|
|
|
|
|
|
auto& uint8_clamped_array_data = static_cast<JS::Uint8ClampedArray&>(*data->raw_object());
|
|
|
|
|
|
|
|
|
|
// 1. Let length be the number of bytes in data.
|
|
|
|
|
auto length = uint8_clamped_array_data.byte_length().length();
|
|
|
|
|
|
|
|
|
|
// 2. If length is not a nonzero integral multiple of four, then throw an "InvalidStateError" DOMException.
|
|
|
|
|
if (length == 0 || length % 4 != 0)
|
2025-08-07 19:31:52 -04:00
|
|
|
|
return WebIDL::InvalidStateError::create(realm, "Source data must have a non-sero length that is a multiple of four."_utf16);
|
2024-03-26 14:31:32 +01:00
|
|
|
|
|
|
|
|
|
// 3. Let length be length divided by four.
|
|
|
|
|
length = length / 4;
|
|
|
|
|
|
|
|
|
|
// 4. If length is not an integral multiple of sw, then throw an "IndexSizeError" DOMException.
|
|
|
|
|
// NOTE: At this step, the length is guaranteed to be greater than zero (otherwise the second step above would have aborted the steps),
|
|
|
|
|
// so if sw is zero, this step will throw the exception and return.
|
|
|
|
|
if (sw == 0 || length % sw != 0)
|
2025-08-07 19:31:52 -04:00
|
|
|
|
return WebIDL::IndexSizeError::create(realm, "Source width must be a multiple of source data's length."_utf16);
|
2024-03-26 14:31:32 +01:00
|
|
|
|
|
|
|
|
|
// 5. Let height be length divided by sw.
|
|
|
|
|
auto height = length / sw;
|
|
|
|
|
|
|
|
|
|
// 6. If sh was given and its value is not equal to height, then throw an "IndexSizeError" DOMException.
|
|
|
|
|
if (sh.has_value() && sh.value() != height)
|
2025-08-07 19:31:52 -04:00
|
|
|
|
return WebIDL::IndexSizeError::create(realm, "Source height must be equal to the calculated height of the data."_utf16);
|
2024-03-26 14:31:32 +01:00
|
|
|
|
|
|
|
|
|
// 7. Initialize this given sw, sh, settings set to settings, and source set to data.
|
2025-02-26 11:14:13 -07:00
|
|
|
|
// FIXME: This seems to be a spec issue, sh is an optional but height always have a value.
|
|
|
|
|
return initialize(realm, height, sw, settings, uint8_clamped_array_data);
|
2024-03-26 14:31:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<ImageData>> ImageData::construct_impl(JS::Realm& realm, GC::Root<WebIDL::BufferSource> const& data, u32 sw, Optional<u32> sh, Optional<ImageDataSettings> const& settings)
|
2024-03-26 14:31:32 +01:00
|
|
|
|
{
|
|
|
|
|
return ImageData::create(realm, data, sw, move(sh), settings);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-26 11:14:13 -07:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/canvas.html#initialize-an-imagedata-object
|
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<ImageData>> ImageData::initialize(JS::Realm& realm, u32 rows, u32 pixels_per_row, Optional<ImageDataSettings> const& settings, GC::Ptr<JS::Uint8ClampedArray> source, Optional<Bindings::PredefinedColorSpace> default_color_space)
|
|
|
|
|
{
|
|
|
|
|
auto data = TRY([&]() -> WebIDL::ExceptionOr<GC::Ref<JS::Uint8ClampedArray>> {
|
|
|
|
|
// 1. If source was given, then initialize the data attribute of imageData to source.
|
|
|
|
|
if (source) {
|
|
|
|
|
return GC::Ref<JS::Uint8ClampedArray> { *source };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Checked<u32> size = rows;
|
|
|
|
|
size *= pixels_per_row;
|
|
|
|
|
size *= sizeof(u32);
|
|
|
|
|
if (size.has_overflow())
|
2025-08-07 19:31:52 -04:00
|
|
|
|
return WebIDL::IndexSizeError::create(realm, "The specified image size could not created"_utf16);
|
2025-02-26 11:14:13 -07:00
|
|
|
|
|
|
|
|
|
// 2. Otherwise (source was not given), initialize the data attribute of imageData to a new Uint8ClampedArray object.
|
|
|
|
|
// The Uint8ClampedArray object must use a new Canvas Pixel ArrayBuffer for its storage, and must have a zero start
|
|
|
|
|
// offset and a length equal to the length of its storage, in bytes. The Canvas Pixel ArrayBuffer must have the
|
|
|
|
|
// correct size to store rows × pixelsPerRow pixels.
|
|
|
|
|
// 3. If the Canvas Pixel ArrayBuffer cannot be allocated, then rethrow the RangeError thrown by JavaScript, and return.
|
|
|
|
|
return TRY(JS::Uint8ClampedArray::create(realm, sizeof(u32) * rows * pixels_per_row));
|
|
|
|
|
}());
|
|
|
|
|
|
|
|
|
|
// AD-HOC: Create the bitmap backed by the Uint8ClampedArray.
|
2025-05-04 13:45:51 +02:00
|
|
|
|
auto bitmap = TRY_OR_THROW_OOM(realm.vm(), create_bitmap_backed_by_uint8_clamped_array(pixels_per_row, rows, *data));
|
2025-02-26 11:14:13 -07:00
|
|
|
|
|
|
|
|
|
// 4. Initialize the width attribute of imageData to pixelsPerRow.
|
|
|
|
|
// 5. Initialize the height attribute of imageData to rows.
|
|
|
|
|
|
|
|
|
|
// 6. If settings was given and settings["colorSpace"] exists, then initialize the colorSpace attribute of imageData to settings["colorSpace"].
|
|
|
|
|
Bindings::PredefinedColorSpace color_space {};
|
|
|
|
|
if (settings.has_value())
|
|
|
|
|
color_space = settings->color_space;
|
|
|
|
|
// 7. Otherwise, if defaultColorSpace was given, then initialize the colorSpace attribute of imageData to defaultColorSpace.
|
|
|
|
|
else if (default_color_space.has_value())
|
|
|
|
|
color_space = *default_color_space;
|
|
|
|
|
// 8. Otherwise, initialize the colorSpace attribute of imageData to "srgb".
|
|
|
|
|
else
|
|
|
|
|
color_space = Bindings::PredefinedColorSpace::Srgb;
|
|
|
|
|
|
|
|
|
|
return realm.create<ImageData>(realm, move(bitmap), data, color_space);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 13:45:51 +02:00
|
|
|
|
ImageData::ImageData(JS::Realm& realm)
|
|
|
|
|
: PlatformObject(realm)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-26 11:14:13 -07:00
|
|
|
|
ImageData::ImageData(JS::Realm& realm, NonnullRefPtr<Gfx::Bitmap> bitmap, GC::Ref<JS::Uint8ClampedArray> data, Bindings::PredefinedColorSpace color_space)
|
2022-09-25 16:38:21 -06:00
|
|
|
|
: PlatformObject(realm)
|
2022-09-02 23:07:05 +02:00
|
|
|
|
, m_bitmap(move(bitmap))
|
2025-02-26 11:14:13 -07:00
|
|
|
|
, m_color_space(color_space)
|
2020-04-21 23:49:51 +02:00
|
|
|
|
, m_data(move(data))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
|
ImageData::~ImageData() = default;
|
2020-04-21 23:49:51 +02:00
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void ImageData::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(ImageData);
|
2025-04-20 16:22:57 +02:00
|
|
|
|
Base::initialize(realm);
|
2023-01-10 06:28:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-02 23:07:05 +02:00
|
|
|
|
void ImageData::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
2023-11-19 16:18:00 +13:00
|
|
|
|
visitor.visit(m_data);
|
2022-09-02 23:07:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-07 12:43:03 +02:00
|
|
|
|
WebIDL::UnsignedLong ImageData::width() const
|
2020-04-21 23:49:51 +02:00
|
|
|
|
{
|
|
|
|
|
return m_bitmap->width();
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-07 12:43:03 +02:00
|
|
|
|
WebIDL::UnsignedLong ImageData::height() const
|
2020-04-21 23:49:51 +02:00
|
|
|
|
{
|
|
|
|
|
return m_bitmap->height();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JS::Uint8ClampedArray* ImageData::data()
|
|
|
|
|
{
|
2022-09-02 23:07:05 +02:00
|
|
|
|
return m_data;
|
2020-04-21 23:49:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-29 13:02:52 +01:00
|
|
|
|
JS::Uint8ClampedArray const* ImageData::data() const
|
2020-04-21 23:49:51 +02:00
|
|
|
|
{
|
2022-09-02 23:07:05 +02:00
|
|
|
|
return m_data;
|
2020-04-21 23:49:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 13:45:51 +02:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/canvas.html#pixel-manipulation:serialization-steps
|
2025-07-17 09:51:04 -04:00
|
|
|
|
WebIDL::ExceptionOr<void> ImageData::serialization_steps(HTML::TransferDataEncoder& serialized, bool for_storage, HTML::SerializationMemory& memory)
|
2025-05-04 13:45:51 +02:00
|
|
|
|
{
|
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
|
|
|
// 1. Set serialized.[[Data]] to the sub-serialization of the value of value's data attribute.
|
|
|
|
|
auto serialized_data = TRY(structured_serialize_internal(vm, m_data, for_storage, memory));
|
2025-07-17 09:51:04 -04:00
|
|
|
|
serialized.append(move(serialized_data));
|
2025-05-04 13:45:51 +02:00
|
|
|
|
|
|
|
|
|
// 2. Set serialized.[[Width]] to the value of value's width attribute.
|
2025-07-17 09:51:04 -04:00
|
|
|
|
serialized.encode(m_bitmap->width());
|
2025-05-04 13:45:51 +02:00
|
|
|
|
|
|
|
|
|
// 3. Set serialized.[[Height]] to the value of value's height attribute.
|
2025-07-17 09:51:04 -04:00
|
|
|
|
serialized.encode(m_bitmap->height());
|
2025-05-04 13:45:51 +02:00
|
|
|
|
|
|
|
|
|
// 4. Set serialized.[[ColorSpace]] to the value of value's colorSpace attribute.
|
2025-07-17 09:51:04 -04:00
|
|
|
|
serialized.encode(m_color_space);
|
2025-05-04 13:45:51 +02:00
|
|
|
|
|
|
|
|
|
// FIXME:: 5. Set serialized.[[PixelFormat]] to the value of value's pixelFormat attribute.
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/canvas.html#pixel-manipulation:deserialization-steps
|
2025-07-17 09:51:04 -04:00
|
|
|
|
WebIDL::ExceptionOr<void> ImageData::deserialization_steps(HTML::TransferDataDecoder& serialized, HTML::DeserializationMemory& memory)
|
2025-05-04 13:45:51 +02:00
|
|
|
|
{
|
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
auto& realm = this->realm();
|
|
|
|
|
|
|
|
|
|
// 1. Initialize value's data attribute to the sub-deserialization of serialized.[[Data]].
|
2025-07-17 09:51:04 -04:00
|
|
|
|
auto deserialized = TRY(structured_deserialize_internal(vm, serialized, realm, memory));
|
2025-07-17 19:51:09 -04:00
|
|
|
|
m_data = as<JS::Uint8ClampedArray>(deserialized.as_object());
|
2025-05-04 13:45:51 +02:00
|
|
|
|
|
|
|
|
|
// 2. Initialize value's width attribute to serialized.[[Width]].
|
2025-07-17 09:51:04 -04:00
|
|
|
|
auto width = serialized.decode<int>();
|
2025-05-04 13:45:51 +02:00
|
|
|
|
|
|
|
|
|
// 3. Initialize value's height attribute to serialized.[[Height]].
|
2025-07-17 09:51:04 -04:00
|
|
|
|
auto height = serialized.decode<int>();
|
2025-05-04 13:45:51 +02:00
|
|
|
|
|
|
|
|
|
// 4. Initialize value's colorSpace attribute to serialized.[[ColorSpace]].
|
2025-07-17 09:51:04 -04:00
|
|
|
|
m_color_space = serialized.decode<Bindings::PredefinedColorSpace>();
|
2025-05-04 13:45:51 +02:00
|
|
|
|
|
|
|
|
|
// FIXME: 5. Initialize value's pixelFormat attribute to serialized.[[PixelFormat]].
|
|
|
|
|
|
|
|
|
|
// AD-HOC: Create the bitmap backed by the Uint8ClampedArray.
|
|
|
|
|
m_bitmap = TRY_OR_THROW_OOM(vm, create_bitmap_backed_by_uint8_clamped_array(width, height, *m_data));
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 23:49:51 +02:00
|
|
|
|
}
|