~ubuntu-branches/ubuntu/utopic/blender/utopic-proposed

« back to all changes in this revision

Viewing changes to source/blender/imbuf/intern/colormanagement.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2014-02-19 11:24:23 UTC
  • mfrom: (14.2.23 sid)
  • Revision ID: package-import@ubuntu.com-20140219112423-rkmaz2m7ha06d4tk
Tags: 2.69-3ubuntu1
* Merge with Debian; remaining changes:
  - Configure without OpenImageIO on armhf, as it is not available on
    Ubuntu.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 *
29
29
 */
30
30
 
 
31
/** \file blender/imbuf/intern/colormanagement.c
 
32
 *  \ingroup imbuf
 
33
 */
 
34
 
31
35
#include "IMB_colormanagement.h"
32
36
#include "IMB_colormanagement_intern.h"
33
37
 
81
85
static char global_role_default_float[MAX_COLORSPACE_NAME];
82
86
static char global_role_default_sequencer[MAX_COLORSPACE_NAME];
83
87
 
84
 
static ListBase global_colorspaces = {NULL};
85
 
static ListBase global_displays = {NULL};
86
 
static ListBase global_views = {NULL};
 
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};
87
92
 
88
93
static int global_tot_colorspace = 0;
89
94
static int global_tot_display = 0;
90
95
static int global_tot_view = 0;
 
96
static int global_tot_looks = 0;
91
97
 
92
98
/* lock used by pre-cached processors getters, so processor wouldn't
93
99
 * be created several times
99
105
typedef struct ColormanageProcessor {
100
106
        OCIO_ConstProcessorRcPtr *processor;
101
107
        CurveMapping *curve_mapping;
102
 
        int is_data_result;
 
108
        bool is_data_result;
103
109
} ColormanageProcessor;
104
110
 
105
111
static struct global_glsl_state {
107
113
        OCIO_ConstProcessorRcPtr *processor;
108
114
 
109
115
        /* Settings of processor for comparison. */
 
116
        char look[MAX_COLORSPACE_NAME];
110
117
        char view[MAX_COLORSPACE_NAME];
111
118
        char display[MAX_COLORSPACE_NAME];
112
119
        char input[MAX_COLORSPACE_NAME];
185
192
 */
186
193
typedef struct ColormanageCacheViewSettings {
187
194
        int flag;
 
195
        int look;
188
196
        int view;
189
197
        float exposure;
190
198
        float gamma;
 
199
        float dither;
191
200
        CurveMapping *curve_mapping;
192
201
} ColormanageCacheViewSettings;
193
202
 
202
211
 
203
212
typedef struct ColormnaageCacheData {
204
213
        int flag;        /* view flags of cached buffer */
 
214
        int look;        /* Additional artistics transform */
205
215
        float exposure;  /* exposure value cached buffer is calculated with */
206
216
        float gamma;     /* gamma value cached buffer is calculated with */
 
217
        float dither;    /* dither value cached buffer is calculated with */
207
218
        CurveMapping *curve_mapping;  /* curve mapping used for cached buffer */
208
219
        int curve_mapping_timestamp;  /* time stamp of curve mapping used for cached buffer */
209
220
} ColormnaageCacheData;
282
293
        ibuf->colormanage_cache->data = data;
283
294
}
284
295
 
285
 
