/src/serenity/Userland/Libraries/LibGfx/ImageFormats/PGMLoader.cpp
Line | Count | Source (jump to first uncovered line) |
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 "PGMLoader.h" |
9 | | #include "PortableImageLoaderCommon.h" |
10 | | |
11 | | namespace Gfx { |
12 | | |
13 | | ErrorOr<void> read_image_data(PGMLoadingContext& context) |
14 | 479 | { |
15 | 479 | TRY(create_bitmap(context)); |
16 | | |
17 | 0 | auto& stream = *context.stream; |
18 | 468 | auto const context_size = context.width * context.height; |
19 | | |
20 | 468 | if (context.type == PGMLoadingContext::Type::ASCII) { |
21 | 326k | for (u64 i = 0; i < context_size; ++i) { |
22 | 326k | auto value = TRY(read_number(stream)); |
23 | | |
24 | 326k | TRY(read_whitespace(context)); |
25 | | |
26 | 0 | Color color { static_cast<u8>(value), static_cast<u8>(value), static_cast<u8>(value) }; |
27 | 326k | if (context.format_details.max_val < 255) |
28 | 325k | color = adjust_color(context.format_details.max_val, color); |
29 | | |
30 | 326k | context.bitmap->set_pixel(i % context.width, i / context.width, color); |
31 | 326k | } |
32 | 353 | } else if (context.type == PGMLoadingContext::Type::RAWBITS) { |
33 | 1.64M | for (u64 i = 0; i < context_size; ++i) { |
34 | 1.64M | auto const pixel = TRY(stream.read_value<u8>()); |
35 | 0 | context.bitmap->set_pixel(i % context.width, i / context.width, { pixel, pixel, pixel }); |
36 | 1.64M | } |
37 | 115 | } |
38 | | |
39 | 14 | return {}; |
40 | 468 | } |
41 | | } |