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