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
|
/* Copyright (C) 2001-2006 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied, modified
or distributed except as expressly authorized under the terms of that
license. Refer to licensing information at http://www.artifex.com/
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
*/
/* $Id$ */
/* Color mapping for Ghostscript */
#include "gx.h"
#include "gserrors.h"
#include "gsccolor.h"
#include "gxalpha.h"
#include "gxcspace.h"
#include "gxfarith.h"
#include "gxfrac.h"
#include "gxdcconv.h"
#include "gxdevice.h"
#include "gxcmap.h"
#include "gxlum.h"
#include "gzstate.h"
#include "gxdither.h"
#include "gxcdevn.h"
#include "string_.h"
#include "gsicc_manage.h"
#include "gdevdevn.h"
#include "gsicc_cache.h"
#include "gscms.h"
#include "gsicc.h"
/* Structure descriptor */
public_st_device_color();
static
ENUM_PTRS_WITH(device_color_enum_ptrs, gx_device_color *cptr)
{
return ENUM_USING(*cptr->type->stype, vptr, size, index);
}
ENUM_PTRS_END
static RELOC_PTRS_WITH(device_color_reloc_ptrs, gx_device_color *cptr)
{
RELOC_USING(*cptr->type->stype, vptr, size);
}
RELOC_PTRS_END
gx_color_index
gx_default_encode_color(gx_device * dev, const gx_color_value cv[])
{
int ncomps = dev->color_info.num_components;
int i;
const byte * comp_shift = dev->color_info.comp_shift;
const byte * comp_bits = dev->color_info.comp_bits;
gx_color_index color = 0;
#ifdef DEBUG
if ( dev->color_info.separable_and_linear != GX_CINFO_SEP_LIN ) {
dprintf( "gx_default_encode_color() requires separable and linear\n" );
return gx_no_color_index;
}
#endif
for (i = 0; i < ncomps; i++) {
COLROUND_VARS;
COLROUND_SETUP(comp_bits[i]);
color |= COLROUND_ROUND(cv[i]) << comp_shift[i];
}
return color;
}
/*
* This routine is only used if the device is 'separable'. See
* separable_and_linear in gxdevcli.h for more information.
*/
int
gx_default_decode_color(gx_device * dev, gx_color_index color, gx_color_value cv[])
{
int ncomps = dev->color_info.num_components;
int i;
const byte * comp_shift = dev->color_info.comp_shift;
const byte * comp_bits = dev->color_info.comp_bits;
const gx_color_index * comp_mask = dev->color_info.comp_mask;
uint shift, ivalue, nbits, scale;
#ifdef DEBUG
if ( dev->color_info.separable_and_linear != GX_CINFO_SEP_LIN ) {
dprintf( "gx_default_decode_color() requires separable and linear\n" );
return gs_error_rangecheck;
}
#endif
for (i = 0; i < ncomps; i++) {
/*
* Convert from the gx_color_index bits to a gx_color_value.
* Split the conversion into an integer and a fraction calculation
* so we can do integer arthmetic. The calculation is equivalent
* to floor(0xffff.fffff * ivalue / ((1 << nbits) - 1))
*/
nbits = comp_bits[i];
scale = gx_max_color_value / ((1 << nbits) - 1);
ivalue = (color & comp_mask[i]) >> comp_shift[i];
cv[i] = ivalue * scale;
/*
* Since our scaling factor is an integer, we lost the fraction.
* Determine what part of the ivalue that the faction would have
* added into the result.
*/
shift = nbits - (gx_color_value_bits % nbits);
cv[i] += ivalue >> shift;
}
return 0;
}
gx_color_index
gx_error_encode_color(gx_device * dev, const gx_color_value colors[])
{
#ifdef DEBUG
/* The "null" device is expected to be missing encode_color */
if (strcmp(dev->dname, "null") != 0)
dprintf("No encode_color proc defined for device.\n");
#endif
return gx_no_color_index;
}
int
gx_error_decode_color(gx_device * dev, gx_color_index cindex, gx_color_value colors[])
{
int i=dev->color_info.num_components;
#ifdef DEBUG
dprintf("No decode_color proc defined for device.\n");
#endif
for(; i>=0; i--)
colors[i] = 0;
return gs_error_rangecheck;
}
/*
* The "back-stop" default encode_color method. This will be used only
* if no applicable color encoding procedure is provided, and the number
* of color model components is 1. The encoding is presumed to induce an
* additive color model (DeviceGray).
*
* The particular method employed is a trivial generalization of the
* default map_rgb_color method used in the pre-DeviceN code (this was
* known as gx_default_w_b_map_rgb_color). Since the DeviceRGB color
* model is assumed additive, any of the procedures used as a default
* map_rgb_color method are assumed to induce an additive color model.
* gx_default_w_b_map_rgb_color mapped white to 1 and black to 0, so
* the new procedure is set up with zero-base and positive slope as well.
* The generalization is the use of depth; the earlier procedure assumed
* a bi-level device.
*
* Two versions of this procedure are provided, the first of which
* applies if max_gray == 2^depth - 1 and is faster, while the second
* applies to the general situation. Note that, as with the encoding
* procedures used in the pre-DeviceN code, both of these methods induce
* a small rounding error if 1 < depth < gx_color_value_bits.
*/
gx_color_index
gx_default_gray_fast_encode(gx_device * dev, const gx_color_value cv[])
{
COLROUND_VARS;
COLROUND_SETUP(dev->color_info.depth);
return COLROUND_ROUND(cv[0]);
}
gx_color_index
gx_default_gray_encode(gx_device * dev, const gx_color_value cv[])
{
return cv[0] * (dev->color_info.max_gray + 1) / (gx_max_color_value + 1);
}
/**
* This routine is provided for old devices which provide a
* map_rgb_color routine but not encode_color. New devices are
* encouraged either to use the defaults or to set encode_color rather
* than map_rgb_color.
**/
gx_color_index
gx_backwards_compatible_gray_encode(gx_device *dev,
const gx_color_value cv[])
{
gx_color_value gray_val = cv[0];
gx_color_value rgb_cv[3];
rgb_cv[0] = gray_val;
rgb_cv[1] = gray_val;
rgb_cv[2] = gray_val;
return (*dev_proc(dev, map_rgb_color))(dev, rgb_cv);
}
/* -------- Default color space to color model conversion routines -------- */
void
gray_cs_to_gray_cm(gx_device * dev, frac gray, frac out[])
{
out[0] = gray;
}
static void
rgb_cs_to_gray_cm(gx_device * dev, const gs_imager_state *pis,
frac r, frac g, frac b, frac out[])
{
out[0] = color_rgb_to_gray(r, g, b, NULL);
}
static void
cmyk_cs_to_gray_cm(gx_device * dev, frac c, frac m, frac y, frac k, frac out[])
{
out[0] = color_cmyk_to_gray(c, m, y, k, NULL);
}
static void
gray_cs_to_rgb_cm(gx_device * dev, frac gray, frac out[])
{
out[0] = out[1] = out[2] = gray;
}
void
rgb_cs_to_rgb_cm(gx_device * dev, const gs_imager_state *pis,
frac r, frac g, frac b, frac out[])
{
out[0] = r;
out[1] = g;
out[2] = b;
}
static void
cmyk_cs_to_rgb_cm(gx_device * dev, frac c, frac m, frac y, frac k, frac out[])
{
color_cmyk_to_rgb(c, m, y, k, NULL, out, dev->memory);
}
static void
gray_cs_to_rgbk_cm(gx_device * dev, frac gray, frac out[])
{
out[0] = out[1] = out[2] = frac_0;
out[3] = gray;
}
static void
rgb_cs_to_rgbk_cm(gx_device * dev, const gs_imager_state *pis,
frac r, frac g, frac b, frac out[])
{
if ((r == g) && (g == b)) {
out[0] = out[1] = out[2] = frac_0;
out[3] = r;
}
else {
out[0] = r;
out[1] = g;
out[2] = b;
out[3] = frac_0;
}
}
static void
cmyk_cs_to_rgbk_cm(gx_device * dev, frac c, frac m, frac y, frac k, frac out[])
{
frac rgb[3];
if ((c == frac_0) && (m == frac_0) && (y == frac_0)) {
out[0] = out[1] = out[2] = frac_0;
out[3] = frac_1 - k;
}
else {
color_cmyk_to_rgb(c, m, y, k, NULL, rgb, dev->memory);
rgb_cs_to_rgbk_cm(dev, NULL, rgb[0], rgb[1], rgb[2], out);
}
}
static void
gray_cs_to_cmyk_cm(gx_device * dev, frac gray, frac out[])
{
out[0] = out[1] = out[2] = frac_0;
out[3] = frac_1 - gray;
}
/*
* Default map from DeviceRGB color space to DeviceCMYK color
* model. Since this mapping is defined by the PostScript language
* it is unlikely that any device with a DeviceCMYK color model
* would define this mapping on its own.
*
* If the imager state is not available, map as though the black
* generation and undercolor removal functions are identity
* transformations. This mode is used primarily to support the
* raster operation (rop) feature of PCL, which requires that
* the raster operation be performed in an RGB color space.
* Note that default black generation and undercolor removal
* functions in PostScript need NOT be identity transformations:
* often they are { pop 0 }.
*/
static void
rgb_cs_to_cmyk_cm(gx_device * dev, const gs_imager_state *pis,
frac r, frac g, frac b, frac out[])
{
if (pis != 0)
color_rgb_to_cmyk(r, g, b, pis, out, dev->memory);
else {
frac c = frac_1 - r, m = frac_1 - g, y = frac_1 - b;
frac k = min(c, min(m, y));
out[0] = c - k;
out[1] = m - k;
out[2] = y - k;
out[3] = k;
}
}
void
cmyk_cs_to_cmyk_cm(gx_device * dev, frac c, frac m, frac y, frac k, frac out[])
{
out[0] = c;
out[1] = m;
out[2] = y;
out[3] = k;
}
/* The list of default color space to color model conversion routines. */
static const gx_cm_color_map_procs DeviceGray_procs = {
gray_cs_to_gray_cm, rgb_cs_to_gray_cm, cmyk_cs_to_gray_cm
};
static const gx_cm_color_map_procs DeviceRGB_procs = {
gray_cs_to_rgb_cm, rgb_cs_to_rgb_cm, cmyk_cs_to_rgb_cm
};
static const gx_cm_color_map_procs DeviceCMYK_procs = {
gray_cs_to_cmyk_cm, rgb_cs_to_cmyk_cm, cmyk_cs_to_cmyk_cm
};
static const gx_cm_color_map_procs DeviceRGBK_procs = {
gray_cs_to_rgbk_cm, rgb_cs_to_rgbk_cm, cmyk_cs_to_rgbk_cm
};
/*
* These are the default handlers for returning the list of color space
* to color model conversion routines.
*/
const gx_cm_color_map_procs *
gx_default_DevGray_get_color_mapping_procs(const gx_device * dev)
{
return &DeviceGray_procs;
}
const gx_cm_color_map_procs *
gx_default_DevRGB_get_color_mapping_procs(const gx_device * dev)
{
return &DeviceRGB_procs;
}
const gx_cm_color_map_procs *
gx_default_DevCMYK_get_color_mapping_procs(const gx_device * dev)
{
return &DeviceCMYK_procs;
}
const gx_cm_color_map_procs *
gx_default_DevRGBK_get_color_mapping_procs(const gx_device * dev)
{
return &DeviceRGBK_procs;
}
const gx_cm_color_map_procs *
gx_error_get_color_mapping_procs(const gx_device * dev)
{
/*
* We should never get here. If we do then we do not have a "get_color_mapping_procs"
* routine for the device. This will be noisy, but better than returning NULL which
* would lead to SEGV (Segmentation Fault) errors when this is used.
*/
emprintf1(dev->memory,
"No get_color_mapping_procs proc defined for device '%s'\n",
dev->dname);
switch (dev->color_info.num_components) {
case 1: /* DeviceGray or DeviceInvertGray */
return gx_default_DevGray_get_color_mapping_procs(dev);
case 3:
return gx_default_DevRGB_get_color_mapping_procs(dev);
case 4:
default: /* Unknown color model - punt with CMYK */
return gx_default_DevCMYK_get_color_mapping_procs(dev);
}
}
/* ----- Default color component name to colorant index conversion routines ------ */
#define compare_color_names(pname, name_size, name_str) \
(name_size == (int)strlen(name_str) && strncmp(pname, name_str, name_size) == 0)
/* Default color component to index for a DeviceGray color model */
int
gx_default_DevGray_get_color_comp_index(gx_device * dev, const char * pname,
int name_size, int component_type)
{
if (compare_color_names(pname, name_size, "Gray") ||
compare_color_names(pname, name_size, "Grey"))
return 0;
else
return -1; /* Indicate that the component name is "unknown" */
}
/* Default color component to index for a DeviceRGB color model */
int
gx_default_DevRGB_get_color_comp_index(gx_device * dev, const char * pname,
int name_size, int component_type)
{
if (compare_color_names(pname, name_size, "Red"))
return 0;
if (compare_color_names(pname, name_size, "Green"))
return 1;
if (compare_color_names(pname, name_size, "Blue"))
return 2;
else
return -1; /* Indicate that the component name is "unknown" */
}
/* Default color component to index for a DeviceCMYK color model */
int
gx_default_DevCMYK_get_color_comp_index(gx_device * dev, const char * pname,
int name_size, int component_type)
{
if (compare_color_names(pname, name_size, "Cyan"))
return 0;
if (compare_color_names(pname, name_size, "Magenta"))
return 1;
if (compare_color_names(pname, name_size, "Yellow"))
return 2;
if (compare_color_names(pname, name_size, "Black"))
return 3;
else
return -1; /* Indicate that the component name is "unknown" */
}
/* Default color component to index for a DeviceRGBK color model */
int
gx_default_DevRGBK_get_color_comp_index(gx_device * dev, const char * pname,
int name_size, int component_type)
{
if (compare_color_names(pname, name_size, "Red"))
return 0;
if (compare_color_names(pname, name_size, "Green"))
return 1;
if (compare_color_names(pname, name_size, "Blue"))
return 2;
if (compare_color_names(pname, name_size, "Black"))
return 3;
else
return -1; /* Indicate that the component name is "unknown" */
}
/* Default color component to index for an unknown color model */
int
gx_error_get_color_comp_index(gx_device * dev, const char * pname,
int name_size, int component_type)
{
/*
* We should never get here. If we do then we do not have a "get_color_comp_index"
* routine for the device.
*/
#ifdef DEBUG
dprintf("No get_color_comp_index proc defined for device.\n");
#endif
return -1; /* Always return "unknown" component name */
}
#undef compare_color_names
/* ---------------- Device color rendering ---------------- */
static cmap_proc_gray(cmap_gray_halftoned);
static cmap_proc_gray(cmap_gray_direct);
static cmap_proc_rgb(cmap_rgb_halftoned);
static cmap_proc_rgb(cmap_rgb_direct);
#define cmap_cmyk_halftoned cmap_cmyk_direct
static cmap_proc_cmyk(cmap_cmyk_direct);
static cmap_proc_rgb_alpha(cmap_rgb_alpha_halftoned);
static cmap_proc_rgb_alpha(cmap_rgb_alpha_direct);
/* Procedure names are only guaranteed unique to 23 characters.... */
static cmap_proc_rgb_alpha(cmap_rgb_alpha_halftoned);
static cmap_proc_rgb_alpha(cmap_rgb_alpha_direct);
static cmap_proc_separation(cmap_separation_halftoned);
static cmap_proc_separation(cmap_separation_direct);
static cmap_proc_devicen(cmap_devicen_halftoned);
static cmap_proc_devicen(cmap_devicen_direct);
static cmap_proc_is_halftoned(cmap_halftoned_is_halftoned);
static cmap_proc_is_halftoned(cmap_direct_is_halftoned);
static const gx_color_map_procs cmap_few = {
cmap_gray_halftoned,
cmap_rgb_halftoned,
cmap_cmyk_halftoned,
cmap_rgb_alpha_halftoned,
cmap_separation_halftoned,
cmap_devicen_halftoned,
cmap_halftoned_is_halftoned
};
static const gx_color_map_procs cmap_many = {
cmap_gray_direct,
cmap_rgb_direct,
cmap_cmyk_direct,
cmap_rgb_alpha_direct,
cmap_separation_direct,
cmap_devicen_direct,
cmap_direct_is_halftoned
};
const gx_color_map_procs *const cmap_procs_default = &cmap_many;
/* Determine the color mapping procedures for a device. */
/* Note that the default procedure doesn't consult the imager state. */
const gx_color_map_procs *
gx_get_cmap_procs(const gs_imager_state *pis, const gx_device * dev)
{
return (pis->get_cmap_procs)(pis, dev);
}
const gx_color_map_procs *
gx_default_get_cmap_procs(const gs_imager_state *pis, const gx_device * dev)
{
return (gx_device_must_halftone(dev) ? &cmap_few : &cmap_many);
}
/* Set the color mapping procedures in the graphics state. */
void
gx_set_cmap_procs(gs_imager_state * pis, const gx_device * dev)
{
pis->cmap_procs = gx_get_cmap_procs(pis, dev);
}
/* Remap the color in the graphics state. */
int
gx_remap_color(gs_state * pgs)
{
const gs_color_space *pcs = gs_currentcolorspace_inline(pgs);
int code;
/* The current color in the graphics state is always used for */
/* the texture, never for the source. */
code = (*pcs->type->remap_color) (gs_currentcolor_inline(pgs),
pcs, gs_currentdevicecolor_inline(pgs),
(gs_imager_state *) pgs, pgs->device,
gs_color_select_texture);
/* if overprint mode is in effect, update the overprint information */
if (code >= 0 && pgs->effective_overprint_mode == 1)
code = gs_do_set_overprint(pgs);
return code;
}
/* Indicate that a color space has no underlying concrete space. */
const gs_color_space *
gx_no_concrete_space(const gs_color_space * pcs, const gs_imager_state * pis)
{
return NULL;
}
/* Indicate that a color space is concrete. */
const gs_color_space *
gx_same_concrete_space(const gs_color_space * pcs, const gs_imager_state * pis)
{
return pcs;
}
/* Indicate that a color cannot be concretized. */
int
gx_no_concretize_color(const gs_client_color * pcc, const gs_color_space * pcs,
frac * pconc, const gs_imager_state * pis, gx_device *dev)
{
return_error(gs_error_rangecheck);
}
/* By default, remap a color by concretizing it and then */
/* remapping the concrete color. */
int
gx_default_remap_color(const gs_client_color * pcc, const gs_color_space * pcs,
gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
gs_color_select_t select)
{
frac conc[GS_CLIENT_COLOR_MAX_COMPONENTS];
const gs_color_space *pconcs;
int i = pcs->type->num_components(pcs);
int code = (*pcs->type->concretize_color)(pcc, pcs, conc, pis, dev);
if (code < 0)
return code;
pconcs = cs_concrete_space(pcs, pis);
code = (*pconcs->type->remap_concrete_color)(conc, pconcs, pdc, pis, dev, select);
/* Save original color space and color info into dev color */
i = any_abs(i);
for (i--; i >= 0; i--)
pdc->ccolor.paint.values[i] = pcc->paint.values[i];
pdc->ccolor_valid = true;
return code;
}
/* Color remappers for the standard color spaces. */
/* Note that we use D... instead of Device... in some places because */
/* gcc under VMS only retains 23 characters of procedure names. */
/* DeviceGray */
int
gx_concretize_DeviceGray(const gs_client_color * pc, const gs_color_space * pcs,
frac * pconc, const gs_imager_state * pis, gx_device *dev)
{
pconc[0] = gx_unit_frac(pc->paint.values[0]);
return 0;
}
int
gx_remap_concrete_DGray(const frac * pconc, const gs_color_space * pcs,
gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
gs_color_select_t select)
{
if (pis->alpha == gx_max_color_value)
(*pis->cmap_procs->map_gray)
(pconc[0], pdc, pis, dev, select);
else
(*pis->cmap_procs->map_rgb_alpha)
(pconc[0], pconc[0], pconc[0], cv2frac(pis->alpha),
pdc, pis, dev, select);
return 0;
}
int
gx_remap_DeviceGray(const gs_client_color * pc, const gs_color_space * pcs,
gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
gs_color_select_t select)
{
frac fgray = gx_unit_frac(pc->paint.values[0]);
int code;
/* We are in here due to the fact that we are using a color space that
was set in the graphic state before the ICC manager was intitialized
and the color space was never actually "installed" and hence set
over to a proper ICC color space. We will "install" this color space
at this time */
if (pis->icc_manager->default_gray != NULL) {
gs_color_space *pcs_notconst = (gs_color_space*) pcs;
gs_state *pgs = (gs_state*) pis;
pcs_notconst->cmm_icc_profile_data = pis->icc_manager->default_gray;
rc_increment(pis->icc_manager->default_gray);
pcs_notconst->type = &gs_color_space_type_ICC;
code =
(*pcs_notconst->type->remap_color)(gs_currentcolor_inline(pgs),
pcs_notconst,
gs_currentdevicecolor_inline(pgs),
pis, pgs->device,
gs_color_select_texture);
return code;
}
/* Save orgxiginal color space and color info into dev color */
pdc->ccolor.paint.values[0] = pc->paint.values[0];
pdc->ccolor_valid = true;
if (pis->alpha == gx_max_color_value)
(*pis->cmap_procs->map_gray)
(fgray, pdc, pis, dev, select);
else
(*pis->cmap_procs->map_rgb_alpha)
(fgray, fgray, fgray, cv2frac(pis->alpha), pdc, pis, dev, select);
return 0;
}
/* DeviceRGB */
int
gx_concretize_DeviceRGB(const gs_client_color * pc, const gs_color_space * pcs,
frac * pconc, const gs_imager_state * pis, gx_device *dev)
{
pconc[0] = gx_unit_frac(pc->paint.values[0]);
pconc[1] = gx_unit_frac(pc->paint.values[1]);
pconc[2] = gx_unit_frac(pc->paint.values[2]);
return 0;
}
int
gx_remap_concrete_DRGB(const frac * pconc, const gs_color_space * pcs,
gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
gs_color_select_t select)
{
if (pis->alpha == gx_max_color_value)
gx_remap_concrete_rgb(pconc[0], pconc[1], pconc[2],
pdc, pis, dev, select);
else
gx_remap_concrete_rgb_alpha(pconc[0], pconc[1], pconc[2],
cv2frac(pis->alpha),
pdc, pis, dev, select);
return 0;
}
int
gx_remap_DeviceRGB(const gs_client_color * pc, const gs_color_space * pcs,
gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
gs_color_select_t select)
{
frac fred = gx_unit_frac(pc->paint.values[0]), fgreen = gx_unit_frac(pc->paint.values[1]),
fblue = gx_unit_frac(pc->paint.values[2]);
/* Save original color space and color info into dev color */
pdc->ccolor.paint.values[0] = pc->paint.values[0];
pdc->ccolor.paint.values[1] = pc->paint.values[1];
pdc->ccolor.paint.values[2] = pc->paint.values[2];
pdc->ccolor_valid = true;
if (pis->alpha == gx_max_color_value)
gx_remap_concrete_rgb(fred, fgreen, fblue,
pdc, pis, dev, select);
else
gx_remap_concrete_rgb_alpha(fred, fgreen, fblue, cv2frac(pis->alpha),
pdc, pis, dev, select);
return 0;
}
/* DeviceCMYK */
int
gx_concretize_DeviceCMYK(const gs_client_color * pc, const gs_color_space * pcs,
frac * pconc, const gs_imager_state * pis, gx_device *dev)
{
pconc[0] = gx_unit_frac(pc->paint.values[0]);
pconc[1] = gx_unit_frac(pc->paint.values[1]);
pconc[2] = gx_unit_frac(pc->paint.values[2]);
pconc[3] = gx_unit_frac(pc->paint.values[3]);
return 0;
}
int
gx_remap_concrete_DCMYK(const frac * pconc, const gs_color_space * pcs,
gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
gs_color_select_t select)
{
/****** IGNORE alpha ******/
gx_remap_concrete_cmyk(pconc[0], pconc[1], pconc[2], pconc[3], pdc,
pis, dev, select, pcs);
return 0;
}
int
gx_remap_DeviceCMYK(const gs_client_color * pc, const gs_color_space * pcs,
gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
gs_color_select_t select)
{
/****** IGNORE alpha ******/
/* Save original color space and color info into dev color */
pdc->ccolor.paint.values[0] = pc->paint.values[0];
pdc->ccolor.paint.values[1] = pc->paint.values[1];
pdc->ccolor.paint.values[2] = pc->paint.values[2];
pdc->ccolor.paint.values[3] = pc->paint.values[3];
pdc->ccolor_valid = true;
gx_remap_concrete_cmyk(gx_unit_frac(pc->paint.values[0]),
gx_unit_frac(pc->paint.values[1]),
gx_unit_frac(pc->paint.values[2]),
gx_unit_frac(pc->paint.values[3]),
pdc, pis, dev, select, pcs);
return 0;
}
/* ------ Render Gray color. ------ */
static void
cmap_gray_halftoned(frac gray, gx_device_color * pdc,
const gs_imager_state * pis, gx_device * dev, gs_color_select_t select)
{
int i, ncomps = dev->color_info.num_components;
frac cm_comps[GX_DEVICE_COLOR_MAX_COMPONENTS];
/* map to the color model */
for (i=0; i < ncomps; i++)
cm_comps[i] = 0;
dev_proc(dev, get_color_mapping_procs)(dev)->map_gray(dev, gray, cm_comps);
/* apply the transfer function(s); convert to color values */
if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE)
for (i = 0; i < ncomps; i++)
cm_comps[i] = gx_map_color_frac(pis,
cm_comps[i], effective_transfer[i]);
else {
if (dev->color_info.opmode == GX_CINFO_OPMODE_UNKNOWN)
check_cmyk_color_model_comps(dev);
if (dev->color_info.opmode == GX_CINFO_OPMODE) { /* CMYK-like color space */
int k = dev->color_info.black_component;
for (i = 0; i < ncomps; i++) {
if (i == k)
cm_comps[i] = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[i]), effective_transfer[i]);
else
cm_comps[i] = cm_comps[i]; /* Ignore transfer, see PLRM3 p. 494 */
}
} else {
for (i = 0; i < ncomps; i++)
cm_comps[i] = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[i]), effective_transfer[i]);
}
}
if (gx_render_device_DeviceN(cm_comps, pdc, dev, pis->dev_ht,
&pis->screen_phase[select]) == 1)
gx_color_load_select(pdc, pis, dev, select);
}
static void
cmap_gray_direct(frac gray, gx_device_color * pdc, const gs_imager_state * pis,
gx_device * dev, gs_color_select_t select)
{
int i, ncomps = dev->color_info.num_components;
frac cm_comps[GX_DEVICE_COLOR_MAX_COMPONENTS];
gx_color_value cv[GX_DEVICE_COLOR_MAX_COMPONENTS];
gx_color_index color;
/* map to the color model */
for (i=0; i < ncomps; i++)
cm_comps[i] = 0;
dev_proc(dev, get_color_mapping_procs)(dev)->map_gray(dev, gray, cm_comps);
/* apply the transfer function(s); convert to color values */
if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE)
for (i = 0; i < ncomps; i++)
cv[i] = frac2cv(gx_map_color_frac(pis,
cm_comps[i], effective_transfer[i]));
else {
if (dev->color_info.opmode == GX_CINFO_OPMODE_UNKNOWN)
check_cmyk_color_model_comps(dev);
if (dev->color_info.opmode == GX_CINFO_OPMODE) { /* CMYK-like color space */
int k = dev->color_info.black_component;
for (i = 0; i < ncomps; i++) {
if (i == k)
cv[i] = frac2cv(frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[i]), effective_transfer[i]));
else
cv[i] = frac2cv(cm_comps[i]); /* Ignore transfer, see PLRM3 p. 494 */
}
} else {
for (i = 0; i < ncomps; i++)
cv[i] = frac2cv(frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[i]), effective_transfer[i]));
}
}
/* encode as a color index */
color = dev_proc(dev, encode_color)(dev, cv);
/* check if the encoding was successful; we presume failure is rare */
if (color != gx_no_color_index)
color_set_pure(pdc, color);
else
cmap_gray_halftoned(gray, pdc, pis, dev, select);
}
/* ------ Render RGB color. ------ */
static void
cmap_rgb_halftoned(frac r, frac g, frac b, gx_device_color * pdc,
const gs_imager_state * pis, gx_device * dev, gs_color_select_t select)
{
int i, ncomps = dev->color_info.num_components;
frac cm_comps[GX_DEVICE_COLOR_MAX_COMPONENTS];
/* map to the color model */
for (i=0; i < ncomps; i++)
cm_comps[i] = 0;
dev_proc(dev, get_color_mapping_procs)(dev)->map_rgb(dev, pis, r, g, b, cm_comps);
/* apply the transfer function(s); convert to color values */
if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE)
for (i = 0; i < ncomps; i++)
cm_comps[i] = gx_map_color_frac(pis,
cm_comps[i], effective_transfer[i]);
else
for (i = 0; i < ncomps; i++)
cm_comps[i] = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[i]), effective_transfer[i]);
if (gx_render_device_DeviceN(cm_comps, pdc, dev, pis->dev_ht,
&pis->screen_phase[select]) == 1)
gx_color_load_select(pdc, pis, dev, select);
}
static void
cmap_rgb_direct(frac r, frac g, frac b, gx_device_color * pdc,
const gs_imager_state * pis, gx_device * dev, gs_color_select_t select)
{
int i, ncomps = dev->color_info.num_components;
frac cm_comps[GX_DEVICE_COLOR_MAX_COMPONENTS];
gx_color_value cv[GX_DEVICE_COLOR_MAX_COMPONENTS];
gx_color_index color;
/* map to the color model */
for (i=0; i < ncomps; i++)
cm_comps[i] = 0;
dev_proc(dev, get_color_mapping_procs)(dev)->map_rgb(dev, pis, r, g, b, cm_comps);
/* apply the transfer function(s); convert to color values */
if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE)
for (i = 0; i < ncomps; i++)
cv[i] = frac2cv(gx_map_color_frac(pis,
cm_comps[i], effective_transfer[i]));
else
for (i = 0; i < ncomps; i++)
cv[i] = frac2cv(frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[i]), effective_transfer[i]));
/* encode as a color index */
color = dev_proc(dev, encode_color)(dev, cv);
/* check if the encoding was successful; we presume failure is rare */
if (color != gx_no_color_index)
color_set_pure(pdc, color);
else
cmap_rgb_halftoned(r, g, b, pdc, pis, dev, select);
}
/* ------ Render CMYK color. ------ */
static void
cmap_cmyk_direct(frac c, frac m, frac y, frac k, gx_device_color * pdc,
const gs_imager_state * pis, gx_device * dev, gs_color_select_t select,
const gs_color_space *source_pcs)
{
int i, ncomps = dev->color_info.num_components;
frac cm_comps[GX_DEVICE_COLOR_MAX_COMPONENTS];
gx_color_value cv[GX_DEVICE_COLOR_MAX_COMPONENTS];
gx_color_index color;
int black_index;
cmm_dev_profile_t *dev_profile;
gsicc_colorbuffer_t src_space = gsUNDEFINED;
int code;
bool gray_to_k;
/* map to the color model */
for (i=0; i < ncomps; i++)
cm_comps[i] = 0;
dev_proc(dev, get_color_mapping_procs)(dev)->map_cmyk(dev, c, m, y, k, cm_comps);
/* apply the transfer function(s); convert to color values */
if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE)
for (i = 0; i < ncomps; i++)
cm_comps[i] = gx_map_color_frac(pis,
cm_comps[i], effective_transfer[i]);
else {
/* Check if source space is gray. In this case we are to use only the
transfer function on the K channel. Do this only if gray to K is
also set */
code = dev_proc(dev, get_profile)(dev, &dev_profile);
gray_to_k = dev_profile->devicegraytok;
if (source_pcs != NULL && source_pcs->cmm_icc_profile_data != NULL) {
src_space = source_pcs->cmm_icc_profile_data->data_cs;
} else if (source_pcs != NULL && source_pcs->icc_equivalent != NULL) {
src_space = source_pcs->icc_equivalent->cmm_icc_profile_data->data_cs;
}
if (src_space == gsGRAY && gray_to_k) {
/* Find the black channel location */
black_index = dev_proc(dev, get_color_comp_index)(dev, "Black",
strlen("Black"), SEPARATION_NAME);
cm_comps[black_index] = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[black_index]),
effective_transfer[black_index]);
} else {
for (i = 0; i < ncomps; i++)
cm_comps[i] = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[i]), effective_transfer[i]);
}
}
/* We make a test for direct vs. halftoned, rather than */
/* duplicating most of the code of this procedure. */
if (gx_device_must_halftone(dev)) {
if (gx_render_device_DeviceN(cm_comps, pdc, dev,
pis->dev_ht, &pis->screen_phase[select]) == 1)
gx_color_load_select(pdc, pis, dev, select);
return;
}
for (i = 0; i < ncomps; i++)
cv[i] = frac2cv(cm_comps[i]);
color = dev_proc(dev, encode_color)(dev, cv);
if (color != gx_no_color_index)
color_set_pure(pdc, color);
else {
if (gx_render_device_DeviceN(cm_comps, pdc, dev,
pis->dev_ht, &pis->screen_phase[select]) == 1)
gx_color_load_select(pdc, pis, dev, select);
return;
}
}
static void
cmap_rgb_alpha_halftoned(frac r, frac g, frac b, frac alpha,
gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
gs_color_select_t select)
{
int i, ncomps = dev->color_info.num_components;
frac cm_comps[GX_DEVICE_COLOR_MAX_COMPONENTS];
/* map to the color model */
for (i=0; i < ncomps; i++)
cm_comps[i] = 0;
dev_proc(dev, get_color_mapping_procs)(dev)->map_rgb(dev, pis, r, g, b, cm_comps);
/* pre-multiply to account for the alpha weighting */
if (alpha != frac_1) {
#ifdef PREMULTIPLY_TOWARDS_WHITE
frac alpha_bias = frac_1 - alpha;
#else
frac alpha_bias = 0;
#endif
for (i = 0; i < ncomps; i++)
cm_comps[i] = (frac)((long)cm_comps[i] * alpha) / frac_1 + alpha_bias;
}
/* apply the transfer function(s); convert to color values */
if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE)
for (i = 0; i < ncomps; i++)
cm_comps[i] = gx_map_color_frac(pis,
cm_comps[i], effective_transfer[i]);
else
for (i = 0; i < ncomps; i++)
cm_comps[i] = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[i]), effective_transfer[i]);
if (gx_render_device_DeviceN(cm_comps, pdc, dev, pis->dev_ht,
&pis->screen_phase[select]) == 1)
gx_color_load_select(pdc, pis, dev, select);
}
static void
cmap_rgb_alpha_direct(frac r, frac g, frac b, frac alpha, gx_device_color * pdc,
const gs_imager_state * pis, gx_device * dev, gs_color_select_t select)
{
int i, ncomps = dev->color_info.num_components;
frac cm_comps[GX_DEVICE_COLOR_MAX_COMPONENTS];
gx_color_value cv_alpha, cv[GX_DEVICE_COLOR_MAX_COMPONENTS];
gx_color_index color;
/* map to the color model */
for (i=0; i < ncomps; i++)
cm_comps[i] = 0;
dev_proc(dev, get_color_mapping_procs)(dev)->map_rgb(dev, pis, r, g, b, cm_comps);
/* pre-multiply to account for the alpha weighting */
if (alpha != frac_1) {
#ifdef PREMULTIPLY_TOWARDS_WHITE
frac alpha_bias = frac_1 - alpha;
#else
frac alpha_bias = 0;
#endif
for (i = 0; i < ncomps; i++)
cm_comps[i] = (frac)((long)cm_comps[i] * alpha) / frac_1 + alpha_bias;
}
/* apply the transfer function(s); convert to color values */
if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE)
for (i = 0; i < ncomps; i++)
cv[i] = frac2cv(gx_map_color_frac(pis,
cm_comps[i], effective_transfer[i]));
else
for (i = 0; i < ncomps; i++)
cv[i] = frac2cv(frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[i]), effective_transfer[i]));
/* encode as a color index */
if (dev_proc(dev, map_rgb_alpha_color) != gx_default_map_rgb_alpha_color &&
(cv_alpha = frac2cv(alpha)) != gx_max_color_value)
color = dev_proc(dev, map_rgb_alpha_color)(dev, cv[0], cv[1], cv[2], cv_alpha);
else
color = dev_proc(dev, encode_color)(dev, cv);
/* check if the encoding was successful; we presume failure is rare */
if (color != gx_no_color_index)
color_set_pure(pdc, color);
else
cmap_rgb_alpha_halftoned(r, g, b, alpha, pdc, pis, dev, select);
}
/* ------ Render Separation All color. ------ */
/*
* This routine maps DeviceN components into the order of the device's
* colorants.
*
* Parameters:
* pcc - Pointer to DeviceN components.
* pcolor_component_map - Map from DeviceN to the Devices colorants.
* A negative value indicates component is not to be mapped.
* plist - Pointer to list for mapped components
*
* Returns:
* Mapped components in plist.
*/
static inline void
map_components_to_colorants(const frac * pcc,
const gs_devicen_color_map * pcolor_component_map, frac * plist)
{
int i = pcolor_component_map->num_colorants - 1;
int pos;
/* Clear all output colorants first */
for (; i >= 0; i--) {
plist[i] = frac_0;
}
/* Map color components into output list */
for (i = pcolor_component_map->num_components - 1; i >= 0; i--) {
pos = pcolor_component_map->color_map[i];
if (pos >= 0)
plist[pos] = pcc[i];
}
}
static void
cmap_separation_halftoned(frac all, gx_device_color * pdc,
const gs_imager_state * pis, gx_device * dev, gs_color_select_t select)
{
int i, ncomps = dev->color_info.num_components;
bool additive = dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE;
frac comp_value = all;
frac cm_comps[GX_DEVICE_COLOR_MAX_COMPONENTS];
for (i=0; i < ncomps; i++)
cm_comps[i] = 0;
if (pis->color_component_map.sep_type == SEP_ALL) {
/*
* Invert the photometric interpretation for additive
* color spaces because separations are always subtractive.
*/
if (additive)
comp_value = frac_1 - comp_value;
/* Use the "all" value for all components */
i = pis->color_component_map.num_colorants - 1;
for (; i >= 0; i--)
cm_comps[i] = comp_value;
}
else {
/* map to the color model */
map_components_to_colorants(&all, &(pis->color_component_map), cm_comps);
}
/* apply the transfer function(s); convert to color values */
if (additive)
for (i = 0; i < ncomps; i++)
cm_comps[i] = gx_map_color_frac(pis,
cm_comps[i], effective_transfer[i]);
else
for (i = 0; i < ncomps; i++)
cm_comps[i] = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[i]), effective_transfer[i]);
if (gx_render_device_DeviceN(cm_comps, pdc, dev, pis->dev_ht,
&pis->screen_phase[select]) == 1)
gx_color_load_select(pdc, pis, dev, select);
}
static void
cmap_separation_direct(frac all, gx_device_color * pdc, const gs_imager_state * pis,
gx_device * dev, gs_color_select_t select)
{
int i, ncomps = dev->color_info.num_components;
bool additive = dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE;
frac comp_value = all;
frac cm_comps[GX_DEVICE_COLOR_MAX_COMPONENTS];
gx_color_value cv[GX_DEVICE_COLOR_MAX_COMPONENTS];
gx_color_index color;
bool use_rgb2dev_icc = false;
gsicc_rendering_intents_t rendering_intent;
int code;
cmm_dev_profile_t *dev_profile = NULL;
cmm_profile_t *des_profile = NULL;
code = dev_proc(dev, get_profile)(dev, &dev_profile);
gsicc_extract_profile(dev->graphics_type_tag,
dev_profile, &des_profile,
&rendering_intent);
for (i=0; i < ncomps; i++)
cm_comps[i] = 0;
if (pis->color_component_map.sep_type == SEP_ALL) {
/*
* Invert the photometric interpretation for additive
* color spaces because separations are always subtractive.
*/
if (additive)
comp_value = frac_1 - comp_value;
/* Use the "all" value for all components */
i = pis->color_component_map.num_colorants - 1;
for (; i >= 0; i--)
cm_comps[i] = comp_value;
/* If our device space is CIELAB then we really want to treat this
as RGB during the fill up here of the separation value and then
go ahead and convert from RGB to CIELAB. The PDF spec is not clear
on how addivite devices should behave with the ALL option but it
is clear from testing the AR 10 does simply do the RGB = 1 - INK
type of mapping */
if (des_profile->data_cs == gsCIELAB) {
use_rgb2dev_icc = true;
}
}
else {
/* map to the color model */
map_components_to_colorants(&comp_value, &(pis->color_component_map), cm_comps);
}
/* apply the transfer function(s); convert to color values */
if (additive)
for (i = 0; i < ncomps; i++)
cv[i] = frac2cv(gx_map_color_frac(pis,
cm_comps[i], effective_transfer[i]));
else
for (i = 0; i < ncomps; i++)
cv[i] = frac2cv(frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[i]), effective_transfer[i]));
if (use_rgb2dev_icc && pis->icc_manager->default_rgb != NULL) {
/* After the transfer function go ahead and do the mapping from RGB to
the device profile. */
gsicc_link_t *icc_link;
gsicc_rendering_param_t rendering_params;
unsigned short psrc[GS_CLIENT_COLOR_MAX_COMPONENTS], psrc_cm[GS_CLIENT_COLOR_MAX_COMPONENTS];
rendering_params.black_point_comp = BP_ON;
rendering_params.graphics_type_tag = GS_PATH_TAG;
rendering_params.rendering_intent = pis->renderingintent;
icc_link = gsicc_get_link_profile(pis, dev, pis->icc_manager->default_rgb,
des_profile, &rendering_params,
pis->memory, dev_profile->devicegraytok);
/* Transform the color */
for (i = 0; i < ncomps; i++) {
psrc[i] = cv[i];
}
(icc_link->procs.map_color)(dev, icc_link, &(psrc[0]), &(psrc_cm[0]), 2);
gsicc_release_link(icc_link);
for (i = 0; i < ncomps; i++) {
cv[i] = psrc_cm[i];
}
}
/* encode as a color index */
color = dev_proc(dev, encode_color)(dev, cv);
/* check if the encoding was successful; we presume failure is rare */
if (color != gx_no_color_index)
color_set_pure(pdc, color);
else
cmap_separation_halftoned(all, pdc, pis, dev, select);
}
/* Routines for handling CM of CMYK components of a DeviceN color space */
static bool
devicen_has_cmyk(gx_device * dev)
{
gs_devn_params *devn_params;
/* Device may not have ret_devn_params! */
if (dev->procs.ret_devn_params != NULL) {
devn_params = dev_proc(dev, ret_devn_params)(dev);
} else {
return false;
}
if (devn_params == NULL) {
return false;
}
return(devn_params->num_std_colorant_names == 4);
}
static int
devicen_icc_cmyk(frac cm_comps[], const gs_imager_state * pis, gx_device *dev)
{
gsicc_link_t *icc_link;
gsicc_rendering_param_t rendering_params;
unsigned short psrc[GS_CLIENT_COLOR_MAX_COMPONENTS];
unsigned short psrc_cm[GS_CLIENT_COLOR_MAX_COMPONENTS];
int k;
unsigned short *psrc_temp;
gsicc_rendering_intents_t rendering_intent;
int code;
cmm_dev_profile_t *dev_profile = NULL;
cmm_profile_t *des_profile = NULL;
code = dev_proc(dev, get_profile)(dev, &dev_profile);
gsicc_extract_profile(dev->graphics_type_tag,
dev_profile, &des_profile,
&rendering_intent);
/* Define the rendering intents. */
rendering_params.black_point_comp = BP_ON;
rendering_params.graphics_type_tag = GS_PATH_TAG;
rendering_params.rendering_intent = pis->renderingintent;
/* Sigh, frac to full 16 bit. Need to clean this up */
for (k = 0; k < 4; k++){
psrc[k] = frac2cv(cm_comps[k]);
}
icc_link = gsicc_get_link_profile(pis, dev, pis->icc_manager->default_cmyk,
des_profile, &rendering_params, pis->memory,
dev_profile->devicegraytok);
/* Transform the color */
if (icc_link->is_identity) {
psrc_temp = &(psrc[0]);
} else {
/* Transform the color */
psrc_temp = &(psrc_cm[0]);
(icc_link->procs.map_color)(dev, icc_link, psrc, psrc_temp, 2);
}
/* This needs to be optimized */
for (k = 0; k < 4; k++){
cm_comps[k] = float2frac(((float) psrc_temp[k])/65535.0);
}
/* Release the link */
gsicc_release_link(icc_link);
return(0);
}
/* ------ DeviceN color mapping */
/*
* This routine is called to map a DeviceN colorspace to a DeviceN
* output device which requires halftoning. T
*/
static void
cmap_devicen_halftoned(const frac * pcc,
gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
gs_color_select_t select)
{
int i, ncomps = dev->color_info.num_components;
frac cm_comps[GX_DEVICE_COLOR_MAX_COMPONENTS];
int code;
gsicc_rendering_intents_t rendering_intent;
cmm_dev_profile_t *dev_profile = NULL;
cmm_profile_t *des_profile = NULL;
code = dev_proc(dev, get_profile)(dev, &dev_profile);
gsicc_extract_profile(dev->graphics_type_tag,
dev_profile, &des_profile,
&rendering_intent);
/* map to the color model */
for (i=0; i < ncomps; i++)
cm_comps[i] = 0;
map_components_to_colorants(pcc, &(pis->color_component_map), cm_comps);
/* See comments in cmap_devicen_direct for details on below operations */
if (devicen_has_cmyk(dev) &&
des_profile->data_cs == gsCMYK) {
code = devicen_icc_cmyk(cm_comps, pis, dev);
}
/* apply the transfer function(s); convert to color values */
if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE)
for (i = 0; i < ncomps; i++)
cm_comps[i] = gx_map_color_frac(pis,
cm_comps[i], effective_transfer[i]);
else
for (i = 0; i < ncomps; i++)
cm_comps[i] = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[i]), effective_transfer[i]);
/* We need to finish halftoning */
if (gx_render_device_DeviceN(cm_comps, pdc, dev, pis->dev_ht,
&pis->screen_phase[select]) == 1)
gx_color_load_select(pdc, pis, dev, select);
}
/*
* This routine is called to map a DeviceN colorspace to a DeviceN
* output device which does not require halftoning.
*/
static void
cmap_devicen_direct(const frac * pcc,
gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
gs_color_select_t select)
{
int i, ncomps = dev->color_info.num_components;
frac cm_comps[GX_DEVICE_COLOR_MAX_COMPONENTS];
gx_color_value cv[GX_DEVICE_COLOR_MAX_COMPONENTS];
gx_color_index color;
int code;
gsicc_rendering_intents_t rendering_intent;
cmm_dev_profile_t *dev_profile = NULL;
cmm_profile_t *des_profile = NULL;
code = dev_proc(dev, get_profile)(dev, &dev_profile);
gsicc_extract_profile(dev->graphics_type_tag,
dev_profile, &des_profile,
&rendering_intent);
/* See the comment below */
/* map to the color model */
for (i=0; i < ncomps; i++)
cm_comps[i] = 0;
map_components_to_colorants(pcc, &(pis->color_component_map), cm_comps);;
/* Check if we have the standard colorants. If yes, then we will apply
ICC color management to those colorants. To understand why, consider
the example where I have a Device with CMYK + O and I have a
DeviceN color in the document that is specified for any set of
these colorants, and suppose that I let them pass through
witout any color management. This is probably not the
desired effect since I could have a DeviceN color fill that had 10% C,
20% M 0% Y 0% K and 0% O. I would like this to look the same
as a CMYK color that will be color managed and specified with 10% C,
20% M 0% Y 0% K. Hence the CMYK values should go through the same
color management as a stand alone CMYK value. */
if (devicen_has_cmyk(dev) && des_profile->data_cs == gsCMYK) {
/* We need to do a CMYK to CMYK conversion here. This will always
use the default CMYK profile and the device's output profile.
We probably need to add some checking here
and possibly permute the colorants, much as is done on the input
side for the case when we add DeviceN icc source profiles for use
in PDF and PS data. */
code = devicen_icc_cmyk(cm_comps, pis, dev);
}
/* apply the transfer function(s); convert to color values */
if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE)
for (i = 0; i < ncomps; i++)
cv[i] = frac2cv(gx_map_color_frac(pis,
cm_comps[i], effective_transfer[i]));
else
for (i = 0; i < ncomps; i++)
cv[i] = frac2cv(frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - cm_comps[i]), effective_transfer[i]));
/* encode as a color index */
color = dev_proc(dev, encode_color)(dev, cv);
/* check if the encoding was successful; we presume failure is rare */
if (color != gx_no_color_index)
color_set_pure(pdc, color);
else
cmap_devicen_halftoned(pcc, pdc, pis, dev, select);
}
/* ------ Halftoning check ----- */
static bool
cmap_halftoned_is_halftoned(const gs_imager_state * pis, gx_device * dev)
{
return true;
}
static bool
cmap_direct_is_halftoned(const gs_imager_state * pis, gx_device * dev)
{
return false;
}
/* ------ Transfer function mapping ------ */
/* Define an identity transfer function. */
float
gs_identity_transfer(floatp value, const gx_transfer_map * pmap)
{
return (float) value;
}
/* Define the generic transfer function for the library layer. */
/* This just returns what's already in the map. */
float
gs_mapped_transfer(floatp value, const gx_transfer_map * pmap)
{
return gx_map_color_float(pmap, value);
}
/* Set a transfer map to the identity map. */
void
gx_set_identity_transfer(gx_transfer_map *pmap)
{
int i;
pmap->proc = gs_identity_transfer;
/* We still have to fill in the cached values. */
for (i = 0; i < transfer_map_size; ++i)
pmap->values[i] = bits2frac(i, log2_transfer_map_size);
}
#if FRAC_MAP_INTERPOLATE /* NOTA BENE */
/* Map a color fraction through a transfer map. */
/* We only use this if we are interpolating. */
frac
gx_color_frac_map(frac cv, const frac * values)
{
#define cp_frac_bits (frac_bits - log2_transfer_map_size)
int cmi = frac2bits_floor(cv, log2_transfer_map_size);
frac mv = values[cmi];
int rem, mdv;
/* Interpolate between two adjacent values if needed. */
rem = cv - bits2frac(cmi, log2_transfer_map_size);
if (rem == 0)
return mv;
mdv = values[cmi + 1] - mv;
#if ARCH_INTS_ARE_SHORT
/* Only use long multiplication if necessary. */
if (mdv < -1 << (16 - cp_frac_bits) ||
mdv > 1 << (16 - cp_frac_bits)
)
return mv + (uint) (((ulong) rem * mdv) >> cp_frac_bits);
#endif
return mv + ((rem * mdv) >> cp_frac_bits);
#undef cp_frac_bits
}
#endif /* FRAC_MAP_INTERPOLATE */
/* ------ Default device color mapping ------ */
/* White-on-black */
gx_color_index
gx_default_w_b_map_rgb_color(gx_device * dev, const gx_color_value cv[])
{ /* Map values >= 1/2 to 1, < 1/2 to 0. */
int i, ncomps = dev->color_info.num_components;
gx_color_value cv_all = 0;
for (i = 0; i < ncomps; i++)
cv_all |= cv[i];
return cv_all > gx_max_color_value / 2 ? (gx_color_index)1
: (gx_color_index)0;
}
int
gx_default_w_b_map_color_rgb(gx_device * dev, gx_color_index color,
gx_color_value prgb[3])
{ /* Map 1 to max_value, 0 to 0. */
prgb[0] = prgb[1] = prgb[2] = -(gx_color_value) color;
return 0;
}
/* Black-on-white */
gx_color_index
gx_default_b_w_map_rgb_color(gx_device * dev, const gx_color_value cv[])
{
int i, ncomps = dev->color_info.num_components;
gx_color_value cv_all = 0;
for (i = 0; i < ncomps; i++)
cv_all |= cv[i];
return cv_all > gx_max_color_value / 2 ? (gx_color_index)0
: (gx_color_index)1;
}
int
gx_default_b_w_map_color_rgb(gx_device * dev, gx_color_index color,
gx_color_value prgb[3])
{ /* Map 0 to max_value, 1 to 0. */
prgb[0] = prgb[1] = prgb[2] = -((gx_color_value) color ^ 1);
return 0;
}
/* RGB mapping for gray-scale devices */
gx_color_index
gx_default_gray_map_rgb_color(gx_device * dev, const gx_color_value cv[])
{ /* We round the value rather than truncating it. */
gx_color_value gray =
(((cv[0] * (ulong) lum_red_weight) +
(cv[1] * (ulong) lum_green_weight) +
(cv[2] * (ulong) lum_blue_weight) +
(lum_all_weights / 2)) / lum_all_weights
* dev->color_info.max_gray +
(gx_max_color_value / 2)) / gx_max_color_value;
return gray;
}
int
gx_default_gray_map_color_rgb(gx_device * dev, gx_color_index color,
gx_color_value prgb[3])
{
gx_color_value gray = (gx_color_value)
(color * gx_max_color_value / dev->color_info.max_gray);
prgb[0] = gray;
prgb[1] = gray;
prgb[2] = gray;
return 0;
}
gx_color_index
gx_default_8bit_map_gray_color(gx_device * dev, const gx_color_value cv[])
{
gx_color_index color = gx_color_value_to_byte(cv[0]);
return (color == gx_no_color_index ? color ^ 1 : color);
}
int
gx_default_8bit_map_color_gray(gx_device * dev, gx_color_index color,
gx_color_value pgray[1])
{
pgray[0] = (gx_color_value)(color * gx_max_color_value / 255);
return 0;
}
/* RGB mapping for 24-bit true (RGB) color devices */
gx_color_index
gx_default_rgb_map_rgb_color(gx_device * dev, const gx_color_value cv[])
{
if (dev->color_info.depth == 24)
return gx_color_value_to_byte(cv[2]) +
((uint) gx_color_value_to_byte(cv[1]) << 8) +
((ulong) gx_color_value_to_byte(cv[0]) << 16);
else {
COLROUND_VARS;
int bpc = dev->color_info.depth / 3;
COLROUND_SETUP(bpc);
return (((COLROUND_ROUND(cv[0]) << bpc) +
COLROUND_ROUND(cv[1])) << bpc) +
COLROUND_ROUND(cv[2]);
}
}
/* Map a color index to a r-g-b color. */
int
gx_default_rgb_map_color_rgb(gx_device * dev, gx_color_index color,
gx_color_value prgb[3])
{
if (dev->color_info.depth == 24) {
prgb[0] = gx_color_value_from_byte(color >> 16);
prgb[1] = gx_color_value_from_byte((color >> 8) & 0xff);
prgb[2] = gx_color_value_from_byte(color & 0xff);
} else {
uint bits_per_color = dev->color_info.depth / 3;
uint color_mask = (1 << bits_per_color) - 1;
prgb[0] = ((color >> (bits_per_color * 2)) & color_mask) *
(ulong) gx_max_color_value / color_mask;
prgb[1] = ((color >> (bits_per_color)) & color_mask) *
(ulong) gx_max_color_value / color_mask;
prgb[2] = (color & color_mask) *
(ulong) gx_max_color_value / color_mask;
}
return 0;
}
/* CMYK mapping for RGB devices (should never be called!) */
gx_color_index
gx_default_map_cmyk_color(gx_device * dev, const gx_color_value cv[])
{ /* Convert to RGB */
frac rgb[3];
gx_color_value rgb_cv[3];
color_cmyk_to_rgb(cv2frac(cv[0]), cv2frac(cv[1]), cv2frac(cv[2]), cv2frac(cv[3]),
NULL, rgb, dev->memory);
rgb_cv[0] = frac2cv(rgb[0]);
rgb_cv[1] = frac2cv(rgb[1]);
rgb_cv[2] = frac2cv(rgb[2]);
return (*dev_proc(dev, map_rgb_color)) (dev, rgb_cv);
}
/* Mapping for CMYK devices */
gx_color_index
cmyk_1bit_map_cmyk_color(gx_device * dev, const gx_color_value cv[])
{
#define CV_BIT(v) ((v) >> (gx_color_value_bits - 1))
return (gx_color_index)
(CV_BIT(cv[3]) + (CV_BIT(cv[2]) << 1) + (CV_BIT(cv[1]) << 2) + (CV_BIT(cv[0]) << 3));
#undef CV_BIT
}
/* Shouldn't be called: decode_color should be cmyk_1bit_map_color_cmyk */
int
cmyk_1bit_map_color_rgb(gx_device * dev, gx_color_index color,
gx_color_value prgb[3])
{
if (color & 1)
prgb[0] = prgb[1] = prgb[2] = 0;
else {
prgb[0] = (color & 8 ? 0 : gx_max_color_value);
prgb[1] = (color & 4 ? 0 : gx_max_color_value);
prgb[2] = (color & 2 ? 0 : gx_max_color_value);
}
return 0;
}
int
cmyk_1bit_map_color_cmyk(gx_device * dev, gx_color_index color,
gx_color_value pcv[4])
{
pcv[0] = (color & 8 ? 0 : gx_max_color_value);
pcv[1] = (color & 4 ? 0 : gx_max_color_value);
pcv[2] = (color & 2 ? 0 : gx_max_color_value);
pcv[3] = (color & 1 ? 0 : gx_max_color_value);
return 0;
}
gx_color_index
cmyk_8bit_map_cmyk_color(gx_device * dev, const gx_color_value cv[])
{
gx_color_index color =
gx_color_value_to_byte(cv[3]) +
((uint)gx_color_value_to_byte(cv[2]) << 8) +
((uint)gx_color_value_to_byte(cv[1]) << 16) +
((uint)gx_color_value_to_byte(cv[0]) << 24);
return (color == gx_no_color_index ? color ^ 1 : color);
}
gx_color_index
cmyk_16bit_map_cmyk_color(gx_device * dev, const gx_color_value cv[])
{
gx_color_index color =
(uint64_t)cv[3] +
((uint64_t)cv[2] << 16) +
((uint64_t)cv[1] << 32) +
((uint64_t)cv[0] << 48);
return (color == gx_no_color_index ? color ^ 1 : color);
}
/* Shouldn't be called: decode_color should be cmyk_8bit_map_color_cmyk */
int
cmyk_8bit_map_color_rgb(gx_device * dev, gx_color_index color,
gx_color_value prgb[3])
{
int
not_k = (int) (~color & 0xff),
r = not_k - (int) (color >> 24),
g = not_k - (int) ((color >> 16) & 0xff),
b = not_k - (int) ((color >> 8) & 0xff);
prgb[0] = (r < 0 ? 0 : gx_color_value_from_byte(r));
prgb[1] = (g < 0 ? 0 : gx_color_value_from_byte(g));
prgb[2] = (b < 0 ? 0 : gx_color_value_from_byte(b));
return 0;
}
int
cmyk_8bit_map_color_cmyk(gx_device * dev, gx_color_index color,
gx_color_value pcv[4])
{
pcv[0] = gx_color_value_from_byte((color >> 24) & 0xff);
pcv[1] = gx_color_value_from_byte((color >> 16) & 0xff);
pcv[2] = gx_color_value_from_byte((color >> 8) & 0xff);
pcv[3] = gx_color_value_from_byte(color & 0xff);
return 0;
}
int
cmyk_16bit_map_color_cmyk(gx_device * dev, gx_color_index color,
gx_color_value pcv[4])
{
pcv[0] = ((color >> 24) >> 24) & 0xffff;
pcv[1] = ((color >> 16) >> 16) & 0xffff;
pcv[2] = ( color >> 16) & 0xffff;
pcv[3] = ( color ) & 0xffff;
return 0;
}
/* Default mapping between RGB+alpha and RGB. */
gx_color_index
gx_default_map_rgb_alpha_color(gx_device * dev,
gx_color_value r, gx_color_value g, gx_color_value b, gx_color_value alpha)
{ /* Colors have been premultiplied: we don't need to do it here. */
gx_color_value cv[3];
cv[0] = r; cv[1] = g; cv[2] = b;
return (*dev_proc(dev, map_rgb_color))(dev, cv);
}
int
gx_default_map_color_rgb_alpha(gx_device * dev, gx_color_index color,
gx_color_value prgba[4])
{
prgba[3] = gx_max_color_value; /* alpha = 1 */
return (*dev_proc(dev, map_color_rgb)) (dev, color, prgba);
}
frac
gx_unit_frac(float fvalue)
{
frac f = frac_0;
if (is_fneg(fvalue))
f = frac_0;
else if (is_fge1(fvalue))
f = frac_1;
else
f = float2frac(fvalue);
return f;
}
/* This is used by image color render to handle the cases where we need to
perform either a transfer function or halftoning on the color values
during an ICC color flow. In this case, the color is already in the
device color space but in 16bpp color values. */
void
cmap_transfer_halftone(gx_color_value *pconc, gx_device_color * pdc,
const gs_imager_state * pis, gx_device * dev, bool has_transfer,
bool has_halftone, gs_color_select_t select)
{
int ncomps = dev->color_info.num_components;
frac frac_value;
int i;
frac cv_frac[GX_DEVICE_COLOR_MAX_COMPONENTS];
gx_color_index color;
gx_color_value color_val[GX_DEVICE_COLOR_MAX_COMPONENTS];
/* apply the transfer function(s) */
if (has_transfer) {
if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE) {
for (i = 0; i < ncomps; i++) {
frac_value = cv2frac(pconc[i]);
cv_frac[i] = gx_map_color_frac(pis,
frac_value, effective_transfer[i]);
}
} else {
if (dev->color_info.opmode == GX_CINFO_OPMODE_UNKNOWN) {
check_cmyk_color_model_comps(dev);
}
if (dev->color_info.opmode == GX_CINFO_OPMODE) { /* CMYK-like color space */
int k = dev->color_info.black_component;
for (i = 0; i < ncomps; i++) {
frac_value = cv2frac(pconc[i]);
if (i == k) {
cv_frac[i] = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - frac_value), effective_transfer[i]);
} else {
cv_frac[i] = cv2frac(pconc[i]); /* Ignore transfer, see PLRM3 p. 494 */
}
}
} else {
for (i = 0; i < ncomps; i++) {
frac_value = cv2frac(pconc[i]);
cv_frac[i] = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - frac_value), effective_transfer[i]);
}
}
}
} else {
if (has_halftone) {
/* We need this to be in frac form */
for (i = 0; i < ncomps; i++) {
cv_frac[i] = cv2frac(pconc[i]);
}
}
}
/* Halftoning */
if (has_halftone) {
if (gx_render_device_DeviceN(&(cv_frac[0]), pdc, dev,
pis->dev_ht, &pis->screen_phase[select]) == 1)
gx_color_load_select(pdc, pis, dev, select);
} else {
/* We have a frac value from the transfer function. Do the encode.
which does not take a frac value... */
for (i = 0; i < ncomps; i++) {
color_val[i] = frac2cv(cv_frac[i]);
}
color = dev_proc(dev, encode_color)(dev, &(color_val[0]));
/* check if the encoding was successful; we presume failure is rare */
if (color != gx_no_color_index)
color_set_pure(pdc, color);
}
}
/* This is used by image color render to apply only the transfer function.
We follow this up with threshold rendering. */
void
cmap_transfer(gx_color_value *pconc, const gs_imager_state * pis, gx_device * dev)
{
int ncomps = dev->color_info.num_components;
frac frac_value;
int i;
frac cv_frac[GX_DEVICE_COLOR_MAX_COMPONENTS];
/* apply the transfer function(s) */
if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE) {
for (i = 0; i < ncomps; i++) {
frac_value = cv2frac(pconc[i]);
cv_frac[i] = gx_map_color_frac(pis,
frac_value, effective_transfer[i]);
pconc[i] = frac2cv(cv_frac[i]);
}
} else {
if (dev->color_info.opmode == GX_CINFO_OPMODE_UNKNOWN) {
check_cmyk_color_model_comps(dev);
}
if (dev->color_info.opmode == GX_CINFO_OPMODE) { /* CMYK-like color space */
int k = dev->color_info.black_component;
for (i = 0; i < ncomps; i++) {
frac_value = cv2frac(pconc[i]);
if (i == k) {
cv_frac[i] = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - frac_value), effective_transfer[i]);
} else {
cv_frac[i] = cv2frac(pconc[i]); /* Ignore transfer, see PLRM3 p. 494 */
}
pconc[i] = frac2cv(cv_frac[i]);
}
} else {
for (i = 0; i < ncomps; i++) {
frac_value = cv2frac(pconc[i]);
cv_frac[i] = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - frac_value), effective_transfer[i]);
pconc[i] = frac2cv(cv_frac[i]);
}
}
}
}
/* A planar version which applies only one transfer function */
void
cmap_transfer_plane(gx_color_value *pconc, const gs_imager_state *pis,
gx_device *dev, int plane)
{
frac frac_value;
frac cv_frac;
/* apply the transfer function(s) */
if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE) {
frac_value = cv2frac(pconc[0]);
cv_frac = gx_map_color_frac(pis, frac_value, effective_transfer[plane]);
pconc[0] = frac2cv(cv_frac);
} else {
if (dev->color_info.opmode == GX_CINFO_OPMODE_UNKNOWN) {
check_cmyk_color_model_comps(dev);
}
if (dev->color_info.opmode == GX_CINFO_OPMODE) { /* CMYK-like color space */
int k = dev->color_info.black_component;
frac_value = cv2frac(pconc[0]);
if (plane == k) {
cv_frac = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - frac_value), effective_transfer[plane]);
} else {
cv_frac = cv2frac(pconc[0]); /* Ignore transfer, see PLRM3 p. 494 */
}
pconc[0] = frac2cv(cv_frac);
} else {
frac_value = cv2frac(pconc[0]);
cv_frac = frac_1 - gx_map_color_frac(pis,
(frac)(frac_1 - frac_value), effective_transfer[plane]);
pconc[0] = frac2cv(cv_frac);
}
}
}
bool
gx_device_uses_std_cmap_procs(gx_device * dev, const gs_imager_state * pis)
{
const gx_cm_color_map_procs *pprocs;
gsicc_rendering_intents_t rendering_intent;
int code;
cmm_dev_profile_t *dev_profile = NULL;
cmm_profile_t *des_profile = NULL;
code = dev_proc(dev, get_profile)(dev, &dev_profile);
gsicc_extract_profile(dev->graphics_type_tag,
dev_profile, &des_profile,
&rendering_intent);
if (des_profile != NULL) {
pprocs = dev_proc(dev, get_color_mapping_procs)(dev);
/* Check if they are forwarding procs */
if (fwd_uses_fwd_cmap_procs(dev)) {
pprocs = fwd_get_target_cmap_procs(dev);
}
switch(des_profile->num_comps) {
case 1:
if (pprocs == &DeviceGray_procs) {
return true;
}
break;
case 3:
if (pprocs == &DeviceRGB_procs) {
return true;
}
break;
case 4:
if (pprocs == &DeviceCMYK_procs) {
return true;
}
break;
default:
break;
}
}
return false;
}
|