mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:58:11 +00:00
PixelPaint: Add a Mask class
The Mask class represents an opacity mask over a rectangular section of an image, linking every pixel to an alpha value ranging from 0 (not selected) to 255 (fully selected). "Partially selected" pixels can be used to simulate anti-aliased curves. This class will be used as the basis for the new non-rectangular selection feature.
This commit is contained in:
parent
bd5b1d1413
commit
0828c75e57
3 changed files with 224 additions and 0 deletions
118
Userland/Applications/PixelPaint/Mask.cpp
Normal file
118
Userland/Applications/PixelPaint/Mask.cpp
Normal file
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
size_t Mask::to_index(int x, int y) const
|
||||
{
|
||||
VERIFY(m_bounding_rect.contains(x, y));
|
||||
|
||||
int dx = x - m_bounding_rect.x();
|
||||
int dy = y - m_bounding_rect.y();
|
||||
return dy * m_bounding_rect.width() + dx;
|
||||
}
|
||||
|
||||
u8 Mask::get(int x, int y) const
|
||||
{
|
||||
if (is_null() || !m_bounding_rect.contains(x, y)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return m_data[to_index(x, y)];
|
||||
}
|
||||
|
||||
void Mask::set(int x, int y, u8 value)
|
||||
{
|
||||
VERIFY(!is_null());
|
||||
VERIFY(m_bounding_rect.contains(x, y));
|
||||
|
||||
m_data[to_index(x, y)] = 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; });
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue