aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDylan <boss@tehbox.org>2026-03-18 16:28:27 +1300
committerDylan <boss@tehbox.org>2026-03-18 16:28:27 +1300
commit93a78ac64327b53f53952b625c7ce8a11bcc8651 (patch)
treedf2bcea7256df8acdab5cb51edb0b59291b5b89c /src
parent8ca054ab8cf04e514df1570617b6a32ed83b9ea4 (diff)
downloadtehimage-master.tar.gz
tehimage-master.zip
feat: Added endianness option to readerHEADmaster
The file reader class can now support either endianness to read in, and will convert to the native endianness
Diffstat (limited to 'src')
-rw-r--r--src/PNGImage.cpp4
-rw-r--r--src/PNGImage.h8
-rw-r--r--src/reader.cpp54
-rw-r--r--src/reader.h19
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));
+ // }
}