blender/intern/opencolorio/ocio_capi.cc
Lukas Stockner 6967255906 Color management: Support white balance as part of the display transform
This implements a von-Kries-style chromatic adaption using the Bradford matrix.
The adaption is performed in scene linear space in the OCIO GLSL shader, with
the matrix being computed on the host.

The parameters specify the white point of the input, which is to be mapped to
the white point of the scene linear space. The main parameter is temperature,
specified in Kelvin, which defines the blackbody spectrum that is used as the
input white point. Additionally, a tint parameter can be used to shift the
white point away from pure blackbody spectra (e.g. to match a D illuminant).

The defaults are set to match D65 so there is no immediate color shift when
enabling the option. Tint = 10 is needed since the D-series illuminants aren't
perfect blackbody emitters.

As an alternative to manually specifying the values, there's also a color
picker. When a color is selected, temperature and tint are set such that this
color ends up being balanced to white.
This only works if the color is close enough to a blackbody emitter -
specifically, for tint values within +-150. Beyond this, there can be ambiguity
in the representation.
Currently, in this case, the input is just ignored and temperature/tint aren't
changed. Ideally, we'd eventually give UI feedback for this.

Presets are supported, and all the CIE standard illuminants are included.

One part that I'm not quite happy with is that the tint parameter starts to
give weird results at moderate values when the temperature is low.
The reason for this can be seen here:
https://commons.wikimedia.org/wiki/File:Planckian-locus.png
Tint is moving along the isotherm lines (with the plot corresponding to +-150),
but below 4000K some of that range is outside of the gamut. Not much can
be done there, other than possibly clipping those values...

Adding support for this to the compositor should be quite easy and is planned
as a next step.

Pull Request: https://projects.blender.org/blender/blender/pulls/123278
2024-06-27 23:27:58 +02:00

352 lines
10 KiB
C++

