author | bae |
Thu, 01 Jul 2010 12:04:14 +0400 | |
changeset 6868 | f9131565859e |
parent 5506 | 202f599c92aa |
child 6879 | 13924eecc282 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include <stdio.h> |
|
27 |
#include "sun_java2d_cmm_lcms_LCMS.h" |
|
28 |
#include "jni_util.h" |
|
29 |
#include "Trace.h" |
|
30 |
#include "Disposer.h" |
|
31 |
#include "lcms.h" |
|
32 |
||
2392 | 33 |
|
34 |
#define ALIGNLONG(x) (((x)+3) & ~(3)) // Aligns to DWORD boundary |
|
35 |
||
36 |
#ifdef USE_BIG_ENDIAN |
|
37 |
#define AdjustEndianess32(a) |
|
38 |
#else |
|
39 |
||
40 |
static |
|
41 |
void AdjustEndianess32(LPBYTE pByte) |
|
42 |
{ |
|
43 |
BYTE temp1; |
|
44 |
BYTE temp2; |
|
45 |
||
46 |
temp1 = *pByte++; |
|
47 |
temp2 = *pByte++; |
|
48 |
*(pByte-1) = *pByte; |
|
49 |
*pByte++ = temp2; |
|
50 |
*(pByte-3) = *pByte; |
|
51 |
*pByte = temp1; |
|
52 |
} |
|
53 |
||
54 |
#endif |
|
55 |
||
56 |
// Transports to properly encoded values - note that icc profiles does use |
|
57 |
// big endian notation. |
|
58 |
||
59 |
static |
|
60 |
icInt32Number TransportValue32(icInt32Number Value) |
|
61 |
{ |
|
62 |
icInt32Number Temp = Value; |
|
63 |
||
64 |
AdjustEndianess32((LPBYTE) &Temp); |
|
65 |
return Temp; |
|
66 |
} |
|
67 |
||
2 | 68 |
#define SigMake(a,b,c,d) \ |
69 |
( ( ((int) ((unsigned char) (a))) << 24) | \ |
|
70 |
( ((int) ((unsigned char) (b))) << 16) | \ |
|
71 |
( ((int) ((unsigned char) (c))) << 8) | \ |
|
72 |
(int) ((unsigned char) (d))) |
|
73 |
||
74 |
#define TagIdConst(a, b, c, d) \ |
|
75 |
((int) SigMake ((a), (b), (c), (d))) |
|
76 |
||
77 |
#define SigHead TagIdConst('h','e','a','d') |
|
78 |
||
79 |
#define DT_BYTE 0 |
|
80 |
#define DT_SHORT 1 |
|
81 |
#define DT_INT 2 |
|
82 |
#define DT_DOUBLE 3 |
|
83 |
||
84 |
/* Default temp profile list size */ |
|
85 |
#define DF_ICC_BUF_SIZE 32 |
|
86 |
||
87 |
#define ERR_MSG_SIZE 20 |
|
88 |
||
89 |
typedef union storeID_s { /* store SProfile stuff in a Java Long */ |
|
90 |
cmsHPROFILE pf; |
|
91 |
cmsHTRANSFORM xf; |
|
92 |
jobject jobj; |
|
93 |
jlong j; |
|
94 |
} storeID_t, *storeID_p; |
|
95 |
||
96 |
static jfieldID Trans_profileIDs_fID; |
|
97 |
static jfieldID Trans_renderType_fID; |
|
98 |
static jfieldID Trans_ID_fID; |
|
99 |
static jfieldID IL_isIntPacked_fID; |
|
100 |
static jfieldID IL_dataType_fID; |
|
101 |
static jfieldID IL_pixelType_fID; |
|
102 |
static jfieldID IL_dataArray_fID; |
|
103 |
static jfieldID IL_offset_fID; |
|
104 |
static jfieldID IL_nextRowOffset_fID; |
|
105 |
static jfieldID IL_width_fID; |
|
106 |
static jfieldID IL_height_fID; |
|
107 |
static jfieldID PF_ID_fID; |
|
108 |
||
109 |
JavaVM *javaVM; |
|
110 |
||
111 |
int errorHandler(int errorCode, const char *errorText) { |
|
112 |
JNIEnv *env; |
|
113 |
char errMsg[ERR_MSG_SIZE]; |
|
114 |
/* We can safely use sprintf here because error message has limited size */ |
|
115 |
sprintf(errMsg, "LCMS error %d", errorCode); |
|
116 |
||
117 |
(*javaVM)->AttachCurrentThread(javaVM, (void**)&env, NULL); |
|
118 |
JNU_ThrowByName(env, "java/awt/color/CMMException", errMsg); |
|
119 |
return 1; |
|
120 |
} |
|
121 |
||
122 |
JNIEXPORT int JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) { |
|
123 |
javaVM = jvm; |
|
124 |
||
125 |
cmsSetErrorHandler(errorHandler); |
|
126 |
return JNI_VERSION_1_6; |
|
127 |
} |
|
128 |
||
129 |
void LCMS_freeTransform(JNIEnv *env, jlong ID) |
|
130 |
{ |
|
131 |
storeID_t sTrans; |
|
132 |
sTrans.j = ID; |
|
133 |
/* Passed ID is always valid native ref so there is no check for zero */ |
|
134 |
cmsDeleteTransform(sTrans.xf); |
|
135 |
} |
|
136 |
||
137 |
/* |
|
138 |
* Class: sun_java2d_cmm_lcms_LCMS |
|
139 |
* Method: createNativeTransform |
|
140 |
* Signature: ([JI)J |
|
141 |
*/ |
|
142 |
JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_createNativeTransform |
|
143 |
(JNIEnv *env, jclass cls, jlongArray profileIDs, jint renderType, |
|
144 |
jobject disposerRef) |
|
145 |
{ |
|
146 |
LPLCMSICCPROFILE _iccArray[DF_ICC_BUF_SIZE]; |
|
147 |
LPLCMSICCPROFILE *iccArray = &_iccArray[0]; |
|
148 |
cmsHTRANSFORM transform; |
|
149 |
storeID_t sTrans; |
|
150 |
int i, j, size; |
|
151 |
jlong* ids; |
|
152 |
||
153 |
size = (*env)->GetArrayLength (env, profileIDs); |
|
154 |
ids = (*env)->GetPrimitiveArrayCritical(env, profileIDs, 0); |
|
155 |
||
156 |
if (DF_ICC_BUF_SIZE < size*2) { |
|
157 |
iccArray = (LPLCMSICCPROFILE*) malloc( |
|
158 |
size*2*sizeof(LPLCMSICCPROFILE)); |
|
159 |
if (iccArray == NULL) { |
|
160 |
J2dRlsTraceLn(J2D_TRACE_ERROR, "getXForm: iccArray == NULL"); |
|
161 |
return NULL; |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
j = 0; |
|
166 |
for (i = 0; i < size; i++) { |
|
167 |
LPLCMSICCPROFILE icc; |
|
168 |
sTrans.j = ids[i]; |
|
169 |
icc = sTrans.pf; |
|
170 |
iccArray[j++] = icc; |
|
171 |
||
172 |
/* Middle non-abstract profiles should be doubled before passing to |
|
173 |
* the cmsCreateMultiprofileTransform function |
|
174 |
*/ |
|
175 |
if (size > 2 && i != 0 && i != size - 1 && |
|
176 |
icc->ColorSpace != icSigXYZData && |
|
177 |
icc->ColorSpace != icSigLabData) |
|
178 |
{ |
|
179 |
iccArray[j++] = icc; |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
sTrans.xf = cmsCreateMultiprofileTransform(iccArray, j, |
|
184 |
0, 0, renderType, 0); |
|
185 |
||
186 |
(*env)->ReleasePrimitiveArrayCritical(env, profileIDs, ids, 0); |
|
187 |
||
188 |
if (sTrans.xf == NULL) { |
|
189 |
J2dRlsTraceLn(J2D_TRACE_ERROR, "LCMS_createNativeTransform: " |
|
190 |
"sTrans.xf == NULL"); |
|
191 |
JNU_ThrowByName(env, "java/awt/color/CMMException", |
|
192 |
"Cannot get color transform"); |
|
6868
f9131565859e
6963489: ZDI-CAN-803: Sun JRE ICC Profile Device Information Tag Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
193 |
} else { |
f9131565859e
6963489: ZDI-CAN-803: Sun JRE ICC Profile Device Information Tag Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
194 |
Disposer_AddRecord(env, disposerRef, LCMS_freeTransform, sTrans.j); |
2 | 195 |
} |
196 |
||
197 |
if (iccArray != &_iccArray[0]) { |
|
198 |
free(iccArray); |
|
199 |
} |
|
200 |
return sTrans.j; |
|
201 |
} |
|
202 |
||
203 |
||
204 |
/* |
|
205 |
* Class: sun_java2d_cmm_lcms_LCMS |
|
206 |
* Method: loadProfile |
|
207 |
* Signature: ([B)J |
|
208 |
*/ |
|
209 |
JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_loadProfile |
|
210 |
(JNIEnv *env, jobject obj, jbyteArray data) |
|
211 |
{ |
|
212 |
jbyte* dataArray; |
|
213 |
jint dataSize; |
|
214 |
storeID_t sProf; |
|
215 |
||
216 |
dataArray = (*env)->GetByteArrayElements (env, data, 0); |
|
217 |
dataSize = (*env)->GetArrayLength (env, data); |
|
218 |
||
219 |
sProf.pf = cmsOpenProfileFromMem((LPVOID)dataArray, (DWORD) dataSize); |
|
220 |
||
2392 | 221 |
(*env)->ReleaseByteArrayElements (env, data, dataArray, 0); |
222 |
||
2 | 223 |
if (sProf.pf == NULL) { |
224 |
JNU_ThrowIllegalArgumentException(env, "Invalid profile data"); |
|
225 |
} |
|
226 |
||
227 |
return sProf.j; |
|
228 |
} |
|
229 |
||
230 |
/* |
|
231 |
* Class: sun_java2d_cmm_lcms_LCMS |
|
232 |
* Method: freeProfile |
|
233 |
* Signature: (J)V |
|
234 |
*/ |
|
235 |
JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_freeProfile |
|
236 |
(JNIEnv *env, jobject obj, jlong id) |
|
237 |
{ |
|
238 |
storeID_t sProf; |
|
239 |
||
240 |
sProf.j = id; |
|
241 |
if (cmsCloseProfile(sProf.pf) == 0) { |
|
242 |
J2dRlsTraceLn1(J2D_TRACE_ERROR, "LCMS_freeProfile: cmsCloseProfile(%d)" |
|
243 |
"== 0", id); |
|
244 |
JNU_ThrowByName(env, "java/awt/color/CMMException", |
|
245 |
"Cannot close profile"); |
|
246 |
} |
|
247 |
||
248 |
} |
|
249 |
||
250 |
/* |
|
251 |
* Class: sun_java2d_cmm_lcms_LCMS |
|
252 |
* Method: getProfileSize |
|
253 |
* Signature: (J)I |
|
254 |
*/ |
|
255 |
JNIEXPORT jint JNICALL Java_sun_java2d_cmm_lcms_LCMS_getProfileSize |
|
256 |
(JNIEnv *env, jobject obj, jlong id) |
|
257 |
{ |
|
258 |
LPLCMSICCPROFILE Icc; |
|
259 |
storeID_t sProf; |
|
260 |
unsigned char pfSize[4]; |
|
261 |
||
262 |
sProf.j = id; |
|
263 |
Icc = (LPLCMSICCPROFILE) sProf.pf; |
|
264 |
Icc -> Seek(Icc, 0); |
|
265 |
Icc -> Read(pfSize, 4, 1, Icc); |
|
266 |
||
267 |
/* TODO: It's a correct but non-optimal for BE machines code, so should |
|
268 |
* be special cased for them |
|
269 |
*/ |
|
270 |
return (pfSize[0]<<24) | (pfSize[1]<<16) | (pfSize[2]<<8) | |
|
271 |
pfSize[3]; |
|
272 |
} |
|
273 |
||
274 |
/* |
|
275 |
* Class: sun_java2d_cmm_lcms_LCMS |
|
276 |
* Method: getProfileData |
|
277 |
* Signature: (J[B)V |
|
278 |
*/ |
|
279 |
JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_getProfileData |
|
280 |
(JNIEnv *env, jobject obj, jlong id, jbyteArray data) |
|
281 |
{ |
|
282 |
LPLCMSICCPROFILE Icc; |
|
283 |
storeID_t sProf; |
|
284 |
unsigned char pfSize[4]; |
|
285 |
jint size; |
|
286 |
jbyte* dataArray; |
|
287 |
||
288 |
sProf.j = id; |
|
289 |
Icc = (LPLCMSICCPROFILE) sProf.pf; |
|
290 |
Icc -> Seek(Icc, 0); |
|
291 |
Icc -> Read(pfSize, 4, 1, Icc); |
|
292 |
||
293 |
dataArray = (*env)->GetByteArrayElements (env, data, 0); |
|
294 |
Icc->Seek(Icc, 0); |
|
295 |
||
296 |
/* TODO: It's a correct but non-optimal for BE machines code, so should |
|
297 |
* be special cased for them |
|
298 |
*/ |
|
299 |
Icc->Read(dataArray, 1, |
|
300 |
(pfSize[0]<<24) | (pfSize[1]<<16) | (pfSize[2]<<8) | pfSize[3], |
|
301 |
Icc); |
|
302 |
(*env)->ReleaseByteArrayElements (env, data, dataArray, 0); |
|
303 |
} |
|
304 |
||
305 |
/* |
|
306 |
* Class: sun_java2d_cmm_lcms_LCMS |
|
307 |
* Method: getTagSize |
|
308 |
* Signature: (JI)I |
|
309 |
*/ |
|
310 |
JNIEXPORT jint JNICALL Java_sun_java2d_cmm_lcms_LCMS_getTagSize |
|
311 |
(JNIEnv *env, jobject obj, jlong id, jint tagSig) |
|
312 |
{ |
|
313 |
LPLCMSICCPROFILE Icc; |
|
314 |
storeID_t sProf; |
|
315 |
int i; |
|
316 |
jint result; |
|
317 |
||
318 |
sProf.j = id; |
|
319 |
Icc = (LPLCMSICCPROFILE) sProf.pf; |
|
320 |
||
321 |
if (tagSig == SigHead) { |
|
322 |
result = sizeof(icHeader); |
|
323 |
} else { |
|
324 |
i = _cmsSearchTag(Icc, tagSig, FALSE); |
|
325 |
if (i >= 0) { |
|
326 |
result = Icc->TagSizes[i]; |
|
327 |
} else { |
|
328 |
JNU_ThrowByName(env, "java/awt/color/CMMException", |
|
329 |
"ICC profile tag not found"); |
|
330 |
result = -1; |
|
331 |
} |
|
332 |
} |
|
333 |
||
334 |
return result; |
|
335 |
} |
|
336 |
||
337 |
/* |
|
338 |
* Class: sun_java2d_cmm_lcms_LCMS |
|
339 |
* Method: getTagData |
|
340 |
* Signature: (JI[B)V |
|
341 |
*/ |
|
342 |
JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_getTagData |
|
343 |
(JNIEnv *env, jobject obj, jlong id, jint tagSig, jbyteArray data) |
|
344 |
{ |
|
345 |
LPLCMSICCPROFILE Icc; |
|
346 |
storeID_t sProf; |
|
347 |
jbyte* dataArray; |
|
348 |
int i, tagSize; |
|
349 |
||
350 |
sProf.j = id; |
|
351 |
Icc = (LPLCMSICCPROFILE) sProf.pf; |
|
352 |
||
353 |
if (tagSig == SigHead) { |
|
354 |
dataArray = (*env)->GetByteArrayElements (env, data, 0); |
|
355 |
tagSize =(*env)->GetArrayLength(env, data); |
|
356 |
Icc -> Seek(Icc, 0); |
|
357 |
Icc -> Read(dataArray, sizeof(icHeader), 1, Icc); |
|
358 |
(*env)->ReleaseByteArrayElements (env, data, dataArray, 0); |
|
359 |
return; |
|
360 |
} |
|
361 |
||
362 |
||
363 |
i = _cmsSearchTag(Icc, tagSig, FALSE); |
|
364 |
if (i >=0) { |
|
365 |
tagSize = Icc->TagSizes[i]; |
|
366 |
dataArray = (*env)->GetByteArrayElements (env, data, 0); |
|
367 |
Icc->Seek(Icc, Icc->TagOffsets[i]); |
|
368 |
Icc->Read(dataArray, 1, tagSize, Icc); |
|
369 |
(*env)->ReleaseByteArrayElements (env, data, dataArray, 0); |
|
370 |
return; |
|
371 |
} |
|
372 |
||
373 |
JNU_ThrowByName(env, "java/awt/color/CMMException", |
|
374 |
"ICC profile tag not found"); |
|
375 |
return; |
|
376 |
} |
|
377 |
||
2394 | 378 |
// Modify data for a tag in a profile |
379 |
LCMSBOOL LCMSEXPORT _cmsModifyTagData(cmsHPROFILE hProfile, |
|
380 |
icTagSignature sig, void *data, size_t size); |
|
381 |
||
2 | 382 |
/* |
383 |
* Class: sun_java2d_cmm_lcms_LCMS |
|
384 |
* Method: setTagData |
|
385 |
* Signature: (JI[B)V |
|
386 |
*/ |
|
387 |
JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_setTagData |
|
388 |
(JNIEnv *env, jobject obj, jlong id, jint tagSig, jbyteArray data) |
|
389 |
{ |
|
2392 | 390 |
cmsHPROFILE profile; |
391 |
storeID_t sProf; |
|
392 |
jbyte* dataArray; |
|
393 |
int tagSize; |
|
394 |
||
395 |
if (tagSig == SigHead) { |
|
396 |
J2dRlsTraceLn(J2D_TRACE_ERROR, "LCMS_setTagData on icSigHead not " |
|
397 |
"permitted"); |
|
398 |
return; |
|
399 |
} |
|
400 |
||
401 |
sProf.j = id; |
|
402 |
profile = (cmsHPROFILE) sProf.pf; |
|
403 |
dataArray = (*env)->GetByteArrayElements(env, data, 0); |
|
404 |
tagSize =(*env)->GetArrayLength(env, data); |
|
405 |
_cmsModifyTagData(profile, (icTagSignature) tagSig, dataArray, tagSize); |
|
406 |
(*env)->ReleaseByteArrayElements(env, data, dataArray, 0); |
|
2 | 407 |
} |
408 |
||
409 |
void* getILData (JNIEnv *env, jobject img, jint* pDataType, |
|
410 |
jobject* pDataObject) { |
|
411 |
void* result = NULL; |
|
412 |
*pDataType = (*env)->GetIntField (env, img, IL_dataType_fID); |
|
413 |
*pDataObject = (*env)->GetObjectField(env, img, IL_dataArray_fID); |
|
414 |
switch (*pDataType) { |
|
415 |
case DT_BYTE: |
|
416 |
result = (*env)->GetByteArrayElements (env, *pDataObject, 0); |
|
417 |
break; |
|
418 |
case DT_SHORT: |
|
419 |
result = (*env)->GetShortArrayElements (env, *pDataObject, 0); |
|
420 |
break; |
|
421 |
case DT_INT: |
|
422 |
result = (*env)->GetIntArrayElements (env, *pDataObject, 0); |
|
423 |
break; |
|
424 |
case DT_DOUBLE: |
|
425 |
result = (*env)->GetDoubleArrayElements (env, *pDataObject, 0); |
|
426 |
break; |
|
427 |
} |
|
428 |
||
429 |
return result; |
|
430 |
} |
|
431 |
||
432 |
void releaseILData (JNIEnv *env, void* pData, jint dataType, |
|
433 |
jobject dataObject) { |
|
434 |
switch (dataType) { |
|
435 |
case DT_BYTE: |
|
436 |
(*env)->ReleaseByteArrayElements(env,dataObject,(jbyte*)pData,0); |
|
437 |
break; |
|
438 |
case DT_SHORT: |
|
439 |
(*env)->ReleaseShortArrayElements(env,dataObject,(jshort*)pData, 0); |
|
440 |
break; |
|
441 |
case DT_INT: |
|
442 |
(*env)->ReleaseIntArrayElements(env,dataObject,(jint*)pData,0); |
|
443 |
break; |
|
444 |
case DT_DOUBLE: |
|
445 |
(*env)->ReleaseDoubleArrayElements(env,dataObject,(jdouble*)pData, |
|
446 |
0); |
|
447 |
break; |
|
448 |
} |
|
449 |
} |
|
450 |
||
451 |
/* |
|
452 |
* Class: sun_java2d_cmm_lcms_LCMS |
|
453 |
* Method: colorConvert |
|
454 |
* Signature: (Lsun/java2d/cmm/lcms/LCMSTransform;Lsun/java2d/cmm/lcms/LCMSImageLayout;Lsun/java2d/cmm/lcms/LCMSImageLayout;)V |
|
455 |
*/ |
|
456 |
JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_colorConvert |
|
457 |
(JNIEnv *env, jclass obj, jobject trans, jobject src, jobject dst) |
|
458 |
{ |
|
459 |
storeID_t sTrans; |
|
460 |
int size, inFmt, outFmt, srcDType, dstDType, outSize, renderType; |
|
461 |
int srcOffset, srcNextRowOffset, dstOffset, dstNextRowOffset; |
|
462 |
int width, height, i; |
|
463 |
void* inputBuffer; |
|
464 |
void* outputBuffer; |
|
465 |
char* inputRow; |
|
466 |
char* outputRow; |
|
467 |
jobject srcData, dstData; |
|
468 |
||
469 |
inFmt = (*env)->GetIntField (env, src, IL_pixelType_fID); |
|
470 |
outFmt = (*env)->GetIntField (env, dst, IL_pixelType_fID); |
|
471 |
srcOffset = (*env)->GetIntField (env, src, IL_offset_fID); |
|
472 |
srcNextRowOffset = (*env)->GetIntField (env, src, IL_nextRowOffset_fID); |
|
473 |
dstOffset = (*env)->GetIntField (env, dst, IL_offset_fID); |
|
474 |
dstNextRowOffset = (*env)->GetIntField (env, dst, IL_nextRowOffset_fID); |
|
475 |
width = (*env)->GetIntField (env, src, IL_width_fID); |
|
476 |
height = (*env)->GetIntField (env, src, IL_height_fID); |
|
477 |
#ifdef _LITTLE_ENDIAN |
|
478 |
/* Reversing data packed into int for LE archs */ |
|
479 |
if ((*env)->GetBooleanField (env, src, IL_isIntPacked_fID) == JNI_TRUE) { |
|
480 |
inFmt ^= DOSWAP_SH(1); |
|
481 |
} |
|
482 |
if ((*env)->GetBooleanField (env, dst, IL_isIntPacked_fID) == JNI_TRUE) { |
|
483 |
outFmt ^= DOSWAP_SH(1); |
|
484 |
} |
|
485 |
#endif |
|
486 |
sTrans.j = (*env)->GetLongField (env, trans, Trans_ID_fID); |
|
487 |
cmsChangeBuffersFormat(sTrans.xf, inFmt, outFmt); |
|
488 |
||
489 |
||
490 |
if (sTrans.xf == NULL) { |
|
491 |
J2dRlsTraceLn(J2D_TRACE_ERROR, "LCMS_colorConvert: transform == NULL"); |
|
492 |
JNU_ThrowByName(env, "java/awt/color/CMMException", |
|
493 |
"Cannot get color transform"); |
|
494 |
return; |
|
495 |
} |
|
496 |
||
497 |
||
498 |
inputBuffer = getILData (env, src, &srcDType, &srcData); |
|
499 |
||
500 |
if (inputBuffer == NULL) { |
|
501 |
J2dRlsTraceLn(J2D_TRACE_ERROR, ""); |
|
502 |
JNU_ThrowByName(env, "java/awt/color/CMMException", |
|
503 |
"Cannot get input data"); |
|
504 |
return; |
|
505 |
} |
|
506 |
||
507 |
outputBuffer = getILData (env, dst, &dstDType, &dstData); |
|
508 |
||
509 |
if (outputBuffer == NULL) { |
|
510 |
releaseILData(env, inputBuffer, srcDType, srcData); |
|
511 |
JNU_ThrowByName(env, "java/awt/color/CMMException", |
|
512 |
"Cannot get output data"); |
|
513 |
return; |
|
514 |
} |
|
515 |
||
516 |
inputRow = (char*)inputBuffer + srcOffset; |
|
517 |
outputRow = (char*)outputBuffer + dstOffset; |
|
518 |
||
519 |
for (i = 0; i < height; i++) { |
|
520 |
cmsDoTransform(sTrans.xf, inputRow, outputRow, width); |
|
521 |
inputRow += srcNextRowOffset; |
|
522 |
outputRow += dstNextRowOffset; |
|
523 |
} |
|
524 |
||
525 |
releaseILData(env, inputBuffer, srcDType, srcData); |
|
526 |
releaseILData(env, outputBuffer, dstDType, dstData); |
|
527 |
} |
|
528 |
||
529 |
/* |
|
530 |
* Class: sun_java2d_cmm_lcms_LCMS |
|
531 |
* Method: getProfileID |
|
532 |
* Signature: (Ljava/awt/color/ICC_Profile;)J |
|
533 |
*/ |
|
534 |
JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_getProfileID |
|
535 |
(JNIEnv *env, jclass cls, jobject pf) |
|
536 |
{ |
|
537 |
return (*env)->GetLongField (env, pf, PF_ID_fID); |
|
538 |
} |
|
539 |
||
540 |
/* |
|
541 |
* Class: sun_java2d_cmm_lcms_LCMS |
|
542 |
* Method: initLCMS |
|
543 |
* Signature: (Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/Class;)V |
|
544 |
*/ |
|
545 |
JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_initLCMS |
|
546 |
(JNIEnv *env, jclass cls, jclass Trans, jclass IL, jclass Pf) |
|
547 |
{ |
|
548 |
/* TODO: move initialization of the IDs to the static blocks of |
|
549 |
* corresponding classes to avoid problems with invalidating ids by class |
|
550 |
* unloading |
|
551 |
*/ |
|
552 |
Trans_profileIDs_fID = (*env)->GetFieldID (env, Trans, "profileIDs", "[J"); |
|
553 |
Trans_renderType_fID = (*env)->GetFieldID (env, Trans, "renderType", "I"); |
|
554 |
Trans_ID_fID = (*env)->GetFieldID (env, Trans, "ID", "J"); |
|
555 |
||
556 |
IL_isIntPacked_fID = (*env)->GetFieldID (env, IL, "isIntPacked", "Z"); |
|
557 |
IL_dataType_fID = (*env)->GetFieldID (env, IL, "dataType", "I"); |
|
558 |
IL_pixelType_fID = (*env)->GetFieldID (env, IL, "pixelType", "I"); |
|
559 |
IL_dataArray_fID = (*env)->GetFieldID(env, IL, "dataArray", |
|
560 |
"Ljava/lang/Object;"); |
|
561 |
IL_width_fID = (*env)->GetFieldID (env, IL, "width", "I"); |
|
562 |
IL_height_fID = (*env)->GetFieldID (env, IL, "height", "I"); |
|
563 |
IL_offset_fID = (*env)->GetFieldID (env, IL, "offset", "I"); |
|
564 |
IL_nextRowOffset_fID = (*env)->GetFieldID (env, IL, "nextRowOffset", "I"); |
|
565 |
||
566 |
PF_ID_fID = (*env)->GetFieldID (env, Pf, "ID", "J"); |
|
567 |
} |
|
2392 | 568 |
|
2394 | 569 |
LCMSBOOL _cmsModifyTagData(cmsHPROFILE hProfile, icTagSignature sig, |
2392 | 570 |
void *data, size_t size) |
571 |
{ |
|
2394 | 572 |
LCMSBOOL isNew; |
2392 | 573 |
int i, idx, delta, count; |
574 |
LPBYTE padChars[3] = {0, 0, 0}; |
|
575 |
LPBYTE beforeBuf, afterBuf, ptr; |
|
576 |
size_t beforeSize, afterSize; |
|
577 |
icUInt32Number profileSize, temp; |
|
578 |
LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
|
579 |
||
580 |
isNew = FALSE; |
|
581 |
idx = _cmsSearchTag(Icc, sig, FALSE); |
|
582 |
if (idx < 0) { |
|
583 |
isNew = TRUE; |
|
584 |
idx = Icc->TagCount++; |
|
585 |
if (Icc->TagCount >= MAX_TABLE_TAG) { |
|
586 |
J2dRlsTraceLn1(J2D_TRACE_ERROR, "_cmsModifyTagData: Too many tags " |
|
587 |
"(%d)\n", Icc->TagCount); |
|
588 |
Icc->TagCount = MAX_TABLE_TAG-1; |
|
589 |
return FALSE; |
|
590 |
} |
|
591 |
} |
|
592 |
||
593 |
/* Read in size from header */ |
|
594 |
Icc->Seek(Icc, 0); |
|
595 |
Icc->Read(&profileSize, sizeof(icUInt32Number), 1, Icc); |
|
596 |
AdjustEndianess32((LPBYTE) &profileSize); |
|
597 |
||
598 |
/* Compute the change in profile size */ |
|
599 |
if (isNew) { |
|
600 |
delta = sizeof(icTag) + ALIGNLONG(size); |
|
601 |
} else { |
|
602 |
delta = ALIGNLONG(size) - ALIGNLONG(Icc->TagSizes[idx]); |
|
603 |
} |
|
604 |
/* Add tag to internal structures */ |
|
605 |
ptr = malloc(size); |
|
606 |
if (ptr == NULL) { |
|
607 |
if(isNew) { |
|
608 |
Icc->TagCount--; |
|
609 |
} |
|
610 |
J2dRlsTraceLn(J2D_TRACE_ERROR, "_cmsModifyTagData: ptr == NULL"); |
|
611 |
return FALSE; |
|
612 |
} |
|
613 |
||
2801 | 614 |
/* We change the size of Icc here only if we know it'll actually |
615 |
* grow: if Icc is about to shrink we must wait until we've read |
|
616 |
* the previous data. */ |
|
617 |
if (delta > 0) { |
|
618 |
if (!Icc->Grow(Icc, delta)) { |
|
619 |
free(ptr); |
|
620 |
if(isNew) { |
|
621 |
Icc->TagCount--; |
|
622 |
} |
|
623 |
J2dRlsTraceLn(J2D_TRACE_ERROR, |
|
624 |
"_cmsModifyTagData: Icc->Grow() == FALSE"); |
|
625 |
return FALSE; |
|
2392 | 626 |
} |
627 |
} |
|
628 |
||
629 |
/* Compute size of tag data before/after the modified tag */ |
|
630 |
beforeSize = ((isNew)?profileSize:Icc->TagOffsets[idx]) - |
|
631 |
Icc->TagOffsets[0]; |
|
632 |
if (Icc->TagCount == (idx + 1)) { |
|
633 |
afterSize = 0; |
|
634 |
} else { |
|
635 |
afterSize = profileSize - Icc->TagOffsets[idx+1]; |
|
636 |
} |
|
637 |
/* Make copies of the data before/after the modified tag */ |
|
638 |
if (beforeSize > 0) { |
|
639 |
beforeBuf = malloc(beforeSize); |
|
640 |
if (!beforeBuf) { |
|
641 |
if(isNew) { |
|
642 |
Icc->TagCount--; |
|
643 |
} |
|
644 |
free(ptr); |
|
645 |
J2dRlsTraceLn(J2D_TRACE_ERROR, |
|
646 |
"_cmsModifyTagData: beforeBuf == NULL"); |
|
647 |
return FALSE; |
|
648 |
} |
|
649 |
Icc->Seek(Icc, Icc->TagOffsets[0]); |
|
650 |
Icc->Read(beforeBuf, beforeSize, 1, Icc); |
|
651 |
} |
|
652 |
||
653 |
if (afterSize > 0) { |
|
654 |
afterBuf = malloc(afterSize); |
|
655 |
if (!afterBuf) { |
|
656 |
free(ptr); |
|
657 |
if(isNew) { |
|
658 |
Icc->TagCount--; |
|
659 |
} |
|
660 |
if (beforeSize > 0) { |
|
661 |
free(beforeBuf); |
|
662 |
} |
|
663 |
J2dRlsTraceLn(J2D_TRACE_ERROR, |
|
664 |
"_cmsModifyTagData: afterBuf == NULL"); |
|
665 |
return FALSE; |
|
666 |
} |
|
667 |
Icc->Seek(Icc, Icc->TagOffsets[idx+1]); |
|
668 |
Icc->Read(afterBuf, afterSize, 1, Icc); |
|
669 |
} |
|
670 |
||
671 |
CopyMemory(ptr, data, size); |
|
672 |
Icc->TagSizes[idx] = size; |
|
673 |
Icc->TagNames[idx] = sig; |
|
674 |
if (Icc->TagPtrs[idx]) { |
|
675 |
free(Icc->TagPtrs[idx]); |
|
676 |
} |
|
677 |
Icc->TagPtrs[idx] = ptr; |
|
678 |
if (isNew) { |
|
679 |
Icc->TagOffsets[idx] = profileSize; |
|
680 |
} |
|
681 |
||
682 |
||
683 |
/* Update the profile size in the header */ |
|
684 |
profileSize += delta; |
|
685 |
Icc->Seek(Icc, 0); |
|
686 |
temp = TransportValue32(profileSize); |
|
687 |
Icc->Write(Icc, sizeof(icUInt32Number), &temp); |
|
688 |
||
2801 | 689 |
/* Shrink Icc, if needed. */ |
690 |
if (delta < 0) { |
|
691 |
if (!Icc->Grow(Icc, delta)) { |
|
692 |
free(ptr); |
|
693 |
if(isNew) { |
|
694 |
Icc->TagCount--; |
|
695 |
} |
|
696 |
J2dRlsTraceLn(J2D_TRACE_ERROR, |
|
697 |
"_cmsModifyTagData: Icc->Grow() == FALSE"); |
|
698 |
return FALSE; |
|
699 |
} |
|
700 |
} |
|
2392 | 701 |
|
702 |
/* Adjust tag offsets: if the tag is new, we must account |
|
703 |
for the new tag table entry; otherwise, only those tags after |
|
704 |
the modified tag are changed (by delta) */ |
|
705 |
if (isNew) { |
|
706 |
for (i = 0; i < Icc->TagCount; ++i) { |
|
707 |
Icc->TagOffsets[i] += sizeof(icTag); |
|
708 |
} |
|
709 |
} else { |
|
710 |
for (i = idx+1; i < Icc->TagCount; ++i) { |
|
711 |
Icc->TagOffsets[i] += delta; |
|
712 |
} |
|
713 |
} |
|
714 |
||
715 |
/* Write out a new tag table */ |
|
716 |
count = 0; |
|
717 |
for (i = 0; i < Icc->TagCount; ++i) { |
|
718 |
if (Icc->TagNames[i] != 0) { |
|
719 |
++count; |
|
720 |
} |
|
721 |
} |
|
722 |
Icc->Seek(Icc, sizeof(icHeader)); |
|
723 |
temp = TransportValue32(count); |
|
724 |
Icc->Write(Icc, sizeof(icUInt32Number), &temp); |
|
725 |
||
726 |
for (i = 0; i < Icc->TagCount; ++i) { |
|
727 |
if (Icc->TagNames[i] != 0) { |
|
728 |
icTag tag; |
|
729 |
tag.sig = TransportValue32(Icc->TagNames[i]); |
|
730 |
tag.offset = TransportValue32((icInt32Number) Icc->TagOffsets[i]); |
|
731 |
tag.size = TransportValue32((icInt32Number) Icc->TagSizes[i]); |
|
732 |
Icc->Write(Icc, sizeof(icTag), &tag); |
|
733 |
} |
|
734 |
} |
|
735 |
||
736 |
/* Write unchanged data before the modified tag */ |
|
737 |
if (beforeSize > 0) { |
|
738 |
Icc->Write(Icc, beforeSize, beforeBuf); |
|
739 |
free(beforeBuf); |
|
740 |
} |
|
741 |
||
742 |
/* Write modified tag data */ |
|
743 |
Icc->Write(Icc, size, data); |
|
744 |
if (size % 4) { |
|
745 |
Icc->Write(Icc, 4 - (size % 4), padChars); |
|
746 |
} |
|
747 |
||
748 |
/* Write unchanged data after the modified tag */ |
|
749 |
if (afterSize > 0) { |
|
750 |
Icc->Write(Icc, afterSize, afterBuf); |
|
751 |
free(afterBuf); |
|
752 |
} |
|
753 |
||
754 |
return TRUE; |
|
755 |
} |