mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:57:45 +00:00
LibGfx/PortableFormat: Simplify read_number
signature
The function signature goes from: `bool read_number(Streamer& streamer, TValue* value)` to `ErrorOr<u16> read_number(Streamer& streamer)` It allows us to, on one hand use `ErrorOr` for error propagation, removing an out parameter in the meantime, and on the other hand remove the useless template.
This commit is contained in:
parent
387894cdf2
commit
9052a6febf
3 changed files with 26 additions and 29 deletions
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue