5#include "util/colorspace.h"
18# include <OpenColorIO/OpenColorIO.h>
19namespace OCIO = OCIO_NAMESPACE;
37static unordered_map<ustring, OCIO::ConstProcessorRcPtr> cache_processors;
40static unordered_map<ustring, ustring> cached_colorspaces;
43static bool cache_scene_linear_interop_id_done =
false;
44static const char *cache_scene_linear_interop_id =
"";
45static const char *cache_scene_linear_srgb_interop_id =
"";
49static OCIO::ConstConfigRcPtr cache_config;
51static void check_invalidate_caches()
54 static string cache_scene_linear_name;
61 OCIO::ConstConfigRcPtr config =
nullptr;
63 config = OCIO::GetCurrentConfig();
65 catch (
const OCIO::Exception &exception) {
66 LOG_ERROR <<
"OCIO config error: " << exception.what();
70 const OCIO::ConstColorSpaceRcPtr scene_linear_colorspace = config->getColorSpace(
"scene_linear");
71 if (scene_linear_colorspace &&
72 (cache_config != config || cache_scene_linear_name != scene_linear_colorspace->getName()))
74 cache_config = config;
75 cache_scene_linear_name = scene_linear_colorspace->getName();
78 cache_processors.clear();
82 cached_colorspaces.clear();
86 cache_scene_linear_interop_id_done =
false;
87 cache_scene_linear_interop_id =
"";
88 cache_scene_linear_srgb_interop_id =
"";
98ColorSpaceProcessor *ColorSpaceManager::get_processor(ustring
colorspace)
108 OCIO::ConstConfigRcPtr config =
nullptr;
110 config = OCIO::GetCurrentConfig();
112 catch (
const OCIO::Exception &exception) {
113 LOG_WARNING <<
"OCIO config error: " << exception.what();
128 if (!cache_processors.contains(colorspace)) {
133 if (strcmp(scene_linear_interop_id,
"lin_rec709_scene") == 0) {
134 cache_processors[colorspace] =
nullptr;
155 OCIO::MatrixTransformRcPtr tfm = OCIO::MatrixTransform::Create();
156 tfm->setMatrix(data);
157 cache_processors[colorspace] = config->getProcessor(tfm, OCIO::TRANSFORM_DIR_FORWARD);
161 cache_processors[colorspace] = config->getProcessor(colorspace.c_str(),
"scene_linear");
164 catch (
const OCIO::Exception &exception) {
165 cache_processors[colorspace] = OCIO::ConstProcessorRcPtr();
167 <<
" can't be converted to scene_linear: " << exception.what();
171 const OCIO::Processor *processor = cache_processors[colorspace].get();
172 return (ColorSpaceProcessor *)processor;
192 OCIO::ConstConfigRcPtr config =
nullptr;
194 config = OCIO::GetCurrentConfig();
196 catch (
const OCIO::Exception &exception) {
197 LOG_WARNING <<
"OCIO config error: " << exception.what();
206 const OCIO::ConstColorSpaceRcPtr space = config->getColorSpace(
colorspace.c_str());
207 return space && space->isData();
209 catch (
const OCIO::Exception &) {
226 return "srgb_rec709_scene";
230 if (strcmp(interop_id,
"unknown") != 0) {
236 if (strcmp(interop_id,
"unknown") != 0) {
242 OCIO::ConstConfigRcPtr config =
nullptr;
244 config = OCIO::GetCurrentConfig();
249 catch (
const OCIO::Exception &) {
254 const OCIO::ConstColorSpaceRcPtr space = config->getColorSpace(
colorspace.c_str());
256 if (space->isData()) {
261 const char *interop_ids[] = {
"lin_rec709_scene",
264 "lin_adobergb_scene",
267 "lin_ciexyzd65_scene",
274 "g22_adobergb_scene"};
275 for (
const char *interop_id : interop_ids) {
276# if OCIO_VERSION_MAJOR == 2 && OCIO_VERSION_MINOR == 2
277 for (
int i = 0; i < space->getNumAliases(); i++) {
283 if (space->hasAlias(interop_id)) {
290 catch (
const OCIO::Exception &) {
299 const char *file_colorspace,
304 OCIO::ConstConfigRcPtr config =
nullptr;
306 config = OCIO::GetCurrentConfig();
308 catch (
const OCIO::Exception &exception) {
309 LOG_WARNING <<
"OCIO config error: " << exception.what();
315 if (file_colorspace[0] && config->getColorSpace(file_colorspace)) {
319# if OCIO_VERSION_MAJOR == 2 && OCIO_VERSION_MINOR == 2
321 int default_float_id = -1;
322 int default_byte_id = -1;
324 for (
int i = 0; i < config->getNumRoles(); i++) {
325 const char *role_name = config->getRoleName(i);
326 if (strcmp(role_name,
"default_float") == 0) {
327 default_float_id = i;
329 else if (strcmp(role_name,
"default_byte") == 0) {
332 else if (strcmp(role_name,
"default") == 0) {
336 const int role_id = (is_float) ? default_float_id : default_byte_id;
337 const char *role_colorspace = (role_id != -1) ? config->getRoleColorSpace(role_id) :
nullptr;
338 if (role_colorspace ==
nullptr && default_id != -1) {
339 role_colorspace = config->getRoleColorSpace(default_id);
342 const char *role_name = (is_float) ?
"default_float" :
"default_byte";
343 const char *role_colorspace = config->hasRole(role_name) ?
344 config->getRoleColorSpace(role_name) :
346 if (role_colorspace ==
nullptr && config->hasRole(
"default")) {
347 role_colorspace = config->getRoleColorSpace(
"default");
350 if (role_colorspace) {
359 const bool is_srgb = !is_float || strcmp(file_colorspace,
"srgb_rec709_scene") == 0 ||
360 strcmp(file_colorspace,
"srgb_rec709_display") == 0;
388 check_invalidate_caches();
393 if (cached_colorspaces.contains(
colorspace)) {
399 bool is_scene_linear;
400 bool is_scene_linear_srgb;
401 is_builtin_colorspace(
colorspace, is_scene_linear, is_scene_linear_srgb);
404 if (is_scene_linear) {
409 if (is_scene_linear_srgb) {
417 if (!config || !config->getColorSpace(
colorspace.c_str())) {
419 <<
" not found, using scene linear instead";
423 <<
" can't be converted to scene_linear, using scene linear instead";
435 <<
" not available, built without OpenColorIO";
440void ColorSpaceManager::is_builtin_colorspace(ustring
colorspace,
441 bool &is_scene_linear,
442 bool &is_scene_linear_srgb)
445 const OCIO::Processor *processor = (
const OCIO::Processor *)get_processor(
colorspace);
447 is_scene_linear =
false;
448 is_scene_linear_srgb =
false;
452 const OCIO::ConstCPUProcessorRcPtr device_processor = processor->getDefaultCPUProcessor();
453 is_scene_linear =
true;
454 is_scene_linear_srgb =
true;
455 for (
int i = 0; i < 256; i++) {
456 const float v = i / 255.0f;
458 float cR[3] = {
v, 0, 0};
459 float cG[3] = {0,
v, 0};
460 float cB[3] = {0, 0,
v};
461 float cW[3] = {
v,
v,
v};
462 device_processor->applyRGB(cR);
463 device_processor->applyRGB(cG);
464 device_processor->applyRGB(cB);
465 device_processor->applyRGB(cW);
468 if (
fabsf(cR[1]) > 1e-5f ||
fabsf(cR[2]) > 1e-5f ||
fabsf(cG[0]) > 1e-5f ||
471 is_scene_linear =
false;
472 is_scene_linear_srgb =
false;
479 is_scene_linear =
false;
480 is_scene_linear_srgb =
false;
485 is_scene_linear =
false;
486 is_scene_linear_srgb =
false;
492 is_scene_linear =
false;
495 is_scene_linear_srgb =
false;
500 is_scene_linear =
false;
501 is_scene_linear_srgb =
false;
507template<
typename T>
inline float4 cast_to_float4(
T *data)
515template<
typename T>
inline void cast_from_float4(
T *data,
const float4 value)
524template<
typename T,
bool compress_as_srgb,
bool expand_from_srgb>
525inline void processor_apply_pixels_rgba(
const OCIO::Processor *processor,
530 const bool ignore_alpha)
535 const OCIO::ConstCPUProcessorRcPtr device_processor = (processor) ?
536 processor->getDefaultCPUProcessor() :
544 const int64_t row = chunk * chunk_rows;
545 const int64_t num_rows = std::min(chunk_rows, height - row);
548 for (
int64_t j = 0; j < num_rows; j++) {
549 T *pixel = pixels + (row + j) * y_stride;
550 float4 *float_pixel = float_pixels.data() + j * width;
551 for (
int64_t i = 0; i < width; i++, pixel += 4, float_pixel++) {
552 float4 value = cast_to_float4(pixel);
554 if (!ignore_alpha && !(value.
w <= 0.0f || value.
w == 1.0f)) {
555 const float inv_alpha = 1.0f / value.
w;
556 value.
x *= inv_alpha;
557 value.
y *= inv_alpha;
558 value.
z *= inv_alpha;
561 if (expand_from_srgb) {
565 *float_pixel = value;
570 const OCIO::PackedImageDesc desc((
float *)float_pixels.data(), num_rows * width, 1, 4);
571 device_processor->apply(desc);
574 for (
int64_t j = 0; j < num_rows; j++) {
575 T *pixel = pixels + (row + j) * y_stride;
576 float4 *float_pixel = float_pixels.data() + j * width;
577 for (
int64_t i = 0; i < width; i++, pixel += 4, float_pixel++) {
578 float4 value = *float_pixel;
580 if (compress_as_srgb) {
584 if (!ignore_alpha && !(value.
w <= 0.0f || value.
w == 1.0f)) {
590 cast_from_float4(pixel, value);
596template<
typename T,
bool compress_as_srgb,
bool expand_from_srgb>
597inline void processor_apply_pixels_grayscale(
const OCIO::Processor *processor,
603 const OCIO::ConstCPUProcessorRcPtr device_processor = (processor) ?
604 processor->getDefaultCPUProcessor() :
612 const int64_t row = chunk * chunk_rows;
613 const int64_t num_rows = std::min(chunk_rows, height - row);
617 for (
int64_t j = 0; j < num_rows; j++) {
618 T *pixel = pixels + (row + j) * y_stride;
619 float *float_pixel = float_pixels.data() + j * width * 3;
620 for (
int64_t i = 0; i < width; i++, pixel++, float_pixel += 3) {
622 if (expand_from_srgb) {
632 const OCIO::PackedImageDesc desc((
float *)float_pixels.data(), num_rows * width, 1, 3);
633 device_processor->apply(desc);
636 for (
int64_t j = 0; j < num_rows; j++) {
637 T *pixel = pixels + (row + j) * y_stride;
638 float *float_pixel = float_pixels.data() + j * width * 3;
639 for (
int64_t i = 0; i < width; i++, pixel++, float_pixel += 3) {
641 if (compress_as_srgb) {
650template<
typename T,
bool compress_as_srgb,
bool expand_from_srgb>
651inline void processor_apply_pixels(
const OCIO::Processor *processor,
657 const bool ignore_alpha)
659 if (width == 0 || height == 0) {
664 processor_apply_pixels_rgba<T, compress_as_srgb, expand_from_srgb>(
665 processor, pixels, width, height, y_stride, ignore_alpha);
668 processor_apply_pixels_grayscale<T, compress_as_srgb, expand_from_srgb>(
669 processor, pixels, width, height, y_stride);
682 bool compress_as_srgb,
686 const OCIO::Processor *processor = (
const OCIO::Processor *)get_processor(
colorspace);
692 if (expand_from_srgb) {
693 if (compress_as_srgb) {
694 processor_apply_pixels<T, true, true>(
695 processor, pixels, width, height, y_stride, is_rgba, ignore_alpha);
698 processor_apply_pixels<T, false, true>(
699 processor, pixels, width, height, y_stride, is_rgba, ignore_alpha);
702 else if (compress_as_srgb) {
704 processor_apply_pixels<T, true, false>(
705 processor, pixels, width, height, y_stride, is_rgba, ignore_alpha);
709 processor_apply_pixels<T, false, false>(
710 processor, pixels, width, height, y_stride, is_rgba, ignore_alpha);
719 (void)compress_as_srgb;
727 map_free_memory(cached_colorspaces);
728 map_free_memory(cache_processors);
729 cache_config.reset();
736 OCIO::SetCurrentConfig(OCIO::Config::CreateRaw());
741static bool to_scene_linear_transform(OCIO::ConstConfigRcPtr &config,
745 OCIO::ConstProcessorRcPtr processor;
747 processor = config->getProcessor(
"scene_linear",
colorspace);
749 catch (OCIO::Exception &) {
757 const OCIO::ConstCPUProcessorRcPtr device_processor = processor->getDefaultCPUProcessor();
758 if (!device_processor) {
830 OCIO::ConstConfigRcPtr config =
nullptr;
832 config = OCIO::GetCurrentConfig();
834 catch (OCIO::Exception &exception) {
835 LOG_WARNING <<
"OCIO config error: " << exception.what();
839 if (!(config && config->hasRole(
"scene_linear"))) {
843 if (config->hasRole(
"aces_interchange")) {
846 if (!to_scene_linear_transform(config,
"aces_interchange", aces_to_rgb)) {
869 if (config->hasRole(
"XYZ")) {
871 if (to_scene_linear_transform(config,
"XYZ",
xyz_to_rgb)) {
886 check_invalidate_caches();
898 "%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g",
919 check_invalidate_caches();
923 if (!cache_scene_linear_interop_id_done) {
927 cache_scene_linear_interop_id =
"lin_rec709_scene";
928 cache_scene_linear_srgb_interop_id =
"srgb_rec709_scene";
931 cache_scene_linear_interop_id =
"lin_rec2020_scene";
932 cache_scene_linear_srgb_interop_id =
"srgb_rec2020_scene";
935 cache_scene_linear_interop_id =
"lin_ap1_scene";
936 cache_scene_linear_srgb_interop_id =
"srgb_ap1_scene";
939 cache_scene_linear_interop_id =
"unknown";
940 cache_scene_linear_srgb_interop_id =
"unknown";
943 cache_scene_linear_interop_id_done =
true;
946 return (srgb_encoded) ? cache_scene_linear_srgb_interop_id : cache_scene_linear_interop_id;
949 return "lin_rec709_scene";
static bool colorspace_is_data(ustring colorspace)
static ustring detect_known_colorspace(ustring colorspace, const char *file_colorspace, const char *file_format, bool is_float)
static string get_xyz_to_scene_linear_rgb_string()
static Transform get_xyz_to_scene_linear_rgb()
static Transform get_xyz_to_rec2020()
static void to_scene_linear(ustring colorspace, T *pixels, const int64_t width, const int64_t height, const int64_t y_stride, bool is_rgba, bool compress_as_srgb, bool ignore_alpha)
static Transform get_xyz_to_rec709()
static void free_memory()
static const char * colorspace_interop_id(ustring colorspace)
static void init_fallback_config()
static Transform get_xyz_to_acescg()
static const char * get_scene_linear_interop_id(const bool srgb_encoded=false)
ccl_device float color_srgb_to_linear(const float c)
ccl_device float4 color_srgb_to_linear_v4(const float4 c)
ccl_device float4 color_linear_to_srgb_v4(const float4 c)
ccl_device float color_linear_to_srgb(const float c)
static string cache_xyz_to_scene_linear_hash
static thread_mutex cache_xyz_to_scene_linear_mutex
CCL_NAMESPACE_BEGIN ustring u_colorspace_auto
T util_image_cast_from_float(const float value)
float util_image_cast_to_float(T value)
#define CCL_NAMESPACE_END
#define assert(assertion)
ccl_device float3 rec709_to_rgb(KernelGlobals kg, const float3 rec709)
ccl_device_inline bool compare_floats(const float a, const float b, float abs_diff, const int ulp_diff)
BLI_INLINE ColorSceneLinear4f< eAlpha::Straight > to_scene_linear(const ColorTheme4f &theme4f)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
ATTR_WARN_UNUSED_RESULT const BMVert * v
BMesh const char void * data
color xyz_to_rgb(float x, float y, float z)
bool string_iequals(const string &a, const string &b)
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
std::unique_lock< std::mutex > thread_scoped_lock
ccl_device_inline size_t divide_up(const size_t x, const size_t y)
ustring u_colorspace_scene_linear_srgb
ustring u_colorspace_scene_linear
ustring u_colorspace_srgb
CCL_NAMESPACE_BEGIN ustring u_colorspace_auto
ustring u_colorspace_data