diff options
author | Lucas CHOLLET <lucas.chollet@free.fr> | 2023-03-16 20:17:14 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-03-24 10:56:58 +0100 |
commit | b9574c180e9d3d896a43feaf764f3e577db8132a (patch) | |
tree | 2eddbead7b5d29d8f996e5ef67141cc3c89397ab /Userland | |
parent | 24087ef6eb8bfc86b40a21954ee94e0094a7d758 (diff) | |
download | serenity-b9574c180e9d3d896a43feaf764f3e577db8132a.zip |
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.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp | 42 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp | 25 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp | 30 |
3 files changed, 48 insertions, 49 deletions
diff --git a/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp index dff6a12d66..666e1e0dd8 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp @@ -15,29 +15,35 @@ namespace Gfx { bool read_image_data(PBMLoadingContext& context, Streamer& streamer) { - u8 byte; Vector<Gfx::Color> color_data; + auto const context_size = context.width * context.height; + color_data.resize(context_size); + if (context.type == PBMLoadingContext::Type::ASCII) { - while (streamer.read(byte)) { - if (byte == '0') { - color_data.append(Color::White); - } else if (byte == '1') { - color_data.append(Color::Black); - } + for (u64 i = 0; i < context_size; ++i) { + u8 byte; + if (!streamer.read(byte)) + return false; + if (byte == '0') + color_data[i] = Color::White; + else if (byte == '1') + color_data[i] = Color::Black; + else + i--; } } else if (context.type == PBMLoadingContext::Type::RAWBITS) { - size_t color_index = 0; - - while (streamer.read(byte)) { + for (u64 color_index = 0; color_index < context_size;) { + u8 byte; + if (!streamer.read(byte)) + return false; for (int i = 0; i < 8; i++) { int val = byte & 0x80; - if (val == 0) { - color_data.append(Color::White); - } else { - color_data.append(Color::Black); - } + if (val == 0) + color_data[color_index] = Color::White; + else + color_data[color_index] = Color::Black; byte = byte << 1; color_index++; @@ -49,12 +55,6 @@ bool read_image_data(PBMLoadingContext& context, Streamer& streamer) } } - 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; } diff --git a/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp index c602967b83..432ecfe39f 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp @@ -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; diff --git a/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp index 7113a3aaa0..38bd341307 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.cpp @@ -19,46 +19,46 @@ namespace Gfx { bool read_image_data(PPMLoadingContext& context, Streamer& streamer) { Vector<Gfx::Color> color_data; - color_data.ensure_capacity(context.width * context.height); + auto const context_size = context.width * context.height; + color_data.resize(context_size); if (context.type == PPMLoadingContext::Type::ASCII) { - while (true) { + for (u64 i = 0; i < context_size; ++i) { auto const red_or_error = read_number(streamer); if (red_or_error.is_error()) - break; + return false; if (read_whitespace(context, streamer).is_error()) - break; + return false; auto const green_or_error = read_number(streamer); if (green_or_error.is_error()) - break; + return false; if (read_whitespace(context, streamer).is_error()) - break; + return false; auto const blue_or_error = read_number(streamer); if (blue_or_error.is_error()) - break; + return false; if (read_whitespace(context, streamer).is_error()) - break; + return false; 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); + color_data[i] = color; } } else if (context.type == PPMLoadingContext::Type::RAWBITS) { - u8 pixel[3]; - while (streamer.read_bytes(pixel, 3)) { - color_data.append({ pixel[0], pixel[1], pixel[2] }); + for (u64 i = 0; i < context_size; ++i) { + u8 pixel[3]; + if (!streamer.read_bytes(pixel, 3)) + return false; + color_data[i] = { pixel[0], pixel[1], pixel[2] }; } } - if (context.width * context.height != color_data.size()) - return false; - if (!create_bitmap(context)) { return false; } |