diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2022-09-30 18:48:50 +0200 |
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-09-30 18:50:32 +0200 |
| commit | 5c9863cf0998b5f1b6107ce09b54cd3e8b484221 (patch) | |
| tree | 7ca268c80a0ec7ac0c1b815d984cf11309b96840 /crop-tool | |
| parent | d3a29055d8b1dce89c77af0988ea840e949d2450 (diff) | |
.
Diffstat (limited to 'crop-tool')
| -rw-r--r-- | crop-tool/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | crop-tool/atlas.cpp | 63 | ||||
| -rw-r--r-- | crop-tool/atlas.hpp | 37 | ||||
| -rw-r--r-- | crop-tool/crop-tool.cpp | 2 |
4 files changed, 102 insertions, 2 deletions
diff --git a/crop-tool/CMakeLists.txt b/crop-tool/CMakeLists.txt index 42aeac2c..251bcac3 100644 --- a/crop-tool/CMakeLists.txt +++ b/crop-tool/CMakeLists.txt @@ -3,7 +3,7 @@ set(self "${PROJECT_NAME}-crop-tool") include_directories(SYSTEM PRIVATE ${OpenCV_INCLUDE_DIRS}) link_libraries(Corrade::Utility) -link_libraries(game-anim) +link_libraries(${PROJECT_NAME}-anim) file(GLOB sources "*.cpp" CONFIGURE_ARGS) add_executable(${self} ${sources}) diff --git a/crop-tool/atlas.cpp b/crop-tool/atlas.cpp new file mode 100644 index 00000000..9e16a3cd --- /dev/null +++ b/crop-tool/atlas.cpp @@ -0,0 +1,63 @@ +#undef NDEBUG + +#include "atlas.hpp" +#include "../anim/serialize.hpp" + +#include <cassert> +#include <filesystem> +#include <opencv2/imgcodecs.hpp> + +void anim_atlas_row::add_entry(const anim_atlas_entry& x) noexcept +{ + auto& frame = *x.frame; + const auto& mat = x.mat; + frame.offset = {xpos, ypos}; + frame.size = {mat.cols, mat.rows}; + + assert(mat.rows > 0 && mat.cols > 0); + data.push_back(x); + xpos += mat.cols; + max_height = std::max(mat.rows, max_height); +} + +void anim_atlas::advance_row() noexcept +{ + auto& row = rows.back(); + if (row.data.empty()) + return; + assert(row.xpos); assert(row.max_height); + ypos += row.max_height; + maxx = std::max(row.xpos, maxx); + rows.push_back({{}, 0, 0, ypos}); +} + +Magnum::Vector2i anim_atlas::offset() const noexcept +{ + const auto& row = rows.back(); + return {row.xpos, row.ypos}; +} + +Magnum::Vector2i anim_atlas::size() const noexcept +{ + const anim_atlas_row& row = rows.back(); + // prevent accidentally writing out of bounds by forgetting to call + // anim_atlas::advance_row() one last time prior to anim_atlas::size() + return {std::max(maxx, row.xpos), ypos + row.max_height}; +} + +bool anim_atlas::dump(const std::filesystem::path& filename) const noexcept +{ + auto sz = size(); + cv::Mat4b mat(sz[1], sz[0]); + mat.setTo(0); + + for (const anim_atlas_row& row : rows) + for (const anim_atlas_entry& x : row.data) + { + auto offset = x.frame->offset, size = x.frame->size; + cv::Rect roi = {offset[0], offset[1], size[0], size[1]}; + x.mat.copyTo(mat(roi)); + } + + return cv::imwrite(filename.string(), mat); +} diff --git a/crop-tool/atlas.hpp b/crop-tool/atlas.hpp new file mode 100644 index 00000000..5c5e918f --- /dev/null +++ b/crop-tool/atlas.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include <vector> +#include <Magnum/Magnum.h> +#include <Magnum/Math/Vector2.h> +#include <opencv2/core/mat.hpp> + +struct anim_frame; + +namespace std::filesystem { class path; } + +struct anim_atlas_entry +{ + anim_frame* frame; + cv::Mat4b mat; +}; + +struct anim_atlas_row +{ + std::vector<anim_atlas_entry> data; + int max_height = 0, xpos = 0, ypos = 0; + + void add_entry(const anim_atlas_entry& x) noexcept; +}; + +class anim_atlas +{ + std::vector<anim_atlas_row> rows = {{}}; + int ypos = 0, maxx = 0; + +public: + void add_entry(const anim_atlas_entry& x) noexcept { rows.back().add_entry(x); } + void advance_row() noexcept; + Magnum::Vector2i offset() const noexcept; + Magnum::Vector2i size() const noexcept; + [[nodiscard]] bool dump(const std::filesystem::path& filename) const noexcept; +}; diff --git a/crop-tool/crop-tool.cpp b/crop-tool/crop-tool.cpp index 064f004d..42d54baa 100644 --- a/crop-tool/crop-tool.cpp +++ b/crop-tool/crop-tool.cpp @@ -1,7 +1,7 @@ #undef NDEBUG #include "defs.hpp" -#include "anim/atlas.hpp" +#include "atlas.hpp" #include "anim/serialize.hpp" #include <cassert> |
