From 93a78ac64327b53f53952b625c7ce8a11bcc8651 Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 18 Mar 2026 16:28:27 +1300 Subject: feat: Added endianness option to reader The file reader class can now support either endianness to read in, and will convert to the native endianness --- src/reader.cpp | 54 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 16 deletions(-) (limited to 'src/reader.cpp') diff --git a/src/reader.cpp b/src/reader.cpp index f8b053f..f0b19ae 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -11,11 +11,12 @@ namespace TehImage { - Reader::Reader(std::string filename) + Reader::Reader(std::string filename, FileEndianness fileEndianness) { file = fopen(filename.c_str(), "rb"); refreshBuffer(); ready = true; + this->fileEndianness = fileEndianness; } Reader::~Reader() { @@ -36,6 +37,24 @@ namespace TehImage pos = 0; } + void Reader::convertEndian(uint8_t* out, size_t bytes) + { + constexpr bool LE = std::endian::native == std::endian::little; + constexpr bool BE = !LE; + if(fileEndianness == NO_CONVERT || (fileEndianness == BIG && BE) || (fileEndianness == LITTLE && LE)) + { + for(int i = 0; i < bytes; i++) + { + out[i] = readByte(); + } + return; + } + for(int i = 0; i < bytes; i++) + { + out[bytes - 1 - i] = readByte(); + } + } + template<> uint8_t Reader::readData() { return readByte(); @@ -44,33 +63,36 @@ namespace TehImage template<> uint16_t Reader::readData() { uint16_t num = 0; - for(int i = 0; i < 2; i++) - { - num += readByte() << (8 * (1-i)); - } + convertEndian((uint8_t*)&num, 2); + // for(int i = 0; i < 2; i++) + // { + // num += readByte() << (8 * (1-i)); + // } return num; } template<> uint32_t Reader::readData() { uint32_t num = 0; - for(int i = 0; i < 4; i++) - { - uint8_t byte = readByte(); - debug(std::cout << std::hex << 0+byte << " "); - num += byte << (8 * (3-i)); - } - debug(std::cout << std::dec << std::endl); + convertEndian((uint8_t*)&num, 4); + // for(int i = 0; i < 4; i++) + // { + // // uint8_t byte = readByte(); + // // debug(std::cout << std::hex << 0+byte << " "); + // num += ((uint8_t) readByte()) << (8 * (3-i)); + // } + // // debug(std::cout << std::dec << std::endl); return num; } template<> uint64_t Reader::readData() { uint64_t num = 0; - for(int i = 0; i < 8; i++) - { - num += readByte() << (8 * (7-i)); - } + convertEndian((uint8_t*)&num, 8); + // for(int i = 0; i < 8; i++) + // { + // num += readByte() << (8 * (7-i)); + // } return num; } -- cgit v1.2.3