LibGfx: Add BMP loader

Adds an *almost fully featured BMP loader to process .bmp files.

Features:
- All header formats are supported
- Full RLE4/8/24 support
- Color scaling (e.g. distributing a 5-bit color throughout the 8-bit
color spectrum, so 5-bit white is still 0xffffff)
- Full BITMASK/ALPHABITMASK support

*Not included:
- 1D Huffman compression. Good luck actually finding a bmp in the wild
that uses this
- Use of any field in the V4/V5 header. Color spaces? Endpoints? No
thanks :)

This loader was tested with the images at
https://entropymine.com/jason/bmpsuite/bmpsuite/html/bmpsuite.html. This
loader correctly displays 81 out of the 90 total images (for reference,
firefox displays 64 correctly). Note that not rendering the images at
the bottom is counted as displaying correctly.
This commit is contained in:
Matthew Olsson 2020-06-17 12:37:16 -07:00 committed by Andreas Kling
parent dfe5b232f4
commit 4e093a7c23
Notes: sideshowbarker 2024-07-19 05:30:21 +09:00
8 changed files with 1477 additions and 15 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
* 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.
*/
#pragma once
#include <AK/Vector.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>
namespace Gfx {
RefPtr<Gfx::Bitmap> load_bmp(const StringView& path);
struct BMPLoadingContext;
class BMPImageDecoderPlugin final : public ImageDecoderPlugin {
public:
virtual ~BMPImageDecoderPlugin() override;
BMPImageDecoderPlugin(const u8*, size_t);
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile() override;
virtual bool sniff() override;
virtual bool is_animated() override;
virtual size_t loop_count() override;
virtual size_t frame_count() override;
virtual ImageFrameDescriptor frame(size_t i) override;
private:
OwnPtr<BMPLoadingContext> m_context;
};
}

View File

