Bitmap.h 12 KB

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