2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2023-02-20 18:03:29 +00:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.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
|
|
|
|
|
2020-02-14 22:02:47 +00:00
|
|
|
#include <AK/Forward.h>
|
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>
|
2023-01-15 00:30:07 +00:00
|
|
|
#include <LibCore/Forward.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
|
|
|
|
2023-02-18 21:19:15 +00:00
|
|
|
#define ENUMERATE_IMAGE_FORMATS \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(bmp, ".bmp") \
|
2023-10-29 04:24:17 +00:00
|
|
|
__ENUMERATE_IMAGE_FORMAT(dds, ".dds") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(gif, ".gif") \
|
2023-02-18 21:19:15 +00:00
|
|
|
__ENUMERATE_IMAGE_FORMAT(ico, ".ico") \
|
2023-07-29 14:24:01 +00:00
|
|
|
__ENUMERATE_IMAGE_FORMAT(iff, ".iff") \
|
2023-02-18 21:19:15 +00:00
|
|
|
__ENUMERATE_IMAGE_FORMAT(jpeg, ".jpeg") \
|
2023-10-29 04:24:17 +00:00
|
|
|
__ENUMERATE_IMAGE_FORMAT(jpeg, ".jpg") \
|
2023-07-04 04:16:52 +00:00
|
|
|
__ENUMERATE_IMAGE_FORMAT(jxl, ".jxl") \
|
2023-12-08 12:50:53 +00:00
|
|
|
__ENUMERATE_IMAGE_FORMAT(iff, ".lbm") \
|
2023-10-29 04:24:17 +00:00
|
|
|
__ENUMERATE_IMAGE_FORMAT(pbm, ".pbm") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(pgm, ".pgm") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(png, ".png") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(ppm, ".ppm") \
|
2023-02-18 21:19:15 +00:00
|
|
|
__ENUMERATE_IMAGE_FORMAT(qoi, ".qoi") \
|
2023-07-02 21:33:15 +00:00
|
|
|
__ENUMERATE_IMAGE_FORMAT(tga, ".tga") \
|
2023-10-29 04:27:37 +00:00
|
|
|
__ENUMERATE_IMAGE_FORMAT(tiff, ".tif") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(tiff, ".tiff") \
|
2023-07-14 22:56:27 +00:00
|
|
|
__ENUMERATE_IMAGE_FORMAT(tvg, ".tvg") \
|
|
|
|
__ENUMERATE_IMAGE_FORMAT(tvg, ".webp")
|
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,
|
2021-03-16 10:48:42 +00:00
|
|
|
BGRx8888,
|
|
|
|
BGRA8888,
|
2021-03-16 11:09:15 +00:00
|
|
|
RGBA8888,
|
2023-10-11 10:35:50 +00:00
|
|
|
|
|
|
|
FirstValid = BGRx8888,
|
|
|
|
LastValid = 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:
|
|
|
|
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,
|
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;
|
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-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:
|
2023-01-20 19:06:05 +00:00
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create(BitmapFormat, IntSize, int intrinsic_scale = 1);
|
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_shareable(BitmapFormat, IntSize, int intrinsic_scale = 1);
|
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_wrapper(BitmapFormat, IntSize, int intrinsic_scale, size_t pitch, void*);
|
2023-07-02 21:36:35 +00:00
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(StringView path, int scale_factor = 1, Optional<IntSize> ideal_size = {});
|
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(NonnullOwnPtr<Core::File>, StringView path, Optional<IntSize> ideal_size = {});
|
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_bytes(ReadonlyBytes, Optional<IntSize> ideal_size = {}, Optional<DeprecatedString> mine_type = {});
|
2023-10-11 10:35:50 +00:00
|
|
|
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize, int intrinsic_scale);
|
2023-01-20 19:06:05 +00:00
|
|
|
static ErrorOr<NonnullRefPtr<Bitmap>> create_from_serialized_bytes(ReadonlyBytes);
|
|
|
|
static ErrorOr<NonnullRefPtr<Bitmap>> 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
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
#define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \
|
|
|
|
if (path.ends_with(Ext##sv, 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;
|
2023-10-18 17:29:52 +00:00
|
|
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> scaled_to_size(Gfx::IntSize) const;
|
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;
|
2023-02-08 17:21:41 +00:00
|
|
|
[[nodiscard]] ErrorOr<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
|
|
|
|
2023-02-19 21:43:49 +00:00
|
|
|
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> inverted() const;
|
2022-05-08 23:00:13 +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
|
|
|
|
2023-04-03 18:29:11 +00:00
|
|
|
[[nodiscard]] ARGB32* begin();
|
|
|
|
[[nodiscard]] ARGB32* end();
|
|
|
|
|
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]] 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) {
|
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; }
|
2023-11-23 01:31:38 +00:00
|
|
|
void add_alpha_channel()
|
|
|
|
{
|
|
|
|
switch (m_format) {
|
|
|
|
case BitmapFormat::BGRx8888:
|
|
|
|
m_format = BitmapFormat::BGRA8888;
|
|
|
|
break;
|
|
|
|
case BitmapFormat::RGBA8888:
|
|
|
|
case BitmapFormat::BGRA8888:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
case BitmapFormat::Invalid:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void set_mmap_name(DeprecatedString 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
|
|
|
|
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]] 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;
|
|
|
|
|
2022-05-08 23:01:21 +00:00
|
|
|
[[nodiscard]] Optional<Color> solid_color(u8 alpha_threshold = 0) const;
|
|
|
|
|
2022-12-06 20:27:44 +00:00
|
|
|
void flood_visit_from_point(Gfx::IntPoint start_point, int threshold, Function<void(Gfx::IntPoint location)> pixel_reached);
|
2022-09-30 17:26:13 +00:00
|
|
|
|
2019-01-09 01:06:04 +00:00
|
|
|
private:
|
2022-12-06 21:35:32 +00:00
|
|
|
Bitmap(BitmapFormat, IntSize, int, BackingStore const&);
|
|
|
|
Bitmap(BitmapFormat, IntSize, int, size_t pitch, void*);
|
2023-10-11 10:35:50 +00:00
|
|
|
Bitmap(BitmapFormat, Core::AnonymousBuffer, IntSize, int);
|
2019-01-09 01:06:04 +00:00
|
|
|
|
2022-12-06 21:35:32 +00:00
|
|
|
static ErrorOr<BackingStore> allocate_backing_store(BitmapFormat format, IntSize size, int scale_factor);
|
2020-09-12 16:17:50 +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 };
|
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
|
|
|
|
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);
|
|
|
|
VERIFY(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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
VERIFY(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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE ARGB32* Bitmap::end()
|
|
|
|
{
|
|
|
|
return reinterpret_cast<ARGB32*>(reinterpret_cast<u8*>(m_data) + (m_size.height() * m_pitch));
|
|
|
|
}
|
|
|
|
|
2019-06-15 09:06:02 +00:00
|
|
|
template<>
|
2023-01-17 21:13:19 +00:00
|
|
|
ALWAYS_INLINE Color Bitmap::get_pixel<StorageFormat::BGRx8888>(int x, int y) const
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
2022-12-19 08:53:57 +00:00
|
|
|
VERIFY(x >= 0);
|
|
|
|
VERIFY(x < physical_width());
|
2019-06-15 09:06:02 +00:00
|
|
|
return Color::from_rgb(scanline(y)[x]);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2023-01-17 21:13:19 +00:00
|
|
|
ALWAYS_INLINE Color Bitmap::get_pixel<StorageFormat::BGRA8888>(int x, int y) const
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
2022-12-19 08:53:57 +00:00
|
|
|
VERIFY(x >= 0);
|
|
|
|
VERIFY(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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
VERIFY(x < physical_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);
|
|
|
|
VERIFY(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
|
|
|
}
|
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);
|
|
|
|
VERIFY(x < physical_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
|
|
|
|
|
|
|
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;
|
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
|
|
|
|
|
|
|
}
|