@ -29,14 +29,13 @@
#include <AK/SharedBuffer.h>
#include <AK/String.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/BMPLoader.h>
#include <LibGfx/GIFLoader.h>
#include <LibGfx/PNGLoader.h>
#include <LibGfx/ShareableBitmap.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
namespace Gfx {
@ -69,8 +68,7 @@ Bitmap::Bitmap(BitmapFormat format, const IntSize& size, Purgeable purgeable)
{
ASSERT(!m_size.is_empty());
ASSERT(!size_would_overflow(format, size));
if (format == BitmapFormat::Indexed8)
m_palette = new RGBA32[256];
allocate_palette_from_format(format);
int map_flags = purgeable == Purgeable::Yes ? (MAP_PURGEABLE | MAP_PRIVATE) : (MAP_ANONYMOUS | MAP_PRIVATE);
m_data = (RGBA32*)mmap_with_name(nullptr, size_in_bytes(), PROT_READ | PROT_WRITE, map_flags, 0, 0, String::format("GraphicsBitmap [%dx%d]", width(), height()).characters());
ASSERT(m_data && m_data != (void*)-1);
@ -102,8 +100,7 @@ Bitmap::Bitmap(BitmapFormat format, const IntSize& size, size_t pitch, RGBA32* d
, m_format(format)
{
ASSERT(!size_would_overflow(format, size));
if (format == BitmapFormat::Indexed8)
m_palette = new RGBA32[256];
allocate_palette_from_format(format);
}
RefPtr<Bitmap> Bitmap::create_with_shared_buffer(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const IntSize& size)
@ -120,7 +117,7 @@ Bitmap::Bitmap(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer,
, m_format(format)
, m_shared_buffer(move(shared_buffer))
{
ASSERT(format != BitmapFormat::Indexed8);
ASSERT(!is_indexed(format));
ASSERT(!size_would_overflow(format, size));
}
@ -200,7 +197,7 @@ void Bitmap::set_mmap_name(const StringView& name)
void Bitmap::fill(Color color)
{
ASSERT(m_format == BitmapFormat::RGB32 || m_format == BitmapFormat::RGBA32);
ASSERT(!is_indexed(m_format));
for (int y = 0; y < height(); ++y) {
auto* scanline = this->scanline(y);
fast_u32_fill(scanline, color.value(), width());
@ -249,4 +246,17 @@ ShareableBitmap Bitmap::to_shareable_bitmap(pid_t peer_pid) const
return ShareableBitmap(*bitmap);
}
void Bitmap::allocate_palette_from_format(BitmapFormat format)
{
if (format == BitmapFormat::Indexed1) {
m_palette = new RGBA32[2];
} else if (format == BitmapFormat::Indexed2) {
m_palette = new RGBA32[4];
} else if (format == BitmapFormat::Indexed4) {
m_palette = new RGBA32[16];
} else if (format == BitmapFormat::Indexed8) {
m_palette = new RGBA32[256];
}
}
}

View File

@ -35,15 +35,19 @@
#define ENUMERATE_IMAGE_FORMATS \
__ENUMERATE_IMAGE_FORMAT(png, ".png") \
__ENUMERATE_IMAGE_FORMAT(gif, ".gif")
__ENUMERATE_IMAGE_FORMAT(gif, ".gif") \
__ENUMERATE_IMAGE_FORMAT(bmp, ".bmp")
namespace Gfx {
enum class BitmapFormat {
Invalid,
Indexed1,
Indexed2,
Indexed4,
Indexed8,
RGB32,
RGBA32,
Indexed8
};
enum RotationDirection {
@ -77,6 +81,8 @@ public:
~Bitmap();
u8* scanline_u8(int y);
const u8* scanline_u8(int y) const;
RGBA32* scanline(int y);
const RGBA32* scanline(int y) const;
@ -93,9 +99,21 @@ public:
SharedBuffer* shared_buffer() { return m_shared_buffer.ptr(); }
const SharedBuffer* shared_buffer() const { return m_shared_buffer.ptr(); }
ALWAYS_INLINE static bool is_indexed(BitmapFormat format)
{
return format == BitmapFormat::Indexed8 || format == BitmapFormat::Indexed4
|| format == BitmapFormat::Indexed2 || format == BitmapFormat::Indexed1;
}
static unsigned bpp_for_format(BitmapFormat format)
{
switch (format) {
case BitmapFormat::Indexed1:
return 1;
case BitmapFormat::Indexed2:
return 2;
case BitmapFormat::Indexed4:
return 4;
case BitmapFormat::Indexed8:
return 8;
case BitmapFormat::RGB32:
@ -167,6 +185,8 @@ private:
Bitmap(BitmapFormat, const IntSize&, size_t pitch, RGBA32*);
Bitmap(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const IntSize&);
void allocate_palette_from_format(BitmapFormat);
IntSize m_size;
RGBA32* m_data { nullptr };
RGBA32* m_palette { nullptr };
@ -178,14 +198,24 @@ private:
RefPtr<SharedBuffer> m_shared_buffer;
};
inline u8* Bitmap::scanline_u8(int y)
{
return (u8*)m_data + (y * m_pitch);
}
inline const u8* Bitmap::scanline_u8(int y) const
{
return (const u8*)m_data + (y * m_pitch);
}
inline RGBA32* Bitmap::scanline(int y)
{
return reinterpret_cast<RGBA32*>((((u8*)m_data) + (y * m_pitch)));
return reinterpret_cast<RGBA32*>(scanline_u8(y));
}
inline const RGBA32* Bitmap::scanline(int y) const
{
return reinterpret_cast<const RGBA32*>((((const u8*)m_data) + (y * m_pitch)));
return reinterpret_cast<const RGBA32*>(scanline_u8(y));
}
inline const u8* Bitmap::bits(int y) const
@ -210,10 +240,28 @@ inline Color Bitmap::get_pixel<BitmapFormat::RGBA32>(int x, int y) const
return Color::from_rgba(scanline(y)[x]);
}
template<>
inline Color Bitmap::get_pixel<BitmapFormat::Indexed1>(int x, int y) const
{
return Color::from_rgb(m_palette[bits(y)[x]]);
}
template<>
inline Color Bitmap::get_pixel<BitmapFormat::Indexed2>(int x, int y) const
{
return Color::from_rgb(m_palette[bits(y)[x]]);
}
template<>
inline Color Bitmap::get_pixel<BitmapFormat::Indexed4>(int x, int y) const
{
return Color::from_rgb(m_palette[bits(y)[x]]);
}
template<>
inline Color Bitmap::get_pixel<BitmapFormat::Indexed8>(int x, int y) const
{
return Color::from_rgba(m_palette[bits(y)[x]]);
return Color::from_rgb(m_palette[bits(y)[x]]);
}
inline Color Bitmap::get_pixel(int x, int y) const
@ -223,11 +271,16 @@ inline Color Bitmap::get_pixel(int x, int y) const
return get_pixel<BitmapFormat::RGB32>(x, y);
case BitmapFormat::RGBA32:
return get_pixel<BitmapFormat::RGBA32>(x, y);
case BitmapFormat::Indexed1:
return get_pixel<BitmapFormat::Indexed1>(x, y);
case BitmapFormat::Indexed2:
return get_pixel<BitmapFormat::Indexed2>(x, y);
case BitmapFormat::Indexed4:
return get_pixel<BitmapFormat::Indexed4>(x, y);
case BitmapFormat::Indexed8:
return get_pixel<BitmapFormat::Indexed8>(x, y);
default:
ASSERT_NOT_REACHED();
return {};
}
}
@ -252,6 +305,9 @@ inline void Bitmap::set_pixel(int x, int y, Color color)
case BitmapFormat::RGBA32:
set_pixel<BitmapFormat::RGBA32>(x, y, color);
break;
case BitmapFormat::Indexed1:
case BitmapFormat::Indexed2:
case BitmapFormat::Indexed4:
case BitmapFormat::Indexed8:
ASSERT_NOT_REACHED();
default:

View File

@ -1,6 +1,7 @@
set(SOURCES
AffineTransform.cpp
Bitmap.cpp
BMPLoader.cpp
CharacterBitmap.cpp
Color.cpp
DisjointRectSet.cpp

View File

@ -25,6 +25,7 @@
*/
#include <LibGfx/ImageDecoder.h>
#include <LibGfx/BMPLoader.h>
#include <LibGfx/GIFLoader.h>
#include <LibGfx/PNGLoader.h>
@ -40,6 +41,10 @@ ImageDecoder::ImageDecoder(const u8* data, size_t size)
if (m_plugin->sniff())
return;
m_plugin = make<BMPImageDecoderPlugin>(data, size);
if (m_plugin->sniff())
return;
m_plugin = nullptr;
}

View File

@ -53,6 +53,12 @@ ALWAYS_INLINE Color get_pixel(const Gfx::Bitmap& bitmap, int x, int y)
{
if constexpr (format == BitmapFormat::Indexed8)
return bitmap.palette_color(bitmap.bits(y)[x]);
if constexpr (format == BitmapFormat::Indexed4)
return bitmap.palette_color(bitmap.bits(y)[x]);
if constexpr (format == BitmapFormat::Indexed2)
return bitmap.palette_color(bitmap.bits(y)[x]);
if constexpr (format == BitmapFormat::Indexed1)
return bitmap.palette_color(bitmap.bits(y)[x]);
if constexpr (format == BitmapFormat::RGB32)
return Color::from_rgb(bitmap.scanline(y)[x]);
if constexpr (format == BitmapFormat::RGBA32)
@ -668,7 +674,7 @@ void Painter::blit(const IntPoint& position, const Gfx::Bitmap& source, const In
return;
}
if (source.format() == BitmapFormat::Indexed8) {
if (Bitmap::is_indexed(source.format())) {
const u8* src = source.bits(src_rect.top() + first_row) + src_rect.left() + first_column;
const size_t src_skip = source.pitch();
for (int row = first_row; row <= last_row; ++row) {
@ -761,6 +767,15 @@ void Painter::draw_scaled_bitmap(const IntRect& a_dst_rect, const Gfx::Bitmap& s
case BitmapFormat::Indexed8:
do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Indexed8>);
break;
case BitmapFormat::Indexed4:
do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Indexed4>);
break;
case BitmapFormat::Indexed2:
do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Indexed2>);
break;
case BitmapFormat::Indexed1:
do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Indexed1>);
break;
default:
do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Invalid>);
break;

View File

@ -83,6 +83,8 @@ static String guess_mime_type_based_on_filename(const URL& url)
return "image/png";
if (url.path().ends_with(".gif"))
return "image/gif";
if (url.path().ends_with(".bmp"))
return "image/bmp";
if (url.path().ends_with(".md"))
return "text/markdown";
if (url.path().ends_with(".html") || url.path().ends_with(".htm"))