aboutsummaryrefslogblamecommitdiffstats
path: root/libclut.h
blob: 2a655924f1770d42d64f4c9098a8000b00dfe619 (plain) (tree)
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
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
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







                                                         
 


































































































































































































































































































































































































































































































































































































































































































































                                                                                                                      
                                                                         

                                                              
                                            





                                                                      
                                                                         

                                                              
                                            





                                                                      
                                                      











                                                                                   
                                                                         

                                                              
                                            





                                                                      
                                                                         

                                                              
                                            





                                                                      

                                                                                   










                                                                      
                                                                         

                                                              
                                            





                                                                      
                                                                         

                                                              
                                            





                                                                      
                                                       




























































                                                                                                                    


                                                                               

































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                    





                                                                                                          

















































































































































































































































































































































                                                                                                                                 





                                                                                                                      




























































                                                                                                                                   
/* See LICENSE file for copyright and license details. */
#ifndef LIBCLUT_H
#define LIBCLUT_H

#include <stddef.h>
#include <string.h>
#include <math.h>


/* Not documented, may be removed or modified in the future. */
#define LIBCLUT_ILLUMINANT_A    .white_x = 0.44757, .white_y = 0.40745, .white_Y = 1
#define LIBCLUT_ILLUMINANT_B    .white_x = 0.34842, .white_y = 0.35161, .white_Y = 1
#define LIBCLUT_ILLUMINANT_C    .white_x = 0.31006, .white_y = 0.31616, .white_Y = 1
#define LIBCLUT_ILLUMINANT_D50  .white_x = 0.34567, .white_y = 0.35850, .white_Y = 1
#define LIBCLUT_ILLUMINANT_D55  .white_x = 0.33242, .white_y = 0.34743, .white_Y = 1
#define LIBCLUT_ILLUMINANT_D65  .white_x = 0.31271, .white_y = 0.32902, .white_Y = 1
#define LIBCLUT_ILLUMINANT_D75  .white_x = 0.29902, .white_y = 0.31485, .white_Y = 1
#define LIBCLUT_ILLUMINANT_E    .white_x = 1. / 3,  .white_y = 1. / 3,  .white_Y = 1
#define LIBCLUT_ILLUMINANT_F1   .white_x = 0.31310, .white_y = 0.33727, .white_Y = 1
#define LIBCLUT_ILLUMINANT_F2   .white_x = 0.37208, .white_y = 0.37529, .white_Y = 1
#define LIBCLUT_ILLUMINANT_F3   .white_x = 0.40910, .white_y = 0.39430, .white_Y = 1
#define LIBCLUT_ILLUMINANT_F4   .white_x = 0.44018, .white_y = 0.40329, .white_Y = 1
#define LIBCLUT_ILLUMINANT_F5   .white_x = 0.31379, .white_y = 0.34531, .white_Y = 1
#define LIBCLUT_ILLUMINANT_F6   .white_x = 0.37790, .white_y = 0.38835, .white_Y = 1
#define LIBCLUT_ILLUMINANT_F7   .white_x = 0.31292, .white_y = 0.32933, .white_Y = 1
#define LIBCLUT_ILLUMINANT_F8   .white_x = 0.34588, .white_y = 0.35875, .white_Y = 1
#define LIBCLUT_ILLUMINANT_F9   .white_x = 0.37417, .white_y = 0.37281, .white_Y = 1
#define LIBCLUT_ILLUMINANT_F10  .white_x = 0.34609, .white_y = 0.35986, .white_Y = 1
#define LIBCLUT_ILLUMINANT_F11  .white_x = 0.38052, .white_y = 0.37713, .white_Y = 1
#define LIBCLUT_ILLUMINANT_F12  .white_x = 0.43695, .white_y = 0.40441, .white_Y = 1

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the sRGB colour space
 * 
 * sRGB does not use a regular gamma function, but rather two different
 * functions. `libclut_model_linear_to_standard1` and
 * and `libclut_model_standard_to_linear1` can be used so to convert
 * between sRGB and linear sRGB
 */
#define LIBCLUT_RGB_COLOUR_SPACE_SRGB_INITIALISER {\
	.red_x   = 0.6400, .red_y   = 0.3300, .red_Y   = 0.212656,\
	.green_x = 0.3000, .green_y = 0.6000, .green_Y = 0.715158,\
	.blue_x  = 0.1500, .blue_y  = 0.0600, .blue_Y  = 0.072186,\
	LIBCLUT_ILLUMINANT_D65}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the Adobe RGB (1998) colour space
 * 
 * This colour space's gamma is 2.2. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_ADOBE_RGB_INITIALISER {\
	.red_x   = 0.6400, .red_y   = 0.3300, .red_Y   = 0.297361,\
	.green_x = 0.2100, .green_y = 0.7100, .green_Y = 0.627355,\
	.blue_x  = 0.1500, .blue_y  = 0.0600, .blue_Y  = 0.075285,\
	LIBCLUT_ILLUMINANT_D65}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the Apple RGB colour space
 * 
 * This colour space's gamma is 1.8. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_APPLE_RGB_INITIALISER {\
	.red_x   = 0.6250, .red_y   = 0.3400, .red_Y   = 0.244634,\
	.green_x = 0.2800, .green_y = 0.5950, .green_Y = 0.672034,\
	.blue_x  = 0.1550, .blue_y  = 0.0700, .blue_Y  = 0.083332,\
	LIBCLUT_ILLUMINANT_D65}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the Best RGB colour space
 * 
 * This colour space's gamma is 2.2. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_BEST_RGB_INITIALISER {\
	.red_x   = 0.7347, .red_y   = 0.2653, .red_Y   = 0.228457,\
	.green_x = 0.2150, .green_y = 0.7750, .green_Y = 0.737352,\
	.blue_x  = 0.1300, .blue_y  = 0.0350, .blue_Y  = 0.034191,\
	LIBCLUT_ILLUMINANT_D50}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the Beta RGB colour space
 * 
 * This colour space's gamma is 2.2. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_BETA_RGB_INITIALISER {\
	.red_x   = 0.6888, .red_y   = 0.3112, .red_Y   = 0.303273,\
	.green_x = 0.1986, .green_y = 0.7551, .green_Y = 0.663786,\
	.blue_x  = 0.1265, .blue_y  = 0.0352, .blue_Y  = 0.032941,\
	LIBCLUT_ILLUMINANT_D50}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the Bruce RGB colour space
 * 
 * This colour space's gamma is 2.2. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_BRUCE_RGB_INITIALISER {\
	.red_x   = 0.6400, .red_y   = 0.3300, .red_Y   = 0.240995,\
	.green_x = 0.2800, .green_y = 0.6500, .green_Y = 0.683554,\
	.blue_x  = 0.1500, .blue_y  = 0.0600, .blue_Y  = 0.075452,\
	LIBCLUT_ILLUMINANT_D65}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the CIE RGB colour space
 * 
 * This colour space's gamma is 2.2. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_CIE_RGB_INITIALISER {\
	.red_x   = 0.7350, .red_y   = 0.2650, .red_Y   = 0.176204,\
	.green_x = 0.2740, .green_y = 0.7170, .green_Y = 0.812985,\
	.blue_x  = 0.1670, .blue_y  = 0.0090, .blue_Y  = 0.010811,\
	LIBCLUT_ILLUMINANT_E}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the ColorMatch RGB colour space
 * 
 * This colour space's gamma is 1.8. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_COLORMATCH_RGB_INITIALISER {\
	.red_x   = 0.6300, .red_y   = 0.3400, .red_Y   = 0.274884,\
	.green_x = 0.2950, .green_y = 0.6050, .green_Y = 0.658132,\
	.blue_x  = 0.1500, .blue_y  = 0.0750, .blue_Y  = 0.066985,\
	LIBCLUT_ILLUMINANT_D50}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the DCI-P3 D65 colour space
 * 
 * This colour space's gamma is 2.6. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_DCI_P3_D65_INITIALISER {\
	.red_x   = 0.680, .red_y   = 0.320, .red_Y   = 0.22897344,\
	.green_x = 0.265, .green_y = 0.690, .green_Y = 0.69175166,\
	.blue_x  = 0.150, .blue_y  = 0.060, .blue_Y  = 0.07927490,\
	LIBCLUT_ILLUMINANT_D50}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the DCI-P3 Theater colour space
 * 
 * This colour space's gamma is 2.6. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_DCI_P3_THEATER_INITIALISER {\
	.red_x   = 0.680, .red_y   = 0.320, .red_Y   = 0.20949168,\
	.green_x = 0.265, .green_y = 0.690, .green_Y = 0.72159525,\
	.blue_x  = 0.150, .blue_y  = 0.060, .blue_Y  = 0.06891307,\
	.white_x = 0.314, .white_y = 0.351, .white_Y = 1}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the Don RGB 4 colour space
 * 
 * This colour space's gamma is 2.2. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_DON_RGB_4_INITIALISER {\
	.red_x   = 0.6960, .red_y   = 0.3000, .red_Y   = 0.278350,\
	.green_x = 0.2150, .green_y = 0.7650, .green_Y = 0.687970,\
	.blue_x  = 0.1300, .blue_y  = 0.0350, .blue_Y  = 0.033680,\
	LIBCLUT_ILLUMINANT_D50}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the ECI RGB v2 colour space
 * 
 * This colour space's used the L* gamma function. It cannot
 * be used with RGB colour space conversion unless the values are
 * tranlated into using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_ECI_RGB_V2_INITIALISER {\
	.red_x   = 0.6700, .red_y   = 0.3300, .red_Y   = 0.320250,\
	.green_x = 0.2100, .green_y = 0.7100, .green_Y = 0.602071,\
	.blue_x  = 0.1400, .blue_y  = 0.0800, .blue_Y  = 0.077679,\
	LIBCLUT_ILLUMINANT_D50}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the Ekta Space PS5 colour space
 * 
 * This colour space's gamma is 2.2. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_EKTA_SPACE_PS5_INITIALISER {\
	.red_x   = 0.6950, .red_y   = 0.3050, .red_Y   = 0.260629,\
	.green_x = 0.2600, .green_y = 0.7000, .green_Y = 0.734946,\
	.blue_x  = 0.1100, .blue_y  = 0.0050, .blue_Y  = 0.004425,\
	LIBCLUT_ILLUMINANT_D50}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the ITU-R Recommendation BT.601 (ITU-R BT.601), 625 line colour
 * space
 * 
 * This colour space uses a custom gamma function. It cannot be used
 * with RGB colour space conversion unless the values are tranlated
 * into using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_ITU_R_BT_601_625_LINE_INITIALISER {\
	.red_x   = 0.640, .red_y   = 0.330, .red_Y   = 0.2220023,\
	.green_x = 0.290, .green_y = 0.600, .green_Y = 0.7066689,\
	.blue_x  = 0.150, .blue_y  = 0.060, .blue_Y  = 0.0713288,\
	LIBCLUT_ILLUMINANT_D65}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the ITU-R Recommendation BT.601 (ITU-R BT.601), 525 line colour
 * space
 * 
 * This colour space uses a custom gamma function. It cannot be used
 * with RGB colour space conversion unless the values are tranlated
 * into using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_ITU_R_BT_601_525_LINE_INITIALISER {\
	.red_x   = 0.630, .red_y   = 0.340, .red_Y   = 0.2220023,\
	.green_x = 0.310, .green_y = 0.595, .green_Y = 0.7066689,\
	.blue_x  = 0.155, .blue_y  = 0.070, .blue_Y  = 0.0713288,\
	LIBCLUT_ILLUMINANT_D65}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the ITU-R Recommendation BT.709 (ITU-R BT.709) colour space
 * 
 * This colour space's gamma is 2._. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_ITU_R_BT_709_INITIALISER {\
	.red_x   = 0.6400, .red_y   = 0.3300, .red_Y   = 0.212656,\
	.green_x = 0.3000, .green_y = 0.6000, .green_Y = 0.715158,\
	.blue_x  = 0.1500, .blue_y  = 0.0600, .blue_Y  = 0.072186,\
	LIBCLUT_ILLUMINANT_D65}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the ITU-R Recommendation BT.2020 (ITU-R BT.2020) colour space
 * 
 * This colour space uses a custom gamma function. It cannot be used
 * with RGB colour space conversion unless the values are tranlated
 * into using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_ITU_R_BT_2020_INITIALISER {\
	.red_x   = 0.7080, .red_y   = 0.2920, .red_Y   = 0.2627296,\
	.green_x = 0.1700, .green_y = 0.7970, .green_Y = 0.6767483,\
	.blue_x  = 0.1310, .blue_y  = 0.0460, .blue_Y  = 0.0605221,\
	LIBCLUT_ILLUMINANT_D65}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the ITU-R Recommendation BT.2100 (ITU-R BT.2100) colour space
 * 
 * This colour space uses a custom gamma function. It cannot be used
 * with RGB colour space conversion unless the values are tranlated
 * into using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_ITU_R_BT_2100_INITIALISER {\
	.red_x   = 0.7080, .red_y   = 0.2920, .red_Y   = 0.2627296,\
	.green_x = 0.1700, .green_y = 0.7970, .green_Y = 0.6767483,\
	.blue_x  = 0.1310, .blue_y  = 0.0460, .blue_Y  = 0.0605221,\
	LIBCLUT_ILLUMINANT_D65}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the Lightroom RGB colour space
 * 
 * This colour space's gamma is 1 (linear). It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_LIGHTROOM_RGB_INITIALISER {\
	.red_x   = 0.7347, .red_y   = 0.2653, .red_Y   = 0.288040,\
	.green_x = 0.1596, .green_y = 0.8404, .green_Y = 0.711874,\
	.blue_x  = 0.0366, .blue_y  = 0.0001, .blue_Y  = 0.000086,\
	LIBCLUT_ILLUMINANT_D50}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the NTSC RGB colour space
 * 
 * This colour space's gamma is 2.2. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_NTSC_RGB_INITIALISER {\
	.red_x   = 0.6700, .red_y   = 0.3300, .red_Y   = 0.298839,\
	.green_x = 0.2100, .green_y = 0.7100, .green_Y = 0.586811,\
	.blue_x  = 0.1400, .blue_y  = 0.0800, .blue_Y  = 0.114350,\
	LIBCLUT_ILLUMINANT_C}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the PAL/SECAM RGB colour space
 * 
 * This colour space's gamma is 2.2. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_PAL_SECAM_RGB_INITIALISER {\
	.red_x   = 0.6400, .red_y   = 0.3300, .red_Y   = 0.222021,\
	.green_x = 0.2900, .green_y = 0.6000, .green_Y = 0.706645,\
	.blue_x  = 0.1500, .blue_y  = 0.0600, .blue_Y  = 0.071334,\
	LIBCLUT_ILLUMINANT_D65}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the ProPhoto RGB colour space
 * 
 * This colour space's gamma is 1.8. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_PROPHOTO_RGB_INITIALISER {\
	.red_x   = 0.7347, .red_y   = 0.2653, .red_Y   = 0.288040,\
	.green_x = 0.1596, .green_y = 0.8404, .green_Y = 0.711874,\
	.blue_x  = 0.0366, .blue_y  = 0.0001, .blue_Y  = 0.000086,\
	LIBCLUT_ILLUMINANT_D50}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the SMPTE-C RGB colour space
 * 
 * This colour space's gamma is 2.2. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_SMPTE_C_RGB_INITIALISER {\
	.red_x   = 0.6300, .red_y   = 0.3400, .red_Y   = 0.212395,\
	.green_x = 0.3100, .green_y = 0.5950, .green_Y = 0.701049,\
	.blue_x  = 0.1550, .blue_y  = 0.0700, .blue_Y  = 0.086556,\
	LIBCLUT_ILLUMINANT_D65}

