mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:27:35 +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:
parent
f6ce06d56b
commit
f3ff9c26bc
5 changed files with 12 additions and 26 deletions
|
@ -49,7 +49,7 @@ ErrorOr<void> read_image_data(PBMLoadingContext& context)
|
|||
}
|
||||
}
|
||||
|
||||
context.state = PBMLoadingContext::State::Bitmap;
|
||||
context.state = PBMLoadingContext::State::BitmapDecoded;
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ ErrorOr<void> read_image_data(PGMLoadingContext& context)
|
|||
}
|
||||
}
|
||||
|
||||
context.state = PGMLoadingContext::State::Bitmap;
|
||||
context.state = PGMLoadingContext::State::BitmapDecoded;
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ ErrorOr<void> read_image_data(PPMLoadingContext& context)
|
|||
}
|
||||
}
|
||||
|
||||
context.state = PPMLoadingContext::State::Bitmap;
|
||||
context.state = PPMLoadingContext::State::BitmapDecoded;
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {};
|
||||
}
|
||||
|
||||
|
|
|
@ -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<TContext>::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<ImageFrameDescriptor> PortableImageDecoderPlugin<TContext>::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");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue