Bitmap.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright (c) 2018-2020, 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 <AK/RefPtr.h>
  10. #include <LibCore/AnonymousBuffer.h>
  11. #include <LibGfx/Color.h>
  12. #include <LibGfx/Forward.h>
  13. #include <LibGfx/Rect.h>
  14. #define ENUMERATE_IMAGE_FORMATS \
  15. __ENUMERATE_IMAGE_FORMAT(pbm, ".pbm") \
  16. __ENUMERATE_IMAGE_FORMAT(pgm, ".pgm") \
  17. __ENUMERATE_IMAGE_FORMAT(png, ".png") \
  18. __ENUMERATE_IMAGE_FORMAT(ppm, ".ppm") \
  19. __ENUMERATE_IMAGE_FORMAT(gif, ".gif") \
  20. __ENUMERATE_IMAGE_FORMAT(bmp, ".bmp") \
  21. __ENUMERATE_IMAGE_FORMAT(ico, ".ico") \
  22. __ENUMERATE_IMAGE_FORMAT(jpg, ".jpg") \
  23. __ENUMERATE_IMAGE_FORMAT(jpg, ".jpeg") \
  24. __ENUMERATE_IMAGE_FORMAT(dds, ".dds")
  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. static RefPtr<Bitmap> create(BitmapFormat, const IntSize&, int intrinsic_scale = 1);
  83. static RefPtr<Bitmap> create_shareable(BitmapFormat, const IntSize&, int intrinsic_scale = 1);
  84. static RefPtr<Bitmap> create_purgeable(BitmapFormat, const IntSize&, int intrinsic_scale = 1);
  85. static RefPtr<Bitmap> create_wrapper(BitmapFormat, const IntSize&, int intrinsic_scale, size_t pitch, void*);
  86. static RefPtr<Bitmap> load_from_file(String const& path, int scale_factor = 1);
  87. static RefPtr<Bitmap> create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, const IntSize&, int intrinsic_scale, const Vector<RGBA32>& palette);
  88. static RefPtr<Bitmap> create_from_serialized_byte_buffer(ByteBuffer&& buffer);
  89. static bool is_path_a_supported_image_format(const 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. RefPtr<Gfx::Bitmap> clone() const;
  99. RefPtr<Gfx::Bitmap> rotated(Gfx::RotationDirection) const;
  100. RefPtr<Gfx::Bitmap> flipped(Gfx::Orientation) const;
  101. RefPtr<Gfx::Bitmap> scaled(int sx, int sy) const;
  102. RefPtr<Gfx::Bitmap> scaled(float sx, float sy) const;
  103. RefPtr<Gfx::Bitmap> cropped(Gfx::IntRect) const;
  104. RefPtr<Bitmap> to_bitmap_backed_by_anonymous_buffer() const;
  105. ByteBuffer serialize_to_byte_buffer() const;
  106. ShareableBitmap to_shareable_bitmap() const;
  107. ~Bitmap();
  108. u8* scanline_u8(int physical_y);
  109. const u8* scanline_u8(int physical_y) const;
  110. RGBA32* scanline(int physical_y);
  111. const RGBA32* scanline(int physical_y) const;
  112. IntRect rect() const { return { {}, m_size }; }
  113. IntSize size() const { return m_size; }
  114. int width() const { return m_size.width(); }
  115. int height() const { return m_size.height(); }
  116. int scale() const { return m_scale; }
  117. IntRect physical_rect() const { return rect() * scale(); }
  118. IntSize physical_size() const { return size() * scale(); }
  119. int physical_width() const { return physical_size().width(); }
  120. int physical_height() const { return physical_size().height(); }
  121. size_t pitch() const { return m_pitch; }
  122. ALWAYS_INLINE bool is_indexed() const
  123. {
  124. return is_indexed(m_format);
  125. }
  126. 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. 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. Vector<RGBA32> palette_to_vector() const;
  147. 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. static size_t minimum_pitch(size_t physical_width, BitmapFormat);
  168. unsigned bpp() const
  169. {
  170. return bpp_for_format(m_format);
  171. }
  172. void fill(Color);
  173. bool has_alpha_channel() const { return m_format == BitmapFormat::BGRA8888; }
  174. BitmapFormat format() const { return m_format; }
  175. void set_mmap_name(String const&);
  176. static constexpr size_t size_in_bytes(size_t pitch, int physical_height) { return pitch * physical_height; }
  177. size_t size_in_bytes() const { return size_in_bytes(m_pitch, physical_height()); }
  178. Color palette_color(u8 index) const { return Color::from_rgba(m_palette[index]); }
  179. void set_palette_color(u8 index, Color color) { m_palette[index] = color.value(); }
  180. template<StorageFormat>
  181. Color get_pixel(int physical_x, int physical_y) const;
  182. Color get_pixel(int physical_x, int physical_y) const;
  183. Color get_pixel(const IntPoint& 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(const IntPoint& physical_position, Color color)
  191. {
  192. set_pixel(physical_position.x(), physical_position.y(), color);
  193. }
  194. bool is_purgeable() const { return m_purgeable; }
  195. bool is_volatile() const { return m_volatile; }
  196. void set_volatile();
  197. [[nodiscard]] bool set_nonvolatile();
  198. Core::AnonymousBuffer& anonymous_buffer() { return m_buffer; }
  199. const Core::AnonymousBuffer& anonymous_buffer() const { return m_buffer; }
  200. private:
  201. enum class Purgeable {
  202. No,
  203. Yes
  204. };
  205. Bitmap(BitmapFormat, const IntSize&, int, Purgeable, const BackingStore&);
  206. Bitmap(BitmapFormat, const IntSize&, int, size_t pitch, void*);
  207. Bitmap(BitmapFormat, Core::AnonymousBuffer, const IntSize&, int, const Vector<RGBA32>& palette);
  208. static Optional<BackingStore> allocate_backing_store(BitmapFormat, const IntSize&, int, Purgeable);
  209. void allocate_palette_from_format(BitmapFormat, const Vector<RGBA32>& source_palette);
  210. IntSize m_size;
  211. int m_scale;
  212. void* m_data { nullptr };
  213. RGBA32* m_palette { nullptr };
  214. size_t m_pitch { 0 };
  215. BitmapFormat m_format { BitmapFormat::Invalid };
  216. bool m_needs_munmap { false };
  217. bool m_purgeable { false };
  218. bool m_volatile { false };
  219. Core::AnonymousBuffer m_buffer;
  220. };
  221. inline u8* Bitmap::scanline_u8(int y)
  222. {
  223. VERIFY(y >= 0 && y < physical_height());
  224. return reinterpret_cast<u8*>(m_data) + (y * m_pitch);
  225. }
  226. inline const u8* Bitmap::scanline_u8(int y) const
  227. {
  228. VERIFY(y >= 0 && y < physical_height());
  229. return reinterpret_cast<const u8*>(m_data) + (y * m_pitch);
  230. }
  231. inline RGBA32* Bitmap::scanline(int y)
  232. {
  233. return reinterpret_cast<RGBA32*>(scanline_u8(y));
  234. }
  235. inline const RGBA32* Bitmap::scanline(int y) const
  236. {
  237. return reinterpret_cast<const RGBA32*>(scanline_u8(y));
  238. }
  239. template<>
  240. inline Color Bitmap::get_pixel<StorageFormat::BGRx8888>(int x, int y) const
  241. {
  242. VERIFY(x >= 0 && x < physical_width());
  243. return Color::from_rgb(scanline(y)[x]);
  244. }
  245. template<>
  246. inline Color Bitmap::get_pixel<StorageFormat::BGRA8888>(int x, int y) const
  247. {
  248. VERIFY(x >= 0 && x < physical_width());
  249. return Color::from_rgba(scanline(y)[x]);
  250. }
  251. template<>
  252. inline Color Bitmap::get_pixel<StorageFormat::Indexed8>(int x, int y) const
  253. {
  254. VERIFY(x >= 0 && x < physical_width());
  255. return Color::from_rgb(m_palette[scanline_u8(y)[x]]);
  256. }
  257. 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. case StorageFormat::Indexed8:
  265. return get_pixel<StorageFormat::Indexed8>(x, y);
  266. default:
  267. VERIFY_NOT_REACHED();
  268. }
  269. }
  270. template<>
  271. inline void Bitmap::set_pixel<StorageFormat::BGRx8888>(int x, int y, Color color)
  272. {
  273. VERIFY(x >= 0 && x < physical_width());
  274. scanline(y)[x] = color.value();
  275. }
  276. template<>
  277. inline void Bitmap::set_pixel<StorageFormat::BGRA8888>(int x, int y, Color color)
  278. {
  279. VERIFY(x >= 0 && x < physical_width());
  280. scanline(y)[x] = color.value(); // drop alpha
  281. }
  282. inline void Bitmap::set_pixel(int x, int y, Color color)
  283. {
  284. switch (determine_storage_format(m_format)) {
  285. case StorageFormat::BGRx8888:
  286. set_pixel<StorageFormat::BGRx8888>(x, y, color);
  287. break;
  288. case StorageFormat::BGRA8888:
  289. set_pixel<StorageFormat::BGRA8888>(x, y, color);
  290. break;
  291. case StorageFormat::Indexed8:
  292. VERIFY_NOT_REACHED();
  293. default:
  294. VERIFY_NOT_REACHED();
  295. }
  296. }
  297. }