/**
 * Initialiser for `struct libclut_rgb_colour_space` with the values
 * of the Wide Gamut RGB colour space
 * 
 * This colour space's gamma is 2.2. It cannot be used with
 * RGB colour space conversion unless the values are tranlated into
 * using the sRGB gamma function.
 */
#define LIBCLUT_RGB_COLOUR_SPACE_WIDE_GAMUT_RGB_INITIALISER {\
	.red_x   = 0.7350, .red_y   = 0.2650, .red_Y   = 0.258187,\
	.green_x = 0.1150, .green_y = 0.8260, .green_Y = 0.724938,\
	.blue_x  = 0.1570, .blue_y  = 0.0180, .blue_Y  = 0.016875,\
	LIBCLUT_ILLUMINANT_D50}

/*
 * TODO gamma functions:
 *   https://en.wikipedia.org/wiki/Rec._2020#Transfer_characteristics
 *   http://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.2100-0-201607-I!!PDF-E.pdf
 *   https://en.wikipedia.org/wiki/Rec._601
 */

/**
 * RGB colour space structure
 */
typedef struct libclut_rgb_colour_space {
  /**
   * The x-component of the red colour's xyY value
   */
  double red_x;
  
  /**
   * The y-component of the red colour's xyY value
   */
  double red_y;
  
  /**
   * The Y-component of the red colour's xyY value
   */
  double red_Y;
  
  /**
   * The x-component of the green colour's xyY value
   */
  double green_x;
  
  /**
   * The y-component of the green colour's xyY value
   */
  double green_y;
  
  /**
   * The Y-component of the green colour's xyY value
   */
  double green_Y;
  
  /**
   * The x-component of the blue colour's xyY value
   */
  double blue_x;
  
  /**
   * The y-component of the blue colour's xyY value
   */
  double blue_y;
  
  /**
   * The Y-component of the blue colour's xyY value
   */
  double blue_Y;
  
  /**
   * The x-component of the white point's xyY value
   */
  double white_x;
  
  /**
   * The y-component of the white point's xyY value
   */
  double white_y;
  
  /**
   * The Y-component of the white point's xyY value
   */
  double white_Y;
} libclut_rgb_colour_space_t;

/**
 * Matrix date-type for colour space conversion
 */
typedef double libclut_colour_space_conversion_matrix_t[3][3];

/* This is to avoid warnings about comparing double, These are only
 * used when it is safe, for example to test whether optimisations
 * are possible. { */
#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wfloat-equal"
#endif
static inline int libclut_eq__(double a, double b)  { return a == b; }
static inline int libclut_1__(double x)             { return libclut_eq__(x, 1); }
static inline int libclut_0__(double x)             { return libclut_eq__(x, 0); }
#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic pop
#endif
/* } */

#if defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdocumentation"
#endif

/**
 * Apply contrast correction on the colour curves using sRGB
 * 
 * In this context, contrast is a measure of difference between
 * the whitepoint and blackpoint, if the difference is 0 than
 * they are both grey
 * 
 * None of the parameter may have side-effects
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  r     The contrast parameter for the red curve
 * @param  g     The contrast parameter for the green curve
 * @param  b     The contrast parameter for the blue curve
 */
#define libclut_rgb_contrast(clut, max, type, r, g, b)\
	do {\
		const double h__ = (double)5 / 10;\
		if (!libclut_1__(r)) libclut__(clut, red,   type, (LIBCLUT_VALUE - (max) * h__) * (r) + (max) * h__);\
		if (!libclut_1__(g)) libclut__(clut, green, type, (LIBCLUT_VALUE - (max) * h__) * (g) + (max) * h__);\
		if (!libclut_1__(b)) libclut__(clut, blue,  type, (LIBCLUT_VALUE - (max) * h__) * (b) + (max) * h__);\
	} while (0)

/**
 * Apply contrast correction on the colour curves using CIE xyY
 * 
 * In this context, contrast is a measure of difference between
 * the whitepoint and blackpoint, if the difference is 0 than
 * they are both grey
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lclut'
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  r     The contrast parameter for the red curve
 * @param  g     The contrast parameter for the green curve
 * @param  b     The contrast parameter for the blue curve
 */
#define libclut_cie_contrast(clut, max, type, r, g, b)\
	do {\
		const double h__ = (double)5 / 10;\
		libclut_cie__(clut, max, type, libclut_eq__((r), (g)) && libclut_eq__((g), (b)),\
		              !libclut_1__(r), !libclut_1__(g), !libclut_1__(b),\
		              (Y__ - h__) * (r) + h__, (Y__ - h__) * (g) + h__, (Y__ - h__) * (b) + h__);\
	} while (0)

/**
 * Apply brightness correction on the colour curves using sRGB
 *  
 * In this context, brightness is a measure of the whiteness of the whitepoint
 * 
 * None of the parameter may have side-effects
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  r     The brightness parameter for the red curve
 * @param  g     The brightness parameter for the green curve
 * @param  b     The brightness parameter for the blue curve
 */
#define libclut_rgb_brightness(clut, max, type, r, g, b)\
	do {\
		if (!libclut_1__(r)) libclut__(clut, red,   type, LIBCLUT_VALUE * (r));\
		if (!libclut_1__(g)) libclut__(clut, green, type, LIBCLUT_VALUE * (g));\
		if (!libclut_1__(b)) libclut__(clut, blue,  type, LIBCLUT_VALUE * (b));\
	} while (0)

/**
 * Apply brightness correction on the colour curves using CIE xyY
 *  
 * In this context, brightness is a measure of the whiteness of the whitepoint
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lclut'
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  r     The brightness parameter for the red curve
 * @param  g     The brightness parameter for the green curve
 * @param  b     The brightness parameter for the blue curve
 */
#define libclut_cie_brightness(clut, max, type, r, g, b)\
	libclut_cie__(clut, max, type, libclut_eq__((r), (g)) && libclut_eq__((g), (b)),\
	              !libclut_1__(r), !libclut_1__(g), !libclut_1__(b),\
	              Y__ * (r), Y__ * (g), Y__ * (b))

/**
 * Convert the curves from formatted in standard RGB to linear sRGB
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_standard_to_linear1` is not undefined
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  r     Whether to convert the red colour curve
 * @param  g     Whether to convert the green colour curve
 * @param  b     Whether to convert the blue colour curve
 */
#define libclut_linearise(clut, max, type, r, g, b)\
	do {\
		double m__ = (double)(max);\
		if (r) libclut__(clut, red,   type, m__ * libclut_model_standard_to_linear1(LIBCLUT_VALUE / m__));\
		if (g) libclut__(clut, green, type, m__ * libclut_model_standard_to_linear1(LIBCLUT_VALUE / m__));\
		if (b) libclut__(clut, blue,  type, m__ * libclut_model_standard_to_linear1(LIBCLUT_VALUE / m__));\
	} while (0)

/**
 * Convert the curves from formatted in linear sRGB to standard RGB
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_linear_to_standard1` is not undefined
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  r     Whether to convert the red colour curve
 * @param  g     Whether to convert the green colour curve
 * @param  b     Whether to convert the blue colour curve
 */
#define libclut_standardise(clut, max, type, r, g, b)\
	do {\
		double m__ = (double)(max);\
		if (r) libclut__(clut, red,   type, m__ * libclut_model_linear_to_standard1(LIBCLUT_VALUE / m__));\
		if (g) libclut__(clut, green, type, m__ * libclut_model_linear_to_standard1(LIBCLUT_VALUE / m__));\
		if (b) libclut__(clut, blue,  type, m__ * libclut_model_linear_to_standard1(LIBCLUT_VALUE / m__));\
	} while (0)

/**
 * Convert the curves between two RGB colour spaces
 * 
 * Both RGB colour spaces must have same gamma functions as sRGB
 * 
 * Requires that `clut->red_size`, `clut->green_size`
 * and `clut->blue_size` are equal
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_linear_to_standard1`,
 * `libclut_model_standard_to_linear1`, or
 * `libclut_model_convert_rgb` is not undefined
 * 
 * @param  clut   Pointer to the gamma ramps, must have the arrays
 *                `red`, `green`, and `blue`, and the scalars
 *                `red_size`, `green_size`, and `blue_size`. Ramp
 *                structures from libgamma or libcoopgamma can be used.
 * @param  max    The maximum value on each stop in the ramps
 * @param  type   The data type used for each stop in the ramps
 * @param  m      Conversion matrix. Can be created with
 *                `libclut_model_get_rgb_conversion_matrix`
 * @param  trunc  Truncate values that are out of gamut
 */
