ImageData.cpp 4.8 KB

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