2020-06-21 12:00:22 +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-21 12:00:22 +03:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-21 12:00:22 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "PBMLoader.h"
|
2023-03-21 14:58:06 -04:00
|
|
|
#include "AK/Endian.h"
|
2020-12-20 10:19:03 -07:00
|
|
|
#include "PortableImageLoaderCommon.h"
|
2023-03-21 14:58:06 -04:00
|
|
|
#include "Userland/Libraries/LibGfx/Streamer.h"
|
2020-06-21 12:00:22 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
2022-03-13 11:58:58 -06:00
|
|
|
bool read_image_data(PBMLoadingContext& context, Streamer& streamer)
|
2020-06-21 12:00:22 +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);
|
|
|
|
|
|
2022-03-12 11:16:30 -07:00
|
|
|
if (context.type == PBMLoadingContext::Type::ASCII) {
|
2023-03-16 20:17:14 -04:00
|
|
|
for (u64 i = 0; i < context_size; ++i) {
|
|
|
|
|
u8 byte;
|
|
|
|
|
if (!streamer.read(byte))
|
|
|
|
|
return false;
|
|
|
|
|
if (byte == '0')
|
|
|
|
|
color_data[i] = Color::White;
|
|
|
|
|
else if (byte == '1')
|
|
|
|
|
color_data[i] = Color::Black;
|
|
|
|
|
else
|
|
|
|
|
i--;
|
2020-06-21 12:00:22 +03:00
|
|
|
}
|
2022-03-12 11:16:30 -07:00
|
|
|
} else if (context.type == PBMLoadingContext::Type::RAWBITS) {
|
2023-03-16 20:17:14 -04:00
|
|
|
for (u64 color_index = 0; color_index < context_size;) {
|
|
|
|
|
u8 byte;
|
|
|
|
|
if (!streamer.read(byte))
|
|
|
|
|
return false;
|
2020-06-21 12:00:22 +03:00
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
|
int val = byte & 0x80;
|
|
|
|
|
|
2023-03-16 20:17:14 -04:00
|
|
|
if (val == 0)
|
|
|
|
|
color_data[color_index] = Color::White;
|
|
|
|
|
else
|
|
|
|
|
color_data[color_index] = Color::Black;
|
2020-06-21 12:00:22 +03:00
|
|
|
|
|
|
|
|
byte = byte << 1;
|
|
|
|
|
color_index++;
|
|
|
|
|
|
|
|
|
|
if (color_index % context.width == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 10:19:03 -07:00
|
|
|
if (!create_bitmap(context)) {
|
2020-12-20 16:04:29 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2020-06-21 12:00:22 +03:00
|
|
|
|
2020-12-20 10:19:03 -07:00
|
|
|
set_pixels(context, color_data);
|
2020-06-21 12:00:22 +03:00
|
|
|
|
|
|
|
|
context.state = PBMLoadingContext::State::Bitmap;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|