/src/serenity/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com> |
3 | | * Copyright (c) 2022, the SerenityOS developers. |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #include "PBMLoader.h" |
9 | | #include "PortableImageLoaderCommon.h" |
10 | | |
11 | | namespace Gfx { |
12 | | |
13 | | ErrorOr<void> read_image_data(PBMLoadingContext& context) |
14 | 332 | { |
15 | 332 | TRY(create_bitmap(context)); |
16 | | |
17 | 321 | auto& stream = *context.stream; |
18 | | |
19 | 321 | auto const context_size = context.width * context.height; |
20 | | |
21 | 321 | if (context.type == PBMLoadingContext::Type::ASCII) { |
22 | 1.20M | for (u64 i = 0; i < context_size; ++i) { |
23 | 1.20M | auto const byte = TRY(stream.read_value<u8>()); |
24 | 1.20M | if (byte == '0') |
25 | 33.9k | context.bitmap->set_pixel(i % context.width, i / context.width, Color::White); |
26 | 1.17M | else if (byte == '1') |
27 | 695k | context.bitmap->set_pixel(i % context.width, i / context.width, Color::Black); |
28 | 475k | else |
29 | 475k | i--; |
30 | 1.20M | } |
31 | 191 | } else if (context.type == PBMLoadingContext::Type::RAWBITS) { |
32 | 57.8k | for (u64 color_index = 0; color_index < context_size;) { |
33 | 57.8k | auto byte = TRY(stream.read_value<u8>()); |
34 | 289k | for (int i = 0; i < 8; i++) { |
35 | 281k | auto const val = byte & 0x80; |
36 | | |
37 | 281k | if (val == 0) |
38 | 162k | context.bitmap->set_pixel(color_index % context.width, color_index / context.width, Color::White); |
39 | 119k | else |
40 | 119k | context.bitmap->set_pixel(color_index % context.width, color_index / context.width, Color::Black); |
41 | | |
42 | 281k | byte = byte << 1; |
43 | 281k | color_index++; |
44 | | |
45 | 281k | if (color_index % context.width == 0) { |
46 | 49.2k | break; |
47 | 49.2k | } |
48 | 281k | } |
49 | 57.6k | } |
50 | 130 | } |
51 | | |
52 | 321 | return {}; |
53 | 321 | } |
54 | | } |