Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.desktop/share/classes/sun/java2d/cmm/lcms/LCMS.java
66647 views
1
/*
2
* Copyright (c) 2007, 2021, 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
23
* questions.
24
*/
25
26
package sun.java2d.cmm.lcms;
27
28
import java.awt.color.CMMException;
29
import java.awt.color.ICC_Profile;
30
import java.util.concurrent.locks.StampedLock;
31
32
import sun.java2d.cmm.ColorTransform;
33
import sun.java2d.cmm.PCMM;
34
import sun.java2d.cmm.Profile;
35
36
final class LCMS implements PCMM {
37
38
/**
39
* Prevent changing profiles data during transform creation.
40
*/
41
private static final StampedLock lock = new StampedLock();
42
43
/* methods invoked from ICC_Profile */
44
@Override
45
public Profile loadProfile(byte[] data) {
46
final Object disposerRef = new Object();
47
48
final long ptr = loadProfileNative(data, disposerRef);
49
50
if (ptr != 0L) {
51
return new LCMSProfile(ptr, disposerRef);
52
}
53
return null;
54
}
55
56
private static LCMSProfile getLcmsProfile(Profile p) {
57
if (p instanceof LCMSProfile) {
58
return (LCMSProfile)p;
59
}
60
throw new CMMException("Invalid profile: " + p);
61
}
62
63
/**
64
* Writes supplied data as a tag into the profile.
65
* Destroys old profile, if new one was successfully
66
* created.
67
*
68
* Returns valid pointer to new profile.
69
*
70
* Throws CMMException if operation fails, preserve old profile from
71
* destruction.
72
*/
73
static native void setTagDataNative(long ptr, int tagSignature, byte[] data);
74
static native byte[] getProfileDataNative(long ptr);
75
static native byte[] getTagNative(long profileID, int signature);
76
private static native long loadProfileNative(byte[] data, Object ref);
77
78
@Override
79
public byte[] getProfileData(Profile p) {
80
return getLcmsProfile(p).getProfileData();
81
}
82
83
@Override
84
public byte[] getTagData(Profile p, int tagSignature) {
85
return getLcmsProfile(p).getTag(tagSignature);
86
}
87
88
@Override
89
public void setTagData(Profile p, int tagSignature, byte[] data) {
90
long stamp = lock.writeLock();
91
try {
92
getLcmsProfile(p).setTag(tagSignature, data);
93
} finally {
94
lock.unlockWrite(stamp);
95
}
96
}
97
98
static synchronized native LCMSProfile getProfileID(ICC_Profile profile);
99
100
/* Helper method used from LCMSColorTransfrom */
101
static long createTransform(
102
LCMSProfile[] profiles, int renderType,
103
int inFormatter, boolean isInIntPacked,
104
int outFormatter, boolean isOutIntPacked,
105
Object disposerRef)
106
{
107
long[] ptrs = new long[profiles.length];
108
long stamp = lock.readLock();
109
try {
110
for (int i = 0; i < profiles.length; i++) {
111
if (profiles[i] == null) {
112
throw new CMMException("Unknown profile ID");
113
}
114
ptrs[i] = profiles[i].getLcmsPtr();
115
}
116
117
return createNativeTransform(ptrs, renderType, inFormatter,
118
isInIntPacked, outFormatter, isOutIntPacked, disposerRef);
119
} finally {
120
lock.unlockRead(stamp);
121
}
122
}
123
124
private static native long createNativeTransform(
125
long[] profileIDs, int renderType,
126
int inFormatter, boolean isInIntPacked,
127
int outFormatter, boolean isOutIntPacked,
128
Object disposerRef);
129
130
/**
131
* Constructs ColorTransform object corresponding to an ICC_profile
132
*/
133
public ColorTransform createTransform(ICC_Profile profile,
134
int renderType,
135
int transformType)
136
{
137
return new LCMSTransform(profile, renderType, renderType);
138
}
139
140
/**
141
* Constructs an ColorTransform object from a list of ColorTransform
142
* objects
143
*/
144
public synchronized ColorTransform createTransform(
145
ColorTransform[] transforms)
146
{
147
return new LCMSTransform(transforms);
148
}
149
150
/* methods invoked from LCMSTransform */
151
public static native void colorConvert(long trans,
152
LCMSImageLayout src,
153
LCMSImageLayout dest);
154
155
public static native void initLCMS(Class<?> Trans, Class<?> IL, Class<?> Pf);
156
157
private LCMS() {};
158
159
private static LCMS theLcms = null;
160
161
@SuppressWarnings("removal")
162
static synchronized PCMM getModule() {
163
if (theLcms != null) {
164
return theLcms;
165
}
166
167
java.security.AccessController.doPrivileged(
168
new java.security.PrivilegedAction<Object>() {
169
public Object run() {
170
/* We need to load awt here because of usage trace and
171
* disposer frameworks
172
*/
173
System.loadLibrary("awt");
174
System.loadLibrary("lcms");
175
return null;
176
}
177
});
178
179
initLCMS(LCMSTransform.class, LCMSImageLayout.class, ICC_Profile.class);
180
181
theLcms = new LCMS();
182
183
return theLcms;
184
}
185
}
186
187