2020-04-21 23:49:51 +02:00
|
|
|
/*
|
2022-09-02 23:07:05 +02:00
|
|
|
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
|
2020-04-21 23:49:51 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-21 23:49:51 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibGfx/Bitmap.h>
|
2021-05-22 23:33:26 +04:30
|
|
|
#include <LibJS/Runtime/TypedArray.h>
|
2020-07-26 15:08:16 +02:00
|
|
|
#include <LibWeb/HTML/ImageData.h>
|
2022-09-02 23:07:05 +02:00
|
|
|
#include <LibWeb/HTML/Window.h>
|
2020-04-21 23:49:51 +02:00
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
namespace Web::HTML {
|
2020-04-21 23:49:51 +02:00
|
|
|
|
2022-09-02 23:07:05 +02:00
|
|
|
JS::GCPtr<ImageData> ImageData::create_with_size(HTML::Window& window, int width, int height)
|
2020-04-21 23:49:51 +02:00
|
|
|
{
|
2022-09-02 23:07:05 +02:00
|
|
|
auto& realm = window.realm();
|
2022-08-16 00:20:49 +01:00
|
|
|
|
2020-04-21 23:49:51 +02:00
|
|
|
if (width <= 0 || height <= 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (width > 16384 || height > 16384)
|
|
|
|
return nullptr;
|
|
|
|
|
2022-08-16 00:20:49 +01:00
|
|
|
auto data_or_error = JS::Uint8ClampedArray::create(realm, width * height * 4);
|
2022-02-07 13:34:32 +01:00
|
|
|
if (data_or_error.is_error())
|
2020-04-21 23:49:51 +02:00
|
|
|
return nullptr;
|
2022-09-02 23:07:05 +02:00
|
|
|
auto data = JS::NonnullGCPtr<JS::Uint8ClampedArray>(*data_or_error.release_value());
|
2020-04-21 23:49:51 +02:00
|
|
|
|
2021-11-06 11:38:14 +01:00
|
|
|
auto bitmap_or_error = Gfx::Bitmap::try_create_wrapper(Gfx::BitmapFormat::RGBA8888, Gfx::IntSize(width, height), 1, width * sizeof(u32), data->data().data());
|
|
|
|
if (bitmap_or_error.is_error())
|
2020-04-21 23:49:51 +02:00
|
|
|
return nullptr;
|
2022-09-02 23:07:05 +02:00
|
|
|
return realm.heap().allocate<ImageData>(realm, window, bitmap_or_error.release_value(), move(data));
|
2020-04-21 23:49:51 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 23:07:05 +02:00
|
|
|
ImageData::ImageData(HTML::Window& window, NonnullRefPtr<Gfx::Bitmap> bitmap, JS::NonnullGCPtr<JS::Uint8ClampedArray> data)
|
|
|
|
: PlatformObject(window.realm())
|
|
|
|
, m_bitmap(move(bitmap))
|
2020-04-21 23:49:51 +02:00
|
|
|
, m_data(move(data))
|
|
|
|
{
|
2022-09-03 18:43:24 +02:00
|
|
|
set_prototype(&window.cached_web_prototype("ImageData"));
|
2020-04-21 23:49:51 +02:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
ImageData::~ImageData() = default;
|
2020-04-21 23:49:51 +02:00
|
|
|
|
2022-09-02 23:07:05 +02:00
|
|
|
void ImageData::visit_edges(Cell::Visitor& visitor)
|
|
|
|
{
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
visitor.visit(m_data.ptr());
|
|
|
|
}
|
|
|
|
|
2020-06-21 15:57:10 +02:00
|
|
|
unsigned ImageData::width() const
|
2020-04-21 23:49:51 +02:00
|
|
|
{
|
|
|
|
return m_bitmap->width();
|
|
|
|
}
|
|
|
|
|
2020-06-21 15:57:10 +02:00
|
|
|
unsigned ImageData::height() const
|
2020-04-21 23:49:51 +02:00
|
|
|
{
|
|
|
|
return m_bitmap->height();
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Uint8ClampedArray* ImageData::data()
|
|
|
|
{
|
2022-09-02 23:07:05 +02:00
|
|
|
return m_data;
|
2020-04-21 23:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const JS::Uint8ClampedArray* ImageData::data() const
|
|
|
|
{
|
2022-09-02 23:07:05 +02:00
|
|
|
return m_data;
|
2020-04-21 23:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|