summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx/PBMLoader.cpp
diff options
context:
space:
mode:
authorLenny Maiorani <lenny@serenityos.org>2022-03-13 11:58:58 -0600
committerAndreas Kling <kling@serenityos.org>2022-03-13 22:35:20 +0100
commit6a23dfbc92ed4dbadacdee6b28a62d13571a3117 (patch)
treef4e7c54fff7f0043abc68f7ac183094e09ce061b /Userland/Libraries/LibGfx/PBMLoader.cpp
parent786b02730c88fd0d202e375fc4798bc994699f43 (diff)
downloadserenity-6a23dfbc92ed4dbadacdee6b28a62d13571a3117.zip
LibGfx: Use common class template for PBM/PGM/PPM image loaders
PBM, PGM, and PPM image loaders are mostly common. The only difference is how the data is read and the associated magic numbers. The magic numbers are already made common using the loading contexts. Now make the implementations common via a class template which accepts the context to disambiguate.
Diffstat (limited to 'Userland/Libraries/LibGfx/PBMLoader.cpp')
-rw-r--r--Userland/Libraries/LibGfx/PBMLoader.cpp89
1 files changed, 1 insertions, 88 deletions
diff --git a/Userland/Libraries/LibGfx/PBMLoader.cpp b/Userland/Libraries/LibGfx/PBMLoader.cpp
index 795437ba8f..8a60c851ff 100644
--- a/Userland/Libraries/LibGfx/PBMLoader.cpp
+++ b/Userland/Libraries/LibGfx/PBMLoader.cpp
@@ -13,7 +13,7 @@
namespace Gfx {
-static bool read_image_data(PBMLoadingContext& context, Streamer& streamer)
+bool read_image_data(PBMLoadingContext& context, Streamer& streamer)
{
u8 byte;
Vector<Gfx::Color> color_data;
@@ -64,91 +64,4 @@ static bool read_image_data(PBMLoadingContext& context, Streamer& streamer)
context.state = PBMLoadingContext::State::Bitmap;
return true;
}
-
-PBMImageDecoderPlugin::PBMImageDecoderPlugin(const u8* data, size_t size)
-{
- m_context = make<PBMLoadingContext>();
- m_context->data = data;
- m_context->data_size = size;
-}
-
-PBMImageDecoderPlugin::~PBMImageDecoderPlugin()
-{
-}
-
-IntSize PBMImageDecoderPlugin::size()
-{
- if (m_context->state == PBMLoadingContext::State::Error)
- return {};
-
- if (m_context->state < PBMLoadingContext::State::Decoded) {
- bool success = decode(*m_context);
- if (!success)
- return {};
- }
-
- return { m_context->width, m_context->height };
-}
-
-void PBMImageDecoderPlugin::set_volatile()
-{
- if (m_context->bitmap)
- m_context->bitmap->set_volatile();
-}
-
-bool PBMImageDecoderPlugin::set_nonvolatile(bool& was_purged)
-{
- if (!m_context->bitmap)
- return false;
-
- return m_context->bitmap->set_nonvolatile(was_purged);
-}
-
-bool PBMImageDecoderPlugin::sniff()
-{
- if (m_context->data_size < 2)
- return false;
-
- if (m_context->data[0] == 'P' && m_context->data[1] == '1')
- return true;
-
- if (m_context->data[0] == 'P' && m_context->data[1] == '4')
- return true;
-
- return false;
-}
-
-bool PBMImageDecoderPlugin::is_animated()
-{
- return false;
-}
-
-size_t PBMImageDecoderPlugin::loop_count()
-{
- return 0;
-}
-
-size_t PBMImageDecoderPlugin::frame_count()
-{
- return 1;
-}
-
-ErrorOr<ImageFrameDescriptor> PBMImageDecoderPlugin::frame(size_t index)
-{
- if (index > 0)
- return Error::from_string_literal("PBMImageDecoderPlugin: Invalid frame index"sv);
-
- if (m_context->state == PBMLoadingContext::State::Error)
- return Error::from_string_literal("PBMImageDecoderPlugin: Decoding failed"sv);
-
- if (m_context->state < PBMLoadingContext::State::Decoded) {
- bool success = decode(*m_context);
- if (!success)
- return Error::from_string_literal("PBMImageDecoderPlugin: Decoding failed"sv);
- }
-
- VERIFY(m_context->bitmap);
- return ImageFrameDescriptor { m_context->bitmap, 0 };
-}
-
}