2020-06-22 11:58:04 +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 11:58:04 +03:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-22 11:58:04 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "PPMLoader.h"
|
2020-12-20 10:19:03 -07:00
|
|
|
#include "PortableImageLoaderCommon.h"
|
2020-06-22 11:58:04 +03:00
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
2023-03-12 22:55:47 -04:00
|
|
|
ErrorOr<void> read_image_data(PPMLoadingContext& context)
|
2020-06-22 11:58:04 +03:00
|
|
|
{
|
2023-03-16 20:17:14 -04:00
|
|
|
auto const context_size = context.width * context.height;
|
2023-06-06 11:59:38 -04:00
|
|
|
|
|
|
|
|
TRY(create_bitmap(context));
|
2020-06-22 11:58:04 +03:00
|
|
|
|
2023-03-12 20:08:29 -04:00
|
|
|
auto& stream = *context.stream;
|
|
|
|
|
|
2022-03-12 11:16:30 -07:00
|
|
|
if (context.type == PPMLoadingContext::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 const red = TRY(read_number(stream));
|
|
|
|
|
TRY(read_whitespace(context));
|
2020-06-22 11:58:04 +03:00
|
|
|
|
2023-03-12 22:55:47 -04:00
|
|
|
auto const green = TRY(read_number(stream));
|
|
|
|
|
TRY(read_whitespace(context));
|
2020-06-22 11:58:04 +03:00
|
|
|
|
2023-03-12 22:55:47 -04:00
|
|
|
auto const blue = TRY(read_number(stream));
|
|
|
|
|
TRY(read_whitespace(context));
|
2020-06-22 11:58:04 +03:00
|
|
|
|
2023-03-16 20:28:40 -04:00
|
|
|
Color color { static_cast<u8>(red), static_cast<u8>(green), static_cast<u8>(blue) };
|
2022-03-12 11:16:30 -07:00
|
|
|
if (context.format_details.max_val < 255)
|
|
|
|
|
color = adjust_color(context.format_details.max_val, color);
|
2023-06-06 11:59:38 -04:00
|
|
|
context.bitmap->set_pixel(i % context.width, i / context.width, color);
|
2020-06-22 11:58:04 +03:00
|
|
|
}
|
2022-03-12 11:16:30 -07:00
|
|
|
} else if (context.type == PPMLoadingContext::Type::RAWBITS) {
|
2023-03-16 20:17:14 -04:00
|
|
|
for (u64 i = 0; i < context_size; ++i) {
|
2023-03-12 20:08:29 -04:00
|
|
|
Array<u8, 3> pixel;
|
|
|
|
|
Bytes buffer { pixel };
|
|
|
|
|
|
2023-03-12 22:55:47 -04:00
|
|
|
TRY(stream.read_until_filled(buffer));
|
|
|
|
|
|
2023-06-06 11:59:38 -04:00
|
|
|
context.bitmap->set_pixel(i % context.width, i / context.width, { pixel[0], pixel[1], pixel[2] });
|
2020-06-22 11:58:04 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 22:55:47 -04:00
|
|
|
return {};
|
2020-06-22 11:58:04 +03:00
|
|
|
}
|
|
|
|
|
}
|