diff options
Diffstat (limited to 'Userland')
3 files changed, 26 insertions, 29 deletions
diff --git a/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp index 0240548013..2ee82006b8 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp @@ -33,11 +33,11 @@ bool read_image_data(PGMLoadingContext& context, Streamer& streamer) Vector<Gfx::Color> color_data; if (context.type == PGMLoadingContext::Type::ASCII) { - u16 value; - while (true) { - if (!read_number(streamer, &value)) + auto number_or_error = read_number(streamer); + if (number_or_error.is_error()) break; + auto value = number_or_error.value(); if (!read_whitespace(context, streamer)) break; diff --git a/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp index c662e76b87..c8859350af 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp @@ -22,30 +22,29 @@ bool read_image_data(PPMLoadingContext& context, Streamer& streamer) color_data.ensure_capacity(context.width * context.height); if (context.type == PPMLoadingContext::Type::ASCII) { - u16 red; - u16 green; - u16 blue; - while (true) { - if (!read_number(streamer, &red)) + auto const red_or_error = read_number(streamer); + if (red_or_error.is_error()) break; if (!read_whitespace(context, streamer)) break; - if (!read_number(streamer, &green)) + auto const green_or_error = read_number(streamer); + if (green_or_error.is_error()) break; if (!read_whitespace(context, streamer)) break; - if (!read_number(streamer, &blue)) + auto const blue_or_error = read_number(streamer); + if (blue_or_error.is_error()) break; if (!read_whitespace(context, streamer)) break; - Color color { (u8)red, (u8)green, (u8)blue }; + Color color { (u8)red_or_error.value(), (u8)green_or_error.value(), (u8)blue_or_error.value() }; if (context.format_details.max_val < 255) color = adjust_color(context.format_details.max_val, color); color_data.append(color); diff --git a/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h b/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h index a17b6c1921..8ab778c555 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h +++ b/Userland/Libraries/LibGfx/ImageFormats/PortableImageLoaderCommon.h @@ -11,6 +11,7 @@ #include <AK/DeprecatedString.h> #include <AK/Endian.h> #include <AK/ScopeGuard.h> +#include <AK/String.h> #include <AK/StringBuilder.h> #include <AK/Types.h> #include <AK/Vector.h> @@ -30,8 +31,7 @@ static constexpr Color adjust_color(u16 max_val, Color color) return color; } -template<typename TValue> -static bool read_number(Streamer& streamer, TValue* value) +static inline ErrorOr<u16> read_number(Streamer& streamer) { u8 byte {}; StringBuilder sb {}; @@ -45,14 +45,11 @@ static bool read_number(Streamer& streamer, TValue* value) sb.append(byte); } - auto const opt_value = sb.to_deprecated_string().to_uint(); - if (!opt_value.has_value()) { - *value = 0; - return false; - } + auto const maybe_value = TRY(sb.to_string()).to_number<u16>(); + if (!maybe_value.has_value()) + return Error::from_string_literal("Can't convert bytes to a number"); - *value = static_cast<u16>(opt_value.value()); - return true; + return *maybe_value; } template<typename TContext> @@ -133,11 +130,11 @@ static bool read_whitespace(TContext& context, Streamer& streamer) template<typename TContext> static bool read_width(TContext& context, Streamer& streamer) { - if (bool const result = read_number(streamer, &context.width); - !result || context.width == 0) { + auto number_or_error = read_number(streamer); + if (number_or_error.is_error()) return false; - } + context.width = number_or_error.value(); context.state = TContext::State::Width; return true; } @@ -145,11 +142,11 @@ static bool read_width(TContext& context, Streamer& streamer) template<typename TContext> static bool read_height(TContext& context, Streamer& streamer) { - if (bool const result = read_number(streamer, &context.height); - !result || context.height == 0) { + auto number_or_error = read_number(streamer); + if (number_or_error.is_error()) return false; - } + context.height = number_or_error.value(); context.state = TContext::State::Height; return true; } @@ -157,10 +154,11 @@ static bool read_height(TContext& context, Streamer& streamer) template<typename TContext> static bool read_max_val(TContext& context, Streamer& streamer) { - if (bool const result = read_number(streamer, &context.format_details.max_val); - !result || context.format_details.max_val == 0) { + auto number_or_error = read_number(streamer); + if (number_or_error.is_error()) return false; - } + + context.format_details.max_val = number_or_error.value(); 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); |