#define libclut_convert_rgb_inplace(clut, max, type, m, trunc)\
	do {\
		double m__ = (double)(max), r__, g__, b__;\
		size_t i__, n__ = (clut)->red_size;\
		for (i__ = 0; i__ < n__; i__++) {\
			r__ = (clut)->red[i__] / m__;\
			g__ = (clut)->green[i__] / m__;\
			b__ = (clut)->blue[i__] / m__;\
			libclut_model_convert_rgb(r__, g__, b__, m, &r__, &g__, &b__);\
			r__ *= m__;\
			g__ *= m__;\
			b__ *= m__;\
			if (trunc) {\
				if (r__ < 0)\
					r__ = 0;\
				else if (r__ > m__)\
					r__ = m__;\
				if (g__ < 0)\
					g__ = 0;\
				else if (g__ > m__)\
					g__ = m__;\
				if (b__ < 0)\
					b__ = 0;\
				else if (b__ > m__)\
					b__ = m__;\
			}\
			(clut)->red[i__] = (type)r__;\
			(clut)->green[i__] = (type)g__;\
			(clut)->blue[i__] = (type)b__;\
		}\
	} while (0)

/**
 * Convert the curves between two RGB colour spaces
 * 
 * Both RGB colour spaces must have same gamma functions as sRGB
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lclut' if
 * `libclut_model_linear_to_standard1`,
 * `libclut_model_standard_to_linear1`, or
 * `libclut_model_convert_rgb` is not undefined.
 * Always requires linking with '-lm'.
 * 
 * @param  clut   Pointer to the input gamma ramps, must have the
 *                arrays `red`, `green`, and `blue`, and the scalars
 *                `red_size`, `green_size`, and `blue_size`. Ramp
 *                structures from libgamma or libcoopgamma can be used.
 * @param  max    The maximum value on each stop in the ramps
 * @param  type   The data type used for each stop in the ramps
 * @param  m      Conversion matrix. Can be created with
 *                `libclut_model_get_rgb_conversion_matrix`
 * @param  trunc  Truncate values that are out of gamut
 * @param  out    Pointer to the output gamma ramps, must have the
 *                arrays `red`, `green`, and `blue`, and the scalars
 *                `red_size`, `green_size`, and `blue_size`. Ramp
 *                structures from libgamma or libcoopgamma can be used.
 */
#define libclut_convert_rgb(clut, max, type, m, trunc, out)\
	do {\
		double m__ = (double)(max), r__, g__, b__, x__, y__;\
		size_t rn__ = (clut)->red_size;\
		size_t gn__ = (clut)->green_size;\
		size_t bn__ = (clut)->blue_size;\
		double w__;\
		size_t i__, j__, jj__;\
		for (i__ = 0; i__ < rn__; i__++) {\
			w__ = (double)i__ * (double)gn__ / (double)rn__;\
			j__ = (size_t)w__;\
			jj__ = j__ == gn__ ? j__ : (j__ + 1);\
			w__ -= (double)j__;\
			x__ = (clut)->green[j__] / m__;\
			y__ = (clut)->green[jj__] / m__;\
			x__ = libclut_model_standard_to_linear1(x__);\
			y__ = libclut_model_standard_to_linear1(y__);\
			g__ = x__ * (1 - w__) + y__ * w__;\
			\
			w__ = (double)i__ * (double)bn__ / (double)rn__;\
			j__ = (size_t)w__;\
			jj__ = j__ == bn__ ? j__ : (j__ + 1);\
			w__ -= (double)j__;\
			x__ = (clut)->blue[j__] / m__;\
			y__ = (clut)->blue[jj__] / m__;\
			x__ = libclut_model_standard_to_linear1(x__);\
			y__ = libclut_model_standard_to_linear1(y__);\
			b__ = x__ * (1 - w__) + y__ * w__;\
			\
			r__ = (clut)->red[i__] / m__;\
			r__ = (M)[0][0] * r__ + (M)[0][1] * g__ + (M)[0][2] * b__;\
			r__ = libclut_model_linear_to_standard1(r__);\
			r__ *= m__;\
			if (trunc) {\
				if (r__ < 0)\
					r__ = 0;\
				else if (r__ > m__)\
					r__ = m__;\
			}\
			(out)->red[i__] = (type)r__;\
		}\
		for (i__ = 0; i__ < gn__; i__++) {\
			w__ = (double)i__ * (double)rn__ / (double)gn__;\
			j__ = (size_t)w__;\
			jj__ = j__ == rn__ ? j__ : (j__ + 1);\
			w__ -= (double)j__;\
			x__ = (clut)->red[j__] / m__;\
			y__ = (clut)->red[jj__] / m__;\
			x__ = libclut_model_standard_to_linear1(x__);\
			y__ = libclut_model_standard_to_linear1(y__);\
			r__ = x__ * (1 - w__) + y__ * w__;\
			\
			w__ = (double)i__ * (double)bn__ / (double)gn__;\
			j__ = (size_t)w__;\
			jj__ = j__ == bn__ ? j__ : (j__ + 1);\
			w__ -= (double)j__;\
			x__ = (clut)->blue[j__] / m__;\
			y__ = (clut)->blue[jj__] / m__;\
			x__ = libclut_model_standard_to_linear1(x__);\
			y__ = libclut_model_standard_to_linear1(y__);\
			b__ = x__ * (1 - w__) + y__ * w__;\
			\
			g__ = (clut)->green[i__] / m__;\
			g__ = (M)[1][0] * r__ + (M)[1][1] * g__ + (M)[1][2] * b__;\
			g__ = libclut_model_linear_to_standard1(g__);\
			g__ *= m__;\
			if (trunc) {\
				if (g__ < 0)\
					g__ = 0;\
				else if (g__ > m__)\
					g__ = m__;\
			}\
			(out)->green[i__] = (type)g__;\
		}\
		for (i__ = 0; i__ < bn__; i__++) {\
			w__ = (double)i__ * (double)rn__ / (double)bn__;\
			j__ = (size_t)w__;\
			jj__ = j__ == rn__ ? j__ : (j__ + 1);\
			w__ -= (double)j__;\
			x__ = (clut)->red[j__] / m__;\
			y__ = (clut)->red[jj__] / m__;\
			x__ = libclut_model_standard_to_linear1(x__);\
			y__ = libclut_model_standard_to_linear1(y__);\
			r__ = x__ * (1 - w__) + y__ * w__;\
			\
			w__ = (double)i__ * (double)gn__ / (double)bn__;\
			j__ = (size_t)w__;\
			jj__ = j__ == gn__ ? j__ : (j__ + 1);\
			w__ -= (double)j__;\
			x__ = (clut)->green[j__] / m__;\
			y__ = (clut)->green[jj__] / m__;\
			x__ = libclut_model_standard_to_linear1(x__);\
			y__ = libclut_model_standard_to_linear1(y__);\
			g__ = x__ * (1 - w__) + y__ * w__;\
			\
			b__ = (clut)->blue[i__] / m__;\
			b__ = (M)[2][0] * r__ + (M)[2][1] * g__ + (M)[2][2] * b__;\
			b__ = libclut_model_linear_to_standard1(b__);\
			b__ *= m__;\
			if (trunc) {\
				if (b__ < 0)\
					b__ = 0;\
				else if (b__ > m__)\
					b__ = m__;\
			}\
			(out)->blue[i__] = (type)b__;\
		}\
	} while (0)

/**
 * Apply gamma correction on the colour curves
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lm'
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  r     The gamma parameter the red colour curve
 * @param  g     The gamma parameter the green colour curve
 * @param  b     The gamma parameter the blue colour curve
 */
#define libclut_gamma(clut, max, type, r, g, b)\
	do {\
		double m__ = (double)(max);\
		if (!libclut_1__(r)) libclut__(clut, red,   type, m__ * pow(LIBCLUT_VALUE / m__, 1 / (double)(r)));\
		if (!libclut_1__(g)) libclut__(clut, green, type, m__ * pow(LIBCLUT_VALUE / m__, 1 / (double)(g)));\
		if (!libclut_1__(b)) libclut__(clut, blue,  type, m__ * pow(LIBCLUT_VALUE / m__, 1 / (double)(b)));\
	} while (0)

/**
 * Reverse the colour curves (negative image with gamma preservation)
 * 
 * None of the parameter may have side-effects
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps.
 *               This parameter is not used, it is just a dummy, to unify
 *               the API with the other functions
 * @param  type  The data type used for each stop in the ramps
 * @param  r     Whether to invert the red colour curve
 * @param  g     Whether to invert the green colour curve
 * @param  b     Whether to invert the blue colour curve
 */
#define libclut_negative(clut, max, type, r, g, b)\
	do {\
		size_t i__, n__;\
		type t__;\
		if (r) {\
			for (i__ = 0, n__ = (clut)->red_size; i__ < (n__ >> 1); i__++) {\
				t__ = (clut)->red[i__];\
				(clut)->red[i__] = (clut)->red[n__ - i__ - 1];\
				(clut)->red[n__ - i__ - 1] = t__;\
			}\
		}\
		if (g) {\
			for (i__ = 0, n__ = (clut)->green_size; i__ < (n__ >> 1); i__++) {\
				t__ = (clut)->green[i__];\
				(clut)->green[i__] = (clut)->green[n__ - i__ - 1];\
				(clut)->green[n__ - i__ - 1] = t__;\
			}\
		}\
		if (b) {\
			for (i__ = 0, n__ = (clut)->blue_size; i__ < (n__ >> 1); i__++)	{\
				t__ = (clut)->blue[i__];\
				(clut)->blue[i__] = (clut)->blue[n__ - i__ - 1];\
				(clut)->blue[n__ - i__ - 1] = t__;\
			}\
		}\
	} while (0)

/**
 * Invert the colour curves (negative image with gamma invertion), using sRGB
 * 
 * None of the parameter may have side-effects
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  r     Whether to invert the red colour curve
 * @param  g     Whether to invert the green colour curve
 * @param  b     Whether to invert the blue colour curve
 */
#define libclut_rgb_invert(clut, max, type, r, g, b)\
	do {\
		if (r) libclut__(clut, red,   type, (max) - LIBCLUT_VALUE);\
		if (g) libclut__(clut, green, type, (max) - LIBCLUT_VALUE);\
		if (b) libclut__(clut, blue,  type, (max) - LIBCLUT_VALUE);\
	} while (0)

/**
 * Invert the colour curves (negative image with gamma invertion), using CIE xyY
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lclut'
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  r     Whether to invert the red colour curve
 * @param  g     Whether to invert the green colour curve
 * @param  b     Whether to invert the blue colour curve
 */
#define libclut_cie_invert(clut, max, type, r, g, b)\
	libclut_cie__(clut, max, type, (r) && (g) && (b), r, g, b, 1 - Y__, 1 - Y__, 1 - Y__)

/**
 * Apply S-curve correction on the colour curves.
 * This is intended for fine tuning LCD monitors,
 * 4.5 is good value start start testing at.
 * You would probably like to use rgb_limits before
 * this to adjust the blackpoint as that is the
 * only way to adjust the blackpoint on many LCD
 * monitors.
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lm'
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  rp    Pointer to the sigmoid parameter for the red curve. `NULL` for no adjustment
 * @param  gp    Pointer to the sigmoid parameter for the green curve. `NULL` for no adjustment
 * @param  bp    Pointer to the sigmoid parameter for the blue curve. `NULL` for no adjustment
 */
#define libclut_sigmoid(clut, max, type, rp, gp, bp)\
	do {\
		double *gcc_6_1_1_workaround, m__ = (double)(max);\
		const double h__ = (double)5 / 10;\
		gcc_6_1_1_workaround = rp;\
		if (gcc_6_1_1_workaround)\
			libclut_sigmoid__(clut, max, type, red);\
		gcc_6_1_1_workaround = gp;\
		if (gcc_6_1_1_workaround)\
			libclut_sigmoid__(clut, max, type, green);\
		gcc_6_1_1_workaround = bp;\
		if (gcc_6_1_1_workaround)\
			libclut_sigmoid__(clut, max, type, blue);\
	}\
	while (0)

