GraphicsBitmap.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #pragma once
  2. #include "Color.h"
  3. #include "Rect.h"
  4. #include "Size.h"
  5. #include <AK/MappedFile.h>
  6. #include <AK/RefCounted.h>
  7. #include <AK/RefPtr.h>
  8. #include <AK/SharedBuffer.h>
  9. #include <AK/String.h>
  10. #include <AK/StringView.h>
  11. class GraphicsBitmap : public RefCounted<GraphicsBitmap> {
  12. public:
  13. enum class Format {
  14. Invalid,
  15. RGB32,
  16. RGBA32,
  17. Indexed8
  18. };
  19. static NonnullRefPtr<GraphicsBitmap> create(Format, const Size&);
  20. static NonnullRefPtr<GraphicsBitmap> create_purgeable(Format, const Size&);
  21. static NonnullRefPtr<GraphicsBitmap> create_wrapper(Format, const Size&, size_t pitch, RGBA32*);
  22. static RefPtr<GraphicsBitmap> load_from_file(const StringView& path);
  23. static RefPtr<GraphicsBitmap> load_from_file(Format, const StringView& path, const Size&);
  24. static NonnullRefPtr<GraphicsBitmap> create_with_shared_buffer(Format, NonnullRefPtr<SharedBuffer>&&, const Size&);
  25. NonnullRefPtr<GraphicsBitmap> to_shareable_bitmap() const;
  26. ~GraphicsBitmap();
  27. RGBA32* scanline(int y);
  28. const RGBA32* scanline(int y) const;
  29. u8* bits(int y);
  30. const u8* bits(int y) const;
  31. Rect rect() const { return { {}, m_size }; }
  32. Size size() const { return m_size; }
  33. int width() const { return m_size.width(); }
  34. int height() const { return m_size.height(); }
  35. size_t pitch() const { return m_pitch; }
  36. int shared_buffer_id() const { return m_shared_buffer ? m_shared_buffer->shared_buffer_id() : -1; }
  37. SharedBuffer* shared_buffer() { return m_shared_buffer.ptr(); }
  38. const SharedBuffer* shared_buffer() const { return m_shared_buffer.ptr(); }
  39. unsigned bpp() const
  40. {
  41. switch (m_format) {
  42. case Format::Indexed8:
  43. return 8;
  44. case Format::RGB32:
  45. case Format::RGBA32:
  46. return 32;
  47. default:
  48. ASSERT_NOT_REACHED();
  49. case Format::Invalid:
  50. return 0;
  51. }
  52. }
  53. void fill(Color);
  54. bool has_alpha_channel() const { return m_format == Format::RGBA32; }
  55. Format format() const { return m_format; }
  56. void set_mmap_name(const StringView&);
  57. size_t size_in_bytes() const { return m_pitch * m_size.height(); }
  58. Color palette_color(u8 index) const { return Color::from_rgba(m_palette[index]); }
  59. void set_palette_color(u8 index, Color color) { m_palette[index] = color.value(); }
  60. template<Format>
  61. Color get_pixel(int x, int y) const
  62. {
  63. ASSERT_NOT_REACHED();
  64. }
  65. Color get_pixel(int x, int y) const;
  66. Color get_pixel(const Point& position) const
  67. {
  68. return get_pixel(position.x(), position.y());
  69. }
  70. template<Format>
  71. void set_pixel(int x, int y, Color)
  72. {
  73. ASSERT_NOT_REACHED();
  74. }
  75. void set_pixel(int x, int y, Color);
  76. void set_pixel(const Point& position, Color color)
  77. {
  78. set_pixel(position.x(), position.y(), color);
  79. }
  80. bool is_purgeable() const { return m_purgeable; }
  81. bool is_volatile() const { return m_volatile; }
  82. void set_volatile();
  83. [[nodiscard]] bool set_nonvolatile();
  84. private:
  85. enum class Purgeable { No,
  86. Yes };
  87. GraphicsBitmap(Format, const Size&, Purgeable);
  88. GraphicsBitmap(Format, const Size&, size_t pitch, RGBA32*);
  89. GraphicsBitmap(Format, const Size&, MappedFile&&);
  90. GraphicsBitmap(Format, NonnullRefPtr<SharedBuffer>&&, const Size&);
  91. Size m_size;
  92. RGBA32* m_data { nullptr };
  93. RGBA32* m_palette { nullptr };
  94. size_t m_pitch { 0 };
  95. Format m_format { Format::Invalid };
  96. bool m_needs_munmap { false };
  97. bool m_purgeable { false };
  98. bool m_volatile { false };
  99. MappedFile m_mapped_file;
  100. RefPtr<SharedBuffer> m_shared_buffer;
  101. };
  102. inline RGBA32* GraphicsBitmap::scanline(int y)
  103. {
  104. return reinterpret_cast<RGBA32*>((((u8*)m_data) + (y * m_pitch)));
  105. }
  106. inline const RGBA32* GraphicsBitmap::scanline(int y) const
  107. {
  108. return reinterpret_cast<const RGBA32*>((((const u8*)m_data) + (y * m_pitch)));
  109. }
  110. inline const u8* GraphicsBitmap::bits(int y) const
  111. {
  112. return reinterpret_cast<const u8*>(scanline(y));
  113. }
  114. inline u8* GraphicsBitmap::bits(int y)
  115. {
  116. return reinterpret_cast<u8*>(scanline(y));
  117. }
  118. template<>
  119. inline Color GraphicsBitmap::get_pixel<GraphicsBitmap::Format::RGB32>(int x, int y) const
  120. {
  121. return Color::from_rgb(scanline(y)[x]);
  122. }
  123. template<>
  124. inline Color GraphicsBitmap::get_pixel<GraphicsBitmap::Format::RGBA32>(int x, int y) const
  125. {
  126. return Color::from_rgba(scanline(y)[x]);
  127. }
  128. template<>
  129. inline Color GraphicsBitmap::get_pixel<GraphicsBitmap::Format::Indexed8>(int x, int y) const
  130. {
  131. return Color::from_rgba(m_palette[bits(y)[x]]);
  132. }
  133. inline Color GraphicsBitmap::get_pixel(int x, int y) const
  134. {
  135. switch (m_format) {
  136. case Format::RGB32:
  137. return get_pixel<Format::RGB32>(x, y);
  138. case Format::RGBA32:
  139. return get_pixel<Format::RGBA32>(x, y);
  140. case Format::Indexed8:
  141. return get_pixel<Format::Indexed8>(x, y);
  142. default:
  143. ASSERT_NOT_REACHED();
  144. return {};
  145. }
  146. }
  147. template<>
  148. inline void GraphicsBitmap::set_pixel<GraphicsBitmap::Format::RGB32>(int x, int y, Color color)
  149. {
  150. scanline(y)[x] = color.value();
  151. }
  152. template<>
  153. inline void GraphicsBitmap::set_pixel<GraphicsBitmap::Format::RGBA32>(int x, int y, Color color)
  154. {
  155. scanline(y)[x] = color.value();
  156. }
  157. inline void GraphicsBitmap::set_pixel(int x, int y, Color color)
  158. {
  159. switch (m_format) {
  160. case Format::RGB32:
  161. set_pixel<Format::RGB32>(x, y, color);
  162. break;
  163. case Format::RGBA32:
  164. set_pixel<Format::RGBA32>(x, y, color);
  165. break;
  166. case Format::Indexed8:
  167. ASSERT_NOT_REACHED();
  168. default:
  169. ASSERT_NOT_REACHED();
  170. }
  171. }