WIP serializer
This commit is contained in:
250
src/abr.h
250
src/abr.h
@@ -20,9 +20,61 @@ public:
|
||||
|
||||
return bint.c[0] == 1 ? ByteOrder::BigEndian : ByteOrder::LittleEndian;
|
||||
}
|
||||
BinaryStream(const BinaryStream&) = delete;
|
||||
BinaryStream() = default;
|
||||
~BinaryStream()
|
||||
template<typename T> T swap(T x)
|
||||
{
|
||||
#if _MSC_VER >= 1400
|
||||
|
||||
if (sizeof(T) == 2)
|
||||
{
|
||||
auto y = _byteswap_ushort(*reinterpret_cast<uint16_t*>(&x));
|
||||
return *reinterpret_cast<T*>(&y);
|
||||
}
|
||||
else if (sizeof(T) == 4)
|
||||
{
|
||||
auto y = _byteswap_ulong(*reinterpret_cast<uint32_t*>(&x));
|
||||
return *reinterpret_cast<T*>(&y);
|
||||
}
|
||||
else if (sizeof(T) == 8)
|
||||
{
|
||||
auto y = _byteswap_uint64(*reinterpret_cast<uint64_t*>(&x));
|
||||
return *reinterpret_cast<T*>(&y);
|
||||
}
|
||||
#else
|
||||
auto p = reinterpret_cast<uint8_t*>(&x);
|
||||
if (sizeof(T) == 2)
|
||||
{
|
||||
std::swap(p[0], p[1]);
|
||||
}
|
||||
else if (sizeof(T) == 4)
|
||||
{
|
||||
std::swap(p[0], p[3]);
|
||||
std::swap(p[1], p[2]);
|
||||
}
|
||||
else if (sizeof(T) == 8)
|
||||
{
|
||||
std::swap(p[0], p[7]);
|
||||
std::swap(p[1], p[6]);
|
||||
std::swap(p[2], p[5]);
|
||||
std::swap(p[3], p[4]);
|
||||
}
|
||||
else
|
||||
{
|
||||
static_assert(true, "Should not reach here");
|
||||
}
|
||||
return x;
|
||||
#endif
|
||||
}
|
||||
protected:
|
||||
bool m_swap = false;
|
||||
ByteOrder m_byte_order = ByteOrder::Host;
|
||||
};
|
||||
|
||||
class BinaryStreamReader : public BinaryStream
|
||||
{
|
||||
public:
|
||||
BinaryStreamReader(const BinaryStreamReader&) = delete;
|
||||
BinaryStreamReader() = default;
|
||||
~BinaryStreamReader()
|
||||
{
|
||||
m_ptr = m_cur = nullptr;
|
||||
m_size = 0;
|
||||
@@ -39,7 +91,7 @@ public:
|
||||
// snap to the next 4-alignment
|
||||
void snap() { if ((size_t)m_cur % 4 != 0) m_cur += 4 - (size_t)m_cur % 4; }
|
||||
bool eof() { return std::distance(m_ptr, m_cur) >= m_size; }
|
||||
bool has_data(size_t sz) { return std::distance(m_ptr, m_cur + sz) < m_size; }
|
||||
bool has_data(size_t sz) { return std::distance(m_ptr, m_cur + sz) <= m_size; }
|
||||
uint8_t ru8() { return read<uint8_t>(); }
|
||||
uint16_t ru16() { return m_swap ? swap(read<uint16_t>()) : read<uint16_t>(); }
|
||||
uint32_t ru32() { return m_swap ? swap(read<uint32_t>()) : read<uint32_t>(); }
|
||||
@@ -102,9 +154,6 @@ public:
|
||||
{
|
||||
n = ri8();
|
||||
j++;
|
||||
// force sign
|
||||
if (n >= 128)
|
||||
n -= 256; // can this even happen?
|
||||
// copy the following char -n + 1 times
|
||||
if (n < 0)
|
||||
{
|
||||
@@ -151,60 +200,115 @@ protected:
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template<typename T> T swap(T x)
|
||||
{
|
||||
#if _MSC_VER >= 1400
|
||||
|
||||
if (sizeof(T) == 2)
|
||||
{
|
||||
auto y = _byteswap_ushort(*reinterpret_cast<uint16_t*>(&x));
|
||||
return *reinterpret_cast<T*>(&y);
|
||||
}
|
||||
else if (sizeof(T) == 4)
|
||||
{
|
||||
auto y = _byteswap_ulong(*reinterpret_cast<uint32_t*>(&x));
|
||||
return *reinterpret_cast<T*>(&y);
|
||||
}
|
||||
else if (sizeof(T) == 8)
|
||||
{
|
||||
auto y = _byteswap_uint64(*reinterpret_cast<uint64_t*>(&x));
|
||||
return *reinterpret_cast<T*>(&y);
|
||||
}
|
||||
#else
|
||||
auto p = reinterpret_cast<uint8_t*>(&x);
|
||||
if (sizeof(T) == 2)
|
||||
{
|
||||
std::swap(p[0], p[1]);
|
||||
}
|
||||
else if (sizeof(T) == 4)
|
||||
{
|
||||
std::swap(p[0], p[3]);
|
||||
std::swap(p[1], p[2]);
|
||||
}
|
||||
else if (sizeof(T) == 8)
|
||||
{
|
||||
std::swap(p[0], p[7]);
|
||||
std::swap(p[1], p[6]);
|
||||
std::swap(p[2], p[5]);
|
||||
std::swap(p[3], p[4]);
|
||||
}
|
||||
else
|
||||
{
|
||||
static_assert(true, "Should not reach here");
|
||||
}
|
||||
return x;
|
||||
#endif
|
||||
}
|
||||
private:
|
||||
uint8_t *m_ptr = nullptr;
|
||||
uint8_t *m_cur = nullptr;
|
||||
size_t m_size = 0;
|
||||
bool m_swap = false;
|
||||
ByteOrder m_byte_order = ByteOrder::Host;
|
||||
};
|
||||
|
||||
class ABR : private BinaryStream
|
||||
class BinaryStreamWriter : public BinaryStream
|
||||
{
|
||||
public:
|
||||
std::vector<uint8_t> m_data;
|
||||
BinaryStreamWriter(const BinaryStreamWriter&) = delete;
|
||||
BinaryStreamWriter() = default;
|
||||
~BinaryStreamWriter() = default;
|
||||
void init(ByteOrder byte_order = ByteOrder::Host)
|
||||
{
|
||||
m_byte_order = byte_order;
|
||||
m_swap = byte_order == ByteOrder::Host ? false : byte_order != sys_order();
|
||||
}
|
||||
//size_t pos() { return std::distance(m_ptr, m_cur); }
|
||||
void skip(size_t bytes, uint8_t fill = 0) { m_data.resize(m_data.size() + bytes); }
|
||||
// snap to the next 4-alignment
|
||||
void snap() { if ((size_t)m_data.size() % 4 != 0) m_data.resize(m_data.size() + 4 - (size_t)m_data.size() % 4); }
|
||||
//bool eof() { return std::distance(m_ptr, m_cur) >= m_size; }
|
||||
//bool has_data(size_t sz) { return std::distance(m_ptr, m_cur + sz) < m_size; }
|
||||
void wu8 (uint8_t v ) { return write<uint8_t>(v); }
|
||||
void wu16(uint16_t v) { return m_swap ? write(swap(v)) : write(v); }
|
||||
void wu32(uint32_t v) { return m_swap ? write(swap(v)) : write(v); }
|
||||
void wu64(uint64_t v) { return m_swap ? write(swap(v)) : write(v); }
|
||||
void wi8 (int8_t v ) { return write<int8_t>(v); }
|
||||
void wi16(int16_t v ) { return m_swap ? write(swap(v)) : write(v); }
|
||||
void wi32(int32_t v ) { return m_swap ? write(swap(v)) : write(v); }
|
||||
void wi64(int64_t v ) { return m_swap ? write(swap(v)) : write(v); }
|
||||
void wflt(float v ) { return m_swap ? write(swap(v)) : write(v); }
|
||||
void wdbl(double v ) { return m_swap ? write(swap(v)) : write(v); }
|
||||
//std::string pick(size_t chars) { return { (char*)m_cur, chars }; }
|
||||
void wstring(std::string s)
|
||||
{
|
||||
wu32(s.size());
|
||||
write(s.data(), s.size());
|
||||
}
|
||||
void wwstring(std::wstring s)
|
||||
{
|
||||
wu32(s.size());
|
||||
std::wstring_convert<std::codecvt_utf16<wchar_t, 0x10ffff>> converter;
|
||||
wstring(converter.to_bytes(s));
|
||||
}
|
||||
void wpascal(std::string s)
|
||||
{
|
||||
wu8(s.size());
|
||||
write(s.data(), s.size());
|
||||
}
|
||||
void wraw(std::vector<uint8_t> raw)
|
||||
{
|
||||
wu32(raw.size());
|
||||
write(raw.data(), raw.size());
|
||||
}
|
||||
void wrle(std::vector<uint8_t> data)
|
||||
{
|
||||
int idx = 0;
|
||||
auto ptr = data.data();
|
||||
|
||||
// find the next sequence and return the position
|
||||
auto next_seq = [&] () -> std::pair<size_t, size_t> {
|
||||
size_t i = idx;
|
||||
while (i < data.size() - 1)
|
||||
{
|
||||
size_t count = 0;
|
||||
for (size_t j = i + 1; j < data.size() && data[i] == data[j]; j++)
|
||||
count++;
|
||||
if (count > 0)
|
||||
return { i, count + 1 };
|
||||
i++;
|
||||
}
|
||||
return { i + 1, 0 };
|
||||
};
|
||||
|
||||
while (idx < data.size())
|
||||
{
|
||||
auto seq = next_seq();
|
||||
// non sequence chars
|
||||
if (seq.first - idx > 0)
|
||||
{
|
||||
wi8(seq.first - idx - 1);
|
||||
write(ptr + idx, seq.first - idx);
|
||||
}
|
||||
if (seq.second > 0)
|
||||
{
|
||||
wi8(-(int8_t)(seq.second - 1));
|
||||
write(ptr[seq.first]);
|
||||
}
|
||||
idx = seq.first + seq.second;
|
||||
}
|
||||
}
|
||||
protected:
|
||||
template<typename T> void write(T x)
|
||||
{
|
||||
uint8_t* bytes = reinterpret_cast<uint8_t*>(&x);
|
||||
m_data.insert(m_data.end(), bytes, bytes + sizeof(T));
|
||||
}
|
||||
template<typename T> void write(const T* ptr, size_t size)
|
||||
{
|
||||
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(ptr);
|
||||
m_data.insert(m_data.end(), bytes, bytes + size * sizeof(T));
|
||||
}
|
||||
};
|
||||
|
||||
class SerializedStream
|
||||
{
|
||||
public:
|
||||
struct Type
|
||||
{
|
||||
using Vec = std::vector<std::shared_ptr<Type>>;
|
||||
@@ -410,11 +514,29 @@ class ABR : private BinaryStream
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Error image with %d channels\n", channels.size());
|
||||
LOG("Error image with %d channels\n", channels.size());
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
class SerializedStreamReader : public SerializedStream, public BinaryStreamReader
|
||||
{
|
||||
public:
|
||||
std::shared_ptr<VMArray> parse_vmem(); // Parse Virtual Memory Array List
|
||||
std::shared_ptr<List> parse_vlls();
|
||||
std::shared_ptr<String> parse_text();
|
||||
std::shared_ptr<Descriptor> parse_objc();
|
||||
std::shared_ptr<UnitFloat> parse_untf();
|
||||
std::shared_ptr<Boolean> parse_bool();
|
||||
std::shared_ptr<Integer> parse_long();
|
||||
std::shared_ptr<Double> parse_doub();
|
||||
std::shared_ptr<Enum> parse_enum();
|
||||
std::shared_ptr<RawData> parse_tdta();
|
||||
|
||||
std::map<std::string /*key*/, std::function<Type::Ref()>> m_parser_table;
|
||||
SerializedStreamReader();
|
||||
|
||||
std::string rkey_or_string()
|
||||
{
|
||||
@@ -449,30 +571,18 @@ class ABR : private BinaryStream
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
class ABR : private SerializedStreamReader
|
||||
{
|
||||
bool section_desc();
|
||||
bool section_samp();
|
||||
bool section_patt();
|
||||
|
||||
std::shared_ptr<VMArray> parse_vmem(); // Parse Virtual Memory Array List
|
||||
std::shared_ptr<List> parse_vlls();
|
||||
std::shared_ptr<String> parse_text();
|
||||
std::shared_ptr<Descriptor> parse_objc();
|
||||
std::shared_ptr<UnitFloat> parse_untf();
|
||||
std::shared_ptr<Boolean> parse_bool();
|
||||
std::shared_ptr<Integer> parse_long();
|
||||
std::shared_ptr<Double> parse_doub();
|
||||
std::shared_ptr<Enum> parse_enum();
|
||||
std::shared_ptr<RawData> parse_tdta();
|
||||
|
||||
std::map<std::string /*key*/, std::function<Type::Ref()>> m_parser_table;
|
||||
|
||||
public:
|
||||
std::vector<std::shared_ptr<Descriptor>> m_presets;
|
||||
std::map<std::string /*uid*/, std::shared_ptr<Image>> m_patterns;
|
||||
std::map<std::string /*uid*/, std::shared_ptr<Image>> m_samples;
|
||||
|
||||
ABR();
|
||||
bool open(const std::string& path);
|
||||
std::vector<std::shared_ptr<Brush>> compute_brushes(const std::string& path);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user