2019-01-08 23:06:04 -02:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-01-10 02:36:32 -02:00
|
|
|
#include "Color.h"
|
2019-01-24 20:40:12 -02:00
|
|
|
#include "Rect.h"
|
2019-01-08 23:06:04 -02:00
|
|
|
#include "Size.h"
|
2019-09-06 10:34:26 -03:00
|
|
|
#include <AK/String.h>
|
2019-04-03 09:32:45 -03:00
|
|
|
#include <AK/MappedFile.h>
|
2019-06-21 13:58:45 -03:00
|
|
|
#include <AK/RefCounted.h>
|
2019-06-25 15:33:24 -03:00
|
|
|
#include <AK/RefPtr.h>
|
2019-06-07 06:46:55 -03:00
|
|
|
#include <AK/StringView.h>
|
2019-03-08 08:22:55 -03:00
|
|
|
#include <SharedBuffer.h>
|
2019-01-14 17:00:42 -02:00
|
|
|
|
2019-06-21 10:29:31 -03:00
|
|
|
class GraphicsBitmap : public RefCounted<GraphicsBitmap> {
|
2019-01-08 23:06:04 -02:00
|
|
|
public:
|
2019-06-07 12:13:23 -03:00
|
|
|
enum class Format {
|
2019-06-07 06:46:55 -03:00
|
|
|
Invalid,
|
|
|
|
|
RGB32,
|
|
|
|
|
RGBA32,
|
|
|
|
|
Indexed8
|
|
|
|
|
};
|
2019-02-18 21:42:53 -03:00
|
|
|
|
2019-06-21 13:37:47 -03:00
|
|
|
static NonnullRefPtr<GraphicsBitmap> create(Format, const Size&);
|
2019-08-19 08:29:19 -03:00
|
|
|
static NonnullRefPtr<GraphicsBitmap> create_wrapper(Format, const Size&, size_t pitch, RGBA32*);
|
2019-06-21 13:37:47 -03:00
|
|
|
static RefPtr<GraphicsBitmap> load_from_file(const StringView& path);
|
|
|
|
|
static RefPtr<GraphicsBitmap> load_from_file(Format, const StringView& path, const Size&);
|
|
|
|
|
static NonnullRefPtr<GraphicsBitmap> create_with_shared_buffer(Format, NonnullRefPtr<SharedBuffer>&&, const Size&);
|
2019-01-08 23:06:04 -02:00
|
|
|
~GraphicsBitmap();
|
|
|
|
|
|
2019-01-10 02:36:32 -02:00
|
|
|
RGBA32* scanline(int y);
|
2019-01-12 03:39:34 -02:00
|
|
|
const RGBA32* scanline(int y) const;
|
2019-01-08 23:06:04 -02:00
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
u8* bits(int y);
|
|
|
|
|
const u8* bits(int y) const;
|
2019-05-06 14:32:56 -03:00
|
|
|
|
2019-01-24 20:40:12 -02:00
|
|
|
Rect rect() const { return { {}, m_size }; }
|
2019-01-08 23:06:04 -02:00
|
|
|
Size size() const { return m_size; }
|
|
|
|
|
int width() const { return m_size.width(); }
|
|
|
|
|
int height() const { return m_size.height(); }
|
2019-01-12 18:45:45 -02:00
|
|
|
size_t pitch() const { return m_pitch; }
|
2019-03-08 08:22:55 -03:00
|
|
|
int shared_buffer_id() const { return m_shared_buffer ? m_shared_buffer->shared_buffer_id() : -1; }
|
2019-02-16 09:13:43 -02:00
|
|
|
|
2019-06-25 15:33:24 -03:00
|
|
|
unsigned bpp() const
|
|
|
|
|
{
|
|
|
|
|
switch (m_format) {
|
|
|
|
|
case Format::Indexed8:
|
|
|
|
|
return 8;
|
|
|
|
|
case Format::RGB32:
|
|
|
|
|
case Format::RGBA32:
|
|
|
|
|
return 32;
|
|
|
|
|
default:
|
|
|
|
|
ASSERT_NOT_REACHED();
|
2019-09-29 16:02:49 -03:00
|
|
|
case Format::Invalid:
|
|
|
|
|
return 0;
|
2019-06-25 15:33:24 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 14:29:50 -03:00
|
|
|
void fill(Color);
|
|
|
|
|
|
2019-02-18 21:42:53 -03:00
|
|
|
bool has_alpha_channel() const { return m_format == Format::RGBA32; }
|
2019-03-23 08:37:33 -03:00
|
|
|
Format format() const { return m_format; }
|
2019-02-18 21:42:53 -03:00
|
|
|
|
2019-06-02 09:58:02 -03:00
|
|
|
void set_mmap_name(const StringView&);
|
2019-04-30 08:46:03 -03:00
|
|
|
|
2019-05-07 22:28:13 -03:00
|
|
|
size_t size_in_bytes() const { return m_pitch * m_size.height(); }
|
2019-05-06 09:04:54 -03:00
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
Color palette_color(u8 index) const { return Color::from_rgba(m_palette[index]); }
|
|
|
|
|
void set_palette_color(u8 index, Color color) { m_palette[index] = color.value(); }
|
2019-05-06 14:32:56 -03:00
|
|
|
|
2019-06-15 06:06:02 -03:00
|
|
|
template<Format>
|
2019-05-06 14:32:56 -03:00
|
|
|
Color get_pixel(int x, int y) const
|
|
|
|
|
{
|
2019-06-15 06:06:02 -03:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-05-06 14:32:56 -03:00
|
|
|
}
|
|
|
|
|
|
2019-06-15 06:06:02 -03:00
|
|
|
Color get_pixel(int x, int y) const;
|
|
|
|
|
|
2019-06-14 14:10:59 -03:00
|
|
|
Color get_pixel(const Point& position) const
|
|
|
|
|
{
|
|
|
|
|
return get_pixel(position.x(), position.y());
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-15 06:06:02 -03:00
|
|
|
template<Format>
|
|
|
|
|
void set_pixel(int x, int y, Color)
|
2019-06-14 14:10:59 -03:00
|
|
|
{
|
2019-06-15 06:06:02 -03:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-06-14 14:10:59 -03:00
|
|
|
}
|
|
|
|
|
|
2019-06-15 06:06:02 -03:00
|
|
|
void set_pixel(int x, int y, Color);
|
|
|
|
|
|
2019-06-14 14:10:59 -03:00
|
|
|
void set_pixel(const Point& position, Color color)
|
|
|
|
|
{
|
|
|
|
|
set_pixel(position.x(), position.y(), color);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 23:06:04 -02:00
|
|
|
private:
|
2019-02-18 21:42:53 -03:00
|
|
|
GraphicsBitmap(Format, const Size&);
|
2019-08-19 08:29:19 -03:00
|
|
|
GraphicsBitmap(Format, const Size&, size_t pitch, RGBA32*);
|
2019-04-03 09:32:45 -03:00
|
|
|
GraphicsBitmap(Format, const Size&, MappedFile&&);
|
2019-06-21 13:37:47 -03:00
|
|
|
GraphicsBitmap(Format, NonnullRefPtr<SharedBuffer>&&, const Size&);
|
2019-01-08 23:06:04 -02:00
|
|
|
|
|
|
|
|
Size m_size;
|
2019-01-10 02:36:32 -02:00
|
|
|
RGBA32* m_data { nullptr };
|
2019-05-06 14:32:56 -03:00
|
|
|
RGBA32* m_palette { nullptr };
|
2019-01-12 18:29:05 -02:00
|
|
|
size_t m_pitch { 0 };
|
2019-02-18 21:42:53 -03:00
|
|
|
Format m_format { Format::Invalid };
|
2019-04-26 13:25:05 -03:00
|
|
|
bool m_needs_munmap { false };
|
2019-04-03 09:32:45 -03:00
|
|
|
MappedFile m_mapped_file;
|
2019-06-21 13:37:47 -03:00
|
|
|
RefPtr<SharedBuffer> m_shared_buffer;
|
2019-01-08 23:06:04 -02:00
|
|
|
};
|
2019-01-16 16:43:01 -02:00
|
|
|
|
|
|
|
|
inline RGBA32* GraphicsBitmap::scanline(int y)
|
|
|
|
|
{
|
2019-07-03 16:17:35 -03:00
|
|
|
return reinterpret_cast<RGBA32*>((((u8*)m_data) + (y * m_pitch)));
|
2019-01-16 16:43:01 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline const RGBA32* GraphicsBitmap::scanline(int y) const
|
|
|
|
|
{
|
2019-07-03 16:17:35 -03:00
|
|
|
return reinterpret_cast<const RGBA32*>((((const u8*)m_data) + (y * m_pitch)));
|
2019-01-16 16:43:01 -02:00
|
|
|
}
|
2019-05-06 14:32:56 -03:00
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
inline const u8* GraphicsBitmap::bits(int y) const
|
2019-05-06 14:32:56 -03:00
|
|
|
{
|
2019-07-03 16:17:35 -03:00
|
|
|
return reinterpret_cast<const u8*>(scanline(y));
|
2019-05-06 14:32:56 -03:00
|
|
|
}
|
2019-05-06 15:29:52 -03:00
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
inline u8* GraphicsBitmap::bits(int y)
|
2019-05-06 15:29:52 -03:00
|
|
|
{
|
2019-07-03 16:17:35 -03:00
|
|
|
return reinterpret_cast<u8*>(scanline(y));
|
2019-05-06 15:29:52 -03:00
|
|
|
}
|
2019-06-15 06:06:02 -03:00
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
inline Color GraphicsBitmap::get_pixel<GraphicsBitmap::Format::RGB32>(int x, int y) const
|
|
|
|
|
{
|
|
|
|
|
return Color::from_rgb(scanline(y)[x]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
inline Color GraphicsBitmap::get_pixel<GraphicsBitmap::Format::RGBA32>(int x, int y) const
|
|
|
|
|
{
|
|
|
|
|
return Color::from_rgba(scanline(y)[x]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
inline Color GraphicsBitmap::get_pixel<GraphicsBitmap::Format::Indexed8>(int x, int y) const
|
|
|
|
|
{
|
|
|
|
|
return Color::from_rgba(m_palette[bits(y)[x]]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Color GraphicsBitmap::get_pixel(int x, int y) const
|
|
|
|
|
{
|
|
|
|
|
switch (m_format) {
|
|
|
|
|
case Format::RGB32:
|
|
|
|
|
return get_pixel<Format::RGB32>(x, y);
|
|
|
|
|
case Format::RGBA32:
|
|
|
|
|
return get_pixel<Format::RGBA32>(x, y);
|
|
|
|
|
case Format::Indexed8:
|
|
|
|
|
return get_pixel<Format::Indexed8>(x, y);
|
|
|
|
|
default:
|
|
|
|
|
ASSERT_NOT_REACHED();
|
2019-09-29 16:02:49 -03:00
|
|
|
return { };
|
2019-06-15 06:06:02 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
inline void GraphicsBitmap::set_pixel<GraphicsBitmap::Format::RGB32>(int x, int y, Color color)
|
|
|
|
|
{
|
|
|
|
|
scanline(y)[x] = color.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
inline void GraphicsBitmap::set_pixel<GraphicsBitmap::Format::RGBA32>(int x, int y, Color color)
|
|
|
|
|
{
|
|
|
|
|
scanline(y)[x] = color.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void GraphicsBitmap::set_pixel(int x, int y, Color color)
|
|
|
|
|
{
|
|
|
|
|
switch (m_format) {
|
|
|
|
|
case Format::RGB32:
|
|
|
|
|
set_pixel<Format::RGB32>(x, y, color);
|
|
|
|
|
break;
|
|
|
|
|
case Format::RGBA32:
|
|
|
|
|
set_pixel<Format::RGBA32>(x, y, color);
|
|
|
|
|
break;
|
|
|
|
|
case Format::Indexed8:
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
|
default:
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|