Conversation
|
I would like to find a way to do this that doesn't involve calling pvt namespaced functions from readers. Anything in "pvt" is meant to be truly internal to the library and not part of the public API or ABI contracts. We should be able to change pvt contents at will with no concern for compatibility. We may default to EMBEDPLUGINS=ON to compile the bundled plugins right into the library (for simplicity and fool-proofedness of not letting it be possible to not find tiff or exr at runtime because of a bad LD_LIBRARY_PATH or what have you). But everything should still work with EMBEDPLUGINS=OFF, making them true dll/dso's, which would mean that any pvt namespaced items they access become de facto part of the public API, and that defeats the purpose of what pvt is supposed to mean. Additionally, the set of format readers/writers is meant to be extensible and 3rd party plugins should not be hobbled or less capable in principle than built-in ones. I strongly feel that we can only be certain of that if we "eat our own dog food", by having our built-in readers/writers implemented using only public APIs. We do have a couple of existing pvt uses, but I strongly desire to eliminate those with haste. So let's decide what is really needed in the reader/writers, and make a public API we can stand behind that provides what they need. |
|
Ack! So sorry, Larry. I meant to keep this in draft until I'd reconciled it with what we discussed in #5490, and I jumped the gun because I needed it in place to submit and discuss #5518, which (apart from this one) is the real load-bearing PR of my "campaign". About ten much smaller PRs depend on what #5518 sets up. The whole campaign is stuff I've spent the past several weeks trying to cut up sanely for review (I had to stop myself adding features back in June or July!). And you're right: readers shouldn't be calling pvt functions. I'm reworking this along the lines you suggested in #5490. set_colorspace() will only touch the color config when it actually needs to, so readers can keep using the public API as before. I'll mark this as a draft and come back with exactly what I have in mind. |
|
Here's the whole picture, so we can agree on the public API once rather than PR by PR. I went through every place the color series has format plugins call into the library, and sorted each by what the plugin actually needs. The goal is exactly what you said: no What readers need
What writers need
Other public API changes in the series
Proposed order
Questions
Assisted-by: Claude Code / Claude Opus 5.5 |
|
Agreed, and that's a better shape anyway. I reworked it along the lines you
The perf win is the same. On my M1 Ultra, cold Left for later: the writers' existing Assisted-by: Claude Code / Claude Opus 5.5 |
b84db6d to
f20d930
Compare
|
Was lazy initialization inside Quick diff (AI generated by Claude Opus)diff --git a/src/include/OpenImageIO/color.h b/src/include/OpenImageIO/color.h
index 865b32ea6..bbc320469 100644
--- a/src/include/OpenImageIO/color.h
+++ b/src/include/OpenImageIO/color.h
@@ -480,7 +480,7 @@ private:
class Impl;
std::unique_ptr<Impl> m_impl;
- Impl* getImpl() const { return m_impl.get(); }
+ Impl* getImpl() const;
};
OIIO_NAMESPACE_3_1_END
diff --git a/src/libOpenImageIO/color_ocio.cpp b/src/libOpenImageIO/color_ocio.cpp
index c206fb6bc..c4901b013 100644
--- a/src/libOpenImageIO/color_ocio.cpp
+++ b/src/libOpenImageIO/color_ocio.cpp
@@ -5,6 +5,7 @@
#include <algorithm>
#include <cmath>
#include <memory>
+#include <mutex>
#include <string>
#include <vector>
@@ -234,10 +235,14 @@ private:
std::string m_configname;
ColorConfig* m_self = nullptr;
bool m_config_is_built_in = false;
+ std::string m_init_filename;
+ std::once_flag m_init_flag;
+ bool m_init_ok = false;
public:
- Impl(ColorConfig* self)
+ Impl(ColorConfig* self, string_view filename)
: m_self(self)
+ , m_init_filename(filename)
{
}
@@ -254,6 +259,15 @@ public:
bool init(string_view filename);
+ // Initialize on first use, as loading the OCIO config is expensive and
+ // not needed for many operations, like reading images.
+ bool init_once()
+ {
+ std::call_once(m_init_flag,
+ [this] { m_init_ok = init(m_init_filename); });
+ return m_init_ok;
+ }
+
void add(const std::string& name, int index, int flags = 0)
{
spin_rw_write_lock lock(m_mutex);
@@ -869,8 +883,8 @@ ColorConfig::Impl::IdentifyBuiltinColorSpace(const char* name) const
ColorConfig::ColorConfig(string_view filename)
+ : m_impl(new ColorConfig::Impl(this, filename))
{
- (void)reset(filename);
}
@@ -1027,8 +1041,17 @@ ColorConfig::reset(string_view filename)
return true;
}
- m_impl.reset(new ColorConfig::Impl(this));
- return m_impl->init(filename);
+ m_impl.reset(new ColorConfig::Impl(this, filename));
+ return m_impl->init_once();
+}
+
+
+
+ColorConfig::Impl*
+ColorConfig::getImpl() const
+{
+ m_impl->init_once();
+ return m_impl.get();
}
@@ -3108,7 +3131,8 @@ ColorConfig::set_colorspace(ImageSpec& spec, string_view colorspace) const
// including some format-specific things that we don't want to propagate
// from input to output if we know that color space transformations have
// occurred.
- if (!equivalent(colorspace, "srgb_rec709_scene"))
+ if (spec.find_attribute("Exif:ColorSpace")
+ && !equivalent(colorspace, "srgb_rec709_scene"))
spec.erase_attribute("Exif:ColorSpace");
spec.erase_attribute("tiff:ColorSpace");
spec.erase_attribute("tiff:PhotometricInterpretation");
diff --git a/src/png.imageio/pnginput.cpp b/src/png.imageio/pnginput.cpp
index 71a9ae5ad..0071c9cce 100644
--- a/src/png.imageio/pnginput.cpp
+++ b/src/png.imageio/pnginput.cpp
@@ -196,7 +196,12 @@ PNGInput::open(const std::string& name, ImageSpec& newspec)
return false;
}
- if (is_colorspace_srgb(m_spec)) {
+ // Only needed for premultiplying in linear space, and avoids loading the
+ // OCIO config otherwise.
+ if (!m_linear_premult) {
+ m_srgb = false;
+ m_gamma = 1.0f;
+ } else if (is_colorspace_srgb(m_spec)) {
m_srgb = true;
m_gamma = 1.0f;
} else {
Performance seems to be the same as this PR. |
|
Heya Brecht! tl;dr -- sure, we could do that here instead (or too)! I am doing something quite similar in #5518 (which is quite large, and probably needs to be broken up further and better explained before it's reasonable to ask for a review); in fact, my original plan was to leave it at that -- initialize the ColorConfig as needed, but cache the initialized OCIO config and gathered / computed information with the string used to initialize ColorConfig; but the idea of using set_colorspace without necessarily initializing a ColorConfig object at all popped up in a couple of other discussions, and I figured it would be easy enough to accommodate and settle some reader-side behavior in one fell swoop, before I finish polishing up Big Bertha 5518 and the ones that follow. That being said, it really should only take, like, ~10-30 ms at most to initialize the builtin OCIO configs once; and initializing OCIO configs only once is one of the areas where #5518 gains in efficiency. So, I'm inclined to agree with Brecht that, if we're cool with taking the initial OCIO-config load-time + classification methods hit up front once and only once, simply sharing the same immutable underlying OCIO Config among ColorConfig instances is the path of least resistance. On the other hand, that worst-case-scenario PixelManager big-mama-jama config that Brecht recommended testing against many moons ago takes, like, ~2.5 seconds for OCIO itself to load. It's definitely an atypical case, but, still, it does demonstrate a plausible case where not loading the OCIO config at all if it can possibly be avoided would make a huge difference. Beyond that, regarding public API changes: I'm pretty confident we can backport all color metadata related things to 3.2 without any public API changes at all; and I think, now, the discussion is more, what kind of public API would we like to see for 3.3? I've made some suggestions, but I'm totally open to reworking this -- whatever makes the most sense to everyone. |
|
I wasn't even aware the config was initialized multiple times, if that can be avoided by caching that sounds good to me. I don't feel particularly strongly about avoiding the initial config load time or not. As far as I can tell Larry asked in #4629 if this is worth solving and where, someone replied with an AI generated comment and that kicked off this whole thing? I feel that if it's going to be solved, I would rather do it in a simple isolated way as in my proposed solution, rather than something that spreads complexity to the various file format plug-ins. |
f20d930 to
c758e0e
Compare
lgritz
left a comment
There was a problem hiding this comment.
A few minor suggestions only, otherwise looks good
Opening a tagged image built the default ColorConfig, and with it the complete OpenColorIO config, even though no reader needs it: readers call set_colorspace(), which only consulted the config to decide whether to keep "Exif:ColorSpace", and some construct the default config just to look up a CICP code in a static table. A ColorConfig now loads its configuration the first time something needs it rather than when it is constructed, so constructing one, including default_colorconfig(), is cheap, and the CICP lookup, which needs no configuration, loads nothing. A configuration that fails to load still reports the error when first asked. set_colorspace() consults the config only when the spec has an "Exif:ColorSpace" attribute to keep or erase, and the PNG reader works out a file's transfer function only when it will premultiply in linear space, the one thing that uses it. Based on a suggestion by Brecht Van Lommel. Fixes AcademySoftwareFoundation#5490. Assisted-by: Claude Code / Claude Opus 5.5 Signed-off-by: Zach Lewis <zachcanbereached@gmail.com>
Keep set_colorspace()'s documentation general rather than spelling out when the config loads, and skip equivalent() for the literal "srgb_rec709_scene" before asking it. Assisted-by: Claude Code / Claude Opus 5.5 Signed-off-by: Zach Lewis <zachcanbereached@gmail.com>
c758e0e to
f6ecdcd
Compare
|
To fill in the history: after Larry's question in #4629, Larry suggested in #5490 that Your approach gets there in one place: Larry's shortcut stays (your diff has it too), and the config just doesn't load until something uses it, so the readers don't change at all. I've switched this PR to it. |
The setup commands that write the test files now run silently, instead of logging to out.txt and being overwritten by the final redirect. A build without an optional writer still produces the same output. Assisted-by: Claude Code / Claude Opus 5.5 Signed-off-by: Zach Lewis <zachcanbereached@gmail.com>
…cademySoftwareFoundation#5507) Opening a tagged image built the default `ColorConfig`, and with it the complete OpenColorIO config, even though nothing in the open needs it. Readers call `set_colorspace()`, which consulted the config only to decide whether to keep `Exif:ColorSpace`, and the PNG, JPEG XL, HEIF and FFmpeg readers construct the default config just to look up a CICP code in a static table. Following Brecht's suggestion, `ColorConfig` now loads its configuration the first time something needs it, rather than when it is constructed. No public API is added, and readers call exactly what they call on main. ## What this changes 1. The `ColorConfig` constructor only records which configuration to use. The configuration loads once, thread-safely (`std::call_once`), the first time a method needs it. `reset()` still loads immediately, since it returns whether loading succeeded. A configuration that fails to load still reports the error through `has_error()` and `geterror()`. 2. `ColorConfig::get_color_interop_id(const int cicp[4])` never needed the configuration, so the readers' CICP lookups no longer load one. 3. `set_colorspace()`, and so `ImageSpec::set_colorspace()` and `set_colorspace_rec709_gamma()`, asks `equivalent()` only when the spec has an `Exif:ColorSpace` attribute to keep or erase. 4. The PNG reader works out whether the file is sRGB or gamma only when `png:linear_premult` will use the answer. 5. Tests. An end-to-end test opens a file from each covered reader in a fresh process, including PNG color chunks and both OpenEXR readers, and checks that no configuration was loaded; it then checks that constructing a `ColorConfig` doesn't load one and using it does. `color_test` checks that the setters and the CICP lookup load nothing, that a non-sRGB name with `Exif:ColorSpace` present still consults the config and erases the attribute, and that a missing configuration still reports an error. ## Changed behavior - Opening a file no longer loads a color configuration for the covered readers, unless a reader sets a non-sRGB name on a spec that already has `Exif:ColorSpace`, or RAW is given a `raw:ColorSpace` hint other than its default. - Results are unchanged for every caller. What changes is when the configuration loads: on first use rather than at construction, so an empty filename picks up `$OCIO` as it is at that moment. - No new public API, and `ColorConfig`'s layout is unchanged. The private `getImpl()` is no longer inline. ## Reader coverage The covered readers are Cineon, DDS, DPX, FFmpeg, GIF, HDR/RGBE, HEIF/AVIF, JPEG, JPEG 2000, JPEG XL, shared Exif decoding, both OpenEXR readers, PNG/ICO, PNM, RAW, RLA, Targa, and WebP. TIFF records its literal color metadata without calling the setter. ## Performance On an Apple M1 Ultra with OpenColorIO 2.5.1, I timed cold `ImageInput::open()` plus spec access in 15 interleaved fresh processes per cell. Medians are in milliseconds: | Input | main | this PR | |---|---:|---:| | JPEG with `Exif:ColorSpace` | 11.619 | 0.604 | | HDR | 11.553 | 0.476 | | EXR with `colorInteropID` | 11.827 | 0.761 | | EXR untagged | 0.771 | 0.725 | | PNG with `gAMA` | 11.769 | 0.551 | | PNG with `cICP` | 11.708 | 0.538 | These match what the earlier version of this PR measured. Fixes AcademySoftwareFoundation#5490. Addresses AcademySoftwareFoundation#4629. Assisted-by: Claude Code / Claude Opus 5.5 --------- Signed-off-by: Zach Lewis <zachcanbereached@gmail.com>
…cademySoftwareFoundation#5507) Opening a tagged image built the default `ColorConfig`, and with it the complete OpenColorIO config, even though nothing in the open needs it. Readers call `set_colorspace()`, which consulted the config only to decide whether to keep `Exif:ColorSpace`, and the PNG, JPEG XL, HEIF and FFmpeg readers construct the default config just to look up a CICP code in a static table. Following Brecht's suggestion, `ColorConfig` now loads its configuration the first time something needs it, rather than when it is constructed. No public API is added, and readers call exactly what they call on main. ## What this changes 1. The `ColorConfig` constructor only records which configuration to use. The configuration loads once, thread-safely (`std::call_once`), the first time a method needs it. `reset()` still loads immediately, since it returns whether loading succeeded. A configuration that fails to load still reports the error through `has_error()` and `geterror()`. 2. `ColorConfig::get_color_interop_id(const int cicp[4])` never needed the configuration, so the readers' CICP lookups no longer load one. 3. `set_colorspace()`, and so `ImageSpec::set_colorspace()` and `set_colorspace_rec709_gamma()`, asks `equivalent()` only when the spec has an `Exif:ColorSpace` attribute to keep or erase. 4. The PNG reader works out whether the file is sRGB or gamma only when `png:linear_premult` will use the answer. 5. Tests. An end-to-end test opens a file from each covered reader in a fresh process, including PNG color chunks and both OpenEXR readers, and checks that no configuration was loaded; it then checks that constructing a `ColorConfig` doesn't load one and using it does. `color_test` checks that the setters and the CICP lookup load nothing, that a non-sRGB name with `Exif:ColorSpace` present still consults the config and erases the attribute, and that a missing configuration still reports an error. ## Changed behavior - Opening a file no longer loads a color configuration for the covered readers, unless a reader sets a non-sRGB name on a spec that already has `Exif:ColorSpace`, or RAW is given a `raw:ColorSpace` hint other than its default. - Results are unchanged for every caller. What changes is when the configuration loads: on first use rather than at construction, so an empty filename picks up `$OCIO` as it is at that moment. - No new public API, and `ColorConfig`'s layout is unchanged. The private `getImpl()` is no longer inline. ## Reader coverage The covered readers are Cineon, DDS, DPX, FFmpeg, GIF, HDR/RGBE, HEIF/AVIF, JPEG, JPEG 2000, JPEG XL, shared Exif decoding, both OpenEXR readers, PNG/ICO, PNM, RAW, RLA, Targa, and WebP. TIFF records its literal color metadata without calling the setter. ## Performance On an Apple M1 Ultra with OpenColorIO 2.5.1, I timed cold `ImageInput::open()` plus spec access in 15 interleaved fresh processes per cell. Medians are in milliseconds: | Input | main | this PR | |---|---:|---:| | JPEG with `Exif:ColorSpace` | 11.619 | 0.604 | | HDR | 11.553 | 0.476 | | EXR with `colorInteropID` | 11.827 | 0.761 | | EXR untagged | 0.771 | 0.725 | | PNG with `gAMA` | 11.769 | 0.551 | | PNG with `cICP` | 11.708 | 0.538 | These match what the earlier version of this PR measured. Fixes AcademySoftwareFoundation#5490. Addresses AcademySoftwareFoundation#4629. Assisted-by: Claude Code / Claude Opus 5.5 --------- Signed-off-by: Zach Lewis <zachcanbereached@gmail.com>
…cademySoftwareFoundation#5507) Opening a tagged image built the default `ColorConfig`, and with it the complete OpenColorIO config, even though nothing in the open needs it. Readers call `set_colorspace()`, which consulted the config only to decide whether to keep `Exif:ColorSpace`, and the PNG, JPEG XL, HEIF and FFmpeg readers construct the default config just to look up a CICP code in a static table. Following Brecht's suggestion, `ColorConfig` now loads its configuration the first time something needs it, rather than when it is constructed. No public API is added, and readers call exactly what they call on main. ## What this changes 1. The `ColorConfig` constructor only records which configuration to use. The configuration loads once, thread-safely (`std::call_once`), the first time a method needs it. `reset()` still loads immediately, since it returns whether loading succeeded. A configuration that fails to load still reports the error through `has_error()` and `geterror()`. 2. `ColorConfig::get_color_interop_id(const int cicp[4])` never needed the configuration, so the readers' CICP lookups no longer load one. 3. `set_colorspace()`, and so `ImageSpec::set_colorspace()` and `set_colorspace_rec709_gamma()`, asks `equivalent()` only when the spec has an `Exif:ColorSpace` attribute to keep or erase. 4. The PNG reader works out whether the file is sRGB or gamma only when `png:linear_premult` will use the answer. 5. Tests. An end-to-end test opens a file from each covered reader in a fresh process, including PNG color chunks and both OpenEXR readers, and checks that no configuration was loaded; it then checks that constructing a `ColorConfig` doesn't load one and using it does. `color_test` checks that the setters and the CICP lookup load nothing, that a non-sRGB name with `Exif:ColorSpace` present still consults the config and erases the attribute, and that a missing configuration still reports an error. ## Changed behavior - Opening a file no longer loads a color configuration for the covered readers, unless a reader sets a non-sRGB name on a spec that already has `Exif:ColorSpace`, or RAW is given a `raw:ColorSpace` hint other than its default. - Results are unchanged for every caller. What changes is when the configuration loads: on first use rather than at construction, so an empty filename picks up `$OCIO` as it is at that moment. - No new public API, and `ColorConfig`'s layout is unchanged. The private `getImpl()` is no longer inline. ## Reader coverage The covered readers are Cineon, DDS, DPX, FFmpeg, GIF, HDR/RGBE, HEIF/AVIF, JPEG, JPEG 2000, JPEG XL, shared Exif decoding, both OpenEXR readers, PNG/ICO, PNM, RAW, RLA, Targa, and WebP. TIFF records its literal color metadata without calling the setter. ## Performance On an Apple M1 Ultra with OpenColorIO 2.5.1, I timed cold `ImageInput::open()` plus spec access in 15 interleaved fresh processes per cell. Medians are in milliseconds: | Input | main | this PR | |---|---:|---:| | JPEG with `Exif:ColorSpace` | 11.619 | 0.604 | | HDR | 11.553 | 0.476 | | EXR with `colorInteropID` | 11.827 | 0.761 | | EXR untagged | 0.771 | 0.725 | | PNG with `gAMA` | 11.769 | 0.551 | | PNG with `cICP` | 11.708 | 0.538 | These match what the earlier version of this PR measured. Fixes AcademySoftwareFoundation#5490. Addresses AcademySoftwareFoundation#4629. Assisted-by: Claude Code / Claude Opus 5.5 --------- Signed-off-by: Zach Lewis <zachcanbereached@gmail.com>
Opening a tagged image built the default
ColorConfig, and with it thecomplete OpenColorIO config, even though nothing in the open needs it.
Readers call
set_colorspace(), which consulted the config only to decidewhether to keep
Exif:ColorSpace, and the PNG, JPEG XL, HEIF and FFmpegreaders construct the default config just to look up a CICP code in a static
table.
Following Brecht's suggestion,
ColorConfignow loads its configuration thefirst time something needs it, rather than when it is constructed. No public
API is added, and readers call exactly what they call on main.
What this changes
ColorConfigconstructor only records which configuration to use. Theconfiguration loads once, thread-safely (
std::call_once), the first timea method needs it.
reset()still loads immediately, since it returnswhether loading succeeded. A configuration that fails to load still
reports the error through
has_error()andgeterror().ColorConfig::get_color_interop_id(const int cicp[4])never needed theconfiguration, so the readers' CICP lookups no longer load one.
set_colorspace(), and soImageSpec::set_colorspace()andset_colorspace_rec709_gamma(), asksequivalent()only when the spec hasan
Exif:ColorSpaceattribute to keep or erase.png:linear_premultwill use the answer.process, including PNG color chunks and both OpenEXR readers, and checks
that no configuration was loaded; it then checks that constructing a
ColorConfigdoesn't load one and using it does.color_testchecks thatthe setters and the CICP lookup load nothing, that a non-sRGB name with
Exif:ColorSpacepresent still consults the config and erases theattribute, and that a missing configuration still reports an error.
Changed behavior
readers, unless a reader sets a non-sRGB name on a spec that already has
Exif:ColorSpace, or RAW is given araw:ColorSpacehint other than itsdefault.
configuration loads: on first use rather than at construction, so an empty
filename picks up
$OCIOas it is at that moment.ColorConfig's layout is unchanged. The privategetImpl()is no longer inline.Reader coverage
The covered readers are Cineon, DDS, DPX, FFmpeg, GIF, HDR/RGBE, HEIF/AVIF,
JPEG, JPEG 2000, JPEG XL, shared Exif decoding, both OpenEXR readers, PNG/ICO,
PNM, RAW, RLA, Targa, and WebP. TIFF records its literal color metadata without
calling the setter.
Performance
On an Apple M1 Ultra with OpenColorIO 2.5.1, I timed cold
ImageInput::open()plus spec access in 15 interleaved fresh processes percell. Medians are in milliseconds:
Exif:ColorSpacecolorInteropIDgAMAcICPThese match what the earlier version of this PR measured.
Validation
full CTest 221/221.
unit_colorand the reader/config test.Fixes #5490.
Addresses #4629.
Assisted-by: Claude Code / Claude Opus 5.5