Bitmap.h 12 KB

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