mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-12 02:30:30 +09:00

Each one of `[PBM, PGM, PPM]Loader` used yet another stream-like relic. This patch ports all of them to `AK::Stream`.
70 lines
2 KiB
C++
70 lines
2 KiB
C++
/*
|
|
* Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include "PPMLoader.h"
|
|
#include "PortableImageLoaderCommon.h"
|
|
|
|
namespace Gfx {
|
|
|
|
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(stream);
|
|
if (red_or_error.is_error())
|
|
return false;
|
|
|
|
if (read_whitespace(context).is_error())
|
|
return false;
|
|
|
|
auto const green_or_error = read_number(stream);
|
|
if (green_or_error.is_error())
|
|
return false;
|
|
|
|
if (read_whitespace(context).is_error())
|
|
return false;
|
|
|
|
auto const blue_or_error = read_number(stream);
|
|
if (blue_or_error.is_error())
|
|
return false;
|
|
|
|
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() };
|
|
if (context.format_details.max_val < 255)
|
|
color = adjust_color(context.format_details.max_val, color);
|
|
color_data[i] = color;
|
|
}
|
|
} else if (context.type == PPMLoadingContext::Type::RAWBITS) {
|
|
for (u64 i = 0; i < context_size; ++i) {
|
|
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] };
|
|
}
|
|
}
|
|
|
|
if (!create_bitmap(context)) {
|
|
return false;
|
|
}
|
|
|
|
set_pixels(context, color_data);
|
|
|
|
context.state = PPMLoadingContext::State::Bitmap;
|
|
return true;
|
|
}
|
|
}
|