2020-04-21 23:49:51 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
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>
|
|
|
|
#include <LibJS/Runtime/Uint8ClampedArray.h>
|
2020-07-26 15:08:16 +02:00
|
|
|
#include <LibWeb/HTML/ImageData.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
|
|
|
|
|
|
|
RefPtr<ImageData> ImageData::create_with_size(JS::GlobalObject& global_object, int width, int height)
|
|
|
|
{
|
|
|
|
if (width <= 0 || height <= 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (width > 16384 || height > 16384)
|
|
|
|
return nullptr;
|
|
|
|
|
2021-01-09 14:02:45 +01:00
|
|
|
dbgln("Creating ImageData with {}x{}", width, height);
|
2020-04-21 23:49:51 +02:00
|
|
|
|
|
|
|
auto* data = JS::Uint8ClampedArray::create(global_object, width * height * 4);
|
|
|
|
if (!data)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto data_handle = JS::make_handle(data);
|
|
|
|
|
2021-03-16 12:10:31 +01:00
|
|
|
auto bitmap = Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::RGBA8888, Gfx::IntSize(width, height), 1, width * sizeof(u32), (u32*)data->data());
|
2020-04-21 23:49:51 +02:00
|
|
|
if (!bitmap)
|
|
|
|
return nullptr;
|
|
|
|
return adopt(*new ImageData(bitmap.release_nonnull(), move(data_handle)));
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageData::ImageData(NonnullRefPtr<Gfx::Bitmap> bitmap, JS::Handle<JS::Uint8ClampedArray> data)
|
|
|
|
: m_bitmap(move(bitmap))
|
|
|
|
, m_data(move(data))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageData::~ImageData()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
return m_data.cell();
|
|
|
|
}
|
|
|
|
|
|
|
|
const JS::Uint8ClampedArray* ImageData::data() const
|
|
|
|
{
|
|
|
|
return m_data.cell();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|