/**
 * Apply S-curve correction on the colour curves.
 * This is intended for fine tuning LCD monitors,
 * 4.5 is good value start start testing at.
 * You would probably like to use rgb_limits before
 * this to adjust the blackpoint as that is the
 * only way to adjust the blackpoint on many LCD
 * monitors.
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lm'
 * 
 * Intended for internal use
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  channel  The channel, must be either "red", "green", or "blue"
 */
#define libclut_sigmoid__(clut, max, type, channel)\
	do {\
		double s__ = *gcc_6_1_1_workaround, l__;\
		size_t i__;\
		for (i__ = 0; i__ < (clut)->channel##_size; i__++) {\
			l__ = log(m__ / (clut)->channel[i__] - 1);\
			if (isnan(l__) || isinf(l__))\
				l__ = 37.024483 * (isinf(l__) > 0 ? +1 : -1);\
			(clut)->channel[i__] = (type)(m__ * (h__ - l__ / s__));\
		}\
	} while (0)

/**
 * Changes the blackpoint and the whitepoint, using sRGB
 * 
 * None of the parameter may have side-effects
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  rmin  The red component value of the blackpoint
 * @param  rmax  The red component value of the whitepoint
 * @param  gmin  The green component value of the blackpoint
 * @param  gmax  The green component value of the whitepoint
 * @param  bmin  The blue component value of the blackpoint
 * @param  bmax  The blue component value of the whitepoint
 */
#define libclut_rgb_limits(clut, max, type, rmin, rmax, gmin, gmax, bmin, bmax)\
	do {\
		double diff__;\
		if (!libclut_0__(rmin) || !libclut_1__(rmax)) {\
			diff__ = (double)(rmax) - (double)(rmin);\
			libclut__(clut, red, type, LIBCLUT_VALUE / (double)(max) * diff__ + (rmin));\
		}\
		if (!libclut_0__(gmin) || !libclut_1__(gmax)) {\
			diff__ = (double)(gmax) - (double)(gmin);\
			libclut__(clut, green, type, LIBCLUT_VALUE / (double)(max) * diff__ + (gmin));\
		}\
		if (!libclut_0__(bmin) || !libclut_1__(bmax)) {\
			diff__ = (double)(bmax) - (double)(bmin);\
			libclut__(clut, blue, type, LIBCLUT_VALUE / (double)(max) * diff__ + (bmin));\
		}\
	} while (0)

/**
 * Changes the blackpoint and the whitepoint, using CIE xyY
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lclut'
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  rmin  The red component value of the blackpoint
 * @param  rmax  The red component value of the whitepoint
 * @param  gmin  The green component value of the blackpoint
 * @param  gmax  The green component value of the whitepoint
 * @param  bmin  The blue component value of the blackpoint
 * @param  bmax  The blue component value of the whitepoint
 */
#define libclut_cie_limits(clut, max, type, rmin, rmax, gmin, gmax, bmin, bmax)\
	do {\
		double rd__ = (rmax) - (rmin), gd__ = (gmax) - (gmin), bd__ = (bmax) - (bmin);\
		libclut_cie__(clut, max, type,\
		              libclut_eq__((rmin), (gmin)) && libclut_eq__((gmin), (bmin)) &&\
	                      libclut_eq__((rmax), (gmax)) && libclut_eq__((gmax), (bmax)),\
		              !libclut_0__(rmin) || !libclut_1__(rmax),\
		              !libclut_0__(gmin) || !libclut_1__(gmax),\
		              !libclut_0__(bmin) || !libclut_1__(bmax),\
		              Y__ * rd__ + (rmin), Y__ * gd__ + (gmin), Y__ * bd__ + (bmin));\
	} while (0)

/**
 * Manipulate the colour curves using a function on the sRGB colour space
 * 
 * None of the parameter may have side-effects
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  r     Function to manipulate the red colour curve, should either
 *               be `NULL` or map a [0, 1] `double` to a [0, 1] `double`
 * @param  g     Function to manipulate the green colour curve, should either
 *               be `NULL` or map a [0, 1] `double` to a [0, 1] `double`
 * @param  b     Function to manipulate the blue colour curve, should either
 *               be `NULL` or map a [0, 1] `double` to a [0, 1] `double`
 */
#define libclut_manipulate(clut, max, type, r, g, b)\
	do {\
		double m__ = (double)(max);\
		double (*gcc_6_1_1_workaround__)(double);\
		gcc_6_1_1_workaround__ = r;\
		if (gcc_6_1_1_workaround__)\
			libclut__(clut, red,   type, m__ * (gcc_6_1_1_workaround__)(LIBCLUT_VALUE / m__));\
		gcc_6_1_1_workaround__ = g;\
		if (gcc_6_1_1_workaround__)\
			libclut__(clut, green, type, m__ * (gcc_6_1_1_workaround__)(LIBCLUT_VALUE / m__));\
		gcc_6_1_1_workaround__ = b;\
		if (gcc_6_1_1_workaround__)\
			libclut__(clut, blue,  type, m__ * (gcc_6_1_1_workaround__)(LIBCLUT_VALUE / m__));\
	} while (0)

/**
 * Manipulate the colour curves using a function on the CIE xyY colour space
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lclut'
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  r     Function to manipulate the red colour curve, should either
 *               be `NULL` or map a [0, 1] `double` to a [0, 1] `double`
 * @param  g     Function to manipulate the green colour curve, should either
 *               be `NULL` or map a [0, 1] `double` to a [0, 1] `double`
 * @param  b     Function to manipulate the blue colour curve, should either
 *               be `NULL` or map a [0, 1] `double` to a [0, 1] `double`
 */
#define libclut_cie_manipulate(clut, max, type, r, g, b)\
	libclut_cie__(clut, max, type, (r) && (g) && (b), r, g, b, (r)(Y__), (g)(Y__), (b)(Y__))

/**
 * Resets colour curvers to linear mappings
 * (Identity mapping if imaginged to map from [0, 1] to [0, 1])
 * 
 * None of the parameter may have side-effects
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  r     Whether to reset the red colour curve
 * @param  g     Whether to reset the green colour curve
 * @param  b     Whether to reset the blue colour curve
 */
#define libclut_start_over(clut, max, type, r, g, b)\
	do {\
		size_t i__;\
		double m__, max__ = (double)(max);\
		if (r) {\
			m__ = (double)((clut)->red_size - 1);\
			for (i__ = 0; i__ < (clut)->red_size; i__++)\
				(clut)->red[i__] = (type)(((double)i__ / m__) * max__);\
		}\
		if (g) {\
			m__ = (double)((clut)->green_size - 1);\
			for (i__ = 0; i__ < (clut)->green_size; i__++)\
				(clut)->green[i__] = (type)(((double)i__ / m__) * max__);\
		}\
		if (b) {\
			m__ = (double)((clut)->blue_size - 1);\
			for (i__ = 0; i__ < (clut)->blue_size; i__++)\
				(clut)->blue[i__] = (type)(((double)i__ / m__) * max__);\
		}\
	} while (0)

/**
 * Clip colour curves to only map to values between the minimum and maximum.
 * This should be done, before apply the curves, and before applying changes
 * with limited domain.
 * 
 * Values below 0 are set to 0, and values above `max` are set to `max`
 * 
 * None of the parameter may have side-effects
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  r     Whether to clip the red colour curve
 * @param  g     Whether to clip the green colour curve
 * @param  b     Whether to clip the blue colour curve
 */
#define libclut_clip(clut, max, type, r, g, b)\
	do {\
		if (r) libclut__(clut, red,   type, libclut_clip__(0, LIBCLUT_VALUE, max));\
		if (g) libclut__(clut, green, type, libclut_clip__(0, LIBCLUT_VALUE, max));\
		if (b) libclut__(clut, blue,  type, libclut_clip__(0, LIBCLUT_VALUE, max));\
	} while (0)

/**
 * Truncates a value to fit a boundary
 * 
 * None of the parameter may have side-effects
 * 
 * Intended for internal use
 * 
 * @param   min  The minimum allowed value
 * @param   val  The current value
 * @param   max  The maximum allowed value
 * @return       The value truncated into its boundary
 */
#define libclut_clip__(min, val, max)\
	(LIBCLUT_VALUE < (min) ? (min) : LIBCLUT_VALUE > (max) ? (max) : LIBCLUT_VALUE)

/**
 * Emulates low colour resolution
 * 
 * None of the parameter may have side-effects
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  rx    The desired emulated red encoding resolution, 0 for unchanged
 * @param  ry    The desired emulated red output resolution, 0 for unchanged
 * @param  gx    The desired emulated green encoding resolution, 0 for unchanged
 * @param  gy    The desired emulated green output resolution, 0 for unchanged
 * @param  bx    The desired emulated blue encoding resolution, 0 for unchanged
 * @param  by    The desired emulated blue output resolution, 0 for unchanged
 */
#define libclut_lower_resolution(clut, max, type, rx, ry, gx, gy, bx, by)\
	do {\
		libclut_lower_resolution__(clut, red,   max, type, rx, ry);\
		libclut_lower_resolution__(clut, green, max, type, gx, gy);\
		libclut_lower_resolution__(clut, blue,  max, type, bx, by);\
	} while (0)

/**
 * Emulates low colour resolution of a channel
 * 
 * None of the parameter may have side-effects
 * 
 * Intended for internal use
 * 
 * @param  clut     Pointer to the gamma ramps, must have the arrays
 *                  `red`, `green`, and `blue`, and the scalars
 *                  `red_size`, `green_size`, and `blue_size`. Ramp
 *                  structures from libgamma or libcoopgamma can be used.
 * @param  channel  The channel, must be either "red", "green", or "blue"
 * @param  max      The maximum value on each stop in the ramps
 * @param  type     The data type used for each stop in the ramps
 * @param  x        The desired emulated encoding resolution, 0 for unchanged
 * @param  y        The desired emulated output resolution, 0 for unchanged
 */
#define libclut_lower_resolution__(clut, channel, max, type, x, y)\
	do {\
		if ((x) || (y)) {\
			size_t x__, y__, i__, n__ = (clut)->channel##_size;\
			double xm__ = (double)((x) - 1), ym__ = (double)((y) - 1);\
			double m__ = (double)(max), nm__ = (double)(n__ - 1);\
			type c__[n__]; /* Do not use alloca! */\
			const double h__ = (double)5 / 10;\
			for (i__ = 0; i__ < n__; i__++) {\
				if ((x__ = i__), (x)) {\
					x__ = (size_t)((double)i__ * (double)(x) / (double)n__);\
					x__ = (size_t)((double)x__ * nm__ / xm__);\
				}\
				if (!(y)) {\
					c__[i__] = (clut)->channel[x__];\
				} else {\
					y__ = (size_t)((double)((clut)->channel[x__]) / (max) * ym__ + h__);\
					c__[i__] = (type)((double)y__ / ym__ * m__);\
				}\
			}\
			memcpy((clut)->channel, c__, n__ * sizeof(type));\
		}\
	} while (0)

/**
 * Translates a gamma ramp structure to another gamma ramp structure type
 * 
 * None of the parameter may have side-effects
 * 
 * @param  dclut  Pointer to the desired gamma ramps, must have the arrays
 *                `red`, `green`, and `blue`, and the scalars `red_size`,
 *                `green_size`, and `blue_size`. Ramp structures from
 *                libgamma or libcoopgamma can be used.
 * @param  dmax   The maximum value on each stop in the ramps in `dclut`
 * @param  dtype  The data type used for each stop in the ramps in `dclut`
 * @param  sclut  Pointer to the set gamma ramps, must have the arrays
 *                `red`, `green`, and `blue`, and the scalars `red_size`,
 *                `green_size`, and `blue_size`. Ramp structures from
 *                libgamma or libcoopgamma can be used
 * @param  smax   The maximum value on each stop in the ramps in `sclut`
 * @param  stype  The data type used for each stop in the ramps in `sclut`
 *                (Not actually used)
 */
