1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 11:57:34 +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

@ -73,9 +73,6 @@ static ErrorOr<void> read_comment(TContext& context)
template<typename TContext>
static ErrorOr<void> 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<void> 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<typename TContext>
static ErrorOr<void> read_width(TContext& context)
{
context.width = TRY(read_number(*context.stream));
context.state = TContext::State::Width;
return {};
}
@ -144,7 +138,6 @@ template<typename TContext>
static ErrorOr<void> read_height(TContext& context)
{
context.height = TRY(read_number(*context.stream));
context.state = TContext::State::Height;
return {};
}
@ -153,18 +146,14 @@ static ErrorOr<void> 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<void> read_header(Context& context)
TRY(read_whitespace(context));
}
context.state = Context::State::HeaderDecoded;
return {};
}
template<typename TContext>
static ErrorOr<void> 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 {};
}