1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:37:36 +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

@ -269,4 +269,50 @@ void Layer::set_edit_mode(Layer::EditMode mode)
m_edit_mode = mode;
}
Optional<Gfx::IntRect> Layer::nonempty_content_bounding_rect() const
{
Optional<int> min_content_y;
Optional<int> min_content_x;
Optional<int> max_content_y;
Optional<int> max_content_x;
for (int y = 0; y < m_content_bitmap->height(); ++y) {
for (int x = 0; x < m_content_bitmap->width(); ++x) {
auto color = m_content_bitmap->get_pixel(x, y);
if (color.alpha() == 0)
continue;
if (!min_content_x.has_value())
min_content_x = x;
else
min_content_x = min(*min_content_x, x);
if (!min_content_y.has_value())
min_content_y = y;
else
min_content_y = min(*min_content_y, y);
if (!max_content_x.has_value())
max_content_x = x;
else
max_content_x = max(*max_content_x, x);
if (!max_content_y.has_value())
max_content_y = y;
else
max_content_y = max(*max_content_y, y);
}
}
if (!min_content_x.has_value())
return {};
return Gfx::IntRect {
*min_content_x,
*min_content_y,
*max_content_x - *min_content_x + 1,
*max_content_y - *min_content_y + 1
};
}
}