#define libclut_translate(dclut, dmax, dtype, sclut, smax, stype)\
	do {\
		libclut_translate__(dclut, dmax, dtype, sclut, smax, stype, red);\
		libclut_translate__(dclut, dmax, dtype, sclut, smax, stype, green);\
		libclut_translate__(dclut, dmax, dtype, sclut, smax, stype, blue);\
	} while (0)

/**
 * Translates a gamma ramp structure to another gamma ramp structure type
 * 
 * None of the parameter may have side-effects
 * 
 * This is intended for internal use
 * 
 * @param  dclut    Pointer to the desired gamma ramps, must have the arrays
 *                  `red`, `green`, and `blue`, and the scalars `red_size`,
 *                  `green_size`, and `blue_size`. Ramp structures from
 *                  libgamma or libcoopgamma can be used.
 * @param  dmax     The maximum value on each stop in the ramps in `dclut`
 * @param  dtype    The data type used for each stop in the ramps in `dclut`
 * @param  sclut    Pointer to the set gamma ramps, must have the arrays
 *                  `red`, `green`, and `blue`, and the scalars `red_size`,
 *                  `green_size`, and `blue_size`. Ramp structures from
 *                  libgamma or libcoopgamma can be used.
 * @param  smax     The maximum value on each stop in the ramps in `sclut`
 * @param  stype    The data type used for each stop in the ramps in `sclut`.
 *                  (Not actually used)
 * @param  channel  The channel, must be either "red", "green", or "blue"
 */
#define libclut_translate__(dclut, dmax, dtype, sclut, smax, stype, channel)\
	do {\
		size_t di__, si__, sj__;\
		size_t dn__ = (dclut)->channel##_size;\
		size_t sn__ = (sclut)->channel##_size;\
		double dm__ = (double)(dmax);\
		double sm__ = (double)(smax);\
		double dmsm__ = dm__ / sm__;\
		double x__, y__;\
		if (dn__ == sn__) {\
			for (di__ = 0; di__ < dn__; di__++) {\
				y__ = (double)((sclut)->channel[di__]) * dmsm__;\
				(dclut)->channel[di__] = (dtype)y__;\
			}\
		} else {\
			for (di__ = 0; di__ < dn__; di__++) {\
				x__ = di__ / (dn__ - 1) * (sn__ - 1);\
				si__ = (size_t)(x__);\
				sj__ = si__ + (si__ != sn__);\
				x__ -= (double)si__;\
				y__  = (double)((sclut)->channel[si__]) * (1 - x__);\
				y__ += (double)((sclut)->channel[sj__]) * (x__);\
				y__ *= dmsm__;\
				(dclut)->channel[di__] = (dtype)y__;\
			}\
		}\
	} while (0)

/**
 * Applies a filter or calibration
 * 
 * None of the parameter may have side-effects
 * 
 * @param  clut    Pointer to the gamma ramps, must have the arrays
 *                 `red`, `green`, and `blue`, and the scalars
 *                 `red_size`, `green_size`, and `blue_size`. Ramp
 *                 structures from libgamma or libcoopgamma can be used.
 * @param  max     The maximum value on each stop in the ramps
 * @param  type    The data type used for each stop in the ramps
 * @param  filter  Same as `clut`, but for the filter to apply
 * @param  fmax    Same as `max`, but for the filter to apply
 * @param  ftype   Same as `type`, but for the filter to apply (Not actually used)
 * @param  r       Whether to apply the filter for the red curve
 * @param  g       Whether to apply the filter for the green curve
 * @param  b       Whether to apply the filter for the blue curve
 */
#define libclut_apply(clut, max, type, filter, fmax, ftype, r, g, b)\
	do {\
		if (r) libclut_apply__(clut, max, type, filter, fmax, ftype, red);\
		if (g) libclut_apply__(clut, max, type, filter, fmax, ftype, green);\
		if (b) libclut_apply__(clut, max, type, filter, fmax, ftype, blue);\
	} while (0)

/**
 * Applies a filter or calibration for one channel
 * 
 * None of the parameter may have side-effects
 * 
 * Intended for internal use
 * 
 * @param  clut     Pointer to the gamma ramps, must have the arrays
 *                  `red`, `green`, and `blue`, and the scalars
 *                  `red_size`, `green_size`, and `blue_size`. Ramp
 *                  structures from libgamma or libcoopgamma can be used.
 * @param  max      The maximum value on each stop in the ramps
 * @param  type     The data type used for each stop in the ramps
 * @param  filter   Same as `clut`, but for the filter to apply
 * @param  fmax     Same as `max`, but for the filter to apply
 * @param  ftype    Same as `type`, but for the filter to apply (Not actually used)
 * @param  channel  The channel, must be either "red", "green", or "blue"
 */
#define libclut_apply__(clut, max, type, filter, fmax, ftype, channel)\
	do {\
		size_t i__, rn__ = (clut)->channel##_size, fn__ = (filter)->channel##_size - 1;\
		double x__, rm__ = (double)(max), m__ = (double)(max) / (double)(fmax);\
		for (i__ = 0; i__ < rn__; i__++) {\
			x__ = (double)((clut)->channel[i__]) / rm__ * (double)fn__;\
			(clut)->channel[i__] = (type)((double)((filter)->channel[(size_t)x__]) * m__);\
		}\
	} while (0)

/**
 * Applies a filter or calibration, using CIE xyY
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lclut'
 * 
 * @param  clut    Pointer to the gamma ramps, must have the arrays
 *                 `red`, `green`, and `blue`, and the scalars
 *                 `red_size`, `green_size`, and `blue_size`. Ramp
 *                 structures from libgamma or libcoopgamma can be used.
 * @param  max     The maximum value on each stop in the ramps
 * @param  type    The data type used for each stop in the ramps
 * @param  filter  Same as `clut`, but for the filter to apply
 * @param  fmax    Same as `max`, but for the filter to apply
 * @param  ftype   Same as `type`, but for the filter to apply (Not actually used)
 * @param  r       Whether to apply the filter for the red curve
 * @param  g       Whether to apply the filter for the green curve
 * @param  b       Whether to apply the filter for the blue curve
 */
#define libclut_cie_apply(clut, max, type, filter, fmax, ftype, r, g, b)\
	do {\
		size_t rfn__ = (filter)->red_size - 1, gfn__ = (filter)->green_size - 1;\
		size_t bfn__ = (filter)->blue_size - 1, x__;\
		size_t rm__ = (double)(max), fm__ = (double)(fmax);\
		libclut_cie__(clut, max, type, 0, r, g, b,\
		              (x__ = (size_t)(Y__ / rm__ * rfn__), (double)((filter)->red[x__])   / fm__),\
		              (x__ = (size_t)(Y__ / rm__ * gfn__), (double)((filter)->green[x__]) / fm__),\
		              (x__ = (size_t)(Y__ / rm__ * bfn__), (double)((filter)->blue[x__])  / fm__));\
	} while (0)

/**
 * Modify a ramp
 * 
 * None of the parameter may have side-effects
 * 
 * This is intended for internal use
 * 
 * @param  clut     Pointer to the gamma ramps, must have and array
 *                  named `channel` and a scalar named `channel` followed
 *                  by "_size"
 * @param  channel  The channel, must be either "red", "green", or "blue"
 * @param  type     The data type used for each stop in the ramps
 * @param  expr     Expression that evalutes the value a stop should have.
 *                  It can use the variable `LIBCLUT_VALUE` to get the
 *                  current value of the stop.
 */
#define libclut__(clut, channel, type, expr)\
	do {\
		size_t i__, n__ = (clut)->channel##_size;\
		type LIBCLUT_VALUE;\
		for (i__ = 0; i__ < n__; i__++) {\
			LIBCLUT_VALUE = (clut)->channel[i__];\
			(clut)->channel[i__] = (type)(expr);\
		}\
	} while (0)

/**
 * Modify a ramp set in CIE xyY
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lclut'
 * 
 * This is intended for internal use
 * 
 * @param  clut   Pointer to the gamma ramps, must have the arrays
 *                `red`, `green`, and `blue`, and the scalars
 *                `red_size`, `green_size`, and `blue_size`. Ramp
 *                structures from libgamma or libcoopgamma can be used.
 * @param  max    The maximum value on each stop in the ramps
 * @param  type   The data type used for each stop in the ramps
 * @param  utest  Whether all channels can be modified at the same time
 *                This test does not have to include the ramp size
 * @param  rtest  Whether the red channel have to be modified
 * @param  gtest  Whether the green channel have to be modified
 * @param  btest  Whether the blue channel have to be modified
 * @param  rexpr  Expression calculating the intensity of the red channel.
 *                The current value is stored in `Y__`.
 * @param  gexpr  Expression calculating the intensity of the green channel.
 *                The current value is stored in `Y__`.
 * @param  bexpr  Expression calculating the intensity of the blue channel.
 *                The current value is stored in `Y__`.
 */
#define libclut_cie__(clut, max, type, utest, rtest, gtest, btest, rexpr, gexpr, bexpr)\
	do {\
		size_t rn__ = (clut)->red_size;\
		size_t gn__ = (clut)->green_size;\
		size_t bn__ = (clut)->blue_size;\
		size_t i__;\
		double x__, y__, Y__, r__, g__, b__;\
		double m__ = (double)(max);\
		type* rs__ = (clut)->red;\
		type* gs__ = (clut)->green;\
		type* bs__ = (clut)->blue;\
		if ((rn__ == gn__) && (gn__ == bn__) && (utest)) {\
			if (!(rtest))\
				break;\
			for (i__ = 0; i__ < rn__; i__++) {\
				libclut_model_srgb_to_ciexyy(rs__[i__] / m__, gs__[i__] / m__,\
				                             bs__[i__] / m__, &x__, &y__, &Y__);\
				libclut_model_ciexyy_to_srgb(x__, y__, rexpr, &r__, &g__, &b__);\
				rs__[i__] = (type)(r__ * m__);\
				gs__[i__] = (type)(g__ * m__);\
				bs__[i__] = (type)(b__ * m__);\
			}\
		} else if ((rn__ == gn__) && (gn__ == bn__)) {\
			if (!(rtest) && !(gtest) && !(btest))\
				break;\
			for (i__ = 0; i__ < rn__; i__++) {\
				libclut_model_srgb_to_ciexyy(rs__[i__] / m__, gs__[i__] / m__,\
				                             bs__[i__] / m__, &x__, &y__, &Y__);\
				if (rtest) {\
					libclut_model_ciexyy_to_srgb(x__, y__, rexpr, &r__, &g__, &b__);\
					rs__[i__] = (type)(r__ * m__);\
				}\
				if (gtest) {\
					libclut_model_ciexyy_to_srgb(x__, y__, gexpr, &r__, &g__, &b__);\
					gs__[i__] = (type)(g__ * m__);\
				}\
				if (btest) {\
					libclut_model_ciexyy_to_srgb(x__, y__, bexpr, &r__, &g__, &b__);\
					bs__[i__] = (type)(b__ * m__);\
				}\
			}\
		} else {\
			if (rtest) {\
				for (i__ = 0; i__ < rn__; i__++) {\
					libclut_cie___(clut, max, type, r, rexpr, i__,\
						       libclut_i__(i__, rn__, gn__),\
						       libclut_i__(i__, rn__, bn__));\
				}\
			}\
			if (gtest) {\
				for (i__ = 0; i__ < rn__; i__++) {\
					libclut_cie___(clut, max, type, g, gexpr,\
						       libclut_i__(i__, gn__, rn__), i__,\
						       libclut_i__(i__, gn__, bn__));\
				}\
			}\
			if (btest) {\
				for (i__ = 0; i__ < rn__; i__++) {\
					libclut_cie___(clut, max, type, b, bexpr,\
						       libclut_i__(i__, bn__, rn__),\
						       libclut_i__(i__, bn__, gn__), i__);\
				}\
			}\
		}\
	} while (0)

