Registry Mirror
Skip to content

Conversation

@zachlewis

@zachlewis zachlewis commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

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.

Validation

  • macOS arm64, AppleClang, Release, warnings as errors, OpenColorIO 2.5.1:
    full CTest 221/221.
  • Loading the configuration eagerly in the constructor again fails both
    unit_color and the reader/config test.
  • Clang-format 22, as CI uses, is clean.

Fixes #5490.
Addresses #4629.

Assisted-by: Claude Code / Claude Opus 5.5

@lgritz

lgritz commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

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.

@zachlewis
zachlewis marked this pull request as draft September 29, 2026 16:53
@zachlewis

Copy link
Copy Markdown
Collaborator Author

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.

@zachlewis

Copy link
Copy Markdown
Collaborator Author

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 pvt calls from readers or writers, and a small public API we can stand behind.

What readers need

Need Readers Proposed public API
Label the spec with the color space name the file states Cineon, DDS, DPX, GIF, HDR, HEIF, JPEG, JPEG 2000, OpenEXR, PNM, RAW, Targa, WebP No new API. Readers keep calling ImageSpec::set_colorspace() and set_colorspace_rec709_gamma() as they do on main. Following your suggestion in #5490, those only consult the color config when they must: when the name isn't literally sRGB and the spec already has Exif:ColorSpace to reconcile. A reader labeling the spec it's building doesn't have one at that point, so opening a file doesn't build a config (#5507's test checks this for each reader it covers), and behavior is unchanged for every caller. (#5507, reworked)
Map CICP code points to a color interop ID PNG, JPEG XL, HEIF, FFmpeg One new free function, config-free, next to get_colorspace_cicp() in color.h: string_view get_color_interop_id(cspan<const int> cicp). Today readers call ColorConfig::default_colorconfig().get_color_interop_id(cicp), which builds a config for a static table lookup; that member would forward to the new function. (#5507, reworked)
Map JPEG XL's structured primaries and transfer to a color interop ID JPEG XL Either reader-local, or a sibling of the function above, taking chromaticities and a transfer. Your call; see question 2. (a later JPEG XL PR)

What writers need

Need Writers Proposed public API
The primaries and transfer of the spec's color space (to write gAMA/cHRM, DPX/RLA/Targa gamma, and so on) DPX, RLA, Targa and PNG writers; the PNG reader (for premultiplication) #5521: ColorSpaceInfo from ColorConfig::get_color_space_info() / derive_color_space_info(). This also retires the existing pvt::get_colorspace_rec709_gamma() calls these plugins make on main today (the function we agreed in #5390 to keep private).
CICP primaries to chromaticities PNG A small table local to the PNG plugin; no API.
The color identity a spec's metadata establishes (the OpenEXR writer's colorInteropID; the terminal output's source space) OpenEXR, term Open, see question 3. Today these are pvt calls into the metadata resolver described in #5516.

Other public API changes in the series

Proposed order

  1. fix(color): load the color config on first use, not on construction #5507 reworked as above, so readers use only public API. feat(color): add chromaticities and transfer gamma queries to ColorConfig #5521 alongside it.
  2. feat(color): recognize color spaces and query their properties #5518 then moves the writers onto feat(color): add chromaticities and transfer gamma queries to ColorConfig #5521's ColorSpaceInfo and drops its own pvt uses.
  3. The resolver-dependent writers wait until we've agreed on question 3.

Questions

  1. Is the name and placement of the CICP lookup OK, or would you prefer something else?
  2. Should the JPEG XL primaries-and-transfer lookup be public, or stay inside the JPEG XL reader?
  3. For writers that need "the identity this spec establishes", which do you prefer? (a) a public query, e.g. a ColorConfig method taking an ImageSpec and returning the resolved name and how it was established, or (b) the library resolving that before the writer sees the spec, so writers only read ordinary attributes?
  4. The OpenEXR plugin's existing, non-color pvt uses (openexr_core, set_exr_threads, split_name, check_texture_metadata_sanity) are out of scope here. OK to leave them for a separate cleanup?

Assisted-by: Claude Code / Claude Opus 5.5

@zachlewis zachlewis changed the title fix(imageio): don't consult the color config when reading images fix(color): don't construct the color config when reading images Sep 29, 2026
@zachlewis

Copy link
Copy Markdown
Collaborator Author

Agreed, and that's a better shape anyway. I reworked it along the lines you
suggested in #5490, so the readers use only public API again:

  • The readers are back to calling the public set_colorspace(),
    ImageSpec::set_colorspace() and set_colorspace_rec709_gamma() exactly as
    on main. The private helpers from the first version are gone, and no reader
    or writer calls a pvt function that it didn't already call on main. (Checked
    with nm -u on an EMBEDPLUGINS=OFF build: the undefined pvt symbols in
    every plugin are identical to main's.)
  • set_colorspace() is now lazy. It sets oiio:ColorSpace and erases the
    other contradicting attributes without a config. It only asks the default
    config whether the name is equivalent() to srgb_rec709_scene when the spec
    actually has an Exif:ColorSpace and the name isn't literally
    srgb_rec709_scene. ColorConfig::set_colorspace() shares the same code with
    its own config. Every caller gets the same result as on main, so the
    custom-alias behavior changes I listed before no longer apply.
  • The only new public function is a config-free CICP to Color Interop ID
    lookup, get_color_interop_id(cspan<int> cicp), next to
    get_colorspace_cicp(), with a Python binding. The PNG, JPEG XL, HEIF and
    FFmpeg readers use it rather than constructing the default config for a
    static table lookup. ColorConfig::get_color_interop_id(const int cicp[4])
    forwards to it. Happy to rename it if you'd prefer something else.
  • Two readers had other config uses. RAW now checks the literal
    raw:ColorSpace names before it calls equivalent_colorspace(). PNG only
    works out whether the file is sRGB or gamma when png:linear_premult is going
    to use the answer.

The perf win is the same. On my M1 Ultra, cold ImageInput::open() plus spec
(median of 15 fresh processes) goes from about 11.5 ms on main to 0.45 to
0.7 ms for JPEG, HDR, a tagged EXR, and PNGs with gAMA or cICP.

Left for later: the writers' existing pvt::get_colorspace_rec709_gamma()
calls (DPX, RLA, Targa, PNG), and the PNG reader's use of it for linear
premult, could move to the public ColorSpaceInfo query in #5521 as a
follow-up.

Assisted-by: Claude Code / Claude Opus 5.5

@brechtvl

Copy link
Copy Markdown
Contributor

Was lazy initialization inside ColorConfig considered? Then other code doesn't have to be as careful constructing the default config, and no new API function is needed.

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.

@zachlewis

Copy link
Copy Markdown
Collaborator Author

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.

@brechtvl

brechtvl commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

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.

@zachlewis
zachlewis force-pushed the codex/sept-final14-n-reader-color-config-on-main branch from f20d930 to c758e0e Compare October 2, 2026 20:11
@zachlewis zachlewis changed the title fix(color): don't construct the color config when reading images fix(color): load the color config on first use, not on construction Oct 2, 2026

@lgritz lgritz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor suggestions only, otherwise looks good

Comment thread src/include/OpenImageIO/imageio.h Resolved
Comment thread src/include/OpenImageIO/imageio.h Resolved
Comment thread src/libOpenImageIO/color_ocio.cpp Resolved
Comment thread src/libOpenImageIO/color_ocio.cpp Resolved
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>
@zachlewis
zachlewis force-pushed the codex/sept-final14-n-reader-color-config-on-main branch from c758e0e to f6ecdcd Compare October 5, 2026 15:57
@zachlewis

Copy link
Copy Markdown
Collaborator Author

To fill in the history: after Larry's question in #4629, Larry suggested in #5490 that ImageSpec::set_colorspace() shouldn't instantiate a ColorConfig unless it has to, which is only to check Exif:ColorSpace against a non-sRGB name. That's what I meant by "using set_colorspace without initializing a ColorConfig". I'd replied that readers can only safely set oiio:ColorSpace directly to the few names ColorConfig identifies when it loads, which is why #5507 kept readers on set_colorspace() and taught it Larry's shortcut.

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.

@zachlewis
zachlewis requested a review from lgritz October 5, 2026 15:59
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>

@lgritz lgritz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lgritz
lgritz merged commit f056564 into AcademySoftwareFoundation:main Oct 6, 2026
68 checks passed
lgritz pushed a commit to lgritz/OpenImageIO that referenced this pull request Oct 9, 2026
…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>
lgritz pushed a commit to lgritz/OpenImageIO that referenced this pull request Oct 10, 2026
…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>
lgritz pushed a commit to lgritz/OpenImageIO that referenced this pull request Oct 13, 2026
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: ImageInput::open() forces full OpenColorIO config initialization even when no color management is requested

3 participants