static void colormanage_view_settings_to_cache(ColormanageCacheViewSettings *cache_view_settings,
 
296
static void colormanage_view_settings_to_cache(ImBuf *ibuf,
 
297
                                               ColormanageCacheViewSettings *cache_view_settings,
286
298
                                               const ColorManagedViewSettings *view_settings)
287
299
{
 
300
        int look = IMB_colormanagement_look_get_named_index(view_settings->look);
288
301
        int view = IMB_colormanagement_view_get_named_index(view_settings->view_transform);
289
302
 
 
303
        cache_view_settings->look = look;
290
304
        cache_view_settings->view = view;
291
305
        cache_view_settings->exposure = view_settings->exposure;
292
306
        cache_view_settings->gamma = view_settings->gamma;
 
307
        cache_view_settings->dither = ibuf->dither;
293
308
        cache_view_settings->flag = view_settings->flag;
294
309
        cache_view_settings->curve_mapping = view_settings->curve_mapping;
295
310
}
364
379
                 */
365
380
                cache_data = colormanage_cachedata_get(cache_ibuf);
366
381
 
367
 
                if (cache_data->exposure != view_settings->exposure ||
 
382
                if (cache_data->look != view_settings->look ||
 
383
                    cache_data->exposure != view_settings->exposure ||
368
384
                    cache_data->gamma != view_settings->gamma ||
 
385
                    cache_data->dither != view_settings->dither ||
369
386
                    cache_data->flag != view_settings->flag ||
370
387
                    cache_data->curve_mapping != curve_mapping ||
371
388
                    cache_data->curve_mapping_timestamp != curve_mapping_timestamp)
409
426
 
410
427
        /* store data which is needed to check whether cached buffer could be used for color managed display settings */
411
428
        cache_data = MEM_callocN(sizeof(ColormnaageCacheData), "color manage cache imbuf data");
 
429
        cache_data->look = view_settings->look;
412
430
        cache_data->exposure = view_settings->exposure;
413
431
        cache_data->gamma = view_settings->gamma;
 
432
        cache_data->dither = view_settings->dither;
414
433
        cache_data->flag = view_settings->flag;
415
434
        cache_data->curve_mapping = curve_mapping;
416
435
        cache_data->curve_mapping_timestamp = curve_mapping_timestamp;
454
473
 
455
474
static void colormanage_load_config(OCIO_ConstConfigRcPtr *config)
456
475
{
457
 
        int tot_colorspace, tot_display, tot_display_view, index, viewindex, viewindex2;
 
476
        int tot_colorspace, tot_display, tot_display_view, tot_looks;
 
477
        int index, viewindex, viewindex2;
458
478
        const char *name;
459
479
 
460
480
        /* get roles */
470
490
        for (index = 0 ; index < tot_colorspace; index++) {
471
491
                OCIO_ConstColorSpaceRcPtr *ocio_colorspace;
472
492
                const char *description;
473
 
                int is_invertible, is_data;
 
493
                bool is_invertible, is_data;
474
494
 
475
495
                name = OCIO_configGetColorSpaceNameByIndex(config, index);
476
496
 
519
539
        }
520
540
 
521
541
        global_tot_display = tot_display;
 
542
 
 
543
        /* load looks */
 
544
        tot_looks = OCIO_configGetNumLooks(config);
 
545
        colormanage_look_add("None", "", true);
 
546
        for (index = 0; index < tot_looks; index++) {
 
547
                OCIO_ConstLookRcPtr *ocio_look;
 
548
                const char *process_space;
 
549
 
 
550
                name = OCIO_configGetLookNameByIndex(config, index);
 
551
                ocio_look = OCIO_configGetLook(config, name);
 
552
                process_space = OCIO_lookGetProcessSpace(ocio_look);
 
553
                OCIO_lookRelease(ocio_look);
 
554
 
 
555
                colormanage_look_add(name, process_space, false);
 
556
        }
522
557
}
523
558
 
524
559
static void colormanage_free_config(void)
566
601
        /* free views */
567
602
        BLI_freelistN(&global_views);
568
603
 
 
604
        /* free looks */
 
605
        BLI_freelistN(&global_looks);
 
606
 
569
607
        OCIO_exit();
570
608
}
571
609
 
705
743
        return NULL;
706
744
}
707
745
 
708
 
static OCIO_ConstProcessorRcPtr *create_display_buffer_processor(const char *view_transform,
 
746
static OCIO_ConstProcessorRcPtr *create_display_buffer_processor(const char *look,
 
747
                                                                 const char *view_transform,
709
748
                                                                 const char *display,
710
749
                                                                 float exposure, float gamma,
711
750
                                                                 const char *from_colorspace)
713
752
        OCIO_ConstConfigRcPtr *config = OCIO_getCurrentConfig();
714
753
        OCIO_DisplayTransformRcPtr *dt;
715
754
        OCIO_ConstProcessorRcPtr *processor;
 
755
        ColorManagedLook *look_descr = colormanage_look_get_named(look);
716
756
 
717
757
        dt = OCIO_createDisplayTransform();
718
758
 
720
760
        OCIO_displayTransformSetView(dt, view_transform);
721
761
        OCIO_displayTransformSetDisplay(dt, display);
722
762
 
 
763
        if (look_descr->is_noop == false) {
 
764
                OCIO_displayTransformSetLooksOverrideEnabled(dt, true);
 
765
                OCIO_displayTransformSetLooksOverride(dt, look);
 
766
        }
 
767
 
723
768
        /* fstop exposure control */
724
769
        if (exposure != 0.0f) {
725
770
                OCIO_MatrixTransformRcPtr *mt;
871
916
        else
872
917
                view_settings->view_transform[0] = '\0';
873
918
 
 
919
        BLI_strncpy(view_settings->look, "None", sizeof(view_settings->look));
874
920
        view_settings->flag = 0;
875
921
        view_settings->gamma = 1.0f;
876
922
        view_settings->exposure = 0.0f;
923
969
                        imb_freerectImBuf(ibuf);
924
970
 
925
971
                IMB_colormanagement_transform(ibuf->rect_float, ibuf->x, ibuf->y, ibuf->channels,
926
 
                                              from_colorspace, to_colorspace, TRUE);
 
972
                                              from_colorspace, to_colorspace, true);
927
973
        }
928
974
}
929
975
 
953
999
{
954
1000
        ColorManagedDisplay *display;
955
1001
        ColorManagedView *default_view = NULL;
 
1002
        ColorManagedLook *default_look = (ColorManagedLook *) global_looks.first;
956
1003
 
957
1004
        if (view_settings->view_transform[0] == '\0') {
958
1005
                display = colormanage_display_get_named(display_settings->display_device);
981
1028
                }
982
1029
        }
983
1030
 
 
1031
        if (view_settings->look[0] == '\0') {
 
1032
                BLI_strncpy(view_settings->look, default_look->name, sizeof(view_settings->look));
 
1033
        }
 
1034
        else {
 
1035
                ColorManagedLook *look = colormanage_look_get_named(view_settings->look);
 
1036
                if (look == NULL) {
 
1037
                        printf("Color management: %s look \"%s\" not found, setting default \"%s\".\n",
 
1038
                               what, view_settings->look, default_look->name);
 
1039
 
 
1040
                        BLI_strncpy(view_settings->look, default_look->name, sizeof(view_settings->look));
 
1041
                }
 
1042
        }
 
