Skip to content

Commit 754802f

Browse files
committed
Add color management to texture lookups
This isn't 100% final -- I'm sure there are some corner cases I missed, maybe things that will eventually need some performance work, but enough works and passes tests that I'm confident in the API and so I want to get this merged and we can iterate on internals later. The use case is as follows: User or shader knows that a particular texture is in something other than the default working color space, this is not necessarily apparent from the file itself and so cannot be fully automatic, but it is presumed that it can be communicated in the texture call. We aim to be efficient, fully converting each tile upon input. Note that we are sweeping under the rug the fact that a non-default color space lookup is not mathematically correct with respect to the MIP-map downsizing math if that was not considered at texture creation time. But by converting texels as they are read, rather than the shader itself trying to do a color transformation on the final texture result, at least we are doing the right math for the filtering within and among MIP levels. The new interface consists of communicating the presumed working color space, finding the colortransform ID of a given from/to (with the "to" defaulting to the working space, if not specified), and then on each texture call, supplying that colortransform ID as one of the settings in TextureOpt (or TextureOptBatch). The outline of changes are as follows: * BREAKING CHANGE: TextureOpt and TextureOptBatch now contain an additional int `colortransformid`. The default, 0, is the old (and, frankly, preferred) behavior. Nonzero values indicate that we want the texels to go through a color transformation as the tiles are read into the cache. * BREAKING CHANGE: New public methods have been added to TextureSystem `get_colortransform_id(fromspace, tospace)` that return a transform ID given the from and to color spaces (ustring or ustringhash). This is the value that goes in the TextureOpt to request a color transformation on the pixels as they are read. * BREAKING CHANGE: IC::get_image_handle / TS::get_texture_handle now take an additional optinal parameter that is a TextureOpt*. This is currently unused, but is reserved for possible future use and/or for alternate TextureSystem implementations that choose a different implementation strategy wherein an entirely different TextureHandle may correspond to each color transformation. That's not how I've done it here, but it's a valid approach (and maybe one we'll switch to in the future). We presume that it's ill-defined to ask for a handle with one explicit color transformation, then use that handle with a TextureOpt that implies a different color transformation. Using those consistently ensure that either TS strategy will work equally well. * New TextureSystem attribute "colorspace" gives the working color space name (defaulting to "scene_linear"), and "colorconfig" can optionally point to a color config file (though this is not yet functional -- it just uses the default color config). * Internally, the tile cache hash has been adjusted so that the same tile in the disk file will appear as a separate tile in the cache for each color transform that is applied to it. Thus, there is no conflict if some different texture calls ask for different color space interpretations of the same texture file. (That said, it's not recommended to be throwing color spaces around willy nilly -- it still takes multiple cache entries, plus extra color conversion math as the tiles are read in.) * A new preprocessor symbol OIIO_TEXTURESYSTEM_SUPPORTS_COLORSPACE in texture.h allows client software can easily tell if this is a version new enough to support color management and has the additional fields in the texture option structures. Because there are breaking changes here to ABI, this is destined only for master (future 2.5) and will not be backported to OIIO 2.4.
1 parent e2e5eba commit 754802f

File tree

17 files changed

+293
-73
lines changed

17 files changed

+293
-73
lines changed

CHANGES.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@ Release 2.5 (summer 2023?) -- compared to 2.4
33
New minimum dependencies and compatibility changes:
44

55
New major features and public API changes:
6+
* TextureSystem color management: #3761 (2.5.1.0)
7+
- TextureOpt and TextureOptBatch have a new field, `colortransformid`,
8+
which supplies an integer ID for a requested color space transformation
9+
to be applied as texture tiles are read. The default value 0 means no
10+
transformation because the texture is presumed to be in the working
11+
color space (this is the old behavior, and most performant). Tiles from
12+
the same texture file but using different color transformations are
13+
allowed and will not interfere with each other in the cache.
14+
- New `TextureSystem::get_colortransform_id(from, to)` maps from/to named
15+
color spaces to a color transform ID that can be passed to texture
16+
lookup calls.
17+
- `ImageCache::get_image_handle` and `TextureSystem::get_texture_handle`
18+
now take an optional `TextureOpt*` parameter that can supply additional
19+
constraints (such as color transformation) that TS/IC implementations
20+
may wish to split into separate handles. This is currently not used, but
21+
is reserved so that the API doesn't need to be changed if we use it in
22+
the future.
23+
- texture.h defines symbol `OIIO_TEXTURESYSTEM_SUPPORTS_COLORSPACE` that
24+
can be tested for existence to know if the new fields are in the
25+
TextureOpt structure.
626
* ImageBufAlgo additions:
727
- A new flavor of `ociodisplay()` now contains an inverse parameter.
828
#3650 (2.5.0.0)

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
cmake_minimum_required (VERSION 3.12)
66

7-
set (OpenImageIO_VERSION "2.5.0.2")
7+
set (OpenImageIO_VERSION "2.5.1.0")
88
set (OpenImageIO_VERSION_OVERRIDE "" CACHE STRING
99
"Version override (use with caution)!")
1010
mark_as_advanced (OpenImageIO_VERSION_OVERRIDE)

src/cmake/testing.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ macro (oiio_add_all_tests)
186186
texture-stats
187187
texture-threadtimes
188188
texture-env
189+
texture-colorspace
189190
)
190191
oiio_add_tests (${all_texture_tests})
191192
# Duplicate texture tests with batch mode

src/include/OpenImageIO/imagecache.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828

2929
OIIO_NAMESPACE_BEGIN
3030

31+
// Forward declaration
32+
class TextureOpt;
33+
3134
namespace pvt {
3235
// Forward declaration
3336
class ImageCacheImpl;
@@ -216,6 +219,11 @@ class OIIO_API ImageCache {
216219
/// enabled, this reduces the number of file opens, at the
217220
/// expense of not being able to open files if their format do
218221
/// not actually match their filename extension). Default: 0
222+
/// - `string colorspace` :
223+
/// The working colorspace of the texture system. Default: none.
224+
/// - `string colorconfig` :
225+
/// Name of the OCIO config to use. Default: "" (meaning to use
226+
/// the default color config).
219227
///
220228
/// - `string options`
221229
/// This catch-all is simply a comma-separated list of
@@ -467,6 +475,25 @@ class OIIO_API ImageCache {
467475
/// internals.
468476
typedef pvt::ImageCacheFile ImageHandle;
469477

478+
#if OIIO_VERSION_GREATER_EQUAL(2, 5, 1)
479+
/// Retrieve an opaque handle for fast texture lookups. The filename is
480+
/// presumed to be UTF-8 encoded. The `options`, if not null, may be used
481+
/// to create a separate handle for certain texture option choices
482+
/// (currently: the colorspace). The opaque pointer `thread_info` is
483+
/// thread-specific information returned by `get_perthread_info()`. Return
484+
/// nullptr if something has gone horribly wrong.
485+
virtual ImageHandle* get_image_handle(ustring filename,
486+
Perthread* thread_info = nullptr,
487+
const TextureOpt* options = nullptr) = 0;
488+
489+
/// Get an ImageHandle using a UTF-16 encoded wstring filename.
490+
ImageHandle* get_image_handle(const std::wstring& filename,
491+
Perthread* thread_info = nullptr,
492+
const TextureOpt* options = nullptr) {
493+
return get_image_handle(ustring(Strutil::utf16_to_utf8(filename)),
494+
thread_info, options);
495+
}
496+
#else
470497
/// Retrieve an opaque handle for fast image lookups. The filename is
471498
/// presumed to be UTF-8 encoded. The opaque `pointer thread_info` is
472499
/// thread-specific information returned by `get_perthread_info()`.
@@ -480,6 +507,7 @@ class OIIO_API ImageCache {
480507
return get_image_handle (ustring(Strutil::utf16_to_utf8(filename)),
481508
thread_info);
482509
}
510+
#endif
483511

484512
/// Return true if the image handle (previously returned by
485513
/// `get_image_handle()`) is a valid image that can be subsequently read.

src/include/OpenImageIO/texture.h

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// Define symbols that let client applications determine if newly added
2121
// features are supported.
2222
#define OIIO_TEXTURESYSTEM_SUPPORTS_CLOSE 1
23+
#define OIIO_TEXTURESYSTEM_SUPPORTS_COLORSPACE 1
2324

2425
// Is the getattributetype() method present? (Added in 2.5)
2526
#define OIIO_TEXTURESYSTEM_SUPPORTS_GETATTRIBUTETYPE 1
@@ -232,6 +233,7 @@ class OIIO_API TextureOpt {
232233
fill(0.0f), missingcolor(nullptr),
233234
time(0.0f), rnd(-1.0f), samples(1),
234235
rwrap(WrapDefault), rblur(0.0f), rwidth(1.0f),
236+
colortransformid(0),
235237
envlayout(0)
236238
{ }
237239

@@ -264,6 +266,8 @@ class OIIO_API TextureOpt {
264266
float rblur; ///< Blur amount in the r direction
265267
float rwidth; ///< Multiplier for derivatives in r direction
266268

269+
int colortransformid; ///< Color space id of the texture
270+
267271
/// Utility: Return the Wrap enum corresponding to a wrap name:
268272
/// "default", "black", "clamp", "periodic", "mirror".
269273
static Wrap decode_wrapmode(const char* name)
@@ -313,9 +317,7 @@ class OIIO_API TextureOptBatch {
313317
alignas(Tex::BatchAlign) float twidth[Tex::BatchWidth];
314318
alignas(Tex::BatchAlign) float rwidth[Tex::BatchWidth];
315319
// Note: rblur,rwidth only used for volumetric lookups
316-
#if OIIO_VERSION_GREATER_EQUAL(2,4,0)
317320
alignas(Tex::BatchAlign) float rnd[Tex::BatchWidth];
318-
#endif
319321

320322
// Options that must be the same for all points we're texturing at once
321323
int firstchannel = 0; ///< First channel of the lookup
@@ -330,6 +332,7 @@ class OIIO_API TextureOptBatch {
330332
int conservative_filter = 1; ///< True: over-blur rather than alias
331333
float fill = 0.0f; ///< Fill value for missing channels
332334
const float *missingcolor = nullptr; ///< Color for missing texture
335+
int colortransformid = 0; ///< Color space id of the texture
333336

334337
private:
335338
// Options set INTERNALLY by libtexture after the options are passed
@@ -551,6 +554,10 @@ class OIIO_API TextureSystem {
551554
/// - `int max_errors_per_file` :
552555
/// Limits how many errors to issue for each file. (default:
553556
/// 100)
557+
/// - `string colorspace` :
558+
/// The working colorspace of the texture system.
559+
/// - `string colorconfig` :
560+
/// Name of the OCIO config to use (default: "").
554561
///
555562
/// Texture-specific settings:
556563
/// - `matrix44 worldtocommon` / `matrix44 commontoworld` :
@@ -788,16 +795,20 @@ class OIIO_API TextureSystem {
788795
class TextureHandle;
789796

790797
/// Retrieve an opaque handle for fast texture lookups. The filename is
791-
/// presumed to be UTF-8 encoded. The opaque pointer `thread_info` is
792-
/// thread-specific information returned by `get_perthread_info()`.
793-
/// Return nullptr if something has gone horribly wrong.
794-
virtual TextureHandle* get_texture_handle (ustring filename,
795-
Perthread *thread_info=nullptr) = 0;
798+
/// presumed to be UTF-8 encoded. The `options`, if not null, may be used
799+
/// to create a separate handle for certain texture option choices
800+
/// (currently: the colorspace). The opaque pointer `thread_info` is
801+
/// thread-specific information returned by `get_perthread_info()`. Return
802+
/// nullptr if something has gone horribly wrong.
803+
virtual TextureHandle* get_texture_handle(ustring filename,
804+
Perthread* thread_info = nullptr,
805+
const TextureOpt* options = nullptr) = 0;
796806
/// Get a TextureHandle using a UTF-16 encoded wstring filename.
797-
TextureHandle* get_texture_handle (const std::wstring& filename,
798-
Perthread *thread_info=nullptr) {
799-
return get_texture_handle (ustring(Strutil::utf16_to_utf8(filename)),
800-
thread_info);
807+
TextureHandle* get_texture_handle(const std::wstring& filename,
808+
Perthread* thread_info = nullptr,
809+
const TextureOpt* options = nullptr) {
810+
return get_texture_handle(ustring(Strutil::utf16_to_utf8(filename)),
811+
thread_info, options);
801812
}
802813

803814
/// Return true if the texture handle (previously returned by
@@ -810,6 +821,14 @@ class OIIO_API TextureSystem {
810821
/// This method was added in OpenImageIO 2.3.
811822
virtual ustring filename_from_handle(TextureHandle* handle) = 0;
812823

824+
/// Retrieve an id for a color transformation by name. This ID can be used
825+
/// as the value for TextureOpt::colortransformid. The returned value will
826+
/// be -1 if either color space is unknown, and 0 for a null
827+
/// transformation.
828+
virtual int get_colortransform_id(ustring fromspace,
829+
ustring tospace) const = 0;
830+
virtual int get_colortransform_id(ustringhash fromspace,
831+
ustringhash tospace) const = 0;
813832
/// @}
814833

815834
/// @{

0 commit comments

Comments
 (0)