diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp index 69d26e8637..dcfa39986f 100644 --- a/Userland/Applications/PixelPaint/Image.cpp +++ b/Userland/Applications/PixelPaint/Image.cpp @@ -529,13 +529,24 @@ void Image::crop(Gfx::IntRect const& cropped_rect) void Image::resize(Gfx::IntSize const& 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(size().width()); + } + + if (size().height() != 0.0f) { + scale_y = new_size.height() / static_cast(size().height()); + } + for (auto& layer : m_layers) { - layer.resize(new_size, scaling_mode); + Gfx::IntPoint new_location(scale_x * layer.location().x(), scale_y * layer.location().y()); + layer.resize(new_size, new_location, scaling_mode); } m_size = { new_size.width(), new_size.height() }; did_change_rect(); - } Color Image::color_at(Gfx::IntPoint const& point) const diff --git a/Userland/Applications/PixelPaint/Layer.cpp b/Userland/Applications/PixelPaint/Layer.cpp index 57b4099ba4..d4e4fd0b7d 100644 --- a/Userland/Applications/PixelPaint/Layer.cpp +++ b/Userland/Applications/PixelPaint/Layer.cpp @@ -180,31 +180,42 @@ void Layer::crop(Gfx::IntRect const& rect) did_modify_bitmap(); } -void Layer::resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode) +void Layer::resize(Gfx::IntSize const& new_size, Gfx::IntPoint const& new_location, Gfx::Painter::ScalingMode scaling_mode) { - const Gfx::IntRect old_rect(Gfx::IntPoint(0, 0), size()); - const Gfx::IntRect new_rect(Gfx::IntPoint(0, 0), new_size); + auto src_rect = Gfx::IntRect(Gfx::IntPoint(0, 0), size()); + auto dst_rect = Gfx::IntRect(Gfx::IntPoint(0, 0), new_size); { - auto resized = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors(); - Gfx::Painter painter(resized); + auto dst = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors(); + Gfx::Painter painter(dst); - painter.draw_scaled_bitmap(new_rect, *m_content_bitmap, old_rect, 1.0f, scaling_mode); + painter.draw_scaled_bitmap(dst_rect, *m_content_bitmap, src_rect, 1.0f, scaling_mode); - m_content_bitmap = move(resized); + m_content_bitmap = move(dst); } if (m_mask_bitmap) { - auto resized = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors(); - Gfx::Painter painter(resized); + auto dst = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors(); + Gfx::Painter painter(dst); - painter.draw_scaled_bitmap(new_rect, *m_mask_bitmap, old_rect, 1.0f, scaling_mode); - m_mask_bitmap = move(resized); + painter.draw_scaled_bitmap(dst_rect, *m_mask_bitmap, src_rect, 1.0f, scaling_mode); + m_mask_bitmap = move(dst); } + set_location(new_location); did_modify_bitmap(); } +void Layer::resize(Gfx::IntRect const& new_rect, Gfx::Painter::ScalingMode scaling_mode) +{ + resize(new_rect.size(), new_rect.location(), scaling_mode); +} + +void Layer::resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode) +{ + resize(new_size, location(), scaling_mode); +} + void Layer::update_cached_bitmap() { if (!is_masked()) { diff --git a/Userland/Applications/PixelPaint/Layer.h b/Userland/Applications/PixelPaint/Layer.h index 408fee937d..ab24d66cb3 100644 --- a/Userland/Applications/PixelPaint/Layer.h +++ b/Userland/Applications/PixelPaint/Layer.h @@ -58,6 +58,8 @@ public: void rotate(Gfx::RotationDirection direction); void crop(Gfx::IntRect const& rect); void resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode); + void resize(Gfx::IntRect const& new_rect, Gfx::Painter::ScalingMode scaling_mode); + void resize(Gfx::IntSize const& new_size, Gfx::IntPoint const& new_location, Gfx::Painter::ScalingMode scaling_mode); ErrorOr try_set_bitmaps(NonnullRefPtr content, RefPtr mask);