Skip to content

Instantly share code, notes, and snippets.

@retep998
Created December 6, 2012 18:18
Show Gist options
  • Save retep998/4226732 to your computer and use it in GitHub Desktop.
Save retep998/4226732 to your computer and use it in GitHub Desktop.
NX png dumper
//////////////////////////////////////////////////////////////////////////
// Copyright 2012 Peter Atechian (Retep998) //
//////////////////////////////////////////////////////////////////////////
// This file is part of the NoLifeStory project. //
// //
// NoLifeStory is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// NoLifeStory is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with NoLifeStory. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////
#include "NX.hpp"
#include <string>
#include <filesystem>
#include <png/png.hpp>
using namespace std;
using namespace tr2::sys;
using namespace NL;
using namespace png;
void recurse(string s, Node n) {
s += (string)n.Name();
if (n.T() == n.bitmap) {
s += ".png";
Bitmap b = n;
image<rgba_pixel> img(b.Width(), b.Height());
uint8_t const * d = static_cast<uint8_t const *>(b.Data());
for (size_t y = 0, i = 0; y < b.Height(); ++y) {
auto & r = img.get_pixbuf().get_row(y);
for (size_t x = 0; x < b.Width(); ++x, ++i) {
r[x].blue = d[i * 4];
r[x].green = d[i * 4 + 1];
r[x].red = d[i * 4 + 2];
r[x].alpha = d[i * 4 + 3];
}
}
img.write(s);
} else {
s += '.';
for (Node nn : n) recurse(s, nn);
}
}
void frecurse(string s, Node n) {
s += '/';
s += (string)n.Name();
create_directory((path)s);
if (((string)n.Name()).find(".img") != string::npos) for (Node nn : n) recurse(s + "/", nn);
else for (Node nn : n) frecurse(s, nn);
}
void dump(string name) {
File file(name.c_str());
path p = path(name).replace_extension();
create_directory(p);
for (Node nn : file.Base()) frecurse(p, nn);
}
int main(int argc, char ** argv) {
for (int i = 1; i < argc; ++i) dump(argv[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment