diff options
| author | Mel <einebeere@gmail.com> | 2023-07-08 03:25:44 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2023-07-08 03:25:44 +0200 |
| commit | fe2baedc760c2f29e2c720f6b1132a2de33c5430 (patch) | |
| tree | dfbe1c72a17805a3cab6e0d47433e9021890c9ca /src/GFX/Image | |
| parent | 41fbca10f6c6cdd9c1623f1347e7ecb40f5e7f59 (diff) | |
| download | meowcraft-fe2baedc760c2f29e2c720f6b1132a2de33c5430.tar.zst meowcraft-fe2baedc760c2f29e2c720f6b1132a2de33c5430.zip | |
Use own size types
Diffstat (limited to 'src/GFX/Image')
| -rw-r--r-- | src/GFX/Image/PPMParser.cpp | 24 | ||||
| -rw-r--r-- | src/GFX/Image/PPMParser.hpp | 18 | ||||
| -rw-r--r-- | src/GFX/Image/RawImage.cpp | 14 | ||||
| -rw-r--r-- | src/GFX/Image/RawImage.hpp | 17 |
4 files changed, 36 insertions, 37 deletions
diff --git a/src/GFX/Image/PPMParser.cpp b/src/GFX/Image/PPMParser.cpp index 1f62871..3c95bba 100644 --- a/src/GFX/Image/PPMParser.cpp +++ b/src/GFX/Image/PPMParser.cpp @@ -18,7 +18,7 @@ RawImage PPMParser::parse() { auto pixel_count = header.width * header.height; RawImage image(header.width, header.height); - for (uint64_t pixel_index = 0; pixel_index < pixel_count; pixel_index++) { + for (U64 pixel_index = 0; pixel_index < pixel_count; pixel_index++) { RawImage::Pixel pixel = parse_pixel(header.max_color); image.add(pixel); } @@ -57,7 +57,7 @@ PPMParser::PPMHeader PPMParser::parse_header() { return header; } -RawImage::Pixel PPMParser::parse_pixel(uint8_t max_color) { +RawImage::Pixel PPMParser::parse_pixel(U8 max_color) { auto r_sample = parse_sample(); auto g_sample = parse_sample(); auto b_sample = parse_sample(); @@ -66,7 +66,7 @@ RawImage::Pixel PPMParser::parse_pixel(uint8_t max_color) { throw std::runtime_error("Sample can not be greater than Maxval."); } - auto map_to_range = [=](uint64_t s) -> uint8_t { return s * 255 / max_color; }; + auto map_to_range = [=](U64 s) -> U8 { return s * 255 / max_color; }; RawImage::Pixel p{ map_to_range(r_sample), @@ -82,23 +82,23 @@ RawImage::Pixel PPMParser::parse_pixel(uint8_t max_color) { return p; } -uint64_t PPMParser::parse_sample() { +U64 PPMParser::parse_sample() { skip_whitespace(); auto sample = chomp_number(); skip_whitespace(); return sample; } -uint64_t PPMParser::chomp_number() { +U64 PPMParser::chomp_number() { auto raw = chomp_part(); - uint64_t number = 0; - for (uint8_t digit_ascii : raw) { + U64 number = 0; + for (U8 digit_ascii : raw) { if (digit_ascii < '0' || digit_ascii > '9') { throw std::runtime_error("Number contains non ASCII digits."); } - uint8_t digit = digit_ascii - '0'; + U8 digit = digit_ascii - '0'; number *= 10; number += digit; @@ -108,7 +108,7 @@ uint64_t PPMParser::chomp_number() { } std::string_view PPMParser::chomp_part() { - uint64_t length = 0; + U64 length = 0; while (!is_eof()) { auto c = m_source[m_cursor + length]; @@ -127,7 +127,7 @@ std::string_view PPMParser::chomp_part() { void PPMParser::skip_whitespace() { while (!is_eof()) { - uint8_t c = m_source[m_cursor]; + U8 c = m_source[m_cursor]; if (c == '#') { skip_comment(); continue; @@ -142,7 +142,7 @@ void PPMParser::skip_whitespace() { } void PPMParser::skip_comment() { - uint8_t c = m_source[m_cursor]; + U8 c = m_source[m_cursor]; if (c != '#') { return; } @@ -152,7 +152,7 @@ void PPMParser::skip_comment() { } } -bool PPMParser::is_eof() const { +Bool PPMParser::is_eof() const { return m_cursor >= m_source.size(); } diff --git a/src/GFX/Image/PPMParser.hpp b/src/GFX/Image/PPMParser.hpp index a7e9b41..77f1cec 100644 --- a/src/GFX/Image/PPMParser.hpp +++ b/src/GFX/Image/PPMParser.hpp @@ -1,6 +1,6 @@ #pragma once -#include <cstdint> +#include "../../Common/Sizes.hpp" #include <string_view> #include "RawImage.hpp" @@ -20,27 +20,27 @@ private: struct PPMHeader { PPMType type; - uint32_t width; - uint32_t height; - uint8_t max_color; + U32 width; + U32 height; + U8 max_color; }; PPMHeader parse_header(); - RawImage::Pixel parse_pixel(uint8_t max_color); - uint64_t parse_sample(); + RawImage::Pixel parse_pixel(U8 max_color); + U64 parse_sample(); - uint64_t chomp_number(); + U64 chomp_number(); std::string_view chomp_part(); void skip_whitespace(); void skip_comment(); - bool is_eof() const; + Bool is_eof() const; static constexpr RawImage::Pixel alpha_color{192, 0, 255}; std::string_view m_source; - uint64_t m_cursor = 0; + U64 m_cursor = 0; }; } diff --git a/src/GFX/Image/RawImage.cpp b/src/GFX/Image/RawImage.cpp index 0bd2947..9f156b8 100644 --- a/src/GFX/Image/RawImage.cpp +++ b/src/GFX/Image/RawImage.cpp @@ -7,30 +7,30 @@ void RawImage::add(Pixel pixel) { m_pixels.push_back(pixel); } -size_t RawImage::size() const { +USize RawImage::size() const { return m_pixels.size(); } -uint8_t* RawImage::raw() const { - return (uint8_t*)m_pixels.data(); +U8* RawImage::raw() const { + return (U8*)m_pixels.data(); } -uint32_t RawImage::width() const { +U32 RawImage::width() const { return m_width; } -uint32_t RawImage::height() const { +U32 RawImage::height() const { return m_height; } std::string RawImage::string() const { std::stringstream str{}; - bool comma = false; + Bool comma = false; str << "["; for (const auto [r, g, b, a] : m_pixels) { if (comma) { str << ", "; } - str << "{r=" << (uint)r << ", g=" << (uint)g << ", b=" << (uint)b << ", a=" << (uint)a << "}"; + str << "{r=" << (UInt)r << ", g=" << (UInt)g << ", b=" << (UInt)b << ", a=" << (UInt)a << "}"; comma = true; } str << "]"; diff --git a/src/GFX/Image/RawImage.hpp b/src/GFX/Image/RawImage.hpp index 809ddd9..5a95dad 100644 --- a/src/GFX/Image/RawImage.hpp +++ b/src/GFX/Image/RawImage.hpp @@ -1,7 +1,6 @@ #pragma once -#include <cstdint> -#include <cstddef> +#include "../../Common/Sizes.hpp" #include <string> #include <vector> @@ -11,26 +10,26 @@ class RawImage { public: RawImage() : m_width(0), m_height(0) {} - RawImage(uint32_t width, uint32_t height) : m_width(width), m_height(height) { + RawImage(U32 width, U32 height) : m_width(width), m_height(height) { m_pixels.reserve(width * height); } struct Pixel { - uint8_t r, g, b, a; + U8 r, g, b, a; }; void add(Pixel pixel); - size_t size() const; - uint8_t* raw() const; + USize size() const; + U8* raw() const; - uint32_t width() const; - uint32_t height() const; + U32 width() const; + U32 height() const; std::string string() const; private: std::vector<Pixel> m_pixels; - uint32_t m_width, m_height; + U32 m_width, m_height; }; } |
