summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx/PGMLoader.cpp
diff options
context:
space:
mode:
authorLenny Maiorani <lenny@serenityos.org>2022-03-12 11:16:30 -0700
committerAndreas Kling <kling@serenityos.org>2022-03-13 22:35:20 +0100
commit786b02730c88fd0d202e375fc4798bc994699f43 (patch)
treedc4bf176b79ecd92dcafe2c89d6135af9512ca52 /Userland/Libraries/LibGfx/PGMLoader.cpp
parent5c21f963ff7a11ec160707d1a6f6831af38a3af0 (diff)
downloadserenity-786b02730c88fd0d202e375fc4798bc994699f43.zip
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.
Diffstat (limited to 'Userland/Libraries/LibGfx/PGMLoader.cpp')
-rw-r--r--Userland/Libraries/LibGfx/PGMLoader.cpp41
1 files changed, 5 insertions, 36 deletions
diff --git a/Userland/Libraries/LibGfx/PGMLoader.cpp b/Userland/Libraries/LibGfx/PGMLoader.cpp
index 9280f7f528..1550673fea 100644
--- a/Userland/Libraries/LibGfx/PGMLoader.cpp
+++ b/Userland/Libraries/LibGfx/PGMLoader.cpp
@@ -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 });