mirror of
https://github.com/SerenityOS/serenity
synced 2024-09-20 08:22:19 +00:00
LibGfx/PortableFormat: Propagate errors from read_image_data()
This commit is contained in:
parent
2356b48f13
commit
7ec310384a
|
@ -10,7 +10,7 @@
|
|||
|
||||
namespace Gfx {
|
||||
|
||||
bool read_image_data(PBMLoadingContext& context)
|
||||
ErrorOr<void> read_image_data(PBMLoadingContext& context)
|
||||
{
|
||||
auto& stream = *context.stream;
|
||||
Vector<Gfx::Color> color_data;
|
||||
|
@ -20,10 +20,7 @@ bool read_image_data(PBMLoadingContext& context)
|
|||
|
||||
if (context.type == PBMLoadingContext::Type::ASCII) {
|
||||
for (u64 i = 0; i < context_size; ++i) {
|
||||
auto byte_or_error = stream.read_value<u8>();
|
||||
if (byte_or_error.is_error())
|
||||
return false;
|
||||
auto const byte = byte_or_error.value();
|
||||
auto const byte = TRY(stream.read_value<u8>());
|
||||
if (byte == '0')
|
||||
color_data[i] = Color::White;
|
||||
else if (byte == '1')
|
||||
|
@ -33,10 +30,7 @@ bool read_image_data(PBMLoadingContext& context)
|
|||
}
|
||||
} else if (context.type == PBMLoadingContext::Type::RAWBITS) {
|
||||
for (u64 color_index = 0; color_index < context_size;) {
|
||||
auto byte_or_error = stream.read_value<u8>();
|
||||
if (byte_or_error.is_error())
|
||||
return false;
|
||||
auto byte = byte_or_error.value();
|
||||
auto byte = TRY(stream.read_value<u8>());
|
||||
for (int i = 0; i < 8; i++) {
|
||||
auto const val = byte & 0x80;
|
||||
|
||||
|
@ -55,13 +49,11 @@ bool read_image_data(PBMLoadingContext& context)
|
|||
}
|
||||
}
|
||||
|
||||
if (!create_bitmap(context)) {
|
||||
return false;
|
||||
}
|
||||
TRY(create_bitmap(context));
|
||||
|
||||
set_pixels(context, color_data);
|
||||
|
||||
context.state = PBMLoadingContext::State::Bitmap;
|
||||
return true;
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,5 +22,5 @@ struct PBM {
|
|||
using PBMLoadingContext = PortableImageMapLoadingContext<PBM>;
|
||||
using PBMImageDecoderPlugin = PortableImageDecoderPlugin<PBMLoadingContext>;
|
||||
|
||||
bool read_image_data(PBMLoadingContext& context);
|
||||
ErrorOr<void> read_image_data(PBMLoadingContext& context);
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ static void set_adjusted_pixels(PGMLoadingContext& context, Vector<Gfx::Color> c
|
|||
}
|
||||
}
|
||||
|
||||
bool read_image_data(PGMLoadingContext& context)
|
||||
ErrorOr<void> read_image_data(PGMLoadingContext& context)
|
||||
{
|
||||
auto& stream = *context.stream;
|
||||
Vector<Gfx::Color> color_data;
|
||||
|
@ -35,32 +35,24 @@ bool read_image_data(PGMLoadingContext& context)
|
|||
|
||||
if (context.type == PGMLoadingContext::Type::ASCII) {
|
||||
for (u64 i = 0; i < context_size; ++i) {
|
||||
auto number_or_error = read_number(stream);
|
||||
if (number_or_error.is_error())
|
||||
return false;
|
||||
auto value = number_or_error.value();
|
||||
auto value = TRY(read_number(stream));
|
||||
|
||||
if (read_whitespace(context).is_error())
|
||||
return false;
|
||||
TRY(read_whitespace(context));
|
||||
|
||||
color_data[i] = { (u8)value, (u8)value, (u8)value };
|
||||
}
|
||||
} else if (context.type == PGMLoadingContext::Type::RAWBITS) {
|
||||
for (u64 i = 0; i < context_size; ++i) {
|
||||
auto pixel_or_error = stream.read_value<u8>();
|
||||
if (pixel_or_error.is_error())
|
||||
return false;
|
||||
auto const pixel = pixel_or_error.value();
|
||||
auto const pixel = TRY(stream.read_value<u8>());
|
||||
color_data[i] = { pixel, pixel, pixel };
|
||||
}
|
||||
}
|
||||
|
||||
if (!create_bitmap(context))
|
||||
return false;
|
||||
TRY(create_bitmap(context));
|
||||
|
||||
set_adjusted_pixels(context, color_data);
|
||||
|
||||
context.state = PGMLoadingContext::State::Bitmap;
|
||||
return true;
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,5 +23,5 @@ struct PGM {
|
|||
using PGMLoadingContext = PortableImageMapLoadingContext<PGM>;
|
||||
using PGMImageDecoderPlugin = PortableImageDecoderPlugin<PGMLoadingContext>;
|
||||
|
||||
bool read_image_data(PGMLoadingContext& context);
|
||||
ErrorOr<void> read_image_data(PGMLoadingContext& context);
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
namespace Gfx {
|
||||
|
||||
bool read_image_data(PPMLoadingContext& context)
|
||||
ErrorOr<void> read_image_data(PPMLoadingContext& context)
|
||||
{
|
||||
Vector<Gfx::Color> color_data;
|
||||
auto const context_size = context.width * context.height;
|
||||
|
@ -20,28 +20,16 @@ bool read_image_data(PPMLoadingContext& context)
|
|||
|
||||
if (context.type == PPMLoadingContext::Type::ASCII) {
|
||||
for (u64 i = 0; i < context_size; ++i) {
|
||||
auto const red_or_error = read_number(stream);
|
||||
if (red_or_error.is_error())
|
||||
return false;
|
||||
auto const red = TRY(read_number(stream));
|
||||
TRY(read_whitespace(context));
|
||||
|
||||
if (read_whitespace(context).is_error())
|
||||
return false;
|
||||
auto const green = TRY(read_number(stream));
|
||||
TRY(read_whitespace(context));
|
||||
|
||||
auto const green_or_error = read_number(stream);
|
||||
if (green_or_error.is_error())
|
||||
return false;
|
||||
auto const blue = TRY(read_number(stream));
|
||||
TRY(read_whitespace(context));
|
||||
|
||||
if (read_whitespace(context).is_error())
|
||||
return false;
|
||||
|
||||
auto const blue_or_error = read_number(stream);
|
||||
if (blue_or_error.is_error())
|
||||
return false;
|
||||
|
||||
if (read_whitespace(context).is_error())
|
||||
return false;
|
||||
|
||||
Color color { (u8)red_or_error.value(), (u8)green_or_error.value(), (u8)blue_or_error.value() };
|
||||
Color color { (u8)red, (u8)green, (u8)blue };
|
||||
if (context.format_details.max_val < 255)
|
||||
color = adjust_color(context.format_details.max_val, color);
|
||||
color_data[i] = color;
|
||||
|
@ -51,20 +39,17 @@ bool read_image_data(PPMLoadingContext& context)
|
|||
Array<u8, 3> pixel;
|
||||
Bytes buffer { pixel };
|
||||
|
||||
auto const result = stream.read_until_filled(buffer);
|
||||
if (result.is_error())
|
||||
return false;
|
||||
TRY(stream.read_until_filled(buffer));
|
||||
|
||||
color_data[i] = { pixel[0], pixel[1], pixel[2] };
|
||||
}
|
||||
}
|
||||
|
||||
if (!create_bitmap(context)) {
|
||||
return false;
|
||||
}
|
||||
TRY(create_bitmap(context));
|
||||
|
||||
set_pixels(context, color_data);
|
||||
|
||||
context.state = PPMLoadingContext::State::Bitmap;
|
||||
return true;
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,5 +23,5 @@ struct PPM {
|
|||
using PPMLoadingContext = PortableImageMapLoadingContext<PPM>;
|
||||
using PPMImageDecoderPlugin = PortableImageDecoderPlugin<PPMLoadingContext>;
|
||||
|
||||
bool read_image_data(PPMLoadingContext& context);
|
||||
ErrorOr<void> read_image_data(PPMLoadingContext& context);
|
||||
}
|
||||
|
|
|
@ -165,15 +165,10 @@ static ErrorOr<void> read_max_val(TContext& context)
|
|||
}
|
||||
|
||||
template<typename TContext>
|
||||
static bool create_bitmap(TContext& context)
|
||||
static ErrorOr<void> create_bitmap(TContext& context)
|
||||
{
|
||||
auto bitmap_or_error = Bitmap::create(BitmapFormat::BGRx8888, { context.width, context.height });
|
||||
if (bitmap_or_error.is_error()) {
|
||||
context.state = TContext::State::Error;
|
||||
return false;
|
||||
}
|
||||
context.bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
|
||||
return true;
|
||||
context.bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { context.width, context.height }));
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename TContext>
|
||||
|
@ -229,7 +224,7 @@ static bool decode(TContext& context)
|
|||
return false;
|
||||
}
|
||||
|
||||
if (!read_image_data(context))
|
||||
if (read_image_data(context).is_error())
|
||||
return false;
|
||||
|
||||
error_guard.disarm();
|
||||
|
|
Loading…
Reference in a new issue