From d8f23adb353b8caf3f570e95327e58acf369737c Mon Sep 17 00:00:00 2001 From: omigamedev Date: Fri, 8 Feb 2019 13:40:37 +0100 Subject: [PATCH] parse samp section for ABR version 6.2 --- src/abr.cpp | 148 ++++++++++++++++++++++++++++++++++------------------ src/abr.h | 21 ++++++++ 2 files changed, 118 insertions(+), 51 deletions(-) diff --git a/src/abr.cpp b/src/abr.cpp index e09d608..23f6037 100644 --- a/src/abr.cpp +++ b/src/abr.cpp @@ -18,28 +18,56 @@ bool ABR::section_desc() bool ABR::section_samp() { - auto sz = align4(ru32()); - skip(sz); + auto section_size = ru32(); + 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 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; } bool ABR::section_patt() { - auto sz = ru32(); - auto sz_aligned = align4(sz); - + auto section_size = ru32(); auto start = pos(); - auto end = start + sz; + auto end = start + section_size; while (pos() < end) { - auto length = ru32(); // length of this pattern - auto version = ru32(); // = 1 + auto patt_length = ru32(); // length of this pattern + auto patt_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); + skip(patt_length - 8); snap(); continue; } @@ -52,59 +80,73 @@ bool ABR::section_patt() // read(...); // no worries indexed is skipped anyway - // Virtual Memory Array List + auto image = parse_vmem(); + if (image_mode == 3) { - 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 out(rect.area()); - bool valid = false; - for (int ch = 0; ch < channels; ch++) + if (image->channels.size() >= 3) { - 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) + std::vector out((image->channels[0].depth >> 3) * image->rect.area()); + for (int ch = 0; ch < 3; ch++) { - 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++) + auto const& raw = image->channels[ch].data; + for (int i = 0; i < raw.size(); i++) out[i][ch] = raw[i]; } - else - { - for (int i = 0; i < data_size; i++) - out[i] = glm::u8vec3(raw[i]); - } - valid = true; + stbi_write_png(fmt::format("x64/out/{}.png", uid).c_str(), + image->rect.width(), image->rect.height(), 3, out.data(), 0); } - if (valid) - stbi_write_png(fmt::format("{}.png", uid).c_str(), rect.width(), rect.height(), 3, out.data(), 0); - snap(); + 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); } } - - //skip(sz); return true; } +std::shared_ptr ABR::parse_vmem() +{ + // 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 + auto ret = std::make_shared(version, rect); + 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) + { + printf("unsupported compression mode %d\n", compression); + skip(length - 23); + continue; + } + int data_size = (depth >> 3) * rect.area(); + ret->channels.emplace_back(depth, rect, compression, rraw(data_size)); + } + snap(); + return ret; +} + std::shared_ptr ABR::parse_vlls() { auto ret = std::make_shared(); @@ -246,6 +288,10 @@ bool ABR::open(const std::string& path) { section_patt(); } + else if (t == "samp") + { + section_samp(); + } else { printf("skip section %s\n", t.c_str()); diff --git a/src/abr.h b/src/abr.h index 55ba274..2bfb00a 100644 --- a/src/abr.h +++ b/src/abr.h @@ -271,6 +271,25 @@ class ABR : public BinaryStream virtual std::string str(int indent, const std::string& prefix) const override { return std::string(indent, '-') + prefix + fmt::format("point: [{}, {}]", x, y); } }; + struct Channel : public Type + { + uint32_t depth; + Rectangle rect; + uint8_t compression; + std::vector data; + Channel() = default; + Channel(uint32_t depth, const Rectangle& rect, uint8_t compression, std::vector& data) : + depth(depth), rect(rect), compression(compression), data(std::move(data)) { } + }; + struct Image : public Type + { + uint32_t version; // = 3 + Rectangle rect; + std::vector channels; + Image() = default; + Image(uint32_t version, const Rectangle& rect) : + version(version), rect(rect) { } + }; public: ABR(); @@ -302,6 +321,8 @@ private: bool section_desc(); bool section_samp(); bool section_patt(); + // Parse Virtual Memory Array List + std::shared_ptr parse_vmem(); std::shared_ptr parse_vlls(); std::shared_ptr parse_text(); std::shared_ptr parse_objc();