diff --git a/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp index e18eb3a1b1..fe26449f8b 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp @@ -49,7 +49,7 @@ ErrorOr read_image_data(PBMLoadingContext& context) } } - context.state = PBMLoadingContext::State::Bitmap; + context.state = PBMLoadingContext::State::BitmapDecoded; return {}; } } diff --git a/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp index f1a793662c..3e4b15528a 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp @@ -36,7 +36,7 @@ ErrorOr read_image_data(PGMLoadingContext& context) } } - context.state = PGMLoadingContext::State::Bitmap; + context.state = PGMLoadingContext::State::BitmapDecoded; return {}; } } diff --git a/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp index 562e586006..726bccc7fe 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp @@ -45,7 +45,7 @@ ErrorOr read_image_data(PPMLoadingContext& context) } } - context.state = PPMLoadingContext::State::Bitmap; + context.state = PPMLoadingContext::State::BitmapDecoded; return {}; } } diff --git a/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h b/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h index 0e675cd653..c9538111e4 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h +++ b/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h @@ -73,9 +73,6 @@ static ErrorOr read_comment(TContext& context) template static ErrorOr read_magic_number(TContext& context) { - if (context.state >= TContext::State::MagicNumber) - return {}; - if (TRY(context.stream->size()) < 2) { dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "There is no enough data for {}", TContext::FormatDetails::image_type); return Error::from_string_literal("There is no enough data to read magic number."); @@ -86,13 +83,11 @@ static ErrorOr read_magic_number(TContext& context) if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::ascii_magic_number) { context.type = TContext::Type::ASCII; - context.state = TContext::State::MagicNumber; return {}; } if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::binary_magic_number) { context.type = TContext::Type::RAWBITS; - context.state = TContext::State::MagicNumber; return {}; } @@ -136,7 +131,6 @@ template static ErrorOr read_width(TContext& context) { context.width = TRY(read_number(*context.stream)); - context.state = TContext::State::Width; return {}; } @@ -144,7 +138,6 @@ template static ErrorOr read_height(TContext& context) { context.height = TRY(read_number(*context.stream)); - context.state = TContext::State::Height; return {}; } @@ -153,18 +146,14 @@ static ErrorOr read_max_val(TContext& context) { context.format_details.max_val = TRY(read_number(*context.stream)); - if (context.format_details.max_val == 0) { - context.state = TContext::State::Error; + if (context.format_details.max_val == 0) return Error::from_string_literal("The image has a maximum value of 0"); - } if (context.format_details.max_val > 255) { dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "We can't parse 2 byte color for {}", TContext::FormatDetails::image_type); - context.state = TContext::State::Error; return Error::from_string_literal("Can't parse 2 byte color"); } - context.state = TContext::State::Maxval; return {}; } @@ -198,19 +187,20 @@ static ErrorOr read_header(Context& context) TRY(read_whitespace(context)); } + context.state = Context::State::HeaderDecoded; + return {}; } template static ErrorOr decode(TContext& context) { - if (context.state >= TContext::State::Decoded) - return {}; + VERIFY(context.state == TContext::State::NotDecoded); TRY(read_header(context)); TRY(read_image_data(context)); - context.state = TContext::State::Decoded; + context.state = TContext::State::BitmapDecoded; return {}; } diff --git a/Userland/Libraries/LibGfx/ImageFormats/PortableImageMapLoader.h b/Userland/Libraries/LibGfx/ImageFormats/PortableImageMapLoader.h index 68dac7d481..80f6bc20a5 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PortableImageMapLoader.h +++ b/Userland/Libraries/LibGfx/ImageFormats/PortableImageMapLoader.h @@ -30,12 +30,8 @@ struct PortableImageMapLoadingContext { enum class State { NotDecoded = 0, Error, - MagicNumber, - Width, - Height, - Maxval, - Bitmap, - Decoded + HeaderDecoded, + BitmapDecoded, }; Type type { Type::Unknown }; @@ -89,7 +85,7 @@ IntSize PortableImageDecoderPlugin::size() if (m_context->state == TContext::State::Error) return {}; - if (m_context->state < TContext::State::Decoded) { + if (m_context->state < TContext::State::BitmapDecoded) { if (decode(*m_context).is_error()) { m_context->state = TContext::State::Error; // FIXME: We should propagate errors @@ -156,7 +152,7 @@ ErrorOr PortableImageDecoderPlugin::frame(size_t if (m_context->state == TContext::State::Error) return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed"); - if (m_context->state < TContext::State::Decoded) { + if (m_context->state < TContext::State::BitmapDecoded) { if (decode(*m_context).is_error()) { m_context->state = TContext::State::Error; return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed");