Blender V5.3
colorspace.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2026 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "util/colorspace.h"
6#include "util/color.h"
7#include "util/image.h"
8#include "util/log.h"
9#include "util/map.h"
10#include "util/math.h"
11#include "util/string.h"
12#include "util/tbb.h"
13#include "util/thread.h"
14#include "util/transform.h"
15#include "util/vector.h"
16
17#ifdef WITH_OCIO
18# include <OpenColorIO/OpenColorIO.h>
19namespace OCIO = OCIO_NAMESPACE;
20#endif
21
23
24/* Builtin colorspaces. */
26ustring u_colorspace_data("data");
27ustring u_colorspace_scene_linear("scene_linear");
28ustring u_colorspace_scene_linear_srgb("scene_linear_srgb");
29ustring u_colorspace_srgb("__builtin_srgb");
30
31/* Cached data. */
34
35#ifdef WITH_OCIO
36static thread_mutex cache_processors_mutex;
37static unordered_map<ustring, OCIO::ConstProcessorRcPtr> cache_processors;
38
39static thread_mutex cache_colorspaces_mutex;
40static unordered_map<ustring, ustring> cached_colorspaces;
41
42static thread_mutex cache_scene_linear_interop_id_mutex;
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 = "";
46
47/* Config the caches were built for, holding a reference so a new config can't
48 * reuse its address. */
49static OCIO::ConstConfigRcPtr cache_config;
50
51static void check_invalidate_caches()
52{
53 static thread_mutex cache_scene_linear_mutex;
54 static string cache_scene_linear_name;
55
56 /* Invalidate cached processors and colorspace, in case Blender changed the
57 * scene linear space or switched to another config.
58 * Note this should not happen during rendering, all render should be stopped
59 * before it is changed. */
60 const thread_scoped_lock cache_scene_linear_lock(cache_scene_linear_mutex);
61 OCIO::ConstConfigRcPtr config = nullptr;
62 try {
63 config = OCIO::GetCurrentConfig();
64 }
65 catch (const OCIO::Exception &exception) {
66 LOG_ERROR << "OCIO config error: " << exception.what();
67 return;
68 }
69
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()))
73 {
74 cache_config = config;
75 cache_scene_linear_name = scene_linear_colorspace->getName();
76 {
77 const thread_scoped_lock cache_processors_lock(cache_processors_mutex);
78 cache_processors.clear();
79 }
80 {
81 const thread_scoped_lock cache_lock(cache_colorspaces_mutex);
82 cached_colorspaces.clear();
83 }
84 {
85 const thread_scoped_lock cache_lock(cache_scene_linear_interop_id_mutex);
86 cache_scene_linear_interop_id_done = false;
87 cache_scene_linear_interop_id = "";
88 cache_scene_linear_srgb_interop_id = "";
89 }
90 {
93 }
94 }
95}
96#endif
97
98ColorSpaceProcessor *ColorSpaceManager::get_processor(ustring colorspace)
99{
100#ifdef WITH_OCIO
101 /* Only use this for OpenColorIO color spaces, not the builtin ones. */
102 assert(colorspace != u_colorspace_scene_linear_srgb && colorspace != u_colorspace_auto);
103
104 if (colorspace == u_colorspace_data || colorspace == u_colorspace_scene_linear) {
105 return nullptr;
106 }
107
108 OCIO::ConstConfigRcPtr config = nullptr;
109 try {
110 config = OCIO::GetCurrentConfig();
111 }
112 catch (const OCIO::Exception &exception) {
113 LOG_WARNING << "OCIO config error: " << exception.what();
114 return nullptr;
115 }
116
117 if (!config) {
118 return nullptr;
119 }
120
121 /* Outside the mutex lock. This will also invalidate the cache if
122 * scene linear colorspace changed. */
123 const char *scene_linear_interop_id = get_scene_linear_interop_id();
124
125 /* Cache processor until free_memory(), memory overhead is expected to be
126 * small and the processor is likely to be reused. */
127 const thread_scoped_lock cache_processors_lock(cache_processors_mutex);
128 if (!cache_processors.contains(colorspace)) {
129 try {
130 if (colorspace == u_colorspace_srgb) {
131 /* The sRGB transfer function is handled separately in to_scene_linear, here
132 * we only need the matrix transform from Linear Rec.709 to scene_linear. */
133 if (strcmp(scene_linear_interop_id, "lin_rec709_scene") == 0) {
134 cache_processors[colorspace] = nullptr;
135 }
136 else {
139 const double data[16] = {rec709_to_rgb.x.x,
151 0.0f,
152 0.0f,
153 0.0f,
154 1.0f};
155 OCIO::MatrixTransformRcPtr tfm = OCIO::MatrixTransform::Create();
156 tfm->setMatrix(data);
157 cache_processors[colorspace] = config->getProcessor(tfm, OCIO::TRANSFORM_DIR_FORWARD);
158 }
159 }
160 else {
161 cache_processors[colorspace] = config->getProcessor(colorspace.c_str(), "scene_linear");
162 }
163 }
164 catch (const OCIO::Exception &exception) {
165 cache_processors[colorspace] = OCIO::ConstProcessorRcPtr();
166 LOG_WARNING << "Colorspace " << colorspace.c_str()
167 << " can't be converted to scene_linear: " << exception.what();
168 }
169 }
170
171 const OCIO::Processor *processor = cache_processors[colorspace].get();
172 return (ColorSpaceProcessor *)processor;
173#else
174 /* No OpenColorIO. */
175 (void)colorspace;
176 return nullptr;
177#endif
178}
179
181{
183 return true;
184 }
187 {
188 return false;
189 }
190
191#ifdef WITH_OCIO
192 OCIO::ConstConfigRcPtr config = nullptr;
193 try {
194 config = OCIO::GetCurrentConfig();
195 }
196 catch (const OCIO::Exception &exception) {
197 LOG_WARNING << "OCIO config error: " << exception.what();
198 return false;
199 }
200
201 if (!config) {
202 return false;
203 }
204
205 try {
206 const OCIO::ConstColorSpaceRcPtr space = config->getColorSpace(colorspace.c_str());
207 return space && space->isData();
208 }
209 catch (const OCIO::Exception &) {
210 return false;
211 }
212#else
213 return false;
214#endif
215}
216
218{
220 return nullptr;
221 }
223 return "data";
224 }
226 return "srgb_rec709_scene";
227 }
229 const char *interop_id = get_scene_linear_interop_id(false);
230 if (strcmp(interop_id, "unknown") != 0) {
231 return interop_id;
232 }
233 }
235 const char *interop_id = get_scene_linear_interop_id(true);
236 if (strcmp(interop_id, "unknown") != 0) {
237 return interop_id;
238 }
239 }
240
241#ifdef WITH_OCIO
242 OCIO::ConstConfigRcPtr config = nullptr;
243 try {
244 config = OCIO::GetCurrentConfig();
245 if (!config) {
246 return nullptr;
247 }
248 }
249 catch (const OCIO::Exception &) {
250 return nullptr;
251 }
252
253 try {
254 const OCIO::ConstColorSpaceRcPtr space = config->getColorSpace(colorspace.c_str());
255 if (space) {
256 if (space->isData()) {
257 return "data";
258 }
259 /* From https://github.com/AcademySoftwareFoundation/ColorInterop. */
260 /* TODO: Use native interop ID support in OpenColorIO 2.5. */
261 const char *interop_ids[] = {"lin_rec709_scene",
262 "lin_p3d65_scene",
263 "lin_rec2020_scene",
264 "lin_adobergb_scene",
265 "lin_ap1_scene",
266 "lin_ap0_scene",
267 "lin_ciexyzd65_scene",
268 "srgb_rec709_scene",
269 "g22_rec709_scene",
270 "g18_rec709_scene",
271 "srgb_ap1_scene",
272 "g22_ap1_scene",
273 "srgb_p3d65_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++) {
278 if (string_iequals(space->getAlias(i), interop_id)) {
279 return interop_id;
280 }
281 }
282# else
283 if (space->hasAlias(interop_id)) {
284 return interop_id;
285 }
286# endif
287 }
288 }
289 }
290 catch (const OCIO::Exception &) {
291 return nullptr;
292 }
293#endif
294
295 return nullptr;
296}
297
299 const char *file_colorspace,
300 const char * /*file_format*/,
301 bool is_float)
302{
303#ifdef WITH_OCIO
304 OCIO::ConstConfigRcPtr config = nullptr;
305 try {
306 config = OCIO::GetCurrentConfig();
307 }
308 catch (const OCIO::Exception &exception) {
309 LOG_WARNING << "OCIO config error: " << exception.what();
310 }
311
312 /* Rely on OpenImageIO and OpenColorIO guessed color spaces when available. This relies on
313 * recent OpenImageIO versions supporting interop IDs. */
314 if (config && colorspace == u_colorspace_auto) {
315 if (file_colorspace[0] && config->getColorSpace(file_colorspace)) {
316 colorspace = file_colorspace;
317 }
318 else {
319# if OCIO_VERSION_MAJOR == 2 && OCIO_VERSION_MINOR == 2
320 /* Roles are looked up by index, where -1 means the role is not defined in the config. */
321 int default_float_id = -1;
322 int default_byte_id = -1;
323 int default_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;
328 }
329 else if (strcmp(role_name, "default_byte") == 0) {
330 default_byte_id = i;
331 }
332 else if (strcmp(role_name, "default") == 0) {
333 default_id = i;
334 }
335 }
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);
340 }
341# else
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) :
345 nullptr;
346 if (role_colorspace == nullptr && config->hasRole("default")) {
347 role_colorspace = config->getRoleColorSpace("default");
348 }
349# endif
350 if (role_colorspace) {
351 colorspace = role_colorspace;
352 }
353 }
354 }
355#endif
356
357 /* Fall back to simple guess if we don't have OpenColorIO. */
359 const bool is_srgb = !is_float || strcmp(file_colorspace, "srgb_rec709_scene") == 0 ||
360 strcmp(file_colorspace, "srgb_rec709_display") == 0;
361
362 if (!is_srgb) {
364 }
365 else if (strcmp(get_scene_linear_interop_id(), "lin_rec709_scene") == 0) {
366 /* If working space is Rec.709, scene_linear_srgb is more efficient since
367 * the matrix transform can be skipped. */
369 }
370 else {
372 }
373 }
374
375 /* Builtin colorspaces. */
378 {
379 return colorspace;
380 }
381
383 return u_colorspace_data;
384 }
385
386 /* Use OpenColorIO. */
387#ifdef WITH_OCIO
388 check_invalidate_caches();
389
390 {
391 const thread_scoped_lock cache_lock(cache_colorspaces_mutex);
392 /* Cached lookup. */
393 if (cached_colorspaces.contains(colorspace)) {
394 return cached_colorspaces[colorspace];
395 }
396 }
397
398 /* Detect if it matches a simple builtin colorspace. */
399 bool is_scene_linear;
400 bool is_scene_linear_srgb;
401 is_builtin_colorspace(colorspace, is_scene_linear, is_scene_linear_srgb);
402
403 const thread_scoped_lock cache_lock(cache_colorspaces_mutex);
404 if (is_scene_linear) {
405 LOG_DEBUG << "Colorspace " << colorspace.string() << " is no-op";
406 cached_colorspaces[colorspace] = u_colorspace_scene_linear;
408 }
409 if (is_scene_linear_srgb) {
410 LOG_DEBUG << "Colorspace " << colorspace.string() << " is scene linear sRGB";
411 cached_colorspaces[colorspace] = u_colorspace_scene_linear_srgb;
413 }
414
415 /* Verify if we can convert from the requested color space. */
416 if (!get_processor(colorspace)) {
417 if (!config || !config->getColorSpace(colorspace.c_str())) {
418 LOG_WARNING << "Colorspace " << colorspace.c_str()
419 << " not found, using scene linear instead";
420 }
421 else {
422 LOG_WARNING << "Colorspace " << colorspace.c_str()
423 << " can't be converted to scene_linear, using scene linear instead";
424 }
425 cached_colorspaces[colorspace] = u_colorspace_scene_linear;
427 }
428
429 /* Convert to/from colorspace with OpenColorIO. */
430 LOG_DEBUG << "Colorspace " << colorspace.string() << " handled through OpenColorIO";
431 cached_colorspaces[colorspace] = colorspace;
432 return colorspace;
433#else
434 LOG_WARNING << "Colorspace " << colorspace.c_str()
435 << " not available, built without OpenColorIO";
437#endif
438}
439
440void ColorSpaceManager::is_builtin_colorspace(ustring colorspace,
441 bool &is_scene_linear,
442 bool &is_scene_linear_srgb)
443{
444#ifdef WITH_OCIO
445 const OCIO::Processor *processor = (const OCIO::Processor *)get_processor(colorspace);
446 if (!processor) {
447 is_scene_linear = false;
448 is_scene_linear_srgb = false;
449 return;
450 }
451
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;
457
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);
466
467 /* Make sure that there is no channel crosstalk. */
468 if (fabsf(cR[1]) > 1e-5f || fabsf(cR[2]) > 1e-5f || fabsf(cG[0]) > 1e-5f ||
469 fabsf(cG[2]) > 1e-5f || fabsf(cB[0]) > 1e-5f || fabsf(cB[1]) > 1e-5f)
470 {
471 is_scene_linear = false;
472 is_scene_linear_srgb = false;
473 break;
474 }
475 /* Make sure that the three primaries combine linearly. */
476 if (!compare_floats(cR[0], cW[0], 1e-6f, 64) || !compare_floats(cG[1], cW[1], 1e-6f, 64) ||
477 !compare_floats(cB[2], cW[2], 1e-6f, 64))
478 {
479 is_scene_linear = false;
480 is_scene_linear_srgb = false;
481 break;
482 }
483 /* Make sure that the three channels behave identically. */
484 if (!compare_floats(cW[0], cW[1], 1e-6f, 64) || !compare_floats(cW[1], cW[2], 1e-6f, 64)) {
485 is_scene_linear = false;
486 is_scene_linear_srgb = false;
487 break;
488 }
489
490 const float out_v = average(make_float3(cW[0], cW[1], cW[2]));
491 if (!compare_floats(v, out_v, 1e-6f, 64)) {
492 is_scene_linear = false;
493 }
494 if (!compare_floats(color_srgb_to_linear(v), out_v, 1e-4f, 64)) {
495 is_scene_linear_srgb = false;
496 }
497 }
498#else
499 (void)colorspace;
500 is_scene_linear = false;
501 is_scene_linear_srgb = false;
502#endif
503}
504
505#ifdef WITH_OCIO
506
507template<typename T> inline float4 cast_to_float4(T *data)
508{
512 util_image_cast_to_float(data[3]));
513}
514
515template<typename T> inline void cast_from_float4(T *data, const float4 value)
516{
521}
522
523/* Slower versions for other all data types, which needs to convert to float and back. */
524template<typename T, bool compress_as_srgb, bool expand_from_srgb>
525inline void processor_apply_pixels_rgba(const OCIO::Processor *processor,
526 T *pixels,
527 const int64_t width,
528 const int64_t height,
529 const int64_t y_stride,
530 const bool ignore_alpha)
531{
532 /* TODO: implement faster version for when we know the conversion
533 * is a simple matrix transform between linear spaces. In that case
534 * un-premultiply is not needed. */
535 const OCIO::ConstCPUProcessorRcPtr device_processor = (processor) ?
536 processor->getDefaultCPUProcessor() :
537 nullptr;
538
539 /* Process large images in parallel chunks to keep temporary memory requirement down. */
540 const int64_t chunk_rows = divide_up(std::min((int64_t)(1024 * 1024), width * height), width);
541 const int64_t num_chunks = divide_up(height, chunk_rows);
542
543 parallel_for((int64_t)0, num_chunks, [&](const int64_t chunk) {
544 const int64_t row = chunk * chunk_rows;
545 const int64_t num_rows = std::min(chunk_rows, height - row);
546 vector<float4> float_pixels(num_rows * width);
547
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);
553
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;
559 }
560
561 if (expand_from_srgb) {
562 value = color_srgb_to_linear_v4(value);
563 }
564
565 *float_pixel = value;
566 }
567 }
568
569 if (processor) {
570 const OCIO::PackedImageDesc desc((float *)float_pixels.data(), num_rows * width, 1, 4);
571 device_processor->apply(desc);
572 }
573
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;
579
580 if (compress_as_srgb) {
581 value = color_linear_to_srgb_v4(value);
582 }
583
584 if (!ignore_alpha && !(value.w <= 0.0f || value.w == 1.0f)) {
585 value.x *= value.w;
586 value.y *= value.w;
587 value.z *= value.w;
588 }
589
590 cast_from_float4(pixel, value);
591 }
592 }
593 });
594}
595
596template<typename T, bool compress_as_srgb, bool expand_from_srgb>
597inline void processor_apply_pixels_grayscale(const OCIO::Processor *processor,
598 T *pixels,
599 const int64_t width,
600 const int64_t height,
601 const int64_t y_stride)
602{
603 const OCIO::ConstCPUProcessorRcPtr device_processor = (processor) ?
604 processor->getDefaultCPUProcessor() :
605 nullptr;
606
607 /* Process large images in parallel chunks to keep temporary memory requirement down. */
608 const int64_t chunk_rows = divide_up(std::min((int64_t)(1024 * 1024), width * height), width);
609 const int64_t num_chunks = divide_up(height, chunk_rows);
610
611 parallel_for((int64_t)0, num_chunks, [&](const int64_t chunk) {
612 const int64_t row = chunk * chunk_rows;
613 const int64_t num_rows = std::min(chunk_rows, height - row);
614 vector<float> float_pixels(num_rows * width * 3);
615
616 /* Convert to 3 channels, since that's the minimum required by OpenColorIO. */
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) {
621 float f = util_image_cast_to_float<T>(*pixel);
622 if (expand_from_srgb) {
624 }
625 float_pixel[0] = f;
626 float_pixel[1] = f;
627 float_pixel[2] = f;
628 }
629 }
630
631 if (processor) {
632 const OCIO::PackedImageDesc desc((float *)float_pixels.data(), num_rows * width, 1, 3);
633 device_processor->apply(desc);
634 }
635
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) {
640 float f = average(make_float3(float_pixel[0], float_pixel[1], float_pixel[2]));
641 if (compress_as_srgb) {
643 }
645 }
646 }
647 });
648}
649
650template<typename T, bool compress_as_srgb, bool expand_from_srgb>
651inline void processor_apply_pixels(const OCIO::Processor *processor,
652 T *pixels,
653 const int64_t width,
654 const int64_t height,
655 const int64_t y_stride,
656 const bool is_rgba,
657 const bool ignore_alpha)
658{
659 if (width == 0 || height == 0) {
660 return;
661 }
662
663 if (is_rgba) {
664 processor_apply_pixels_rgba<T, compress_as_srgb, expand_from_srgb>(
665 processor, pixels, width, height, y_stride, ignore_alpha);
666 }
667 else {
668 processor_apply_pixels_grayscale<T, compress_as_srgb, expand_from_srgb>(
669 processor, pixels, width, height, y_stride);
670 }
671}
672
673#endif
674
675template<typename T>
677 T *pixels,
678 const int64_t width,
679 const int64_t height,
680 const int64_t y_stride,
681 bool is_rgba,
682 bool compress_as_srgb,
683 bool ignore_alpha)
684{
685#ifdef WITH_OCIO
686 const OCIO::Processor *processor = (const OCIO::Processor *)get_processor(colorspace);
687
688 /* The processor for sRGB is only a matrix on the primaries, so the sRGB transfer
689 * function has to be undone before it. */
690 const bool expand_from_srgb = (colorspace == u_colorspace_srgb);
691
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);
696 }
697 else {
698 processor_apply_pixels<T, false, true>(
699 processor, pixels, width, height, y_stride, is_rgba, ignore_alpha);
700 }
701 }
702 else if (compress_as_srgb) {
703 /* Compress output as sRGB. */
704 processor_apply_pixels<T, true, false>(
705 processor, pixels, width, height, y_stride, is_rgba, ignore_alpha);
706 }
707 else {
708 /* Write output as scene linear directly. */
709 processor_apply_pixels<T, false, false>(
710 processor, pixels, width, height, y_stride, is_rgba, ignore_alpha);
711 }
712#else
713 (void)colorspace;
714 (void)pixels;
715 (void)width;
716 (void)height;
717 (void)y_stride;
718 (void)is_rgba;
719 (void)compress_as_srgb;
720 (void)ignore_alpha;
721#endif
722}
723
725{
726#ifdef WITH_OCIO
727 map_free_memory(cached_colorspaces);
728 map_free_memory(cache_processors);
729 cache_config.reset();
730#endif
731}
732
734{
735#ifdef WITH_OCIO
736 OCIO::SetCurrentConfig(OCIO::Config::CreateRaw());
737#endif
738}
739
740#ifdef WITH_OCIO
741static bool to_scene_linear_transform(OCIO::ConstConfigRcPtr &config,
742 const char *colorspace,
743 Transform &to_scene_linear)
744{
745 OCIO::ConstProcessorRcPtr processor;
746 try {
747 processor = config->getProcessor("scene_linear", colorspace);
748 }
749 catch (OCIO::Exception &) {
750 return false;
751 }
752
753 if (!processor) {
754 return false;
755 }
756
757 const OCIO::ConstCPUProcessorRcPtr device_processor = processor->getDefaultCPUProcessor();
758 if (!device_processor) {
759 return false;
760 }
761
763 device_processor->applyRGB(&to_scene_linear.x.x);
764 device_processor->applyRGB(&to_scene_linear.y.x);
765 device_processor->applyRGB(&to_scene_linear.z.x);
767 return true;
768}
769#endif
770
772{
773 /* Default to ITU-BT.709 in case no appropriate transform found.
774 * Note XYZ here is defined as having a D65 white point.
775 *
776 * Dumped from OpenColorIO so it matches those transforms exactly, same as
777 * XYZ_TO_REC709 in Blender. */
778 return make_transform(3.2409699f,
779 -1.5373832f,
780 -0.4986108f,
781 0.0f,
782 -0.9692436f,
783 1.8759675f,
784 0.0415551f,
785 0.0f,
786 0.0556301f,
787 -0.2039770f,
788 1.0569715f,
789 0.0f);
790}
791
793{
794 return make_transform(1.7166512f,
795 -0.3556708f,
796 -0.2533663f,
797 0.0f,
798 -0.6666844,
799 1.6164812f,
800 0.0157685f,
801 0.0f,
802 0.0176399f,
803 -0.0427706f,
804 0.9421031f,
805 0.0f);
806}
807
809{
810 return transform_inverse(make_transform(0.652238f,
811 0.128237f,
812 0.169983f,
813 0.0f,
814 0.267672f,
815 0.674340f,
816 0.057988f,
817 0.0f,
818 -0.005382f,
819 0.001369f,
820 1.093071f,
821 0.0f));
822}
823
825{
827
828#ifdef WITH_OCIO
829 /* Get from OpenColorO config if it has the required roles. */
830 OCIO::ConstConfigRcPtr config = nullptr;
831 try {
832 config = OCIO::GetCurrentConfig();
833 }
834 catch (OCIO::Exception &exception) {
835 LOG_WARNING << "OCIO config error: " << exception.what();
836 return xyz_to_rgb;
837 }
838
839 if (!(config && config->hasRole("scene_linear"))) {
840 return xyz_to_rgb;
841 }
842
843 if (config->hasRole("aces_interchange")) {
844 /* Standard OpenColorIO role, defined as ACES AP0 (ACES2065-1). */
845 Transform aces_to_rgb;
846 if (!to_scene_linear_transform(config, "aces_interchange", aces_to_rgb)) {
847 return xyz_to_rgb;
848 }
849
850 /* This is the OpenColorIO builtin transform:
851 * UTILITY - ACES-AP0_to_CIE-XYZ-D65_BFD. */
852 const Transform ACES_AP0_to_xyz_D65 = make_transform(0.938280f,
853 -0.004451f,
854 0.016628f,
855 0.000000f,
856 0.337369f,
857 0.729522f,
858 -0.066890f,
859 0.000000f,
860 0.001174f,
861 -0.003711f,
862 1.091595f,
863 0.000000f);
864 const Transform xyz_to_aces = transform_inverse(ACES_AP0_to_xyz_D65);
865 xyz_to_rgb = aces_to_rgb * xyz_to_aces;
866 return xyz_to_rgb;
867 }
868
869 if (config->hasRole("XYZ")) {
870 /* Custom role used before the standard existed. */
871 if (to_scene_linear_transform(config, "XYZ", xyz_to_rgb)) {
872 return xyz_to_rgb;
873 }
874 }
875
876 /* No reference role found to determine XYZ. */
877#endif
878
879 return xyz_to_rgb;
880}
881
883{
884#ifdef WITH_OCIO
885 /* Clear the cached hash if the scene linear colorspace changed. */
886 check_invalidate_caches();
887#endif
888
889 /* NOTE: Be careful not to change existing hashes if at all possible, as this
890 * will cause all texture files to be regenerated with significantly increased
891 * disk usage. */
893
894 if (cache_xyz_to_scene_linear_hash.empty()) {
895 /* TODO: Verify this results in the same hash across platforms. */
898 "%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g_%.4g",
899 xyz_to_rgb.x.x,
900 xyz_to_rgb.x.y,
901 xyz_to_rgb.x.z,
902 xyz_to_rgb.x.w,
903 xyz_to_rgb.y.x,
904 xyz_to_rgb.y.y,
905 xyz_to_rgb.y.z,
906 xyz_to_rgb.y.w,
907 xyz_to_rgb.z.x,
908 xyz_to_rgb.z.y,
909 xyz_to_rgb.z.z,
910 xyz_to_rgb.z.w);
911 }
912
914}
915
916const char *ColorSpaceManager::get_scene_linear_interop_id(const bool srgb_encoded)
917{
918#ifdef WITH_OCIO
919 check_invalidate_caches();
920
921 const thread_scoped_lock cache_lock(cache_scene_linear_interop_id_mutex);
922
923 if (!cache_scene_linear_interop_id_done) {
925
927 cache_scene_linear_interop_id = "lin_rec709_scene";
928 cache_scene_linear_srgb_interop_id = "srgb_rec709_scene";
929 }
931 cache_scene_linear_interop_id = "lin_rec2020_scene";
932 cache_scene_linear_srgb_interop_id = "srgb_rec2020_scene";
933 }
935 cache_scene_linear_interop_id = "lin_ap1_scene";
936 cache_scene_linear_srgb_interop_id = "srgb_ap1_scene";
937 }
938 else {
939 cache_scene_linear_interop_id = "unknown";
940 cache_scene_linear_srgb_interop_id = "unknown";
941 }
942
943 cache_scene_linear_interop_id_done = true;
944 }
945
946 return (srgb_encoded) ? cache_scene_linear_srgb_interop_id : cache_scene_linear_interop_id;
947#else
948 (void)srgb_encoded;
949 return "lin_rec709_scene";
950#endif
951}
952
953/* Template instantiations so we don't have to inline functions. */
955 ustring, uchar *, int64_t, int64_t, int64_t, bool, bool, bool);
957 ustring, ushort *, int64_t, int64_t, int64_t, bool, bool, bool);
959 ustring, half *, int64_t, int64_t, int64_t, bool, bool, bool);
961 ustring, float *, int64_t, int64_t, int64_t, bool, bool, bool);
962
long long int int64_t
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)
Definition half.h:29
ccl_device float color_srgb_to_linear(const float c)
Definition color.h:51
ccl_device float4 color_srgb_to_linear_v4(const float4 c)
Definition color.h:335
ccl_device float4 color_linear_to_srgb_v4(const float4 c)
Definition color.h:329
ccl_device float color_linear_to_srgb(const float c)
Definition color.h:59
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
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define assert(assertion)
unsigned int uchar
float average(float2 a)
ccl_device float3 rec709_to_rgb(KernelGlobals kg, const float3 rec709)
#define LOG_DEBUG
Definition log.h:107
#define LOG_ERROR
Definition log.h:101
#define LOG_WARNING
Definition log.h:103
ccl_device_inline bool compare_floats(const float a, const float b, float abs_diff, const int ulp_diff)
Definition math_base.h:815
#define T
BLI_INLINE ColorSceneLinear4f< eAlpha::Straight > to_scene_linear(const ColorTheme4f &theme4f)
Definition BLI_color.hh:126
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
ATTR_WARN_UNUSED_RESULT const BMVert * v
BMesh const char void * data
color xyz_to_rgb(float x, float y, float z)
Definition node_color.h:73
#define fabsf
#define make_float4
bool string_iequals(const string &a, const string &b)
Definition string.cpp:57
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
std::mutex thread_mutex
Definition thread.h:27
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:28
CCL_NAMESPACE_BEGIN Transform transform_transposed_inverse(const Transform &tfm)
Definition transform.cpp:15
ccl_device_inline Transform transform_identity()
Definition transform.h:355
ccl_device_inline bool transform_equal_threshold(const Transform &A, const Transform &B, const float threshold)
Definition transform.h:372
ccl_device_inline Transform transform_inverse(const Transform tfm)
Definition transform.h:563
ccl_device_inline Transform make_transform(const float a, const float b, const float c, const float d, const float e, const float f, const float g, const float h, const float i, const float j, const float k, const float l)
Definition transform.h:185
ccl_device_inline size_t divide_up(const size_t x, const size_t y)
Definition types_base.h:54
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