1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:17:36 +00:00

LibGfx/PortableFormat: Simplify the State enum

This enum used to store very precise state about the decoding process,
let's simplify that by only including two steps: HeaderDecoder and
BitmapDecoded.
This commit is contained in:
Lucas CHOLLET 2023-07-10 16:41:42 -04:00 committed by Sam Atkins
parent f6ce06d56b
commit f3ff9c26bc
5 changed files with 12 additions and 26 deletions

View file

@ -49,7 +49,7 @@ ErrorOr<void> read_image_data(PBMLoadingContext& context)
} }
} }
context.state = PBMLoadingContext::State::Bitmap; context.state = PBMLoadingContext::State::BitmapDecoded;
return {}; return {};
} }
} }

View file

@ -36,7 +36,7 @@ ErrorOr<void> read_image_data(PGMLoadingContext& context)
} }
} }
context.state = PGMLoadingContext::State::Bitmap; context.state = PGMLoadingContext::State::BitmapDecoded;
return {}; return {};
} }
} }

View file

@ -45,7 +45,7 @@ ErrorOr<void> read_image_data(PPMLoadingContext& context)
} }
} }
context.state = PPMLoadingContext::State::Bitmap; context.state = PPMLoadingContext::State::BitmapDecoded;
return {}; return {};
} }
} }

View file

@ -73,9 +73,6 @@ static ErrorOr<void> read_comment(TContext& context)
template<typename TContext> template<typename TContext>
static ErrorOr<void> read_magic_number(TContext& context) static ErrorOr<void> read_magic_number(TContext& context)
{ {
if (context.state >= TContext::State::MagicNumber)
return {};
if (TRY(context.stream->size()) < 2) { if (TRY(context.stream->size()) < 2) {
dbgln_if(PORTABLE_IMAGE_LOADER_DEBUG, "There is no enough data for {}", TContext::FormatDetails::image_type); 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."); return Error::from_string_literal("There is no enough data to read magic number.");
@ -86,13 +83,11 @@ static ErrorOr<void> read_magic_number(TContext& context)
if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::ascii_magic_number) { if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::ascii_magic_number) {
context.type = TContext::Type::ASCII; context.type = TContext::Type::ASCII;
context.state = TContext::State::MagicNumber;
return {}; return {};
} }
if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::binary_magic_number) { if (magic_number[0] == 'P' && magic_number[1] == TContext::FormatDetails::binary_magic_number) {
context.type = TContext::Type::RAWBITS; context.type = TContext::Type::RAWBITS;
context.state = TContext::State::MagicNumber;
return {}; return {};
} }
@ -136,7 +131,6 @@ template<typename TContext>
static ErrorOr<void> read_width(TContext& context) static ErrorOr<void> read_width(TContext& context)
{ {
context.width = TRY(read_number(*context.stream)); context.width = TRY(read_number(*context.stream));
context.state = TContext::State::Width;
return {}; return {};
} }
@ -144,7 +138,6 @@ template<typename TContext>
static ErrorOr<void> read_height(TContext& context) static ErrorOr<void> read_height(TContext& context)
{ {
context.height = TRY(read_number(*context.stream)); context.height = TRY(read_number(*context.stream));
context.state = TContext::State::Height;
return {}; return {};
} }
@ -153,18 +146,14 @@ static ErrorOr<void> read_max_val(TContext& context)
{ {
context.format_details.max_val = TRY(read_number(*context.stream)); context.format_details.max_val = TRY(read_number(*context.stream));
if (context.format_details.max_val == 0) { if (context.format_details.max_val == 0)
context.state = TContext::State::Error;
return Error::from_string_literal("The image has a maximum value of 0"); return Error::from_string_literal("The image has a maximum value of 0");
}
if (context.format_details.max_val > 255) { 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); 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"); return Error::from_string_literal("Can't parse 2 byte color");
} }
context.state = TContext::State::Maxval;
return {}; return {};
} }
@ -198,19 +187,20 @@ static ErrorOr<void> read_header(Context& context)
TRY(read_whitespace(context)); TRY(read_whitespace(context));
} }
context.state = Context::State::HeaderDecoded;
return {}; return {};
} }
template<typename TContext> template<typename TContext>
static ErrorOr<void> decode(TContext& context) static ErrorOr<void> decode(TContext& context)
{ {
if (context.state >= TContext::State::Decoded) VERIFY(context.state == TContext::State::NotDecoded);
return {};
TRY(read_header(context)); TRY(read_header(context));
TRY(read_image_data(context)); TRY(read_image_data(context));
context.state = TContext::State::Decoded; context.state = TContext::State::BitmapDecoded;
return {}; return {};
} }

View file

@ -30,12 +30,8 @@ struct PortableImageMapLoadingContext {
enum class State { enum class State {
NotDecoded = 0, NotDecoded = 0,
Error, Error,
MagicNumber, HeaderDecoded,
Width, BitmapDecoded,
Height,
Maxval,
Bitmap,
Decoded
}; };
Type type { Type::Unknown }; Type type { Type::Unknown };
@ -89,7 +85,7 @@ IntSize PortableImageDecoderPlugin<TContext>::size()
if (m_context->state == TContext::State::Error) if (m_context->state == TContext::State::Error)
return {}; return {};
if (m_context->state < TContext::State::Decoded) { if (m_context->state < TContext::State::BitmapDecoded) {
if (decode(*m_context).is_error()) { if (decode(*m_context).is_error()) {
m_context->state = TContext::State::Error; m_context->state = TContext::State::Error;
// FIXME: We should propagate errors // FIXME: We should propagate errors
@ -156,7 +152,7 @@ ErrorOr<ImageFrameDescriptor> PortableImageDecoderPlugin<TContext>::frame(size_t
if (m_context->state == TContext::State::Error) if (m_context->state == TContext::State::Error)
return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed"); 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()) { if (decode(*m_context).is_error()) {
m_context->state = TContext::State::Error; m_context->state = TContext::State::Error;
return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed"); return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed");