From 2960379eba9ae14e364c97e2e905f1c29db53fdf Mon Sep 17 00:00:00 2001 From: Arthur Brainville Date: Mon, 19 Mar 2018 20:24:00 +0100 Subject: [PATCH] Can select image file format with -f argument in gltf util Signed-off-by: Arthur Brainville (Ybalrid) --- examples/gltfutil/gltfuilconfig.h | 5 +++++ examples/gltfutil/main.cc | 18 ++++++++++++++++-- examples/gltfutil/texture_dumper.cc | 22 +++++++++++++++++++++- examples/gltfutil/texture_dumper.h | 3 +++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/examples/gltfutil/gltfuilconfig.h b/examples/gltfutil/gltfuilconfig.h index fc5c427..b6bb196 100644 --- a/examples/gltfutil/gltfuilconfig.h +++ b/examples/gltfutil/gltfuilconfig.h @@ -4,6 +4,8 @@ #include #include +#include "texture_dumper.h" + namespace gltfutil { enum class ui_mode { cli, interactive }; @@ -45,6 +47,9 @@ struct configuration { std::string input_path, output_dir; ui_mode mode; cli_action action = cli_action::not_set; + texture_dumper::texture_output_format requested_format = + texture_dumper::texture_output_format::not_specified; + bool has_output_dir; bool is_valid() { // TODO impl check diff --git a/examples/gltfutil/main.cc b/examples/gltfutil/main.cc index ac5a3f0..ff18538 100644 --- a/examples/gltfutil/main.cc +++ b/examples/gltfutil/main.cc @@ -16,10 +16,12 @@ int usage(int ret = 0) { using std::cout; cout << "gltfutil: tool for manipulating gltf files\n" << " usage information:\n\n" - << "\t gltfutil (-d|-h) [path to .gltf/glb] (-o [path to output " - "directory])\n\n" + << "\t gltfutil (-d|-h|) (-f [png|bmp|tga]) [path to .gltf/glb] (-o " + "[path to output directory])\n\n" //<< "\t\t -i: start in interactive mode\n" << "\t\t -d: dump enclosed content (image assets)\n" + << "\t\t -f: file format for image output" + << "\t\t -o: ouptput directory path" << "\t\t -h: print this help\n"; return ret; } @@ -49,6 +51,11 @@ int parse_args(int argc, char** argv) { i++; config.output_dir = argv[i]; break; + case 'f': + i++; + config.requested_format = + texture_dumper::get_fromat_from_string(argv[i]); + break; default: return arg_error(); } @@ -85,12 +92,19 @@ int parse_args(int argc, char** argv) { switch (config.action) { case cli_action::help: return usage(); + break; + case cli_action::dump: { texture_dumper dumper(model); + if (config.requested_format != + texture_dumper::texture_output_format::not_specified) + dumper.set_output_format(config.requested_format); + if (config.output_dir.empty()) dumper.dump_to_folder(); else dumper.dump_to_folder(config.output_dir); + } break; default: return arg_error(); diff --git a/examples/gltfutil/texture_dumper.cc b/examples/gltfutil/texture_dumper.cc index 6e4f01a..6627040 100644 --- a/examples/gltfutil/texture_dumper.cc +++ b/examples/gltfutil/texture_dumper.cc @@ -3,6 +3,8 @@ #include "stb_image_write.h" #include "texture_dumper.h" +#include + using namespace gltfutil; using namespace tinygltf; using std::cout; @@ -15,7 +17,8 @@ texture_dumper::texture_dumper(const Model& input) void texture_dumper::dump_to_folder(const std::string& path) { cout << "dumping to folder " << path << '\n'; cout << "model file has " << model.textures.size() << " textures.\n"; - size_t index; + size_t index = 0; + for (const auto& texture : model.textures) { index++; const auto& image = model.images[texture.source]; @@ -23,6 +26,7 @@ void texture_dumper::dump_to_folder(const std::string& path) { cout << "image size is: " << image.width << 'x' << image.height << '\n'; cout << "pixel channel count :" << image.component << '\n'; std::string name = image.name.empty() ? std::to_string(index) : image.name; + switch (configured_format) { case texture_output_format::png: name = path + "/" + name + ".png"; @@ -45,3 +49,19 @@ void texture_dumper::dump_to_folder(const std::string& path) { } } } + +void texture_dumper::set_output_format(texture_output_format format) { + configured_format = format; +} + +texture_dumper::texture_output_format texture_dumper::get_fromat_from_string( + const std::string& str) { + std::string type = str; + std::transform(str.begin(), str.end(), type.begin(), ::tolower); + + if (type == "png") return texture_output_format::png; + if (type == "bmp") return texture_output_format::bmp; + if (type == "tga") return texture_output_format::tga; + + return texture_output_format::not_specified; +} diff --git a/examples/gltfutil/texture_dumper.h b/examples/gltfutil/texture_dumper.h index 809b43a..bd30318 100644 --- a/examples/gltfutil/texture_dumper.h +++ b/examples/gltfutil/texture_dumper.h @@ -16,5 +16,8 @@ class texture_dumper { public: texture_dumper(const tinygltf::Model& inputModel); void dump_to_folder(const std::string& path = "./"); + void set_output_format(texture_output_format format); + + static texture_output_format get_fromat_from_string(const std::string& str); }; } // namespace gltfutil