Skip to content

Commit

Permalink
Make ouput path optional, support piping (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Reinhard committed Dec 10, 2017
1 parent b9e7e94 commit ef27d5c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
19 changes: 16 additions & 3 deletions src/imaging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ VImage createVipsThumbnail(VImage& img, Opts& options)
* @param fileName
* @param options
*/
void saveImage(VImage& img, const string& fileName, Opts& options)
void saveOutputImageToFile(VImage &img, Opts &options)
{
if (options.outputPath.empty()) {
throw new logic_error("Can't save to file without 'outputPath' option");
}

chrono::steady_clock::time_point begin_buildImage = chrono::steady_clock::now();

char * outName = const_cast<char *>(fileName.c_str());
char * outName = const_cast<char *>(options.outputPath.c_str());

string ext = fileName.substr(fileName.find_last_of('.') + 1);
string ext = options.outputPath.substr(options.outputPath.find_last_of('.') + 1);

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

void printOutputImageToStdout(VImage& img, Opts& options)
{
VipsBlob* jpegBuffer = img.jpegsave_buffer(VImage::option()->set("Q", options.quality));

cout.write(static_cast<const char *>(jpegBuffer->area.data), jpegBuffer->area.length);

cout.flush();
}

#endif
24 changes: 15 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void sanityCheck(const string& inputFilename) {
* @param options
* @return
*/
int convert(const string& inputFilename, const string& outputFilename, Opts& options)
int convert(const string& inputFilename, Opts& options)
{
sanityCheck(inputFilename);

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

saveImage(image, outputFilename, options);
if (!options.outputPath.empty()) {
saveOutputImageToFile(image, options);
}
else {
printOutputImageToStdout(image, options);
}

vips_shutdown();

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

if (options.count("output"))
opts.outputPath = options["output"].as<string>();
if (options.count("width"))
opts.width = options["width"].as<int>();
if (options.count("height"))
Expand All @@ -110,7 +117,7 @@ Opts getTifigOptions(cxxopts::Options& options)
opts.parallel = true;
if (options.count("thumbnail"))
opts.thumbnail = true;
if (options.count("verbose"))
if (options.count("verbose") && !opts.outputPath.empty())
opts.verbose = true;

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

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

options.positional_help("input_file output_file");
options.positional_help("input_file [output_file]");

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

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

Opts tifigOptions = getTifigOptions(options);

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

retval = convert(inputFileName, outputFileName, tifigOptions);
retval = convert(inputFileName, tifigOptions);

chrono::steady_clock::time_point end = chrono::steady_clock::now();
long duration = chrono::duration_cast<chrono::milliseconds>(end - begin).count();
Expand All @@ -172,11 +178,11 @@ int main(int argc, char* argv[])
cout << "Total Time: " << duration << "ms" << endl;
}
} else {
cout << options.help() << endl;
cerr << options.help() << endl;
}
}
catch (const cxxopts::OptionException& oe) {
cout << "error parsing options: " << oe.what() << endl;
cerr << "error parsing options: " << oe.what() << endl;
}
catch (const FileReaderException& fre) {
cerr << "Could not read HEIF image: " << fre.what() << endl;
Expand Down
1 change: 1 addition & 0 deletions src/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct RgbData

struct Opts
{
std::string outputPath = "";
int width = 0;
int height = 0;
int quality = 90;
Expand Down

0 comments on commit ef27d5c

Please sign in to comment.