Skip to content

Commit

Permalink
heif-convert: output filename is now optional, or one can use '-o'
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 14, 2023
1 parent 3130d8c commit d159f8a
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions examples/heif_convert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,16 @@ static void show_help(const char* argv0)
{
std::cerr << " heif-convert libheif version: " << heif_get_version() << "\n"
<< "-------------------------------------------\n"
"Usage: heif-convert [options] <input-image> <output-image>\n"
"Usage: heif-convert [options] <input-image> [output-image]\n"
"\n"
"The program determines the output file format from the output filename suffix.\n"
"These suffixes are recognized: jpg, jpeg, png, y4m."
"These suffixes are recognized: jpg, jpeg, png, y4m. If no output filename is specified, 'jpg' is used.\n"
"\n"
"Options:\n"
" -h, --help show help\n"
" -v, --version show version\n"
" -q, --quality quality (for JPEG output)\n"
" -o, --output FILENAME write output to FILENAME (optional)\n"
" -d, --decoder ID use a specific decoder (see --list-decoders)\n"
" --with-aux also write auxiliary images (e.g. depth images)\n"
" --with-xmp write XMP metadata to file (output filename with .xmp suffix)\n"
Expand Down Expand Up @@ -115,6 +116,7 @@ int option_with_exif = 0;
int option_skip_exif_offset = 0;
int option_list_decoders = 0;
int option_png_compression_level = -1; // use zlib default
std::string output_filename;

std::string chroma_upsampling;

Expand All @@ -125,6 +127,7 @@ static struct option long_options[] = {
{(char* const) "quality", required_argument, 0, 'q'},
{(char* const) "strict", no_argument, 0, 's'},
{(char* const) "decoder", required_argument, 0, 'd'},
{(char* const) "output", required_argument, 0, 'o'},
{(char* const) "quiet", no_argument, &option_quiet, 1},
{(char* const) "with-aux", no_argument, &option_aux, 1},
{(char* const) "with-xmp", no_argument, &option_with_xmp, 1},
Expand Down Expand Up @@ -226,7 +229,7 @@ int main(int argc, char** argv)
//while ((opt = getopt(argc, argv, "q:s")) != -1) {
while (true) {
int option_index = 0;
int c = getopt_long(argc, argv, "hq:sd:C:v", long_options, &option_index);
int c = getopt_long(argc, argv, "hq:sd:C:vo:", long_options, &option_index);
if (c == -1) {
break;
}
Expand Down Expand Up @@ -273,6 +276,9 @@ int main(int argc, char** argv)
case 'v':
show_version();
return 0;
case 'o':
output_filename = optarg;
break;
}
}

Expand All @@ -281,17 +287,37 @@ int main(int argc, char** argv)
return 0;
}

if (optind + 2 > argc) {
// Need input and output filenames as additional arguments.
if (optind >= argc || optind + 2 < argc) {
// Need at least input filename as additional argument, but not more as two filenames.
show_help(argv[0]);
return 5;
}

std::string input_filename(argv[optind++]);
std::string output_filename(argv[optind++]);
std::string output_filename_stem;
std::string output_filename_suffix;

if (output_filename.empty()) {
if (optind == argc) {
std::string input_stem;
size_t dot_pos = input_filename.rfind('.');
if (dot_pos != std::string::npos) {
input_stem = input_filename.substr(0, dot_pos);
}
else {
input_stem = input_filename;
}

output_filename = input_stem + ".jpg";
}
else if (optind == argc-1) {
output_filename = argv[optind];
}
else {
assert(false);
}
}

std::unique_ptr<Encoder> encoder;

size_t dot_pos = output_filename.rfind('.');
Expand Down

0 comments on commit d159f8a

Please sign in to comment.