/**
 * Modify a ramp stop in CIE xyY
 * 
 * None of the parameter may have side-effects
 * 
 * Requires linking with '-lclut'
 * 
 * This is intended for internal use.
 * Assumes the existence of variables defined in `libclut_cie__`.
 * 
 * @param  clut  Pointer to the gamma ramps, must have the arrays
 *               `red`, `green`, and `blue`, and the scalars
 *               `red_size`, `green_size`, and `blue_size`. Ramp
 *               structures from libgamma or libcoopgamma can be used.
 * @param  max   The maximum value on each stop in the ramps
 * @param  type  The data type used for each stop in the ramps
 * @param  c     Either "r" for red, "g" for green, or "b" for blue
 * @param  expr  Expression calculating the intensity of the channel
 * @param  ri    The index of the stop translated to the red channel
 * @param  gi    The index of the stop translated to the green channel
 * @param  bi    The index of the stop translated to the blue channel
 */
#define libclut_cie___(clut, max, type, c, expr, ri, gi, bi)\
	do {\
		for (i__ = 0; i__ < c##n__; i__++) {\
			libclut_model_srgb_to_ciexyy(rs__[(ri)] / m__, gs__[(gi)] / m__,\
			                             bs__[(bi)] / m__, &x__, &y__, &Y__);\
			libclut_model_ciexyy_to_srgb(x__, y__, expr, &r__, &g__, &b__);\
			c##s__[i__] = (type)(c##__ * m__);\
		}\
	} while (0)

/**
 * Translate an index from one channel to another
 * 
 * @param   i    The index in the input channel
 * @param   in   The size of the input channel
 * @param   out  The size of the output channel
 * @return       The index in the output channel
 */
#define libclut_i__(i, in, out)\
	(size_t)((double)(i) * (double)(out) / (double)(in))

#if defined(__GNUC__) && !defined(__clang__)
# define LIBCLUT_GCC_ONLY__(x)  x
#else
# define LIBCLUT_GCC_ONLY__(x)  /* do nothing */
#endif

/**
 * Convert one component from [0, 1] linear sRGB to [0, 1] sRGB
 * 
 * If the macro variant is used, the argument must not have
 * any side-effects. The macro variant requires linking with
 * '-lm'
 * 
 * @param   c  The linear sRGB value
 * @return     Corresponding sRGB value
 */
LIBCLUT_GCC_ONLY__(__attribute__((__const__, __leaf__)))
double (libclut_model_linear_to_standard1)(double);
#define libclut_model_linear_to_standard1(c)\
	(((double)(c) <= 0.0031308) ? (12.92 * (double)(c)) : ((1.055) * pow((double)(c), 1 / 2.4) - 0.055))

/**
 * Convert [0, 1] linear sRGB to [0, 1] sRGB
 * 
 * The macro variant requires linking with '-lm',
 * if the 'libclut_model_linear_to_standard1' is defined,
 * otherwise it requires linking with '-lclut'
 * 
 * @param  r  Pointer to the linear red component,
 *            and output parameter for the red component
 * @param  g  Pointer to the linear green component,
 *            and output parameter for the green component
 * @param  b  Pointer to the linear blue component,
 *            and output parameter for the blue component
 */
void (libclut_model_linear_to_standard)(double *, double *, double *);
#define libclut_model_linear_to_standard(r, g, b)\
	do {\
		double *r__ = (r), *g__ = (g), *b__ = (b);\
		*r__ = libclut_model_linear_to_standard1(*r__);\
		*g__ = libclut_model_linear_to_standard1(*g__);\
		*b__ = libclut_model_linear_to_standard1(*b__);\
	} while (0)

/**
 * Convert one component from [0, 1] sRGB to [0, 1] linear sRGB
 * 
 * If the macro variant is used, the argument must not have
 * any side-effects. The macro variant requires linking with
 * '-lm'
 * 
 * @param   c  The sRGB value
 * @return     Corresponding linear sRGB value
 */
LIBCLUT_GCC_ONLY__(__attribute__((__const__, __leaf__)))
double (libclut_model_standard_to_linear1)(double);
#define libclut_model_standard_to_linear1(c) \
	(((double)(c) <= 0.04045) ? ((double)(c) / 12.92) : pow(((double)(c) + 0.055) / 1.055, 2.4))

/**
 * Convert [0, 1] sRGB to [0, 1] linear sRGB
 * 
 * The macro variant requires linking with '-lm',
 * if the 'libclut_model_standard_to_linear1' is defined,
 * otherwise it requires linking with '-lclut'
 * 
 * @param  r  Pointer to the red component, and output
 *            parameter for the linear red component
 * @param  g  Pointer to the green component, and output
 *            parameter for the linear green component
 * @param  b  Pointer to the blue component, and output
 *            parameter for the linear blue component
 */
void (libclut_model_standard_to_linear)(double *, double *, double *);
#define libclut_model_standard_to_linear(r, g, b)\
	do {\
		double *r__ = (r), *g__ = (g), *b__ = (b);\
		*r__ = libclut_model_standard_to_linear1(*r__);\
		*g__ = libclut_model_standard_to_linear1(*g__);\
		*b__ = libclut_model_standard_to_linear1(*b__);\
	} while (0)

/**
 * Convert CIE xyY to CIE XYZ
 * 
 * @param  x  The x parameter
 * @param  y  The y parameter
 * @param  Y  The Y parameter; this is also the Y (middle) parameter for the CIE XYZ colour
 * @param  X  Output parameter for the X parameter
 * @param  Z  Output parameter for the Z parameter
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_ciexyy_to_ciexyz)(double, double, double, double *, double *);
#define libclut_model_ciexyy_to_ciexyz(x, y, Y, X, Z)\
	do {\
		double x__ = (x), y__ = (y), Y__ = (Y), *X__ = (X), *Z__ = (Z);\
		*X__ = libclut_0__(y__) ? Y__ : (Y__ * x__ / y__);\
		*Z__ = libclut_0__(y__) ? Y__ : (Y__ * (1 - x__ - y__) / y__);\
	} while (0)

/**
 * Convert CIE XYZ to CIE xyY
 * 
 * @param  X  The X parameter
 * @param  Y  The Y parameter; this is also the Y (last) parameter for the CIE xyY colour
 * @param  Z  The Z parameter
 * @param  x  Output parameter for the x parameter
 * @param  y  Output parameter for the y parameter
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_ciexyz_to_ciexyy)(double, double, double, double *, double *);
#define libclut_model_ciexyz_to_ciexyy(X, Y, Z, x, y)\
	do {\
		double X__ = (X), Y__ = (Y), Z__ = (Z), *x__ = (x), *y__ = (y);\
		double s__ = X__ + Y__ + Z__;\
		if (libclut_0__(s__))\
			*x__ = *y__ = 0;\
		else\
			*x__ = X__ / s__, *y__ = Y__ / s__;\
	} while (0)

/**
 * Convert CIE XYZ to [0, 1] linear sRGB
 * 
 * @param  X  The X parameter
 * @param  Y  The Y parameter
 * @param  Z  The Z parameter
 * @param  r  Output parameter for the red component
 * @param  g  Output parameter for the green component
 * @param  b  Output parameter for the blue component
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_ciexyz_to_linear)(double, double, double, double *, double *, double *);
#define libclut_model_ciexyz_to_linear(X, Y, Z, r, g, b)\
	do {\
		double X__ = (X), Y__ = (Y), Z__ = (Z);\
		*(r) = ( 3.2404500 * X__) + (-1.537140 * Y__) + (-0.4985320 * Z__);\
		*(g) = (-0.9692660 * X__) + ( 1.876010 * Y__) + ( 0.0415561 * Z__);\
		*(b) = ( 0.0556434 * X__) + (-0.204026 * Y__) + ( 1.0572300 * Z__);\
	} while (0)

/**
 * Convert [0, 1] linear sRGB to CIE XYZ
 * 
 * @param  r  The red component
 * @param  g  The green component
 * @param  b  The blue component
 * @param  X  Output parameter for the X parameter
 * @param  Y  Output parameter for the Y parameter
 * @param  Z  Output parameter for the Z parameter
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_linear_to_ciexyz)(double, double, double, double *, double *, double *);
#define libclut_model_linear_to_ciexyz(r, g, b, X, Y, Z)\
	do {\
		double r__ = (r), g__ = (g), b__ = (b);\
		*(X) = (0.4124564 * r__) + (0.3575761 * g__) + (0.1804375 * b__);\
		*(Y) = (0.2126729 * r__) + (0.7151522 * g__) + (0.0721750 * b__);\
		*(Z) = (0.0193339 * r__) + (0.1191920 * g__) + (0.9503041 * b__);\
	} while (0)

/**
 * Convert [0, 1] sRGB to CIE xyY
 * 
 * The macro variant requires linking with '-lclut'
 * if any of `libclut_model_ciexyz_to_ciexyy`,
 * `libclut_model_linear_to_ciexyz`, and
 * `libclut_model_standard_to_linear` are undefined.
 * The macro variant requires linking with '-lm' if
 * neither `libclut_model_standard_to_linear` nor
 * `libclut_model_standard_to_linear1` are undefined.
 * 
 * @param  r  The red component
 * @param  g  The green component
 * @param  b  The blue component
 * @param  x  Output parameter for the x parameter
 * @param  y  Output parameter for the y parameter
 * @param  Y  Output parameter for the Y parameter
 */
void (libclut_model_srgb_to_ciexyy)(double, double, double, double *, double *, double *);
#define libclut_model_srgb_to_ciexyy(r, g, b, x, y, Y)\
	do {\
		double r___ = (r), g___ = (g), b___ = (b);\
		double *x___ = (x), *y___ = (y), *Y___ = (Y);\
		double X___, Z___;\
		libclut_model_standard_to_linear(&r___, &g___, &b___);\
		libclut_model_linear_to_ciexyz(r___, g___, b___, &X___, Y___, &Z___);\
		libclut_model_ciexyz_to_ciexyy(X___, *Y___, Z___, x___, y___);\
	} while (0)

/**
 * Convert CIE xyY to [0, 1] sRGB
 * 
 * The macro variant requires linking with '-lclut'
 * if any of `libclut_model_ciexyy_to_ciexyz`,
 * `libclut_model_ciexyz_to_linear`, and
 * `libclut_model_linear_to_standard` are undefined.
 * The macro variant requires linking with '-lm' if
 * neither `libclut_model_linear_to_standard` nor
 * `libclut_model_linear_to_standard1` are undefined.
 * 
 * @param  x  The x parameter
 * @param  y  The y parameter
 * @param  Y  The Y parameter
 * @param  r  Output parameter for the red component
 * @param  g  Output parameter for the green component
 * @param  b  Output parameter for the blue component
 */
void (libclut_model_ciexyy_to_srgb)(double, double, double, double *, double *, double *);
#define libclut_model_ciexyy_to_srgb(x, y, Y, r, g, b)\
	do {\
		double x___ = (x), y___ = (y), Y___ = (Y);\
		double *r___ = (r), *g___ = (g), *b___ = (b);\
		double X___, Z___;\
		libclut_model_ciexyy_to_ciexyz(x___, y___, Y___, &X___, &Z___);\
		libclut_model_ciexyz_to_linear(X___, Y___, Z___, r___, g___, b___);\
		libclut_model_linear_to_standard(r___, g___, b___);\
	} while(0)

