1 7 8 17 18 package java.awt.color; 19 20 import sun.awt.color.CMM; 21 import sun.awt.color.ProfileDeferralMgr; 22 import sun.awt.color.ProfileDeferralInfo; 23 import sun.awt.color.ProfileActivator; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.ObjectInputStream ; 32 import java.io.ObjectOutputStream ; 33 import java.io.ObjectStreamException ; 34 import java.io.OutputStream ; 35 import java.io.Serializable ; 36 37 import java.util.StringTokenizer ; 38 39 import java.security.AccessController ; 40 import java.security.PrivilegedAction ; 41 42 71 72 73 public class ICC_Profile implements Serializable { 74 75 private static final long serialVersionUID = -3938515861990936766L; 76 77 transient long ID; 78 79 private transient ProfileDeferralInfo deferralInfo; 80 private transient ProfileActivator profileActivator; 81 82 private static ICC_Profile sRGBprofile; 86 private static ICC_Profile XYZprofile; 87 private static ICC_Profile PYCCprofile; 88 private static ICC_Profile GRAYprofile; 89 private static ICC_Profile LINEAR_RGBprofile; 90 91 92 95 public static final int CLASS_INPUT = 0; 96 97 100 public static final int CLASS_DISPLAY = 1; 101 102 105 public static final int CLASS_OUTPUT = 2; 106 107 110 public static final int CLASS_DEVICELINK = 3; 111 112 115 public static final int CLASS_COLORSPACECONVERSION = 4; 116 117 120 public static final int CLASS_ABSTRACT = 5; 121 122 125 public static final int CLASS_NAMEDCOLOR = 6; 126 127 128 131 public static final int icSigXYZData = 0x58595A20; 132 133 136 public static final int icSigLabData = 0x4C616220; 137 138 141 public static final int icSigLuvData = 0x4C757620; 142 143 146 public static final int icSigYCbCrData = 0x59436272; 147 148 151 public static final int icSigYxyData = 0x59787920; 152 153 156 public static final int icSigRgbData = 0x52474220; 157 158 161 public static final int icSigGrayData = 0x47524159; 162 163 166 public static final int icSigHsvData = 0x48535620; 167 168 171 public static final int icSigHlsData = 0x484C5320; 172 173 176 public static final int icSigCmykData = 0x434D594B; 177 178 181 public static final int icSigCmyData = 0x434D5920; 182 183 186 public static final int icSigSpace2CLR = 0x32434C52; 187 188 191 public static final int icSigSpace3CLR = 0x33434C52; 192 193 196 public static final int icSigSpace4CLR = 0x34434C52; 197 198 201 public static final int icSigSpace5CLR = 0x35434C52; 202 203 206 public static final int icSigSpace6CLR = 0x36434C52; 207 208 211 public static final int icSigSpace7CLR = 0x37434C52; 212 213 216 public static final int icSigSpace8CLR = 0x38434C52; 217 218 221 public static final int icSigSpace9CLR = 0x39434C52; 222 223 226 public static final int icSigSpaceACLR = 0x41434C52; 227 228 231 public static final int icSigSpaceBCLR = 0x42434C52; 232 233 236 public static final int icSigSpaceCCLR = 0x43434C52; 237 238 241 public static final int icSigSpaceDCLR = 0x44434C52; 242 243 246 public static final int icSigSpaceECLR = 0x45434C52; 247 248 251 public static final int icSigSpaceFCLR = 0x46434C52; 252 253 254 257 public static final int icSigInputClass = 0x73636E72; 258 259 262 public static final int icSigDisplayClass = 0x6D6E7472; 263 264 267 public static final int icSigOutputClass = 0x70727472; 268 269 272 public static final int icSigLinkClass = 0x6C696E6B; 273 274 277 public static final int icSigAbstractClass = 0x61627374; 278 279 282 public static final int icSigColorSpaceClass = 0x73706163; 283 284 287 public static final int icSigNamedColorClass = 0x6e6d636c; 288 289 290 293 public static final int icPerceptual = 0; 294 295 298 public static final int icRelativeColorimetric = 1; 299 300 303 public static final int icMediaRelativeColorimetric = 1; 304 305 308 public static final int icSaturation = 2; 309 310 313 public static final int icAbsoluteColorimetric = 3; 314 315 318 public static final int icICCAbsoluteColorimetric = 3; 319 320 321 324 public static final int icSigHead = 0x68656164; 325 326 329 public static final int icSigAToB0Tag = 0x41324230; 330 331 334 public static final int icSigAToB1Tag = 0x41324231; 335 336 339 public static final int icSigAToB2Tag = 0x41324232; 340 341 344 public static final int icSigBlueColorantTag = 0x6258595A; 345 346 349 public static final int icSigBlueMatrixColumnTag = 0x6258595A; 350 351 354 public static final int icSigBlueTRCTag = 0x62545243; 355 356 359 public static final int icSigBToA0Tag = 0x42324130; 360 361 364 public static final int icSigBToA1Tag = 0x42324131; 365 366 369 public static final int icSigBToA2Tag = 0x42324132; 370 371 374 public static final int icSigCalibrationDateTimeTag = 0x63616C74; 375 376 377 380 public static final int icSigCharTargetTag = 0x74617267; 381 382 385 public static final int icSigCopyrightTag = 0x63707274; 386 387 390 public static final int icSigCrdInfoTag = 0x63726469; 391 392 395 public static final int icSigDeviceMfgDescTag = 0x646D6E64; 396 397 400 public static final int icSigDeviceModelDescTag = 0x646D6464; 401 402 405 public static final int icSigDeviceSettingsTag = 0x64657673; 406 407 410 public static final int icSigGamutTag = 0x67616D74; 411 412 415 public static final int icSigGrayTRCTag = 0x6b545243; 416 417 420 public static final int icSigGreenColorantTag = 0x6758595A; 421 422 425 public static final int icSigGreenMatrixColumnTag = 0x6758595A; 426 427 430 public static final int icSigGreenTRCTag = 0x67545243; 431 432 435 public static final int icSigLuminanceTag = 0x6C756d69; 436 437 440 public static final int icSigMeasurementTag = 0x6D656173; 441 442 445 public static final int icSigMediaBlackPointTag = 0x626B7074; 446 447 450 public static final int icSigMediaWhitePointTag = 0x77747074; 451 452 455 public static final int icSigNamedColor2Tag = 0x6E636C32; 456 457 460 public static final int icSigOutputResponseTag = 0x72657370; 461 462 465 public static final int icSigPreview0Tag = 0x70726530; 466 467 470 public static final int icSigPreview1Tag = 0x70726531; 471 472 475 public static final int icSigPreview2Tag = 0x70726532; 476 477 480 public static final int icSigProfileDescriptionTag = 0x64657363; 481 482 483 486 public static final int icSigProfileSequenceDescTag = 0x70736571; 487 488 489 492 public static final int icSigPs2CRD0Tag = 0x70736430; 493 494 497 public static final int icSigPs2CRD1Tag = 0x70736431; 498 499 502 public static final int icSigPs2CRD2Tag = 0x70736432; 503 504 507 public static final int icSigPs2CRD3Tag = 0x70736433; 508 509 512 public static final int icSigPs2CSATag = 0x70733273; 513 514 517 public static final int icSigPs2RenderingIntentTag = 0x70733269; 518 519 520 523 public static final int icSigRedColorantTag = 0x7258595A; 524 525 528 public static final int icSigRedMatrixColumnTag = 0x7258595A; 529 530 533 public static final int icSigRedTRCTag = 0x72545243; 534 535 538 public static final int icSigScreeningDescTag = 0x73637264; 539 540 543 public static final int icSigScreeningTag = 0x7363726E; 544 545 548 public static final int icSigTechnologyTag = 0x74656368; 549 550 553 public static final int icSigUcrBgTag = 0x62666420; 554 555 558 public static final int icSigViewingCondDescTag = 0x76756564; 559 560 563 public static final int icSigViewingConditionsTag = 0x76696577; 564 565 568 public static final int icSigChromaticityTag = 0x6368726d; 569 570 573 public static final int icSigChromaticAdaptationTag = 0x63686164; 574 575 578 public static final int icSigColorantOrderTag = 0x636C726F; 579 580 583 public static final int icSigColorantTableTag = 0x636C7274; 584 585 586 589 public static final int icHdrSize = 0; 590 591 594 public static final int icHdrCmmId = 4; 595 596 599 public static final int icHdrVersion = 8; 600 601 604 public static final int icHdrDeviceClass = 12; 605 606 609 public static final int icHdrColorSpace = 16; 610 611 614 public static final int icHdrPcs = 20; 615 616 619 public static final int icHdrDate = 24; 620 621 624 public static final int icHdrMagic = 36; 625 626 629 public static final int icHdrPlatform = 40; 630 631 634 public static final int icHdrFlags = 44; 635 636 639 public static final int icHdrManufacturer = 48; 640 641 644 public static final int icHdrModel = 52; 645 646 649 public static final int icHdrAttributes = 56; 650 651 654 public static final int icHdrRenderingIntent = 64; 655 656 659 public static final int icHdrIlluminant = 68; 660 661 664 public static final int icHdrCreator = 80; 665 666 669 public static final int icHdrProfileID = 84; 670 671 672 675 public static final int icTagType = 0; 676 677 680 public static final int icTagReserved = 4; 681 682 685 public static final int icCurveCount = 8; 686 687 690 public static final int icCurveData = 12; 691 692 695 public static final int icXYZNumberX = 8; 696 697 698 701 ICC_Profile(long ID) { 702 this.ID = ID; 703 } 704 705 706 710 ICC_Profile(ProfileDeferralInfo pdi) { 711 this.deferralInfo = pdi; 712 this.profileActivator = new ProfileActivator() { 713 public void activate() { 714 activateDeferredProfile(); 715 } 716 }; 717 ProfileDeferralMgr.registerDeferral(this.profileActivator); 718 } 719 720 721 724 protected void finalize () { 725 if (ID != 0) { 726 CMM.checkStatus(CMM.cmmFreeProfile(ID)); 727 } else if (profileActivator != null) { 728 ProfileDeferralMgr.unregisterDeferral(profileActivator); 729 } 730 } 731 732 733 741 public static ICC_Profile getInstance(byte[] data) { 742 ICC_Profile thisProfile; 743 744 long[] theID = new long [1]; 745 746 if (ProfileDeferralMgr.deferring) { 747 ProfileDeferralMgr.activateProfiles(); 748 } 749 750 try { 751 CMM.checkStatus(CMM.cmmLoadProfile(data, theID)); 752 } catch (CMMException c) { 753 throw new IllegalArgumentException ("Invalid ICC Profile Data"); 754 } 755 756 try { 757 if ((getColorSpaceType (theID[0]) == ColorSpace.TYPE_GRAY) && 758 (getData (theID[0], icSigMediaWhitePointTag) != null) && 759 (getData (theID[0], icSigGrayTRCTag) != null)) { 760 thisProfile = new ICC_ProfileGray (theID[0]); 761 } 762 else if ((getColorSpaceType (theID[0]) == ColorSpace.TYPE_RGB) && 763 (getData (theID[0], icSigMediaWhitePointTag) != null) && 764 (getData (theID[0], icSigRedColorantTag) != null) && 765 (getData (theID[0], icSigGreenColorantTag) != null) && 766 (getData (theID[0], icSigBlueColorantTag) != null) && 767 (getData (theID[0], icSigRedTRCTag) != null) && 768 (getData (theID[0], icSigGreenTRCTag) != null) && 769 (getData (theID[0], icSigBlueTRCTag) != null)) { 770 thisProfile = new ICC_ProfileRGB (theID[0]); 771 } 772 else { 773 thisProfile = new ICC_Profile (theID[0]); 774 } 775 } catch (CMMException c) { 776 thisProfile = new ICC_Profile (theID[0]); 777 } 778 return thisProfile; 779 } 780 781 782 783 798 public static ICC_Profile getInstance (int cspace) { 799 ICC_Profile thisProfile = null; 800 String fileName; 801 802 switch (cspace) { 803 case ColorSpace.CS_sRGB: 804 synchronized(ICC_Profile .class) { 805 if (sRGBprofile == null) { 806 try { 807 812 sRGBprofile = getDeferredInstance( 813 new ProfileDeferralInfo("sRGB.pf", 814 ColorSpace.TYPE_RGB, 815 3, CLASS_DISPLAY)); 816 } catch (IOException e) { 817 throw new IllegalArgumentException ( 818 "Can't load standard profile: sRGB.pf"); 819 } 820 } 821 thisProfile = sRGBprofile; 822 } 823 824 break; 825 826 case ColorSpace.CS_CIEXYZ: 827 synchronized(ICC_Profile .class) { 828 if (XYZprofile == null) { 829 XYZprofile = getStandardProfile("CIEXYZ.pf"); 830 } 831 thisProfile = XYZprofile; 832 } 833 834 break; 835 836 case ColorSpace.CS_PYCC: 837 synchronized(ICC_Profile .class) { 838 if (PYCCprofile == null) { 839 PYCCprofile = getStandardProfile("PYCC.pf"); 840 } 841 thisProfile = PYCCprofile; 842 } 843 844 break; 845 846 case ColorSpace.CS_GRAY: 847 synchronized(ICC_Profile .class) { 848 if (GRAYprofile == null) { 849 GRAYprofile = getStandardProfile("GRAY.pf"); 850 } 851 thisProfile = GRAYprofile; 852 } 853 854 break; 855 856 case ColorSpace.CS_LINEAR_RGB: 857 synchronized(ICC_Profile .class) { 858 if (LINEAR_RGBprofile == null) { 859 LINEAR_RGBprofile = getStandardProfile("LINEAR_RGB.pf"); 860 } 861 thisProfile = LINEAR_RGBprofile; 862 } 863 864 break; 865 866 default: 867 throw new IllegalArgumentException ("Unknown color space"); 868 } 869 870 return thisProfile; 871 } 872 873 876 private static ICC_Profile getStandardProfile(final String name) { 877 878 return (ICC_Profile ) AccessController.doPrivileged( 879 new PrivilegedAction () { 880 public Object run() { 881 ICC_Profile p = null; 882 try { 883 p = getInstance (name); 884 } catch (IOException ex) { 885 throw new IllegalArgumentException ( 886 "Can't load standard profile: " + name); 887 } 888 return p; 889 } 890 }); 891 } 892 893 919 public static ICC_Profile getInstance(String fileName) throws IOException { 920 ICC_Profile thisProfile; 921 FileInputStream fis; 922 923 SecurityManager security = System.getSecurityManager(); 924 if (security != null) { 925 security.checkRead(fileName); 926 } 927 928 if ((fis = openProfile(fileName)) == null) { 929 throw new IOException ("Cannot open file " + fileName); 930 } 931 932 thisProfile = getInstance(fis); 933 934 fis.close(); 935 936 return thisProfile; 937 } 938 939 940 955 public static ICC_Profile getInstance(InputStream s) throws IOException { 956 byte profileData[]; 957 958 if (s instanceof ProfileDeferralInfo) { 959 960 return getDeferredInstance((ProfileDeferralInfo) s); 961 } 962 963 if ((profileData = getProfileDataFromStream(s)) == null) { 964 throw new IllegalArgumentException ("Invalid ICC Profile Data"); 965 } 966 967 return getInstance(profileData); 968 } 969 970 971 static byte[] getProfileDataFromStream(InputStream s) throws IOException { 972 byte profileData[]; 973 int profileSize; 974 975 byte header[] = new byte[128]; 976 int bytestoread = 128; 977 int bytesread = 0; 978 int n; 979 980 while (bytestoread != 0) { 981 if ((n = s.read(header, bytesread, bytestoread)) < 0) { 982 return null; 983 } 984 bytesread += n; 985 bytestoread -= n; 986 } 987 if (header[36] != 0x61 || header[37] != 0x63 || 988 header[38] != 0x73 || header[39] != 0x70) { 989 return null; 990 } 991 profileSize = ((header[0] & 0xff) << 24) | 992 ((header[1] & 0xff) << 16) | 993 ((header[2] & 0xff) << 8) | 994 (header[3] & 0xff); 995 profileData = new byte[profileSize]; 996 System.arraycopy(header, 0, profileData, 0, 128); 997 bytestoread = profileSize - 128; 998 bytesread = 128; 999 while (bytestoread != 0) { 1000 if ((n = s.read(profileData, bytesread, bytestoread)) < 0) { 1001 return null; 1002 } 1003 bytesread += n; 1004 bytestoread -= n; 1005 } 1006 1007 return profileData; 1008 } 1009 1010 1011 1023 static ICC_Profile getDeferredInstance(ProfileDeferralInfo pdi) 1024 throws IOException { 1025 1026 if (!ProfileDeferralMgr.deferring) { 1027 return getStandardProfile(pdi.filename); 1028 } 1029 if (pdi.colorSpaceType == ColorSpace.TYPE_RGB) { 1030 return new ICC_ProfileRGB (pdi); 1031 } else if (pdi.colorSpaceType == ColorSpace.TYPE_GRAY) { 1032 return new ICC_ProfileGray (pdi); 1033 } else { 1034 return new ICC_Profile (pdi); 1035 } 1036 } 1037 1038 1039 void activateDeferredProfile() { 1040 long[] theID = new long [1]; 1041 byte profileData[]; 1042 FileInputStream fis; 1043 String fileName = deferralInfo.filename; 1044 1045 profileActivator = null; 1046 deferralInfo = null; 1047 if ((fis = openProfile(fileName)) == null) { 1048 throw new IllegalArgumentException ("Cannot open file " + fileName); 1049 } 1050 try { 1051 profileData = getProfileDataFromStream(fis); 1052 fis.close(); 1053 } 1054 catch (IOException e) { 1055 throw new IllegalArgumentException ("Invalid ICC Profile Data" + 1056 fileName); 1057 } 1058 if (profileData == null) { 1059 throw new IllegalArgumentException ("Invalid ICC Profile Data" + 1060 fileName); 1061 } 1062 try { 1063 CMM.checkStatus(CMM.cmmLoadProfile(profileData, theID)); 1064 } catch (CMMException c) { 1065 throw new IllegalArgumentException ("Invalid ICC Profile Data" + 1066 fileName); 1067 } 1068 ID = theID[0]; 1069 } 1070 1071 1072 1076 public int getMajorVersion() { 1077 byte[] theHeader; 1078 1079 theHeader = getData(icSigHead); 1081 1082 return (int) theHeader[8]; 1083 } 1084 1085 1089 public int getMinorVersion() { 1090 byte[] theHeader; 1091 1092 theHeader = getData(icSigHead); 1094 1095 return (int) theHeader[9]; 1096 } 1097 1098 1102 public int getProfileClass() { 1103 byte[] theHeader; 1104 int theClassSig, theClass; 1105 1106 if (deferralInfo != null) { 1107 return deferralInfo.profileClass; 1111 } 1112 1113 theHeader = getData(icSigHead); 1114 1115 theClassSig = intFromBigEndian (theHeader, icHdrDeviceClass); 1116 1117 switch (theClassSig) { 1118 case icSigInputClass: 1119 theClass = CLASS_INPUT; 1120 break; 1121 1122 case icSigDisplayClass: 1123 theClass = CLASS_DISPLAY; 1124 break; 1125 1126 case icSigOutputClass: 1127 theClass = CLASS_OUTPUT; 1128 break; 1129 1130 case icSigLinkClass: 1131 theClass = CLASS_DEVICELINK; 1132 break; 1133 1134 case icSigColorSpaceClass: 1135 theClass = CLASS_COLORSPACECONVERSION; 1136 break; 1137 1138 case icSigAbstractClass: 1139 theClass = CLASS_ABSTRACT; 1140 break; 1141 1142 case icSigNamedColorClass: 1143 theClass = CLASS_NAMEDCOLOR; 1144 break; 1145 1146 default: 1147 throw new IllegalArgumentException ("Unknown profile class"); 1148 } 1149 1150 return theClass; 1151 } 1152 1153 1165 public int getColorSpaceType() { 1166 if (deferralInfo != null) { 1167 return deferralInfo.colorSpaceType; 1171 } 1172 return getColorSpaceType(ID); 1173 } 1174 1175 static int getColorSpaceType(long profileID) { 1176 byte[] theHeader; 1177 int theColorSpaceSig, theColorSpace; 1178 1179 theHeader = getData(profileID, icSigHead); 1180 theColorSpaceSig = intFromBigEndian(theHeader, icHdrColorSpace); 1181 theColorSpace = iccCStoJCS (theColorSpaceSig); 1182 return theColorSpace; 1183 } 1184 1185 1197 public int getPCSType() { 1198 if (ProfileDeferralMgr.deferring) { 1199 ProfileDeferralMgr.activateProfiles(); 1200 } 1201 return getPCSType(ID); 1202 } 1203 1204 1205 static int getPCSType(long profileID) { 1206 byte[] theHeader; 1207 int thePCSSig, thePCS; 1208 1209 theHeader = getData(profileID, icSigHead); 1210 thePCSSig = intFromBigEndian(theHeader, icHdrPcs); 1211 thePCS = iccCStoJCS(thePCSSig); 1212 return thePCS; 1213 } 1214 1215 1216 1224 public void write(String fileName) throws IOException { 1225 FileOutputStream outputFile; 1226 byte profileData[]; 1227 1228 profileData = getData(); 1230 outputFile = new FileOutputStream (fileName); 1231 outputFile.write(profileData); 1232 outputFile.close (); 1233 } 1234 1235 1236 1244 public void write(OutputStream s) throws IOException { 1245 byte profileData[]; 1246 1247 profileData = getData(); 1249 s.write(profileData); 1250 } 1251 1252 1253 1258 public byte[] getData() { 1259 int[] profileSize = new int [1]; 1260 byte[] profileData; 1261 1262 if (ProfileDeferralMgr.deferring) { 1263 ProfileDeferralMgr.activateProfiles(); 1264 } 1265 1266 1267 CMM.checkStatus(CMM.cmmGetProfileSize(ID, profileSize)); 1268 1269 profileData = new byte [profileSize[0]]; 1270 1271 1272 CMM.checkStatus(CMM.cmmGetProfileData(ID, profileData)); 1273 1274 return profileData; 1275 } 1276 1277 1278 1293 public byte[] getData(int tagSignature) { 1294 1295 if (ProfileDeferralMgr.deferring) { 1296 ProfileDeferralMgr.activateProfiles(); 1297 } 1298 1299 return getData(ID, tagSignature); 1300 } 1301 1302 1303 static byte[] getData(long profileID, int tagSignature) { 1304 int[] tagSize = new int [1]; 1305 byte[] tagData; 1306 1307 try { 1308 1309 CMM.checkStatus(CMM.cmmGetTagSize(profileID, tagSignature, 1310 tagSize)); 1311 1312 tagData = new byte[tagSize[0]]; 1313 1314 1315 CMM.checkStatus(CMM.cmmGetTagData(profileID, tagSignature, 1316 tagData)); 1317 } catch(CMMException c) { 1318 tagData = null; 1319 } 1320 1321 return tagData; 1322 } 1323 1324 1335 public void setData(int tagSignature, byte[] tagData) { 1336 1337 if (ProfileDeferralMgr.deferring) { 1338 ProfileDeferralMgr.activateProfiles(); 1339 } 1340 1341 CMM.checkStatus(CMM.cmmSetTagData(ID, tagSignature, tagData)); 1342 } 1343 1344 1349 void setRenderingIntent(int renderingIntent) { 1350 byte[] theHeader = getData(icSigHead); 1352 intToBigEndian (renderingIntent, theHeader, icHdrRenderingIntent); 1353 1354 setData (icSigHead, theHeader); 1355 } 1356 1357 1358 1364 int getRenderingIntent() { 1365 byte[] theHeader = getData(icSigHead); 1367 1368 int renderingIntent = intFromBigEndian(theHeader, icHdrRenderingIntent); 1369 1370 return renderingIntent; 1371 } 1372 1373 1374 1385 public int getNumComponents() { 1386 byte[] theHeader; 1387 int theColorSpaceSig, theNumComponents; 1388 1389 if (deferralInfo != null) { 1390 return deferralInfo.numComponents; 1394 } 1395 theHeader = getData(icSigHead); 1396 1397 theColorSpaceSig = intFromBigEndian (theHeader, icHdrColorSpace); 1398 1399 switch (theColorSpaceSig) { 1400 case icSigGrayData: 1401 theNumComponents = 1; 1402 break; 1403 1404 case icSigSpace2CLR: 1405 theNumComponents = 2; 1406 break; 1407 1408 case icSigXYZData: 1409 case icSigLabData: 1410 case icSigLuvData: 1411 case icSigYCbCrData: 1412 case icSigYxyData: 1413 case icSigRgbData: 1414 case icSigHsvData: 1415 case icSigHlsData: 1416 case icSigCmyData: 1417 case icSigSpace3CLR: 1418 theNumComponents = 3; 1419 break; 1420 1421 case icSigCmykData: 1422 case icSigSpace4CLR: 1423 theNumComponents = 4; 1424 break; 1425 1426 case icSigSpace5CLR: 1427 theNumComponents = 5; 1428 break; 1429 1430 case icSigSpace6CLR: 1431 theNumComponents = 6; 1432 break; 1433 1434 case icSigSpace7CLR: 1435 theNumComponents = 7; 1436 break; 1437 1438 case icSigSpace8CLR: 1439 theNumComponents = 8; 1440 break; 1441 1442 case icSigSpace9CLR: 1443 theNumComponents = 9; 1444 break; 1445 1446 case icSigSpaceACLR: 1447 theNumComponents = 10; 1448 break; 1449 1450 case icSigSpaceBCLR: 1451 theNumComponents = 11; 1452 break; 1453 1454 case icSigSpaceCCLR: 1455 theNumComponents = 12; 1456 break; 1457 1458 case icSigSpaceDCLR: 1459 theNumComponents = 13; 1460 break; 1461 1462 case icSigSpaceECLR: 1463 theNumComponents = 14; 1464 break; 1465 1466 case icSigSpaceFCLR: 1467 theNumComponents = 15; 1468 break; 1469 1470 default: 1471 throw new ProfileDataException ("invalid ICC color space"); 1472 } 1473 1474 return theNumComponents; 1475 } 1476 1477 1478 1482 float[] getMediaWhitePoint() { 1483 return getXYZTag(icSigMediaWhitePointTag); 1484 1485 } 1486 1487 1488 1492 float[] getXYZTag(int theTagSignature) { 1493 byte[] theData; 1494 float[] theXYZNumber; 1495 int i1, i2, theS15Fixed16; 1496 1497 theData = getData(theTagSignature); 1498 1500 1501 theXYZNumber = new float [3]; 1502 1503 1504 for (i1 = 0, i2 = icXYZNumberX; i1 < 3; i1++, i2 += 4) { 1505 theS15Fixed16 = intFromBigEndian(theData, i2); 1506 theXYZNumber [i1] = ((float) theS15Fixed16) / 65536.0f; 1507 } 1508 return theXYZNumber; 1509 } 1510 1511 1512 1523 float getGamma(int theTagSignature) { 1524 byte[] theTRCData; 1525 float theGamma; 1526 int theU8Fixed8; 1527 1528 theTRCData = getData(theTagSignature); 1529 1531 1532 if (intFromBigEndian (theTRCData, icCurveCount) != 1) { 1533 throw new ProfileDataException ("TRC is not a gamma"); 1534 } 1535 1536 1537 theU8Fixed8 = (shortFromBigEndian(theTRCData, icCurveData)) & 0xffff; 1538 1539 theGamma = ((float) theU8Fixed8) / 256.0f; 1540 1541 return theGamma; 1542 } 1543 1544 1545 1565 short[] getTRC(int theTagSignature) { 1566 byte[] theTRCData; 1567 short[] theTRC; 1568 int i1, i2, nElements, theU8Fixed8; 1569 1570 theTRCData = getData(theTagSignature); 1571 1573 1574 nElements = intFromBigEndian(theTRCData, icCurveCount); 1575 1576 if (nElements == 1) { 1577 throw new ProfileDataException ("TRC is not a table"); 1578 } 1579 1580 1581 theTRC = new short [nElements]; 1582 1583 for (i1 = 0, i2 = icCurveData; i1 < nElements; i1++, i2 += 2) { 1584 theTRC[i1] = shortFromBigEndian(theTRCData, i2); 1585 } 1586 1587 return theTRC; 1588 } 1589 1590 1591 1592 static int iccCStoJCS(int theColorSpaceSig) { 1593 int theColorSpace; 1594 1595 switch (theColorSpaceSig) { 1596 case icSigXYZData: 1597 theColorSpace = ColorSpace.TYPE_XYZ; 1598 break; 1599 1600 case icSigLabData: 1601 theColorSpace = ColorSpace.TYPE_Lab; 1602 break; 1603 1604 case icSigLuvData: 1605 theColorSpace = ColorSpace.TYPE_Luv; 1606 break; 1607 1608 case icSigYCbCrData: 1609 theColorSpace = ColorSpace.TYPE_YCbCr; 1610 break; 1611 1612 case icSigYxyData: 1613 theColorSpace = ColorSpace.TYPE_Yxy; 1614 break; 1615 1616 case icSigRgbData: 1617 theColorSpace = ColorSpace.TYPE_RGB; 1618 break; 1619 1620 case icSigGrayData: 1621 theColorSpace = ColorSpace.TYPE_GRAY; 1622 break; 1623 1624 case icSigHsvData: 1625 theColorSpace = ColorSpace.TYPE_HSV; 1626 break; 1627 1628 case icSigHlsData: 1629 theColorSpace = ColorSpace.TYPE_HLS; 1630 break; 1631 1632 case icSigCmykData: 1633 theColorSpace = ColorSpace.TYPE_CMYK; 1634 break; 1635 1636 case icSigCmyData: 1637 theColorSpace = ColorSpace.TYPE_CMY; 1638 break; 1639 1640 case icSigSpace2CLR: 1641 theColorSpace = ColorSpace.TYPE_2CLR; 1642 break; 1643 1644 case icSigSpace3CLR: 1645 theColorSpace = ColorSpace.TYPE_3CLR; 1646 break; 1647 1648 case icSigSpace4CLR: 1649 theColorSpace = ColorSpace.TYPE_4CLR; 1650 break; 1651 1652 case icSigSpace5CLR: 1653 theColorSpace = ColorSpace.TYPE_5CLR; 1654 break; 1655 1656 case icSigSpace6CLR: 1657 theColorSpace = ColorSpace.TYPE_6CLR; 1658 break; 1659 1660 case icSigSpace7CLR: 1661 theColorSpace = ColorSpace.TYPE_7CLR; 1662 break; 1663 1664 case icSigSpace8CLR: 1665 theColorSpace = ColorSpace.TYPE_8CLR; 1666 break; 1667 1668 case icSigSpace9CLR: 1669 theColorSpace = ColorSpace.TYPE_9CLR; 1670 break; 1671 1672 case icSigSpaceACLR: 1673 theColorSpace = ColorSpace.TYPE_ACLR; 1674 break; 1675 1676 case icSigSpaceBCLR: 1677 theColorSpace = ColorSpace.TYPE_BCLR; 1678 break; 1679 1680 case icSigSpaceCCLR: 1681 theColorSpace = ColorSpace.TYPE_CCLR; 1682 break; 1683 1684 case icSigSpaceDCLR: 1685 theColorSpace = ColorSpace.TYPE_DCLR; 1686 break; 1687 1688 case icSigSpaceECLR: 1689 theColorSpace = ColorSpace.TYPE_ECLR; 1690 break; 1691 1692 case icSigSpaceFCLR: 1693 theColorSpace = ColorSpace.TYPE_FCLR; 1694 break; 1695 1696 default: 1697 throw new IllegalArgumentException ("Unknown color space"); 1698 } 1699 1700 return theColorSpace; 1701 } 1702 1703 1704 static int intFromBigEndian(byte[] array, int index) { 1705 return (((array[index] & 0xff) << 24) | 1706 ((array[index+1] & 0xff) << 16) | 1707 ((array[index+2] & 0xff) << 8) | 1708 (array[index+3] & 0xff)); 1709 } 1710 1711 1712 static void intToBigEndian(int value, byte[] array, int index) { 1713 array[index] = (byte) (value >> 24); 1714 array[index+1] = (byte) (value >> 16); 1715 array[index+2] = (byte) (value >> 8); 1716 array[index+3] = (byte) (value); 1717 } 1718 1719 1720 static short shortFromBigEndian(byte[] array, int index) { 1721 return (short) (((array[index] & 0xff) << 8) | 1722 (array[index+1] & 0xff)); 1723 } 1724 1725 1726 static void shortToBigEndian(short value, byte[] array, int index) { 1727 array[index] = (byte) (value >> 8); 1728 array[index+1] = (byte) (value); 1729 } 1730 1731 1732 1741 private static FileInputStream openProfile(final String fileName) { 1742 return (FileInputStream )java.security.AccessController.doPrivileged( 1743 new java.security.PrivilegedAction () { 1744 public Object run() { 1745 return privilegedOpenProfile(fileName); 1746 } 1747 }); 1748 } 1749 1750 1754 private static FileInputStream privilegedOpenProfile(String fileName) { 1755 FileInputStream fis = null; 1756 String path, dir, fullPath; 1757 1758 File f = new File (fileName); 1759 1760 if ((!f.isFile()) && 1761 ((path = System.getProperty("java.iccprofile.path")) != null)){ 1762 1763 StringTokenizer st = 1764 new StringTokenizer (path, File.pathSeparator); 1765 while (st.hasMoreTokens() && (!f.isFile())) { 1766 dir = st.nextToken(); 1767 fullPath = dir + File.separatorChar + fileName; 1768 f = new File (fullPath); 1769 } 1770 } 1771 1772 if ((!f.isFile()) && 1773 ((path = System.getProperty("java.class.path")) != null)) { 1774 1775 StringTokenizer st = 1776 new StringTokenizer (path, File.pathSeparator); 1777 while (st.hasMoreTokens() && (!f.isFile())) { 1778 dir = st.nextToken(); 1779 fullPath = dir + File.separatorChar + fileName; 1780 f = new File (fullPath); 1781 } 1782 } 1783 1784 if (!f.isFile()) { 1785 dir = System.getProperty("java.home") + 1786 File.separatorChar + "lib" + File.separatorChar + "cmm"; 1787 fullPath = dir + File.separatorChar + fileName; 1788 f = new File (fullPath); 1789 } 1790 1791 if (f.isFile()) { 1792 try { 1793 fis = new FileInputStream (f); 1794 } catch (FileNotFoundException e) { 1795 } 1796 } 1797 return fis; 1798 } 1799 1800 1801 1827 1828 1829 1836 private int iccProfileSerializedDataVersion = 1; 1837 1838 1839 1863 private void writeObject(ObjectOutputStream s) 1864 throws IOException 1865 { 1866 s.defaultWriteObject(); 1867 1868 String csName = null; 1869 if (this == sRGBprofile) { 1870 csName = "CS_sRGB"; 1871 } else if (this == XYZprofile) { 1872 csName = "CS_CIEXYZ"; 1873 } else if (this == PYCCprofile) { 1874 csName = "CS_PYCC"; 1875 } else if (this == GRAYprofile) { 1876 csName = "CS_GRAY"; 1877 } else if (this == LINEAR_RGBprofile) { 1878 csName = "CS_LINEAR_RGB"; 1879 } 1880 1881 byte[] data = null; 1886 if (csName == null) { 1887 data = getData(); 1889 } 1890 1891 s.writeObject(csName); 1892 s.writeObject(data); 1893 } 1894 1895 private transient ICC_Profile resolvedDeserializedProfile; 1898 1899 1931 private void readObject(ObjectInputStream s) 1932 throws IOException , ClassNotFoundException 1933 { 1934 s.defaultReadObject(); 1935 1936 String csName = (String )s.readObject(); 1937 byte[] data = (byte[])s.readObject(); 1938 1939 int cspace = 0; boolean isKnownPredefinedCS = false; 1941 if (csName != null) { 1942 isKnownPredefinedCS = true; 1943 if (csName.equals("CS_sRGB")) { 1944 cspace = ColorSpace.CS_sRGB; 1945 } else if (csName.equals("CS_CIEXYZ")) { 1946 cspace = ColorSpace.CS_CIEXYZ; 1947 } else if (csName.equals("CS_PYCC")) { 1948 cspace = ColorSpace.CS_PYCC; 1949 } else if (csName.equals("CS_GRAY")) { 1950 cspace = ColorSpace.CS_GRAY; 1951 } else if (csName.equals("CS_LINEAR_RGB")) { 1952 cspace = ColorSpace.CS_LINEAR_RGB; 1953 } else { 1954 isKnownPredefinedCS = false; 1955 } 1956 } 1957 1958 if (isKnownPredefinedCS) { 1959 resolvedDeserializedProfile = getInstance(cspace); 1960 } else { 1961 resolvedDeserializedProfile = getInstance(data); 1962 } 1963 } 1964 1965 1972 protected Object readResolve() throws ObjectStreamException { 1973 return resolvedDeserializedProfile; 1974 } 1975} 1976 | Popular Tags |