1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:08:10 +00:00

LibGfx/PortableFormat: Port to Stream

Each one of `[PBM, PGM, PPM]Loader` used yet another stream-like relic.
This patch ports all of them to `AK::Stream`.
This commit is contained in:
Lucas CHOLLET 2023-03-12 20:08:29 -04:00 committed by Andreas Kling
parent b9574c180e
commit 7cafd7d177
8 changed files with 88 additions and 79 deletions

View file

@ -7,42 +7,38 @@
#include "PPMLoader.h"
#include "PortableImageLoaderCommon.h"
#include <AK/Endian.h>
#include <AK/LexicalPath.h>
#include <AK/ScopeGuard.h>
#include <AK/StringBuilder.h>
#include <LibGfx/Streamer.h>
#include <string.h>
namespace Gfx {
bool read_image_data(PPMLoadingContext& context, Streamer& streamer)
bool read_image_data(PPMLoadingContext& context)
{
Vector<Gfx::Color> color_data;
auto const context_size = context.width * context.height;
color_data.resize(context_size);
auto& stream = *context.stream;
if (context.type == PPMLoadingContext::Type::ASCII) {
for (u64 i = 0; i < context_size; ++i) {
auto const red_or_error = read_number(streamer);
auto const red_or_error = read_number(stream);
if (red_or_error.is_error())
return false;
if (read_whitespace(context, streamer).is_error())
if (read_whitespace(context).is_error())
return false;
auto const green_or_error = read_number(streamer);
auto const green_or_error = read_number(stream);
if (green_or_error.is_error())
return false;
if (read_whitespace(context, streamer).is_error())
if (read_whitespace(context).is_error())
return false;
auto const blue_or_error = read_number(streamer);
auto const blue_or_error = read_number(stream);
if (blue_or_error.is_error())
return false;
if (read_whitespace(context, streamer).is_error())
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() };
@ -52,8 +48,11 @@ bool read_image_data(PPMLoadingContext& context, Streamer& streamer)
}
} else if (context.type == PPMLoadingContext::Type::RAWBITS) {
for (u64 i = 0; i < context_size; ++i) {
u8 pixel[3];
if (!streamer.read_bytes(pixel, 3))
Array<u8, 3> pixel;
Bytes buffer { pixel };
auto const result = stream.read_until_filled(buffer);
if (result.is_error())
return false;
color_data[i] = { pixel[0], pixel[1], pixel[2] };
}