ladybird/Userland/Applications/PixelPaint/Mask.cpp
Idan Horowitz b41954182a PixelPaint: Move Mask::{get, set, to_index} to the header file
They were previously taking up 9% of samples in a profile of PixelPaint
while selecting a mask, and as a result of moving them to the header
they were inlined, which effectively eliminated them from the profile.
2021-10-24 17:02:44 +02:00

93 lines
1.8 KiB
C++

/*
* Copyright (c) 2021, Davipb <daviparca@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Mask.h"
namespace PixelPaint {
Mask::Mask(Gfx::IntRect bounding_rect, u8 default_value)
: m_bounding_rect(bounding_rect)
{
auto data_size = bounding_rect.size().area();
m_data.resize(data_size);
for (auto& x : m_data) {
x = default_value;
}
}
Mask Mask::with_bounding_rect(Gfx::IntRect inner_rect) const
{
auto result = Mask::empty(inner_rect);
result.for_each_pixel([&](int x, int y) {
result.set(x, y, get(x, y));
});
return result;
}
void Mask::shrink_to_fit()
{
int topmost = NumericLimits<int>::max();
int bottommost = NumericLimits<int>::min();
int leftmost = NumericLimits<int>::max();
int rightmost = NumericLimits<int>::min();
bool empty = true;
for_each_pixel([&](auto x, auto y) {
if (get(x, y) == 0) {
return;
}
empty = false;
topmost = min(topmost, y);
bottommost = max(bottommost, y);
leftmost = min(leftmost, x);
rightmost = max(rightmost, x);
});
if (empty) {
m_bounding_rect = {};
m_data.clear();
return;
}
Gfx::IntRect new_bounding_rect(
leftmost,
topmost,
rightmost - leftmost + 1,
bottommost - topmost + 1);
*this = with_bounding_rect(new_bounding_rect);
}
void Mask::invert()
{
for_each_pixel([&](int x, int y) {
set(x, y, 0xFF - get(x, y));
});
}
void Mask::add(Mask const& other)
{
combine(other, [](int a, int b) { return a + b; });
}
void Mask::subtract(Mask const& other)
{
combine(other, [](int a, int b) { return a - b; });
}
void Mask::intersect(Mask const& other)
{
combinef(other, [](float a, float b) { return a * b; });
}
}