1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
999 return thisProfile;
1000 }
1001
1002
1003 /**
1004 * Constructs an ICC_Profile corresponding to the data in an InputStream.
1005 * This method throws an IllegalArgumentException if the stream does not
1006 * contain valid ICC Profile data. It throws an IOException if an I/O
1007 * error occurs while reading the stream.
1008 * @param s The input stream from which to read the profile data.
1009 *
1010 * @return an {@code ICC_Profile} object corresponding to the
1011 * data in the specified {@code InputStream}.
1012 *
1013 * @exception IOException If an I/O error occurs while reading the stream.
1014 *
1015 * @exception IllegalArgumentException If the stream does not
1016 * contain valid ICC Profile data.
1017 */
1018 public static ICC_Profile getInstance(InputStream s) throws IOException {
1019 byte profileData[];
1020
1021 if (s instanceof ProfileDeferralInfo) {
1022 /* hack to detect profiles whose loading can be deferred */
1023 return getDeferredInstance((ProfileDeferralInfo) s);
1024 }
1025
1026 if ((profileData = getProfileDataFromStream(s)) == null) {
1027 throw new IllegalArgumentException("Invalid ICC Profile Data");
1028 }
1029
1030 return getInstance(profileData);
1031 }
1032
1033
1034 static byte[] getProfileDataFromStream(InputStream s) throws IOException {
1035 byte profileData[];
1036 int profileSize;
1037
1038 byte header[] = new byte[128];
1039 int bytestoread = 128;
1040 int bytesread = 0;
1041 int n;
1042
1043 while (bytestoread != 0) {
1044 if ((n = s.read(header, bytesread, bytestoread)) < 0) {
1045 return null;
1046 }
1047 bytesread += n;
1048 bytestoread -= n;
1049 }
1050 if (header[36] != 0x61 || header[37] != 0x63 ||
1051 header[38] != 0x73 || header[39] != 0x70) {
1052 return null; /* not a valid profile */
1053 }
1054 profileSize = ((header[0] & 0xff) << 24) |
1055 ((header[1] & 0xff) << 16) |
1056 ((header[2] & 0xff) << 8) |
1057 (header[3] & 0xff);
1058 profileData = new byte[profileSize];
1081 * when loading this profile.
1082 * If deferring is enabled, then the deferred activation
1083 * code will take care of access privileges.
1084 * @see #activateDeferredProfile()
1085 */
1086 static ICC_Profile getDeferredInstance(ProfileDeferralInfo pdi) {
1087 if (!ProfileDeferralMgr.deferring) {
1088 return getStandardProfile(pdi.filename);
1089 }
1090 if (pdi.colorSpaceType == ColorSpace.TYPE_RGB) {
1091 return new ICC_ProfileRGB(pdi);
1092 } else if (pdi.colorSpaceType == ColorSpace.TYPE_GRAY) {
1093 return new ICC_ProfileGray(pdi);
1094 } else {
1095 return new ICC_Profile(pdi);
1096 }
1097 }
1098
1099
1100 void activateDeferredProfile() throws ProfileDataException {
1101 byte profileData[];
1102 final String fileName = deferralInfo.filename;
1103
1104 profileActivator = null;
1105 deferralInfo = null;
1106 InputStream is = getStandardProfileInputStream(fileName);
1107 if (is == null) {
1108 throw new ProfileDataException("Cannot open file " + fileName);
1109 }
1110 try {
1111 profileData = getProfileDataFromStream(is);
1112 is.close(); /* close the file */
1113 }
1114 catch (IOException e) {
1115 ProfileDataException pde = new
1116 ProfileDataException("Invalid ICC Profile Data" + fileName);
1117 pde.initCause(e);
1118 throw pde;
1119 }
1120 if (profileData == null) {
1121 throw new ProfileDataException("Invalid ICC Profile Data" +
1269 byte[] theHeader;
1270 int thePCSSig, thePCS;
1271
1272 theHeader = getData(p, icSigHead);
1273 thePCSSig = intFromBigEndian(theHeader, icHdrPcs);
1274 thePCS = iccCStoJCS(thePCSSig);
1275 return thePCS;
1276 }
1277
1278
1279 /**
1280 * Write this ICC_Profile to a file.
1281 *
1282 * @param fileName The file to write the profile data to.
1283 *
1284 * @exception IOException If the file cannot be opened for writing
1285 * or an I/O error occurs while writing to the file.
1286 */
1287 public void write(String fileName) throws IOException {
1288 FileOutputStream outputFile;
1289 byte profileData[];
1290
1291 profileData = getData(); /* this will activate deferred
1292 profiles if necessary */
1293 outputFile = new FileOutputStream(fileName);
1294 outputFile.write(profileData);
1295 outputFile.close ();
1296 }
1297
1298
1299 /**
1300 * Write this ICC_Profile to an OutputStream.
1301 *
1302 * @param s The stream to write the profile data to.
1303 *
1304 * @exception IOException If an I/O error occurs while writing to the
1305 * stream.
1306 */
1307 public void write(OutputStream s) throws IOException {
1308 byte profileData[];
1309
1310 profileData = getData(); /* this will activate deferred
1311 profiles if necessary */
1312 s.write(profileData);
1313 }
1314
1315
1316 /**
1317 * Returns a byte array corresponding to the data of this ICC_Profile.
1318 * @return A byte array that contains the profile data.
1319 * @see #setData(int, byte[])
1320 */
1321 public byte[] getData() {
1322 int profileSize;
1323 byte[] profileData;
1324
1325 if (ProfileDeferralMgr.deferring) {
1326 ProfileDeferralMgr.activateProfiles();
1327 }
1328
|
1 /*
2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
999 return thisProfile;
1000 }
1001
1002
1003 /**
1004 * Constructs an ICC_Profile corresponding to the data in an InputStream.
1005 * This method throws an IllegalArgumentException if the stream does not
1006 * contain valid ICC Profile data. It throws an IOException if an I/O
1007 * error occurs while reading the stream.
1008 * @param s The input stream from which to read the profile data.
1009 *
1010 * @return an {@code ICC_Profile} object corresponding to the
1011 * data in the specified {@code InputStream}.
1012 *
1013 * @exception IOException If an I/O error occurs while reading the stream.
1014 *
1015 * @exception IllegalArgumentException If the stream does not
1016 * contain valid ICC Profile data.
1017 */
1018 public static ICC_Profile getInstance(InputStream s) throws IOException {
1019 byte[] profileData;
1020
1021 if (s instanceof ProfileDeferralInfo) {
1022 /* hack to detect profiles whose loading can be deferred */
1023 return getDeferredInstance((ProfileDeferralInfo) s);
1024 }
1025
1026 if ((profileData = getProfileDataFromStream(s)) == null) {
1027 throw new IllegalArgumentException("Invalid ICC Profile Data");
1028 }
1029
1030 return getInstance(profileData);
1031 }
1032
1033
1034 static byte[] getProfileDataFromStream(InputStream s) throws IOException {
1035 byte[] profileData;
1036 int profileSize;
1037
1038 byte[] header = new byte[128];
1039 int bytestoread = 128;
1040 int bytesread = 0;
1041 int n;
1042
1043 while (bytestoread != 0) {
1044 if ((n = s.read(header, bytesread, bytestoread)) < 0) {
1045 return null;
1046 }
1047 bytesread += n;
1048 bytestoread -= n;
1049 }
1050 if (header[36] != 0x61 || header[37] != 0x63 ||
1051 header[38] != 0x73 || header[39] != 0x70) {
1052 return null; /* not a valid profile */
1053 }
1054 profileSize = ((header[0] & 0xff) << 24) |
1055 ((header[1] & 0xff) << 16) |
1056 ((header[2] & 0xff) << 8) |
1057 (header[3] & 0xff);
1058 profileData = new byte[profileSize];
1081 * when loading this profile.
1082 * If deferring is enabled, then the deferred activation
1083 * code will take care of access privileges.
1084 * @see #activateDeferredProfile()
1085 */
1086 static ICC_Profile getDeferredInstance(ProfileDeferralInfo pdi) {
1087 if (!ProfileDeferralMgr.deferring) {
1088 return getStandardProfile(pdi.filename);
1089 }
1090 if (pdi.colorSpaceType == ColorSpace.TYPE_RGB) {
1091 return new ICC_ProfileRGB(pdi);
1092 } else if (pdi.colorSpaceType == ColorSpace.TYPE_GRAY) {
1093 return new ICC_ProfileGray(pdi);
1094 } else {
1095 return new ICC_Profile(pdi);
1096 }
1097 }
1098
1099
1100 void activateDeferredProfile() throws ProfileDataException {
1101 byte[] profileData;
1102 final String fileName = deferralInfo.filename;
1103
1104 profileActivator = null;
1105 deferralInfo = null;
1106 InputStream is = getStandardProfileInputStream(fileName);
1107 if (is == null) {
1108 throw new ProfileDataException("Cannot open file " + fileName);
1109 }
1110 try {
1111 profileData = getProfileDataFromStream(is);
1112 is.close(); /* close the file */
1113 }
1114 catch (IOException e) {
1115 ProfileDataException pde = new
1116 ProfileDataException("Invalid ICC Profile Data" + fileName);
1117 pde.initCause(e);
1118 throw pde;
1119 }
1120 if (profileData == null) {
1121 throw new ProfileDataException("Invalid ICC Profile Data" +
1269 byte[] theHeader;
1270 int thePCSSig, thePCS;
1271
1272 theHeader = getData(p, icSigHead);
1273 thePCSSig = intFromBigEndian(theHeader, icHdrPcs);
1274 thePCS = iccCStoJCS(thePCSSig);
1275 return thePCS;
1276 }
1277
1278
1279 /**
1280 * Write this ICC_Profile to a file.
1281 *
1282 * @param fileName The file to write the profile data to.
1283 *
1284 * @exception IOException If the file cannot be opened for writing
1285 * or an I/O error occurs while writing to the file.
1286 */
1287 public void write(String fileName) throws IOException {
1288 FileOutputStream outputFile;
1289 byte[] profileData;
1290
1291 profileData = getData(); /* this will activate deferred
1292 profiles if necessary */
1293 outputFile = new FileOutputStream(fileName);
1294 outputFile.write(profileData);
1295 outputFile.close ();
1296 }
1297
1298
1299 /**
1300 * Write this ICC_Profile to an OutputStream.
1301 *
1302 * @param s The stream to write the profile data to.
1303 *
1304 * @exception IOException If an I/O error occurs while writing to the
1305 * stream.
1306 */
1307 public void write(OutputStream s) throws IOException {
1308 byte[] profileData;
1309
1310 profileData = getData(); /* this will activate deferred
1311 profiles if necessary */
1312 s.write(profileData);
1313 }
1314
1315
1316 /**
1317 * Returns a byte array corresponding to the data of this ICC_Profile.
1318 * @return A byte array that contains the profile data.
1319 * @see #setData(int, byte[])
1320 */
1321 public byte[] getData() {
1322 int profileSize;
1323 byte[] profileData;
1324
1325 if (ProfileDeferralMgr.deferring) {
1326 ProfileDeferralMgr.activateProfiles();
1327 }
1328
|