Skip to content

Commit 8d04dae

Browse files
feat(iv): Implement Directory Argument Loading for iv (#4010)
Solves the issue "Load a directory: iv dirname ought to be the same as specifying all the image files in that directory" from the Great Big iv Wish List #2058 This PR addresses the request from the Master list of iv feature requests to allow loading a directory as an argument, which in turn loads all images within the specified directory. The code modifications include checks to validate the file or directory existence, and if a directory is provided, iterates through its files to load all valid images into iv. This enhancement simplifies the process of viewing multiple images collectively located in a single directory. --------- Signed-off-by: Chaitanya Sharma <39400946+CheeksTheGeek@users.noreply.github.com>
1 parent 0d2e990 commit 8d04dae

1 file changed

Lines changed: 45 additions & 3 deletions

File tree

src/iv/ivmain.cpp

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ getargs(int argc, char* argv[])
3939
// clang-format off
4040
ap.intro("iv -- image viewer\n"
4141
OIIO_INTRO_STRING)
42-
.usage("iv [options] [filename...]")
42+
.usage("iv [options] [filename... | dirname...]")
4343
.add_version(OIIO_VERSION_STRING);
4444

4545
ap.arg("filename")
@@ -107,9 +107,51 @@ main(int argc, char* argv[])
107107
mainWin->raise();
108108
mainWin->activateWindow();
109109

110+
ustring uexists("exists");
111+
std::vector<std::string> extensionsVector; // Vector to hold all extensions
112+
auto all_extensions = OIIO::get_string_attribute("extension_list");
113+
for (auto oneformat : OIIO::Strutil::splitsv(all_extensions, ";")) { // Split the extensions by semicolon
114+
auto format_exts = OIIO::Strutil::splitsv(oneformat, ":", 2);
115+
for (auto ext : OIIO::Strutil::splitsv(format_exts[1], ","))
116+
extensionsVector.emplace_back(ext);
117+
}
118+
110119
// Add the images
111-
for (auto& f : ap["filename"].as_vec<std::string>())
112-
mainWin->add_image(f);
120+
for (auto& f : ap["filename"].as_vec<std::string>()) {
121+
// Check if the file exists
122+
if (!Filesystem::exists(f)) {
123+
std::cerr << "Error: File or directory does not exist: " << f << "\n";
124+
continue;
125+
}
126+
127+
if (Filesystem::is_directory(f)) {
128+
// If f is a directory, iterate through its files
129+
std::vector<std::string> files;
130+
Filesystem::get_directory_entries(f, files);
131+
132+
std::vector<std::string> validImages; // Vector to hold valid images
133+
for (auto& file : files) {
134+
std::string extension = Filesystem::extension(file).substr(1); // Remove the leading dot
135+
if (std::find(extensionsVector.begin(), extensionsVector.end(), extension) != extensionsVector.end()) {
136+
int exists = 0;
137+
bool ok = imagecache->get_image_info(ustring(file), 0, 0, uexists, OIIO::TypeInt, &exists);
138+
if (ok && exists)
139+
validImages.push_back(file);
140+
}
141+
}
142+
143+
if (validImages.empty()) {
144+
std::cerr << "Error: No valid images found in directory: " << f << "\n";
145+
} else {
146+
std::sort(validImages.begin(), validImages.end()); // Sort the valid images lexicographically
147+
for (auto& validImage : validImages) {
148+
mainWin->add_image(validImage);
149+
}
150+
}
151+
} else {
152+
mainWin->add_image(f);
153+
}
154+
}
113155

114156
mainWin->current_image(0);
115157

0 commit comments

Comments
 (0)