2019-06-07 11:46:55 +02:00
|
|
|
#include <AK/MappedFile.h>
|
2019-07-18 10:15:00 +02:00
|
|
|
#include <LibDraw/GraphicsBitmap.h>
|
|
|
|
#include <LibDraw/PNGLoader.h>
|
2019-02-07 23:13:47 +01:00
|
|
|
#include <errno.h>
|
2019-06-07 11:46:55 +02:00
|
|
|
#include <fcntl.h>
|
2019-02-07 23:13:47 +01:00
|
|
|
#include <stdio.h>
|
2019-06-07 11:46:55 +02:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
2019-02-07 23:13:47 +01:00
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
NonnullRefPtr<GraphicsBitmap> GraphicsBitmap::create(Format format, const Size& size)
|
2019-02-11 09:47:10 +01:00
|
|
|
{
|
2019-02-19 01:42:53 +01:00
|
|
|
return adopt(*new GraphicsBitmap(format, size));
|
2019-02-11 09:47:10 +01:00
|
|
|
}
|
|
|
|
|
2019-02-19 01:42:53 +01:00
|
|
|
GraphicsBitmap::GraphicsBitmap(Format format, const Size& size)
|
2019-02-11 09:47:10 +01:00
|
|
|
: m_size(size)
|
2019-05-06 14:04:54 +02:00
|
|
|
, m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
|
2019-02-19 01:42:53 +01:00
|
|
|
, m_format(format)
|
2019-02-11 09:47:10 +01:00
|
|
|
{
|
2019-05-06 19:32:56 +02:00
|
|
|
if (format == Format::Indexed8)
|
|
|
|
m_palette = new RGBA32[256];
|
2019-05-19 15:54:56 +02:00
|
|
|
m_data = (RGBA32*)mmap_with_name(nullptr, size_in_bytes(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, String::format("GraphicsBitmap [%dx%d]", width(), height()).characters());
|
2019-02-16 12:22:00 +01:00
|
|
|
ASSERT(m_data && m_data != (void*)-1);
|
2019-04-26 18:25:05 +02:00
|
|
|
m_needs_munmap = true;
|
2019-02-16 12:22:00 +01:00
|
|
|
}
|
2019-01-14 20:00:42 +01:00
|
|
|
|
2019-08-19 13:29:19 +02:00
|
|
|
NonnullRefPtr<GraphicsBitmap> GraphicsBitmap::create_wrapper(Format format, const Size& size, size_t pitch, RGBA32* data)
|
2019-01-14 20:00:42 +01:00
|
|
|
{
|
2019-08-19 13:29:19 +02:00
|
|
|
return adopt(*new GraphicsBitmap(format, size, pitch, data));
|
2019-01-14 20:00:42 +01:00
|
|
|
}
|
2019-01-09 03:51:34 +01:00
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<GraphicsBitmap> GraphicsBitmap::load_from_file(const StringView& path)
|
2019-03-22 00:19:53 +01:00
|
|
|
{
|
|
|
|
return load_png(path);
|
|
|
|
}
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<GraphicsBitmap> GraphicsBitmap::load_from_file(Format format, const StringView& path, const Size& size)
|
2019-02-07 23:13:47 +01:00
|
|
|
{
|
2019-04-03 14:32:45 +02:00
|
|
|
MappedFile mapped_file(path);
|
|
|
|
if (!mapped_file.is_valid())
|
2019-02-07 23:13:47 +01:00
|
|
|
return nullptr;
|
2019-04-03 14:32:45 +02:00
|
|
|
return adopt(*new GraphicsBitmap(format, size, move(mapped_file)));
|
2019-02-07 23:13:47 +01:00
|
|
|
}
|
|
|
|
|
2019-08-19 13:29:19 +02:00
|
|
|
GraphicsBitmap::GraphicsBitmap(Format format, const Size& size, size_t pitch, RGBA32* data)
|
2019-01-09 03:51:34 +01:00
|
|
|
: m_size(size)
|
2019-01-10 05:36:32 +01:00
|
|
|
, m_data(data)
|
2019-08-19 13:29:19 +02:00
|
|
|
, m_pitch(pitch)
|
2019-02-19 01:42:53 +01:00
|
|
|
, m_format(format)
|
2019-01-09 03:51:34 +01:00
|
|
|
{
|
2019-05-06 19:32:56 +02:00
|
|
|
ASSERT(format != Format::Indexed8);
|
2019-01-09 02:06:04 +01:00
|
|
|
}
|
|
|
|
|
2019-04-03 14:32:45 +02:00
|
|
|
GraphicsBitmap::GraphicsBitmap(Format format, const Size& size, MappedFile&& mapped_file)
|
|
|
|
: m_size(size)
|
|
|
|
, m_data((RGBA32*)mapped_file.pointer())
|
2019-05-06 14:04:54 +02:00
|
|
|
, m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
|
2019-04-03 14:32:45 +02:00
|
|
|
, m_format(format)
|
|
|
|
, m_mapped_file(move(mapped_file))
|
|
|
|
{
|
2019-05-06 19:32:56 +02:00
|
|
|
ASSERT(format != Format::Indexed8);
|
2019-04-03 14:32:45 +02:00
|
|
|
}
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
NonnullRefPtr<GraphicsBitmap> GraphicsBitmap::create_with_shared_buffer(Format format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
|
2019-02-16 12:13:43 +01:00
|
|
|
{
|
2019-03-08 12:22:55 +01:00
|
|
|
return adopt(*new GraphicsBitmap(format, move(shared_buffer), size));
|
2019-02-16 12:13:43 +01:00
|
|
|
}
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
GraphicsBitmap::GraphicsBitmap(Format format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const Size& size)
|
2019-02-16 12:13:43 +01:00
|
|
|
: m_size(size)
|
2019-03-08 12:22:55 +01:00
|
|
|
, m_data((RGBA32*)shared_buffer->data())
|
2019-05-06 14:04:54 +02:00
|
|
|
, m_pitch(round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16))
|
2019-02-19 01:42:53 +01:00
|
|
|
, m_format(format)
|
2019-03-08 12:22:55 +01:00
|
|
|
, m_shared_buffer(move(shared_buffer))
|
2019-02-16 12:13:43 +01:00
|
|
|
{
|
2019-05-06 19:32:56 +02:00
|
|
|
ASSERT(format != Format::Indexed8);
|
2019-02-16 12:13:43 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 02:06:04 +01:00
|
|
|
GraphicsBitmap::~GraphicsBitmap()
|
|
|
|
{
|
2019-04-26 18:25:05 +02:00
|
|
|
if (m_needs_munmap) {
|
2019-05-06 14:04:54 +02:00
|
|
|
int rc = munmap(m_data, size_in_bytes());
|
2019-04-26 18:25:05 +02:00
|
|
|
ASSERT(rc == 0);
|
|
|
|
}
|
2019-01-09 03:51:34 +01:00
|
|
|
m_data = nullptr;
|
2019-06-07 11:46:55 +02:00
|
|
|
delete[] m_palette;
|
2019-01-09 02:06:04 +01:00
|
|
|
}
|
|
|
|
|
2019-06-02 14:58:02 +02:00
|
|
|
void GraphicsBitmap::set_mmap_name(const StringView& name)
|
2019-04-30 13:46:03 +02:00
|
|
|
{
|
|
|
|
ASSERT(m_needs_munmap);
|
2019-07-08 15:38:44 +02:00
|
|
|
::set_mmap_name(m_data, size_in_bytes(), String(name).characters());
|
2019-04-30 13:46:03 +02:00
|
|
|
}
|
2019-06-10 19:29:50 +02:00
|
|
|
|
|
|
|
void GraphicsBitmap::fill(Color color)
|
|
|
|
{
|
|
|
|
ASSERT(m_format == GraphicsBitmap::Format::RGB32 || m_format == GraphicsBitmap::Format::RGBA32);
|
|
|
|
for (int y = 0; y < height(); ++y) {
|
|
|
|
auto* scanline = this->scanline(y);
|
2019-07-03 21:17:35 +02:00
|
|
|
fast_u32_fill(scanline, color.value(), width());
|
2019-06-10 19:29:50 +02:00
|
|
|
}
|
|
|
|
}
|