mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-25 17:25:08 +02:00
LibGfx/PortableFormat: Use finite loops in read_image_data
The `read_image_data` function of each one of[PBM, PGM, PPM]Loader use the same structure to read an image. This patch harmonizes the three functions and use finite loops instead of reading until EOF. It allows to quit early on bloated file, but it's mainly done for refactoring purpose.
This commit is contained in:
committed by
Andreas Kling
parent
24087ef6eb
commit
b9574c180e
Notes:
sideshowbarker
2024-07-19 01:59:31 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/b9574c180e Pull-request: https://github.com/SerenityOS/serenity/pull/17831 Reviewed-by: https://github.com/kleinesfilmroellchen ✅ Reviewed-by: https://github.com/nico
@@ -31,32 +31,31 @@ static void set_adjusted_pixels(PGMLoadingContext& context, Vector<Gfx::Color> c
|
||||
bool read_image_data(PGMLoadingContext& context, Streamer& streamer)
|
||||
{
|
||||
Vector<Gfx::Color> color_data;
|
||||
auto const context_size = context.width * context.height;
|
||||
|
||||
color_data.resize(context_size);
|
||||
|
||||
if (context.type == PGMLoadingContext::Type::ASCII) {
|
||||
while (true) {
|
||||
for (u64 i = 0; i < context_size; ++i) {
|
||||
auto number_or_error = read_number(streamer);
|
||||
if (number_or_error.is_error())
|
||||
break;
|
||||
return false;
|
||||
auto value = number_or_error.value();
|
||||
|
||||
if (read_whitespace(context, streamer).is_error())
|
||||
break;
|
||||
return false;
|
||||
|
||||
color_data.append({ (u8)value, (u8)value, (u8)value });
|
||||
color_data[i] = { (u8)value, (u8)value, (u8)value };
|
||||
}
|
||||
} else if (context.type == PGMLoadingContext::Type::RAWBITS) {
|
||||
u8 pixel;
|
||||
while (streamer.read(pixel)) {
|
||||
color_data.append({ pixel, pixel, pixel });
|
||||
for (u64 i = 0; i < context_size; ++i) {
|
||||
u8 pixel;
|
||||
if (!streamer.read(pixel))
|
||||
return false;
|
||||
color_data[i] = { pixel, pixel, pixel };
|
||||
}
|
||||
}
|
||||
|
||||
size_t context_size = (u32)context.width * (u32)context.height;
|
||||
if (context_size != color_data.size()) {
|
||||
dbgln("Not enough color data in image.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!create_bitmap(context))
|
||||
return false;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user