Blender V5.3
colormanagement.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
12
13#include <cmath>
14#include <cstring>
15#include <optional>
16#include <string>
17
18#include "DNA_ID.h"
19#include "DNA_color_types.h"
20#include "DNA_image_types.h"
21#include "DNA_movieclip_types.h"
22#include "DNA_node_types.h"
23#include "DNA_scene_types.h"
24#include "DNA_sequence_types.h"
25#include "DNA_space_types.h"
26
27#include "IMB_filetype.hh"
28#include "IMB_filter.hh"
29#include "IMB_imbuf.hh"
30#include "IMB_imbuf_types.hh"
31
32#include "MEM_guardedalloc.h"
33
34#include "BLI_color.hh"
35#include "BLI_colorspace.hh"
36#include "BLI_fileops.hh"
37#include "BLI_listbase.hh"
38#include "BLI_math_color.hh"
39#include "BLI_math_color_c.hh"
40#include "BLI_math_matrix.hh"
43#include "BLI_path_utils.hh"
44#include "BLI_rect.hh"
45#include "BLI_string.hh"
46#include "BLI_string_ref.hh"
47#include "BLI_string_utf8.hh"
48#include "BLI_task.hh"
49#include "BLI_threads.hh"
50#include "BLI_vector_set.hh"
51
52#include "BKE_appdir.hh"
54#include "BKE_colortools.hh"
55#include "BKE_context.hh"
56#include "BKE_idtype.hh"
57#include "BKE_image_format.hh"
58#include "BKE_library.hh"
59#include "BKE_main.hh"
60#include "BKE_node.hh"
62
63#include "GPU_capabilities.hh"
64
65#include "RNA_define.hh"
66
67#include "SEQ_iterator.hh"
68
69#include "DEG_depsgraph.hh"
70
71#include "CLG_log.h"
72
73#include "OCIO_api.hh"
74
75namespace blender {
76
77static CLG_LogRef LOG = {"color_management"};
78
79/* -------------------------------------------------------------------- */
82
83static void processor_transform_apply_threaded(uchar *byte_buffer,
84 float *float_buffer,
85 int width,
86 int height,
87 int channels,
88 ColormanageProcessor *cm_processor,
89 bool predivide);
90
91/* Lazily init inside function so it gets destructed before guardedalloc leak check. */
92static std::unique_ptr<ocio::Config> &g_config()
93{
94 static std::unique_ptr<ocio::Config> g_config_ = nullptr;
95 return g_config_;
96}
97
99{
100 static VectorSet<StringRefNull> g_all_view_names_;
101 return g_all_view_names_;
102}
103
105 /* Absolute file path or URI. */
106 std::string path;
107 /* Unresolved path with templates. */
108 std::string unresolved_path;
110
112 {
113 return a.source == b.source && a.path == b.path && a.unresolved_path == b.unresolved_path;
114 }
115};
116
117/* The active config. */
119{
120 static ColorManagedConfigPath g_config_active_;
121 return g_config_active_;
122}
123
124/* The config that was requested, possibly not active if failed to load. */
126{
127 static Vector<ColorManagedConfigPath> g_config_requested_;
128 return g_config_requested_;
129}
130
131/* Environment variables at startup, as we modify these ourselves and need them
132 * for determining the appropriate config to load when opening a project. */
134 std::optional<std::string> blender_ocio;
135 std::optional<std::string> ocio;
136};
138{
139 static ColorManagedStartupEnv g_startup_env_;
140 return g_startup_env_;
141}
142
143#define DISPLAY_BUFFER_CHANNELS 4
144
145/* ** list of all supported color spaces, displays and views */
155
156/* Defaults from the config that never change with working space. */
158
160
161/* lock used by pre-cached processors getters, so processor wouldn't
162 * be created several times
163 */
164static pthread_mutex_t processor_lock = BLI_MUTEX_INITIALIZER;
165
167 GlobalGPUState() = default;
168
170 {
171 reset();
172 }
173
174 void reset()
175 {
176 gpu_shader_bound = false;
177 if (curve_mapping) {
179 curve_mapping = nullptr;
180 }
181 orig_curve_mapping = nullptr;
182 use_curve_mapping = false;
184 }
185
186 /* GPU shader currently bound. */
187 bool gpu_shader_bound = false;
188
189 /* Curve mapping. */
191 bool use_curve_mapping = false;
194
197
198 /* Cached processor for color picking conversion. */
199 std::shared_ptr<const ocio::CPUProcessor> cpu_processor_to;
200 std::shared_ptr<const ocio::CPUProcessor> cpu_processor_from;
201 bool failed = false;
203
205
206/* -------------------------------------------------------------------- */
209
211 char *colorspace_name,
212 const char *role,
213 const char *backup_role,
214 const bool optional = false)
215{
216 const ColorSpace *ociocs = config.get_color_space(role);
217
218 if (ociocs == nullptr && backup_role) {
219 ociocs = config.get_color_space(backup_role);
220 }
221
222 if (ociocs == nullptr) {
223 /* Overall fallback role. */
224 ociocs = config.get_color_space("default");
225 }
226
227 if (ociocs == nullptr) {
228 if (!optional) {
229 CLOG_ERROR(&LOG, "Could not find role \"%s\"", role);
230 }
231 colorspace_name[0] = '\0';
232 return false;
233 }
234
235 /* assume function was called with buffer properly allocated to MAX_COLORSPACE_NAME chars */
236 BLI_strncpy_utf8(colorspace_name, ociocs->name().c_str(), MAX_COLORSPACE_NAME);
237 return true;
238}
239
264
270
275static bool colormanage_load_config(const ocio::Config &config, const bool validate_only = false)
276{
277 bool ok = true;
278
279 /* get roles */
280 char role_data[MAX_COLORSPACE_NAME];
281 char role_scene_linear[MAX_COLORSPACE_NAME];
282 char role_color_picking[MAX_COLORSPACE_NAME];
283 char role_texture_painting[MAX_COLORSPACE_NAME];
284 char role_default_sequencer[MAX_COLORSPACE_NAME];
285 char role_default_byte[MAX_COLORSPACE_NAME];
286 char role_default_float[MAX_COLORSPACE_NAME];
287 char role_aces_interchange[MAX_COLORSPACE_NAME];
288 char role_video_rec709[MAX_COLORSPACE_NAME] = "";
289
290 ok &= colormanage_role_color_space_name_get(config, role_data, OCIO_ROLE_DATA, nullptr);
292 config, role_scene_linear, OCIO_ROLE_SCENE_LINEAR, nullptr);
294 config, role_color_picking, OCIO_ROLE_COLOR_PICKING, nullptr);
296 config, role_texture_painting, OCIO_ROLE_TEXTURE_PAINT, nullptr);
298 config, role_default_sequencer, OCIO_ROLE_DEFAULT_SEQUENCER, OCIO_ROLE_SCENE_LINEAR);
300 config, role_default_byte, OCIO_ROLE_DEFAULT_BYTE, OCIO_ROLE_TEXTURE_PAINT);
302 config, role_default_float, OCIO_ROLE_DEFAULT_FLOAT, OCIO_ROLE_SCENE_LINEAR);
303
305 config, role_aces_interchange, OCIO_ROLE_ACES_INTERCHANGE, nullptr, true);
306
307 /* Default to sRGB, see #video_rec709_colorspace. */
308 const ColorSpace *video_rec709 = config.get_color_space(OCIO_ROLE_VIDEO_REC709);
309 if (video_rec709 == nullptr) {
310 video_rec709 = config.get_color_space_by_interop_id("srgb_rec709_display");
311 }
312 if (video_rec709) {
313 STRNCPY_UTF8(role_video_rec709, video_rec709->name().c_str());
314 }
315
316 if (config.get_num_displays() == 0) {
317 CLOG_ERROR(&LOG, "Could not find any displays");
318 ok = false;
319 }
320 /* NOTE: The look "None" is expected to be hard-coded to exist in the OpenColorIO integration. */
321 if (config.get_num_looks() == 0) {
322 CLOG_ERROR(&LOG, "Could not find any looks");
323 ok = false;
324 }
325
326 for (const int display_index : IndexRange(config.get_num_displays())) {
327 const ocio::Display *display = config.get_display_by_index(display_index);
328 if (display->get_num_views() <= 0) {
329 CLOG_ERROR(&LOG, "Could not find any views for display %s", display->name().c_str());
330 ok = false;
331 break;
332 }
333 }
334
335 if (validate_only) {
336 return ok;
337 }
338
340
341 STRNCPY(global_role_data, role_data);
342 STRNCPY(global_role_scene_linear, role_scene_linear);
343 STRNCPY(global_role_color_picking, role_color_picking);
344 STRNCPY(global_role_texture_painting, role_texture_painting);
345 STRNCPY(global_role_default_sequencer, role_default_sequencer);
346 STRNCPY(global_role_default_byte, role_default_byte);
347 STRNCPY(global_role_default_float, role_default_float);
348 STRNCPY(global_role_aces_interchange, role_aces_interchange);
349 STRNCPY(global_role_video_rec709, role_video_rec709);
350
351 for (const int display_index : IndexRange(config.get_num_displays())) {
352 const ocio::Display *display = config.get_display_by_index(display_index);
353 for (const int view_index : IndexRange(display->get_num_views())) {
354 const ocio::View *view = display->get_view_by_index(view_index);
355 g_all_view_names().add(view->name());
356 }
357 }
358
360
361 /* Defaults that don't change with file working space. */
364
365 return ok;
366}
367
369{
371 g_config() = nullptr;
372 g_config_active() = {};
373}
374
375static void colormanage_set_ocio_env(const char *filepath,
376 std::optional<std::string> &r_old_ocio_env)
377{
378 /* OpenColorIO has issues with "$" in paths, as it uses that for variable expansion
379 * and there appears to be no way to escape the symbol.
380 *
381 * Work around it by setting the environment variable, which may also be useful for
382 * plug-ins to inherit the Blender OCIO config. */
383 if (const char *ocio_env = BLI_getenv("OCIO")) {
384 r_old_ocio_env = ocio_env;
385 }
386
387 BLI_setenv("OCIO", filepath);
388}
389
390static void colormanage_restore_ocio_env(const std::optional<std::string> &old_ocio_env)
391{
392 BLI_setenv("OCIO", old_ocio_env.has_value() ? old_ocio_env->c_str() : nullptr);
393}
394
397{
398 /* URI for configs built into OpenColorIO. */
399 if (path.startswith("ocio://")) {
400 return path;
401 }
402
404}
405
407{
409
411 if (env.blender_ocio.has_value()) {
412 if (!env.blender_ocio->empty()) {
413 candidates.append(
415 }
416 }
417 else if (env.ocio.has_value() && !env.ocio->empty()) {
418 candidates.append({*env.ocio, *env.ocio, ColorManagedConfigSource::EnvOCIO});
419 }
420
421 /* Active project. */
422 if (bmain != nullptr) {
424 if (project == nullptr) {
425 return;
426 }
427 const StringRefNull unresolved_path = project->get_ocio_config_path();
428 if (!unresolved_path.is_empty()) {
429 std::string path = colormanage_project_config_path_resolve(unresolved_path, *project);
430 candidates.append({std::move(path), unresolved_path, ColorManagedConfigSource::Project});
431 }
432 });
433 }
434
435 /* Blender config. */
436 const std::optional<std::string> configdir = BKE_appdir_folder_id(BLENDER_DATAFILES,
437 "colormanagement");
438 if (configdir.has_value()) {
439 char configfile[FILE_MAX];
440 BLI_path_join(configfile, sizeof(configfile), configdir->c_str(), BCM_CONFIG_FILE);
441 candidates.append({configfile, configfile, ColorManagedConfigSource::Blender});
442 }
443
444 /* Fallback config. */
445 candidates.append({"", "", ColorManagedConfigSource::Fallback});
446
447 return candidates;
448}
449
451{
452 switch (source) {
454 return "BLENDER_OCIO";
456 return "OCIO";
458 return "project";
460 return "Blender";
462 return "fallback";
463 }
464
466 return "";
467}
468
470{
471 g_config_requested() = candidates;
472
473 for (const ColorManagedConfigPath &candidate : candidates) {
474 /* Already active? */
475 if (g_config() && g_config_active() == candidate) {
476 break;
477 }
478
479 if (candidate.source == ColorManagedConfigSource::Project &&
480 !candidate.unresolved_path.empty() && candidate.path.empty())
481 {
483 &LOG, "Invalid project config path, skipping: %s", candidate.unresolved_path.c_str());
484 continue;
485 }
486
487 const bool is_fallback = candidate.source == ColorManagedConfigSource::Fallback;
488
489 /* Load the config. */
490 std::unique_ptr<ocio::Config> config;
491 std::optional<std::string> old_ocio_env;
492 if (is_fallback) {
494 }
495 else {
496 BLI_assert(!candidate.path.empty());
497 colormanage_set_ocio_env(candidate.path.c_str(), old_ocio_env);
499 }
500
501 /* Validate without touching global state, so a failed candidate leaves the active
502 * config as is. Fallback is always accepted, should never fail in practice. */
503 const auto validate = [&](const ocio::Config &new_config) {
504 return colormanage_load_config(new_config, true) || is_fallback;
505 };
506
507 bool loaded = false;
508 if (config && g_config()) {
509 loaded = g_config()->switch_to(*config, validate);
510 }
511 else if (config && validate(*config)) {
512 g_config() = std::move(config);
513 loaded = true;
514 }
515
516 if (loaded) {
517 /* Load roles, matrices and view names from the new configuration. */
519 g_config_active() = candidate;
520 break;
521 }
522
523 colormanage_restore_ocio_env(old_ocio_env);
524
526 "Failed to load config from %s: %s",
527 colormanage_config_source_identifier(candidate.source),
528 candidate.path.c_str());
529 }
530
532 CLOG_INFO(&LOG, "Using Blender config: \"%s\"", g_config_active().path.c_str());
533 }
535 CLOG_INFO(&LOG, "Using project config: \"%s\"", g_config_active().path.c_str());
536 }
537 else {
539 "Using config from %s: \"%s\"",
541 g_config_active().path.c_str());
542 }
543}
544
546{
547 /* Remember the environment before loading overwrites it. */
548 if (const char *env = BLI_getenv("BLENDER_OCIO")) {
550 }
551 if (const char *env = BLI_getenv("OCIO")) {
552 g_startup_env().ocio = env;
553 }
554
555 /* No project yet without a `Main`, the config is re-resolved on file read. */
557
559}
560
561void colormanage_environment_setup_for_test(std::optional<std::string> blender_ocio_env,
562 std::optional<std::string> ocio_env)
563{
564 g_startup_env().blender_ocio = std::move(blender_ocio_env);
565 g_startup_env().ocio = std::move(ocio_env);
566}
567
573
578
583
585{
587
588 /* Already loaded? */
589 if (candidates == g_config_requested()) {
590 return false;
591 }
592
593 CLOG_INFO(&LOG, "Reloading OpenColorIO config");
594
595 const ColorManagedConfigPath old_config = g_config_active();
596
598
599 return g_config_active() != old_config;
600}
601
603
604/* -------------------------------------------------------------------- */
607
608bool IMB_colormanagement_switch_config(const char *filepath)
609{
610 std::optional<std::string> old_ocio_env;
611 colormanage_set_ocio_env(filepath, old_ocio_env);
612
613 const auto validate = [](const ocio::Config &config) {
614 return colormanage_load_config(config, true);
615 };
616
617 /* Switch in place. */
618 if (!g_config()->switch_to_from_environment(validate)) {
619 CLOG_ERROR(&LOG, "Failed to load OpenColorIO config from '%s'", filepath);
620 colormanage_restore_ocio_env(old_ocio_env);
621 return false;
622 }
623
624 /* Load roles, matrices and view names from the new configuration. */
626
627 /* The `OCIO` environment variable now points to this config. */
628 g_config_active() = {filepath, filepath, ColorManagedConfigSource::EnvOCIO};
629
630 CLOG_INFO_NOCHECK(&LOG, "Switched OpenColorIO config to '%s'", filepath);
631 return true;
632}
633
635
636/* -------------------------------------------------------------------- */
639
641{
642 if (view_name.is_empty()) {
643 return StringRef();
644 }
645
646 /* First try to find any looks with the full name prefix. */
647 for (const int look_index : IndexRange(g_config()->get_num_looks())) {
648 const ocio::Look *look = g_config()->get_look_by_index(look_index);
649 if (look->view() == view_name) {
650 return view_name;
651 }
652 }
653
654 /* Then try with the short name prefix. */
655 const int64_t separator_offset = view_name.find(" - ");
656 if (separator_offset == -1) {
657 return StringRef();
658 }
659 StringRef view_short_name = view_name.substr(0, separator_offset);
660
661 for (const int look_index : IndexRange(g_config()->get_num_looks())) {
662 const ocio::Look *look = g_config()->get_look_by_index(look_index);
663 if (look->view() == view_short_name) {
664 return view_short_name;
665 }
666 }
667
668 return StringRef();
669}
670
671static bool colormanage_compatible_look(const ocio::Look *look, StringRef view_filter)
672{
673 if (look->is_noop) {
674 return true;
675 }
676
677 /* Skip looks only relevant to specific view transforms.
678 * If the view transform has view-specific look ignore non-specific looks. */
679 return view_filter.is_empty() ? look->view().is_empty() : look->view() == view_filter;
680}
681
682static bool colormanage_compatible_look(const ocio::Look *look, const char *view_name)
683{
684 const StringRef view_filter = view_filter_for_look(view_name);
685 return colormanage_compatible_look(look, view_filter);
686}
687
688static bool colormanage_use_look(const char *look_name, const char *view_name)
689{
690 const ocio::Look *look = g_config()->get_look_by_name(look_name);
691 return (look && look->is_noop == false && colormanage_compatible_look(look, view_name));
692}
693
695 const bContext *C,
696 ColorManagedViewSettings **r_view_settings,
697 ColorManagedDisplaySettings **r_display_settings)
698{
699 Scene *scene = CTX_data_scene(C);
701
702 *r_view_settings = &scene->view_settings;
703 *r_display_settings = &scene->display_settings;
704
705 if (sima && sima->image) {
706 if ((sima->image->flag & IMA_VIEW_AS_RENDER) == 0) {
707 *r_view_settings = nullptr;
708 }
709 }
710}
711
712static bool get_display_emulation(const ColorManagedDisplaySettings &display_settings)
713{
714 switch (display_settings.emulation) {
716 return false;
718 return true;
719 }
720
721 return true;
722}
723
724static std::shared_ptr<const ocio::CPUProcessor> get_display_buffer_processor(
725 const ColorManagedDisplaySettings &display_settings,
726 const char *look,
727 const char *view_transform,
728 const float exposure,
729 const float gamma,
730 const float temperature,
731 const float tint,
732 const bool use_white_balance,
733 const char *from_colorspace,
734 const ColorManagedDisplaySpace target,
735 const bool inverse = false)
736{
737 ocio::DisplayParameters display_parameters;
738 display_parameters.from_colorspace = from_colorspace;
739 display_parameters.view = view_transform;
740 display_parameters.display = display_settings.display_device;
741 display_parameters.look = colormanage_use_look(look, view_transform) ? look : "";
742 display_parameters.scale = (exposure == 0.0f) ? 1.0f : powf(2.0f, exposure);
743 display_parameters.exponent = (gamma == 1.0f) ? 1.0f : 1.0f / max_ff(FLT_EPSILON, gamma);
744 display_parameters.temperature = temperature;
745 display_parameters.tint = tint;
746 display_parameters.use_white_balance = use_white_balance;
747 display_parameters.inverse = inverse;
748 display_parameters.use_hdr_buffer = GPU_hdr_support();
749 display_parameters.use_hdr_display = IMB_colormanagement_display_is_hdr(&display_settings,
750 view_transform);
751 display_parameters.is_image_output = (target == DISPLAY_SPACE_IMAGE_OUTPUT);
752 display_parameters.use_display_emulation = (target == DISPLAY_SPACE_DRAW) ?
753 get_display_emulation(display_settings) :
754 false;
755 display_parameters.use_scope_space = (target == DISPLAY_SPACE_SCOPE);
756
757 return g_config()->get_display_cpu_processor(display_parameters);
758}
759
761 ColorManagedViewSettings *view_settings,
762 const ColorManagedDisplaySettings * /*display_settings*/)
763{
764 /* Empty view transform name means skip tone mapping. */
765 view_settings->view_transform[0] = '\0';
766 /* TODO(sergey): Find a way to safely/reliable un-hardcode this. */
767 STRNCPY_UTF8(view_settings->look, "None");
768 /* Initialize rest of the settings. */
769 view_settings->flag = {};
770 view_settings->gamma = 1.0f;
771 view_settings->exposure = 0.0f;
772 view_settings->temperature = 6500.0f;
773 view_settings->tint = 10.0f;
774 view_settings->curve_mapping = nullptr;
775}
776
777static void curve_mapping_apply_pixel(const CurveMapping *curve_mapping,
778 float *pixel,
779 int channels)
780{
781 if (channels == 1) {
782 pixel[0] = BKE_curvemap_evaluateF(curve_mapping, curve_mapping->cm, pixel[0]);
783 }
784 else if (channels == 2) {
785 pixel[0] = BKE_curvemap_evaluateF(curve_mapping, curve_mapping->cm, pixel[0]);
786 pixel[1] = BKE_curvemap_evaluateF(curve_mapping, curve_mapping->cm, pixel[1]);
787 }
788 else {
789 BKE_curvemapping_evaluate_premulRGBF(curve_mapping, pixel, pixel);
790 }
791}
792
794{
795 /* Regression tests are allocating ImBuf. Guard against access of uninitialized color
796 * management configuration. */
797 /* TODO(sergey): Always allocate the fallback color management configuration for such cases? */
798 if (!g_config()) {
799 return;
800 }
801 ibuf->byte_buffer.colorspace = g_config()->get_color_space(global_role_default_byte);
802}
803
805 const char *from_colorspace,
807{
808 const ColorSpace *colorspace = g_config()->get_color_space(from_colorspace);
809
810 if (colorspace && colorspace->is_data()) {
811 if (ibuf->float_data()) {
813 }
814 return;
815 }
816
817 if (ibuf->float_data()) {
818 const char *to_colorspace = global_role_scene_linear;
819 const bool predivide = IMB_alpha_affects_rgb(ibuf);
820
821 if (ibuf->byte_data()) {
823 }
824
826 const ColorSpace *image_colorspace = g_config()->get_color_space_for_hdr_image(
827 from_colorspace);
828 if (image_colorspace) {
829 from_colorspace = image_colorspace->name().c_str();
830 }
831 }
832
833 /* Clear colorspace to indicate it's scene linear. */
834 ibuf->float_buffer.colorspace = nullptr;
835
836 if (from_colorspace[0] == '\0') {
837 return;
838 }
839
841 from_colorspace, to_colorspace);
842 if (cm_processor.is_noop()) {
843 return;
844 }
845
847 ibuf->float_data_for_write(),
848 ibuf->x,
849 ibuf->y,
850 ibuf->channels,
851 &cm_processor,
852 predivide);
853 }
854}
855
857
858/* -------------------------------------------------------------------- */
861
863 const char *what,
864 const ocio::Display *default_display,
865 const CLG_Level log_level)
866{
867 StringRefNull display_name = display_settings->display_device;
868
869 if (display_name.is_empty()) {
870 STRNCPY_UTF8(display_settings->display_device, default_display->name().c_str());
871 return true;
872 }
873
874 const ocio::Display *display = g_config()->get_display_by_name(display_name);
875 if (display) {
876 return true;
877 }
878
879 StringRefNull new_display_name = default_display->name();
880
881 /* Try to find a similar name, so that we can match e.g. "sRGB - Display" and "sRGB".
882 * There are aliases for color spaces, but not displays. */
883 for (const int display_index : IndexRange(g_config()->get_num_displays())) {
884 display = g_config()->get_display_by_index(display_index);
885 if (display->name().startswith(display_name) || display_name.startswith(display->name())) {
886 new_display_name = display->name();
887 break;
888 }
889 }
890
891 /* Don't warn when only one display is available (e.g. fallback config). */
892 if (g_config()->get_num_displays() > 1) {
894 log_level,
895 "Display \"%s\" used by %s not found, setting to \"%s\".",
896 display_settings->display_device,
897 what,
898 new_display_name.c_str());
899 }
900 STRNCPY_UTF8(display_settings->display_device, new_display_name.c_str());
901 return false;
902}
903
905 StringRefNull view_name)
906{
907 /* Match untonemapped view conventions between Blender and ACES 2.0. */
908 if (view_name == "Standard" && display->get_view_by_name("Un-tone-mapped")) {
909 return "Un-tone-mapped";
910 }
911 if (view_name == "Un-tone-mapped" && display->get_view_by_name("Standard")) {
912 return "Standard";
913 }
914
915 /* Try to find a similar name, so that we can match e.g. "ACES 2.0" and "ACES 2.0 - HDR 1000"
916 * when switching between SDR and HDR displays. */
917 for (const int view_index : IndexRange(display->get_num_views())) {
918 const ocio::View *view = display->get_view_by_index(view_index);
919 if (view->name().startswith(view_name) || view_name.startswith(view->name())) {
920 return view->name();
921 }
922 }
923
924 const int64_t separator_offset = view_name.find(" - ");
925 if (separator_offset != -1) {
926 const StringRef view_short_name = view_name.substr(0, separator_offset);
927 for (const int view_index : IndexRange(display->get_num_views())) {
928 const ocio::View *view = display->get_view_by_index(view_index);
929 if (view->name().startswith(view_short_name)) {
930 return view->name();
931 }
932 }
933 }
934
935 if (const ocio::View *view = display->get_default_view()) {
936 return view->name();
937 }
938
939 return "";
940}
941
943 ColorManagedViewSettings *view_settings,
944 const char *what,
945 const CLG_Level log_level)
946{
947 const ocio::Display *display = g_config()->get_display_by_name(display_settings->display_device);
948 if (!display) {
949 return false;
950 }
951
952 bool ok = true;
953
954 const char *default_look_name = IMB_colormanagement_look_get_default_name();
955 StringRefNull view_name = view_settings->view_transform;
956
957 if (view_name.is_empty()) {
958 const ocio::View *default_view = display->get_default_view();
959 if (default_view) {
960 STRNCPY_UTF8(view_settings->view_transform, default_view->name().c_str());
961 }
962 }
963 else {
964 const ocio::View *view = display->get_view_by_name(view_name);
965 if (!view) {
966 StringRefNull new_view_name = colormanage_find_matching_view_name(display, view_name);
967 if (!new_view_name.is_empty()) {
968 /* Don't warn when only one view is available (e.g. fallback config). */
969 if (display->get_num_views() > 1) {
971 log_level,
972 "%s view \"%s\" not found, setting to \"%s\".",
973 what,
974 view_settings->view_transform,
975 new_view_name.c_str());
976 }
977 STRNCPY_UTF8(view_settings->view_transform, new_view_name.c_str());
978 ok = false;
979 }
980 }
981 }
982
983 if (view_settings->look[0] == '\0') {
984 STRNCPY_UTF8(view_settings->look, default_look_name);
985 }
986 else {
987 const ocio::Look *look = g_config()->get_look_by_name(view_settings->look);
988 if (look == nullptr) {
989 /* Don't warn when only one look is available (e.g. fallback config). */
990 if (g_config()->get_num_looks() > 1) {
992 log_level,
993 "%s look \"%s\" not found, setting default \"%s\".",
994 what,
995 view_settings->look,
996 default_look_name);
997 }
998 STRNCPY_UTF8(view_settings->look, default_look_name);
999 ok = false;
1000 }
1001 else if (!colormanage_compatible_look(look, view_settings->view_transform)) {
1002 CLOG_INFO(&LOG,
1003 "%s look \"%s\" is not compatible with view \"%s\", setting "
1004 "default "
1005 "\"%s\".",
1006 what,
1007 view_settings->look,
1008 view_settings->view_transform,
1009 default_look_name);
1010
1011 STRNCPY_UTF8(view_settings->look, default_look_name);
1012 ok = false;
1013 }
1014 }
1015
1016 /* OCIO_TODO: move to do_versions() */
1017 if (view_settings->exposure == 0.0f && view_settings->gamma == 0.0f) {
1018 view_settings->exposure = 0.0f;
1019 view_settings->gamma = 1.0f;
1020 }
1021
1022 return ok;
1023}
1024
1025static void colormanage_name_to_interop_id(const char *name, char *interop_id)
1026{
1027 /* Roles are left without an interop ID, as their color space depends on the configuration. */
1028 const ColorSpace *colorspace = (g_config() && name[0] && !g_config()->is_role(name)) ?
1029 g_config()->get_color_space(name) :
1030 nullptr;
1031 const bool is_primary = colorspace && colorspace->is_primary_interop_id();
1032 BLI_strncpy(interop_id, is_primary ? colorspace->interop_id().c_str() : "", MAX_COLORSPACE_NAME);
1033}
1034
1036 char *interop_id,
1037 const char *what,
1038 const CLG_Level log_level)
1039{
1040 if (name[0] == '\0') {
1041 return true;
1042 }
1043
1044 /* The interop ID takes precedence, as a name may refer to a different color space
1045 * in different configuration. */
1046 const ColorSpace *colorspace_by_name = g_config()->get_color_space(name);
1047 const ColorSpace *colorspace = colorspace_by_name;
1048 if (interop_id[0] != '\0') {
1049 if (const ColorSpace *colorspace_by_interop_id = g_config()->get_color_space_by_interop_id(
1050 interop_id))
1051 {
1052 colorspace = colorspace_by_interop_id;
1053 }
1054 }
1055
1056 if (!colorspace) {
1058 &LOG, log_level, "%s colorspace \"%s\" not found, will use default instead.", what, name);
1059 name[0] = '\0';
1060 interop_id[0] = '\0';
1061 return false;
1062 }
1063
1064 /* Sync the name to the one used by the active configuration.
1065 * Roles are kept unchanged as they don't depend on the config. */
1066 if (colorspace->name() != name &&
1067 !(colorspace == colorspace_by_name && g_config()->is_role(name)))
1068 {
1069 BLI_strncpy(name, colorspace->name().c_str(), MAX_COLORSPACE_NAME);
1070 }
1071
1072 /* Set interop ID for older blend files that did not have it. */
1073 if (interop_id[0] == '\0') {
1074 colormanage_name_to_interop_id(name, interop_id);
1075 }
1076
1077 return true;
1078}
1079
1081 const char *what,
1082 const CLG_Level log_level)
1083{
1084 return colormanage_check_colorspace_name(settings->name, settings->interop_id, what, log_level);
1085}
1086
1091
1093{
1094 const ocio::Display *default_display = g_config()->get_default_display();
1095 if (!default_display) {
1096 /* happens when OCIO configuration is incorrect */
1097 return;
1098 }
1099
1100 /* Check display, view and colorspace names in datablocks. */
1101 bool is_missing_opencolorio_config = false;
1102
1103 /* New files are quietly changed to use the active config, automatic
1104 * switching to different display/view/colorspace names is expected. */
1105 const bool is_new_file = bmain->filepath[0] == '\0';
1106 const CLG_Level log_level = is_new_file ? CLG_LEVEL_DEBUG : CLG_LEVEL_WARN;
1107
1108 /* Check scenes. */
1109 for (Scene &scene : bmain->scenes) {
1110 ColorManagedColorspaceSettings *sequencer_colorspace_settings;
1111 bool ok = true;
1112
1113 /* check scene color management settings */
1115 &scene.display_settings, "scene", default_display, log_level);
1117 &scene.display_settings, &scene.view_settings, "scene", log_level);
1118
1120 &scene.r.im_format.display_settings, "scene output", default_display, log_level);
1122 &scene.r.im_format.view_settings,
1123 "scene output",
1124 log_level);
1126 &scene.r.im_format.linear_colorspace_settings, "scene output", log_level);
1128 &scene.r.bake.im_format.linear_colorspace_settings, "bake output", log_level);
1129
1130 sequencer_colorspace_settings = &scene.sequencer_colorspace_settings;
1131
1133 sequencer_colorspace_settings, "sequencer", log_level);
1134
1135 if (sequencer_colorspace_settings->name[0] == '\0') {
1136 IMB_colormanagement_colorspace_settings_set(sequencer_colorspace_settings,
1138 }
1139
1140 /* Check sequencer strip input colorspace. */
1141 if (scene.ed != nullptr) {
1142 seq::foreach_strip(&scene.ed->seqbase, [&](Strip *strip) {
1143 if (strip->data) {
1144 ok &= colormanage_check_colorspace_settings(
1145 &strip->data->colorspace_settings, "sequencer strip", log_level);
1146 }
1147 return true;
1148 });
1149 }
1150
1151 is_missing_opencolorio_config |= (!ok && !ID_IS_LINKED(&scene.id));
1152 }
1153
1154 /* Check image and movie input colorspace. */
1155 for (Image &image : bmain->images) {
1157 &image.colorspace_settings, "image", log_level);
1158 is_missing_opencolorio_config |= (!ok && !ID_IS_LINKED(&image.id));
1159 }
1160
1161 for (MovieClip &clip : bmain->movieclips) {
1163 &clip.colorspace_settings, "clip", log_level);
1164 is_missing_opencolorio_config |= (!ok && !ID_IS_LINKED(&clip.id));
1165 }
1166
1167 /* Check compositing nodes. */
1168 for (bNodeTree &ntree : bmain->nodetrees) {
1169 if (ntree.type == NTREE_COMPOSIT) {
1170 bool ok = true;
1171 for (bNode &node : ntree.nodes) {
1172 if (node.type_legacy == CMP_NODE_CONVERT_TO_DISPLAY) {
1173 NodeConvertToDisplay *nctd = static_cast<NodeConvertToDisplay *>(node.storage);
1175 &nctd->display_settings, "node", default_display, log_level);
1177 &nctd->display_settings, &nctd->view_settings, "node", log_level);
1178 }
1179 else if (node.type_legacy == CMP_NODE_CONVERT_COLOR_SPACE) {
1180 NodeConvertColorSpace *ncs = static_cast<NodeConvertColorSpace *>(node.storage);
1182 ncs->from_color_space, ncs->from_interop_id, "node", log_level);
1184 ncs->to_color_space, ncs->to_interop_id, "node", log_level);
1185 }
1186 else if (node.type_legacy == CMP_NODE_OUTPUT_FILE) {
1187 NodeCompositorFileOutput *nfo = static_cast<NodeCompositorFileOutput *>(node.storage);
1189 &nfo->format.linear_colorspace_settings, "node", log_level);
1190 for (NodeCompositorFileOutputItem &item : MutableSpan(nfo->items, nfo->items_count)) {
1192 &item.format.linear_colorspace_settings, "node", log_level);
1193 }
1194 }
1195 }
1196 is_missing_opencolorio_config |= (!ok && !ID_IS_LINKED(&ntree.id));
1197 }
1198 }
1199
1200 /* Inform users about mismatch, but not for new files. Linked datablocks are also ignored,
1201 * as we are not overwriting them on blend file save which is the main purpose of this
1202 * warning. */
1203 if (!is_new_file && is_missing_opencolorio_config) {
1204 bmain->colorspace.is_missing_opencolorio_config = true;
1205 }
1206}
1207
1209 ColorManagedViewSettings *view_settings)
1210{
1211 const ocio::Display *display = g_config()->get_display_by_name(display_settings->display_device);
1212
1213 bool found = false;
1214 for (const int view_index : IndexRange(display->get_num_views())) {
1215 const ocio::View *view = display->get_view_by_index(view_index);
1216 if (view->name() == view_settings->view_transform) {
1217 found = true;
1218 break;
1219 }
1220 }
1221
1222 if (!found) {
1224 display, view_settings->view_transform);
1225 if (!new_view_name.is_empty()) {
1226 STRNCPY_UTF8(view_settings->view_transform, new_view_name.c_str());
1227 }
1228 }
1229}
1230
1232{
1233 switch (role) {
1234 case COLOR_ROLE_DATA:
1235 return global_role_data;
1250 default:
1251 CLOG_WARN(&LOG, "Unknown role was passed to %s", __func__);
1252 BLI_assert(0);
1253 break;
1254 }
1255
1256 return nullptr;
1257}
1258
1260{
1265 if (flag_is_set(ibuf_src->flags, ImBufFlags::AlphaPremul)) {
1266 ibuf_dst->flags |= ImBufFlags::AlphaPremul;
1267 }
1268 else if (flag_is_set(ibuf_src->flags, ImBufFlags::AlphaChannelPacked)) {
1270 }
1271 else if (flag_is_set(ibuf_src->flags, ImBufFlags::AlphaIgnore)) {
1272 ibuf_dst->flags |= ImBufFlags::AlphaIgnore;
1273 }
1274}
1275
1277{
1278 const ColorSpace *colorspace = g_config()->get_color_space(name);
1280}
1281
1283{
1284 const ColorSpace *colorspace = g_config()->get_color_space(name);
1286}
1287
1289{
1290 return ibuf->float_colorspace().name().c_str();
1291}
1292
1294{
1295 return ibuf->byte_colorspace().name().c_str();
1296}
1297
1298const char *IMB_colormanagement_space_from_filepath_rules(const char *filepath)
1299{
1300 return g_config()->get_color_space_from_filepath(filepath);
1301}
1302
1304{
1305 return g_config()->get_color_space(name);
1306}
1308{
1309 return g_config()->get_color_space(name);
1310}
1311
1313{
1314 return (colorspace && colorspace->is_data());
1315}
1316
1318{
1319 return (colorspace && colorspace->is_scene_linear());
1320}
1321
1323{
1324 return (colorspace && colorspace->is_srgb());
1325}
1326
1331
1333{
1334 const ColorSpace *colorspace = g_config()->get_color_space(name);
1335 return (colorspace && colorspace->is_data());
1336}
1337
1339{
1340 const ColorSpace *colorspace = g_config()->get_color_space(name);
1342}
1343
1345{
1346 const ColorSpace *colorspace = g_config()->get_color_space(name);
1348}
1349
1351{
1352 /* Try interop ID. */
1353 for (const char *interop_id : {"srgb_rec709_scene", "srgb_rec709_display"}) {
1354 if (const ColorSpace *colorspace = g_config()->get_color_space_by_interop_id(interop_id)) {
1355 return colorspace->name().c_str();
1356 }
1357 }
1358
1359 /* Common names in configs without interop IDs. */
1360 for (const char *name :
1361 {"sRGB Encoded Rec.709 (sRGB)", "Utility - sRGB - Texture", "sRGB - Texture", "sRGB"})
1362 {
1363 if (const ColorSpace *colorspace = g_config()->get_color_space(name)) {
1364 return colorspace->name().c_str();
1365 }
1366 }
1367
1368 /* Fallback if nothing can be found. */
1370}
1371
1373{
1374 if (filepath.is_empty()) {
1375 return {};
1376 }
1377 fstream f(filepath, std::ios::binary | std::ios::in | std::ios::ate);
1378 if (!f.is_open()) {
1379 return {};
1380 }
1381
1382 const std::streamsize size = f.tellg();
1383 if (size <= 0) {
1384 return {};
1385 }
1386
1387 Vector<char> icc_profile(size);
1388 f.seekg(0, std::ios::beg);
1389 if (!f.read(icc_profile.data(), icc_profile.size())) {
1390 icc_profile.clear();
1391 }
1392 return icc_profile;
1393}
1394
1396{
1397 /* First try icc_profile attribute from the config. */
1398 Vector<char> icc_profile = imb_icc_profile_from_filepath(colorspace->icc_profile_path());
1399 if (!icc_profile.is_empty()) {
1400 return icc_profile;
1401 }
1402
1403 /* Try ICC profiles shipped with Blender based on interop ID. */
1404 const StringRefNull interop_id = colorspace->interop_id();
1405 if (interop_id.is_empty()) {
1406 return {};
1407 }
1408
1409 const std::optional<std::string> dir = BKE_appdir_folder_id(BLENDER_DATAFILES,
1410 "colormanagement");
1411 if (!dir.has_value()) {
1412 return {};
1413 }
1414
1415 char icc_filename[FILE_MAX];
1416 STRNCPY(icc_filename, (ocio::interop_id_drop_namespace(interop_id) + ".icc").c_str());
1417 BLI_path_make_safe_filename(icc_filename);
1418
1419 char icc_filepath[FILE_MAX];
1420 BLI_path_join(icc_filepath, sizeof(icc_filepath), dir->c_str(), "icc", icc_filename);
1421
1422 icc_profile = imb_icc_profile_from_filepath(icc_filepath);
1423 if (icc_profile.is_empty()) {
1424 /* If we can't find a scene referred filename, try display referred. */
1425 const StringRef icc_filepath_ref = icc_filepath;
1426 if (icc_filepath_ref.endswith("_scene.icc")) {
1427 const std::string icc_filepath_display = icc_filepath_ref.drop_suffix(strlen("_scene.icc")) +
1428 "_display.icc";
1429 icc_profile = imb_icc_profile_from_filepath(icc_filepath_display.c_str());
1430 }
1431 }
1432
1433 return icc_profile;
1434}
1435
1436/* Primaries */
1437static const int CICP_PRI_REC709 = 1;
1438static const int CICP_PRI_UNSPECIFIED = 2;
1439static const int CICP_PRI_REC2020 = 9;
1440static const int CICP_PRI_XYZD65 = 10;
1441static const int CICP_PRI_P3D65 = 12;
1442/* Transfer functions */
1443static const int CICP_TRC_BT709 = 1;
1444static const int CICP_TRC_UNSPECIFIED = 2;
1445static const int CICP_TRC_G22 = 4;
1446static const int CICP_TRC_LINEAR = 8;
1447static const int CICP_TRC_SRGB = 13;
1448static const int CICP_TRC_PQ = 16;
1449static const int CICP_TRC_SMPTE428 = 17;
1450static const int CICP_TRC_HLG = 18;
1451/* Matrix */
1452static const int CICP_MATRIX_RGB = 0;
1453static const int CICP_MATRIX_BT709 = 1;
1454static const int CICP_MATRIX_REC2020_NCL = 9;
1455/* Range */
1456static const int CICP_RANGE_LIMITED = 0;
1457static const int CICP_RANGE_FULL = 1;
1458
1459/* Color space for Rec.709 tagged video, from the video_rec709 role.
1460 *
1461 * The Color Interop Forum and broadcast standards like Rec.1886 consider this to be gamma
1462 * 2.4, however many applications consider this to be sRGB, or gamma 1.961 on macOS. This
1463 * includes video players, browsers, YouTube uploads, and screen recording software.
1464 *
1465 * So, Blender by default writes sRGB video as Rec.709, and also defaults to sRGB output.
1466 * Keeping the input and output defaults matched means that only cutting the video will
1467 * not change colors, even if they might be in another colorspace. */
1469{
1470 return (global_role_video_rec709[0]) ? g_config()->get_color_space(global_role_video_rec709) :
1471 nullptr;
1472}
1473
1476 const bool rgb_matrix,
1477 int cicp[4])
1478{
1480 /* Ambiguous BT.709 TRC, see #video_rec709_colorspace for details.
1481 * Also use limited range to match existing Blender behavior and avoid potential
1482 * incompatibilities in some software. */
1483 cicp[0] = CICP_PRI_REC709;
1484 cicp[1] = CICP_TRC_BT709;
1485 cicp[2] = (rgb_matrix) ? CICP_MATRIX_RGB : CICP_MATRIX_BT709;
1486 cicp[3] = (rgb_matrix) ? CICP_RANGE_FULL : CICP_RANGE_LIMITED;
1487 return true;
1488 }
1489
1490 const StringRefNull interop_id = colorspace->interop_id();
1491 if (interop_id.is_empty()) {
1492 return false;
1493 }
1494
1495 /* References:
1496 * ASWF Color Interop Forum defined display spaces.
1497 * https://en.wikipedia.org/wiki/Coding-independent_code_points
1498 * https://www.w3.org/TR/png-3/#cICP-chunk
1499 */
1500 if (interop_id == "pq_rec2020_display") {
1501 cicp[0] = CICP_PRI_REC2020;
1502 cicp[1] = CICP_TRC_PQ;
1503 cicp[2] = (rgb_matrix) ? CICP_MATRIX_RGB : CICP_MATRIX_REC2020_NCL;
1504 cicp[3] = CICP_RANGE_FULL;
1505 return true;
1506 }
1507 if (interop_id == "hlg_rec2020_display") {
1508 cicp[0] = CICP_PRI_REC2020;
1509 cicp[1] = CICP_TRC_HLG;
1510 cicp[2] = (rgb_matrix) ? CICP_MATRIX_RGB : CICP_MATRIX_REC2020_NCL;
1511 cicp[3] = CICP_RANGE_FULL;
1512 return true;
1513 }
1514 if (interop_id == "pq_p3d65_display") {
1515 /* Rec.2020 matrix may seem odd, but follows Color Interop Forum recommendation. */
1516 cicp[0] = CICP_PRI_P3D65;
1517 cicp[1] = CICP_TRC_PQ;
1518 cicp[2] = (rgb_matrix) ? CICP_MATRIX_RGB : CICP_MATRIX_REC2020_NCL;
1519 cicp[3] = CICP_RANGE_FULL;
1520 return true;
1521 }
1522 if (interop_id == "pq_xyzd65_display") {
1523 cicp[0] = CICP_PRI_XYZD65;
1524 cicp[1] = CICP_TRC_PQ;
1525 cicp[2] = CICP_MATRIX_RGB;
1526 cicp[3] = CICP_RANGE_FULL;
1527 return true;
1528 }
1529 if (interop_id == "g26_xyzd65_display") {
1530 cicp[0] = CICP_PRI_XYZD65;
1531 cicp[1] = CICP_TRC_SMPTE428;
1532 cicp[2] = CICP_MATRIX_RGB;
1533 cicp[3] = CICP_RANGE_FULL;
1534 return true;
1535 }
1536 if (interop_id == "g26_p3d65_display") {
1537 /* One might think the same transfer function as g26_xyzd65_display can be
1538 * used here. But actually it's that one has headroom scaling, and this one
1539 * does not. And there is no CICP for regular gamma 2.6. */
1540 return false;
1541 }
1542 if (ELEM(interop_id, "g22_rec709_display", "g22_rec709_scene")) {
1543 cicp[0] = CICP_PRI_REC709;
1544 cicp[1] = CICP_TRC_G22;
1545 cicp[2] = (rgb_matrix) ? CICP_MATRIX_RGB : CICP_MATRIX_BT709;
1546 cicp[3] = CICP_RANGE_FULL;
1547 return true;
1548 }
1549 if (ELEM(interop_id, "blender:g24_rec2020_display", "g24_rec2020_scene")) {
1550 /* In CICP terms, Rec709 == BT.1886 == 2.4 gamma */
1551 cicp[0] = CICP_PRI_REC2020;
1552 cicp[1] = CICP_TRC_BT709;
1553 cicp[2] = (rgb_matrix) ? CICP_MATRIX_RGB : CICP_MATRIX_REC2020_NCL;
1554 cicp[3] = CICP_RANGE_FULL;
1555 return true;
1556 }
1557 if (ELEM(interop_id, "g24_rec709_display", "g24_rec709_scene")) {
1558 /* In CICP terms, Rec709 == BT.1886 == 2.4 gamma */
1559 cicp[0] = CICP_PRI_REC709;
1560 cicp[1] = CICP_TRC_BT709;
1561 cicp[2] = (rgb_matrix) ? CICP_MATRIX_RGB : CICP_MATRIX_BT709;
1562 cicp[3] = CICP_RANGE_FULL;
1563 return true;
1564 }
1565 if (ELEM(interop_id, "srgb_p3d65_display", "srgbe_p3d65_display", "srgb_p3d65_scene")) {
1566 cicp[0] = CICP_PRI_P3D65;
1567 cicp[1] = CICP_TRC_SRGB;
1568 cicp[2] = (rgb_matrix) ? CICP_MATRIX_RGB : CICP_MATRIX_BT709;
1569 cicp[3] = CICP_RANGE_FULL;
1570 return true;
1571 }
1572 if (ELEM(interop_id, "srgb_rec709_display", "srgb_rec709_scene")) {
1573 cicp[0] = CICP_PRI_REC709;
1574 cicp[1] = CICP_TRC_SRGB;
1575 cicp[2] = (rgb_matrix) ? CICP_MATRIX_RGB : CICP_MATRIX_BT709;
1576 cicp[3] = CICP_RANGE_FULL;
1577 return true;
1578 }
1579 if (interop_id == "blender:g1961_rec709_display") {
1580 /* Apple interpretation of Rec.709 TRC with Gamma 1.961. */
1581 cicp[0] = CICP_PRI_REC709;
1582 cicp[1] = CICP_TRC_BT709;
1583 cicp[2] = (rgb_matrix) ? CICP_MATRIX_RGB : CICP_MATRIX_BT709;
1584 cicp[3] = CICP_RANGE_FULL;
1585 return true;
1586 }
1587 if (ELEM(interop_id, "lin_rec709_display", "lin_rec709_scene")) {
1588 cicp[0] = CICP_PRI_REC709;
1589 cicp[1] = CICP_TRC_LINEAR;
1590 cicp[2] = (rgb_matrix) ? CICP_MATRIX_RGB : CICP_MATRIX_BT709;
1591 cicp[3] = CICP_RANGE_FULL;
1592 return true;
1593 }
1594 if (ELEM(interop_id, "lin_p3d65_display", "lin_p3d65_scene")) {
1595 cicp[0] = CICP_PRI_P3D65;
1596 cicp[1] = CICP_TRC_LINEAR;
1597 cicp[2] = (rgb_matrix) ? CICP_MATRIX_RGB : CICP_MATRIX_BT709;
1598 cicp[3] = CICP_RANGE_FULL;
1599 return true;
1600 }
1601 if (ELEM(interop_id, "lin_rec2020_display", "lin_rec2020_scene")) {
1602 cicp[0] = CICP_PRI_REC2020;
1603 cicp[1] = CICP_TRC_LINEAR;
1604 cicp[2] = (rgb_matrix) ? CICP_MATRIX_RGB : CICP_MATRIX_REC2020_NCL;
1605 cicp[3] = CICP_RANGE_FULL;
1606 return true;
1607 }
1608 if (interop_id == "lin_ciexyzd65_scene") {
1609 cicp[0] = CICP_PRI_XYZD65;
1610 cicp[1] = CICP_TRC_LINEAR;
1611 cicp[2] = CICP_MATRIX_RGB;
1612 cicp[3] = CICP_RANGE_FULL;
1613 return true;
1614 }
1615
1616 return false;
1617}
1618
1620 const ColorManagedFileOutput /*output*/)
1621{
1622 StringRefNull interop_id;
1623
1624 /* We don't care about matrix or range, we assume decoding handles that and we get
1625 * full range RGB values out. */
1626 if (cicp[0] == CICP_PRI_REC2020 && cicp[1] == CICP_TRC_PQ) {
1627 interop_id = "pq_rec2020_display";
1628 }
1629 else if (cicp[0] == CICP_PRI_REC2020 && cicp[1] == CICP_TRC_HLG) {
1630 interop_id = "hlg_rec2020_display";
1631 }
1632 else if (cicp[0] == CICP_PRI_P3D65 && cicp[1] == CICP_TRC_PQ) {
1633 interop_id = "pq_p3d65_display";
1634 }
1635 else if (cicp[0] == CICP_PRI_XYZD65 && cicp[1] == CICP_TRC_PQ) {
1636 interop_id = "pq_xyzd65_display";
1637 }
1638 else if (cicp[0] == CICP_PRI_XYZD65 && cicp[1] == CICP_TRC_SMPTE428) {
1639 interop_id = "g26_xyzd65_display";
1640 }
1641 else if (cicp[0] == CICP_PRI_REC709 && cicp[1] == CICP_TRC_G22) {
1642 interop_id = "g22_rec709_display";
1643 }
1644 else if (cicp[0] == CICP_PRI_REC2020 && cicp[1] == CICP_TRC_BT709) {
1645 interop_id = "blender:g24_rec2020_display";
1646 }
1647 else if (cicp[0] == CICP_PRI_REC709 && cicp[1] == CICP_TRC_BT709) {
1648 /* Ambiguous BT.709 TRC, see #video_rec709_colorspace for details. */
1649 return video_rec709_colorspace();
1650 }
1651 else if (cicp[0] == CICP_PRI_P3D65 && ELEM(cicp[1], CICP_TRC_SRGB, CICP_TRC_BT709)) {
1652 interop_id = "srgb_p3d65_display";
1653 }
1654 else if (cicp[0] == CICP_PRI_REC709 && cicp[1] == CICP_TRC_SRGB) {
1655 interop_id = "srgb_rec709_display";
1656 }
1657 else if (cicp[0] == CICP_PRI_REC709 && cicp[1] == CICP_TRC_LINEAR) {
1658 interop_id = "lin_rec709_display";
1659 }
1660 else if (cicp[0] == CICP_PRI_P3D65 && cicp[1] == CICP_TRC_LINEAR) {
1661 interop_id = "lin_p3d65_display";
1662 }
1663 else if (cicp[0] == CICP_PRI_REC2020 && cicp[1] == CICP_TRC_LINEAR) {
1664 interop_id = "lin_rec2020_display";
1665 }
1666 else if (cicp[0] == CICP_PRI_XYZD65 && cicp[1] == CICP_TRC_LINEAR) {
1667 interop_id = "lin_ciexyzd65_scene";
1668 }
1669 else if (cicp[0] == CICP_PRI_UNSPECIFIED && cicp[1] == CICP_TRC_UNSPECIFIED) {
1670 /* Read unspecified color spaces as sRGB. */
1671 interop_id = "srgb_rec709_display";
1672 }
1673
1674 return interop_id.is_empty() ? nullptr : g_config()->get_color_space_by_interop_id(interop_id);
1675}
1676
1681
1683{
1684 return g_config()->get_color_space_by_interop_id(interop_id);
1685}
1686
1687int IMB_colormanagement_colorspace_get_interop_id_index(const char *name, const char *interop_id)
1688{
1689 const ColorSpace *colorspace = name[0] ? g_config()->get_color_space(name) : nullptr;
1690 if (!colorspace || !colorspace->is_primary_interop_id()) {
1691 return -1;
1692 }
1693 if (interop_id[0] && colorspace->alternate_interop_id() == interop_id) {
1694 return colorspace->index + g_config()->get_num_all_color_spaces();
1695 }
1696 return colorspace->index;
1697}
1698
1699void IMB_colormanagement_colorspace_interop_id_set(char *name, char *interop_id, const int index)
1700{
1701 const int offset = g_config()->get_num_all_color_spaces();
1702 const bool is_alternate = index >= offset;
1703 const ColorSpace *colorspace = g_config()->get_color_space_by_index(
1704 is_alternate ? index - offset : index);
1705 if (!colorspace) {
1706 return;
1707 }
1708
1709 IMB_colormanagement_colorspace_name_set(name, interop_id, colorspace->name().c_str());
1710 if (is_alternate) {
1711 BLI_strncpy(interop_id, colorspace->alternate_interop_id().c_str(), MAX_COLORSPACE_NAME);
1712 }
1713}
1714
1719
1724
1726
1727/* -------------------------------------------------------------------- */
1730
1731void IMB_colormanagement_get_whitepoint(const float temperature,
1732 const float tint,
1733 float whitepoint[3])
1734{
1735 float3 xyz = math::whitepoint_from_temp_tint(temperature, tint);
1737}
1738
1739bool IMB_colormanagement_set_whitepoint(const float whitepoint[3], float &temperature, float &tint)
1740{
1741 float3 xyz;
1743 return math::whitepoint_to_temp_tint(xyz, temperature, tint);
1744}
1745
1747
1748/* -------------------------------------------------------------------- */
1751
1773
1786
1788 int start_line,
1789 int tot_line,
1790 DisplayBufferInitData *init_data)
1791{
1792 ImBuf *ibuf = init_data->ibuf;
1793
1794 int channels = ibuf->channels;
1795 float dither = ibuf->dither;
1796 bool is_data = ibuf->colorspace_is_data();
1797
1798 size_t offset = size_t(channels) * start_line * ibuf->x;
1799 size_t display_buffer_byte_offset = size_t(DISPLAY_BUFFER_CHANNELS) * start_line * ibuf->x;
1800
1801 memset(handle, 0, sizeof(DisplayBufferThread));
1802
1803 handle->cm_processor = init_data->cm_processor;
1804
1805 if (init_data->buffer) {
1806 handle->buffer = init_data->buffer + offset;
1807 }
1808
1809 if (init_data->byte_buffer) {
1810 handle->byte_buffer = init_data->byte_buffer + offset;
1811 }
1812
1813 if (init_data->display_buffer) {
1814 handle->display_buffer = init_data->display_buffer + offset;
1815 }
1816
1817 if (init_data->display_buffer_byte) {
1818 handle->display_buffer_byte = init_data->display_buffer_byte + display_buffer_byte_offset;
1819 }
1820
1821 handle->width = ibuf->x;
1822
1823 handle->start_line = start_line;
1824 handle->tot_line = tot_line;
1825
1826 handle->channels = channels;
1827 handle->dither = dither;
1828 handle->is_data = is_data;
1829 handle->predivide = IMB_alpha_affects_rgb(ibuf);
1830
1831 handle->byte_colorspace = init_data->byte_colorspace;
1832 handle->float_colorspace = init_data->float_colorspace;
1833}
1834
1836 int height,
1837 float *linear_buffer,
1838 bool *is_straight_alpha)
1839{
1840 int channels = handle->channels;
1841 int width = handle->width;
1842
1843 size_t buffer_size = size_t(channels) * width * height;
1844
1845 bool is_data = handle->is_data;
1846 bool is_data_display = handle->cm_processor->is_data_result();
1847 bool predivide = handle->predivide;
1848
1849 if (!handle->buffer) {
1850 uchar *byte_buffer = handle->byte_buffer;
1851
1852 const char *from_colorspace = handle->byte_colorspace;
1853 const char *to_colorspace = global_role_scene_linear;
1854
1855 float *fp;
1856 uchar *cp;
1857 const size_t i_last = size_t(width) * height;
1858 size_t i;
1859
1860 /* first convert byte buffer to float, keep in image space */
1861 for (i = 0, fp = linear_buffer, cp = byte_buffer; i != i_last;
1862 i++, fp += channels, cp += channels)
1863 {
1864 if (channels == 3) {
1865 rgb_uchar_to_float(fp, cp);
1866 }
1867 else if (channels == 4) {
1868 rgba_uchar_to_float(fp, cp);
1869 }
1870 else {
1871 BLI_assert_msg(0, "Buffers of 3 or 4 channels are only supported here");
1872 }
1873 }
1874
1875 if (!is_data && !is_data_display) {
1876 /* convert float buffer to scene linear space */
1878 linear_buffer, width, height, channels, from_colorspace, to_colorspace, false);
1879 }
1880
1881 *is_straight_alpha = true;
1882 }
1883 else if (handle->float_colorspace) {
1884 /* currently float is non-linear only in sequencer, which is working
1885 * in its own color space even to handle float buffers.
1886 * This color space is the same for byte and float images.
1887 * Need to convert float buffer to linear space before applying display transform
1888 */
1889
1890 const char *from_colorspace = handle->float_colorspace;
1891 const char *to_colorspace = global_role_scene_linear;
1892
1893 memcpy(linear_buffer, handle->buffer, buffer_size * sizeof(float));
1894
1895 if (!is_data && !is_data_display) {
1897 linear_buffer, width, height, channels, from_colorspace, to_colorspace, predivide);
1898 }
1899
1900 *is_straight_alpha = false;
1901 }
1902 else {
1903 /* some processors would want to modify float original buffer
1904 * before converting it into display byte buffer, so we need to
1905 * make sure original's ImBuf buffers wouldn't be modified by
1906 * using duplicated buffer here
1907 */
1908
1909 memcpy(linear_buffer, handle->buffer, buffer_size * sizeof(float));
1910
1911 *is_straight_alpha = false;
1912 }
1913}
1914
1916{
1917 const int width = handle->width;
1918 const int height = handle->tot_line;
1919 if (handle->display_buffer_byte && handle->display_buffer_byte != handle->byte_buffer) {
1920 if (handle->byte_buffer) {
1921 memcpy(handle->display_buffer_byte, handle->byte_buffer, size_t(width) * height * 4);
1922 }
1923 else if (handle->buffer) {
1925 handle->buffer,
1926 handle->channels,
1927 handle->dither,
1928 handle->predivide,
1929 width,
1930 height,
1931 width,
1932 handle->start_line);
1933 }
1934 }
1935
1936 if (handle->display_buffer) {
1937 if (handle->byte_buffer) {
1939 handle->display_buffer, handle->byte_buffer, width, height, width, width);
1940 }
1941 else if (handle->buffer && handle->display_buffer != handle->buffer) {
1943 handle->display_buffer, handle->buffer, handle->channels, width, height);
1944 }
1945 }
1946}
1947
1949{
1950 ColormanageProcessor *cm_processor = handle->cm_processor;
1951 if (cm_processor == nullptr) {
1953 return;
1954 }
1955
1956 float *display_buffer = handle->display_buffer;
1957 uchar *display_buffer_byte = handle->display_buffer_byte;
1958 int channels = handle->channels;
1959 int width = handle->width;
1960 int height = handle->tot_line;
1961 float *linear_buffer = MEM_new_array_uninitialized<float>(
1962 size_t(channels) * size_t(width) * size_t(height), "color conversion linear buffer");
1963
1964 bool is_straight_alpha;
1965 display_buffer_apply_get_linear_buffer(handle, height, linear_buffer, &is_straight_alpha);
1966
1967 bool predivide = handle->predivide && (is_straight_alpha == false);
1968
1969 /* Apply processor (note: data buffers never get color space conversions). */
1970 if (!handle->is_data) {
1971 cm_processor->apply(linear_buffer, width, height, channels, predivide);
1972 }
1973
1974 /* copy result to output buffers */
1975 if (display_buffer_byte) {
1976 /* do conversion */
1977 IMB_buffer_byte_from_float(display_buffer_byte,
1978 linear_buffer,
1979 channels,
1980 handle->dither,
1981 predivide,
1982 width,
1983 height,
1984 width,
1985 handle->start_line);
1986 }
1987
1988 if (display_buffer) {
1989 memcpy(display_buffer, linear_buffer, size_t(width) * height * channels * sizeof(float));
1990
1991 if (is_straight_alpha && channels == 4) {
1992 const size_t i_last = size_t(width) * height;
1993 size_t i;
1994 float *fp;
1995
1996 for (i = 0, fp = display_buffer; i != i_last; i++, fp += channels) {
1998 }
1999 }
2000 }
2001
2002 MEM_delete(linear_buffer);
2003}
2004
2006 const float *buffer,
2007 uchar *byte_buffer,
2008 float *display_buffer,
2009 uchar *display_buffer_byte,
2010 ColormanageProcessor *cm_processor)
2011{
2012 DisplayBufferInitData init_data;
2013
2014 init_data.ibuf = ibuf;
2015 init_data.cm_processor = cm_processor;
2016 init_data.buffer = buffer;
2017 init_data.byte_buffer = byte_buffer;
2018 init_data.display_buffer = display_buffer;
2019 init_data.display_buffer_byte = display_buffer_byte;
2020
2021 if (ibuf->byte_buffer.colorspace != nullptr) {
2022 init_data.byte_colorspace = ibuf->byte_buffer.colorspace->name().c_str();
2023 }
2024 else {
2025 /* happens for viewer images, which are not so simple to determine where to
2026 * set image buffer's color spaces
2027 */
2029 }
2030
2031 if (ibuf->float_buffer.colorspace != nullptr) {
2032 /* sequencer stores float buffers in non-linear space */
2033 init_data.float_colorspace = ibuf->float_buffer.colorspace->name().c_str();
2034 }
2035 else {
2036 init_data.float_colorspace = nullptr;
2037 }
2038
2039 threading::parallel_for(IndexRange(ibuf->y), 64, [&](const IndexRange y_range) {
2040 DisplayBufferThread handle;
2041 display_buffer_init_handle(&handle, y_range.first(), y_range.size(), &init_data);
2042 do_display_buffer_apply_thread(&handle);
2043 });
2044}
2045
2046/* Checks if given colorspace can be used for display as-is:
2047 * - View settings do not have extra curves, exposure, gamma or look applied, and
2048 * - Display colorspace is the same as given colorspace. */
2050 const ColorManagedViewSettings *view_settings,
2051 const ColorManagedDisplaySettings *display_settings)
2052{
2053 if (view_settings) {
2054 if ((view_settings->flag & COLORMANAGE_VIEW_USE_CURVES) != 0 ||
2055 (view_settings->flag & COLORMANAGE_VIEW_USE_WHITE_BALANCE) != 0 ||
2056 view_settings->exposure != 0.0f || view_settings->gamma != 1.0f)
2057 {
2058 return false;
2059 }
2060
2061 const ocio::Look *look = g_config()->get_look_by_name(view_settings->look);
2062 if (look != nullptr && !look->process_space().is_empty()) {
2063 return false;
2064 }
2065 }
2066
2067 const ColorSpace *display_colorspace = IMB_colormangement_display_get_color_space(
2068 view_settings, display_settings);
2069 if (display_colorspace != colorspace) {
2070 return false;
2071 }
2072
2073 if (view_settings == nullptr) {
2074 return true;
2075 }
2076
2077 const ocio::Display *display = g_config()->get_display_by_name(display_settings->display_device);
2078 const ocio::View *untonemapped_view = (display) ? display->get_untonemapped_view() : nullptr;
2079 return untonemapped_view && untonemapped_view->name() == view_settings->view_transform;
2080}
2081
2083 const ImBuf *ibuf,
2084 const ColorManagedViewSettings *view_settings,
2085 const ColorManagedDisplaySettings *display_settings,
2086 const ColorManagedDisplaySpace display_space)
2087{
2088 switch (display_space) {
2089 case DISPLAY_SPACE_DRAW:
2093 break;
2095 /* Always needed for custom scope space. */
2096 return true;
2097 }
2098
2099 if (ibuf->float_data() == nullptr && ibuf->byte_buffer.colorspace) {
2101 ibuf->byte_buffer.colorspace, view_settings, display_settings);
2102 }
2103 if (ibuf->byte_data() == nullptr && ibuf->float_buffer.colorspace) {
2105 ibuf->float_buffer.colorspace, view_settings, display_settings);
2106 }
2107 return true;
2108}
2109
2111 float *display_buffer,
2112 uchar *display_buffer_byte,
2113 const ColorManagedViewSettings *view_settings,
2114 const ColorManagedDisplaySettings *display_settings,
2115 const ColorManagedDisplaySpace display_space)
2116{
2117 std::optional<ColormanageProcessor> cm_processor;
2119 ibuf, view_settings, display_settings, display_space))
2120 {
2122 view_settings, display_settings, display_space, false, nullptr);
2123 }
2124
2125 ColormanageProcessor *processor = cm_processor ? &*cm_processor : nullptr;
2126
2128 ibuf->float_data(),
2129 ibuf->byte_data_for_write(),
2130 display_buffer,
2131 display_buffer_byte,
2132 processor);
2133}
2134
2136
2137/* -------------------------------------------------------------------- */
2140
2151
2161
2163 int start_line,
2164 int tot_line,
2165 ProcessorTransformInitData *init_data)
2166{
2167 const int channels = init_data->channels;
2168 const int width = init_data->width;
2169 const bool predivide = init_data->predivide;
2170
2171 const size_t offset = size_t(channels) * start_line * width;
2172
2173 memset(handle, 0, sizeof(ProcessorTransformThread));
2174
2175 handle->cm_processor = init_data->cm_processor;
2176
2177 if (init_data->byte_buffer != nullptr) {
2178 /* TODO(serge): Offset might be different for byte and float buffers. */
2179 handle->byte_buffer = init_data->byte_buffer + offset;
2180 }
2181 if (init_data->float_buffer != nullptr) {
2182 handle->float_buffer = init_data->float_buffer + offset;
2183 }
2184
2185 handle->width = width;
2186
2187 handle->start_line = start_line;
2188 handle->tot_line = tot_line;
2189
2190 handle->channels = channels;
2191 handle->predivide = predivide;
2192}
2193
2195{
2196 uchar *byte_buffer = handle->byte_buffer;
2197 float *float_buffer = handle->float_buffer;
2198 const int channels = handle->channels;
2199 const int width = handle->width;
2200 const int height = handle->tot_line;
2201 const bool predivide = handle->predivide;
2202 if (byte_buffer != nullptr) {
2203 handle->cm_processor->apply_byte(byte_buffer, width, height, channels);
2204 }
2205 if (float_buffer != nullptr) {
2206 handle->cm_processor->apply(float_buffer, width, height, channels, predivide);
2207 }
2208}
2209
2211 float *float_buffer,
2212 const int width,
2213 const int height,
2214 const int channels,
2215 ColormanageProcessor *cm_processor,
2216 const bool predivide)
2217{
2219
2220 init_data.cm_processor = cm_processor;
2221 init_data.byte_buffer = byte_buffer;
2222 init_data.float_buffer = float_buffer;
2223 init_data.width = width;
2224 init_data.height = height;
2225 init_data.channels = channels;
2226 init_data.predivide = predivide;
2227
2228 threading::parallel_for(IndexRange(height), 64, [&](const IndexRange y_range) {
2230 processor_transform_init_handle(&handle, y_range.first(), y_range.size(), &init_data);
2232 });
2233}
2234
2236
2237/* -------------------------------------------------------------------- */
2240
2241/* Convert the whole buffer from specified by name color space to another -
2242 * internal implementation. */
2243static void colormanagement_transform_ex(uchar *byte_buffer,
2244 float *float_buffer,
2245 int width,
2246 int height,
2247 int channels,
2248 const char *from_colorspace,
2249 const char *to_colorspace,
2250 bool predivide)
2251{
2252 if (from_colorspace[0] == '\0') {
2253 return;
2254 }
2255
2257 from_colorspace, to_colorspace);
2258 if (cm_processor.is_noop()) {
2259 return;
2260 }
2261
2263 byte_buffer, float_buffer, width, height, channels, &cm_processor, predivide);
2264}
2265
2267 int width,
2268 int height,
2269 int channels,
2270 const char *from_colorspace,
2271 const char *to_colorspace,
2272 bool predivide)
2273{
2275 nullptr, buffer, width, height, channels, from_colorspace, to_colorspace, predivide);
2276}
2277
2279 int width,
2280 int height,
2281 int channels,
2282 const char *from_colorspace,
2283 const char *to_colorspace)
2284{
2286 buffer, nullptr, width, height, channels, from_colorspace, to_colorspace, false);
2287}
2288
2290 const uchar *byte_buffer,
2291 int width,
2292 int height,
2293 int channels,
2294 const char *from_colorspace,
2295 const char *to_colorspace)
2296{
2297 if (from_colorspace == nullptr || from_colorspace[0] == '\0') {
2298 return;
2299 }
2300 if (STREQ(from_colorspace, to_colorspace) && channels == 4) {
2301 /* Color spaces are the same, just do a simple byte->float conversion. */
2302 int64_t pixel_count = int64_t(width) * height;
2303 threading::parallel_for(IndexRange(pixel_count), 256 * 1024, [&](IndexRange pix_range) {
2304 float *dst_ptr = float_buffer + pix_range.first() * channels;
2305 const uchar *src_ptr = byte_buffer + pix_range.first() * channels;
2306 for ([[maybe_unused]] const int i : pix_range) {
2307 /* Equivalent of rgba_uchar_to_float + premultiply. */
2308 float cr = float(src_ptr[0]) * (1.0f / 255.0f);
2309 float cg = float(src_ptr[1]) * (1.0f / 255.0f);
2310 float cb = float(src_ptr[2]) * (1.0f / 255.0f);
2311 float ca = float(src_ptr[3]) * (1.0f / 255.0f);
2312 dst_ptr[0] = cr * ca;
2313 dst_ptr[1] = cg * ca;
2314 dst_ptr[2] = cb * ca;
2315 dst_ptr[3] = ca;
2316 src_ptr += 4;
2317 dst_ptr += 4;
2318 }
2319 });
2320 return;
2321 }
2323 from_colorspace, to_colorspace);
2324 threading::parallel_for(IndexRange(height), 64, [&](const IndexRange y_range) {
2325 const size_t offset = size_t(channels) * y_range.first() * width;
2326 const uchar *src = byte_buffer + offset;
2327 float *dst = float_buffer + offset;
2328 IMB_buffer_float_from_byte(dst, src, width, y_range.size(), width, width);
2329 cm_processor.apply(dst, width, y_range.size(), channels, false);
2330 IMB_premultiply_rect_float(dst, 4, width, y_range.size());
2331 });
2332}
2333
2335 const char *from_colorspace,
2336 const char *to_colorspace)
2337{
2338 if (from_colorspace[0] == '\0') {
2339 return;
2340 }
2341
2342 if (STREQ(from_colorspace, to_colorspace)) {
2343 /* if source and destination color spaces are identical, skip
2344 * threading overhead and simply do nothing
2345 */
2346 return;
2347 }
2348
2350 from_colorspace, to_colorspace);
2351
2352 cm_processor.apply_v4(pixel);
2353}
2354
2356 const ColorSpace *colorspace)
2357{
2358 if (colorspace == nullptr) { /* should never happen */
2359 printf("%s: perform conversion from unknown color space\n", __func__);
2360 return;
2361 }
2362 const ocio::CPUProcessor *processor = colorspace->get_to_scene_linear_cpu_processor();
2363 if (processor == nullptr) {
2364 return;
2365 }
2366 processor->apply_rgb(pixel);
2367}
2368
2370 const ColorSpace *colorspace)
2371{
2372 if (colorspace == nullptr) { /* should never happen */
2373 printf("%s: perform conversion from unknown color space\n", __func__);
2374 return;
2375 }
2376 const ocio::CPUProcessor *processor = colorspace->get_from_scene_linear_cpu_processor();
2377 if (processor == nullptr) {
2378 return;
2379 }
2380 processor->apply_rgb(pixel);
2381}
2382
2384 const bool predivide,
2385 const ColorSpace *colorspace)
2386{
2387 if (colorspace == nullptr) { /* should never happen */
2388 printf("%s: perform conversion from unknown color space\n", __func__);
2389 return;
2390 }
2391 const ocio::CPUProcessor *processor = colorspace->get_to_scene_linear_cpu_processor();
2392 if (processor == nullptr) {
2393 return;
2394 }
2395 if (predivide) {
2396 processor->apply_rgba_predivide(pixel);
2397 }
2398 else {
2399 processor->apply_rgba(pixel);
2400 }
2401}
2402
2404 const int width,
2405 const int height,
2406 const int channels,
2407 const ColorSpace *colorspace,
2408 const bool predivide)
2409{
2410 if (colorspace == nullptr) { /* should never happen */
2411 printf("%s: perform conversion from unknown color space\n", __func__);
2412 return;
2413 }
2414 const ocio::CPUProcessor *processor = colorspace->get_to_scene_linear_cpu_processor();
2415 if (processor == nullptr) {
2416 return;
2417 }
2418 ocio::PackedImage img(buffer,
2419 width,
2420 height,
2421 channels,
2423 sizeof(float),
2424 size_t(channels) * sizeof(float),
2425 size_t(channels) * sizeof(float) * width);
2426
2427 if (predivide) {
2428 processor->apply_predivide(img);
2429 }
2430 else {
2431 processor->apply(img);
2432 }
2433}
2434
2436 const int width,
2437 const int height,
2438 const int channels,
2439 const ColorSpace *colorspace)
2440{
2441 if (colorspace == nullptr) { /* should never happen */
2442 printf("%s: perform conversion from unknown color space\n", __func__);
2443 return;
2444 }
2445 const ocio::CPUProcessor *processor = colorspace->get_from_scene_linear_cpu_processor();
2446 if (processor == nullptr) {
2447 return;
2448 }
2449 const ocio::PackedImage img(buffer,
2450 width,
2451 height,
2452 channels,
2454 sizeof(float),
2455 size_t(channels) * sizeof(float),
2456 size_t(channels) * sizeof(float) * width);
2457 processor->apply(img);
2458}
2459
2461 const int offset_x,
2462 const int offset_y,
2463 const int width,
2464 const int height,
2465 const ImBuf *ibuf,
2466 const bool store_premultiplied)
2467{
2468 /* Byte buffer storage, only for scene linear + sRGB, scene linear and data texture
2469 * since other color space conversions can't currently be done on the GPU. */
2470 BLI_assert(ibuf->byte_data());
2471 BLI_assert(ibuf->float_data() == nullptr);
2475
2476 const uchar *in_buffer = ibuf->byte_data();
2477 const bool use_premultiply = IMB_alpha_affects_rgb(ibuf) && store_premultiplied;
2478
2479 for (int y = 0; y < height; y++) {
2480 const size_t in_offset = (offset_y + y) * ibuf->x + offset_x;
2481 const size_t out_offset = y * width;
2482 const uchar *in = in_buffer + in_offset * 4;
2483 uchar *out = out_buffer + out_offset * 4;
2484
2485 if (use_premultiply) {
2486 /* Premultiply only. */
2487 for (int x = 0; x < width; x++, in += 4, out += 4) {
2488 out[0] = (in[0] * in[3]) >> 8;
2489 out[1] = (in[1] * in[3]) >> 8;
2490 out[2] = (in[2] * in[3]) >> 8;
2491 out[3] = in[3];
2492 }
2493 }
2494 else {
2495 /* Copy only. */
2496 for (int x = 0; x < width; x++, in += 4, out += 4) {
2497 out[0] = in[0];
2498 out[1] = in[1];
2499 out[2] = in[2];
2500 out[3] = in[3];
2501 }
2502 }
2503 }
2504}
2505
2507 const int offset_x,
2508 const int offset_y,
2509 const int width,
2510 const int height,
2511 const ImBuf *ibuf,
2512 const bool store_premultiplied)
2513{
2514 /* Float texture are stored in scene linear color space, with premultiplied
2515 * alpha depending on the image alpha mode. */
2516 if (ibuf->float_data()) {
2517 /* Float source buffer. */
2518 const float *in_buffer = ibuf->float_data();
2519 const int in_channels = ibuf->channels;
2520 const bool use_unpremultiply = IMB_alpha_affects_rgb(ibuf) && !store_premultiplied;
2521
2522 for (int y = 0; y < height; y++) {
2523 const size_t in_offset = (offset_y + y) * ibuf->x + offset_x;
2524 const size_t out_offset = y * width;
2525 const float *in = in_buffer + in_offset * in_channels;
2526 float *out = out_buffer + out_offset * 4;
2527
2528 if (in_channels == 1) {
2529 /* Copy single channel. */
2530 for (int x = 0; x < width; x++, in += 1, out += 4) {
2531 out[0] = in[0];
2532 out[1] = in[0];
2533 out[2] = in[0];
2534 out[3] = in[0];
2535 }
2536 }
2537 else if (in_channels == 3) {
2538 /* Copy RGB. */
2539 for (int x = 0; x < width; x++, in += 3, out += 4) {
2540 out[0] = in[0];
2541 out[1] = in[1];
2542 out[2] = in[2];
2543 out[3] = 1.0f;
2544 }
2545 }
2546 else if (in_channels == 4) {
2547 /* Copy or convert RGBA. */
2548 if (use_unpremultiply) {
2549 for (int x = 0; x < width; x++, in += 4, out += 4) {
2551 }
2552 }
2553 else {
2554 memcpy(out, in, sizeof(float[4]) * width);
2555 }
2556 }
2557 }
2558 }
2559 else {
2560 /* Byte source buffer. */
2561 const uchar *in_buffer = ibuf->byte_data();
2562 const bool use_premultiply = IMB_alpha_affects_rgb(ibuf) && store_premultiplied;
2563
2564 const ocio::CPUProcessor *processor =
2565 (ibuf->byte_buffer.colorspace) ?
2567 nullptr;
2568
2569 threading::parallel_for(IndexRange(height), 128, [&](const IndexRange y_range) {
2570 for (const int y : y_range) {
2571 const size_t in_offset = (offset_y + y) * ibuf->x + offset_x;
2572 const size_t out_offset = y * width;
2573 const uchar *in = in_buffer + in_offset * 4;
2574 float *out = out_buffer + out_offset * 4;
2575 for (int x = 0; x < width; x++, in += 4, out += 4) {
2576 /* Convert to scene linear and premultiply. */
2577 float pixel[4];
2578 rgba_uchar_to_float(pixel, in);
2579 if (processor) {
2580 processor->apply_rgb(pixel);
2581 }
2582 else {
2583 srgb_to_linearrgb_v3_v3(pixel, pixel);
2584 }
2585 if (use_premultiply) {
2586 mul_v3_fl(pixel, pixel[3]);
2587 }
2588 copy_v4_v4(out, pixel);
2589 }
2590 }
2591 });
2592 }
2593}
2594
2596 const float scene_linear[3])
2597{
2599 /* Create processor if none exists. */
2601
2603 std::shared_ptr<const ocio::CPUProcessor> cpu_processor = g_config()->get_cpu_processor(
2605
2606 if (cpu_processor) {
2608 }
2609 else {
2611 }
2612 }
2613
2615 }
2616
2617 copy_v3_v3(color_picking, scene_linear);
2618
2620 global_color_picking_state.cpu_processor_to->apply_rgb(color_picking);
2621 }
2622}
2623
2625 const float color_picking[3])
2626{
2628 /* Create processor if none exists. */
2630
2632 std::shared_ptr<const ocio::CPUProcessor> cpu_processor = g_config()->get_cpu_processor(
2634
2635 if (cpu_processor) {
2637 }
2638 else {
2640 }
2641 }
2642
2644 }
2645
2646 copy_v3_v3(scene_linear, color_picking);
2647
2649 global_color_picking_state.cpu_processor_from->apply_rgb(scene_linear);
2650 }
2651}
2652
2654 const ColorManagedDisplay *display,
2655 const ColorManagedDisplaySpace display_space)
2656{
2657 const ocio::CPUProcessor *processor = display->get_from_scene_linear_cpu_processor(
2658 display_space == DISPLAY_SPACE_DRAW);
2659 if (processor != nullptr) {
2660 processor->apply_rgb(pixel);
2661 }
2662}
2663
2665 const ColorManagedDisplay *display,
2666 const ColorManagedDisplaySpace display_space)
2667{
2668 const ocio::CPUProcessor *processor = display->get_to_scene_linear_cpu_processor(
2669 display_space == DISPLAY_SPACE_DRAW);
2670 if (processor != nullptr) {
2671 processor->apply_rgb(pixel);
2672 }
2673}
2674
2676 float result[4],
2677 const float pixel[4],
2678 const ColorManagedViewSettings *view_settings,
2679 const ColorManagedDisplaySettings *display_settings,
2680 const ColorManagedDisplaySpace display_space)
2681{
2683 view_settings, display_settings, display_space);
2684
2685 copy_v4_v4(result, pixel);
2686
2687 cm_processor.apply_v4(result);
2688}
2689
2690static ImBuf *imbuf_ensure_editable(ImBuf *ibuf, ImBuf *colormanaged_ibuf, bool allocate_result)
2691{
2692 if (colormanaged_ibuf != ibuf) {
2693 /* Is already an editable copy. */
2694 return colormanaged_ibuf;
2695 }
2696
2697 if (allocate_result) {
2698 /* Copy full image buffer. */
2699 return IMB_dupImBuf(ibuf);
2700 }
2701
2702 return ibuf;
2703}
2704
2705static const char *imbuf_colorspace_name(const ImBuf *ibuf, const bool prefer_byte_buffer)
2706{
2707 const bool use_float = ibuf->float_data() && !(prefer_byte_buffer && ibuf->byte_data());
2708 const ColorSpace &colorspace = use_float ? ibuf->float_colorspace() : ibuf->byte_colorspace();
2709 return colorspace.name().c_str();
2710}
2711
2713 bool save_as_render,
2714 bool allocate_result,
2715 const ImageFormatData *image_format)
2716{
2717 ImBuf *colormanaged_ibuf = ibuf;
2718
2719 /* Detect if we are writing to a file format that needs a linear float buffer. */
2720 const bool linear_float_output = BKE_imtype_requires_linear_float(image_format->imtype);
2721
2722 /* Detect if we are writing output a byte buffer, which we would need to create
2723 * with color management conversions applied. This may be for either applying the
2724 * display transform for renders, or a user specified color space for the file. */
2725 const bool byte_output = BKE_image_format_is_byte(image_format);
2726
2727 /* Create temporary byte image buffer if we are saving byte as float. */
2728 if (byte_output && ibuf->float_data() && ibuf->byte_data()) {
2729 colormanaged_ibuf = imbuf_ensure_editable(ibuf, colormanaged_ibuf, allocate_result);
2730 IMB_free_byte_pixels(colormanaged_ibuf);
2731 }
2732
2733 /* If we're saving from RGBA to RGB buffer then it's not so much useful to just ignore alpha --
2734 * it leads to bad artifacts especially when saving byte images.
2735 *
2736 * What we do here is we're overlaying our image on top of background color (which is currently
2737 * black). This is quite much the same as what Gimp does and it seems to be what artists
2738 * expects from saving.
2739 *
2740 * Do a conversion here, so image format writers could happily assume all the alpha tricks were
2741 * made already. helps keep things locally here, not spreading it to all possible image writers
2742 * we've got.
2743 */
2744 if (image_format->color_mode != ImColorMode::RGBA) {
2745 float color[3] = {0, 0, 0};
2746
2747 colormanaged_ibuf = imbuf_ensure_editable(ibuf, colormanaged_ibuf, allocate_result);
2748
2749 if (colormanaged_ibuf->float_data() && colormanaged_ibuf->channels == 4) {
2751 colormanaged_ibuf->x,
2752 colormanaged_ibuf->y,
2753 color);
2754 }
2755
2756 if (colormanaged_ibuf->byte_data()) {
2758 colormanaged_ibuf->x,
2759 colormanaged_ibuf->y,
2760 color);
2761 }
2762 }
2763
2764 if (save_as_render && !linear_float_output) {
2765 /* Render output: perform conversion to display space using view transform. */
2766 colormanaged_ibuf = imbuf_ensure_editable(ibuf, colormanaged_ibuf, allocate_result);
2767
2768 if (!colormanaged_ibuf->byte_data() && byte_output) {
2769 IMB_alloc_byte_pixels(colormanaged_ibuf);
2770 }
2771
2772 colormanage_display_buffer_process(colormanaged_ibuf,
2773 colormanaged_ibuf->float_data_for_write(),
2774 colormanaged_ibuf->byte_data_for_write(),
2775 &image_format->view_settings,
2776 &image_format->display_settings,
2777 image_format->media_type == MEDIA_TYPE_VIDEO ?
2780
2781 if (colormanaged_ibuf->float_data()) {
2782 /* Float buffer isn't linear anymore.
2783 * - Image format write callback checks for this flag and assumes no space
2784 * conversion should happen if ibuf->float_buffer.colorspace != nullptr. */
2786 &image_format->view_settings, &image_format->display_settings);
2787 if (byte_output) {
2788 colormanaged_ibuf->byte_buffer.colorspace = colormanaged_ibuf->float_buffer.colorspace;
2789 }
2790 }
2791 }
2792 else {
2793 /* Linear render or regular file output: conversion between two color spaces. */
2794
2795 /* Detect which color space we need to convert between. */
2796 const char *from_colorspace = imbuf_colorspace_name(colormanaged_ibuf, byte_output);
2797 const char *to_colorspace = image_format->linear_colorspace_settings.name;
2798
2799 /* to_colorspace may need to modified to compensate for 100 vs 203 nits conventions. */
2800 if (image_format->media_type != MEDIA_TYPE_VIDEO) {
2801 const ColorSpace *image_colorspace = g_config()->get_color_space_for_hdr_image(
2802 to_colorspace);
2803 if (image_colorspace) {
2804 to_colorspace = image_colorspace->name().c_str();
2805 }
2806 }
2807
2808 /* TODO: can we check with OCIO if color spaces are the same but have different names? */
2809 if (to_colorspace[0] == '\0' || STREQ(from_colorspace, to_colorspace)) {
2810 /* No conversion needed, but may still need to allocate byte buffer for output. */
2811 if (byte_output && !colormanaged_ibuf->byte_data()) {
2812 colormanaged_ibuf = imbuf_ensure_editable(ibuf, colormanaged_ibuf, allocate_result);
2813 colormanaged_ibuf->byte_buffer.colorspace = colormanaged_ibuf->float_buffer.colorspace;
2814 IMB_byte_from_float(colormanaged_ibuf);
2815 }
2816 }
2817 else {
2818 /* Color space conversion needed. */
2819 colormanaged_ibuf = imbuf_ensure_editable(ibuf, colormanaged_ibuf, allocate_result);
2820
2821 if (byte_output) {
2823 to_colorspace);
2824
2825 if (colormanaged_ibuf->byte_data()) {
2826 /* Byte to byte. */
2828 colormanaged_ibuf->x,
2829 colormanaged_ibuf->y,
2830 colormanaged_ibuf->channels,
2831 from_colorspace,
2832 to_colorspace);
2833 }
2834 else {
2835 /* Float to byte. */
2836 IMB_byte_from_float(colormanaged_ibuf);
2837 }
2838 }
2839 else {
2840 if (!colormanaged_ibuf->float_data()) {
2841 /* Byte to float. */
2842 IMB_float_from_byte(colormanaged_ibuf);
2843 IMB_free_byte_pixels(colormanaged_ibuf);
2844
2845 /* This conversion always goes to scene linear. */
2846 from_colorspace = global_role_scene_linear;
2847 }
2848
2849 if (colormanaged_ibuf->float_data()) {
2850 /* Float to float. */
2852 colormanaged_ibuf->x,
2853 colormanaged_ibuf->y,
2854 colormanaged_ibuf->channels,
2855 from_colorspace,
2856 to_colorspace,
2857 false);
2858
2860 to_colorspace);
2861 }
2862 }
2863 }
2864 }
2865
2866 return colormanaged_ibuf;
2867}
2868
2870 uint8_t *display_buffer,
2871 const float *linear_buffer,
2872 int width,
2873 int height,
2874 const ColorManagedViewSettings *view_settings,
2875 const ColorManagedDisplaySettings *display_settings)
2876{
2878 display_settings);
2879 float *buffer = MEM_new_array_uninitialized<float>(size_t(4) * width * height,
2880 "display transform temp buffer");
2881 memcpy(buffer, linear_buffer, sizeof(float) * 4 * width * height);
2882 processor.apply(buffer, width, height, 4, true);
2883 IMB_buffer_byte_from_float(display_buffer, buffer, 4, 0.0f, false, width, height, width);
2884 MEM_delete(buffer);
2885}
2886
2888
2889/* -------------------------------------------------------------------- */
2892
2894{
2895 const ocio::Display *display = g_config()->get_display_by_name(name);
2896 if (display) {
2897 return display->index;
2898 }
2899 return -1;
2900}
2901
2903{
2904 const ocio::Display *display = g_config()->get_display_by_index(index);
2905 if (!display) {
2906 return "";
2907 }
2908 return display->name().c_str();
2909}
2910
2912{
2913 const ocio::Display *display = g_config()->get_default_display();
2914 return display->name().c_str();
2915}
2916
2918{
2919 return g_config()->get_display_by_name(name);
2920}
2921
2923{
2924 if (g_config()->get_display_by_name("None") != nullptr) {
2925 return "None";
2926 }
2928}
2929
2931 const ColorManagedDisplay *display)
2932{
2933 const ocio::View *default_view = display->get_default_view();
2934 if (!default_view) {
2935 return "";
2936 }
2937 return default_view->name().c_str();
2938}
2939
2941 const ColorManagedViewSettings *view_settings,
2942 const ColorManagedDisplaySettings *display_settings)
2943{
2944 /* Get the colorspace that the image is in after applying this view and display
2945 * transform. If we are going to a display referred colorspace we can use that. */
2946 const ocio::Display *display = g_config()->get_display_by_name(display_settings->display_device);
2947 const ocio::View *view = (view_settings && display) ?
2948 display->get_view_by_name(view_settings->view_transform) :
2949 nullptr;
2950 const ColorSpace *colorspace = (view) ? view->display_colorspace() : nullptr;
2951 if (colorspace && colorspace->is_display_referred()) {
2952 return colorspace;
2953 }
2954 /* If not available, try to guess what the untonemapped view is and use its colorspace.
2955 * This is especially needed for v1 configs. */
2956 const ocio::View *untonemapped_view = (display) ? display->get_untonemapped_view() : nullptr;
2957 const ocio::ColorSpace *untonemapped_colorspace = (untonemapped_view) ?
2958 g_config()->get_display_view_color_space(
2959 display_settings->display_device,
2960 untonemapped_view->name()) :
2961 nullptr;
2962 return (untonemapped_colorspace) ? untonemapped_colorspace : colorspace;
2963}
2964
2966 const char *view_name)
2967{
2968 const ocio::Display *display = g_config()->get_display_by_name(display_settings->display_device);
2969 if (display == nullptr) {
2970 return false;
2971 }
2972 const ocio::View *view = display->get_view_by_name(view_name);
2973 return (view) ? view->is_hdr() : false;
2974}
2975
2977 const char *view_name)
2978{
2979 const ocio::Display *display = g_config()->get_display_by_name(display_settings->display_device);
2980 if (display == nullptr) {
2981 return false;
2982 }
2983 const ocio::View *view = display->get_view_by_name(view_name);
2984 return (view) ? view->gamut() != ocio::Gamut::Rec709 : false;
2985}
2986
2988 const ColorManagedDisplaySettings *display_settings, const char *view_name)
2989{
2990 const ocio::Display *display = g_config()->get_display_by_name(display_settings->display_device);
2991 if (display == nullptr) {
2992 return false;
2993 }
2994 const ocio::View *view = display->get_view_by_name(view_name);
2995 return (view) ? view->support_emulation() : false;
2996}
2997
2998int IMB_colormanagement_view_max_nits(const char *display_name, const char *view_name)
2999{
3000 const ocio::Display *display = g_config()->get_display_by_name(display_name);
3001 if (display == nullptr) {
3002 return 0;
3003 }
3004 const ocio::View *view = display->get_view_by_name(view_name);
3005 return view ? view->max_nits() : 0;
3006}
3007
3009 const ColorManagedDisplaySettings *display_settings,
3010 const ColorManagedViewSettings *view_settings)
3011{
3012 const ocio::Display *display = g_config()->get_display_by_name(display_settings->display_device);
3013 if (display == nullptr) {
3014 return {};
3015 }
3016 const ocio::View *view = (display) ? (view_settings) ?
3017 display->get_view_by_name(view_settings->view_transform) :
3018 display->get_untonemapped_view() :
3019 nullptr;
3020 if (view == nullptr) {
3021 return {};
3022 }
3023 return view->scope_info();
3024}
3025
3027
3028/* -------------------------------------------------------------------- */
3031
3033{
3034 return g_all_view_names().index_of_try(name);
3035}
3036
3038{
3039 /* The code is expected to be used for the purposes of the EnumPropertyItem and maintaining the
3040 * DNA values from RNA's get() and set(). It is unexpected that an invalid index will be passed
3041 * here, as it will indicate a coding error somewhere else. */
3042 if (index < 0 || index >= g_all_view_names().size()) {
3043 BLI_assert(0);
3044 return "";
3045 }
3046
3047 return g_all_view_names()[index].c_str();
3048}
3049
3050const char *IMB_colormanagement_view_get_default_name(const char *display_name)
3051{
3052 const ocio::Display *display = g_config()->get_display_by_name(display_name);
3053 if (!display) {
3054 return "";
3055 }
3056
3057 const ocio::View *view = display->get_default_view();
3058 if (!view) {
3059 return "";
3060 }
3061
3062 return view->name().c_str();
3063}
3064
3065const char *IMB_colormanagement_view_get_raw_or_default_name(const char *display_name)
3066{
3067 const ocio::Display *display = g_config()->get_display_by_name(display_name);
3068 if (!display) {
3069 return "";
3070 }
3071
3072 const ocio::View *view = nullptr;
3073
3074 if (!view) {
3075 view = display->get_view_by_name("Raw");
3076 }
3077
3078 if (!view) {
3079 view = display->get_default_view();
3080 }
3081
3082 if (!view) {
3083 return "";
3084 }
3085
3086 return view->name().c_str();
3087}
3088
3090
3091/* -------------------------------------------------------------------- */
3094
3096{
3097 return g_config()->get_color_space(name);
3098}
3099
3101{
3102 const char *role_colorspace = IMB_colormanagement_role_colorspace_name_get(role);
3103
3104 return colormanage_colorspace_get_named(role_colorspace);
3105}
3106
3108{
3109 /* Roles. */
3110 if (STREQ(name, OCIO_ROLE_SCENE_LINEAR)) {
3111 return g_config()->get_num_all_color_spaces();
3112 }
3113
3114 /* Regular color spaces. */
3116 if (colorspace) {
3117 return colorspace->index;
3118 }
3119 return -1;
3120}
3121
3123{
3124 /* Roles. */
3125 if (index == g_config()->get_num_all_color_spaces()) {
3127 }
3128
3129 /* Regular color spaces. */
3130 const ColorSpace *colorspace = g_config()->get_color_space_by_index(index);
3131 if (colorspace) {
3132 return colorspace->name().c_str();
3133 }
3134 return "";
3135}
3136
3138{
3139 return colorspace->name().c_str();
3140}
3141
3142void IMB_colormanagement_colorspace_name_set(char *name, char *interop_id, const char *new_name)
3143{
3144 BLI_strncpy_utf8(name, new_name, MAX_COLORSPACE_NAME);
3145 colormanage_name_to_interop_id(name, interop_id);
3146}
3147
3149 const char *name)
3150{
3151 IMB_colormanagement_colorspace_name_set(settings->name, settings->interop_id, name);
3152}
3153
3155{
3156 return colorspace->family().c_str();
3157}
3158
3160{
3161 return colorspace->description().c_str();
3162}
3163
3165 ColorManagedColorspaceSettings *colorspace_settings, ImBuf *ibuf)
3166{
3167 /* Don't modify non-color data space, it does not change with file type. */
3168 const ColorSpace *colorspace = g_config()->get_color_space(colorspace_settings->name);
3169
3170 if (colorspace && colorspace->is_data()) {
3171 return;
3172 }
3173
3174 /* Get color space from file type. */
3175 const ImFileType *type = IMB_file_type_from_ibuf(ibuf);
3176 if (type != nullptr) {
3177 if (type->save != nullptr) {
3178 const char *role_colorspace = IMB_colormanagement_role_colorspace_name_get(
3179 type->default_save_role);
3180 IMB_colormanagement_colorspace_settings_set(colorspace_settings, role_colorspace);
3181 }
3182 }
3183}
3184
3186
3187/* -------------------------------------------------------------------- */
3190
3192{
3193 const ocio::Look *look = g_config()->get_look_by_name(name);
3194 if (look) {
3195 return look->index;
3196 }
3197 return -1;
3198}
3199
3201{
3202 const ocio::Look *look = g_config()->get_look_by_index(index);
3203 if (!look) {
3204 return "";
3205 }
3206 return look->name().c_str();
3207}
3208
3210{
3211 const ocio::Look *look = g_config()->get_look_by_index(0);
3212 if (!look) {
3213 return "";
3214 }
3215 return look->name().c_str();
3216}
3217
3218const char *IMB_colormanagement_look_validate_for_view(const char *view_name,
3219 const char *look_name)
3220{
3221 const ocio::Look *look = g_config()->get_look_by_name(look_name);
3222 if (!look) {
3223 return look_name;
3224 }
3225
3226 /* Keep same look if compatible. */
3227 if (colormanage_compatible_look(look, view_name)) {
3228 return look_name;
3229 }
3230
3231 /* Try to find another compatible look with the same UI name, in case of looks specialized for
3232 * view transform, */
3233 for (const int other_look_index : IndexRange(g_config()->get_num_looks())) {
3234 const ocio::Look *other_look = g_config()->get_look_by_index(other_look_index);
3235 if (look->ui_name() == other_look->ui_name() &&
3236 colormanage_compatible_look(other_look, view_name))
3237 {
3238 return other_look->name().c_str();
3239 }
3240 }
3241
3243}
3244
3246
3247/* -------------------------------------------------------------------- */
3250
3251/* Should have enough bits of precision, and this can be reasonably high assuming
3252 * that if colorspaces are really this close, no point converting anyway. */
3253static const float imb_working_space_compare_threshold = 0.001f;
3254
3259
3264
3269
3271{
3272 const ColorSpace *scene_linear = g_config()->get_color_space(global_role_scene_linear_default);
3273
3274 /* Keep this in sync with known color spaces in
3275 * imb_colormanagement_working_space_set_from_matrix. */
3276 Vector<const ColorSpace *> working_spaces = {
3278 IMB_colormanagement_space_from_interop_id("lin_rec2020_scene"),
3280
3281 if (!working_spaces.contains(scene_linear)) {
3282 working_spaces.prepend(scene_linear);
3283 }
3284
3285 for (const ColorSpace *colorspace : working_spaces) {
3286 if (colorspace == nullptr) {
3287 continue;
3288 }
3289
3290 EnumPropertyItem item;
3291
3292 item.value = colorspace->index;
3293 item.name = colorspace->name().c_str();
3294 item.identifier = colorspace->name().c_str();
3295 item.icon = 0;
3296 item.description = colorspace->description().c_str();
3297
3298 RNA_enum_item_add(items, totitem, &item);
3299 }
3300}
3301
3306
3308{
3309 if (STREQ(global_role_scene_linear, name)) {
3310 return false;
3311 }
3312
3313 CLOG_DEBUG(&LOG, "Setting blend file working color space to '%s'", name);
3314
3316 g_config()->set_scene_linear_role(name);
3317
3321
3322 return true;
3323}
3324
3326 const char *name,
3327 const float3x3 &scene_linear_to_xyz,
3328 const bool report_missing)
3329{
3330 StringRefNull interop_id;
3331
3332 /* Check if we match the working space defined by the config. */
3333 if (math::is_equal(scene_linear_to_xyz,
3336 {
3337 /* Update scene linear name in case it is different for this config. */
3340 }
3341
3342 /* Check if we match a known working space made available in
3343 * IMB_colormanagement_working_space_items_add, that hopefully exists in the config. */
3344 if (math::is_equal(
3345 scene_linear_to_xyz, ocio::ACESCG_TO_XYZ, imb_working_space_compare_threshold))
3346 {
3347 interop_id = "lin_ap1_scene";
3348 }
3349 else if (math::is_equal(scene_linear_to_xyz,
3350 math::invert(ocio::XYZ_TO_REC709),
3352 {
3353 interop_id = "lin_rec709_scene";
3354 }
3355 else if (math::is_equal(scene_linear_to_xyz,
3356 math::invert(ocio::XYZ_TO_REC2020),
3358 {
3359 interop_id = "lin_rec2020_scene";
3360 }
3361
3362 if (!interop_id.is_empty()) {
3363 const ColorSpace *colorspace = g_config()->get_color_space_by_interop_id(interop_id);
3364 if (colorspace) {
3365 /* Update scene linear name in case it is different for this config. */
3366 STRNCPY(bmain->colorspace.scene_linear_name, colorspace->name().c_str());
3368 }
3369 }
3370
3371 /* We couldn't find a matching colorspace, set to the default and inform users.
3372 * We could try to preserve the original scene linear space, but that would require
3373 * editing the config at runtime to add it. Not trying to do that for now. */
3376
3377 if (report_missing) {
3378 CLOG_ERROR(
3379 &LOG, "Unknown scene linear working space '%s'. Missing OpenColorIO configuration?", name);
3381 }
3382
3384}
3385
3386static void imb_colormanagement_working_space_set_from_file(Main *bmain, const bool report_missing)
3387{
3388 /* For old files without info, assume current OpenColorIO config. */
3392 CLOG_DEBUG(&LOG,
3393 "Blend file has unknown scene linear working color space, setting to default");
3394 }
3395
3396 /* Change the working space to the one from the blend file. */
3400 report_missing);
3401}
3402
3404 const bool is_smaller_gamut,
3405 const float3 in_rgb)
3406{
3407 float3 rgb = m * in_rgb;
3408
3409 for (int i = 0; i < 3; i++) {
3410 /* Round to nicer fractions. */
3411 rgb[i] = 1e-5f * roundf(rgb[i] * 1e5f);
3412 /* Snap to 0 and 1. */
3413 if (fabsf(rgb[i]) < 5e-5) {
3414 rgb[i] = 0.0f;
3415 }
3416 else if (fabsf(1.0f - rgb[i]) < 5e-5) {
3417 rgb[i] = 1.0f;
3418 }
3419 /* Clamp when going to smaller gamut. We can't really distinguish
3420 * between HDR and out of gamut colors. */
3421 if (is_smaller_gamut) {
3422 rgb[i] = math::clamp(rgb[i], 0.0f, 1.0f);
3423 }
3424 }
3425
3426 return rgb;
3427}
3428
3430 const bool is_smaller_gamut,
3431 const ColorGeometry4f color)
3432{
3434 const float3 rgb = imb_working_space_convert(m, is_smaller_gamut, in_rgb);
3435 return color::premultiply_alpha(ColorGeometry4f(rgb[0], rgb[1], rgb[2], color[3]));
3436}
3437
3439 const float3x3 &current_scene_linear_to_xyz,
3440 const float3x3 &new_xyz_to_scene_linear,
3441 const bool depsgraph_tag,
3442 const bool linked_only,
3443 const bool editable_assets_only)
3444{
3445 /* If unknown, assume it's the OpenColorIO config scene linear space. */
3446 float3x3 bmain_scene_linear_to_xyz = math::is_zero(current_scene_linear_to_xyz) ?
3448 current_scene_linear_to_xyz;
3449
3450 float3x3 M = new_xyz_to_scene_linear * bmain_scene_linear_to_xyz;
3451
3452 /* Already in the same space? */
3454 return;
3455 }
3456
3457 if (math::determinant(M) == 0.0f) {
3458 CLOG_ERROR(&LOG, "Working space conversion matrix is not invertible");
3459 return;
3460 }
3461
3462 /* Determine if we are going to a smaller gamut and need to clamp. We prefer not to,
3463 * to preserve HDR colors, although they should not be common in properties. */
3464 bool is_smaller_gamut = false;
3465 for (int i = 0; i < 3; i++) {
3466 for (int j = 0; j < 3; j++) {
3467 if (M[i][j] < 0.0f) {
3468 is_smaller_gamut = true;
3469 }
3470 }
3471 }
3472
3473 /* Single color. */
3474 const auto single = [&M, is_smaller_gamut](float rgb[3]) {
3475 copy_v3_v3(rgb, imb_working_space_convert(M, is_smaller_gamut, float3(rgb)));
3476 };
3477
3478 /* Array with implicit sharing.
3479 *
3480 * We store references to all color arrays, so we can efficiently preserve implicit
3481 * sharing and write in place when possible. */
3482 struct ColorArrayInfo {
3484 Vector<ImplicitSharingPtr<> *> sharing_info_ptrs;
3485 /* Though it is unlikely, the same data array could be used among multiple geometries with
3486 * different domain sizes, so keep track of the maximum size among all users. */
3487 size_t max_size;
3488 };
3490
3491 const auto implicit_sharing_array =
3492 [&](ImplicitSharingPtr<> &sharing_info, ColorGeometry4f *&data, size_t size) {
3493 /* No data? */
3494 if (!sharing_info) {
3495 BLI_assert(size == 0);
3496 return;
3497 }
3498 color_array_map.add_or_modify(
3499 sharing_info.get(),
3500 [&](ColorArrayInfo *value) {
3501 new (value) ColorArrayInfo();
3502 value->data_ptrs.append(&data);
3503 value->sharing_info_ptrs.append(&sharing_info);
3504 value->max_size = size;
3505 },
3506 [&](ColorArrayInfo *value) {
3507 BLI_assert(data == *value->data_ptrs.last());
3508 value->data_ptrs.append(&data);
3509 value->sharing_info_ptrs.append(&sharing_info);
3510 value->max_size = std::max(value->max_size, size);
3511 });
3512 };
3513
3514 IDTypeForeachColorFunctionCallback fn = {single, implicit_sharing_array};
3515
3516 /* Iterate over IDs and embedded IDs. No need to do it for master collections
3517 * though, they don't have colors. */
3518 /* TODO: Multithreading over IDs? */
3519 ID *id_iter;
3520 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
3521 if (linked_only) {
3522 if (!id_iter->lib) {
3523 continue;
3524 }
3525 }
3526 if (editable_assets_only) {
3527 if (!(id_iter->lib && (id_iter->lib->runtime->tag & LIBRARY_ASSET_EDITABLE))) {
3528 continue;
3529 }
3530 }
3531
3532 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id_iter);
3533 if (id_type->foreach_working_space_color) {
3534 id_type->foreach_working_space_color(id_iter, fn);
3535 if (depsgraph_tag) {
3537 }
3538 }
3539
3540 if (bNodeTree *node_tree = bke::node_tree_from_id(id_iter)) {
3541 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(&node_tree->id);
3542 if (id_type->foreach_working_space_color) {
3543 id_type->foreach_working_space_color(&node_tree->id, fn);
3544 }
3545 }
3546 }
3548
3549 /* Handle implicit sharing arrays. */
3551 color_array_map.items().begin(), color_array_map.items().end());
3552
3553 threading::parallel_for(color_array_items.index_range(), 64, [&](const IndexRange range) {
3554 for (const int item_index : range) {
3555 const auto &item = color_array_items[item_index];
3556
3557 if (item.value.data_ptrs.size() == item.key->strong_users()) {
3558 /* All of the users of the array data are from the main we're converting, so we can change
3559 * the data array in place without allocating a new version. */
3560 item.key->tag_ensured_mutable();
3561 MutableSpan<ColorGeometry4f> data(*item.value.data_ptrs.first(), item.value.max_size);
3562 threading::parallel_for(data.index_range(), 1024, [&](const IndexRange range) {
3563 for (const int64_t i : range) {
3564 data[i] = imb_working_space_convert(M, is_smaller_gamut, data[i]);
3565 }
3566 });
3567 }
3568 else {
3569 /* Somehow the data is used by something outside of the Main we're currently converting, it
3570 * has to be duplicated before being converted to avoid changing the original. */
3571 const Span<ColorGeometry4f> src_data(*item.value.data_ptrs.first(), item.value.max_size);
3572
3573 auto *dst_data = MEM_new_array_uninitialized<ColorGeometry4f>(
3574 src_data.size(), "IMB_colormanagement_working_space_convert");
3575 const ImplicitSharingPtr<> sharing_ptr(implicit_sharing::info_for_mem_free(dst_data));
3576
3577 threading::parallel_for(src_data.index_range(), 1024, [&](const IndexRange range) {
3578 for (const int64_t i : range) {
3579 dst_data[i] = imb_working_space_convert(M, is_smaller_gamut, src_data[i]);
3580 }
3581 });
3582
3583 /* Replace the data pointer and the sharing info pointer with the new data in all of the
3584 * users from the main data-base. The sharing pointer assignment adds a user. */
3585 for (ColorGeometry4f **pointer : item.value.data_ptrs) {
3586 *pointer = dst_data;
3587 }
3588 for (ImplicitSharingPtr<> *pointer : item.value.sharing_info_ptrs) {
3589 *pointer = sharing_ptr;
3590 }
3591 }
3592 }
3593 });
3594}
3595
3596void IMB_colormanagement_working_space_convert(Main *bmain, const Main *reference_bmain)
3597{
3598 /* If unknown, assume it's the OpenColorIO config scene linear space. */
3599 float3x3 reference_scene_linear_to_xyz = math::is_zero(
3600 reference_bmain->colorspace.scene_linear_to_xyz) ?
3602 reference_bmain->colorspace.scene_linear_to_xyz;
3603
3606 math::invert(reference_scene_linear_to_xyz),
3607 false);
3608
3611}
3612
3618
3620{
3621 /* If using the default config, keep the one saved in the startup blend.
3622 * If using the non-default OCIO config, assume we want the working space from that config. */
3623 const bool is_custom_config = !ELEM(g_config_active().source,
3626 if (math::is_zero(bmain->colorspace.scene_linear_to_xyz) || is_custom_config) {
3628 }
3629}
3630
3635
3637 Main *old_bmain,
3638 const bool is_startup,
3639 const bool have_editable_assets)
3640{
3641 if (is_startup) {
3643 }
3644
3645 const bool report_missing = !is_startup;
3647
3648 /* Inform user when project config failed to load. */
3649 for (const ColorManagedConfigPath &candidate : g_config_requested()) {
3650 if (candidate == g_config_active()) {
3651 break;
3652 }
3653 if (candidate.source == ColorManagedConfigSource::Project) {
3656 break;
3657 }
3658 }
3659
3660 /* Convert editable assets in the previous file, before they are moved to the new file. */
3661 if (have_editable_assets) {
3665 false,
3666 false,
3667 true);
3668 }
3669}
3670
3672{
3673 /* Keep project config failure, unless the file was saved into another project. */
3676 }
3677
3678 /* Missing color spaces in the file are resolved by saving. */
3680}
3681
3682void IMB_colormanagement_undo_read_post(Main *bmain, const MainColorspace &old_colorspace)
3683{
3684 /* Preserve config warnings. */
3687
3688 /* Undo leaves linked data unchanged, in the working space from before undo. */
3689 const float3x3 previous_scene_linear_to_xyz = colorspace::scene_linear_to_xyz;
3690
3691 const bool report_missing = bmain->filepath[0] != '\0';
3693
3695 previous_scene_linear_to_xyz,
3697 true,
3698 true);
3699}
3700
3702
3703/* -------------------------------------------------------------------- */
3706
3708{
3709 /* Group by SDR/HDR, to help communicate what obscure Rec.XXXX names do. */
3710 for (const bool hdr : {false, true}) {
3711 bool first = true;
3712
3713 for (const int display_index : IndexRange(g_config()->get_num_displays())) {
3714 const ocio::Display *display = g_config()->get_display_by_index(display_index);
3715
3716 if (display->is_hdr() != hdr) {
3717 continue;
3718 }
3719
3720 if (first) {
3721 EnumPropertyItem item;
3722 item.value = -1;
3723 item.name = (hdr) ? "HDR" : "SDR";
3724 item.identifier = "";
3725 item.icon = 0;
3726 item.description = "";
3727 RNA_enum_item_add(items, totitem, &item);
3728
3729 first = false;
3730 }
3731
3732 EnumPropertyItem item;
3733
3734 item.value = display->index;
3735 item.name = display->ui_name().c_str();
3736 item.identifier = display->name().c_str();
3737 item.icon = 0;
3738 item.description = display->description().c_str();
3739
3740 RNA_enum_item_add(items, totitem, &item);
3741 }
3742 }
3743}
3744
3746 int *totitem,
3747 const char *display_name)
3748{
3749 const ocio::Display *display = g_config()->get_display_by_name(display_name);
3750 if (!display) {
3751 return;
3752 }
3753
3754 for (const int view_index : IndexRange(display->get_num_views())) {
3755 const ocio::View *view = display->get_view_by_index(view_index);
3756
3757 EnumPropertyItem item;
3758
3760 item.name = view->name().c_str();
3761 item.identifier = view->name().c_str();
3762 item.icon = 0;
3763 item.description = view->description().c_str();
3764
3765 RNA_enum_item_add(items, totitem, &item);
3766 }
3767}
3768
3770 int *totitem,
3771 const char *view_name)
3772{
3773 const StringRef view_filter = view_filter_for_look(view_name);
3774
3775 for (const int look_index : IndexRange(g_config()->get_num_looks())) {
3776 const ocio::Look *look = g_config()->get_look_by_index(look_index);
3777 if (!colormanage_compatible_look(look, view_filter)) {
3778 continue;
3779 }
3780
3781 EnumPropertyItem item;
3782
3783 item.value = look->index;
3784 item.name = look->ui_name().c_str();
3785 item.identifier = look->name().c_str();
3786 item.icon = 0;
3787 item.description = look->description().c_str();
3788
3789 RNA_enum_item_add(items, totitem, &item);
3790 }
3791}
3792
3794{
3795 /* Same color spaces as #IMB_colormanagement_colorspace_items_add, with Primary
3796 * interop IDs only to avoid duplicates. */
3797 Vector<const ColorSpace *> colorspaces;
3798 for (const int colorspace_index : IndexRange(g_config()->get_num_active_color_spaces())) {
3799 const ColorSpace *colorspace = g_config()->get_sorted_color_space_by_index(colorspace_index);
3800 if (colorspace->is_primary_interop_id()) {
3801 colorspaces.append(colorspace);
3802 }
3803 }
3804 std::ranges::sort(colorspaces, [](const ColorSpace *a, const ColorSpace *b) {
3805 return a->interop_id() < b->interop_id();
3806 });
3807
3808 for (const ColorSpace *colorspace : colorspaces) {
3809 EnumPropertyItem item;
3810 item.value = colorspace->index;
3811 item.name = colorspace->interop_id().c_str();
3812 item.identifier = colorspace->interop_id().c_str();
3813 item.icon = 0;
3814 item.description = colorspace->name().c_str();
3815 RNA_enum_item_add(items, totitem, &item);
3816 }
3817
3818 /* Alternate interop IDs, for example srgb_rec709_scene for srgb_rec709_display. */
3819 for (const ColorSpace *colorspace : colorspaces) {
3820 if (colorspace->alternate_interop_id().is_empty()) {
3821 continue;
3822 }
3823
3824 EnumPropertyItem item;
3825 item.value = colorspace->index + g_config()->get_num_all_color_spaces();
3826 item.name = colorspace->alternate_interop_id().c_str();
3827 item.identifier = colorspace->alternate_interop_id().c_str();
3828 item.icon = 0;
3829 item.description = colorspace->name().c_str();
3830 RNA_enum_item_add(items, totitem, &item);
3831 }
3832}
3833
3835{
3836 /* Regular color spaces. */
3837 for (const int colorspace_index : IndexRange(g_config()->get_num_active_color_spaces())) {
3838 const ColorSpace *colorspace = g_config()->get_sorted_color_space_by_index(colorspace_index);
3839
3840 EnumPropertyItem item;
3841
3842 item.value = colorspace->index;
3843 item.name = colorspace->name().c_str();
3844 item.identifier = colorspace->name().c_str();
3845 item.icon = 0;
3846 item.description = colorspace->description().c_str();
3847
3848 RNA_enum_item_add(items, totitem, &item);
3849 }
3850
3851 /* Scene linear role. This is useful for example to create compositing convert colorspace
3852 * nodes that work the same regardless of working space. */
3853 EnumPropertyItem item;
3854
3855 item.value = g_config()->get_num_all_color_spaces();
3856 item.name = "Working Space";
3858 item.icon = 0;
3859 item.description = "Working color space of the current file";
3860
3861 RNA_enum_item_add(items, totitem, &item);
3862}
3863
3865
3866/* -------------------------------------------------------------------- */
3869
3871{
3872 cpu_processor_ = std::move(other.cpu_processor_);
3873 curve_mapping_ = other.curve_mapping_;
3874 is_data_result_ = other.is_data_result_;
3875
3876 other.cpu_processor_ = nullptr;
3877 other.curve_mapping_ = nullptr;
3878 other.is_data_result_ = false;
3879}
3881{
3882 if (this == &other) {
3883 return *this;
3884 }
3885
3886 if (curve_mapping_) {
3887 BKE_curvemapping_free(curve_mapping_);
3888 }
3889
3890 cpu_processor_ = std::move(other.cpu_processor_);
3891 curve_mapping_ = other.curve_mapping_;
3892 is_data_result_ = other.is_data_result_;
3893
3894 other.cpu_processor_ = nullptr;
3895 other.curve_mapping_ = nullptr;
3896 other.is_data_result_ = false;
3897
3898 return *this;
3899}
3900
3902{
3903 if (curve_mapping_) {
3904 BKE_curvemapping_free(curve_mapping_);
3905 }
3906}
3907
3909 StringRefNull to_colorspace)
3910{
3911 ColormanageProcessor processor;
3912
3913 processor.is_data_result_ = IMB_colormanagement_space_name_is_data(to_colorspace.c_str());
3914
3915 if (from_colorspace == to_colorspace) {
3916 return processor;
3917 }
3918
3919 processor.cpu_processor_ = g_config()->get_cpu_processor(from_colorspace, to_colorspace);
3920
3921 return processor;
3922}
3923
3925 const ColorSpace &to_colorspace)
3926{
3927 ColormanageProcessor processor;
3928
3929 processor.is_data_result_ = to_colorspace.is_data();
3930 processor.cpu_processor_ = to_colorspace.get_from_scene_linear_cpu_processor();
3931
3932 return processor;
3933}
3934
3936 const ColorSpace &from_colorspace)
3937{
3938 ColormanageProcessor processor;
3939
3940 processor.is_data_result_ = from_colorspace.is_data();
3941 processor.cpu_processor_ = from_colorspace.get_to_scene_linear_cpu_processor();
3942
3943 return processor;
3944}
3945
3947 const ColorManagedViewSettings *view_settings,
3948 const ColorManagedDisplaySettings *display_settings,
3949 const ColorManagedDisplaySpace display_space,
3950 const bool inverse,
3951 const char *from_colorspace)
3952{
3953 ColorManagedViewSettings untonemapped_view_settings;
3954 const ColorManagedViewSettings *applied_view_settings;
3955 const ColorSpace *display_colorspace;
3956
3957 ColormanageProcessor cm_processor;
3958
3959 if (view_settings) {
3960 applied_view_settings = view_settings;
3961 }
3962 else {
3963 IMB_colormanagement_init_untonemapped_view_settings(&untonemapped_view_settings,
3964 display_settings);
3965 applied_view_settings = &untonemapped_view_settings;
3966 }
3967
3968 display_colorspace = IMB_colormangement_display_get_color_space(applied_view_settings,
3969 display_settings);
3970 if (display_colorspace) {
3971 cm_processor.is_data_result_ = display_colorspace->is_data();
3972 }
3973
3974 const bool use_white_balance = applied_view_settings->flag & COLORMANAGE_VIEW_USE_WHITE_BALANCE;
3975 const char *processor_from_colorspace = from_colorspace ? from_colorspace :
3977 cm_processor.cpu_processor_ = get_display_buffer_processor(*display_settings,
3978 applied_view_settings->look,
3979 applied_view_settings->view_transform,
3980 applied_view_settings->exposure,
3981 applied_view_settings->gamma,
3982 applied_view_settings->temperature,
3983 applied_view_settings->tint,
3984 use_white_balance,
3985 processor_from_colorspace,
3986 display_space,
3987 inverse);
3988
3989 if (applied_view_settings->flag & COLORMANAGE_VIEW_USE_CURVES) {
3990 cm_processor.curve_mapping_ = BKE_curvemapping_copy(applied_view_settings->curve_mapping);
3991 BKE_curvemapping_premultiply(cm_processor.curve_mapping_, false);
3992 }
3993
3994 return cm_processor;
3995}
3996
3997std::optional<ColormanageProcessor> ColormanageProcessor::display_processor_for_imbuf(
3998 const ImBuf *ibuf,
3999 const ColorManagedViewSettings *view_settings,
4000 const ColorManagedDisplaySettings *display_settings,
4001 const ColorManagedDisplaySpace display_space)
4002{
4004 ibuf, view_settings, display_settings, display_space))
4005 {
4006 return std::nullopt;
4007 }
4008 const char *from_colorspace = imbuf_colorspace_name(ibuf, false);
4009 return display_processor_new(
4010 view_settings, display_settings, display_space, false, from_colorspace);
4011}
4012
4014{
4015 return is_data_result_;
4016}
4017
4019{
4020 if (curve_mapping_) {
4021 /* Consider processor which has curve mapping as a non no-op.
4022 * This is mainly for the simplicity of the check, since the current cases where this
4023 * function is used the curve mapping is never assigned. */
4024 return false;
4025 }
4026
4027 const ocio::CPUProcessor *cpu_processor = get_cpu_processor();
4028 if (!cpu_processor) {
4029 /* The CPU processor might have failed to be created, for example when the requested color
4030 * space does not exist in the configuration, or if there is a missing lookup table, or the
4031 * configuration is invalid due to other reasons.
4032 *
4033 * The actual processing checks for the cpu_processor not being null pointer, and it if is
4034 * then processing does not apply it. However, processing could still apply curve mapping.
4035 *
4036 * Hence a null-pointer here, which happens after the curve mapping check, but before
4037 * accessing cpu_processor. */
4038 return true;
4039 }
4040
4041 return cpu_processor->is_noop();
4042}
4043
4044void ColormanageProcessor::apply_v4(float pixel[4]) const
4045{
4046 if (curve_mapping_) {
4047 BKE_curvemapping_evaluate_premulRGBF(curve_mapping_, pixel, pixel);
4048 }
4049
4050 const ocio::CPUProcessor *cpu_processor = get_cpu_processor();
4051 if (cpu_processor) {
4052 cpu_processor->apply_rgba(pixel);
4053 }
4054}
4055
4057{
4058 if (curve_mapping_) {
4059 BKE_curvemapping_evaluate_premulRGBF(curve_mapping_, pixel, pixel);
4060 }
4061
4062 const ocio::CPUProcessor *cpu_processor = get_cpu_processor();
4063 if (cpu_processor) {
4064 cpu_processor->apply_rgba_predivide(pixel);
4065 }
4066}
4067
4068void ColormanageProcessor::apply_v3(float pixel[3]) const
4069{
4070 if (curve_mapping_) {
4071 BKE_curvemapping_evaluate_premulRGBF(curve_mapping_, pixel, pixel);
4072 }
4073
4074 const ocio::CPUProcessor *cpu_processor = get_cpu_processor();
4075 if (cpu_processor) {
4076 cpu_processor->apply_rgb(pixel);
4077 }
4078}
4079
4080void ColormanageProcessor::apply_pixel(float *pixel, int channels) const
4081{
4082 if (channels == 4) {
4083 apply_v4_predivide(pixel);
4084 }
4085 else if (channels == 3) {
4086 apply_v3(pixel);
4087 }
4088 else if (channels == 1) {
4089 if (curve_mapping_) {
4090 curve_mapping_apply_pixel(curve_mapping_, pixel, 1);
4091 }
4092 }
4093 else {
4095 false, "Incorrect number of channels passed to IMB_colormanagement_processor_apply_pixel");
4096 }
4097}
4098
4100 float *buffer, int width, int height, int channels, bool predivide) const
4101{
4102 /* apply curve mapping */
4103 if (curve_mapping_) {
4104 int x, y;
4105
4106 for (y = 0; y < height; y++) {
4107 for (x = 0; x < width; x++) {
4108 float *pixel = buffer + channels * (size_t(y) * width + x);
4109
4110 curve_mapping_apply_pixel(curve_mapping_, pixel, channels);
4111 }
4112 }
4113 }
4114
4115 const ocio::CPUProcessor *cpu_processor = get_cpu_processor();
4116 if (cpu_processor && channels >= 3) {
4117 /* apply OCIO processor */
4118 const ocio::PackedImage img(buffer,
4119 width,
4120 height,
4121 channels,
4123 sizeof(float),
4124 size_t(channels) * sizeof(float),
4125 size_t(channels) * sizeof(float) * width);
4126
4127 if (predivide) {
4128 cpu_processor->apply_predivide(img);
4129 }
4130 else {
4131 cpu_processor->apply(img);
4132 }
4133 }
4134}
4135
4136void ColormanageProcessor::apply_byte(uchar *buffer, int width, int height, int channels) const
4137{
4138 /* TODO(sergey): Would be nice to support arbitrary channels configurations,
4139 * but for now it's not so important.
4140 */
4141 BLI_assert(channels == 4);
4142 float pixel[4];
4143 for (int y = 0; y < height; y++) {
4144 for (int x = 0; x < width; x++) {
4145 size_t offset = channels * (size_t(y) * width + x);
4146 rgba_uchar_to_float(pixel, buffer + offset);
4147 apply_v4(pixel);
4148 rgba_float_to_uchar(buffer + offset, pixel);
4149 }
4150 }
4151}
4152
4153/* **** OpenGL drawing routines using GLSL for color space transform ***** */
4154
4156{
4157 /* Using curve mapping? */
4158 const bool use_curve_mapping = (view_settings->flag & COLORMANAGE_VIEW_USE_CURVES) != 0;
4159 if (!use_curve_mapping) {
4160 return nullptr;
4161 }
4162
4163 /* Already up to date? */
4164 if (view_settings->curve_mapping->changed_timestamp ==
4167 {
4168 return view_settings->curve_mapping;
4169 }
4170
4171 /* We're using curve mapping's address as a cache ID, so we need to make sure re-allocation
4172 * gives new address here. We do this by allocating new curve mapping before freeing old one.
4173 */
4174 CurveMapping *new_curve_mapping = BKE_curvemapping_copy(view_settings->curve_mapping);
4175
4179 }
4180
4181 /* Fill in OCIO's curve mapping settings. */
4182 global_gpu_state.curve_mapping = new_curve_mapping;
4186
4188}
4189
4191 const ColorManagedViewSettings *view_settings,
4192 const ColorManagedDisplaySettings *display_settings,
4193 const ColorSpace *from_colorspace,
4194 float dither,
4195 bool predivide,
4196 bool do_overlay_merge,
4197 ColorManagedDisplaySpace display_space,
4198 float opacity)
4199{
4200 ColorManagedViewSettings untonemapped_view_settings;
4201 const ColorManagedViewSettings *applied_view_settings;
4202
4203 if (view_settings) {
4204 applied_view_settings = view_settings;
4205 }
4206 else {
4207 /* If no view settings were specified, use default ones, which will attempt not to do any
4208 * extra color correction. */
4209 IMB_colormanagement_init_untonemapped_view_settings(&untonemapped_view_settings,
4210 display_settings);
4211 applied_view_settings = &untonemapped_view_settings;
4212 }
4213
4214 /* Ensure curve mapping is up to date. */
4215 CurveMapping *applied_curve_mapping = update_glsl_curve_mapping(applied_view_settings);
4216
4217 /* GPU shader parameters. */
4218 const bool use_look = colormanage_use_look(applied_view_settings->look,
4219 applied_view_settings->view_transform);
4220 const float exposure = applied_view_settings->exposure;
4221 const float gamma = applied_view_settings->gamma;
4222
4223 /* TODO(@sergey): Use designated initializer. */
4224 ocio::GPUDisplayParameters display_parameters;
4225 display_parameters.from_colorspace = from_colorspace ? from_colorspace->name().c_str() :
4227 display_parameters.view = applied_view_settings->view_transform;
4228 display_parameters.display = display_settings->display_device;
4229 display_parameters.look = (use_look) ? applied_view_settings->look : "";
4230 display_parameters.curve_mapping = applied_curve_mapping;
4231 display_parameters.scale = (exposure == 0.0f) ? 1.0f : powf(2.0f, exposure);
4232 display_parameters.exponent = (gamma == 1.0f) ? 1.0f : 1.0f / max_ff(FLT_EPSILON, gamma);
4233 display_parameters.dither = dither;
4234 display_parameters.temperature = applied_view_settings->temperature;
4235 display_parameters.tint = applied_view_settings->tint;
4236 display_parameters.use_white_balance = (applied_view_settings->flag &
4238 display_parameters.use_predivide = predivide;
4239 display_parameters.do_overlay_merge = do_overlay_merge;
4240 display_parameters.use_hdr_buffer = GPU_hdr_support();
4242 display_settings, display_parameters.view.c_str());
4243 display_parameters.use_display_emulation = (display_space == DISPLAY_SPACE_DRAW) ?
4244 get_display_emulation(*display_settings) :
4245 false;
4246 display_parameters.use_scope_space = (display_space == DISPLAY_SPACE_SCOPE);
4247 display_parameters.opacity = opacity;
4248
4249 /* Bind shader. Internally GPU shaders are created and cached on demand. */
4250 global_gpu_state.gpu_shader_bound = g_config()->get_gpu_shader_binder().display_bind(
4251 display_parameters);
4252
4254}
4255
4257 const ColorManagedDisplaySettings *display_settings,
4258 float dither,
4259 bool predivide)
4260{
4262 view_settings, display_settings, nullptr, dither, predivide, false);
4263}
4264
4266 const ColorSpace *from_colorspace,
4267 float dither,
4268 bool predivide)
4269{
4270 ColorManagedViewSettings *view_settings;
4271 ColorManagedDisplaySettings *display_settings;
4272
4273 IMB_colormanagement_display_settings_from_ctx(C, &view_settings, &display_settings);
4274
4276 view_settings, display_settings, from_colorspace, dither, predivide, false);
4277}
4278
4279bool IMB_colormanagement_setup_glsl_draw_ctx(const bContext *C, float dither, bool predivide)
4280{
4281 return IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, nullptr, dither, predivide);
4282}
4283
4284bool IMB_colormanagement_setup_glsl_draw_to_scene_linear(const char *from_colorspace_name,
4285 const bool predivide)
4286{
4287 global_gpu_state.gpu_shader_bound = g_config()->get_gpu_shader_binder().to_scene_linear_bind(
4288 from_colorspace_name, predivide);
4290}
4291
4293{
4295 g_config()->get_gpu_shader_binder().unbind();
4297 }
4298}
4299
4301
4302/* -------------------------------------------------------------------- */
4305
4306/* Calculate color in range 800..12000 using an approximation
4307 * a/x+bx+c for R and G and ((at + b)t + c)t + d for B
4308 *
4309 * The result of this can be negative to support gamut wider than
4310 * than rec.709, just needs to be clamped. */
4311
4312static const float blackbody_table_r[7][3] = {{1.61919106e+03f, -2.05010916e-03f, 5.02995757e+00f},
4313 {2.48845471e+03f, -1.11330907e-03f, 3.22621544e+00f},
4314 {3.34143193e+03f, -4.86551192e-04f, 1.76486769e+00f},
4315 {4.09461742e+03f, -1.27446582e-04f, 7.25731635e-01f},
4316 {4.67028036e+03f, 2.91258199e-05f, 1.26703442e-01f},
4317 {4.59509185e+03f, 2.87495649e-05f, 1.50345020e-01f},
4318 {3.78717450e+03f, 9.35907826e-06f, 3.99075871e-01f}};
4319
4320static const float blackbody_table_g[7][3] = {
4321 {-4.88999748e+02f, 6.04330754e-04f, -7.55807526e-02f},
4322 {-7.55994277e+02f, 3.16730098e-04f, 4.78306139e-01f},
4323 {-1.02363977e+03f, 1.20223470e-04f, 9.36662319e-01f},
4324 {-1.26571316e+03f, 4.87340896e-06f, 1.27054498e+00f},
4325 {-1.42529332e+03f, -4.01150431e-05f, 1.43972784e+00f},
4326 {-1.17554822e+03f, -2.16378048e-05f, 1.30408023e+00f},
4327 {-5.00799571e+02f, -4.59832026e-06f, 1.09098763e+00f}};
4328
4329static const float blackbody_table_b[7][4] = {
4330 {5.96945309e-11f, -4.85742887e-08f, -9.70622247e-05f, -4.07936148e-03f},
4331 {2.40430366e-11f, 5.55021075e-08f, -1.98503712e-04f, 2.89312858e-02f},
4332 {-1.40949732e-11f, 1.89878968e-07f, -3.56632824e-04f, 9.10767778e-02f},
4333 {-3.61460868e-11f, 2.84822009e-07f, -4.93211319e-04f, 1.56723440e-01f},
4334 {-1.97075738e-11f, 1.75359352e-07f, -2.50542825e-04f, -2.22783266e-02f},
4335 {-1.61997957e-13f, -1.64216008e-08f, 3.86216271e-04f, -7.38077418e-01f},
4336 {6.72650283e-13f, -2.73078809e-08f, 4.24098264e-04f, -7.52335691e-01f}};
4337
4338static void blackbody_temperature_to_rec709(float rec709[3], float t)
4339{
4340 if (t >= 12000.0f) {
4341 rec709[0] = 0.8262954810464208f;
4342 rec709[1] = 0.9945080501520986f;
4343 rec709[2] = 1.566307710274283f;
4344 }
4345 else if (t < 800.0f) {
4346 rec709[0] = 5.413294490189271f;
4347 rec709[1] = -0.20319390035873933f;
4348 rec709[2] = -0.0822535242887164f;
4349 }
4350 else {
4351 int i = (t >= 6365.0f) ? 6 :
4352 (t >= 3315.0f) ? 5 :
4353 (t >= 1902.0f) ? 4 :
4354 (t >= 1449.0f) ? 3 :
4355 (t >= 1167.0f) ? 2 :
4356 (t >= 965.0f) ? 1 :
4357 0;
4358
4359 const float *r = blackbody_table_r[i];
4360 const float *g = blackbody_table_g[i];
4361 const float *b = blackbody_table_b[i];
4362
4363 const float t_inv = 1.0f / t;
4364 rec709[0] = r[0] * t_inv + r[1] * t + r[2];
4365 rec709[1] = g[0] * t_inv + g[1] * t + g[2];
4366 rec709[2] = ((b[0] * t + b[1]) * t + b[2]) * t + b[3];
4367 }
4368}
4369
4371{
4372 float rec709[3];
4373 blackbody_temperature_to_rec709(rec709, value);
4374
4375 float rgb[3];
4377 clamp_v3(rgb, 0.0f, FLT_MAX);
4378
4379 copy_v3_v3(r_dest, rgb);
4380 r_dest[3] = 1.0f;
4381}
4382
4384 const int width,
4385 const float min,
4386 const float max)
4387{
4388 for (int i = 0; i < width; i++) {
4389 float temperature = min + (max - min) / float(width) * float(i);
4390 IMB_colormanagement_blackbody_temperature_to_rgb(&r_table[i * 4], temperature);
4391 }
4392}
4393
4405
4406static float cie_color_match[81][3] = {
4407 {0.0014f, 0.0000f, 0.0065f}, {0.0022f, 0.0001f, 0.0105f}, {0.0042f, 0.0001f, 0.0201f},
4408 {0.0076f, 0.0002f, 0.0362f}, {0.0143f, 0.0004f, 0.0679f}, {0.0232f, 0.0006f, 0.1102f},
4409 {0.0435f, 0.0012f, 0.2074f}, {0.0776f, 0.0022f, 0.3713f}, {0.1344f, 0.0040f, 0.6456f},
4410 {0.2148f, 0.0073f, 1.0391f}, {0.2839f, 0.0116f, 1.3856f}, {0.3285f, 0.0168f, 1.6230f},
4411 {0.3483f, 0.0230f, 1.7471f}, {0.3481f, 0.0298f, 1.7826f}, {0.3362f, 0.0380f, 1.7721f},
4412 {0.3187f, 0.0480f, 1.7441f}, {0.2908f, 0.0600f, 1.6692f}, {0.2511f, 0.0739f, 1.5281f},
4413 {0.1954f, 0.0910f, 1.2876f}, {0.1421f, 0.1126f, 1.0419f}, {0.0956f, 0.1390f, 0.8130f},
4414 {0.0580f, 0.1693f, 0.6162f}, {0.0320f, 0.2080f, 0.4652f}, {0.0147f, 0.2586f, 0.3533f},
4415 {0.0049f, 0.3230f, 0.2720f}, {0.0024f, 0.4073f, 0.2123f}, {0.0093f, 0.5030f, 0.1582f},
4416 {0.0291f, 0.6082f, 0.1117f}, {0.0633f, 0.7100f, 0.0782f}, {0.1096f, 0.7932f, 0.0573f},
4417 {0.1655f, 0.8620f, 0.0422f}, {0.2257f, 0.9149f, 0.0298f}, {0.2904f, 0.9540f, 0.0203f},
4418 {0.3597f, 0.9803f, 0.0134f}, {0.4334f, 0.9950f, 0.0087f}, {0.5121f, 1.0000f, 0.0057f},
4419 {0.5945f, 0.9950f, 0.0039f}, {0.6784f, 0.9786f, 0.0027f}, {0.7621f, 0.9520f, 0.0021f},
4420 {0.8425f, 0.9154f, 0.0018f}, {0.9163f, 0.8700f, 0.0017f}, {0.9786f, 0.8163f, 0.0014f},
4421 {1.0263f, 0.7570f, 0.0011f}, {1.0567f, 0.6949f, 0.0010f}, {1.0622f, 0.6310f, 0.0008f},
4422 {1.0456f, 0.5668f, 0.0006f}, {1.0026f, 0.5030f, 0.0003f}, {0.9384f, 0.4412f, 0.0002f},
4423 {0.8544f, 0.3810f, 0.0002f}, {0.7514f, 0.3210f, 0.0001f}, {0.6424f, 0.2650f, 0.0000f},
4424 {0.5419f, 0.2170f, 0.0000f}, {0.4479f, 0.1750f, 0.0000f}, {0.3608f, 0.1382f, 0.0000f},
4425 {0.2835f, 0.1070f, 0.0000f}, {0.2187f, 0.0816f, 0.0000f}, {0.1649f, 0.0610f, 0.0000f},
4426 {0.1212f, 0.0446f, 0.0000f}, {0.0874f, 0.0320f, 0.0000f}, {0.0636f, 0.0232f, 0.0000f},
4427 {0.0468f, 0.0170f, 0.0000f}, {0.0329f, 0.0119f, 0.0000f}, {0.0227f, 0.0082f, 0.0000f},
4428 {0.0158f, 0.0057f, 0.0000f}, {0.0114f, 0.0041f, 0.0000f}, {0.0081f, 0.0029f, 0.0000f},
4429 {0.0058f, 0.0021f, 0.0000f}, {0.0041f, 0.0015f, 0.0000f}, {0.0029f, 0.0010f, 0.0000f},
4430 {0.0020f, 0.0007f, 0.0000f}, {0.0014f, 0.0005f, 0.0000f}, {0.0010f, 0.0004f, 0.0000f},
4431 {0.0007f, 0.0002f, 0.0000f}, {0.0005f, 0.0002f, 0.0000f}, {0.0003f, 0.0001f, 0.0000f},
4432 {0.0002f, 0.0001f, 0.0000f}, {0.0002f, 0.0001f, 0.0000f}, {0.0001f, 0.0000f, 0.0000f},
4433 {0.0001f, 0.0000f, 0.0000f}, {0.0001f, 0.0000f, 0.0000f}, {0.0000f, 0.0000f, 0.0000f}};
4434
4435static void wavelength_to_xyz(float xyz[3], float lambda_nm)
4436{
4437 float ii = (lambda_nm - 380.0f) * (1.0f / 5.0f); /* Scaled 0..80. */
4438 int i = int(ii);
4439
4440 if (i < 0 || i >= 80) {
4441 xyz[0] = 0.0f;
4442 xyz[1] = 0.0f;
4443 xyz[2] = 0.0f;
4444 }
4445 else {
4446 ii -= float(i);
4447 const float *c = cie_color_match[i];
4448 xyz[0] = c[0] + ii * (c[3] - c[0]);
4449 xyz[1] = c[1] + ii * (c[4] - c[1]);
4450 xyz[2] = c[2] + ii * (c[5] - c[2]);
4451 }
4452}
4453
4454void IMB_colormanagement_wavelength_to_rgb(float r_dest[4], float value)
4455{
4456 float xyz[3];
4457 wavelength_to_xyz(xyz, value);
4458
4459 float rgb[3];
4461 clamp_v3(rgb, 0.0f, FLT_MAX);
4462
4463 copy_v3_v3(r_dest, rgb);
4464 r_dest[3] = 1.0f;
4465}
4466
4467void IMB_colormanagement_wavelength_to_rgb_table(float *r_table, const int width)
4468{
4469 for (int i = 0; i < width; i++) {
4470 float wavelength = 380 + 400 / float(width) * float(i);
4471 IMB_colormanagement_wavelength_to_rgb(&r_table[i * 4], wavelength);
4472 }
4473}
4474
4476
4477} // namespace blender
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
#define CMP_NODE_OUTPUT_FILE
#define CMP_NODE_CONVERT_COLOR_SPACE
#define CMP_NODE_CONVERT_TO_DISPLAY
File and directory operations.
#define FILE_MAX
#define BLI_path_join(...)
#define STRNCPY_UTF8(dst, src)
#define BLI_MUTEX_INITIALIZER
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define CLOG_INFO_NOCHECK(clg_ref, format,...)
Definition CLG_log.h:204
CLG_Level
Definition CLG_log.h:52
@ CLG_LEVEL_DEBUG
Definition CLG_log.h:62
@ CLG_LEVEL_WARN
Definition CLG_log.h:58
#define CLOG_AT_LEVEL(clg_ref, verbose_level,...)
Definition CLG_log.h:150
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
ID and Library types, which are fundamental for SDNA.
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define BCM_CONFIG_FILE
#define MAX_COLORSPACE_NAME
Function declarations for filter.cc.
Read Blender memory allocation and freeing.
#define OCIO_ROLE_COLOR_PICKING
#define OCIO_ROLE_VIDEO_REC709
#define OCIO_ROLE_TEXTURE_PAINT
#define OCIO_ROLE_DEFAULT_FLOAT
#define OCIO_ROLE_DEFAULT_SEQUENCER
#define OCIO_ROLE_DEFAULT_BYTE
#define OCIO_ROLE_ACES_INTERCHANGE
#define OCIO_ROLE_DATA
#define OCIO_ROLE_SCENE_LINEAR
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
btScalar max
#define output
static ColormanageProcessor display_processor_new(const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings, ColorManagedDisplaySpace display_space=DISPLAY_SPACE_DRAW, bool inverse=false, const char *from_colorspace=nullptr)
void apply(float *buffer, int width, int height, int channels, bool predivide) const
void apply_v3(float pixel[3]) const
ColormanageProcessor & operator=(ColormanageProcessor &&other) noexcept
void apply_byte(unsigned char *buffer, int width, int height, int channels) const
static ColormanageProcessor colorspace_processor_from_scene_linear_new(const ColorSpace &to_colorspace)
void apply_v4_predivide(float pixel[4]) const
static ColormanageProcessor colorspace_processor_new(StringRefNull from_colorspace, StringRefNull to_colorspace)
void apply_v4(float pixel[4]) const
static ColormanageProcessor colorspace_processor_to_scene_linear_new(const ColorSpace &from_colorspace)
static std::optional< ColormanageProcessor > display_processor_for_imbuf(const ImBuf *ibuf, const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings, ColorManagedDisplaySpace display_space=DISPLAY_SPACE_DRAW)
void apply_pixel(float *pixel, int channels) const
constexpr int64_t first() const
constexpr int64_t size() const
constexpr IndexRange()=default
SubIterator begin() const
Definition BLI_map.hh:768
SubIterator end() const
Definition BLI_map.hh:778
auto add_or_modify(const Key &key, const CreateValueF &create_value, const ModifyValueF &modify_value) -> decltype(create_value(nullptr))
Definition BLI_map.hh:481
ItemIterator items() const &
Definition BLI_map.hh:902
constexpr MutableSpan()=default
constexpr const char * c_str() const
constexpr StringRef drop_suffix(int64_t n) const
constexpr StringRef()
int64_t size() const
void prepend(const T &value)
bool contains(const T &value) const
void append(const T &value)
bool is_empty() const
IndexRange index_range() const
NonCopyable(const NonCopyable &other)=delete
constexpr bool startswith(StringRef prefix) const
constexpr bool is_empty() const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr bool endswith(StringRef suffix) const
constexpr const char * c_str() const
virtual void apply_rgba_predivide(float rgba[4]) const =0
virtual void apply_predivide(const PackedImage &image) const =0
virtual bool is_noop() const =0
virtual void apply_rgb(float rgb[3]) const =0
virtual void apply(const PackedImage &image) const =0
virtual void apply_rgba(float rgba[4]) const =0
virtual StringRefNull name() const =0
virtual const CPUProcessor * get_to_scene_linear_cpu_processor() const =0
virtual const CPUProcessor * get_from_scene_linear_cpu_processor() const =0
virtual bool is_data() const =0
virtual StringRefNull interop_id() const =0
virtual float3 get_default_luma_coefs() const =0
virtual int get_num_looks() const =0
static std::unique_ptr< Config > create_fallback()
Definition config.cc:16
virtual const Display * get_display_by_index(int index) const =0
virtual const ColorSpace * get_color_space_by_interop_id(StringRefNull interop_id) const =0
virtual const ColorSpace * get_color_space(StringRefNull name) const =0
static std::unique_ptr< Config > create_from_environment()
Definition config.cc:11
virtual int get_num_displays() const =0
virtual float3x3 get_xyz_to_scene_linear_matrix() const =0
virtual StringRefNull name() const =0
virtual const View * get_view_by_name(StringRefNull name) const =0
virtual const CPUProcessor * get_to_scene_linear_cpu_processor(bool use_display_emulation) const =0
virtual const CPUProcessor * get_from_scene_linear_cpu_processor(bool use_display_emulation) const =0
virtual const View * get_default_view() const =0
virtual bool is_hdr() const =0
virtual int get_num_views() const =0
virtual StringRefNull ui_name() const =0
virtual const View * get_untonemapped_view() const =0
virtual StringRefNull description() const =0
virtual const View * get_view_by_index(int index) const =0
virtual StringRefNull view() const =0
virtual StringRefNull description() const =0
virtual StringRefNull name() const =0
virtual StringRefNull ui_name() const =0
virtual StringRefNull process_space() const =0
virtual const ColorSpace * display_colorspace() const =0
virtual StringRefNull name() const =0
virtual bool support_emulation() const =0
virtual StringRefNull description() const =0
virtual Gamut gamut() const =0
virtual bool is_hdr() const =0
virtual const ScopeInfo & scope_info() const =0
virtual int max_nits() const
Definition OCIO_view.hh:84
#define DISPLAY_BUFFER_CHANNELS
#define powf(x, y)
#define roundf(x)
#define FLT_MAX
#define FLT_EPSILON
#define image(slot, qualifiers, format)
#define index(i)
MatBase< C, R > inverse(MatBase< C, R >) RET
unsigned int uchar
#define M
bNodeTree * node_tree_from_id(ID *id)
Definition node.cc:5337
ColorSceneLinear4f< eAlpha::Straight > unpremultiply_alpha(const ColorSceneLinear4f< Alpha > &color)
Definition BLI_color.hh:111
ColorSceneLinear4f< eAlpha::Premultiplied > premultiply_alpha(const ColorSceneLinear4f< Alpha > &color)
Definition BLI_color.hh:93
float3x3 rec2020_to_scene_linear
float3x3 acescg_to_scene_linear
float3x3 scene_linear_to_rec2020
float3x3 rec709_to_scene_linear
float3x3 scene_linear_to_rec709
float3x3 scene_linear_to_acescg
T clamp(const T &a, const T &min, const T &max)
float3 whitepoint_from_temp_tint(float temperature, float tint)
bool whitepoint_to_temp_tint(const float3 &white, float &temperature, float &tint)
bool is_zero(const T &a)
CartesianBasis invert(const CartesianBasis &basis)
bool is_equal(const MatBase< T, NumCol, NumRow > &a, const MatBase< T, NumCol, NumRow > &b, const T epsilon=T(0))
T determinant(const MatBase< T, Size, Size > &mat)
StringRef interop_id_drop_namespace(const StringRef interop_id)
void foreach_strip(ListBaseT< Strip > *seqbase, ForEachFunc callback, void *user_data)
Definition iterator.cc:60
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
BMesh Iterators.
void IMB_colormanagement_transform_byte_to_float(float *float_buffer, const unsigned char *byte_buffer, int width, int height, int channels, const char *from_colorspace, const char *to_colorspace)
void IMB_alpha_under_color_byte(unsigned char *rect, int x, int y, const float backcol[3])
blender::GlobalGPUState global_gpu_state
static bool colormanage_check_colorspace_settings(ColorManagedColorspaceSettings *settings, const char *what, const CLG_Level log_level)
static void curve_mapping_apply_pixel(const CurveMapping *curve_mapping, float *pixel, int channels)
bool BKE_image_format_is_byte(const ImageFormatData *imf)
void IMB_colormanagement_assign_float_colorspace(ImBuf *ibuf, const char *name)
const char * IMB_colormanagement_display_get_none_name()
static void do_processor_transform_thread(ProcessorTransformThread *handle)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void colormanage_update_matrices(const ocio::Config &config)
void IMB_colormanagement_view_items_add(EnumPropertyItem **items, int *totitem, const char *display_name)
static const char * imbuf_colorspace_name(const ImBuf *ibuf, const bool prefer_byte_buffer)
static bool get_display_emulation(const ColorManagedDisplaySettings &display_settings)
static char global_role_video_rec709[MAX_COLORSPACE_NAME]
static void colormanage_restore_ocio_env(const std::optional< std::string > &old_ocio_env)
void RNA_enum_item_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
__int64 int64_t
Definition BKE_text.h:90
CurveMapping * BKE_curvemapping_copy(const CurveMapping *cumap)
@ COLORMANAGE_VIEW_USE_WHITE_BALANCE
@ COLORMANAGE_VIEW_USE_CURVES
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
void IMB_buffer_float_from_byte(float *dest, const unsigned char *src, int width, int height, int dest_stride, int src_stride)
char * STRNCPY(char(&dst)[N], const char *src)
void IMB_colormanagement_colorspace_items_add(EnumPropertyItem **items, int *totitem)
void IMB_free_byte_pixels(ImBuf *ibuf)
void IMB_colormanagement_colorspace_from_ibuf_ftype(ColorManagedColorspaceSettings *colorspace_settings, ImBuf *ibuf)
@ LIBRARY_ASSET_EDITABLE
void IMB_colormanagement_scene_linear_to_colorspace(float *buffer, int width, int height, int channels, const ColorSpace *colorspace)
std::string BKE_blender_project_path_resolve(StringRef path, const bke::BlenderProject &project)
const ColorSpace * IMB_colormanagement_space_from_cicp(const int cicp[4], ColorManagedFileOutput output)
static void do_display_buffer_apply_thread(DisplayBufferThread *handle)
static const float imb_working_space_compare_threshold
bool IMB_colormanagement_display_is_hdr(const ColorManagedDisplaySettings *display_settings, const char *view_name)
BLI_INLINE void IMB_colormanagement_xyz_to_scene_linear(float scene_linear[3], const float xyz[3])
int IMB_colormanagement_colorspace_get_named_index(const char *name)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void IMB_colormanagement_project_read_post(Main *bmain)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void IMB_colormanagement_finish_glsl_draw()
static void colormanagement_transform_ex(uchar *byte_buffer, float *float_buffer, int width, int height, int channels, const char *from_colorspace, const char *to_colorspace, bool predivide)
static void display_buffer_apply_threaded(ImBuf *ibuf, const float *buffer, uchar *byte_buffer, float *display_buffer, uchar *display_buffer_byte, ColormanageProcessor *cm_processor)
Vector< char > IMB_colormanagement_space_to_icc_profile(const ColorSpace *colorspace)
const char * IMB_colormanagement_display_get_default_view_transform_name(const ColorManagedDisplay *display)
const ImFileType * IMB_file_type_from_ibuf(const ImBuf *ibuf)
Definition filetype.cc:338
const char * IMB_colormanagement_view_get_raw_or_default_name(const char *display_name)
bool IMB_colormanagement_space_is_srgb(const ColorSpace *colorspace)
void IMB_colormanagement_look_items_add(EnumPropertyItem **items, int *totitem, const char *view_name)
int IMB_colormanagement_look_get_named_index(const char *name)
const char * IMB_colormanagement_colorspace_get_indexed_name(int index)
static void display_buffer_apply_get_linear_buffer(DisplayBufferThread *handle, int height, float *linear_buffer, bool *is_straight_alpha)
static const int CICP_TRC_BT709
StringRefNull IMB_colormanagement_config_path_get()
static const int CICP_RANGE_LIMITED
static const ColorSpace * video_rec709_colorspace()
void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
bool IMB_colormanagement_display_is_wide_gamut(const ColorManagedDisplaySettings *display_settings, const char *view_name)
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition threads.cc:353
void IMB_premultiply_rect_float(float *rect_float, int channels, int w, int h)
Definition filter.cc:364
bool IMB_alloc_byte_pixels(ImBuf *ibuf, bool initialize_pixels=true)
const char * IMB_colormanagement_look_validate_for_view(const char *view_name, const char *look_name)
void BLI_init_srgb_conversion()
void IMB_colormanagement_working_space_items_add(EnumPropertyItem **items, int *totitem)
static const int CICP_TRC_PQ
void IMB_colormanagement_transform_v4(float pixel[4], const char *from_colorspace, const char *to_colorspace)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
float BKE_curvemap_evaluateF(const CurveMapping *cumap, const CurveMap *cuma, float value)
static Vector< ColorManagedConfigPath > colormanage_config_candidates_get(const Main *bmain)
static char global_role_scene_linear_default[MAX_COLORSPACE_NAME]
float3x3 global_scene_linear_to_xyz_default
static float cie_color_match[81][3]
static bool colormanage_check_display_settings(ColorManagedDisplaySettings *display_settings, const char *what, const ocio::Display *default_display, const CLG_Level log_level)
bool IMB_colormanagement_setup_glsl_draw_from_space(const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings, const ColorSpace *from_colorspace, float dither, bool predivide, bool do_overlay_merge, ColorManagedDisplaySpace display_space=DISPLAY_SPACE_DRAW, float opacity=1.0f)
void colormanage_imbuf_make_linear(ImBuf *ibuf, const char *from_colorspace, const ColorManagedFileOutput output)
static const int CICP_TRC_LINEAR
const char * IMB_colormanagement_get_byte_colorspace(const ImBuf *ibuf)
static const int CICP_TRC_SRGB
const char * IMB_colormanagement_display_get_indexed_name(int index)
auto BKE_blender_project_read_callback(const Main *bmain, Fn lambda)
static CurveMapping * update_glsl_curve_mapping(const ColorManagedViewSettings *view_settings)
void IMB_buffer_byte_from_float(unsigned char *dest, const float *src, int src_channels, float dither, bool predivide, int width, int height, int stride, int start_y=0)
Definition conversion.cc:50
bool IMB_colormanagement_space_to_cicp(const ColorSpace *colorspace, ColorManagedFileOutput output, bool rgb_matrix, int cicp[4])
void BKE_curvemapping_premultiply(CurveMapping *cumap, bool restore)
void BKE_curvemapping_free(CurveMapping *cumap)
static const char * colormanage_config_source_identifier(const ColorManagedConfigSource source)
void BLI_setenv(const char *env, const char *val) ATTR_NONNULL(1)
static bool colormanage_load_config(const ocio::Config &config, const bool validate_only=false)
void IMB_colormanagement_colorspace_interop_id_set(char *name, char *interop_id, int index)
void IMB_colormanagement_scene_linear_to_display_buffer(uint8_t *display_buffer, const float *linear_buffer, int width, int height, const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings)
void IMB_colormanagement_imbuf_to_byte_texture(unsigned char *out_buffer, int offset_x, int offset_y, int width, int height, const ImBuf *ibuf, bool store_premultiplied)
const char * IMB_colormanagement_working_space_get_default()
MINLINE void mul_v3_fl(float r[3], float f)
ImBuf * IMB_dupImBuf(const ImBuf *ibuf1)
static StringRef view_filter_for_look(StringRefNull view_name)
void colormanage_environment_setup_for_test(std::optional< std::string > blender_ocio_env, std::optional< std::string > ocio_env)
void colormanagement_init()
static void colormanage_set_ocio_env(const char *filepath, std::optional< std::string > &r_old_ocio_env)
static bool imb_colormanagement_working_space_set_from_matrix(Main *bmain, const char *name, const float3x3 &scene_linear_to_xyz, const bool report_missing)
void IMB_colormanagement_interop_id_items_add(EnumPropertyItem **items, int *totitem)
static char global_role_data[MAX_COLORSPACE_NAME]
void IMB_colormanagement_working_space_init_default(Main *bmain)
bool IMB_colormanagement_setup_glsl_draw(const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings, float dither, bool predivide)
void IMB_float_from_byte(ImBuf *ibuf)
bool IMB_colormanagement_space_is_scene_linear(const ColorSpace *colorspace)
static pthread_mutex_t processor_lock
bool IMB_colormanagement_space_is_scene_linear_srgb(const ColorSpace *colorspace)
void IMB_colormanagement_colorspace_to_scene_linear_v3(float pixel[3], const ColorSpace *colorspace)
int IMB_colormanagement_view_get_id_by_name(const char *name)
std::optional< std::string > BKE_appdir_folder_id(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
Definition appdir.cc:762
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
static bool is_colorspace_same_as_display(const ColorSpace *colorspace, const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings)
static char global_role_aces_interchange[MAX_COLORSPACE_NAME]
const IDTypeInfo * BKE_idtype_get_info_from_id(const ID *id)
Definition idtype.cc:232
const char * IMB_colormanagement_view_get_name_by_id(int index)
void IMB_colormanagement_scene_linear_to_color_picking_v3(float color_picking[3], const float scene_linear[3])
void BLI_mutex_lock(ThreadMutex *mutex)
Definition threads.cc:348
int IMB_colormanagement_working_space_get_named_index(const char *name)
static std::shared_ptr< const ocio::CPUProcessor > get_display_buffer_processor(const ColorManagedDisplaySettings &display_settings, const char *look, const char *view_transform, const float exposure, const float gamma, const float temperature, const float tint, const bool use_white_balance, const char *from_colorspace, const ColorManagedDisplaySpace target, const bool inverse=false)
static bool colormanage_use_look(const char *look_name, const char *view_name)
bool IMB_colormanagement_display_support_emulation(const ColorManagedDisplaySettings *display_settings, const char *view_name)
bool IMB_colormanagement_switch_config(const char *filepath)
unsigned char uchar
Definition BKE_text.h:58
bool colormanage_config_reload(Main *bmain)
static const float blackbody_table_b[7][4]
static const int CICP_PRI_REC709
void IMB_colormanagement_scene_linear_to_colorspace_v3(float pixel[3], const ColorSpace *colorspace)
BLI_INLINE void IMB_colormanagement_rec709_to_scene_linear(float scene_linear[3], const float rec709[3])
static char global_role_texture_painting[MAX_COLORSPACE_NAME]
ocio::ScopeInfo IMB_colormanagement_get_scope_info(const ColorManagedDisplaySettings *display_settings, const ColorManagedViewSettings *view_settings)
static Vector< char > imb_icc_profile_from_filepath(StringRef filepath)
static bool colormanage_check_colorspace_name(char *name, char *interop_id, const char *what, const CLG_Level log_level)
void DEG_id_tag_update(ID *id, unsigned int flags)
const char * IMB_colormanagement_role_colorspace_name_get(int role)
static const int CICP_MATRIX_REC2020_NCL
BMesh const char void * data
static const int CICP_TRC_G22
int IMB_colormanagement_colorspace_get_interop_id_index(const char *name, const char *interop_id)
static char global_role_scene_linear[MAX_COLORSPACE_NAME]
void IMB_colormanagement_transform_byte(unsigned char *buffer, int width, int height, int channels, const char *from_colorspace, const char *to_colorspace)
static void colormanage_free_config()
void IMB_colormanagement_validate_settings(const ColorManagedDisplaySettings *display_settings, ColorManagedViewSettings *view_settings)
const char * IMB_colormanagement_colorspace_get_family(const ColorSpace *colorspace)
void IMB_colormanagement_colorspace_settings_set(ColorManagedColorspaceSettings *settings, const char *name)
unsigned char uint8_t
Definition BKE_text.h:79
const ColorSpace * colormanage_colorspace_get_roled(const int role)
static void blackbody_temperature_to_rec709(float rec709[3], float t)
const char * IMB_colormanagement_space_from_filepath_rules(const char *filepath)
static const float blackbody_table_r[7][3]
ColorManagedConfigSource IMB_colormanagement_config_source_get()
bool GPU_hdr_support()
const char * IMB_colormanagement_colorspace_get_name(const ColorSpace *colorspace)
const char * IMB_colormanagement_view_get_default_name(const char *display_name)
static void imb_colormanagement_working_space_set_from_file(Main *bmain, const bool report_missing)
int IMB_colormanagement_view_max_nits(const char *display_name, const char *view_name)
void IMB_buffer_float_rgba_from_float(float *dest, const float *src, int src_channels, int width, int height)
static const float blackbody_table_g[7][3]
static const int CICP_RANGE_FULL
static VectorSet< StringRefNull > & g_all_view_names()
static bool colormanage_check_view_settings(ColorManagedDisplaySettings *display_settings, ColorManagedViewSettings *view_settings, const char *what, const CLG_Level log_level)
static const int CICP_PRI_UNSPECIFIED
void IMB_colormanagement_working_space_convert(Main *bmain, const float3x3 &current_scene_linear_to_xyz, const float3x3 &new_xyz_to_scene_linear, bool depsgraph_tag=false, bool linked_only=false, bool editable_assets_only=false)
void IMB_colormanagement_wavelength_to_rgb(float r_dest[4], float value)
static struct blender::GlobalColorPickingState global_color_picking_state
void IMB_colormanagement_blackbody_temperature_to_rgb(float r_dest[4], float value)
MatBase< float, 3, 3 > float3x3
void IMB_colormanagement_display_to_scene_linear_v3(float pixel[3], const ColorManagedDisplay *display, ColorManagedDisplaySpace display_space=DISPLAY_SPACE_DRAW)
void IMB_colormanagement_init_untonemapped_view_settings(ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings)
void IMB_colormanagement_colorspace_to_scene_linear_v4(float pixel[4], bool predivide, const ColorSpace *colorspace)
bool IMB_colormanagement_setup_glsl_draw_ctx(const bContext *C, float dither, bool predivide)
void IMB_colormanagement_display_settings_from_ctx(const bContext *C, ColorManagedViewSettings **r_view_settings, ColorManagedDisplaySettings **r_display_settings)
bool IMB_colormanagement_working_space_set_from_name(const char *name)
ImBuf * IMB_colormanagement_imbuf_for_write(ImBuf *ibuf, bool save_as_render, bool allocate_result, const ImageFormatData *image_format)
bool BKE_imtype_requires_linear_float(char imtype)
void IMB_colormanagement_assign_byte_colorspace(ImBuf *ibuf, const char *name)
static char global_role_default_byte[MAX_COLORSPACE_NAME]
bool IMB_colormanagement_set_whitepoint(const float whitepoint[3], float &temperature, float &tint)
bool IMB_alpha_affects_rgb(const ImBuf *ibuf)
Definition conversion.cc:45
const char * IMB_colormanagement_srgb_colorspace_name_get()
static const int CICP_MATRIX_BT709
SpaceImage * CTX_wm_space_image(const bContext *C)
void IMB_colormanagement_file_save_post(Main *bmain)
float3x3 IMB_colormanagement_get_scene_linear_to_xyz()
void BKE_curvemapping_evaluate_premulRGBF(const CurveMapping *cumap, float vecout[3], const float vecin[3])
static char global_role_default_sequencer[MAX_COLORSPACE_NAME]
void IMB_colormanagement_undo_read_post(Main *bmain, const MainColorspace &old_colorspace)
const ColorSpace * IMB_colormanagement_space_from_interop_id(StringRefNull interop_id)
const ColorSpace * IMB_colormangement_display_get_color_space(const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings)
static ColorManagedStartupEnv & g_startup_env()
void IMB_colormanagement_scene_linear_to_display_v3(float pixel[3], const ColorManagedDisplay *display, ColorManagedDisplaySpace display_space=DISPLAY_SPACE_DRAW)
ocio::Config ColorManagedConfig
bool IMB_colormanagement_space_is_data(const ColorSpace *colorspace)
static void imb_colormanagement_working_space_init_startup(Main *bmain)
StringRefNull IMB_colormanagement_space_get_interop_id(const ColorSpace *colorspace)
static void colormanage_clear_config()
void IMB_colormanagement_color_picking_to_scene_linear_v3(float scene_linear[3], const float color_picking[3])
void IMB_colormanagement_blackbody_temperature_to_rgb_table(float *r_table, int width, float min, float max)
void IMB_colormanagement_file_read_post(Main *bmain, Main *old_bmain, bool is_startup, bool have_editable_assets)
void IMB_byte_from_float(ImBuf *ibuf)
float3x3 IMB_colormanagement_get_xyz_to_scene_linear()
const ColorSpace * colormanage_colorspace_get_named(const char *name)
static char global_role_default_float[MAX_COLORSPACE_NAME]
MINLINE float max_ff(float a, float b)
void colormanagement_exit()
const char * IMB_colormanagement_display_get_default_name()
const ColorSpace * IMB_colormanagement_space_get_named(const char *name)
static void colormanage_config_load_candidates(const Span< ColorManagedConfigPath > candidates)
@ ID_RECALC_ALL
Definition DNA_ID.h:1190
static const int CICP_MATRIX_RGB
MINLINE void clamp_v3(float vec[3], float min, float max)
void IMB_colormanagement_colorspace_name_set(char *name, char *interop_id, const char *new_name)
static void do_display_buffer_apply_no_processor(DisplayBufferThread *handle)
ColorManagedConfig & IMB_colormanagement_get_config()
bool IMB_colormanagement_setup_glsl_draw_to_scene_linear(const char *from_colorspace_name, bool predivide)
const char * IMB_colormanagement_colorspace_get_description(const ColorSpace *colorspace)
static const int CICP_TRC_SMPTE428
const char * IMB_colormanagement_look_get_indexed_name(int index)
static CLG_LogRef LOG
Definition logging.cpp:12
void IMB_colormanagement_wavelength_to_rgb_table(float *r_table, int width)
MINLINE void rgb_uchar_to_float(float r_col[3], const unsigned char col_ub[3])
static void colormanage_display_buffer_process(ImBuf *ibuf, float *display_buffer, uchar *display_buffer_byte, const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings, const ColorManagedDisplaySpace display_space)
static float3 imb_working_space_convert(const float3x3 &m, const bool is_smaller_gamut, const float3 in_rgb)
@ COLORMANAGE_DISPLAY_EMULATION_OFF
@ COLORMANAGE_DISPLAY_EMULATION_AUTO
Scene * CTX_data_scene(const bContext *C)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
const ColorManagedDisplay * IMB_colormanagement_display_get_named(const char *name)
MINLINE void copy_v4_v4(float r[4], const float a[4])
void IMB_colormanagement_get_whitepoint(float temperature, float tint, float whitepoint[3])
static ImBuf * imbuf_ensure_editable(ImBuf *ibuf, ImBuf *colormanaged_ibuf, bool allocate_result)
static void processor_transform_apply_threaded(uchar *byte_buffer, float *float_buffer, int width, int height, int channels, ColormanageProcessor *cm_processor, bool predivide)
const char * IMB_colormanagement_working_space_get_indexed_name(int index)
static bool colormanage_role_color_space_name_get(const ocio::Config &config, char *colorspace_name, const char *role, const char *backup_role, const bool optional=false)
static std::unique_ptr< ocio::Config > & g_config()
static void colormanage_name_to_interop_id(const char *name, char *interop_id)
void IMB_alpha_under_color_float(float *rect_float, int x, int y, float backcol[3])
bool BLI_path_make_safe_filename(char *filename) ATTR_NONNULL(1)
@ DISPLAY_SPACE_COLOR_INSPECTION
VecBase< float, 3 > float3
void IMB_colormanagement_display_items_add(EnumPropertyItem **items, int *totitem)
void colormanage_imbuf_set_default_spaces(ImBuf *ibuf)
@ BLENDER_DATAFILES
bool IMB_colormanagement_space_name_is_data(const char *name)
static const int CICP_TRC_HLG
static char global_role_color_picking[MAX_COLORSPACE_NAME]
static void display_buffer_init_handle(DisplayBufferThread *handle, int start_line, int tot_line, DisplayBufferInitData *init_data)
void IMB_colormanagement_transform_float(float *buffer, int width, int height, int channels, const char *from_colorspace, const char *to_colorspace, bool predivide)
static StringRefNull colormanage_find_matching_view_name(const ocio::Display *display, StringRefNull view_name)
static Vector< ColorManagedConfigPath > & g_config_requested()
static bool colormanage_compatible_look(const ocio::Look *look, StringRef view_filter)
bool IMB_colormanagement_setup_glsl_draw_from_space_ctx(const bContext *C, const ColorSpace *from_colorspace, float dither, bool predivide)
static const int CICP_TRC_UNSPECIFIED
static void processor_transform_init_handle(ProcessorTransformThread *handle, int start_line, int tot_line, ProcessorTransformInitData *init_data)
static const int CICP_PRI_P3D65
static bool imb_colormanagement_display_processor_needed(const ImBuf *ibuf, const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings, const ColorManagedDisplaySpace display_space)
void IMB_colormanagement_colorspace_to_scene_linear(float *buffer, int width, int height, int channels, const ColorSpace *colorspace, bool predivide)
void IMB_colormanagement_imbuf_to_float_texture(float *out_buffer, int offset_x, int offset_y, int width, int height, const ImBuf *ibuf, bool store_premultiplied)
void IMB_colormanagement_check_file_config(Main *bmain)
const char * IMB_colormanagement_get_float_colorspace(const ImBuf *ibuf)
void IMB_colormanagement_pixel_to_display_space_v4(float result[4], const float pixel[4], const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings, ColorManagedDisplaySpace display_space=DISPLAY_SPACE_DRAW)
void IMB_colormanagement_copy_settings(ImBuf *ibuf_src, ImBuf *ibuf_dst)
static const int CICP_PRI_XYZD65
bool IMB_colormanagement_space_name_is_scene_linear(const char *name)
MINLINE void straight_to_premul_v4(float color[4])
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
const char * BLI_getenv(const char *env) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
BLI_INLINE void IMB_colormanagement_scene_linear_to_xyz(float xyz[3], const float scene_linear[3])
MINLINE void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
ocio::ColorSpace ColorSpace
Definition BLF_api.hh:42
static const int CICP_PRI_REC2020
const char * IMB_colormanagement_look_get_default_name()
const char * IMB_colormanagement_working_space_get()
int IMB_colormanagement_display_get_named_index(const char *name)
ocio::Display ColorManagedDisplay
static void wavelength_to_xyz(float xyz[3], float lambda_nm)
bool IMB_colormanagement_space_name_is_srgb(const char *name)
static ColorManagedConfigPath & g_config_active()
static std::string colormanage_project_config_path_resolve(const StringRef path, const bke::BlenderProject &project)
MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4])
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
ImageFormatData im_format
friend bool operator==(const ColorManagedConfigPath &a, const ColorManagedConfigPath &b)
ColorManagedConfigSource source
eColorManageDisplay_Emulation emulation
std::optional< std::string > blender_ocio
std::optional< std::string > ocio
struct CurveMapping * curve_mapping
ColormanageProcessor * cm_processor
ColormanageProcessor * cm_processor
ListBaseT< Strip > seqbase
std::shared_ptr< const ocio::CPUProcessor > cpu_processor_to
std::shared_ptr< const ocio::CPUProcessor > cpu_processor_from
CurveMapping * orig_curve_mapping
IDTypeForeachColorFunction foreach_working_space_color
struct Library * lib
Definition DNA_ID.h:421
const ColorSpace * colorspace
const ColorSpace * colorspace
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
const ColorSpace & byte_colorspace() const
Definition allocimbuf.cc:98
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
bool colorspace_is_data() const
const float * float_data() const
const ColorSpace & float_colorspace() const
float * float_data_for_write()
Definition allocimbuf.cc:79
bool(*) save(ImBuf *ibuf, const char *filepath, ImBufFlags flags)
ColorManagedColorspaceSettings linear_colorspace_settings
ColorManagedViewSettings view_settings
ColorManagedDisplaySettings display_settings
bke::library::LibraryRuntime * runtime
Definition DNA_ID.h:591
char filepath[1024]
Definition BKE_main.hh:267
MainColorspace colorspace
Definition BKE_main.hh:371
ListBaseT< Scene > scenes
Definition BKE_main.hh:386
ImageFormatData im_format
struct Editing * ed
ColorManagedViewSettings view_settings
ColorManagedDisplaySettings display_settings
ColorManagedColorspaceSettings sequencer_colorspace_settings
struct RenderData r