-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
95 lines (76 loc) · 2.78 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "logging.hpp"
#include "transmogrify.hpp"
#include <Magick++.h>
#include <boost/program_options.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
namespace po = boost::program_options;
std::string
generateUsage(const char *progName, const po::options_description &options) {
std::ostringstream ss;
ss << "Usage: " << progName << " [options] INPUT_IMAGE OUTPUT_IMAGE\n" << options << std::endl;
return ss.str();
}
int
main (int argc, char *argv[])
{
transmogrifier::init();
transmogrifier::programName = argv[0];
po::options_description genericOptions("Allowed options");
genericOptions.add_options()
("version,V", "print program version")
("help,h", "print usage")
("input-image", po::value<std::string>()->required(), "image to be transmogrified")
("output-image", po::value<std::string>()->required(), "location of transmogrified image")
("output-format,f", po::value<std::string>(), "format for output image ONLY if writing to stdout")
("iterations,i", po::value<unsigned>()->default_value(8), "number of iterations")
;
po::positional_options_description positionalOptions;
positionalOptions.add("input-image", 1);
positionalOptions.add("output-image", 2);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv)
.options(genericOptions)
.positional(positionalOptions)
.run(), vm);
if (vm.count("help")) {
std::cout << generateUsage(argv[0], genericOptions);
return 0;
}
if (vm.count("version")) {
std::cout << "Transmogrify 0.1" << std::endl;
return 0;
}
try {
po::notify(vm);
}
catch(po::error& e) {
std::cerr << e.what() << "\n\n" << generateUsage(argv[0], genericOptions);
return 1;
}
std::string inputImgName = vm["input-image"].as<std::string>();
std::string outputImgName = vm["output-image"].as<std::string>();
unsigned iterations = vm["iterations"].as<unsigned>();
if (outputImgName == "-" && vm.count("output-format") == 0) {
transmogrifier::error() << "Must pass `--output-format' when output is sent to stdout (`--output-image' is \"-\")" << std::endl;
return 1;
}
if (outputImgName != "-" && vm.count("output-format") > 0) {
transmogrifier::error() << "Must pass `--output-format' ONLY when output is sent to stdout (`--output-image' is \"-\")" << std::endl;
return 1;
}
if (inputImgName == "-") {
if (outputImgName == "-")
transmogrifier::streamToStream(std::cin, std::cout, vm["output-format"].as<std::string>(), iterations);
else
transmogrifier::streamToNamedFile(std::cin, outputImgName, iterations);
}
else {
if (outputImgName == "-")
transmogrifier::namedFileToStream(inputImgName, std::cout, vm["output-format"].as<std::string>(), iterations);
else
transmogrifier::namedFileToNamedFile(inputImgName, outputImgName, iterations);
}
return 0;
}