1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
|
/*
* International Color Consortium color transform expanded support
*
* Author: Graeme W. Gill
* Date: 2/7/00
* Version: 1.00
*
* Copyright 2000, 2001 Graeme W. Gill
* All rights reserved.
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
* see the License.txt file for licencing details.
*
* This is the third major version of xlut.c (originally called xlut2.c)
* Based on the old xlut.c code (preserved as xlut1.c)
* This version uses xfit.c to do the curve and rspl fitting.
*/
/*
* This module provides the expanded icclib functionality
* for lut based profiles.
* This file is #included in xicc.c, to keep its functions private.
*
* This version creates both input and output 1D luts by
* optimising the accuracy of the profile for a linear clut.
*
*/
/*
TTBD:
See gamut/gammap.c for notes on the desirability of
a non-minimum delta E colorimetric intent as the default.
Should fix gamut's so that they have enough information
to spefify a gamut mapping without the need for
a source colorspace profile, and fix the code
to work with just a gamut. This would take us further
towards supporting the PRMG reference gamut interoperability
as an option.
Should the input profile white point determination
be made a bit smarter about determining the chromaticity ?
ie. not take on the tint of the whitest patch, but an
average of neutral patches ?
Should xlutfix.c be revived (also adding ICM_CLUT_SET_APXLS support),
to improve "bumpy black" problem ?
*/
/*
NOTE :- an alternative to the way display profile absolute is handled here
would be to always chromatically adapt the illuminant to D50, and encode
that in the Chromatic adapation tag. To make absolute colorimetric
do anything useful though, the chromatic adapation tag would
have to be used for absolute intent.
This may be the way of improving compatibility with other systems,
and is needed for V4, but would break compatibility with existing
Argyll profiles, unless special measures are taken:
ie.
1) if (display profile & using chromatic adaptation tag)
Create Bradford chromatic adapation matrix and store it in tag
Adapt all the readings using Bradford
Create white point and store it in tag (white point will be D50)
Adapt all the readings to the white point using wrong Von-Kries (== NOOP)
Store relative colorimetric cLUT
Set version >= 2.4
else
2) if (display scheme A or using Argyll historical printer scheme)
Create white point and store it in tag
Adapt all the readings to the white point using Bradford
Store relative colorimetric tag
Set version < 2.4 for V2 profile
Add private Absolute Transform Matrix (labels V4 profile)
3) else (display scheme B or strict ICC printer compatibility)
Create white point and store it in tag
Adapt all the readings to the white point using Wrong Von-Kries
Store relative colorimetric tag
Set version >= 2.4
Argyll Processing for each type
1) if display and chromatic adapation matrix
Un-adapt matrix or cLUT using wrong Von-Kries from white point
Un-adapt matrix or cLUT using chromatic matrix
Un-adapt apparant white point & black point using chromatic transform
if (not absolute intent)
Create Bradford transfor from white to PCS D50
Adapt all matrix or cLUT
else
2) if (display scheme A or using Argyll < V2.4. profile
or find Absolute Transform Matrix)
if (absolute intent)
Un-adapt matrix or cLUT using Bradford from white point
3) else (display scheme B or !Argyll profile or ( >= V2.4 profile
and !Absolute Transform Matrix))
Un-adapt matrix or cLUT using wrong Von-Kries from white point
if (not absolute intent)
Create Bradford transfor from white to PCS D50
Adapt all matrix or cLUT to white
The problem with this is that it wouldn't do the right thing on old Argyll
type profiles that weren't labeled or recognized.
Is there a way of recognizing Bradford Absolute transform Matricies if
the color chromaticities are given ?
*/
/*
A similar condrum is that it seems that an unwritten convention for
V2 profiles is to scale the black point of the perceptual and
saturation tables to 0 (Part of the V4 spec is to scale to Y = 3.1373).
To get better gamut mapping we should therefore unscale the perceptual
and saturation A2B table to have the same black point as the colorimetric
table before computing the gamut mapping, and then apply the opposite
transform to our perceptual B2A and A2B tables.
*/
/*
There is interesting behaviour for Jab 0,0,0 in, in that
it gets mapped to (something like) Lab -1899.019855 -213.574625 -231.914285
which after per-component clipping of the inv out tables looks like
0, -128, -128, which may be mapped to (say) RGB 0.085455 1.000000 0.936951,
which is not black.
*/
#include "xfit.h"
#undef USE_CIE94_DE /* [Undef] Use CIE94 delta E measure when creating in/out curves */
/* Don't use CIE94 because it makes peak error worse ? */
#undef DEBUG /* [Undef] Verbose debug information */
#undef CNDTRACE /* [Undef] Enable xicc->trace conditional verbosity */
#undef DEBUG_OLUT /* [Undef] Print steps in overall fwd & rev lookups */
#undef DEBUG_RLUT /* [Undef] Print values being reverse lookup up */
#undef DEBUG_SPEC /* [Undef] Debug some specific cases */
#undef INK_LIMIT_TEST /* [Undef] Turn off input tables for ink limit testing */
#undef CHECK_ILIMIT /* [Undef] Do sanity checks on meeting ink limit */
#undef WARN_CLUT_CLIPPING /* [Undef] Print warning if setting clut clips */
#undef DISABLE_KCURVE_FILTER /* [Undef] don't filter the Kcurve */
#undef REPORT_LOCUS_SEGMENTS /* [Undef[ Examine how many segments there are in aux inversion */
#define XYZ_EXTRA_SMOOTH 20.0 /* Extra smoothing factor for XYZ profiles */
#define SHP_SMOOTH 1.0 /* Input shaper curve smoothing */
#define OUT_SMOOTH1 1.0 /* Output shaper curve smoothing for L*, X,Y,Z */
#define OUT_SMOOTH2 1.0 /* Output shaper curve smoothing for a*, b* */
#define CAMCLIPTRANS 1.0 /* [1.0] Cam clipping transition region Delta E */
/* Should this be smaller ? */
#undef USECAMCLIPSPLINE /* [Und] use spline blend between PCS and Jab */
#define CCJSCALE 2.0 /* [2.0] Amount to emphasize J delta E in in computing clip */
/*
* TTBD:
*
* Reverse lookup of Lab
* Make NEARCLIP the default ??
*
* XYZ vector clipping isn't implemented.
*
* Some of the error handling is crude. Shouldn't use
* error(), should return status.
*/
#ifndef _CAT2
#define _CAT2(n1,n2) n1 ## n2
#define CAT2(n1,n2) _CAT2(n1,n2)
#endif
static double icxLimitD(icxLuLut *p, double *in); /* For input' */
#define icxLimitD_void ((double (*)(void *, double *))icxLimitD) /* Cast with void 1st arg */
static double icxLimit(icxLuLut *p, double *in); /* For input */
static int icxLuLut_init_clut_camclip(icxLuLut *p);
/* Debug overall lookup */
#ifdef DEBUG_OLUT
#undef DBOL
#ifdef CNDTRACE
#define DBOL(xxx) if (p->trace) printf xxx ;
#else
#define DBOL(xxx) printf xxx ;
#endif
#else
#undef DBOL
#define DBOL(xxx)
#endif
/* Debug reverse lookup */
#ifdef DEBUG_RLUT
#undef DBR
#ifdef CNDTRACE
#define DBR(xxx) if (p->trace) printf xxx ;
#else
#define DBR(xxx) printf xxx ;
#endif
#else
#undef DBR
#define DBR(xxx)
#endif
/* Debug some specific cases (fwd_relpcs_outpcs, bwd_outpcs_relpcs) */
#ifdef DEBUG_SPEC
# undef DBS
# ifdef CNDTRACE
# define DBS(xxx) if (p->trace) printf xxx ;
# else
# define DBS(xxx) printf xxx ;
# endif
#else
# undef DBS
# define DBS(xxx)
#endif
/* ========================================================== */
/* xicc lookup code. */
/* ========================================================== */
/* Forward and Backward Multi-Dimensional Interpolation type conversion */
/* Return 0 on success, 1 if clipping occured, 2 on other error */
/* Components of overall lookup, in order */
int icxLuLut_in_abs(icxLuLut *p, double *out, double *in) {
int rv = 0;
if (p->ins == icxSigJabData) {
DBOL(("xlut in_abs: CAM in = %s\n", icmPdv(p->inputChan, in)));
p->cam->cam_to_XYZ(p->cam, out, in);
DBOL(("xlut in_abs: XYZ = %s\n", icmPdv(p->inputChan, out)));
/* Hack to prevent CAM02 weirdness being amplified by inv_abs() */
/* or any later per channel clipping. */
/* Limit -Y to non-stupid values by scaling */
if (out[1] < -0.1) {
out[0] *= -0.1/out[1];
out[2] *= -0.1/out[1];
out[1] = -0.1;
DBOL(("xlut in_abs: after clipping -Y %s\n",icmPdv(p->outputChan, out)));
}
rv |= ((icmLuLut *)p->plu)->in_abs((icmLuLut *)p->plu, out, out);
DBOL(("xlut in_abs: XYZ out = %s\n", icmPdv(p->inputChan, out)));
} else {
DBOL(("xlut in_abs: PCS in = %s\n", icmPdv(p->inputChan, in)));
rv |= ((icmLuLut *)p->plu)->in_abs((icmLuLut *)p->plu, out, in);
DBOL(("xlut in_abs: PCS out = %s\n", icmPdv(p->inputChan, out)));
}
return rv;
}
/* Possible matrix lookup */
/* input->input (not distinguishing matrix altered input values) */
int icxLuLut_matrix(icxLuLut *p, double *out, double *in) {
int rv = 0;
rv |= ((icmLuLut *)p->plu)->matrix((icmLuLut *)p->plu, out, in);
return rv;
}
/* Do input -> input' lookup */
int icxLuLut_input(icxLuLut *p, double *out, double *in) {
#ifdef NEVER
return ((icmLuLut *)p->plu)->input((icmLuLut *)p->plu, out, in);
#else
int rv = 0;
co tc;
int i;
for (i = 0; i < p->inputChan; i++) {
tc.p[0] = in[i];
rv |= p->inputTable[i]->interp(p->inputTable[i], &tc);
out[i] = tc.v[0];
}
return rv;
#endif
}
/* Do input'->output' lookup, with aux' return */
/* (The aux' is just extracted from the in' values) */
int icxLuLut_clut_aux(icxLuLut *p,
double *out, /* output' value */
double *oink, /* If not NULL, return amount input is over the ink limit, 0 if not */
double *auxv, /* If not NULL, return aux value used (packed) */
double *in /* input' value */
) {
int rv = 0;
co tc;
int i;
for (i = 0; i < p->inputChan; i++)
tc.p[i] = in[i];
rv |= p->clutTable->interp(p->clutTable, &tc);
for (i = 0; i < p->outputChan; i++)
out[i] = tc.v[i];
if (auxv != NULL) {
int ee = 0;
for (i = 0; i < p->clutTable->di; i++) {
double v = in[i];
if (p->auxm[i] != 0) {
auxv[ee] = v;
ee++;
}
}
}
if (oink != NULL) {
double lim = 0.0;
if (p->ink.tlimit >= 0.0 || p->ink.klimit >= 0.0) {
lim = icxLimitD(p, in);
if (lim < 0.0)
lim = 0.0;
}
*oink = lim;
}
return rv;
}
/* Do input'->output' lookup */
int icxLuLut_clut(icxLuLut *p, double *out, double *in) {
#ifdef NEVER
return ((icmLuLut *)p->plu)->clut((icmLuLut *)p->plu, out, in);
#else
return icxLuLut_clut_aux(p, out, NULL, NULL, in);
#endif
}
/* Do output'->output lookup */
int icxLuLut_output(icxLuLut *p, double *out, double *in) {
int rv = 0;
if (p->mergeclut == 0) {
#ifdef NEVER
rv = ((icmLuLut *)p->plu)->output((icmLuLut *)p->plu, out, in);
#else
co tc;
int i;
for (i = 0; i < p->outputChan; i++) {
tc.p[0] = in[i];
rv |= p->outputTable[i]->interp(p->outputTable[i], &tc);
out[i] = tc.v[0];
}
#endif
} else {
int i;
for (i = 0; i < p->outputChan; i++)
out[i] = in[i];
}
return rv;
}
/* Relative to absolute conversion + PCS to PCS override (Effective PCS) conversion */
int icxLuLut_out_abs(icxLuLut *p, double *out, double *in) {
int rv = 0;
if (p->mergeclut == 0) {
DBOL(("xlut out_abs: PCS in = %s\n", icmPdv(p->outputChan, in)));
rv |= ((icmLuLut *)p->plu)->out_abs((icmLuLut *)p->plu, out, in);
DBOL(("xlut out_abs: ABS PCS out = %s\n", icmPdv(p->outputChan, out)));
if (p->outs == icxSigJabData) {
p->cam->XYZ_to_cam(p->cam, out, out);
DBOL(("xlut out_abs: CAM out = %s\n", icmPdv(p->outputChan, out)));
}
} else {
int i;
for (i = 0; i < p->outputChan; i++)
out[i] = in[i];
}
return rv;
}
/* Overall lookup */
static int
icxLuLut_lookup (
icxLuBase *pp, /* This */
double *out, /* Vector of output values */
double *in /* Vector of input values */
) {
icxLuLut *p = (icxLuLut *)pp;
int rv = 0;
double temp[MAX_CHAN];
DBOL(("xicclu: in = %s\n", icmPdv(p->inputChan, in)));
rv |= p->in_abs (p, temp, in);
DBOL(("xicclu: after abs = %s\n", icmPdv(p->inputChan, temp)));
rv |= p->matrix (p, temp, temp);
DBOL(("xicclu: after matrix = %s\n", icmPdv(p->inputChan, temp)));
rv |= p->input (p, temp, temp);
DBOL(("xicclu: after inout = %s\n", icmPdv(p->inputChan, temp)));
rv |= p->clut (p, out, temp);
DBOL(("xicclu: after clut = %s\n", icmPdv(p->outputChan, out)));
if (p->mergeclut == 0) {
rv |= p->output (p, out, out);
DBOL(("xicclu: after output = %s\n", icmPdv(p->outputChan, out)));
rv |= p->out_abs (p, out, out);
DBOL(("xicclu: after outabs = %s\n", icmPdv(p->outputChan, out)));
}
return rv;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Given a relative XYZ or Lab PCS value, convert in the fwd direction into */
/* the nominated output PCS (ie. Absolute, Jab etc.) */
/* (This is used in generating gamut compression in B2A tables) */
void icxLuLut_fwd_relpcs_outpcs(
icxLuBase *pp,
icColorSpaceSignature is, /* Input space, XYZ or Lab */
double *out, double *in) {
icxLuLut *p = (icxLuLut *)pp;
/* Convert to the ICC PCS */
if (is == icSigLabData && p->natpcs == icSigXYZData) {
DBS(("fwd_relpcs_outpcs: Lab in = %s\n", icmPdv(p->inputChan, in)));
icmLab2XYZ(&icmD50, out, in);
DBS(("fwd_relpcs_outpcs: XYZ = %s\n", icmPdv(p->inputChan, out)));
} else if (is == icSigXYZData && p->natpcs == icSigLabData) {
DBS(("fwd_relpcs_outpcs: XYZ in = %s\n", icmPdv(p->inputChan, in)));
icmXYZ2Lab(&icmD50, out, in);
DBS(("fwd_relpcs_outpcs: Lab = %s\n", icmPdv(p->inputChan, out)));
} else {
DBS(("fwd_relpcs_outpcs: PCS in = %s\n", icmPdv(p->inputChan, in)));
icmCpy3(out, in);
}
/* Convert to absolute */
((icmLuLut *)p->plu)->out_abs((icmLuLut *)p->plu, out, out);
DBS(("fwd_relpcs_outpcs: abs PCS = %s\n", icmPdv(p->inputChan, out)));
if (p->outs == icxSigJabData) {
/* Convert to CAM */
p->cam->XYZ_to_cam(p->cam, out, out);
DBS(("fwd_relpcs_outpcs: Jab = %s\n", icmPdv(p->inputChan, out)));
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Components of inverse lookup, in order */
/* Utility function - compute the clip vector direction. */
/* return NULL if vector clip isn't used. */
double *icxClipVector(
icxClip *p, /* Clipping setup information */
double *in, /* Target point */
double *cdirv /* Space for returned clip vector */
) {
int f;
if (p->nearclip != 0)
return NULL; /* Doing nearest clipping, not vector */
/* Default is simple vector clip */
for (f = 0; f < p->fdi; f++)
cdirv[f] = p->ocent[f] - in[f]; /* Clip towards output gamut center */
if (p->ocentl != 0.0) { /* Graduated vector clip */
double cvl, nll;
/* Compute (negative) clip vector length */
for (cvl = 0.0, f = 0; f < p->fdi; f++) {
cvl += cdirv[f] * cdirv[f];
}
cvl = sqrt(cvl);
if (cvl > 1e-8) {
/* Dot product of clip vector and clip center line */
for (nll = 0.0, f = 0; f < p->fdi; f++)
nll -= cdirv[f] * p->ocentv[f]; /* (Fix -ve clip vector) */
nll /= (p->ocentl * p->ocentl); /* Normalised location along line */
/* Limit to line */
if (nll < 0.0)
nll = 0.0;
else if (nll > 1.0)
nll = 1.0;
if (p->LabLike) {
/* Aim more towards center for saturated targets */
double sat = sqrt(in[1] * in[1] + in[2] * in[2]);
nll += sat/150.0 * (0.5 - nll);
}
/* Compute target clip direction */
for (f = 0; f < p->fdi; f++)
cdirv[f] = p->ocent[f] + nll * p->ocentv[f] - in[f];
}
}
return cdirv;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Typically inv is used to invert a device->PCS table, */
/* so it is doing a PCS->device conversion. */
/* This doesn't always have to be the case though. */
/* PCS override (Effective PCS) to PCS conversion + absolute to relative conversion */
int icxLuLut_inv_out_abs(icxLuLut *p, double *out, double *in) {
int rv = 0;
DBR(("\nicxLuLut_inv_out_abs got PCS %s\n",icmPdv(p->outputChan, in)));
if (p->mergeclut == 0) {
if (p->outs == icxSigJabData) {
p->cam->cam_to_XYZ(p->cam, out, in);
DBR(("icxLuLut_inv_out_abs after cam2XYZ %s\n",icmPdv(p->outputChan, out)));
/* Hack to prevent CAM02 weirdness being amplified by inv_out_abs() */
/* or per channel clipping. */
/* Limit -Y to non-stupid values by scaling */
if (out[1] < -0.1) {
out[0] *= -0.1/out[1];
out[2] *= -0.1/out[1];
out[1] = -0.1;
DBR(("icxLuLut_inv_out_abs after clipping -Y %s\n",icmPdv(p->outputChan, out)));
}
rv |= ((icmLuLut *)p->plu)->inv_out_abs((icmLuLut *)p->plu, out, out);
DBR(("icxLuLut_inv_out_abs after icmLut inv_out_abs %s\n",icmPdv(p->outputChan, out)));
} else {
rv |= ((icmLuLut *)p->plu)->inv_out_abs((icmLuLut *)p->plu, out, in);
DBR(("icxLuLut_inv_out_abs after icmLut inv_out_abs %s\n",icmPdv(p->outputChan, out)));
}
} else {
int i;
for (i = 0; i < p->outputChan; i++)
out[i] = in[i];
}
DBR(("icxLuLut_inv_out_abs returning PCS %f %f %f\n",out[0],out[1],out[2]))
return rv;
}
/* Do output->output' inverse lookup */
int icxLuLut_inv_output(icxLuLut *p, double *out, double *in) {
int rv = 0;
DBR(("icxLuLut_inv_output got PCS %f %f %f\n",in[0],in[1],in[2]))
if (p->mergeclut == 0) {
#ifdef NEVER
rv = ((icmLuLut *)p->plu)->inv_output((icmLuLut *)p->plu, out, in);
#else
int i,j;
int nsoln; /* Number of solutions found */
co pp[MAX_INVSOLN]; /* Room for all the solutions found */
double cdir;
for (i = 0; i < p->outputChan; i++) {
pp[0].p[0] = p->outputClipc[i];
pp[0].v[0] = in[i];
cdir = p->outputClipc[i] - in[i]; /* Clip towards output range */
nsoln = p->outputTable[i]->rev_interp (
p->outputTable[i], /* this */
RSPL_NEARCLIP, /* Clip to nearest (faster than vector) */
MAX_INVSOLN, /* Maximum number of solutions allowed for */
NULL, /* No auxiliary input targets */
&cdir, /* Clip vector direction and length */
pp); /* Input and output values */
if (nsoln & RSPL_DIDCLIP)
rv = 1;
nsoln &= RSPL_NOSOLNS; /* Get number of solutions */
if (nsoln == 1) { /* Exactly one solution */
j = 0;
} else if (nsoln == 0) { /* Zero solutions. This is unexpected. */
error("xlut: Unexpected failure to find reverse solution for output table");
return 2;
} else { /* Multiple solutions */
/* Use a simple minded resolution - choose the one closest to the center */
double bdist = 1e300;
int bsoln = 0;
/* Don't expect this - 1D luts are meant to be monotonic */
warning("1D lut inversion got %d reverse solutions\n",nsoln);
warning("solution 0 = %f\n",pp[0].p[0]);
warning("solution 1 = %f\n",pp[1].p[0]);
for (j = 0; j < nsoln; j++) {
double tt;
tt = pp[i].p[0] - p->outputClipc[i];
tt *= tt;
if (tt < bdist) { /* Better solution */
bdist = tt;
bsoln = j;
}
}
j = bsoln;
}
out[i] = pp[j].p[0];
}
#endif /* NEVER */
} else {
int i;
for (i = 0; i < p->outputChan; i++)
out[i] = in[i];
}
DBR(("icxLuLut_inv_output returning PCS' %f %f %f\n",out[0],out[1],out[2]))
return rv;
}
/* Ink limit+gamut limit calculation function for xLuLut. */
/* Returns < 0.0 if input value is within limits, */
/* > 0.0 if outside limits. Value is proportinal to distance to limits. */
/* We implement device gamut check to improve utility outside rspl, */
/* in optimisation routines. */
/* The limits are assumed to be post calibrated device values (ie. real */
/* final device values) */
static double icxLimit(
icxLuLut *p,
double *in
) {
double cin[MAX_CHAN]; /* Calibrated input values */
double tlim, klim;
double ovr, val;
int e;
if (p->pp->cal != NULL) { /* We have device calibration information */
p->pp->cal->interp(p->pp->cal, cin, in);
} else {
for (e = 0; e < p->inputChan; e++)
cin[e] = in[e];
}
if ((tlim = p->ink.tlimit) < 0.0)
tlim = (double)p->inputChan; /* Default is no limit */
if ((klim = p->ink.klimit) < 0.0)
klim = 1.0;
/* Compute amount outside total limit */
{ /* No calibration */
double sum;
for (sum = 0.0, e = 0; e < p->inputChan; e++)
sum += cin[e];
val = sum - tlim;
}
/* Compute amount outside black limit */
if (p->ink.klimit >= 0.0) {
double kval = 0.0;
switch(p->natis) {
case icSigCmykData:
kval = cin[3] - klim;
break;
default:
/* NOTE !!! p->kch isn't being initialized !!! */
if (p->kch >= 0) {
kval = cin[p->kch] - klim;
} else {
error("xlut: Unknown colorspace when black limit specified");
}
}
if (kval > val)
val = kval;
}
/* Compute amount outside device value limits 0.0 - 1.0 */
for (ovr = -1.0, e = 0; e < p->inputChan; e++) {
if (in[e] < 0.0) { /* ! Not cin[] */
if (-in[e] > ovr)
ovr = -in[e];
} else if (in[e] > 1.0) {
if ((in[e] - 1.0) > ovr)
ovr = in[e] - 1.0;
}
}
if (ovr > val)
val = ovr;
return val;
}
/* Same as above, but works with input' values */
/* (If an ink limit is being used we assume that the */
/* input space is not PCS, hence inv_in_abs() is doing nothing) */
static double icxLimitD(
icxLuLut *p,
double *ind
) {
double in[MAX_CHAN];
co tc;
int e;
/* Convert input' to input through revinput Luts (for speed) */
for (e = 0; e < p->inputChan; e++) {
tc.p[0] = ind[e];
p->revinputTable[e]->interp(p->revinputTable[e], &tc);
in[e] = tc.v[0];
}
return icxLimit(p, in);
}
/* Ink limit+gamut limit clipping function for xLuLut (CMYK). */
/* Return nz if there was clipping */
static int icxDoLimit(
icxLuLut *p,
double *out,
double *in
) {
double tlim, klim = -1.0;
double sum;
int clip = 0, e;
int kch = -1;
for (e = 0; e < p->inputChan; e++)
out[e] = in[e];
if ((tlim = p->ink.tlimit) < 0.0)
tlim = (double)p->inputChan;
if ((klim = p->ink.klimit) < 0.0)
klim = 1.0;
/* Clip black */
if (p->natis == icSigCmykData)
kch = 3;
else
kch = p->kch;
if (kch >= 0) {
if (out[p->kch] > klim) {
out[p->kch] = klim;
clip = 1;
}
}
/* Compute amount outside total limit */
for (sum = 0.0, e = 0; e < p->inputChan; e++)
sum += out[e];
if (sum > tlim) {
clip = 1;
sum /= (double)p->inputChan;
for (e = 0; e < p->inputChan; e++)
out[e] -= sum;
}
return clip;
}
#ifdef NEVER
#undef DBK
#define DBK(xxx) printf xxx ;
#else
#undef DBK
#define DBK(xxx)
#endif
/* helper function that creates our standard K locus curve value, */
/* given the curve parameters, and the normalised L 0.0 - 1.0 value. */
/* No filtering version. */
/* !!! Should add K limit in here so that smoothing takes it into account !!! */
static double icxKcurveNF(double L, icxInkCurve *c) {
double Kstpo, Kenpo, Kstle, Kenle;
double rv;
DBK(("icxKcurve got L = %f, smth %f skew %f, Parms %f %f %f %f %f\n",L, c->Ksmth, c->Kskew, c->Kstle, c->Kstpo, c->Kenpo, c->Kenle, c->Kshap));
/* Invert sense of L, so that 0.0 = white, 1.0 = black */
L = 1.0 - L;
/* Clip L, just in case */
if (L < 0.0) {
L = 0.0;
} else if (L > 1.0) {
L = 1.0;
}
DBK(("Clipped inverted L = %f\n",L));
/* Make sure breakpoints are ordered */
if (c->Kstpo < c->Kenpo) {
Kstle = c->Kstle;
Kstpo = c->Kstpo;
Kenpo = c->Kenpo;
Kenle = c->Kenle;
} else { /* They're swapped */
Kstle = c->Kenle;
Kstpo = c->Kenpo;
Kenpo = c->Kstpo;
Kenle = c->Kstle;
}
if (L <= Kstpo) {
/* We are at white level */
rv = Kstle;
DBK(("At white level %f\n",rv));
} else if (L >= Kenpo) {
/* We are at black level */
rv = Kenle;
DBK(("At black level %f\n",rv));
} else {
double Lp, g;
/* We must be on the curve from start to end levels */
Lp = (L - Kstpo)/(Kenpo - Kstpo);
DBK(("Curve position %f\n",Lp));
Lp = pow(Lp, c->Kskew);
DBK(("Skewed curve position %f\n",Lp));
g = c->Kshap/2.0;
DBK(("Curve bf %f, g %g\n",Lp,g));
/* A value of 0.5 will be tranlated to g */
Lp = Lp/((1.0/g - 2.0) * (1.0 - Lp) + 1.0);
DBK(("Skewed shaped %f\n",Lp));
Lp = pow(Lp, 1.0/c->Kskew);
DBK(("Shaped %f\n",Lp));
/* Transition between start end end levels */
rv = Lp * (Kenle - Kstle) + Kstle;
DBK(("Scaled to start and end levele %f\n",rv));
}
DBK(("Returning %f\n",rv));
return rv;
}
#ifdef DBK
#undef DBK
#define DBK(xxx)
#endif
#ifdef NEVER
#undef DBK
#define DBK(xxx) printf xxx ;
#else
#undef DBK
#define DBK(xxx)
#endif
/* Same as above, but implement transition filters accross inflection points. */
/* (The convolultion filter previously used could be */
/* re-instituted if something was done about compressing */
/* the filter at the boundaries so that the levels are met.) */
static double icxKcurve(double L, icxInkCurve *c) {
#ifdef DISABLE_KCURVE_FILTER
return icxKcurveNF(L, c);
#else /* !DISABLE_KCURVE_FILTER */
double Kstpo, Kenpo, Kstle, Kenle, Ksmth;
double rv;
DBK(("icxKcurve got L = %f, smth %f skew %f, Parms %f %f %f %f %f\n",L, c->Ksmth, c->Kskew, c->Kstle, c->Kstpo, c->Kenpo, c->Kenle, c->Kshap));
/* Invert sense of L, so that 0.0 = white, 1.0 = black */
L = 1.0 - L;
/* Clip L, just in case */
if (L < 0.0) {
L = 0.0;
} else if (L > 1.0) {
L = 1.0;
}
DBK(("Clipped inverted L = %f\n",L));
/* Make sure breakpoints are ordered */
if (c->Kstpo < c->Kenpo) {
Kstle = c->Kstle;
Kstpo = c->Kstpo;
Kenpo = c->Kenpo;
Kenle = c->Kenle;
} else { /* They're swapped */
Kstle = c->Kenle;
Kstpo = c->Kenpo;
Kenpo = c->Kstpo;
Kenle = c->Kstle;
}
Ksmth = c->Ksmth;
/* Curve value at point */
rv = icxKcurveNF(1.0 - L, c);
DBK(("Raw output at iL = %f, rv\n",L));
/* Create filtered value */
{
double wbs, wbe; /* White transitioin start, end */
double wbl, wfv; /* White blend factor, value at filter band */
double mt; /* Middle of the two transitions */
double bbs, bbe; /* Black transitioin start, end */
double bbl, bfv; /* Black blend factor, value at filter band */
wbs = Kstpo - Ksmth;
wbe = Kstpo + Ksmth;
bbs = Kenpo - 1.0 * Ksmth;
bbe = Kenpo + 1.0 * Ksmth;
mt = 0.5 * (wbe + bbs);
/* Make sure that the whit & black transition regions */
/* don't go out of bounts or overlap */
if (wbs < 0.0) {
wbe += wbs;
wbs = 0.0;
}
if (bbe > 1.0) {
bbs += (bbe - 1.0);
bbe = 1.0;
}
if (wbe > mt) {
wbs += (wbe - mt);
wbe = mt;
}
if (bbs < mt) {
bbe += (mt - bbs);
bbs = mt;
}
DBK(("Transition windows %f - %f, %f - %f\n",wbs, wbe, bbw, bbe));
if (wbs < wbe) {
wbl = (L - wbe)/(wbs - wbe);
if (wbl > 0.0 && wbl < 1.0) {
wfv = icxKcurveNF(1.0 - wbe, c);
DBK(("iL = %f, wbl = %f, wfv = %f\n",L,Kstpo,wbl,wfv));
wbl = 1.0 - pow(1.0 - wbl, 2.0);
rv = wbl * Kstle + (1.0 - wbl) * wfv;
}
}
if (bbs < bbe) {
bbl = (L - bbe)/(bbs - bbe);
if (bbl > 0.0 && bbl < 1.0) {
bfv = icxKcurveNF(1.0 - bbs, c);
DBK(("iL = %f, bbl = %f, bfv = %f\n",L,Kstpo,bbl,bfv));
bbl = pow(bbl, 2.0);
rv = bbl * bfv + (1.0 - bbl) * Kenle;
}
}
}
/* To be safe */
if (rv < 0.0)
rv = 0.0;
else if (rv > 1.0)
rv = 1.0;
DBK(("Returning %f\n",rv));
return rv;
#endif /* !DISABLE_KCURVE_FILTER */
}
#ifdef DBK
#undef DBK
#define DBK(xxx)
#endif
/* Do output'->input' lookup with aux details. */
/* Note that out[] will be used as the inking value if icxKrule is */
/* icxKvalue, icxKlocus, icxKl5l or icxKl5lk, and that the auxiliar values, PCS ranges */
/* and icxKrule value will all be evaluated in output->input space (not ' space). */
/* Note that the ink limit will be computed after converting input' to input, auxt */
/* will override the inking rule, and auxr[] reflects the available auxiliary range */
/* that the locus was to choose from, and auxv[] was the actual auxiliary used. */
/* Returns clip status. */
int icxLuLut_inv_clut_aux(
icxLuLut *p,
double *out, /* Function return values, plus aux value or locus target input if auxt == NULL */
double *auxv, /* If not NULL, return aux value used (packed) */
double *auxr, /* If not NULL, return aux locus range (packed, 2 at a time) */
double *auxt, /* If not NULL, specify the aux target for this lookup (override ink) */
double *clipd, /* If not NULL, return DE to gamut on clipi, 0 for not clip */
double *in /* Function input values to invert (== clut output' values) */
) {
co pp[MAX_INVSOLN]; /* Room for all the solutions found */
int nsoln; /* Number of solutions found */
double *cdir, cdirv[MXDO]; /* Clip vector direction and length */
int e,f,i;
int fdi = p->clutTable->fdi;
int flags = 0; /* reverse interp flags */
int xflags = 0; /* extra clip/noclip flags */
double tin[MXDO]; /* PCS value to be inverted */
double cdist = 0.0; /* clip DE */
int crv = 0; /* Return value - set to 1 if clipped */
if (p->nearclip != 0)
flags |= RSPL_NEARCLIP; /* Use nearest clipping rather than clip vector */
DBR(("inv_clut_aux input is %f %f %f\n",in[0], in[1], in[2]))
if (auxr != NULL) { /* Set a default locus range */
int ee = 0;
for (e = 0; e < p->clutTable->di; e++) {
if (p->auxm[e] != 0) {
auxr[ee++] = 1e60;
auxr[ee++] = -1e60;
}
}
}
/* Setup for reverse lookup */
for (f = 0; f < fdi; f++)
pp[0].v[f] = in[f]; /* Target value */
/* Compute clip vector, if any */
cdir = icxClipVector(&p->clip, in, cdirv);
if (p->clutTable->di > fdi) { /* ie. CMYK->Lab, there will be ambiguity */
double min[MXDI], max[MXDI]; /* Auxiliary locus range */
#ifdef REPORT_LOCUS_SEGMENTS /* Examine how many segments there are */
{ /* ie. CMYK->Lab, there will be ambiguity */
double smin[10][MXRI], smax[10][MXRI]; /* Auxiliary locus segment ranges */
double min[MXRI], max[MXRI]; /* Auxiliary min and max locus range */
nsoln = p->clutTable->rev_locus_segs(
p->clutTable, /* rspl object */
p->auxm, /* Auxiliary mask */
pp, /* Input target and output solutions */
10, /* Maximum number of solutions to return */
smin, smax); /* Returned locus of valid auxiliary values */
if (nsoln != 0) {
/* Convert the locuses from input' -> input space */
/* and get overall min/max locus range */
for (e = 0; e < p->clutTable->di; e++) {
co tc;
/* (Is speed more important than precision ?) */
if (p->auxm[e] != 0) {
for (i = 0; i < nsoln; i++) {
tc.p[0] = smin[i][e];
p->revinputTable[e]->interp(p->revinputTable[e], &tc);
smin[i][e] = tc.v[0];
tc.p[0] = smax[i][e];
p->revinputTable[e]->interp(p->revinputTable[e], &tc);
smax[i][e] = tc.v[0];
printf(" Locus seg %d:[%d] %f -> %f\n",i, e, smin[i][e], smax[i][e]);
}
}
}
}
}
#endif /* REPORT_LOCUS_SEGMENTS */
/* Compute auxiliary locus on the fly. This is in dev' == input' space. */
nsoln = p->clutTable->rev_locus(
p->clutTable, /* rspl object */
p->auxm, /* Auxiliary mask */
pp, /* Input target and output solutions */
min, max); /* Returned locus of valid auxiliary values */
if (nsoln == 0) {
xflags |= RSPL_WILLCLIP; /* No valid locus, so we expect to have to clip */
#ifdef DEBUG_RLUT
printf("inv_clut_aux: no valid locus, expect clip\n");
#endif
/* Make sure that the auxiliar value is initialized, */
/* even though it won't have any effect. */
for (e = 0; e < p->clutTable->di; e++) {
if (p->auxm[e] != 0) {
pp[0].p[e] = 0.5;
}
}
} else { /* Got a valid locus */
/* Convert the locuses from input' -> input space */
for (e = 0; e < p->clutTable->di; e++) {
co tc;
/* (Is speed more important than precision ?) */
if (p->auxm[e] != 0) {
tc.p[0] = min[e];
p->revinputTable[e]->interp(p->revinputTable[e], &tc);
min[e] = tc.v[0];
tc.p[0] = max[e];
p->revinputTable[e]->interp(p->revinputTable[e], &tc);
max[e] = tc.v[0];
}
}
if (auxr != NULL) { /* Report the locus range */
int ee = 0;
for (e = 0; e < p->clutTable->di; e++) {
if (p->auxm[e] != 0) {
auxr[ee++] = min[e];
auxr[ee++] = max[e];
}
}
}
if (auxt != NULL) { /* overiding auxiliary target */
int ee = 0;
for (e = 0; e < p->clutTable->di; e++) {
if (p->auxm[e] != 0) {
double iv = auxt[ee++];
if (iv < min[e])
iv = min[e];
else if (iv > max[e])
iv = max[e];
pp[0].p[e] = iv;
}
}
DBR(("inv_clut_aux: aux %f from auxt[] %f\n",pp[0].p[3],auxt[0]))
} else if (p->ink.k_rule == icxKvalue) {
/* Implement the auxiliary inking rule */
/* Target auxiliary values are provided in out[] K value */
for (e = 0; e < p->clutTable->di; e++) {
if (p->auxm[e] != 0) {
double iv = out[e]; /* out[] holds aux target value */
if (iv < min[e])
iv = min[e];
else if (iv > max[e])
iv = max[e];
pp[0].p[e] = iv;
}
}
DBR(("inv_clut_aux: aux %f from out[0] K target %f min %f max %f\n",pp[0].p[3],out[3],min[3],max[3]))
} else if (p->ink.k_rule == icxKlocus) {
/* Set target auxliary input values from values in out[] and locus */
for (e = 0; e < p->clutTable->di; e++) {
if (p->auxm[e] != 0) {
double ii, iv;
ii = out[e]; /* Input ink locus */
iv = min[e] + ii * (max[e] - min[e]); /* Output ink from locus */
if (iv < min[e])
iv = min[e];
else if (iv > max[e])
iv = max[e];
pp[0].p[e] = iv;
}
}
DBR(("inv_clut_aux: aux %f from out[0] locus %f min %f max %f\n",pp[0].p[3],out[3],min[3],max[3]))
} else { /* p->ink.k_rule == icxKluma5 || icxKluma5k || icxKl5l || icxKl5lk */
/* Auxiliaries are driven by a rule and the output values */
double rv, L;
/* If we've got a mergeclut, then the PCS' is the same as the */
/* effective PCS, and we need to convert to native PCS */
if (p->mergeclut) {
p->mergeclut = 0; /* Hack to be able to use inv_out_abs() */
icxLuLut_inv_out_abs(p, tin, in);
p->mergeclut = 1;
} else {
/* Convert native PCS' to native PCS values */
p->output(p, tin, in);
}
/* Figure out Luminance number */
if (p->natos == icSigXYZData) {
icmXYZ2Lab(&icmD50, tin, tin);
} else if (p->natos != icSigLabData) { /* Hmm. that's unexpected */
error("Assert: xlut K locus, unexpected native pcs of 0x%x\n",p->natos);
}
L = 0.01 * tin[0];
DBR(("inv_clut_aux: aux from Luminance, raw L = %f\n",L));
/* Normalise L to its possible range from min to max */
L = (L - p->Lmin)/(p->Lmax - p->Lmin);
DBR(("inv_clut_aux: Normalize L = %f\n",L));
/* Convert L to curve value */
rv = icxKcurve(L, &p->ink.c);
DBR(("inv_clut_aux: icxKurve lookup returns = %f\n",rv));
if (p->ink.k_rule == icxKluma5) { /* Curve is locus value */
/* Set target black as K fraction within locus */
for (e = 0; e < p->clutTable->di; e++) {
if (p->auxm[e] != 0) {
pp[0].p[e] = min[e] + rv * (max[e] - min[e]);
}
}
DBR(("inv_clut_aux: aux %f from locus %f min %f max %f\n",pp[0].p[3],rv,min[3],max[3]))
} else if (p->ink.k_rule == icxKluma5k) { /* Curve is K value */
for (e = 0; e < p->clutTable->di; e++) {
if (p->auxm[e] != 0) {
double iv = rv;
if (iv < min[e]) /* Clip to locus */
iv = min[e];
else if (iv > max[e])
iv = max[e];
pp[0].p[e] = iv;
}
}
DBR(("inv_clut_aux: aux %f from out[0] K target %f min %f max %f\n",pp[0].p[3],rv,min[3],max[3]))
} else { /* icxKl5l || icxKl5lk */
/* Create second curve, and use input locus to */
/* blend between */
double rv2; /* Upper limit */
/* Convert L to max curve value */
rv2 = icxKcurve(L, &p->ink.x);
if (rv2 < rv) { /* Ooops - better swap. */
double tt;
tt = rv;
rv = rv2;
rv2 = tt;
}
for (e = 0; e < p->clutTable->di; e++) {
if (p->auxm[e] != 0) {
if (p->ink.k_rule == icxKl5l) {
double ii;
ii = out[e]; /* Input K locus */
if (ii < 0.0)
ii = 0.0;
else if (ii > 1.0)
ii = 1.0;
ii = (1.0 - ii) * rv + ii * rv2;/* Blend between locus rule curves */
/* Out ink from output locus */
pp[0].p[e] = min[e] + ii * (max[e] - min[e]);
} else {
double iv;
iv = out[e]; /* Input K level */
if (iv < rv) /* Constrain to curves */
iv = rv;
else if (iv > rv2)
iv = rv2;
pp[0].p[e] = iv;
}
}
}
DBR(("inv_clut_aux: aux %f from 2 curves\n",pp[0].p[3]))
}
}
/* Convert to input/dev aux target to input'/dev' space for rspl inversion */
for (e = 0; e < p->clutTable->di; e++) {
double tv, bv = 0.0, bd = 1e6;
co tc;
if (p->auxm[e] != 0) {
tv = pp[0].p[e];
/* Clip to overall locus range (belt and braces) */
if (tv < min[e])
tv = min[e];
if (tv > max[e])
tv = max[e];
tc.p[0] = tv;
p->inputTable[e]->interp(p->inputTable[e], &tc);
pp[0].p[e] = tc.v[0];
}
}
xflags |= RSPL_EXACTAUX; /* Since we confine aux to locus */
#ifdef DEBUG_RLUT
printf("inv_clut_aux computed aux values ");
for (e = 0; e < p->clutTable->di; e++) {
if (p->auxm[e] != 0)
printf("%d: %f ",e,pp[0].p[e]);
}
printf("\n");
#endif /* DEBUG_RLUT */
}
if (clipd != NULL) { /* Copy pp.v[] to compute clip DE */
for (f = 0; f < fdi; f++)
tin[f] = pp[0].v[f];
}
/* Find reverse solution with target auxiliaries */
/* We choose the closest aux at or above the target */
/* to try and avoid glitches near black due to */
/* possible forked black locuses. */
nsoln = p->clutTable->rev_interp(
p->clutTable, /* rspl object */
RSPL_MAXAUX | flags | xflags, /* Combine all the flags */
MAX_INVSOLN, /* Maxumum solutions to return */
p->auxm, /* Auxiliary input chanel mask */
cdir, /* Clip vector direction and length */
pp); /* Input target and output solutions */
/* returned solutions in pp[0..retval-1].p[] */
} else {
DBR(("inv_clut_aux needs no aux value\n"))
if (clipd != NULL) { /* Copy pp.v[] to compute clip DE */
for (f = 0; f < fdi; f++)
tin[f] = pp[0].v[f];
}
/* Color spaces don't need auxiliaries to choose from solution locus */
nsoln = p->clutTable->rev_interp(
p->clutTable, /* rspl object */
flags, /* No extra flags */
MAX_INVSOLN, /* Maxumum solutions to return */
NULL, /* No auxiliary input targets */
cdir, /* Clip vector direction and length */
pp); /* Input target and output solutions */
/* returned solutions in pp[0..retval-1].p[] */
}
if (nsoln & RSPL_DIDCLIP)
crv = 1; /* Clipped on PCS inverse lookup */
if (crv && clipd != NULL) {
/* Compute the clip DE */
for (cdist = 0.0, f = 0; f < fdi; f++) {
double tt;
tt = pp[0].v[f] - tin[f];
cdist += tt * tt;
}
cdist = sqrt(cdist);
DBR(("Targ PCS %f %f %f Clipped %f %f %f cdist %f\n",tin[0],tin[1],tin[2],pp[0].v[0],pp[0].v[1],pp[0].v[2],cdist))
}
nsoln &= RSPL_NOSOLNS; /* Get number of solutions */
DBR(("inv_clut_aux got %d rev_interp solutions, clipflag = %d\n",nsoln,crv))
/* If we clipped and we should clip in CAM Jab space, compute reverse */
/* clip solution using our additional CAM space. */
/* (Note that we don't support vector clip in CAM space at the moment) */
if (crv != 0 && p->camclip && p->nearclip) {
co cpp; /* Alternate CAM space solution */
double bf; /* Blend factor */
DBR(("inv_clut_aux got clip, compute CAM clip\n"))
if (nsoln != 1) { /* This would be unexpected */
error("Unexpected failure to return 1 solution on clip for input to output table");
}
if (p->cclutTable == NULL) { /* we haven't created this yet, so do so */
if (icxLuLut_init_clut_camclip(p))
error("Creating CAM rspl for camclip failed");
}
/* Setup for reverse lookup */
DBR(("inv_clut_aux cam clip PCS in %f %f %f\n",in[0],in[1],in[2]))
/* Convert from PCS' to (XYZ) PCS */
((icmLuLut *)p->absxyzlu)->output((icmLuLut *)p->absxyzlu, tin, in);
DBR(("inv_clut_aux cam clip PCS' -> PCS %f %f %f\n",tin[0],tin[1],tin[2]))
((icmLuLut *)p->absxyzlu)->out_abs((icmLuLut *)p->absxyzlu, tin, tin);
DBR(("inv_clut_aux cam clip abs XYZ PCS %f %f %f\n",tin[0],tin[1],tin[2]))
p->cam->XYZ_to_cam(p->cam, tin, tin);
DBR(("inv_clut_aux cam clip PCS after XYZtoCAM %f %f %f\n",tin[0],tin[1],tin[2]))
for (f = 0; f < fdi; f++) /* Transfer CAM targ */
cpp.v[f] = tin[f];
cpp.v[0] *= CCJSCALE;
/* Make sure that the auxiliar value is initialized, */
/* even though it shouldn't have any effect, since should clipp. */
for (e = 0; e < p->clutTable->di; e++) {
if (p->auxm[e] != 0) {
cpp.p[e] = 0.5;
}
}
if (p->clutTable->di > fdi) { /* ie. CMYK->Lab, there will be ambiguity */
nsoln = p->cclutTable->rev_interp(
p->cclutTable, /* rspl object */
flags | xflags | RSPL_WILLCLIP, /* Combine all the flags + clip ?? */
1, /* Maximum solutions to return */
p->auxm, /* Auxiliary input chanel mask */
cdir, /* Clip vector direction and length */
&cpp); /* Input target and output solutions */
} else {
nsoln = p->cclutTable->rev_interp(
p->cclutTable, /* rspl object */
flags | RSPL_WILLCLIP, /* Because we know it will clip ?? */
1, /* Maximum solutions to return */
NULL, /* No auxiliary input targets */
cdir, /* Clip vector direction and length */
&cpp); /* Input target and output solutions */
}
nsoln &= RSPL_NOSOLNS; /* Get number of solutions */
if (nsoln != 1) { /* This would be unexpected */
error("Unexpected failure to return 1 solution on CAM clip for input to output table");
}
/* Compute the CAM clip distances */
cpp.v[0] /= CCJSCALE;
for (cdist = 0.0, f = 0; f < fdi; f++) {
double tt;
tt = cpp.v[f] - tin[f];
cdist += tt * tt;
}
cdist = sqrt(cdist);
DBR(("Targ CAM %f %f %f Clipped %f %f %f cdist %f\n",tin[0],tin[1],tin[2],cpp.v[0],cpp.v[1],cpp.v[2],cdist))
/* Use magic number to set blend distance, and compute a blend factor. */
/* Blend over 1 delta E, otherwise the Lab & CAM02 divergence can result */
/* in reversals. */
bf = cdist/CAMCLIPTRANS; /* 0.0 for PCS result, 1.0 for CAM result */
if (bf > 1.0)
bf = 1.0;
#ifdef USECAMCLIPSPLINE
bf = bf * bf * (3.0 - 2.0 * bf); /* Convert to spline blend */
#endif
DBR(("cdist %f, spline blend %f\n",cdist,bf))
/* Blend between solution values for PCS and CAM clip result. */
/* We're hoping that the solutions are close, and expect them to be */
/* that way when we're close to the gamut surface (since the cell */
/* vertex values should be exact, irrespective of which interpolation */
/* space they're in), but weird things could happen away from the surface. */
/* Weird things can happen anyway with "clip to nearest", since this is not */
/* guaranteed to be a smooth function, depending on the gamut surface */
/* geometry, without taking some precaution such as clipping to a */
/* convex hull "wrapper" to create a clip vector, which we're not */
/* current doing. */
DBR(("Clip blend between device:\n"))
DBR(("Lab: %s & ",icmPdv(p->clutTable->di, pp[0].p)))
DBR(("Jab: %s ",icmPdv(p->clutTable->di, cpp.p)))
for (e = 0; e < p->clutTable->di; e++) {
out[e] = (1.0 - bf) * pp[0].p[e] + bf * cpp.p[e];
}
DBR((" = %s\n",icmPdv(p->clutTable->di, out)))
/* Not CAM clip case */
} else {
if (nsoln == 1) { /* Exactly one solution */
i = 0;
} else if (nsoln == 0) { /* Zero solutions. This is unexpected. */
double in_v[MXDO];
p->output(p, in_v, pp[0].v); /* Get ICC inverse input values */
p->out_abs(p, in_v, in_v);
error("Unexpected failure to find reverse solution for input to output table for value %f %f %f (ICC input %f %f %f)",pp[0].v[0],pp[0].v[1],pp[0].v[2], in_v[0], in_v[1], in_v[2]);
return 2;
} else { /* Multiple solutions */
/* Use a simple minded resolution - choose the one closest to the center */
double bdist = 1e300;
int bsoln = 0;
DBR(("got multiple reverse solutions\n"));
for (i = 0; i < nsoln; i++) {
double ss;
DBR(("Soln %d: %s\n",i, icmPdv(p->clutTable->di, pp[i].p)))
for (ss = 0.0, e = 0; e < p->clutTable->di; e++) {
double tt;
tt = pp[i].p[e] - p->licent[e];
tt *= tt;
if (tt < bdist) { /* Better solution */
bdist = tt;
bsoln = i;
}
}
}
#ifndef NEVER
// ~~99 average them
for (i = 1; i < nsoln; i++) {
for (e = 0; e < p->clutTable->di; e++)
pp[0].p[e] += pp[i].p[e];
}
for (e = 0; e < p->clutTable->di; e++)
pp[0].p[e] /= (double)nsoln;
bsoln = 0;
#endif
//printf("~1 chose %d\n",bsoln);
i = bsoln;
}
for (e = 0; e < p->clutTable->di; e++) {
/* Save solution as atractor for next one, on the basis */
/* that it might have better continuity given pesudo-hilbert inversion path. */
p->licent[e] = out[e] = pp[i].p[e]; /* Solution */
}
}
/* Sanitise auxiliary locus range and auxiliary value return */
if (auxr != NULL || auxv != NULL) {
int ee = 0;
for (e = 0; e < p->clutTable->di; e++) {
double v = out[e]; /* Solution */
if (p->auxm[e] != 0) {
if (auxr != NULL) { /* Make sure locus encloses actual value */
if (auxr[2 * ee] > v)
auxr[2 * ee] = v;
if (auxr[2 * ee + 1] < v)
auxr[2 * ee + 1] = v;
}
if (auxv != NULL) {
auxv[ee] = v;
}
ee++;
}
}
}
#ifdef CHECK_ILIMIT /* Do sanity checks on meeting ink limit */
if (p->ink.tlimit >= 0.0 || p->ink.klimit >= 0.0) {
double sum = icxLimitD(p, out);
if (sum > 0.0)
printf("xlut assert%s: icxLuLut_inv_clut returned outside limits by %f > tlimit %f\n",crv ? " (clip)" : "", sum, p->ink.tlimit);
}
#endif
if (clipd != NULL) {
*clipd = cdist;
DBR(("inv_clut_aux returning clip DE %f\n",cdist))
}
DBR(("inv_clut_aux returning %f %f %f %f\n",out[0],out[1],out[2],out[3]))
return crv;
}
/* Do output'->input' lookup, simple version */
/* Note than out[] will carry inking value if icxKrule is icxKvalue of icxKlocus */
/* and that the icxKrule value will be in the input (NOT input') space. */
/* Note that the ink limit will be computed after converting input' to input */
int icxLuLut_inv_clut(icxLuLut *p, double *out, double *in) {
return icxLuLut_inv_clut_aux(p, out, NULL, NULL, NULL, NULL, in);
}
/* Given the proposed auxiliary input values in in[di], */
/* and the target output' (ie. PCS') values in out[fdi], */
/* return the auxiliary input (NOT input' space) values as a proportion of their */
/* possible locus in locus[di]. */
/* This is generally used on a source CMYK profile to convey the black intent */
/* to destination CMYK profile. */
int icxLuLut_clut_aux_locus(icxLuLut *p, double *locus, double *out, double *in) {
co pp[1]; /* Room for all the solutions found */
int nsoln; /* Number of solutions found */
int e,f;
if (p->clutTable->di > p->clutTable->fdi) { /* ie. CMYK->Lab, there will be ambiguity */
double min[MXDI], max[MXDI]; /* Auxiliary locus range */
/* Setup for auxiliary locus lookup */
for (f = 0; f < p->clutTable->fdi; f++) {
pp[0].v[f] = out[f]; /* Target output' (i.e. PCS) value */
}
/* Compute auxiliary locus */
nsoln = p->clutTable->rev_locus(
p->clutTable, /* rspl object */
p->auxm, /* Auxiliary mask */
pp, /* Input target and output solutions */
min, max); /* Returned locus of valid auxiliary values */
if (nsoln == 0) {
for (e = 0; e < p->clutTable->di; e++)
locus[e] = 0.0; /* Return some safe values */
} else { /* Got a valid locus */
/* Convert the locus from input' -> input space */
for (e = 0; e < p->clutTable->di; e++) {
co tc;
/* (Is speed more important than precision ?) */
if (p->auxm[e] != 0) {
tc.p[0] = min[e];
p->revinputTable[e]->interp(p->revinputTable[e], &tc);
min[e] = tc.v[0];
tc.p[0] = max[e];
p->revinputTable[e]->interp(p->revinputTable[e], &tc);
max[e] = tc.v[0];
}
}
/* Figure out the proportion of the locus */
for (e = 0; e < p->clutTable->di; e++) {
if (p->auxm[e] != 0) {
double iv = in[e];
if (iv <= min[e])
locus[e] = 0.0;
else if (iv >= max[e])
locus[e] = 1.0;
else {
double lpl = max[e] - min[e]; /* Locus path length */
if (lpl > 1e-6)
locus[e] = (iv - min[e])/lpl;
else
locus[e] = 0.0;
}
}
}
}
} else {
/* There should be no auxiliaries */
for (e = 0; e < p->clutTable->di; e++)
locus[e] = 0.0; /* Return some safe values */
}
return 0;
}
/* Do input' -> input inverse lookup */
int icxLuLut_inv_input(icxLuLut *p, double *out, double *in) {
#ifdef NEVER
return ((icmLuLut *)p->plu)->inv_input((icmLuLut *)p->plu, out, in);
#else
int rv = 0;
int i,j;
int nsoln; /* Number of solutions found */
co pp[MAX_INVSOLN]; /* Room for all the solutions found */
double cdir;
DBR(("inv_input got DEV' %f %f %f %f\n",in[0],in[1],in[2],in[3]))
for (i = 0; i < p->inputChan; i++) {
pp[0].p[0] = p->inputClipc[i];
pp[0].v[0] = in[i];
cdir = p->inputClipc[i] - in[i]; /* Clip towards output range */
nsoln = p->inputTable[i]->rev_interp (
p->inputTable[i], /* this */
RSPL_NEARCLIP, /* Clip to nearest (faster than vector) */
MAX_INVSOLN, /* Maximum number of solutions allowed for */
NULL, /* No auxiliary input targets */
&cdir, /* Clip vector direction and length */
pp); /* Input and output values */
if (nsoln & RSPL_DIDCLIP)
rv = 1;
nsoln &= RSPL_NOSOLNS; /* Get number of solutions */
if (nsoln == 1) { /* Exactly one solution */
j = 0;
} else if (nsoln == 0) { /* Zero solutions. This is unexpected. */
error("Unexpected failure to find reverse solution for input table");
return 2;
} else { /* Multiple solutions */
/* Use a simple minded resolution - choose the one closest to the center */
double bdist = 1e300;
int bsoln = 0;
/* Don't expect this - 1D luts are meant to be monotonic */
warning("1D lut inversion got %d reverse solutions\n",nsoln);
warning("solution 0 = %f\n",pp[0].p[0]);
warning("solution 1 = %f\n",pp[1].p[0]);
for (j = 0; j < nsoln; j++) {
double tt;
tt = pp[i].p[0] - p->inputClipc[i];
tt *= tt;
if (tt < bdist) { /* Better solution */
bdist = tt;
bsoln = j;
}
}
j = bsoln;
}
out[i] = pp[j].p[0];
}
DBR(("inv_input returning DEV %f %f %f %f\n",out[0],out[1],out[2],out[3]))
return rv;
#endif /* NEVER */
}
/* Possible inverse matrix lookup */
/* (Will do nothing if input is device space) */
int icxLuLut_inv_matrix(icxLuLut *p, double *out, double *in) {
int rv = 0;
rv |= ((icmLuLut *)p->plu)->inv_matrix((icmLuLut *)p->plu, out, in);
return rv;
}
/* Inverse input absolute intent conversion */
/* (Will do nothing if input is device space) */
int icxLuLut_inv_in_abs(icxLuLut *p, double *out, double *in) {
int rv = 0;
rv |= ((icmLuLut *)p->plu)->inv_in_abs((icmLuLut *)p->plu, out, in);
if (p->ins == icxSigJabData) {
p->cam->XYZ_to_cam(p->cam, out, out);
}
return rv;
}
/* Overall inverse lookup */
/* Note that all auxiliary values are in input (NOT input') space */
static int
icxLuLut_inv_lookup(
icxLuBase *pp, /* This */
double *out, /* Vector of output values/input auxiliary values */
double *in /* Vector of input values */
) {
icxLuLut *p = (icxLuLut *)pp;
int rv = 0;
int i;
double temp[MAX_CHAN];
DBOL(("xiccilu: input = %s\n", icmPdv(p->outputChan, in)));
if (p->mergeclut == 0) { /* Do this if it's not merger with clut */
rv |= p->inv_out_abs (p, temp, in);
DBOL(("xiccilu: after inv abs = %s\n", icmPdv(p->outputChan, temp)));
rv |= p->inv_output (p, temp, temp);
DBOL(("xiccilu: after inv out = %s\n", icmPdv(p->outputChan, temp)));
} else {
for (i = 0; i < p->outputChan; i++)
temp[i] = in[i];
}
DBOL(("xiccilu: aux targ = %s\n", icmPdv(p->inputChan,out)));
rv |= p->inv_clut (p, out, temp);
DBOL(("xiccilu: after inv clut = %s\n", icmPdv(p->inputChan,out)));
rv |= p->inv_input (p, out, out);
DBOL(("xiccilu: after inv input = %s\n", icmPdv(p->inputChan,out)));
rv |= p->inv_matrix (p, out, out);
DBOL(("xiccilu: after inv matrix = %s\n", icmPdv(p->inputChan,out)));
rv |= p->inv_in_abs (p, out, out);
DBOL(("xiccilu: after inv abs = %s\n", icmPdv(p->inputChan,out)));
return rv;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Given a nominated output PCS (ie. Absolute, Jab etc.), convert it in the bwd */
/* direction into a relative XYZ or Lab PCS value */
/* (This is used in generating gamut compression in B2A tables) */
void icxLuLut_bwd_outpcs_relpcs(
icxLuBase *pp,
icColorSpaceSignature os, /* Output space, XYZ or Lab */
double *out, double *in) {
icxLuLut *p = (icxLuLut *)pp;
if (p->outs == icxSigJabData) {
DBS(("bwd_outpcs_relpcs: Jab in = %s\n", icmPdv(3, in)));
p->cam->cam_to_XYZ(p->cam, out, in);
DBS(("bwd_outpcs_relpcs: abs XYZ = %s\n", icmPdv(3, out)));
/* Hack to prevent CAM02 weirdness being amplified by */
/* per channel clipping. */
/* Limit -Y to non-stupid values by scaling */
if (out[1] < -0.1) {
out[0] *= -0.1/out[1];
out[2] *= -0.1/out[1];
out[1] = -0.1;
DBS(("bwd_outpcs_relpcs: after clipping -Y %s\n",icmPdv(p->outputChan, out)));
}
} else {
DBS(("bwd_outpcs_relpcs: abs PCS in = %s\n", icmPdv(3, out)));
icmCpy3(out, in);
}
((icmLuLut *)p->plu)->inv_out_abs((icmLuLut *)p->plu, out, out);
DBS(("bwd_outpcs_relpcs: rel PCS = %s\n", icmPdv(3, out)));
if (os == icSigXYZData && p->natpcs == icSigLabData) {
icmLab2XYZ(&icmD50, out, out);
DBS(("bwd_outpcs_relpcs: rel XYZ = %s\n", icmPdv(3, out)));
} else if (os == icSigXYZData && p->natpcs == icSigLabData) {
icmXYZ2Lab(&icmD50, out, out);
DBS(("bwd_outpcs_relpcs: rel Lab = %s\n", icmPdv(3, out)));
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Return LuLut information */
static void icxLuLut_get_info(
icxLuLut *p, /* this */
icmLut **lutp, /* Pointer to icc lut type return value */
icmXYZNumber *pcswhtp, /* Pointer to profile PCS white point return value */
icmXYZNumber *whitep, /* Pointer to profile absolute white point return value */
icmXYZNumber *blackp /* Pointer to profile absolute black point return value */
) {
((icmLuLut *)p->plu)->get_info((icmLuLut *)p->plu, lutp, pcswhtp, whitep, blackp);
}
/* Return the underlying Lut matrix */
static void
icxLuLut_get_matrix (
icxLuLut *p,
double m[3][3]
) {
((icmLuLut *)p->plu)->get_matrix((icmLuLut *)p->plu, m);
}
static void
icxLuLut_free(
icxLuBase *pp
) {
icxLuLut *p = (icxLuLut *)pp;
int i;
for (i = 0; i < p->inputChan; i++) {
if (p->inputTable[i] != NULL)
p->inputTable[i]->del(p->inputTable[i]);
if (p->revinputTable[i] != NULL)
p->revinputTable[i]->del(p->revinputTable[i]);
}
if (p->clutTable != NULL)
p->clutTable->del(p->clutTable);
if (p->cclutTable != NULL)
p->cclutTable->del(p->cclutTable);
for (i = 0; i < p->outputChan; i++) {
if (p->outputTable[i] != NULL)
p->outputTable[i]->del(p->outputTable[i]);
}
if (p->plu != NULL)
p->plu->del(p->plu);
if (p->cam != NULL)
p->cam->del(p->cam);
if (p->absxyzlu != NULL)
p->absxyzlu->del(p->absxyzlu);
free(p);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */
static gamut *icxLuLutGamut(icxLuBase *plu, double detail);
/* Do the basic icxLuLut creation and initialisation */
static icxLuLut *
alloc_icxLuLut(
xicc *xicp,
icmLuBase *plu, /* Pointer to Lu we are expanding (ours) */
int flags /* clip, merge flags */
) {
icxLuLut *p; /* Object being created */
icmLuLut *luluto = (icmLuLut *)plu; /* Lookup Lut type object */
if ((p = (icxLuLut *) calloc(1,sizeof(icxLuLut))) == NULL)
return NULL;
p->pp = xicp;
p->plu = plu;
p->del = icxLuLut_free;
p->lutspaces = icxLutSpaces;
p->spaces = icxLuSpaces;
p->get_native_ranges = icxLu_get_native_ranges;
p->get_ranges = icxLu_get_ranges;
p->efv_wh_bk_points = icxLuEfv_wh_bk_points;
p->get_gamut = icxLuLutGamut;
p->fwd_relpcs_outpcs = icxLuLut_fwd_relpcs_outpcs;
p->bwd_outpcs_relpcs = icxLuLut_bwd_outpcs_relpcs;
p->nearclip = 0; /* Set flag defaults */
p->mergeclut = 0;
p->noisluts = 0;
p->noipluts = 0;
p->nooluts = 0;
p->intsep = 0;
p->lookup = icxLuLut_lookup;
p->in_abs = icxLuLut_in_abs;
p->matrix = icxLuLut_matrix;
p->input = icxLuLut_input;
p->clut = icxLuLut_clut;
p->clut_aux = icxLuLut_clut_aux;
p->output = icxLuLut_output;
p->out_abs = icxLuLut_out_abs;
p->inv_lookup = icxLuLut_inv_lookup;
p->inv_in_abs = icxLuLut_inv_in_abs;
p->inv_matrix = icxLuLut_inv_matrix;
p->inv_input = icxLuLut_inv_input;
p->inv_clut = icxLuLut_inv_clut;
p->inv_clut_aux = icxLuLut_inv_clut_aux;
p->inv_output = icxLuLut_inv_output;
p->inv_out_abs = icxLuLut_inv_out_abs;
p->clut_locus = icxLuLut_clut_aux_locus;
p->get_info = icxLuLut_get_info;
p->get_matrix = icxLuLut_get_matrix;
/* Setup all the rspl analogs of the icc Lut */
/* NOTE: We assume that none of this relies on the flag settings, */
/* since they will be set on our return. */
/* Get details of underlying, native icc color space */
p->plu->lutspaces(p->plu, &p->natis, NULL, &p->natos, NULL, &p->natpcs);
/* Get other details of conversion */
p->plu->spaces(p->plu, NULL, &p->inputChan, NULL, &p->outputChan, NULL, NULL, NULL, NULL, NULL);
/* Remember flags */
p->flags = flags;
/* Sanity check */
if (p->inputChan > MXDI) {
sprintf(p->pp->err,"xicc can only handle input channels of %d or less",MXDI);
p->inputChan = MXDI; /* Avoid going outside array bounds */
p->pp->errc = 1;
p->del((icxLuBase *)p);
return NULL;
}
if (p->outputChan > MXDO) {
sprintf(p->pp->err,"xicc can only handle output channels of %d or less",MXDO);
p->outputChan = MXDO; /* Avoid going outside array bounds */
p->pp->errc = 1;
p->del((icxLuBase *)p);
return NULL;
}
/* Get pointer to icmLut */
luluto->get_info(luluto, &p->lut, NULL, NULL, NULL);
return p;
}
/* Initialise the clut ink limiting and black */
/* generation information. */
/* return 0 or error code */
static int
setup_ink_icxLuLut(
icxLuLut *p, /* Object being initialised */
icxInk *ink, /* inking details (NULL for default) */
int setLminmax /* Figure the L locus for inking rule */
) {
int devchan = p->func == icmFwd ? p->inputChan : p->outputChan;
if (ink) {
p->ink = *ink; /* Copy the structure */
} else {
p->ink.tlimit = 3.0; /* default ink limit of 300% */
p->ink.klimit = -1.0; /* default no black limit */
p->ink.KonlyLmin = 0; /* default don't use K only black as Locus Lmin */
p->ink.k_rule = icxKluma5; /* default K generation rule */
p->ink.c.Ksmth = ICXINKDEFSMTH; /* Default smoothing */
p->ink.c.Kskew = ICXINKDEFSKEW; /* Default shape skew */
p->ink.c.Kstle = 0.0; /* Min K at white end */
p->ink.c.Kstpo = 0.0; /* Start of transition is at white */
p->ink.c.Kenle = 1.0; /* Max K at black end */
p->ink.c.Kenpo = 1.0; /* End transition at black */
p->ink.c.Kshap = 1.0; /* Linear transition */
}
/* Normalise total and black ink limits */
if (p->ink.tlimit <= 1e-4 || p->ink.tlimit >= (double)devchan)
p->ink.tlimit = -1.0; /* Turn ink limit off if not effective */
if (devchan < 4 || p->ink.klimit < 0.0 || p->ink.klimit >= 1.0)
p->ink.klimit = -1.0; /* Turn black limit off if not effective */
/* Set the ink limit information for any reverse interpolation. */
/* Calling this will clear the reverse interpolaton cache. */
p->clutTable->rev_set_limit(
p->clutTable, /* this */
p->ink.tlimit >= 0.0 || p->ink.klimit >= 0.0 ? icxLimitD_void : NULL,
/* Optional input space limit() function. */
/* Function should evaluate in[0..di-1], and return */
/* number that is not to exceed 0.0. NULL if not used. */
(void *)p, /* Context passed to limit() */
0.0 /* Value that limit() is not to exceed */
);
/* Duplicate in the CAM clip rspl if it exists */
if (p->cclutTable != NULL) {
p->cclutTable->rev_set_limit(
p->cclutTable, /* this */
p->ink.tlimit >= 0.0 || p->ink.klimit >= 0.0 ? icxLimitD_void : NULL,
/* Optional input space limit() function. */
/* Function should evaluate in[0..di-1], and return */
/* number that is not to exceed 0.0. NULL if not used. */
(void *)p, /* Context passed to limit() */
0.0 /* Value that limit() is not to exceed */
);
}
/* Figure Lmin and Lmax for icxKluma5 curve basis */
if (setLminmax
&& p->clutTable->di > p->clutTable->fdi) { /* If K generation makes sense */
double wh[3], bk[3], kk[3];
int mergeclut; /* Save/restore mergeclut value */
/* Get white/black in effective xlu PCS space */
p->efv_wh_bk_points((icxLuBase *)p, wh, bk, kk);
/* Convert from effective PCS (ie. Jab) to native XYZ or Lab PCS */
mergeclut = p->mergeclut; /* Hack to be able to use inv_out_abs() */
p->mergeclut = 0; /* if mergeclut is active. */
icxLuLut_inv_out_abs(p, wh, wh);
icxLuLut_inv_out_abs(p, bk, bk);
icxLuLut_inv_out_abs(p, kk, kk);
p->mergeclut = mergeclut; /* Restore */
/* Convert to Lab PCS */
if (p->natos == icSigXYZData) { /* Always do K rule in L space */
icmXYZ2Lab(&icmD50, wh, wh);
icmXYZ2Lab(&icmD50, bk, bk);
icmXYZ2Lab(&icmD50, kk, kk);
}
p->Lmax = 0.01 * wh[0];
if (p->ink.KonlyLmin != 0)
p->Lmin = 0.01 * kk[0];
else
p->Lmin = 0.01 * bk[0];
} else {
/* Some sane defaults */
p->Lmax = 1.0;
p->Lmin = 0.0;
}
return 0;
}
/* Initialise the clut clipping information, ink limiting */
/* and auxiliary parameter settings for all the rspl. */
/* return 0 or error code */
static int
setup_clip_icxLuLut(
icxLuLut *p /* Object being initialised */
) {
double tmin[MXDIDO], tmax[MXDIDO];
int i;
/* Setup for inversion of multi-dim clut */
p->kch = -1; /* Not known yet */
/* Set auxiliaries */
for (i = 0; i < p->inputChan; i++)
p->auxm[i] = 0;
if (p->inputChan > p->outputChan) {
switch(p->natis) {
case icSigCmykData:
/* Should fix icm/xicc to remember K channel */
p->auxm[3] = 1; /* K is the auxiliary channel */
break;
default:
if (p->kch >= 0) /* It's been discovered */
p->auxm[p->kch] = 1;
else {
p->pp->errc = 2;
sprintf(p->pp->err,"Unknown colorspace %s when setting auxliaries",
icm2str(icmColorSpaceSignature, p->natis));
return p->pp->errc;
}
break;
}
}
// p->auxlinf = NULL; /* Input space auxiliary linearisation function - not implemented */
// p->auxlinf_ctx = NULL; /* Opaque context for auxliliary linearisation function */
/* Aproximate center of clut input gamut - used for */
/* resolving multiple reverse solutions. */
p->clutTable->get_in_range(p->clutTable, tmin, tmax);
for (i = 0; i < p->clutTable->di; i++) {
p->licent[i] = p->icent[i] = (tmin[i] + tmax[i])/2.0;
}
/* Compute clip setup information relating to clut output gamut. */
if (p->nearclip != 0 /* Near clip requested */
|| p->inputChan == 1) { /* or vector clip won't work */
p->clip.nearclip = 1;
} else { /* Vector clip */
/* !!!! NOTE NOTE NOTE !!!! */
/* We should re-write this to avoid calling p->clutTable->rev_interp(), */
/* since this sets up all the rev acceleration tables for two calls, */
/* if this lut is being setup from scattered data, and never used */
/* for rev lookup */
icColorSpaceSignature clutos = p->natos;
fprintf(stderr, "!!!!! setup_clip_icxLuLut with vector clip - possibly unnecessary rev setup !!!!\n");
p->clip.nearclip = 0;
p->clip.LabLike = 0;
p->clip.fdi = p->clutTable->fdi;
switch(clutos) {
case icxSigJabData:
case icSigLabData: {
co pp; /* Room for all the solutions found */
int nsoln; /* Number of solutions found */
double cdir[MXDO]; /* Clip vector direction and length */
p->clip.LabLike = 1;
/* Find high clip target */
for (i = 0; i < p->inputChan; i++)
pp.p[i] = 0.0; /* Set aux values */
pp.v[0] = 105.0; pp.v[1] = pp.v[2] = 0.0; /* PCS Target value */
cdir[0] = cdir[1] = cdir[2] = 0.0; /* Clip Target */
p->inv_output(p, pp.v, pp.v); /* Compensate for output curve */
p->inv_output(p, cdir, cdir);
cdir[0] -= pp.v[0]; /* Clip vector */
cdir[1] -= pp.v[1];
cdir[2] -= pp.v[2];
/* PCS -> Device with clipping */
nsoln = p->clutTable->rev_interp(
p->clutTable, /* rspl object */
0, /* No hint flags - might be in gamut, might vector clip */
1, /* Maxumum solutions to return */
p->auxm, /* Auxiliary input targets */
cdir, /* Clip vector direction and length */
&pp); /* Input target and output solutions */
/* returned solutions in pp[0..retval-1].p[] */
nsoln &= RSPL_NOSOLNS; /* Get number of solutions */
if (nsoln != 1) {
p->pp->errc = 2;
sprintf(p->pp->err,"Failed to find high clip target for Lab space");
return p->pp->errc;
}
p->clip.ocent[0] = pp.v[0] - 0.001; /* Got main target */
p->clip.ocent[1] = pp.v[1];
p->clip.ocent[2] = pp.v[2];
/* Find low clip target */
pp.v[0] = -5.0; pp.v[1] = pp.v[2] = 0.0; /* PCS Target value */
cdir[0] = 100.0; cdir[1] = cdir[2] = 0.0; /* Clip Target */
p->inv_output(p, pp.v, pp.v); /* Compensate for output curve */
p->inv_output(p, cdir, cdir);
cdir[0] -= pp.v[0]; /* Clip vector */
cdir[1] -= pp.v[1];
cdir[2] -= pp.v[2];
/* PCS -> Device with clipping */
nsoln = p->clutTable->rev_interp(
p->clutTable, /* rspl object */
RSPL_WILLCLIP, /* Since there was no locus, we expect to have to clip */
1, /* Maxumum solutions to return */
NULL, /* No auxiliary input targets */
cdir, /* Clip vector direction and length */
&pp); /* Input target and output solutions */
/* returned solutions in pp[0..retval-1].p[] */
nsoln &= RSPL_NOSOLNS; /* Get number of solutions */
if (nsoln != 1) {
p->pp->errc = 2;
sprintf(p->pp->err,"Failed to find low clip target for Lab space");
return p->pp->errc;
}
p->clip.ocentv[0] = pp.v[0] + 0.001 - p->clip.ocent[0]; /* Raw line vector */
p->clip.ocentv[1] = pp.v[1] - p->clip.ocent[1];
p->clip.ocentv[2] = pp.v[2] - p->clip.ocent[2];
/* Compute vectors length */
for (p->clip.ocentl = 0.0, i = 0; i < 3; i++)
p->clip.ocentl += p->clip.ocentv[i] * p->clip.ocentv[i];
p->clip.ocentl = sqrt(p->clip.ocentl);
if (p->clip.ocentl <= 1e-8)
p->clip.ocentl = 0.0;
break;
}
case icSigXYZData:
// ~~~~~~1 need to add this.
default:
/* Do a crude approximation, that may not work. */
p->clutTable->get_out_range(p->clutTable, tmin, tmax);
for (i = 0; i < p->clutTable->fdi; i++) {
p->clip.ocent[i] = (tmin[i] + tmax[i])/2.0;
}
p->clip.ocentl = 0.0;
break;
}
}
return 0;
}
/* Function to pass to rspl to set secondary input/output transfer functions */
static void
icxLuLut_inout_func(
void *pp, /* icxLuLut */
double *out, /* output value */
double *in /* inut value */
) {
icxLuLut *p = (icxLuLut *)pp; /* this */
icmLuLut *luluto = (icmLuLut *)p->plu; /* Get icmLuLut object */
double tin[MAX_CHAN];
double tout[MAX_CHAN];
int i;
if (p->iol_out == 0) { /* fwd input */
#ifdef INK_LIMIT_TEST
tout[p->iol_ch] = in[0];
#else
for (i = 0; i < p->inputChan; i++)
tin[i] = 0.0;
tin[p->iol_ch] = in[0];
luluto->input(luluto, tout, tin);
#endif
} else if (p->iol_out == 1) { /* fwd output */
for (i = 0; i < p->outputChan; i++)
tin[i] = 0.0;
tin[p->iol_ch] = in[0];
luluto->output(luluto, tout, tin);
} else { /* bwd input */
#ifdef INK_LIMIT_TEST
tout[p->iol_ch] = in[0];
#else
for (i = 0; i < p->inputChan; i++)
tin[i] = 0.0;
tin[p->iol_ch] = in[0];
luluto->inv_input(luluto, tout, tin);
/* This won't be valid if matrix is used or there is a PCS conversion !!! */
/* Shouldn't be a problem because this is only used for inverting dev->PCS ? */
luluto->inv_in_abs(luluto, tout, tout);
#endif
}
out[0] = tout[p->iol_ch];
}
/* Function to pass to rspl to set clut up, when mergeclut is set */
static void
icxLuLut_clut_merge_func(
void *pp, /* icxLuLut */
double *out, /* output value */
double *in /* input value */
) {
icxLuLut *p = (icxLuLut *)pp; /* this */
icmLuLut *luluto = (icmLuLut *)p->plu; /* Get icmLuLut object */
luluto->clut(luluto, out, in);
luluto->output(luluto, out, out);
luluto->out_abs(luluto, out, out);
if (p->outs == icxSigJabData) {
p->cam->XYZ_to_cam(p->cam, out, out);
}
}
/* Implimenation of Lut create from icc. */
/* Note that xicc_get_luobj() will have set the pcsor & */
/* intent to consistent values if Jab and/or icxAppearance */
/* has been requested. */
/* It will also have created the underlying icm lookup object */
/* that is used to create and implement the icx one. The icm */
/* will be used to translate from native to effective PCS, unless */
/* the effective PCS is Jab, in which case the icm will be set to */
/* have an effective PCS of XYZ. Since native<->effecive PCS conversion */
/* is done at the to/from_abs() stage, none of it affects the individual */
/* conversion steps, which will all talk the native PCS (unless merged). */
static icxLuBase *
new_icxLuLut(
xicc *xicp,
int flags, /* clip, merge flags */
icmLuBase *plu, /* Pointer to Lu we are expanding (ours) */
icmLookupFunc func, /* Functionality requested */
icRenderingIntent intent, /* Rendering intent */
icColorSpaceSignature pcsor, /* PCS override (0 = def) */
icxViewCond *vc, /* Viewing Condition (NULL if not using CAM) */
icxInk *ink /* inking details (NULL for default) */
) {
icxLuLut *p; /* Object being created */
icmLuLut *luluto = (icmLuLut *)plu; /* Lookup Lut type object */
int i;
/* Do basic creation and initialisation */
if ((p = alloc_icxLuLut(xicp, plu, flags)) == NULL)
return NULL;
p->func = func;
/* Set LuLut "use" specific creation flags: */
if (flags & ICX_CLIP_NEAREST)
p->nearclip = 1;
if (flags & ICX_MERGE_CLUT)
p->mergeclut = 1;
if (flags & ICX_FAST_SETUP)
p->fastsetup = 1;
/* We're only implementing this under specific conditions. */
if (flags & ICX_CAM_CLIP
&& func == icmFwd
&& !(p->mergeclut != 0 && pcsor == icxSigJabData)) /* Don't need camclip if merged Jab */
p->camclip = 1;
if (flags & ICX_INT_SEPARATE) {
fprintf(stderr,"~1 Internal optimised 4D separations not yet implemented!\n");
p->intsep = 1;
}
/* Init the CAM model if it will be used */
if (pcsor == icxSigJabData || p->camclip) {
if (vc != NULL) /* One has been provided */
p->vc = *vc; /* Copy the structure */
else
xicc_enum_viewcond(xicp, &p->vc, -1, NULL, 0, NULL); /* Use a default */
p->cam = new_icxcam(cam_default);
p->cam->set_view(p->cam, p->vc.Ev, p->vc.Wxyz, p->vc.La, p->vc.Yb, p->vc.Lv,
p->vc.Yf, p->vc.Yg, p->vc.Gxyz, XICC_USE_HK);
} else {
p->cam = NULL;
}
/* Remember the effective intent */
p->intent = intent;
/* Get the effective spaces of underlying icm */
plu->spaces(plu, &p->ins, NULL, &p->outs, NULL, NULL, NULL, NULL, &p->pcs, NULL);
/* Override with pcsor */
/* We assume that any profile that has a CIE color as a "device" color */
/* intends it to stay that way, and not be overridden. */
if (pcsor == icxSigJabData) {
p->pcs = pcsor;
if (xicp->pp->header->deviceClass == icSigAbstractClass) {
p->ins = pcsor;
p->outs = pcsor;
} else if (xicp->pp->header->deviceClass != icSigLinkClass) {
if (func == icmBwd || func == icmGamut || func == icmPreview)
p->ins = pcsor;
if (func == icmFwd || func == icmPreview)
p->outs = pcsor;
}
}
/* In general the native and effective ranges of the icx will be the same as the */
/* underlying icm lookup object. */
p->plu->get_lutranges(p->plu, p->ninmin, p->ninmax, p->noutmin, p->noutmax);
p->plu->get_ranges(p->plu, p->inmin, p->inmax, p->outmin, p->outmax);
/* If we have a Jab PCS override, reflect this in the effective icx range. */
/* Note that the ab ranges are nominal. They will exceed this range */
/* for colors representable in L*a*b* PCS */
if (p->ins == icxSigJabData) {
p->inmin[0] = 0.0; p->inmax[0] = 100.0;
p->inmin[1] = -128.0; p->inmax[1] = 128.0;
p->inmin[2] = -128.0; p->inmax[2] = 128.0;
} else if (p->outs == icxSigJabData) {
p->outmin[0] = 0.0; p->outmax[0] = 100.0;
p->outmin[1] = -128.0; p->outmax[1] = 128.0;
p->outmin[2] = -128.0; p->outmax[2] = 128.0;
}
/* If we have a merged clut, reflect this in the icx native PCS range. */
/* Merging merges output processing (irrespective of whether we are using */
/* the forward or backward cluts) */
if (p->mergeclut != 0) {
int i;
for (i = 0; i < p->outputChan; i++) {
p->noutmin[i] = p->outmin[i];
p->noutmax[i] = p->outmax[i];
}
}
/* ------------------------------- */
/* Create rspl based input lookups */
for (i = 0; i < p->inputChan; i++) {
if ((p->inputTable[i] = new_rspl(RSPL_NOFLAGS, 1, 1)) == NULL) {
p->pp->errc = 2;
sprintf(p->pp->err,"Creation of input table rspl failed");
p->del((icxLuBase *)p);
return NULL;
}
p->iol_out = 0; /* Input lookup */
p->iol_ch = i; /* Chanel */
p->inputTable[i]->set_rspl(p->inputTable[i], RSPL_NOFLAGS,
(void *)p, icxLuLut_inout_func,
&p->ninmin[i], &p->ninmax[i], (int *)&p->lut->inputEnt, &p->ninmin[i], &p->ninmax[i]);
}
/* Setup center clip target for inversion */
for (i = 0; i < p->inputChan; i++) {
p->inputClipc[i] = (p->ninmin[i] + p->ninmax[i])/2.0;
}
/* Create rspl based reverse input lookups used in ink limit and locus range functions. */
for (i = 0; i < p->inputChan; i++) {
int gres = p->inputTable[i]->g.mres; /* Same res as curve being inverted */
if (gres < 256)
gres = 256;
if ((p->revinputTable[i] = new_rspl(RSPL_NOFLAGS, 1, 1)) == NULL) {
p->pp->errc = 2;
sprintf(p->pp->err,"Creation of reverse input table rspl failed");
p->del((icxLuBase *)p);
return NULL;
}
p->iol_out = 2; /* Input lookup */
p->iol_ch = i; /* Chanel */
p->revinputTable[i]->set_rspl(p->revinputTable[i], RSPL_NOFLAGS,
(void *)p, icxLuLut_inout_func,
&p->ninmin[i], &p->ninmax[i], &gres, &p->ninmin[i], &p->ninmax[i]);
}
/* ------------------------------- */
{
int gres[MXDI];
for (i = 0; i < p->inputChan; i++)
gres[i] = p->lut->clutPoints;
/* Create rspl based multi-dim table */
if ((p->clutTable = new_rspl((p->fastsetup ? RSPL_FASTREVSETUP : RSPL_NOFLAGS)
| (flags & ICX_VERBOSE ? RSPL_VERBOSE : RSPL_NOFLAGS),
p->inputChan, p->outputChan)) == NULL) {
p->pp->errc = 2;
sprintf(p->pp->err,"Creation of clut table rspl failed");
p->del((icxLuBase *)p);
return NULL;
}
if (p->mergeclut == 0) { /* Do this if it's not merged with clut, */
p->clutTable->set_rspl(p->clutTable, RSPL_NOFLAGS,
(void *)luluto, (void (*)(void *, double *, double *))luluto->clut,
p->ninmin, p->ninmax, gres, p->noutmin, p->noutmax);
} else { /* If mergeclut */
p->clutTable->set_rspl(p->clutTable, RSPL_NOFLAGS,
(void *)p, icxLuLut_clut_merge_func,
p->ninmin, p->ninmax, gres, p->noutmin, p->noutmax);
}
/* clut clipping is setup separately */
}
/* ------------------------------- */
/* Create rspl based output lookups */
for (i = 0; i < p->outputChan; i++) {
if ((p->outputTable[i] = new_rspl(RSPL_NOFLAGS, 1, 1)) == NULL) {
p->pp->errc = 2;
sprintf(p->pp->err,"Creation of output table rspl failed");
p->del((icxLuBase *)p);
return NULL;
}
p->iol_out = 1; /* Output lookup */
p->iol_ch = i; /* Chanel */
p->outputTable[i]->set_rspl(p->outputTable[i], RSPL_NOFLAGS,
(void *)p, icxLuLut_inout_func,
&p->noutmin[i], &p->noutmax[i], (int *)&p->lut->outputEnt, &p->noutmin[i], &p->noutmax[i]);
}
/* Setup center clip target for inversion */
for (i = 0; i < p->outputChan; i++) {
p->outputClipc[i] = (p->noutmin[i] + p->noutmax[i])/2.0;
}
/* ------------------------------- */
/* Setup all the clipping, ink limiting and auxiliary stuff, */
/* in case a reverse call is used. Only do this if we know */
/* the reverse stuff isn't going to fail due to channel limits. */
if (p->clutTable->within_restrictedsize(p->clutTable)) {
if (setup_ink_icxLuLut(p, ink, 1) != 0) {
p->del((icxLuBase *)p);
return NULL;
}
if (setup_clip_icxLuLut(p) != 0) {
p->del((icxLuBase *)p);
return NULL;
}
}
return (icxLuBase *)p;
}
/* Function to pass to rspl to set clut up, when camclip is going to be used. */
/* We use the temporary icm fwd absolute xyz lookup, then convert to CAM Jab. */
static void
icxLuLut_clut_camclip_func(
void *pp, /* icxLuLut */
double *out, /* output value */
double *in /* inut value */
) {
icxLuLut *p = (icxLuLut *)pp; /* this */
icmLuLut *luluto = (icmLuLut *)p->absxyzlu;
luluto->clut(luluto, out, in);
luluto->output(luluto, out, out);
luluto->out_abs(luluto, out, out);
p->cam->XYZ_to_cam(p->cam, out, out);
out[0] *= CCJSCALE;
}
/* Initialise the additional CAM space clut rspl, used to compute */
/* reverse lookup CAM clipping results when the camclip flag is set. */
/* We scale J by CCJSCALE, to give a more L* preserving clip direction */
/* return error code. */
static int
icxLuLut_init_clut_camclip(
icxLuLut *p) {
int e, gres[MXDI];
/* Setup so clut contains transform to CAM Jab */
/* (camclip is only used in fwd or invfwd direction lookup) */
double cmin[3], cmax[3];
cmin[0] = 0.0; cmax[0] = CCJSCALE * 100.0; /* Nominal Jab output ranges */
cmin[1] = -128.0; cmax[1] = 128.0;
cmin[2] = -128.0; cmax[2] = 128.0;
/* Get icm lookup we need for seting up and using CAM icx clut */
if ((p->absxyzlu = p->pp->pp->get_luobj(p->pp->pp, icmFwd, icAbsoluteColorimetric,
icSigXYZData, icmLuOrdNorm)) == NULL) {
p->pp->errc = p->pp->pp->errc; /* Copy error to xicc */
strcpy(p->pp->err, p->pp->pp->err);
return p->pp->errc;
}
/* Create CAM rspl based multi-dim table */
if ((p->cclutTable = new_rspl((p->fastsetup ? RSPL_FASTREVSETUP : RSPL_NOFLAGS)
| (p->flags & ICX_VERBOSE ? RSPL_VERBOSE : RSPL_NOFLAGS),
p->inputChan, p->outputChan)) == NULL) {
p->pp->errc = 2;
sprintf(p->pp->err,"Creation of clut table rspl failed");
return p->pp->errc;
}
for (e = 0; e < p->inputChan; e++)
gres[e] = p->lut->clutPoints;
/* Setup our special CAM space rspl */
p->cclutTable->set_rspl(p->cclutTable, RSPL_NOFLAGS, (void *)p,
icxLuLut_clut_camclip_func,
p->ninmin, p->ninmax, gres, cmin, cmax);
/* Duplicate the ink limit information for any reverse interpolation. */
p->cclutTable->rev_set_limit(
p->cclutTable, /* this */
p->ink.tlimit >= 0.0 || p->ink.klimit >= 0.0 ? icxLimitD_void : NULL,
/* Optional input space limit() function. */
/* Function should evaluate in[0..di-1], and return */
/* number that is not to exceed 0.0. NULL if not used. */
(void *)p, /* Context passed to limit() */
0.0 /* Value that limit() is not to exceed */
);
return 0;
}
/* ========================================================== */
/* xicc creation code */
/* ========================================================== */
/* Callback for computing delta E squared for two output (PCS) */
/* values, passed as a callback to xfit */
static double xfit_to_de2(void *cntx, double *in1, double *in2) {
icxLuLut *p = (icxLuLut *)cntx;
double rv;
if (p->pcs == icSigLabData) {
#ifdef USE_CIE94_DE
rv = icmCIE94sq(in1, in2);
#else
rv = icmLabDEsq(in1, in2);
#endif
} else {
double lab1[3], lab2[3];
icmXYZ2Lab(&icmD50, lab1, in1);
icmXYZ2Lab(&icmD50, lab2, in2);
#ifdef USE_CIE94_DE
rv = icmCIE94sq(lab1, lab2);
#else
rv = icmLabDEsq(lab1, lab2);
#endif
}
return rv;
}
/* Same as above plus partial derivatives */
static double xfit_to_dde2(void *cntx, double dout[2][MXDIDO], double *in1, double *in2) {
icxLuLut *p = (icxLuLut *)cntx;
double rv;
if (p->pcs == icSigLabData) {
int j,k;
double tdout[2][3];
#ifdef USE_CIE94_DE
rv = icxdCIE94sq(tdout, in1, in2);
#else
rv = icxdLabDEsq(tdout, in1, in2);
#endif
for (k = 0; k < 2; k++) {
for (j = 0; j < 3; j++)
dout[k][j] = tdout[k][j];
}
} else {
double lab1[3], lab2[3];
double dout12[2][3][3];
double tdout[2][3];
int i,j,k;
icxdXYZ2Lab(&icmD50, lab1, dout12[0], in1);
icxdXYZ2Lab(&icmD50, lab2, dout12[1], in2);
#ifdef USE_CIE94_DE
rv = icxdCIE94sq(tdout, lab1, lab2);
#else
rv = icxdLabDEsq(tdout, lab1, lab2);
#endif
/* Compute partial derivative (is this correct ??) */
for (k = 0; k < 2; k++) {
for (j = 0; j < 3; j++) {
dout[k][j] = 0.0;
for (i = 0; i < 3; i++) {
dout[k][j] += tdout[k][i] * dout12[k][i][j];
}
}
}
}
return rv;
}
#ifdef NEVER
/* Check partial derivative function within xfit_to_dde2() */
static double _xfit_to_dde2(void *cntx, double dout[2][MXDIDO], double *in1, double *in2) {
icxLuLut *pp = (icxLuLut *)cntx;
int k, i;
double rv, drv;
double trv;
rv = xfit_to_de2(cntx, in1, in2);
drv = xfit_to_dde2(cntx, dout, in1, in2);
if (fabs(rv - drv) > 1e-6)
printf("######## DDE2: RV MISMATCH is %f should be %f ########\n",rv,drv);
/* Check each parameter delta */
for (k = 0; k < 2; k++) {
for (i = 0; i < 3; i++) {
double *in;
double del;
if (k == 0)
in = in1;
else
in = in2;
in[i] += 1e-9;
trv = xfit_to_de2(cntx, in1, in2);
in[i] -= 1e-9;
/* Check that del is correct */
del = (trv - rv)/1e-9;
if (fabs(dout[k][i] - del) > 0.04) {
printf("######## DDE2: EXCESSIVE at in[%d][%d] is %f should be %f ########\n",k,i,dout[k][i],del);
}
}
}
return rv;
}
#define xfit_to_dde2 _xfit_to_dde2
#endif
/* Context for rspl setting input and output curves */
typedef struct {
int iix;
int oix;
xfit *xf; /* Optimisation structure */
} curvectx;
/* Function to pass to rspl to set input and output */
/* transfer function for xicc lookup function */
static void
set_linfunc(
void *cc, /* curvectx structure */
double *out, /* Device output value */
double *in /* Device input value */
) {
curvectx *c = (curvectx *)cc; /* this */
xfit *p = c->xf;
if (c->iix >= 0) { /* Input curve */
*out = p->incurve(p, *in, c->iix);
} else if (c->oix >= 0) { /* Output curve */
*out = p->outcurve(p, *in, c->oix);
}
}
/* Function to pass to rspl to set inverse input transfer function, */
/* used for ink limiting calculation. */
static void
icxLuLut_invinput_func(
void *cc, /* curvectx structure */
double *out, /* Device output value */
double *in /* Device input value */
) {
curvectx *c = (curvectx *)cc; /* this */
xfit *p = c->xf;
*out = p->invincurve(p, *in, c->iix);
}
/* Functions to pass to icc settables() to setup icc A2B Lut: */
/* Input table */
static void set_input(void *cntx, double *out, double *in) {
icxLuLut *p = (icxLuLut *)cntx;
if (p->noisluts != 0 && p->noipluts != 0) { /* Input table must be linear */
int i;
for (i = 0; i < p->inputChan; i++)
out[i] = in[i];
} else {
if (p->input(p, out, in) > 1)
error ("%d, %s",p->pp->errc,p->pp->err);
}
}
/* clut */
static void set_clut(void *cntx, double *out, double *in) {
icxLuLut *p = (icxLuLut *)cntx;
int f;
if (p->clut(p, out, in) > 1)
error ("%d, %s",p->pp->errc,p->pp->err);
/* Convert from efective pcs to natural pcs */
if (p->pcs != p->plu->icp->header->pcs) {
if (p->pcs == icSigLabData)
icmLab2XYZ(&icmD50, out, out);
else
icmXYZ2Lab(&icmD50, out, out);
}
}
/* output */
static void set_output(void *cntx, double *out, double *in) {
icxLuLut *p = (icxLuLut *)cntx;
if (p->nooluts != 0) { /* Output table must be linear */
int i;
for (i = 0; i < p->outputChan; i++)
out[i] = in[i];
} else {
if (p->output(p, out, in) > 1)
error ("%d, %s",p->pp->errc,p->pp->err);
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Routine to figure out a suitable black point for CMYK */
/* Structure to hold optimisation information */
typedef struct {
icxLuLut *p; /* Object being created */
double toAbs[3][3]; /* To abs from aprox relative */
double p1[3]; /* white pivot point in abs Lab */
double p2[3]; /* Point on vector towards black */
double toll; /* Tollerance of black direction */
} bfinds;
/* Optimise device values to minimise L, while remaining */
/* within the ink limit, and staying in line between p1 (white) and p2 (black dir) */
static double bfindfunc(void *adata, double pv[]) {
bfinds *b = (bfinds *)adata;
double rv = 0.0;
double tt[3], Lab[3];
co bcc;
double lr, ta, tb, terr; /* L ratio, target a, target b, target error */
double ovr;
//printf("~1 bfindfunc got %f %f %f %f\n", pv[0], pv[1], pv[2], pv[3]);
/* See if over ink limit or outside device range */
ovr = icxLimit(b->p, pv); /* > 0.0 if outside device gamut or ink limit */
if (ovr < 0.0)
ovr = 0.0;
//printf("~1 bfindfunc got ovr %f\n", ovr);
/* Compute the absolute Lab value: */
b->p->input(b->p, bcc.p, pv); /* Through input tables */
b->p->clutTable->interp(b->p->clutTable, &bcc); /* Through clut */
b->p->output(b->p, bcc.v, bcc.v); /* Through the output tables */
if (b->p->pcs != icSigXYZData) /* Convert PCS to XYZ */
icmLab2XYZ(&icmD50, bcc.v, bcc.v);
/* Convert from relative to Absolute colorimetric */
icmMulBy3x3(tt, b->toAbs, bcc.v);
icmXYZ2Lab(&icmD50, Lab, tt); /* Convert to Lab */
#ifdef DEBUG
printf("~1 p1 = %f %f %f, p2 = %f %f %f\n",b->p1[0],b->p1[1],b->p1[2],b->p2[0],b->p2[1],b->p2[2]);
printf("~1 device value %f %f %f %f, Lab = %f %f %f\n",pv[0],pv[1],pv[2],pv[3],Lab[0],Lab[1],Lab[2]);
#endif
/* Primary aim is to minimise L value */
rv = Lab[0];
/* See how out of line from p1 to p2 we are */
lr = (Lab[0] - b->p1[0])/(b->p2[0] - b->p1[0]); /* Distance towards p2 from p1 */
ta = lr * (b->p2[1] - b->p1[1]) + b->p1[1]; /* Target a value */
tb = lr * (b->p2[2] - b->p1[2]) + b->p1[2]; /* Target b value */
terr = (ta - Lab[1]) * (ta - Lab[1])
+ (tb - Lab[2]) * (tb - Lab[2]);
if (terr < b->toll) /* Tollerance error doesn't count until it's over tollerance */
terr = 0.0;
#ifdef DEBUG
printf("~1 target error %f\n",terr);
#endif
rv += XICC_BLACK_FIND_ABERR_WEIGHT * terr; /* Make ab match more important than min. L */
#ifdef DEBUG
printf("~1 out of range error %f\n",ovr);
#endif
rv += 200 * ovr;
#ifdef DEBUG
printf("~1 black find tc ret %f\n",rv);
#endif
return rv;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Create icxLuLut and underlying fwd Lut from scattered data */
/* The scattered data is assumed to map Device -> native PCS */
/* NOTE:- in theory once this icxLuLut is setup, it can be */
/* called to translate color values. In practice I suspect */
/* that the icxLuLut hasn't been setup completely enough to allows this. */
/* Might be easier to close it and re-open it ? */
static icxLuBase *
set_icxLuLut(
xicc *xicp,
icmLuBase *plu, /* Pointer to Lu we are expanding (ours) */
icmLookupFunc func, /* Functionality requested */
icRenderingIntent intent, /* Rendering intent */
int flags, /* white/black point flags etc. */
int nodp, /* Number of points */
int nodpbw, /* Number of points to look for white & black patches in */
cow *ipoints, /* Array of input points (Lab or XYZ normalized to 1.0) */
icxMatrixModel *skm, /* Optional skeleton model (used for input profiles) */
double dispLuminance, /* > 0.0 if display luminance value and is known */
double wpscale, /* > 0.0 if white point is to be scaled */
double smooth, /* RSPL smoothing factor, -ve if raw */
double avgdev, /* reading Average Deviation as a prop. of the input range */
double demph, /* dark emphasis factor for cLUT grid res. */
icxViewCond *vc, /* Viewing Condition (NULL if not using CAM) */
icxInk *ink, /* inking details (NULL for default) */
int quality /* Quality metric, 0..3 */
) {
icxLuLut *p; /* Object being created */
icc *icco = xicp->pp; /* Underlying icc object */
int luflags = 0; /* icxLuLut alloc clip, merge flags */
int pcsy; /* Effective PCS L or Y chanel index */
double pcsymax; /* Effective PCS L or Y maximum value */
icmHeader *h = icco->header; /* Pointer to icc header */
int maxchan; /* max(inputChan, outputChan) */
int rsplflags = RSPL_NOFLAGS; /* Flags for scattered data rspl */
int e, f, i, j;
double dwhite[MXDI], dblack[MXDI]; /* Device white and black values */
double wp[3]; /* Absolute White point in XYZ */
double bp[3]; /* Absolute Black point in XYZ */
double dgwhite[MXDI]; /* Device space gamut boundary white (ie. RGB 1,1,1) */
double oavgdev[MXDO]; /* Average output value deviation */
int gres[MXDI]; /* RSPL/CLUT resolution */
xfit *xf = NULL; /* Curve fitting class instance */
if (flags & ICX_VERBOSE)
rsplflags |= RSPL_VERBOSE;
// if (flags & ICX_2PASSSMTH)
// rsplflags |= RSPL_2PASSSMTH; /* Smooth data using Gaussian */
// if (flags & ICX_EXTRA_FIT)
// rsplflags |= RSPL_EXTRAFIT2; /* Try extra hard to fit data */
luflags = flags; /* Transfer straight though ? */
/* Do basic creation and initialisation */
if ((p = alloc_icxLuLut(xicp, plu, luflags)) == NULL)
return NULL;
p->func = func;
/* Set LuLut "use" specific creation flags: */
if (flags & ICX_CLIP_NEAREST)
p->nearclip = 1;
/* Set LuLut "create" specific flags: */
if (flags & ICX_NO_IN_SHP_LUTS)
p->noisluts = 1;
if (flags & ICX_NO_IN_POS_LUTS)
p->noipluts = 1;
if (flags & ICX_NO_OUT_LUTS)
p->nooluts = 1;
/* Get the effective spaces of underlying icm, and set icx the same */
plu->spaces(plu, &p->ins, NULL, &p->outs, NULL, NULL, &p->intent, NULL, &p->pcs, NULL);
/* For set_icx the effective pcs has to be the same as the native pcs */
if (p->pcs == icSigXYZData) {
pcsy = 1; /* Y of XYZ */
pcsymax = 1.0;
} else {
pcsy = 0; /* L or Lab */
pcsymax = 100.0;
}
maxchan = p->inputChan > p->outputChan ? p->inputChan : p->outputChan;
/* Translate overall average deviation into output channel deviation */
/* (This is for rspl scattered data fitting smoothness adjustment) */
/* (This could do with more tuning) */
/* XYZ display models are under-smoothed, because the mapping is typically */
/* very "straight", and the lack of tension reduces any noise reduction effect. */
/* !!! This probably means that we should switch to 3rd order smoothness criteria !! */
/* We apply an arbitrary correction here */
if (p->pcs == icSigXYZData) {
oavgdev[0] = XYZ_EXTRA_SMOOTH * 0.70 * avgdev;
oavgdev[1] = XYZ_EXTRA_SMOOTH * 1.00 * avgdev;
oavgdev[2] = XYZ_EXTRA_SMOOTH * 0.70 * avgdev;
} else if (p->pcs == icSigLabData) {
oavgdev[0] = 1.00 * avgdev;
oavgdev[1] = 0.70 * avgdev;
oavgdev[2] = 0.70 * avgdev;
} else { /* Hmmm */
for (f = 0; f < p->outputChan; f++)
oavgdev[f] = avgdev;
}
/* In general the native and effective ranges of the icx will be the same as the */
/* underlying icm lookup object. */
p->plu->get_lutranges(p->plu, p->ninmin, p->ninmax, p->noutmin, p->noutmax);
p->plu->get_ranges(p->plu, p->inmin, p->inmax, p->outmin, p->outmax);
/* ??? Does this ever happen with set_icxLuLut() ??? */
/* If we have a Jab PCS override, reflect this in the effective icx range. */
/* Note that the ab ranges are nominal. They will exceed this range */
/* for colors representable in L*a*b* PCS */
if (p->ins == icxSigJabData) {
p->inmin[0] = 0.0; p->inmax[0] = 100.0;
p->inmin[1] = -128.0; p->inmax[1] = 128.0;
p->inmin[2] = -128.0; p->inmax[2] = 128.0;
} else if (p->outs == icxSigJabData) {
p->outmin[0] = 0.0; p->outmax[0] = 100.0;
p->outmin[1] = -128.0; p->outmax[1] = 128.0;
p->outmin[2] = -128.0; p->outmax[2] = 128.0;
}
/* ------------------------------- */
if (flags & ICX_VERBOSE)
printf("Estimating white point\n");
icmXYZ2Ary(wp, icmD50); /* Set a default value - D50 */
icmXYZ2Ary(bp, icmBlack); /* Set a default value - absolute black */
{
/* Figure out as best we can the device white and black points */
/* Compute device white and black points as if */
/* we are doing an Output or Display device */
{
switch (h->colorSpace) {
case icSigCmykData:
for (e = 0; e < p->inputChan; e++) {
dwhite[e] = 0.0;
dblack[e] = 1.0;
}
break;
case icSigCmyData:
for (e = 0; e < p->inputChan; e++) {
dwhite[e] = 0.0;
dblack[e] = 1.0;
}
break;
case icSigRgbData:
for (e = 0; e < p->inputChan; e++) {
dwhite[e] = 1.0;
dblack[e] = 0.0;
}
break;
case icSigGrayData: { /* Could be additive or subtractive */
double maxY, minY; /* Y min and max */
double maxd, mind; /* Corresponding device values */
maxY = -1e8, minY = 1e8;
/* Figure out if it's additive or subtractive */
for (i = 0; i < nodpbw; i++) {
double Y;
if (p->pcs != icSigXYZData) { /* Convert white point to XYZ */
double xyz[3];
icmLab2XYZ(&icmD50, xyz, ipoints[i].v);
Y = xyz[1];
} else {
Y = ipoints[i].v[1];
}
if (Y > maxY) {
maxY = Y;
maxd = ipoints[i].p[0];
}
if (Y < minY) {
minY = Y;
mind = ipoints[i].p[0];
}
}
if (maxd < mind) { /* Subtractive */
for (e = 0; e < p->inputChan; e++) {
dwhite[e] = 0.0;
dblack[e] = 1.0;
}
} else { /* Additive */
for (e = 0; e < p->inputChan; e++) {
dwhite[e] = 1.0;
dblack[e] = 0.0;
}
}
break;
}
default:
xicp->errc = 1;
sprintf(xicp->err,"set_icxLuLut: can't handle color space %s",
icm2str(icmColorSpaceSignature, h->colorSpace));
p->del((icxLuBase *)p);
return NULL;
break;
}
}
/* dwhite is what we want for dgwhite[], used for XFIT_OUT_WP_REL_US */
for (e = 0; e < p->inputChan; e++)
dgwhite[e] = dwhite[e];
/* If this is actuall an input device, lookup wp & bp */
/* and override dwhite & dblack */
if (h->deviceClass == icSigInputClass) {
double wpy = -1e60, bpy = 1e60;
int wix = -1, bix = -1;
/* We assume that the input target is well behaved, */
/* and that it includes a white and black point patch, */
/* and that they have the extreme L/Y values */
/*
NOTE that this may not be the best approach !
It may be better to average the chromaticity
of all the neutral seeming patches, since
the whitest patch may have (for instance)
a blue tint.
*/
/* Discover the white and black patches */
for (i = 0; i < nodpbw; i++) {
double labv[3], yv;
/* Create D50 Lab to allow some chromatic sensitivity */
/* in picking the white point */
if (p->pcs == icSigXYZData)
icmXYZ2Lab(&icmD50, labv, ipoints[i].v);
else
icmCpy3(labv,ipoints[i].v);
#ifdef NEVER
/* Choose largest Y or L* */
if (ipoints[i].v[pcsy] > wpy) {
wp[0] = ipoints[i].v[0];
wp[1] = ipoints[i].v[1];
wp[2] = ipoints[i].v[2];
for (e = 0; e < p->inputChan; e++)
dwhite[e] = ipoints[i].p[e];
wpy = ipoints[i].v[pcsy];
wix = i;
}
#else
/* Tilt things towards D50 neutral white patches */
yv = labv[0] - 0.3 * sqrt(labv[1] * labv[1] + labv[2] * labv[2]);
//printf("~1 patch %d Lab = %s, yv = %f\n",i+1,icmPdv(3,labv),yv);
if (yv > wpy) {
//printf("~1 best so far\n");
wp[0] = ipoints[i].v[0];
wp[1] = ipoints[i].v[1];
wp[2] = ipoints[i].v[2];
for (e = 0; e < p->inputChan; e++)
dwhite[e] = ipoints[i].p[e];
wpy = yv;
wix = i;
}
#endif
if (ipoints[i].v[pcsy] < bpy) {
bp[0] = ipoints[i].v[0];
bp[1] = ipoints[i].v[1];
bp[2] = ipoints[i].v[2];
for (e = 0; e < p->inputChan; e++)
dblack[e] = ipoints[i].p[e];
bpy = ipoints[i].v[pcsy];
bix = i;
}
}
/* Make sure values are XYZ */
if (p->pcs != icSigXYZData) {
icmLab2XYZ(&icmD50, wp, wp);
icmLab2XYZ(&icmD50, bp, bp);
}
if (flags & ICX_VERBOSE) {
printf("Picked white patch %d with dev = %s\n XYZ = %s, Lab = %s\n",
wix+1, icmPdv(p->inputChan, dwhite), icmPdv(3, wp), icmPLab(wp));
printf("Picked black patch %d with dev = %s\n XYZ = %s, Lab = %s\n",
bix+1, icmPdv(p->inputChan, dblack), icmPdv(3, bp), icmPLab(bp));
}
/* else Output or Display device */
} else {
/* We assume that the output target is well behaved, */
/* and that it includes a white point patch. */
int nw = 0;
wp[0] = wp[1] = wp[2] = 0.0;
switch (h->colorSpace) {
case icSigCmykData:
for (i = 0; i < nodpbw; i++) {
if (ipoints[i].p[0] < 0.001
&& ipoints[i].p[1] < 0.001
&& ipoints[i].p[2] < 0.001
&& ipoints[i].p[3] < 0.001) {
wp[0] += ipoints[i].v[0];
wp[1] += ipoints[i].v[1];
wp[2] += ipoints[i].v[2];
nw++;
}
}
break;
case icSigCmyData:
for (i = 0; i < nodpbw; i++) {
if (ipoints[i].p[0] < 0.001
&& ipoints[i].p[1] < 0.001
&& ipoints[i].p[2] < 0.001) {
wp[0] += ipoints[i].v[0];
wp[1] += ipoints[i].v[1];
wp[2] += ipoints[i].v[2];
nw++;
}
}
break;
case icSigRgbData:
for (i = 0; i < nodpbw; i++) {
if (ipoints[i].p[0] > 0.999
&& ipoints[i].p[1] > 0.999
&& ipoints[i].p[2] > 0.999) {
wp[0] += ipoints[i].v[0];
wp[1] += ipoints[i].v[1];
wp[2] += ipoints[i].v[2];
nw++;
}
}
break;
case icSigGrayData: { /* Could be additive or subtractive */
double minwp[3], maxwp[3];
int nminwp = 0, nmaxwp = 0;
minwp[0] = minwp[1] = minwp[2] = 0.0;
maxwp[0] = maxwp[1] = maxwp[2] = 0.0;
/* Look for both */
for (i = 0; i < nodpbw; i++) {
if (ipoints[i].p[0] < 0.001)
minwp[0] += ipoints[i].v[0];
minwp[1] += ipoints[i].v[1];
minwp[2] += ipoints[i].v[2]; {
nminwp++;
}
if (ipoints[i].p[0] > 0.999)
maxwp[0] += ipoints[i].v[0];
maxwp[1] += ipoints[i].v[1];
maxwp[2] += ipoints[i].v[2]; {
nmaxwp++;
}
}
if (nminwp > 0) { /* Subtractive */
wp[0] = minwp[0];
wp[1] = minwp[1];
wp[2] = minwp[2];
nw = nminwp;
if (minwp[pcsy]/nminwp < (0.5 * pcsymax))
nw = 0; /* Looks like a mistake */
}
if (nmaxwp > 0 /* Additive */
&& (nminwp == 0 || maxwp[pcsy]/nmaxwp > minwp[pcsy]/nminwp)) {
wp[0] = maxwp[0];
wp[1] = maxwp[1];
wp[2] = maxwp[2];
nw = nmaxwp;
if (maxwp[pcsy]/nmaxwp < (0.5 * pcsymax))
nw = 0; /* Looks like a mistake */
}
break;
}
default:
xicp->errc = 1;
sprintf(xicp->err,"set_icxLuLut: can't handle color space %s",
icm2str(icmColorSpaceSignature, h->colorSpace));
p->del((icxLuBase *)p);
return NULL;
break;
}
if (nw == 0) {
xicp->errc = 1;
sprintf(xicp->err,"set_icxLuLut: can't handle test points without a white patch");
p->del((icxLuBase *)p);
return NULL;
}
wp[0] /= (double)nw;
wp[1] /= (double)nw;
wp[2] /= (double)nw;
if (p->pcs != icSigXYZData) /* Convert white point to XYZ */
icmLab2XYZ(&icmD50, wp, wp);
}
if (flags & ICX_VERBOSE) {
printf("Approximate White point XYZ = %s, Lab = %s\n", icmPdv(3,wp),icmPLab(wp));
}
}
if (h->colorSpace == icSigGrayData) { /* Don't use device or PCS curves for monochrome */
p->noisluts = p->noipluts = p->nooluts = 1;
}
if ((flags & ICX_VERBOSE) && (p->noisluts == 0 || p->noipluts == 0 || p->nooluts == 0))
printf("Creating optimised per channel curves\n");
/* Set the target CLUT grid resolution so in/out curves can be optimised for it */
for (e = 0; e < p->inputChan; e++)
gres[e] = p->lut->clutPoints;
/* Setup and then create xfit object that does most of the work */
{
int xfflags = 0; /* xfit flags */
double in_min[MXDI]; /* Input value scaling minimum */
double in_max[MXDI]; /* Input value scaling maximum */
double out_min[MXDO]; /* Output value scaling minimum */
double out_max[MXDO]; /* Output value scaling maximum */
int iluord, sluord, oluord;
int iord[MXDI]; /* Input curve orders */
int sord[MXDI]; /* Input sub-grid curve orders */
int oord[MXDO]; /* Output curve orders */
double shp_smooth[MXDI];/* Smoothing factors for each curve, nom = 1.0 */
double out_smooth[MXDO];
optcomb tcomb = oc_ipo; /* Create all by default */
if ((xf = CAT2(new_, xfit)()) == NULL) {
p->pp->errc = 2;
sprintf(p->pp->err,"Creation of xfit object failed");
p->del((icxLuBase *)p);
return NULL;
}
/* Setup for optimising run */
if (p->noisluts)
tcomb &= ~oc_i;
if (p->noipluts)
tcomb &= ~oc_p;
if (p->nooluts)
tcomb &= ~oc_o;
if (flags & ICX_VERBOSE)
xfflags |= XFIT_VERB;
if (flags & ICX_SET_WHITE) {
xfflags |= XFIT_OUT_WP_REL;
if ((flags & ICX_SET_WHITE_C) == ICX_SET_WHITE_C) {
xfflags |= XFIT_OUT_WP_REL_C;
}
else if ((flags & ICX_SET_WHITE_US) == ICX_SET_WHITE_US)
xfflags |= XFIT_OUT_WP_REL_US;
if (p->pcs != icSigXYZData)
xfflags |= XFIT_OUT_LAB;
}
/* If asked, clip the absolute white point to have Y <= 1.0 ? */
if (flags & ICX_CLIP_WB)
xfflags |= XFIT_CLIP_WP;
/* With current B2A code, make sure a & b curves */
/* pass through zero. */
if (p->pcs == icSigLabData) {
xfflags |= XFIT_OUT_ZERO;
}
/* Let xfit create the clut */
xfflags |= XFIT_MAKE_CLUT;
/* Set the curve orders for input (device) and output (PCS) */
if (quality >= 3) { /* Ultra high */
iluord = 25;
sluord = 4;
oluord = 25;
} else if (quality == 2) { /* High */
iluord = 20;
sluord = 3;
oluord = 20;
} else if (quality == 1) { /* Medium */
iluord = 17;
sluord = 2;
oluord = 17;
} else { /* Low */
iluord = 10;
sluord = 1;
oluord = 10;
}
for (e = 0; e < p->inputChan; e++) {
iord[e] = iluord;
sord[e] = sluord;
in_min[e] = p->inmin[e];
in_max[e] = p->inmax[e];
shp_smooth[e] = SHP_SMOOTH;
}
for (f = 0; f < p->outputChan; f++) {
oord[f] = oluord;
out_min[f] = p->outmin[f];
out_max[f] = p->outmax[f];
/* Hack to prevent a convex L curve pushing */
/* the clut L values above the maximum value */
/* that can be represented, causing clipping. */
/* Do this by making sure that the L curve pivots */
/* through 100.0 to 100.0 */
if (f == 0 && p->pcs == icSigLabData) {
if (out_min[f] < 0.0001 && out_max[f] > 100.0) {
out_max[f] = 100.0;
}
}
out_smooth[f] = OUT_SMOOTH1;
if (f != 0 && p->pcs == icSigLabData) /* a* & b* */
out_smooth[f] = OUT_SMOOTH2;
}
//printf("~1 wp before xfit %f %f %f\n", wp[0], wp[1], wp[2]);
/* Create input, sub and output per channel curves (if configured), */
/* adjust for white point to make relative (if configured), */
/* and create clut rspl using xfit class. */
/* The true white point for the returned curves and rspl is returned. */
if (xf->fit(xf, xfflags, p->inputChan, p->outputChan,
rsplflags, wp, dwhite, wpscale, dgwhite,
ipoints, nodp, skm, in_min, in_max, gres, out_min, out_max,
smooth, oavgdev, demph, iord, sord, oord, shp_smooth, out_smooth, tcomb,
(void *)p, xfit_to_de2, xfit_to_dde2) != 0) {
p->pp->errc = 2;
sprintf(p->pp->err,"xfit fitting failed");
xf->del(xf);
p->del((icxLuBase *)p);
return NULL;
}
//printf("~1 wp after xfit %f %f %f\n", wp[0], wp[1], wp[2]);
/* - - - - - - - - - - - - - - - */
/* Set the xicc input curve rspl */
for (e = 0; e < p->inputChan; e++) {
curvectx cx;
cx.xf = xf;
cx.oix = -1;
cx.iix = e;
if ((p->inputTable[e] = new_rspl(RSPL_NOFLAGS, 1, 1)) == NULL) {
p->pp->errc = 2;
sprintf(p->pp->err,"Creation of input table rspl failed");
xf->del(xf);
p->del((icxLuBase *)p);
return NULL;
}
p->inputTable[e]->set_rspl(p->inputTable[e], RSPL_NOFLAGS,
(void *)&cx, set_linfunc,
&p->ninmin[e], &p->ninmax[e],
(int *)&p->lut->inputEnt,
&p->ninmin[e], &p->ninmax[e]);
}
/* - - - - - - - - - - - - - - - */
/* Set the xicc output curve rspl */
for (f = 0; f < p->outputChan; f++) {
curvectx cx;
cx.xf = xf;
cx.iix = -1;
cx.oix = f;
if ((p->outputTable[f] = new_rspl(RSPL_NOFLAGS, 1, 1)) == NULL) {
p->pp->errc = 2;
sprintf(p->pp->err,"Creation of output table rspl failed");
xf->del(xf);
p->del((icxLuBase *)p);
return NULL;
}
p->outputTable[f]->set_rspl(p->outputTable[f], RSPL_NOFLAGS,
(void *)&cx, set_linfunc,
&p->noutmin[f], &p->noutmax[f],
(int *)&p->lut->outputEnt,
&p->noutmin[f], &p->noutmax[f]);
}
}
if (flags & ICX_VERBOSE)
printf("Creating fast inverse input lookups\n");
/* Create rspl based reverse input lookups used in ink limit function. */
for (e = 0; e < p->inputChan; e++) {
int res = 256;
curvectx cx;
cx.xf = xf;
cx.oix = -1;
cx.iix = e;
if ((p->revinputTable[e] = new_rspl(RSPL_NOFLAGS, 1, 1)) == NULL) {
p->pp->errc = 2;
sprintf(p->pp->err,"Creation of reverse input table rspl failed");
xf->del(xf);
p->del((icxLuBase *)p);
return NULL;
}
p->revinputTable[e]->set_rspl(p->revinputTable[e], RSPL_NOFLAGS,
(void *)&cx, icxLuLut_invinput_func,
&p->ninmin[e], &p->ninmax[e], &res, &p->ninmin[e], &p->ninmax[e]);
}
/* ------------------------------- */
/* Set clut lookup table from xfit */
p->clutTable = xf->clut;
xf->clut = NULL;
/* Setup all the clipping, ink limiting and auxiliary stuff, */
/* in case a reverse call is used. Need to avoid relying on inking */
/* stuff that makes use of the white/black points, since they haven't */
/* been set up properly yet. */
if (setup_ink_icxLuLut(p, ink, 0) != 0) {
xf->del(xf);
p->del((icxLuBase *)p);
return NULL;
}
/* Deal with finalizing white/black points */
{
if ((flags & ICX_SET_WHITE) && (flags & ICX_VERBOSE)) {
printf("White point XYZ = %s, Lab = %s\n", icmPdv(3,wp),icmPLab(wp));
}
/* Lookup the black point */
{ /* Black Point Tag: */
co bcc;
if (flags & ICX_VERBOSE)
printf("Find black point\n");
/* !!! Hmm. For CMY and RGB we are simply using the device */
/* combination values as the black point. In reality we might */
/* want to have the option of using a neutral black point, */
/* just like CMYK ?? */
/* For CMYK devices, we choose a black that has minumum L within */
/* the ink limits, and if XICC_NEUTRAL_CMYK_BLACK it will in the direction */
/* that has the same chromaticity as the white point, else choose the same */
/* Lab vector direction as K, with the minimum L value. */
/* (Note this is duplicated in xicc.c icxLu_comp_bk_point() !!!) */
if (h->deviceClass != icSigInputClass
&& h->colorSpace == icSigCmykData) {
bfinds bfs; /* Callback context */
double sr[MXDO]; /* search radius */
double tt[MXDO]; /* Temporary */
double rs0[MXDO], rs1[MXDO]; /* Random start candidates */
int trial;
double brv;
int kch = 3;
/* Setup callback function context */
bfs.p = p;
bfs.toll = XICC_BLACK_POINT_TOLL;
/* !!! we should use an accessor funcion of xfit !!! */
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
bfs.toAbs[i][j] = xf->toAbs[i][j];
}
}
/* Lookup abs Lab value of white point */
icmXYZ2Lab(&icmD50, bfs.p1, wp);
#ifdef XICC_NEUTRAL_CMYK_BLACK
icmScale3(tt, wp, 0.02); /* Scale white XYZ towards 0,0,0 */
icmXYZ2Lab(&icmD50, bfs.p2, tt); /* Convert black direction to Lab */
if (flags & ICX_VERBOSE)
printf("Neutral black direction (Lab) = %f %f %f\n",bfs.p2[0], bfs.p2[1], bfs.p2[2]);
#else /* K direction black */
/* Now figure abs Lab value of K only, as the direction */
/* to use for the rich black. */
for (e = 0; e < p->inputChan; e++)
bcc.p[e] = 0.0;
if (p->ink.klimit < 0.0)
bcc.p[kch] = 1.0;
else
bcc.p[kch] = p->ink.klimit; /* K value */
p->input(p, bcc.p, bcc.p); /* Through input tables */
p->clutTable->interp(p->clutTable, &bcc); /* Through clut */
p->output(p, bcc.v, bcc.v); /* Through the output tables */
if (p->pcs != icSigXYZData) /* Convert PCS to XYZ */
icmLab2XYZ(&icmD50, bcc.v, bcc.v);
/* Convert from relative to Absolute colorimetric */
icmMulBy3x3(tt, xf->toAbs, bcc.v);
icmXYZ2Lab(&icmD50, bfs.p2, tt); /* Convert K only black point to Lab */
if (flags & ICX_VERBOSE)
printf("K only black direction (Lab) = %f %f %f\n",bfs.p2[0], bfs.p2[1], bfs.p2[2]);
#endif
/* Start with the K only as the current best value */
brv = bfindfunc((void *)&bfs, bcc.p);
//printf("~1 initial brv for K only = %f\n",brv);
/* Set the random start 0 location as 000K */
/* and the random start 1 location as CMY0 */
{
double tv;
for (e = 0; e < p->inputChan; e++)
rs0[e] = 0.0;
if (p->ink.klimit < 0.0)
rs0[kch] = 1.0;
else
rs0[kch] = p->ink.klimit; /* K value */
if (p->ink.tlimit < 0.0)
tv = 1.0;
else
tv = p->ink.tlimit/(p->inputChan - 1.0);
for (e = 0; e < p->inputChan; e++)
rs1[e] = tv;
rs1[kch] = 0.0; /* K value */
}
/* Find the device black point using optimization */
/* Do several trials to avoid local minima. */
rand32(0x12345678); /* Make trial values deterministic */
for (trial = 0; trial < 200; trial++) {
double rv; /* Temporary */
/* Start first trial at 000K */
if (trial == 0) {
for (j = 0; j < p->inputChan; j++) {
tt[j] = rs0[j];
sr[j] = 0.1;
}
} else {
/* Base is random between 000K and CMY0: */
if (trial < 100) {
rv = d_rand(0.0, 1.0);
for (j = 0; j < p->inputChan; j++) {
tt[j] = rv * rs0[j] + (1.0 - rv) * rs1[j];
sr[j] = 0.1;
}
/* Base on current best */
} else {
for (j = 0; j < p->inputChan; j++) {
tt[j] = bcc.p[j];
sr[j] = 0.1;
}
}
/* Then add random start offset */
for (rv = 0.0, j = 0; j < p->inputChan; j++) {
tt[j] += d_rand(-0.5, 0.5);
if (tt[j] < 0.0)
tt[j] = 0.0;
else if (tt[j] > 1.0)
tt[j] = 1.0;
}
}
/* Clip to be within ink limit */
icxDoLimit(p, tt, tt);
if (powell(&rv, p->inputChan, tt, sr, 0.000001, 1000,
bfindfunc, (void *)&bfs, NULL, NULL) == 0) {
//printf("~1 trial %d, rv %f bp %f %f %f %f\n",trial,rv,tt[0],tt[1],tt[2],tt[3]);
if (rv < brv) {
//printf("~1 new best\n");
brv = rv;
for (j = 0; j < p->inputChan; j++)
bcc.p[j] = tt[j];
}
}
}
if (brv > 1000.0)
error ("set_icxLuLut: Black point powell failed");
/* Make sure resulting device values are strictly in range */
for (j = 0; j < p->inputChan; j++) {
if (bcc.p[j] < 0.0)
bcc.p[j] = 0.0;
else if (bcc.p[j] > 1.0)
bcc.p[j] = 1.0;
}
/* Now have device black in bcc.p[] */
//printf("~1 Black point is CMYK %f %f %f %f\n", bcc.p[0], bcc.p[1], bcc.p[2], bcc.p[3]);
/* Else not a CMYK output device, */
/* use the previously determined device black value. */
} else {
for (e = 0; e < p->inputChan; e++)
bcc.p[e] = dblack[e];
}
/* Lookup the PCS for the device black: */
p->input(p, bcc.p, bcc.p); /* Through input tables */
p->clutTable->interp(p->clutTable, &bcc); /* Through clut */
p->output(p, bcc.v, bcc.v); /* Through the output tables */
if (p->pcs != icSigXYZData) /* Convert PCS to XYZ */
icmLab2XYZ(&icmD50, bcc.v, bcc.v);
/* Convert from relative to Absolute colorimetric */
icmMulBy3x3(bp, xf->toAbs, bcc.v);
/* Got XYZ black point in bp[] */
if (flags & ICX_VERBOSE) {
printf("Black point XYZ = %s, Lab = %s\n", icmPdv(3,bp),icmPLab(bp));
}
/* Some ICC sw gets upset if the bp is at all -ve. */
/* Clip it if this is the case */
/* (Or we could get xfit to rescale the rspl instead ??) */
if ((flags & ICX_CLIP_WB)
&& (bp[0] < 0.0 || bp[1] < 0.0 || bp[2] < 0.0)) {
if (bp[0] < 0.0)
bp[0] = 0.0;
if (bp[1] < 0.0)
bp[1] = 0.0;
if (bp[2] < 0.0)
bp[2] = 0.0;
if (flags & ICX_VERBOSE) {
printf("Black point clipped to XYZ = %s, Lab = %s\n",icmPdv(3,bp),icmPLab(bp));
}
}
}
/* If this is a display, adjust the white point to be */
/* exactly Y = 1.0, and compensate the dispLuminance */
/* and black point accordingly. The Lut is already set to */
/* assume device white maps to perfect PCS white. */
if (h->deviceClass == icSigDisplayClass) {
double scale = 1.0/wp[1];
int i;
dispLuminance /= scale;
for (i = 0; i < 3; i++) {
wp[i] *= scale;
bp[i] *= scale;
}
}
if (h->deviceClass == icSigDisplayClass
&& dispLuminance > 0.0) {
icmXYZArray *wo;
if ((wo = (icmXYZArray *)icco->read_tag(
icco, icSigLuminanceTag)) == NULL) {
xicp->errc = 1;
sprintf(xicp->err,"icx_set_luminance: couldn't find luminance tag");
p->del((icxLuBase *)p);
return NULL;
}
if (wo->ttype != icSigXYZArrayType) {
xicp->errc = 1;
sprintf(xicp->err,"luminance: tag has wrong type");
p->del((icxLuBase *)p);
return NULL;
}
wo->size = 1;
wo->allocate((icmBase *)wo); /* Allocate space */
wo->data[0].X = 0.0;
wo->data[0].Y = dispLuminance * wp[1];
wo->data[0].Z = 0.0;
if (flags & ICX_VERBOSE)
printf("Display Luminance = %f\n", wo->data[0].Y);
}
/* Write white and black points */
if (flags & ICX_SET_WHITE) { /* White Point Tag: */
icmXYZArray *wo;
if ((wo = (icmXYZArray *)icco->read_tag(
icco, icSigMediaWhitePointTag)) == NULL) {
xicp->errc = 1;
sprintf(xicp->err,"icx_set_white_black: couldn't find white tag");
xf->del(xf);
p->del((icxLuBase *)p);
return NULL;
}
if (wo->ttype != icSigXYZArrayType) {
xicp->errc = 1;
sprintf(xicp->err,"icx_set_white_black: white tag has wrong type");
xf->del(xf);
p->del((icxLuBase *)p);
return NULL;
}
wo->size = 1;
wo->allocate((icmBase *)wo); /* Allocate space */
wo->data[0].X = wp[0];
wo->data[0].Y = wp[1];
wo->data[0].Z = wp[2];
}
if (flags & ICX_SET_BLACK) { /* Black Point Tag: */
icmXYZArray *wo;
if ((wo = (icmXYZArray *)icco->read_tag(
icco, icSigMediaBlackPointTag)) == NULL) {
xicp->errc = 1;
sprintf(xicp->err,"icx_set_white_black: couldn't find black tag");
xf->del(xf);
p->del((icxLuBase *)p);
return NULL;
}
if (wo->ttype != icSigXYZArrayType) {
xicp->errc = 1;
sprintf(xicp->err,"icx_set_white_black: black tag has wrong type");
xf->del(xf);
p->del((icxLuBase *)p);
return NULL;
}
wo->size = 1;
wo->allocate((icmBase *)wo); /* Allocate space */
wo->data[0].X = bp[0];
wo->data[0].Y = bp[1];
wo->data[0].Z = bp[2];
}
if ((flags & ICX_SET_WHITE) || (flags & ICX_SET_BLACK)) {
/* Make sure ICC white/black point lookup notices the new white and black points */
p->plu->init_wh_bk(p->plu);
}
/* Setup the clut clipping, ink limiting and auxiliary stuff again */
/* since re_set_rspl will have invalidated */
if (setup_ink_icxLuLut(p, ink, 1) != 0) {
xf->del(xf);
p->del((icxLuBase *)p);
return NULL;
}
}
/* Done with xfit now */
xf->del(xf);
xf = NULL;
if (setup_clip_icxLuLut(p) != 0) {
p->del((icxLuBase *)p);
return NULL;
}
/* ------------------------------- */
//Debugging clipping of clut
//printf("~1 xlut.c: noutmin = %f %f %f\n", p->noutmin[0], p->noutmin[1], p->noutmin[2]);
//printf("~1 xlut.c: noutmax = %f %f %f\n", p->noutmax[0], p->noutmax[1], p->noutmax[2]);
//printf("~1 xlut.c: outmin = %f %f %f\n", p->outmin[0], p->outmin[1], p->outmin[2]);
//printf("~1 xlut.c: outmax = %f %f %f\n", p->outmax[0], p->outmax[1], p->outmax[2]);
/* Do a specific test for out of Lab encoding range RGB primaries */
/* (A more general check seems to get false positives - why ??) */
if (h->pcs == icSigLabData
&& ( h->deviceClass == icSigDisplayClass
|| h->deviceClass == icSigOutputClass)
&& h->colorSpace == icSigRgbData) {
double dev[3] = { 0.0, 0.0, 0.0 };
double pcs[3];
double clip = 0;
for (i = 0; i < 3; i++) {
dev[i] = 1.0;
if (p->clut(p, pcs, dev) > 1)
error ("%d, %s",p->pp->errc,p->pp->err);
/* Convert from efective pcs to natural pcs */
if (p->pcs != icSigLabData)
icmXYZ2Lab(&icmD50, pcs, pcs);
if (pcs[1] < -128.0 || pcs[1] > 128.0
|| pcs[2] < -128.0 || pcs[2] > 128.0) {
warning("\n *** %s primary value can't be encoded in L*a*b* PCS (%f %f %f)",
i == 0 ? "Red" : i == 1 ? "Green" : "Blue", pcs[0],pcs[1],pcs[2]);
clip = 1;
}
dev[i] = 0.0;
}
if (clip)
a1logw(g_log," *** Try switching to XYZ PCS ***\n");
}
/* Use our rspl's to set the icc Lut AtoB table values. */
/* Use helper function to do the hard work. */
if (p->lut->set_tables(p->lut, ICM_CLUT_SET_EXACT, (void *)p,
h->colorSpace, /* Input color space */
h->pcs, /* Output color space */
set_input, /* Input transfer function, Dev->Dev' */
NULL, NULL, /* Use default Maximum range of Dev' values */
set_clut, /* Dev' -> PCS' transfer function */
NULL, NULL, /* Use default Maximum range of PCS' values */
set_output, /* Linear output transform PCS'->PCS */
NULL, NULL /* No APXLS */
) != 0)
error("Setting 16 bit %s->%s Lut failed: %d, %s",
icm2str(icmColorSpaceSignature, h->colorSpace),
icm2str(icmColorSpaceSignature, h->pcs),
p->pp->pp->errc,p->pp->pp->err);
#ifdef WARN_CLUT_CLIPPING
if (p->pp->pp->warnc) {
warning("Values clipped in setting A2B LUT!");
if (p->pp->pp->warnc == 2 && h->pcs == icSigLabData) {
a1logw(g_log,"*** Try switching to XYZ PCS ***\n");
}
}
#endif /* WARN_CLUT_CLIPPING */
/* Init a CAM model in case it will be used (ie. in profile with camclip flag) */
if (vc != NULL) /* One has been provided */
p->vc = *vc; /* Copy the structure */
else
xicc_enum_viewcond(xicp, &p->vc, -1, NULL, 0, NULL); /* Use a default */
p->cam = new_icxcam(cam_default);
p->cam->set_view(p->cam, p->vc.Ev, p->vc.Wxyz, p->vc.La, p->vc.Yb, p->vc.Lv,
p->vc.Yf, p->vc.Yg, p->vc.Gxyz, XICC_USE_HK);
if (flags & ICX_VERBOSE)
printf("Done A to B table creation\n");
return (icxLuBase *)p;
}
/* ========================================================== */
/* Gamut boundary code. */
/* ========================================================== */
/* Context for creating gamut boundary points fro, xicc */
typedef struct {
gamut *g; /* Gamut being created */
icxLuLut *x; /* xLut we are working from */
icxLuBase *flu; /* Forward xlookup */
double in[MAX_CHAN]; /* Device input values */
} lutgamctx;
/* Function to hand to zbrent to find a clut input' value at the ink limit */
/* Returns value < 0.0 when within gamut, > 0.0 when out of gamut */
static double icxLimitFind(void *fdata, double tp) {
int i;
double in[MAX_CHAN];
lutgamctx *p = (lutgamctx *)fdata;
double tt;
for (i = 0; i < p->x->inputChan; i++) {
in[i] = tp * p->in[i]; /* Scale given input value */
}
tt = icxLimitD(p->x, in);
return tt;
}
/* Function to pass to rspl to create gamut boundary from */
/* forward xLut transform grid points. */
static void
lutfwdgam_func(
void *pp, /* lutgamctx structure */
double *out, /* output' value at clut grid point (ie. PCS' value) */
double *in /* input' value at clut grid point (ie. device' value) */
) {
lutgamctx *p = (lutgamctx *)pp;
double pcso[3]; /* PCS output value */
/* Figure if we are over the ink limit. */
if ( (p->x->ink.tlimit >= 0.0 || p->x->ink.klimit >= 0.0)
&& icxLimitD(p->x, in) > 0.0) {
int i;
double sf;
/* We are, so use the bracket search to discover a scale */
/* for the clut input' value that will put us on the ink limit. */
for (i = 0; i < p->x->inputChan; i++)
p->in[i] = in[i];
if (zbrent(&sf, 0.0, 1.0, 1e-4, icxLimitFind, pp) != 0) {
return; /* Give up */
}
/* Compute ink limit value */
for (i = 0; i < p->x->inputChan; i++)
p->in[i] = sf * in[i];
/* Compute the clut output for this clut input */
p->x->clut(p->x, pcso, p->in);
p->x->output(p->x, pcso, pcso);
p->x->out_abs(p->x, pcso, pcso);
} else { /* No ink limiting */
/* Convert the clut PCS' values to PCS output values */
p->x->output(p->x, pcso, out);
p->x->out_abs(p->x, pcso, pcso);
}
/* Expand the gamut surface with this point */
p->g->expand(p->g, pcso);
/* Leave out[] unchanged */
}
/* Function to pass to rspl to create gamut boundary from */
/* backwards Lut transform. This is called for every node in the */
/* B2A grid. */
static void
lutbwdgam_func(
void *pp, /* lutgamctx structure */
double *out, /* output value */
double *in /* input value */
) {
lutgamctx *p = (lutgamctx *)pp;
double devo[MAX_CHAN]; /* Device output value */
double pcso[3]; /* PCS output value */
/* Convert the clut values to device output values */
p->x->output(p->x, devo, out); /* (Device never uses out_abs()) */
/* Convert from device values to PCS values */
p->flu->lookup(p->flu, pcso, devo);
/* Expand the gamut surface with this point */
p->g->expand(p->g, pcso);
/* Leave out[] unchanged */
}
/* Given an xicc lookup object, return a gamut object. */
/* Note that the PCS must be Lab or Jab */
/* An icxLuLut type must be icmFwd or icmBwd, */
/* and for icmFwd, the ink limit (if supplied) will be applied. */
/* Return NULL on error, check errc+err for reason */
static gamut *icxLuLutGamut(
icxLuBase *plu, /* this */
double detail /* gamut detail level, 0.0 = def */
) {
xicc *p = plu->pp; /* parent xicc */
icxLuLut *luluto = (icxLuLut *)plu; /* Lookup xLut type object */
icColorSpaceSignature ins, pcs, outs;
icmLookupFunc func;
icRenderingIntent intent;
double white[3], black[3], kblack[3];
int inn, outn;
gamut *gam;
/* get some details */
plu->spaces(plu, &ins, &inn, &outs, &outn, NULL, &intent, &func, &pcs);
if (func != icmFwd && func != icmBwd) {
p->errc = 1;
sprintf(p->err,"Creating Gamut surface for anything other than Device <-> PCS is not supported.");
return NULL;
}
if (pcs != icSigLabData && pcs != icxSigJabData) {
p->errc = 1;
sprintf(p->err,"Creating Gamut surface PCS of other than Lab or Jab is not supported.");
return NULL;
}
if (func == icmFwd) {
lutgamctx cx;
cx.g = gam = new_gamut(detail, pcs == icxSigJabData, 0);
cx.x = luluto;
/* Scan through grid. */
/* (Note this can give problems for a strange input space - ie. Lab */
/* and a low grid resolution - ie. 2) */
luluto->clutTable->scan_rspl(
luluto->clutTable, /* this */
RSPL_NOFLAGS, /* Combination of flags */
(void *)&cx, /* Opaque function context */
lutfwdgam_func /* Function to set from */
);
/* Make sure the white and point goes in too, if it isn't in the grid */
plu->efv_wh_bk_points(plu, white, NULL, NULL);
gam->expand(gam, white);
if (detail == 0.0)
detail = 10.0;
/* If the gamut is more than cursary, add some more detail surface points */
if (detail < 20.0 || luluto->clutTable->g.mres < 4) {
int res;
DCOUNT(co, MAX_CHAN, inn, 0, 0, 2);
res = (int)(500.0/detail); /* Establish an appropriate sampling density */
if (res < 10)
res = 10;
/* Itterate over all the faces in the device space */
DC_INIT(co);
while(!DC_DONE(co)) { /* Count through the corners of hyper cube */
int e, m1, m2;
double in[MAX_CHAN];
double out[3];
for (e = 0; e < inn; e++) { /* Base value */
in[e] = (double)co[e]; /* Base value */
in[e] = in[e] * (luluto->inmax[e] - luluto->inmin[e])
+ luluto->inmin[e];
}
/* Figure if we are over the ink limit. */
if ((luluto->ink.tlimit >= 0.0 || luluto->ink.klimit >= 0.0)
&& icxLimit(luluto, in) > 0.0) {
DC_INC(co);
continue; /* Skip points over limit */
}
/* Scan only device surface */
for (m1 = 0; m1 < inn; m1++) { /* Choose first coord to scan */
if (co[m1] != 0)
continue; /* Not at lower corner */
for (m2 = m1 + 1; m2 < inn; m2++) { /* Choose second coord to scan */
int x, y;
if (co[m2] != 0)
continue; /* Not at lower corner */
for (x = 0; x < res; x++) { /* step over surface */
in[m1] = x/(res - 1.0);
in[m1] = in[m1] * (luluto->inmax[m1] - luluto->inmin[m1])
+ luluto->inmin[m1];
for (y = 0; y < res; y++) {
in[m2] = y/(res - 1.0);
in[m2] = in[m2] * (luluto->inmax[m2] - luluto->inmin[m2])
+ luluto->inmin[m2];
/* Figure if we are over the ink limit. */
if ( (luluto->ink.tlimit >= 0.0 || luluto->ink.klimit >= 0.0)
&& icxLimit(luluto, in) > 0.0) {
continue; /* Skip points over limit */
}
luluto->lookup((icxLuBase *)luluto, out, in);
gam->expand(gam, out);
}
}
}
}
/* Increment index within block */
DC_INC(co);
}
}
/* Now set the cusp points by itterating through colorant 0 & 100% combinations */
/* If we know what sort of space it is: */
if (ins == icSigRgbData || ins == icSigCmyData || ins == icSigCmykData) {
DCOUNT(co, 3, 3, 0, 0, 2);
gam->setcusps(gam, 0, NULL);
DC_INIT(co);
while(!DC_DONE(co)) {
int e;
double in[MAX_CHAN];
double out[3];
if (!(co[0] == 0 && co[1] == 0 && co[2] == 0)
&& !(co[0] == 1 && co[1] == 1 && co[2] == 1)) { /* Skip white and black */
for (e = 0; e < 3; e++)
in[e] = (double)co[e];
in[e] = 0; /* K */
/* Always use the device->PCS conversion */
if (luluto->lookup((icxLuBase *)luluto, out, in) > 1)
error ("%d, %s",p->errc,p->err);
gam->setcusps(gam, 3, out);
}
DC_INC(co);
}
gam->setcusps(gam, 2, NULL);
/* Do all ink combinations and hope we can sort it out */
/* (This may not be smart, since bodgy cusp values will cause gamut mapping to fail...) */
} else if (ins != icSigXYZData
&& ins != icSigLabData
&& ins != icSigLuvData
&& ins != icSigYxyData) {
DCOUNT(co, MAX_CHAN, inn, 0, 0, 2);
gam->setcusps(gam, 0, NULL);
DC_INIT(co);
while(!DC_DONE(co)) {
int e;
double in[MAX_CHAN];
double out[3];
for (e = 0; e < inn; e++) {
in[e] = (double)co[e];
in[e] = in[e] * (luluto->inmax[e] - luluto->inmin[e])
+ luluto->inmin[e];
}
/* Figure if we are over the ink limit. */
if ((luluto->ink.tlimit >= 0.0 || luluto->ink.klimit >= 0.0)
&& icxLimit(luluto, in) > 0.0) {
DC_INC(co);
continue; /* Skip points over limit */
}
luluto->lookup((icxLuBase *)luluto, out, in);
gam->setcusps(gam, 1, out);
DC_INC(co);
}
gam->setcusps(gam, 2, NULL);
}
} else { /* Must be icmBwd */
lutgamctx cx;
/* Get an appropriate device to PCS conversion for the fwd conversion */
/* we use after bwd conversion in lutbwdgam_func() */
switch (intent) {
/* If it is relative */
case icmDefaultIntent: /* Shouldn't happen */
case icPerceptual:
case icRelativeColorimetric:
case icSaturation:
intent = icRelativeColorimetric; /* Choose relative */
break;
/* If it is absolute */
case icAbsoluteColorimetric:
case icxAppearance:
case icxAbsAppearance:
break; /* Leave unchanged */
default:
break;
}
if ((cx.flu = p->get_luobj(p, ICX_CLIP_NEAREST , icmFwd, intent, pcs, icmLuOrdNorm,
&plu->vc, NULL)) == NULL) {
return NULL; /* oops */
}
cx.g = gam = new_gamut(detail, pcs == icxSigJabData, 0);
cx.x = luluto;
luluto->clutTable->scan_rspl(
luluto->clutTable, /* this */
RSPL_NOFLAGS, /* Combination of flags */
(void *)&cx, /* Opaque function context */
lutbwdgam_func /* Function to set from */
);
/* Now set the cusp points by using the fwd conversion and */
/* itterating through colorant 0 & 100% combinations. */
/* If we know what sort of space it is: */
if (outs == icSigRgbData || outs == icSigCmyData || outs == icSigCmykData) {
DCOUNT(co, 3, 3, 0, 0, 2);
gam->setcusps(gam, 0, NULL);
DC_INIT(co);
while(!DC_DONE(co)) {
int e;
double in[MAX_CHAN];
double out[3];
if (!(co[0] == 0 && co[1] == 0 && co[2] == 0)
&& !(co[0] == 1 && co[1] == 1 && co[2] == 1)) { /* Skip white and black */
for (e = 0; e < 3; e++)
in[e] = (double)co[e];
in[e] = 0; /* K */
/* Always use the device->PCS conversion */
if (cx.flu->lookup((icxLuBase *)cx.flu, out, in) > 1)
error ("%d, %s",p->errc,p->err);
gam->setcusps(gam, 3, out);
}
DC_INC(co);
}
gam->setcusps(gam, 2, NULL);
/* Do all ink combinations and hope we can sort it out */
/* (This may not be smart, since bodgy cusp values will cause gamut mapping to fail...) */
} else if (ins != icSigXYZData
&& ins != icSigLabData
&& ins != icSigLuvData
&& ins != icSigYxyData) {
DCOUNT(co, MAX_CHAN, inn, 0, 0, 2);
gam->setcusps(gam, 0, NULL);
DC_INIT(co);
while(!DC_DONE(co)) {
int e;
double in[MAX_CHAN];
double out[3];
for (e = 0; e < inn; e++) {
in[e] = (double)co[e];
in[e] = in[e] * (luluto->inmax[e] - luluto->inmin[e])
+ luluto->inmin[e];
}
/* Figure if we are over the ink limit. */
if ((luluto->ink.tlimit >= 0.0 || luluto->ink.klimit >= 0.0)
&& icxLimit(luluto, in) > 0.0) {
DC_INC(co);
continue; /* Skip points over limit */
}
cx.flu->lookup((icxLuBase *)cx.flu, out, in);
gam->setcusps(gam, 1, out);
DC_INC(co);
}
gam->setcusps(gam, 2, NULL);
}
cx.flu->del(cx.flu); /* Done with the fwd conversion */
}
/* Set the white and black points */
plu->efv_wh_bk_points(plu, white, black, kblack);
gam->setwb(gam, white, black, kblack);
//printf("~1 icxLuLutGamut: set black %f %f %f and kblack %f %f %f\n", black[0], black[1], black[2], kblack[0], kblack[1], kblack[2]);
#ifdef NEVER /* Not sure if this is a good idea ?? */
/* Since we know this is a profile, force the space and gamut points to be the same */
gam->getwb(gam, NULL, NULL, white, black, kblack); /* Get the actual gamut white and black points */
gam->setwb(gam, white, black, kblack); /* Put it back as colorspace one */
#endif
return gam;
}
/* ----------------------------------------------- */
#ifdef DEBUG
#undef DEBUG /* Limit extent to this file */
#endif
|