1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:27:35 +00:00

LibGfx: Commonize P[BGP]M file loading contexts

Much of the code in PBM, PGM, and PPM image loaders is common. The
contexts are nearly identical. Instead of writing multiple contexts,
write 1 with a template argument to pass in the details of the given
format.
This commit is contained in:
Lenny Maiorani 2022-03-12 11:16:30 -07:00 committed by Andreas Kling
parent 5c21f963ff
commit 786b02730c
8 changed files with 109 additions and 124 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -12,46 +13,14 @@
namespace Gfx {
struct PGMLoadingContext {
enum Type {
Unknown,
ASCII,
RAWBITS
};
enum State {
NotDecoded = 0,
Error,
MagicNumber,
Width,
Height,
Maxval,
Bitmap,
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 };
size_t data_size { 0 };
u16 width { 0 };
u16 height { 0 };
u16 max_val { 0 };
RefPtr<Gfx::Bitmap> bitmap;
};
static void set_adjusted_pixels(PGMLoadingContext& context, const Vector<Gfx::Color>& color_data)
{
size_t index = 0;
for (size_t y = 0; y < context.height; ++y) {
for (size_t x = 0; x < context.width; ++x) {
Color color = color_data.at(index);
if (context.max_val < 255) {
color = adjust_color(context.max_val, color);
if (context.format_details.max_val < 255) {
color = adjust_color(context.format_details.max_val, color);
}
context.bitmap->set_pixel(x, y, color);
++index;
@ -63,7 +32,7 @@ static bool read_image_data(PGMLoadingContext& context, Streamer& streamer)
{
Vector<Gfx::Color> color_data;
if (context.type == PGMLoadingContext::ASCII) {
if (context.type == PGMLoadingContext::Type::ASCII) {
u16 value;
while (true) {
@ -75,7 +44,7 @@ static bool read_image_data(PGMLoadingContext& context, Streamer& streamer)
color_data.append({ (u8)value, (u8)value, (u8)value });
}
} else if (context.type == PGMLoadingContext::RAWBITS) {
} else if (context.type == PGMLoadingContext::Type::RAWBITS) {
u8 pixel;
while (streamer.read(pixel)) {
color_data.append({ pixel, pixel, pixel });