Skip to content

Instantly share code, notes, and snippets.

@EAirPeter
Last active July 1, 2017 16:43
Show Gist options
  • Save EAirPeter/75414c05c0e50954ac8538be5ced0e53 to your computer and use it in GitHub Desktop.
Save EAirPeter/75414c05c0e50954ac8538be5ced0e53 to your computer and use it in GitHub Desktop.
AchSign
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/gil/image.hpp>
#include <boost/gil/extension/io/png_io.hpp>
#include <boost/lexical_cast.hpp>
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <string>
#include <tuple>
#include <unordered_set>
#include <utility>
#include <vector>
using boost::filesystem::directory_iterator;
using boost::filesystem::path;
using boost::gil::rgba8_image_t;
using std::cout;
using std::endl;
using std::string;
using std::tuple;
using std::unordered_set;
using std::vector;
using image_t = rgba8_image_t;
using view_t = image_t::view_t;
using coord_t = image_t::coord_t;
using ccoord_t = const coord_t;
void CopyPxs(image_t &imgDst, const image_t &imgSrc, coord_t dx, coord_t dy, coord_t sx, coord_t sy, coord_t cx, coord_t cy) {
for (coord_t vx = 0; vx < cx; ++vx)
for (coord_t vy = 0; vy < cy; ++vy)
imgDst._view(dx + vx, dy + vy) = imgSrc._view(sx + vx, sy + vy);
}
decltype(auto) OpenImage(const tuple<coord_t, string, path> &tpInfo) {
image_t img;
auto &sExt = std::get<1>(tpInfo);
auto &pa = std::get<2>(tpInfo);
if (sExt == ".png")
boost::gil::png_read_and_convert_image(pa.string(), img);
else
throw std::invalid_argument("Invalid file: " + pa.filename().string());
if (img.dimensions().x != 800 || img.dimensions().y != 480)
throw std::invalid_argument("Invalid file: " + pa.filename().string());
return img;
}
decltype(auto) CreateImage(vector<tuple<coord_t, string, path>> &v) {
auto N = (coord_t) v.size();
std::sort(v.begin(), v.end());
image_t img(685, 83 + (N - 1) * 30 + 29 + 28);
// Items
coord_t j = 0;
for (auto &tp : v) {
cout << "Adding " << std::get<2>(tp) << " ..." << endl;
auto img_ = OpenImage(tp);
auto i = (std::get<0>(tp) - 1) % 10 + 1;
ccoord_t sy = 153 + (i - 1) * 30;
ccoord_t dy = 83 + j * 30;
CopyPxs(img, img_, 20, dy, 134, sy, 630, 29);
CopyPxs(img, img_, 20, dy + 29, 134, i == 10 ? 422 : sy + 29, 630, 1);
++j;
}
cout << "Adding frame..." << endl;
auto img_ = OpenImage(v.front());
// Header
CopyPxs(img, img_, 0, 0, 114, 70, 685, 83);
// Footer
ccoord_t fy = 83 + (N - 1) * 30 + 29;
CopyPxs(img, img_, 0, fy, 114, 452, 685, 28);
// Left
ccoord_t my = std::min(fy - 83, (coord_t) 299);
CopyPxs(img, img_, 0, 83, 114, 153, 20, my);
for (coord_t y = 83 + my; y < fy; ++y)
CopyPxs(img, img_, 0, y, 114, 451, 20, 1);
// Right
CopyPxs(img, img_, 20 + 630, 83, 764, 153, 35, my);
for (coord_t y = 83 + my; y < fy; ++y)
CopyPxs(img, img_, 20 + 630, y, 764, 451, 35, 1);
return img;
}
int main() {
path dir = boost::filesystem::current_path();
unordered_set<string> seExts{ ".png" };
vector<tuple<coord_t, string, path>> v;
for (directory_iterator i(dir / "rank"); i != directory_iterator{}; ++i)
if (boost::filesystem::is_regular_file(*i)) {
auto pa = i->path();
auto sExt = pa.extension().extension().string();
boost::to_lower(sExt);
if (seExts.find(sExt) != seExts.end()) {
try {
auto stem = pa.stem().string();
auto nHyp = stem.find('-');
if (nHyp != stem.npos) {
auto i = boost::lexical_cast<coord_t>(stem.substr(0, nHyp));
v.emplace_back(i, sExt, pa);
cout << "Found valid file: " << pa.filename() << endl;
}
else
throw 0;
}
catch (...) {
cout << "Skipped for illegal file name: " << pa.filename() << endl;
}
}
}
if (v.empty()) {
cout << "No valid file found. Press Enter to exit..." << endl;
std::cin.get();
}
else
try {
cout << "Processing..." << endl;
auto img = CreateImage(v);
auto paOut = dir / "all.png";
boost::gil::png_write_view(paOut.string(), img._view);
cout << "Output: " << paOut.filename() << endl;
cout << "Done." << endl;
}
catch (std::exception e) {
cout << "Exception caught: " << e.what() << endl;
cout << "Press Enter to exit..." << endl;
std::cin.get();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment