1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:47:34 +00:00

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. :^)
This commit is contained in:
Andreas Kling 2022-08-23 21:33:52 +02:00
parent 5ded6904d8
commit 34a09bbb54
5 changed files with 83 additions and 0 deletions

View file

@ -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;