/**
 * Convert from CIE XYZ to CIE L*a*b*
 * 
 * The macro variant requires linking with '-lm'
 * 
 * @param  X  The X parameter
 * @param  Y  The Y parameter
 * @param  Z  The Z parameter
 * @param  L  Output parameter for the L* component
 * @param  a  Output parameter for the a* component
 * @param  b  Output parameter for the b* component
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_ciexyz_to_cielab)(double, double, double, double *, double *, double *);
#define libclut_model_ciexyz_to_cielab(X, Y, Z, L, a, b)\
	do {\
		double X__ = (X), Y__ = (Y), Z__ = (Z);\
		X__ /= 0.95047, Z__ /= 1.08883;\
		X__ = LIBCLUT_MODEL_CIEXYZ_TO_CIELAB__(X__);\
		Y__ = LIBCLUT_MODEL_CIEXYZ_TO_CIELAB__(Y__);\
		Z__ = LIBCLUT_MODEL_CIEXYZ_TO_CIELAB__(Z__);\
		*(L) = 116 * Y__ - 16;\
		*(a) = 500 * (X__ - Y__);\
		*(b) = 200 * (Y__ - Z__);\
	} while (0)
#define LIBCLUT_MODEL_CIEXYZ_TO_CIELAB__(C)\
	(((C) > 0.00885642) ? pow((C), 1.0 / 3) : ((7.78 + 703.0 / 99900) * (C) + 0.1379310))

/**
 * Convert from CIE L*a*b* to CIE XYZ
 * 
 * @param  L  The L* component
 * @param  a  The a* component
 * @param  b  The b* component
 * @param  X  Output parameter for the X parameter
 * @param  Y  Output parameter for the Y parameter
 * @param  Z  Output parameter for the Z parameter
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_cielab_to_ciexyz)(double, double, double, double *, double *, double *);
#define libclut_model_cielab_to_ciexyz(L, a, b, X, Y, Z)\
	do {\
		double L__ = (L), a__ = (a), b__ = (b);\
		double *X__ = (X), *Y__ = (Y), *Z__ = (Z);\
		*Y__ = (L__ + 16) / 116;\
		*X__ = a__ / 500 + *Y__;\
		*Z__ = *Y__ - b__ / 200;\
		*X__ = LIBCLUT_MODEL_CIELAB_TO_CIEXYZ__(*X__) * 0.95047;\
		*Y__ = LIBCLUT_MODEL_CIELAB_TO_CIEXYZ__(*Y__) * 1.08883;\
		*Z__ = LIBCLUT_MODEL_CIELAB_TO_CIEXYZ__(*Z__);\
	} while (0)
#define LIBCLUT_MODEL_CIELAB_TO_CIEXYZ__(C)\
	(((C)*(C)*(C) > 0.00885642) ? ((C)*(C)*(C)) : (((C) - 0.1379310) / (7.78 + 703.0 / 99900)))

/**
 * Convert from CIE XYZ to CIELUV
 * 
 * Requires linking with `-lm`
 * 
 * @param  X   The X component
 * @param  Y   The Y component
 * @param  Z   The Z component
 * @param  Xn  The X component of the specified white object (white point)
 * @param  Yn  The Y component of the specified white object (white point)
 * @param  Zn  The Z component of the specified white object (white point)
 * @param  L   Output parameter for the L* parameter
 * @param  u   Output parameter for the u* parameter
 * @param  v   Output parameter for the v* parameter
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_ciexyz_to_cieluv)(double, double, double, double, double, double, double *, double *, double *);
#define libclut_model_ciexyz_to_cieluv(X, Y, Z, Xn, Yn, Zn, L, u, v)\
	do {\
		double xn__ = (Xn), yn__ = (Yn);\
		double t__ = xn__ + 15 * yn__ + 3 * (Zn);\
		double u__ = 4 * xn__ / t__;\
		double v__ = 9 * yn__ / t__;\
		double x__ = (X), y__ = (Y);\
		t__ = x__ + 15 * y__ + 3 * (Z);\
		u__ = 4 * x__ / t__ - u__;\
		v__ = 9 * y__ / t__ - v__;\
		y__ /= yn__;\
		if (y__ * 24389 <= (double)216)\
			y__ *= 24389, y__ /= 27;\
		else\
			y__ = cbrt(y__) * 116 - 16;\
		*(L) = y__;\
		y__ *= 13;\
		*(u) = y__ * u__;\
		*(v) = y__ * v__;\
	} while (0)

/**
 * Convert from CIELUV to CIE XYZ
 * 
 * @param  L   The L* component
 * @param  u   The u* component
 * @param  v   The v* component
 * @param  Xn  The X component of the specified white object (white point)
 * @param  Yn  The Y component of the specified white object (white point)
 * @param  Zn  The Z component of the specified white object (white point)
 * @param  X   Output parameter for the X parameter
 * @param  Y   Output parameter for the Y parameter
 * @param  Z   Output parameter for the Z parameter
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_cieluv_to_ciexyz)(double, double, double, double, double, double, double *, double *, double *);
#define libclut_model_cieluv_to_ciexyz(L, u, v, Xn, Yn, Zn, X, Y, Z)\
	do {\
		double x__ = (Xn), y__ = (Yn), l__ = (L), l13__ = l__ * 13;\
		double t__ = x__ + 15 * y__ + 3 * (Zn);\
		double u__ = 4 * x__ / t__;\
		double v__ = 9 * y__ / t__;\
		u__ = (u) / l13__ + u__;\
		v__ = (v) / l13__ + v__;\
		if (l__ <= (double)8) {\
			y__ *= l__ * 27 / 24389;\
		} else {\
			l__ += 16, l__ /= 116;\
			y__ *= l__ *= l__ * l__;\
		}\
		*(Y) = y__;\
		*(X) = y__ * 9 * u__ / (4 * v__);\
		*(Z) = y__ * (12 - 3 * u__ - 20 * v__) / (4 * v__);\
	} while (0)

/**
 * Convert from CIELCh to CIE L*u*v*
 * 
 * Requires linking with `-lm`
 * 
 * @param  C  The C*_uv component
 * @param  h  The h_uv component
 * @param  u  Output parameter for the u* parameter
 * @param  v  Output parameter for the v* parameter
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_cielch_to_cieluv)(double, double, double *, double *);
#define libclut_model_cielch_to_cieluv(C, h, u, v)\
	do {\
		double h__ = (h), C__ = (C);\
		*(v) = sin(h__) * C__;\
		*(u) = cos(h__) * C__;\
	} while (0)

/**
 * Convert from CIE L*u*v* to CIELCh
 * 
 * Requires linking with `-lm`
 * 
 * @param  u  The u* component
 * @param  v  The v* component
 * @param  C  Output parameter for the C*_uv parameter
 * @param  h  Output parameter for the h_uv parameter
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_cieluv_to_cielch)(double, double, double *, double *);
#define libclut_model_cieluv_to_cielch(u, v, C, h)\
	do {\
		double u__ = (u), v__ = (v);\
		*(C) = sqrt(u__ * u__ + v__ * v__);\
		*(h) = atan2(v__, u__);	\
	} while (0)

/**
 * Convert from sRGB to YIQ
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_standard_to_linear1` is not undefined
 * 
 * @param  r  The R component
 * @param  g  The G component
 * @param  b  The B component
 * @param  y  Output parameter for the Y parameter
 * @param  i  Output parameter for the I parameter
 * @param  q  Output parameter for the Q parameter
 */
void (libclut_model_srgb_to_yiq)(double, double, double, double *, double *, double *);
#define libclut_model_srgb_to_yiq(r, g, b, y, i, q)\
	do {\
		double r__ = libclut_model_standard_to_linear1(r);\
		double g__ = libclut_model_standard_to_linear1(g);\
		double b__ = libclut_model_standard_to_linear1(b);\
		*(y) = r__ * 0.29893602129377540 + g__ * 0.58704307445112120 + b__ * 0.11402090425510336;\
		*(i) = r__ * 0.59594574307079930 - g__ * 0.27438863574578920 - b__ * 0.32155710732501010;\
		*(q) = r__ * 0.21149734030682846 - g__ * 0.52291069030297390 + b__ * 0.31141334999614540;\
	} while (0)

/**
 * Convert from YIQ to sRGB
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_linear_to_standard1` is not undefined
 * 
 * @param  y  The Y component
 * @param  i  The I component
 * @param  q  The Q component
 * @param  r  Output parameter for the R parameter
 * @param  g  Output parameter for the G parameter
 * @param  b  Output parameter for the B parameter
 */
void (libclut_model_yiq_to_srgb)(double, double, double, double *, double *, double *);
#define libclut_model_yiq_to_srgb(y, i, q, r, g, b)\
	do {\
		double y__ = (y), i__ = (i), q__ = (q), r__, g__, b__;\
		r__ = y__ + i__ *  956 / 1000 + q__ *  621 / 1000;\
		g__ = y__ - i__ *  272 / 1000 - q__ *  647 / 1000;\
		b__ = y__ - i__ * 1106 / 1000 + q__ * 1703 / 1000;\
		*(r) = libclut_model_linear_to_standard1(r__);\
		*(g) = libclut_model_linear_to_standard1(g__);\
		*(b) = libclut_model_linear_to_standard1(b__);\
	} while (0)

/**
 * Convert from sRGB to YDbDr
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_standard_to_linear1` is not undefined
 * 
 * @param  r   The R component
 * @param  g   The G component
 * @param  b   The B component
 * @param  y   Output parameter for the Y parameter
 * @param  db  Output parameter for the Db parameter
 * @param  dr  Output parameter for the Dr parameter
 */
void (libclut_model_srgb_to_ydbdr)(double, double, double, double *, double *, double *);
#define libclut_model_srgb_to_ydbdr(r, g, b, y, db, dr)\
	do {\
		double r__ = libclut_model_standard_to_linear1(r);\
		double g__ = libclut_model_standard_to_linear1(g);\
		double b__ = libclut_model_standard_to_linear1(b);\
		*(y)  =  r__ *  299 / 1000 + g__ *  587 / 1000 + b__ *  114 / 1000;\
		*(db) = -r__ *  450 / 1000 - g__ *  883 / 1000 + b__ * 1333 / 1000;\
		*(dr) = -r__ * 1333 / 1000 + g__ * 1116 / 1000 + b__ *  217 / 1000;\
	} while (0)

/**
 * Convert from YDbDr to sRGB
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_linear_to_standard1` is not undefined
 * 
 * @param  y   The Y component
 * @param  db  The Db component
 * @param  dr  The Dr component
 * @param  r   Output parameter for the R parameter
 * @param  g   Output parameter for the G parameter
 * @param  b   Output parameter for the B parameter
 */
void (libclut_model_ydbdr_to_srgb)(double, double, double, double *, double *, double *);
#define libclut_model_ydbdr_to_srgb(y, db, dr, r, g, b)\
	do {\
		double y__ = (y), db__ = (db), dr__ = (dr), r__, g__, b__;\
		db__ /= 1000000000000000ULL;\
		dr__ /= 1000000000000000ULL;\
		r__ = y__ + db__ *     92303716148ULL - dr__ * 525912630661865ULL;\
		g__ = y__ - db__ * 129132898890509ULL + dr__ * 267899328207599ULL;\
		b__ = y__ + db__ * 664679059978955ULL - dr__ *     79202543533ULL;\
		*(r) = libclut_model_linear_to_standard1(r__);\
		*(g) = libclut_model_linear_to_standard1(g__);\
		*(b) = libclut_model_linear_to_standard1(b__);\
	} while (0)

/**
 * Convert from YUV to YDbDr
 * 
 * @param  u   The U component
 * @param  v   The V component
 * @param  db  Output parameter for the Db parameter
 * @param  dr  Output parameter for the Dr parameter
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_yuv_to_ydbdr)(double, double, double *, double *);
#define libclut_model_yuv_to_ydbdr(u, v, db, dr)\
	do {\
		*(db) =  3069 * (u) / 1000;\
		*(dr) = -2169 * (v) / 1000;\
	} while (0)

/**
 * Convert from YDbDr to YUV
 * 
 * @param  db  The Db component
 * @param  dr  The Dr component
 * @param  u   Output parameter for the U parameter
 * @param  v   Output parameter for the V parameter
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_ydbdr_to_yuv)(double, double, double *, double *);
#define libclut_model_ydbdr_to_yuv(db, dr, u, v)\
	do {\
		*(u) = (db) * 1000 /  3069;\
		*(v) = (dr) * 1000 / -2169;\
	} while (0)

/**
 * Convert from sRGB to YPbPr
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_standard_to_linear1` is not undefined
 * 
 * @param  r   The R component
 * @param  g   The G component
 * @param  b   The B component
 * @param  y   Output parameter for the Y parameter
 * @param  pb  Output parameter for the Pb parameter
 * @param  pr  Output parameter for the Pr parameter
 */
