summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx/ImageFormats/PBMLoader.cpp
blob: c036f8b9f9c5a0f4f908144394cd9c7049c12b5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "PBMLoader.h"
#include "PortableImageLoaderCommon.h"

namespace Gfx {

ErrorOr<void> read_image_data(PBMLoadingContext& context)
{
    auto& stream = *context.stream;
    Vector<Gfx::Color> color_data;

    auto const context_size = context.width * context.height;
    color_data.resize(context_size);

    if (context.type == PBMLoadingContext::Type::ASCII) {
        for (u64 i = 0; i < context_size; ++i) {
            auto const byte = TRY(stream.read_value<u8>());
            if (byte == '0')
                color_data[i] = Color::White;
            else if (byte == '1')
                color_data[i] = Color::Black;
            else
                i--;
        }
    } else if (context.type == PBMLoadingContext::Type::RAWBITS) {
        for (u64 color_index = 0; color_index < context_size;) {
            auto byte = TRY(stream.read_value<u8>());
            for (int i = 0; i < 8; i++) {
                auto const val = byte & 0x80;

                if (val == 0)
                    color_data[color_index] = Color::White;
                else
                    color_data[color_index] = Color::Black;

                byte = byte << 1;
                color_index++;

                if (color_index % context.width == 0) {
                    break;
                }
            }
        }
    }

    TRY(create_bitmap(context));

    set_pixels(context, color_data);

    context.state = PBMLoadingContext::State::Bitmap;
    return {};
}
}