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
|
#include "mupdf/fitz.h"
#include "colorspace-imp.h"
#include "fitz-imp.h"
#include <assert.h>
#include <math.h>
#include <string.h>
/* CMM module */
int
fz_cmm_avoid_white_fix_flag(fz_context *ctx)
{
if (ctx && ctx->colorspace && ctx->colorspace->cmm && ctx->cmm_instance)
return ctx->colorspace->cmm->avoid_white_fix_flag;
return 0;
}
void
fz_cmm_transform_pixmap(fz_context *ctx, fz_icclink *link, fz_pixmap *dst, fz_pixmap *src)
{
if (ctx && ctx->colorspace && ctx->colorspace->cmm && ctx->cmm_instance)
ctx->colorspace->cmm->transform_pixmap(ctx->cmm_instance, link, dst, src);
}
void
fz_cmm_transform_color(fz_context *ctx, fz_icclink *link, unsigned short *dst, const unsigned short *src)
{
if (ctx && ctx->colorspace && ctx->colorspace->cmm && ctx->cmm_instance)
ctx->colorspace->cmm->transform_color(ctx->cmm_instance, link, dst, src);
}
void
fz_cmm_init_link(fz_context *ctx, fz_icclink *link, const fz_color_params *rend, int cmm_flags, int num_bytes, int alpha, const fz_iccprofile *src, const fz_iccprofile *prf, const fz_iccprofile *des)
{
if (ctx && ctx->colorspace && ctx->colorspace->cmm && ctx->cmm_instance)
ctx->colorspace->cmm->init_link(ctx->cmm_instance, link, rend, cmm_flags, num_bytes, alpha, src, prf, des);
}
void
fz_cmm_fin_link(fz_context *ctx, fz_icclink *link)
{
if (ctx && ctx->colorspace && ctx->colorspace->cmm && ctx->cmm_instance)
ctx->colorspace->cmm->fin_link(ctx->cmm_instance, link);
}
fz_cmm_instance *fz_cmm_new_instance(fz_context *ctx)
{
if (ctx && ctx->colorspace && ctx->colorspace->cmm)
return ctx->colorspace->cmm->new_instance(ctx);
return NULL;
}
void fz_cmm_drop_instance(fz_context *ctx)
{
if (ctx && ctx->colorspace && ctx->colorspace->cmm && ctx->cmm_instance)
ctx->colorspace->cmm->drop_instance(ctx->cmm_instance);
}
void fz_cmm_init_profile(fz_context *ctx, fz_iccprofile *profile)
{
if (ctx && ctx->colorspace && ctx->colorspace->cmm && ctx->cmm_instance)
ctx->colorspace->cmm->init_profile(ctx->cmm_instance, profile);
}
void fz_cmm_fin_profile(fz_context *ctx, fz_iccprofile *profile)
{
if (ctx && ctx->colorspace && ctx->colorspace->cmm && ctx->cmm_instance)
if (profile && profile->cmm_handle != NULL)
ctx->colorspace->cmm->fin_profile(ctx->cmm_instance, profile);
}
#define SLOWCMYK
const unsigned char *
fz_lookup_icc(fz_context *ctx, const char *name, size_t *size)
{
#ifndef NO_ICC
if (fz_get_cmm_engine(ctx) == NULL)
return *size = 0, NULL;
if (!strcmp(name, "gray-icc")) {
extern const int fz_resources_icc_gray_icc_size;
extern const unsigned char fz_resources_icc_gray_icc[];
*size = fz_resources_icc_gray_icc_size;
return fz_resources_icc_gray_icc;
}
if (!strcmp(name, "rgb-icc") || !strcmp(name, "bgr-icc")) {
extern const int fz_resources_icc_rgb_icc_size;
extern const unsigned char fz_resources_icc_rgb_icc[];
*size = fz_resources_icc_rgb_icc_size;
return fz_resources_icc_rgb_icc;
}
if (!strcmp(name, "cmyk-icc")) {
extern const int fz_resources_icc_cmyk_icc_size;
extern const unsigned char fz_resources_icc_cmyk_icc[];
*size = fz_resources_icc_cmyk_icc_size;
return fz_resources_icc_cmyk_icc;
}
if (!strcmp(name, "lab-icc")) {
extern const int fz_resources_icc_lab_icc_size;
extern const unsigned char fz_resources_icc_lab_icc[];
*size = fz_resources_icc_lab_icc_size;
return fz_resources_icc_lab_icc;
}
#endif
return *size = 0, NULL;
}
/* Same order as needed by lcms */
static const char *fz_intent_names[] =
{
"Perceptual",
"RelativeColorimetric",
"Saturation",
"AbsoluteColorimetric",
};
int
fz_lookup_rendering_intent(const char *name)
{
int i;
for (i = 0; i < nelem(fz_intent_names); i++)
if (!strcmp(name, fz_intent_names[i]))
return i;
return FZ_RI_RELATIVE_COLORIMETRIC;
}
char *
fz_rendering_intent_name(int ri)
{
if (ri >= 0 && ri < nelem(fz_intent_names))
return (char*)fz_intent_names[ri];
return "RelativeColorimetric";
}
void
fz_drop_colorspace_imp(fz_context *ctx, fz_storable *cs_)
{
fz_colorspace *cs = (fz_colorspace *)cs_;
if (cs->free_data && cs->data)
cs->free_data(ctx, cs);
fz_free(ctx, cs);
}
static void
clamp_default(const fz_colorspace *cs, const float *src, float *dst)
{
int i;
for (i = 0; i < cs->n; i++)
dst[i] = fz_clamp(src[i], 0, 1);
}
fz_colorspace *
fz_new_colorspace(fz_context *ctx, char *name, int is_static, int n, int is_subtractive, fz_colorspace_convert_fn *to_ccs, fz_colorspace_convert_fn *from_ccs, fz_colorspace_base_fn *base, fz_colorspace_clamp_fn *clamp, fz_colorspace_destruct_fn *destruct, void *data, size_t size)
{
fz_colorspace *cs = fz_malloc_struct(ctx, fz_colorspace);
FZ_INIT_STORABLE(cs, is_static ? -1 : 1, fz_drop_colorspace_imp);
cs->size = sizeof(fz_colorspace) + size;
fz_strlcpy(cs->name, name, sizeof cs->name);
cs->n = n;
cs->is_subtractive = is_subtractive;
cs->to_ccs = to_ccs;
cs->from_ccs = from_ccs;
cs->get_base = base;
if (clamp != NULL)
cs->clamp = clamp;
else
cs->clamp = clamp_default;
cs->free_data = destruct;
cs->data = data;
return cs;
}
fz_colorspace *
fz_keep_colorspace(fz_context *ctx, fz_colorspace *cs)
{
return fz_keep_storable(ctx, &cs->storable);
}
void
fz_drop_colorspace(fz_context *ctx, fz_colorspace *cs)
{
fz_drop_storable(ctx, &cs->storable);
}
/* icc links */
typedef struct fz_link_key_s fz_link_key;
struct fz_link_key_s {
int refs;
unsigned char src_md5[16];
unsigned char dst_md5[16];
fz_color_params rend;
int alpha;
int depth;
int proof;
};
static void *
fz_keep_link_key(fz_context *ctx, void *key_)
{
fz_link_key *key = (fz_link_key *)key_;
return fz_keep_imp(ctx, key, &key->refs);
}
static void
fz_drop_link_key(fz_context *ctx, void *key_)
{
fz_link_key *key = (fz_link_key *)key_;
if (fz_drop_imp(ctx, key, &key->refs))
fz_free(ctx, key);
}
static int
fz_cmp_link_key(fz_context *ctx, void *k0_, void *k1_)
{
fz_link_key *k0 = (fz_link_key *)k0_;
fz_link_key *k1 = (fz_link_key *)k1_;
return k0->proof == k1->proof &&
k0->alpha == k1->alpha &&
k0->depth == k1->depth &&
k0->rend.bp == k1->rend.bp &&
k0->rend.ri == k1->rend.ri &&
memcmp(k0->dst_md5, k1->dst_md5, 16) == 0 &&
memcmp(k0->src_md5, k1->src_md5, 16);
}
static void
fz_format_link_key(fz_context *ctx, char *s, int n, void *key_)
{
static const char *hex = "0123456789abcdef";
fz_link_key *key = (fz_link_key *)key_;
char sm[33], dm[33];
int i;
for (i = 0; i < 16; ++i)
{
sm[i*2+0] = hex[key->src_md5[i]>>4];
sm[i*2+1] = hex[key->src_md5[i]&15];
dm[i*2+0] = hex[key->dst_md5[i]>>4];
dm[i*2+1] = hex[key->dst_md5[i]&15];
}
sm[32] = 0;
dm[32] = 0;
fz_snprintf(s, n, "(link src_md5=%s dst_md5=%s)", sm, dm);
}
static int
fz_make_hash_link_key(fz_context *ctx, fz_store_hash *hash, void *key_)
{
fz_link_key *key = (fz_link_key *)key_;
memcpy(hash->u.link.dst_md5, key->dst_md5, 16);
memcpy(hash->u.link.src_md5, key->src_md5, 16);
hash->u.link.ri = key->rend.ri;
hash->u.link.bp = key->rend.bp;
hash->u.link.alpha = key->alpha;
hash->u.link.depth = key->depth;
hash->u.link.proof = key->proof;
return 1;
}
static fz_store_type fz_link_store_type =
{
fz_make_hash_link_key,
fz_keep_link_key,
fz_drop_link_key,
fz_cmp_link_key,
fz_format_link_key,
NULL
};
static void
fz_drop_link_imp(fz_context *ctx, fz_storable *storable)
{
fz_icclink *link = (fz_icclink *)storable;
fz_cmm_fin_link(ctx, link);
fz_free(ctx, link);
}
static void
fz_drop_icclink(fz_context *ctx, fz_icclink *link)
{
fz_drop_storable(ctx, &link->storable);
}
static fz_iccprofile *
get_base_icc_profile(fz_context *ctx, fz_colorspace *cs)
{
fz_colorspace *base;
fz_cal_colorspace *cal;
fz_iccprofile *cal_icc;
if (!cs || !cs->get_base)
return NULL;
base = cs->get_base(cs);
if (base == NULL)
return NULL;
if (fz_colorspace_is_icc(ctx, base))
return base->data;
if (!fz_colorspace_is_cal(ctx, base))
return get_base_icc_profile(ctx, base);
cal = base->data;
cal_icc = cal->profile;
if (cal_icc && cal_icc->cmm_handle == NULL)
fz_cmm_init_profile(ctx, cal_icc);
return cal_icc;
}
static fz_icclink *
fz_new_icc_link(fz_context *ctx, fz_iccprofile *src, fz_iccprofile *prf, fz_iccprofile *dst, const fz_color_params *rend, int num_bytes, int alpha)
{
fz_icclink *link = fz_malloc_struct(ctx, fz_icclink);
FZ_INIT_STORABLE(link, 1, fz_drop_link_imp);
link->num_in = src->num_devcomp;
link->num_out = dst->num_devcomp;
if (memcmp(src->md5, dst->md5, 16) == 0 && rend->ri == FZ_RI_RELATIVE_COLORIMETRIC && prf == NULL)
{
link->is_identity = 1;
return link;
}
fz_try(ctx)
fz_cmm_init_link(ctx, link, rend, 0, num_bytes, alpha, src, prf, dst);
fz_catch(ctx)
{
fz_free(ctx, link);
fz_rethrow(ctx);
}
return link;
}
static void
fz_md5_icc(fz_context *ctx, fz_iccprofile *profile)
{
if (profile)
fz_md5_buffer(ctx, profile->buffer, profile->md5);
}
/* Create icc profile from calrgb, calgray values */
static fz_iccprofile *
fz_icc_from_cal(fz_context *ctx, fz_colorspace *cs)
{
fz_cal_colorspace *cal_data = cs->data;
fz_iccprofile *profile;
if (cal_data->profile != NULL)
return cal_data->profile;
profile = fz_malloc_struct(ctx, fz_iccprofile);
fz_try(ctx)
{
profile->buffer = fz_new_icc_data_from_cal_colorspace(ctx, cal_data);
fz_md5_icc(ctx, profile);
cal_data->profile = profile;
}
fz_catch(ctx)
{
fz_free(ctx, profile);
fz_rethrow(ctx);
}
return profile;
}
static fz_icclink *
fz_get_icc_link(fz_context *ctx, fz_colorspace *src, fz_colorspace *prf, fz_colorspace *dst, const fz_color_params *rend, int num_bytes, int alpha, int *src_n)
{
fz_icclink *link = NULL;
fz_iccprofile *src_icc = NULL;
fz_iccprofile *dst_icc = dst->data;
fz_iccprofile *prf_icc = NULL;
fz_link_key *key;
fz_icclink *new_link;
if (prf != NULL)
prf_icc = prf->data;
if (fz_colorspace_is_icc(ctx, src))
src_icc = src->data;
else if (fz_colorspace_is_cal(ctx, src))
{
fz_cal_colorspace *cal;
cal = src->data;
src_icc = cal->profile;
/* Check if we have any work to do. */
if (src_icc == NULL)
src_icc = fz_icc_from_cal(ctx, src);
if (src_icc->cmm_handle == NULL)
{
fz_cmm_init_profile(ctx, src_icc);
/* The CMM failed to make a profile. Use the default. */
if (src_icc->cmm_handle == NULL)
{
switch (src->n)
{
case 1:
src_icc = fz_device_gray(ctx)->data;
break;
case 3:
src_icc = fz_device_rgb(ctx)->data;
break;
case 4:
src_icc = fz_device_cmyk(ctx)->data;
break;
default:
fz_throw(ctx, FZ_ERROR_GENERIC, "Poorly formed Cal color space");
}
/* To avoid repeated failures building the pdf-cal color space,
* assign the default profile. */
fz_cmm_fin_profile(ctx, src_icc);
cal->profile = src_icc;
}
}
}
else
src_icc = get_base_icc_profile(ctx, src);
if (src_icc == NULL)
fz_throw(ctx, FZ_ERROR_GENERIC, "Profile missing during link creation");
*src_n = src_icc->num_devcomp;
fz_var(link);
fz_var(key);
fz_try(ctx)
{
/* Check the storable to see if we have a copy. */
key = fz_malloc_struct(ctx, fz_link_key);
key->refs = 1;
memcpy(&key->dst_md5, dst_icc->md5, 16);
memcpy(&key->src_md5, src_icc->md5, 16);
key->rend.ri = rend->ri;
key->rend.bp = rend->bp;
key->alpha = alpha;
key->depth = num_bytes;
key->proof = (prf_icc != NULL);
link = fz_find_item(ctx, fz_drop_link_imp, key, &fz_link_store_type);
/* Not found. Make new one add to store. */
if (link == NULL)
{
link = fz_new_icc_link(ctx, src_icc, prf_icc, dst_icc, rend, num_bytes, alpha);
new_link = fz_store_item(ctx, key, link, sizeof(fz_icclink), &fz_link_store_type);
if (new_link != NULL)
{
/* Found one while adding! Perhaps from another thread? */
fz_drop_icclink(ctx, link);
link = new_link;
}
}
}
fz_always(ctx)
{
fz_drop_link_key(ctx, key);
}
fz_catch(ctx)
{
/* Ignore any error that came just from the enstoring. */
if (link == NULL)
fz_rethrow(ctx);
}
return link;
}
/* Device colorspace definitions */
static void gray_to_rgb(fz_context *ctx, fz_colorspace *cs, const float *gray, float *rgb)
{
rgb[0] = gray[0];
rgb[1] = gray[0];
rgb[2] = gray[0];
}
static void rgb_to_gray(fz_context *ctx, fz_colorspace *cs, const float *rgb, float *gray)
{
float r = rgb[0];
float g = rgb[1];
float b = rgb[2];
gray[0] = r * 0.3f + g * 0.59f + b * 0.11f;
}
static void rgb_to_rgb(fz_context *ctx, fz_colorspace *cs, const float *rgb, float *xyz)
{
xyz[0] = rgb[0];
xyz[1] = rgb[1];
xyz[2] = rgb[2];
}
static void bgr_to_rgb(fz_context *ctx, fz_colorspace *cs, const float *bgr, float *rgb)
{
rgb[0] = bgr[2];
rgb[1] = bgr[1];
rgb[2] = bgr[0];
}
static void rgb_to_bgr(fz_context *ctx, fz_colorspace *cs, const float *rgb, float *bgr)
{
bgr[0] = rgb[2];
bgr[1] = rgb[1];
bgr[2] = rgb[0];
}
static void cmyk_to_rgb(fz_context *ctx, fz_colorspace *cs, const float *cmyk, float *rgb)
{
#ifdef SLOWCMYK /* from poppler */
float c = cmyk[0], m = cmyk[1], y = cmyk[2], k = cmyk[3];
float r, g, b, x;
float cm = c * m;
float c1m = m - cm;
float cm1 = c - cm;
float c1m1 = 1 - m - cm1;
float c1m1y = c1m1 * y;
float c1m1y1 = c1m1 - c1m1y;
float c1my = c1m * y;
float c1my1 = c1m - c1my;
float cm1y = cm1 * y;
float cm1y1 = cm1 - cm1y;
float cmy = cm * y;
float cmy1 = cm - cmy;
/* this is a matrix multiplication, unrolled for performance */
x = c1m1y1 * k; /* 0 0 0 1 */
r = g = b = c1m1y1 - x; /* 0 0 0 0 */
r += 0.1373f * x;
g += 0.1216f * x;
b += 0.1255f * x;
x = c1m1y * k; /* 0 0 1 1 */
r += 0.1098f * x;
g += 0.1020f * x;
x = c1m1y - x; /* 0 0 1 0 */
r += x;
g += 0.9490f * x;
x = c1my1 * k; /* 0 1 0 1 */
r += 0.1412f * x;
x = c1my1 - x; /* 0 1 0 0 */
r += 0.9255f * x;
b += 0.5490f * x;
x = c1my * k; /* 0 1 1 1 */
r += 0.1333f * x;
x = c1my - x; /* 0 1 1 0 */
r += 0.9294f * x;
g += 0.1098f * x;
b += 0.1412f * x;
x = cm1y1 * k; /* 1 0 0 1 */
g += 0.0588f * x;
b += 0.1412f * x;
x = cm1y1 - x; /* 1 0 0 0 */
g += 0.6784f * x;
b += 0.9373f * x;
x = cm1y * k; /* 1 0 1 1 */
g += 0.0745f * x;
x = cm1y - x; /* 1 0 1 0 */
g += 0.6510f * x;
b += 0.3137f * x;
x = cmy1 * k; /* 1 1 0 1 */
b += 0.0078f * x;
x = cmy1 - x; /* 1 1 0 0 */
r += 0.1804f * x;
g += 0.1922f * x;
b += 0.5725f * x;
x = cmy * (1-k); /* 1 1 1 0 */
r += 0.2118f * x;
g += 0.2119f * x;
b += 0.2235f * x;
rgb[0] = fz_clamp(r, 0, 1);
rgb[1] = fz_clamp(g, 0, 1);
rgb[2] = fz_clamp(b, 0, 1);
#else
rgb[0] = 1 - fz_min(1, cmyk[0] + cmyk[3]);
rgb[1] = 1 - fz_min(1, cmyk[1] + cmyk[3]);
rgb[2] = 1 - fz_min(1, cmyk[2] + cmyk[3]);
#endif
}
static void rgb_to_cmyk(fz_context *ctx, fz_colorspace *cs, const float *rgb, float *cmyk)
{
float c, m, y, k;
c = 1 - rgb[0];
m = 1 - rgb[1];
y = 1 - rgb[2];
k = fz_min(c, fz_min(m, y));
cmyk[0] = c - k;
cmyk[1] = m - k;
cmyk[2] = y - k;
cmyk[3] = k;
}
static inline float fung(float x)
{
if (x >= 6.0f / 29.0f)
return x * x * x;
return (108.0f / 841.0f) * (x - (4.0f / 29.0f));
}
static void
lab_to_rgb(fz_context *ctx, fz_colorspace *cs, const float *lab, float *rgb)
{
/* input is in range (0..100, -128..127, -128..127) not (0..1, 0..1, 0..1) */
float lstar, astar, bstar, l, m, n, x, y, z, r, g, b;
lstar = lab[0];
astar = lab[1];
bstar = lab[2];
m = (lstar + 16) / 116;
l = m + astar / 500;
n = m - bstar / 200;
x = fung(l);
y = fung(m);
z = fung(n);
r = (3.240449f * x + -1.537136f * y + -0.498531f * z) * 0.830026f;
g = (-0.969265f * x + 1.876011f * y + 0.041556f * z) * 1.05452f;
b = (0.055643f * x + -0.204026f * y + 1.057229f * z) * 1.1003f;
rgb[0] = sqrtf(fz_clamp(r, 0, 1));
rgb[1] = sqrtf(fz_clamp(g, 0, 1));
rgb[2] = sqrtf(fz_clamp(b, 0, 1));
}
static void
rgb_to_lab(fz_context *ctx, fz_colorspace *cs, const float *rgb, float *lab)
{
fz_warn(ctx, "cannot convert into L*a*b colorspace");
lab[0] = rgb[0];
lab[1] = rgb[1];
lab[2] = rgb[2];
}
/* This could be different for a, b */
static void
clamp_lab(const fz_colorspace *cs, const float *src, float *dst)
{
int i;
for (i = 0; i < 3; i++)
dst[i] = fz_clamp(src[i], i ? -128 : 0, i ? 127 : 100);
}
static int fz_colorspace_is_lab(fz_context *ctx, const fz_colorspace *cs)
{
return cs && cs->to_ccs == lab_to_rgb;
}
int
fz_colorspace_is_subtractive(fz_context *ctx, fz_colorspace *cs)
{
return (cs && cs->is_subtractive);
}
static fz_colorspace k_default_gray = { {-1, fz_drop_colorspace_imp}, 0, "DeviceGray", 1, 0, gray_to_rgb, rgb_to_gray, clamp_default, NULL, NULL, NULL };
static fz_colorspace k_default_rgb = { {-1, fz_drop_colorspace_imp}, 0, "DeviceRGB", 3, 0, rgb_to_rgb, rgb_to_rgb, clamp_default, NULL, NULL, NULL };
static fz_colorspace k_default_bgr = { {-1, fz_drop_colorspace_imp}, 0, "DeviceBGR", 3, 0, bgr_to_rgb, rgb_to_bgr, clamp_default, NULL, NULL, NULL };
static fz_colorspace k_default_cmyk = { {-1, fz_drop_colorspace_imp}, 0, "DeviceCMYK", 4, 1, cmyk_to_rgb, rgb_to_cmyk, clamp_default, NULL, NULL, NULL };
static fz_colorspace k_default_lab = { {-1, fz_drop_colorspace_imp}, 0, "Lab", 3, 0, lab_to_rgb, rgb_to_lab, clamp_lab, NULL, NULL, NULL};
static fz_color_params k_default_color_params = { FZ_RI_RELATIVE_COLORIMETRIC, 1, 0, 0 };
static fz_colorspace *default_gray = &k_default_gray;
static fz_colorspace *default_rgb = &k_default_rgb;
static fz_colorspace *default_bgr = &k_default_bgr;
static fz_colorspace *default_cmyk = &k_default_cmyk;
static fz_colorspace *default_lab = &k_default_lab;
static fz_color_params *default_color_params = &k_default_color_params;
const fz_cmm_engine *fz_get_cmm_engine(fz_context *ctx)
{
return ctx->colorspace ? ctx->colorspace->cmm : NULL;
}
static void
set_no_icc(fz_colorspace_context *cct)
{
cct->gray = default_gray;
cct->rgb = default_rgb;
cct->bgr = default_bgr;
cct->cmyk = default_cmyk;
cct->lab = default_lab;
}
void fz_set_cmm_engine(fz_context *ctx, const fz_cmm_engine *engine)
{
fz_colorspace_context *cct;
if (!ctx)
return;
cct = ctx->colorspace;
if (!cct)
return;
#ifdef NO_ICC
if (engine)
fz_throw(ctx, FZ_ERROR_GENERIC, "ICC workflow not supported in NO_ICC build");
#else
if (cct->cmm == engine)
return;
fz_drop_cmm_context(ctx);
fz_drop_colorspace(ctx, cct->gray);
fz_drop_colorspace(ctx, cct->rgb);
fz_drop_colorspace(ctx, cct->bgr);
fz_drop_colorspace(ctx, cct->cmyk);
fz_drop_colorspace(ctx, cct->lab);
cct->gray = NULL;
cct->rgb = NULL;
cct->bgr = NULL;
cct->cmyk = NULL;
cct->lab = NULL;
cct->cmm = engine;
fz_new_cmm_context(ctx);
if (engine)
{
cct->gray = fz_new_icc_colorspace(ctx, 1, 1, NULL, "gray-icc");
cct->rgb = fz_new_icc_colorspace(ctx, 1, 3, NULL, "rgb-icc");
cct->bgr = fz_new_icc_colorspace(ctx, 1, 3, NULL, "bgr-icc");
cct->cmyk = fz_new_icc_colorspace(ctx, 1, 4, NULL, "cmyk-icc");
cct->lab = fz_new_icc_colorspace(ctx, 1, 3, NULL, "lab-icc");
}
else
set_no_icc(cct);
#endif
}
void fz_new_colorspace_context(fz_context *ctx)
{
ctx->colorspace = fz_malloc_struct(ctx, fz_colorspace_context);
ctx->colorspace->ctx_refs = 1;
set_no_icc(ctx->colorspace);
#ifdef NO_ICC
fz_set_cmm_engine(ctx, NULL);
#else
fz_set_cmm_engine(ctx, &fz_cmm_engine_lcms);
#endif
}
void
fz_new_cmm_context(fz_context *ctx)
{
ctx->cmm_instance = fz_cmm_new_instance(ctx);
}
void
fz_drop_cmm_context(fz_context *ctx)
{
fz_cmm_drop_instance(ctx);
ctx->cmm_instance = NULL;
}
fz_colorspace_context *
fz_keep_colorspace_context(fz_context *ctx)
{
if (!ctx)
return NULL;
return fz_keep_imp(ctx, ctx->colorspace, &ctx->colorspace->ctx_refs);
}
void fz_drop_colorspace_context(fz_context *ctx)
{
if (!ctx)
return;
if (fz_drop_imp(ctx, ctx->colorspace, &ctx->colorspace->ctx_refs))
{
fz_drop_colorspace(ctx, ctx->colorspace->gray);
fz_drop_colorspace(ctx, ctx->colorspace->rgb);
/* FIXME: bgr */
fz_drop_colorspace(ctx, ctx->colorspace->cmyk);
fz_drop_colorspace(ctx, ctx->colorspace->lab);
fz_drop_cmm_context(ctx);
fz_free(ctx, ctx->colorspace);
ctx->colorspace = NULL;
}
}
fz_colorspace *
fz_device_gray(fz_context *ctx)
{
return ctx->colorspace->gray;
}
fz_colorspace *
fz_device_rgb(fz_context *ctx)
{
return ctx->colorspace->rgb;
}
fz_colorspace *
fz_device_bgr(fz_context *ctx)
{
return ctx->colorspace->bgr;
}
fz_colorspace *
fz_device_cmyk(fz_context *ctx)
{
return ctx->colorspace->cmyk;
}
fz_colorspace *
fz_device_lab(fz_context *ctx)
{
return ctx->colorspace->lab;
}
const fz_color_params *
fz_default_color_params(fz_context *ctx)
{
return default_color_params;
}
/* Fast pixmap color conversions */
static void fast_gray_to_rgb(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
size_t w = src->w;
int h = src->h;
ptrdiff_t d_line_inc = dst->stride - w * (dst->alpha + 3);
ptrdiff_t s_line_inc = src->stride - w * (src->alpha + 1);
if ((int)w < 0 || h < 0)
return;
if (d_line_inc == 0 && s_line_inc == 0)
{
w *= h;
h = 1;
}
if (dst->alpha)
{
if (src->alpha)
{
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = s[0];
d[1] = s[0];
d[2] = s[0];
d[3] = s[1];
s += 2;
d += 4;
}
d += d_line_inc;
s += s_line_inc;
}
}
else
{
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = s[0];
d[1] = s[0];
d[2] = s[0];
d[3] = 255;
s++;
d += 4;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
else
{
int si = 1 + src->alpha;
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = s[0];
d[1] = s[0];
d[2] = s[0];
s += si;
d += 3;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
static void fast_gray_to_cmyk(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
size_t w = src->w;
int h = src->h;
ptrdiff_t d_line_inc = dst->stride - w * (dst->alpha + 4);
ptrdiff_t s_line_inc = src->stride - w * (src->alpha + 1);
if ((int)w < 0 || h < 0)
return;
if (d_line_inc == 0 && s_line_inc == 0)
{
w *= h;
h = 1;
}
if (dst->alpha)
{
if (src->alpha)
{
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = 0;
d[1] = 0;
d[2] = 0;
d[3] = 255 - s[0];
d[4] = s[1];
s += 2;
d += 5;
}
d += d_line_inc;
s += s_line_inc;
}
}
else
{
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = 0;
d[1] = 0;
d[2] = 0;
d[3] = 255 - s[0];
d[4] = 255;
s++;
d += 5;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
else
{
int si = 1 + src->alpha;
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = 0;
d[1] = 0;
d[2] = 0;
d[3] = 255 - s[0];
s += si;
d += 4;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
static void fast_rgb_to_gray(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
size_t w = src->w;
int h = src->h;
ptrdiff_t d_line_inc = dst->stride - w * (dst->alpha + 1);
ptrdiff_t s_line_inc = src->stride - w * (src->alpha + 3);
if ((int)w < 0 || h < 0)
return;
if (d_line_inc == 0 && s_line_inc == 0)
{
w *= h;
h = 1;
}
if (dst->alpha)
{
if (src->alpha)
{
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = ((s[0]+1) * 77 + (s[1]+1) * 150 + (s[2]+1) * 28) >> 8;
d[1] = s[3];
s += 4;
d += 2;
}
d += d_line_inc;
s += s_line_inc;
}
}
else
{
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = ((s[0]+1) * 77 + (s[1]+1) * 150 + (s[2]+1) * 28) >> 8;
d[1] = 255;
s += 3;
d += 2;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
else
{
int sn = src->n;
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = ((s[0]+1) * 77 + (s[1]+1) * 150 + (s[2]+1) * 28) >> 8;
s += sn;
d++;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
static void fast_bgr_to_gray(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
size_t w = src->w;
int h = src->h;
ptrdiff_t d_line_inc = dst->stride - w * (dst->alpha + 1);
ptrdiff_t s_line_inc = src->stride - w * (src->alpha + 3);
if ((int)w < 0 || h < 0)
return;
if (d_line_inc == 0 && s_line_inc == 0)
{
w *= h;
h = 1;
}
if (dst->alpha)
{
if (src->alpha)
{
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = ((s[0]+1) * 28 + (s[1]+1) * 150 + (s[2]+1) * 77) >> 8;
d[1] = s[3];
s += 4;
d += 2;
}
d += d_line_inc;
s += s_line_inc;
}
}
else
{
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = ((s[0]+1) * 28 + (s[1]+1) * 150 + (s[2]+1) * 77) >> 8;
d[1] = 255;
s += 3;
d += 2;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
else
{
int si = 3 + src->alpha;
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = ((s[0]+1) * 28 + (s[1]+1) * 150 + (s[2]+1) * 77) >> 8;
s += si;
d++;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
static void fast_rgb_to_cmyk(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
size_t w = src->w;
int h = src->h;
ptrdiff_t d_line_inc = dst->stride - w * (dst->alpha + 4);
ptrdiff_t s_line_inc = src->stride - w * (src->alpha + 3);
if ((int)w < 0 || h < 0)
return;
if (d_line_inc == 0 && s_line_inc == 0)
{
w *= h;
h = 1;
}
if (dst->alpha)
{
if (src->alpha)
{
while (h--)
{
size_t ww = w;
while (ww--)
{
unsigned char c = 255 - s[0];
unsigned char m = 255 - s[1];
unsigned char y = 255 - s[2];
unsigned char k = (unsigned char)fz_mini(c, fz_mini(m, y));
d[0] = c - k;
d[1] = m - k;
d[2] = y - k;
d[3] = k;
d[4] = s[3];
s += 4;
d += 5;
}
d += d_line_inc;
s += s_line_inc;
}
}
else
{
while (h--)
{
size_t ww = w;
while (ww--)
{
unsigned char c = 255 - s[0];
unsigned char m = 255 - s[1];
unsigned char y = 255 - s[2];
unsigned char k = (unsigned char)fz_mini(c, fz_mini(m, y));
d[0] = c - k;
d[1] = m - k;
d[2] = y - k;
d[3] = k;
d[4] = 255;
s += 3;
d += 5;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
else
{
int si = 3 + src->alpha;
while (h--)
{
size_t ww = w;
while (ww--)
{
unsigned char c = 255 - s[0];
unsigned char m = 255 - s[1];
unsigned char y = 255 - s[2];
unsigned char k = (unsigned char)fz_mini(c, fz_mini(m, y));
d[0] = c - k;
d[1] = m - k;
d[2] = y - k;
d[3] = k;
s += si;
d += 4;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
static void fast_bgr_to_cmyk(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
size_t w = src->w;
int h = src->h;
ptrdiff_t d_line_inc = dst->stride - w * (dst->alpha + 4);
ptrdiff_t s_line_inc = src->stride - w * (src->alpha + 3);
if ((int)w < 0 || h < 0)
return;
if (d_line_inc == 0 && s_line_inc == 0)
{
w *= h;
h = 1;
}
if (dst->alpha)
{
if (src->alpha)
{
while (h--)
{
size_t ww = w;
while (ww--)
{
unsigned char c = 255 - s[2];
unsigned char m = 255 - s[1];
unsigned char y = 255 - s[0];
unsigned char k = (unsigned char)fz_mini(c, fz_mini(m, y));
d[0] = c - k;
d[1] = m - k;
d[2] = y - k;
d[3] = k;
d[4] = s[3];
s += 4;
d += 5;
}
d += d_line_inc;
s += s_line_inc;
}
}
else
{
while (h--)
{
size_t ww = w;
while (ww--)
{
unsigned char c = 255 - s[2];
unsigned char m = 255 - s[1];
unsigned char y = 255 - s[0];
unsigned char k = (unsigned char)fz_mini(c, fz_mini(m, y));
d[0] = c - k;
d[1] = m - k;
d[2] = y - k;
d[3] = k;
d[4] = 255;
s += 3;
d += 5;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
else
{
int si = 3 + src->alpha;
while (h--)
{
size_t ww = w;
while (ww--)
{
unsigned char c = 255 - s[2];
unsigned char m = 255 - s[1];
unsigned char y = 255 - s[0];
unsigned char k = (unsigned char)fz_mini(c, fz_mini(m, y));
d[0] = c - k;
d[1] = m - k;
d[2] = y - k;
d[3] = k;
s += si;
d += 4;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
static void fast_cmyk_to_gray(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
size_t w = src->w;
int h = src->h;
ptrdiff_t d_line_inc = dst->stride - w * (dst->alpha + 1);
ptrdiff_t s_line_inc = src->stride - w * (src->alpha + 4);
if ((int)w < 0 || h < 0)
return;
if (d_line_inc == 0 && s_line_inc == 0)
{
w *= h;
h = 1;
}
if (dst->alpha)
{
if (src->alpha)
{
while (h--)
{
size_t ww = w;
while (ww--)
{
unsigned char c = fz_mul255(s[0], 77);
unsigned char m = fz_mul255(s[1], 150);
unsigned char y = fz_mul255(s[2], 28);
d[0] = 255 - (unsigned char)fz_mini(c + m + y + s[3], 255);
d[1] = s[4];
s += 5;
d += 2;
}
d += d_line_inc;
s += s_line_inc;
}
}
else
{
while (h--)
{
size_t ww = w;
while (ww--)
{
unsigned char c = fz_mul255(s[0], 77);
unsigned char m = fz_mul255(s[1], 150);
unsigned char y = fz_mul255(s[2], 28);
d[0] = 255 - (unsigned char)fz_mini(c + m + y + s[3], 255);
d[1] = 255;
s += 3;
d += 2;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
else
{
int si = 4 + src->alpha;
while (h--)
{
size_t ww = w;
while (ww--)
{
unsigned char c = fz_mul255(s[0], 77);
unsigned char m = fz_mul255(s[1], 150);
unsigned char y = fz_mul255(s[2], 28);
d[0] = 255 - (unsigned char)fz_mini(c + m + y + s[3], 255);
s += si;
d++;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
#ifdef ARCH_ARM
static void
fast_cmyk_to_rgb_ARM(unsigned char *dst, unsigned char *src, int n)
__attribute__((naked));
static void
fast_cmyk_to_rgb_ARM(unsigned char *dst, unsigned char *src, int n)
{
asm volatile(
ENTER_ARM
"stmfd r13!,{r4-r11,r14} \n"
"@ r0 = dst \n"
"@ r1 = src \n"
"@ r2 = n \n"
"mov r12, #0 @ r12= CMYK = 0 \n"
"b 2f @ enter loop \n"
"1: @ White or Black \n"
"@ Cunning trick: On entry r11 = 0 if black, r11 = FF if white \n"
"eor r12,r11,#0xFF @ r12= FF if black, 0 if white \n"
"ldrb r7, [r1],#1 @ r8 = s[4] \n"
"strb r11,[r0],#1 @ d[0] = r \n"
"strb r11,[r0],#1 @ d[1] = g \n"
"strb r11,[r0],#1 @ d[2] = b \n"
"strb r7, [r0],#1 @ d[3] = s[4] \n"
"mov r12,r12,LSL #24 @ r12 = CMYK \n"
"subs r2, r2, #1 @ r2 = n-- \n"
"beq 9f \n"
"2: @ Main loop starts here \n"
"ldrb r3, [r1], #4 @ r3 = c \n"
"ldrb r6, [r1, #-1] @ r6 = k \n"
"ldrb r5, [r1, #-2] @ r5 = y \n"
"ldrb r4, [r1, #-3] @ r4 = m \n"
"eors r11,r6, #0xFF @ if (k == 255) \n"
"beq 1b @ goto black \n"
"orr r7, r3, r4, LSL #8 \n"
"orr r14,r5, r6, LSL #8 \n"
"orrs r7, r7, r14,LSL #16 @ r7 = cmyk \n"
"beq 1b @ if (cmyk == 0) white \n"
"@ At this point, we have to decode a new pixel \n"
"@ r0 = dst r1 = src r2 = n r7 = cmyk \n"
"3: @ unmatched \n"
"stmfd r13!,{r0-r1,r7} @ stash regs for space \n"
"add r3, r3, r3, LSR #7 @ r3 = c += c>>7 \n"
"add r4, r4, r4, LSR #7 @ r4 = m += m>>7 \n"
"add r5, r5, r5, LSR #7 @ r5 = y += y>>7 \n"
"add r6, r6, r6, LSR #7 @ r6 = k += k>>7 \n"
"mov r5, r5, LSR #1 @ sacrifice 1 bit of Y \n"
"mul r8, r3, r4 @ r8 = cm = c * m \n"
"rsb r9, r8, r4, LSL #8 @ r9 = c1m = (m<<8) - cm \n"
"rsb r3, r8, r3, LSL #8 @ r3 = cm1 = (c<<8) - cm \n"
"rsb r4, r4, #0x100 @ r4 = 256-m \n"
"rsb r4, r3, r4, LSL #8 @ r4 = c1m1 =((256-m)<<8)-cm1 \n"
"mul r7, r4, r5 @ r7 = c1m1y = c1m1 * y \n"
"rsb r4, r7, r4, LSL #7 @ r4 = c1m1y1 = (c1m1<<7)-c1m1y \n"
"mul r10,r9, r5 @ r10= c1my = c1m * y \n"
"rsb r9, r10,r9, LSL #7 @ r9 = c1my1 = (c1m<<7) - c1my \n"
"mul r11,r3, r5 @ r11= cm1y = cm1 * y \n"
"rsb r3, r11,r3, LSL #7 @ r3 = cm1y1 = (cm1<<7) - cm1y \n"
"mul r5, r8, r5 @ r5 = cmy = cm * y \n"
"rsb r8, r5, r8, LSL #7 @ r8 = cmy1 = (cm<<7) - cmy \n"
"@ Register recap: \n"
"@ r3 = cm1y1 \n"
"@ r4 = c1m1y1 \n"
"@ r5 = cmy \n"
"@ r6 = k \n"
"@ r7 = c1m1y \n"
"@ r8 = cmy1 \n"
"@ r9 = c1my1 \n"
"@ r10= c1my \n"
"@ r11= cm1y \n"
"@ The actual matrix multiplication \n"
"mul r14,r4, r6 @ r14= x1 = c1m1y1 * k \n"
"rsb r4, r14,r4, LSL #8 @ r4 = x0 = (c1m1y1<<8) - x1 \n"
"add r4, r4, r14,LSR #8-5 @ r4 = b = x0 + 32*(x1>>8) \n"
"sub r1, r4, r14,LSR #8 @ r1 = g = x0 + 31*(x1>>8) \n"
"add r0, r1, r14,LSR #8-2 @ r0 = r = x0 + 35*(x1>>8) \n"
" \n"
"mul r14,r7, r6 @ r14= x1 = c1m1y * k \n"
"rsb r7, r14,r7, LSL #8 @ r7 = x0 = (c1m1y<<8) - x1 \n"
"add r0, r0, r7 @ r0 = r += x0 \n"
"add r1, r1, r7 @ r1 = g += (x0>>8 * 256) \n"
"sub r1, r1, r7, LSR #8-3 @ 248 \n"
"sub r1, r1, r7, LSR #8-2 @ 244 \n"
"sub r1, r1, r7, LSR #8 @ 243 \n"
"sub r7, r14,r14,LSR #3 @ r7 = 28*(x1>>5) \n"
"add r0, r0, r7, LSR #8-5 @ r0 = r += 28 * x1 \n"
"sub r7, r7, r14,LSR #4 @ r7 = 26*(x1>>5) \n"
"add r1, r1, r7, LSR #8-5 @ r1 = g += 26 * x1 \n"
" \n"
"mul r14,r9, r6 @ r14= x1 = c1my1 * k \n"
"sub r9, r9, r14,LSR #8 @ r9 = x0>>8 = c1my1 - (x1>>8) \n"
"add r0, r0, r14,LSR #8-5 @ r0 = r += (x1>>8)*32 \n"
"add r0, r0, r14,LSR #8-2 @ r0 = r += (x1>>8)*36 \n"
"mov r14,#237 @ r14= 237 \n"
"mla r0,r14,r9,r0 @ r14= r += x0*237 \n"
"mov r14,#141 @ r14= 141 \n"
"mla r4,r14,r9,r4 @ r14= b += x0*141 \n"
" \n"
"mul r14,r10,r6 @ r14= x1 = c1my * k \n"
"sub r10,r10,r14,LSR #8 @ r10= x0>>8 = c1my - (x1>>8) \n"
"add r0, r0, r14,LSR #8-5 @ r0 = r += 32 * x1 \n"
"add r0, r0, r14,LSR #8-1 @ r0 = r += 34 * x1 \n"
"mov r14,#238 @ r14= 238 \n"
"mla r0,r14,r10,r0 @ r0 = r += 238 * x0 \n"
"mov r14,#28 @ r14= 28 \n"
"mla r1,r14,r10,r1 @ r1 = g += 28 * x0 \n"
"mov r14,#36 @ r14= 36 \n"
"mla r4,r14,r10,r4 @ r4 = b += 36 * x0 \n"
" \n"
"mul r14,r3, r6 @ r14= x1 = cm1y1 * k \n"
"sub r3, r3, r14,LSR #8 @ r3 = x1>>8 = cm1y1 - (x1>>8) \n"
"add r1, r1, r14,LSR #8-4 @ r1 = g += 16*x1 \n"
"sub r1, r1, r14,LSR #8 @ 15*x1 \n"
"add r4, r4, r14,LSR #8-5 @ r4 = b += 32*x1 \n"
"add r4, r4, r14,LSR #8-2 @ 36*x1 \n"
"mov r14,#174 @ r14= 174 \n"
"mla r1, r14,r3, r1 @ r1 = g += 174 * x0 \n"
"mov r14,#240 @ r14= 240 \n"
"mla r4, r14,r3, r4 @ r4 = b += 240 * x0 \n"
" \n"
"mul r14,r11,r6 @ r14= x1 = cm1y * k \n"
"sub r11,r11,r14,LSR #8 @ r11= x0>>8 = cm1y - (x1>>8) \n"
"add r1, r1, r14,LSR #8-4 @ r1 = g += x1 * 16 \n"
"add r1, r1, r14,LSR #8 @ x1 * 17 \n"
"add r1, r1, r14,LSR #8-1 @ x1 * 19 \n"
"mov r14,#167 @ r14 = 167 \n"
"mla r1, r14,r11,r1 @ r1 = g += 167 * x0 \n"
"mov r14,#80 @ r14 = 80 \n"
"mla r4, r14,r11,r4 @ r4 = b += 80 * x0 \n"
" \n"
"mul r14,r8, r6 @ r14= x1 = cmy1 * k \n"
"sub r8, r8, r14,LSR #8 @ r8 = x0>>8 = cmy1 - (x1>>8) \n"
"add r4, r4, r14,LSR #8-1 @ r4 = b += x1 * 2 \n"
"mov r14,#46 @ r14=46 \n"
"mla r0, r14,r8, r0 @ r0 = r += 46 * x0 \n"
"mov r14,#49 @ r14=49 \n"
"mla r1, r14,r8, r1 @ r1 = g += 49 * x0 \n"
"mov r14,#147 @ r14=147 \n"
"mla r4, r14,r8, r4 @ r4 = b += 147 * x0 \n"
" \n"
"rsb r6, r6, #256 @ r6 = k = 256-k \n"
"mul r14,r5, r6 @ r14= x0 = cmy * (256-k) \n"
"mov r11,#54 @ r11= 54 \n"
"mov r14,r14,LSR #8 @ r14= (x0>>8) \n"
"mov r8,#57 @ r8 = 57 \n"
"mla r0,r14,r11,r0 @ r0 = r += 54*x0 \n"
"mla r1,r14,r11,r1 @ r1 = g += 54*x0 \n"
"mla r4,r14,r8, r4 @ r4 = b += 57*x0 \n"
" \n"
"sub r8, r0, r0, LSR #8 @ r8 = r -= (r>>8) \n"
"sub r9, r1, r1, LSR #8 @ r9 = g -= (r>>8) \n"
"sub r10,r4, r4, LSR #8 @ r10= b -= (r>>8) \n"
"ldmfd r13!,{r0-r1,r12} \n"
"mov r8, r8, LSR #23 @ r8 = r>>23 \n"
"mov r9, r9, LSR #23 @ r9 = g>>23 \n"
"mov r10,r10,LSR #23 @ r10= b>>23 \n"
"ldrb r14,[r1],#1 @ r8 = s[4] \n"
"strb r8, [r0],#1 @ d[0] = r \n"
"strb r9, [r0],#1 @ d[1] = g \n"
"strb r10,[r0],#1 @ d[2] = b \n"
"strb r14,[r0],#1 @ d[3] = s[4] \n"
"subs r2, r2, #1 @ r2 = n-- \n"
"beq 9f \n"
"@ At this point, we've just decoded a pixel \n"
"@ r0 = dst r1 = src r2 = n r8 = r r9 = g r10= b r12= CMYK \n"
"4: \n"
"ldrb r3, [r1], #4 @ r3 = c \n"
"ldrb r6, [r1, #-1] @ r6 = k \n"
"ldrb r5, [r1, #-2] @ r5 = y \n"
"ldrb r4, [r1, #-3] @ r4 = m \n"
"eors r11,r6, #0xFF @ if (k == 255) \n"
"beq 1b @ goto black \n"
"orr r7, r3, r4, LSL #8 \n"
"orr r14,r5, r6, LSL #8 \n"
"orrs r7, r7, r14,LSL #16 @ r7 = cmyk \n"
"beq 1b @ if (cmyk == 0) white \n"
"cmp r7, r12 @ if (cmyk != CMYK) \n"
"bne 3b @ not the same, loop \n"
"@ If we get here, we just matched a pixel we have just decoded \n"
"ldrb r3, [r1],#1 @ r8 = s[4] \n"
"strb r8, [r0],#1 @ d[0] = r \n"
"strb r9, [r0],#1 @ d[1] = g \n"
"strb r10,[r0],#1 @ d[2] = b \n"
"strb r3, [r0],#1 @ d[3] = s[4] \n"
"subs r2, r2, #1 @ r2 = n-- \n"
"bne 4b \n"
"9: \n"
"ldmfd r13!,{r4-r11,PC} @ pop, return to thumb \n"
ENTER_THUMB
);
}
#endif
static inline void cached_cmyk_conv(unsigned char *restrict const pr, unsigned char *restrict const pg, unsigned char *restrict const pb,
unsigned int *restrict const C, unsigned int *restrict const M, unsigned int *restrict const Y, unsigned int *restrict const K,
unsigned int c, unsigned int m, unsigned int y, unsigned int k)
{
#ifdef SLOWCMYK
unsigned int r, g, b;
unsigned int cm, c1m, cm1, c1m1, c1m1y, c1m1y1, c1my, c1my1, cm1y, cm1y1, cmy, cmy1;
unsigned int x0, x1;
if (c == *C && m == *M && y == *Y && k == *K)
{
/* Nothing to do */
}
else if (k == 0 && c == 0 && m == 0 && y == 0)
{
*C = 0;
*M = 0;
*Y = 0;
*K = 0;
*pr = *pg = *pb = 255;
}
else if (k == 255)
{
*C = 0;
*M = 0;
*Y = 0;
*K = 255;
*pr = *pg = *pb = 0;
}
else
{
*C = c;
*M = m;
*Y = y;
*K = k;
c += c>>7;
m += m>>7;
y += y>>7;
k += k>>7;
y >>= 1; /* Ditch 1 bit of Y to avoid overflow */
cm = c * m;
c1m = (m<<8) - cm;
cm1 = (c<<8) - cm;
c1m1 = ((256 - m)<<8) - cm1;
c1m1y = c1m1 * y;
c1m1y1 = (c1m1<<7) - c1m1y;
c1my = c1m * y;
c1my1 = (c1m<<7) - c1my;
cm1y = cm1 * y;
cm1y1 = (cm1<<7) - cm1y;
cmy = cm * y;
cmy1 = (cm<<7) - cmy;
/* this is a matrix multiplication, unrolled for performance */
x1 = c1m1y1 * k; /* 0 0 0 1 */
x0 = (c1m1y1<<8) - x1; /* 0 0 0 0 */
x1 = x1>>8; /* From 23 fractional bits to 15 */
r = g = b = x0;
r += 35 * x1; /* 0.1373f */
g += 31 * x1; /* 0.1216f */
b += 32 * x1; /* 0.1255f */
x1 = c1m1y * k; /* 0 0 1 1 */
x0 = (c1m1y<<8) - x1; /* 0 0 1 0 */
x1 >>= 8; /* From 23 fractional bits to 15 */
r += 28 * x1; /* 0.1098f */
g += 26 * x1; /* 0.1020f */
r += x0;
x0 >>= 8; /* From 23 fractional bits to 15 */
g += 243 * x0; /* 0.9490f */
x1 = c1my1 * k; /* 0 1 0 1 */
x0 = (c1my1<<8) - x1; /* 0 1 0 0 */
x1 >>= 8; /* From 23 fractional bits to 15 */
x0 >>= 8; /* From 23 fractional bits to 15 */
r += 36 * x1; /* 0.1412f */
r += 237 * x0; /* 0.9255f */
b += 141 * x0; /* 0.5490f */
x1 = c1my * k; /* 0 1 1 1 */
x0 = (c1my<<8) - x1; /* 0 1 1 0 */
x1 >>= 8; /* From 23 fractional bits to 15 */
x0 >>= 8; /* From 23 fractional bits to 15 */
r += 34 * x1; /* 0.1333f */
r += 238 * x0; /* 0.9294f */
g += 28 * x0; /* 0.1098f */
b += 36 * x0; /* 0.1412f */
x1 = cm1y1 * k; /* 1 0 0 1 */
x0 = (cm1y1<<8) - x1; /* 1 0 0 0 */
x1 >>= 8; /* From 23 fractional bits to 15 */
x0 >>= 8; /* From 23 fractional bits to 15 */
g += 15 * x1; /* 0.0588f */
b += 36 * x1; /* 0.1412f */
g += 174 * x0; /* 0.6784f */
b += 240 * x0; /* 0.9373f */
x1 = cm1y * k; /* 1 0 1 1 */
x0 = (cm1y<<8) - x1; /* 1 0 1 0 */
x1 >>= 8; /* From 23 fractional bits to 15 */
x0 >>= 8; /* From 23 fractional bits to 15 */
g += 19 * x1; /* 0.0745f */
g += 167 * x0; /* 0.6510f */
b += 80 * x0; /* 0.3137f */
x1 = cmy1 * k; /* 1 1 0 1 */
x0 = (cmy1<<8) - x1; /* 1 1 0 0 */
x1 >>= 8; /* From 23 fractional bits to 15 */
x0 >>= 8; /* From 23 fractional bits to 15 */
b += 2 * x1; /* 0.0078f */
r += 46 * x0; /* 0.1804f */
g += 49 * x0; /* 0.1922f */
b += 147 * x0; /* 0.5725f */
x0 = cmy * (256-k); /* 1 1 1 0 */
x0 >>= 8; /* From 23 fractional bits to 15 */
r += 54 * x0; /* 0.2118f */
g += 54 * x0; /* 0.2119f */
b += 57 * x0; /* 0.2235f */
r -= (r>>8);
g -= (g>>8);
b -= (b>>8);
*pr = r>>23;
*pg = g>>23;
*pb = b>>23;
}
#else
*pr = 255 - (unsigned char)fz_mini(c + k, 255);
*pg = 255 - (unsigned char)fz_mini(m + k, 255);
*pb = 255 - (unsigned char)fz_mini(y + k, 255);
#endif
}
static void fast_cmyk_to_rgb(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
size_t w = src->w;
int h = src->h;
ptrdiff_t d_line_inc = dst->stride - w * (dst->alpha + 3);
ptrdiff_t s_line_inc = src->stride - w * (src->alpha + 4);
unsigned int C,M,Y,K;
unsigned char r,g,b;
if ((int)w < 0 || h < 0)
return;
C = 0;
M = 0;
Y = 0;
K = 0;
r = 255;
g = 255;
b = 255;
if (d_line_inc == 0 && s_line_inc == 0)
{
w *= h;
h = 1;
}
if (dst->alpha)
{
if (src->alpha)
{
#ifdef ARCH_ARM
if (h == 1)
{
fast_cmyk_to_rgb_ARM(d, s, w);
return;
}
#endif
while (h--)
{
size_t ww = w;
while (ww--)
{
cached_cmyk_conv(&r, &g, &b, &C, &M, &Y, &K, s[0], s[1], s[2], s[3]);
d[0] = r;
d[1] = g;
d[2] = b;
d[3] = s[4];
s += 5;
d += 4;
}
d += d_line_inc;
s += s_line_inc;
}
}
else
{
while (h--)
{
size_t ww = w;
while (ww--)
{
cached_cmyk_conv(&r, &g, &b, &C, &M, &Y, &K, s[0], s[1], s[2], s[3]);
d[0] = r;
d[1] = g;
d[2] = b;
d[3] = 255;
s += 4;
d += 4;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
else
{
/* We shouldn't lose alpha */
assert(src->alpha == 0);
while (h--)
{
size_t ww = w;
while (ww--)
{
cached_cmyk_conv(&r, &g, &b, &C, &M, &Y, &K, s[0], s[1], s[2], s[3]);
d[0] = r;
d[1] = g;
d[2] = b;
s += 4;
d += 3;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
static void fast_cmyk_to_bgr(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
size_t w = src->w;
int h = src->h;
ptrdiff_t d_line_inc = dst->stride - w * (dst->alpha + 3);
ptrdiff_t s_line_inc = src->stride - w * (src->alpha + 4);
unsigned int C,M,Y,K;
unsigned char r,g,b;
if ((int)w < 0 || h < 0)
return;
C = 0;
M = 0;
Y = 0;
K = 0;
r = 255;
g = 255;
b = 255;
if (d_line_inc == 0 && s_line_inc == 0)
{
w *= h;
h = 1;
}
if (dst->alpha)
{
if (src->alpha)
{
while (h--)
{
size_t ww = w;
while (ww--)
{
cached_cmyk_conv(&r, &g, &b, &C, &M, &Y, &K, s[0], s[1], s[2], s[3]);
d[0] = b;
d[1] = g;
d[2] = r;
d[3] = s[4];
s += 5;
d += 4;
}
d += d_line_inc;
s += s_line_inc;
}
}
else
{
while (h--)
{
size_t ww = w;
while (ww--)
{
cached_cmyk_conv(&r, &g, &b, &C, &M, &Y, &K, s[0], s[1], s[2], s[3]);
d[0] = b;
d[1] = g;
d[2] = r;
d[3] = 255;
s += 4;
d += 4;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
else
{
/* We shouldn't lose alpha */
assert(src->alpha == 0);
while (h--)
{
size_t ww = w;
while (ww--)
{
cached_cmyk_conv(&r, &g, &b, &C, &M, &Y, &K, s[0], s[1], s[2], s[3]);
d[0] = b;
d[1] = g;
d[2] = r;
s += 4;
d += 3;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
static void fast_rgb_to_bgr(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
size_t w = src->w;
int h = src->h;
ptrdiff_t d_line_inc = dst->stride - w * (dst->alpha + 3);
ptrdiff_t s_line_inc = src->stride - w * (src->alpha + 3);
if ((int)w < 0 || h < 0)
return;
if (d_line_inc == 0 && s_line_inc == 0)
{
w *= h;
h = 1;
}
if (dst->alpha)
{
if (src->alpha)
{
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = s[2];
d[1] = s[1];
d[2] = s[0];
d[3] = s[3];
s += 4;
d += 4;
}
}
}
else
{
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = s[2];
d[1] = s[1];
d[2] = s[0];
d[3] = 255;
s += 3;
d += 4;
}
}
}
}
else
{
/* We shouldn't lose alpha */
assert(src->alpha == 0);
while (h--)
{
size_t ww = w;
while (ww--)
{
d[0] = s[2];
d[1] = s[1];
d[2] = s[0];
s += 3;
d += 3;
}
}
}
}
static void
icc_conv_pixmap(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
fz_colorspace *srcs = src->colorspace;
fz_colorspace *dsts = dst->colorspace;
fz_icclink *link;
int i;
unsigned char *inputpos, *outputpos;
int src_n;
/* Check if we have to do a color space default substitution */
if (default_cs)
{
switch (fz_colorspace_n(ctx, src->colorspace))
{
case 1:
if (src->colorspace == fz_device_gray(ctx))
srcs = fz_default_gray(ctx, default_cs);
break;
case 3:
if (src->colorspace == fz_device_rgb(ctx))
srcs = fz_default_rgb(ctx, default_cs);
break;
case 4:
if (src->colorspace == fz_device_cmyk(ctx))
srcs = fz_default_cmyk(ctx, default_cs);
break;
}
}
inputpos = src->samples;
outputpos = dst->samples;
link = fz_get_icc_link(ctx, srcs, prf, dsts, color_params, 1, dst->alpha, &src_n);
if (link->is_identity)
{
for (i = 0; i < src->h; i++)
{
memcpy(outputpos, inputpos, src->stride);
inputpos = inputpos + src->stride;
outputpos = outputpos + dst->stride;
}
}
else
fz_cmm_transform_pixmap(ctx, link, dst, src);
fz_drop_icclink(ctx, link);
}
/* Drill down through the base spaces until we get the either a pdf-cal or
* an ICC base space. This is where we want our pixmap to be decoded prior
* to application of the link transform */
static fz_colorspace*
get_base_icc_space(fz_context *ctx, fz_colorspace *srcs)
{
while (1)
{
srcs = srcs->get_base(srcs);
if (srcs == NULL)
fz_throw(ctx, FZ_ERROR_GENERIC, "Final color space should be icc or pdf-cal or lab");
if (fz_colorspace_is_icc(ctx, srcs) || fz_colorspace_is_cal(ctx, srcs) || fz_colorspace_is_lab(ctx, srcs))
return srcs;
}
}
/* Cope with cases where we have to convert through multiple base spaces before
* getting to the final cm color space */
static void
convert_to_icc_base(fz_context *ctx, fz_colorspace *srcs, float *src_f, float *des_f)
{
float temp_f[FZ_MAX_COLORS];
fz_colorspace *base_cs = srcs->get_base(srcs);
if (fz_colorspace_is_icc(ctx, base_cs) || fz_colorspace_is_cal(ctx, base_cs) || fz_colorspace_is_lab(ctx, base_cs))
srcs->to_ccs(ctx, srcs, src_f, des_f);
else
{
srcs->to_ccs(ctx, srcs, src_f, temp_f);
convert_to_icc_base(ctx, base_cs, temp_f, des_f);
}
}
/* For DeviceN and Separation CS, where we require an alternate tint tranform
* prior to the application of an icc profile. Also, indexed images have to
* be handled. Realize those can map from index->devn->pdf-cal->icc for
* example. */
static void
icc_base_conv_pixmap(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
fz_colorspace *srcs = src->colorspace;
fz_colorspace *base_cs = get_base_icc_space(ctx, srcs);
int i;
unsigned char *inputpos, *outputpos;
fz_pixmap *base;
fz_irect bbox;
int h, len;
float src_f[FZ_MAX_COLORS], des_f[FZ_MAX_COLORS];
int sn = src->n;
int stride_src = src->stride - src->w * sn;
int stride_base;
int bn;
base = fz_new_pixmap_with_bbox(ctx, base_cs, fz_pixmap_bbox(ctx, src, &bbox), src->alpha);
bn = base->n;
stride_base = base->stride - base->w * bn;
inputpos = src->samples;
outputpos = base->samples;
h = src->h;
while (h--)
{
len = src->w;
while (len--)
{
for (i = 0; i < sn; i++)
src_f[i] = (float) inputpos[i] / 255.0;
convert_to_icc_base(ctx, srcs, src_f, des_f);
base_cs->clamp(base_cs, des_f, des_f);
for (i = 0; i < bn; i++)
outputpos[i] = des_f[i] * 255.0;
outputpos += bn;
inputpos += sn;
}
outputpos += stride_base;
inputpos += stride_src;
}
fz_try(ctx)
icc_conv_pixmap(ctx, dst, base, prf, default_cs, color_params);
fz_always(ctx)
fz_drop_pixmap(ctx, base);
fz_catch(ctx)
fz_rethrow(ctx);
}
static void
std_conv_pixmap(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
float srcv[FZ_MAX_COLORS];
float dstv[FZ_MAX_COLORS];
int srcn, dstn;
int k, i;
size_t w = src->w;
int h = src->h;
ptrdiff_t d_line_inc = dst->stride - w * dst->n;
ptrdiff_t s_line_inc = src->stride - w * src->n;
int da = dst->alpha;
int sa = src->alpha;
fz_colorspace *ss = src->colorspace;
fz_colorspace *ds = dst->colorspace;
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
if ((int)w < 0 || h < 0)
return;
if (color_params == NULL)
color_params = fz_default_color_params(ctx);
srcn = ss->n;
dstn = ds->n;
assert(src->w == dst->w && src->h == dst->h);
assert(src->n == srcn + sa);
assert(dst->n == dstn + da);
if (d_line_inc == 0 && s_line_inc == 0)
{
w *= h;
h = 1;
}
/* Special case for Lab colorspace (scaling of components to float) */
if ((fz_colorspace_is_lab(ctx, ss) || fz_colorspace_is_lab_icc(ctx, ss)) && srcn == 3)
{
fz_color_converter cc;
fz_find_color_converter(ctx, &cc, NULL, ds, ss, color_params);
while (h--)
{
size_t ww = w;
while (ww--)
{
srcv[0] = *s++ / 255.0f * 100;
srcv[1] = *s++ - 128;
srcv[2] = *s++ - 128;
cc.convert(ctx, &cc, dstv, srcv);
for (k = 0; k < dstn; k++)
*d++ = dstv[k] * 255;
if (da)
*d++ = (sa ? *s : 255);
s += sa;
}
d += d_line_inc;
s += s_line_inc;
}
fz_drop_color_converter(ctx, &cc);
}
/* Brute-force for small images */
else if (w*h < 256)
{
fz_color_converter cc;
fz_find_color_converter(ctx, &cc, NULL, ds, ss, color_params);
while (h--)
{
size_t ww = w;
while (ww--)
{
for (k = 0; k < srcn; k++)
srcv[k] = *s++ / 255.0f;
cc.convert(ctx, &cc, dstv, srcv);
for (k = 0; k < dstn; k++)
*d++ = dstv[k] * 255;
if (da)
*d++ = (sa ? *s : 255);
s += sa;
}
d += d_line_inc;
s += s_line_inc;
}
fz_drop_color_converter(ctx, &cc);
}
/* 1-d lookup table for separation and similar colorspaces */
else if (srcn == 1)
{
unsigned char lookup[FZ_MAX_COLORS * 256];
fz_color_converter cc;
fz_find_color_converter(ctx, &cc, NULL, ds, ss, color_params);
for (i = 0; i < 256; i++)
{
srcv[0] = i / 255.0f;
cc.convert(ctx, &cc, dstv, srcv);
for (k = 0; k < dstn; k++)
lookup[i * dstn + k] = dstv[k] * 255;
}
fz_drop_color_converter(ctx, &cc);
while (h--)
{
size_t ww = w;
while (ww--)
{
i = *s++;
for (k = 0; k < dstn; k++)
*d++ = lookup[i * dstn + k];
if (da)
*d++ = (sa ? *s : 255);
s += sa;
}
d += d_line_inc;
s += s_line_inc;
}
}
/* Memoize colors using a hash table for the general case */
else
{
fz_hash_table *lookup;
unsigned char *color;
unsigned char dummy = s[0] ^ 255;
unsigned char *sold = &dummy;
unsigned char *dold;
fz_color_converter cc;
lookup = fz_new_hash_table(ctx, 509, srcn, -1, NULL);
fz_find_color_converter(ctx, &cc, NULL, ds, ss, color_params);
fz_try(ctx)
{
while (h--)
{
size_t ww = w;
while (ww--)
{
if (*s == *sold && memcmp(sold,s,srcn) == 0)
{
sold = s;
memcpy(d, dold, dstn);
d += dstn;
s += srcn;
if (da)
*d++ = (sa ? *s : 255);
s += sa;
}
else
{
sold = s;
dold = d;
color = fz_hash_find(ctx, lookup, s);
if (color)
{
memcpy(d, color, dstn);
s += srcn;
d += dstn;
if (dst->alpha)
*d++ = (sa ? *s : 255);
s += sa;
}
else
{
for (k = 0; k < srcn; k++)
srcv[k] = *s++ / 255.0f;
cc.convert(ctx, &cc, dstv, srcv);
for (k = 0; k < dstn; k++)
*d++ = dstv[k] * 255;
fz_hash_insert(ctx, lookup, s - srcn, d - dstn);
if (dst->alpha)
*d++ = (sa ? *s : 255);
s += sa;
}
}
}
d += d_line_inc;
s += s_line_inc;
}
}
fz_always(ctx)
fz_drop_color_converter(ctx, &cc);
fz_catch(ctx)
fz_rethrow(ctx);
fz_drop_hash_table(ctx, lookup);
}
}
static void fast_any_to_alpha(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src, fz_colorspace *prf, fz_default_colorspaces *default_cs, const fz_color_params *color_params)
{
if (!src->alpha)
fz_clear_pixmap_with_value(ctx, dst, 255);
else
{
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
size_t w = src->w;
int h = src->h;
int n = src->n;
ptrdiff_t d_line_inc = dst->stride - w * dst->n;
ptrdiff_t s_line_inc = src->stride - w * src->n;
if ((int)w < 0 || h < 0)
return;
assert(dst->alpha && src->alpha && dst->n == 1);
if (d_line_inc == 0 && s_line_inc == 0)
{
w *= h;
h = 1;
}
s += n-1;
while (h--)
{
size_t ww = w;
while (ww--)
{
*d++ = *s;
s += n;
}
d += d_line_inc;
s += s_line_inc;
}
}
}
/* Used for testing all color managed source color spaces. If it is icc, cal or
* has a base space that is managed */
static fz_colorspace *
fz_source_colorspace_cm(fz_context *ctx, fz_colorspace *cs)
{
while (cs)
{
if (fz_colorspace_is_icc(ctx, cs))
return cs;
if (fz_colorspace_is_cal(ctx, cs))
return cs;
cs = fz_colorspace_base(ctx, cs);
}
return NULL;
}
fz_pixmap_converter *fz_lookup_pixmap_converter(fz_context *ctx, fz_colorspace *ds, fz_colorspace *ss)
{
if (ds == NULL)
return fast_any_to_alpha;
if (ss == default_gray)
{
if (ds == default_rgb) return fast_gray_to_rgb;
else if (ds == default_bgr) return fast_gray_to_rgb; /* bgr == rgb here */
else if (ds == default_cmyk) return fast_gray_to_cmyk;
else return std_conv_pixmap;
}
else if (ss == default_rgb)
{
if (ds == default_gray) return fast_rgb_to_gray;
else if (ds == default_bgr) return fast_rgb_to_bgr;
else if (ds == default_cmyk) return fast_rgb_to_cmyk;
else return std_conv_pixmap;
}
else if (ss == default_bgr)
{
if (ds == default_gray) return fast_bgr_to_gray;
else if (ds == default_rgb) return fast_rgb_to_bgr; /* bgr = rgb here */
else if (ds == default_cmyk) return fast_bgr_to_cmyk;
else return std_conv_pixmap;
}
else if (ss == default_cmyk)
{
if (ds == default_gray) return fast_cmyk_to_gray;
else if (ds == default_bgr) return fast_cmyk_to_bgr;
else if (ds == default_rgb) return fast_cmyk_to_rgb;
else return std_conv_pixmap;
}
else
{
fz_colorspace *ss_base = fz_source_colorspace_cm(ctx, ss);
if (ss_base != NULL && fz_colorspace_is_icc(ctx, ds))
{
if (ss_base == ss)
return icc_conv_pixmap;
else
return icc_base_conv_pixmap;
}
else return std_conv_pixmap;
}
}
/* Single color conversion with ICC profiles. ToDo: Check if it makes sense
* to use lcms float link here or to do the conversion to short and back.
*/
static void
icc_conv_color(fz_context *ctx, fz_color_converter *cc, float *dstv, const float *srcv)
{
fz_colorspace *dsts = cc->ds;
int src_n = cc->n;
int dst_n = dsts->n;
fz_icclink *link = (fz_icclink *)cc->link;
int i;
unsigned short dstv_s[FZ_MAX_COLORS];
unsigned short srcv_s[FZ_MAX_COLORS];
if (link->is_identity)
{
for (i = 0; i < src_n; i++)
dstv[i] = srcv[i];
}
else
{
for (i = 0; i < src_n; i++)
srcv_s[i] = srcv[i] * 65535;
fz_cmm_transform_color(ctx, link, dstv_s, srcv_s);
for (i = 0; i < dst_n; i++)
dstv[i] = fz_clamp((float) dstv_s[i] / 65535.0, 0, 1);
}
}
/* Single ICC color conversion but for DeviceN, Sep and Indexed spaces.
* Does premapping to get to ICC */
static void
icc_base_conv_color(fz_context *ctx, fz_color_converter *cc, float *dstv, const float *srcv)
{
fz_colorspace *srcs = cc->ss;
float local_src_map[FZ_MAX_COLORS];
float local_src_map2[FZ_MAX_COLORS];
float *src_map = local_src_map;
do
{
srcs->to_ccs(ctx, srcs, srcv, src_map);
srcs = srcs->get_base(srcs);
srcs->clamp(srcs, src_map, src_map);
srcv = src_map;
src_map = (src_map == local_src_map ? local_src_map2 : local_src_map);
}
while (!fz_colorspace_is_icc(ctx, srcs) && !fz_colorspace_is_cal(ctx, srcs));
icc_conv_color(ctx, cc, dstv, srcv);
}
/* Convert a single color */
static void
std_conv_color(fz_context *ctx, fz_color_converter *cc, float *dstv, const float *srcv)
{
float rgb[3];
int i;
fz_colorspace *srcs = cc->ss;
fz_colorspace *dsts = cc->ds;
if (srcs == NULL)
srcs = fz_device_rgb(ctx);
if (dsts == NULL)
dsts = fz_device_rgb(ctx);
if (srcs != dsts)
{
assert(srcs->to_ccs && dsts->from_ccs);
srcs->to_ccs(ctx, srcs, srcv, rgb);
dsts->from_ccs(ctx, dsts, rgb, dstv);
for (i = 0; i < dsts->n; i++)
dstv[i] = fz_clamp(dstv[i], 0, 1);
}
else
{
for (i = 0; i < srcs->n; i++)
dstv[i] = srcv[i];
}
}
static void
g2rgb(fz_context *ctx, fz_color_converter *cc, float *dv, const float *sv)
{
dv[0] = sv[0];
dv[1] = sv[0];
dv[2] = sv[0];
}
static void
g2cmyk(fz_context *ctx, fz_color_converter *cc, float *dv, const float *sv)
{
dv[0] = 0;
dv[1] = 0;
dv[2] = 0;
dv[3] = 1 - sv[0];
}
static void
rgb2g(fz_context *ctx, fz_color_converter *cc, float *dv, const float *sv)
{
dv[0] = sv[0] * 0.3f + sv[1] * 0.59f + sv[2] * 0.11f;
}
static void
rgb2bgr(fz_context *ctx, fz_color_converter *cc, float *dv, const float *sv)
{
dv[0] = sv[2];
dv[1] = sv[1];
dv[2] = sv[0];
}
static void
rgb2cmyk(fz_context *ctx, fz_color_converter *cc, float *dv, const float *sv)
{
float c = 1 - sv[0];
float m = 1 - sv[1];
float y = 1 - sv[2];
float k = fz_min(c, fz_min(m, y));
dv[0] = c - k;
dv[1] = m - k;
dv[2] = y - k;
dv[3] = k;
}
static void
bgr2g(fz_context *ctx, fz_color_converter *cc, float *dv, const float *sv)
{
dv[0] = sv[0] * 0.11f + sv[1] * 0.59f + sv[2] * 0.3f;
}
static void
bgr2cmyk(fz_context *ctx, fz_color_converter *cc, float *dv, const float *sv)
{
float c = 1 - sv[2];
float m = 1 - sv[1];
float y = 1 - sv[0];
float k = fz_min(c, fz_min(m, y));
dv[0] = c - k;
dv[1] = m - k;
dv[2] = y - k;
dv[3] = k;
}
static void
cmyk2g(fz_context *ctx, fz_color_converter *cc, float *dv, const float *sv)
{
float c = sv[0] * 0.3f;
float m = sv[1] * 0.59f;
float y = sv[2] * 0.11f;
dv[0] = 1 - fz_min(c + m + y + sv[3], 1);
}
static void
cmyk2rgb(fz_context *ctx, fz_color_converter *cc, float *dv, const float *sv)
{
#ifdef SLOWCMYK
cmyk_to_rgb(ctx, NULL, sv, dv);
#else
dv[0] = 1 - fz_min(sv[0] + sv[3], 1);
dv[1] = 1 - fz_min(sv[1] + sv[3], 1);
dv[2] = 1 - fz_min(sv[2] + sv[3], 1);
#endif
}
static void
cmyk2bgr(fz_context *ctx, fz_color_converter *cc, float *dv, const float *sv)
{
#ifdef SLOWCMYK
float rgb[3];
cmyk_to_rgb(ctx, NULL, sv, rgb);
dv[0] = rgb[2];
dv[1] = rgb[1];
dv[2] = rgb[0];
#else
dv[0] = 1 - fz_min(sv[2] + sv[3], 1);
dv[1] = 1 - fz_min(sv[1] + sv[3], 1);
dv[2] = 1 - fz_min(sv[0] + sv[3], 1);
#endif
}
void fz_find_color_converter(fz_context *ctx, fz_color_converter *cc, fz_colorspace *is, fz_colorspace *ds, fz_colorspace *ss, const fz_color_params *params)
{
cc->ds = ds;
cc->ss = ss;
cc->is = is;
cc->link = NULL;
if (ss == default_gray)
{
if ((ds == default_rgb) || (ds == default_bgr))
cc->convert = g2rgb;
else if (ds == default_cmyk)
cc->convert = g2cmyk;
else
cc->convert = std_conv_color;
}
else if (ss == default_rgb)
{
if (ds == default_gray)
cc->convert = rgb2g;
else if (ds == default_bgr)
cc->convert = rgb2bgr;
else if (ds == default_cmyk)
cc->convert = rgb2cmyk;
else
cc->convert = std_conv_color;
}
else if (ss == default_bgr)
{
if (ds == default_gray)
cc->convert = bgr2g;
else if (ds == default_rgb)
cc->convert = rgb2bgr;
else if (ds == default_cmyk)
cc->convert = bgr2cmyk;
else
cc->convert = std_conv_color;
}
else if (ss == default_cmyk)
{
if (ds == default_gray)
cc->convert = cmyk2g;
else if (ds == default_rgb)
cc->convert = cmyk2rgb;
else if (ds == default_bgr)
cc->convert = cmyk2bgr;
else
cc->convert = std_conv_color;
}
else
{
fz_colorspace *ss_base = fz_source_colorspace_cm(ctx, ss);
if (ss_base != NULL && fz_colorspace_is_icc(ctx, ds))
{
if (ss_base == ss)
cc->convert = icc_conv_color;
else
cc->convert = icc_base_conv_color;
cc->link = fz_get_icc_link(ctx, ss_base, is, ds, params, 2, 0, &cc->n);
}
else
cc->convert = std_conv_color;
}
}
void
fz_drop_color_converter(fz_context *ctx, fz_color_converter *cc)
{
fz_icclink *link = (fz_icclink *)cc->link;
if (link)
fz_drop_icclink(ctx, link);
cc->link = NULL;
}
void
fz_convert_color(fz_context *ctx, const fz_color_params *params, fz_colorspace *is, fz_colorspace *ds, float *dv, fz_colorspace *ss, const float *sv)
{
fz_color_converter cc;
fz_find_color_converter(ctx, &cc, is, ds, ss, params);
cc.convert(ctx, &cc, dv, sv);
fz_drop_color_converter(ctx, &cc);
}
/* Indexed */
struct indexed
{
fz_colorspace *base;
int high;
unsigned char *lookup;
};
static void
indexed_to_alt(fz_context *ctx, fz_colorspace *cs, const float *color, float *alt)
{
struct indexed *idx = cs->data;
int i, k;
int n = idx->base->n;
i = color[0] * 255;
i = fz_clampi(i, 0, idx->high);
for (k = 0; k < n; k++)
alt[k] = idx->lookup[i * n + k] / 255.0f;
}
static void
indexed_to_rgb(fz_context *ctx, fz_colorspace *cs, const float *color, float *rgb)
{
float alt[FZ_MAX_COLORS];
struct indexed *idx = cs->data;
indexed_to_alt(ctx, cs, color, alt);
idx->base->to_ccs(ctx, idx->base, alt, rgb);
}
static void
free_indexed(fz_context *ctx, fz_colorspace *cs)
{
struct indexed *idx = cs->data;
fz_drop_colorspace(ctx, idx->base);
fz_free(ctx, idx->lookup);
fz_free(ctx, idx);
}
static fz_colorspace *
base_indexed(const fz_colorspace *cs)
{
struct indexed *idx = cs->data;
return idx->base;
}
static void
clamp_indexed(const fz_colorspace *cs, const float *in, float *out)
{
struct indexed *idx = cs->data;
*out = fz_clamp(*in, 0, idx->high) / 255.0; /* To do, avoid 255 divide */
}
int fz_colorspace_is_indexed(fz_context *ctx, const fz_colorspace *cs)
{
return cs && cs->clamp == clamp_indexed;
}
fz_colorspace *
fz_new_indexed_colorspace(fz_context *ctx, fz_colorspace *base, int high, unsigned char *lookup)
{
fz_colorspace *cs;
struct indexed *idx;
idx = fz_malloc_struct(ctx, struct indexed);
idx->lookup = lookup;
idx->base = base;
idx->high = high;
fz_try(ctx)
cs = fz_new_colorspace(ctx, "Indexed", 0, 1, 0, fz_colorspace_is_icc(ctx, fz_device_rgb(ctx)) ? indexed_to_alt : indexed_to_rgb, NULL, base_indexed, clamp_indexed, free_indexed, idx, sizeof(*idx) + (base->n * (idx->high + 1)) + base->size);
fz_catch(ctx)
{
fz_free(ctx, idx);
fz_rethrow(ctx);
}
return cs;
}
fz_pixmap *
fz_expand_indexed_pixmap(fz_context *ctx, const fz_pixmap *src, int alpha)
{
struct indexed *idx;
fz_pixmap *dst;
const unsigned char *s;
unsigned char *d;
int y, x, k, n, high;
unsigned char *lookup;
fz_irect bbox;
int s_line_inc, d_line_inc;
assert(src->colorspace->to_ccs == indexed_to_rgb || src->colorspace->to_ccs == indexed_to_alt);
assert(src->n == 1 + alpha);
idx = src->colorspace->data;
high = idx->high;
lookup = idx->lookup;
n = idx->base->n;
dst = fz_new_pixmap_with_bbox(ctx, idx->base, fz_pixmap_bbox(ctx, src, &bbox), alpha);
s = src->samples;
d = dst->samples;
s_line_inc = src->stride - src->w * src->n;
d_line_inc = dst->stride - dst->w * dst->n;
if (alpha)
{
for (y = 0; y < src->h; y++)
{
for (x = 0; x < src->w; x++)
{
int v = *s++;
int a = *s++;
int aa = a + (a>>7);
v = fz_mini(v, high);
for (k = 0; k < n; k++)
*d++ = (aa * lookup[v * n + k] + 128)>>8;
*d++ = a;
}
s += s_line_inc;
d += d_line_inc;
}
}
else
{
for (y = 0; y < src->h; y++)
{
for (x = 0; x < src->w; x++)
{
int v = *s++;
v = fz_mini(v, high);
for (k = 0; k < n; k++)
*d++ = lookup[v * n + k];
}
s += s_line_inc;
d += d_line_inc;
}
}
dst->interpolate = src->interpolate;
return dst;
}
typedef struct fz_cached_color_converter
{
fz_color_converter base;
fz_hash_table *hash;
} fz_cached_color_converter;
static void fz_cached_color_convert(fz_context *ctx, fz_color_converter *cc_, float *ds, const float *ss)
{
fz_cached_color_converter *cc = cc_->opaque;
void *val = fz_hash_find(ctx, cc->hash, ss);
int n = cc->base.ds->n * sizeof(float);
fz_color_converter *base_cc = &cc->base;
if (val)
{
memcpy(ds, val, n);
return;
}
base_cc->convert(ctx, base_cc, ds, ss);
val = fz_malloc(ctx, n);
memcpy(val, ds, n);
fz_try(ctx)
{
fz_hash_insert(ctx, cc->hash, ss, val);
}
fz_catch(ctx)
{
fz_free(ctx, val);
}
}
void fz_init_cached_color_converter(fz_context *ctx, fz_color_converter *cc, fz_colorspace *is, fz_colorspace *ds, fz_colorspace *ss, const fz_color_params *params)
{
int n = ss->n;
fz_cached_color_converter *cached = fz_malloc_struct(ctx, fz_cached_color_converter);
cc->opaque = cached;
cc->convert = fz_cached_color_convert;
cc->ds = ds;
cc->ss = ss;
cc->is = is;
fz_try(ctx)
{
fz_find_color_converter(ctx, &cached->base, is, ds, ss, params);
cached->hash = fz_new_hash_table(ctx, 256, n * sizeof(float), -1, fz_free);
}
fz_catch(ctx)
{
fz_drop_color_converter(ctx, &cached->base);
fz_drop_hash_table(ctx, cached->hash);
fz_free(ctx, cached);
fz_rethrow(ctx);
}
}
void fz_fin_cached_color_converter(fz_context *ctx, fz_color_converter *cc_)
{
fz_cached_color_converter *cc;
if (cc_ == NULL)
return;
cc = cc_->opaque;
if (cc == NULL)
return;
cc_->opaque = NULL;
fz_drop_hash_table(ctx, cc->hash);
fz_drop_color_converter(ctx, &cc->base);
fz_free(ctx, cc);
}
fz_colorspace *fz_colorspace_base(fz_context *ctx, const fz_colorspace *cs)
{
return cs && cs->get_base ? cs->get_base(cs) : NULL;
}
int fz_colorspace_n(fz_context *ctx, const fz_colorspace *cs)
{
return cs ? cs->n : 0;
}
const char *fz_colorspace_name(fz_context *ctx, const fz_colorspace *cs)
{
return cs ? cs->name : "";
}
static void
free_icc(fz_context *ctx, fz_colorspace *cs)
{
fz_iccprofile *profile = cs->data;
fz_drop_buffer(ctx, profile->buffer);
fz_cmm_fin_profile(ctx, profile);
fz_free(ctx, profile);
}
/* This could be different for a* b* */
static void
clamp_lab_icc(const fz_colorspace *cs, const float *src, float *dst)
{
dst[0] = (fz_clamp(src[0], 0, 100)) / 100.0;
dst[1] = (fz_clamp(src[1], -128, 127) + 128.0) / 256;
dst[2] = (fz_clamp(src[2], -128, 127) + 128.0) / 256;
}
/* Embedded icc profiles could have different range */
static void
clamp_default_icc(const fz_colorspace *cs, const float *src, float *dst)
{
int i;
fz_iccprofile *profile = cs->data;
for (i = 0; i < profile->num_devcomp; i++)
dst[i] = fz_clamp(src[i], 0, 1);
}
int fz_colorspace_is_icc(fz_context *ctx, const fz_colorspace *cs)
{
return cs && cs->free_data == free_icc;
}
int fz_colorspace_is_lab_icc(fz_context *ctx, const fz_colorspace *cs)
{
return cs && cs->clamp == clamp_lab_icc;
}
fz_colorspace *
fz_new_icc_colorspace(fz_context *ctx, int is_static, int num, fz_buffer *buf, const char *name)
{
fz_colorspace *cs = NULL;
fz_iccprofile *profile;
int is_lab = 0;
profile = fz_malloc_struct(ctx, fz_iccprofile);
fz_try(ctx)
{
profile->buffer = buf;
if (name != NULL)
{
size_t size;
const unsigned char *data;
data = fz_lookup_icc(ctx, name, &size);
profile->buffer = fz_new_buffer_from_shared_data(ctx, data, size);
is_lab = (strcmp(name, "lab-icc") == 0);
profile->bgr = (strcmp(name, "bgr-icc") == 0);
}
fz_cmm_init_profile(ctx, profile);
/* Check if correct type */
if (num != profile->num_devcomp)
{
if (name != NULL)
fz_drop_buffer(ctx, profile->buffer);
fz_cmm_fin_profile(ctx, profile);
fz_free(ctx, profile);
}
else
{
fz_keep_buffer(ctx, buf);
fz_md5_icc(ctx, profile);
cs = fz_new_colorspace(ctx, "icc", is_static, num, 0, NULL, NULL, NULL, is_lab ? clamp_lab_icc : clamp_default_icc, free_icc, profile, sizeof(profile));
}
}
fz_catch(ctx)
{
fz_drop_buffer(ctx, profile->buffer);
fz_cmm_fin_profile(ctx, profile);
fz_free(ctx, profile);
}
return cs;
}
/* Gets the icc data from a color space. Used in the writing out of the icc
* data for output formats.
*/
fz_buffer *
fz_icc_data_from_icc_colorspace(fz_context *ctx, const fz_colorspace *cs)
{
fz_iccprofile *profile;
if (cs == NULL || !fz_colorspace_is_icc(ctx, cs))
return NULL;
profile = cs->data;
if (!profile)
return NULL;
return profile->buffer;
}
static void
free_cal(fz_context *ctx, fz_colorspace *cs)
{
fz_cal_colorspace *cal_data = cs->data;
if (cal_data->profile != NULL)
{
fz_drop_buffer(ctx, cal_data->profile->buffer);
fz_cmm_fin_profile(ctx, cal_data->profile);
fz_free(ctx, cal_data->profile);
}
fz_free(ctx, cal_data);
}
int fz_colorspace_is_cal(fz_context *ctx, const fz_colorspace *cs)
{
return cs && cs->free_data == free_cal;
}
/* Profile created if needed during draw command. */
fz_colorspace *
fz_new_cal_colorspace(fz_context *ctx, float *wp, float *bp, float *gamma, float *matrix)
{
fz_colorspace *cs = NULL;
int num = (matrix == NULL ? 1 : 3);
fz_cal_colorspace *cal_data = fz_malloc_struct(ctx, fz_cal_colorspace);
memcpy(&cal_data->bp, bp, sizeof(float) * 3);
memcpy(&cal_data->wp, wp, sizeof(float) * 3);
memcpy(&cal_data->gamma, gamma, sizeof(float) * num);
if (matrix != NULL)
memcpy(&cal_data->matrix, matrix, sizeof(float) * 9);
cal_data->n = num;
fz_try(ctx)
cs = fz_new_colorspace(ctx, "pdf-cal", 0, num, 0, NULL, NULL, NULL, NULL, free_cal, cal_data, sizeof(cal_data));
fz_catch(ctx)
{
fz_free(ctx, cal_data);
fz_rethrow(ctx);
}
return cs;
}
void
fz_clamp_color(fz_context *ctx, const fz_colorspace *cs, const float *in, float *out)
{
cs->clamp(cs, in, out);
}
/* Default CS. To handle the page specific default settings that PDF can do in
* its page resource dictionary as well as the output intent. Both need to
* to be accessible by the device on the other side of the display list.
* Same with the output intent. */
void
fz_set_default_gray(fz_context *ctx, fz_default_colorspaces *default_cs, fz_colorspace *cs)
{
if (cs->n == 1)
{
fz_drop_colorspace(ctx, default_cs->gray);
default_cs->gray = fz_keep_colorspace(ctx, cs);
}
}
void
fz_set_default_rgb(fz_context *ctx, fz_default_colorspaces *default_cs, fz_colorspace *cs)
{
if (cs->n == 3)
{
fz_drop_colorspace(ctx, default_cs->rgb);
default_cs->rgb = fz_keep_colorspace(ctx, cs);
}
}
void
fz_set_default_cmyk(fz_context *ctx, fz_default_colorspaces *default_cs, fz_colorspace *cs)
{
if (cs->n == 4)
{
fz_drop_colorspace(ctx, default_cs->cmyk);
default_cs->cmyk = fz_keep_colorspace(ctx, cs);
}
}
void
fz_set_default_output_intent(fz_context *ctx, fz_default_colorspaces *default_cs, fz_colorspace *cs)
{
fz_drop_colorspace(ctx, default_cs->oi);
default_cs->oi = fz_keep_colorspace(ctx, cs);
switch (cs->n)
{
case 1:
fz_drop_colorspace(ctx, default_cs->gray);
default_cs->gray = fz_keep_colorspace(ctx, cs);
break;
case 3:
fz_drop_colorspace(ctx, default_cs->rgb);
default_cs->rgb = fz_keep_colorspace(ctx, cs);
break;
case 4:
fz_drop_colorspace(ctx, default_cs->cmyk);
default_cs->cmyk = fz_keep_colorspace(ctx, cs);
break;
}
}
fz_colorspace *
fz_default_gray(fz_context *ctx, fz_default_colorspaces *default_cs)
{
if (default_cs)
return default_cs->gray;
else
return fz_device_gray(ctx);
}
fz_colorspace *
fz_default_rgb(fz_context *ctx, fz_default_colorspaces *default_cs)
{
if (default_cs)
return default_cs->rgb;
else
return fz_device_rgb(ctx);
}
fz_colorspace *
fz_default_cmyk(fz_context *ctx, fz_default_colorspaces *default_cs)
{
if (default_cs)
return default_cs->cmyk;
else
return fz_device_cmyk(ctx);
}
fz_colorspace *
fz_default_output_intent(fz_context *ctx, fz_default_colorspaces *default_cs)
{
if (default_cs)
return default_cs->oi;
else
return NULL;
}
fz_default_colorspaces *
fz_new_default_colorspaces(fz_context *ctx)
{
fz_default_colorspaces *default_cs = fz_malloc_struct(ctx, fz_default_colorspaces);
default_cs->refs = 1;
default_cs->gray = fz_keep_colorspace(ctx, fz_device_gray(ctx));
default_cs->rgb = fz_keep_colorspace(ctx, fz_device_rgb(ctx));
default_cs->cmyk = fz_keep_colorspace(ctx, fz_device_cmyk(ctx));
default_cs->oi = NULL;
return default_cs;
}
fz_default_colorspaces *
fz_keep_default_colorspaces(fz_context *ctx, fz_default_colorspaces *default_cs)
{
return fz_keep_imp(ctx, default_cs, &default_cs->refs);
}
void
fz_drop_default_colorspaces(fz_context *ctx, fz_default_colorspaces *default_cs)
{
if (fz_drop_imp(ctx, default_cs, &default_cs->refs))
{
fz_drop_colorspace(ctx, default_cs->gray);
fz_drop_colorspace(ctx, default_cs->rgb);
fz_drop_colorspace(ctx, default_cs->cmyk);
fz_drop_colorspace(ctx, default_cs->oi);
fz_free(ctx, default_cs);
}
}
|