From bc6eebb1d7713b66fca6ecfe098604862c766fd9 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sun, 7 Jan 2024 20:29:24 -0500 Subject: [PATCH] LibGfx: Add CMYKBitmap This is a simple container for 2d CMYK data. This is a new class instead of a new format in Bitmap because: * Almost all clients of Bitmap will want to deal with RGB data * It keeps the ARGB32 typedef working * Bitmap already does a lot of things The idea is that a few select places will be able to use CMYKBitmap and a color profile to do a better CMYK -> RGB conversion than an image decoder could do, and then store the result in a Bitmap for later display then. CMYKBitmap misses some of Bitmap's features, such as shared memory support, high-dpi scaling capability, etc. --- Userland/Libraries/LibGfx/CMYKBitmap.cpp | 18 +++++++ Userland/Libraries/LibGfx/CMYKBitmap.h | 68 ++++++++++++++++++++++++ Userland/Libraries/LibGfx/CMakeLists.txt | 1 + Userland/Libraries/LibGfx/Forward.h | 1 + 4 files changed, 88 insertions(+) create mode 100644 Userland/Libraries/LibGfx/CMYKBitmap.cpp create mode 100644 Userland/Libraries/LibGfx/CMYKBitmap.h diff --git a/Userland/Libraries/LibGfx/CMYKBitmap.cpp b/Userland/Libraries/LibGfx/CMYKBitmap.cpp new file mode 100644 index 00000000000..b587bc570e6 --- /dev/null +++ b/Userland/Libraries/LibGfx/CMYKBitmap.cpp @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2024, Nico Weber + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace Gfx { + +ErrorOr> CMYKBitmap::create_with_size(IntSize const& size) +{ + VERIFY(size.width() >= 0 && size.height() >= 0); + auto data = TRY(ByteBuffer::create_uninitialized(size.width() * size.height() * sizeof(CMYK))); + return adopt_ref(*new CMYKBitmap(size, move(data))); +} + +} diff --git a/Userland/Libraries/LibGfx/CMYKBitmap.h b/Userland/Libraries/LibGfx/CMYKBitmap.h new file mode 100644 index 00000000000..e24bb2913ea --- /dev/null +++ b/Userland/Libraries/LibGfx/CMYKBitmap.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2024, Nico Weber + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include + +namespace Gfx { + +struct CMYK { + u8 c; + u8 m; + u8 y; + u8 k; +}; + +class CMYKBitmap : public RefCounted { +public: + static ErrorOr> create_with_size(IntSize const& size); + + IntSize const& size() const { return m_size; } + + [[nodiscard]] CMYK* scanline(int y); + [[nodiscard]] CMYK const* scanline(int y) const; + + [[nodiscard]] CMYK* begin(); + [[nodiscard]] CMYK* end(); + +private: + CMYKBitmap(IntSize const& size, ByteBuffer data) + : m_size(size) + , m_data(move(data)) + { + } + + IntSize m_size; + ByteBuffer m_data; +}; + +inline CMYK* CMYKBitmap::scanline(int y) +{ + VERIFY(y >= 0 && y < m_size.height()); + return reinterpret_cast(m_data.data() + y * m_size.width() * sizeof(CMYK)); +} + +inline CMYK const* CMYKBitmap::scanline(int y) const +{ + VERIFY(y >= 0 && y < m_size.height()); + return reinterpret_cast(m_data.data() + y * m_size.width() * sizeof(CMYK)); +} + +inline CMYK* CMYKBitmap::begin() +{ + return reinterpret_cast(m_data.data()); +} + +inline CMYK* CMYKBitmap::end() +{ + return reinterpret_cast(m_data.data() + m_data.size()); +} + +} diff --git a/Userland/Libraries/LibGfx/CMakeLists.txt b/Userland/Libraries/LibGfx/CMakeLists.txt index 7348ba6d7c7..b753abf1aa1 100644 --- a/Userland/Libraries/LibGfx/CMakeLists.txt +++ b/Userland/Libraries/LibGfx/CMakeLists.txt @@ -3,6 +3,7 @@ set(SOURCES AntiAliasingPainter.cpp Bitmap.cpp BitmapMixer.cpp + CMYKBitmap.cpp ClassicStylePainter.cpp ClassicWindowTheme.cpp Color.cpp diff --git a/Userland/Libraries/LibGfx/Forward.h b/Userland/Libraries/LibGfx/Forward.h index 6e251678561..361453d7489 100644 --- a/Userland/Libraries/LibGfx/Forward.h +++ b/Userland/Libraries/LibGfx/Forward.h @@ -9,6 +9,7 @@ namespace Gfx { class Bitmap; +class CMYKBitmap; class ImmutableBitmap; class CharacterBitmap; class Color;