2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-01-09 01:06:04 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-14 22:02:47 +00:00
|
|
|
#include <AK/Forward.h>
|
2019-06-21 16:58:45 +00:00
|
|
|
#include <AK/RefCounted.h>
|
2019-06-25 18:33:24 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2020-02-14 22:02:47 +00:00
|
|
|
#include <LibGfx/Color.h>
|
|
|
|
#include <LibGfx/Rect.h>
|
2019-01-14 19:00:42 +00:00
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
namespace Gfx {
|
|
|
|
|
2020-02-14 22:02:47 +00:00
|
|
|
enum class BitmapFormat {
|
|
|
|
Invalid,
|
|
|
|
RGB32,
|
|
|
|
RGBA32,
|
|
|
|
Indexed8
|
|
|
|
};
|
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
class Bitmap : public RefCounted<Bitmap> {
|
2019-01-09 01:06:04 +00:00
|
|
|
public:
|
2020-02-14 22:02:47 +00:00
|
|
|
static NonnullRefPtr<Bitmap> create(BitmapFormat, const Size&);
|
|
|
|
static NonnullRefPtr<Bitmap> create_purgeable(BitmapFormat, const Size&);
|
|
|
|
static NonnullRefPtr<Bitmap> create_wrapper(BitmapFormat, const Size&, size_t pitch, RGBA32*);
|
2020-02-06 10:56:38 +00:00
|
|
|
static RefPtr<Bitmap> load_from_file(const StringView& path);
|
2020-02-14 22:02:47 +00:00
|
|
|
static NonnullRefPtr<Bitmap> create_with_shared_buffer(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const Size&);
|
2019-12-08 16:07:44 +00:00
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
NonnullRefPtr<Bitmap> to_shareable_bitmap() const;
|
2019-12-08 16:07:44 +00:00
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
~Bitmap();
|
2019-01-09 01:06:04 +00:00
|
|
|
|
2019-01-10 04:36:32 +00:00
|
|
|
RGBA32* scanline(int y);
|
2019-01-12 05:39:34 +00:00
|
|
|
const RGBA32* scanline(int y) const;
|
2019-01-09 01:06:04 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
u8* bits(int y);
|
|
|
|
const u8* bits(int y) const;
|
2019-05-06 17:32:56 +00:00
|
|
|
|
2019-01-24 22:40:12 +00:00
|
|
|
Rect rect() const { return { {}, m_size }; }
|
2019-01-09 01:06:04 +00:00
|
|
|
Size size() const { return m_size; }
|
|
|
|
int width() const { return m_size.width(); }
|
|
|
|
int height() const { return m_size.height(); }
|
2019-01-12 20:45:45 +00:00
|
|
|
size_t pitch() const { return m_pitch; }
|
2020-02-28 10:45:19 +00:00
|
|
|
int shbuf_id() const;
|
2019-02-16 11:13:43 +00:00
|
|
|
|
2019-12-08 16:07:44 +00:00
|
|
|
SharedBuffer* shared_buffer() { return m_shared_buffer.ptr(); }
|
|
|
|
const SharedBuffer* shared_buffer() const { return m_shared_buffer.ptr(); }
|
|
|
|
|
2019-06-25 18:33:24 +00:00
|
|
|
unsigned bpp() const
|
|
|
|
{
|
|
|
|
switch (m_format) {
|
2020-02-14 22:02:47 +00:00
|
|
|
case BitmapFormat::Indexed8:
|
2019-06-25 18:33:24 +00:00
|
|
|
return 8;
|
2020-02-14 22:02:47 +00:00
|
|
|
case BitmapFormat::RGB32:
|
|
|
|
case BitmapFormat::RGBA32:
|
2019-06-25 18:33:24 +00:00
|
|
|
return 32;
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
2020-02-14 22:02:47 +00:00
|
|
|
case BitmapFormat::Invalid:
|
2019-09-29 19:02:49 +00:00
|
|
|
return 0;
|
2019-06-25 18:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 17:29:50 +00:00
|
|
|
void fill(Color);
|
|
|
|
|
2020-02-14 22:02:47 +00:00
|
|
|
bool has_alpha_channel() const { return m_format == BitmapFormat::RGBA32; }
|
|
|
|
BitmapFormat format() const { return m_format; }
|
2019-02-19 00:42:53 +00:00
|
|
|
|
2019-06-02 12:58:02 +00:00
|
|
|
void set_mmap_name(const StringView&);
|
2019-04-30 11:46:03 +00:00
|
|
|
|
2019-05-08 01:28:13 +00:00
|
|
|
size_t size_in_bytes() const { return m_pitch * m_size.height(); }
|
2019-05-06 12:04:54 +00:00
|
|
|
|
2019-07-03 19:17:35 +00: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 17:32:56 +00:00
|
|
|
|
2020-02-14 22:02:47 +00:00
|
|
|
template<BitmapFormat>
|
2019-05-06 17:32:56 +00:00
|
|
|
Color get_pixel(int x, int y) const
|
|
|
|
{
|
2020-01-30 23:58:23 +00:00
|
|
|
(void)x;
|
|
|
|
(void)y;
|
2019-06-15 09:06:02 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-05-06 17:32:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 09:06:02 +00:00
|
|
|
Color get_pixel(int x, int y) const;
|
|
|
|
|
2019-06-14 17:10:59 +00:00
|
|
|
Color get_pixel(const Point& position) const
|
|
|
|
{
|
|
|
|
return get_pixel(position.x(), position.y());
|
|
|
|
}
|
|
|
|
|
2020-02-14 22:02:47 +00:00
|
|
|
template<BitmapFormat>
|
2019-06-15 09:06:02 +00:00
|
|
|
void set_pixel(int x, int y, Color)
|
2019-06-14 17:10:59 +00:00
|
|
|
{
|
2020-01-30 23:58:23 +00:00
|
|
|
(void)x;
|
|
|
|
(void)y;
|
2019-06-15 09:06:02 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-06-14 17:10:59 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 09:06:02 +00:00
|
|
|
void set_pixel(int x, int y, Color);
|
|
|
|
|
2019-06-14 17:10:59 +00:00
|
|
|
void set_pixel(const Point& position, Color color)
|
|
|
|
{
|
|
|
|
set_pixel(position.x(), position.y(), color);
|
|
|
|
}
|
|
|
|
|
2019-12-18 19:50:05 +00:00
|
|
|
bool is_purgeable() const { return m_purgeable; }
|
|
|
|
bool is_volatile() const { return m_volatile; }
|
|
|
|
void set_volatile();
|
|
|
|
[[nodiscard]] bool set_nonvolatile();
|
|
|
|
|
2019-01-09 01:06:04 +00:00
|
|
|
private:
|
2020-01-01 17:53:34 +00:00
|
|
|
enum class Purgeable { No,
|
|
|
|
Yes };
|
2020-02-14 22:02:47 +00:00
|
|
|
Bitmap(BitmapFormat, const Size&, Purgeable);
|
|
|
|
Bitmap(BitmapFormat, const Size&, size_t pitch, RGBA32*);
|
|
|
|
Bitmap(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const Size&);
|
2019-01-09 01:06:04 +00:00
|
|
|
|
|
|
|
Size m_size;
|
2019-01-10 04:36:32 +00:00
|
|
|
RGBA32* m_data { nullptr };
|
2019-05-06 17:32:56 +00:00
|
|
|
RGBA32* m_palette { nullptr };
|
2019-01-12 20:29:05 +00:00
|
|
|
size_t m_pitch { 0 };
|
2020-02-14 22:02:47 +00:00
|
|
|
BitmapFormat m_format { BitmapFormat::Invalid };
|
2019-04-26 16:25:05 +00:00
|
|
|
bool m_needs_munmap { false };
|
2019-12-18 19:50:05 +00:00
|
|
|
bool m_purgeable { false };
|
|
|
|
bool m_volatile { false };
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<SharedBuffer> m_shared_buffer;
|
2019-01-09 01:06:04 +00:00
|
|
|
};
|
2019-01-16 18:43:01 +00:00
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
inline RGBA32* Bitmap::scanline(int y)
|
2019-01-16 18:43:01 +00:00
|
|
|
{
|
2019-07-03 19:17:35 +00:00
|
|
|
return reinterpret_cast<RGBA32*>((((u8*)m_data) + (y * m_pitch)));
|
2019-01-16 18:43:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
inline const RGBA32* Bitmap::scanline(int y) const
|
2019-01-16 18:43:01 +00:00
|
|
|
{
|
2019-07-03 19:17:35 +00:00
|
|
|
return reinterpret_cast<const RGBA32*>((((const u8*)m_data) + (y * m_pitch)));
|
2019-01-16 18:43:01 +00:00
|
|
|
}
|
2019-05-06 17:32:56 +00:00
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
inline const u8* Bitmap::bits(int y) const
|
2019-05-06 17:32:56 +00:00
|
|
|
{
|
2019-07-03 19:17:35 +00:00
|
|
|
return reinterpret_cast<const u8*>(scanline(y));
|
2019-05-06 17:32:56 +00:00
|
|
|
}
|
2019-05-06 18:29:52 +00:00
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
inline u8* Bitmap::bits(int y)
|
2019-05-06 18:29:52 +00:00
|
|
|
{
|
2019-07-03 19:17:35 +00:00
|
|
|
return reinterpret_cast<u8*>(scanline(y));
|
2019-05-06 18:29:52 +00:00
|
|
|
}
|
2019-06-15 09:06:02 +00:00
|
|
|
|
|
|
|
template<>
|
2020-02-14 22:02:47 +00:00
|
|
|
inline Color Bitmap::get_pixel<BitmapFormat::RGB32>(int x, int y) const
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
|
|
|
return Color::from_rgb(scanline(y)[x]);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2020-02-14 22:02:47 +00:00
|
|
|
inline Color Bitmap::get_pixel<BitmapFormat::RGBA32>(int x, int y) const
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
|
|
|
return Color::from_rgba(scanline(y)[x]);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2020-02-14 22:02:47 +00:00
|
|
|
inline Color Bitmap::get_pixel<BitmapFormat::Indexed8>(int x, int y) const
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
|
|
|
return Color::from_rgba(m_palette[bits(y)[x]]);
|
|
|
|
}
|
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
inline Color Bitmap::get_pixel(int x, int y) const
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
|
|
|
switch (m_format) {
|
2020-02-14 22:02:47 +00:00
|
|
|
case BitmapFormat::RGB32:
|
|
|
|
return get_pixel<BitmapFormat::RGB32>(x, y);
|
|
|
|
case BitmapFormat::RGBA32:
|
|
|
|
return get_pixel<BitmapFormat::RGBA32>(x, y);
|
|
|
|
case BitmapFormat::Indexed8:
|
|
|
|
return get_pixel<BitmapFormat::Indexed8>(x, y);
|
2019-06-15 09:06:02 +00:00
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
2019-12-08 16:07:44 +00:00
|
|
|
return {};
|
2019-06-15 09:06:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2020-02-14 22:02:47 +00:00
|
|
|
inline void Bitmap::set_pixel<BitmapFormat::RGB32>(int x, int y, Color color)
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
|
|
|
scanline(y)[x] = color.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2020-02-14 22:02:47 +00:00
|
|
|
inline void Bitmap::set_pixel<BitmapFormat::RGBA32>(int x, int y, Color color)
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
|
|
|
scanline(y)[x] = color.value();
|
|
|
|
}
|
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
inline void Bitmap::set_pixel(int x, int y, Color color)
|
2019-06-15 09:06:02 +00:00
|
|
|
{
|
|
|
|
switch (m_format) {
|
2020-02-14 22:02:47 +00:00
|
|
|
case BitmapFormat::RGB32:
|
|
|
|
set_pixel<BitmapFormat::RGB32>(x, y, color);
|
2019-06-15 09:06:02 +00:00
|
|
|
break;
|
2020-02-14 22:02:47 +00:00
|
|
|
case BitmapFormat::RGBA32:
|
|
|
|
set_pixel<BitmapFormat::RGBA32>(x, y, color);
|
2019-06-15 09:06:02 +00:00
|
|
|
break;
|
2020-02-14 22:02:47 +00:00
|
|
|
case BitmapFormat::Indexed8:
|
2019-06-15 09:06:02 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 10:56:38 +00:00
|
|
|
|
|
|
|
}
|