Skip to content

Commit ef27d5c

Browse files
author
Stefan Reinhard
committed
Make ouput path optional, support piping (#18)
1 parent b9e7e94 commit ef27d5c

3 files changed

Lines changed: 32 additions & 12 deletions

File tree

src/imaging.hpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,17 @@ VImage createVipsThumbnail(VImage& img, Opts& options)
6161
* @param fileName
6262
* @param options
6363
*/
64-
void saveImage(VImage& img, const string& fileName, Opts& options)
64+
void saveOutputImageToFile(VImage &img, Opts &options)
6565
{
66+
if (options.outputPath.empty()) {
67+
throw new logic_error("Can't save to file without 'outputPath' option");
68+
}
69+
6670
chrono::steady_clock::time_point begin_buildImage = chrono::steady_clock::now();
6771

68-
char * outName = const_cast<char *>(fileName.c_str());
72+
char * outName = const_cast<char *>(options.outputPath.c_str());
6973

70-
string ext = fileName.substr(fileName.find_last_of('.') + 1);
74+
string ext = options.outputPath.substr(options.outputPath.find_last_of('.') + 1);
7175

7276
// Supported image output formats
7377
set<string> jpgExt = {"jpg", "jpeg", "JPG", "JPEG"};
@@ -95,4 +99,13 @@ void saveImage(VImage& img, const string& fileName, Opts& options)
9599
}
96100
}
97101

102+
void printOutputImageToStdout(VImage& img, Opts& options)
103+
{
104+
VipsBlob* jpegBuffer = img.jpegsave_buffer(VImage::option()->set("Q", options.quality));
105+
106+
cout.write(static_cast<const char *>(jpegBuffer->area.data), jpegBuffer->area.length);
107+
108+
cout.flush();
109+
}
110+
98111
#endif

src/main.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void sanityCheck(const string& inputFilename) {
3434
* @param options
3535
* @return
3636
*/
37-
int convert(const string& inputFilename, const string& outputFilename, Opts& options)
37+
int convert(const string& inputFilename, Opts& options)
3838
{
3939
sanityCheck(inputFilename);
4040

@@ -87,7 +87,12 @@ int convert(const string& inputFilename, const string& outputFilename, Opts& opt
8787
image = createVipsThumbnail(image, options);
8888
}
8989

90-
saveImage(image, outputFilename, options);
90+
if (!options.outputPath.empty()) {
91+
saveOutputImageToFile(image, options);
92+
}
93+
else {
94+
printOutputImageToStdout(image, options);
95+
}
9196

9297
vips_shutdown();
9398

@@ -98,6 +103,8 @@ Opts getTifigOptions(cxxopts::Options& options)
98103
{
99104
Opts opts = {};
100105

106+
if (options.count("output"))
107+
opts.outputPath = options["output"].as<string>();
101108
if (options.count("width"))
102109
opts.width = options["width"].as<int>();
103110
if (options.count("height"))
@@ -110,7 +117,7 @@ Opts getTifigOptions(cxxopts::Options& options)
110117
opts.parallel = true;
111118
if (options.count("thumbnail"))
112119
opts.thumbnail = true;
113-
if (options.count("verbose"))
120+
if (options.count("verbose") && !opts.outputPath.empty())
114121
opts.verbose = true;
115122

116123
return opts;
@@ -132,7 +139,7 @@ int main(int argc, char* argv[])
132139

133140
cxxopts::Options options(argv[0], "Converts iOS 11 HEIC images to practical formats");
134141

135-
options.positional_help("input_file output_file");
142+
options.positional_help("input_file [output_file]");
136143

137144
options.parse_positional(vector<string>{"input", "output"});
138145

@@ -155,15 +162,14 @@ int main(int argc, char* argv[])
155162
if (options.count("version")) {
156163
printVersion();
157164
retval = 0;
158-
} else if (options.count("input") && options.count("output")) {
165+
} else if (options.count("input")) {
159166
string inputFileName = options["input"].as<string>();
160-
string outputFileName = options["output"].as<string>();
161167

162168
Opts tifigOptions = getTifigOptions(options);
163169

164170
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
165171

166-
retval = convert(inputFileName, outputFileName, tifigOptions);
172+
retval = convert(inputFileName, tifigOptions);
167173

168174
chrono::steady_clock::time_point end = chrono::steady_clock::now();
169175
long duration = chrono::duration_cast<chrono::milliseconds>(end - begin).count();
@@ -172,11 +178,11 @@ int main(int argc, char* argv[])
172178
cout << "Total Time: " << duration << "ms" << endl;
173179
}
174180
} else {
175-
cout << options.help() << endl;
181+
cerr << options.help() << endl;
176182
}
177183
}
178184
catch (const cxxopts::OptionException& oe) {
179-
cout << "error parsing options: " << oe.what() << endl;
185+
cerr << "error parsing options: " << oe.what() << endl;
180186
}
181187
catch (const FileReaderException& fre) {
182188
cerr << "Could not read HEIF image: " << fre.what() << endl;

src/types.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct RgbData
1818

1919
struct Opts
2020
{
21+
std::string outputPath = "";
2122
int width = 0;
2223
int height = 0;
2324
int quality = 90;

0 commit comments

Comments
 (0)