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