2020-05-12 17:22:45 -03:00
|
|
|
/*
|
2021-06-11 18:06:46 -03:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-05-12 17:22:45 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-12 17:22:45 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Layer.h"
|
2020-05-25 17:49:50 -03:00
|
|
|
#include "Image.h"
|
2021-06-14 12:59:15 -03:00
|
|
|
#include "Selection.h"
|
2020-05-12 17:22:45 -03:00
|
|
|
#include <LibGfx/Bitmap.h>
|
|
|
|
|
|
2020-05-20 15:35:35 -03:00
|
|
|
namespace PixelPaint {
|
2020-05-12 17:22:45 -03:00
|
|
|
|
2021-06-12 04:45:07 -03:00
|
|
|
RefPtr<Layer> Layer::try_create_with_size(Image& image, Gfx::IntSize const& size, String name)
|
2020-05-12 17:22:45 -03:00
|
|
|
{
|
|
|
|
|
if (size.is_empty())
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
if (size.width() > 16384 || size.height() > 16384)
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
2021-07-21 13:02:15 -03:00
|
|
|
auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, size);
|
2021-06-12 05:24:04 -03:00
|
|
|
if (!bitmap)
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
return adopt_ref(*new Layer(image, *bitmap, move(name)));
|
2020-05-12 17:22:45 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 05:24:04 -03:00
|
|
|
RefPtr<Layer> Layer::try_create_with_bitmap(Image& image, NonnullRefPtr<Gfx::Bitmap> bitmap, String name)
|
2020-09-05 11:53:51 -03:00
|
|
|
{
|
2021-06-12 05:24:04 -03:00
|
|
|
if (bitmap->size().is_empty())
|
2020-09-05 11:53:51 -03:00
|
|
|
return nullptr;
|
|
|
|
|
|
2021-06-12 05:24:04 -03:00
|
|
|
if (bitmap->size().width() > 16384 || bitmap->size().height() > 16384)
|
2020-09-05 11:53:51 -03:00
|
|
|
return nullptr;
|
|
|
|
|
|
2021-06-12 04:45:07 -03:00
|
|
|
return adopt_ref(*new Layer(image, bitmap, move(name)));
|
2020-09-05 11:53:51 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-11 18:06:46 -03:00
|
|
|
RefPtr<Layer> Layer::try_create_snapshot(Image& image, Layer const& layer)
|
2020-10-17 13:47:34 -03:00
|
|
|
{
|
2021-06-11 18:06:46 -03:00
|
|
|
auto snapshot = try_create_with_bitmap(image, *layer.bitmap().clone(), layer.name());
|
2021-04-04 06:05:43 -03:00
|
|
|
/*
|
|
|
|
|
We set these properties directly because calling the setters might
|
|
|
|
|
notify the image of an update on the newly created layer, but this
|
|
|
|
|
layer has not yet been added to the image.
|
|
|
|
|
*/
|
|
|
|
|
snapshot->m_opacity_percent = layer.opacity_percent();
|
|
|
|
|
snapshot->m_visible = layer.is_visible();
|
|
|
|
|
|
2020-10-17 13:47:34 -03:00
|
|
|
snapshot->set_selected(layer.is_selected());
|
|
|
|
|
snapshot->set_location(layer.location());
|
2021-04-04 06:05:43 -03:00
|
|
|
|
2020-10-17 13:47:34 -03:00
|
|
|
return snapshot;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-12 05:24:04 -03:00
|
|
|
Layer::Layer(Image& image, NonnullRefPtr<Gfx::Bitmap> bitmap, String name)
|
2020-09-05 11:53:51 -03:00
|
|
|
: m_image(image)
|
2021-06-12 04:45:07 -03:00
|
|
|
, m_name(move(name))
|
2021-06-12 05:24:04 -03:00
|
|
|
, m_bitmap(move(bitmap))
|
2020-09-05 11:53:51 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-06 15:35:19 -03:00
|
|
|
void Layer::did_modify_bitmap(Gfx::IntRect const& rect)
|
2020-05-25 17:49:50 -03:00
|
|
|
{
|
2021-07-06 15:35:19 -03:00
|
|
|
m_image.layer_did_modify_bitmap({}, *this, rect);
|
2020-05-25 17:49:50 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-23 15:33:38 -03:00
|
|
|
void Layer::set_visible(bool visible)
|
|
|
|
|
{
|
|
|
|
|
if (m_visible == visible)
|
|
|
|
|
return;
|
|
|
|
|
m_visible = visible;
|
|
|
|
|
m_image.layer_did_modify_properties({}, *this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Layer::set_opacity_percent(int opacity_percent)
|
|
|
|
|
{
|
|
|
|
|
if (m_opacity_percent == opacity_percent)
|
|
|
|
|
return;
|
|
|
|
|
m_opacity_percent = opacity_percent;
|
|
|
|
|
m_image.layer_did_modify_properties({}, *this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-12 04:45:07 -03:00
|
|
|
void Layer::set_name(String name)
|
2020-07-23 15:48:28 -03:00
|
|
|
{
|
|
|
|
|
if (m_name == name)
|
|
|
|
|
return;
|
2021-06-12 04:45:07 -03:00
|
|
|
m_name = move(name);
|
2020-07-23 15:48:28 -03:00
|
|
|
m_image.layer_did_modify_properties({}, *this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 12:59:15 -03:00
|
|
|
RefPtr<Gfx::Bitmap> Layer::try_copy_bitmap(Selection const& selection) const
|
|
|
|
|
{
|
2021-07-14 20:43:11 -03:00
|
|
|
if (selection.is_empty()) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2021-06-20 10:59:21 -03:00
|
|
|
auto selection_rect = selection.bounding_rect();
|
|
|
|
|
|
2021-07-21 13:02:15 -03:00
|
|
|
auto result = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, selection_rect.size());
|
2021-06-20 10:59:21 -03:00
|
|
|
VERIFY(result->has_alpha_channel());
|
|
|
|
|
|
|
|
|
|
for (int y = selection_rect.top(); y <= selection_rect.bottom(); y++) {
|
|
|
|
|
for (int x = selection_rect.left(); x <= selection_rect.right(); x++) {
|
|
|
|
|
|
|
|
|
|
Gfx::IntPoint image_point { x, y };
|
|
|
|
|
auto layer_point = image_point - m_location;
|
|
|
|
|
auto result_point = image_point - selection_rect.top_left();
|
|
|
|
|
|
|
|
|
|
if (!m_bitmap->physical_rect().contains(layer_point)) {
|
|
|
|
|
result->set_pixel(result_point, Gfx::Color::Transparent);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto pixel = m_bitmap->get_pixel(layer_point);
|
|
|
|
|
|
|
|
|
|
// Widen to int before multiplying to avoid overflow issues
|
|
|
|
|
auto pixel_alpha = static_cast<int>(pixel.alpha());
|
|
|
|
|
auto selection_alpha = static_cast<int>(selection.get_selection_alpha(image_point));
|
|
|
|
|
auto new_alpha = (pixel_alpha * selection_alpha) / 0xFF;
|
|
|
|
|
pixel.set_alpha(static_cast<u8>(clamp(new_alpha, 0, 0xFF)));
|
|
|
|
|
|
|
|
|
|
result->set_pixel(result_point, pixel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2021-06-14 12:59:15 -03:00
|
|
|
}
|
|
|
|
|
|
2020-05-12 17:22:45 -03:00
|
|
|
}
|