123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673 |
- /*
- * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
- * Copyright (c) 2021-2022, Mustafa Quraish <mustafa@serenityos.org>
- * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include "Image.h"
- #include "Layer.h"
- #include "Selection.h"
- #include <AK/Base64.h>
- #include <AK/JsonObject.h>
- #include <LibGUI/Painter.h>
- #include <LibGfx/BMPWriter.h>
- #include <LibGfx/Bitmap.h>
- #include <LibGfx/PNGWriter.h>
- #include <LibGfx/QOIWriter.h>
- #include <LibImageDecoderClient/Client.h>
- #include <stdio.h>
- namespace PixelPaint {
- ErrorOr<NonnullRefPtr<Image>> Image::try_create_with_size(Gfx::IntSize size)
- {
- VERIFY(!size.is_empty());
- if (size.width() > 16384 || size.height() > 16384)
- return Error::from_string_literal("Image size too large");
- return adopt_nonnull_ref_or_enomem(new (nothrow) Image(size));
- }
- Image::Image(Gfx::IntSize size)
- : m_size(size)
- , m_selection(*this)
- {
- }
- void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect) const
- {
- float scale = (float)dest_rect.width() / (float)rect().width();
- Gfx::PainterStateSaver saver(painter);
- painter.add_clip_rect(dest_rect);
- for (auto const& layer : m_layers) {
- if (!layer.is_visible())
- continue;
- auto target = dest_rect.translated(layer.location().x() * scale, layer.location().y() * scale);
- target.set_size(layer.size().width() * scale, layer.size().height() * scale);
- painter.draw_scaled_bitmap(target, layer.display_bitmap(), layer.rect(), (float)layer.opacity_percent() / 100.0f);
- }
- }
- ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::try_decode_bitmap(ReadonlyBytes bitmap_data)
- {
- // Spawn a new ImageDecoder service process and connect to it.
- auto client = TRY(ImageDecoderClient::Client::try_create());
- // FIXME: Find a way to avoid the memory copying here.
- auto maybe_decoded_image = client->decode_image(bitmap_data);
- if (!maybe_decoded_image.has_value())
- return Error::from_string_literal("Image decode failed");
- // FIXME: Support multi-frame images?
- auto decoded_image = maybe_decoded_image.release_value();
- if (decoded_image.frames.is_empty())
- return Error::from_string_literal("Image decode failed (no frames)");
- auto decoded_bitmap = decoded_image.frames.first().bitmap;
- if (decoded_bitmap.is_null())
- return Error::from_string_literal("Image decode failed (no bitmap for frame)");
- return decoded_bitmap.release_nonnull();
- }
- 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 layer = TRY(Layer::try_create_with_bitmap(*image, *bitmap, "Background"));
- image->add_layer(move(layer));
- return image;
- }
- ErrorOr<NonnullRefPtr<Image>> Image::try_create_from_pixel_paint_json(JsonObject const& json)
- {
- 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);
- for (auto const& layer_value : layers_value.as_array().values()) {
- auto const& layer_object = layer_value.as_object();
- auto name = layer_object.get("name"sv).as_string();
- auto bitmap_base64_encoded = layer_object.get("bitmap"sv).as_string();
- auto bitmap_data = TRY(decode_base64(bitmap_base64_encoded));
- auto bitmap = TRY(try_decode_bitmap(bitmap_data));
- auto layer = TRY(Layer::try_create_with_bitmap(*image, move(bitmap), name));
- if (auto const& mask_object = layer_object.get("mask"sv); !mask_object.is_null()) {
- auto mask_base64_encoded = mask_object.as_string();
- auto mask_data = TRY(decode_base64(mask_base64_encoded));
- auto mask = TRY(try_decode_bitmap(mask_data));
- TRY(layer->try_set_bitmaps(layer->content_bitmap(), mask));
- }
- auto width = layer_object.get("width"sv).to_i32();
- auto height = layer_object.get("height"sv).to_i32();
- if (width != layer->size().width() || height != layer->size().height())
- return Error::from_string_literal("Decoded layer bitmap has wrong size");
- image->add_layer(*layer);
- layer->set_location({ layer_object.get("locationx"sv).to_i32(), layer_object.get("locationy"sv).to_i32() });
- layer->set_opacity_percent(layer_object.get("opacity_percent"sv).to_i32());
- layer->set_visible(layer_object.get("visible"sv).as_bool());
- layer->set_selected(layer_object.get("selected"sv).as_bool());
- }
- return image;
- }
- ErrorOr<void> Image::serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const
- {
- TRY(json.add("width"sv, m_size.width()));
- TRY(json.add("height"sv, m_size.height()));
- {
- auto json_layers = TRY(json.add_array("layers"sv));
- for (auto const& layer : m_layers) {
- auto json_layer = TRY(json_layers.add_object());
- TRY(json_layer.add("width"sv, layer.size().width()));
- TRY(json_layer.add("height"sv, layer.size().height()));
- TRY(json_layer.add("name"sv, layer.name()));
- TRY(json_layer.add("locationx"sv, layer.location().x()));
- TRY(json_layer.add("locationy"sv, layer.location().y()));
- TRY(json_layer.add("opacity_percent"sv, layer.opacity_percent()));
- TRY(json_layer.add("visible"sv, layer.is_visible()));
- TRY(json_layer.add("selected"sv, layer.is_selected()));
- TRY(json_layer.add("bitmap"sv, TRY(encode_base64(TRY(Gfx::PNGWriter::encode(layer.content_bitmap()))))));
- if (layer.is_masked())
- TRY(json_layer.add("mask"sv, TRY(encode_base64(TRY(Gfx::PNGWriter::encode(*layer.mask_bitmap()))))));
- TRY(json_layer.finish());
- }
- TRY(json_layers.finish());
- }
- return {};
- }
- ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::try_compose_bitmap(Gfx::BitmapFormat format) const
- {
- auto bitmap = TRY(Gfx::Bitmap::try_create(format, m_size));
- GUI::Painter painter(bitmap);
- paint_into(painter, { 0, 0, m_size.width(), m_size.height() });
- return bitmap;
- }
- RefPtr<Gfx::Bitmap> Image::try_copy_bitmap(Selection const& selection) const
- {
- if (selection.is_empty())
- return {};
- auto selection_rect = selection.bounding_rect();
- // FIXME: Add a way to only compose a certain part of the image
- auto bitmap_or_error = try_compose_bitmap(Gfx::BitmapFormat::BGRA8888);
- if (bitmap_or_error.is_error())
- return {};
- auto full_bitmap = bitmap_or_error.release_value();
- auto cropped_bitmap_or_error = full_bitmap->cropped(selection_rect);
- if (cropped_bitmap_or_error.is_error())
- return nullptr;
- 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) const
- {
- auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
- auto bitmap = TRY(try_compose_bitmap(bitmap_format));
- Gfx::BMPWriter dumper;
- auto encoded_data = dumper.dump(bitmap);
- if (!file.write(encoded_data.data(), encoded_data.size()))
- return Error::from_errno(file.error());
- return {};
- }
- 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 = TRY(try_compose_bitmap(bitmap_format));
- auto encoded_data = TRY(Gfx::PNGWriter::encode(*bitmap));
- if (!file.write(encoded_data.data(), encoded_data.size()))
- return Error::from_errno(file.error());
- return {};
- }
- ErrorOr<void> Image::export_qoi_to_file(Core::File& file) const
- {
- auto bitmap = TRY(try_compose_bitmap(Gfx::BitmapFormat::BGRA8888));
- auto encoded_data = Gfx::QOIWriter::encode(bitmap);
- if (!file.write(encoded_data.data(), encoded_data.size()))
- return Error::from_errno(file.error());
- return {};
- }
- void Image::add_layer(NonnullRefPtr<Layer> layer)
- {
- for (auto& existing_layer : m_layers) {
- VERIFY(&existing_layer != layer.ptr());
- }
- m_layers.append(move(layer));
- for (auto* client : m_clients)
- client->image_did_add_layer(m_layers.size() - 1);
- did_modify_layer_stack();
- }
- ErrorOr<NonnullRefPtr<Image>> Image::take_snapshot() const
- {
- auto snapshot = TRY(try_create_with_size(m_size));
- for (auto const& layer : m_layers) {
- auto layer_snapshot = TRY(Layer::try_create_snapshot(*snapshot, layer));
- snapshot->add_layer(move(layer_snapshot));
- }
- snapshot->m_selection.set_mask(m_selection.mask());
- return snapshot;
- }
- ErrorOr<void> Image::restore_snapshot(Image const& snapshot)
- {
- m_layers.clear();
- select_layer(nullptr);
- bool layer_selected = false;
- for (auto const& snapshot_layer : snapshot.m_layers) {
- auto layer = TRY(Layer::try_create_snapshot(*this, snapshot_layer));
- if (layer->is_selected()) {
- select_layer(layer.ptr());
- layer_selected = true;
- }
- add_layer(*layer);
- }
- if (!layer_selected)
- select_layer(&layer(0));
- m_size = snapshot.size();
- m_selection.set_mask(snapshot.m_selection.mask());
- did_change_rect();
- did_modify_layer_stack();
- return {};
- }
- size_t Image::index_of(Layer const& layer) const
- {
- for (size_t i = 0; i < m_layers.size(); ++i) {
- if (&m_layers.at(i) == &layer)
- return i;
- }
- VERIFY_NOT_REACHED();
- }
- void Image::move_layer_to_back(Layer& layer)
- {
- NonnullRefPtr<Layer> protector(layer);
- auto index = index_of(layer);
- m_layers.remove(index);
- m_layers.prepend(layer);
- did_modify_layer_stack();
- }
- void Image::move_layer_to_front(Layer& layer)
- {
- NonnullRefPtr<Layer> protector(layer);
- auto index = index_of(layer);
- m_layers.remove(index);
- m_layers.append(layer);
- did_modify_layer_stack();
- }
- void Image::move_layer_down(Layer& layer)
- {
- NonnullRefPtr<Layer> protector(layer);
- auto index = index_of(layer);
- if (!index)
- return;
- m_layers.remove(index);
- m_layers.insert(index - 1, layer);
- did_modify_layer_stack();
- }
- void Image::move_layer_up(Layer& layer)
- {
- NonnullRefPtr<Layer> protector(layer);
- auto index = index_of(layer);
- if (index == m_layers.size() - 1)
- return;
- m_layers.remove(index);
- m_layers.insert(index + 1, layer);
- did_modify_layer_stack();
- }
- void Image::change_layer_index(size_t old_index, size_t new_index)
- {
- VERIFY(old_index < m_layers.size());
- VERIFY(new_index < m_layers.size());
- auto layer = m_layers.take(old_index);
- m_layers.insert(new_index, move(layer));
- did_modify_layer_stack();
- }
- void Image::did_modify_layer_stack()
- {
- for (auto* client : m_clients)
- client->image_did_modify_layer_stack();
- did_change();
- }
- void Image::remove_layer(Layer& layer)
- {
- NonnullRefPtr<Layer> protector(layer);
- auto index = index_of(layer);
- m_layers.remove(index);
- for (auto* client : m_clients)
- client->image_did_remove_layer(index);
- did_modify_layer_stack();
- }
- void Image::flatten_all_layers()
- {
- if (m_layers.size() < 2)
- return;
- auto& bottom_layer = m_layers.at(0);
- GUI::Painter painter(bottom_layer.content_bitmap());
- paint_into(painter, { 0, 0, m_size.width(), m_size.height() });
- for (size_t index = m_layers.size() - 1; index > 0; index--) {
- auto& layer = m_layers.at(index);
- remove_layer(layer);
- }
- bottom_layer.set_name("Background");
- select_layer(&bottom_layer);
- }
- void Image::merge_visible_layers()
- {
- if (m_layers.size() < 2)
- return;
- size_t index = 0;
- while (index < m_layers.size()) {
- if (m_layers.at(index).is_visible()) {
- auto& bottom_layer = m_layers.at(index);
- GUI::Painter painter(bottom_layer.content_bitmap());
- paint_into(painter, { 0, 0, m_size.width(), m_size.height() });
- select_layer(&bottom_layer);
- index++;
- break;
- }
- index++;
- }
- while (index < m_layers.size()) {
- if (m_layers.at(index).is_visible()) {
- auto& layer = m_layers.at(index);
- remove_layer(layer);
- } else {
- index++;
- }
- }
- }
- void Image::merge_active_layer_up(Layer& layer)
- {
- if (m_layers.size() < 2)
- return;
- size_t layer_index = this->index_of(layer);
- if ((layer_index + 1) == m_layers.size()) {
- dbgln("Cannot merge layer up: layer is already at the top");
- return; // FIXME: Notify user of error properly.
- }
- auto& layer_above = m_layers.at(layer_index + 1);
- GUI::Painter painter(layer_above.content_bitmap());
- painter.draw_scaled_bitmap(rect(), layer.display_bitmap(), layer.rect(), (float)layer.opacity_percent() / 100.0f);
- remove_layer(layer);
- select_layer(&layer_above);
- }
- void Image::merge_active_layer_down(Layer& layer)
- {
- if (m_layers.size() < 2)
- return;
- int layer_index = this->index_of(layer);
- if (layer_index == 0) {
- dbgln("Cannot merge layer down: layer is already at the bottom");
- return; // FIXME: Notify user of error properly.
- }
- auto& layer_below = m_layers.at(layer_index - 1);
- GUI::Painter painter(layer_below.content_bitmap());
- painter.draw_scaled_bitmap(rect(), layer.display_bitmap(), layer.rect(), (float)layer.opacity_percent() / 100.0f);
- remove_layer(layer);
- select_layer(&layer_below);
- }
- void Image::select_layer(Layer* layer)
- {
- for (auto* client : m_clients)
- client->image_select_layer(layer);
- }
- void Image::add_client(ImageClient& client)
- {
- VERIFY(!m_clients.contains(&client));
- m_clients.set(&client);
- }
- void Image::remove_client(ImageClient& client)
- {
- VERIFY(m_clients.contains(&client));
- m_clients.remove(&client);
- }
- void Image::layer_did_modify_bitmap(Badge<Layer>, Layer const& layer, Gfx::IntRect const& modified_layer_rect)
- {
- auto layer_index = index_of(layer);
- for (auto* client : m_clients)
- client->image_did_modify_layer_bitmap(layer_index);
- did_change(modified_layer_rect.translated(layer.location()));
- }
- void Image::layer_did_modify_properties(Badge<Layer>, Layer const& layer)
- {
- auto layer_index = index_of(layer);
- for (auto* client : m_clients)
- client->image_did_modify_layer_properties(layer_index);
- did_change();
- }
- void Image::did_change(Gfx::IntRect const& a_modified_rect)
- {
- auto modified_rect = a_modified_rect.is_empty() ? this->rect() : a_modified_rect;
- for (auto* client : m_clients)
- client->image_did_change(modified_rect);
- }
- void Image::did_change_rect(Gfx::IntRect const& a_modified_rect)
- {
- auto modified_rect = a_modified_rect.is_empty() ? this->rect() : a_modified_rect;
- for (auto* client : m_clients)
- client->image_did_change_rect(modified_rect);
- }
- ImageUndoCommand::ImageUndoCommand(Image& image, DeprecatedString action_text)
- : m_snapshot(image.take_snapshot().release_value_but_fixme_should_propagate_errors())
- , m_image(image)
- , m_action_text(move(action_text))
- {
- }
- void ImageUndoCommand::undo()
- {
- // FIXME: Handle errors.
- (void)m_image.restore_snapshot(*m_snapshot);
- }
- void ImageUndoCommand::redo()
- {
- undo();
- }
- ErrorOr<void> Image::flip(Gfx::Orientation orientation)
- {
- Vector<NonnullRefPtr<Layer>> flipped_layers;
- TRY(flipped_layers.try_ensure_capacity(m_layers.size()));
- VERIFY(m_layers.size() > 0);
- size_t selected_layer_index = 0;
- for (size_t i = 0; i < m_layers.size(); ++i) {
- auto& layer = m_layers[i];
- auto new_layer = TRY(Layer::try_create_snapshot(*this, layer));
- if (layer.is_selected())
- selected_layer_index = i;
- TRY(new_layer->flip(orientation, Layer::NotifyClients::No));
- flipped_layers.unchecked_append(new_layer);
- }
- m_layers = move(flipped_layers);
- for (auto& layer : m_layers)
- layer.did_modify_bitmap({}, Layer::NotifyClients::No);
- select_layer(&m_layers[selected_layer_index]);
- did_change();
- return {};
- }
- ErrorOr<void> Image::rotate(Gfx::RotationDirection direction)
- {
- Vector<NonnullRefPtr<Layer>> rotated_layers;
- TRY(rotated_layers.try_ensure_capacity(m_layers.size()));
- VERIFY(m_layers.size() > 0);
- size_t selected_layer_index = 0;
- for (size_t i = 0; i < m_layers.size(); ++i) {
- auto& layer = m_layers[i];
- auto new_layer = TRY(Layer::try_create_snapshot(*this, layer));
- if (layer.is_selected())
- selected_layer_index = i;
- TRY(new_layer->rotate(direction, Layer::NotifyClients::No));
- rotated_layers.unchecked_append(new_layer);
- }
- m_layers = move(rotated_layers);
- for (auto& layer : m_layers)
- layer.did_modify_bitmap({}, Layer::NotifyClients::Yes);
- select_layer(&m_layers[selected_layer_index]);
- m_size = { m_size.height(), m_size.width() };
- did_change_rect();
- return {};
- }
- ErrorOr<void> Image::crop(Gfx::IntRect const& cropped_rect)
- {
- Vector<NonnullRefPtr<Layer>> cropped_layers;
- TRY(cropped_layers.try_ensure_capacity(m_layers.size()));
- VERIFY(m_layers.size() > 0);
- size_t selected_layer_index = 0;
- for (size_t i = 0; i < m_layers.size(); ++i) {
- auto& layer = m_layers[i];
- auto new_layer = TRY(Layer::try_create_snapshot(*this, layer));
- if (layer.is_selected())
- selected_layer_index = i;
- 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());
- 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_y = max(0, layer_location.y() - cropped_rect.y());
- new_layer->set_location({ new_layer_x, new_layer_y });
- cropped_layers.unchecked_append(new_layer);
- }
- m_layers = move(cropped_layers);
- for (auto& layer : m_layers)
- layer.did_modify_bitmap({}, Layer::NotifyClients::Yes);
- select_layer(&m_layers[selected_layer_index]);
- m_size = { cropped_rect.width(), cropped_rect.height() };
- did_change_rect(cropped_rect);
- return {};
- }
- Optional<Gfx::IntRect> Image::nonempty_content_bounding_rect() const
- {
- if (m_layers.is_empty())
- return {};
- Optional<Gfx::IntRect> bounding_rect;
- for (auto const& layer : m_layers) {
- auto layer_content_rect_in_layer_coordinates = layer.nonempty_content_bounding_rect();
- if (!layer_content_rect_in_layer_coordinates.has_value())
- continue;
- auto layer_content_rect_in_image_coordinates = layer_content_rect_in_layer_coordinates->translated(layer.location());
- if (!bounding_rect.has_value())
- bounding_rect = layer_content_rect_in_image_coordinates;
- else
- bounding_rect = bounding_rect->united(layer_content_rect_in_image_coordinates);
- }
- return bounding_rect;
- }
- ErrorOr<void> Image::resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode scaling_mode)
- {
- float scale_x = 1.0f;
- float scale_y = 1.0f;
- if (size().width() != 0.0f) {
- scale_x = new_size.width() / static_cast<float>(size().width());
- }
- if (size().height() != 0.0f) {
- scale_y = new_size.height() / static_cast<float>(size().height());
- }
- Vector<NonnullRefPtr<Layer>> resized_layers;
- TRY(resized_layers.try_ensure_capacity(m_layers.size()));
- VERIFY(m_layers.size() > 0);
- size_t selected_layer_index = 0;
- for (size_t i = 0; i < m_layers.size(); ++i) {
- auto& layer = m_layers[i];
- auto new_layer = TRY(Layer::try_create_snapshot(*this, layer));
- if (layer.is_selected())
- selected_layer_index = i;
- 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));
- resized_layers.unchecked_append(new_layer);
- }
- m_layers = move(resized_layers);
- for (auto& layer : m_layers)
- layer.did_modify_bitmap({}, Layer::NotifyClients::Yes);
- select_layer(&m_layers[selected_layer_index]);
- m_size = { new_size.width(), new_size.height() };
- did_change_rect();
- return {};
- }
- Color Image::color_at(Gfx::IntPoint point) const
- {
- Color color;
- for (auto const& layer : m_layers) {
- if (!layer.is_visible() || !layer.rect().contains(point))
- continue;
- auto layer_color = layer.display_bitmap().get_pixel(point);
- float layer_opacity = layer.opacity_percent() / 100.0f;
- layer_color.set_alpha((u8)(layer_color.alpha() * layer_opacity));
- color = color.blend(layer_color);
- }
- return color;
- }
- }
|