diff options
| -rw-r--r-- | src/PNGImage.cpp | 4 | ||||
| -rw-r--r-- | src/PNGImage.h | 8 | ||||
| -rw-r--r-- | src/reader.cpp | 54 | ||||
| -rw-r--r-- | src/reader.h | 19 |
4 files changed, 62 insertions, 23 deletions
diff --git a/src/PNGImage.cpp b/src/PNGImage.cpp index 9ba7404..9ab0751 100644 --- a/src/PNGImage.cpp +++ b/src/PNGImage.cpp @@ -1,4 +1,5 @@ #include "PNGImage.h" +#include "reader.h" #include "zlib.h" #include "debug.h" @@ -9,6 +10,7 @@ #include <cstdio> #include <cstdlib> #include <cstring> +#include <endian.h> #include <iostream> #include <memory> #include <stdexcept> @@ -49,7 +51,7 @@ namespace TehImage int PNGImage::readFromFile(std::string filename) { - std::unique_ptr<Reader> readerMem(new Reader(filename)); + std::unique_ptr<Reader> readerMem(new Reader(filename, FileEndianness::BIG)); reader = readerMem.get(); char signature[8]; diff --git a/src/PNGImage.h b/src/PNGImage.h index 3c1bf1f..a5d8f3a 100644 --- a/src/PNGImage.h +++ b/src/PNGImage.h @@ -19,10 +19,6 @@ namespace TehImage class PNGImage : public Image { - private: - ZLibInflator zlib; - uint8_t* idatData; - unsigned long idatDataSize; public: PNGImage(); ~PNGImage(); @@ -64,6 +60,10 @@ namespace TehImage CHUNK_READER(IDAT); CHUNK_READER(IEND); + ZLibInflator zlib; + uint8_t* idatData; + unsigned long idatDataSize; + bool end = false; Reader *reader; 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<uint8_t>() { return readByte(); @@ -44,33 +63,36 @@ namespace TehImage template<> uint16_t Reader::readData<uint16_t>() { 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>() { 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>() { 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; } diff --git a/src/reader.h b/src/reader.h index a4f913c..bb181e6 100644 --- a/src/reader.h +++ b/src/reader.h @@ -1,6 +1,7 @@ #pragma once #include <cstddef> +#include <cstdint> #include <cstdio> #include <string> @@ -8,12 +9,18 @@ namespace TehImage { - + enum FileEndianness + { + NO_CONVERT, + LITTLE, + BIG + }; + class Reader { public: //Bytes are big endian - Reader(std::string file); + Reader(std::string file, FileEndianness fileEndianness); ~Reader(); template <typename T> @@ -31,8 +38,16 @@ namespace TehImage size_t pos; FILE* file; bool ready = false; + FileEndianness fileEndianness; void refreshBuffer(); + void convertEndian(uint8_t* out, size_t bytes); }; + // template <typename T> + // T Reader::readData() + // { + // T num = 0; + // convertEndian((uint8_t*)&num, sizeof(T)); + // } } |
