diff options
7 files changed, 30 insertions, 66 deletions
diff --git a/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp index 959f214d6f..c036f8b9f9 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp @@ -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 {}; } } diff --git a/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.h b/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.h index 767dda0337..6bc29697f5 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.h +++ b/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.h @@ -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); } diff --git a/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp index 18a3d7fd74..6b33a7f8de 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp @@ -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 {}; } } diff --git a/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.h b/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.h index c02a921539..67c593be91 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.h +++ b/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.h @@ -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); } diff --git a/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp index 10b442de54..8daf98ff47 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp @@ -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 {}; } } diff --git a/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.h b/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.h index 9803839958..846ba8557a 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.h +++ b/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.h @@ -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); } diff --git a/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h b/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h index 27c9f8d1db..39c635db4b 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h +++ b/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h @@ -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(); |