parse patterns section

This commit is contained in:
2019-02-08 01:51:04 +01:00
parent fcc66ab9b5
commit a1e4f0b536
2 changed files with 156 additions and 64 deletions

View File

@@ -1,40 +1,6 @@
#include "pch.h"
#include "abr.h"
//std::map<std::string, std::function<ABR::Type::Ref()>> ABR::m_parser_table;
//{
// { "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) },
// //{ "Dmtr", &ABR::parse_prop },
// //{ "Hrdn", &ABR::parse_prop },
// //{ "Angl", &ABR::parse_prop },
// //{ "Rndn", &ABR::parse_prop },
// //{ "Spcn", &ABR::parse_prop },
// //{ "Intr", &ABR::parse_prop },
// //{ "flipX", &ABR::parse_prop },
// //{ "flipY", &ABR::parse_prop },
// //{ "useTipDynamics", &ABR::parse_prop },
// //{ "useScatter", &ABR::parse_prop },
// //{ "dualBrush", &ABR::parse_prop },
// //{ "useDualBrush", &ABR::parse_prop },
// //{ "brushGroup", &ABR::parse_prop },
// //{ "useBrushGroup", &ABR::parse_prop },
// //{ "useTexture", &ABR::parse_prop },
// //{ "usePaintDynamics", &ABR::parse_prop },
// //{ "useColorDynamics", &ABR::parse_prop },
// //{ "Wtdg", &ABR::parse_prop },
// //{ "Nose", &ABR::parse_prop },
// //{ "Rpt ", &ABR::parse_prop },
// //{ "useBrushSize", &ABR::parse_prop },
// //{ "useBrushPose", &ABR::parse_prop },
//};
bool ABR::section_desc()
{
auto sz = align4(ru32());
@@ -59,8 +25,83 @@ bool ABR::section_samp()
bool ABR::section_patt()
{
auto sz = align4(ru32());
skip(sz);
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;
}
@@ -160,6 +201,13 @@ std::shared_ptr<ABR::Enum> ABR::parse_enum()
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()>>
@@ -172,28 +220,7 @@ ABR::ABR()
{ "long", std::bind(&ABR::parse_long, this) },
{ "doub", std::bind(&ABR::parse_doub, this) },
{ "enum", std::bind(&ABR::parse_enum, this) },
//{ "Dmtr", &ABR::parse_prop },
//{ "Hrdn", &ABR::parse_prop },
//{ "Angl", &ABR::parse_prop },
//{ "Rndn", &ABR::parse_prop },
//{ "Spcn", &ABR::parse_prop },
//{ "Intr", &ABR::parse_prop },
//{ "flipX", &ABR::parse_prop },
//{ "flipY", &ABR::parse_prop },
//{ "useTipDynamics", &ABR::parse_prop },
//{ "useScatter", &ABR::parse_prop },
//{ "dualBrush", &ABR::parse_prop },
//{ "useDualBrush", &ABR::parse_prop },
//{ "brushGroup", &ABR::parse_prop },
//{ "useBrushGroup", &ABR::parse_prop },
//{ "useTexture", &ABR::parse_prop },
//{ "usePaintDynamics", &ABR::parse_prop },
//{ "useColorDynamics", &ABR::parse_prop },
//{ "Wtdg", &ABR::parse_prop },
//{ "Nose", &ABR::parse_prop },
//{ "Rpt ", &ABR::parse_prop },
//{ "useBrushSize", &ABR::parse_prop },
//{ "useBrushPose", &ABR::parse_prop },
{ "tdta", std::bind(&ABR::parse_tdta, this) },
};
}
@@ -215,6 +242,10 @@ bool ABR::open(const std::string& path)
{
section_desc();
}
else if (t == "patt")
{
section_patt();
}
else
{
printf("skip section %s\n", t.c_str());