Bitmap.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Forward.h>
  28. #include <AK/RefCounted.h>
  29. #include <AK/RefPtr.h>
  30. #include <LibGfx/Color.h>
  31. #include <LibGfx/Forward.h>
  32. #include <LibGfx/Rect.h>
  33. #define ENUMERATE_IMAGE_FORMATS \
  34. __ENUMERATE_IMAGE_FORMAT(pbm, ".pbm") \
  35. __ENUMERATE_IMAGE_FORMAT(pgm, ".pgm") \
  36. __ENUMERATE_IMAGE_FORMAT(png, ".png") \
  37. __ENUMERATE_IMAGE_FORMAT(ppm, ".ppm") \
  38. __ENUMERATE_IMAGE_FORMAT(gif, ".gif") \
  39. __ENUMERATE_IMAGE_FORMAT(bmp, ".bmp") \
  40. __ENUMERATE_IMAGE_FORMAT(ico, ".ico") \
  41. __ENUMERATE_IMAGE_FORMAT(jpg, ".jpg") \
  42. __ENUMERATE_IMAGE_FORMAT(jpg, ".jpeg")
  43. namespace Gfx {
  44. enum class BitmapFormat {
  45. Invalid,
  46. Indexed1,
  47. Indexed2,
  48. Indexed4,
  49. Indexed8,
  50. RGB32,
  51. RGBA32,
  52. };
  53. enum RotationDirection {
  54. Left,
  55. Right
  56. };
  57. class Bitmap : public RefCounted<Bitmap> {
  58. public:
  59. static RefPtr<Bitmap> create(BitmapFormat, const IntSize&);
  60. static RefPtr<Bitmap> create_purgeable(BitmapFormat, const IntSize&);
  61. static RefPtr<Bitmap> create_wrapper(BitmapFormat, const IntSize&, size_t pitch, RGBA32*);
  62. static RefPtr<Bitmap> load_from_file(const StringView& path);
  63. static RefPtr<Bitmap> create_with_shared_buffer(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const IntSize&);
  64. static RefPtr<Bitmap> create_with_shared_buffer(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const IntSize&, const Vector<RGBA32>& palette);
  65. static bool is_path_a_supported_image_format(const StringView& path)
  66. {
  67. #define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \
  68. if (path.ends_with(Ext)) \
  69. return true;
  70. ENUMERATE_IMAGE_FORMATS
  71. #undef __ENUMERATE_IMAGE_FORMAT
  72. return false;
  73. }
  74. RefPtr<Gfx::Bitmap> rotated(Gfx::RotationDirection) const;
  75. RefPtr<Gfx::Bitmap> flipped(Gfx::Orientation) const;
  76. RefPtr<Bitmap> to_bitmap_backed_by_shared_buffer() const;
  77. ShareableBitmap to_shareable_bitmap(pid_t peer_pid = -1) const;
  78. ~Bitmap();
  79. u8* scanline_u8(int y);
  80. const u8* scanline_u8(int y) const;
  81. RGBA32* scanline(int y);
  82. const RGBA32* scanline(int y) const;
  83. u8* bits(int y);
  84. const u8* bits(int y) const;
  85. IntRect rect() const { return { {}, m_size }; }
  86. IntSize size() const { return m_size; }
  87. int width() const { return m_size.width(); }
  88. int height() const { return m_size.height(); }
  89. size_t pitch() const { return m_pitch; }
  90. int shbuf_id() const;
  91. SharedBuffer* shared_buffer() { return m_shared_buffer.ptr(); }
  92. const SharedBuffer* shared_buffer() const { return m_shared_buffer.ptr(); }
  93. ALWAYS_INLINE bool is_indexed() const
  94. {
  95. return is_indexed(m_format);
  96. }
  97. ALWAYS_INLINE static bool is_indexed(BitmapFormat format)
  98. {
  99. return format == BitmapFormat::Indexed8 || format == BitmapFormat::Indexed4
  100. || format == BitmapFormat::Indexed2 || format == BitmapFormat::Indexed1;
  101. }
  102. size_t palette_size(BitmapFormat format) const
  103. {
  104. switch (format) {
  105. case BitmapFormat::Indexed1:
  106. return 2;
  107. case BitmapFormat::Indexed2:
  108. return 4;
  109. case BitmapFormat::Indexed4:
  110. return 16;
  111. case BitmapFormat::Indexed8:
  112. return 256;
  113. default:
  114. return 0;
  115. }
  116. }
  117. Vector<RGBA32> palette_to_vector() const;
  118. static unsigned bpp_for_format(BitmapFormat format)
  119. {
  120. switch (format) {
  121. case BitmapFormat::Indexed1:
  122. return 1;
  123. case BitmapFormat::Indexed2:
  124. return 2;
  125. case BitmapFormat::Indexed4:
  126. return 4;
  127. case BitmapFormat::Indexed8:
  128. return 8;
  129. case BitmapFormat::RGB32:
  130. case BitmapFormat::RGBA32:
  131. return 32;
  132. default:
  133. ASSERT_NOT_REACHED();
  134. case BitmapFormat::Invalid:
  135. return 0;
  136. }
  137. }
  138. unsigned bpp() const
  139. {
  140. return bpp_for_format(m_format);
  141. }
  142. void fill(Color);
  143. bool has_alpha_channel() const { return m_format == BitmapFormat::RGBA32; }
  144. BitmapFormat format() const { return m_format; }
  145. void set_mmap_name(const StringView&);
  146. size_t size_in_bytes() const { return m_pitch * m_size.height(); }
  147. Color palette_color(u8 index) const { return Color::from_rgba(m_palette[index]); }
  148. void set_palette_color(u8 index, Color color) { m_palette[index] = color.value(); }
  149. template<BitmapFormat>
  150. Color get_pixel(int x, int y) const
  151. {
  152. (void)x;
  153. (void)y;
  154. ASSERT_NOT_REACHED();
  155. }
  156. Color get_pixel(int x, int y) const;
  157. Color get_pixel(const IntPoint& position) const
  158. {
  159. return get_pixel(position.x(), position.y());
  160. }
  161. template<BitmapFormat>
  162. void set_pixel(int x, int y, Color)
  163. {
  164. (void)x;
  165. (void)y;
  166. ASSERT_NOT_REACHED();
  167. }
  168. void set_pixel(int x, int y, Color);
  169. void set_pixel(const IntPoint& position, Color color)
  170. {
  171. set_pixel(position.x(), position.y(), color);
  172. }
  173. bool is_purgeable() const { return m_purgeable; }
  174. bool is_volatile() const { return m_volatile; }
  175. void set_volatile();
  176. [[nodiscard]] bool set_nonvolatile();
  177. private:
  178. enum class Purgeable { No,
  179. Yes };
  180. Bitmap(BitmapFormat, const IntSize&, Purgeable);
  181. Bitmap(BitmapFormat, const IntSize&, size_t pitch, RGBA32*);
  182. Bitmap(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const IntSize&, const Vector<RGBA32>& palette);
  183. void allocate_palette_from_format(BitmapFormat, const Vector<RGBA32>& source_palette );
  184. IntSize m_size;
  185. RGBA32* m_data { nullptr };
  186. RGBA32* m_palette { nullptr };
  187. size_t m_pitch { 0 };
  188. BitmapFormat m_format { BitmapFormat::Invalid };
  189. bool m_needs_munmap { false };
  190. bool m_purgeable { false };
  191. bool m_volatile { false };
  192. RefPtr<SharedBuffer> m_shared_buffer;
  193. };
  194. inline u8* Bitmap::scanline_u8(int y)
  195. {
  196. return (u8*)m_data + (y * m_pitch);
  197. }
  198. inline const u8* Bitmap::scanline_u8(int y) const
  199. {
  200. return (const u8*)m_data + (y * m_pitch);
  201. }
  202. inline RGBA32* Bitmap::scanline(int y)
  203. {
  204. return reinterpret_cast<RGBA32*>(scanline_u8(y));
  205. }
  206. inline const RGBA32* Bitmap::scanline(int y) const
  207. {
  208. return reinterpret_cast<const RGBA32*>(scanline_u8(y));
  209. }
  210. inline const u8* Bitmap::bits(int y) const
  211. {
  212. return reinterpret_cast<const u8*>(scanline(y));
  213. }
  214. inline u8* Bitmap::bits(int y)
  215. {
  216. return reinterpret_cast<u8*>(scanline(y));
  217. }
  218. template<>
  219. inline Color Bitmap::get_pixel<BitmapFormat::RGB32>(int x, int y) const
  220. {
  221. return Color::from_rgb(scanline(y)[x]);
  222. }
  223. template<>
  224. inline Color Bitmap::get_pixel<BitmapFormat::RGBA32>(int x, int y) const
  225. {
  226. return Color::from_rgba(scanline(y)[x]);
  227. }
  228. template<>
  229. inline Color Bitmap::get_pixel<BitmapFormat::Indexed1>(int x, int y) const
  230. {
  231. return Color::from_rgb(m_palette[bits(y)[x]]);
  232. }
  233. template<>
  234. inline Color Bitmap::get_pixel<BitmapFormat::Indexed2>(int x, int y) const
  235. {
  236. return Color::from_rgb(m_palette[bits(y)[x]]);
  237. }
  238. template<>
  239. inline Color Bitmap::get_pixel<BitmapFormat::Indexed4>(int x, int y) const
  240. {
  241. return Color::from_rgb(m_palette[bits(y)[x]]);
  242. }
  243. template<>
  244. inline Color Bitmap::get_pixel<BitmapFormat::Indexed8>(int x, int y) const
  245. {
  246. return Color::from_rgb(m_palette[bits(y)[x]]);
  247. }
  248. inline Color Bitmap::get_pixel(int x, int y) const
  249. {
  250. switch (m_format) {
  251. case BitmapFormat::RGB32:
  252. return get_pixel<BitmapFormat::RGB32>(x, y);
  253. case BitmapFormat::RGBA32:
  254. return get_pixel<BitmapFormat::RGBA32>(x, y);
  255. case BitmapFormat::Indexed1:
  256. return get_pixel<BitmapFormat::Indexed1>(x, y);
  257. case BitmapFormat::Indexed2:
  258. return get_pixel<BitmapFormat::Indexed2>(x, y);
  259. case BitmapFormat::Indexed4:
  260. return get_pixel<BitmapFormat::Indexed4>(x, y);
  261. case BitmapFormat::Indexed8:
  262. return get_pixel<BitmapFormat::Indexed8>(x, y);
  263. default:
  264. ASSERT_NOT_REACHED();
  265. }
  266. }
  267. template<>
  268. inline void Bitmap::set_pixel<BitmapFormat::RGB32>(int x, int y, Color color)
  269. {
  270. scanline(y)[x] = color.value();
  271. }
  272. template<>
  273. inline void Bitmap::set_pixel<BitmapFormat::RGBA32>(int x, int y, Color color)
  274. {
  275. scanline(y)[x] = color.value();
  276. }
  277. inline void Bitmap::set_pixel(int x, int y, Color color)
  278. {
  279. switch (m_format) {
  280. case BitmapFormat::RGB32:
  281. set_pixel<BitmapFormat::RGB32>(x, y, color);
  282. break;
  283. case BitmapFormat::RGBA32:
  284. set_pixel<BitmapFormat::RGBA32>(x, y, color);
  285. break;
  286. case BitmapFormat::Indexed1:
  287. case BitmapFormat::Indexed2:
  288. case BitmapFormat::Indexed4:
  289. case BitmapFormat::Indexed8:
  290. ASSERT_NOT_REACHED();
  291. default:
  292. ASSERT_NOT_REACHED();
  293. }
  294. }
  295. }