ImageData.cpp 4.9 KB

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