ImageData.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.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. GC_DEFINE_ALLOCATOR(ImageData);
  17. // https://html.spec.whatwg.org/multipage/canvas.html#dom-imagedata
  18. WebIDL::ExceptionOr<GC::Ref<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."_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. //
  27. // If the Canvas Pixel ArrayBuffer cannot be allocated, then rethrow the RangeError thrown by JavaScript, and return.
  28. Checked<u32> size = sw;
  29. size *= sh;
  30. size *= sizeof(u32);
  31. if (size.has_overflow())
  32. return WebIDL::IndexSizeError::create(realm, "The specified image size could not created"_string);
  33. auto data = TRY(JS::Uint8ClampedArray::create(realm, size.value()));
  34. 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()));
  35. return realm.create<ImageData>(realm, bitmap, data);
  36. }
  37. WebIDL::ExceptionOr<GC::Ref<ImageData>> ImageData::construct_impl(JS::Realm& realm, u32 sw, u32 sh, Optional<ImageDataSettings> const& settings)
  38. {
  39. return ImageData::create(realm, sw, sh, settings);
  40. }
  41. // https://html.spec.whatwg.org/multipage/canvas.html#dom-imagedata-with-data
  42. WebIDL::ExceptionOr<GC::Ref<ImageData>> ImageData::create(JS::Realm& realm, GC::Root<WebIDL::BufferSource> const& data, u32 sw, Optional<u32> sh, Optional<ImageDataSettings> const&)
  43. {
  44. auto& vm = realm.vm();
  45. if (!is<JS::Uint8ClampedArray>(*data->raw_object()))
  46. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Uint8ClampedArray");
  47. auto& uint8_clamped_array_data = static_cast<JS::Uint8ClampedArray&>(*data->raw_object());
  48. // 1. Let length be the number of bytes in data.
  49. auto length = uint8_clamped_array_data.byte_length().length();
  50. // 2. If length is not a nonzero integral multiple of four, then throw an "InvalidStateError" DOMException.
  51. if (length == 0 || length % 4 != 0)
  52. return WebIDL::InvalidStateError::create(realm, "Source data must have a non-sero length that is a multiple of four."_string);
  53. // 3. Let length be length divided by four.
  54. length = length / 4;
  55. // 4. If length is not an integral multiple of sw, then throw an "IndexSizeError" DOMException.
  56. // NOTE: At this step, the length is guaranteed to be greater than zero (otherwise the second step above would have aborted the steps),
  57. // so if sw is zero, this step will throw the exception and return.
  58. if (sw == 0 || length % sw != 0)
  59. return WebIDL::IndexSizeError::create(realm, "Source width must be a multiple of source data's length."_string);
  60. // 5. Let height be length divided by sw.
  61. auto height = length / sw;
  62. // 6. If sh was given and its value is not equal to height, then throw an "IndexSizeError" DOMException.
  63. if (sh.has_value() && sh.value() != height)
  64. return WebIDL::IndexSizeError::create(realm, "Source height must be equal to the calculated height of the data."_string);
  65. // 7. Initialize this given sw, sh, settings set to settings, and source set to data.
  66. auto bitmap = TRY_OR_THROW_OOM(vm, Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::RGBA8888, Gfx::AlphaType::Unpremultiplied, Gfx::IntSize(sw, height), sw * sizeof(u32), uint8_clamped_array_data.data().data()));
  67. return realm.create<ImageData>(realm, bitmap, uint8_clamped_array_data);
  68. }
  69. 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)
  70. {
  71. return ImageData::create(realm, data, sw, move(sh), settings);
  72. }
  73. ImageData::ImageData(JS::Realm& realm, NonnullRefPtr<Gfx::Bitmap> bitmap, GC::Ref<JS::Uint8ClampedArray> data)
  74. : PlatformObject(realm)
  75. , m_bitmap(move(bitmap))
  76. , m_data(move(data))
  77. {
  78. }
  79. ImageData::~ImageData() = default;
  80. void ImageData::initialize(JS::Realm& realm)
  81. {
  82. Base::initialize(realm);
  83. WEB_SET_PROTOTYPE_FOR_INTERFACE(ImageData);
  84. }
  85. void ImageData::visit_edges(Cell::Visitor& visitor)
  86. {
  87. Base::visit_edges(visitor);
  88. visitor.visit(m_data);
  89. }
  90. unsigned ImageData::width() const
  91. {
  92. return m_bitmap->width();
  93. }
  94. unsigned ImageData::height() const
  95. {
  96. return m_bitmap->height();
  97. }
  98. JS::Uint8ClampedArray* ImageData::data()
  99. {
  100. return m_data;
  101. }
  102. const JS::Uint8ClampedArray* ImageData::data() const
  103. {
  104. return m_data;
  105. }
  106. }