1043
 
984
1044
        /* OCIO_TODO: move to do_versions() */
985
1045
        if (view_settings->exposure == 0.0f && view_settings->gamma == 0.0f) {
986
1046
                view_settings->exposure = 0.0f;
1094
1154
                default:
1095
1155
                        printf("Unknown role was passed to %s\n", __func__);
1096
1156
                        BLI_assert(0);
 
1157
                        break;
1097
1158
        }
1098
1159
 
1099
1160
        return NULL;
1133
1194
                ibuf->colormanage_flag &= ~IMB_COLORMANAGE_IS_DATA;
1134
1195
}
1135
1196
 
 
1197
const char *IMB_colormanagement_get_float_colorspace(ImBuf *ibuf)
 
1198
{
 
1199
        if (ibuf->float_colorspace) {
 
1200
                return ibuf->float_colorspace->name;
 
1201
        }
 
1202
        else {
 
1203
                return IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
 
1204
        }
 
1205
}
 
1206
 
 
1207
const char *IMB_colormanagement_get_rect_colorspace(ImBuf *ibuf)
 
1208
{
 
1209
        return ibuf->rect_colorspace->name;
 
1210
}
 
1211
 
1136
1212
/*********************** Threaded display buffer transform routines *************************/
1137
1213
 
1138
1214
typedef struct DisplayBufferThread {
1150
1226
 
1151
1227
        int channels;
1152
1228
        float dither;
1153
 
        int is_data;
 
1229
        bool is_data;
1154
1230
 
1155
1231
        const char *byte_colorspace;
1156
1232
        const char *float_colorspace;
1179
1255
 
1180
1256
        int channels = ibuf->channels;
1181
1257
        float dither = ibuf->dither;
1182
 
        int is_data = ibuf->colormanage_flag & IMB_COLORMANAGE_IS_DATA;
 
1258
        bool is_data = (ibuf->colormanage_flag & IMB_COLORMANAGE_IS_DATA) != 0;
1183
1259
 
1184
1260
        int offset = channels * start_line * ibuf->x;
1185
1261
        int display_buffer_byte_offset = DISPLAY_BUFFER_CHANNELS * start_line * ibuf->x;
1223
1299
 
1224
1300
        int buffer_size = channels * width * height;
1225
1301
 
1226
 
        int is_data = handle->is_data;
1227
 
        int is_data_display = handle->cm_processor->is_data_result;
 
1302
        bool is_data = handle->is_data;
 
1303
        bool is_data_display = handle->cm_processor->is_data_result;
1228
1304
 
1229
1305
        linear_buffer = MEM_callocN(buffer_size * sizeof(float), "color conversion linear buffer");
1230
1306
 
1257
1333
                if (!is_data && !is_data_display) {
1258
1334
                        /* convert float buffer to scene linear space */
1259
1335
                        IMB_colormanagement_transform(linear_buffer, width, height, channels,
1260
 
                                                      from_colorspace, to_colorspace, FALSE);
 
1336
                                                      from_colorspace, to_colorspace, false);
1261
1337
                }
1262
1338
 
1263
1339
                *is_straight_alpha = true;
1276
1352
 
1277
1353
                if (!is_data && !is_data_display) {
1278
1354
                        IMB_colormanagement_transform(linear_buffer, width, height, channels,
1279
 
                                                      from_colorspace, to_colorspace, TRUE);
 
1355
                                                      from_colorspace, to_colorspace, true);
1280
1356
                }
1281
1357
 
1282
1358
                *is_straight_alpha = false;
1309
1385
        int width = handle->width;
1310
1386
        int height = handle->tot_line;
1311
1387
        float dither = handle->dither;
1312
 
        int is_data = handle->is_data;
 
1388
        bool is_data = handle->is_data;
1313
1389
 
1314
1390
        if (cm_processor == NULL) {
1315
1391
                if (display_buffer_byte) {
1402
1478
                                     display_buffer_init_handle, do_display_buffer_apply_thread);
1403
1479
}
1404
1480
 
1405
 
static int is_ibuf_rect_in_display_space(ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
1406
 
                                         const ColorManagedDisplaySettings *display_settings)
 
1481
static bool is_ibuf_rect_in_display_space(ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
 
1482
                                          const ColorManagedDisplaySettings *display_settings)
1407
1483
{
1408
1484
        if ((view_settings->flag & COLORMANAGE_VIEW_USE_CURVES) == 0 &&
1409
1485
            view_settings->exposure == 0.0f &&
1413
1489
                const char *to_colorspace = display_transform_get_colorspace_name(view_settings, display_settings);
1414
1490
 
1415
1491
                if (to_colorspace && !strcmp(from_colorspace, to_colorspace))
1416
 
                        return TRUE;
 
1492
                        return true;
1417
1493
        }
1418
1494
 
1419
 
        return FALSE;
 
1495
        return false;
1420
1496
}
1421
1497
 
1422
1498
static void colormanage_display_buffer_process_ex(ImBuf *ibuf, float *display_buffer, unsigned char *display_buffer_byte,
1424
1500
                                                  const ColorManagedDisplaySettings *display_settings)
