From 024e879ee6ff3f0e4507ceebdfc0616f28c7c593 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Thu, 2 Nov 2023 22:12:04 +0900 Subject: [PATCH] tools/lottie2gif: introduce a new converter tool. Usage: lottie2gif [Lottie file] or [Lottie folder] [-r resolution] [-f fps] Examples: $ lottie2gif input.json $ lottie2gif input.json -f 30 $ lottie2gif input.json -r 600x600 -f 30 $ lottie2gif lottiefolder $ lottie2gif lottiefolder -r 600x600 $ lottie2gif lottiefolder -r 600x600 -f 30 --- meson.build | 2 + meson_options.txt | 2 +- src/tools/lottie2gif/lottie2gif.cpp | 258 ++++++++++++++++++++++++++++ src/tools/lottie2gif/meson.build | 8 + src/tools/meson.build | 3 + 5 files changed, 272 insertions(+), 1 deletion(-) create mode 100644 src/tools/lottie2gif/lottie2gif.cpp create mode 100644 src/tools/lottie2gif/meson.build diff --git a/meson.build b/meson.build index 0526dc85..ee4e325f 100644 --- a/meson.build +++ b/meson.build @@ -148,6 +148,7 @@ Summary: Examples: @19@ Tool (Svg2Tvg): @20@ Tool (Svg2Png): @21@ + Tool (Lottie2Gif): @22@ '''.format( meson.project_version(), @@ -172,6 +173,7 @@ Summary: get_option('examples'), all_tools or get_option('tools').contains('svg2tvg'), all_tools or get_option('tools').contains('svg2png'), + all_tools or get_option('tools').contains('lottie2gif'), ) message(summary) diff --git a/meson_options.txt b/meson_options.txt index ba0ccf83..828af39e 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -29,7 +29,7 @@ option('bindings', option('tools', type: 'array', - choices: ['', 'svg2tvg', 'svg2png', 'all'], + choices: ['', 'svg2tvg', 'svg2png', 'lottie2gif', 'all'], value: [''], description: 'Enable building thorvg tools') diff --git a/src/tools/lottie2gif/lottie2gif.cpp b/src/tools/lottie2gif/lottie2gif.cpp new file mode 100644 index 00000000..0d61c517 --- /dev/null +++ b/src/tools/lottie2gif/lottie2gif.cpp @@ -0,0 +1,258 @@ +/* + * Copyright (c) 2023 the ThorVG project. All rights reserved. + + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include +#ifdef _WIN32 + #include + #ifndef PATH_MAX + #define PATH_MAX MAX_PATH + #endif +#else + #include + #include + #include + #include +#endif + +using namespace std; +using namespace tvg; + + +struct App +{ +private: + char full[PATH_MAX]; //full path + uint32_t fps = 30; + uint32_t width = 600; + uint32_t height = 600; + + void helpMsg() + { + cout << "Usage: \n lottie2gif [Lottie file] or [Lottie folder] [-r resolution] [-f fps]\n\nExamples: \n $ lottie2gif input.json\n $ lottie2gif input.json -r 600x600\n $ lottie2gif input.json -f 30\n $ lottie2gif input.json -r 600x600 -f 30\n $ lottie2gif lottiefolder\n $ lottie2gif lottiefolder -r 600x600 -f 30\n\n"; + } + + bool validate(string& lottieName) + { + string extn = ".json"; + + if (lottieName.size() <= extn.size() || lottieName.substr(lottieName.size() - extn.size()) != extn) { + cout << "Error: \"" << lottieName << "\" is invalid." << endl; + return false; + } + return true; + } + + bool convert(string& in, string& out) + { + if (Initializer::init(0, CanvasEngine::Sw) != Result::Success) return false; + + auto animation = Animation::gen(); + auto picture = animation->picture(); + if (picture->load(in) != Result::Success) return false; + + picture->size(static_cast(width), static_cast(height)); + + auto saver = Saver::gen(); + if (saver->save(std::move(animation), out, 100, fps) != Result::Success) return false; + if (saver->sync() != Result::Success) return false; + + if (Initializer::term(CanvasEngine::Sw) != Result::Success) return false; + + return true; + } + + void convert(string& lottieName) + { + //Get gif file + auto gifName = lottieName; + gifName.replace(gifName.length() - 4, 4, "gif"); + + if (convert(lottieName, gifName)) { + cout << "Generated Gif file : " << gifName << endl; + } else { + cout << "Failed Converting Gif file : " << lottieName << endl; + } + } + + const char* realPath(const char* path) + { +#ifdef _WIN32 + return _fullpath(full, path, PATH_MAX); +#else + return realpath(path, full); +#endif + } + + bool isDirectory(const char* path) + { +#ifdef _WIN32 + DWORD attr = GetFileAttributes(path); + if (attr == INVALID_FILE_ATTRIBUTES) return false; + return attr & FILE_ATTRIBUTE_DIRECTORY; +#else + struct stat buf; + if (stat(path, &buf) != 0) return false; + return S_ISDIR(buf.st_mode); +#endif + } + + bool handleDirectory(const string& path) + { +#ifdef _WIN32 + //open directory + WIN32_FIND_DATA fd; + HANDLE h = FindFirstFileEx((path + "\\*").c_str(), FindExInfoBasic, &fd, FindExSearchNameMatch, NULL, 0); + if (h == INVALID_HANDLE_VALUE) { + cout << "Couldn't open directory \"" << path.c_str() << "\"." << endl; + return false; + } + //List directories + do { + if (*fd.cFileName == '.' || *fd.cFileName == '$') continue; + //sub directory + if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + string subpath = string(path); + subpath += '\\'; + subpath += fd.cFileName; + if (!handleDirectory(subpath)) continue; + //file + } else { + string lottieName(fd.cFileName); + if (!validate(lottieName)) continue; + lottieName = string(path); + lottieName += '\\'; + lottieName += fd.cFileName; + convert(lottieName); + } + } while (FindNextFile(h, &fd)); + + FindClose(h); +#else + //open directory + auto dir = opendir(path.c_str()); + if (!dir) { + cout << "Couldn't open directory \"" << path.c_str() << "\"." << endl; + return false; + } + //List directories + while (auto entry = readdir(dir)) { + if (*entry->d_name == '.' || *entry->d_name == '$') continue; + //sub directory + if (entry->d_type == DT_DIR) { + string subpath = string(path); + subpath += '/'; + subpath += entry->d_name; + if (!handleDirectory(subpath)) continue; + //file + } else { + string svgName(entry->d_name); + if (!validate(svgName)) continue; + svgName = string(path); + svgName += '/'; + svgName += entry->d_name; + convert(svgName); + } + } +#endif + return true; + } + +public: + int setup(int argc, char** argv) + { + //Collect input files + vector inputs; + + for (int i = 1; i < argc; ++i) { + const char* p = argv[i]; + if (*p == '-') { + const char* p_arg = (i + 1 < argc) ? argv[++i] : nullptr; + + //image resolution + if (p[1] == 'r') { + if (!p_arg) { + cout << "Error: Missing resolution attribute. Expected eg. -r 600x600." << endl; + return 1; + } + + const char* x = strchr(p_arg, 'x'); + if (x) { + width = atoi(p_arg); + height = atoi(x + 1); + } + if (!x || width <= 0 || height <= 0) { + cout << "Error: Resolution (" << p_arg << ") is corrupted. Expected eg. -r 600x600." << endl; + return 1; + } + //fps + } else if (p[1] == 'f') { + if (!p_arg) { + cout << "Error: Missing fps value. Expected eg. -f 30." << endl; + return 1; + } + fps = atoi(p_arg); + } else { + cout << "Warning: Unknown flag (" << p << ")." << endl; + } + }else { + inputs.push_back(argv[i]); + } + } + + //No Input Lottie + if (inputs.empty()) { + helpMsg(); + return 0; + } + + for (auto input : inputs) { + + auto path = realPath(input); + if (!path) { + cout << "Invalid file or path name: \"" << input << "\"" << endl; + continue; + } + + if (isDirectory(path)) { + //load from directory + cout << "Directory: \"" << path << "\"" << endl; + if (!handleDirectory(path)) break; + } + else { + string lottieName(input); + if (!validate(lottieName)) continue; + convert(lottieName); + } + } + return 0; + } +}; + + +int main(int argc, char **argv) +{ + App app; + return app.setup(argc, argv); +} \ No newline at end of file diff --git a/src/tools/lottie2gif/meson.build b/src/tools/lottie2gif/meson.build new file mode 100644 index 00000000..e23aa4d9 --- /dev/null +++ b/src/tools/lottie2gif/meson.build @@ -0,0 +1,8 @@ +lottie2gif_src = files('lottie2gif.cpp') + +executable('lottie2gif', + lottie2gif_src, + include_directories : headers, + cpp_args : compiler_flags, + install : true, + link_with : thorvg_lib) diff --git a/src/tools/meson.build b/src/tools/meson.build index 2e6cd012..276992de 100644 --- a/src/tools/meson.build +++ b/src/tools/meson.build @@ -10,3 +10,6 @@ if all_tools or get_option('tools').contains('svg2tvg') == true subdir('svg2tvg') endif +if all_tools or get_option('tools').contains('lottie2gif') == true + subdir('lottie2gif') +endif \ No newline at end of file