Skip to content

Commit 32af55f

Browse files
committed
Add basic Archive flags for configuring archiving
Signed-off-by: Kevin Wheatley <kevin.wheatley@framestore.com>
1 parent e39c485 commit 32af55f

File tree

12 files changed

+93
-13
lines changed

12 files changed

+93
-13
lines changed

docs/api/constants.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ These environmental variables are used by the OpenColorIO library
4747
Ex: OCIO_OPTIMIZATION_FLAGS="20479" or "0x4FFF" for
4848
OPTIMIZATION_LOSSLESS.
4949

50+
.. data:: PyOpenColorIO.OCIO_ARCHIVE_FLAGS_ENVVAR
51+
52+
The envvar 'OCIO_ARCHIVE_FLAGS' provides a way to modify the parameters
53+
used to create an ocioz archive. Remove the variable or set the value
54+
to empty to not use it. Set the value of the variable to the desired
55+
parameters as either an integer or hexadecimal value.
56+
Ex: OCIO_ARCHIVE_FLAGS="256" or "0x0100" for ARCHIVE_FLAGS_MINIMAL.
57+
5058
.. group-tab:: C++
5159

5260
.. doxygengroup:: VarsEnvvar

include/OpenColorIO/OpenColorIO.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1524,15 +1524,18 @@ class OCIOEXPORT Config
15241524
* trying to resolve all the FileTransforms in the Config to specific files is because of the
15251525
* goal to allow context variables to continue to work.
15261526
*
1527+
* Archiving a minimal Config will try resolve these file references using the current context.
1528+
*
15271529
* If a Config is created with CreateFromStream, CreateFromFile with an OCIOZ archive, or
15281530
* CreateFromConfigIOProxy, it cannot be archived unless the working directory is manually set
15291531
* to a directory that contains any necessary LUT files.
15301532
*
15311533
* The provided output stream must be closed by the caller, if necessary (e.g., an ofstream).
15321534
*
15331535
* \param ostream The output stream to write to.
1536+
* \param flags Flags top control archive creation
15341537
*/
1535-
void archive(std::ostream & ostream) const;
1538+
void archive(std::ostream & ostream, ArchiveFlags flags) const;
15361539

15371540
Config(const Config &) = delete;
15381541
Config& operator= (const Config &) = delete;

include/OpenColorIO/OpenColorTypes.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,15 @@ extern OCIOEXPORT const char * OCIO_INACTIVE_COLORSPACES_ENVVAR;
774774
*/
775775
extern OCIOEXPORT const char * OCIO_OPTIMIZATION_FLAGS_ENVVAR;
776776

