From 06ae5b3536a14558469c4cd7e90ef60d60006b0e Mon Sep 17 00:00:00 2001 From: Tobias Christiansen Date: Mon, 3 Jan 2022 13:48:20 +0100 Subject: [PATCH] LibGfx: Add BitmapMixer With this BitmapMixer one can draw one Bitmap onto another with different modes. For now the only supported mixing methods implemented are Add and Lightest (which is very naive). --- Userland/Libraries/LibGfx/BitmapMixer.cpp | 48 +++++++++++++++++++++++ Userland/Libraries/LibGfx/BitmapMixer.h | 29 ++++++++++++++ Userland/Libraries/LibGfx/CMakeLists.txt | 1 + 3 files changed, 78 insertions(+) create mode 100644 Userland/Libraries/LibGfx/BitmapMixer.cpp create mode 100644 Userland/Libraries/LibGfx/BitmapMixer.h diff --git a/Userland/Libraries/LibGfx/BitmapMixer.cpp b/Userland/Libraries/LibGfx/BitmapMixer.cpp new file mode 100644 index 00000000000..2e5269a1ae5 --- /dev/null +++ b/Userland/Libraries/LibGfx/BitmapMixer.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022, Tobias Christiansen + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "BitmapMixer.h" + +namespace Gfx { + +void BitmapMixer::mix_with(Bitmap& other_bitmap, MixingMethod mixing_method) +{ + VERIFY(m_bitmap.size() == other_bitmap.size()); + + int height = m_bitmap.height(); + int width = m_bitmap.width(); + + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + auto original_color = m_bitmap.get_pixel(x, y); + auto other_color = other_bitmap.get_pixel(x, y); + + Color output_color = { 0, 0, 0, original_color.alpha() }; + switch (mixing_method) { + case MixingMethod::Add: + output_color.set_red(original_color.red() + other_color.red()); + output_color.set_green(original_color.green() + other_color.green()); + output_color.set_blue(original_color.blue() + other_color.blue()); + break; + case MixingMethod::Lightest: + auto original_lightness = original_color.red() + original_color.green() + original_color.blue(); + auto other_lightness = other_color.red() + other_color.green() + other_color.blue(); + if (original_lightness > other_lightness) { + output_color = original_color; + } else { + output_color.set_red(other_color.red()); + output_color.set_green(other_color.green()); + output_color.set_blue(other_color.blue()); + } + break; + } + if (original_color != output_color) + m_bitmap.set_pixel(x, y, output_color); + } + } +} + +} diff --git a/Userland/Libraries/LibGfx/BitmapMixer.h b/Userland/Libraries/LibGfx/BitmapMixer.h new file mode 100644 index 00000000000..bd895d7ff57 --- /dev/null +++ b/Userland/Libraries/LibGfx/BitmapMixer.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022, Tobias Christiansen + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include "Bitmap.h" + +namespace Gfx { + +class BitmapMixer { +public: + enum class MixingMethod { + Add, + Lightest, + }; + + BitmapMixer(Bitmap& bitmap) + : m_bitmap(bitmap) {}; + + void mix_with(Bitmap&, MixingMethod); + +private: + Bitmap& m_bitmap; +}; + +} diff --git a/Userland/Libraries/LibGfx/CMakeLists.txt b/Userland/Libraries/LibGfx/CMakeLists.txt index 3086cd75d28..8e1663abcd0 100644 --- a/Userland/Libraries/LibGfx/CMakeLists.txt +++ b/Userland/Libraries/LibGfx/CMakeLists.txt @@ -2,6 +2,7 @@ set(SOURCES AffineTransform.cpp AntiAliasingPainter.cpp Bitmap.cpp + BitmapMixer.cpp BitmapFont.cpp BMPLoader.cpp BMPWriter.cpp