summary refs log tree commit diff
path: root/src/GFX/Image/PPMParser.hpp
blob: 07ff3c2cb1d703fa98f33be0b5a03b24dc99b484 (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
#pragma once

#include <cstdint>
#include <string_view>
#include "RawImage.hpp"

namespace MC::GFX::Image {

class PPMParser {
public:
    explicit PPMParser(std::string_view source) : m_source(source) {}

    RawImage parse();
private:
    enum PPMType {
        None,
        P3,
        P6
    };

    struct PPMHeader {
        PPMType type;
        uint32_t width;
        uint32_t height;
        uint8_t max_color;
    };

    PPMHeader parse_header();
    RawImage::Pixel parse_pixel(uint8_t max_color);
    uint64_t parse_sample();

    uint64_t chomp_number();
    std::string_view chomp_part();

    void skip_whitespace();
    void skip_comment();

    bool is_eof() const;

    std::string_view m_source;
    uint64_t m_cursor = 0;
};

}