Skip to content

Commit

Permalink
[2058] Load a directory as an argument and all images inside are adde…
Browse files Browse the repository at this point in the history
…d to the viewer

Signed-off-by: Chaitanya Sharma <39400946+CheeksTheGeek@users.noreply.github.com>
  • Loading branch information
CheeksTheGeek committed Oct 12, 2023
1 parent ab1a0ee commit 2bf1a90
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/iv/ivmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ getargs(int argc, char* argv[])
// clang-format off
ap.intro("iv -- image viewer\n"
OIIO_INTRO_STRING)
.usage("iv [options] [filename...]")
.usage("iv [options] [filename... | dirname...]")
.add_version(OIIO_VERSION_STRING);

ap.arg("filename")
Expand Down Expand Up @@ -108,8 +108,38 @@ main(int argc, char* argv[])
mainWin->activateWindow();

// Add the images
for (auto& f : ap["filename"].as_vec<std::string>())
mainWin->add_image(f);
for (auto& f : ap["filename"].as_vec<std::string>()) {
// Check if the file exists
if (!Filesystem::exists(f)) {
std::cerr << "Error: File or directory does not exist: " << f << "\n";
continue;
}

if (Filesystem::is_directory(f)) {
// If f is a directory, iterate through its files
std::vector<std::string> files;
Filesystem::get_directory_entries(f, files);

std::vector<std::string> validImages; // Vector to hold valid images
for (auto& file : files) {
auto in = ImageInput::open(file);
if (in) {
validImages.push_back(file);
in->close(); // Close the ImageInput object to free resources
}
}

if (validImages.empty()) {
std::cerr << "Error: No valid images found in directory: " << f << "\n";
} else {
for (auto& validImage : validImages) {
mainWin->add_image(validImage);
}
}
} else {
mainWin->add_image(f);
}
}

mainWin->current_image(0);

Expand Down

0 comments on commit 2bf1a90

Please sign in to comment.