777+
/**
778+
* The envvar 'OCIO_OPTIMIZATION_FLAGS' provides a way to force a given
779+
* optimization level. Remove the variable or set the value to empty to
780+
* not use it. Set the value of the variable to the desired optimization
781+
* level as either an integer or hexadecimal value.
782+
* Ex: OCIO_OPTIMIZATION_FLAGS="1" or "0x01" for ARCHIVE_FLAGS_MINIMAL.
783+
*/
784+
extern OCIOEXPORT const char * OCIO_ARCHIVE_FLAGS_ENVVAR;
785+
777786
/**
778787
* The envvar 'OCIO_USER_CATEGORIES' allows the end-user to filter color spaces shown by
779788
* applications. Only color spaces that include at least one of the supplied categories will be
@@ -955,6 +964,28 @@ extern OCIOEXPORT const char * OCIO_CONFIG_DEFAULT_NAME;
955964
extern OCIOEXPORT const char * OCIO_CONFIG_DEFAULT_FILE_EXT;
956965
extern OCIOEXPORT const char * OCIO_CONFIG_ARCHIVE_FILE_EXT;
957966

967+
//!cpp:type:: Enum to control the behavior of archiving a :cpp:class:`Config`.
968+
//
969+
// TODO: extend docs
970+
// TODO: Python bindings
971+
// TODO: environment variable?
972+
//
973+
enum ArchiveFlags : unsigned long
974+
{
975+
ARCHIVE_FLAGS_NONE = 0x0000,
976+
977+
ARCHIVE_FLAGS_FAST_COMPRESSION = 0x02, // Compression level
978+
ARCHIVE_FLAGS_NORMAL_COMPRESSION = 0x06, // Compression level
979+
ARCHIVE_FLAGS_BEST_COMPRESSION = 0x09, // Compression level
980+
ARCHIVE_FLAGS_COMPRESSION_MASK = 0x0F, // Compression level mask
981+
982+
ARCHIVE_FLAGS_MINIMAL = 0x0100, // Generate a Minimal Archive
983+
984+
ARCHIVE_FLAGS_DEFAULT = (ARCHIVE_FLAGS_NONE | ARCHIVE_FLAGS_BEST_COMPRESSION),
985+
986+
ARCHIVE_FLAGS_SAFEST = (ARCHIVE_FLAGS_NONE | ARCHIVE_FLAGS_BEST_COMPRESSION) // TODO: name is poor
987+
};
988+
958989
// Built-in config feature
959990
// URI Prefix
960991
extern OCIOEXPORT const char * OCIO_BUILTIN_URI_PREFIX;

src/OpenColorIO/Config.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const char * OCIO_ACTIVE_DISPLAYS_ENVVAR = "OCIO_ACTIVE_DISPLAYS";
4949
const char * OCIO_ACTIVE_VIEWS_ENVVAR = "OCIO_ACTIVE_VIEWS";
5050
const char * OCIO_INACTIVE_COLORSPACES_ENVVAR = "OCIO_INACTIVE_COLORSPACES";
5151
const char * OCIO_OPTIMIZATION_FLAGS_ENVVAR = "OCIO_OPTIMIZATION_FLAGS";
52+
const char * OCIO_ARCHIVE_FLAGS_ENVVAR = "OCIO_ARCHIVE_FLAGS";
5253
const char * OCIO_USER_CATEGORIES_ENVVAR = "OCIO_USER_CATEGORIES";
5354

5455
// Default filename (with extension) of a config and archived config.
@@ -5559,7 +5560,7 @@ bool Config::isArchivable() const
55595560
// 2) Path may not start with double dot ".." (going above working directory).
55605561
pystring::startswith(normPath, "..") ||
55615562
// 3) A context variable may not be located at the start of the path.
5562-
(ContainsContextVariables(path) &&
5563+
(ContainsContextVariables(path) && //TODO: if we can resolve context, do so and validate path to file
55635564
(StringUtils::Find(path, "$") == 0 ||
55645565
StringUtils::Find(path, "%") == 0)))
55655566
{
@@ -5605,10 +5606,10 @@ bool Config::isArchivable() const
56055606
return true;
56065607
}
56075608

5608-
void Config::archive(std::ostream & ostream) const
5609+
void Config::archive(std::ostream & ostream, ArchiveFlags flags) const
56095610
{
56105611
// Using utility functions in OCIOZArchive.cpp.
5611-
archiveConfig(ostream, *this, getCurrentContext()->getWorkingDir());
5612+
archiveConfig(ostream, *this, getCurrentContext()->getWorkingDir(), flags);
56125613
}
56135614

56145615
} // namespace OCIO_NAMESPACE

src/OpenColorIO/OCIOZArchive.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,28 @@ void addSupportedFiles(void * archiver, const char * path, const char * configWo
202202
}
203203
//////////////////////////////////////////////////////////////////////////////////////
204204

205-
void archiveConfig(std::ostream & ostream, const Config & config, const char * configWorkingDirectory)
205+
ArchiveFlags EnvironmentOverride(ArchiveFlags oFlags) // TODO: test override
206+
{
207+
const std::string envFlag = GetEnvVariable(OCIO_ARCHIVE_FLAGS_ENVVAR);
208+
if (!envFlag.empty())
209+
{
210+
// Use 0 to allow base to be determined by the format.
211+
oFlags = static_cast<ArchiveFlags>(std::stoul(envFlag, nullptr, 0));
212+
}
213+
return oFlags;
214+
}
215+
216+
void archiveConfig(std::ostream & ostream, const Config & config, const char * configWorkingDirectory, ArchiveFlags flags)
206217
{
207218
void * archiver = nullptr;
208219
void *write_mem_stream = NULL;
209220
const uint8_t *buffer_ptr = NULL;
210221
int32_t buffer_size = 0;
211222
mz_zip_file file_info;
212223

213-
if (!config.isArchivable())
224+
flags = EnvironmentOverride(flags);
225+
226+
if (!config.isArchivable()) // TODO: pass in flags?
214227
{
215228
std::ostringstream os;
216229
os << "Config is not archivable.";
@@ -238,8 +251,10 @@ void archiveConfig(std::ostream & ostream, const Config & config, const char * c
238251
ArchiveOptions options;
239252
// Make sure that the compression method is set to DEFLATE.
240253
options.compress_method = ArchiveCompressionMethods::DEFLATE;
241-
// Make sure that the compression level is set to BEST.
242-
options.compress_level = ArchiveCompressionLevels::BEST;
254+
// Default compression level is set to BEST.
255+
options.compress_level = flags & ARCHIVE_FLAGS_COMPRESSION_MASK
256+
? ArchiveCompressionLevels(flags & ARCHIVE_FLAGS_COMPRESSION_MASK)
257+
: ArchiveCompressionLevels::BEST;
243258

244259
// Create the writer handle.
245260
#if MZ_VERSION_BUILD >= 040000
@@ -305,7 +320,10 @@ void archiveConfig(std::ostream & ostream, const Config & config, const char * c
305320
///////////////////////
306321
// Add all supported files to in-memory zip from any directories under working directory.
307322
// (recursive)
308-
addSupportedFiles(archiver, configWorkingDirectory, configWorkingDirectory);
323+
if (HasFlag(flags, ARCHIVE_FLAGS_MINIMAL))
324+
addSupportedFiles(archiver, configWorkingDirectory, configWorkingDirectory);
325+
else
326+
addSupportedFiles(archiver, configWorkingDirectory, configWorkingDirectory);
309327

310328
// Close in-memory zip.
311329
mz_zip_writer_close(archiver);

src/OpenColorIO/OCIOZArchive.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ namespace OCIO_NAMESPACE
2323
* \param ostream Output stream to write the data into.
2424
* \param config Config object.
2525
* \param configWorkingDirectory Working directory of the current config.
26+
* \param flags Archive flags used to .
2627
*/
2728
void archiveConfig(
2829
std::ostream & ostream,
2930
const Config & config,
30-
const char * configWorkingDirectory);
31+
const char * configWorkingDirectory,
32+
ArchiveFlags flags);
3133

