add ABR brushes and presets with properties and patterns

This commit is contained in:
2019-02-10 16:52:37 +01:00
parent e629a2a7b5
commit 1d6c26f2ba
12 changed files with 379 additions and 124 deletions

140
src/abr.h
View File

@@ -3,6 +3,8 @@
#include <codecvt>
#include <fmt/core.h>
#include "util.h"
#include "image.h"
#include "brush.h"
class BinaryStream
{
@@ -66,7 +68,11 @@ public:
auto ptr = advance<char>(len * 2);
for (int i = 0; i < len; i++)
std::swap(ptr[i * 2], ptr[i * 2 + 1]);
return std::wstring((wchar_t*)ptr, len);
// right trim trailing zeroes
auto wptr = (wchar_t*)ptr;
for (int i = len - 1; i >= 0; i--)
if (wptr[i] == 0) len--;
return std::wstring(wptr, len);
}
std::string rpascal()
{
@@ -194,9 +200,9 @@ private:
ByteOrder m_byte_order = ByteOrder::Host;
};
class ABR : public BinaryStream
class ABR : private BinaryStream
{
struct Type
struct Type
{
using Vec = std::vector<std::shared_ptr<Type>>;
using Map = std::map<std::string, std::shared_ptr<Type>>;
@@ -222,19 +228,22 @@ class ABR : public BinaryStream
};
struct Double : public Type
{
using native_type = double;
double value;
virtual std::string str(int indent, const std::string& prefix) const override
{ return std::string(indent, '-') + prefix + fmt::format("double: {}", value); }
};
struct UnitFloat : public Type
{
using native_type = double;
std::string unit;
double value;
virtual std::string str(int indent, const std::string& prefix) const override
{ return std::string(indent, '-') + prefix + fmt::format("float: {} ({})", value, unit); }
};
struct String : public Type
{
{
using native_type = std::wstring;
std::wstring value;
virtual std::string str(int indent, const std::string& prefix) const override
{ return std::string(indent, '-') + prefix + fmt::format("string: {}", wstr2str(value)); }
@@ -253,13 +262,15 @@ class ABR : public BinaryStream
struct Name : public Type { };
struct Integer : public Type
{
int32_t value;
using native_type = int32_t;
int32_t value;
virtual std::string str(int indent, const std::string& prefix) const override
{ return std::string(indent, '-') + prefix + fmt::format("int: {}", value); }
};
struct LargeInteger : public Type { };
struct Boolean : public Type
{
{
using native_type = bool;
bool value;
virtual std::string str(int indent, const std::string& prefix) const override
{ return std::string(indent, '-') + prefix + fmt::format("bool: {}", value); }
@@ -278,12 +289,37 @@ class ABR : public BinaryStream
Type::Map props;
virtual std::string str(int indent, const std::string& prefix) const override
{
auto ret = std::string(indent, '-') +
auto ret = std::string(indent, '-') + prefix +
fmt::format("objc {} ({}): {} props:", wstr2str(name), class_id, props.size());
for (const auto& p : props)
ret += "\n" + p.second->str(indent + 1, fmt::format("'{}' ", p.first));
return ret;
}
bool has(const std::string& key) const
{
return props.find(key) != props.end();
}
template<typename T> std::shared_ptr<T> get(const std::string& key) const
{
return has(key) ? std::dynamic_pointer_cast<T>(props.at(key)) : nullptr;
}
template<typename T> auto value(const std::string& key) const
{
if (auto v = get<T>(key))
return v->value;
return decltype(T::value){};
}
template<typename T, typename D> auto value_or(const std::string& key, const D val) const
{
if (auto v = get<T>(key))
return v->value;
return val;
}
template<typename T, typename D> void value(const std::string& key, D& dest) const
{
if (auto v = get<T>(key))
dest = static_cast<D>(v->value);
}
};
struct Rectangle : public Type
{
@@ -314,20 +350,53 @@ class ABR : public BinaryStream
Channel(uint32_t depth, const Rectangle& rect, uint8_t compression, std::vector<uint8_t>& data) :
depth(depth), rect(rect), compression(compression), data(std::move(data)) { }
};
struct Image : public Type
struct VMArray : public Type
{
uint32_t version; // = 3
Rectangle rect;
std::vector<Channel> channels;
Image() = default;
Image(uint32_t version, const Rectangle& rect) :
version(version), rect(rect) { }
VMArray() = default;
VMArray(uint32_t version, const Rectangle& rect) : version(version), rect(rect) { }
std::shared_ptr<Image> image(bool grayscale) const
{
int nc = channels.size();
auto pixels = (channels[0].depth >> 3) * rect.area();
if (nc == 1 || nc >= 3)
{
auto img = std::make_shared<Image>();
img->comp = 4;
img->width = rect.width();
img->height = rect.height();
img->m_data = std::make_unique<uint8_t[]>(pixels * 4);
auto out = reinterpret_cast<glm::u8vec4*>(img->m_data.get());
if (grayscale)
{
auto const& raw = channels[0].data;
for (int i = 0; i < raw.size(); i++)
out[i] = glm::u8vec4(glm::u8vec3(255 - raw[i]), 255);
}
else
{
std::fill_n(out, pixels, glm::u8vec4(255));
for (int ch = 0; ch < std::min(nc, 3); ch++)
{
auto const& raw = channels[ch].data;
for (int i = 0; i < raw.size(); i++)
out[i][ch] = 255 - raw[i];
}
}
return img;
//stbi_write_png(fmt::format("x64/out/{}.png", uid).c_str(),
// image->rect.width(), image->rect.height(), 4, out.data(), 0);
}
else
{
printf("Error image with %d channels\n", channels.size());
}
return nullptr;
}
};
public:
ABR();
bool open(const std::string& path);
private:
std::string rkey_or_string()
{
auto len = ru32();
@@ -351,11 +420,22 @@ private:
ret.y = ru16();
return ret;
}
Type::Ref call(std::string t)
{
if (m_parser_table.find(t) != m_parser_table.end())
{
auto& method = m_parser_table[t];
return method();
}
return nullptr;
}
bool section_desc();
bool section_samp();
bool section_patt();
// Parse Virtual Memory Array List
std::shared_ptr<Image> parse_vmem();
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();
@@ -365,20 +445,16 @@ private:
std::shared_ptr<Double> parse_doub();
std::shared_ptr<Enum> parse_enum();
std::shared_ptr<RawData> parse_tdta();
Type::Ref call(std::string t)
{
if (m_parser_table.find(t) != m_parser_table.end())
{
auto& method = m_parser_table[t];
return method();
}
//else if (m_parser_table.find(pick(4)) != m_parser_table.end())
//{
// auto& method = m_parser_table[rstring(4)];
// return method();
//}
return nullptr;
}
std::map<std::string, std::function<Type::Ref()>> m_parser_table;
std::map<std::string /*key*/, std::function<Type::Ref()>> m_parser_table;
std::vector<std::shared_ptr<Descriptor>> m_presets;
public:
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);
};