diff options
author | Andreas Kling <kling@serenityos.org> | 2022-08-23 21:33:52 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-08-23 22:39:27 +0200 |
commit | 34a09bbb5410b866e35bb03fcb489a61f4b262b3 (patch) | |
tree | e69f7aeeab726ae14637019f38012cde7a2a5050 /Userland/Applications/PixelPaint/Image.cpp | |
parent | 5ded6904d8e9b1453548b196f39d09dc20ba3e2b (diff) | |
download | serenity-34a09bbb5410b866e35bb03fcb489a61f4b262b3.zip |
PixelPaint: Add simple "Crop Image to Content" feature
This command finds the smallest non-empty content bounding rect
by looking for the outermost non-transparent pixels in the image,
and then crops the image to that rect.
It's implemented in a pretty naive way, but it's a start. :^)
Diffstat (limited to 'Userland/Applications/PixelPaint/Image.cpp')
-rw-r--r-- | Userland/Applications/PixelPaint/Image.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp index 5832fefdd7..98bc5760a2 100644 --- a/Userland/Applications/PixelPaint/Image.cpp +++ b/Userland/Applications/PixelPaint/Image.cpp @@ -530,6 +530,26 @@ void Image::crop(Gfx::IntRect const& cropped_rect) did_change_rect(cropped_rect); } +Optional<Gfx::IntRect> Image::nonempty_content_bounding_rect() const +{ + if (m_layers.is_empty()) + return {}; + + Optional<Gfx::IntRect> bounding_rect; + for (auto& 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.value().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; +} + void Image::resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode) { float scale_x = 1.0f; |