Implement GPU-side dither
[blender-staging.git] / source / blender / imbuf / intern / colormanagement.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2012 by Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Xavier Thomas,
24  *                 Lukas Toenne,
25  *                 Sergey Sharybin
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 /** \file blender/imbuf/intern/colormanagement.c
32  *  \ingroup imbuf
33  */
34
35 #include "IMB_colormanagement.h"
36 #include "IMB_colormanagement_intern.h"
37
38 #include <string.h>
39 #include <math.h>
40
41 #include "DNA_color_types.h"
42 #include "DNA_image_types.h"
43 #include "DNA_movieclip_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_space_types.h"
47 #include "DNA_windowmanager_types.h"
48
49 #include "IMB_filter.h"
50 #include "IMB_imbuf.h"
51 #include "IMB_imbuf_types.h"
52 #include "IMB_filetype.h"
53 #include "IMB_moviecache.h"
54
55 #include "MEM_guardedalloc.h"
56
57 #include "BLI_blenlib.h"
58 #include "BLI_fileops.h"
59 #include "BLI_math.h"
60 #include "BLI_math_color.h"
61 #include "BLI_path_util.h"
62 #include "BLI_string.h"
63 #include "BLI_threads.h"
64 #include "BLI_rect.h"
65
66 #include "BKE_colortools.h"
67 #include "BKE_context.h"
68 #include "BKE_image.h"
69 #include "BKE_utildefines.h"
70 #include "BKE_main.h"
71
72 #include "RNA_define.h"
73
74 #include <ocio_capi.h>
75
76 /*********************** Global declarations *************************/
77
78 #define DISPLAY_BUFFER_CHANNELS 4
79
80 /* ** list of all supported color spaces, displays and views */
81 static char global_role_scene_linear[MAX_COLORSPACE_NAME];
82 static char global_role_color_picking[MAX_COLORSPACE_NAME];
83 static char global_role_texture_painting[MAX_COLORSPACE_NAME];
84 static char global_role_default_byte[MAX_COLORSPACE_NAME];
85 static char global_role_default_float[MAX_COLORSPACE_NAME];
86 static char global_role_default_sequencer[MAX_COLORSPACE_NAME];
87
88 static ListBase global_colorspaces = {NULL, NULL};
89 static ListBase global_displays = {NULL, NULL};
90 static ListBase global_views = {NULL, NULL};
91 static ListBase global_looks = {NULL, NULL};
92
93 static int global_tot_colorspace = 0;
94 static int global_tot_display = 0;
95 static int global_tot_view = 0;
96 static int global_tot_looks = 0;
97
98 /* lock used by pre-cached processors getters, so processor wouldn't
99  * be created several times
100  * LOCK_COLORMANAGE can not be used since this mutex could be needed to
101  * be locked before pre-cached processor are creating
102  */
103 static pthread_mutex_t processor_lock = BLI_MUTEX_INITIALIZER;
104
105 typedef struct ColormanageProcessor {
106         OCIO_ConstProcessorRcPtr *processor;
107         CurveMapping *curve_mapping;
108         bool is_data_result;
109 } ColormanageProcessor;
110
111 static struct global_glsl_state {
112         /* Actual processor used for GLSL baked LUTs. */
113         OCIO_ConstProcessorRcPtr *processor;
114
115         /* Settings of processor for comparison. */
116         char look[MAX_COLORSPACE_NAME];
117         char view[MAX_COLORSPACE_NAME];
118         char display[MAX_COLORSPACE_NAME];
119         char input[MAX_COLORSPACE_NAME];
120         float exposure, gamma;
121
122         CurveMapping *curve_mapping, *orig_curve_mapping;
123         bool use_curve_mapping;
124         int curve_mapping_timestamp;
125         OCIO_CurveMappingSettings curve_mapping_settings;
126
127         /* Container for GLSL state needed for OCIO module. */
128         struct OCIO_GLSLDrawState *ocio_glsl_state;
129         struct OCIO_GLSLDrawState *transform_ocio_glsl_state;
130 } global_glsl_state;
131
132 /*********************** Color managed cache *************************/
133
134 /* Cache Implementation Notes
135  * ==========================
136  *
137  * All color management cache stuff is stored in two properties of
138  * image buffers:
139  *
140  *   1. display_buffer_flags
141  *
142  *      This is a bit field which used to mark calculated transformations
143  *      for particular image buffer. Index inside of this array means index
144  *      of a color managed display. Element with given index matches view
145  *      transformations applied for a given display. So if bit B of array
146  *      element B is set to 1, this means display buffer with display index
147  *      of A and view transform of B was ever calculated for this imbuf.
148  *
149  *      In contrast with indices in global lists of displays and views this
150  *      indices are 0-based, not 1-based. This is needed to save some bytes
151  *      of memory.
152  *
153  *   2. colormanage_cache
154  *
155  *      This is a pointer to a structure which holds all data which is
156  *      needed for color management cache to work.
157  *
158  *      It contains two parts:
159  *        - data
160  *        - moviecache
161  *
162  *      Data field is used to store additional information about cached
163  *      buffers which affects on whether cached buffer could be used.
164  *      This data can't go to cache key because changes in this data
165  *      shouldn't lead extra buffers adding to cache, it shall
166  *      invalidate cached images.
167  *
168  *      Currently such a data contains only exposure and gamma, but
169  *      would likely extended further.
170  *
171  *      data field is not null only for elements of cache, not used for
172  *      original image buffers.
173  *
174  *      Color management cache is using generic MovieCache implementation
175  *      to make it easier to deal with memory limitation.
176  *
177  *      Currently color management is using the same memory limitation
178  *      pool as sequencer and clip editor are using which means color
179  *      managed buffers would be removed from the cache as soon as new
180  *      frames are loading for the movie clip and there's no space in
181  *      cache.
182  *
183  *      Every image buffer has got own movie cache instance, which
184  *      means keys for color managed buffers could be really simple
185  *      and look up in this cache would be fast and independent from
186  *      overall amount of color managed images.
187  */
188
189 /* NOTE: ColormanageCacheViewSettings and ColormanageCacheDisplaySettings are
190  *       quite the same as ColorManagedViewSettings and ColorManageDisplaySettings
191  *       but they holds indexes of all transformations and color spaces, not
192  *       their names.
193  *
194  *       This helps avoid extra colorspace / display / view lookup without
195  *       requiring to pass all variables which affects on display buffer
196  *       to color management cache system and keeps calls small and nice.
197  */
198 typedef struct ColormanageCacheViewSettings {
199         int flag;
200         int look;
201         int view;
202         float exposure;
203         float gamma;
204         float dither;
205         CurveMapping *curve_mapping;
206 } ColormanageCacheViewSettings;
207
208 typedef struct ColormanageCacheDisplaySettings {
209         int display;
210 } ColormanageCacheDisplaySettings;
211
212 typedef struct ColormanageCacheKey {
213         int view;            /* view transformation used for display buffer */
214         int display;         /* display device name */
215 } ColormanageCacheKey;
216
217 typedef struct ColormnaageCacheData {
218         int flag;        /* view flags of cached buffer */
219         int look;        /* Additional artistics transform */
220         float exposure;  /* exposure value cached buffer is calculated with */
221         float gamma;     /* gamma value cached buffer is calculated with */
222         float dither;    /* dither value cached buffer is calculated with */
223         CurveMapping *curve_mapping;  /* curve mapping used for cached buffer */
224         int curve_mapping_timestamp;  /* time stamp of curve mapping used for cached buffer */
225 } ColormnaageCacheData;
226
227 typedef struct ColormanageCache {
228         struct MovieCache *moviecache;
229
230         ColormnaageCacheData *data;
231 } ColormanageCache;
232
233 static struct MovieCache *colormanage_moviecache_get(const ImBuf *ibuf)
234 {
235         if (!ibuf->colormanage_cache)
236                 return NULL;
237
238         return ibuf->colormanage_cache->moviecache;
239 }
240
241 static ColormnaageCacheData *colormanage_cachedata_get(const ImBuf *ibuf)
242 {
243         if (!ibuf->colormanage_cache)
244                 return NULL;
245
246         return ibuf->colormanage_cache->data;
247 }
248
249 static unsigned int colormanage_hashhash(const void *key_v)
250 {
251         ColormanageCacheKey *key = (ColormanageCacheKey *)key_v;
252
253         unsigned int rval = (key->display << 16) | (key->view % 0xffff);
254
255         return rval;
256 }
257
258 static int colormanage_hashcmp(const void *av, const void *bv)
259 {
260         const ColormanageCacheKey *a = (ColormanageCacheKey *) av;
261         const ColormanageCacheKey *b = (ColormanageCacheKey *) bv;
262
263         if (a->view < b->view)
264                 return -1;
265         else if (a->view > b->view)
266                 return 1;
267
268         if (a->display < b->display)
269                 return -1;
270         else if (a->display > b->display)
271                 return 1;
272
273         return 0;
274 }
275
276 static struct MovieCache *colormanage_moviecache_ensure(ImBuf *ibuf)
277 {
278         if (!ibuf->colormanage_cache)
279                 ibuf->colormanage_cache = MEM_callocN(sizeof(ColormanageCache), "imbuf colormanage cache");
280
281         if (!ibuf->colormanage_cache->moviecache) {
282                 struct MovieCache *moviecache;
283
284                 moviecache = IMB_moviecache_create("colormanage cache", sizeof(ColormanageCacheKey),
285                                                    colormanage_hashhash, colormanage_hashcmp);
286
287                 ibuf->colormanage_cache->moviecache = moviecache;
288         }
289
290         return ibuf->colormanage_cache->moviecache;
291 }
292
293 static void colormanage_cachedata_set(ImBuf *ibuf, ColormnaageCacheData *data)
294 {
295         if (!ibuf->colormanage_cache)
296                 ibuf->colormanage_cache = MEM_callocN(sizeof(ColormanageCache), "imbuf colormanage cache");
297
298         ibuf->colormanage_cache->data = data;
299 }
300
301 static void colormanage_view_settings_to_cache(ImBuf *ibuf,
302                                                ColormanageCacheViewSettings *cache_view_settings,
303                                                const ColorManagedViewSettings *view_settings)
304 {
305         int look = IMB_colormanagement_look_get_named_index(view_settings->look);
306         int view = IMB_colormanagement_view_get_named_index(view_settings->view_transform);
307
308         cache_view_settings->look = look;
309         cache_view_settings->view = view;
310         cache_view_settings->exposure = view_settings->exposure;
311         cache_view_settings->gamma = view_settings->gamma;
312         cache_view_settings->dither = ibuf->dither;
313         cache_view_settings->flag = view_settings->flag;
314         cache_view_settings->curve_mapping = view_settings->curve_mapping;
315 }
316
317 static void colormanage_display_settings_to_cache(ColormanageCacheDisplaySettings *cache_display_settings,
318                                                   const ColorManagedDisplaySettings *display_settings)
319 {
320         int display = IMB_colormanagement_display_get_named_index(display_settings->display_device);
321
322         cache_display_settings->display = display;
323 }
324
325 static void colormanage_settings_to_key(ColormanageCacheKey *key,
326                                         const ColormanageCacheViewSettings *view_settings,
327                                         const ColormanageCacheDisplaySettings *display_settings)
328 {
329         key->view = view_settings->view;
330         key->display = display_settings->display;
331 }
332
333 static ImBuf *colormanage_cache_get_ibuf(ImBuf *ibuf, ColormanageCacheKey *key, void **cache_handle)
334 {
335         ImBuf *cache_ibuf;
336         struct MovieCache *moviecache = colormanage_moviecache_get(ibuf);
337
338         if (!moviecache) {
339                 /* if there's no moviecache it means no color management was applied on given image buffer before */
340
341                 return NULL;
342         }
343
344         *cache_handle = NULL;
345
346         cache_ibuf = IMB_moviecache_get(moviecache, key);
347
348         *cache_handle = cache_ibuf;
349
350         return cache_ibuf;
351 }
352
353 static unsigned char *colormanage_cache_get(ImBuf *ibuf, const ColormanageCacheViewSettings *view_settings,
354                                             const ColormanageCacheDisplaySettings *display_settings,
355                                             void **cache_handle)
356 {
357         ColormanageCacheKey key;
358         ImBuf *cache_ibuf;
359         int view_flag = 1 << (view_settings->view - 1);
360         CurveMapping *curve_mapping = view_settings->curve_mapping;
361         int curve_mapping_timestamp = curve_mapping ? curve_mapping->changed_timestamp : 0;
362
363         colormanage_settings_to_key(&key, view_settings, display_settings);
364
365         /* check whether image was marked as dirty for requested transform */
366         if ((ibuf->display_buffer_flags[display_settings->display - 1] & view_flag) == 0) {
367                 return NULL;
368         }
369
370         cache_ibuf = colormanage_cache_get_ibuf(ibuf, &key, cache_handle);
371
372         if (cache_ibuf) {
373                 ColormnaageCacheData *cache_data;
374
375                 BLI_assert(cache_ibuf->x == ibuf->x &&
376                            cache_ibuf->y == ibuf->y);
377
378                 /* only buffers with different color space conversions are being stored
379                  * in cache separately. buffer which were used only different exposure/gamma
380                  * are re-suing the same cached buffer
381                  *
382                  * check here which exposure/gamma/curve was used for cached buffer and if they're
383                  * different from requested buffer should be re-generated
384                  */
385                 cache_data = colormanage_cachedata_get(cache_ibuf);
386
387                 if (cache_data->look != view_settings->look ||
388                     cache_data->exposure != view_settings->exposure ||
389                     cache_data->gamma != view_settings->gamma ||
390                     cache_data->dither != view_settings->dither ||
391                     cache_data->flag != view_settings->flag ||
392                     cache_data->curve_mapping != curve_mapping ||
393                     cache_data->curve_mapping_timestamp != curve_mapping_timestamp)
394                 {
395                         *cache_handle = NULL;
396
397                         IMB_freeImBuf(cache_ibuf);
398
399                         return NULL;
400                 }
401
402                 return (unsigned char *) cache_ibuf->rect;
403         }
404
405         return NULL;
406 }
407
408 static void colormanage_cache_put(ImBuf *ibuf, const ColormanageCacheViewSettings *view_settings,
409                                   const ColormanageCacheDisplaySettings *display_settings,
410                                   unsigned char *display_buffer, void **cache_handle)
411 {
412         ColormanageCacheKey key;
413         ImBuf *cache_ibuf;
414         ColormnaageCacheData *cache_data;
415         int view_flag = 1 << (view_settings->view - 1);
416         struct MovieCache *moviecache = colormanage_moviecache_ensure(ibuf);
417         CurveMapping *curve_mapping = view_settings->curve_mapping;
418         int curve_mapping_timestamp = curve_mapping ? curve_mapping->changed_timestamp : 0;
419
420         colormanage_settings_to_key(&key, view_settings, display_settings);
421
422         /* mark display buffer as valid */
423         ibuf->display_buffer_flags[display_settings->display - 1] |= view_flag;
424
425         /* buffer itself */
426         cache_ibuf = IMB_allocImBuf(ibuf->x, ibuf->y, ibuf->planes, 0);
427         cache_ibuf->rect = (unsigned int *) display_buffer;
428
429         cache_ibuf->mall |= IB_rect;
430         cache_ibuf->flags |= IB_rect;
431
432         /* store data which is needed to check whether cached buffer could be used for color managed display settings */
433         cache_data = MEM_callocN(sizeof(ColormnaageCacheData), "color manage cache imbuf data");
434         cache_data->look = view_settings->look;
435         cache_data->exposure = view_settings->exposure;
436         cache_data->gamma = view_settings->gamma;
437         cache_data->dither = view_settings->dither;
438         cache_data->flag = view_settings->flag;
439         cache_data->curve_mapping = curve_mapping;
440         cache_data->curve_mapping_timestamp = curve_mapping_timestamp;
441
442         colormanage_cachedata_set(cache_ibuf, cache_data);
443
444         *cache_handle = cache_ibuf;
445
446         IMB_moviecache_put(moviecache, &key, cache_ibuf);
447 }
448
449 static void colormanage_cache_handle_release(void *cache_handle)
450 {
451         ImBuf *cache_ibuf = cache_handle;
452
453         IMB_freeImBuf(cache_ibuf);
454 }
455
456 /*********************** Initialization / De-initialization *************************/
457
458 static void colormanage_role_color_space_name_get(OCIO_ConstConfigRcPtr *config, char *colorspace_name, const char *role, const char *backup_role)
459 {
460         OCIO_ConstColorSpaceRcPtr *ociocs;
461
462         ociocs = OCIO_configGetColorSpace(config, role);
463
464         if (!ociocs && backup_role)
465                 ociocs = OCIO_configGetColorSpace(config, backup_role);
466
467         if (ociocs) {
468                 const char *name = OCIO_colorSpaceGetName(ociocs);
469
470                 /* assume function was called with buffer properly allocated to MAX_COLORSPACE_NAME chars */
471                 BLI_strncpy(colorspace_name, name, MAX_COLORSPACE_NAME);
472                 OCIO_colorSpaceRelease(ociocs);
473         }
474         else {
475                 printf("Color management: Error could not find role %s role.\n", role);
476         }
477 }
478
479 static void colormanage_load_config(OCIO_ConstConfigRcPtr *config)
480 {
481         int tot_colorspace, tot_display, tot_display_view, tot_looks;
482         int index, viewindex, viewindex2;
483         const char *name;
484
485         /* get roles */
486         colormanage_role_color_space_name_get(config, global_role_scene_linear, OCIO_ROLE_SCENE_LINEAR, NULL);
487         colormanage_role_color_space_name_get(config, global_role_color_picking, OCIO_ROLE_COLOR_PICKING, NULL);
488         colormanage_role_color_space_name_get(config, global_role_texture_painting, OCIO_ROLE_TEXTURE_PAINT, NULL);
489         colormanage_role_color_space_name_get(config, global_role_default_sequencer, OCIO_ROLE_DEFAULT_SEQUENCER, OCIO_ROLE_SCENE_LINEAR);
490         colormanage_role_color_space_name_get(config, global_role_default_byte, OCIO_ROLE_DEFAULT_BYTE, OCIO_ROLE_TEXTURE_PAINT);
491         colormanage_role_color_space_name_get(config, global_role_default_float, OCIO_ROLE_DEFAULT_FLOAT, OCIO_ROLE_SCENE_LINEAR);
492
493         /* load colorspaces */
494         tot_colorspace = OCIO_configGetNumColorSpaces(config);
495         for (index = 0 ; index < tot_colorspace; index++) {
496                 OCIO_ConstColorSpaceRcPtr *ocio_colorspace;
497                 const char *description;
498                 bool is_invertible, is_data;
499
500                 name = OCIO_configGetColorSpaceNameByIndex(config, index);
501
502                 ocio_colorspace = OCIO_configGetColorSpace(config, name);
503                 description = OCIO_colorSpaceGetDescription(ocio_colorspace);
504                 is_invertible = OCIO_colorSpaceIsInvertible(ocio_colorspace);
505                 is_data = OCIO_colorSpaceIsData(ocio_colorspace);
506
507                 colormanage_colorspace_add(name, description, is_invertible, is_data);
508
509                 OCIO_colorSpaceRelease(ocio_colorspace);
510         }
511
512         /* load displays */
513         viewindex2 = 0;
514         tot_display = OCIO_configGetNumDisplays(config);
515
516         for (index = 0 ; index < tot_display; index++) {
517                 const char *displayname;
518                 ColorManagedDisplay *display;
519
520                 displayname = OCIO_configGetDisplay(config, index);
521
522                 display = colormanage_display_add(displayname);
523
524                 /* load views */
525                 tot_display_view = OCIO_configGetNumViews(config, displayname);
526                 for (viewindex = 0 ; viewindex < tot_display_view; viewindex++, viewindex2++) {
527                         const char *viewname;
528                         ColorManagedView *view;
529                         LinkData *display_view;
530
531                         viewname = OCIO_configGetView(config, displayname, viewindex);
532
533                         /* first check if view transform with given name was already loaded */
534                         view = colormanage_view_get_named(viewname);
535
536                         if (!view) {
537                                 view = colormanage_view_add(viewname);
538                         }
539
540                         display_view = BLI_genericNodeN(view);
541
542                         BLI_addtail(&display->views, display_view);
543                 }
544         }
545
546         global_tot_display = tot_display;
547
548         /* load looks */
549         tot_looks = OCIO_configGetNumLooks(config);
550         colormanage_look_add("None", "", true);
551         for (index = 0; index < tot_looks; index++) {
552                 OCIO_ConstLookRcPtr *ocio_look;
553                 const char *process_space;
554
555                 name = OCIO_configGetLookNameByIndex(config, index);
556                 ocio_look = OCIO_configGetLook(config, name);
557                 process_space = OCIO_lookGetProcessSpace(ocio_look);
558                 OCIO_lookRelease(ocio_look);
559
560                 colormanage_look_add(name, process_space, false);
561         }
562 }
563
564 static void colormanage_free_config(void)
565 {
566         ColorSpace *colorspace;
567         ColorManagedDisplay *display;
568
569         /* free color spaces */
570         colorspace = global_colorspaces.first;
571         while (colorspace) {
572                 ColorSpace *colorspace_next = colorspace->next;
573
574                 /* free precomputer processors */
575                 if (colorspace->to_scene_linear)
576                         OCIO_processorRelease((OCIO_ConstProcessorRcPtr *) colorspace->to_scene_linear);
577
578                 if (colorspace->from_scene_linear)
579                         OCIO_processorRelease((OCIO_ConstProcessorRcPtr *) colorspace->from_scene_linear);
580
581                 /* free color space itself */
582                 MEM_freeN(colorspace);
583
584                 colorspace = colorspace_next;
585         }
586
587         /* free displays */
588         display = global_displays.first;
589         while (display) {
590                 ColorManagedDisplay *display_next = display->next;
591
592                 /* free precomputer processors */
593                 if (display->to_scene_linear)
594                         OCIO_processorRelease((OCIO_ConstProcessorRcPtr *) display->to_scene_linear);
595
596                 if (display->from_scene_linear)
597                         OCIO_processorRelease((OCIO_ConstProcessorRcPtr *) display->from_scene_linear);
598
599                 /* free list of views */
600                 BLI_freelistN(&display->views);
601
602                 MEM_freeN(display);
603                 display = display_next;
604         }
605
606         /* free views */
607         BLI_freelistN(&global_views);
608
609         /* free looks */
610         BLI_freelistN(&global_looks);
611
612         OCIO_exit();
613 }
614
615 void colormanagement_init(void)
616 {
617         const char *ocio_env;
618         const char *configdir;
619         char configfile[FILE_MAX];
620         OCIO_ConstConfigRcPtr *config = NULL;
621
622         OCIO_init();
623
624         ocio_env = getenv("OCIO");
625
626         if (ocio_env && ocio_env[0] != '\0')
627                 config = OCIO_configCreateFromEnv();
628
629         if (config == NULL) {
630                 configdir = BLI_get_folder(BLENDER_DATAFILES, "colormanagement");
631
632                 if (configdir) {
633                         BLI_join_dirfile(configfile, sizeof(configfile), configdir, BCM_CONFIG_FILE);
634
635 #ifdef WIN32
636                         {
637                                 /* quite a hack to support loading configuration from path with non-acii symbols */
638
639                                 char short_name[256];
640                                 BLI_get_short_name(short_name, configfile);
641                                 config = OCIO_configCreateFromFile(short_name);
642                         }
643 #else
644                         config = OCIO_configCreateFromFile(configfile);
645 #endif
646                 }
647         }
648
649         if (config == NULL) {
650                 printf("Color management: using fallback mode for management\n");
651
652                 config = OCIO_configCreateFallback();
653         }
654
655         if (config) {
656                 OCIO_setCurrentConfig(config);
657
658                 colormanage_load_config(config);
659
660                 OCIO_configRelease(config);
661         }
662
663         BLI_init_srgb_conversion();
664 }
665
666 void colormanagement_exit(void)
667 {
668         if (global_glsl_state.processor)
669                 OCIO_processorRelease(global_glsl_state.processor);
670
671         if (global_glsl_state.curve_mapping)
672                 curvemapping_free(global_glsl_state.curve_mapping);
673
674         if (global_glsl_state.curve_mapping_settings.lut)
675                 MEM_freeN(global_glsl_state.curve_mapping_settings.lut);
676
677         if (global_glsl_state.ocio_glsl_state)
678                 OCIO_freeOGLState(global_glsl_state.ocio_glsl_state);
679
680         if (global_glsl_state.transform_ocio_glsl_state)
681                 OCIO_freeOGLState(global_glsl_state.transform_ocio_glsl_state);
682
683         colormanage_free_config();
684 }
685
686 /*********************** Internal functions *************************/
687
688 void colormanage_cache_free(ImBuf *ibuf)
689 {
690         if (ibuf->display_buffer_flags) {
691                 MEM_freeN(ibuf->display_buffer_flags);
692
693                 ibuf->display_buffer_flags = NULL;
694         }
695
696         if (ibuf->colormanage_cache) {
697                 ColormnaageCacheData *cache_data = colormanage_cachedata_get(ibuf);
698                 struct MovieCache *moviecache = colormanage_moviecache_get(ibuf);
699
700                 if (cache_data) {
701                         MEM_freeN(cache_data);
702                 }
703
704                 if (moviecache) {
705                         IMB_moviecache_free(moviecache);
706                 }
707
708                 MEM_freeN(ibuf->colormanage_cache);
709
710                 ibuf->colormanage_cache = NULL;
711         }
712 }
713
714 void IMB_colormanagement_display_settings_from_ctx(const bContext *C,
715                                                    ColorManagedViewSettings **view_settings_r,
716                                                    ColorManagedDisplaySettings **display_settings_r)
717 {
718         Scene *scene = CTX_data_scene(C);
719         SpaceImage *sima = CTX_wm_space_image(C);
720
721         *view_settings_r = &scene->view_settings;
722         *display_settings_r = &scene->display_settings;
723
724         if (sima && sima->image) {
725                 if ((sima->image->flag & IMA_VIEW_AS_RENDER) == 0)
726                         *view_settings_r = NULL;
727         }
728 }
729
730 static const char *display_transform_get_colorspace_name(const ColorManagedViewSettings *view_settings,
731                                                          const ColorManagedDisplaySettings *display_settings)
732 {
733         OCIO_ConstConfigRcPtr *config = OCIO_getCurrentConfig();
734
735         const char *display = display_settings->display_device;
736         const char *view = view_settings->view_transform;
737         const char *colorspace_name;
738
739         colorspace_name = OCIO_configGetDisplayColorSpaceName(config, display, view);
740
741         OCIO_configRelease(config);
742
743         return colorspace_name;
744 }
745
746 static ColorSpace *display_transform_get_colorspace(const ColorManagedViewSettings *view_settings,
747                                                     const ColorManagedDisplaySettings *display_settings)
748 {
749         const char *colorspace_name = display_transform_get_colorspace_name(view_settings, display_settings);
750
751         if (colorspace_name)
752                 return colormanage_colorspace_get_named(colorspace_name);
753
754         return NULL;
755 }
756
757 static OCIO_ConstProcessorRcPtr *create_display_buffer_processor(const char *look,
758                                                                  const char *view_transform,
759                                                                  const char *display,
760                                                                  float exposure, float gamma,
761                                                                  const char *from_colorspace)
762 {
763         OCIO_ConstConfigRcPtr *config = OCIO_getCurrentConfig();
764         OCIO_DisplayTransformRcPtr *dt;
765         OCIO_ConstProcessorRcPtr *processor;
766         ColorManagedLook *look_descr = colormanage_look_get_named(look);
767
768         dt = OCIO_createDisplayTransform();
769
770         OCIO_displayTransformSetInputColorSpaceName(dt, from_colorspace);
771         OCIO_displayTransformSetView(dt, view_transform);
772         OCIO_displayTransformSetDisplay(dt, display);
773
774         if (look_descr->is_noop == false) {
775                 OCIO_displayTransformSetLooksOverrideEnabled(dt, true);
776                 OCIO_displayTransformSetLooksOverride(dt, look);
777         }
778
779         /* fstop exposure control */
780         if (exposure != 0.0f) {
781                 OCIO_MatrixTransformRcPtr *mt;
782                 float gain = powf(2.0f, exposure);
783                 const float scale4f[] = {gain, gain, gain, 1.0f};
784                 float m44[16], offset4[4];
785
786                 OCIO_matrixTransformScale(m44, offset4, scale4f);
787                 mt = OCIO_createMatrixTransform();
788                 OCIO_matrixTransformSetValue(mt, m44, offset4);
789                 OCIO_displayTransformSetLinearCC(dt, (OCIO_ConstTransformRcPtr *) mt);
790
791                 OCIO_matrixTransformRelease(mt);
792         }
793
794         /* post-display gamma transform */
795         if (gamma != 1.0f) {
796                 OCIO_ExponentTransformRcPtr *et;
797                 float exponent = 1.0f / MAX2(FLT_EPSILON, gamma);
798                 const float exponent4f[] = {exponent, exponent, exponent, exponent};
799
800                 et = OCIO_createExponentTransform();
801                 OCIO_exponentTransformSetValue(et, exponent4f);
802                 OCIO_displayTransformSetDisplayCC(dt, (OCIO_ConstTransformRcPtr *) et);
803
804                 OCIO_exponentTransformRelease(et);
805         }
806
807         processor = OCIO_configGetProcessor(config, (OCIO_ConstTransformRcPtr *) dt);
808
809         OCIO_displayTransformRelease(dt);
810         OCIO_configRelease(config);
811
812         return processor;
813 }
814
815 static OCIO_ConstProcessorRcPtr *create_colorspace_transform_processor(const char *from_colorspace,
816                                                                        const char *to_colorspace)
817 {
818         OCIO_ConstConfigRcPtr *config = OCIO_getCurrentConfig();
819         OCIO_ConstProcessorRcPtr *processor;
820
821         processor = OCIO_configGetProcessorWithNames(config, from_colorspace, to_colorspace);
822
823         OCIO_configRelease(config);
824
825         return processor;
826 }
827
828 static OCIO_ConstProcessorRcPtr *colorspace_to_scene_linear_processor(ColorSpace *colorspace)
829 {
830         if (colorspace->to_scene_linear == NULL) {
831                 BLI_mutex_lock(&processor_lock);
832
833                 if (colorspace->to_scene_linear == NULL) {
834                         OCIO_ConstProcessorRcPtr *to_scene_linear;
835                         to_scene_linear = create_colorspace_transform_processor(colorspace->name, global_role_scene_linear);
836                         colorspace->to_scene_linear = (struct OCIO_ConstProcessorRcPtr *) to_scene_linear;
837                 }
838
839                 BLI_mutex_unlock(&processor_lock);
840         }
841
842         return (OCIO_ConstProcessorRcPtr *) colorspace->to_scene_linear;
843 }
844
845 static OCIO_ConstProcessorRcPtr *colorspace_from_scene_linear_processor(ColorSpace *colorspace)
846 {
847         if (colorspace->from_scene_linear == NULL) {
848                 BLI_mutex_lock(&processor_lock);
849
850                 if (colorspace->from_scene_linear == NULL) {
851                         OCIO_ConstProcessorRcPtr *from_scene_linear;
852                         from_scene_linear = create_colorspace_transform_processor(global_role_scene_linear, colorspace->name);
853                         colorspace->from_scene_linear = (struct OCIO_ConstProcessorRcPtr *) from_scene_linear;
854                 }
855
856                 BLI_mutex_unlock(&processor_lock);
857         }
858
859         return (OCIO_ConstProcessorRcPtr *) colorspace->from_scene_linear;
860 }
861
862 static OCIO_ConstProcessorRcPtr *display_from_scene_linear_processor(ColorManagedDisplay *display)
863 {
864         if (display->from_scene_linear == NULL) {
865                 BLI_mutex_lock(&processor_lock);
866
867                 if (display->from_scene_linear == NULL) {
868                         const char *view_name = colormanage_view_get_default_name(display);
869                         OCIO_ConstConfigRcPtr *config = OCIO_getCurrentConfig();
870                         OCIO_ConstProcessorRcPtr *processor = NULL;
871
872                         if (view_name && config) {
873                                 const char *view_colorspace = OCIO_configGetDisplayColorSpaceName(config, display->name, view_name);
874                                 processor = OCIO_configGetProcessorWithNames(config, global_role_scene_linear, view_colorspace);
875
876                                 OCIO_configRelease(config);
877                         }
878
879                         display->from_scene_linear = (struct OCIO_ConstProcessorRcPtr *) processor;
880                 }
881
882                 BLI_mutex_unlock(&processor_lock);
883         }
884
885         return (OCIO_ConstProcessorRcPtr *) display->from_scene_linear;
886 }
887
888 static OCIO_ConstProcessorRcPtr *display_to_scene_linear_processor(ColorManagedDisplay *display)
889 {
890         if (display->to_scene_linear == NULL) {
891                 BLI_mutex_lock(&processor_lock);
892
893                 if (display->to_scene_linear == NULL) {
894                         const char *view_name = colormanage_view_get_default_name(display);
895                         OCIO_ConstConfigRcPtr *config = OCIO_getCurrentConfig();
896                         OCIO_ConstProcessorRcPtr *processor = NULL;
897
898                         if (view_name && config) {
899                                 const char *view_colorspace = OCIO_configGetDisplayColorSpaceName(config, display->name, view_name);
900                                 processor = OCIO_configGetProcessorWithNames(config, view_colorspace, global_role_scene_linear);
901
902                                 OCIO_configRelease(config);
903                         }
904
905                         display->to_scene_linear = (struct OCIO_ConstProcessorRcPtr *) processor;
906                 }
907
908                 BLI_mutex_unlock(&processor_lock);
909         }
910
911         return (OCIO_ConstProcessorRcPtr *) display->to_scene_linear;
912 }
913
914 static void init_default_view_settings(const ColorManagedDisplaySettings *display_settings,
915                                        ColorManagedViewSettings *view_settings)
916 {
917         ColorManagedDisplay *display;
918         ColorManagedView *default_view = NULL;
919
920         display = colormanage_display_get_named(display_settings->display_device);
921
922         if (display)
923                 default_view = colormanage_view_get_default(display);
924
925         if (default_view)
926                 BLI_strncpy(view_settings->view_transform, default_view->name, sizeof(view_settings->view_transform));
927         else
928                 view_settings->view_transform[0] = '\0';
929
930         BLI_strncpy(view_settings->look, "None", sizeof(view_settings->look));
931         view_settings->flag = 0;
932         view_settings->gamma = 1.0f;
933         view_settings->exposure = 0.0f;
934         view_settings->curve_mapping = NULL;
935 }
936
937 static void curve_mapping_apply_pixel(CurveMapping *curve_mapping, float *pixel, int channels)
938 {
939         if (channels == 1) {
940                 pixel[0] = curvemap_evaluateF(curve_mapping->cm, pixel[0]);
941         }
942         else if (channels == 2) {
943                 pixel[0] = curvemap_evaluateF(curve_mapping->cm, pixel[0]);
944                 pixel[1] = curvemap_evaluateF(curve_mapping->cm, pixel[1]);
945         }
946         else {
947                 curvemapping_evaluate_premulRGBF(curve_mapping, pixel, pixel);
948         }
949 }
950
951 void colorspace_set_default_role(char *colorspace, int size, int role)
952 {
953         if (colorspace && colorspace[0] == '\0') {
954                 const char *role_colorspace;
955
956                 role_colorspace = IMB_colormanagement_role_colorspace_name_get(role);
957
958                 BLI_strncpy(colorspace, role_colorspace, size);
959         }
960 }
961
962 void colormanage_imbuf_set_default_spaces(ImBuf *ibuf)
963 {
964         ibuf->rect_colorspace = colormanage_colorspace_get_named(global_role_default_byte);
965 }
966
967 void colormanage_imbuf_make_linear(ImBuf *ibuf, const char *from_colorspace)
968 {
969         ColorSpace *colorspace = colormanage_colorspace_get_named(from_colorspace);
970
971         if (colorspace && colorspace->is_data) {
972                 ibuf->colormanage_flag |= IMB_COLORMANAGE_IS_DATA;
973                 return;
974         }
975
976         if (ibuf->rect_float) {
977                 const char *to_colorspace = global_role_scene_linear;
978
979                 if (ibuf->rect)
980                         imb_freerectImBuf(ibuf);
981
982                 IMB_colormanagement_transform(ibuf->rect_float, ibuf->x, ibuf->y, ibuf->channels,
983                                               from_colorspace, to_colorspace, true);
984         }
985 }
986
987 /*********************** Generic functions *************************/
988
989 static void colormanage_check_display_settings(ColorManagedDisplaySettings *display_settings, const char *what,
990                                                const ColorManagedDisplay *default_display)
991 {
992         if (display_settings->display_device[0] == '\0') {
993                 BLI_strncpy(display_settings->display_device, default_display->name, sizeof(display_settings->display_device));
994         }
995         else {
996                 ColorManagedDisplay *display = colormanage_display_get_named(display_settings->display_device);
997
998                 if (!display) {
999                         printf("Color management: display \"%s\" used by %s not found, setting to default (\"%s\").\n",
1000                                display_settings->display_device, what, default_display->name);
1001
1002                         BLI_strncpy(display_settings->display_device, default_display->name,
1003                                     sizeof(display_settings->display_device));
1004                 }
1005         }
1006 }
1007
1008 static void colormanage_check_view_settings(ColorManagedDisplaySettings *display_settings,
1009                                             ColorManagedViewSettings *view_settings, const char *what)
1010 {
1011         ColorManagedDisplay *display;
1012         ColorManagedView *default_view = NULL;
1013         ColorManagedLook *default_look = (ColorManagedLook *) global_looks.first;
1014
1015         if (view_settings->view_transform[0] == '\0') {
1016                 display = colormanage_display_get_named(display_settings->display_device);
1017
1018                 if (display)
1019                         default_view = colormanage_view_get_default(display);
1020
1021                 if (default_view)
1022                         BLI_strncpy(view_settings->view_transform, default_view->name, sizeof(view_settings->view_transform));
1023         }
1024         else {
1025                 ColorManagedView *view = colormanage_view_get_named(view_settings->view_transform);
1026
1027                 if (!view) {
1028                         display = colormanage_display_get_named(display_settings->display_device);
1029
1030                         if (display)
1031                                 default_view = colormanage_view_get_default(display);
1032
1033                         if (default_view) {
1034                                 printf("Color management: %s view \"%s\" not found, setting default \"%s\".\n",
1035                                        what, view_settings->view_transform, default_view->name);
1036
1037                                 BLI_strncpy(view_settings->view_transform, default_view->name, sizeof(view_settings->view_transform));
1038                         }
1039                 }
1040         }
1041
1042         if (view_settings->look[0] == '\0') {
1043                 BLI_strncpy(view_settings->look, default_look->name, sizeof(view_settings->look));
1044         }
1045         else {
1046                 ColorManagedLook *look = colormanage_look_get_named(view_settings->look);
1047                 if (look == NULL) {
1048                         printf("Color management: %s look \"%s\" not found, setting default \"%s\".\n",
1049                                what, view_settings->look, default_look->name);
1050
1051                         BLI_strncpy(view_settings->look, default_look->name, sizeof(view_settings->look));
1052                 }
1053         }
1054
1055         /* OCIO_TODO: move to do_versions() */
1056         if (view_settings->exposure == 0.0f && view_settings->gamma == 0.0f) {
1057                 view_settings->exposure = 0.0f;
1058                 view_settings->gamma = 1.0f;
1059         }
1060 }
1061
1062 static void colormanage_check_colorspace_settings(ColorManagedColorspaceSettings *colorspace_settings, const char *what)
1063 {
1064         if (colorspace_settings->name[0] == '\0') {
1065                 /* pass */
1066         }
1067         else {
1068                 ColorSpace *colorspace = colormanage_colorspace_get_named(colorspace_settings->name);
1069
1070                 if (!colorspace) {
1071                         printf("Color management: %s colorspace \"%s\" not found, will use default instead.\n",
1072                                what, colorspace_settings->name);
1073
1074                         BLI_strncpy(colorspace_settings->name, "", sizeof(colorspace_settings->name));
1075                 }
1076         }
1077
1078         (void) what;
1079 }
1080
1081 void IMB_colormanagement_check_file_config(Main *bmain)
1082 {
1083         Scene *scene;
1084         Image *image;
1085         MovieClip *clip;
1086
1087         ColorManagedDisplay *default_display;
1088
1089         default_display = colormanage_display_get_default();
1090
1091         if (!default_display) {
1092                 /* happens when OCIO configuration is incorrect */
1093                 return;
1094         }
1095
1096         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
1097                 ColorManagedColorspaceSettings *sequencer_colorspace_settings;
1098
1099                 colormanage_check_display_settings(&scene->display_settings, "scene", default_display);
1100                 colormanage_check_view_settings(&scene->display_settings, &scene->view_settings, "scene");
1101
1102                 sequencer_colorspace_settings = &scene->sequencer_colorspace_settings;
1103
1104                 colormanage_check_colorspace_settings(sequencer_colorspace_settings, "sequencer");
1105
1106                 if (sequencer_colorspace_settings->name[0] == '\0') {
1107                         BLI_strncpy(sequencer_colorspace_settings->name, global_role_default_sequencer, MAX_COLORSPACE_NAME);
1108                 }
1109         }
1110
1111         /* ** check input color space settings ** */
1112
1113         for (image = bmain->image.first; image; image = image->id.next) {
1114                 colormanage_check_colorspace_settings(&image->colorspace_settings, "image");
1115         }
1116
1117         for (clip = bmain->movieclip.first; clip; clip = clip->id.next) {
1118                 colormanage_check_colorspace_settings(&clip->colorspace_settings, "clip");
1119         }
1120 }
1121
1122 void IMB_colormanagement_validate_settings(ColorManagedDisplaySettings *display_settings,
1123                                            ColorManagedViewSettings *view_settings)
1124 {
1125         ColorManagedDisplay *display;
1126         ColorManagedView *default_view = NULL;
1127         LinkData *view_link;
1128
1129         display = colormanage_display_get_named(display_settings->display_device);
1130
1131         default_view = colormanage_view_get_default(display);
1132
1133         for (view_link = display->views.first; view_link; view_link = view_link->next) {
1134                 ColorManagedView *view = view_link->data;
1135
1136                 if (!strcmp(view->name, view_settings->view_transform))
1137                         break;
1138         }
1139
1140         if (view_link == NULL && default_view)
1141                 BLI_strncpy(view_settings->view_transform, default_view->name, sizeof(view_settings->view_transform));
1142 }
1143
1144 const char *IMB_colormanagement_role_colorspace_name_get(int role)
1145 {
1146         switch (role) {
1147                 case COLOR_ROLE_SCENE_LINEAR:
1148                         return global_role_scene_linear;
1149                         break;
1150                 case COLOR_ROLE_COLOR_PICKING:
1151                         return global_role_color_picking;
1152                         break;
1153                 case COLOR_ROLE_TEXTURE_PAINTING:
1154                         return global_role_texture_painting;
1155                         break;
1156                 case COLOR_ROLE_DEFAULT_SEQUENCER:
1157                         return global_role_default_sequencer;
1158                         break;
1159                 case COLOR_ROLE_DEFAULT_FLOAT:
1160                         return global_role_default_float;
1161                         break;
1162                 case COLOR_ROLE_DEFAULT_BYTE:
1163                         return global_role_default_byte;
1164                         break;
1165                 default:
1166                         printf("Unknown role was passed to %s\n", __func__);
1167                         BLI_assert(0);
1168                         break;
1169         }
1170
1171         return NULL;
1172 }
1173
1174 void IMB_colormanagement_check_is_data(ImBuf *ibuf, const char *name)
1175 {
1176         ColorSpace *colorspace = colormanage_colorspace_get_named(name);
1177
1178         if (colorspace && colorspace->is_data)
1179                 ibuf->colormanage_flag |= IMB_COLORMANAGE_IS_DATA;
1180         else
1181                 ibuf->colormanage_flag &= ~IMB_COLORMANAGE_IS_DATA;
1182 }
1183
1184 void IMB_colormanagement_assign_float_colorspace(ImBuf *ibuf, const char *name)
1185 {
1186         ColorSpace *colorspace = colormanage_colorspace_get_named(name);
1187
1188         ibuf->float_colorspace = colorspace;
1189
1190         if (colorspace && colorspace->is_data)
1191                 ibuf->colormanage_flag |= IMB_COLORMANAGE_IS_DATA;
1192         else
1193                 ibuf->colormanage_flag &= ~IMB_COLORMANAGE_IS_DATA;
1194 }
1195
1196 void IMB_colormanagement_assign_rect_colorspace(ImBuf *ibuf, const char *name)
1197 {
1198         ColorSpace *colorspace = colormanage_colorspace_get_named(name);
1199
1200         ibuf->rect_colorspace = colorspace;
1201
1202         if (colorspace && colorspace->is_data)
1203                 ibuf->colormanage_flag |= IMB_COLORMANAGE_IS_DATA;
1204         else
1205                 ibuf->colormanage_flag &= ~IMB_COLORMANAGE_IS_DATA;
1206 }
1207
1208 const char *IMB_colormanagement_get_float_colorspace(ImBuf *ibuf)
1209 {
1210         if (ibuf->float_colorspace) {
1211                 return ibuf->float_colorspace->name;
1212         }
1213         else {
1214                 return IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
1215         }
1216 }
1217
1218 const char *IMB_colormanagement_get_rect_colorspace(ImBuf *ibuf)
1219 {
1220         return ibuf->rect_colorspace->name;
1221 }
1222
1223 /*********************** Threaded display buffer transform routines *************************/
1224
1225 typedef struct DisplayBufferThread {
1226         ColormanageProcessor *cm_processor;
1227
1228         float *buffer;
1229         unsigned char *byte_buffer;
1230
1231         float *display_buffer;
1232         unsigned char *display_buffer_byte;
1233
1234         int width;
1235         int start_line;
1236         int tot_line;
1237
1238         int channels;
1239         float dither;
1240         bool is_data;
1241
1242         const char *byte_colorspace;
1243         const char *float_colorspace;
1244 } DisplayBufferThread;
1245
1246 typedef struct DisplayBufferInitData {
1247         ImBuf *ibuf;
1248         ColormanageProcessor *cm_processor;
1249         float *buffer;
1250         unsigned char *byte_buffer;
1251
1252         float *display_buffer;
1253         unsigned char *display_buffer_byte;
1254
1255         int width;
1256
1257         const char *byte_colorspace;
1258         const char *float_colorspace;
1259 } DisplayBufferInitData;
1260
1261 static void display_buffer_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
1262 {
1263         DisplayBufferThread *handle = (DisplayBufferThread *) handle_v;
1264         DisplayBufferInitData *init_data = (DisplayBufferInitData *) init_data_v;
1265         ImBuf *ibuf = init_data->ibuf;
1266
1267         int channels = ibuf->channels;
1268         float dither = ibuf->dither;
1269         bool is_data = (ibuf->colormanage_flag & IMB_COLORMANAGE_IS_DATA) != 0;
1270
1271         int offset = channels * start_line * ibuf->x;
1272         int display_buffer_byte_offset = DISPLAY_BUFFER_CHANNELS * start_line * ibuf->x;
1273
1274         memset(handle, 0, sizeof(DisplayBufferThread));
1275
1276         handle->cm_processor = init_data->cm_processor;
1277
1278         if (init_data->buffer)
1279                 handle->buffer = init_data->buffer + offset;
1280
1281         if (init_data->byte_buffer)
1282                 handle->byte_buffer = init_data->byte_buffer + offset;
1283
1284         if (init_data->display_buffer)
1285                 handle->display_buffer = init_data->display_buffer + offset;
1286
1287         if (init_data->display_buffer_byte)
1288                 handle->display_buffer_byte = init_data->display_buffer_byte + display_buffer_byte_offset;
1289
1290         handle->width = ibuf->x;
1291
1292         handle->start_line = start_line;
1293         handle->tot_line = tot_line;
1294
1295         handle->channels = channels;
1296         handle->dither = dither;
1297         handle->is_data = is_data;
1298
1299         handle->byte_colorspace = init_data->byte_colorspace;
1300         handle->float_colorspace = init_data->float_colorspace;
1301 }
1302
1303 static void display_buffer_apply_get_linear_buffer(DisplayBufferThread *handle, int start_scanline, int num_scanlines,
1304                                                    float *linear_buffer, bool *is_straight_alpha)
1305 {
1306         int channels = handle->channels;
1307         int width = handle->width;
1308         int height = num_scanlines;
1309         int scanline_offset = channels * start_scanline * width;
1310
1311         int buffer_size = channels * width * height;
1312
1313         bool is_data = handle->is_data;
1314         bool is_data_display = handle->cm_processor->is_data_result;
1315
1316         if (!handle->buffer) {
1317                 unsigned char *byte_buffer = handle->byte_buffer;
1318
1319                 const char *from_colorspace = handle->byte_colorspace;
1320                 const char *to_colorspace = global_role_scene_linear;
1321
1322                 float *fp;
1323                 unsigned char *cp;
1324                 int i;
1325
1326                 /* first convert byte buffer to float, keep in image space */
1327                 for (i = 0, fp = linear_buffer, cp = byte_buffer + scanline_offset;
1328                      i < width * height;
1329                      i++, fp += channels, cp += channels)
1330                 {
1331                         if (channels == 3) {
1332                                 rgb_uchar_to_float(fp, cp);
1333                         }
1334                         else if (channels == 4) {
1335                                 rgba_uchar_to_float(fp, cp);
1336                         }
1337                         else {
1338                                 BLI_assert(!"Buffers of 3 or 4 channels are only supported here");
1339                         }
1340                 }
1341
1342                 if (!is_data && !is_data_display) {
1343                         /* convert float buffer to scene linear space */
1344                         IMB_colormanagement_transform(linear_buffer, width, height, channels,
1345                                                       from_colorspace, to_colorspace, false);
1346                 }
1347
1348                 *is_straight_alpha = true;
1349         }
1350         else if (handle->float_colorspace) {
1351                 /* currently float is non-linear only in sequencer, which is working
1352                  * in it's own color space even to handle float buffers.
1353                  * This color space is the same for byte and float images.
1354                  * Need to convert float buffer to linear space before applying display transform
1355                  */
1356
1357                 const char *from_colorspace = handle->float_colorspace;
1358                 const char *to_colorspace = global_role_scene_linear;
1359
1360                 memcpy(linear_buffer, handle->buffer + scanline_offset, buffer_size * sizeof(float));
1361
1362                 if (!is_data && !is_data_display) {
1363                         IMB_colormanagement_transform(linear_buffer, width, height, channels,
1364                                                       from_colorspace, to_colorspace, true);
1365                 }
1366
1367                 *is_straight_alpha = false;
1368         }
1369         else {
1370                 /* some processors would want to modify float original buffer
1371                  * before converting it into display byte buffer, so we need to
1372                  * make sure original's ImBuf buffers wouldn't be modified by
1373                  * using duplicated buffer here
1374                  */
1375
1376                 memcpy(linear_buffer, handle->buffer + scanline_offset, buffer_size * sizeof(float));
1377
1378                 *is_straight_alpha = false;
1379         }
1380 }
1381
1382 static void *do_display_buffer_apply_thread(void *handle_v)
1383 {
1384         DisplayBufferThread *handle = (DisplayBufferThread *) handle_v;
1385         ColormanageProcessor *cm_processor = handle->cm_processor;
1386         float *display_buffer = handle->display_buffer;
1387         unsigned char *display_buffer_byte = handle->display_buffer_byte;
1388         int channels = handle->channels;
1389         int width = handle->width;
1390         int height = handle->tot_line;
1391         float dither = handle->dither;
1392         bool is_data = handle->is_data;
1393
1394         if (cm_processor == NULL) {
1395                 if (display_buffer_byte) {
1396                         IMB_buffer_byte_from_byte(display_buffer_byte, handle->byte_buffer, IB_PROFILE_SRGB, IB_PROFILE_SRGB,
1397                                                   FALSE, width, height, width, width);
1398                 }
1399
1400                 if (display_buffer) {
1401                         IMB_buffer_float_from_byte(display_buffer, handle->byte_buffer, IB_PROFILE_SRGB, IB_PROFILE_SRGB,
1402                                                    FALSE, width, height, width, width);
1403                 }
1404         }
1405         else {
1406 #define SCANLINE_BLOCK_SIZE 64
1407                 /* TODO(sergey): Instead of nasty scanline-blocking in per-scanline-block thread we might
1408                  *               better to use generic task scheduler, but that would need extra testing
1409                  *               before deploying into production.
1410                  */
1411
1412                 int scanlines = (height + SCANLINE_BLOCK_SIZE - 1) / SCANLINE_BLOCK_SIZE;
1413                 int i;
1414                 float *linear_buffer = MEM_mallocN(channels * width * SCANLINE_BLOCK_SIZE * sizeof(float),
1415                                                    "color conversion linear buffer");
1416
1417                 for (i = 0; i < scanlines; i ++) {
1418                         int start_scanline = i * SCANLINE_BLOCK_SIZE;
1419                         int num_scanlines = (i == scanlines - 1) ?
1420                                             (height - SCANLINE_BLOCK_SIZE * i) :
1421                                             SCANLINE_BLOCK_SIZE;
1422                         int scanline_offset = channels * start_scanline * width;
1423                         int scanline_offset4 = 4 * start_scanline * width;
1424                         bool is_straight_alpha, predivide;
1425
1426                         display_buffer_apply_get_linear_buffer(handle, start_scanline, num_scanlines,
1427                                                                linear_buffer, &is_straight_alpha);
1428                         predivide = is_straight_alpha == false;
1429
1430                         if (is_data) {
1431                                 /* special case for data buffers - no color space conversions,
1432                                  * only generate byte buffers
1433                                  */
1434                         }
1435                         else {
1436                                 /* apply processor */
1437                                 IMB_colormanagement_processor_apply(cm_processor, linear_buffer, width, num_scanlines, channels,
1438                                                                     predivide);
1439                         }
1440
1441                         /* copy result to output buffers */
1442                         if (display_buffer_byte) {
1443                                 /* do conversion */
1444                                 IMB_buffer_byte_from_float(display_buffer_byte + scanline_offset4, linear_buffer,
1445                                                            channels, dither, IB_PROFILE_SRGB, IB_PROFILE_SRGB,
1446                                                            predivide, width, num_scanlines, width, width);
1447                         }
1448
1449                         if (display_buffer) {
1450                                 memcpy(display_buffer + scanline_offset, linear_buffer, width * num_scanlines * channels * sizeof(float));
1451
1452                                 if (is_straight_alpha && channels == 4) {
1453                                         int i;
1454                                         float *fp;
1455
1456                                         for (i = 0, fp = display_buffer;
1457                                              i < width * num_scanlines;
1458                                              i++, fp += channels)
1459                                         {
1460                                                 straight_to_premul_v4(fp);
1461                                         }
1462                                 }
1463                         }
1464                 }
1465
1466                 MEM_freeN(linear_buffer);
1467
1468 #undef SCANLINE_BLOCK_SIZE
1469         }
1470
1471         return NULL;
1472 }
1473
1474 static void display_buffer_apply_threaded(ImBuf *ibuf, float *buffer, unsigned char *byte_buffer, float *display_buffer,
1475                                           unsigned char *display_buffer_byte, ColormanageProcessor *cm_processor)
1476 {
1477         DisplayBufferInitData init_data;
1478
1479         init_data.ibuf = ibuf;
1480         init_data.cm_processor = cm_processor;
1481         init_data.buffer = buffer;
1482         init_data.byte_buffer = byte_buffer;
1483         init_data.display_buffer = display_buffer;
1484         init_data.display_buffer_byte = display_buffer_byte;
1485
1486         if (ibuf->rect_colorspace != NULL) {
1487                 init_data.byte_colorspace = ibuf->rect_colorspace->name;
1488         }
1489         else {
1490                 /* happens for viewer images, which are not so simple to determine where to
1491                  * set image buffer's color spaces
1492                  */
1493                 init_data.byte_colorspace = global_role_default_byte;
1494         }
1495
1496         if (ibuf->float_colorspace != NULL) {
1497                 /* sequencer stores float buffers in non-linear space */
1498                 init_data.float_colorspace = ibuf->float_colorspace->name;
1499         }
1500         else {
1501                 init_data.float_colorspace = NULL;
1502         }
1503
1504         IMB_processor_apply_threaded(ibuf->y, sizeof(DisplayBufferThread), &init_data,
1505                                      display_buffer_init_handle, do_display_buffer_apply_thread);
1506 }
1507
1508 static bool is_ibuf_rect_in_display_space(ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
1509                                           const ColorManagedDisplaySettings *display_settings)
1510 {
1511         if ((view_settings->flag & COLORMANAGE_VIEW_USE_CURVES) == 0 &&
1512             view_settings->exposure == 0.0f &&
1513             view_settings->gamma == 1.0f)
1514         {
1515                 const char *from_colorspace = ibuf->rect_colorspace->name;
1516                 const char *to_colorspace = display_transform_get_colorspace_name(view_settings, display_settings);
1517
1518                 if (to_colorspace && !strcmp(from_colorspace, to_colorspace))
1519                         return true;
1520         }
1521
1522         return false;
1523 }
1524
1525 static void colormanage_display_buffer_process_ex(ImBuf *ibuf, float *display_buffer, unsigned char *display_buffer_byte,
1526                                                   const ColorManagedViewSettings *view_settings,
1527                                                   const ColorManagedDisplaySettings *display_settings)
1528 {
1529         ColormanageProcessor *cm_processor = NULL;
1530         bool skip_transform = false;
1531
1532         /* if we're going to transform byte buffer, check whether transformation would
1533          * happen to the same color space as byte buffer itself is
1534          * this would save byte -> float -> byte conversions making display buffer
1535          * computation noticeable faster
1536          */
1537         if (ibuf->rect_float == NULL && ibuf->rect_colorspace) {
1538                 skip_transform = is_ibuf_rect_in_display_space(ibuf, view_settings, display_settings);
1539         }
1540
1541         if (skip_transform == false)
1542                 cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
1543
1544         display_buffer_apply_threaded(ibuf, ibuf->rect_float, (unsigned char *) ibuf->rect,
1545                                       display_buffer, display_buffer_byte, cm_processor);
1546
1547         if (cm_processor)
1548                 IMB_colormanagement_processor_free(cm_processor);
1549 }
1550
1551 static void colormanage_display_buffer_process(ImBuf *ibuf, unsigned char *display_buffer,
1552                                                const ColorManagedViewSettings *view_settings,
1553                                                const ColorManagedDisplaySettings *display_settings)
1554 {
1555         colormanage_display_buffer_process_ex(ibuf, NULL, display_buffer, view_settings, display_settings);
1556 }
1557
1558 /*********************** Threaded processor transform routines *************************/
1559
1560 typedef struct ProcessorTransformThread {
1561         ColormanageProcessor *cm_processor;
1562         float *buffer;
1563         int width;
1564         int start_line;
1565         int tot_line;
1566         int channels;
1567         bool predivide;
1568 } ProcessorTransformThread;
1569
1570 typedef struct ProcessorTransformInit {
1571         ColormanageProcessor *cm_processor;
1572         float *buffer;
1573         int width;
1574         int height;
1575         int channels;
1576         bool predivide;
1577 } ProcessorTransformInitData;
1578
1579 static void processor_transform_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
1580 {
1581         ProcessorTransformThread *handle = (ProcessorTransformThread *) handle_v;
1582         ProcessorTransformInitData *init_data = (ProcessorTransformInitData *) init_data_v;
1583
1584         int channels = init_data->channels;
1585         int width = init_data->width;
1586         bool predivide = init_data->predivide;
1587
1588         int offset = channels * start_line * width;
1589
1590         memset(handle, 0, sizeof(ProcessorTransformThread));
1591
1592         handle->cm_processor = init_data->cm_processor;
1593
1594         handle->buffer = init_data->buffer + offset;
1595
1596         handle->width = width;
1597
1598         handle->start_line = start_line;
1599         handle->tot_line = tot_line;
1600
1601         handle->channels = channels;
1602         handle->predivide = predivide;
1603 }
1604
1605 static void *do_processor_transform_thread(void *handle_v)
1606 {
1607         ProcessorTransformThread *handle = (ProcessorTransformThread *) handle_v;
1608         float *buffer = handle->buffer;
1609         int channels = handle->channels;
1610         int width = handle->width;
1611         int height = handle->tot_line;
1612         bool predivide = handle->predivide;
1613
1614         IMB_colormanagement_processor_apply(handle->cm_processor, buffer, width, height, channels, predivide);
1615
1616         return NULL;
1617 }
1618
1619 static void processor_transform_apply_threaded(float *buffer, int width, int height, int channels,
1620                                                ColormanageProcessor *cm_processor, bool predivide)
1621 {
1622         ProcessorTransformInitData init_data;
1623
1624         init_data.cm_processor = cm_processor;
1625         init_data.buffer = buffer;
1626         init_data.width = width;
1627         init_data.height = height;
1628         init_data.channels = channels;
1629         init_data.predivide = predivide;
1630
1631         IMB_processor_apply_threaded(height, sizeof(ProcessorTransformThread), &init_data,
1632                                      processor_transform_init_handle, do_processor_transform_thread);
1633 }
1634
1635 /*********************** Color space transformation functions *************************/
1636
1637 /* convert the whole buffer from specified by name color space to another - internal implementation */
1638 static void colormanagement_transform_ex(float *buffer, int width, int height, int channels, const char *from_colorspace,
1639                                          const char *to_colorspace, bool predivide, bool do_threaded)
1640 {
1641         ColormanageProcessor *cm_processor;
1642
1643         if (from_colorspace[0] == '\0') {
1644                 return;
1645         }
1646
1647         if (!strcmp(from_colorspace, to_colorspace)) {
1648                 /* if source and destination color spaces are identical, skip
1649                  * threading overhead and simply do nothing
1650                  */
1651                 return;
1652         }
1653
1654         cm_processor = IMB_colormanagement_colorspace_processor_new(from_colorspace, to_colorspace);
1655
1656         if (do_threaded)
1657                 processor_transform_apply_threaded(buffer, width, height, channels, cm_processor, predivide);
1658         else
1659                 IMB_colormanagement_processor_apply(cm_processor, buffer, width, height, channels, predivide);
1660
1661         IMB_colormanagement_processor_free(cm_processor);
1662 }
1663
1664 /* convert the whole buffer from specified by name color space to another */
1665 void IMB_colormanagement_transform(float *buffer, int width, int height, int channels,
1666                                    const char *from_colorspace, const char *to_colorspace, bool predivide)
1667 {
1668         colormanagement_transform_ex(buffer, width, height, channels, from_colorspace, to_colorspace, predivide, false);
1669 }
1670
1671 /* convert the whole buffer from specified by name color space to another
1672  * will do threaded conversion
1673  */
1674 void IMB_colormanagement_transform_threaded(float *buffer, int width, int height, int channels,
1675                                             const char *from_colorspace, const char *to_colorspace, bool predivide)
1676 {
1677         colormanagement_transform_ex(buffer, width, height, channels, from_colorspace, to_colorspace, predivide, true);
1678 }
1679
1680 void IMB_colormanagement_transform_v4(float pixel[4], const char *from_colorspace, const char *to_colorspace)
1681 {
1682         ColormanageProcessor *cm_processor;
1683
1684         if (from_colorspace[0] == '\0') {
1685                 return;
1686         }
1687
1688         if (!strcmp(from_colorspace, to_colorspace)) {
1689                 /* if source and destination color spaces are identical, skip
1690                  * threading overhead and simply do nothing
1691                  */
1692                 return;
1693         }
1694
1695         cm_processor = IMB_colormanagement_colorspace_processor_new(from_colorspace, to_colorspace);
1696
1697         IMB_colormanagement_processor_apply_v4(cm_processor, pixel);
1698
1699         IMB_colormanagement_processor_free(cm_processor);
1700 }
1701
1702 /* convert pixel from specified by descriptor color space to scene linear
1703  * used by performance-critical areas such as renderer and baker
1704  */
1705 void IMB_colormanagement_colorspace_to_scene_linear_v3(float pixel[3], ColorSpace *colorspace)
1706 {
1707         OCIO_ConstProcessorRcPtr *processor;
1708
1709         if (!colorspace) {
1710                 /* should never happen */
1711                 printf("%s: perform conversion from unknown color space\n", __func__);
1712                 return;
1713         }
1714
1715         processor = colorspace_to_scene_linear_processor(colorspace);
1716
1717         if (processor)
1718                 OCIO_processorApplyRGB(processor, pixel);
1719 }
1720
1721 /* same as above, but converts colors in opposite direction */
1722 void IMB_colormanagement_scene_linear_to_colorspace_v3(float pixel[3], ColorSpace *colorspace)
1723 {
1724         OCIO_ConstProcessorRcPtr *processor;
1725
1726         if (!colorspace) {
1727                 /* should never happen */
1728                 printf("%s: perform conversion from unknown color space\n", __func__);
1729                 return;
1730         }
1731
1732         processor = colorspace_from_scene_linear_processor(colorspace);
1733
1734         if (processor)
1735                 OCIO_processorApplyRGB(processor, pixel);
1736 }
1737
1738 void IMB_colormanagement_colorspace_to_scene_linear_v4(float pixel[4], bool predivide, ColorSpace *colorspace)
1739 {
1740         OCIO_ConstProcessorRcPtr *processor;
1741
1742         if (!colorspace) {
1743                 /* should never happen */
1744                 printf("%s: perform conversion from unknown color space\n", __func__);
1745                 return;
1746         }
1747
1748         processor = colorspace_to_scene_linear_processor(colorspace);
1749
1750         if (processor) {
1751                 if (predivide)
1752                         OCIO_processorApplyRGBA_predivide(processor, pixel);
1753                 else
1754                         OCIO_processorApplyRGBA(processor, pixel);
1755         }
1756 }
1757
1758 void IMB_colormanagement_colorspace_to_scene_linear(float *buffer, int width, int height, int channels, struct ColorSpace *colorspace, bool predivide)
1759 {
1760         OCIO_ConstProcessorRcPtr *processor;
1761
1762         if (!colorspace) {
1763                 /* should never happen */
1764                 printf("%s: perform conversion from unknown color space\n", __func__);
1765                 return;
1766         }
1767
1768         processor = colorspace_to_scene_linear_processor(colorspace);
1769
1770         if (processor) {
1771                 OCIO_PackedImageDesc *img;
1772
1773                 img = OCIO_createOCIO_PackedImageDesc(buffer, width, height, channels, sizeof(float),
1774                                                       channels * sizeof(float), channels * sizeof(float) * width);
1775
1776                 if (predivide)
1777                         OCIO_processorApply_predivide(processor, img);
1778                 else
1779                         OCIO_processorApply(processor, img);
1780
1781                 OCIO_PackedImageDescRelease(img);
1782         }
1783 }
1784
1785 /* convert pixel from scene linear to display space using default view
1786  * used by performance-critical areas such as color-related widgets where we want to reduce
1787  * amount of per-widget allocations
1788  */
1789 void IMB_colormanagement_scene_linear_to_display_v3(float pixel[3], ColorManagedDisplay *display)
1790 {
1791         OCIO_ConstProcessorRcPtr *processor;
1792
1793         processor = display_from_scene_linear_processor(display);
1794
1795         if (processor)
1796                 OCIO_processorApplyRGB(processor, pixel);
1797 }
1798
1799 /* same as above, but converts color in opposite direction */
1800 void IMB_colormanagement_display_to_scene_linear_v3(float pixel[3], ColorManagedDisplay *display)
1801 {
1802         OCIO_ConstProcessorRcPtr *processor;
1803
1804         processor = display_to_scene_linear_processor(display);
1805
1806         if (processor)
1807                 OCIO_processorApplyRGB(processor, pixel);
1808 }
1809
1810 void IMB_colormanagement_pixel_to_display_space_v4(float result[4], const float pixel[4],
1811                                                    const ColorManagedViewSettings *view_settings,
1812                                                    const ColorManagedDisplaySettings *display_settings)
1813 {
1814         ColormanageProcessor *cm_processor;
1815
1816         copy_v4_v4(result, pixel);
1817
1818         cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
1819         IMB_colormanagement_processor_apply_v4(cm_processor, result);
1820         IMB_colormanagement_processor_free(cm_processor);
1821 }
1822
1823 void IMB_colormanagement_pixel_to_display_space_v3(float result[3], const float pixel[3],
1824                                                    const ColorManagedViewSettings *view_settings,
1825                                                    const ColorManagedDisplaySettings *display_settings)
1826 {
1827         ColormanageProcessor *cm_processor;
1828
1829         copy_v3_v3(result, pixel);
1830
1831         cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
1832         IMB_colormanagement_processor_apply_v3(cm_processor, result);
1833         IMB_colormanagement_processor_free(cm_processor);
1834 }
1835
1836 static void colormanagement_imbuf_make_display_space(ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
1837                                                      const ColorManagedDisplaySettings *display_settings, bool make_byte)
1838 {
1839         if (!ibuf->rect && make_byte)
1840                 imb_addrectImBuf(ibuf);
1841
1842         if (global_tot_display == 0 || global_tot_view == 0) {
1843                 IMB_buffer_float_from_float(ibuf->rect_float, ibuf->rect_float, ibuf->channels, IB_PROFILE_SRGB, IB_PROFILE_LINEAR_RGB,
1844                                             TRUE, ibuf->x, ibuf->y, ibuf->x, ibuf->x);
1845         }
1846         else {
1847                 colormanage_display_buffer_process_ex(ibuf, ibuf->rect_float, (unsigned char *)ibuf->rect,
1848                                                       view_settings, display_settings);
1849         }
1850 }
1851
1852 void IMB_colormanagement_imbuf_make_display_space(ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
1853                                                   const ColorManagedDisplaySettings *display_settings)
1854 {
1855         colormanagement_imbuf_make_display_space(ibuf, view_settings, display_settings, false);
1856 }
1857
1858 /* prepare image buffer to be saved on disk, applying color management if needed
1859  * color management would be applied if image is saving as render result and if
1860  * file format is not expecting float buffer to be in linear space (currently
1861  * JPEG2000 and TIFF are such formats -- they're storing image as float but
1862  * file itself stores applied color space).
1863  *
1864  * Both byte and float buffers would contain applied color space, and result's
1865  * float_colorspace would be set to display color space. This should be checked
1866  * in image format write callback and if float_colorspace is not NULL, no color
1867  * space transformation should be applied on this buffer.
1868  */
1869 ImBuf *IMB_colormanagement_imbuf_for_write(ImBuf *ibuf, bool save_as_render, bool allocate_result, const ColorManagedViewSettings *view_settings,
1870                                            const ColorManagedDisplaySettings *display_settings, ImageFormatData *image_format_data)
1871 {
1872         ImBuf *colormanaged_ibuf = ibuf;
1873         bool do_colormanagement;
1874         bool is_movie = BKE_imtype_is_movie(image_format_data->imtype);
1875         bool requires_linear_float = BKE_imtype_requires_linear_float(image_format_data->imtype);
1876         bool do_alpha_under = image_format_data->planes != R_IMF_PLANES_RGBA;
1877
1878         do_colormanagement = save_as_render && (is_movie || !requires_linear_float);
1879
1880         if (do_colormanagement || do_alpha_under) {
1881                 if (allocate_result) {
1882                         colormanaged_ibuf = IMB_dupImBuf(ibuf);
1883                 }
1884                 else {
1885                         /* render pipeline is constructing image buffer itself, but it's re-using byte and float buffers from render result
1886                          * make copy of this buffers here sine this buffers would be transformed to other color space here
1887                          */
1888
1889                         if (ibuf->rect && (ibuf->mall & IB_rect) == 0) {
1890                                 ibuf->rect = MEM_dupallocN(ibuf->rect);
1891                                 ibuf->mall |= IB_rect;
1892                         }
1893
1894                         if (ibuf->rect_float && (ibuf->mall & IB_rectfloat) == 0) {
1895                                 ibuf->rect_float = MEM_dupallocN(ibuf->rect_float);
1896                                 ibuf->mall |= IB_rectfloat;
1897                         }
1898                 }
1899         }
1900
1901         /* If we're saving from RGBA to RGB buffer then it's not
1902          * so much useful to just ignore alpha -- it leads to bad
1903          * artifacts especially when saving byte images.
1904          *
1905          * What we do here is we're overing our image on top of
1906          * background color (which is currently black).
1907          *
1908          * This is quite much the same as what Gimp does and it
1909          * seems to be what artists expects from saving.
1910          *
1911          * Do a conversion here, so image format writers could
1912          * happily assume all the alpha tricks were made already.
1913          * helps keep things locally here, not spreading it to
1914          * all possible image writers we've got.
1915          */
1916         if (do_alpha_under) {
1917                 float color[3] = {0, 0, 0};
1918
1919                 if (colormanaged_ibuf->rect_float && colormanaged_ibuf->channels == 4) {
1920                         IMB_alpha_under_color_float(colormanaged_ibuf->rect_float, colormanaged_ibuf->x,
1921                                                     colormanaged_ibuf->y, color);
1922                 }
1923
1924                 if (colormanaged_ibuf->rect) {
1925                         IMB_alpha_under_color_byte((unsigned char *)colormanaged_ibuf->rect,
1926                                                    colormanaged_ibuf->x, colormanaged_ibuf->y,
1927                                                    color);
1928                 }
1929         }
1930
1931         if (do_colormanagement) {
1932                 bool make_byte = false;
1933                 ImFileType *type;
1934
1935                 /* for proper check whether byte buffer is required by a format or not
1936                  * should be pretty safe since this image buffer is supposed to be used for
1937                  * saving only and ftype would be overwritten a bit later by BKE_imbuf_write
1938                  */
1939                 colormanaged_ibuf->ftype = BKE_imtype_to_ftype(image_format_data->imtype);
1940
1941                 /* if file format isn't able to handle float buffer itself,
1942                  * we need to allocate byte buffer and store color managed
1943                  * image there
1944                  */
1945                 for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
1946                         if (type->save && type->ftype(type, colormanaged_ibuf)) {
1947                                 if ((type->flag & IM_FTYPE_FLOAT) == 0)
1948                                         make_byte = true;
1949
1950                                 break;
1951                         }
1952                 }
1953
1954                 /* perform color space conversions */
1955                 colormanagement_imbuf_make_display_space(colormanaged_ibuf, view_settings, display_settings, make_byte);
1956
1957                 if (colormanaged_ibuf->rect_float) {
1958                         /* float buffer isn't linear anymore,
1959                          * image format write callback should check for this flag and assume
1960                          * no space conversion should happen if ibuf->float_colorspace != NULL
1961                          */
1962                         colormanaged_ibuf->float_colorspace = display_transform_get_colorspace(view_settings, display_settings);
1963                 }
1964         }
1965
1966         return colormanaged_ibuf;
1967 }
1968
1969 void IMB_colormanagement_buffer_make_display_space(float *buffer, unsigned char *display_buffer,
1970                                                    int width, int height, int channels, float dither,
1971                                                    const ColorManagedViewSettings *view_settings,
1972                                                    const ColorManagedDisplaySettings *display_settings)
1973 {
1974         ColormanageProcessor *cm_processor;
1975         size_t float_buffer_size = width * height * channels * sizeof(float);
1976         float *display_buffer_float = MEM_mallocN(float_buffer_size, "byte_buffer_make_display_space");
1977
1978         memcpy(display_buffer_float, buffer, float_buffer_size);
1979
1980         cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
1981
1982         processor_transform_apply_threaded(display_buffer_float, width, height, channels,
1983                                            cm_processor, true);
1984
1985         IMB_buffer_byte_from_float(display_buffer, display_buffer_float,
1986                                    channels, dither, IB_PROFILE_SRGB, IB_PROFILE_SRGB,
1987                                    TRUE, width, height, width, width);
1988
1989         MEM_freeN(display_buffer_float);
1990         IMB_colormanagement_processor_free(cm_processor);
1991 }
1992
1993 static void imbuf_verify_float(ImBuf *ibuf)
1994 {
1995         /* multiple threads could request for display buffer at once and in case
1996          * view transform is not used it'll lead to display buffer calculated
1997          * several times
1998          * it is harmless, but would take much more time (assuming thread lock
1999          * happens faster than running float->byte conversion for average image)
2000          */
2001         BLI_lock_thread(LOCK_COLORMANAGE);
2002
2003         if (ibuf->rect_float && (ibuf->rect == NULL || (ibuf->userflags & (IB_DISPLAY_BUFFER_INVALID | IB_RECT_INVALID)))) {
2004                 IMB_rect_from_float(ibuf);
2005
2006                 ibuf->userflags &= ~(IB_RECT_INVALID | IB_DISPLAY_BUFFER_INVALID);
2007         }
2008
2009         BLI_unlock_thread(LOCK_COLORMANAGE);
2010 }
2011
2012 /*********************** Public display buffers interfaces *************************/
2013
2014 /* acquire display buffer for given image buffer using specified view and display settings */
2015 unsigned char *IMB_display_buffer_acquire(ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
2016                                           const ColorManagedDisplaySettings *display_settings, void **cache_handle)
2017 {
2018         *cache_handle = NULL;
2019
2020         if (!ibuf->x || !ibuf->y)
2021                 return NULL;
2022
2023         if (global_tot_display == 0 || global_tot_view == 0) {
2024                 /* if there's no view transform or display transforms, fallback to standard sRGB/linear conversion
2025                  * the same logic would be used if OCIO is disabled
2026                  */
2027
2028                 imbuf_verify_float(ibuf);
2029
2030                 return (unsigned char *) ibuf->rect;
2031         }
2032         else {
2033                 unsigned char *display_buffer;
2034                 int buffer_size;
2035                 ColormanageCacheViewSettings cache_view_settings;
2036                 ColormanageCacheDisplaySettings cache_display_settings;
2037                 ColorManagedViewSettings default_view_settings;
2038                 const ColorManagedViewSettings *applied_view_settings;
2039
2040                 if (view_settings) {
2041                         applied_view_settings = view_settings;
2042                 }
2043                 else {
2044                         /* if no view settings were specified, use default display transformation
2045                          * this happens for images which don't want to be displayed with render settings
2046                          */
2047
2048                         init_default_view_settings(display_settings,  &default_view_settings);
2049                         applied_view_settings = &default_view_settings;
2050                 }
2051
2052                 /* early out: no float buffer and byte buffer is already in display space,
2053                  * let's just use if
2054                  */
2055                 if (ibuf->rect_float == NULL && ibuf->rect_colorspace && ibuf->channels == 4) {
2056                         if (is_ibuf_rect_in_display_space(ibuf, applied_view_settings, display_settings))
2057                                 return (unsigned char *) ibuf->rect;
2058                 }
2059
2060                 colormanage_view_settings_to_cache(ibuf, &cache_view_settings, applied_view_settings);
2061                 colormanage_display_settings_to_cache(&cache_display_settings, display_settings);
2062
2063                 if (ibuf->invalid_rect.xmin != ibuf->invalid_rect.xmax) {
2064                         if ((ibuf->userflags & IB_DISPLAY_BUFFER_INVALID) == 0) {
2065                                 IMB_partial_display_buffer_update(ibuf, ibuf->rect_float, (unsigned char *) ibuf->rect,
2066                                                                   ibuf->x, 0, 0, applied_view_settings, display_settings,
2067                                                                   ibuf->invalid_rect.xmin, ibuf->invalid_rect.ymin,
2068                                                                   ibuf->invalid_rect.xmax, ibuf->invalid_rect.ymax,
2069                                                                   false);
2070                         }
2071
2072                         BLI_rcti_init(&ibuf->invalid_rect, 0, 0, 0, 0);
2073                 }
2074
2075                 BLI_lock_thread(LOCK_COLORMANAGE);
2076
2077                 /* ensure color management bit fields exists */
2078                 if (!ibuf->display_buffer_flags) {
2079                         if (global_tot_display)
2080                                 ibuf->display_buffer_flags = MEM_callocN(sizeof(unsigned int) * global_tot_display, "imbuf display_buffer_flags");
2081                 }
2082                 else if (ibuf->userflags & IB_DISPLAY_BUFFER_INVALID) {
2083                         /* all display buffers were marked as invalid from other areas,
2084                          * now propagate this flag to internal color management routines
2085                          */
2086                         memset(ibuf->display_buffer_flags, 0, global_tot_display * sizeof(unsigned int));
2087
2088                         ibuf->userflags &= ~IB_DISPLAY_BUFFER_INVALID;
2089                 }
2090
2091                 display_buffer = colormanage_cache_get(ibuf, &cache_view_settings, &cache_display_settings, cache_handle);
2092
2093                 if (display_buffer) {
2094                         BLI_unlock_thread(LOCK_COLORMANAGE);
2095                         return display_buffer;
2096                 }
2097
2098                 buffer_size = DISPLAY_BUFFER_CHANNELS * ibuf->x * ibuf->y * sizeof(char);
2099                 display_buffer = MEM_callocN(buffer_size, "imbuf display buffer");
2100
2101                 colormanage_display_buffer_process(ibuf, display_buffer, applied_view_settings, display_settings);
2102
2103                 colormanage_cache_put(ibuf, &cache_view_settings, &cache_display_settings, display_buffer, cache_handle);
2104
2105                 BLI_unlock_thread(LOCK_COLORMANAGE);
2106
2107                 return display_buffer;
2108         }
2109 }
2110
2111 /* same as IMB_display_buffer_acquire but gets view and display settings from context */
2112 unsigned char *IMB_display_buffer_acquire_ctx(const bContext *C, ImBuf *ibuf, void **cache_handle)
2113 {
2114         ColorManagedViewSettings *view_settings;
2115         ColorManagedDisplaySettings *display_settings;
2116
2117         IMB_colormanagement_display_settings_from_ctx(C, &view_settings, &display_settings);
2118
2119         return IMB_display_buffer_acquire(ibuf, view_settings, display_settings, cache_handle);
2120 }
2121
2122 void IMB_display_buffer_transform_apply(unsigned char *display_buffer, float *linear_buffer, int width, int height,
2123                                         int channels, const ColorManagedViewSettings *view_settings,
2124                                         const ColorManagedDisplaySettings *display_settings, bool predivide)
2125 {
2126         if (global_tot_display == 0 || global_tot_view == 0) {
2127                 IMB_buffer_byte_from_float(display_buffer, linear_buffer, channels, 0.0f, IB_PROFILE_SRGB, IB_PROFILE_LINEAR_RGB, FALSE,
2128                                            width, height, width, width);
2129         }
2130         else {
2131                 float *buffer;
2132                 ColormanageProcessor *cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
2133
2134                 buffer = MEM_callocN(channels * width * height * sizeof(float), "display transform temp buffer");
2135                 memcpy(buffer, linear_buffer, channels * width * height * sizeof(float));
2136
2137                 IMB_colormanagement_processor_apply(cm_processor, buffer, width, height, channels, predivide);
2138
2139                 IMB_colormanagement_processor_free(cm_processor);
2140
2141                 IMB_buffer_byte_from_float(display_buffer, buffer, channels, 0.0f, IB_PROFILE_SRGB, IB_PROFILE_SRGB,
2142                                            FALSE, width, height, width, width);
2143
2144                 MEM_freeN(buffer);
2145         }
2146 }
2147
2148 void IMB_display_buffer_release(void *cache_handle)
2149 {
2150         if (cache_handle) {
2151                 BLI_lock_thread(LOCK_COLORMANAGE);
2152
2153                 colormanage_cache_handle_release(cache_handle);
2154
2155                 BLI_unlock_thread(LOCK_COLORMANAGE);
2156         }
2157 }
2158
2159 /*********************** Display functions *************************/
2160
2161 const char *colormanage_display_get_default_name(void)
2162 {
2163         OCIO_ConstConfigRcPtr *config = OCIO_getCurrentConfig();
2164         const char *display_name;
2165
2166         display_name = OCIO_configGetDefaultDisplay(config);
2167
2168         OCIO_configRelease(config);
2169
2170         return display_name;
2171 }
2172
2173 ColorManagedDisplay *colormanage_display_get_default(void)
2174 {
2175         const char *display_name = colormanage_display_get_default_name();
2176
2177         if (display_name[0] == '\0')
2178                 return NULL;
2179
2180         return colormanage_display_get_named(display_name);
2181 }
2182
2183 ColorManagedDisplay *colormanage_display_add(const char *name)
2184 {
2185         ColorManagedDisplay *display;
2186         int index = 0;
2187
2188         if (global_displays.last) {
2189                 ColorManagedDisplay *last_display = global_displays.last;
2190
2191                 index = last_display->index;
2192         }
2193
2194         display = MEM_callocN(sizeof(ColorManagedDisplay), "ColorManagedDisplay");
2195
2196         display->index = index + 1;
2197
2198         BLI_strncpy(display->name, name, sizeof(display->name));
2199
2200         BLI_addtail(&global_displays, display);
2201
2202         return display;
2203 }
2204
2205 ColorManagedDisplay *colormanage_display_get_named(const char *name)
2206 {
2207         ColorManagedDisplay *display;
2208
2209         for (display = global_displays.first; display; display = display->next) {
2210                 if (!strcmp(display->name, name))
2211                         return display;
2212         }
2213
2214         return NULL;
2215 }
2216
2217 ColorManagedDisplay *colormanage_display_get_indexed(int index)
2218 {
2219         /* display indices are 1-based */
2220         return BLI_findlink(&global_displays, index - 1);
2221 }
2222
2223 int IMB_colormanagement_display_get_named_index(const char *name)
2224 {
2225         ColorManagedDisplay *display;
2226
2227         display = colormanage_display_get_named(name);
2228
2229         if (display) {
2230                 return display->index;
2231         }
2232
2233         return 0;
2234 }
2235
2236 const char *IMB_colormanagement_display_get_indexed_name(int index)
2237 {
2238         ColorManagedDisplay *display;
2239
2240         display = colormanage_display_get_indexed(index);
2241
2242         if (display) {
2243                 return display->name;
2244         }
2245
2246         return NULL;
2247 }
2248
2249 const char *IMB_colormanagement_display_get_default_name(void)
2250 {
2251         ColorManagedDisplay *display = colormanage_display_get_default();
2252
2253         return display->name;
2254 }
2255
2256 /* used by performance-critical pixel processing areas, such as color widgets */
2257 ColorManagedDisplay *IMB_colormanagement_display_get_named(const char *name)
2258 {
2259         return colormanage_display_get_named(name);
2260 }
2261
2262 const char *IMB_colormanagement_display_get_none_name(void)
2263 {
2264         if (colormanage_display_get_named("None") != NULL)
2265                 return "None";
2266
2267         return colormanage_display_get_default_name();
2268 }
2269
2270 /*********************** View functions *************************/
2271
2272 const char *colormanage_view_get_default_name(const ColorManagedDisplay *display)
2273 {
2274         OCIO_ConstConfigRcPtr *config = OCIO_getCurrentConfig();
2275         const char *name;
2276
2277         name = OCIO_configGetDefaultView(config, display->name);
2278
2279         OCIO_configRelease(config);
2280
2281         return name;
2282 }
2283
2284 ColorManagedView *colormanage_view_get_default(const ColorManagedDisplay *display)
2285 {
2286         const char *name = colormanage_view_get_default_name(display);
2287
2288         if (!name || name[0] == '\0')
2289                 return NULL;
2290
2291         return colormanage_view_get_named(name);
2292 }
2293
2294 ColorManagedView *colormanage_view_add(const char *name)
2295 {
2296         ColorManagedView *view;
2297         int index = global_tot_view;
2298
2299         view = MEM_callocN(sizeof(ColorManagedView), "ColorManagedView");
2300         view->index = index + 1;
2301         BLI_strncpy(view->name, name, sizeof(view->name));
2302
2303         BLI_addtail(&global_views, view);
2304
2305         global_tot_view++;
2306
2307         return view;
2308 }
2309
2310 ColorManagedView *colormanage_view_get_named(const char *name)
2311 {
2312         ColorManagedView *view;
2313
2314         for (view = global_views.first; view; view = view->next) {
2315                 if (!strcmp(view->name, name))
2316                         return view;
2317         }
2318
2319         return NULL;
2320 }
2321
2322 ColorManagedView *colormanage_view_get_indexed(int index)
2323 {
2324         /* view transform indices are 1-based */
2325         return BLI_findlink(&global_views, index - 1);
2326 }
2327
2328 int IMB_colormanagement_view_get_named_index(const char *name)
2329 {
2330         ColorManagedView *view = colormanage_view_get_named(name);
2331
2332         if (view) {
2333                 return view->index;
2334         }
2335
2336         return 0;
2337 }
2338
2339 const char *IMB_colormanagement_view_get_indexed_name(int index)
2340 {
2341         ColorManagedView *view = colormanage_view_get_indexed(index);
2342
2343         if (view) {
2344                 return view->name;
2345         }
2346
2347         return NULL;
2348 }
2349
2350 const char *IMB_colormanagement_view_get_default_name(const char *display_name)
2351 {
2352         ColorManagedDisplay *display = colormanage_display_get_named(display_name);
2353         ColorManagedView *view = NULL;
2354         
2355         if (display)
2356                 view = colormanage_view_get_default(display);
2357
2358         if (view)
2359                 return view->name;
2360
2361         return NULL;
2362 }
2363
2364 /*********************** Color space functions *************************/
2365
2366 static void colormanage_description_strip(char *description)
2367 {
2368         int i, n;
2369
2370         for (i = (int)strlen(description) - 1; i >= 0; i--) {
2371                 if (ELEM(description[i], '\r', '\n')) {
2372                         description[i] = '\0';
2373                 }
2374                 else {
2375                         break;
2376                 }
2377         }
2378
2379         for (i = 0, n = strlen(description); i < n; i++) {
2380                 if (ELEM(description[i], '\r', '\n')) {
2381                         description[i] = ' ';
2382                 }
2383         }
2384 }
2385
2386 ColorSpace *colormanage_colorspace_add(const char *name, const char *description, bool is_invertible, bool is_data)
2387 {
2388         ColorSpace *colorspace, *prev_space;
2389         int counter = 1;
2390
2391         colorspace = MEM_callocN(sizeof(ColorSpace), "ColorSpace");
2392
2393         BLI_strncpy(colorspace->name, name, sizeof(colorspace->name));
2394
2395         if (description) {
2396                 BLI_strncpy(colorspace->description, description, sizeof(colorspace->description));
2397
2398                 colormanage_description_strip(colorspace->description);
2399         }
2400
2401         colorspace->is_invertible = is_invertible;
2402         colorspace->is_data = is_data;
2403
2404         for (prev_space = global_colorspaces.first; prev_space; prev_space = prev_space->next) {
2405                 if (BLI_strcasecmp(prev_space->name, colorspace->name) > 0)
2406                         break;
2407
2408                 prev_space->index = counter++;
2409         }
2410
2411         if (!prev_space)
2412                 BLI_addtail(&global_colorspaces, colorspace);
2413         else
2414                 BLI_insertlinkbefore(&global_colorspaces, prev_space, colorspace);
2415
2416         colorspace->index = counter++;
2417         for (; prev_space; prev_space = prev_space->next) {
2418                 prev_space->index = counter++;
2419         }
2420
2421         global_tot_colorspace++;
2422
2423         return colorspace;
2424 }
2425
2426 ColorSpace *colormanage_colorspace_get_named(const char *name)
2427 {
2428         ColorSpace *colorspace;
2429
2430         for (colorspace = global_colorspaces.first; colorspace; colorspace = colorspace->next) {
2431                 if (!strcmp(colorspace->name, name))
2432                         return colorspace;
2433         }
2434
2435         return NULL;
2436 }
2437
2438 ColorSpace *colormanage_colorspace_get_roled(int role)
2439 {
2440         const char *role_colorspace = IMB_colormanagement_role_colorspace_name_get(role);
2441
2442         return colormanage_colorspace_get_named(role_colorspace);
2443 }
2444
2445 ColorSpace *colormanage_colorspace_get_indexed(int index)
2446 {
2447         /* color space indices are 1-based */
2448         return BLI_findlink(&global_colorspaces, index - 1);
2449 }
2450
2451 int IMB_colormanagement_colorspace_get_named_index(const char *name)
2452 {
2453         ColorSpace *colorspace;
2454
2455         colorspace = colormanage_colorspace_get_named(name);
2456
2457         if (colorspace) {
2458                 return colorspace->index;
2459         }
2460
2461         return 0;
2462 }
2463
2464 const char *IMB_colormanagement_colorspace_get_indexed_name(int index)
2465 {
2466         ColorSpace *colorspace;
2467
2468         colorspace = colormanage_colorspace_get_indexed(index);
2469
2470         if (colorspace) {
2471                 return colorspace->name;
2472         }
2473
2474         return "";
2475 }
2476
2477 void IMB_colormanagment_colorspace_from_ibuf_ftype(ColorManagedColorspaceSettings *colorspace_settings, ImBuf *ibuf)
2478 {
2479         ImFileType *type;
2480
2481         for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
2482                 if (type->save && type->ftype(type, ibuf)) {
2483                         const char *role_colorspace;
2484
2485                         role_colorspace = IMB_colormanagement_role_colorspace_name_get(type->default_save_role);
2486
2487                         BLI_strncpy(colorspace_settings->name, role_colorspace, sizeof(colorspace_settings->name));
2488                 }
2489         }
2490 }
2491
2492 /*********************** Looks functions *************************/
2493
2494 ColorManagedLook *colormanage_look_add(const char *name, const char *process_space, bool is_noop)
2495 {
2496         ColorManagedLook *look;
2497         int index = global_tot_looks;
2498
2499         look = MEM_callocN(sizeof(ColorManagedLook), "ColorManagedLook");
2500         look->index = index + 1;
2501         BLI_strncpy(look->name, name, sizeof(look->name));
2502         BLI_strncpy(look->process_space, process_space, sizeof(look->process_space));
2503         look->is_noop = is_noop;
2504
2505         BLI_addtail(&global_looks, look);
2506
2507         global_tot_looks++;
2508
2509         return look;
2510 }
2511
2512 ColorManagedLook *colormanage_look_get_named(const char *name)
2513 {
2514         ColorManagedLook *look;
2515
2516         for (look = global_looks.first; look; look = look->next) {
2517                 if (!strcmp(look->name, name)) {
2518                         return look;
2519                 }
2520         }
2521
2522         return NULL;
2523 }
2524
2525 ColorManagedLook *colormanage_look_get_indexed(int index)
2526 {
2527         /* look indices are 1-based */
2528         return BLI_findlink(&global_looks, index - 1);
2529 }
2530
2531 int IMB_colormanagement_look_get_named_index(const char *name)
2532 {
2533         ColorManagedLook *look;
2534
2535         look = colormanage_look_get_named(name);
2536
2537         if (look) {
2538                 return look->index;
2539         }
2540
2541         return 0;
2542 }
2543
2544 const char *IMB_colormanagement_look_get_indexed_name(int index)
2545 {
2546         ColorManagedLook *look;
2547
2548         look = colormanage_look_get_indexed(index);
2549
2550         if (look) {
2551                 return look->name;
2552         }
2553
2554         return NULL;
2555 }
2556
2557 /*********************** RNA helper functions *************************/
2558
2559 void IMB_colormanagement_display_items_add(EnumPropertyItem **items, int *totitem)
2560 {
2561         ColorManagedDisplay *display;
2562
2563         for (display = global_displays.first; display; display = display->next) {
2564                 EnumPropertyItem item;
2565
2566                 item.value = display->index;
2567                 item.name = display->name;
2568                 item.identifier = display->name;
2569                 item.icon = 0;
2570                 item.description = "";
2571
2572                 RNA_enum_item_add(items, totitem, &item);
2573         }
2574 }
2575
2576 static void colormanagement_view_item_add(EnumPropertyItem **items, int *totitem, ColorManagedView *view)
2577 {
2578         EnumPropertyItem item;
2579
2580         item.value = view->index;
2581         item.name = view->name;
2582         item.identifier = view->name;
2583         item.icon = 0;
2584         item.description = "";
2585
2586         RNA_enum_item_add(items, totitem, &item);
2587 }
2588
2589 void IMB_colormanagement_view_items_add(EnumPropertyItem **items, int *totitem, const char *display_name)
2590 {
2591         ColorManagedDisplay *display = colormanage_display_get_named(display_name);
2592         ColorManagedView *view;
2593
2594         if (display) {
2595                 LinkData *display_view;
2596
2597                 for (display_view = display->views.first; display_view; display_view = display_view->next) {
2598                         view = display_view->data;
2599
2600                         colormanagement_view_item_add(items, totitem, view);
2601                 }
2602         }
2603 }
2604
2605 void IMB_colormanagement_look_items_add(struct EnumPropertyItem **items, int *totitem)
2606 {
2607         ColorManagedLook *look;
2608
2609         for (look = global_looks.first; look; look = look->next) {
2610                 EnumPropertyItem item;
2611
2612                 item.value = look->index;
2613                 item.name = look->name;
2614                 item.identifier = look->name;
2615                 item.icon = 0;
2616                 item.description = "";
2617
2618                 RNA_enum_item_add(items, totitem, &item);
2619         }
2620 }
2621
2622 void IMB_colormanagement_colorspace_items_add(EnumPropertyItem **items, int *totitem)
2623 {
2624         ColorSpace *colorspace;
2625
2626         for (colorspace = global_colorspaces.first; colorspace; colorspace = colorspace->next) {
2627                 EnumPropertyItem item;
2628
2629                 if (!colorspace->is_invertible)
2630                         continue;
2631
2632                 item.value = colorspace->index;
2633                 item.name = colorspace->name;
2634                 item.identifier = colorspace->name;
2635                 item.icon = 0;
2636                 item.description = colorspace->description;
2637
2638                 RNA_enum_item_add(items, totitem, &item);
2639         }
2640 }
2641
2642 /*********************** Partial display buffer update  *************************/
2643
2644 /*
2645  * Partial display update is supposed to be used by such areas as
2646  * compositor and renderer, This areas are calculating tiles of the
2647  * images and because of performance reasons only this tiles should
2648  * be color managed.
2649  * This gives nice visual feedback without slowing things down.
2650  *
2651  * Updating happens for active display transformation only, all
2652  * the rest buffers would be marked as dirty
2653  */
2654
2655 static void partial_buffer_update_rect(ImBuf *ibuf, unsigned char *display_buffer, const float *linear_buffer,
2656                                        const unsigned char *byte_buffer, int display_stride, int linear_stride,
2657                                        int linear_offset_x, int linear_offset_y, ColormanageProcessor *cm_processor,
2658                                        const int xmin, const int ymin, const int xmax, const int ymax)
2659 {
2660         int x, y;
2661         int channels = ibuf->channels;
2662         float dither = ibuf->dither;
2663         ColorSpace *rect_colorspace = ibuf->rect_colorspace;
2664         float *display_buffer_float = NULL;
2665         const int width = xmax - xmin;
2666         const int height = ymax - ymin;
2667         bool is_data = (ibuf->colormanage_flag & IMB_COLORMANAGE_IS_DATA) != 0;
2668
2669         if (dither != 0.0f) {
2670                 /* cm_processor is NULL in cases byte_buffer's space matches display
2671                  * buffer's space
2672                  * in this case we could skip extra transform and only apply dither
2673                  * use 4 channels for easier byte->float->byte conversion here so
2674                  * (this is only needed to apply dither, in other cases we'll convert
2675                  * byte buffer to display directly)
2676                  */
2677                 if (!cm_processor)
2678                         channels = 4;
2679
2680                 display_buffer_float = MEM_callocN(channels * width * height * sizeof(float), "display buffer for dither");
2681         }
2682
2683         if (cm_processor) {
2684                 for (y = ymin; y < ymax; y++) {
2685                         for (x = xmin; x < xmax; x++) {
2686                                 int display_index = (y * display_stride + x) * channels;
2687                                 int linear_index = ((y - linear_offset_y) * linear_stride + (x - linear_offset_x)) * channels;
2688                                 float pixel[4];
2689
2690                                 if (linear_buffer) {
2691                                         copy_v4_v4(pixel, (float *) linear_buffer + linear_index);
2692                                 }
2693                                 else if (byte_buffer) {
2694                                         rgba_uchar_to_float(pixel, byte_buffer + linear_index);
2695                                         IMB_colormanagement_colorspace_to_scene_linear_v3(pixel, rect_colorspace);
2696                                         straight_to_premul_v4(pixel);
2697                                 }
2698
2699                                 if (!is_data) {
2700                                         IMB_colormanagement_processor_apply_v4_predivide(cm_processor, pixel);
2701                                 }
2702
2703                                 if (display_buffer_float) {
2704                                         int index = ((y - ymin) * width + (x - xmin)) * channels;
2705
2706                                         copy_v4_v4(display_buffer_float + index, pixel);
2707                                 }
2708                                 else {
2709                                         float pixel_straight[4];
2710                                         premul_to_straight_v4_v4(pixel_straight, pixel);
2711                                         rgba_float_to_uchar(display_buffer + display_index, pixel_straight);
2712                                 }
2713                         }
2714                 }
2715         }
2716         else {
2717                 if (display_buffer_float) {
2718                         /* huh, for dither we need float buffer first, no cheaper way. currently */
2719                         IMB_buffer_float_from_byte(display_buffer_float, byte_buffer,
2720                                                    IB_PROFILE_SRGB, IB_PROFILE_SRGB, TRUE,
2721                                                    width, height, width, display_stride);
2722                 }
2723                 else {
2724                         int i;
2725
2726                         for (i = ymin; i < ymax; i++) {
2727                                 int byte_offset = (linear_stride * i + xmin) * 4;
2728                                 int display_offset = (display_stride * i + xmin) * 4;
2729
2730                                 memcpy(display_buffer + display_offset, byte_buffer + byte_offset, 4 * sizeof(char) * width);
2731                         }
2732                 }
2733         }
2734
2735         if (display_buffer_float) {
2736                 int display_index = (ymin * display_stride + xmin) * channels;
2737
2738                 IMB_buffer_byte_from_float(display_buffer + display_index, display_buffer_float, channels, dither,
2739                                            IB_PROFILE_SRGB, IB_PROFILE_SRGB, TRUE, width, height, display_stride, width);
2740
2741                 MEM_freeN(display_buffer_float);
2742         }
2743 }
2744
2745 void IMB_partial_display_buffer_update(ImBuf *ibuf, const float *linear_buffer, const unsigned char *byte_buffer,
2746                                        int stride, int offset_x, int offset_y, const ColorManagedViewSettings *view_settings,
2747                                        const ColorManagedDisplaySettings *display_settings,
2748                                        int xmin, int ymin, int xmax, int ymax, bool update_orig_byte_buffer)
2749 {
2750         if ((ibuf->rect && ibuf->rect_float) || update_orig_byte_buffer) {
2751                 /* update byte buffer created by legacy color management */
2752
2753                 unsigned char *rect = (unsigned char *) ibuf->rect;
2754                 int channels = ibuf->channels;
2755                 int width = xmax - xmin;
2756                 int height = ymax - ymin;
2757                 int rect_index = (ymin * ibuf->x + xmin) * channels;
2758                 int linear_index = ((ymin - offset_y) * stride + (xmin - offset_x)) * channels;
2759
2760                 IMB_buffer_byte_from_float(rect + rect_index, linear_buffer + linear_index, channels, ibuf->dither,
2761                                            IB_PROFILE_SRGB, IB_PROFILE_LINEAR_RGB, TRUE, width, height, ibuf->x, stride);
2762         }
2763
2764         if (ibuf->display_buffer_flags) {
2765                 ColormanageCacheViewSettings cache_view_settings;
2766                 ColormanageCacheDisplaySettings cache_display_settings;
2767                 void *cache_handle = NULL;
2768                 unsigned char *display_buffer = NULL;
2769                 int view_flag, display_index, buffer_width;
2770
2771                 colormanage_view_settings_to_cache(ibuf, &cache_view_settings, view_settings);
2772                 colormanage_display_settings_to_cache(&cache_display_settings, display_settings);
2773
2774                 view_flag = 1 << (cache_view_settings.view - 1);
2775                 display_index = cache_display_settings.display - 1;
2776
2777                 BLI_lock_thread(LOCK_COLORMANAGE);
2778                 if ((ibuf->userflags & IB_DISPLAY_BUFFER_INVALID) == 0)
2779                         display_buffer = colormanage_cache_get(ibuf, &cache_view_settings, &cache_display_settings, &cache_handle);
2780
2781                 /* in some rare cases buffer's dimension could be changing directly from
2782                  * different thread
2783                  * this i.e. happens when image editor acquires render result
2784                  */
2785                 buffer_width = ibuf->x;
2786
2787                 /* mark all other buffers as invalid */
2788                 memset(ibuf->display_buffer_flags, 0, global_tot_display * sizeof(unsigned int));
2789                 ibuf->display_buffer_flags[display_index] |= view_flag;
2790
2791                 BLI_unlock_thread(LOCK_COLORMANAGE);
2792
2793                 if (display_buffer) {
2794                         ColormanageProcessor *cm_processor = NULL;
2795                         bool skip_transform = false;
2796
2797                         /* byte buffer is assumed to be in imbuf's rect space, so if byte buffer
2798                          * is known we could skip display->linear->display conversion in case
2799                          * display color space matches imbuf's rect space
2800                          */
2801                         if (byte_buffer != NULL)
2802                                 skip_transform = is_ibuf_rect_in_display_space(ibuf, view_settings, display_settings);
2803
2804                         if (!skip_transform)
2805                                 cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
2806
2807                         partial_buffer_update_rect(ibuf, display_buffer, linear_buffer, byte_buffer, buffer_width, stride,
2808                                                    offset_x, offset_y, cm_processor, xmin, ymin, xmax, ymax);
2809
2810                         if (cm_processor)
2811                                 IMB_colormanagement_processor_free(cm_processor);
2812
2813                         IMB_display_buffer_release(cache_handle);
2814                 }
2815         }
2816 }
2817
2818 void IMB_partial_display_buffer_update_delayed(ImBuf *ibuf, int xmin, int ymin, int xmax, int ymax)
2819 {
2820         if (ibuf->invalid_rect.xmin == ibuf->invalid_rect.xmax) {
2821                 BLI_rcti_init(&ibuf->invalid_rect, xmin, xmax, ymin, ymax);
2822         }
2823         else {
2824                 rcti rect;
2825                 BLI_rcti_init(&rect, xmin, xmax, ymin, ymax);
2826                 BLI_rcti_union(&ibuf->invalid_rect, &rect);
2827         }
2828 }
2829
2830 /*********************** Pixel processor functions *************************/
2831
2832 ColormanageProcessor *IMB_colormanagement_display_processor_new(const ColorManagedViewSettings *view_settings,
2833                                                                 const ColorManagedDisplaySettings *display_settings)
2834 {
2835         ColormanageProcessor *cm_processor;
2836         ColorManagedViewSettings default_view_settings;
2837         const ColorManagedViewSettings *applied_view_settings;
2838         ColorSpace *display_space;
2839
2840         cm_processor = MEM_callocN(sizeof(ColormanageProcessor), "colormanagement processor");
2841
2842         if (view_settings) {
2843                 applied_view_settings = view_settings;
2844         }
2845         else {
2846                 init_default_view_settings(display_settings,  &default_view_settings);
2847                 applied_view_settings = &default_view_settings;
2848         }
2849
2850         display_space =  display_transform_get_colorspace(applied_view_settings, display_settings);
2851         if (display_space)
2852                 cm_processor->is_data_result = display_space->is_data;
2853
2854         cm_processor->processor = create_display_buffer_processor(applied_view_settings->look,
2855                                                                   applied_view_settings->view_transform,
2856                                                                   display_settings->display_device,
2857                                                                   applied_view_settings->exposure,
2858                                                                   applied_view_settings->gamma,
2859                                                                   global_role_scene_linear);
2860
2861         if (applied_view_settings->flag & COLORMANAGE_VIEW_USE_CURVES) {
2862                 cm_processor->curve_mapping = curvemapping_copy(applied_view_settings->curve_mapping);
2863                 curvemapping_premultiply(cm_processor->curve_mapping, false);
2864         }
2865
2866         return cm_processor;
2867 }
2868
2869 ColormanageProcessor *IMB_colormanagement_colorspace_processor_new(const char *from_colorspace, const char *to_colorspace)
2870 {
2871         ColormanageProcessor *cm_processor;
2872         ColorSpace *color_space;
2873
2874         cm_processor = MEM_callocN(sizeof(ColormanageProcessor), "colormanagement processor");
2875
2876         color_space = colormanage_colorspace_get_named(to_colorspace);
2877         cm_processor->is_data_result = color_space->is_data;
2878
2879         cm_processor->processor = create_colorspace_transform_processor(from_colorspace, to_colorspace);
2880
2881         return cm_processor;
2882 }
2883
2884 void IMB_colormanagement_processor_apply_v4(ColormanageProcessor *cm_processor, float pixel[4])
2885 {
2886         if (cm_processor->curve_mapping)
2887                 curvemapping_evaluate_premulRGBF(cm_processor->curve_mapping, pixel, pixel);
2888
2889         if (cm_processor->processor)
2890                 OCIO_processorApplyRGBA(cm_processor->processor, pixel);
2891 }
2892
2893 void IMB_colormanagement_processor_apply_v4_predivide(ColormanageProcessor *cm_processor, float pixel[4])
2894 {
2895         if (cm_processor->curve_mapping)
2896                 curvemapping_evaluate_premulRGBF(cm_processor->curve_mapping, pixel, pixel);
2897
2898         if (cm_processor->processor)
2899                 OCIO_processorApplyRGBA_predivide(cm_processor->processor, pixel);
2900 }
2901
2902 void IMB_colormanagement_processor_apply_v3(ColormanageProcessor *cm_processor, float pixel[3])
2903 {
2904         if (cm_processor->curve_mapping)
2905                 curvemapping_evaluate_premulRGBF(cm_processor->curve_mapping, pixel, pixel);
2906
2907         if (cm_processor->processor)
2908                 OCIO_processorApplyRGB(cm_processor->processor, pixel);
2909 }
2910
2911 void IMB_colormanagement_processor_apply(ColormanageProcessor *cm_processor, float *buffer, int width, int height,
2912                                          int channels, bool predivide)
2913 {
2914         /* apply curve mapping */
2915         if (cm_processor->curve_mapping) {
2916                 int x, y;
2917
2918                 for (y = 0; y < height; y++) {
2919                         for (x = 0; x < width; x++) {
2920                                 float *pixel = buffer + channels * (y * width + x);
2921
2922                                 curve_mapping_apply_pixel(cm_processor->curve_mapping, pixel, channels);
2923                         }
2924                 }
2925         }
2926
2927         if (cm_processor->processor && channels >= 3) {
2928                 OCIO_PackedImageDesc *img;
2929
2930                 /* apply OCIO processor */
2931                 img = OCIO_createOCIO_PackedImageDesc(buffer, width, height, channels, sizeof(float),
2932                                                       channels * sizeof(float), channels * sizeof(float) * width);
2933
2934                 if (predivide)
2935                         OCIO_processorApply_predivide(cm_processor->processor, img);
2936                 else
2937                         OCIO_processorApply(cm_processor->processor, img);
2938
2939                 OCIO_PackedImageDescRelease(img);
2940         }
2941 }
2942
2943 void IMB_colormanagement_processor_free(ColormanageProcessor *cm_processor)
2944 {
2945         if (cm_processor->curve_mapping)
2946                 curvemapping_free(cm_processor->curve_mapping);
2947         if (cm_processor->processor)
2948                 OCIO_processorRelease(cm_processor->processor);
2949
2950         MEM_freeN(cm_processor);
2951 }
2952
2953 /* **** OpenGL drawing routines using GLSL for color space transform ***** */
2954
2955 static bool check_glsl_display_processor_changed(const ColorManagedViewSettings *view_settings,
2956                                                  const ColorManagedDisplaySettings *display_settings,
2957                                                  const char *from_colorspace)
2958 {
2959         return !(global_glsl_state.exposure == view_settings->exposure &&
2960                  global_glsl_state.gamma == view_settings->gamma &&
2961                  STREQ(global_glsl_state.look, view_settings->look) &&
2962                  STREQ(global_glsl_state.view, view_settings->view_transform) &&
2963                  STREQ(global_glsl_state.display, display_settings->display_device) &&
2964                  STREQ(global_glsl_state.input, from_colorspace));
2965 }
2966
2967 static void curve_mapping_to_ocio_settings(CurveMapping *curve_mapping,
2968                                            OCIO_CurveMappingSettings *curve_mapping_settings)
2969 {
2970         int i;
2971
2972         curvemapping_initialize(curve_mapping);
2973         curvemapping_premultiply(curve_mapping, false);
2974         curvemapping_table_RGBA(curve_mapping,
2975                                 &curve_mapping_settings->lut,
2976                                 &curve_mapping_settings->lut_size);
2977
2978         for (i = 0; i < 4; i++) {
2979                 CurveMap *cuma = curve_mapping->cm + i;
2980                 curve_mapping_settings->use_extend_extrapolate[i] = (cuma->flag & CUMA_EXTEND_EXTRAPOLATE) != 0;
2981                 curve_mapping_settings->range[i] = cuma->range;
2982                 curve_mapping_settings->mintable[i] = cuma->mintable;
2983                 curve_mapping_settings->ext_in_x[i] = cuma->ext_in[0];
2984                 curve_mapping_settings->ext_in_y[i] = cuma->ext_in[1];
2985                 curve_mapping_settings->ext_out_x[i] = cuma->ext_out[0];
2986                 curve_mapping_settings->ext_out_y[i] = cuma->ext_out[1];
2987                 curve_mapping_settings->first_x[i] = cuma->table[0].x;
2988                 curve_mapping_settings->first_y[i] = cuma->table[0].y;
2989                 curve_mapping_settings->last_x[i] = cuma->table[CM_TABLE].x;
2990                 curve_mapping_settings->last_y[i] = cuma->table[CM_TABLE].y;
2991         }
2992
2993         copy_v3_v3(curve_mapping_settings->black, curve_mapping->black);
2994         copy_v3_v3(curve_mapping_settings->bwmul, curve_mapping->bwmul);
2995
2996         curve_mapping_settings->cache_id = (size_t) curve_mapping;
2997 }
2998
2999 static void update_glsl_display_processor(const ColorManagedViewSettings *view_settings,
3000                                           const ColorManagedDisplaySettings *display_settings,
3001                                           const char *from_colorspace)
3002 {
3003         bool use_curve_mapping = (view_settings->flag & COLORMANAGE_VIEW_USE_CURVES) != 0;
3004         bool need_update = false;
3005
3006         need_update = global_glsl_state.processor == NULL ||
3007                       check_glsl_display_processor_changed(view_settings, display_settings, from_colorspace) ||
3008                       use_curve_mapping != global_glsl_state.use_curve_mapping;
3009
3010         if (use_curve_mapping && need_update == false) {
3011                 need_update |= view_settings->curve_mapping->changed_timestamp != global_glsl_state.curve_mapping_timestamp ||
3012                                view_settings->curve_mapping != global_glsl_state.orig_curve_mapping;
3013         }
3014
3015         /* Update state if there's no processor yet or
3016          * processor settings has been changed.
3017          */
3018         if (need_update) {
3019                 OCIO_CurveMappingSettings *curve_mapping_settings = &global_glsl_state.curve_mapping_settings;
3020                 CurveMapping *new_curve_mapping = NULL;
3021
3022                 /* Store settings of processor for further comparison. */
3023                 BLI_strncpy(global_glsl_state.look, view_settings->look, MAX_COLORSPACE_NAME);
3024                 BLI_strncpy(global_glsl_state.view, view_settings->view_transform, MAX_COLORSPACE_NAME);
3025                 BLI_strncpy(global_glsl_state.display, display_settings->display_device, MAX_COLORSPACE_NAME);
3026                 BLI_strncpy(global_glsl_state.input, from_colorspace, MAX_COLORSPACE_NAME);
3027                 global_glsl_state.exposure = view_settings->exposure;
3028                 global_glsl_state.gamma = view_settings->gamma;
3029
3030                 /* We're using curve mapping's address as a acache ID,
3031                  * so we need to make sure re-allocation gives new address here.
3032                  * We do this by allocating new curve mapping before freeing ol one.
3033                  */
3034                 if (use_curve_mapping) {
3035                         new_curve_mapping = curvemapping_copy(view_settings->curve_mapping);
3036                 }
3037
3038                 if (global_glsl_state.curve_mapping) {
3039                         curvemapping_free(global_glsl_state.curve_mapping);
3040                         MEM_freeN(curve_mapping_settings->lut);
3041                         global_glsl_state.curve_mapping = NULL;
3042                         curve_mapping_settings->lut = NULL;
3043                 }
3044
3045                 /* Fill in OCIO's curve mapping settings. */
3046                 if (use_curve_mapping) {
3047                         curve_mapping_to_ocio_settings(new_curve_mapping, &global_glsl_state.curve_mapping_settings);
3048
3049                         global_glsl_state.curve_mapping = new_curve_mapping;
3050                         global_glsl_state.curve_mapping_timestamp = view_settings->curve_mapping->changed_timestamp;
3051                         global_glsl_state.orig_curve_mapping = view_settings->curve_mapping;
3052                         global_glsl_state.use_curve_mapping = true;
3053                 }
3054                 else {
3055                         global_glsl_state.orig_curve_mapping = NULL;
3056                         global_glsl_state.use_curve_mapping = false;
3057                 }
3058
3059                 /* Free old processor, if any. */
3060                 if (global_glsl_state.processor)
3061                         OCIO_processorRelease(global_glsl_state.processor);
3062
3063                 /* We're using display OCIO processor, no RGB curves yet. */
3064                 global_glsl_state.processor =
3065                         create_display_buffer_processor(global_glsl_state.look,
3066                                                         global_glsl_state.view,
3067                                                         global_glsl_state.display,
3068                                                         global_glsl_state.exposure,
3069                                                         global_glsl_state.gamma,
3070                                                         global_glsl_state.input);
3071         }
3072 }
3073
3074 bool IMB_colormanagement_support_glsl_draw(const ColorManagedViewSettings *UNUSED(view_settings))
3075 {
3076         return OCIO_supportGLSLDraw();
3077 }
3078
3079 /**
3080  * Configures GLSL shader for conversion from specified to
3081  * display color space
3082  *
3083  * Will create appropriate OCIO processor and setup GLSL shader,
3084  * so further 2D texture usage will use this conversion.
3085  *
3086  * When there's no need to apply transform on 2D textures, use
3087  * IMB_colormanagement_finish_glsl_draw().
3088  *
3089  * This is low-level function, use glaDrawImBuf_glsl_ctx if you
3090  * only need to display given image buffer
3091  */
3092 bool IMB_colormanagement_setup_glsl_draw_from_space(const ColorManagedViewSettings *view_settings,
3093                                                     const ColorManagedDisplaySettings *display_settings,
3094                                                     struct ColorSpace *from_colorspace,
3095                                                     float dither, bool predivide)
3096 {
3097         ColorManagedViewSettings default_view_settings;
3098         const ColorManagedViewSettings *applied_view_settings;
3099
3100         if (view_settings) {
3101                 applied_view_settings = view_settings;
3102         }
3103         else {
3104                 /* if no view settings were specified, use default display transformation
3105                  * this happens for images which don't want to be displayed with render settings
3106                  */
3107
3108                 init_default_view_settings(display_settings,  &default_view_settings);
3109                 applied_view_settings = &default_view_settings;
3110         }
3111
3112         /* Make sure OCIO processor is up-to-date. */
3113         update_glsl_display_processor(applied_view_settings, display_settings,
3114                                       from_colorspace ? from_colorspace->name : global_role_scene_linear);
3115
3116         return OCIO_setupGLSLDraw(&global_glsl_state.ocio_glsl_state, global_glsl_state.processor,
3117                                   global_glsl_state.use_curve_mapping ? &global_glsl_state.curve_mapping_settings : NULL,
3118                                   dither, predivide);
3119 }
3120
3121 /* Configures GLSL shader for conversion from scene linear to display space */
3122 bool IMB_colormanagement_setup_glsl_draw(const ColorManagedViewSettings *view_settings,
3123                                          const ColorManagedDisplaySettings *display_settings,
3124                                          float dither, bool predivide)
3125 {
3126         return IMB_colormanagement_setup_glsl_draw_from_space(view_settings, display_settings,
3127                                                               NULL, dither, predivide);
3128 }
3129
3130 /* Same as setup_glsl_draw_from_space, but color management settings are guessing from a given context */
3131 bool IMB_colormanagement_setup_glsl_draw_from_space_ctx(const bContext *C, struct ColorSpace *from_colorspace,
3132                                                         float dither, bool predivide)
3133 {
3134         ColorManagedViewSettings *view_settings;
3135         ColorManagedDisplaySettings *display_settings;
3136
3137         IMB_colormanagement_display_settings_from_ctx(C, &view_settings, &display_settings);
3138
3139         return IMB_colormanagement_setup_glsl_draw_from_space(view_settings, display_settings, from_colorspace,
3140                                                               dither, predivide);
3141 }
3142
3143 /* Same as setup_glsl_draw, but color management settings are guessing from a given context */
3144 bool IMB_colormanagement_setup_glsl_draw_ctx(const bContext *C, float dither, bool predivide)
3145 {
3146         return IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, NULL, dither, predivide);
3147 }
3148
3149 /* Finish GLSL-based display space conversion */
3150 void IMB_colormanagement_finish_glsl_draw(void)
3151 {
3152         OCIO_finishGLSLDraw(global_glsl_state.ocio_glsl_state);
3153 }