/src/serenity/Userland/Libraries/LibGfx/ImageFormats/PPMLoader.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 "PPMLoader.h" |
9 | | #include "PortableImageLoaderCommon.h" |
10 | | |
11 | | namespace Gfx { |
12 | | |
13 | | ErrorOr<void> read_image_data(PPMLoadingContext& context) |
14 | 477 | { |
15 | 477 | auto const context_size = context.width * context.height; |
16 | | |
17 | 477 | TRY(create_bitmap(context)); |
18 | | |
19 | 461 | auto& stream = *context.stream; |
20 | | |
21 | 461 | if (context.type == PPMLoadingContext::Type::ASCII) { |
22 | 89.2k | for (u64 i = 0; i < context_size; ++i) { |
23 | 89.2k | auto const red = TRY(read_number(stream)); |
24 | 89.0k | TRY(read_whitespace(context)); |
25 | | |
26 | 89.0k | auto const green = TRY(read_number(stream)); |
27 | 88.9k | TRY(read_whitespace(context)); |
28 | | |
29 | 88.9k | auto const blue = TRY(read_number(stream)); |
30 | 88.9k | TRY(read_whitespace(context)); |
31 | | |
32 | 88.9k | Color color { static_cast<u8>(red), static_cast<u8>(green), static_cast<u8>(blue) }; |
33 | 88.9k | if (context.format_details.max_val < 255) |
34 | 88.7k | color = adjust_color(context.format_details.max_val, color); |
35 | 88.9k | context.bitmap->set_pixel(i % context.width, i / context.width, color); |
36 | 88.9k | } |
37 | 330 | } else if (context.type == PPMLoadingContext::Type::RAWBITS) { |
38 | 371k | for (u64 i = 0; i < context_size; ++i) { |
39 | 371k | Array<u8, 3> pixel; |
40 | 371k | Bytes buffer { pixel }; |
41 | | |
42 | 371k | TRY(stream.read_until_filled(buffer)); |
43 | | |
44 | 371k | context.bitmap->set_pixel(i % context.width, i / context.width, { pixel[0], pixel[1], pixel[2] }); |
45 | 371k | } |
46 | 131 | } |
47 | | |
48 | 461 | return {}; |
49 | 461 | } |
50 | | } |