mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 00:05:07 +00:00
LibGfx: Commonize functions in P*MLoader class implementations
Problem: - Functions are duplicated in [PBM,PGM,PPM]Loader class implementations. They are functionally equivalent. This does not follow the DRY (Don't Repeat Yourself) principle. Solution: - Factor out the common functions into a separate file. - Refactor common code to generic functions. - Change `PPM_DEBUG` macro to be `PORTABLE_IMAGE_LOADER_DEBUG` to work with all the supported types. This requires adding the image type to the debug log messages for easier debugging.
This commit is contained in:
parent
6fac1abac4
commit
a95d230a3e
6 changed files with 367 additions and 685 deletions
|
@ -25,6 +25,7 @@
|
|||
*/
|
||||
|
||||
#include "PGMLoader.h"
|
||||
#include "PortableImageLoaderCommon.h"
|
||||
#include "Streamer.h"
|
||||
#include <AK/Endian.h>
|
||||
#include <AK/LexicalPath.h>
|
||||
|
@ -37,8 +38,8 @@ namespace Gfx {
|
|||
struct PGMLoadingContext {
|
||||
enum Type {
|
||||
Unknown,
|
||||
P2_ASCII,
|
||||
P5_RAWBITS
|
||||
ASCII,
|
||||
RAWBITS
|
||||
};
|
||||
|
||||
enum State {
|
||||
|
@ -52,6 +53,10 @@ struct PGMLoadingContext {
|
|||
Decoded
|
||||
};
|
||||
|
||||
static constexpr auto ascii_magic_number = '2';
|
||||
static constexpr auto binary_magic_number = '5';
|
||||
static constexpr auto image_type = "PGM";
|
||||
|
||||
Type type { Type::Unknown };
|
||||
State state { State::NotDecoded };
|
||||
const u8* data { nullptr };
|
||||
|
@ -62,169 +67,11 @@ struct PGMLoadingContext {
|
|||
RefPtr<Gfx::Bitmap> bitmap;
|
||||
};
|
||||
|
||||
ALWAYS_INLINE static Color adjust_color(u16 max_val, Color& color)
|
||||
{
|
||||
color.set_red((color.red() * 255) / max_val);
|
||||
color.set_green((color.green() * 255) / max_val);
|
||||
color.set_blue((color.blue() * 255) / max_val);
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
static bool read_number(Streamer& streamer, u16* value)
|
||||
{
|
||||
u8 byte;
|
||||
StringBuilder sb;
|
||||
|
||||
while (streamer.read(byte)) {
|
||||
if (byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r') {
|
||||
streamer.step_back();
|
||||
break;
|
||||
}
|
||||
|
||||
sb.append(byte);
|
||||
}
|
||||
|
||||
auto opt_value = sb.to_string().to_uint();
|
||||
if (!opt_value.has_value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*value = (u16)opt_value.value();
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool read_comment([[maybe_unused]] PGMLoadingContext& context, Streamer& streamer)
|
||||
{
|
||||
bool exist = false;
|
||||
u8 byte;
|
||||
|
||||
while (streamer.read(byte)) {
|
||||
switch (byte) {
|
||||
case '#': {
|
||||
exist = true;
|
||||
break;
|
||||
}
|
||||
case '\t':
|
||||
case '\n': {
|
||||
return exist;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return exist;
|
||||
}
|
||||
|
||||
static bool read_magic_number(PGMLoadingContext& context, Streamer& streamer)
|
||||
{
|
||||
if (context.state >= PGMLoadingContext::MagicNumber)
|
||||
return true;
|
||||
|
||||
if (!context.data || context.data_size < 2) {
|
||||
context.state = PGMLoadingContext::State::Error;
|
||||
dbg() << "There is no enough data.";
|
||||
return false;
|
||||
}
|
||||
|
||||
u8 magic_number[2];
|
||||
if (!streamer.read_bytes(magic_number, 2)) {
|
||||
context.state = PGMLoadingContext::State::Error;
|
||||
dbg() << "We can't read magic number.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (magic_number[0] == 'P' && magic_number[1] == '2') {
|
||||
context.type = PGMLoadingContext::P2_ASCII;
|
||||
context.state = PGMLoadingContext::MagicNumber;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (magic_number[0] == 'P' && magic_number[1] == '5') {
|
||||
context.type = PGMLoadingContext::P5_RAWBITS;
|
||||
context.state = PGMLoadingContext::MagicNumber;
|
||||
return true;
|
||||
}
|
||||
|
||||
context.state = PGMLoadingContext::State::Error;
|
||||
dbg() << "Magic number is not valid:" << (char)magic_number[0] << (char)magic_number[1];
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool read_white_space(PGMLoadingContext& context, Streamer& streamer)
|
||||
{
|
||||
bool exist = false;
|
||||
u8 byte;
|
||||
|
||||
while (streamer.read(byte)) {
|
||||
switch (byte) {
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\n':
|
||||
case '\r': {
|
||||
exist = true;
|
||||
break;
|
||||
}
|
||||
case '#': {
|
||||
streamer.step_back();
|
||||
read_comment(context, streamer);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
streamer.step_back();
|
||||
return exist;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return exist;
|
||||
}
|
||||
|
||||
static bool read_width(PGMLoadingContext& context, Streamer& streamer)
|
||||
{
|
||||
bool result = read_number(streamer, &context.width);
|
||||
if (!result || context.width == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
context.state = PGMLoadingContext::Width;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool read_height(PGMLoadingContext& context, Streamer& streamer)
|
||||
{
|
||||
bool result = read_number(streamer, &context.height);
|
||||
if (!result || context.height == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
context.state = PGMLoadingContext::Height;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool read_max_val(PGMLoadingContext& context, Streamer& streamer)
|
||||
{
|
||||
bool result = read_number(streamer, &context.max_val);
|
||||
if (!result || context.max_val == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (context.max_val > 255) {
|
||||
dbg() << "We can't pars 2 byte color.";
|
||||
context.state = PGMLoadingContext::Error;
|
||||
return false;
|
||||
}
|
||||
|
||||
context.state = PGMLoadingContext::Maxval;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool read_image_data(PGMLoadingContext& context, Streamer& streamer)
|
||||
{
|
||||
Vector<Gfx::Color> color_data;
|
||||
|
||||
if (context.type == PGMLoadingContext::P2_ASCII) {
|
||||
if (context.type == PGMLoadingContext::ASCII) {
|
||||
u16 value;
|
||||
|
||||
while (true) {
|
||||
|
@ -236,100 +83,31 @@ static bool read_image_data(PGMLoadingContext& context, Streamer& streamer)
|
|||
|
||||
color_data.append({ (u8)value, (u8)value, (u8)value });
|
||||
}
|
||||
} else if (context.type == PGMLoadingContext::P5_RAWBITS) {
|
||||
} else if (context.type == PGMLoadingContext::RAWBITS) {
|
||||
u8 pixel;
|
||||
while (streamer.read(pixel)) {
|
||||
color_data.append({ pixel, pixel, pixel });
|
||||
}
|
||||
}
|
||||
|
||||
context.bitmap = Bitmap::create_purgeable(BitmapFormat::RGB32, { context.width, context.height });
|
||||
if (!context.bitmap) {
|
||||
context.state = PGMLoadingContext::State::Error;
|
||||
if (!create_bitmap(context)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
for (int y = 0; y < context.height; ++y) {
|
||||
for (int x = 0; x < context.width; ++x) {
|
||||
Color color = color_data.at(index);
|
||||
if (context.max_val < 255)
|
||||
color = adjust_color(context.max_val, color);
|
||||
context.bitmap->set_pixel(x, y, color);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
set_adjusted_pixels(context, color_data);
|
||||
|
||||
context.state = PGMLoadingContext::State::Bitmap;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool decode_pgm(PGMLoadingContext& context)
|
||||
{
|
||||
if (context.state >= PGMLoadingContext::State::Decoded)
|
||||
return true;
|
||||
|
||||
Streamer streamer(context.data, context.data_size);
|
||||
|
||||
if (!read_magic_number(context, streamer))
|
||||
return false;
|
||||
|
||||
if (!read_white_space(context, streamer))
|
||||
return false;
|
||||
|
||||
if (!read_width(context, streamer))
|
||||
return false;
|
||||
|
||||
if (!read_white_space(context, streamer))
|
||||
return false;
|
||||
|
||||
if (!read_height(context, streamer))
|
||||
return false;
|
||||
|
||||
if (!read_white_space(context, streamer))
|
||||
return false;
|
||||
|
||||
if (!read_max_val(context, streamer))
|
||||
return false;
|
||||
|
||||
if (!read_white_space(context, streamer))
|
||||
return false;
|
||||
|
||||
if (!read_image_data(context, streamer))
|
||||
return false;
|
||||
|
||||
context.state = PGMLoadingContext::State::Decoded;
|
||||
return true;
|
||||
}
|
||||
|
||||
static RefPtr<Gfx::Bitmap> load_pgm_impl(const u8* data, size_t data_size)
|
||||
{
|
||||
PGMLoadingContext context;
|
||||
context.data = data;
|
||||
context.data_size = data_size;
|
||||
|
||||
if (!decode_pgm(context))
|
||||
return nullptr;
|
||||
|
||||
return context.bitmap;
|
||||
}
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_pgm(const StringView& path)
|
||||
{
|
||||
MappedFile mapped_file(path);
|
||||
if (!mapped_file.is_valid()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto bitmap = load_pgm_impl((const u8*)mapped_file.data(), mapped_file.size());
|
||||
if (bitmap)
|
||||
bitmap->set_mmap_name(String::format("Gfx::Bitmap [%dx%d] - Decoded PGM: %s", bitmap->width(), bitmap->height(), LexicalPath::canonicalized_path(path).characters()));
|
||||
return bitmap;
|
||||
return load<PGMLoadingContext>(path);
|
||||
}
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_pgm_from_memory(const u8* data, size_t length)
|
||||
{
|
||||
auto bitmap = load_pgm_impl(data, length);
|
||||
auto bitmap = load_impl<PGMLoadingContext>(data, length);
|
||||
if (bitmap)
|
||||
bitmap->set_mmap_name(String::format("Gfx::Bitmap [%dx%d] - Decoded PGM: <memory>", bitmap->width(), bitmap->height()));
|
||||
return bitmap;
|
||||
|
@ -352,7 +130,7 @@ IntSize PGMImageDecoderPlugin::size()
|
|||
return {};
|
||||
|
||||
if (m_context->state < PGMLoadingContext::State::Decoded) {
|
||||
bool success = decode_pgm(*m_context);
|
||||
bool success = decode(*m_context);
|
||||
if (!success)
|
||||
return {};
|
||||
}
|
||||
|
@ -366,7 +144,7 @@ RefPtr<Gfx::Bitmap> PGMImageDecoderPlugin::bitmap()
|
|||
return nullptr;
|
||||
|
||||
if (m_context->state < PGMLoadingContext::State::Decoded) {
|
||||
bool success = decode_pgm(*m_context);
|
||||
bool success = decode(*m_context);
|
||||
if (!success)
|
||||
return nullptr;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue