GraphicsBitmap.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/String.h>
  9. #include <AK/StringView.h>
  10. #include <SharedBuffer.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, Yes };
  86. GraphicsBitmap(Format, const Size&, Purgeable);
  87. GraphicsBitmap(Format, const Size&, size_t pitch, RGBA32*);
  88. GraphicsBitmap(Format, const Size&, MappedFile&&);
  89. GraphicsBitmap(Format, NonnullRefPtr<SharedBuffer>&&, const Size&);
  90. Size m_size;
  91. RGBA32* m_data { nullptr };
  92. RGBA32* m_palette { nullptr };
  93. size_t m_pitch { 0 };
  94. Format m_format { Format::Invalid };
  95. bool m_needs_munmap { false };
  96. bool m_purgeable { false };
  97. bool m_volatile { false };
  98. MappedFile m_mapped_file;
  99. RefPtr<SharedBuffer> m_shared_buffer;
  100. };
  101. inline RGBA32* GraphicsBitmap::scanline(int y)
  102. {
  103. return reinterpret_cast<RGBA32*>((((u8*)m_data) + (y * m_pitch)));
  104. }
  105. inline const RGBA32* GraphicsBitmap::scanline(int y) const
  106. {
  107. return reinterpret_cast<const RGBA32*>((((const u8*)m_data) + (y * m_pitch)));
  108. }
  109. inline const u8* GraphicsBitmap::bits(int y) const
  110. {
  111. return reinterpret_cast<const u8*>(scanline(y));
  112. }
  113. inline u8* GraphicsBitmap::bits(int y)
  114. {
  115. return reinterpret_cast<u8*>(scanline(y));
  116. }
  117. template<>
  118. inline Color GraphicsBitmap::get_pixel<GraphicsBitmap::Format::RGB32>(int x, int y) const
  119. {
  120. return Color::from_rgb(scanline(y)[x]);
  121. }
  122. template<>
  123. inline Color GraphicsBitmap::get_pixel<GraphicsBitmap::Format::RGBA32>(int x, int y) const
  124. {
  125. return Color::from_rgba(scanline(y)[x]);
  126. }
  127. template<>
  128. inline Color GraphicsBitmap::get_pixel<GraphicsBitmap::Format::Indexed8>(int x, int y) const
  129. {
  130. return Color::from_rgba(m_palette[bits(y)[x]]);
  131. }
  132. inline Color GraphicsBitmap::get_pixel(int x, int y) const
  133. {
  134. switch (m_format) {
  135. case Format::RGB32:
  136. return get_pixel<Format::RGB32>(x, y);
  137. case Format::RGBA32:
  138. return get_pixel<Format::RGBA32>(x, y);
  139. case Format::Indexed8:
  140. return get_pixel<Format::Indexed8>(x, y);
  141. default:
  142. ASSERT_NOT_REACHED();
  143. return {};
  144. }
  145. }
  146. template<>
  147. inline void GraphicsBitmap::set_pixel<GraphicsBitmap::Format::RGB32>(int x, int y, Color color)
  148. {
  149. scanline(y)[x] = color.value();
  150. }
  151. template<>
  152. inline void GraphicsBitmap::set_pixel<GraphicsBitmap::Format::RGBA32>(int x, int y, Color color)
  153. {
  154. scanline(y)[x] = color.value();
  155. }
  156. inline void GraphicsBitmap::set_pixel(int x, int y, Color color)
  157. {
  158. switch (m_format) {
  159. case Format::RGB32:
  160. set_pixel<Format::RGB32>(x, y, color);
  161. break;
  162. case Format::RGBA32:
  163. set_pixel<Format::RGBA32>(x, y, color);
  164. break;
  165. case Format::Indexed8:
  166. ASSERT_NOT_REACHED();
  167. default:
  168. ASSERT_NOT_REACHED();
  169. }
  170. }