ImageData.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Bitmap.h>
  7. #include <LibJS/Runtime/TypedArray.h>
  8. #include <LibWeb/HTML/ImageData.h>
  9. namespace Web::HTML {
  10. RefPtr<ImageData> ImageData::create_with_size(JS::GlobalObject& global_object, int width, int height)
  11. {
  12. if (width <= 0 || height <= 0)
  13. return nullptr;
  14. if (width > 16384 || height > 16384)
  15. return nullptr;
  16. dbgln("Creating ImageData with {}x{}", width, height);
  17. auto* data = JS::Uint8ClampedArray::create(global_object, width * height * 4);
  18. if (!data)
  19. return nullptr;
  20. auto data_handle = JS::make_handle(data);
  21. auto bitmap = Gfx::Bitmap::try_create_wrapper(Gfx::BitmapFormat::RGBA8888, Gfx::IntSize(width, height), 1, width * sizeof(u32), data->data().data());
  22. if (!bitmap)
  23. return nullptr;
  24. return adopt_ref(*new ImageData(bitmap.release_nonnull(), move(data_handle)));
  25. }
  26. ImageData::ImageData(NonnullRefPtr<Gfx::Bitmap> bitmap, JS::Handle<JS::Uint8ClampedArray> data)
  27. : m_bitmap(move(bitmap))
  28. , m_data(move(data))
  29. {
  30. }
  31. ImageData::~ImageData()
  32. {
  33. }
  34. unsigned ImageData::width() const
  35. {
  36. return m_bitmap->width();
  37. }
  38. unsigned ImageData::height() const
  39. {
  40. return m_bitmap->height();
  41. }
  42. JS::Uint8ClampedArray* ImageData::data()
  43. {
  44. return m_data.cell();
  45. }
  46. const JS::Uint8ClampedArray* ImageData::data() const
  47. {
  48. return m_data.cell();
  49. }
  50. }