mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:47:35 +00:00
PixelPaint: Cleanup the Image class
This patch just introduces some general cleanup regarding unused imports and adding the const qualifier to eligible functions and variables.
This commit is contained in:
parent
e89c649be1
commit
bd65ecf05c
4 changed files with 35 additions and 38 deletions
|
@ -11,9 +11,6 @@
|
||||||
#include "Selection.h"
|
#include "Selection.h"
|
||||||
#include <AK/Base64.h>
|
#include <AK/Base64.h>
|
||||||
#include <AK/JsonObject.h>
|
#include <AK/JsonObject.h>
|
||||||
#include <AK/JsonObjectSerializer.h>
|
|
||||||
#include <AK/JsonValue.h>
|
|
||||||
#include <AK/StringBuilder.h>
|
|
||||||
#include <LibGUI/Painter.h>
|
#include <LibGUI/Painter.h>
|
||||||
#include <LibGfx/BMPWriter.h>
|
#include <LibGfx/BMPWriter.h>
|
||||||
#include <LibGfx/Bitmap.h>
|
#include <LibGfx/Bitmap.h>
|
||||||
|
@ -45,7 +42,7 @@ void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect) con
|
||||||
float scale = (float)dest_rect.width() / (float)rect().width();
|
float scale = (float)dest_rect.width() / (float)rect().width();
|
||||||
Gfx::PainterStateSaver saver(painter);
|
Gfx::PainterStateSaver saver(painter);
|
||||||
painter.add_clip_rect(dest_rect);
|
painter.add_clip_rect(dest_rect);
|
||||||
for (auto& layer : m_layers) {
|
for (auto const& layer : m_layers) {
|
||||||
if (!layer.is_visible())
|
if (!layer.is_visible())
|
||||||
continue;
|
continue;
|
||||||
auto target = dest_rect.translated(layer.location().x() * scale, layer.location().y() * scale);
|
auto target = dest_rect.translated(layer.location().x() * scale, layer.location().y() * scale);
|
||||||
|
@ -75,7 +72,7 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::try_decode_bitmap(ReadonlyBytes bitma
|
||||||
return decoded_bitmap.release_nonnull();
|
return decoded_bitmap.release_nonnull();
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<Image>> Image::try_create_from_bitmap(NonnullRefPtr<Gfx::Bitmap> bitmap)
|
ErrorOr<NonnullRefPtr<Image>> Image::try_create_from_bitmap(NonnullRefPtr<Gfx::Bitmap> const& bitmap)
|
||||||
{
|
{
|
||||||
auto image = TRY(try_create_with_size({ bitmap->width(), bitmap->height() }));
|
auto image = TRY(try_create_with_size({ bitmap->width(), bitmap->height() }));
|
||||||
auto layer = TRY(Layer::try_create_with_bitmap(*image, *bitmap, "Background"));
|
auto layer = TRY(Layer::try_create_with_bitmap(*image, *bitmap, "Background"));
|
||||||
|
@ -88,8 +85,8 @@ ErrorOr<NonnullRefPtr<Image>> Image::try_create_from_pixel_paint_json(JsonObject
|
||||||
auto image = TRY(try_create_with_size({ json.get("width"sv).to_i32(), json.get("height"sv).to_i32() }));
|
auto image = TRY(try_create_with_size({ json.get("width"sv).to_i32(), json.get("height"sv).to_i32() }));
|
||||||
|
|
||||||
auto layers_value = json.get("layers"sv);
|
auto layers_value = json.get("layers"sv);
|
||||||
for (auto& layer_value : layers_value.as_array().values()) {
|
for (auto const& layer_value : layers_value.as_array().values()) {
|
||||||
auto& layer_object = layer_value.as_object();
|
auto const& layer_object = layer_value.as_object();
|
||||||
auto name = layer_object.get("name"sv).as_string();
|
auto name = layer_object.get("name"sv).as_string();
|
||||||
|
|
||||||
auto bitmap_base64_encoded = layer_object.get("bitmap"sv).as_string();
|
auto bitmap_base64_encoded = layer_object.get("bitmap"sv).as_string();
|
||||||
|
@ -97,7 +94,7 @@ ErrorOr<NonnullRefPtr<Image>> Image::try_create_from_pixel_paint_json(JsonObject
|
||||||
auto bitmap = TRY(try_decode_bitmap(bitmap_data));
|
auto bitmap = TRY(try_decode_bitmap(bitmap_data));
|
||||||
auto layer = TRY(Layer::try_create_with_bitmap(*image, move(bitmap), name));
|
auto layer = TRY(Layer::try_create_with_bitmap(*image, move(bitmap), name));
|
||||||
|
|
||||||
if (auto mask_object = layer_object.get("mask"sv); !mask_object.is_null()) {
|
if (auto const& mask_object = layer_object.get("mask"sv); !mask_object.is_null()) {
|
||||||
auto mask_base64_encoded = mask_object.as_string();
|
auto mask_base64_encoded = mask_object.as_string();
|
||||||
auto mask_data = TRY(decode_base64(mask_base64_encoded));
|
auto mask_data = TRY(decode_base64(mask_base64_encoded));
|
||||||
auto mask = TRY(try_decode_bitmap(mask_data));
|
auto mask = TRY(try_decode_bitmap(mask_data));
|
||||||
|
@ -174,7 +171,7 @@ RefPtr<Gfx::Bitmap> Image::try_copy_bitmap(Selection const& selection) const
|
||||||
return cropped_bitmap_or_error.release_value_but_fixme_should_propagate_errors();
|
return cropped_bitmap_or_error.release_value_but_fixme_should_propagate_errors();
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<void> Image::export_bmp_to_file(Core::File& file, bool preserve_alpha_channel)
|
ErrorOr<void> Image::export_bmp_to_file(Core::File& file, bool preserve_alpha_channel) const
|
||||||
{
|
{
|
||||||
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
|
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
|
||||||
auto bitmap = TRY(try_compose_bitmap(bitmap_format));
|
auto bitmap = TRY(try_compose_bitmap(bitmap_format));
|
||||||
|
@ -188,7 +185,7 @@ ErrorOr<void> Image::export_bmp_to_file(Core::File& file, bool preserve_alpha_ch
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<void> Image::export_png_to_file(Core::File& file, bool preserve_alpha_channel)
|
ErrorOr<void> Image::export_png_to_file(Core::File& file, bool preserve_alpha_channel) const
|
||||||
{
|
{
|
||||||
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
|
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
|
||||||
auto bitmap = TRY(try_compose_bitmap(bitmap_format));
|
auto bitmap = TRY(try_compose_bitmap(bitmap_format));
|
||||||
|
@ -507,14 +504,14 @@ ErrorOr<void> Image::flip(Gfx::Orientation orientation)
|
||||||
if (layer.is_selected())
|
if (layer.is_selected())
|
||||||
selected_layer_index = i;
|
selected_layer_index = i;
|
||||||
|
|
||||||
TRY(new_layer->flip(orientation, Layer::NotifyClients::NO));
|
TRY(new_layer->flip(orientation, Layer::NotifyClients::No));
|
||||||
|
|
||||||
flipped_layers.append(new_layer);
|
flipped_layers.unchecked_append(new_layer);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_layers = move(flipped_layers);
|
m_layers = move(flipped_layers);
|
||||||
for (auto& layer : m_layers)
|
for (auto& layer : m_layers)
|
||||||
layer.did_modify_bitmap({}, Layer::NotifyClients::YES);
|
layer.did_modify_bitmap({}, Layer::NotifyClients::No);
|
||||||
|
|
||||||
select_layer(&m_layers[selected_layer_index]);
|
select_layer(&m_layers[selected_layer_index]);
|
||||||
|
|
||||||
|
@ -538,14 +535,14 @@ ErrorOr<void> Image::rotate(Gfx::RotationDirection direction)
|
||||||
if (layer.is_selected())
|
if (layer.is_selected())
|
||||||
selected_layer_index = i;
|
selected_layer_index = i;
|
||||||
|
|
||||||
TRY(new_layer->rotate(direction, Layer::NotifyClients::NO));
|
TRY(new_layer->rotate(direction, Layer::NotifyClients::No));
|
||||||
|
|
||||||
rotated_layers.append(new_layer);
|
rotated_layers.unchecked_append(new_layer);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_layers = move(rotated_layers);
|
m_layers = move(rotated_layers);
|
||||||
for (auto& layer : m_layers)
|
for (auto& layer : m_layers)
|
||||||
layer.did_modify_bitmap({}, Layer::NotifyClients::YES);
|
layer.did_modify_bitmap({}, Layer::NotifyClients::Yes);
|
||||||
|
|
||||||
select_layer(&m_layers[selected_layer_index]);
|
select_layer(&m_layers[selected_layer_index]);
|
||||||
|
|
||||||
|
@ -572,19 +569,19 @@ ErrorOr<void> Image::crop(Gfx::IntRect const& cropped_rect)
|
||||||
|
|
||||||
auto layer_location = new_layer->location();
|
auto layer_location = new_layer->location();
|
||||||
auto layer_local_crop_rect = new_layer->relative_rect().intersected(cropped_rect).translated(-layer_location.x(), -layer_location.y());
|
auto layer_local_crop_rect = new_layer->relative_rect().intersected(cropped_rect).translated(-layer_location.x(), -layer_location.y());
|
||||||
TRY(new_layer->crop(layer_local_crop_rect, Layer::NotifyClients::NO));
|
TRY(new_layer->crop(layer_local_crop_rect, Layer::NotifyClients::No));
|
||||||
|
|
||||||
auto new_layer_x = max(0, layer_location.x() - cropped_rect.x());
|
auto new_layer_x = max(0, layer_location.x() - cropped_rect.x());
|
||||||
auto new_layer_y = max(0, layer_location.y() - cropped_rect.y());
|
auto new_layer_y = max(0, layer_location.y() - cropped_rect.y());
|
||||||
|
|
||||||
new_layer->set_location({ new_layer_x, new_layer_y });
|
new_layer->set_location({ new_layer_x, new_layer_y });
|
||||||
|
|
||||||
cropped_layers.append(new_layer);
|
cropped_layers.unchecked_append(new_layer);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_layers = move(cropped_layers);
|
m_layers = move(cropped_layers);
|
||||||
for (auto& layer : m_layers)
|
for (auto& layer : m_layers)
|
||||||
layer.did_modify_bitmap({}, Layer::NotifyClients::YES);
|
layer.did_modify_bitmap({}, Layer::NotifyClients::Yes);
|
||||||
|
|
||||||
select_layer(&m_layers[selected_layer_index]);
|
select_layer(&m_layers[selected_layer_index]);
|
||||||
|
|
||||||
|
@ -600,7 +597,7 @@ Optional<Gfx::IntRect> Image::nonempty_content_bounding_rect() const
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
Optional<Gfx::IntRect> bounding_rect;
|
Optional<Gfx::IntRect> bounding_rect;
|
||||||
for (auto& layer : m_layers) {
|
for (auto const& layer : m_layers) {
|
||||||
auto layer_content_rect_in_layer_coordinates = layer.nonempty_content_bounding_rect();
|
auto layer_content_rect_in_layer_coordinates = layer.nonempty_content_bounding_rect();
|
||||||
if (!layer_content_rect_in_layer_coordinates.has_value())
|
if (!layer_content_rect_in_layer_coordinates.has_value())
|
||||||
continue;
|
continue;
|
||||||
|
@ -641,14 +638,14 @@ ErrorOr<void> Image::resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode sca
|
||||||
selected_layer_index = i;
|
selected_layer_index = i;
|
||||||
|
|
||||||
Gfx::IntPoint new_location(scale_x * new_layer->location().x(), scale_y * new_layer->location().y());
|
Gfx::IntPoint new_location(scale_x * new_layer->location().x(), scale_y * new_layer->location().y());
|
||||||
TRY(new_layer->resize(new_size, new_location, scaling_mode, Layer::NotifyClients::NO));
|
TRY(new_layer->resize(new_size, new_location, scaling_mode, Layer::NotifyClients::No));
|
||||||
|
|
||||||
resized_layers.append(new_layer);
|
resized_layers.unchecked_append(new_layer);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_layers = move(resized_layers);
|
m_layers = move(resized_layers);
|
||||||
for (auto& layer : m_layers)
|
for (auto& layer : m_layers)
|
||||||
layer.did_modify_bitmap({}, Layer::NotifyClients::YES);
|
layer.did_modify_bitmap({}, Layer::NotifyClients::Yes);
|
||||||
|
|
||||||
select_layer(&m_layers[selected_layer_index]);
|
select_layer(&m_layers[selected_layer_index]);
|
||||||
|
|
||||||
|
@ -661,7 +658,7 @@ ErrorOr<void> Image::resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode sca
|
||||||
Color Image::color_at(Gfx::IntPoint point) const
|
Color Image::color_at(Gfx::IntPoint point) const
|
||||||
{
|
{
|
||||||
Color color;
|
Color color;
|
||||||
for (auto& layer : m_layers) {
|
for (auto const& layer : m_layers) {
|
||||||
if (!layer.is_visible() || !layer.rect().contains(point))
|
if (!layer.is_visible() || !layer.rect().contains(point))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,7 @@ class Image : public RefCounted<Image> {
|
||||||
public:
|
public:
|
||||||
static ErrorOr<NonnullRefPtr<Image>> try_create_with_size(Gfx::IntSize);
|
static ErrorOr<NonnullRefPtr<Image>> try_create_with_size(Gfx::IntSize);
|
||||||
static ErrorOr<NonnullRefPtr<Image>> try_create_from_pixel_paint_json(JsonObject const&);
|
static ErrorOr<NonnullRefPtr<Image>> try_create_from_pixel_paint_json(JsonObject const&);
|
||||||
static ErrorOr<NonnullRefPtr<Image>> try_create_from_bitmap(NonnullRefPtr<Gfx::Bitmap>);
|
static ErrorOr<NonnullRefPtr<Image>> try_create_from_bitmap(NonnullRefPtr<Gfx::Bitmap> const&);
|
||||||
|
|
||||||
static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> try_decode_bitmap(ReadonlyBytes);
|
static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> try_decode_bitmap(ReadonlyBytes);
|
||||||
|
|
||||||
|
@ -73,8 +73,8 @@ public:
|
||||||
void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const;
|
void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect) const;
|
||||||
|
|
||||||
ErrorOr<void> serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const;
|
ErrorOr<void> serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const;
|
||||||
ErrorOr<void> export_bmp_to_file(Core::File&, bool preserve_alpha_channel);
|
ErrorOr<void> export_bmp_to_file(Core::File&, bool preserve_alpha_channel) const;
|
||||||
ErrorOr<void> export_png_to_file(Core::File&, bool preserve_alpha_channel);
|
ErrorOr<void> export_png_to_file(Core::File&, bool preserve_alpha_channel) const;
|
||||||
ErrorOr<void> export_qoi_to_file(Core::File&) const;
|
ErrorOr<void> export_qoi_to_file(Core::File&) const;
|
||||||
|
|
||||||
void move_layer_to_front(Layer&);
|
void move_layer_to_front(Layer&);
|
||||||
|
|
|
@ -83,7 +83,7 @@ void Layer::did_modify_bitmap(Gfx::IntRect const& rect, NotifyClients notify_cli
|
||||||
|
|
||||||
// NOTE: If NotifyClients::NO is passed to this function the caller should handle notifying
|
// NOTE: If NotifyClients::NO is passed to this function the caller should handle notifying
|
||||||
// the clients of any bitmap changes.
|
// the clients of any bitmap changes.
|
||||||
if (notify_clients == NotifyClients::YES)
|
if (notify_clients == NotifyClients::Yes)
|
||||||
m_image.layer_did_modify_bitmap({}, *this, rect);
|
m_image.layer_did_modify_bitmap({}, *this, rect);
|
||||||
update_cached_bitmap();
|
update_cached_bitmap();
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,23 +56,23 @@ public:
|
||||||
DeprecatedString const& name() const { return m_name; }
|
DeprecatedString const& name() const { return m_name; }
|
||||||
void set_name(DeprecatedString);
|
void set_name(DeprecatedString);
|
||||||
|
|
||||||
enum NotifyClients {
|
enum class NotifyClients {
|
||||||
YES,
|
Yes,
|
||||||
NO
|
No
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorOr<void> flip(Gfx::Orientation orientation, NotifyClients notify_clients = NotifyClients::YES);
|
ErrorOr<void> flip(Gfx::Orientation orientation, NotifyClients notify_clients = NotifyClients::Yes);
|
||||||
ErrorOr<void> rotate(Gfx::RotationDirection direction, NotifyClients notify_clients = NotifyClients::YES);
|
ErrorOr<void> rotate(Gfx::RotationDirection direction, NotifyClients notify_clients = NotifyClients::Yes);
|
||||||
ErrorOr<void> crop(Gfx::IntRect const& rect, NotifyClients notify_clients = NotifyClients::YES);
|
ErrorOr<void> crop(Gfx::IntRect const& rect, NotifyClients notify_clients = NotifyClients::Yes);
|
||||||
ErrorOr<void> resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode scaling_mode, NotifyClients notify_clients = NotifyClients::YES);
|
ErrorOr<void> resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode scaling_mode, NotifyClients notify_clients = NotifyClients::Yes);
|
||||||
ErrorOr<void> resize(Gfx::IntRect const& new_rect, Gfx::Painter::ScalingMode scaling_mode, NotifyClients notify_clients = NotifyClients::YES);
|
ErrorOr<void> resize(Gfx::IntRect const& new_rect, Gfx::Painter::ScalingMode scaling_mode, NotifyClients notify_clients = NotifyClients::Yes);
|
||||||
ErrorOr<void> resize(Gfx::IntSize new_size, Gfx::IntPoint new_location, Gfx::Painter::ScalingMode scaling_mode, NotifyClients notify_clients = NotifyClients::YES);
|
ErrorOr<void> resize(Gfx::IntSize new_size, Gfx::IntPoint new_location, Gfx::Painter::ScalingMode scaling_mode, NotifyClients notify_clients = NotifyClients::Yes);
|
||||||
|
|
||||||
Optional<Gfx::IntRect> nonempty_content_bounding_rect() const;
|
Optional<Gfx::IntRect> nonempty_content_bounding_rect() const;
|
||||||
|
|
||||||
ErrorOr<void> try_set_bitmaps(NonnullRefPtr<Gfx::Bitmap> content, RefPtr<Gfx::Bitmap> mask);
|
ErrorOr<void> try_set_bitmaps(NonnullRefPtr<Gfx::Bitmap> content, RefPtr<Gfx::Bitmap> mask);
|
||||||
|
|
||||||
void did_modify_bitmap(Gfx::IntRect const& = {}, NotifyClients notify_clients = NotifyClients::YES);
|
void did_modify_bitmap(Gfx::IntRect const& = {}, NotifyClients notify_clients = NotifyClients::Yes);
|
||||||
|
|
||||||
void set_selected(bool selected) { m_selected = selected; }
|
void set_selected(bool selected) { m_selected = selected; }
|
||||||
bool is_selected() const { return m_selected; }
|
bool is_selected() const { return m_selected; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue