Files
panopainter/src/abr.cpp
2019-02-08 01:51:04 +01:00

258 lines
7.0 KiB
C++

#include "pch.h"
#include "abr.h"
bool ABR::section_desc()
{
auto sz = align4(ru32());
auto n1 = ri32(); // some integer
auto s = rwstring(); // maybe a string
auto null = rkey_or_string();
auto null_val = ri32(); // integer following the null
auto name = rkey_or_string();
auto list = rstring(4);
auto list_val = call(list);
auto out = list_val->str(0, "");
std::cout << out;
return true;
}
bool ABR::section_samp()
{
auto sz = align4(ru32());
skip(sz);
return true;
}
bool ABR::section_patt()
{
auto sz = ru32();
auto sz_aligned = align4(sz);
auto start = pos();
auto end = start + sz;
while (pos() < end)
{
auto length = ru32(); // length of this pattern
auto version = ru32(); // = 1
// Bitmap = 0; Grayscale = 1; Indexed = 2; RGB = 3;
// CMYK = 4; Multichannel = 7; Duotone = 8; Lab = 9
auto image_mode = ru32();
if (!(image_mode == 1 || image_mode == 3))
{
skip(length - 8);
snap();
continue;
}
auto point = rpoint();
auto name = rwstring();
auto uid = rpascal();
// Index color table (256 * 3 RGB values):
// only present when image mode is indexed color
// read(...);
// no worries indexed is skipped anyway
// Virtual Memory Array List
{
auto version = ru32(); // = 3
auto length = ru32();
auto rect = rrect();
auto 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.
channels += 2; // user and sheet mask
std::vector<glm::u8vec3> out(rect.area());
bool valid = false;
for (int ch = 0; ch < 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 (compression != 0 || depth != 8)
{
skip(length - 23);
continue;
}
int data_size = (depth >> 3) * rect.area();
auto raw = rraw(data_size);
if (image_mode == 3)
{
for (int i = 0; i < data_size; i++)
out[i][ch] = raw[i];
}
else
{
for (int i = 0; i < data_size; i++)
out[i] = glm::u8vec3(raw[i]);
}
valid = true;
}
if (valid)
stbi_write_png(fmt::format("{}.png", uid).c_str(), rect.width(), rect.height(), 3, out.data(), 0);
snap();
}
}
//skip(sz);
return true;
}
std::shared_ptr<ABR::List> ABR::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<ABR::String> ABR::parse_text()
{
auto ret = std::make_shared<String>();
ret->value = rwstring();
//wprintf(L"text: %s\n", ret->value.c_str());
return ret;
}
//ABR::Type::Ref ABR::parse_prop()
//{
// auto name = rstring(4);
// //printf("prop type %s\n", t.c_str());
// if (!call(name))
// return false;
//}
std::shared_ptr<ABR::Descriptor> ABR::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())
printf("DUPLICATE prop %d\n", key.c_str());
ret->props[key] = property;
}
return ret;
}
std::shared_ptr<ABR::UnitFloat> ABR::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<ABR::Boolean> ABR::parse_bool()
{
auto ret = std::make_shared<Boolean>();
ret->value = ru8();
//printf("bool: %s\n", ret->value ? "true" : "false");
return ret;
}
std::shared_ptr<ABR::Integer> ABR::parse_long()
{
auto ret = std::make_shared<Integer>();
ret->value = ru32();
//printf("long: %d\n", ret->value);
return ret;
}
std::shared_ptr<ABR::Double> ABR::parse_doub()
{
auto ret = std::make_shared<Double>();
ret->value = rdbl();
//printf("double: %d\n", ret->value);
return ret;
}
std::shared_ptr<ABR::Enum> ABR::parse_enum()
{
auto ret = std::make_shared<Enum>();
auto t = rkey_or_string();
auto e = rkey_or_string();
//printf("enum: %s %s\n", t.c_str(), e.c_str());
return ret;
}
std::shared_ptr<ABR::RawData> ABR::parse_tdta()
{
auto ret = std::make_shared<RawData>();
ret->data = rraw();
return ret;
}
ABR::ABR()
{
m_parser_table = std::map<std::string, std::function<ABR::Type::Ref()>>
{
{ "VlLs", std::bind(&ABR::parse_vlls, this) },
{ "TEXT", std::bind(&ABR::parse_text, this) },
{ "Objc", std::bind(&ABR::parse_objc, this) },
{ "UntF", std::bind(&ABR::parse_untf, this) },
{ "bool", std::bind(&ABR::parse_bool, this) },
{ "long", std::bind(&ABR::parse_long, this) },
{ "doub", std::bind(&ABR::parse_doub, this) },
{ "enum", std::bind(&ABR::parse_enum, this) },
{ "tdta", std::bind(&ABR::parse_tdta, this) },
};
}
bool ABR::open(const std::string& path)
{
m_parser_table[""] = std::bind(&ABR::parse_bool, this);
Asset asset;
if (asset.open(path.c_str()))
{
init(asset.read_all(), asset.m_len, BinaryStream::ByteOrder::BigEndian);
auto version_major = ru16();
auto version_minor = ru16();
while (!eof())
{
if (rstring(4) != "8BIM")
return false;
auto t = rstring(4);
if (t == "desc")
{
section_desc();
}
else if (t == "patt")
{
section_patt();
}
else
{
printf("skip section %s\n", t.c_str());
skip(align4(ru32()));
}
}
}
return false;
}