-
Notifications
You must be signed in to change notification settings - Fork 0
/
jplstran_options.cpp
137 lines (127 loc) · 4.8 KB
/
jplstran_options.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) Mathieu Malaterre
// SPDX-License-Identifier: BSD-3-Clause
#include "jplstran_options.h"
#include "tuple.h"
#include "version.h"
#include <charls/charls.h>
#include <boost/program_options.hpp>
#include <iostream>
namespace jlst {
bool tran_options::process(int argc, char* argv[])
{
typedef tuple<int, 4> region_type; // region type
namespace po = boost::program_options;
{
po::options_description desc("Allowed options");
std::vector<std::string> inputs{};
std::vector<std::string> outputs{};
region_type region_tuple;
std::string flip;
// by default unix_style includes `allow_guessing`, so that user can use abbreviation:
desc.add_options()("help,h", "print usage message") // help
("version", "print version") // version
("input,i", po::value(&inputs) /*->required()*/, "inputs. Required.") // input
("output,o", po::value(&outputs) /*->required()*/, "outputs.") // output
("crop", po::value(®ion_tuple), "crop WxH+X+Y") // crop
("flip", po::value(&flip), "flip horizontal|vertical") // flip
("rotate", po::value(°ree), "rotate 90|180|270") // rotate
("transpose", "transpose") // transpose
("transverse", "transverse") // transverse
("wipe", po::value(®ion_tuple), "wipe WxH+X+Y") // crop
("jai_imageio", po::value(&jai_imageio),
"Fix JPEG-LS header (JAI-ImageIO bug)") // JAI-ImageIO
("standard_spiff_header", po::value(&standard_spiff_header),
"Write a standard spiff header: 'yes'/'no'.") // spiff header
;
po::positional_options_description p;
// do not allow more than one positional arg for now:
p.add("input", 1);
p.add("output", 1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
if (vm.count("help"))
{
std::cout << "usage: jplstran [options] input.jls output.jls\n";
std::cout << desc << std::endl;
return false;
}
if (vm.count("version"))
{
std::cout << "jplstran version: " << JLST_VERSION << "\n";
std::cout << "charls version: " << charls_get_version_string() << std::endl;
return false;
}
// https://stackoverflow.com/questions/5395503/required-and-optional-arguments-using-boost-library-program-options
try
{
po::notify(vm);
if (vm.count("input"))
{
add_inputs(inputs);
}
else
{
add_stdin_input();
}
if (vm.count("output"))
{
add_outputs(outputs);
}
else
{
add_stdout_output(false);
}
}
catch (std::exception&)
{
// default value missing. Let's print usage before re-throw:
std::cout << "usage: jplstran [options] input.jls output.jls\n";
std::cout << desc << std::endl;
throw;
}
if (vm.count("crop"))
{
type = transform_type::crop;
region.Width = region_tuple.values[0];
region.Height = region_tuple.values[1];
region.X = region_tuple.values[2];
region.Y = region_tuple.values[3];
}
else if (vm.count("flip"))
{
type = transform_type::flip;
if (flip == "vertical")
{
vertical = true;
}
else if (flip == "horizontal")
{
vertical = false;
}
else
{
throw std::runtime_error("flip:" + flip);
}
}
else if (vm.count("rotate"))
{
type = transform_type::rotate;
if (degree != 90 && degree != 180 && degree != 270)
throw std::runtime_error("rotate:" + degree);
}
else if (vm.count("transpose"))
type = transform_type::transpose;
else if (vm.count("transverse"))
type = transform_type::transverse;
else if (vm.count("wipe"))
{
type = transform_type::wipe;
region.Width = region_tuple.values[0];
region.Height = region_tuple.values[1];
region.X = region_tuple.values[2];
region.Y = region_tuple.values[3];
}
} // namespace boost::program_options;
return true;
}
} // namespace jlst