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-08-25 15:11:15 +02:00
|
|
|
#include <AK/Endian.h>
|
2020-06-22 11:58:04 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
2020-11-13 11:58:00 +01:00
|
|
|
#include <AK/ScopeGuard.h>
|
2020-06-22 11:58:04 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2023-03-21 14:58:06 -04:00
|
|
|
#include <LibGfx/Streamer.h>
|
2020-06-22 11:58:04 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
2022-03-13 11:58:58 -06:00
|
|
|
bool read_image_data(PPMLoadingContext& context, Streamer& streamer)
|
2020-06-22 11:58:04 +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 11:58:04 +03:00
|
|
|
|
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 15:02:03 -04:00
|
|
|
auto const red_or_error = read_number(streamer);
|
|
|
|
|
if (red_or_error.is_error())
|
2023-03-16 20:17:14 -04:00
|
|
|
return false;
|
2020-06-22 11:58:04 +03:00
|
|
|
|
2023-03-12 16:05:12 -04:00
|
|
|
if (read_whitespace(context, streamer).is_error())
|
2023-03-16 20:17:14 -04:00
|
|
|
return false;
|
2020-06-22 11:58:04 +03:00
|
|
|
|
2023-03-12 15:02:03 -04:00
|
|
|
auto const green_or_error = read_number(streamer);
|
|
|
|
|
if (green_or_error.is_error())
|
2023-03-16 20:17:14 -04:00
|
|
|
return false;
|
2020-06-22 11:58:04 +03:00
|
|
|
|
2023-03-12 16:05:12 -04:00
|
|
|
if (read_whitespace(context, streamer).is_error())
|
2023-03-16 20:17:14 -04:00
|
|
|
return false;
|
2020-06-22 11:58:04 +03:00
|
|
|
|
2023-03-12 15:02:03 -04:00
|
|
|
auto const blue_or_error = read_number(streamer);
|
|
|
|
|
if (blue_or_error.is_error())
|
2023-03-16 20:17:14 -04:00
|
|
|
return false;
|
2020-06-22 11:58:04 +03:00
|
|
|
|
2023-03-12 16:05:12 -04:00
|
|
|
if (read_whitespace(context, streamer).is_error())
|
2023-03-16 20:17:14 -04:00
|
|
|
return false;
|
2020-06-22 11:58:04 +03:00
|
|
|
|
2023-03-12 15:02:03 -04:00
|
|
|
Color color { (u8)red_or_error.value(), (u8)green_or_error.value(), (u8)blue_or_error.value() };
|
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-03-16 20:17:14 -04:00
|
|
|
color_data[i] = 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) {
|
|
|
|
|
u8 pixel[3];
|
|
|
|
|
if (!streamer.read_bytes(pixel, 3))
|
|
|
|
|
return false;
|
|
|
|
|
color_data[i] = { pixel[0], pixel[1], pixel[2] };
|
2020-06-22 11:58:04 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 10:19:03 -07:00
|
|
|
if (!create_bitmap(context)) {
|
2020-12-20 16:04:29 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2020-06-22 11:58:04 +03:00
|
|
|
|
2020-12-20 10:19:03 -07:00
|
|
|
set_pixels(context, color_data);
|
2020-06-22 11:58:04 +03:00
|
|
|
|
|
|
|
|
context.state = PPMLoadingContext::State::Bitmap;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|