summary refs log tree commit diff
path: root/src/Image/PPMParser.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Image/PPMParser.hpp')
-rw-r--r--src/Image/PPMParser.hpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/Image/PPMParser.hpp b/src/Image/PPMParser.hpp
new file mode 100644
index 0000000..7d81cb8
--- /dev/null
+++ b/src/Image/PPMParser.hpp
@@ -0,0 +1,44 @@
+#pragma once
+
+#include <cstdint>
+#include <string_view>
+#include "RawImage.hpp"
+
+namespace MC::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();
+
+    std::string_view m_source;
+    uint64_t m_cursor = 0;
+};
+
+}