2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-06-06 17:06:47 -03:00
|
|
|
* Copyright (c) 2022, Aaron Yoder <aaronjyoder@gmail.com>
|
2022-02-10 16:28:48 -03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2022-08-27 09:50:37 -03:00
|
|
|
* Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com>.
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-06-14 13:51:57 -03:00
|
|
|
#include "BucketTool.h"
|
2021-09-17 04:54:55 -03:00
|
|
|
#include "../ImageEditor.h"
|
|
|
|
|
#include "../Layer.h"
|
2022-08-29 15:25:34 -03:00
|
|
|
#include "../Mask.h"
|
2021-09-04 20:16:33 -03:00
|
|
|
#include <AK/HashTable.h>
|
2019-06-15 05:39:45 -03:00
|
|
|
#include <AK/Queue.h>
|
2020-10-15 14:57:07 -03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
|
#include <LibGUI/Label.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Painter.h>
|
2021-08-05 14:36:38 -03:00
|
|
|
#include <LibGUI/ValueSlider.h>
|
2020-02-06 08:07:05 -03:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2020-05-12 18:44:46 -03:00
|
|
|
#include <LibGfx/Rect.h>
|
|
|
|
|
|
2020-05-20 15:35:35 -03:00
|
|
|
namespace PixelPaint {
|
2019-06-14 13:51:57 -03:00
|
|
|
|
|
|
|
|
BucketTool::BucketTool()
|
|
|
|
|
{
|
2022-07-11 14:32:29 -03:00
|
|
|
m_cursor = Gfx::Bitmap::try_load_from_file("/res/icons/pixelpaint/bucket.png"sv).release_value_but_fixme_should_propagate_errors();
|
2019-06-14 13:51:57 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-11 08:27:47 -03:00
|
|
|
static float color_distance_squared(Gfx::Color const& lhs, Gfx::Color const& rhs)
|
2020-10-15 14:57:07 -03:00
|
|
|
{
|
|
|
|
|
int a = rhs.red() - lhs.red();
|
|
|
|
|
int b = rhs.green() - lhs.green();
|
|
|
|
|
int c = rhs.blue() - lhs.blue();
|
2022-08-27 09:50:37 -03:00
|
|
|
int d = rhs.alpha() - lhs.alpha();
|
|
|
|
|
return (a * a + b * b + c * c + d * d) / (4.0f * 255.0f * 255.0f);
|
2020-10-15 14:57:07 -03:00
|
|
|
}
|
|
|
|
|
|
2022-08-29 15:25:34 -03:00
|
|
|
static bool can_paint(Gfx::IntPoint point, Gfx::Bitmap& bitmap, Gfx::Color const& target_color, float threshold_normalized_squared)
|
2022-06-06 17:06:47 -03:00
|
|
|
{
|
2022-08-29 15:25:34 -03:00
|
|
|
auto pixel_color = bitmap.get_pixel<Gfx::StorageFormat::BGRA8888>(point.x(), point.y());
|
2022-06-06 17:06:47 -03:00
|
|
|
return color_distance_squared(pixel_color, target_color) <= threshold_normalized_squared;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 08:27:47 -03:00
|
|
|
static void flood_fill(Gfx::Bitmap& bitmap, Gfx::IntPoint const& start_position, Color target_color, Color fill_color, int threshold)
|
2019-06-14 13:51:57 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(bitmap.bpp() == 32);
|
2019-06-15 06:06:02 -03:00
|
|
|
|
2019-06-16 01:34:29 -03:00
|
|
|
if (target_color == fill_color)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-04-19 14:29:07 -03:00
|
|
|
if (!bitmap.rect().contains(start_position))
|
|
|
|
|
return;
|
|
|
|
|
|
2020-10-15 14:57:07 -03:00
|
|
|
float threshold_normalized_squared = (threshold / 100.0f) * (threshold / 100.0f);
|
|
|
|
|
|
2022-08-29 15:25:34 -03:00
|
|
|
// Create Mask which will track already-colored pixels
|
|
|
|
|
Mask flood_mask = Mask::empty(bitmap.rect());
|
|
|
|
|
|
|
|
|
|
Queue<Gfx::IntPoint> points_to_visit = Queue<Gfx::IntPoint>();
|
|
|
|
|
|
|
|
|
|
points_to_visit.enqueue({ start_position.x(), start_position.y() });
|
|
|
|
|
bitmap.set_pixel<Gfx::StorageFormat::BGRA8888>(start_position.x(), start_position.y(), fill_color);
|
|
|
|
|
flood_mask.set(start_position.x(), start_position.y(), 1);
|
|
|
|
|
|
|
|
|
|
// This implements a non-recursive flood fill. This is a breadth-first search of paintable neighbors
|
|
|
|
|
// As we find neighbors that are paintable we update their pixel, add them to the queue, and mark them in the mask
|
|
|
|
|
while (!points_to_visit.is_empty()) {
|
|
|
|
|
auto current_point = points_to_visit.dequeue();
|
|
|
|
|
auto candidate_points = Array {
|
|
|
|
|
current_point.moved_left(1),
|
|
|
|
|
current_point.moved_right(1),
|
|
|
|
|
current_point.moved_up(1),
|
|
|
|
|
current_point.moved_down(1)
|
|
|
|
|
};
|
|
|
|
|
for (auto candidate_point : candidate_points) {
|
|
|
|
|
if (!bitmap.rect().contains(candidate_point))
|
|
|
|
|
continue;
|
|
|
|
|
if (flood_mask.get(candidate_point.x(), candidate_point.y()) == 0 && can_paint(candidate_point, bitmap, target_color, threshold_normalized_squared)) {
|
|
|
|
|
points_to_visit.enqueue(candidate_point);
|
|
|
|
|
bitmap.set_pixel<Gfx::StorageFormat::BGRA8888>(candidate_point.x(), candidate_point.y(), fill_color);
|
|
|
|
|
}
|
|
|
|
|
flood_mask.set(candidate_point.x(), candidate_point.y(), 0xFF);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-14 14:11:22 -03:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 05:07:24 -03:00
|
|
|
void BucketTool::on_mousedown(Layer* layer, MouseEvent& event)
|
2019-06-14 14:11:22 -03:00
|
|
|
{
|
2021-08-25 05:07:24 -03:00
|
|
|
if (!layer)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-08-25 05:06:00 -03:00
|
|
|
auto& layer_event = event.layer_event();
|
2021-08-25 05:07:24 -03:00
|
|
|
if (!layer->rect().contains(layer_event.position()))
|
2019-06-14 14:11:22 -03:00
|
|
|
return;
|
|
|
|
|
|
2022-03-07 18:37:26 -03:00
|
|
|
GUI::Painter painter(layer->currently_edited_bitmap());
|
|
|
|
|
auto target_color = layer->currently_edited_bitmap().get_pixel(layer_event.x(), layer_event.y());
|
2019-06-14 14:11:22 -03:00
|
|
|
|
2022-03-07 18:37:26 -03:00
|
|
|
flood_fill(layer->currently_edited_bitmap(), layer_event.position(), target_color, m_editor->color_for(layer_event), m_threshold);
|
2020-05-12 18:44:46 -03:00
|
|
|
|
2021-08-25 05:07:24 -03:00
|
|
|
layer->did_modify_bitmap();
|
2022-08-21 15:33:03 -03:00
|
|
|
m_editor->did_complete_action(tool_name());
|
2020-05-12 18:44:46 -03:00
|
|
|
}
|
2019-06-14 14:11:22 -03:00
|
|
|
|
2020-10-15 14:57:07 -03:00
|
|
|
GUI::Widget* BucketTool::get_properties_widget()
|
|
|
|
|
{
|
|
|
|
|
if (!m_properties_widget) {
|
|
|
|
|
m_properties_widget = GUI::Widget::construct();
|
|
|
|
|
m_properties_widget->set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
|
|
|
|
|
|
auto& threshold_container = m_properties_widget->add<GUI::Widget>();
|
2020-12-29 21:23:32 -03:00
|
|
|
threshold_container.set_fixed_height(20);
|
2020-10-15 14:57:07 -03:00
|
|
|
threshold_container.set_layout<GUI::HorizontalBoxLayout>();
|
|
|
|
|
|
|
|
|
|
auto& threshold_label = threshold_container.add<GUI::Label>("Threshold:");
|
|
|
|
|
threshold_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
2020-12-29 21:23:32 -03:00
|
|
|
threshold_label.set_fixed_size(80, 20);
|
2020-10-15 14:57:07 -03:00
|
|
|
|
2021-08-05 14:36:38 -03:00
|
|
|
auto& threshold_slider = threshold_container.add<GUI::ValueSlider>(Orientation::Horizontal, "%");
|
2020-10-15 14:57:07 -03:00
|
|
|
threshold_slider.set_range(0, 100);
|
|
|
|
|
threshold_slider.set_value(m_threshold);
|
2021-08-03 07:46:10 -03:00
|
|
|
|
|
|
|
|
threshold_slider.on_change = [&](int value) {
|
2020-10-15 14:57:07 -03:00
|
|
|
m_threshold = value;
|
|
|
|
|
};
|
2021-09-03 02:03:15 -03:00
|
|
|
set_primary_slider(&threshold_slider);
|
2020-10-15 14:57:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_properties_widget.ptr();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 13:51:57 -03:00
|
|
|
}
|