Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Convert iconvert.cpp stream io and sprintf to modern #3925

Merged
merged 2 commits into from
Jul 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 48 additions & 44 deletions src/iconvert/iconvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


using namespace OIIO;

using OIIO::Strutil::print;
lgritz marked this conversation as resolved.
Show resolved Hide resolved

static std::string uninitialized = "uninitialized \001 HHRU dfvAS: efjl";
static std::string dataformatname = "";
Expand Down Expand Up @@ -97,13 +97,13 @@ getargs(int argc, char* argv[])
"--sRGB", &sRGB, "This file is in sRGB color space",
"--separate", &separate, "Force planarconfig separate",
"--contig", &contig, "Force planarconfig contig",
"--no-clobber", &noclobber, "Do no overwrite existing files",
"--no-clobber", &noclobber, "Do not overwrite existing files",
//FIXME "-z", &zfile, "Treat input as a depth file",
//FIXME "-c %s", &channellist, "Restrict/shuffle channels",
nullptr);
// clang-format on
if (ap.parse(argc, (const char**)argv) < 0) {
std::cerr << ap.geterror() << std::endl;
print(stderr, "{}\n", ap.geterror());
ap.usage();
ap.abort();
return_code = EXIT_FAILURE;
Expand All @@ -117,23 +117,25 @@ getargs(int argc, char* argv[])
}

if (filenames.size() != 2 && !inplace) {
std::cerr
<< "iconvert: Must have both an input and output filename specified.\n";
print(
stderr,
"iconvert: Must have both an input and output filename specified.\n");
ap.usage();
ap.abort();
return_code = EXIT_FAILURE;
return;
}
if (filenames.size() == 0 && inplace) {
std::cerr << "iconvert: Must have at least one filename\n";
print(stderr, "iconvert: Must have at least one filename\n");
ap.usage();
ap.abort();
return_code = EXIT_FAILURE;
return;
}
if (((int)rotcw + (int)rotccw + (int)rot180 + (orientation > 0)) > 1) {
std::cerr
<< "iconvert: more than one of --rotcw, --rotccw, --rot180, --orientation\n";
print(
stderr,
"iconvert: more than one of --rotcw, --rotccw, --rot180, --orientation\n");
ap.usage();
ap.abort();
return_code = EXIT_FAILURE;
Expand Down Expand Up @@ -312,14 +314,15 @@ static bool
convert_file(const std::string& in_filename, const std::string& out_filename)
{
if (noclobber && Filesystem::exists(out_filename)) {
std::cerr << "iconvert ERROR: Output file already exists \""
<< out_filename << "\"\n";
print(stderr, "iconvert ERROR: Output file already exists \"{}\"\n",
out_filename);
return false;
}

if (verbose)
std::cout << "Converting " << in_filename << " to " << out_filename
<< "\n";
if (verbose) {
print("Converting {} to {}\n", in_filename, out_filename);
fflush(stdout);
}

std::string tempname = out_filename;
if (tempname == in_filename) {
Expand All @@ -330,11 +333,10 @@ convert_file(const std::string& in_filename, const std::string& out_filename)
auto in = ImageInput::open(in_filename);
if (!in) {
std::string err = geterror();
std::cerr << "iconvert ERROR: "
<< (err.length() ? err
: Strutil::sprintf("Could not open \"%s\"",
in_filename))
<< "\n";
print(stderr, "iconvert ERROR: {}\n",
(err.length() ? err
: Strutil::fmt::format("Could not open \"{}\"",
in_filename)));
return false;
}
ImageSpec inspec = in->spec();
Expand All @@ -343,9 +345,10 @@ convert_file(const std::string& in_filename, const std::string& out_filename)
// Find an ImageIO plugin that can open the output file, and open it
auto out = ImageOutput::create(tempname);
if (!out) {
std::cerr
<< "iconvert ERROR: Could not find an ImageIO plugin to write \""
<< out_filename << "\" : " << geterror() << "\n";
print(
stderr,
"iconvert ERROR: Could not find an ImageIO plugin to write \"{}\": {}\n",
out_filename, geterror());
return false;
}

Expand Down Expand Up @@ -375,9 +378,10 @@ convert_file(const std::string& in_filename, const std::string& out_filename)
for (int subimage = 0; ok && in->seek_subimage(subimage, 0, inspec);
++subimage) {
if (subimage > 0 && !out->supports("multiimage")) {
std::cerr << "iconvert WARNING: " << out->format_name()
<< " does not support multiple subimages.\n";
std::cerr << "\tOnly the first subimage has been copied.\n";
print(stderr,
"iconvert WARNING: {} does not support multiple subimages.\n"
"\tOnly the first subimage has been copied.\n",
out->format_name());
break; // we're done
}

Expand All @@ -396,16 +400,17 @@ convert_file(const std::string& in_filename, const std::string& out_filename)
mode = ImageOutput::AppendSubimage; // use if we must
if (!mip_to_subimage_warning
&& strcmp(out->format_name(), "tiff")) {
std::cerr << "iconvert WARNING: " << out->format_name()
<< " does not support MIPmaps.\n";
std::cerr
<< "\tStoring the MIPmap levels in subimages.\n";
print(stderr,
"iconvert WARNING: {} does not support MIPmaps.\n"
"\tStoring the MIPmap levels in subimages.\n",
out->format_name());
}
mip_to_subimage_warning = true;
} else {
std::cerr << "iconvert WARNING: " << out->format_name()
<< " does not support MIPmaps.\n";
std::cerr << "\tOnly the first level has been copied.\n";
print(stderr,
"iconvert WARNING: {} does not support MIPmaps.\n"
"\tOnly the first level has been copied.\n",
out->format_name());
break; // on to the next subimage
}
ok = out->open(tempname.c_str(), outspec, mode);
Expand All @@ -424,12 +429,11 @@ convert_file(const std::string& in_filename, const std::string& out_filename)
}
if (!ok) {
std::string err = out->geterror();
std::cerr << "iconvert ERROR: "
<< (err.length()
? err
: Strutil::sprintf("Could not open \"%s\"",
out_filename))
<< "\n";
print(stderr, "iconvert ERROR: {}\n",
(err.length()
? err
: Strutil::fmt::format("Could not open \"{}\"",
out_filename)));
ok = false;
break;
}
Expand All @@ -448,23 +452,23 @@ convert_file(const std::string& in_filename, const std::string& out_filename)
if (!nocopy) {
ok = out->copy_image(in.get());
if (!ok)
std::cerr << "iconvert ERROR copying \"" << in_filename
<< "\" to \"" << out_filename << "\" :\n\t"
<< out->geterror() << "\n";
print(stderr,
"iconvert ERROR copying \"{}\" to \"{}\" :\n\t{}\n",
in_filename, out_filename, out->geterror());
} else {
// Need to do it by hand for some reason. Future expansion in which
// only a subset of channels are copied, or some such.
std::vector<char> pixels((size_t)outspec.image_bytes(true));
ok = in->read_image(subimage, miplevel, 0, outspec.nchannels,
outspec.format, &pixels[0]);
if (!ok) {
std::cerr << "iconvert ERROR reading \"" << in_filename
<< "\" : " << in->geterror() << "\n";
print(stderr, "iconvert ERROR reading \"{}\": {}\n",
in_filename, in->geterror());
} else {
ok = out->write_image(outspec.format, &pixels[0]);
if (!ok)
std::cerr << "iconvert ERROR writing \"" << out_filename
<< "\" : " << out->geterror() << "\n";
print(stderr, "iconvert ERROR writing \"{}\": {}\n",
out_filename, out->geterror());
}
}

Expand Down
Loading