1425
1501
{
1426
1502
        ColormanageProcessor *cm_processor = NULL;
1427
 
        int skip_transform = FALSE;
 
1503
        bool skip_transform = false;
1428
1504
 
1429
1505
        /* if we're going to transform byte buffer, check whether transformation would
1430
1506
         * happen to the same color space as byte buffer itself is
1435
1511
                skip_transform = is_ibuf_rect_in_display_space(ibuf, view_settings, display_settings);
1436
1512
        }
1437
1513
 
1438
 
        if (skip_transform == FALSE)
 
1514
        if (skip_transform == false)
1439
1515
                cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
1440
1516
 
1441
1517
        display_buffer_apply_threaded(ibuf, ibuf->rect_float, (unsigned char *) ibuf->rect,
1461
1537
        int start_line;
1462
1538
        int tot_line;
1463
1539
        int channels;
1464
 
        int predivide;
 
1540
        bool predivide;
1465
1541
} ProcessorTransformThread;
1466
1542
 
1467
1543
typedef struct ProcessorTransformInit {
1470
1546
        int width;
1471
1547
        int height;
1472
1548
        int channels;
1473
 
        int predivide;
 
1549
        bool predivide;
1474
1550
} ProcessorTransformInitData;
1475
1551
 
1476
1552
static void processor_transform_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
1480
1556
 
1481
1557
        int channels = init_data->channels;
1482
1558
        int width = init_data->width;
1483
 
        int predivide = init_data->predivide;
 
1559
        bool predivide = init_data->predivide;
1484
1560
 
1485
1561
        int offset = channels * start_line * width;
1486
1562
 
1506
1582
        int channels = handle->channels;
1507
1583
        int width = handle->width;
1508
1584
        int height = handle->tot_line;
1509
 
        int predivide = handle->predivide;
 
1585
        bool predivide = handle->predivide;
1510
1586
 
1511
1587
        IMB_colormanagement_processor_apply(handle->cm_processor, buffer, width, height, channels, predivide);
1512
1588
 
1514
1590
}
1515
1591
 
1516
1592
static void processor_transform_apply_threaded(float *buffer, int width, int height, int channels,
1517
 
                                               ColormanageProcessor *cm_processor, int predivide)
 
1593
                                               ColormanageProcessor *cm_processor, bool predivide)
1518
1594
{
1519
1595
        ProcessorTransformInitData init_data;
1520
1596
 
1533
1609
 
1534
1610
/* convert the whole buffer from specified by name color space to another - internal implementation */
1535
1611
static void colormanagement_transform_ex(float *buffer, int width, int height, int channels, const char *from_colorspace,
1536
 
                                         const char *to_colorspace, int predivide, int do_threaded)
 
1612
                                         const char *to_colorspace, bool predivide, bool do_threaded)
1537
1613
{
1538
1614
        ColormanageProcessor *cm_processor;
1539
1615
 
1560
1636
 
1561
1637
/* convert the whole buffer from specified by name color space to another */
1562
1638
void IMB_colormanagement_transform(float *buffer, int width, int height, int channels,
1563
 
                                   const char *from_colorspace, const char *to_colorspace, int predivide)
 
1639
                                   const char *from_colorspace, const char *to_colorspace, bool predivide)
1564
1640
{
1565
 
        colormanagement_transform_ex(buffer, width, height, channels, from_colorspace, to_colorspace, predivide, FALSE);
 
1641
        colormanagement_transform_ex(buffer, width, height, channels, from_colorspace, to_colorspace, predivide, false);
1566
1642
}
1567
1643
 
1568
1644
/* convert the whole buffer from specified by name color space to another
1569
1645
 * will do threaded conversion
1570
1646
 */
1571
1647
void IMB_colormanagement_transform_threaded(float *buffer, int width, int height, int channels,
1572
 
                                            const char *from_colorspace, const char *to_colorspace, int predivide)
 
1648
                                            const char *from_colorspace, const char *to_colorspace, bool predivide)
1573
1649
{
1574
 
        colormanagement_transform_ex(buffer, width, height, channels, from_colorspace, to_colorspace, predivide, TRUE);
 
1650
        colormanagement_transform_ex(buffer, width, height, channels, from_colorspace, to_colorspace, predivide, true);
1575
1651
}
1576
1652
 
1577
1653
void IMB_colormanagement_transform_v4(float pixel[4], const char *from_colorspace, const char *to_colorspace)
1632
1708
                OCIO_processorApplyRGB(processor, pixel);
1633
1709
}
1634
1710
 
1635
 
void IMB_colormanagement_colorspace_to_scene_linear_v4(float pixel[4], int predivide, ColorSpace *colorspace)
 
1711
void IMB_colormanagement_colorspace_to_scene_linear_v4(float pixel[4], bool predivide, ColorSpace *colorspace)
1636
1712
{
1637
1713
        OCIO_ConstProcessorRcPtr *processor;
1638
1714
 
1652
1728
        }
1653
1729
}
1654
1730
 
1655
 
void IMB_colormanagement_colorspace_to_scene_linear(float *buffer, int width, int height, int channels, struct ColorSpace *colorspace, int predivide)
 
