2020-06-22 15:19:57 +03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
|
2022-03-12 11:16:30 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-06-22 15:19:57 +03:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-22 15:19:57 +03:00
|
|
|
*/
|
|
|
|
|
|
2023-03-12 20:08:29 -04:00
|
|
|
#include "PGMLoader.h"
|
|
|
|
|
#include "PortableImageLoaderCommon.h"
|
2020-06-22 15:19:57 +03:00
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
static void set_adjusted_pixels(PGMLoadingContext& context, Vector<Gfx::Color> const& color_data)
|
2021-01-05 15:14:29 +01:00
|
|
|
{
|
|
|
|
|
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);
|
2022-03-12 11:16:30 -07:00
|
|
|
if (context.format_details.max_val < 255) {
|
|
|
|
|
color = adjust_color(context.format_details.max_val, color);
|
2021-01-05 15:14:29 +01:00
|
|
|
}
|
|
|
|
|
context.bitmap->set_pixel(x, y, color);
|
|
|
|
|
++index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 22:55:47 -04:00
|
|
|
ErrorOr<void> read_image_data(PGMLoadingContext& context)
|
2020-06-22 15:19:57 +03:00
|
|
|
{
|
2023-03-12 20:08:29 -04:00
|
|
|
auto& stream = *context.stream;
|
2020-06-22 15:19:57 +03:00
|
|
|
Vector<Gfx::Color> color_data;
|
2023-03-16 20:17:14 -04:00
|
|
|
auto const context_size = context.width * context.height;
|
|
|
|
|
|
|
|
|
|
color_data.resize(context_size);
|
2020-06-22 15:19:57 +03:00
|
|
|
|
2022-03-12 11:16:30 -07:00
|
|
|
if (context.type == PGMLoadingContext::Type::ASCII) {
|
2023-03-16 20:17:14 -04:00
|
|
|
for (u64 i = 0; i < context_size; ++i) {
|
2023-03-12 22:55:47 -04:00
|
|
|
auto value = TRY(read_number(stream));
|
2020-06-22 15:19:57 +03:00
|
|
|
|
2023-03-12 22:55:47 -04:00
|
|
|
TRY(read_whitespace(context));
|
2020-06-22 15:19:57 +03:00
|
|
|
|
2023-03-16 20:28:40 -04:00
|
|
|
color_data[i] = { static_cast<u8>(value), static_cast<u8>(value), static_cast<u8>(value) };
|
2020-06-22 15:19:57 +03:00
|
|
|
}
|
2022-03-12 11:16:30 -07:00
|
|
|
} else if (context.type == PGMLoadingContext::Type::RAWBITS) {
|
2023-03-16 20:17:14 -04:00
|
|
|
for (u64 i = 0; i < context_size; ++i) {
|
2023-03-12 22:55:47 -04:00
|
|
|
auto const pixel = TRY(stream.read_value<u8>());
|
2023-03-16 20:17:14 -04:00
|
|
|
color_data[i] = { pixel, pixel, pixel };
|
2020-06-22 15:19:57 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 22:55:47 -04:00
|
|
|
TRY(create_bitmap(context));
|
2021-01-05 15:14:29 +01:00
|
|
|
|
2020-12-20 10:19:03 -07:00
|
|
|
set_adjusted_pixels(context, color_data);
|
2020-06-22 15:19:57 +03:00
|
|
|
|
|
|
|
|
context.state = PGMLoadingContext::State::Bitmap;
|
2023-03-12 22:55:47 -04:00
|
|
|
return {};
|
2020-06-22 15:19:57 +03:00
|
|
|
}
|
|
|
|
|
}
|