| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-02-20 19:03:29 +01:00
										 |  |  |  * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-09-30 12:26:13 -05:00
										 |  |  |  * Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-09 02:06:04 +01:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 23:02:47 +01:00
										 |  |  | #include <AK/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-30 12:26:13 -05:00
										 |  |  | #include <AK/Function.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-21 18:58:45 +02:00
										 |  |  | #include <AK/RefCounted.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-24 12:24:38 +02:00
										 |  |  | #include <LibCore/AnonymousBuffer.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-14 19:30:07 -05:00
										 |  |  | #include <LibCore/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 23:02:47 +01:00
										 |  |  | #include <LibGfx/Color.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-29 19:04:05 +02:00
										 |  |  | #include <LibGfx/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 23:02:47 +01:00
										 |  |  | #include <LibGfx/Rect.h>
 | 
					
						
							| 
									
										
										
										
											2019-01-14 20:00:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-18 16:19:15 -05: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(jpeg, ".jpg")  \ | 
					
						
							|  |  |  |     __ENUMERATE_IMAGE_FORMAT(jpeg, ".jpeg") \ | 
					
						
							|  |  |  |     __ENUMERATE_IMAGE_FORMAT(dds, ".dds")   \ | 
					
						
							|  |  |  |     __ENUMERATE_IMAGE_FORMAT(qoi, ".qoi")   \ | 
					
						
							| 
									
										
										
										
											2023-07-02 22:33:15 +01:00
										 |  |  |     __ENUMERATE_IMAGE_FORMAT(tga, ".tga")   \ | 
					
						
							|  |  |  |     __ENUMERATE_IMAGE_FORMAT(tvg, ".tvg") | 
					
						
							| 
									
										
										
										
											2020-06-15 15:13:12 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | namespace Gfx { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 23:02:47 +01:00
										 |  |  | enum class BitmapFormat { | 
					
						
							|  |  |  |     Invalid, | 
					
						
							| 
									
										
										
										
											2020-06-17 12:37:16 -07:00
										 |  |  |     Indexed1, | 
					
						
							|  |  |  |     Indexed2, | 
					
						
							|  |  |  |     Indexed4, | 
					
						
							|  |  |  |     Indexed8, | 
					
						
							| 
									
										
										
										
											2021-03-16 11:48:42 +01:00
										 |  |  |     BGRx8888, | 
					
						
							|  |  |  |     BGRA8888, | 
					
						
							| 
									
										
										
										
											2021-03-16 12:09:15 +01:00
										 |  |  |     RGBA8888, | 
					
						
							| 
									
										
										
										
											2020-02-14 23:02:47 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-16 23:57:57 +01: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 11:48:42 +01:00
										 |  |  |     case (unsigned)BitmapFormat::BGRx8888: | 
					
						
							|  |  |  |     case (unsigned)BitmapFormat::BGRA8888: | 
					
						
							| 
									
										
										
										
											2021-03-16 12:09:15 +01:00
										 |  |  |     case (unsigned)BitmapFormat::RGBA8888: | 
					
						
							| 
									
										
										
										
											2021-01-16 23:57:57 +01:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  | enum class StorageFormat { | 
					
						
							|  |  |  |     Indexed8, | 
					
						
							| 
									
										
										
										
											2021-03-16 12:00:43 +01:00
										 |  |  |     BGRx8888, | 
					
						
							|  |  |  |     BGRA8888, | 
					
						
							| 
									
										
										
										
											2021-03-16 12:09:15 +01:00
										 |  |  |     RGBA8888, | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-13 19:10:22 -04:00
										 |  |  | inline StorageFormat determine_storage_format(BitmapFormat format) | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (format) { | 
					
						
							| 
									
										
										
										
											2021-03-16 11:48:42 +01:00
										 |  |  |     case BitmapFormat::BGRx8888: | 
					
						
							| 
									
										
										
										
											2021-03-16 12:00:43 +01:00
										 |  |  |         return StorageFormat::BGRx8888; | 
					
						
							| 
									
										
										
										
											2021-03-16 11:48:42 +01:00
										 |  |  |     case BitmapFormat::BGRA8888: | 
					
						
							| 
									
										
										
										
											2021-03-16 12:00:43 +01:00
										 |  |  |         return StorageFormat::BGRA8888; | 
					
						
							| 
									
										
										
										
											2021-03-16 12:09:15 +01:00
										 |  |  |     case BitmapFormat::RGBA8888: | 
					
						
							|  |  |  |         return StorageFormat::RGBA8888; | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  |     case BitmapFormat::Indexed1: | 
					
						
							|  |  |  |     case BitmapFormat::Indexed2: | 
					
						
							|  |  |  |     case BitmapFormat::Indexed4: | 
					
						
							|  |  |  |     case BitmapFormat::Indexed8: | 
					
						
							|  |  |  |         return StorageFormat::Indexed8; | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 17:17:50 +01:00
										 |  |  | struct BackingStore; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-12 13:19:18 +03:00
										 |  |  | enum RotationDirection { | 
					
						
							| 
									
										
										
										
											2021-05-02 22:38:29 -07:00
										 |  |  |     CounterClockwise, | 
					
						
							|  |  |  |     Clockwise | 
					
						
							| 
									
										
										
										
											2020-04-12 13:19:18 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | class Bitmap : public RefCounted<Bitmap> { | 
					
						
							| 
									
										
										
										
											2019-01-09 02:06:04 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-01-20 20:06:05 +01: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 22:36:35 +01: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-01-20 20:06:05 +01:00
										 |  |  |     [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize, int intrinsic_scale, Vector<ARGB32> const& palette); | 
					
						
							|  |  |  |     static ErrorOr<NonnullRefPtr<Bitmap>> create_from_serialized_bytes(ReadonlyBytes); | 
					
						
							|  |  |  |     static ErrorOr<NonnullRefPtr<Bitmap>> create_from_serialized_byte_buffer(ByteBuffer&&); | 
					
						
							| 
									
										
										
										
											2021-07-21 18:02:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-11 00:55:02 +01:00
										 |  |  |     static bool is_path_a_supported_image_format(StringView path) | 
					
						
							| 
									
										
										
										
											2020-06-15 15:13:12 +03: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 15:13:12 +03:00
										 |  |  |         return true; | 
					
						
							|  |  |  |         ENUMERATE_IMAGE_FORMATS | 
					
						
							|  |  |  | #undef __ENUMERATE_IMAGE_FORMAT
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-12-08 17:07:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-06 11:52:35 +01:00
										 |  |  |     ErrorOr<NonnullRefPtr<Gfx::Bitmap>> clone() const; | 
					
						
							| 
									
										
										
										
											2020-09-12 12:20:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-06 11:56:44 +01:00
										 |  |  |     ErrorOr<NonnullRefPtr<Gfx::Bitmap>> rotated(Gfx::RotationDirection) const; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:58:00 +01:00
										 |  |  |     ErrorOr<NonnullRefPtr<Gfx::Bitmap>> flipped(Gfx::Orientation) const; | 
					
						
							| 
									
										
										
										
											2021-11-06 12:26:04 +01:00
										 |  |  |     ErrorOr<NonnullRefPtr<Gfx::Bitmap>> scaled(int sx, int sy) const; | 
					
						
							|  |  |  |     ErrorOr<NonnullRefPtr<Gfx::Bitmap>> scaled(float sx, float sy) const; | 
					
						
							| 
									
										
										
										
											2022-09-15 08:31:29 +01:00
										 |  |  |     ErrorOr<NonnullRefPtr<Gfx::Bitmap>> cropped(Gfx::IntRect, Optional<BitmapFormat> new_bitmap_format = {}) const; | 
					
						
							| 
									
										
										
										
											2021-11-06 13:15:43 +01:00
										 |  |  |     ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_bitmap_backed_by_anonymous_buffer() const; | 
					
						
							| 
									
										
										
										
											2023-02-08 18:21:41 +01:00
										 |  |  |     [[nodiscard]] ErrorOr<ByteBuffer> serialize_to_byte_buffer() const; | 
					
						
							| 
									
										
										
										
											2020-03-29 19:04:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02:00
										 |  |  |     [[nodiscard]] ShareableBitmap to_shareable_bitmap() const; | 
					
						
							| 
									
										
										
										
											2019-12-08 17:07:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-19 22:43:49 +01:00
										 |  |  |     ErrorOr<NonnullRefPtr<Gfx::Bitmap>> inverted() const; | 
					
						
							| 
									
										
										
										
											2022-05-09 00:00:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     ~Bitmap(); | 
					
						
							| 
									
										
										
										
											2019-01-09 02:06:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02:00
										 |  |  |     [[nodiscard]] u8* scanline_u8(int physical_y); | 
					
						
							| 
									
										
										
										
											2021-11-06 13:29:13 +01:00
										 |  |  |     [[nodiscard]] u8 const* scanline_u8(int physical_y) const; | 
					
						
							| 
									
										
										
										
											2022-03-04 22:05:20 +01:00
										 |  |  |     [[nodiscard]] ARGB32* scanline(int physical_y); | 
					
						
							|  |  |  |     [[nodiscard]] ARGB32 const* scanline(int physical_y) const; | 
					
						
							| 
									
										
										
										
											2019-01-09 02:06:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-03 14:29:11 -04:00
										 |  |  |     [[nodiscard]] ARGB32* begin(); | 
					
						
							|  |  |  |     [[nodiscard]] ARGB32* end(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02: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 12:10:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02: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 17:07:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02:00
										 |  |  |     [[nodiscard]] ALWAYS_INLINE bool is_indexed() const | 
					
						
							| 
									
										
										
										
											2020-06-22 21:35:22 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return is_indexed(m_format); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02:00
										 |  |  |     [[nodiscard]] ALWAYS_INLINE static bool is_indexed(BitmapFormat format) | 
					
						
							| 
									
										
										
										
											2020-06-17 12:37:16 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         return format == BitmapFormat::Indexed8 || format == BitmapFormat::Indexed4 | 
					
						
							|  |  |  |             || format == BitmapFormat::Indexed2 || format == BitmapFormat::Indexed1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02:00
										 |  |  |     [[nodiscard]] static size_t palette_size(BitmapFormat format) | 
					
						
							| 
									
										
										
										
											2020-06-22 21:35:22 +02: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 22:05:20 +01:00
										 |  |  |     [[nodiscard]] Vector<ARGB32> palette_to_vector() const; | 
					
						
							| 
									
										
										
										
											2020-06-22 21:35:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02:00
										 |  |  |     [[nodiscard]] static unsigned bpp_for_format(BitmapFormat format) | 
					
						
							| 
									
										
										
										
											2019-06-25 20:33:24 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-04-15 11:57:24 +02:00
										 |  |  |         switch (format) { | 
					
						
							| 
									
										
										
										
											2020-06-17 12:37:16 -07:00
										 |  |  |         case BitmapFormat::Indexed1: | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         case BitmapFormat::Indexed2: | 
					
						
							|  |  |  |             return 2; | 
					
						
							|  |  |  |         case BitmapFormat::Indexed4: | 
					
						
							|  |  |  |             return 4; | 
					
						
							| 
									
										
										
										
											2020-02-14 23:02:47 +01:00
										 |  |  |         case BitmapFormat::Indexed8: | 
					
						
							| 
									
										
										
										
											2019-06-25 20:33:24 +02:00
										 |  |  |             return 8; | 
					
						
							| 
									
										
										
										
											2021-03-16 11:48:42 +01:00
										 |  |  |         case BitmapFormat::BGRx8888: | 
					
						
							|  |  |  |         case BitmapFormat::BGRA8888: | 
					
						
							| 
									
										
										
										
											2019-06-25 20:33:24 +02:00
										 |  |  |             return 32; | 
					
						
							|  |  |  |         default: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |             VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-02-14 23:02:47 +01:00
										 |  |  |         case BitmapFormat::Invalid: | 
					
						
							| 
									
										
										
										
											2019-09-29 21:02:49 +02:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2019-06-25 20:33:24 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02:00
										 |  |  |     [[nodiscard]] static size_t minimum_pitch(size_t physical_width, BitmapFormat); | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02:00
										 |  |  |     [[nodiscard]] unsigned bpp() const | 
					
						
							| 
									
										
										
										
											2020-04-15 11:57:24 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return bpp_for_format(m_format); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-10 19:29:50 +02:00
										 |  |  |     void fill(Color); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 17:30:34 +01:00
										 |  |  |     [[nodiscard]] bool has_alpha_channel() const { return m_format == BitmapFormat::BGRA8888 || m_format == BitmapFormat::RGBA8888; } | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02:00
										 |  |  |     [[nodiscard]] BitmapFormat format() const { return m_format; } | 
					
						
							| 
									
										
										
										
											2019-02-19 01:42:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-13 19:23:38 -04: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 13:46:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02: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 14:04:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-04 22:28:59 +01:00
										 |  |  |     [[nodiscard]] Color palette_color(u8 index) const { return Color::from_argb(m_palette[index]); } | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     void set_palette_color(u8 index, Color color) { m_palette[index] = color.value(); } | 
					
						
							| 
									
										
										
										
											2019-05-06 19:32:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  |     template<StorageFormat> | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02: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 19:10:59 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-01-19 12:10:47 -05:00
										 |  |  |         return get_pixel(physical_position.x(), physical_position.y()); | 
					
						
							| 
									
										
										
										
											2019-06-14 19:10:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  |     template<StorageFormat> | 
					
						
							| 
									
										
										
										
											2021-01-19 12:10:47 -05: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 19:10:59 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-01-19 12:10:47 -05:00
										 |  |  |         set_pixel(physical_position.x(), physical_position.y(), color); | 
					
						
							| 
									
										
										
										
											2019-06-14 19:10:59 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02:00
										 |  |  |     [[nodiscard]] bool is_volatile() const { return m_volatile; } | 
					
						
							| 
									
										
										
										
											2019-12-18 20:50:05 +01:00
										 |  |  |     void set_volatile(); | 
					
						
							| 
									
										
										
										
											2021-07-24 22:49:48 +02: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 20:50:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:42 +02:00
										 |  |  |     [[nodiscard]] Core::AnonymousBuffer& anonymous_buffer() { return m_buffer; } | 
					
						
							|  |  |  |     [[nodiscard]] Core::AnonymousBuffer const& anonymous_buffer() const { return m_buffer; } | 
					
						
							| 
									
										
										
										
											2021-01-15 12:09:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 01:07:15 +02:00
										 |  |  |     [[nodiscard]] bool visually_equals(Bitmap const&) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-09 00:01:21 +01: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 12:26:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-09 02:06:04 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-12-06 21:35:32 +00:00
										 |  |  |     Bitmap(BitmapFormat, IntSize, int, BackingStore const&); | 
					
						
							|  |  |  |     Bitmap(BitmapFormat, IntSize, int, size_t pitch, void*); | 
					
						
							|  |  |  |     Bitmap(BitmapFormat, Core::AnonymousBuffer, IntSize, int, Vector<ARGB32> const& palette); | 
					
						
							| 
									
										
										
										
											2019-01-09 02:06:04 +01: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 17:17:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-04 22:05:20 +01:00
										 |  |  |     void allocate_palette_from_format(BitmapFormat, Vector<ARGB32> const& source_palette); | 
					
						
							| 
									
										
										
										
											2020-06-17 12:37:16 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:57:59 +02:00
										 |  |  |     IntSize m_size; | 
					
						
							| 
									
										
										
										
											2021-01-19 12:10:47 -05:00
										 |  |  |     int m_scale; | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  |     void* m_data { nullptr }; | 
					
						
							| 
									
										
										
										
											2022-03-04 22:05:20 +01:00
										 |  |  |     ARGB32* m_palette { nullptr }; | 
					
						
							| 
									
										
										
										
											2019-01-12 21:29:05 +01:00
										 |  |  |     size_t m_pitch { 0 }; | 
					
						
							| 
									
										
										
										
											2020-02-14 23:02:47 +01:00
										 |  |  |     BitmapFormat m_format { BitmapFormat::Invalid }; | 
					
						
							| 
									
										
										
										
											2019-04-26 18:25:05 +02:00
										 |  |  |     bool m_needs_munmap { false }; | 
					
						
							| 
									
										
										
										
											2019-12-18 20:50:05 +01:00
										 |  |  |     bool m_volatile { false }; | 
					
						
							| 
									
										
										
										
											2021-05-24 12:24:38 +02:00
										 |  |  |     Core::AnonymousBuffer m_buffer; | 
					
						
							| 
									
										
										
										
											2019-01-09 02:06:04 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-01-16 19:43:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 22:13:19 +01:00
										 |  |  | ALWAYS_INLINE u8* Bitmap::scanline_u8(int y) | 
					
						
							| 
									
										
										
										
											2020-06-17 12:37:16 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-19 10:53:57 +02:00
										 |  |  |     VERIFY(y >= 0); | 
					
						
							|  |  |  |     VERIFY(y < physical_height()); | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  |     return reinterpret_cast<u8*>(m_data) + (y * m_pitch); | 
					
						
							| 
									
										
										
										
											2020-06-17 12:37:16 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 22:13:19 +01:00
										 |  |  | ALWAYS_INLINE u8 const* Bitmap::scanline_u8(int y) const | 
					
						
							| 
									
										
										
										
											2020-06-17 12:37:16 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-19 10:53:57 +02:00
										 |  |  |     VERIFY(y >= 0); | 
					
						
							|  |  |  |     VERIFY(y < physical_height()); | 
					
						
							| 
									
										
										
										
											2021-11-06 13:29:13 +01:00
										 |  |  |     return reinterpret_cast<u8 const*>(m_data) + (y * m_pitch); | 
					
						
							| 
									
										
										
										
											2020-06-17 12:37:16 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 22:13:19 +01:00
										 |  |  | ALWAYS_INLINE ARGB32* Bitmap::scanline(int y) | 
					
						
							| 
									
										
										
										
											2019-01-16 19:43:01 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-04 22:05:20 +01:00
										 |  |  |     return reinterpret_cast<ARGB32*>(scanline_u8(y)); | 
					
						
							| 
									
										
										
										
											2019-01-16 19:43:01 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 22:13:19 +01:00
										 |  |  | ALWAYS_INLINE ARGB32 const* Bitmap::scanline(int y) const | 
					
						
							| 
									
										
										
										
											2019-01-16 19:43:01 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-04 22:05:20 +01:00
										 |  |  |     return reinterpret_cast<ARGB32 const*>(scanline_u8(y)); | 
					
						
							| 
									
										
										
										
											2019-01-16 19:43:01 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-06 19:32:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-03 14:29:11 -04: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 11:06:02 +02:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-17 22:13:19 +01:00
										 |  |  | ALWAYS_INLINE Color Bitmap::get_pixel<StorageFormat::BGRx8888>(int x, int y) const | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-19 10:53:57 +02:00
										 |  |  |     VERIFY(x >= 0); | 
					
						
							|  |  |  |     VERIFY(x < physical_width()); | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  |     return Color::from_rgb(scanline(y)[x]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-17 22:13:19 +01:00
										 |  |  | ALWAYS_INLINE Color Bitmap::get_pixel<StorageFormat::BGRA8888>(int x, int y) const | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-19 10:53:57 +02:00
										 |  |  |     VERIFY(x >= 0); | 
					
						
							|  |  |  |     VERIFY(x < physical_width()); | 
					
						
							| 
									
										
										
										
											2022-03-04 22:28:59 +01:00
										 |  |  |     return Color::from_argb(scanline(y)[x]); | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-17 12:37:16 -07:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-17 22:13:19 +01:00
										 |  |  | ALWAYS_INLINE Color Bitmap::get_pixel<StorageFormat::Indexed8>(int x, int y) const | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-19 10:53:57 +02:00
										 |  |  |     VERIFY(x >= 0); | 
					
						
							|  |  |  |     VERIFY(x < physical_width()); | 
					
						
							| 
									
										
										
										
											2020-09-06 01:10:33 +02:00
										 |  |  |     return Color::from_rgb(m_palette[scanline_u8(y)[x]]); | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-17 22:13:19 +01:00
										 |  |  | ALWAYS_INLINE Color Bitmap::get_pixel(int x, int y) const | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  |     switch (determine_storage_format(m_format)) { | 
					
						
							| 
									
										
										
										
											2021-03-16 12:00:43 +01:00
										 |  |  |     case StorageFormat::BGRx8888: | 
					
						
							|  |  |  |         return get_pixel<StorageFormat::BGRx8888>(x, y); | 
					
						
							|  |  |  |     case StorageFormat::BGRA8888: | 
					
						
							|  |  |  |         return get_pixel<StorageFormat::BGRA8888>(x, y); | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  |     case StorageFormat::Indexed8: | 
					
						
							|  |  |  |         return get_pixel<StorageFormat::Indexed8>(x, y); | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-17 22:13:19 +01:00
										 |  |  | ALWAYS_INLINE void Bitmap::set_pixel<StorageFormat::BGRx8888>(int x, int y, Color color) | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-19 10:53:57 +02:00
										 |  |  |     VERIFY(x >= 0); | 
					
						
							|  |  |  |     VERIFY(x < physical_width()); | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  |     scanline(y)[x] = color.value(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-01-17 22:13:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-17 22:13:19 +01:00
										 |  |  | ALWAYS_INLINE void Bitmap::set_pixel<StorageFormat::BGRA8888>(int x, int y, Color color) | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-19 10:53:57 +02:00
										 |  |  |     VERIFY(x >= 0); | 
					
						
							|  |  |  |     VERIFY(x < physical_width()); | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  |     scanline(y)[x] = color.value(); // drop alpha
 | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-01-17 22:13:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-04 19:03:55 +01:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-01-17 22:13:19 +01:00
										 |  |  | ALWAYS_INLINE void Bitmap::set_pixel<StorageFormat::RGBA8888>(int x, int y, Color color) | 
					
						
							| 
									
										
										
										
											2022-03-04 19:03:55 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-19 10:53:57 +02:00
										 |  |  |     VERIFY(x >= 0); | 
					
						
							|  |  |  |     VERIFY(x < physical_width()); | 
					
						
							| 
									
										
										
										
											2022-03-04 19:03:55 +01: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 22:13:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ALWAYS_INLINE void Bitmap::set_pixel(int x, int y, Color color) | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  |     switch (determine_storage_format(m_format)) { | 
					
						
							| 
									
										
										
										
											2021-03-16 12:00:43 +01:00
										 |  |  |     case StorageFormat::BGRx8888: | 
					
						
							|  |  |  |         set_pixel<StorageFormat::BGRx8888>(x, y, color); | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2021-03-16 12:00:43 +01:00
										 |  |  |     case StorageFormat::BGRA8888: | 
					
						
							|  |  |  |         set_pixel<StorageFormat::BGRA8888>(x, y, color); | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2022-03-04 19:03:55 +01:00
										 |  |  |     case StorageFormat::RGBA8888: | 
					
						
							|  |  |  |         set_pixel<StorageFormat::RGBA8888>(x, y, color); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2020-09-06 23:59:20 +02:00
										 |  |  |     case StorageFormat::Indexed8: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2019-06-15 11:06:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |