Serenity Operating System
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
27#pragma once
28
29#include <AK/Forward.h>
30#include <AK/RefCounted.h>
31#include <AK/RefPtr.h>
32#include <LibGfx/Color.h>
33#include <LibGfx/Rect.h>
34
35namespace Gfx {
36
37enum class BitmapFormat {
38 Invalid,
39 RGB32,
40 RGBA32,
41 Indexed8
42};
43
44class Bitmap : public RefCounted<Bitmap> {
45public:
46 static NonnullRefPtr<Bitmap> create(BitmapFormat, const Size&);
47 static NonnullRefPtr<Bitmap> create_purgeable(BitmapFormat, const Size&);
48 static NonnullRefPtr<Bitmap> create_wrapper(BitmapFormat, const Size&, size_t pitch, RGBA32*);
49 static RefPtr<Bitmap> load_from_file(const StringView& path);
50 static NonnullRefPtr<Bitmap> create_with_shared_buffer(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const Size&);
51
52 NonnullRefPtr<Bitmap> to_shareable_bitmap() const;
53
54 ~Bitmap();
55
56 RGBA32* scanline(int y);
57 const RGBA32* scanline(int y) const;
58
59 u8* bits(int y);
60 const u8* bits(int y) const;
61
62 Rect rect() const { return { {}, m_size }; }
63 Size size() const { return m_size; }
64 int width() const { return m_size.width(); }
65 int height() const { return m_size.height(); }
66 size_t pitch() const { return m_pitch; }
67 int shared_buffer_id() const;
68
69 SharedBuffer* shared_buffer() { return m_shared_buffer.ptr(); }
70 const SharedBuffer* shared_buffer() const { return m_shared_buffer.ptr(); }
71
72 unsigned bpp() const
73 {
74 switch (m_format) {
75 case BitmapFormat::Indexed8:
76 return 8;
77 case BitmapFormat::RGB32:
78 case BitmapFormat::RGBA32:
79 return 32;
80 default:
81 ASSERT_NOT_REACHED();
82 case BitmapFormat::Invalid:
83 return 0;
84 }
85 }
86
87 void fill(Color);
88
89 bool has_alpha_channel() const { return m_format == BitmapFormat::RGBA32; }
90 BitmapFormat format() const { return m_format; }
91
92 void set_mmap_name(const StringView&);
93
94 size_t size_in_bytes() const { return m_pitch * m_size.height(); }
95
96 Color palette_color(u8 index) const { return Color::from_rgba(m_palette[index]); }
97 void set_palette_color(u8 index, Color color) { m_palette[index] = color.value(); }
98
99 template<BitmapFormat>
100 Color get_pixel(int x, int y) const
101 {
102 (void)x;
103 (void)y;
104 ASSERT_NOT_REACHED();
105 }
106
107 Color get_pixel(int x, int y) const;
108
109 Color get_pixel(const Point& position) const
110 {
111 return get_pixel(position.x(), position.y());
112 }
113
114 template<BitmapFormat>
115 void set_pixel(int x, int y, Color)
116 {
117 (void)x;
118 (void)y;
119 ASSERT_NOT_REACHED();
120 }
121
122 void set_pixel(int x, int y, Color);
123
124 void set_pixel(const Point& position, Color color)
125 {
126 set_pixel(position.x(), position.y(), color);
127 }
128
129 bool is_purgeable() const { return m_purgeable; }
130 bool is_volatile() const { return m_volatile; }
131 void set_volatile();
132 [[nodiscard]] bool set_nonvolatile();
133
134private:
135 enum class Purgeable { No,
136 Yes };
137 Bitmap(BitmapFormat, const Size&, Purgeable);
138 Bitmap(BitmapFormat, const Size&, size_t pitch, RGBA32*);
139 Bitmap(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const Size&);
140
141 Size m_size;
142 RGBA32* m_data { nullptr };
143 RGBA32* m_palette { nullptr };
144 size_t m_pitch { 0 };
145 BitmapFormat m_format { BitmapFormat::Invalid };
146 bool m_needs_munmap { false };
147 bool m_purgeable { false };
148 bool m_volatile { false };
149 RefPtr<SharedBuffer> m_shared_buffer;
150};
151
152inline RGBA32* Bitmap::scanline(int y)
153{
154 return reinterpret_cast<RGBA32*>((((u8*)m_data) + (y * m_pitch)));
155}
156
157inline const RGBA32* Bitmap::scanline(int y) const
158{
159 return reinterpret_cast<const RGBA32*>((((const u8*)m_data) + (y * m_pitch)));
160}
161
162inline const u8* Bitmap::bits(int y) const
163{
164 return reinterpret_cast<const u8*>(scanline(y));
165}
166
167inline u8* Bitmap::bits(int y)
168{
169 return reinterpret_cast<u8*>(scanline(y));
170}
171
172template<>
173inline Color Bitmap::get_pixel<BitmapFormat::RGB32>(int x, int y) const
174{
175 return Color::from_rgb(scanline(y)[x]);
176}
177
178template<>
179inline Color Bitmap::get_pixel<BitmapFormat::RGBA32>(int x, int y) const
180{
181 return Color::from_rgba(scanline(y)[x]);
182}
183
184template<>
185inline Color Bitmap::get_pixel<BitmapFormat::Indexed8>(int x, int y) const
186{
187 return Color::from_rgba(m_palette[bits(y)[x]]);
188}
189
190inline Color Bitmap::get_pixel(int x, int y) const
191{
192 switch (m_format) {
193 case BitmapFormat::RGB32:
194 return get_pixel<BitmapFormat::RGB32>(x, y);
195 case BitmapFormat::RGBA32:
196 return get_pixel<BitmapFormat::RGBA32>(x, y);
197 case BitmapFormat::Indexed8:
198 return get_pixel<BitmapFormat::Indexed8>(x, y);
199 default:
200 ASSERT_NOT_REACHED();
201 return {};
202 }
203}
204
205template<>
206inline void Bitmap::set_pixel<BitmapFormat::RGB32>(int x, int y, Color color)
207{
208 scanline(y)[x] = color.value();
209}
210
211template<>
212inline void Bitmap::set_pixel<BitmapFormat::RGBA32>(int x, int y, Color color)
213{
214 scanline(y)[x] = color.value();
215}
216
217inline void Bitmap::set_pixel(int x, int y, Color color)
218{
219 switch (m_format) {
220 case BitmapFormat::RGB32:
221 set_pixel<BitmapFormat::RGB32>(x, y, color);
222 break;
223 case BitmapFormat::RGBA32:
224 set_pixel<BitmapFormat::RGBA32>(x, y, color);
225 break;
226 case BitmapFormat::Indexed8:
227 ASSERT_NOT_REACHED();
228 default:
229 ASSERT_NOT_REACHED();
230 }
231}
232
233}