diff --git a/Libraries/LibGfx/PGMLoader.cpp b/Libraries/LibGfx/PGMLoader.cpp index 88f50f5206..122a698a7b 100644 --- a/Libraries/LibGfx/PGMLoader.cpp +++ b/Libraries/LibGfx/PGMLoader.cpp @@ -67,6 +67,21 @@ struct PGMLoadingContext { RefPtr bitmap; }; +static void set_adjusted_pixels(PGMLoadingContext& context, const AK::Vector& color_data) +{ + size_t index = 0; + for (size_t y = 0; y < context.height; ++y) { + for (size_t x = 0; x < context.width; ++x) { + Color color = color_data.at(index); + if (context.max_val < 255) { + color = adjust_color(context.max_val, color); + } + context.bitmap->set_pixel(x, y, color); + ++index; + } + } +} + static bool read_image_data(PGMLoadingContext& context, Streamer& streamer) { Vector color_data; @@ -90,10 +105,15 @@ static bool read_image_data(PGMLoadingContext& context, Streamer& streamer) } } - if (!create_bitmap(context)) { + 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; + set_adjusted_pixels(context, color_data); context.state = PGMLoadingContext::State::Bitmap; diff --git a/Libraries/LibGfx/PortableImageLoaderCommon.h b/Libraries/LibGfx/PortableImageLoaderCommon.h index 63b71ca28e..b5b1c5e91f 100644 --- a/Libraries/LibGfx/PortableImageLoaderCommon.h +++ b/Libraries/LibGfx/PortableImageLoaderCommon.h @@ -216,22 +216,6 @@ static bool create_bitmap(TContext& context) return true; } -template -static void set_adjusted_pixels(TContext& context, const AK::Vector& color_data) -{ - size_t index = 0; - for (size_t y = 0; y < context.height; ++y) { - for (size_t x = 0; x < context.width; ++x) { - Color color = color_data.at(index); - if (context.max_val < 255) { - color = adjust_color(context.max_val, color); - } - context.bitmap->set_pixel(x, y, color); - index++; - } - } -} - template static void set_pixels(TContext& context, const AK::Vector& color_data) {