Bitmap.h 12 KB

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