huge abr refactoring

This commit is contained in:
2019-03-07 18:22:34 +01:00
parent 1b56dff7d0
commit 8a581ed59e
2 changed files with 319 additions and 255 deletions

View File

@@ -2,6 +2,27 @@
#include "abr.h"
#include "log.h"
SerializedStream::Type::Ref SerializedStream::instanciate(const std::string& key)
{
if (m_ctor_table.find(key) != m_ctor_table.end())
return m_ctor_table[key]();
return nullptr;
}
std::map<std::string /*key*/, std::function<SerializedStream::Type::Ref()>> SerializedStream::m_ctor_table =
{
{ "VlLs", []{ return std::make_shared<SerializedStream::List>(); } },
{ "TEXT", []{ return std::make_shared<SerializedStream::String>(); } },
{ "Objc", []{ return std::make_shared<SerializedStream::Descriptor>(); } },
{ "UntF", []{ return std::make_shared<SerializedStream::UnitFloat>(); } },
{ "bool", []{ return std::make_shared<SerializedStream::Boolean>(); } },
{ "long", []{ return std::make_shared<SerializedStream::Integer>(); } },
{ "doub", []{ return std::make_shared<SerializedStream::Double>(); } },
{ "enum", []{ return std::make_shared<SerializedStream::Enum>(); } },
{ "tdta", []{ return std::make_shared<SerializedStream::RawData>(); } },
};
bool ABR::section_desc()
{
auto sz = align4(ru32());
@@ -13,8 +34,9 @@ bool ABR::section_desc()
// presets list
auto name = rkey_or_string(); // "Brsh"
auto list = rstring(4); // "VlLs"
if (auto presets = std::dynamic_pointer_cast<List>(call(list)))
if (auto presets = std::dynamic_pointer_cast<List>(instanciate(list)))
{
presets->read(*this);
for (auto const& pr : presets->items)
{
if (auto desc = std::dynamic_pointer_cast<Descriptor>(pr))
@@ -42,8 +64,8 @@ bool ABR::section_samp()
auto uid = rpascal();
//printf("sample brush %s\n", uid.c_str());
skip(4); // unknown bytes usually 00 01 00 00
auto vm = parse_vmem();
if (vm)
auto vm = std::make_shared<VMArray>();
if (vm->read(*this))
{
if (auto img = vm->image(true, true))
{
@@ -74,7 +96,8 @@ bool ABR::section_patt()
snap();
continue;
}
auto point = rpoint();
Point point;
point.read(*this);
auto name = rwstring();
auto uid = rpascal();
@@ -83,8 +106,8 @@ bool ABR::section_patt()
// read(...);
// no worries indexed is skipped anyway
auto vm = parse_vmem();
if (vm)
auto vm = std::make_shared<VMArray>();
if (vm->read(*this))
{
int nc = std::min((int)vm->channels.size(), 3);
if (nc != image_mode)
@@ -349,184 +372,6 @@ std::vector<std::shared_ptr<Brush>> ABR::compute_brushes(const std::string& path
return ret;
}
std::shared_ptr<SerializedStreamReader::VMArray> SerializedStreamReader::parse_vmem()
{
// Virtual Memory Array List
auto vmem_version = ru32(); // = 3
assert(vmem_version == 3);
auto vmem_length = ru32();
// TODO: check if at the end there's good data
// check if the bounds are within the parent's size
auto vmem_rect = rrect();
auto vmem_channels = ru32();
// The following is a virtual memory array,
// repeated for the number of channels
// + one for a user mask + one for a sheet mask.
vmem_channels += 2; // user and sheet mask
auto ret = std::make_shared<VMArray>(vmem_version, vmem_rect);
for (int ch = 0; ch < vmem_channels; ch++)
{
auto array_written = ru32(); // skip if 0
if (array_written == 0)
continue;
auto length = ru32(); // skip if 0, length is from the next field to the end
if (length == 0)
continue;
auto depth = ru32(); // Pixel depth: 1, 8, 16 or 32
auto rect = rrect();
auto depth2 = ru16(); // again?
auto compression = ru8(); // 1 = zip
if (depth != 8)
{
LOG("unsupported depth %d bits\n", depth);
skip(length - 23);
continue;
}
if (compression == 0)
{
int data_size = (depth >> 3) * rect.area();
ret->channels.emplace_back(depth, rect, compression, rraw(data_size));
}
else if (compression == 1)
{
auto start = pos();
auto height = rect.height();
// contain the compressed length of each scanline
std::vector<uint16_t> scanlines;
scanlines.reserve(height);
for (int i = 0; i < height; i++)
scanlines.push_back(ru16());
std::vector<uint8_t> raw;
for (auto sl : scanlines)
{
auto decoded = rrle(sl);
raw.insert(raw.end(), decoded.begin(), decoded.end());
}
ret->channels.emplace_back(depth, rect, compression, raw);
auto len = pos() - start;
}
else
{
LOG("unsupported compression mode %d\n", compression);
skip(length - 23);
continue;
}
}
snap();
return ret;
}
std::shared_ptr<SerializedStreamReader::List> SerializedStreamReader::parse_vlls()
{
auto ret = std::make_shared<List>();
auto count = ru32();
//printf("list: %d\n", count);
for (int i = 0; i < count; i++)
{
auto type = rstring(4);
auto item = call(type);
if (!item)
return nullptr;
ret->items.push_back(item);
}
return ret;
}
std::shared_ptr<SerializedStreamReader::String> SerializedStreamReader::parse_text()
{
auto ret = std::make_shared<String>();
ret->value = rwstring();
//wprintf(L"text: %s\n", ret->value.c_str());
return ret;
}
std::shared_ptr<SerializedStreamReader::Descriptor> SerializedStreamReader::parse_objc()
{
auto ret = std::make_shared<Descriptor>();
ret->name = rwstring();
ret->class_id = rkey_or_string();
auto count = ru32();
//printf("objc type %s, %d props\n", ret->class_id.c_str(), count);
for (int i = 0; i < count; i++)
{
auto key = rkey_or_string();
auto type = rstring(4);
//printf("prop %s\n", t.c_str());
auto property = call(type);
if (!property)
return nullptr;
if (ret->props.find(key) != ret->props.end())
LOG("DUPLICATE prop %s\n", key.c_str());
ret->props[key] = property;
}
return ret;
}
std::shared_ptr<SerializedStreamReader::UnitFloat> SerializedStreamReader::parse_untf()
{
auto ret = std::make_shared<UnitFloat>();
ret->unit = rstring(4);
ret->value = rdbl();
//printf("float %s: %f\n", ret->unit.c_str(), ret->value);
return ret;
}
std::shared_ptr<SerializedStreamReader::Boolean> SerializedStreamReader::parse_bool()
{
auto ret = std::make_shared<Boolean>();
ret->value = ru8();
//printf("bool: %s\n", ret->value ? "true" : "false");
return ret;
}
std::shared_ptr<SerializedStreamReader::Integer> SerializedStreamReader::parse_long()
{
auto ret = std::make_shared<Integer>();
ret->value = ru32();
//printf("long: %d\n", ret->value);
return ret;
}
std::shared_ptr<SerializedStreamReader::Double> SerializedStreamReader::parse_doub()
{
auto ret = std::make_shared<Double>();
ret->value = rdbl();
//printf("double: %d\n", ret->value);
return ret;
}
std::shared_ptr<SerializedStreamReader::Enum> SerializedStreamReader::parse_enum()
{
auto ret = std::make_shared<Enum>();
ret->type = rkey_or_string();
ret->value = rkey_or_string();
//printf("enum: %s %s\n", t.c_str(), e.c_str());
return ret;
}
std::shared_ptr<SerializedStreamReader::RawData> SerializedStreamReader::parse_tdta()
{
auto ret = std::make_shared<RawData>();
ret->data = rraw();
return ret;
}
SerializedStreamReader::SerializedStreamReader()
{
m_parser_table = std::map<std::string, std::function<SerializedStreamReader::Type::Ref()>>
{
{ "VlLs", std::bind(&SerializedStreamReader::parse_vlls, this) },
{ "TEXT", std::bind(&SerializedStreamReader::parse_text, this) },
{ "Objc", std::bind(&SerializedStreamReader::parse_objc, this) },
{ "UntF", std::bind(&SerializedStreamReader::parse_untf, this) },
{ "bool", std::bind(&SerializedStreamReader::parse_bool, this) },
{ "long", std::bind(&SerializedStreamReader::parse_long, this) },
{ "doub", std::bind(&SerializedStreamReader::parse_doub, this) },
{ "enum", std::bind(&SerializedStreamReader::parse_enum, this) },
{ "tdta", std::bind(&SerializedStreamReader::parse_tdta, this) },
};
}
bool ABR::open(const std::string& path)
{
Asset asset;