Bitmap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Forward.h>
  9. #include <AK/Function.h>
  10. #include <AK/RefCounted.h>
  11. #include <LibCore/AnonymousBuffer.h>
  12. #include <LibCore/Forward.h>
  13. #include <LibGfx/Color.h>
  14. #include <LibGfx/Forward.h>
  15. #include <LibGfx/Rect.h>
  16. #define ENUMERATE_IMAGE_FORMATS \
  17. __ENUMERATE_IMAGE_FORMAT(pbm, ".pbm") \
  18. __ENUMERATE_IMAGE_FORMAT(pgm, ".pgm") \
  19. __ENUMERATE_IMAGE_FORMAT(png, ".png") \
  20. __ENUMERATE_IMAGE_FORMAT(ppm, ".ppm") \
  21. __ENUMERATE_IMAGE_FORMAT(gif, ".gif") \
  22. __ENUMERATE_IMAGE_FORMAT(bmp, ".bmp") \
  23. __ENUMERATE_IMAGE_FORMAT(ico, ".ico") \
  24. __ENUMERATE_IMAGE_FORMAT(jpg, ".jpg") \
  25. __ENUMERATE_IMAGE_FORMAT(jpg, ".jpeg") \
  26. __ENUMERATE_IMAGE_FORMAT(dds, ".dds") \
  27. __ENUMERATE_IMAGE_FORMAT(qoi, ".qoi") \
  28. __ENUMERATE_IMAGE_FORMAT(tga, ".tga")
  29. namespace Gfx {
  30. enum class BitmapFormat {
  31. Invalid,
  32. Indexed1,
  33. Indexed2,
  34. Indexed4,
  35. Indexed8,
  36. BGRx8888,
  37. BGRA8888,
  38. RGBA8888,
  39. };
  40. inline bool is_valid_bitmap_format(unsigned format)
  41. {
  42. switch (format) {
  43. case (unsigned)BitmapFormat::Invalid:
  44. case (unsigned)BitmapFormat::Indexed1:
  45. case (unsigned)BitmapFormat::Indexed2:
  46. case (unsigned)BitmapFormat::Indexed4:
  47. case (unsigned)BitmapFormat::Indexed8:
  48. case (unsigned)BitmapFormat::BGRx8888:
  49. case (unsigned)BitmapFormat::BGRA8888:
  50. case (unsigned)BitmapFormat::RGBA8888:
  51. return true;
  52. }
  53. return false;
  54. }
  55. enum class StorageFormat {
  56. Indexed8,
  57. BGRx8888,
  58. BGRA8888,
  59. RGBA8888,
  60. };
  61. static StorageFormat determine_storage_format(BitmapFormat format)
  62. {
  63. switch (format) {
  64. case BitmapFormat::BGRx8888:
  65. return StorageFormat::BGRx8888;
  66. case BitmapFormat::BGRA8888:
  67. return StorageFormat::BGRA8888;
  68. case BitmapFormat::RGBA8888:
  69. return StorageFormat::RGBA8888;
  70. case BitmapFormat::Indexed1:
  71. case BitmapFormat::Indexed2:
  72. case BitmapFormat::Indexed4:
  73. case BitmapFormat::Indexed8:
  74. return StorageFormat::Indexed8;
  75. default:
  76. VERIFY_NOT_REACHED();
  77. }
  78. }
  79. struct BackingStore;
  80. enum RotationDirection {
  81. CounterClockwise,
  82. Clockwise
  83. };
  84. class Bitmap : public RefCounted<Bitmap> {
  85. public:
  86. [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create(BitmapFormat, IntSize, int intrinsic_scale = 1);
  87. [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_shareable(BitmapFormat, IntSize, int intrinsic_scale = 1);
  88. [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_wrapper(BitmapFormat, IntSize, int intrinsic_scale, size_t pitch, void*);
  89. [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(StringView path, int scale_factor = 1);
  90. [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(NonnullOwnPtr<Core::File>, StringView path);
  91. [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize, int intrinsic_scale, Vector<ARGB32> const& palette);
  92. static ErrorOr<NonnullRefPtr<Bitmap>> create_from_serialized_bytes(ReadonlyBytes);
  93. static ErrorOr<NonnullRefPtr<Bitmap>> create_from_serialized_byte_buffer(ByteBuffer&&);
  94. static bool is_path_a_supported_image_format(StringView path)
  95. {
  96. #define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \
  97. if (path.ends_with(Ext##sv, CaseSensitivity::CaseInsensitive)) \
  98. return true;
  99. ENUMERATE_IMAGE_FORMATS
  100. #undef __ENUMERATE_IMAGE_FORMAT
  101. return false;
  102. }
  103. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> clone() const;
  104. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> rotated(Gfx::RotationDirection) const;
  105. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> flipped(Gfx::Orientation) const;
  106. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> scaled(int sx, int sy) const;
  107. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> scaled(float sx, float sy) const;
  108. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> cropped(Gfx::IntRect, Optional<BitmapFormat> new_bitmap_format = {}) const;
  109. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_bitmap_backed_by_anonymous_buffer() const;
  110. [[nodiscard]] ErrorOr<ByteBuffer> serialize_to_byte_buffer() const;
  111. [[nodiscard]] ShareableBitmap to_shareable_bitmap() const;
  112. void invert();
  113. ~Bitmap();
  114. [[nodiscard]] u8* scanline_u8(int physical_y);
  115. [[nodiscard]] u8 const* scanline_u8(int physical_y) const;
  116. [[nodiscard]] ARGB32* scanline(int physical_y);
  117. [[nodiscard]] ARGB32 const* scanline(int physical_y) const;
  118. [[nodiscard]] IntRect rect() const { return { {}, m_size }; }
  119. [[nodiscard]] IntSize size() const { return m_size; }
  120. [[nodiscard]] int width() const { return m_size.width(); }
  121. [[nodiscard]] int height() const { return m_size.height(); }
  122. [[nodiscard]] int scale() const { return m_scale; }
  123. [[nodiscard]] IntRect physical_rect() const { return rect() * scale(); }
  124. [[nodiscard]] IntSize physical_size() const { return size() * scale(); }
  125. [[nodiscard]] int physical_width() const { return physical_size().width(); }
  126. [[nodiscard]] int physical_height() const { return physical_size().height(); }
  127. [[nodiscard]] size_t pitch() const { return m_pitch; }
  128. [[nodiscard]] ALWAYS_INLINE bool is_indexed() const
  129. {
  130. return is_indexed(m_format);
  131. }
  132. [[nodiscard]] ALWAYS_INLINE static bool is_indexed(BitmapFormat format)
  133. {
  134. return format == BitmapFormat::Indexed8 || format == BitmapFormat::Indexed4
  135. || format == BitmapFormat::Indexed2 || format == BitmapFormat::Indexed1;
  136. }
  137. [[nodiscard]] static size_t palette_size(BitmapFormat format)
  138. {
  139. switch (format) {
  140. case BitmapFormat::Indexed1:
  141. return 2;
  142. case BitmapFormat::Indexed2:
  143. return 4;
  144. case BitmapFormat::Indexed4:
  145. return 16;
  146. case BitmapFormat::Indexed8:
  147. return 256;
  148. default:
  149. return 0;
  150. }
  151. }
  152. [[nodiscard]] Vector<ARGB32> palette_to_vector() const;
  153. [[nodiscard]] static unsigned bpp_for_format(BitmapFormat format)
  154. {
  155. switch (format) {
  156. case BitmapFormat::Indexed1:
  157. return 1;
  158. case BitmapFormat::Indexed2:
  159. return 2;
  160. case BitmapFormat::Indexed4:
  161. return 4;
  162. case BitmapFormat::Indexed8:
  163. return 8;
  164. case BitmapFormat::BGRx8888:
  165. case BitmapFormat::BGRA8888:
  166. return 32;
  167. default:
  168. VERIFY_NOT_REACHED();
  169. case BitmapFormat::Invalid:
  170. return 0;
  171. }
  172. }
  173. [[nodiscard]] static size_t minimum_pitch(size_t physical_width, BitmapFormat);
  174. [[nodiscard]] unsigned bpp() const
  175. {
  176. return bpp_for_format(m_format);
  177. }
  178. void fill(Color);
  179. [[nodiscard]] bool has_alpha_channel() const { return m_format == BitmapFormat::BGRA8888 || m_format == BitmapFormat::RGBA8888; }
  180. [[nodiscard]] BitmapFormat format() const { return m_format; }
  181. void set_mmap_name(DeprecatedString const&);
  182. [[nodiscard]] static constexpr size_t size_in_bytes(size_t pitch, int physical_height) { return pitch * physical_height; }
  183. [[nodiscard]] size_t size_in_bytes() const { return size_in_bytes(m_pitch, physical_height()); }
  184. [[nodiscard]] Color palette_color(u8 index) const { return Color::from_argb(m_palette[index]); }
  185. void set_palette_color(u8 index, Color color) { m_palette[index] = color.value(); }
  186. template<StorageFormat>
  187. [[nodiscard]] Color get_pixel(int physical_x, int physical_y) const;
  188. [[nodiscard]] Color get_pixel(int physical_x, int physical_y) const;
  189. [[nodiscard]] Color get_pixel(IntPoint physical_position) const
  190. {
  191. return get_pixel(physical_position.x(), physical_position.y());
  192. }
  193. template<StorageFormat>
  194. void set_pixel(int physical_x, int physical_y, Color);
  195. void set_pixel(int physical_x, int physical_y, Color);
  196. void set_pixel(IntPoint physical_position, Color color)
  197. {
  198. set_pixel(physical_position.x(), physical_position.y(), color);
  199. }
  200. [[nodiscard]] bool is_volatile() const { return m_volatile; }
  201. void set_volatile();
  202. // Returns true if making the bitmap non-volatile succeeded. `was_purged` indicates status of contents.
  203. // Returns false if there was not enough memory.
  204. [[nodiscard]] bool set_nonvolatile(bool& was_purged);
  205. [[nodiscard]] Core::AnonymousBuffer& anonymous_buffer() { return m_buffer; }
  206. [[nodiscard]] Core::AnonymousBuffer const& anonymous_buffer() const { return m_buffer; }
  207. [[nodiscard]] bool visually_equals(Bitmap const&) const;
  208. [[nodiscard]] Optional<Color> solid_color(u8 alpha_threshold = 0) const;
  209. void flood_visit_from_point(Gfx::IntPoint start_point, int threshold, Function<void(Gfx::IntPoint location)> pixel_reached);
  210. private:
  211. Bitmap(BitmapFormat, IntSize, int, BackingStore const&);
  212. Bitmap(BitmapFormat, IntSize, int, size_t pitch, void*);
  213. Bitmap(BitmapFormat, Core::AnonymousBuffer, IntSize, int, Vector<ARGB32> const& palette);
  214. static ErrorOr<BackingStore> allocate_backing_store(BitmapFormat format, IntSize size, int scale_factor);
  215. void allocate_palette_from_format(BitmapFormat, Vector<ARGB32> const& source_palette);
  216. IntSize m_size;
  217. int m_scale;
  218. void* m_data { nullptr };
  219. ARGB32* m_palette { nullptr };
  220. size_t m_pitch { 0 };
  221. BitmapFormat m_format { BitmapFormat::Invalid };
  222. bool m_needs_munmap { false };
  223. bool m_volatile { false };
  224. Core::AnonymousBuffer m_buffer;
  225. };
  226. ALWAYS_INLINE u8* Bitmap::scanline_u8(int y)
  227. {
  228. VERIFY(y >= 0);
  229. VERIFY(y < physical_height());
  230. return reinterpret_cast<u8*>(m_data) + (y * m_pitch);
  231. }
  232. ALWAYS_INLINE u8 const* Bitmap::scanline_u8(int y) const
  233. {
  234. VERIFY(y >= 0);
  235. VERIFY(y < physical_height());
  236. return reinterpret_cast<u8 const*>(m_data) + (y * m_pitch);
  237. }
  238. ALWAYS_INLINE ARGB32* Bitmap::scanline(int y)
  239. {
  240. return reinterpret_cast<ARGB32*>(scanline_u8(y));
  241. }
  242. ALWAYS_INLINE ARGB32 const* Bitmap::scanline(int y) const
  243. {
  244. return reinterpret_cast<ARGB32 const*>(scanline_u8(y));
  245. }
  246. template<>
  247. ALWAYS_INLINE Color Bitmap::get_pixel<StorageFormat::BGRx8888>(int x, int y) const
  248. {
  249. VERIFY(x >= 0);
  250. VERIFY(x < physical_width());
  251. return Color::from_rgb(scanline(y)[x]);
  252. }
  253. template<>
  254. ALWAYS_INLINE Color Bitmap::get_pixel<StorageFormat::BGRA8888>(int x, int y) const
  255. {
  256. VERIFY(x >= 0);
  257. VERIFY(x < physical_width());
  258. return Color::from_argb(scanline(y)[x]);
  259. }
  260. template<>
  261. ALWAYS_INLINE Color Bitmap::get_pixel<StorageFormat::Indexed8>(int x, int y) const
  262. {
  263. VERIFY(x >= 0);
  264. VERIFY(x < physical_width());
  265. return Color::from_rgb(m_palette[scanline_u8(y)[x]]);
  266. }
  267. ALWAYS_INLINE Color Bitmap::get_pixel(int x, int y) const
  268. {
  269. switch (determine_storage_format(m_format)) {
  270. case StorageFormat::BGRx8888:
  271. return get_pixel<StorageFormat::BGRx8888>(x, y);
  272. case StorageFormat::BGRA8888:
  273. return get_pixel<StorageFormat::BGRA8888>(x, y);
  274. case StorageFormat::Indexed8:
  275. return get_pixel<StorageFormat::Indexed8>(x, y);
  276. default:
  277. VERIFY_NOT_REACHED();
  278. }
  279. }
  280. template<>
  281. ALWAYS_INLINE void Bitmap::set_pixel<StorageFormat::BGRx8888>(int x, int y, Color color)
  282. {
  283. VERIFY(x >= 0);
  284. VERIFY(x < physical_width());
  285. scanline(y)[x] = color.value();
  286. }
  287. template<>
  288. ALWAYS_INLINE void Bitmap::set_pixel<StorageFormat::BGRA8888>(int x, int y, Color color)
  289. {
  290. VERIFY(x >= 0);
  291. VERIFY(x < physical_width());
  292. scanline(y)[x] = color.value(); // drop alpha
  293. }
  294. template<>
  295. ALWAYS_INLINE void Bitmap::set_pixel<StorageFormat::RGBA8888>(int x, int y, Color color)
  296. {
  297. VERIFY(x >= 0);
  298. VERIFY(x < physical_width());
  299. // FIXME: There's a lot of inaccurately named functions in the Color class right now (RGBA vs BGRA),
  300. // clear those up and then make this more convenient.
  301. auto rgba = (color.alpha() << 24) | (color.blue() << 16) | (color.green() << 8) | color.red();
  302. scanline(y)[x] = rgba;
  303. }
  304. ALWAYS_INLINE void Bitmap::set_pixel(int x, int y, Color color)
  305. {
  306. switch (determine_storage_format(m_format)) {
  307. case StorageFormat::BGRx8888:
  308. set_pixel<StorageFormat::BGRx8888>(x, y, color);
  309. break;
  310. case StorageFormat::BGRA8888:
  311. set_pixel<StorageFormat::BGRA8888>(x, y, color);
  312. break;
  313. case StorageFormat::RGBA8888:
  314. set_pixel<StorageFormat::RGBA8888>(x, y, color);
  315. break;
  316. case StorageFormat::Indexed8:
  317. VERIFY_NOT_REACHED();
  318. default:
  319. VERIFY_NOT_REACHED();
  320. }
  321. }
  322. }