ImageData.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2024, Kenneth Myhra <kennethmyhra@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGfx/Bitmap.h>
  8. #include <LibJS/Runtime/TypedArray.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/HTML/ImageData.h>
  11. #include <LibWeb/WebIDL/Buffers.h>
  12. #include <LibWeb/WebIDL/DOMException.h>
  13. #include <LibWeb/WebIDL/ExceptionOr.h>
  14. namespace Web::HTML {
  15. JS_DEFINE_ALLOCATOR(ImageData);
  16. WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> ImageData::create(JS::Realm& realm, u32 sw, u32 sh, Optional<ImageDataSettings> const&)
  17. {
  18. auto& vm = realm.vm();
  19. // 1. If one or both of sw and sh are zero, then throw an "IndexSizeError" DOMException.
  20. if (sw == 0 || sh == 0)
  21. return WebIDL::IndexSizeError::create(realm, "The source width and height must be greater than zero."_fly_string);
  22. // 2. Initialize this given sw, sh, and settings set to settings.
  23. // 3. Initialize the image data of this to transparent black.
  24. auto data = TRY(JS::Uint8ClampedArray::create(realm, sw * sh * 4));
  25. 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()));
  26. return realm.heap().allocate<ImageData>(realm, realm, bitmap, data);
  27. }
  28. WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> ImageData::construct_impl(JS::Realm& realm, u32 sw, u32 sh, Optional<ImageDataSettings> const& settings)
  29. {
  30. return ImageData::create(realm, sw, sh, settings);
  31. }
  32. // https://html.spec.whatwg.org/multipage/canvas.html#dom-imagedata-with-data
  33. WebIDL::ExceptionOr<JS::NonnullGCPtr<ImageData>> ImageData::create(JS::Realm& realm, JS::Handle<WebIDL::BufferSource> const& data, u32 sw, Optional<u32> sh, Optional<ImageDataSettings> const&)
  34. {
  35. auto& vm = realm.vm();
  36. if (!is<JS::Uint8ClampedArray>(*data->raw_object()))
  37. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Uint8ClampedArray");
  38. auto& uint8_clamped_array_data = static_cast<JS::Uint8ClampedArray&>(*data->raw_object());
  39. // 1. Let length be the number of bytes in data.
  40. auto length = uint8_clamped_array_data.byte_length().length();
  41. // 2. If length is not a nonzero integral multiple of four, then throw an "InvalidStateError" DOMException.
  42. if (length == 0 || length % 4 != 0)
  43. return WebIDL::InvalidStateError::create(realm, "Source data must have a non-sero length that is a multiple of four."_fly_string);
  44. // 3. Let length be length divided by four.
  45. length = length / 4;
  46. // 4. If length is not an integral multiple of sw, then throw an "IndexSizeError" DOMException.
  47. // NOTE: At this step, the length is guaranteed to be greater than zero (otherwise the second step above would have aborted the steps),
  48. // so if sw is zero, this step will throw the exception and return.
  49. if (sw == 0 || length % sw != 0)
  50. return WebIDL::IndexSizeError::create(realm, "Source width must be a multiple of source data's length."_fly_string);
  51. // 5. Let height be length divided by sw.
  52. auto height = length / sw;
  53. // 6. If sh was given and its value is not equal to height, then throw an "IndexSizeError" DOMException.
  54. if (sh.has_value() && sh.value() != height)
  55. return WebIDL::IndexSizeError::create(realm, "Source height must be equal to the calculated height of the data."_fly_string);
  56. // 7. Initialize this given sw, sh, settings set to settings, and source set to data.
  57. 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()));
  58. return realm.heap().allocate<ImageData>(realm, realm, bitmap, uint8_clamped_array_data);
  59. }
  60. 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)
  61. {
  62. return ImageData::create(realm, data, sw, move(sh), settings);
  63. }
  64. ImageData::ImageData(JS::Realm& realm, NonnullRefPtr<Gfx::Bitmap> bitmap, JS::NonnullGCPtr<JS::Uint8ClampedArray> data)
  65. : PlatformObject(realm)
  66. , m_bitmap(move(bitmap))
  67. , m_data(move(data))
  68. {
  69. }
  70. ImageData::~ImageData() = default;
  71. void ImageData::initialize(JS::Realm& realm)
  72. {
  73. Base::initialize(realm);
  74. WEB_SET_PROTOTYPE_FOR_INTERFACE(ImageData);
  75. }
  76. void ImageData::visit_edges(Cell::Visitor& visitor)
  77. {
  78. Base::visit_edges(visitor);
  79. visitor.visit(m_data);
  80. }
  81. unsigned ImageData::width() const
  82. {
  83. return m_bitmap->width();
  84. }
  85. unsigned ImageData::height() const
  86. {
  87. return m_bitmap->height();
  88. }
  89. JS::Uint8ClampedArray* ImageData::data()
  90. {
  91. return m_data;
  92. }
  93. const JS::Uint8ClampedArray* ImageData::data() const
  94. {
  95. return m_data;
  96. }
  97. }