/* SPDX-FileCopyrightText: 2012 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "MEM_guardedalloc.h"
#include "ocio_impl.h"
static IOCIOImpl *impl = NULL;
void OCIO_init()
{
#ifdef WITH_OCIO
impl = new OCIOImpl();
#else
impl = new FallbackImpl();
#endif
}
void OCIO_exit()
{
delete impl;
impl = NULL;
}
OCIO_ConstConfigRcPtr *OCIO_getCurrentConfig()
{
return impl->getCurrentConfig();
}
OCIO_ConstConfigRcPtr *OCIO_configCreateFallback()
{
delete impl;
impl = new FallbackImpl();
return impl->getCurrentConfig();
}
void OCIO_setCurrentConfig(const OCIO_ConstConfigRcPtr *config)
{
impl->setCurrentConfig(config);
}
OCIO_ConstConfigRcPtr *OCIO_configCreateFromEnv()
{
return impl->configCreateFromEnv();
}
OCIO_ConstConfigRcPtr *OCIO_configCreateFromFile(const char *filename)
{
return impl->configCreateFromFile(filename);
}
void OCIO_configRelease(OCIO_ConstConfigRcPtr *config)
{
impl->configRelease(config);
}
int OCIO_configGetNumColorSpaces(OCIO_ConstConfigRcPtr *config)
{
return impl->configGetNumColorSpaces(config);
}
const char *OCIO_configGetColorSpaceNameByIndex(OCIO_ConstConfigRcPtr *config, int index)
{
return impl->configGetColorSpaceNameByIndex(config, index);
}
OCIO_ConstColorSpaceRcPtr *OCIO_configGetColorSpace(OCIO_ConstConfigRcPtr *config,
const char *name)
{
return impl->configGetColorSpace(config, name);
}
int OCIO_configGetIndexForColorSpace(OCIO_ConstConfigRcPtr *config, const char *name)
{
return impl->configGetIndexForColorSpace(config, name);
}
const char *OCIO_configGetDefaultDisplay(OCIO_ConstConfigRcPtr *config)
{
return impl->configGetDefaultDisplay(config);
}
int OCIO_configGetNumDisplays(OCIO_ConstConfigRcPtr *config)
{
return impl->configGetNumDisplays(config);
}
const char *OCIO_configGetDisplay(OCIO_ConstConfigRcPtr *config, int index)
{
return impl->configGetDisplay(config, index);
}
const char *OCIO_configGetDefaultView(OCIO_ConstConfigRcPtr *config, const char *display)
{
return impl->configGetDefaultView(config, display);
}
int OCIO_configGetNumViews(OCIO_ConstConfigRcPtr *config, const char *display)
{
return impl->configGetNumViews(config, display);
}
const char *OCIO_configGetView(OCIO_ConstConfigRcPtr *config, const char *display, int index)
{
return impl->configGetView(config, display, index);
}
const char *OCIO_configGetDisplayColorSpaceName(OCIO_ConstConfigRcPtr *config,
const char *display,
const char *view)
{
return impl->configGetDisplayColorSpaceName(config, display, view);
}
void OCIO_configGetDefaultLumaCoefs(OCIO_ConstConfigRcPtr *config, float *rgb)
{
impl->configGetDefaultLumaCoefs(config, rgb);
}
void OCIO_configGetXYZtoSceneLinear(OCIO_ConstConfigRcPtr *config, float xyz_to_scene_linear[3][3])
{
impl->configGetXYZtoSceneLinear(config, xyz_to_scene_linear);
}
int OCIO_configGetNumLooks(OCIO_ConstConfigRcPtr *config)
{
return impl->configGetNumLooks(config);
}
const char *OCIO_configGetLookNameByIndex(OCIO_ConstConfigRcPtr *config, int index)
{
return impl->configGetLookNameByIndex(config, index);
}
OCIO_ConstLookRcPtr *OCIO_configGetLook(OCIO_ConstConfigRcPtr *config, const char *name)
{
return impl->configGetLook(config, name);
}
const char *OCIO_lookGetProcessSpace(OCIO_ConstLookRcPtr *look)
{
return impl->lookGetProcessSpace(look);
}
void OCIO_lookRelease(OCIO_ConstLookRcPtr *look)
{
impl->lookRelease(look);
}
int OCIO_colorSpaceIsInvertible(OCIO_ConstColorSpaceRcPtr *cs)
{
return impl->colorSpaceIsInvertible(cs);
}
int OCIO_colorSpaceIsData(OCIO_ConstColorSpaceRcPtr *cs)
{
return impl->colorSpaceIsData(cs);
}
void OCIO_colorSpaceIsBuiltin(OCIO_ConstConfigRcPtr *config,
OCIO_ConstColorSpaceRcPtr *cs,
bool *is_scene_linear,
bool *is_srgb)
{
impl->colorSpaceIsBuiltin(config, cs, *is_scene_linear, *is_srgb);
}
void OCIO_colorSpaceRelease(OCIO_ConstColorSpaceRcPtr *cs)
{
impl->colorSpaceRelease(cs);
}
OCIO_ConstProcessorRcPtr *OCIO_configGetProcessorWithNames(OCIO_ConstConfigRcPtr *config,
const char *srcName,
const char *dstName)
{
return impl->configGetProcessorWithNames(config, srcName, dstName);
}
void OCIO_processorRelease(OCIO_ConstProcessorRcPtr *processor)
{
impl->processorRelease(processor);
}
OCIO_ConstCPUProcessorRcPtr *OCIO_processorGetCPUProcessor(OCIO_ConstProcessorRcPtr *processor)
{
return impl->processorGetCPUProcessor(processor);
}
bool OCIO_cpuProcessorIsNoOp(OCIO_ConstCPUProcessorRcPtr *cpu_processor)
{
return impl->cpuProcessorIsNoOp(cpu_processor);
}
void OCIO_cpuProcessorApply(OCIO_ConstCPUProcessorRcPtr *cpu_processor, OCIO_PackedImageDesc *img)
{
impl->cpuProcessorApply(cpu_processor, img);
}
void OCIO_cpuProcessorApply_predivide(OCIO_ConstCPUProcessorRcPtr *cpu_processor,
OCIO_PackedImageDesc *img)
{
impl->cpuProcessorApply_predivide(cpu_processor, img);
}
void OCIO_cpuProcessorApplyRGB(OCIO_ConstCPUProcessorRcPtr *cpu_processor, float *pixel)
{
impl->cpuProcessorApplyRGB(cpu_processor, pixel);
}
void OCIO_cpuProcessorApplyRGBA(OCIO_ConstCPUProcessorRcPtr *cpu_processor, float *pixel)
{
impl->cpuProcessorApplyRGBA(cpu_processor, pixel);
}
void OCIO_cpuProcessorApplyRGBA_predivide(OCIO_ConstCPUProcessorRcPtr *processor, float *pixel)
{
impl->cpuProcessorApplyRGBA_predivide(processor, pixel);
}
void OCIO_cpuProcessorRelease(OCIO_ConstCPUProcessorRcPtr *cpu_processor)
{
impl->cpuProcessorRelease(cpu_processor);
}
const char *OCIO_colorSpaceGetName(OCIO_ConstColorSpaceRcPtr *cs)
{
return impl->colorSpaceGetName(cs);
}
const char *OCIO_colorSpaceGetDescription(OCIO_ConstColorSpaceRcPtr *cs)
{
return impl->colorSpaceGetDescription(cs);
}
const char *OCIO_colorSpaceGetFamily(OCIO_ConstColorSpaceRcPtr *cs)
{
return impl->colorSpaceGetFamily(cs);
}
int OCIO_colorSpaceGetNumAliases(OCIO_ConstColorSpaceRcPtr *cs)
{
return impl->colorSpaceGetNumAliases(cs);
}
const char *OCIO_colorSpaceGetAlias(OCIO_ConstColorSpaceRcPtr *cs, const int index)
{
return impl->colorSpaceGetAlias(cs, index);
}
OCIO_ConstProcessorRcPtr *OCIO_createDisplayProcessor(OCIO_ConstConfigRcPtr *config,
const char *input,
const char *view,
const char *display,
const char *look,
const float scale,
const float exponent,
const float temperature,
const float tint,
const bool use_white_balance,
const bool inverse)
{
return impl->createDisplayProcessor(config,
input,
view,
display,
look,
scale,
exponent,
temperature,
tint,
use_white_balance,
inverse);
}
OCIO_PackedImageDesc *OCIO_createOCIO_PackedImageDesc(float *data,
long width,
long height,
long numChannels,
long chanStrideBytes,
long xStrideBytes,
long yStrideBytes)
{
return impl->createOCIO_PackedImageDesc(
data, width, height, numChannels, chanStrideBytes, xStrideBytes, yStrideBytes);
}
void OCIO_PackedImageDescRelease(OCIO_PackedImageDesc *id)
{
impl->OCIO_PackedImageDescRelease(id);
}
bool OCIO_supportGPUShader()
{
return impl->supportGPUShader();
}
bool OCIO_gpuDisplayShaderBind(OCIO_ConstConfigRcPtr *config,
const char *input,
const char *view,
const char *display,
const char *look,
OCIO_CurveMappingSettings *curve_mapping_settings,
const float scale,
const float exponent,
const float dither,
const float temperature,
const float tint,
const bool use_predivide,
const bool use_overlay,
const bool use_hdr,
const bool use_white_balance)
{
return impl->gpuDisplayShaderBind(config,
input,
view,
display,
look,
curve_mapping_settings,
scale,
exponent,
dither,
temperature,
tint,
use_predivide,
use_overlay,
use_hdr,
use_white_balance);
}
void OCIO_gpuDisplayShaderUnbind()
{
impl->gpuDisplayShaderUnbind();
}
void OCIO_gpuCacheFree()
{
impl->gpuCacheFree();
}
const char *OCIO_getVersionString()
{
return impl->getVersionString();
}
int OCIO_getVersionHex()
{
return impl->getVersionHex();
}