Skip to content

Commit

Permalink
feat(idiff): allow users to specify a directory as the 2nd argument (#…
Browse files Browse the repository at this point in the history
…4015)

Teach `idiff` to accept a directory as the 2nd argument.

Fixes #4009

## Tests

Functional tests for `idiff` don't currently exist. Manual testing
verifies it works as advertised.

---------

Signed-off-by: David Aguilar <[email protected]>
  • Loading branch information
davvid authored Oct 12, 2023
1 parent bda5141 commit 0d2e990
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/doc/idiff.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ Using `idiff`

The `idiff` utility is invoked as follows:

`idiff` [*options*] *image1* *image2*
`idiff` [*options*] *input1* *input2|directory*

Where *input1* and *input2* are the names of two image files that should be
compared. They may be of any format recognized by OpenImageIO (i.e., for
which image-reading plugins are available).

When a *directory* is specified instead of *input2* then `idiff` will use
the same-named file as *input1* in the specified directory.

If the two input images are not the same resolutions, or do not have the
same number of channels, the comparison will return FAILURE immediately and
will not attempt to compare the pixels of the two images. If they are the
Expand Down
24 changes: 22 additions & 2 deletions src/idiff/idiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ getargs(int argc, char* argv[])
ArgParse ap;
ap.intro("idiff -- compare two images\n"
OIIO_INTRO_STRING)
.usage("idiff [options] image1 image2")
.usage("idiff [options] <image1> <image2 | directory>")
.add_version(OIIO_VERSION_STRING)
.print_defaults(true);

Expand Down Expand Up @@ -169,6 +169,24 @@ print_subimage(ImageBuf& img0, int subimage, int miplevel)
}


// Append the filename from "first" when "second" is a directory.
// "second" is an output variable and modified in-place.
inline void
add_filename_to_directory(const std::string& first, std::string& second)
{
if (Filesystem::is_directory(second)) {
char last_byte = second.at(second.size() - 1);
if (last_byte != '/' && last_byte != '\\') {
#if defined(_MSC_VER)
second += '\\';
#else
second += '/';
#endif
}
second += Filesystem::filename(first);
}
}


int
main(int argc, char* argv[])
Expand All @@ -181,7 +199,9 @@ main(int argc, char* argv[])
ArgParse ap = getargs(argc, argv);

std::vector<std::string> filenames = ap["filename"].as_vec<std::string>();
if (filenames.size() != 2) {
if (filenames.size() == 2) {
add_filename_to_directory(filenames[0], filenames[1]);
} else {
print(stderr, "idiff: Must have two input filenames.\n");
print(stderr, "> {}\n", Strutil::join(filenames, ", "));
ap.usage();
Expand Down

0 comments on commit 0d2e990

Please sign in to comment.