void (libclut_model_srgb_to_ypbpr)(double, double, double, double *, double *, double *);
#define libclut_model_srgb_to_ypbpr(r, g, b, y, pb, pr)\
	do {\
		double r__ = libclut_model_standard_to_linear1(r);\
		double g__ = libclut_model_standard_to_linear1(g);\
		double b__ = libclut_model_standard_to_linear1(b);\
		double y__;\
		y__  = r__ * 2126 / 10000;\
		y__ += g__ * 7152 / 10000;\
		y__ += b__ *  722 / 10000;\
		*(y) = y__;\
		*(pb) = b__ - y__;\
		*(pr) = r__ - y__;\
	} while (0)

/**
 * Convert from YPbPr to sRGB
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_linear_to_standard1` is not undefined
 * 
 * @param  y   The Y component
 * @param  pb  The Pb component
 * @param  pr  The Pr component
 * @param  r   Output parameter for the R parameter
 * @param  g   Output parameter for the G parameter
 * @param  b   Output parameter for the B parameter
 */
void (libclut_model_ypbpr_to_srgb)(double, double, double, double *, double *, double *);
#define libclut_model_ypbpr_to_srgb(y, pb, pr, r, g, b)\
	do {\
		double y__ = (y), r__ = (pr) + y__, g__, b__ = (pb) + y__;\
		y__ -= r__ * 2126 / 10000;\
		y__ -= b__ * 722 / 10000;\
		g__ = y__ * 10000 / 7152;\
		*(r) = libclut_model_linear_to_standard1(r__);\
		*(g) = libclut_model_linear_to_standard1(g__);\
		*(b) = libclut_model_linear_to_standard1(b__);\
	} while (0)

/**
 * Convert from sRGB to YCgCo
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_standard_to_linear1` is not undefined
 * 
 * @param  r   The R component
 * @param  g   The G component
 * @param  b   The B component
 * @param  y   Output parameter for the Y parameter
 * @param  cg  Output parameter for the Cg parameter
 * @param  co  Output parameter for the Co parameter
 */
void (libclut_model_srgb_to_ycgco)(double, double, double, double *, double *, double *);
#define libclut_model_srgb_to_ycgco(r, g, b, y, cg, co)\
	do {\
		double r__ = libclut_model_standard_to_linear1(r);\
		double g__ = libclut_model_standard_to_linear1(g);\
		double b__ = libclut_model_standard_to_linear1(b);\
		*(y)  = r__ /  4 + g__ / 2 + b__ / 4;\
		*(cg) = r__ / -4 + g__ / 2 - b__ / 4;\
		*(co) = r__ /  2           - b__ / 2;\
	} while (0)

/**
 * Convert from YCgCo to sRGB
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_linear_to_standard1` is not undefined
 * 
 * @param  y   The Y component
 * @param  cg  The Cg component
 * @param  co  The Co component
 * @param  r   Output parameter for the R parameter
 * @param  g   Output parameter for the G parameter
 * @param  b   Output parameter for the B parameter
 */
void (libclut_model_ycgco_to_srgb)(double, double, double, double *, double *, double *);
#define libclut_model_ycgco_to_srgb(y, cg, co, r, g, b)\
	do {\
		double y__ = (y), cg__ = (cg), co__ = (co);\
		double r__ = y__ - cg__ + co__;\
		double g__ = y__ + cg__;\
		double b__ = y__ - cg__ - co__;\
		*(r) = libclut_model_linear_to_standard1(r__);\
		*(g) = libclut_model_linear_to_standard1(g__);\
		*(b) = libclut_model_linear_to_standard1(b__);\
	} while (0)

/**
 * Convert from CIE 1960 UCS to CIE XYZ
 * 
 * @param  u  The u component
 * @param  v  The v component
 * @param  Y  The Y component
 * @param  x  Output parameter for the X parameter
 * @param  y  Output parameter for the Y parameter
 * @param  z  Output parameter for the Z parameter
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_cie_1960_ucs_to_ciexyz)(double, double, double, double *, double *, double *);
#define libclut_model_cie_1960_ucs_to_ciexyz(u, v, Y, x, y, z)\
	do {\
		double u__ = (u), v__ = (v), y__ = (Y);\
		*(y) = y__;\
		*(x) = 3 * y__ * u__ / (2 * v__);\
		*(z) = (4 * y__ - y__ * u__ - 10 * y__ * v__) / (2 * v__);\
	} while (0)

/**
 * Convert from CIE XYZ to CIE 1960 UCS
 * 
 * @param  x  The X component
 * @param  y  The Y component
 * @param  z  The Z component
 * @param  u  Output parameter for the u parameter
 * @param  v  Output parameter for the v parameter
 * @param  Y  Output parameter for the Y parameter
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_ciexyz_to_cie_1960_ucs)(double, double, double, double *, double *, double *);
#define libclut_model_ciexyz_to_cie_1960_ucs(x, y, z, u, v, Y)\
	do {\
		double x__ = (x), y__ = (y);\
		double d__ = x__ + 15 * y__ + 3 * (z);\
		*(u) = 4 * x__ / d__;\
		*(v) = 6 * y__ / d__;\
		*(Y) = y__;\
	} while (0)

/**
 * Convert from CIEUVW to CIE 1960 UCS
 * 
 * @param  U:double   The U* component
 * @param  V:double   The V* component
 * @param  W:double   The W* component
 * @param  u0:double  The u parameter for the white point
 * @param  v0:double  The v parameter for the white point
 * @param  u:double*  Output parameter for the u parameter
 * @param  v:double*  Output parameter for the v parameter
 * @param  Y:double*  Output parameter for the Y parameter
 */
LIBCLUT_GCC_ONLY__(__attribute__((__leaf__)))
void (libclut_model_cieuvw_to_cie_1960_ucs)(double, double, double, double, double, double *, double *, double *);
#define libclut_model_cieuvw_to_cie_1960_ucs(U, V, W, u0, v0, u, v, Y)\
	do {\
		double w__ = (W), y__ = (w__ + 17) / 25;\
		*(Y) = y__ *= y__ * y__;\
		w__ *= 13;\
		*(u) = (U) / w__ + (u0);\
		*(v) = (V) / w__ + (v0);\
	} while (0)

/**
 * Convert from CIE 1960 UCS to CIEUVW
 * 
 * Requires linking with `-lm`
 * 
 * @param  u:double   The u component
 * @param  v:double   The v component
 * @param  Y:double   The Y component
 * @param  u0:double  The u parameter for the white point
 * @param  v0:double  The v parameter for the white point
 * @param  U:double*  Output parameter for the U* parameter
 * @param  V:double*  Output parameter for the V* parameter
 * @param  W:double*  Output parameter for the W* parameter
 */
void (libclut_model_cie_1960_ucs_to_cieuvw)(double, double, double, double, double, double *, double *, double *);
#define libclut_model_cie_1960_ucs_to_cieuvw(u, v, Y, u0, v0, U, V, W)\
	do {\
		double w__ = 25 * cbrt(Y) - 17;\
		*(W) = w__;\
		w__ *= 13;\
		*(U) = w__ * ((u) - (u0));\
		*(V) = w__ * ((v) - (v0));\
	} while (0)

/**
 * Create a matrix for converting values between
 * two RGB colour spaces
 * 
 * @param   from  The input colour space, the Y-component is only necessary
 *                for the white point, `NULL` for CIE XYZ
 * @param   to    The output colour space, the Y-component is only necessary
 *                for the white point, `NULL` for CIE XYZ
 * @param   M     Output matrix for conversion from `from` to `to`
 * @param   Minv  Output matrix for conversion from `to` to `from`, may be `NULL`
 * @return        Zero on success, -1 on error
 * 
 * @throws  EINVAL  The colour space cannot be used
 */
int libclut_model_get_rgb_conversion_matrix(const libclut_rgb_colour_space_t *, const libclut_rgb_colour_space_t *,
                                            libclut_colour_space_conversion_matrix_t, libclut_colour_space_conversion_matrix_t);

/**
 * Convert an RGB colour into another RGB colour space
 * 
 * Both RGB colour spaces must have same gamma functions as sRGB
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_standard_to_linear1` or
 * `libclut_model_linear_to_standard1` is not undefined
 * 
 * @param  r      The red component of the colour to convert
 * @param  g      The green component of the colour to convert
 * @param  b      The blue component of the colour to convert
 * @param  M      Conversion matrix, create with `libclut_model_get_rgb_conversion_matrix`,
 *                must not have side-effects
 * @param  out_r  Output parameter for the new red component
 * @param  out_g  Output parameter for the new green component
 * @param  out_b  Output parameter for the new blue component
 */
void (libclut_model_convert_rgb)(double, double, double, libclut_colour_space_conversion_matrix_t, double *, double *, double *);
#define libclut_model_convert_rgb(r, g, b, M, out_r, out_g, out_b)\
	do {\
		double r___ = libclut_model_standard_to_linear1(r);\
		double g___ = libclut_model_standard_to_linear1(g);\
		double b___ = libclut_model_standard_to_linear1(b);\
		*(out_r) = libclut_model_linear_to_standard1((M)[0][0] * r___ + (M)[0][1] * g___ + (M)[0][2] * b___);\
		*(out_g) = libclut_model_linear_to_standard1((M)[1][0] * r___ + (M)[1][1] * g___ + (M)[1][2] * b___);\
		*(out_b) = libclut_model_linear_to_standard1((M)[2][0] * r___ + (M)[2][1] * g___ + (M)[2][2] * b___);\
	} while (0)

/**
 * Convert an RGB colour of a custom RGB colour space to CIE XYZ
 * 
 * The RGB colour space must have same gamma functions as sRGB
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_standard_to_linear1` is not undefined
 * 
 * @param  r  The red component
 * @param  g  The green component
 * @param  b  The blue component
 * @param  M  Conversion matrix, create with `libclut_model_get_rgb_conversion_matrix`,
 *            must not have side-effects
 * @param  x  Output parameter for the X component
 * @param  y  Output parameter for the Y component
 * @param  z  Output parameter for the Z component
 */
void (libclut_model_rgb_to_ciexyz)(double, double, double, libclut_colour_space_conversion_matrix_t, double *, double *, double *);
#define libclut_model_rgb_to_ciexyz(r, g, b, M, x, y, z)\
	do {\
		double r__ = libclut_model_standard_to_linear1(r);\
		double g__ = libclut_model_standard_to_linear1(g);\
		double b__ = libclut_model_standard_to_linear1(b);\
		*(x) = (M)[0][0] * r__ + (M)[0][1] * g__ + (M)[0][2] * b__;\
		*(y) = (M)[1][0] * r__ + (M)[1][1] * g__ + (M)[1][2] * b__;\
		*(z) = (M)[2][0] * r__ + (M)[2][1] * g__ + (M)[2][2] * b__;\
	} while (0)

/**
 * Convert a CIE XYZ colour to a custom RGB colour space
 * 
 * The RGB colour space must have same gamma functions as sRGB
 * 
 * Requires linking with '-lclut', or '-lm' if
 * `libclut_model_linear_to_standard1` is not undefined
 * 
 * @param  x  The X component
 * @param  y  The Y component
 * @param  z  The Z component
 * @param  M  Conversion matrix, create with `libclut_model_get_rgb_conversion_matrix`,
 *            must not have side-effects.
 * @param  r  Output parameter for the red component
 * @param  g  Output parameter for the green component
 * @param  b  Output parameter for the blue component
 */
void (libclut_model_ciexyz_to_rgb)(double, double, double, libclut_colour_space_conversion_matrix_t, double *, double *, double *);
#define libclut_model_ciexyz_to_rgb(x, y, z, M, r, g, b)\
	do {\
		double x__ = (x), y__ = (y), z__ = (z);\
		*(r) = libclut_model_linear_to_standard1((M)[0][0] * x__ + (M)[0][1] * y__ + (M)[0][2] * z__);\
		*(g) = libclut_model_linear_to_standard1((M)[1][0] * x__ + (M)[1][1] * y__ + (M)[1][2] * z__);\
		*(b) = libclut_model_linear_to_standard1((M)[2][0] * x__ + (M)[2][1] * y__ + (M)[2][2] * z__);\
	} while (0)

#if defined(__clang__)
# pragma GCC diagnostic pop
#endif

#endif