From 5c9863cf0998b5f1b6107ce09b54cd3e8b484221 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 30 Sep 2022 18:48:50 +0200 Subject: . --- anim/atlas.cpp | 63 ----------------------------------------- anim/atlas.hpp | 37 ------------------------ anim/serialize.cpp | 83 +++--------------------------------------------------- anim/serialize.hpp | 2 +- 4 files changed, 5 insertions(+), 180 deletions(-) delete mode 100644 anim/atlas.cpp delete mode 100644 anim/atlas.hpp (limited to 'anim') diff --git a/anim/atlas.cpp b/anim/atlas.cpp deleted file mode 100644 index fe4b5f65..00000000 --- a/anim/atlas.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#undef NDEBUG - -#include "atlas.hpp" -#include "serialize.hpp" - -#include -#include -#include - -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/anim/atlas.hpp b/anim/atlas.hpp deleted file mode 100644 index 5c5e918f..00000000 --- a/anim/atlas.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -struct anim_frame; - -namespace std::filesystem { class path; } - -struct anim_atlas_entry -{ - anim_frame* frame; - cv::Mat4b mat; -}; - -struct anim_atlas_row -{ - std::vector data; - int max_height = 0, xpos = 0, ypos = 0; - - void add_entry(const anim_atlas_entry& x) noexcept; -}; - -class anim_atlas -{ - std::vector 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/anim/serialize.cpp b/anim/serialize.cpp index b0082c3c..83d2b2f9 100644 --- a/anim/serialize.cpp +++ b/anim/serialize.cpp @@ -1,50 +1,12 @@ #include "serialize.hpp" -#include -#include -#include -#include - #include #include -#include "../fake-json.hpp" +#include "../json-magnum.hpp" using Corrade::Utility::Error; -namespace nlohmann { - -template<> -struct adl_serializer final { - static void to_json(json& j, const Magnum::Vector2i& x); - static void from_json(const json& j, Magnum::Vector2i& x); -}; - -void adl_serializer::to_json(json& j, const Magnum::Vector2i& val) -{ - char buf[64]; - snprintf(buf, sizeof(buf), "%d x %d", val[0], val[1]); - j = buf; -} - -void adl_serializer::from_json(const json& j, Magnum::Vector2i& val) -{ - std::string str = j; - int x = 0, y = 0, n = 0; - int ret = std::sscanf(str.c_str(), "%d x %d%n", &x, &y, &n); - if (ret != 2 || (std::size_t)n != str.size()) - { - std::string msg; msg.reserve(64 + str.size()); - msg += "failed to parse string '"; - msg += str; - msg += "' as Magnum::Vector2i"; - throw std::invalid_argument(msg); - } - val = { x, y }; -} - -} // namespace nlohmann - #if defined __clang__ || defined __CLION_IDE__ # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wweak-vtables" @@ -61,47 +23,10 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim, name, nframes, actionframe, fps, groups std::tuple anim::from_json(const std::filesystem::path& pathname) noexcept { - using namespace nlohmann; - std::ifstream s; - s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); - try { - s.open(pathname, std::ios_base::in); - } catch (const std::ios::failure& e) { - Error{Error::Flag::NoSpace} << "failed to open " << pathname << ": " << e.what(); - return { {}, false }; - } - anim ret; - try { - json j; - s >> j; - using nlohmann::from_json; - from_json(j, ret); - } catch (const std::exception& e) { - Error{Error::Flag::NoSpace} << "failed to parse " << pathname << ": " << e.what(); - return { {}, false }; - } - return { std::move(ret), true }; + return json_helper::from_json(pathname); } -bool anim::to_json(const std::filesystem::path& pathname) noexcept +bool anim::to_json(const std::filesystem::path& pathname) const noexcept { - try { - nlohmann::json j = *this; - - std::ofstream s; - s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); - try { - s.open(pathname, std::ios_base::out | std::ios_base::trunc); - } catch (const std::ios::failure& e) { - Error{} << "failed to open" << pathname << "for writing:" << e.what(); - return false; - } - s << j.dump(4); - s.flush(); - } catch (const std::exception& e) { - Error{Error::Flag::NoSpace} << "failed writing to " << pathname << ": " << e.what(); - return false; - } - - return true; + return json_helper::to_json(*this, pathname); } diff --git a/anim/serialize.hpp b/anim/serialize.hpp index a8b13d41..49641fb5 100644 --- a/anim/serialize.hpp +++ b/anim/serialize.hpp @@ -32,7 +32,7 @@ struct anim_group final struct anim final { static std::tuple from_json(const std::filesystem::path& pathname) noexcept; - [[nodiscard]] bool to_json(const std::filesystem::path& pathname) noexcept; + [[nodiscard]] bool to_json(const std::filesystem::path& pathname) const noexcept; static constexpr int default_fps = 24; std::string name; -- cgit v1.2.3