-
Notifications
You must be signed in to change notification settings - Fork 664
Expand file tree
/
Copy pathivmain.cpp
More file actions
224 lines (189 loc) · 6.92 KB
/
ivmain.cpp
File metadata and controls
224 lines (189 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright Contributors to the OpenImageIO project.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/AcademySoftwareFoundation/OpenImageIO
#if defined(_MSC_VER)
// Ignore warnings about conditional expressions that always evaluate true
// on a given platform but may evaluate differently on another. There's
// nothing wrong with such conditionals.
// Also ignore warnings about not being able to generate default assignment
// operators for some Qt classes included in headers below.
# pragma warning(disable : 4127 4512)
#endif
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iterator>
#include <QApplication>
#include "imageviewer.h"
#include <OpenImageIO/argparse.h>
#include <OpenImageIO/filesystem.h>
#include <OpenImageIO/imagecache.h>
#include <OpenImageIO/strutil.h>
#include <OpenImageIO/sysutil.h>
#include <OpenImageIO/timer.h>
using namespace OIIO;
static ArgParse
getargs(int argc, char* argv[])
{
ArgParse ap;
// clang-format off
ap.intro("iv -- image viewer\n"
OIIO_INTRO_STRING)
.usage("iv [options] [filename... | dirname...]")
.add_version(OIIO_VERSION_STRING);
ap.arg("filename")
.action(ArgParse::append())
.hidden();
ap.arg("-v")
.help("Verbose status messages")
.dest("verbose");
ap.arg("-F")
.help("Foreground mode")
.dest("foreground_mode")
.store_true();
ap.arg("--no-autopremult")
.help("Turn off automatic premultiplication of images with unassociated alpha")
.store_true();
ap.arg("--rawcolor")
.help("Do not automatically transform to RGB");
ap.arg("--dark")
.help("Start in dark mode")
.dest("dark")
.store_true();
ap.arg("--display")
.help("OCIO display")
.metavar("STRING")
.defaultval("")
.action(ArgParse::store());
ap.arg("--image-color-space")
.help("OCIO image color space")
.metavar("STRING")
.defaultval("")
.action(ArgParse::store());
ap.arg("--view")
.help("OCIO view")
.metavar("STRING")
.defaultval("")
.action(ArgParse::store());
ap.parse(argc, (const char**)argv);
return ap;
// clang-format on
}
#ifdef _MSC_VER
// if we are not in DEBUG mode this code switch the app to
// full windowed mode (no console and no need to define WinMain)
// FIXME: this should be done in CMakeLists.txt but first we have to
// fix Windows Debug build
# ifdef NDEBUG
# pragma comment(linker, "/subsystem:windows /entry:mainCRTStartup")
# endif
#endif
int
main(int argc, char* argv[])
{
// Helpful for debugging to make sure that any crashes dump a stack
// trace.
Sysutil::setup_crash_stacktrace("stdout");
Filesystem::convert_native_arguments(argc, (const char**)argv);
ArgParse ap = getargs(argc, argv);
if (!ap["foreground_mode"].get<int>())
Sysutil::put_in_background();
// LG
// Q_INIT_RESOURCE(iv);
QApplication app(argc, argv);
std::string color_space = ap["image-color-space"].as_string("");
std::string display = ap["display"].as_string("");
std::string view = ap["view"].as_string("");
// std::string look = ap["look"].as_string("");
bool use_ocio = color_space != "" && display != "" && view != "";
std::string ocioenv = Sysutil::getenv("OCIO");
if (ocioenv.empty() || !Filesystem::exists(ocioenv)) {
#ifdef _MSC_VER
_putenv_s("OCIO", "ocio://default");
#else
setenv("OCIO", "ocio://default", 1);
#endif
}
ImageViewer* mainWin = new ImageViewer(use_ocio, color_space, display, view,
ap["dark"].get<int>());
mainWin->show();
// Set up the imagecache with parameters that make sense for iv
auto imagecache = ImageCache::create(true);
imagecache->attribute("autotile", 256);
imagecache->attribute("deduplicate", (int)0);
if (ap["no-autopremult"].get<int>())
imagecache->attribute("unassociatedalpha", 1);
if (ap["rawcolor"].get<int>())
mainWin->rawcolor(true);
QApplication::processEvents(); // Process any pending events
// Make sure we are the top window with the focus.
mainWin->raise();
mainWin->activateWindow();
ustring uexists("exists");
std::vector<std::string> extensionsVector; // Vector to hold all extensions
auto all_extensions = OIIO::get_string_attribute("extension_list");
for (auto oneformat : OIIO::Strutil::splitsv(all_extensions, ";")) {
// Split the extensions by semicolon
auto format_exts = OIIO::Strutil::splitsv(oneformat, ":", 2);
for (auto ext : OIIO::Strutil::splitsv(format_exts[1], ","))
extensionsVector.emplace_back(ext);
}
// Add the images
for (auto& f : ap["filename"].as_vec<std::string>()) {
// Check if the file exists
if (!Filesystem::exists(f)) {
print(stderr, "Error: File or directory does not exist: {}\n", f);
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) {
std::string extension
= Filesystem::extension(file,
false); // Remove the leading dot
Strutil::to_lower(extension);
if (std::find(extensionsVector.begin(), extensionsVector.end(),
extension)
!= extensionsVector.end()) {
int exists = 0;
bool ok = imagecache->get_image_info(ustring(file), 0, 0,
uexists, OIIO::TypeInt,
&exists);
if (ok && exists)
validImages.push_back(file);
}
}
if (validImages.empty()) {
print(stderr, "Error: No valid images found in directory: {}\n",
f);
} else {
// Sort the valid images lexicographically
std::sort(validImages.begin(), validImages.end());
for (auto& validImage : validImages) {
mainWin->add_image(validImage);
}
}
} else {
mainWin->add_image(f);
}
}
mainWin->current_image(0);
int r = app.exec();
// OK to clean up here
int verbose = ap["verbose"].get<int>();
#ifdef NDEBUG
if (verbose)
#endif
{
size_t mem = Sysutil::memory_used(true);
print("iv total memory used: {}\n\n", Strutil::memformat(mem));
print("{}\n", imagecache->getstats(1 + verbose));
}
shutdown();
return r;
}