3234
/**
3335
* \brief Get the content of a file inside an OCIOZ archive as a buffer.
@@ -106,6 +108,11 @@ class CIOPOciozArchive : public ConfigIOProxy
106108
std::map<std::string, std::string> m_entries;
107109
};
108110

111+
// TODO: This "duplicates" flags function from Op.h could be a template?
112+
inline bool HasFlag(ArchiveFlags flags, ArchiveFlags queryFlag)
113+
{
114+
return (flags & queryFlag) == queryFlag;
115+
}
109116
} // namespace OCIO_NAMESPACE
110117

111118
#endif

src/apps/ocioarchive/main.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int main(int argc, const char **argv)
4545
// Default value is current directory.
4646
std::string extractDestination;
4747

48+
bool minimal = false;
4849
bool extract = false;
4950
bool list = false;
5051
bool help = false;
@@ -61,6 +62,9 @@ int main(int argc, const char **argv)
6162
" ocioarchive myarchive\n\n"
6263
" # Archive myconfig/config.ocio into myarchive.ocioz\n"
6364
" ocioarchive myarchive --iconfig myconfig/config.ocio\n\n"
65+
" # Archive from the OCIO environment variable into myarchive.ocioz\n"
66+
" # requires all environment variables to be resolvable\n"
67+
" ocioarchive myarchive --minimal\n\n"
6468
" # Extract myarchive.ocioz into new directory named myarchive\n"
6569
" ocioarchive --extract myarchive.ocioz\n\n"
6670
" # Extract myarchive.ocioz into new directory named ocio_config\n"
@@ -70,6 +74,7 @@ int main(int argc, const char **argv)
7074
"%*", parse_end_args, "",
7175
"<SEPARATOR>", "Options:",
7276
"--iconfig %s", &configFilename, "Config to archive (takes precedence over $OCIO)",
77+
"--minimal", &minimal, "Create a minimal archive",
7378
"--extract", &extract, "Extract an OCIOZ config archive",
7479
"--dir %s", &extractDestination, "Path where to extract the files (folders are created if missing)",
7580
"--list", &list, "List the files inside an archive without extracting it",
@@ -156,7 +161,11 @@ int main(int argc, const char **argv)
156161
std::ofstream ofstream(archiveName, std::ofstream::out | std::ofstream::binary);
157162
if (ofstream.good())
158163
{
159-
config->archive(ofstream);
164+
OCIO::ArchiveFlags flags = OCIO::ARCHIVE_FLAGS_DEFAULT;
165+
if (minimal)
166+
flags = OCIO::ArchiveFlags(flags | OCIO::ARCHIVE_FLAGS_MINIMAL);
167+
168+
config->archive(ofstream, flags);
160169
ofstream.close();
161170
}
162171
else

src/apps/ocioview/ocioview/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def setup_env() -> None:
4747
ocio.OCIO_INACTIVE_COLORSPACES_ENVVAR,
4848
ocio.OCIO_OPTIMIZATION_FLAGS_ENVVAR,
4949
ocio.OCIO_USER_CATEGORIES_ENVVAR,
50+
ocio.OCIO_ARCHIVE_FLAGS_ENVVAR,
5051
):
5152
if env_var in os.environ:
5253
del os.environ[env_var]

src/bindings/python/PyConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ void bindPyConfig(py::module & m)
894894
.def("archive", [](ConfigRcPtr & self, const char * filepath)
895895
{
896896
std::ofstream f(filepath, std::ofstream::out | std::ofstream::binary);
897-
self->archive(f);
897+
self->archive(f, ARCHIVE_FLAGS_DEFAULT); // TODO: pass flags in rather than default
898898
f.close();
899899
},
900900
DOC(Config, archive))

src/bindings/python/PyTypes.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ void bindPyTypes(py::module & m)
865865
m.attr("OCIO_ACTIVE_VIEWS_ENVVAR") = OCIO_ACTIVE_VIEWS_ENVVAR;
866866
m.attr("OCIO_INACTIVE_COLORSPACES_ENVVAR") = OCIO_INACTIVE_COLORSPACES_ENVVAR;
867867
m.attr("OCIO_OPTIMIZATION_FLAGS_ENVVAR") = OCIO_OPTIMIZATION_FLAGS_ENVVAR;
868+
m.attr("OCIO_ARCHIVE_FLAGS_ENVVAR") = OCIO_ARCHIVE_FLAGS_ENVVAR;
868869
m.attr("OCIO_USER_CATEGORIES_ENVVAR") = OCIO_USER_CATEGORIES_ENVVAR;
869870

870871
// Roles

0 commit comments

Comments
 (0)