2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
|
2022-09-30 17:26:13 +00:00
|
|
|
* Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-01-09 01:06:04 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-09-30 17:26:13 +00:00
|
|
|
#include <AK/Function.h>
|
2019-06-21 16:58:45 +00:00
|
|
|
#include <AK/RefCounted.h>
|
2021-05-24 10:24:38 +00:00
|
|
|
#include <LibCore/AnonymousBuffer.h>
|
2020-02-14 22:02:47 +00:00
|
|
|
#include <LibGfx/Color.h>
|
2020-03-29 17:04:05 +00:00
|
|
|
#include <LibGfx/Forward.h>
|
2020-02-14 22:02:47 +00:00
|
|
|
#include <LibGfx/Rect.h>
|
2019-01-14 19:00:42 +00:00
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
namespace Gfx {
|
|
|
|
|
2020-02-14 22:02:47 +00:00
|
|
|
enum class BitmapFormat {
|
|
|
|
Invalid,
|
2021-03-16 10:48:42 +00:00
|
|
|
BGRx8888,
|
|
|
|
BGRA8888,
|
2024-11-02 23:12:14 +00:00
|
|
|
RGBx8888,
|
2021-03-16 11:09:15 +00:00
|
|
|
RGBA8888,
|
2020-02-14 22:02:47 +00:00
|
|
|
};
|
|
|
|
|
2021-01-16 22:57:57 +00:00
|
|
|
inline bool is_valid_bitmap_format(unsigned format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case (unsigned)BitmapFormat::Invalid:
|
2021-03-16 10:48:42 +00:00
|
|
|
case (unsigned)BitmapFormat::BGRx8888:
|
2024-11-02 23:12:14 +00:00
|
|
|
case (unsigned)BitmapFormat::RGBx8888:
|
2021-03-16 10:48:42 +00:00
|
|
|
case (unsigned)BitmapFormat::BGRA8888:
|
2021-03-16 11:09:15 +00:00
|
|
|
case (unsigned)BitmapFormat::RGBA8888:
|
2021-01-16 22:57:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-06 21:59:20 +00:00
|
|
|
enum class StorageFormat {
|
2021-03-16 11:00:43 +00:00
|
|
|
BGRx8888,
|
|
|
|
BGRA8888,
|
2021-03-16 11:09:15 +00:00
|
|
|
RGBA8888,
|
2024-11-02 23:12:14 +00:00
|
|
|
RGBx8888,
|
2020-09-06 21:59:20 +00:00
|
|
|
};
|
|
|
|
|
2023-06-13 23:10:22 +00:00
|
|
|
inline StorageFormat determine_storage_format(BitmapFormat format)
|
2020-09-06 21:59:20 +00:00
|
|
|
{
|
|
|
|
switch (format) {
|
2021-03-16 10:48:42 +00:00
|
|
|
case BitmapFormat::BGRx8888:
|
2021-03-16 11:00:43 +00:00
|
|
|
return StorageFormat::BGRx8888;
|
2021-03-16 10:48:42 +00:00
|
|
|
case BitmapFormat::BGRA8888:
|
2021-03-16 11:00:43 +00:00
|
|
|
return StorageFormat::BGRA8888;
|
2021-03-16 11:09:15 +00:00
|
|
|
case BitmapFormat::RGBA8888:
|
|
|
|
return StorageFormat::RGBA8888;
|
2024-11-02 23:12:14 +00:00
|
|
|
case BitmapFormat::RGBx8888:
|
|
|
|
return StorageFormat::RGBx8888;
|
2020-09-06 21:59:20 +00:00
|
|
|
default:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-09-06 21:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 16:17:50 +00:00
|
|
|
struct BackingStore;
|
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
class Bitmap : public RefCounted<Bitmap> {
|
2019-01-09 01:06:04 +00:00
|
|
|
public:
|
2024-06-05 06:17:28 +00:00
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create(BitmapFormat, IntSize);
|
LibGfx: Store alpha type information in `Gfx::Bitmap`
We use instances of `Gfx::Bitmap` to move pixel data all the way from
raw image bytes up to the Skia renderer. A vital piece of information
for correct blending of bitmaps is the alpha type, i.e. are we dealing
with premultiplied or unpremultiplied color values?
Premultiplied means that the RGB colors have been multiplied with the
associated alpha value, i.e. RGB(255, 255, 255) with an alpha of 2% is
stored as RGBA(5, 5, 5, 2%).
Unpremultiplied means that the original RGB colors are stored,
regardless of the alpha value. I.e. RGB(255, 255, 255) with an alpha of
2% is stored as RGBA(255, 255, 255, 2%).
It is important to know how the color data is stored in a
`Gfx::Bitmap`, because correct blending depends on knowing the alpha
type: premultiplied blending uses `S + (1 - A) * D`, while
unpremultiplied blending uses `A * S + (1 - A) * D`.
This adds the alpha type information to `Gfx::Bitmap` across the board.
It isn't used anywhere yet.
2024-08-02 10:52:14 +00:00
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create(BitmapFormat, AlphaType, IntSize);
|
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_shareable(BitmapFormat, AlphaType, IntSize);
|
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_wrapper(BitmapFormat, AlphaType, IntSize, size_t pitch, void*, Function<void()>&& destruction_callback = {});
|
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_with_anonymous_buffer(BitmapFormat, AlphaType, Core::AnonymousBuffer, IntSize);
|
2019-12-08 16:07:44 +00:00
|
|
|
|
2021-11-06 10:52:35 +00:00
|
|
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> clone() const;
|
2020-09-12 11:20:34 +00:00
|
|
|
|
2022-09-15 07:31:29 +00:00
|
|
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> cropped(Gfx::IntRect, Optional<BitmapFormat> new_bitmap_format = {}) const;
|
2021-11-06 12:15:43 +00:00
|
|
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_bitmap_backed_by_anonymous_buffer() const;
|
2020-03-29 17:04:05 +00:00
|
|
|
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] ShareableBitmap to_shareable_bitmap() const;
|
2019-12-08 16:07:44 +00:00
|
|
|
|
2023-09-03 18:24:00 +00:00
|
|
|
enum class MaskKind {
|
|
|
|
Alpha,
|
|
|
|
Luminance
|
|
|
|
};
|
|
|
|
|
|
|
|
void apply_mask(Gfx::Bitmap const& mask, MaskKind);
|
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
~Bitmap();
|
2019-01-09 01:06:04 +00:00
|
|
|
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] u8* scanline_u8(int physical_y);
|
2021-11-06 12:29:13 +00:00
|
|
|
[[nodiscard]] u8 const* scanline_u8(int physical_y) const;
|
2022-03-04 21:05:20 +00:00
|
|
|
[[nodiscard]] ARGB32* scanline(int physical_y);
|
|
|
|
[[nodiscard]] ARGB32 const* scanline(int physical_y) const;
|
2019-01-09 01:06:04 +00:00
|
|
|
|
2024-09-07 11:21:51 +00:00
|
|
|
[[nodiscard]] u8* unchecked_scanline_u8(int physical_y);
|
|
|
|
[[nodiscard]] u8 const* unchecked_scanline_u8(int physical_y) const;
|
|
|
|
[[nodiscard]] ARGB32* unchecked_scanline(int physical_y);
|
|
|
|
[[nodiscard]] ARGB32 const* unchecked_scanline(int physical_y) const;
|
|
|
|
|
2023-04-03 18:29:11 +00:00
|
|
|
[[nodiscard]] ARGB32* begin();
|
2024-05-04 17:38:26 +00:00
|
|
|
[[nodiscard]] ARGB32 const* begin() const;
|
2023-04-03 18:29:11 +00:00
|
|
|
[[nodiscard]] ARGB32* end();
|
2024-05-04 17:38:26 +00:00
|
|
|
[[nodiscard]] ARGB32 const* end() const;
|
2024-01-24 16:43:39 +00:00
|
|
|
[[nodiscard]] size_t data_size() const;
|
2023-04-03 18:29:11 +00:00
|
|
|
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] IntRect rect() const { return { {}, m_size }; }
|
|
|
|
[[nodiscard]] IntSize size() const { return m_size; }
|
|
|
|
[[nodiscard]] int width() const { return m_size.width(); }
|
|
|
|
[[nodiscard]] int height() const { return m_size.height(); }
|
2021-01-19 17:10:47 +00:00
|
|
|
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] size_t pitch() const { return m_pitch; }
|
2019-12-08 16:07:44 +00:00
|
|
|
|
2024-06-05 06:17:28 +00:00
|
|
|
[[nodiscard]] static size_t minimum_pitch(size_t width, BitmapFormat);
|
2020-09-06 21:59:20 +00:00
|
|
|
|
2022-02-14 16:30:34 +00:00
|
|
|
[[nodiscard]] bool has_alpha_channel() const { return m_format == BitmapFormat::BGRA8888 || m_format == BitmapFormat::RGBA8888; }
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] BitmapFormat format() const { return m_format; }
|
2019-02-19 00:42:53 +00:00
|
|
|
|
2023-06-13 23:23:38 +00:00
|
|
|
// Call only for BGRx8888 and BGRA8888 bitmaps.
|
|
|
|
void strip_alpha_channel();
|
|
|
|
|
2024-06-05 06:17:28 +00:00
|
|
|
[[nodiscard]] static constexpr size_t size_in_bytes(size_t pitch, int height) { return pitch * height; }
|
|
|
|
[[nodiscard]] size_t size_in_bytes() const { return size_in_bytes(m_pitch, height()); }
|
2019-05-06 12:04:54 +00:00
|
|
|
|
2024-09-07 11:21:51 +00:00
|
|
|
template<StorageFormat>
|
|
|
|
[[nodiscard]] Color unchecked_get_pixel(int physical_x, int physical_y) const;
|
|
|
|
|
2020-09-06 21:59:20 +00:00
|
|
|
template<StorageFormat>
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] Color get_pixel(int physical_x, int physical_y) const;
|
|
|
|
[[nodiscard]] Color get_pixel(int physical_x, int physical_y) const;
|
2022-12-06 20:27:44 +00:00
|
|
|
[[nodiscard]] Color get_pixel(IntPoint physical_position) const
|
2019-06-14 17:10:59 +00:00
|
|
|
{
|
2021-01-19 17:10:47 +00:00
|
|
|
return get_pixel(physical_position.x(), physical_position.y());
|
2019-06-14 17:10:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 21:59:20 +00:00
|
|
|
template<StorageFormat>
|
2021-01-19 17:10:47 +00:00
|
|
|
void set_pixel(int physical_x, int physical_y, Color);
|
|
|
|
void set_pixel(int physical_x, int physical_y, Color);
|
2022-12-06 20:27:44 +00:00
|
|
|
void set_pixel(IntPoint physical_position, Color color)
|
2019-06-14 17:10:59 +00:00
|
|
|
{
|
2021-01-19 17:10:47 +00:00
|
|
|
set_pixel(physical_position.x(), physical_position.y(), color);
|
2019-06-14 17:10:59 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] Core::AnonymousBuffer& anonymous_buffer() { return m_buffer; }
|
|
|
|
[[nodiscard]] Core::AnonymousBuffer const& anonymous_buffer() const { return m_buffer; }
|
2021-01-15 11:09:37 +00:00
|
|
|
|
2022-04-14 23:07:15 +00:00
|
|
|
[[nodiscard]] bool visually_equals(Bitmap const&) const;
|
|
|
|
|
LibGfx: Store alpha type information in `Gfx::Bitmap`
We use instances of `Gfx::Bitmap` to move pixel data all the way from
raw image bytes up to the Skia renderer. A vital piece of information
for correct blending of bitmaps is the alpha type, i.e. are we dealing
with premultiplied or unpremultiplied color values?
Premultiplied means that the RGB colors have been multiplied with the
associated alpha value, i.e. RGB(255, 255, 255) with an alpha of 2% is
stored as RGBA(5, 5, 5, 2%).
Unpremultiplied means that the original RGB colors are stored,
regardless of the alpha value. I.e. RGB(255, 255, 255) with an alpha of
2% is stored as RGBA(255, 255, 255, 2%).
It is important to know how the color data is stored in a
`Gfx::Bitmap`, because correct blending depends on knowing the alpha
type: premultiplied blending uses `S + (1 - A) * D`, while
unpremultiplied blending uses `A * S + (1 - A) * D`.
This adds the alpha type information to `Gfx::Bitmap` across the board.
It isn't used anywhere yet.
2024-08-02 10:52:14 +00:00
|
|
|
[[nodiscard]] AlphaType alpha_type() const { return m_alpha_type; }
|
|
|
|
|
2019-01-09 01:06:04 +00:00
|
|
|
private:
|
LibGfx: Store alpha type information in `Gfx::Bitmap`
We use instances of `Gfx::Bitmap` to move pixel data all the way from
raw image bytes up to the Skia renderer. A vital piece of information
for correct blending of bitmaps is the alpha type, i.e. are we dealing
with premultiplied or unpremultiplied color values?
Premultiplied means that the RGB colors have been multiplied with the
associated alpha value, i.e. RGB(255, 255, 255) with an alpha of 2% is
stored as RGBA(5, 5, 5, 2%).
Unpremultiplied means that the original RGB colors are stored,
regardless of the alpha value. I.e. RGB(255, 255, 255) with an alpha of
2% is stored as RGBA(255, 255, 255, 2%).
It is important to know how the color data is stored in a
`Gfx::Bitmap`, because correct blending depends on knowing the alpha
type: premultiplied blending uses `S + (1 - A) * D`, while
unpremultiplied blending uses `A * S + (1 - A) * D`.
This adds the alpha type information to `Gfx::Bitmap` across the board.
It isn't used anywhere yet.
2024-08-02 10:52:14 +00:00
|
|
|
Bitmap(BitmapFormat, AlphaType, IntSize, BackingStore const&);
|
|
|
|
Bitmap(BitmapFormat, AlphaType, IntSize, size_t pitch, void*, Function<void()>&& destruction_callback);
|
|
|
|
Bitmap(BitmapFormat, AlphaType, Core::AnonymousBuffer, IntSize);
|
2019-01-09 01:06:04 +00:00
|
|
|
|
2024-06-05 06:17:28 +00:00
|
|
|
static ErrorOr<BackingStore> allocate_backing_store(BitmapFormat format, IntSize size);
|
2020-09-12 16:17:50 +00:00
|
|
|
|
2020-06-10 08:57:59 +00:00
|
|
|
IntSize m_size;
|
2020-09-06 21:59:20 +00:00
|
|
|
void* m_data { nullptr };
|
2019-01-12 20:29:05 +00:00
|
|
|
size_t m_pitch { 0 };
|
2020-02-14 22:02:47 +00:00
|
|
|
BitmapFormat m_format { BitmapFormat::Invalid };
|
LibGfx: Store alpha type information in `Gfx::Bitmap`
We use instances of `Gfx::Bitmap` to move pixel data all the way from
raw image bytes up to the Skia renderer. A vital piece of information
for correct blending of bitmaps is the alpha type, i.e. are we dealing
with premultiplied or unpremultiplied color values?
Premultiplied means that the RGB colors have been multiplied with the
associated alpha value, i.e. RGB(255, 255, 255) with an alpha of 2% is
stored as RGBA(5, 5, 5, 2%).
Unpremultiplied means that the original RGB colors are stored,
regardless of the alpha value. I.e. RGB(255, 255, 255) with an alpha of
2% is stored as RGBA(255, 255, 255, 2%).
It is important to know how the color data is stored in a
`Gfx::Bitmap`, because correct blending depends on knowing the alpha
type: premultiplied blending uses `S + (1 - A) * D`, while
unpremultiplied blending uses `A * S + (1 - A) * D`.
This adds the alpha type information to `Gfx::Bitmap` across the board.
It isn't used anywhere yet.
2024-08-02 10:52:14 +00:00
|
|
|
AlphaType m_alpha_type { AlphaType::Premultiplied };
|
2021-05-24 10:24:38 +00:00
|
|
|
Core::AnonymousBuffer m_buffer;
|
2024-06-17 23:11:35 +00:00
|
|
|
Function<void()> m_destruction_callback;
|
2019-01-09 01:06:04 +00:00
|
|
|
};
|
2019-01-16 18:43:01 +00:00
|
|
|
|
2024-09-07 11:21:51 +00:00
|
|
|
ALWAYS_INLINE u8* Bitmap::unchecked_scanline_u8(int y)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<u8*>(m_data) + (y * m_pitch);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE u8 const* Bitmap::unchecked_scanline_u8(int y) const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<u8 const*>(m_data) + (y * m_pitch);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE ARGB32* Bitmap::unchecked_scanline(int y)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<ARGB32*>(unchecked_scanline_u8(y));
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE ARGB32 const* Bitmap::unchecked_scanline(int y) const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<ARGB32 const*>(unchecked_scanline_u8(y));
|
|
|
|
}
|
|
|
|
|
2023-01-17 21:13:19 +00:00
|
|
|
ALWAYS_INLINE u8* Bitmap::scanline_u8(int y)
|
2020-06-17 19:37:16 +00:00
|
|
|
{
|
2022-12-19 08:53:57 +00:00
|
|
|
VERIFY(y >= 0);
|
2024-06-05 06:17:28 +00:00
|
|
|
VERIFY(y < height());
|
2024-09-07 11:21:51 +00:00
|
|
|
return unchecked_scanline_u8(y);
|
2020-06-17 19:37:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 21:13:19 +00:00
|
|
|
ALWAYS_INLINE u8 const* Bitmap::scanline_u8(int y) const
|
2020-06-17 19:37:16 +00:00
|
|
|
{
|
2022-12-19 08:53:57 +00:00
|
|
|
VERIFY(y >= 0);
|
2024-06-05 06:17:28 +00:00
|
|
|
VERIFY(y < height());
|
2024-09-07 11:21:51 +00:00
|
|
|
return unchecked_scanline_u8(y);
|
2020-06-17 19:37:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 21:13:19 +00:00
|
|
|
ALWAYS_INLINE ARGB32* Bitmap::scanline(int y)
|
2019-01-16 18:43:01 +00:00
|
|
|
{
|
2022-03-04 21:05:20 +00:00
|
|
|
return reinterpret_cast<ARGB32*>(scanline_u8(y));
|
2019-01-16 18:43:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 21:13:19 +00:00
|
|
|
ALWAYS_INLINE ARGB32 const* Bitmap::scanline(int y) const
|
2019-01-16 18:43:01 +00:00
|
|
|
{
|
2022-03-04 21:05:20 +00:00
|
|
|
return reinterpret_cast<ARGB32 const*>(scanline_u8(y));
|
2019-01-16 18:43:01 +00:00
|
|
|
}
|
2019-05-06 17:32:56 +00:00
|
|
|
|
2023-04-03 18:29:11 +00:00
|
|
|
ALWAYS_INLINE ARGB32* Bitmap::begin()
|
|
|
|
{
|
|
|
|
return scanline(0);
|
|
|
|
}
|
|
|
|
|
2024-05-04 17:38:26 +00:00
|
|
|
ALWAYS_INLINE ARGB32 const* Bitmap::begin() const
|
|
|
|
{
|
|
|
|
return scanline(0);
|
|
|
|
}
|
|
|
|
|
2023-04-03 18:29:11 +00:00
|
|
|
ALWAYS_INLINE ARGB32* Bitmap::end()
|
|
|
|
{
|
2024-01-24 16:43:39 +00:00
|
|
|
return reinterpret_cast<ARGB32*>(reinterpret_cast<u8*>(m_data) + data_size());
|
|
|
|
}
|
|
|
|
|
2024-05-04 17:38:26 +00:00
|
|
|
ALWAYS_INLINE ARGB32 const* Bitmap::end() const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<ARGB32 const*>(reinterpret_cast<u8 const*>(m_data) + data_size());
|
|
|
|
}
|
|
|
|
|
2024-01-24 16:43:39 +00:00
|
|
|
ALWAYS_INLINE size_t Bitmap::data_size() const
|
|
|
|
{
|
|
|
|
return m_size.height() * m_pitch;
|
2023-04-03 18:29:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 09:06:02 +00:00
|
|
|
template<>
|
2024-09-07 11:21:51 +00:00
|
|
|
ALWAYS_INLINE Color Bitmap::unchecked_get_pixel<StorageFormat::BGRx8888>(int x, int y) const
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
2024-09-07 11:21:51 +00:00
|
|
|
return Color::from_rgb(unchecked_scanline(y)[x]);
|
2019-06-15 09:06:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2024-09-07 11:21:51 +00:00
|
|
|
ALWAYS_INLINE Color Bitmap::unchecked_get_pixel<StorageFormat::BGRA8888>(int x, int y) const
|
|
|
|
{
|
|
|
|
return Color::from_argb(unchecked_scanline(y)[x]);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<StorageFormat storage_format>
|
|
|
|
ALWAYS_INLINE Color Bitmap::get_pixel(int x, int y) const
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
2022-12-19 08:53:57 +00:00
|
|
|
VERIFY(x >= 0);
|
2024-06-05 06:17:28 +00:00
|
|
|
VERIFY(x < width());
|
2024-09-07 11:21:51 +00:00
|
|
|
return unchecked_get_pixel<storage_format>(x, y);
|
2019-06-15 09:06:02 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 21:13:19 +00:00
|
|
|
ALWAYS_INLINE Color Bitmap::get_pixel(int x, int y) const
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
2020-09-06 21:59:20 +00:00
|
|
|
switch (determine_storage_format(m_format)) {
|
2021-03-16 11:00:43 +00:00
|
|
|
case StorageFormat::BGRx8888:
|
|
|
|
return get_pixel<StorageFormat::BGRx8888>(x, y);
|
|
|
|
case StorageFormat::BGRA8888:
|
|
|
|
return get_pixel<StorageFormat::BGRA8888>(x, y);
|
2019-06-15 09:06:02 +00:00
|
|
|
default:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-06-15 09:06:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2023-01-17 21:13:19 +00:00
|
|
|
ALWAYS_INLINE void Bitmap::set_pixel<StorageFormat::BGRx8888>(int x, int y, Color color)
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
2022-12-19 08:53:57 +00:00
|
|
|
VERIFY(x >= 0);
|
2024-06-05 06:17:28 +00:00
|
|
|
VERIFY(x < width());
|
2019-06-15 09:06:02 +00:00
|
|
|
scanline(y)[x] = color.value();
|
|
|
|
}
|
2023-01-17 21:13:19 +00:00
|
|
|
|
2019-06-15 09:06:02 +00:00
|
|
|
template<>
|
2023-01-17 21:13:19 +00:00
|
|
|
ALWAYS_INLINE void Bitmap::set_pixel<StorageFormat::BGRA8888>(int x, int y, Color color)
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
2022-12-19 08:53:57 +00:00
|
|
|
VERIFY(x >= 0);
|
2024-06-05 06:17:28 +00:00
|
|
|
VERIFY(x < width());
|
2020-09-06 21:59:20 +00:00
|
|
|
scanline(y)[x] = color.value(); // drop alpha
|
2019-06-15 09:06:02 +00:00
|
|
|
}
|
2023-01-17 21:13:19 +00:00
|
|
|
|
2022-03-04 18:03:55 +00:00
|
|
|
template<>
|
2023-01-17 21:13:19 +00:00
|
|
|
ALWAYS_INLINE void Bitmap::set_pixel<StorageFormat::RGBA8888>(int x, int y, Color color)
|
2022-03-04 18:03:55 +00:00
|
|
|
{
|
2022-12-19 08:53:57 +00:00
|
|
|
VERIFY(x >= 0);
|
2024-06-05 06:17:28 +00:00
|
|
|
VERIFY(x < width());
|
2022-03-04 18:03:55 +00:00
|
|
|
// FIXME: There's a lot of inaccurately named functions in the Color class right now (RGBA vs BGRA),
|
|
|
|
// clear those up and then make this more convenient.
|
|
|
|
auto rgba = (color.alpha() << 24) | (color.blue() << 16) | (color.green() << 8) | color.red();
|
|
|
|
scanline(y)[x] = rgba;
|
|
|
|
}
|
2023-01-17 21:13:19 +00:00
|
|
|
|
2024-11-02 23:12:14 +00:00
|
|
|
template<>
|
|
|
|
ALWAYS_INLINE void Bitmap::set_pixel<StorageFormat::RGBx8888>(int x, int y, Color color)
|
|
|
|
{
|
|
|
|
VERIFY(x >= 0);
|
|
|
|
VERIFY(x < width());
|
|
|
|
auto rgb = (color.blue() << 16) | (color.green() << 8) | color.red();
|
|
|
|
scanline(y)[x] = rgb;
|
|
|
|
}
|
|
|
|
|
2023-01-17 21:13:19 +00:00
|
|
|
ALWAYS_INLINE void Bitmap::set_pixel(int x, int y, Color color)
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
2020-09-06 21:59:20 +00:00
|
|
|
switch (determine_storage_format(m_format)) {
|
2021-03-16 11:00:43 +00:00
|
|
|
case StorageFormat::BGRx8888:
|
|
|
|
set_pixel<StorageFormat::BGRx8888>(x, y, color);
|
2019-06-15 09:06:02 +00:00
|
|
|
break;
|
2021-03-16 11:00:43 +00:00
|
|
|
case StorageFormat::BGRA8888:
|
|
|
|
set_pixel<StorageFormat::BGRA8888>(x, y, color);
|
2019-06-15 09:06:02 +00:00
|
|
|
break;
|
2022-03-04 18:03:55 +00:00
|
|
|
case StorageFormat::RGBA8888:
|
|
|
|
set_pixel<StorageFormat::RGBA8888>(x, y, color);
|
|
|
|
break;
|
2024-11-02 23:12:14 +00:00
|
|
|
case StorageFormat::RGBx8888:
|
|
|
|
set_pixel<StorageFormat::RGBx8888>(x, y, color);
|
|
|
|
break;
|
2019-06-15 09:06:02 +00:00
|
|
|
default:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-06-15 09:06:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-06 10:56:38 +00:00
|
|
|
|
|
|
|
}
|