1731
void IMB_colormanagement_colorspace_to_scene_linear(float *buffer, int width, int height, int channels, struct ColorSpace *colorspace, bool predivide)
1656
1732
{
1657
1733
        OCIO_ConstProcessorRcPtr *processor;
1658
1734
 
1731
1807
}
1732
1808
 
1733
1809
static void colormanagement_imbuf_make_display_space(ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
1734
 
                                                     const ColorManagedDisplaySettings *display_settings, int make_byte)
 
1810
                                                     const ColorManagedDisplaySettings *display_settings, bool make_byte)
1735
1811
{
1736
1812
        if (!ibuf->rect && make_byte)
1737
1813
                imb_addrectImBuf(ibuf);
1749
1825
void IMB_colormanagement_imbuf_make_display_space(ImBuf *ibuf, const ColorManagedViewSettings *view_settings,
1750
1826
                                                  const ColorManagedDisplaySettings *display_settings)
1751
1827
{
1752
 
        colormanagement_imbuf_make_display_space(ibuf, view_settings, display_settings, FALSE);
 
1828
        colormanagement_imbuf_make_display_space(ibuf, view_settings, display_settings, false);
1753
1829
}
1754
1830
 
1755
1831
/* prepare image buffer to be saved on disk, applying color management if needed
1763
1839
 * in image format write callback and if float_colorspace is not NULL, no color
1764
1840
 * space transformation should be applied on this buffer.
1765
1841
 */
1766
 
ImBuf *IMB_colormanagement_imbuf_for_write(ImBuf *ibuf, int save_as_render, int allocate_result, const ColorManagedViewSettings *view_settings,
 
1842
ImBuf *IMB_colormanagement_imbuf_for_write(ImBuf *ibuf, bool save_as_render, bool allocate_result, const ColorManagedViewSettings *view_settings,
1767
1843
                                           const ColorManagedDisplaySettings *display_settings, ImageFormatData *image_format_data)
1768
1844
{
1769
1845
        ImBuf *colormanaged_ibuf = ibuf;
1770
 
        int do_colormanagement;
 
1846
        bool do_colormanagement;
1771
1847
        bool is_movie = BKE_imtype_is_movie(image_format_data->imtype);
1772
 
        int requires_linear_float = BKE_imtype_requires_linear_float(image_format_data->imtype);
1773
 
        int do_alpha_under = image_format_data->planes != R_IMF_PLANES_RGBA;
 
1848
        bool requires_linear_float = BKE_imtype_requires_linear_float(image_format_data->imtype);
 
1849
        bool do_alpha_under = image_format_data->planes != R_IMF_PLANES_RGBA;
1774
1850
 
1775
1851
        do_colormanagement = save_as_render && (is_movie || !requires_linear_float);
1776
1852
 
1826
1902
        }
1827
1903
 
1828
1904
        if (do_colormanagement) {
1829
 
                int make_byte = FALSE;
 
1905
                bool make_byte = false;
1830
1906
                ImFileType *type;
1831
1907
 
1832
1908
                /* for proper check whether byte buffer is required by a format or not
1842
1918
                for (type = IMB_FILE_TYPES; type->is_a; type++) {
1843
1919
                        if (type->save && type->ftype(type, colormanaged_ibuf)) {
1844
1920
                                if ((type->flag & IM_FTYPE_FLOAT) == 0)
1845
 
                                        make_byte = TRUE;
 
1921
                                        make_byte = true;
1846
1922
 
1847
1923
                                break;
1848
1924
                        }
1877
1953
        cm_processor = IMB_colormanagement_display_processor_new(view_settings, display_settings);
1878
1954
 
1879
1955
        processor_transform_apply_threaded(display_buffer_float, width, height, channels,
1880
 
                                           cm_processor, TRUE);
 
1956
                                           cm_processor, true);
1881
1957
 
1882
1958
        IMB_buffer_byte_from_float(display_buffer, display_buffer_float,
1883
1959
                                   channels, dither, IB_PROFILE_SRGB, IB_PROFILE_SRGB,
1954
2030
                                return (unsigned char *) ibuf->rect;
1955
2031
                }
1956
2032
 
1957
 
                colormanage_view_settings_to_cache(&cache_view_settings, applied_view_settings);
 
2033
                colormanage_view_settings_to_cache(ibuf, &cache_view_settings, applied_view_settings);
1958
2034
                colormanage_display_settings_to_cache(&cache_display_settings, display_settings);
1959
2035
 
1960
2036
                if (ibuf->invalid_rect.xmin != ibuf->invalid_rect.xmax) {
1963
2039
                                                                  ibuf->x, 0, 0, applied_view_settings, display_settings,
1964
2040
                                                                  ibuf->invalid_rect.xmin, ibuf->invalid_rect.ymin,
1965
2041
                                                                  ibuf->invalid_rect.xmax, ibuf->invalid_rect.ymax,
1966
 
                                                                  FALSE);
 
2042
                                                                  false);
1967
2043
                        }
1968
2044
 
1969
2045
                        BLI_rcti_init(&ibuf->invalid_rect, 0, 0, 0, 0);
2018
2094
 
2019
2095
void IMB_display_buffer_transform_apply(unsigned char *display_buffer, float *linear_buffer, int width, int height,
2020
2096
                                        int channels, const ColorManagedViewSettings *view_settings,
2021
 
                                        const ColorManagedDisplaySettings *display_settings, int predivide)
 
2097
                                        const ColorManagedDisplaySettings *display_settings, bool predivide)
2022
2098
{
2023
2099
        if (global_tot_display == 0 || global_tot_view == 0) {
2024
2100
                IMB_buffer_byte_from_float(display_buffer, linear_buffer, channels, 0.0f, IB_PROFILE_SRGB, IB_PROFILE_LINEAR_RGB, FALSE,
2280
2356
        }
2281
2357
}
2282
2358
 
2283
 
ColorSpace *colormanage_colorspace_add(const char *name, const char *description, int is_invertible, int is_data)
 
2359
ColorSpace *colormanage_colorspace_add(const char *name, const char *description, bool is_invertible, bool is_data)
2284
2360
{
2285
2361
        ColorSpace *colorspace, *prev_space;
2286
2362
        int counter = 1;
2341
2417
 
2342
2418
ColorSpace *colormanage_colorspace_get_indexed(int index)
2343
2419
{
2344
 
        /* display indices are 1-based */
 
2420
        /* color space indices are 1-based */
2345
2421
        return BLI_findlink(&global_colorspaces, index - 1);
2346
2422
}
2347
2423
 
2386
2462
        }
2387
2463
}
2388
2464
 
 
2465
/*********************** Looks functions *************************/
 
