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
|
/* Copyright (C) 2001-2012 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 the license contained in the file LICENSE in this distribution.
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.
*/
/* This is the code that is used to convert the various PDF and PS CIE
based color spaces to ICC profiles. This enables the use of an
external CMS that is ICC centric to be used for ALL color management.
The following spaces are handled:
From PDF
% Input Spaces
CalRGB --> ICC 1-D LUTS and Matrix
CalGray --> ICC 1-D LUT
LAB --> ICC MLUT with a 2x2 sized table
From PS
%% Input Spaces
CIEBasedABC --> ICC 1-D LUTs and Matrix
CIEBasedA --> ICC 1-D LUT
CIEBasedDEF --> 3-D MLUT plus 1-D LUTs
CIEBasedDEFG --> 4-D MLUT pluse 1-D LUTs
%% Output Spaces
Type1 CRD --> ICC will have MLUT if render table present.
A few notes:
Required Tags for ALL profiles include:
profileDescriptionTag
copyrightTag
mediaWhatePointTag
chromaticAdaptationTag (V4 - when measurement data is for other than D50)
For color input profiles:
Required if N-component LUT-based:
AToB0Tag (NOTE ONE WAY! BtoA0Tag is optional. Not true for
display profiles.)
Required if 3 component matrix based:
redMatrixColumnTag
greenMatrixColumnTag
blueMatrixColumnTag
redTRCTag
greenTRCTag
blueTRCTag
Notes:
3-component can include AToB0Tag.
Only CIEXYZ encoding can be used with matrix/TRC models.
If CIELAB encoding is to be used, we must use LUT-based.
For Monochrome input:
Required:
grayTRCTag
Optional
AToB0Tag
For Color Display Profiles:
Required if N-Component LUT-Based
AtoB0Tag
BToA0Tag (Note inverse required here).
Required if 3 component matrix based display profiles
redMatrixColumnTag
greenMatrixColumnTag
blueMatrixColumnTag
redTRCTag
greenTRCTag
blueTRCTag
Optional
AtoB0Tag
BToA0Tag (Note inverse required here).
For Monochrome Display Profiles
Required
grayTRCTag
Optional
AtoB0Tag
BtoA0Tag
Note: All profile data must be encoded as big-endian
*/
#include "icc34.h" /* Note this header is needed even if lcms is not
compiled as default CMS */
#include "string_.h"
#include "gsmemory.h"
#include "gx.h"
#include "gxistate.h"
#include "gstypes.h"
#include "gscspace.h"
#include "gscie.h"
#include "gsicc_create.h"
#include "gxarith.h"
#include "gsicc_manage.h"
#include "gsicc_cache.h"
#include "math_.h"
#include "gscolor2.h"
#include "gxcie.h"
static void
add_xyzdata(unsigned char *input_ptr, icS15Fixed16Number temp_XYZ[]);
#define SAVEICCPROFILE 0
#define USE_V4 1
#define HEADER_SIZE 128
#define TAG_SIZE 12
#define XYZPT_SIZE 12
#define DATATYPE_SIZE 8
#define CURVE_SIZE 512
#define IDENT_CURVE_SIZE 0
#define NUMBER_COMMON_TAGS 2
#define icMultiUnicodeText 0x6d6c7563 /* 'mluc' v4 text type */
#define icMultiFunctionAtoBType 0x6d414220 /* 'mAB ' v4 lutAtoBtype type */
#define icSigChromaticAdaptationTag 0x63686164 /* 'chad' */
#define D50_X 0.9642f
#define D50_Y 1.0f
#define D50_Z 0.8249f
#define DEFAULT_TABLE_NSIZE 9
#define DEFAULT_TABLE_GRAYSIZE 128
typedef unsigned short u1Fixed15Number;
#if SAVEICCPROFILE
unsigned int icc_debug_index = 0;
#endif
typedef struct cielab_s {
float lstar;
float astar;
float bstar;
} cielab_t;
static const char desc_name[] = "Ghostscript Internal Profile";
static const char copy_right[] = "Copyright Artifex Software 2009";
typedef struct {
icTagSignature sig; /* The tag signature */
icUInt32Number offset; /* Start of tag relative to
* start of header, Spec
* Clause 5 */
icUInt32Number size; /* Size in bytes */
unsigned char byte_padding;
} gsicc_tag;
/* In generating 2x2x2 approximations as well as cases
where we will need to squash components together we
will go to float and then to 16 bit tables, hence the
float pointer. Otherwise we will keep the data
in the existing byte form that it is in the CIEDEF(G)
tables of postscript */
typedef struct {
unsigned short *data_short;
unsigned char *data_byte; /* Used for cases where we can
use the table as is */
int clut_dims[4];
int clut_num_input;
int clut_num_output;
int clut_num_entries; /* Number of entries */
int clut_word_width; /* Word width of table, 1 or 2 */
} gsicc_clut;
typedef struct {
float *a_curves;
gsicc_clut *clut;
float *m_curves;
gs_matrix3 *matrix;
float *b_curves;
int num_in;
int num_out;
gs_vector3 *white_point;
gs_vector3 *black_point;
float *cam;
} gsicc_lutatob;
static int
get_padding(int x)
{
return( (4 -x%4)%4 );
}
/* For some weird reason I cant link to the one in gscie.c */
static void
gsicc_matrix_init(register gs_matrix3 * mat)
{
mat->is_identity =
mat->cu.u == 1.0 && is_fzero2(mat->cu.v, mat->cu.w) &&
mat->cv.v == 1.0 && is_fzero2(mat->cv.u, mat->cv.w) &&
mat->cw.w == 1.0 && is_fzero2(mat->cw.u, mat->cw.v);
}
static void
gsicc_make_diag_matrix(gs_matrix3 *matrix, gs_vector3 * vec)
{
matrix->cu.u = vec->u;
matrix->cv.v = vec->v;
matrix->cw.w = vec->w;
matrix->cu.v = 0;
matrix->cu.w = 0;
matrix->cw.u = 0;
matrix->cw.v = 0;
matrix->cv.u = 0;
matrix->cv.w = 0;
matrix->is_identity = (vec->u == 1.0)&&(vec->v == 1.0)&&(vec->w == 1.0);
}
/* This function maps a gs matrix type to an ICC CLUT.
This is required due to the multiple matrix and 1-D LUT
forms for postscript management, which the ICC does not
support (at least the older versions). clut is allocated
externally */
static void
gsicc_matrix3_to_mlut(gs_matrix3 *mat, unsigned short *clut)
{
/* Step through the grid values */
float grid_points[8][3]={{0,0,0},
{0,0,1},
{0,1,0},
{0,1,1},
{1,0,0},
{1,0,1},
{1,1,0},
{1,1,1}};
int k;
gs_vector3 input,output;
unsigned short *curr_ptr = clut, value;
float valueflt;
for (k = 0; k < 8; k++) {
input.u = grid_points[k][0];
input.v = grid_points[k][1];
input.w = grid_points[k][2];
cie_mult3(&input, mat, &output);
valueflt = output.u;
if (valueflt < 0) valueflt = 0;
if (valueflt > 1) valueflt = 1;
value = (unsigned short) (valueflt*65535.0);
*curr_ptr ++= value;
valueflt = output.v;
if (valueflt < 0) valueflt = 0;
if (valueflt > 1) valueflt = 1;
value = (unsigned short) (valueflt*65535.0);
*curr_ptr ++= value;
valueflt = output.w;
if (valueflt < 0) valueflt = 0;
if (valueflt > 1) valueflt = 1;
value = (unsigned short) (valueflt*65535.0);
*curr_ptr ++= value;
}
}
static u1Fixed15Number
double2u1Fixed15Number(float number_in)
{
float value;
value = number_in/(1.0 + (32767.0/32768.0));
value = value * 65535.0;
if (value < 0) {
value = 0;
}
if (value > 65535) {
value = 65535;
}
return((u1Fixed15Number) value);
}
/* This function mashes all the elements together into a single CLUT
for the ICC profile. This is an approach of last resort, but
guaranteed to work. */
static int
gsicc_create_clut(const gs_color_space *pcs, gsicc_clut *clut, gs_range *ranges,
gs_vector3 *white_point, bool range_adjust, gs_memory_t *memory)
{
gs_imager_state *pis;
int code;
int num_points = clut->clut_num_entries;
int table_size = clut->clut_dims[0]; /* Same resolution in each direction*/
int num_components = clut->clut_num_input;
int j,i,index;
float *input_samples[4], *fltptr;
gs_range *curr_range;
unsigned short *ptr_short;
gs_client_color cc;
frac xyz[3];
float xyz_float[3];
float temp;
gs_color_space_index cs_index;
/* This completes the joint cache inefficiently so that
we can sample through it and get our table entries */
code = gx_cie_to_xyz_alloc(&pis, pcs, memory);
cs_index = gs_color_space_get_index(pcs);
if (code < 0)
return code;
/* Create the sample indices across the input ranges
for each color component. When the concretization/remap occurs
to be fed into this icc profile, we may will need to apply a linear
map to the input if the range is something other than 0 to 1 */
for (i = 0; i < num_components; i++) {
input_samples[i] = (float*) gs_alloc_bytes(memory,
sizeof(float)*table_size,"gsicc_create_clut");
fltptr = input_samples[i];
curr_range = &(ranges[i]);
for (j = 0; j < table_size; j++ ) {
*fltptr ++= ((float) j/ (float) (table_size-1)) *
(curr_range->rmax - curr_range->rmin) + curr_range->rmin;
}
}
/* Go through all the entries.
Uniformly from min range to max range */
ptr_short = clut->data_short;
for (i = 0; i < num_points; i++) {
if (num_components == 1) {
/* Get the input vector value */
fltptr = input_samples[0];
index = i%table_size;
cc.paint.values[0] = fltptr[index];
}
if (num_components == 3) {
/* The first channel varies least rapidly in the ICC table */
fltptr = input_samples[2];
index = i%table_size;
cc.paint.values[2] = fltptr[index];
fltptr = input_samples[1];
index = (unsigned int) floor((float) i/(float) table_size)%table_size;
cc.paint.values[1] = fltptr[index];
fltptr = input_samples[0];
index = (unsigned int) floor((float) i/(float) (table_size*
table_size))%table_size;
cc.paint.values[0] = fltptr[index];
}
if (num_components == 4) {
/* The first channel varies least rapidly in the ICC table */
fltptr = input_samples[3];
index = i%table_size;
cc.paint.values[3] = fltptr[index];
fltptr = input_samples[2];
index = (unsigned int) floor((float) i/(float) table_size)%table_size;
cc.paint.values[2] = fltptr[index];
fltptr = input_samples[1];
index = (unsigned int) floor((float) i/(float) (table_size*
table_size))%table_size;
cc.paint.values[1] = fltptr[index];
fltptr = input_samples[0];
index = (unsigned int) floor((float) i/(float) (table_size*
table_size*table_size))%table_size;
cc.paint.values[0] = fltptr[index];
}
/* These special concretizations functions do not go through
the ICC mapping like the procs associated with the color space */
switch (cs_index) {
case gs_color_space_index_CIEA:
gx_psconcretize_CIEA(&cc, pcs, xyz, pis);
/* AR forces this case to always be achromatic. We will
do the same even though it does not match the PS
specification */
/* Use the resulting Y value to scale the D50 Illumination.
note that we scale to the whitepoint here. Matrix out
handles mapping to CIE D50 */
xyz_float[1] = frac2float(xyz[1]);
xyz_float[0] = white_point->u * xyz_float[1];
xyz_float[2] = white_point->w * xyz_float[1];
break;
case gs_color_space_index_CIEABC:
gx_psconcretize_CIEABC(&cc, pcs, xyz, pis);
break;
case gs_color_space_index_CIEDEF:
gx_psconcretize_CIEDEF(&cc, pcs, xyz, pis);
break;
case gs_color_space_index_CIEDEFG:
gx_psconcretize_CIEDEFG(&cc, pcs, xyz, pis);
break;
default:
break;
}
/* Correct for range of ICC CIEXYZ table data */
for (j = 0; j < 3; j++) {
if ( cs_index == gs_color_space_index_CIEA) {
temp = xyz_float[j]/(1 + 32767.0/32768);
} else {
temp = frac2float(xyz[j])/(1 + 32767.0/32768);
}
if (temp < 0) temp = 0;
if (temp > 1) temp = 1;
*ptr_short ++= (unsigned int)(temp * 65535);
}
}
gx_cie_to_xyz_free(pis); /* Free the joint cache we created */
for (i = 0; i < num_components; i++) {
gs_free_object(memory, input_samples[i], "gsicc_create_clut");
}
return(0);
}
/* This function maps a gs vector type to an ICC CLUT.
This is used in the CIEA type. clut is allocated
externally. We may need to replace this with a range value.
For now we are mapping to an output between 0 and the vector */
static void
gsicc_vec_to_mlut(gs_vector3 *vec, unsigned short *clut)
{
unsigned short *curr_ptr = clut;
*curr_ptr ++= 0;
*curr_ptr ++= 0;
*curr_ptr ++= 0;
*curr_ptr ++= double2u1Fixed15Number(vec->u);
*curr_ptr ++= double2u1Fixed15Number(vec->v);
*curr_ptr ++= double2u1Fixed15Number(vec->w);
}
#if SAVEICCPROFILE
/* Debug dump of internally created ICC profile for testing */
static void
save_profile(unsigned char *buffer, char filename[], int buffer_size)
{
char full_file_name[50];
FILE *fid;
sprintf(full_file_name,"%d)Profile_%s.icc",icc_debug_index,filename);
fid = fopen(full_file_name,"wb");
fwrite(buffer,sizeof(unsigned char),buffer_size,fid);
fclose(fid);
icc_debug_index++;
}
#endif
static void
write_bigendian_4bytes(unsigned char *curr_ptr,ulong input)
{
*curr_ptr++ = (0xff & (input >> 24));
*curr_ptr++ = (0xff & (input >> 16));
*curr_ptr++ = (0xff & (input >> 8));
*curr_ptr++ = (0xff & input);
}
static void
write_bigendian_2bytes(unsigned char *curr_ptr,ushort input)
{
*curr_ptr++ = (0xff & (input >> 8));
*curr_ptr++ = (0xff & input);
}
static void
setdatetime(icDateTimeNumber *datetime)
{
datetime->day = 0;
datetime->hours = 0;
datetime->minutes = 0;
datetime->month = 0;
datetime->seconds = 0;
datetime->year = 0;
}
static icS15Fixed16Number
double2XYZtype(float number_in)
{
short s;
unsigned short m;
if (number_in < 0) {
number_in = 0;
#ifdef DEBUG
gs_warn("Negative CIEXYZ in created ICC Profile");
#endif
}
s = (short) number_in;
m = (unsigned short) ((number_in - s) * 65536.0);
return((icS15Fixed16Number) ((s << 16) | m) );
}
static icS15Fixed16Number
double2icS15Fixed16Number(float number_in)
{
short s;
unsigned short m;
icS15Fixed16Number temp;
float number;
if (number_in < 0) {
number = -number_in;
s = (short) number;
m = (unsigned short) ((number - s) * 65536.0);
temp = (icS15Fixed16Number) ((s << 16) | m);
temp = -temp;
return(temp);
} else {
s = (short) number_in;
m = (unsigned short) ((number_in - s) * 65536.0);
return((icS15Fixed16Number) ((s << 16) | m) );
}
}
static unsigned short
float2u8Fixed8(float number_in)
{
unsigned short m;
m = (unsigned short) (number_in * 256);
return( m );
}
static
void init_common_tags(gsicc_tag tag_list[],int num_tags, int *last_tag)
{
/* profileDescriptionTag
copyrightTag */
int curr_tag, temp_size;
if (*last_tag < 0)
curr_tag = 0;
else
curr_tag = (*last_tag)+1;
tag_list[curr_tag].offset = HEADER_SIZE+num_tags*TAG_SIZE + 4;
tag_list[curr_tag].sig = icSigProfileDescriptionTag;
/* temp_size = DATATYPE_SIZE + 4 + strlen(desc_name) + 1 + 4 + 4 + 3 + 67; */
temp_size = 2*strlen(desc_name) + 28;
/* +1 for NULL + 4 + 4 for unicode + 3 + 67 script code */
tag_list[curr_tag].byte_padding = get_padding(temp_size);
tag_list[curr_tag].size = temp_size + tag_list[curr_tag].byte_padding;
curr_tag++;
tag_list[curr_tag].offset = tag_list[curr_tag-1].offset +
tag_list[curr_tag-1].size;
tag_list[curr_tag].sig = icSigCopyrightTag;
/* temp_size = DATATYPE_SIZE + strlen(copy_right) + 1; */
temp_size = 2*strlen(copy_right) + 28;
tag_list[curr_tag].byte_padding = get_padding(temp_size);
tag_list[curr_tag].size = temp_size + tag_list[curr_tag].byte_padding;
*last_tag = curr_tag;
}
/* Code to write out v4 text type which is a table of unicode text
for different regions */
static void
add_v4_text_tag(unsigned char *buffer,const char text[], gsicc_tag tag_list[],
int curr_tag)
{
unsigned char *curr_ptr;
int k;
curr_ptr = buffer;
write_bigendian_4bytes(curr_ptr,icMultiUnicodeText);
curr_ptr += 4;
memset(curr_ptr,0,4);
curr_ptr += 4;
write_bigendian_4bytes(curr_ptr,1); /* Number of names */
curr_ptr += 4;
write_bigendian_4bytes(curr_ptr,12); /* Record size */
curr_ptr += 4;
write_bigendian_2bytes(curr_ptr,0x656e); /* ISO 639-1, en */
curr_ptr += 2;
write_bigendian_2bytes(curr_ptr,0x5553); /* ISO 3166, US */
curr_ptr += 2;
write_bigendian_4bytes(curr_ptr,2*strlen(text)); /* String length */
curr_ptr += 4;
write_bigendian_4bytes(curr_ptr,28); /* Offset to string */
curr_ptr += 4;
/* String written as UTF-16BE. No NULL */
for (k = 0; k < strlen(text); k++) {
*curr_ptr ++= 0;
*curr_ptr ++= text[k];
}
memset(curr_ptr,0,tag_list[curr_tag].byte_padding); /* padding */
}
static void
add_common_tag_data(unsigned char *buffer,gsicc_tag tag_list[])
{
#if USE_V4
unsigned char *curr_ptr;
curr_ptr = buffer;
add_v4_text_tag(curr_ptr, desc_name, tag_list, 0);
curr_ptr += tag_list[0].size;
add_v4_text_tag(curr_ptr, copy_right, tag_list, 1);
#else
unsigned char *curr_ptr;
curr_ptr = buffer;
add_desc_tag(curr_ptr, desc_name, tag_list, 0);
curr_ptr += tag_list[0].size;
add_text_tag(curr_ptr, copy_right, tag_list, 1);
#endif
}
static
void init_tag(gsicc_tag tag_list[], int *last_tag, icTagSignature tagsig,
int datasize)
{
/* This should never be called first. Common tags should be taken care of */
int curr_tag = (*last_tag)+1;
tag_list[curr_tag].offset = tag_list[curr_tag-1].offset +
tag_list[curr_tag-1].size;
tag_list[curr_tag].sig = tagsig;
tag_list[curr_tag].byte_padding = get_padding(DATATYPE_SIZE + datasize);
tag_list[curr_tag].size = DATATYPE_SIZE + datasize +
tag_list[curr_tag].byte_padding;
*last_tag = curr_tag;
}
static void
setheader_common(icHeader *header)
{
/* This needs to all be predefined for a simple copy. MJV todo */
header->cmmId = 0;
header->version = 0x04200000;
setdatetime(&(header->date));
header->magic = icMagicNumber;
header->platform = icSigMacintosh;
header->flags = 0;
header->manufacturer = 0;
header->model = 0;
header->attributes[0] = 0;
header->attributes[1] = 0;
header->renderingIntent = 3;
header->illuminant.X = double2XYZtype((float) 0.9642);
header->illuminant.Y = double2XYZtype((float) 1.0);
header->illuminant.Z = double2XYZtype((float) 0.8249);
header->creator = 0;
/* Version 4 includes a profile id, field which is an md5 sum */
memset(header->reserved,0,44);
}
static void
copy_header(unsigned char *buffer,icHeader *header)
{
unsigned char *curr_ptr;
curr_ptr = buffer;
write_bigendian_4bytes(curr_ptr,header->size);
curr_ptr += 4;
memset(curr_ptr,0,4);
curr_ptr += 4;
write_bigendian_4bytes(curr_ptr,header->version);
curr_ptr += 4;
write_bigendian_4bytes(curr_ptr,header->deviceClass);
curr_ptr += 4;
write_bigendian_4bytes(curr_ptr,header->colorSpace);
curr_ptr += 4;
write_bigendian_4bytes(curr_ptr,header->pcs);
curr_ptr += 4;
/* Date and time */
memset(curr_ptr,0,12);
curr_ptr += 12;
write_bigendian_4bytes(curr_ptr,header->magic);
curr_ptr += 4;
write_bigendian_4bytes(curr_ptr,header->platform);
curr_ptr += 4;
memset(curr_ptr,0,24);
curr_ptr += 24;
write_bigendian_4bytes(curr_ptr,header->illuminant.X);
curr_ptr += 4;
write_bigendian_4bytes(curr_ptr,header->illuminant.Y);
curr_ptr += 4;
write_bigendian_4bytes(curr_ptr,header->illuminant.Z);
curr_ptr += 4;
memset(curr_ptr,0,48);
}
static void
copy_tagtable(unsigned char *buffer,gsicc_tag *tag_list, ulong num_tags)
{
unsigned int k;
unsigned char *curr_ptr;
curr_ptr = buffer;
write_bigendian_4bytes(curr_ptr,num_tags);
curr_ptr += 4;
for (k = 0; k < num_tags; k++) {
write_bigendian_4bytes(curr_ptr,tag_list[k].sig);
curr_ptr += 4;
write_bigendian_4bytes(curr_ptr,tag_list[k].offset);
curr_ptr += 4;
write_bigendian_4bytes(curr_ptr,tag_list[k].size);
curr_ptr += 4;
}
}
static void
get_D50(icS15Fixed16Number XYZ[])
{
XYZ[0] = double2XYZtype(D50_X);
XYZ[1] = double2XYZtype(D50_Y);
XYZ[2] = double2XYZtype(D50_Z);
}
static void
get_XYZ(icS15Fixed16Number XYZ[], gs_vector3 *vector)
{
XYZ[0] = double2XYZtype(vector->u);
XYZ[1] = double2XYZtype(vector->v);
XYZ[2] = double2XYZtype(vector->w);
}
static void
get_XYZ_floatptr(icS15Fixed16Number XYZ[], float *vector)
{
XYZ[0] = double2XYZtype(vector[0]);
XYZ[1] = double2XYZtype(vector[1]);
XYZ[2] = double2XYZtype(vector[2]);
}
static void
scale_matrix(float *matrix_input,float scale_factor)
{
int k;
for (k = 0; k < 9; k++) {
matrix_input[k] = matrix_input[k]/2.0;
}
}
static void
add_gammadata(unsigned char *input_ptr, unsigned short gamma,
icTagSignature curveType)
{
unsigned char *curr_ptr;
curr_ptr = input_ptr;
write_bigendian_4bytes(curr_ptr,curveType);
curr_ptr += 4;
memset(curr_ptr,0,4);
curr_ptr += 4;
/* one entry for gamma */
write_bigendian_4bytes(curr_ptr, 1);
curr_ptr += 4;
/* The encode (8frac8) gamma, with padding */
write_bigendian_2bytes(curr_ptr, gamma);
curr_ptr += 2;
/* pad two bytes */
memset(curr_ptr,0,2);
}
static void
add_xyzdata(unsigned char *input_ptr, icS15Fixed16Number temp_XYZ[])
{
int j;
unsigned char *curr_ptr;
curr_ptr = input_ptr;
write_bigendian_4bytes(curr_ptr,icSigXYZType);
curr_ptr += 4;
memset(curr_ptr,0,4);
curr_ptr += 4;
for (j = 0; j < 3; j++) {
write_bigendian_4bytes(curr_ptr, temp_XYZ[j]);
curr_ptr += 4;
}
}
/* If abc matrix is identity the abc and lmn curves can be mashed together */
static void
merge_abc_lmn_curves(gx_cie_vector_cache *DecodeABC_caches,
gx_cie_scalar_cache *DecodeLMN)
{
}
static void
add_matrixwithbias(unsigned char *input_ptr, float *float_ptr_in, bool has_bias)
{
unsigned char *curr_ptr;
float *float_ptr = float_ptr_in;
int k;
/* GS Matrix is coming in with data arranged in row ordered form */
curr_ptr = input_ptr;
for (k = 0; k < 9; k++ ){
write_bigendian_4bytes(curr_ptr, double2icS15Fixed16Number(*float_ptr));
curr_ptr += 4;
float_ptr++;
}
if (has_bias){
memset(curr_ptr,0,4*3);
}
}
static void
matrixmult(float leftmatrix[], int nlrow, int nlcol,
float rightmatrix[], int nrrow, int nrcol, float result[])
{
float *curr_row;
int k,l,j,ncols,nrows;
float sum;
nrows = nlrow;
ncols = nrcol;
if (nlcol == nrrow) {
for (k = 0; k < nrows; k++) {
curr_row = &(leftmatrix[k*nlcol]);
for (l = 0; l < ncols; l++) {
sum = 0.0;
for (j = 0; j < nlcol; j++) {
sum = sum + curr_row[j] * rightmatrix[j*nrcol+l];
}
result[k*ncols+l] = sum;
}
}
}
}
static void
gsicc_create_copy_matrix3(float *src, float *des)
{
memcpy(des,src,9*sizeof(float));
}
static void
gsicc_create_compute_cam( gs_vector3 *white_src, gs_vector3 *white_des,
float *cam)
{
float cat02matrix[] = {0.7328f, 0.4296f, -0.1624f,
-0.7036f, 1.6975f, 0.0061f,
0.003f, 0.0136f, 0.9834f};
float cat02matrixinv[] = {1.0961f, -0.2789f, 0.1827f,
0.4544f, 0.4735f, 0.0721f,
-0.0096f, -0.0057f, 1.0153f};
float vonkries_diag[9];
float temp_matrix[9];
float lms_wp_src[3], lms_wp_des[3];
int k;
matrixmult(cat02matrix,3,3,&(white_src->u),3,1,&(lms_wp_src[0]));
matrixmult(cat02matrix,3,3,&(white_des->u),3,1,&(lms_wp_des[0]));
memset(&(vonkries_diag[0]),0,sizeof(float)*9);
for (k = 0; k < 3; k++) {
if (lms_wp_src[k] > 0 ) {
vonkries_diag[k*3+k] = lms_wp_des[k]/lms_wp_src[k];
} else {
vonkries_diag[k*3+k] = 1;
}
}
matrixmult(&(vonkries_diag[0]), 3, 3, &(cat02matrix[0]), 3, 3,
&(temp_matrix[0]));
matrixmult(&(cat02matrixinv[0]), 3, 3, &(temp_matrix[0]), 3, 3, &(cam[0]));
}
/* Hardcoded chad for D65 to D50. This should be computed on the fly
based upon the PS specified white point and ICC D50. We don't use
the chad tag with littleCMS since it takes care of the chromatic
adaption itself based upon D50 and the media white point. */
static void
add_chad_data(unsigned char *input_ptr, float *data)
{
unsigned char *curr_ptr = input_ptr;
/* float data[] = {1.04790738171017, 0.0229333845542104, -0.0502016347980104,
0.0296059594177168, 0.990456039910785, -0.01707552919587,
-0.00924679432678241, 0.0150626801401488, 0.751791232609078};*/
/* Signature should be sf32 */
curr_ptr = input_ptr;
write_bigendian_4bytes(curr_ptr,icSigS15Fixed16ArrayType);
curr_ptr += 4;
/* Reserved */
memset(curr_ptr,0,4);
curr_ptr += 4;
add_matrixwithbias(curr_ptr, &(data[0]), false);
}
static void
add_ident_curves(unsigned char *input_ptr,int number_of_curves)
{
unsigned char *curr_ptr;
int k;
curr_ptr = input_ptr;
for (k = 0; k < number_of_curves; k++) {
/* Signature */
write_bigendian_4bytes(curr_ptr,icSigCurveType);
curr_ptr += 4;
/* Reserved */
memset(curr_ptr,0,4);
curr_ptr += 4;
/* Count */
write_bigendian_4bytes(curr_ptr, 0);
curr_ptr += 4;
}
}
static void
add_clutAtoB(unsigned char *input_ptr, gsicc_clut *clut)
{
unsigned char *curr_ptr = input_ptr;
int k;
int num_channels_in = clut->clut_num_input;
int number_samples = clut->clut_num_entries;
/* First write out the dimensions for each channel */
for (k = 0; k < num_channels_in; k++) {
memset(curr_ptr, clut->clut_dims[k], 1);
curr_ptr++;
}
/* Set the remainder of the dimenensions */
memset(curr_ptr, 0, 16-num_channels_in);
curr_ptr += (16-num_channels_in);
/* word size */
memset(curr_ptr, clut->clut_word_width, 1);
curr_ptr++;
/* padding */
memset(curr_ptr, 0, 3);
curr_ptr += 3;
if (clut->data_byte != NULL) {
/* A byte table */
memcpy(curr_ptr,clut->data_byte,number_samples*3);
} else {
/* A float table */
for ( k = 0; k < number_samples*3; k++ ) {
write_bigendian_2bytes(curr_ptr,clut->data_short[k]);
curr_ptr += 2;
}
}
}
static void
add_curve(unsigned char *input_ptr, float *curve_data, int num_samples)
{
unsigned char *curr_ptr;
unsigned short value;
int k;
/* Signature */
curr_ptr = input_ptr;
write_bigendian_4bytes(curr_ptr,icSigCurveType);
curr_ptr += 4;
/* Reserved */
memset(curr_ptr,0,4);
curr_ptr += 4;
/* Count */
write_bigendian_4bytes(curr_ptr, num_samples);
curr_ptr += 4;
/* Now the data uInt16 Number 0 to 65535. For now assume input is 0 to 1.
Need to fix this. MJV */
for (k = 0; k < num_samples; k++) {
if (curve_data[k] < 0) curve_data[k] = 0;
if (curve_data[k] > 1) curve_data[k] = 1;
value = (unsigned int) (curve_data[k]*65535.0);
write_bigendian_2bytes(curr_ptr,value);
curr_ptr+=2;
}
}
/* See comments before add_lutAtoBtype about allowable forms, which will
explain much of these size calculations */
static int
getsize_lutAtoBtype(gsicc_lutatob *lutatobparts)
{
int data_offset, mlut_size;
int numout = lutatobparts->num_out;
int numin = lutatobparts->num_in;
int pad_bytes;
data_offset = 32;
/* B curves always present */
if (lutatobparts->b_curves != NULL) {
data_offset += (numout*(CURVE_SIZE*2+12));
} else {
data_offset += (numout*(IDENT_CURVE_SIZE*2+12));
}
/* M curves present if Matrix is present */
if (lutatobparts->matrix != NULL ) {
data_offset += (12*4);
/* M curves */
if (lutatobparts->m_curves != NULL) {
data_offset += (numout*(CURVE_SIZE*2+12));
} else {
data_offset += (numout*(IDENT_CURVE_SIZE*2+12));
}
}
/* A curves present if clut is present */
if (lutatobparts->clut != NULL) {
/* We may need to pad the clut to make sure we are on a 4 byte boundary */
mlut_size = lutatobparts->clut->clut_num_entries *
lutatobparts->clut->clut_word_width * 3;
pad_bytes = (4 - mlut_size%4)%4;
data_offset += (mlut_size + pad_bytes + 20);
if (lutatobparts->a_curves != NULL) {
data_offset += (numin*(CURVE_SIZE*2+12));
} else {
data_offset += (numin*(IDENT_CURVE_SIZE*2+12));
}
}
return(data_offset);
}
/* Note: ICC V4 fomat allows ONLY these forms
B
M - Matrix - B
A - CLUT - B
A - CLUT - M - Matrix - B
Other forms are created by making some of these items identity. In other words
the B curves must always be included. If CLUT is present, A curves must be present.
Also, if Matrix is present M curves must be present. A curves cannot be
present if CLUT is not present. */
static void
add_lutAtoBtype(unsigned char *input_ptr, gsicc_lutatob *lutatobparts)
{
/* We need to figure out all the offsets to the various objects based upon
which ones are actually present */
unsigned char *curr_ptr;
long mlut_size;
int data_offset;
int k;
int numout = lutatobparts->num_out;
int numin = lutatobparts->num_in;
int pad_bytes = 0;
/* Signature */
curr_ptr = input_ptr;
write_bigendian_4bytes(curr_ptr,icMultiFunctionAtoBType);
curr_ptr += 4;
/* Reserved */
memset(curr_ptr,0,4);
curr_ptr += 4;
/* Padded sizes */
*curr_ptr++ = numin;
*curr_ptr++ = numout;
memset(curr_ptr,0,2);
curr_ptr += 2;
/* Note if data offset is zero, element is not present */
/* offset to B curves (last curves) */
data_offset = 32;
if (lutatobparts->b_curves == NULL) {
/* identity curve must be present */
write_bigendian_4bytes(curr_ptr,data_offset);
data_offset += (numout*(IDENT_CURVE_SIZE*2+12));
} else {
write_bigendian_4bytes(curr_ptr,data_offset);
data_offset += (numout*(CURVE_SIZE*2+12));
}
curr_ptr += 4;
/* offset to matrix and M curves */
if (lutatobparts->matrix == NULL) {
memset(curr_ptr,0,4); /* Matrix */
curr_ptr += 4;
memset(curr_ptr,0,4); /* M curves */
} else {
write_bigendian_4bytes(curr_ptr,data_offset);
data_offset += (12*4);
curr_ptr += 4;
/* offset to M curves (Matrix curves -- only come with matrix) */
if (lutatobparts->m_curves == NULL) {
/* identity curve must be present */
write_bigendian_4bytes(curr_ptr,data_offset);
data_offset += (numout*(IDENT_CURVE_SIZE*2+12));
} else {
write_bigendian_4bytes(curr_ptr,data_offset);
data_offset += (numout*(CURVE_SIZE*2+12));
}
}
curr_ptr += 4;
/* offset to CLUT and A curves */
if (lutatobparts->clut == NULL) {
memset(curr_ptr,0,4); /* CLUT */
curr_ptr += 4;
memset(curr_ptr,0,4); /* A curves */
} else {
write_bigendian_4bytes(curr_ptr,data_offset);
mlut_size = lutatobparts->clut->clut_num_entries *
lutatobparts->clut->clut_word_width * 3;
pad_bytes = (4 - mlut_size%4)%4;
data_offset += (mlut_size + pad_bytes + 20);
curr_ptr += 4;
/* offset to A curves (first curves) */
if (lutatobparts->a_curves == NULL || lutatobparts->clut == NULL) {
/* identity curve must be present */
write_bigendian_4bytes(curr_ptr,data_offset);
data_offset += (numin*(IDENT_CURVE_SIZE*2+12));
} else {
write_bigendian_4bytes(curr_ptr,data_offset);
data_offset += (numin*(CURVE_SIZE*2+12));
}
}
curr_ptr += 4;
/* Header is completed */
/* Now write out the various parts (i.e. curves, matrix and clut) */
/* First the B curves */
if (lutatobparts->b_curves != NULL) {
for (k = 0; k < numout; k++) {
add_curve(curr_ptr, (lutatobparts->b_curves)+k*CURVE_SIZE, CURVE_SIZE);
curr_ptr += (12 + CURVE_SIZE*2);
}
} else {
add_ident_curves(curr_ptr,numout);
curr_ptr += numout*(12 + IDENT_CURVE_SIZE*2);
}
/* Then the matrix */
if (lutatobparts->matrix != NULL) {
add_matrixwithbias(curr_ptr,&(lutatobparts->matrix->cu.u),true);
curr_ptr += (12*4);
/* M curves */
if (lutatobparts->m_curves != NULL) {
for (k = 0; k < numout; k++) {
add_curve(curr_ptr, (lutatobparts->m_curves)+k*CURVE_SIZE, CURVE_SIZE);
curr_ptr += (12 + CURVE_SIZE*2);
}
} else {
add_ident_curves(curr_ptr,numout);
curr_ptr += numout*(12 + IDENT_CURVE_SIZE*2);
}
}
/* Then the clut */
if (lutatobparts->clut != NULL) {
add_clutAtoB(curr_ptr, lutatobparts->clut);
curr_ptr += (20 + mlut_size);
memset(curr_ptr,0,pad_bytes); /* 4 byte boundary */
curr_ptr += pad_bytes;
/* The A curves */
if (lutatobparts->a_curves != NULL) {
for (k = 0; k < numin; k++) {
add_curve(curr_ptr, (lutatobparts->a_curves)+k*CURVE_SIZE,
CURVE_SIZE);
curr_ptr += (12 + CURVE_SIZE*2);
}
} else {
add_ident_curves(curr_ptr,numin);
curr_ptr += numin*(12 + IDENT_CURVE_SIZE*2);
}
}
}
/* This creates an ICC profile from the PDF calGray and calRGB definitions */
cmm_profile_t*
gsicc_create_from_cal(float *white, float *black, float *gamma, float *matrix,
gs_memory_t *memory, int num_colors)
{
icProfile iccprofile;
icHeader *header = &(iccprofile.header);
int profile_size,k;
int num_tags;
gsicc_tag *tag_list;
unsigned short encode_gamma;
unsigned char *curr_ptr;
int last_tag;
icS15Fixed16Number temp_XYZ[3];
int tag_location;
icTagSignature TRC_Tags[3] = {icSigRedTRCTag, icSigGreenTRCTag,
icSigBlueTRCTag};
int trc_tag_size;
unsigned char *buffer;
cmm_profile_t *result;
/* Fill in the common stuff */
setheader_common(header);
header->pcs = icSigXYZData;
profile_size = HEADER_SIZE;
header->deviceClass = icSigInputClass;
if (num_colors == 3) {
header->colorSpace = icSigRgbData;
num_tags = 10; /* common (2) + rXYZ,gXYZ,bXYZ,rTRC,gTRC,bTRC,bkpt,wtpt */
} else if (num_colors == 1) {
header->colorSpace = icSigGrayData;
num_tags = 5; /* common (2) + GrayTRC,bkpt,wtpt */
TRC_Tags[0] = icSigGrayTRCTag;
} else {
return(NULL);
}
tag_list = (gsicc_tag*) gs_alloc_bytes(memory,
sizeof(gsicc_tag)*num_tags,"gsicc_create_from_cal");
/* Let us precompute the sizes of everything and all our offsets */
profile_size += TAG_SIZE*num_tags;
profile_size += 4; /* number of tags.... */
last_tag = -1;
init_common_tags(tag_list, num_tags, &last_tag);
if (num_colors == 3) {
init_tag(tag_list, &last_tag, icSigRedColorantTag, XYZPT_SIZE);
init_tag(tag_list, &last_tag, icSigGreenColorantTag, XYZPT_SIZE);
init_tag(tag_list, &last_tag, icSigBlueColorantTag, XYZPT_SIZE);
}
init_tag(tag_list, &last_tag, icSigMediaWhitePointTag, XYZPT_SIZE);
init_tag(tag_list, &last_tag, icSigMediaBlackPointTag, XYZPT_SIZE);
/* 4 for count, 2 for gamma, Extra 2 bytes for 4 byte alignment requirement */
trc_tag_size = 8;
for (k = 0; k < num_colors; k++) {
init_tag(tag_list, &last_tag, TRC_Tags[k], trc_tag_size);
}
for(k = 0; k < num_tags; k++) {
profile_size += tag_list[k].size;
}
/* Now we can go ahead and fill our buffer with the data. Profile
buffer data is in non-gc memory */
buffer = gs_alloc_bytes(memory->non_gc_memory,
profile_size, "gsicc_create_from_cal");
curr_ptr = buffer;
/* The header */
header->size = profile_size;
copy_header(curr_ptr,header);
curr_ptr += HEADER_SIZE;
/* Tag table */
copy_tagtable(curr_ptr,tag_list,num_tags);
curr_ptr += TAG_SIZE*num_tags;
curr_ptr += 4;
/* Now the data. Must be in same order as we created the tag table */
/* First the common tags */
add_common_tag_data(curr_ptr, tag_list);
for (k = 0; k< NUMBER_COMMON_TAGS; k++) {
curr_ptr += tag_list[k].size;
}
tag_location = NUMBER_COMMON_TAGS;
/* The matrix */
if (num_colors == 3) {
for ( k = 0; k < 3; k++ ) {
get_XYZ_floatptr(temp_XYZ,&(matrix[k*3]));
add_xyzdata(curr_ptr,temp_XYZ);
curr_ptr += tag_list[tag_location].size;
tag_location++;
}
}
/* White and black points */
/* Need to adjust for the D65/D50 issue */
get_XYZ_floatptr(temp_XYZ,white);
add_xyzdata(curr_ptr,temp_XYZ);
curr_ptr += tag_list[tag_location].size;
tag_location++;
/* Black point */
get_XYZ_floatptr(temp_XYZ,black);
add_xyzdata(curr_ptr,temp_XYZ);
curr_ptr += tag_list[tag_location].size;
tag_location++;
/* Now the gamma values */
for ( k = 0; k < num_colors; k++ ) {
encode_gamma = float2u8Fixed8(gamma[k]);
add_gammadata(curr_ptr, encode_gamma, icSigCurveType);
curr_ptr += tag_list[tag_location].size;
tag_location++;
}
result = gsicc_profile_new(NULL, memory, NULL, 0);
result->buffer = buffer;
result->buffer_size = profile_size;
result->num_comps = num_colors;
if (num_colors == 3) {
result->data_cs = gsRGB;
result->default_match = CAL_RGB;
} else {
result->data_cs = gsGRAY;
result->default_match = CAL_GRAY;
}
/* Set the hash code */
gsicc_get_icc_buff_hash(buffer, &(result->hashcode), result->buffer_size);
result->hash_is_valid = true;
/* Free up the tag list */
gs_free_object(memory, tag_list, "gsicc_create_from_cal");
#if SAVEICCPROFILE
/* Dump the buffer to a file for testing if its a valid ICC profile */
if (num_colors == 3)
save_profile(buffer,"from_calRGB",profile_size);
else
save_profile(buffer,"from_calGray",profile_size);
#endif
return(result);
}
static void
gsicc_create_free_luta2bpart(gs_memory_t *memory, gsicc_lutatob *icc_luta2bparts)
{
/* Note that white_point, black_point and matrix are not allocated but
are on the local stack */
gs_free_object(memory, icc_luta2bparts->a_curves,
"gsicc_create_free_luta2bpart");
gs_free_object(memory, icc_luta2bparts->b_curves,
"gsicc_create_free_luta2bpart");
gs_free_object(memory, icc_luta2bparts->m_curves,
"gsicc_create_free_luta2bpart");
gs_free_object(memory, icc_luta2bparts->cam,
"gsicc_create_free_luta2bpart");
if (icc_luta2bparts->clut) {
/* Note, data_byte is handled externally. We do not free that member here */
gs_free_object(memory, icc_luta2bparts->clut->data_short,
"gsicc_create_free_luta2bpart");
gs_free_object(memory, icc_luta2bparts->clut,
"gsicc_create_free_luta2bpart");
}
}
static void
gsicc_create_init_luta2bpart(gsicc_lutatob *icc_luta2bparts)
{
icc_luta2bparts->a_curves = NULL;
icc_luta2bparts->b_curves = NULL;
icc_luta2bparts->clut = NULL;
icc_luta2bparts->m_curves = NULL;
icc_luta2bparts->cam = NULL;
icc_luta2bparts->matrix = NULL;
icc_luta2bparts->white_point = NULL;
icc_luta2bparts->black_point = NULL;
icc_luta2bparts->num_in = 0;
icc_luta2bparts->num_out = 0;
}
static void
gsicc_create_initialize_clut(gsicc_clut *clut)
{
int k;
clut->clut_num_entries = clut->clut_dims[0];
for (k = 1; k < clut->clut_num_input; k++) {
clut->clut_num_entries *= clut->clut_dims[k];
}
clut->data_byte = NULL;
clut->data_short = NULL;
}
/* A common form used for most of the PS CIE color spaces */
static void
create_lutAtoBprofile(unsigned char **pp_buffer_in, icHeader *header,
gsicc_lutatob *lutatobparts, bool yonly,
gs_memory_t *memory)
{
int num_tags = 6; /* common (2), AToB0Tag,bkpt, wtpt and chad.*/
int k;
gsicc_tag *tag_list;
int profile_size, last_tag, tag_location, tag_size;
unsigned char *buffer,*curr_ptr;
icS15Fixed16Number temp_XYZ[3];
gs_vector3 d50;
float *cam;
gs_matrix3 temp_matrix;
float lmn_vector[3],d50_cieA[3];
profile_size = HEADER_SIZE;
tag_list = (gsicc_tag*) gs_alloc_bytes(memory,sizeof(gsicc_tag)*num_tags,
"create_lutAtoBprofile");
/* Let us precompute the sizes of everything and all our offsets */
profile_size += TAG_SIZE*num_tags;
profile_size += 4; /* number of tags.... */
last_tag = -1;
init_common_tags(tag_list, num_tags, &last_tag);
init_tag(tag_list, &last_tag, icSigMediaWhitePointTag, XYZPT_SIZE);
init_tag(tag_list, &last_tag, icSigMediaBlackPointTag, XYZPT_SIZE);
init_tag(tag_list, &last_tag, icSigChromaticAdaptationTag, 9*4); /* chad tag */
/* Get the tag size of the A2B0 with the lutAtoBType */
/* Compensate for init_tag() adding DATATYPE_SIZE */
tag_size = getsize_lutAtoBtype(lutatobparts) - DATATYPE_SIZE;
init_tag(tag_list, &last_tag, icSigAToB0Tag, tag_size);
/* Add all the tag sizes to get the new profile size */
for(k = 0; k < num_tags; k++) {
profile_size += tag_list[k].size;
}
/* End of tag table information */
/* Now we can go ahead and fill our buffer with the data. Profile
is in non-gc memory */
buffer = gs_alloc_bytes(memory->non_gc_memory, profile_size,
"create_lutAtoBprofile");
curr_ptr = buffer;
/* The header */
header->size = profile_size;
copy_header(curr_ptr,header);
curr_ptr += HEADER_SIZE;
/* Tag table */
copy_tagtable(curr_ptr,tag_list,num_tags);
curr_ptr += TAG_SIZE*num_tags;
curr_ptr += 4;
/* Now the data. Must be in same order as we created the tag table */
/* First the common tags */
add_common_tag_data(curr_ptr, tag_list);
for (k = 0; k< NUMBER_COMMON_TAGS; k++) {
curr_ptr += tag_list[k].size;
}
tag_location = NUMBER_COMMON_TAGS;
/* Here we take care of chromatic adapatation. Compute the
matrix. We will need to hit the data with the matrix and
store it in the profile. */
d50.u = D50_X;
d50.v = D50_Y;
d50.w = D50_Z;
cam = (float*) gs_alloc_bytes(memory,9*sizeof(float),"create_lutAtoBprofile");
gsicc_create_compute_cam(lutatobparts->white_point, &(d50), cam);
lutatobparts->cam = cam;
get_D50(temp_XYZ); /* See Appendix D6 in spec */
/* get_XYZ(temp_XYZ,lutatobparts->white_point); */
add_xyzdata(curr_ptr,temp_XYZ);
curr_ptr += tag_list[tag_location].size;
tag_location++;
get_XYZ(temp_XYZ,lutatobparts->black_point);
add_xyzdata(curr_ptr,temp_XYZ);
curr_ptr += tag_list[tag_location].size;
tag_location++;
add_chad_data(curr_ptr, cam);
curr_ptr += tag_list[tag_location].size;
tag_location++;
/* Multiply the matrix in the AtoB object by the cam so that the data
is in D50 */
if (lutatobparts->matrix == NULL) {
gsicc_create_copy_matrix3(cam,&(temp_matrix.cu.u));
lutatobparts->matrix = &temp_matrix;
} else {
if (yonly) {
/* Used for CIEBaseA case. Studies of CIEBasedA spaces
and AR rendering of these reveals that they only look
at the product sum of the MatrixA and the 2nd column of
the LM Matrix (if there is one). This is used as a Y
decode value from which to map between the black point
and the white point. The black point is actually ignored
and a black point of 0 is used. Essentialy we have
weighted versions of D50 in each column of the matrix
which ensures we stay on the achromatic axis */
lmn_vector[0] = lutatobparts->matrix->cv.u;
lmn_vector[1] = lutatobparts->matrix->cv.v;
lmn_vector[2] = lutatobparts->matrix->cv.w;
d50_cieA[0] = D50_X;
d50_cieA[1] = D50_Y;
d50_cieA[2] = D50_Z;
matrixmult(&(d50_cieA[0]),3,1,&(lmn_vector[0]), 1, 3,
&(lutatobparts->matrix->cu.u));
} else {
matrixmult(cam, 3, 3, &(lutatobparts->matrix->cu.u), 3, 3,
&(temp_matrix.cu.u));
lutatobparts->matrix = &temp_matrix;
}
}
/* Now the AToB0Tag Data. Here this will include the M curves, the matrix
and the B curves. We may need to do some adustements with respect
to encode and decode. For now assume all is between 0 and 1. */
add_lutAtoBtype(curr_ptr, lutatobparts);
*pp_buffer_in = buffer;
gs_free_object(memory, tag_list, "create_lutAtoBprofile");
}
/* Shared code between all the PS types whereby we mash together all the
components into a single CLUT. Not preferable in general but necessary
when the PS components do not map easily into the ICC forms */
static void
gsicc_create_mashed_clut(gsicc_lutatob *icc_luta2bparts,
icHeader *header, gx_color_lookup_table *Table,
const gs_color_space *pcs, gs_range *ranges,
unsigned char **pp_buffer_in, int *profile_size_out,
bool range_adjust, gs_memory_t* memory)
{
int k;
int code;
gsicc_clut *clut;
gs_matrix3 ident_matrix;
gs_vector3 ones_vec;
/* A table is going to be mashed form of all the transform */
/* Allocate space for the clut */
clut = (gsicc_clut*) gs_alloc_bytes(memory,sizeof(gsicc_clut),
"gsicc_create_mashed_clut");
icc_luta2bparts->clut = clut;
if ( icc_luta2bparts->num_in == 1 ) {
/* Use a larger sample for 1-D input */
clut->clut_dims[0] = DEFAULT_TABLE_GRAYSIZE;
} else {
for (k = 0; k < icc_luta2bparts->num_in; k++) {
if (Table != NULL && Table->dims[k] > DEFAULT_TABLE_NSIZE ) {
/* If it has a table use the existing table size if
it is larger than our default size */
clut->clut_dims[k] = Table->dims[k];
} else {
/* If not, then use a default size */
clut->clut_dims[k] = DEFAULT_TABLE_NSIZE;
}
}
}
clut->clut_num_input = icc_luta2bparts->num_in;
clut->clut_num_output = 3; /* CIEXYZ */
clut->clut_word_width = 2; /* 16 bit */
gsicc_create_initialize_clut(clut);
/* Allocate space for the table data */
clut->data_short =
(unsigned short*) gs_alloc_bytes(memory,
clut->clut_num_entries*3*sizeof(unsigned short),"gsicc_create_mashed_clut");
/* Create the table */
code = gsicc_create_clut(pcs, clut, ranges, icc_luta2bparts->white_point,
range_adjust, memory);
/* Initialize other parts. Also make sure acurves are reset since
they have been mashed into the table. */
gs_free_object(memory, icc_luta2bparts->a_curves, "gsicc_create_mashed_clut");
icc_luta2bparts->a_curves = NULL;
icc_luta2bparts->b_curves = NULL;
icc_luta2bparts->m_curves = NULL;
ones_vec.u = 1;
ones_vec.v = 1;
ones_vec.w = 1;
gsicc_make_diag_matrix(&ident_matrix,&ones_vec);
icc_luta2bparts->matrix = &ident_matrix;
/* Now create the profile */
if (icc_luta2bparts->num_in == 1 ) {
create_lutAtoBprofile(pp_buffer_in, header, icc_luta2bparts, true, memory);
} else {
create_lutAtoBprofile(pp_buffer_in, header, icc_luta2bparts, false, memory);
}
}
/* Shared code by ABC, DEF and DEFG compaction of ABC/LMN parts. This is used
when we either MatrixABC is identity, LMN Decode is identity or MatrixLMN
is identity */
static void
gsicc_create_abc_merge(gsicc_lutatob *atob_parts, gs_matrix3 *matrixLMN,
gs_matrix3 *matrixABC, bool has_abc_procs,
bool has_lmn_procs, gx_cie_vector_cache *abc_caches,
gx_cie_scalar_cache *lmn_caches, gs_memory_t *memory)
{
gs_matrix3 temp_matrix;
gs_matrix3 *matrix_ptr;
float *curr_pos;
/* Determine the matrix that we will be using */
if (!(matrixLMN->is_identity) && !(matrixABC->is_identity)){
/* Use the product of the ABC and LMN matrices, since lmn_procs identity.
Product must be LMN_Matrix*ABC_Matrix */
cie_matrix_mult3(matrixLMN, matrixABC, &temp_matrix);
cie_matrix_transpose3(&temp_matrix, atob_parts->matrix);
} else {
/* Either ABC matrix or LMN matrix is identity */
if (matrixABC->is_identity) {
matrix_ptr = matrixLMN;
} else {
matrix_ptr = matrixABC;
}
cie_matrix_transpose3(matrix_ptr, atob_parts->matrix);
}
/* Merge the curves */
if (has_abc_procs && has_lmn_procs && matrixABC->is_identity) {
/* Merge the curves into the abc curves. no b curves */
merge_abc_lmn_curves(abc_caches, lmn_caches);
has_lmn_procs = false;
}
/* Figure out what curves get mapped to where. The only time
we will use the b curves is if matrixABC is not the identity and we have
lmn procs */
if ( !(matrixABC->is_identity) && has_lmn_procs) {
/* A matrix followed by a curve */
atob_parts->b_curves = (float*) gs_alloc_bytes(memory,
3*CURVE_SIZE*sizeof(float),"gsicc_create_abc_merge");
curr_pos = atob_parts->b_curves;
memcpy(curr_pos,&(lmn_caches[0].floats.values[0]),CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&(lmn_caches[1].floats.values[0]),CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&(lmn_caches[2].floats.values[0]),CURVE_SIZE*sizeof(float));
if (has_abc_procs) {
/* Also a curve before the matrix */
atob_parts->m_curves = (float*) gs_alloc_bytes(memory,
3*CURVE_SIZE*sizeof(float),"gsicc_create_abc_merge");
curr_pos = atob_parts->m_curves;
memcpy(curr_pos,&(abc_caches[0].floats.values[0]),CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&(abc_caches[1].floats.values[0]),CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&(abc_caches[2].floats.values[0]),CURVE_SIZE*sizeof(float));
}
} else {
/* Only one set of curves before a matrix */
if (has_abc_procs) {
atob_parts->m_curves = (float*) gs_alloc_bytes(memory,
3*CURVE_SIZE*sizeof(float),"gsicc_create_abc_merge");
curr_pos = atob_parts->m_curves;
memcpy(curr_pos,&(abc_caches[0].floats.values[0]),CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&(abc_caches[1].floats.values[0]),CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&(abc_caches[2].floats.values[0]),CURVE_SIZE*sizeof(float));
}
if (has_lmn_procs) {
atob_parts->m_curves = (float*) gs_alloc_bytes(memory,
3*CURVE_SIZE*sizeof(float),"gsicc_create_abc_merge");
curr_pos = atob_parts->m_curves;
memcpy(curr_pos,&(lmn_caches[0].floats.values[0]),CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&(lmn_caches[1].floats.values[0]),CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&(lmn_caches[2].floats.values[0]),CURVE_SIZE*sizeof(float));
}
}
/* Note that if the b_curves are null and we have a matrix we need to scale
the matrix values by 2. Otherwise an input value of 50% gray, which is
32767 would get mapped to 32767 by the matrix. This will be interpreted
as a max XYZ value (s15.16) when it is eventually mapped to u16.16 due
to the mapping of X=Y by the identity table. If there are b_curves
these have an output that is 16 bit. */
if (atob_parts->b_curves == NULL) {
scale_matrix(&(atob_parts->matrix->cu.u),2.0);
}
}
/* The ABC color space is modeled using the V4 lutAtoBType which has the
flexibility to model the various parameters. Simplified versions are used
it possible when certain parameters in the ABC color space definition are
the identity. */
int
gsicc_create_fromabc(const gs_color_space *pcs, unsigned char **pp_buffer_in,
int *profile_size_out, gs_memory_t *memory,
gx_cie_vector_cache *abc_caches,
gx_cie_scalar_cache *lmn_caches, bool *islab)
{
icProfile iccprofile;
icHeader *header = &(iccprofile.header);
#if SAVEICCPROFILE
int debug_catch = 1;
#endif
int k;
gs_matrix3 matrix_input_trans;
gsicc_lutatob icc_luta2bparts;
float *curr_pos;
bool has_abc_procs = !((abc_caches->floats.params.is_identity &&
(abc_caches)[1].floats.params.is_identity &&
(abc_caches)[2].floats.params.is_identity));
bool has_lmn_procs = !((lmn_caches->floats.params.is_identity &&
(lmn_caches)[1].floats.params.is_identity &&
(lmn_caches)[2].floats.params.is_identity));
gs_cie_abc *pcie = pcs->params.abc;
bool input_range_ok;
gsicc_create_init_luta2bpart(&icc_luta2bparts);
gsicc_matrix_init(&(pcie->common.MatrixLMN)); /* Need this set now */
gsicc_matrix_init(&(pcie->MatrixABC)); /* Need this set now */
/* Fill in the common stuff */
setheader_common(header);
/* We will use an input type class which keeps us from having to
create an inverse. We will keep the data a generic 3 color.
Since we are doing PS color management the PCS is XYZ */
header->colorSpace = icSigRgbData;
header->deviceClass = icSigInputClass;
header->pcs = icSigXYZData;
icc_luta2bparts.num_in = 3;
icc_luta2bparts.num_out = 3;
icc_luta2bparts.white_point = &(pcie->common.points.WhitePoint);
icc_luta2bparts.black_point = &(pcie->common.points.BlackPoint);
/* Detect if the space is CIELAB. We don't have access to pis here though */
/* *islab = cie_is_lab(pcie); This is not working yet */
*islab = false;
/* Check what combination we have with respect to the various
LMN and ABC parameters. Depending upon the situation we
may be able to use a standard 3 channel input profile type. If we
do not have the LMN decode we can mash together the ABC and LMN
matrix. Also, if ABC is identity we can mash the ABC and LMN
decode procs. If we have an ABC matrix, LMN procs and an LMN
matrix we will need to create a small (2x2x2) CLUT for the ICC format. */
input_range_ok = check_range(&(pcie->RangeABC.ranges[0]),3);
if (!input_range_ok) {
/* We have a range problem at input */
gsicc_create_mashed_clut(&icc_luta2bparts, header, NULL, pcs,
&(pcie->RangeABC.ranges[0]), pp_buffer_in,
profile_size_out, true, memory);
} else {
if (pcie->MatrixABC.is_identity || !has_lmn_procs ||
pcie->common.MatrixLMN.is_identity) {
/* The merging of these parts into the curves/matrix/curves of the
lutAtoBtype portion can be used by abc, def and defg */
icc_luta2bparts.matrix = &matrix_input_trans;
gsicc_create_abc_merge(&(icc_luta2bparts), &(pcie->common.MatrixLMN),
&(pcie->MatrixABC), has_abc_procs,
has_lmn_procs, pcie->caches.DecodeABC.caches,
pcie->common.caches.DecodeLMN, memory);
icc_luta2bparts.clut = NULL;
/* Create the profile. This is for the common generic form we will use
for almost everything. */
create_lutAtoBprofile(pp_buffer_in, header,&icc_luta2bparts,false, memory);
} else {
/* This will be a bit more complex as we have an ABC matrix, LMN decode
and an LMN matrix. We will need to create an MLUT to handle this properly.
Any ABC decode will be handled as the A curves. ABC matrix will be the
MLUT, LMN decode will be the M curves. LMN matrix will be the Matrix
and b curves will be identity. */
if (has_abc_procs) {
icc_luta2bparts.a_curves = (float*) gs_alloc_bytes(memory,
3*CURVE_SIZE*sizeof(float),"gsicc_create_fromabc");
curr_pos = icc_luta2bparts.a_curves;
memcpy(curr_pos,&(pcie->caches.DecodeABC.caches->floats.values[0]),
CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&((pcie->caches.DecodeABC.caches[1]).floats.values[0]),
CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&((pcie->caches.DecodeABC.caches[2]).floats.values[0]),
CURVE_SIZE*sizeof(float));
}
if (has_lmn_procs) {
icc_luta2bparts.m_curves = (float*) gs_alloc_bytes(memory,
3*CURVE_SIZE*sizeof(float),"gsicc_create_fromabc");
curr_pos = icc_luta2bparts.m_curves;
memcpy(curr_pos,&(pcie->common.caches.DecodeLMN->floats.values[0]),
CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&((pcie->common.caches.DecodeLMN[1]).floats.values[0]),
CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&((pcie->common.caches.DecodeLMN[2]).floats.values[0]),
CURVE_SIZE*sizeof(float));
}
/* Convert ABC matrix to 2x2x2 MLUT type */
icc_luta2bparts.clut = (gsicc_clut*) gs_alloc_bytes(memory,
sizeof(gsicc_clut),"gsicc_create_fromabc");
for (k = 0; k < 3; k++) {
icc_luta2bparts.clut->clut_dims[k] = 2;
}
icc_luta2bparts.clut->clut_num_input = 3;
icc_luta2bparts.clut->clut_num_output = 3;
icc_luta2bparts.clut->clut_word_width = 2;
gsicc_create_initialize_clut(icc_luta2bparts.clut);
/* 8 grid points, 3 outputs */
icc_luta2bparts.clut->data_short =
(unsigned short*) gs_alloc_bytes(memory,
8*3*sizeof(short),"gsicc_create_fromabc");
gsicc_matrix3_to_mlut(&(pcie->MatrixABC), icc_luta2bparts.clut->data_short);
/* LMN Matrix */
cie_matrix_transpose3(&(pcie->common.MatrixLMN), &matrix_input_trans);
icc_luta2bparts.matrix = &matrix_input_trans;
/* Create the profile */
create_lutAtoBprofile(pp_buffer_in, header, &icc_luta2bparts, false, memory);
}
}
gsicc_create_free_luta2bpart(memory, &icc_luta2bparts);
*profile_size_out = header->size;
#if SAVEICCPROFILE
/* Dump the buffer to a file for testing if its a valid ICC profile */
if(debug_catch)
save_profile(*pp_buffer_in,"fromabc",header->size);
#endif
return(0);
}
int
gsicc_create_froma(const gs_color_space *pcs, unsigned char **pp_buffer_in,
int *profile_size_out, gs_memory_t *memory,
gx_cie_vector_cache *a_cache, gx_cie_scalar_cache *lmn_caches)
{
icProfile iccprofile;
icHeader *header = &(iccprofile.header);
#if SAVEICCPROFILE
int debug_catch = 1;
#endif
gs_matrix3 matrix_input;
float *curr_pos;
bool has_a_proc = !(a_cache->floats.params.is_identity);
bool has_lmn_procs = !(lmn_caches->floats.params.is_identity &&
(lmn_caches)[1].floats.params.is_identity &&
(lmn_caches)[2].floats.params.is_identity);
gsicc_lutatob icc_luta2bparts;
bool common_range_ok;
gs_cie_a *pcie = pcs->params.a;
bool input_range_ok;
gsicc_create_init_luta2bpart(&icc_luta2bparts);
/* Fill in the common stuff */
setheader_common(header);
/* We will use an input type class which keeps us from having to
create an inverse. We will keep the data a generic 3 color.
Since we are doing PS color management the PCS is XYZ */
header->colorSpace = icSigGrayData;
header->deviceClass = icSigInputClass;
header->pcs = icSigXYZData;
icc_luta2bparts.num_out = 3;
icc_luta2bparts.num_in = 1;
icc_luta2bparts.white_point = &(pcie->common.points.WhitePoint);
icc_luta2bparts.black_point = &(pcie->common.points.BlackPoint);
/* Check the range values. If the internal ranges are outside of
0 to 1 then we will need to sample as a full CLUT. The input
range can be different, but we we will correct for this. Finally
we need to worry about enforcing the achromatic constraint for the
CLUT if we are creating the entire thing. */
common_range_ok = check_range(&(pcie->common.RangeLMN.ranges[0]),3);
if (!common_range_ok) {
input_range_ok = check_range(&(pcie->RangeA),1);
gsicc_create_mashed_clut(&icc_luta2bparts, header, NULL, pcs,
&(pcie->RangeA), pp_buffer_in, profile_size_out,
!input_range_ok, memory);
} else {
/* We do not need to create a massive CLUT. Try to maintain
the objects as best we can */
/* Since we are going from 1 gray input to 3 XYZ values, we will need
to include the MLUT for the 1 to 3 conversion applied by the matrix A.
Depending upon the other parameters we may have simpiler forms, but this
is required even when Matrix A is the identity. */
if (has_a_proc) {
icc_luta2bparts.a_curves = (float*) gs_alloc_bytes(memory,
CURVE_SIZE*sizeof(float),"gsicc_create_froma");
memcpy(icc_luta2bparts.a_curves,
&(pcie->caches.DecodeA.floats.values[0]),
CURVE_SIZE*sizeof(float));
}
if (has_lmn_procs) {
icc_luta2bparts.m_curves = (float*) gs_alloc_bytes(memory,
3*CURVE_SIZE*sizeof(float),"gsicc_create_froma");
curr_pos = icc_luta2bparts.m_curves;
memcpy(curr_pos,&(pcie->common.caches.DecodeLMN->floats.values[0]),
CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&((pcie->common.caches.DecodeLMN[1]).floats.values[0]),
CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&((pcie->common.caches.DecodeLMN[2]).floats.values[0]),
CURVE_SIZE*sizeof(float));
}
/* Convert diagonal A matrix to 2x1 MLUT type */
icc_luta2bparts.clut = (gsicc_clut*) gs_alloc_bytes(memory,
sizeof(gsicc_clut),"gsicc_create_froma"); /* 2 grid points 3 outputs */
icc_luta2bparts.clut->clut_dims[0] = 2;
icc_luta2bparts.clut->clut_num_input = 1;
icc_luta2bparts.clut->clut_num_output = 3;
icc_luta2bparts.clut->clut_word_width = 2;
gsicc_create_initialize_clut(icc_luta2bparts.clut);
/* 2 grid points 3 outputs */
icc_luta2bparts.clut->data_short = (unsigned short*)
gs_alloc_bytes(memory,2*3*sizeof(short),"gsicc_create_froma");
/* Studies of CIEBasedA spaces
and AR rendering of these reveals that they only look
at the product sum of the MatrixA and the 2nd column of
the LM Matrix (if there is one). This is used as a Y
decode value from which to map between the black point
and the white point. The black point is actually ignored
and a black point of 0 is used. */
gsicc_vec_to_mlut(&(pcie->MatrixA), icc_luta2bparts.clut->data_short);
cie_matrix_transpose3(&(pcie->common.MatrixLMN), &matrix_input);
icc_luta2bparts.matrix = &matrix_input;
icc_luta2bparts.num_in = 1;
icc_luta2bparts.num_out = 3;
/* Create the profile */
/* Note Adobe only looks at the Y value for CIEBasedA spaces.
we will do the same */
create_lutAtoBprofile(pp_buffer_in, header, &icc_luta2bparts, true, memory);
}
*profile_size_out = header->size;
gsicc_create_free_luta2bpart(memory, &icc_luta2bparts);
#if SAVEICCPROFILE
/* Dump the buffer to a file for testing if its a valid ICC profile */
if(debug_catch)
save_profile(*pp_buffer_in,"froma",header->size);
#endif
return(0);
}
/* Common code shared by def and defg generation */
static int
gsicc_create_defg_common(gs_cie_abc *pcie, gsicc_lutatob *icc_luta2bparts,
bool has_lmn_procs, bool has_abc_procs,
icHeader *header, gx_color_lookup_table *Table,
const gs_color_space *pcs, gs_range *ranges,
unsigned char **pp_buffer_in, int *profile_size_out,
gs_memory_t* memory)
{
gs_matrix3 matrix_input_trans;
int k;
bool input_range_ok;
gsicc_matrix_init(&(pcie->common.MatrixLMN)); /* Need this set now */
gsicc_matrix_init(&(pcie->MatrixABC)); /* Need this set now */
setheader_common(header);
/* We will use an input type class which keeps us from having to
create an inverse. We will keep the data a generic 3 color.
Since we are doing PS color management the PCS is XYZ */
header->deviceClass = icSigInputClass;
header->pcs = icSigXYZData;
icc_luta2bparts->num_out = 3;
icc_luta2bparts->white_point = &(pcie->common.points.WhitePoint);
icc_luta2bparts->black_point = &(pcie->common.points.BlackPoint);
/* question now is, can we keep the table as it is, or do we need to merge
some of the def(g) parts. Some merging or operators into the table must occur
if we have MatrixABC, LMN Decode and Matrix LMN, otherwise we can encode
the table directly and squash the rest into the curves matrix curve portion
of the ICC form */
if ( (!(pcie->MatrixABC.is_identity) && has_lmn_procs &&
!(pcie->common.MatrixLMN.is_identity)) || 1 ) {
/* Table must take over some of the other elements. We are going to
go to a 16 bit table in this case. For now, we are going to
mash all the elements in the table. We may want to revisit this later. */
/* We must complete the defg or def decode function such that it is within
the HIJ(K) range AND is scaled to index into the CLUT properly */
if (gs_color_space_get_index(pcs) == gs_color_space_index_CIEDEF) {
input_range_ok = check_range(&(pcs->params.def->RangeDEF.ranges[0]),3);
} else {
input_range_ok = check_range(&(pcs->params.defg->RangeDEFG.ranges[0]),4);
}
gsicc_create_mashed_clut(icc_luta2bparts, header, Table,
pcs, ranges, pp_buffer_in, profile_size_out,
!input_range_ok, memory);
} else {
/* Table can stay as is. Handle the ABC/LMN portions via the curves
matrix curves operation */
icc_luta2bparts->matrix = &matrix_input_trans;
gsicc_create_abc_merge(icc_luta2bparts, &(pcie->common.MatrixLMN),
&(pcie->MatrixABC), has_abc_procs,
has_lmn_procs, pcie->caches.DecodeABC.caches,
pcie->common.caches.DecodeLMN, memory);
/* Get the table data */
icc_luta2bparts->clut = (gsicc_clut*) gs_alloc_bytes(memory,
sizeof(gsicc_clut),"gsicc_create_defg_common");
for (k = 0; k < icc_luta2bparts->num_in; k++) {
icc_luta2bparts->clut->clut_dims[k] = Table->dims[k];
}
icc_luta2bparts->clut->clut_num_input = icc_luta2bparts->num_in;
icc_luta2bparts->clut->clut_num_output = 3;
icc_luta2bparts->clut->clut_word_width = 1;
gsicc_create_initialize_clut(icc_luta2bparts->clut);
/* Get the PS table data directly */
icc_luta2bparts->clut->data_byte = (byte*) Table->table->data;
/* Create the profile. */
create_lutAtoBprofile(pp_buffer_in, header, icc_luta2bparts, false, memory);
}
gsicc_create_free_luta2bpart(memory, icc_luta2bparts);
*profile_size_out = header->size;
return(0);
}
/* If we have an ABC matrix, a DecodeLMN and an LMN matrix we have to mash
together the table, Decode ABC (if present) and ABC matrix. */
int
gsicc_create_fromdefg(const gs_color_space *pcs, unsigned char **pp_buffer_in,
int *profile_size_out, gs_memory_t *memory,
gx_cie_vector_cache *abc_caches,
gx_cie_scalar_cache *lmn_caches,
gx_cie_scalar_cache *defg_caches)
{
gs_cie_defg *pcie = pcs->params.defg;
gsicc_lutatob icc_luta2bparts;
icProfile iccprofile;
icHeader *header = &(iccprofile.header);
#if SAVEICCPROFILE
int debug_catch = 1;
#endif
float *curr_pos;
bool has_abc_procs = !((abc_caches->floats.params.is_identity &&
(abc_caches)[1].floats.params.is_identity &&
(abc_caches)[2].floats.params.is_identity));
bool has_lmn_procs = !((lmn_caches->floats.params.is_identity &&
(lmn_caches)[1].floats.params.is_identity &&
(lmn_caches)[2].floats.params.is_identity));
bool has_defg_procs = !((defg_caches->floats.params.is_identity &&
(defg_caches)[1].floats.params.is_identity &&
(defg_caches)[2].floats.params.is_identity &&
(defg_caches)[3].floats.params.is_identity));
int code;
/* Fill in the uncommon stuff */
gsicc_create_init_luta2bpart(&icc_luta2bparts);
header->colorSpace = icSig4colorData;
icc_luta2bparts.num_in = 4;
/* The a curves stored as def procs */
if (has_defg_procs) {
icc_luta2bparts.a_curves = (float*) gs_alloc_bytes(memory,
4*CURVE_SIZE*sizeof(float),"gsicc_create_fromdefg");
curr_pos = icc_luta2bparts.a_curves;
memcpy(curr_pos,&(pcie->caches_defg.DecodeDEFG->floats.values[0]),
CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&((pcie->caches_defg.DecodeDEFG[1]).floats.values[0]),
CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&((pcie->caches_defg.DecodeDEFG[2]).floats.values[0]),
CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&((pcie->caches_defg.DecodeDEFG[3]).floats.values[0]),
CURVE_SIZE*sizeof(float));
}
/* Note the recast. Should be OK since we only access common stuff in there */
code = gsicc_create_defg_common((gs_cie_abc*) pcie, &icc_luta2bparts,
has_lmn_procs, has_abc_procs,
header, &(pcie->Table), pcs,
&(pcie->RangeDEFG.ranges[0]),
pp_buffer_in, profile_size_out, memory);
#if SAVEICCPROFILE
/* Dump the buffer to a file for testing if its a valid ICC profile */
if(debug_catch)
save_profile(*pp_buffer_in,"fromdefg",header->size);
#endif
return(code);
}
int
gsicc_create_fromdef(const gs_color_space *pcs, unsigned char **pp_buffer_in,
int *profile_size_out, gs_memory_t *memory,
gx_cie_vector_cache *abc_caches,
gx_cie_scalar_cache *lmn_caches,
gx_cie_scalar_cache *def_caches)
{
gs_cie_def *pcie = pcs->params.def;
gsicc_lutatob icc_luta2bparts;
icProfile iccprofile;
icHeader *header = &(iccprofile.header);
#if SAVEICCPROFILE
int debug_catch = 1;
#endif
float *curr_pos;
bool has_abc_procs = !((abc_caches->floats.params.is_identity &&
(abc_caches)[1].floats.params.is_identity &&
(abc_caches)[2].floats.params.is_identity));
bool has_lmn_procs = !((lmn_caches->floats.params.is_identity &&
(lmn_caches)[1].floats.params.is_identity &&
(lmn_caches)[2].floats.params.is_identity));
bool has_def_procs = !((def_caches->floats.params.is_identity &&
(def_caches)[1].floats.params.is_identity &&
(def_caches)[2].floats.params.is_identity));
int code;
gsicc_create_init_luta2bpart(&icc_luta2bparts);
header->colorSpace = icSig3colorData;
icc_luta2bparts.num_in = 3;
/* The a curves stored as def procs */
if (has_def_procs) {
icc_luta2bparts.a_curves = (float*) gs_alloc_bytes(memory,
3*CURVE_SIZE*sizeof(float),"gsicc_create_fromdef");
curr_pos = icc_luta2bparts.a_curves;
memcpy(curr_pos,&(pcie->caches_def.DecodeDEF->floats.values[0]),
CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&((pcie->caches_def.DecodeDEF[1]).floats.values[0]),
CURVE_SIZE*sizeof(float));
curr_pos += CURVE_SIZE;
memcpy(curr_pos,&((pcie->caches_def.DecodeDEF[2]).floats.values[0]),
CURVE_SIZE*sizeof(float));
}
code = gsicc_create_defg_common((gs_cie_abc*) pcie, &icc_luta2bparts,
has_lmn_procs, has_abc_procs, header,
&(pcie->Table), pcs, &(pcie->RangeDEF.ranges[0]),
pp_buffer_in, profile_size_out, memory);
#if SAVEICCPROFILE
/* Dump the buffer to a file for testing if its a valid ICC profile */
if(debug_catch)
save_profile(*pp_buffer_in,"fromdef",header->size);
#endif
return(code);
}
void
gsicc_create_fromcrd(unsigned char *buffer, gs_memory_t *memory)
{
icProfile iccprofile;
icHeader *header = &(iccprofile.header);
setheader_common(header);
}
|