Bitmap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright (c) 2018-2023, 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(bmp, ".bmp") \
  18. __ENUMERATE_IMAGE_FORMAT(dds, ".dds") \
  19. __ENUMERATE_IMAGE_FORMAT(gif, ".gif") \
  20. __ENUMERATE_IMAGE_FORMAT(ico, ".ico") \
  21. __ENUMERATE_IMAGE_FORMAT(iff, ".iff") \
  22. __ENUMERATE_IMAGE_FORMAT(jpeg, ".jb2") \
  23. __ENUMERATE_IMAGE_FORMAT(jpeg, ".jbig2") \
  24. __ENUMERATE_IMAGE_FORMAT(jpeg, ".jpeg") \
  25. __ENUMERATE_IMAGE_FORMAT(jpeg, ".jpg") \
  26. __ENUMERATE_IMAGE_FORMAT(jxl, ".jxl") \
  27. __ENUMERATE_IMAGE_FORMAT(iff, ".lbm") \
  28. __ENUMERATE_IMAGE_FORMAT(pam, ".pam") \
  29. __ENUMERATE_IMAGE_FORMAT(pbm, ".pbm") \
  30. __ENUMERATE_IMAGE_FORMAT(pgm, ".pgm") \
  31. __ENUMERATE_IMAGE_FORMAT(png, ".png") \
  32. __ENUMERATE_IMAGE_FORMAT(ppm, ".ppm") \
  33. __ENUMERATE_IMAGE_FORMAT(qoi, ".qoi") \
  34. __ENUMERATE_IMAGE_FORMAT(tga, ".tga") \
  35. __ENUMERATE_IMAGE_FORMAT(tiff, ".tif") \
  36. __ENUMERATE_IMAGE_FORMAT(tiff, ".tiff") \
  37. __ENUMERATE_IMAGE_FORMAT(tvg, ".tvg") \
  38. __ENUMERATE_IMAGE_FORMAT(tvg, ".webp")
  39. namespace Gfx {
  40. enum class BitmapFormat {
  41. Invalid,
  42. BGRx8888,
  43. BGRA8888,
  44. RGBA8888,
  45. FirstValid = BGRx8888,
  46. LastValid = RGBA8888,
  47. };
  48. inline bool is_valid_bitmap_format(unsigned format)
  49. {
  50. switch (format) {
  51. case (unsigned)BitmapFormat::Invalid:
  52. case (unsigned)BitmapFormat::BGRx8888:
  53. case (unsigned)BitmapFormat::BGRA8888:
  54. case (unsigned)BitmapFormat::RGBA8888:
  55. return true;
  56. }
  57. return false;
  58. }
  59. enum class StorageFormat {
  60. BGRx8888,
  61. BGRA8888,
  62. RGBA8888,
  63. };
  64. inline StorageFormat determine_storage_format(BitmapFormat format)
  65. {
  66. switch (format) {
  67. case BitmapFormat::BGRx8888:
  68. return StorageFormat::BGRx8888;
  69. case BitmapFormat::BGRA8888:
  70. return StorageFormat::BGRA8888;
  71. case BitmapFormat::RGBA8888:
  72. return StorageFormat::RGBA8888;
  73. default:
  74. VERIFY_NOT_REACHED();
  75. }
  76. }
  77. struct BackingStore;
  78. enum class RotationDirection {
  79. CounterClockwise,
  80. Flip,
  81. Clockwise,
  82. };
  83. class Bitmap : public RefCounted<Bitmap> {
  84. public:
  85. [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create(BitmapFormat, IntSize, int intrinsic_scale = 1);
  86. [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_shareable(BitmapFormat, IntSize, int intrinsic_scale = 1);
  87. [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_wrapper(BitmapFormat, IntSize, int intrinsic_scale, size_t pitch, void*);
  88. [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(StringView path, int scale_factor = 1, Optional<IntSize> ideal_size = {});
  89. [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(NonnullOwnPtr<Core::File>, StringView path, Optional<IntSize> ideal_size = {});
  90. [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_bytes(ReadonlyBytes, Optional<IntSize> ideal_size = {}, Optional<ByteString> mine_type = {});
  91. [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize, int intrinsic_scale);
  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>> scaled_to_size(Gfx::IntSize) const;
  109. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> cropped(Gfx::IntRect, Optional<BitmapFormat> new_bitmap_format = {}) const;
  110. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_bitmap_backed_by_anonymous_buffer() const;
  111. [[nodiscard]] ErrorOr<ByteBuffer> serialize_to_byte_buffer() const;
  112. [[nodiscard]] ShareableBitmap to_shareable_bitmap() const;
  113. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> inverted() const;
  114. enum class MaskKind {
  115. Alpha,
  116. Luminance
  117. };
  118. void apply_mask(Gfx::Bitmap const& mask, MaskKind);
  119. ~Bitmap();
  120. [[nodiscard]] u8* scanline_u8(int physical_y);
  121. [[nodiscard]] u8 const* scanline_u8(int physical_y) const;
  122. [[nodiscard]] ARGB32* scanline(int physical_y);
  123. [[nodiscard]] ARGB32 const* scanline(int physical_y) const;
  124. [[nodiscard]] ARGB32* begin();
  125. [[nodiscard]] ARGB32* end();
  126. [[nodiscard]] size_t data_size() const;
  127. [[nodiscard]] IntRect rect() const { return { {}, m_size }; }
  128. [[nodiscard]] IntSize size() const { return m_size; }
  129. [[nodiscard]] int width() const { return m_size.width(); }
  130. [[nodiscard]] int height() const { return m_size.height(); }
  131. [[nodiscard]] int scale() const { return m_scale; }
  132. [[nodiscard]] IntRect physical_rect() const { return rect() * scale(); }
  133. [[nodiscard]] IntSize physical_size() const { return size() * scale(); }
  134. [[nodiscard]] int physical_width() const { return physical_size().width(); }
  135. [[nodiscard]] int physical_height() const { return physical_size().height(); }
  136. [[nodiscard]] size_t pitch() const { return m_pitch; }
  137. [[nodiscard]] static unsigned bpp_for_format(BitmapFormat format)
  138. {
  139. switch (format) {
  140. case BitmapFormat::BGRx8888:
  141. case BitmapFormat::BGRA8888:
  142. return 32;
  143. default:
  144. VERIFY_NOT_REACHED();
  145. case BitmapFormat::Invalid:
  146. return 0;
  147. }
  148. }
  149. [[nodiscard]] static size_t minimum_pitch(size_t physical_width, BitmapFormat);
  150. [[nodiscard]] unsigned bpp() const
  151. {
  152. return bpp_for_format(m_format);
  153. }
  154. void fill(Color);
  155. [[nodiscard]] bool has_alpha_channel() const { return m_format == BitmapFormat::BGRA8888 || m_format == BitmapFormat::RGBA8888; }
  156. void add_alpha_channel()
  157. {
  158. switch (m_format) {
  159. case BitmapFormat::BGRx8888:
  160. m_format = BitmapFormat::BGRA8888;
  161. break;
  162. case BitmapFormat::RGBA8888:
  163. case BitmapFormat::BGRA8888:
  164. // Nothing to do.
  165. break;
  166. case BitmapFormat::Invalid:
  167. VERIFY_NOT_REACHED();
  168. }
  169. }
  170. [[nodiscard]] BitmapFormat format() const { return m_format; }
  171. // Call only for BGRx8888 and BGRA8888 bitmaps.
  172. void strip_alpha_channel();
  173. void set_mmap_name(ByteString const&);
  174. [[nodiscard]] static constexpr size_t size_in_bytes(size_t pitch, int physical_height) { return pitch * physical_height; }
  175. [[nodiscard]] size_t size_in_bytes() const { return size_in_bytes(m_pitch, physical_height()); }
  176. template<StorageFormat>
  177. [[nodiscard]] Color get_pixel(int physical_x, int physical_y) const;
  178. [[nodiscard]] Color get_pixel(int physical_x, int physical_y) const;
  179. [[nodiscard]] Color get_pixel(IntPoint physical_position) const
  180. {
  181. return get_pixel(physical_position.x(), physical_position.y());
  182. }
  183. template<StorageFormat>
  184. void set_pixel(int physical_x, int physical_y, Color);
  185. void set_pixel(int physical_x, int physical_y, Color);
  186. void set_pixel(IntPoint physical_position, Color color)
  187. {
  188. set_pixel(physical_position.x(), physical_position.y(), color);
  189. }
  190. [[nodiscard]] bool is_volatile() const { return m_volatile; }
  191. void set_volatile();
  192. // Returns true if making the bitmap non-volatile succeeded. `was_purged` indicates status of contents.
  193. // Returns false if there was not enough memory.
  194. [[nodiscard]] bool set_nonvolatile(bool& was_purged);
  195. [[nodiscard]] Core::AnonymousBuffer& anonymous_buffer() { return m_buffer; }
  196. [[nodiscard]] Core::AnonymousBuffer const& anonymous_buffer() const { return m_buffer; }
  197. [[nodiscard]] bool visually_equals(Bitmap const&) const;
  198. [[nodiscard]] Optional<Color> solid_color(u8 alpha_threshold = 0) const;
  199. void flood_visit_from_point(Gfx::IntPoint start_point, int threshold, Function<void(Gfx::IntPoint location)> pixel_reached);
  200. private:
  201. Bitmap(BitmapFormat, IntSize, int, BackingStore const&);
  202. Bitmap(BitmapFormat, IntSize, int, size_t pitch, void*);
  203. Bitmap(BitmapFormat, Core::AnonymousBuffer, IntSize, int);
  204. static ErrorOr<BackingStore> allocate_backing_store(BitmapFormat format, IntSize size, int scale_factor);
  205. IntSize m_size;
  206. int m_scale;
  207. void* m_data { nullptr };
  208. size_t m_pitch { 0 };
  209. BitmapFormat m_format { BitmapFormat::Invalid };
  210. bool m_needs_munmap { false };
  211. bool m_volatile { false };
  212. Core::AnonymousBuffer m_buffer;
  213. };
  214. ALWAYS_INLINE u8* Bitmap::scanline_u8(int y)
  215. {
  216. VERIFY(y >= 0);
  217. VERIFY(y < physical_height());
  218. return reinterpret_cast<u8*>(m_data) + (y * m_pitch);
  219. }
  220. ALWAYS_INLINE u8 const* Bitmap::scanline_u8(int y) const
  221. {
  222. VERIFY(y >= 0);
  223. VERIFY(y < physical_height());
  224. return reinterpret_cast<u8 const*>(m_data) + (y * m_pitch);
  225. }
  226. ALWAYS_INLINE ARGB32* Bitmap::scanline(int y)
  227. {
  228. return reinterpret_cast<ARGB32*>(scanline_u8(y));
  229. }
  230. ALWAYS_INLINE ARGB32 const* Bitmap::scanline(int y) const
  231. {
  232. return reinterpret_cast<ARGB32 const*>(scanline_u8(y));
  233. }
  234. ALWAYS_INLINE ARGB32* Bitmap::begin()
  235. {
  236. return scanline(0);
  237. }
  238. ALWAYS_INLINE ARGB32* Bitmap::end()
  239. {
  240. return reinterpret_cast<ARGB32*>(reinterpret_cast<u8*>(m_data) + data_size());
  241. }
  242. ALWAYS_INLINE size_t Bitmap::data_size() const
  243. {
  244. return m_size.height() * m_pitch;
  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. ALWAYS_INLINE Color Bitmap::get_pixel(int x, int y) const
  261. {
  262. switch (determine_storage_format(m_format)) {
  263. case StorageFormat::BGRx8888:
  264. return get_pixel<StorageFormat::BGRx8888>(x, y);
  265. case StorageFormat::BGRA8888:
  266. return get_pixel<StorageFormat::BGRA8888>(x, y);
  267. default:
  268. VERIFY_NOT_REACHED();
  269. }
  270. }
  271. template<>
  272. ALWAYS_INLINE void Bitmap::set_pixel<StorageFormat::BGRx8888>(int x, int y, Color color)
  273. {
  274. VERIFY(x >= 0);
  275. VERIFY(x < physical_width());
  276. scanline(y)[x] = color.value();
  277. }
  278. template<>
  279. ALWAYS_INLINE void Bitmap::set_pixel<StorageFormat::BGRA8888>(int x, int y, Color color)
  280. {
  281. VERIFY(x >= 0);
  282. VERIFY(x < physical_width());
  283. scanline(y)[x] = color.value(); // drop alpha
  284. }
  285. template<>
  286. ALWAYS_INLINE void Bitmap::set_pixel<StorageFormat::RGBA8888>(int x, int y, Color color)
  287. {
  288. VERIFY(x >= 0);
  289. VERIFY(x < physical_width());
  290. // FIXME: There's a lot of inaccurately named functions in the Color class right now (RGBA vs BGRA),
  291. // clear those up and then make this more convenient.
  292. auto rgba = (color.alpha() << 24) | (color.blue() << 16) | (color.green() << 8) | color.red();
  293. scanline(y)[x] = rgba;
  294. }
  295. ALWAYS_INLINE void Bitmap::set_pixel(int x, int y, Color color)
  296. {
  297. switch (determine_storage_format(m_format)) {
  298. case StorageFormat::BGRx8888:
  299. set_pixel<StorageFormat::BGRx8888>(x, y, color);
  300. break;
  301. case StorageFormat::BGRA8888:
  302. set_pixel<StorageFormat::BGRA8888>(x, y, color);
  303. break;
  304. case StorageFormat::RGBA8888:
  305. set_pixel<StorageFormat::RGBA8888>(x, y, color);
  306. break;
  307. default:
  308. VERIFY_NOT_REACHED();
  309. }
  310. }
  311. }