parse samp section for ABR version 6.2

This commit is contained in:
2019-02-08 13:40:37 +01:00
parent a1e4f0b536
commit d8f23adb35
2 changed files with 118 additions and 51 deletions

View File

@@ -18,28 +18,56 @@ bool ABR::section_desc()
bool ABR::section_samp() bool ABR::section_samp()
{ {
auto sz = align4(ru32()); auto section_size = ru32();
skip(sz); auto start = pos();
auto end = start + section_size;
while (pos() < end)
{
auto samp_size = ru32();
auto uid = rpascal();
skip(4);
auto image = parse_vmem();
if (image->channels.size() >= 3)
{
std::vector<glm::u8vec3> out((image->channels[0].depth >> 3) * image->rect.area());
for (int ch = 0; ch < 3; ch++)
{
auto const& raw = image->channels[ch].data;
for (int i = 0; i < raw.size(); i++)
out[i][ch] = raw[i];
}
stbi_write_png(fmt::format("x64/out/{}.png", uid).c_str(),
image->rect.width(), image->rect.height(), 3, out.data(), 0);
}
else if (image->channels.size() == 1)
{
stbi_write_png(fmt::format("x64/out/{}.png", uid).c_str(),
image->rect.width(), image->rect.height(), 1,
std::data(image->channels[0].data), 0);
}
else
{
printf("Error cannot read image of %d channels\n", image->channels.size());
}
}
return true; return true;
} }
bool ABR::section_patt() bool ABR::section_patt()
{ {
auto sz = ru32(); auto section_size = ru32();
auto sz_aligned = align4(sz);
auto start = pos(); auto start = pos();
auto end = start + sz; auto end = start + section_size;
while (pos() < end) while (pos() < end)
{ {
auto length = ru32(); // length of this pattern auto patt_length = ru32(); // length of this pattern
auto version = ru32(); // = 1 auto patt_version = ru32(); // = 1
// Bitmap = 0; Grayscale = 1; Indexed = 2; RGB = 3; // Bitmap = 0; Grayscale = 1; Indexed = 2; RGB = 3;
// CMYK = 4; Multichannel = 7; Duotone = 8; Lab = 9 // CMYK = 4; Multichannel = 7; Duotone = 8; Lab = 9
auto image_mode = ru32(); auto image_mode = ru32();
if (!(image_mode == 1 || image_mode == 3)) if (!(image_mode == 1 || image_mode == 3))
{ {
skip(length - 8); skip(patt_length - 8);
snap(); snap();
continue; continue;
} }
@@ -52,8 +80,39 @@ bool ABR::section_patt()
// read(...); // read(...);
// no worries indexed is skipped anyway // no worries indexed is skipped anyway
// Virtual Memory Array List auto image = parse_vmem();
if (image_mode == 3)
{ {
if (image->channels.size() >= 3)
{
std::vector<glm::u8vec3> out((image->channels[0].depth >> 3) * image->rect.area());
for (int ch = 0; ch < 3; ch++)
{
auto const& raw = image->channels[ch].data;
for (int i = 0; i < raw.size(); i++)
out[i][ch] = raw[i];
}
stbi_write_png(fmt::format("x64/out/{}.png", uid).c_str(),
image->rect.width(), image->rect.height(), 3, out.data(), 0);
}
else
{
printf("Error image mode is 3 but channels are only %d\n", image->channels.size());
}
}
else if (image_mode == 1)
{
stbi_write_png(fmt::format("x64/out/{}.png", uid).c_str(),
image->rect.width(), image->rect.height(), 1,
std::data(image->channels[0].data), 0);
}
}
return true;
}
std::shared_ptr<ABR::Image> ABR::parse_vmem()
{
// Virtual Memory Array List
auto version = ru32(); // = 3 auto version = ru32(); // = 3
auto length = ru32(); auto length = ru32();
auto rect = rrect(); auto rect = rrect();
@@ -62,8 +121,7 @@ bool ABR::section_patt()
// repeated for the number of channels // repeated for the number of channels
// + one for a user mask + one for a sheet mask. // + one for a user mask + one for a sheet mask.
channels += 2; // user and sheet mask channels += 2; // user and sheet mask
std::vector<glm::u8vec3> out(rect.area()); auto ret = std::make_shared<Image>(version, rect);
bool valid = false;
for (int ch = 0; ch < channels; ch++) for (int ch = 0; ch < channels; ch++)
{ {
auto array_written = ru32(); // skip if 0 auto array_written = ru32(); // skip if 0
@@ -78,31 +136,15 @@ bool ABR::section_patt()
auto compression = ru8(); // 1 = zip auto compression = ru8(); // 1 = zip
if (compression != 0 || depth != 8) if (compression != 0 || depth != 8)
{ {
printf("unsupported compression mode %d\n", compression);
skip(length - 23); skip(length - 23);
continue; continue;
} }
int data_size = (depth >> 3) * rect.area(); int data_size = (depth >> 3) * rect.area();
auto raw = rraw(data_size); ret->channels.emplace_back(depth, rect, compression, 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(); snap();
} return ret;
}
//skip(sz);
return true;
} }
std::shared_ptr<ABR::List> ABR::parse_vlls() std::shared_ptr<ABR::List> ABR::parse_vlls()
@@ -246,6 +288,10 @@ bool ABR::open(const std::string& path)
{ {
section_patt(); section_patt();
} }
else if (t == "samp")
{
section_samp();
}
else else
{ {
printf("skip section %s\n", t.c_str()); printf("skip section %s\n", t.c_str());

View File

@@ -271,6 +271,25 @@ class ABR : public BinaryStream
virtual std::string str(int indent, const std::string& prefix) const override virtual std::string str(int indent, const std::string& prefix) const override
{ return std::string(indent, '-') + prefix + fmt::format("point: [{}, {}]", x, y); } { return std::string(indent, '-') + prefix + fmt::format("point: [{}, {}]", x, y); }
}; };
struct Channel : public Type
{
uint32_t depth;
Rectangle rect;
uint8_t compression;
std::vector<uint8_t> data;
Channel() = default;
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
{
uint32_t version; // = 3
Rectangle rect;
std::vector<Channel> channels;
Image() = default;
Image(uint32_t version, const Rectangle& rect) :
version(version), rect(rect) { }
};
public: public:
ABR(); ABR();
@@ -302,6 +321,8 @@ private:
bool section_desc(); bool section_desc();
bool section_samp(); bool section_samp();
bool section_patt(); bool section_patt();
// Parse Virtual Memory Array List
std::shared_ptr<Image> parse_vmem();
std::shared_ptr<List> parse_vlls(); std::shared_ptr<List> parse_vlls();
std::shared_ptr<String> parse_text(); std::shared_ptr<String> parse_text();
std::shared_ptr<Descriptor> parse_objc(); std::shared_ptr<Descriptor> parse_objc();