Bitmap.h 12 KB

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