2466
 
 
2467
ColorManagedLook *colormanage_look_add(const char *name, const char *process_space, bool is_noop)
 
2468
{
 
2469
        ColorManagedLook *look;
 
2470
        int index = global_tot_looks;
 
2471
 
 
2472
        look = MEM_callocN(sizeof(ColorManagedLook), "ColorManagedLook");
 
2473
        look->index = index + 1;
 
2474
        BLI_strncpy(look->name, name, sizeof(look->name));
 
2475
        BLI_strncpy(look->process_space, process_space, sizeof(look->process_space));
 
2476
        look->is_noop = is_noop;
 
2477
 
 
2478
        BLI_addtail(&global_looks, look);
 
2479
 
 
2480
        global_tot_looks++;
 
2481
 
 
2482
        return look;
 
2483
}
 
2484
 
 
2485
ColorManagedLook *colormanage_look_get_named(const char *name)
 
2486
{
 
2487
        ColorManagedLook *look;
 
2488
 
 
2489
        for (look = global_looks.first; look; look = look->next) {
 
2490
                if (!strcmp(look->name, name)) {
 
2491
                        return look;
 
2492
                }
 
2493
        }
 
2494
 
 
2495
        return NULL;
 
2496
}
 
2497
 
 
2498
ColorManagedLook *colormanage_look_get_indexed(int index)
 
2499
{
 
2500
        /* look indices are 1-based */
 
2501
        return BLI_findlink(&global_looks, index - 1);
 
2502
}
 
2503
 
 
2504
int IMB_colormanagement_look_get_named_index(const char *name)
 
2505
{
 
2506
        ColorManagedLook *look;
 
2507
 
 
2508
        look = colormanage_look_get_named(name);
 
2509
 
 
2510
        if (look) {
 
2511
                return look->index;
 
2512
        }
 
2513
 
 
2514
        return 0;
 
2515
}
 
2516
 
 
2517
const char *IMB_colormanagement_look_get_indexed_name(int index)
 
2518
{
 
2519
        ColorManagedLook *look;
 
2520
 
 
2521
        look = colormanage_look_get_indexed(index);
 
2522
 
 
2523
        if (look) {
 
2524
                return look->name;
 
2525
        }
 
2526
 
 
2527
        return NULL;
 
2528
}
 
2529
 
2389
2530
/*********************** RNA helper functions *************************/
2390
2531
 
2391
2532
void IMB_colormanagement_display_items_add(EnumPropertyItem **items, int *totitem)
2434
2575
        }
2435
2576
}
2436
2577
 
 
2578
void IMB_colormanagement_look_items_add(struct EnumPropertyItem **items, int *totitem)
 
2579
{
 
2580
        ColorManagedLook *look;
 
2581
 
 
2582
        for (look = global_looks.first; look; look = look->next) {
 
2583
                EnumPropertyItem item;
 
2584
 
 
2585
                item.value = look->index;
 
2586
                item.name = look->name;
 
2587
                item.identifier = look->name;
 
2588
                item.icon = 0;
 
2589
                item.description = "";
 
2590
 
 
2591
                RNA_enum_item_add(items, totitem, &item);
 
2592
        }
 
2593
}
 
2594
 
