2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-11-06 18:53:32 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
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
|
|
|
|
|
2020-02-14 22:02:47 +00:00
|
|
|
#include <AK/Forward.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
|
|
|
|
2021-05-15 15:02:40 +00:00
|
|
|
#define ENUMERATE_IMAGE_FORMATS \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(pbm, ".pbm") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(pgm, ".pgm") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(png, ".png") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(ppm, ".ppm") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(gif, ".gif") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(bmp, ".bmp") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(ico, ".ico") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(jpg, ".jpg") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(jpg, ".jpeg") \
|
2021-12-21 00:17:13 +00:00
|
|
|
__ENUMERATE_IMAGE_FORMAT(dds, ".dds") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(qoi, ".qoi")
|
2020-06-15 12:13:12 +00:00
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
namespace Gfx {
|
|
|
|
|
2020-02-14 22:02:47 +00:00
|
|
|
enum class BitmapFormat {
|
|
|
|
Invalid,
|
2020-06-17 19:37:16 +00:00
|
|
|
Indexed1,
|
|
|
|
Indexed2,
|
|
|
|
Indexed4,
|
|
|
|
Indexed8,
|
2021-03-16 10:48:42 +00:00
|
|
|
BGRx8888,
|
|
|
|
BGRA8888,
|
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:
|
|
|
|
case (unsigned)BitmapFormat::Indexed1:
|
|
|
|
case (unsigned)BitmapFormat::Indexed2:
|
|
|
|
case (unsigned)BitmapFormat::Indexed4:
|
|
|
|
case (unsigned)BitmapFormat::Indexed8:
|
2021-03-16 10:48:42 +00:00
|
|
|
case (unsigned)BitmapFormat::BGRx8888:
|
|
|
|
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 {
|
|
|
|
Indexed8,
|
2021-03-16 11:00:43 +00:00
|
|
|
BGRx8888,
|
|
|
|
BGRA8888,
|
2021-03-16 11:09:15 +00:00
|
|
|
RGBA8888,
|
2020-09-06 21:59:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static StorageFormat determine_storage_format(BitmapFormat format)
|
|
|
|
{
|
|
|
|
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;
|
2020-09-06 21:59:20 +00:00
|
|
|
case BitmapFormat::Indexed1:
|
|
|
|
case BitmapFormat::Indexed2:
|
|
|
|
case BitmapFormat::Indexed4:
|
|
|
|
case BitmapFormat::Indexed8:
|
|
|
|
return StorageFormat::Indexed8;
|
|
|
|
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-04-12 10:19:18 +00:00
|
|
|
enum RotationDirection {
|
2021-05-03 05:38:29 +00:00
|
|
|
CounterClockwise,
|
|
|
|
Clockwise
|
2020-04-12 10:19:18 +00:00
|
|
|
};
|
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
class Bitmap : public RefCounted<Bitmap> {
|
2019-01-09 01:06:04 +00:00
|
|
|
public:
|
2021-11-06 18:30:59 +00:00
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_create(BitmapFormat, IntSize const&, int intrinsic_scale = 1);
|
2021-11-06 10:44:05 +00:00
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_create_shareable(BitmapFormat, IntSize const&, int intrinsic_scale = 1);
|
2021-11-06 10:38:14 +00:00
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_create_wrapper(BitmapFormat, IntSize const&, int intrinsic_scale, size_t pitch, void*);
|
2021-11-06 15:25:29 +00:00
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_load_from_file(String const& path, int scale_factor = 1);
|
2021-11-14 20:42:12 +00:00
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_load_from_fd_and_close(int fd, String const& path);
|
2022-03-04 21:05:20 +00:00
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize const&, int intrinsic_scale, Vector<ARGB32> const& palette);
|
2021-11-06 18:53:32 +00:00
|
|
|
static ErrorOr<NonnullRefPtr<Bitmap>> try_create_from_serialized_byte_buffer(ByteBuffer&&);
|
2021-07-21 16:02:15 +00:00
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
static bool is_path_a_supported_image_format(StringView path)
|
2020-06-15 12:13:12 +00:00
|
|
|
{
|
2020-11-13 00:25:27 +00:00
|
|
|
#define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \
|
|
|
|
if (path.ends_with(Ext, CaseSensitivity::CaseInsensitive)) \
|
2020-06-15 12:13:12 +00:00
|
|
|
return true;
|
|
|
|
ENUMERATE_IMAGE_FORMATS
|
|
|
|
#undef __ENUMERATE_IMAGE_FORMAT
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
|
2021-11-06 10:56:44 +00:00
|
|
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> rotated(Gfx::RotationDirection) const;
|
2021-11-06 10:58:00 +00:00
|
|
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> flipped(Gfx::Orientation) const;
|
2021-11-06 11:26:04 +00:00
|
|
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> scaled(int sx, int sy) const;
|
|
|
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> scaled(float sx, float sy) const;
|
2021-11-06 12:11:20 +00:00
|
|
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> cropped(Gfx::IntRect) const;
|
2021-11-06 12:15:43 +00:00
|
|
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_bitmap_backed_by_anonymous_buffer() const;
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] ByteBuffer serialize_to_byte_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
|
|
|
|
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
|
|
|
|
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(); }
|
|
|
|
[[nodiscard]] int scale() const { return m_scale; }
|
2021-01-19 17:10:47 +00:00
|
|
|
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] IntRect physical_rect() const { return rect() * scale(); }
|
|
|
|
[[nodiscard]] IntSize physical_size() const { return size() * scale(); }
|
|
|
|
[[nodiscard]] int physical_width() const { return physical_size().width(); }
|
|
|
|
[[nodiscard]] int physical_height() const { return physical_size().height(); }
|
|
|
|
[[nodiscard]] size_t pitch() const { return m_pitch; }
|
2019-12-08 16:07:44 +00:00
|
|
|
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE bool is_indexed() const
|
2020-06-22 19:35:22 +00:00
|
|
|
{
|
|
|
|
return is_indexed(m_format);
|
|
|
|
}
|
|
|
|
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE static bool is_indexed(BitmapFormat format)
|
2020-06-17 19:37:16 +00:00
|
|
|
{
|
|
|
|
return format == BitmapFormat::Indexed8 || format == BitmapFormat::Indexed4
|
|
|
|
|| format == BitmapFormat::Indexed2 || format == BitmapFormat::Indexed1;
|
|
|
|
}
|
|
|
|
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] static size_t palette_size(BitmapFormat format)
|
2020-06-22 19:35:22 +00:00
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case BitmapFormat::Indexed1:
|
|
|
|
return 2;
|
|
|
|
case BitmapFormat::Indexed2:
|
|
|
|
return 4;
|
|
|
|
case BitmapFormat::Indexed4:
|
|
|
|
return 16;
|
|
|
|
case BitmapFormat::Indexed8:
|
|
|
|
return 256;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 21:05:20 +00:00
|
|
|
[[nodiscard]] Vector<ARGB32> palette_to_vector() const;
|
2020-06-22 19:35:22 +00:00
|
|
|
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] static unsigned bpp_for_format(BitmapFormat format)
|
2019-06-25 18:33:24 +00:00
|
|
|
{
|
2020-04-15 09:57:24 +00:00
|
|
|
switch (format) {
|
2020-06-17 19:37:16 +00:00
|
|
|
case BitmapFormat::Indexed1:
|
|
|
|
return 1;
|
|
|
|
case BitmapFormat::Indexed2:
|
|
|
|
return 2;
|
|
|
|
case BitmapFormat::Indexed4:
|
|
|
|
return 4;
|
2020-02-14 22:02:47 +00:00
|
|
|
case BitmapFormat::Indexed8:
|
2019-06-25 18:33:24 +00:00
|
|
|
return 8;
|
2021-03-16 10:48:42 +00:00
|
|
|
case BitmapFormat::BGRx8888:
|
|
|
|
case BitmapFormat::BGRA8888:
|
2019-06-25 18:33:24 +00:00
|
|
|
return 32;
|
|
|
|
default:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-02-14 22:02:47 +00:00
|
|
|
case BitmapFormat::Invalid:
|
2019-09-29 19:02:49 +00:00
|
|
|
return 0;
|
2019-06-25 18:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] static size_t minimum_pitch(size_t physical_width, BitmapFormat);
|
2020-09-06 21:59:20 +00:00
|
|
|
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] unsigned bpp() const
|
2020-04-15 09:57:24 +00:00
|
|
|
{
|
|
|
|
return bpp_for_format(m_format);
|
|
|
|
}
|
|
|
|
|
2019-06-10 17:29:50 +00:00
|
|
|
void fill(Color);
|
|
|
|
|
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
|
|
|
|
2021-04-16 21:40:49 +00:00
|
|
|
void set_mmap_name(String const&);
|
2019-04-30 11:46:03 +00:00
|
|
|
|
2021-07-21 16:08:42 +00:00
|
|
|
[[nodiscard]] static constexpr size_t size_in_bytes(size_t pitch, int physical_height) { return pitch * physical_height; }
|
|
|
|
[[nodiscard]] size_t size_in_bytes() const { return size_in_bytes(m_pitch, physical_height()); }
|
2019-05-06 12:04:54 +00:00
|
|
|
|
2022-03-04 21:28:59 +00:00
|
|
|
[[nodiscard]] Color palette_color(u8 index) const { return Color::from_argb(m_palette[index]); }
|
2019-07-03 19:17:35 +00:00
|
|
|
void set_palette_color(u8 index, Color color) { m_palette[index] = color.value(); }
|
2019-05-06 17:32:56 +00:00
|
|
|
|
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;
|
2021-11-06 12:29:13 +00:00
|
|
|
[[nodiscard]] Color get_pixel(IntPoint const& 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);
|
2021-11-06 12:29:13 +00:00
|
|
|
void set_pixel(IntPoint const& 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]] bool is_volatile() const { return m_volatile; }
|
2019-12-18 19:50:05 +00:00
|
|
|
void set_volatile();
|
2021-07-24 20:49:48 +00:00
|
|
|
|
|
|
|
// Returns true if making the bitmap non-volatile succeeded. `was_purged` indicates status of contents.
|
|
|
|
// Returns false if there was not enough memory.
|
|
|
|
[[nodiscard]] bool set_nonvolatile(bool& was_purged);
|
2019-12-18 19:50:05 +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;
|
|
|
|
|
2019-01-09 01:06:04 +00:00
|
|
|
private:
|
2021-07-24 16:31:59 +00:00
|
|
|
Bitmap(BitmapFormat, IntSize const&, int, BackingStore const&);
|
2021-11-06 12:29:13 +00:00
|
|
|
Bitmap(BitmapFormat, IntSize const&, int, size_t pitch, void*);
|
2022-03-04 21:05:20 +00:00
|
|
|
Bitmap(BitmapFormat, Core::AnonymousBuffer, IntSize const&, int, Vector<ARGB32> const& palette);
|
2019-01-09 01:06:04 +00:00
|
|
|
|
2021-11-06 09:15:13 +00:00
|
|
|
static ErrorOr<BackingStore> allocate_backing_store(BitmapFormat format, IntSize const& size, int scale_factor);
|
2020-09-12 16:17:50 +00:00
|
|
|
|
2022-03-04 21:05:20 +00:00
|
|
|
void allocate_palette_from_format(BitmapFormat, Vector<ARGB32> const& source_palette);
|
2020-06-17 19:37:16 +00:00
|
|
|
|
2020-06-10 08:57:59 +00:00
|
|
|
IntSize m_size;
|
2021-01-19 17:10:47 +00:00
|
|
|
int m_scale;
|
2020-09-06 21:59:20 +00:00
|
|
|
void* m_data { nullptr };
|
2022-03-04 21:05:20 +00:00
|
|
|
ARGB32* m_palette { 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 };
|
2019-04-26 16:25:05 +00:00
|
|
|
bool m_needs_munmap { false };
|
2019-12-18 19:50:05 +00:00
|
|
|
bool m_volatile { false };
|
2021-05-24 10:24:38 +00:00
|
|
|
Core::AnonymousBuffer m_buffer;
|
2019-01-09 01:06:04 +00:00
|
|
|
};
|
2019-01-16 18:43:01 +00:00
|
|
|
|
2020-06-17 19:37:16 +00:00
|
|
|
inline u8* Bitmap::scanline_u8(int y)
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(y >= 0 && y < physical_height());
|
2020-09-06 21:59:20 +00:00
|
|
|
return reinterpret_cast<u8*>(m_data) + (y * m_pitch);
|
2020-06-17 19:37:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 12:29:13 +00:00
|
|
|
inline u8 const* Bitmap::scanline_u8(int y) const
|
2020-06-17 19:37:16 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(y >= 0 && y < physical_height());
|
2021-11-06 12:29:13 +00:00
|
|
|
return reinterpret_cast<u8 const*>(m_data) + (y * m_pitch);
|
2020-06-17 19:37:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 21:05:20 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-04 21:05:20 +00:00
|
|
|
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
|
|
|
|
2019-06-15 09:06:02 +00:00
|
|
|
template<>
|
2021-03-16 11:00:43 +00:00
|
|
|
inline Color Bitmap::get_pixel<StorageFormat::BGRx8888>(int x, int y) const
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(x >= 0 && x < physical_width());
|
2019-06-15 09:06:02 +00:00
|
|
|
return Color::from_rgb(scanline(y)[x]);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2021-03-16 11:00:43 +00:00
|
|
|
inline Color Bitmap::get_pixel<StorageFormat::BGRA8888>(int x, int y) const
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(x >= 0 && x < physical_width());
|
2022-03-04 21:28:59 +00:00
|
|
|
return Color::from_argb(scanline(y)[x]);
|
2019-06-15 09:06:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 19:37:16 +00:00
|
|
|
template<>
|
2020-09-06 21:59:20 +00:00
|
|
|
inline Color Bitmap::get_pixel<StorageFormat::Indexed8>(int x, int y) const
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(x >= 0 && x < physical_width());
|
2020-09-05 23:10:33 +00:00
|
|
|
return Color::from_rgb(m_palette[scanline_u8(y)[x]]);
|
2019-06-15 09:06:02 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
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);
|
2020-09-06 21:59:20 +00:00
|
|
|
case StorageFormat::Indexed8:
|
|
|
|
return get_pixel<StorageFormat::Indexed8>(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<>
|
2021-03-16 11:00:43 +00:00
|
|
|
inline void Bitmap::set_pixel<StorageFormat::BGRx8888>(int x, int y, Color color)
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(x >= 0 && x < physical_width());
|
2019-06-15 09:06:02 +00:00
|
|
|
scanline(y)[x] = color.value();
|
|
|
|
}
|
|
|
|
template<>
|
2021-03-16 11:00:43 +00:00
|
|
|
inline void Bitmap::set_pixel<StorageFormat::BGRA8888>(int x, int y, Color color)
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(x >= 0 && x < physical_width());
|
2020-09-06 21:59:20 +00:00
|
|
|
scanline(y)[x] = color.value(); // drop alpha
|
2019-06-15 09:06:02 +00:00
|
|
|
}
|
2022-03-04 18:03:55 +00:00
|
|
|
template<>
|
|
|
|
inline void Bitmap::set_pixel<StorageFormat::RGBA8888>(int x, int y, Color color)
|
|
|
|
{
|
|
|
|
VERIFY(x >= 0 && x < physical_width());
|
|
|
|
// 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;
|
|
|
|
}
|
2020-02-06 10:56:38 +00:00
|
|
|
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;
|
2020-09-06 21:59:20 +00:00
|
|
|
case StorageFormat::Indexed8:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
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
|
|
|
|
|
|
|
}
|