2437
2595
void IMB_colormanagement_colorspace_items_add(EnumPropertyItem **items, int *totitem)
2438
2596
{
2439
2597
        ColorSpace *colorspace;
2479
2637
        float *display_buffer_float = NULL;
2480
2638
        const int width = xmax - xmin;
2481
2639
        const int height = ymax - ymin;
2482
 
        int is_data = ibuf->colormanage_flag & IMB_COLORMANAGE_IS_DATA;
 
2640
        bool is_data = (ibuf->colormanage_flag & IMB_COLORMANAGE_IS_DATA) != 0;
2483
2641
 
2484
2642
        if (dither != 0.0f) {
2485
2643
                /* cm_processor is NULL in cases byte_buffer's space matches display
2560
2718
void IMB_partial_display_buffer_update(ImBuf *ibuf, const float *linear_buffer, const unsigned char *byte_buffer,
2561
2719
                                       int stride, int offset_x, int offset_y, const ColorManagedViewSettings *view_settings,
2562
2720
                                       const ColorManagedDisplaySettings *display_settings,
2563
 
                                       int xmin, int ymin, int xmax, int ymax, int update_orig_byte_buffer)
 
2721
                                       int xmin, int ymin, int xmax, int ymax, bool update_orig_byte_buffer)
2564
2722
{
2565
2723
        if ((ibuf->rect && ibuf->rect_float) || update_orig_byte_buffer) {
2566
2724
                /* update byte buffer created by legacy color management */
2583
2741
                unsigned char *display_buffer = NULL;
2584
2742
                int view_flag, display_index, buffer_width;
2585
2743
 
2586
 
                colormanage_view_settings_to_cache(&cache_view_settings, view_settings);
 
2744
                colormanage_view_settings_to_cache(ibuf, &cache_view_settings, view_settings);
2587
2745
                colormanage_display_settings_to_cache(&cache_display_settings, display_settings);
2588
2746
 
2589
2747
                view_flag = 1 << (cache_view_settings.view - 1);
2607
2765
 
2608
2766
                if (display_buffer) {
2609
2767
                        ColormanageProcessor *cm_processor = NULL;
2610
 
                        int skip_transform = 0;
 
2768
                        bool skip_transform = false;
2611
2769
 
2612
2770
                        /* byte buffer is assumed to be in imbuf's rect space, so if byte buffer
2613
2771
                         * is known we could skip display->linear->display conversion in case
2666
2824
        if (display_space)
2667
2825
                cm_processor->is_data_result = display_space->is_data;
2668
2826
 
2669
 
        cm_processor->processor = create_display_buffer_processor(applied_view_settings->view_transform, display_settings->display_device,
2670
 
                                                                  applied_view_settings->exposure, applied_view_settings->gamma,
 
2827
        cm_processor->processor = create_display_buffer_processor(applied_view_settings->look,
 
2828
                                                                  applied_view_settings->view_transform,
 
2829
                                                                  display_settings->display_device,
 
2830
                                                                  applied_view_settings->exposure,
 
2831
                                                                  applied_view_settings->gamma,
2671
2832
                                                                  global_role_scene_linear);
2672
2833
 
2673
2834
        if (applied_view_settings->flag & COLORMANAGE_VIEW_USE_CURVES) {
2674
2835
                cm_processor->curve_mapping = curvemapping_copy(applied_view_settings->curve_mapping);
2675
 
                curvemapping_premultiply(cm_processor->curve_mapping, FALSE);
 
2836
                curvemapping_premultiply(cm_processor->curve_mapping, false);
2676
2837
        }
2677
2838
 
2678
2839
        return cm_processor;
2721
2882
}
2722
2883
 
2723
2884
void IMB_colormanagement_processor_apply(ColormanageProcessor *cm_processor, float *buffer, int width, int height,
2724
 
                                         int channels, int predivide)
 
2885
                                         int channels, bool predivide)
2725
2886
{
2726
2887
        /* apply curve mapping */
2727
2888
        if (cm_processor->curve_mapping) {
2770
2931
{
2771
2932
        return !(global_glsl_state.exposure == view_settings->exposure &&
2772
2933
                 global_glsl_state.gamma == view_settings->gamma &&
 
2934
                 STREQ(global_glsl_state.look, view_settings->look) &&
2773
2935
                 STREQ(global_glsl_state.view, view_settings->view_transform) &&
2774
2936
                 STREQ(global_glsl_state.display, display_settings->display_device) &&
2775
2937
                 STREQ(global_glsl_state.input, from_colorspace));
2786
2948
            check_glsl_display_processor_changed(view_settings, display_settings, from_colorspace))
2787
2949
        {
2788
2950
                /* Store settings of processor for further comparison. */
2789
 
                strcpy(global_glsl_state.view, view_settings->view_transform);
2790
 
                strcpy(global_glsl_state.display, display_settings->display_device);
2791
 
                strcpy(global_glsl_state.input, from_colorspace);
 
2951
                BLI_strncpy(global_glsl_state.look, view_settings->look, MAX_COLORSPACE_NAME);
 
2952
                BLI_strncpy(global_glsl_state.view, view_settings->view_transform, MAX_COLORSPACE_NAME);
 
2953
                BLI_strncpy(global_glsl_state.display, display_settings->display_device, MAX_COLORSPACE_NAME);
 
2954
                BLI_strncpy(global_glsl_state.input, from_colorspace, MAX_COLORSPACE_NAME);
2792
2955
                global_glsl_state.exposure = view_settings->exposure;
2793
2956
                global_glsl_state.gamma = view_settings->gamma;
2794
2957
 
2798
2961
 
2799
2962
                /* We're using display OCIO processor, no RGB curves yet. */
2800
2963
                global_glsl_state.processor =
2801
 
                        create_display_buffer_processor(global_glsl_state.view,
 
2964
                        create_display_buffer_processor(global_glsl_state.look,
 
2965
                                                        global_glsl_state.view,
2802
2966
                                                        global_glsl_state.display,
2803
2967
                                                        global_glsl_state.exposure,
2804
2968
                                                        global_glsl_state.gamma,
2806
2970
        }
2807
2971
}
2808
2972
 
 
2973
bool IMB_colormanagement_support_glsl_draw(const ColorManagedViewSettings *view_settings,
 
2974
                                           bool skip_curves)
 
2975
{
 
2976
        /* curves not supported yet */
 
2977
        if (!skip_curves)
 
2978
                if (view_settings && (view_settings->flag & COLORMANAGE_VIEW_USE_CURVES))
 
2979
                        return 0;
 
2980
 
 
2981
        return OCIO_supportGLSLDraw();
 
2982
}
 
2983
 
2809
2984
/**
2810
2985
 * Configures GLSL shader for conversion from specified to
2811
2986
 * display color space
2819
2994
 * This is low-level function, use glaDrawImBuf_glsl_ctx if you
2820
2995
 * only need to display given image buffer
2821
2996
 */
2822
 
int IMB_colormanagement_setup_glsl_draw_from_space(const ColorManagedViewSettings *view_settings,
2823
 
                                                   const ColorManagedDisplaySettings *display_settings,
2824
 
                                                   struct ColorSpace *from_colorspace, int predivide)
 
2997
bool IMB_colormanagement_setup_glsl_draw_from_space(const ColorManagedViewSettings *view_settings,
 
2998
                                                    const ColorManagedDisplaySettings *display_settings,
 
2999
                                                    struct ColorSpace *from_colorspace, bool predivide,
 
3000
                                                    bool skip_curves)
2825
3001
{
2826
3002
        ColorManagedViewSettings default_view_settings;
2827
3003
        const ColorManagedViewSettings *applied_view_settings;
2839
3015
        }
2840
3016
 
2841
3017
        /* RGB curves mapping is not supported on GPU yet. */
2842
 
        if (applied_view_settings->flag & COLORMANAGE_VIEW_USE_CURVES)
2843
 
                return FALSE;
 
3018
        if (!skip_curves)
 
3019
                if (applied_view_settings->flag & COLORMANAGE_VIEW_USE_CURVES)
 
3020
                        return false;
2844
3021
 
2845
3022
        /* Make sure OCIO processor is up-to-date. */
2846
3023
        update_glsl_display_processor(applied_view_settings, display_settings,
2850
3027
}
2851
3028
 
2852
3029
/* Configures GLSL shader for conversion from scene linear to display space */
2853
 
int IMB_colormanagement_setup_glsl_draw(const ColorManagedViewSettings *view_settings,
2854
 
                                        const ColorManagedDisplaySettings *display_settings,
2855
 
                                        int predivide)
 
3030
bool IMB_colormanagement_setup_glsl_draw(const ColorManagedViewSettings *view_settings,
 
3031
                                         const ColorManagedDisplaySettings *display_settings,
 
3032
                                         bool predivide, bool skip_curves)
2856
3033
{
2857
3034
        return IMB_colormanagement_setup_glsl_draw_from_space(view_settings, display_settings,
2858
 
                                                              NULL, predivide);
 
3035
                                                              NULL, predivide, skip_curves);
2859
3036
}
2860
3037
 
2861
3038
/* Same as setup_glsl_draw_from_space, but color management settings are guessing from a given context */
2862
 
int IMB_colormanagement_setup_glsl_draw_from_space_ctx(const struct bContext *C, struct ColorSpace *from_colorspace, int predivide)
 
3039
bool IMB_colormanagement_setup_glsl_draw_from_space_ctx(const struct bContext *C, struct ColorSpace *from_colorspace, bool predivide)
2863
3040
{
2864
3041
        ColorManagedViewSettings *view_settings;
2865
3042
        ColorManagedDisplaySettings *display_settings;
2866
3043
 
2867
3044
        IMB_colormanagement_display_settings_from_ctx(C, &view_settings, &display_settings);
2868
3045
 
2869
 
        return IMB_colormanagement_setup_glsl_draw_from_space(view_settings, display_settings, from_colorspace, predivide);
 
3046
        return IMB_colormanagement_setup_glsl_draw_from_space(view_settings, display_settings, from_colorspace, predivide, false);
2870
3047
}
2871
3048
 
2872
3049
/* Same as setup_glsl_draw, but color management settings are guessing from a given context */
2873
 
int IMB_colormanagement_setup_glsl_draw_ctx(const bContext *C, int predivide)
 
3050
bool IMB_colormanagement_setup_glsl_draw_ctx(const bContext *C, bool predivide)
2874
3051
{
2875
3052
        return IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, NULL, predivide);
2876
3053
}
2896
3073
 * When there's no need to apply transform on 2D textures, use
2897
3074
 * IMB_colormanagement_finish_glsl_transform().
2898
3075
 */
2899
 
int IMB_colormanagement_setup_transform_from_role_glsl(int role, int predivide)
 
3076
bool IMB_colormanagement_setup_transform_from_role_glsl(int role, bool predivide)
2900
3077
{
2901
3078
        OCIO_ConstProcessorRcPtr *processor;
2902
3079
        ColorSpace *colorspace;