1 /*
   2  * Copyright (c) 2007, 2011, 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 #include <stdio.h>
  27 #include <stdlib.h>
  28 #include <memory.h>
  29 #include "sun_java2d_cmm_lcms_LCMS.h"
  30 #include "jni_util.h"
  31 #include "Trace.h"
  32 #include "Disposer.h"
  33 #include "lcms2.h"
  34 
  35 
  36 #define ALIGNLONG(x) (((x)+3) & ~(3))         // Aligns to DWORD boundary
  37 
  38 #ifdef USE_BIG_ENDIAN
  39 #define AdjustEndianess32(a)
  40 #else
  41 
  42 static
  43 void AdjustEndianess32(cmsUInt8Number *pByte)
  44 {
  45     cmsUInt8Number temp1;
  46     cmsUInt8Number temp2;
  47 
  48     temp1 = *pByte++;
  49     temp2 = *pByte++;
  50     *(pByte-1) = *pByte;
  51     *pByte++ = temp2;
  52     *(pByte-3) = *pByte;
  53     *pByte = temp1;
  54 }
  55 
  56 #endif
  57 
  58 // Transports to properly encoded values - note that icc profiles does use
  59 // big endian notation.
  60 
  61 static
  62 cmsInt32Number TransportValue32(cmsInt32Number Value)
  63 {
  64     cmsInt32Number Temp = Value;
  65 
  66     AdjustEndianess32((cmsUInt8Number*) &Temp);
  67     return Temp;
  68 }
  69 
  70 #define SigMake(a,b,c,d) \
  71                     ( ( ((int) ((unsigned char) (a))) << 24) | \
  72                       ( ((int) ((unsigned char) (b))) << 16) | \
  73                       ( ((int) ((unsigned char) (c))) <<  8) | \
  74                           (int) ((unsigned char) (d)))
  75 
  76 #define TagIdConst(a, b, c, d) \
  77                 ((int) SigMake ((a), (b), (c), (d)))
  78 
  79 #define SigHead TagIdConst('h','e','a','d')
  80 
  81 #define DT_BYTE     0
  82 #define DT_SHORT    1
  83 #define DT_INT      2
  84 #define DT_DOUBLE   3
  85 
  86 /* Default temp profile list size */
  87 #define DF_ICC_BUF_SIZE 32
  88 
  89 #define ERR_MSG_SIZE 256
  90 
  91 #ifdef _MSC_VER
  92 # ifndef snprintf
  93 #       define snprintf  _snprintf
  94 # endif
  95 #endif
  96 
  97 typedef union storeID_s {    /* store SProfile stuff in a Java Long */
  98     cmsHPROFILE pf;
  99     cmsHTRANSFORM xf;
 100     jobject jobj;
 101     jlong j;
 102 } storeID_t, *storeID_p;
 103 
 104 typedef union {
 105     cmsTagSignature cms;
 106     jint j;
 107 } TagSignature_t, *TagSignature_p;
 108 
 109 static jfieldID Trans_profileIDs_fID;
 110 static jfieldID Trans_renderType_fID;
 111 static jfieldID Trans_ID_fID;
 112 static jfieldID IL_isIntPacked_fID;
 113 static jfieldID IL_dataType_fID;
 114 static jfieldID IL_pixelType_fID;
 115 static jfieldID IL_dataArray_fID;
 116 static jfieldID IL_offset_fID;
 117 static jfieldID IL_nextRowOffset_fID;
 118 static jfieldID IL_width_fID;
 119 static jfieldID IL_height_fID;
 120 static jfieldID IL_imageAtOnce_fID;
 121 static jfieldID PF_ID_fID;
 122 
 123 JavaVM *javaVM;
 124 
 125 void errorHandler(cmsContext ContextID, cmsUInt32Number errorCode,
 126                   const char *errorText) {
 127     JNIEnv *env;
 128     char errMsg[ERR_MSG_SIZE];
 129 
 130     int count = snprintf(errMsg, ERR_MSG_SIZE,
 131                           "LCMS error %d: %s", errorCode, errorText);
 132     if (count < 0 || count >= ERR_MSG_SIZE) {
 133         count = ERR_MSG_SIZE - 1;
 134     }
 135     errMsg[count] = 0;
 136 
 137     (*javaVM)->AttachCurrentThread(javaVM, (void**)&env, NULL);
 138     JNU_ThrowByName(env, "java/awt/color/CMMException", errMsg);
 139 }
 140 
 141 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
 142     javaVM = jvm;
 143 
 144     cmsSetLogErrorHandler(errorHandler);
 145     return JNI_VERSION_1_6;
 146 }
 147 
 148 void LCMS_freeTransform(JNIEnv *env, jlong ID)
 149 {
 150     storeID_t sTrans;
 151     sTrans.j = ID;
 152     /* Passed ID is always valid native ref so there is no check for zero */
 153     cmsDeleteTransform(sTrans.xf);
 154 }
 155 
 156 /*
 157  * Class:     sun_java2d_cmm_lcms_LCMS
 158  * Method:    createNativeTransform
 159  * Signature: ([JI)J
 160  */
 161 JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_createNativeTransform
 162   (JNIEnv *env, jclass cls, jlongArray profileIDs, jint renderType,
 163    jint inFormatter, jboolean isInIntPacked,
 164    jint outFormatter, jboolean isOutIntPacked, jobject disposerRef)
 165 {
 166     cmsHPROFILE _iccArray[DF_ICC_BUF_SIZE];
 167     cmsHPROFILE *iccArray = &_iccArray[0];
 168     storeID_t sTrans;
 169     int i, j, size;
 170     jlong* ids;
 171 
 172     size = (*env)->GetArrayLength (env, profileIDs);
 173     ids = (*env)->GetPrimitiveArrayCritical(env, profileIDs, 0);
 174 
 175 #ifdef _LITTLE_ENDIAN
 176     /* Reversing data packed into int for LE archs */
 177     if (isInIntPacked) {
 178         inFormatter ^= DOSWAP_SH(1);
 179     }
 180     if (isOutIntPacked) {
 181         outFormatter ^= DOSWAP_SH(1);
 182     }
 183 #endif
 184 
 185     if (DF_ICC_BUF_SIZE < size*2) {
 186         iccArray = (cmsHPROFILE*) malloc(
 187             size*2*sizeof(cmsHPROFILE));
 188         if (iccArray == NULL) {
 189             J2dRlsTraceLn(J2D_TRACE_ERROR, "getXForm: iccArray == NULL");
 190             return 0L;
 191         }
 192     }
 193 
 194     j = 0;
 195     for (i = 0; i < size; i++) {
 196         cmsHPROFILE icc;
 197         cmsColorSpaceSignature cs;
 198 
 199         sTrans.j = ids[i];
 200         icc = sTrans.pf;
 201         iccArray[j++] = icc;
 202 
 203         /* Middle non-abstract profiles should be doubled before passing to
 204          * the cmsCreateMultiprofileTransform function
 205          */
 206 
 207         cs = cmsGetColorSpace(icc);
 208         if (size > 2 && i != 0 && i != size - 1 &&
 209             cs != cmsSigXYZData && cs != cmsSigLabData)
 210         {
 211             iccArray[j++] = icc;
 212         }
 213     }
 214 
 215     sTrans.xf = cmsCreateMultiprofileTransform(iccArray, j,
 216         inFormatter, outFormatter, renderType, 0);
 217 
 218     (*env)->ReleasePrimitiveArrayCritical(env, profileIDs, ids, 0);
 219 
 220     if (sTrans.xf == NULL) {
 221         J2dRlsTraceLn(J2D_TRACE_ERROR, "LCMS_createNativeTransform: "
 222                                        "sTrans.xf == NULL");
 223         JNU_ThrowByName(env, "java/awt/color/CMMException",
 224                         "Cannot get color transform");
 225     } else {
 226         Disposer_AddRecord(env, disposerRef, LCMS_freeTransform, sTrans.j);
 227     }
 228 
 229     if (iccArray != &_iccArray[0]) {
 230         free(iccArray);
 231     }
 232     return sTrans.j;
 233 }
 234 
 235 
 236 /*
 237  * Class:     sun_java2d_cmm_lcms_LCMS
 238  * Method:    loadProfile
 239  * Signature: ([B)J
 240  */
 241 JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_loadProfileNative
 242   (JNIEnv *env, jobject obj, jbyteArray data)
 243 {
 244     jbyte* dataArray;
 245     jint dataSize;
 246     storeID_t sProf;
 247 
 248     if (JNU_IsNull(env, data)) {
 249         JNU_ThrowIllegalArgumentException(env, "Invalid profile data");
 250         return 0L;
 251     }
 252 
 253     dataArray = (*env)->GetByteArrayElements (env, data, 0);
 254     dataSize = (*env)->GetArrayLength (env, data);
 255 
 256     if (dataArray == NULL) {
 257         JNU_ThrowIllegalArgumentException(env, "Invalid profile data");
 258         return 0L;
 259     }
 260 
 261     sProf.pf = cmsOpenProfileFromMem((const void *)dataArray,
 262                                      (cmsUInt32Number) dataSize);
 263 
 264     (*env)->ReleaseByteArrayElements (env, data, dataArray, 0);
 265 
 266     if (sProf.pf == NULL) {
 267         JNU_ThrowIllegalArgumentException(env, "Invalid profile data");
 268     } else {
 269         /* Sanity check: try to save the profile in order
 270          * to force basic validation.
 271          */
 272         cmsUInt32Number pfSize = 0;
 273         if (!cmsSaveProfileToMem(sProf.pf, NULL, &pfSize) ||
 274             pfSize < sizeof(cmsICCHeader))
 275         {
 276             JNU_ThrowIllegalArgumentException(env, "Invalid profile data");
 277         }
 278     }
 279 
 280     return sProf.j;
 281 }
 282 
 283 /*
 284  * Class:     sun_java2d_cmm_lcms_LCMS
 285  * Method:    freeProfile
 286  * Signature: (J)V
 287  */
 288 JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_freeProfileNative
 289   (JNIEnv *env, jobject obj, jlong id)
 290 {
 291     storeID_t sProf;
 292 
 293     sProf.j = id;
 294     if (cmsCloseProfile(sProf.pf) == 0) {
 295         J2dRlsTraceLn1(J2D_TRACE_ERROR, "LCMS_freeProfile: cmsCloseProfile(%d)"
 296                        "== 0", id);
 297         JNU_ThrowByName(env, "java/awt/color/CMMException",
 298                         "Cannot close profile");
 299     }
 300 
 301 }
 302 
 303 /*
 304  * Class:     sun_java2d_cmm_lcms_LCMS
 305  * Method:    getProfileSize
 306  * Signature: (J)I
 307  */
 308 JNIEXPORT jint JNICALL Java_sun_java2d_cmm_lcms_LCMS_getProfileSize
 309   (JNIEnv *env, jobject obj, jlong id)
 310 {
 311     storeID_t sProf;
 312     cmsUInt32Number pfSize = 0;
 313     sProf.j = id;
 314 
 315     if (cmsSaveProfileToMem(sProf.pf, NULL, &pfSize) && ((jint)pfSize > 0)) {
 316         return (jint)pfSize;
 317     } else {
 318       JNU_ThrowByName(env, "java/awt/color/CMMException",
 319                       "Can not access specified profile.");
 320         return -1;
 321     }
 322 }
 323 
 324 /*
 325  * Class:     sun_java2d_cmm_lcms_LCMS
 326  * Method:    getProfileData
 327  * Signature: (J[B)V
 328  */
 329 JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_getProfileData
 330   (JNIEnv *env, jobject obj, jlong id, jbyteArray data)
 331 {
 332     storeID_t sProf;
 333     jint size;
 334     jbyte* dataArray;
 335     cmsUInt32Number pfSize = 0;
 336     cmsBool status;
 337 
 338     sProf.j = id;
 339 
 340     // determine actual profile size
 341     if (!cmsSaveProfileToMem(sProf.pf, NULL, &pfSize)) {
 342         JNU_ThrowByName(env, "java/awt/color/CMMException",
 343                         "Can not access specified profile.");
 344         return;
 345     }
 346 
 347     // verify java buffer capacity
 348     size = (*env)->GetArrayLength(env, data);
 349     if (0 >= size || pfSize > (cmsUInt32Number)size) {
 350         JNU_ThrowByName(env, "java/awt/color/CMMException",
 351                         "Insufficient buffer capacity.");
 352         return;
 353     }
 354 
 355     dataArray = (*env)->GetByteArrayElements (env, data, 0);
 356 
 357     status = cmsSaveProfileToMem(sProf.pf, dataArray, &pfSize);
 358 
 359     (*env)->ReleaseByteArrayElements (env, data, dataArray, 0);
 360 
 361     if (!status) {
 362         JNU_ThrowByName(env, "java/awt/color/CMMException",
 363                         "Can not access specified profile.");
 364         return;
 365     }
 366 }
 367 
 368 /* Get profile header info */
 369 static cmsBool _getHeaderInfo(cmsHPROFILE pf, jbyte* pBuffer, jint bufferSize);
 370 static cmsBool _setHeaderInfo(cmsHPROFILE pf, jbyte* pBuffer, jint bufferSize);
 371 static cmsBool _writeCookedTag(cmsHPROFILE pfTarget, cmsTagSignature sig, jbyte *pData, jint size);
 372 











 373 

















 374 /*
 375  * Class:     sun_java2d_cmm_lcms_LCMS
 376  * Method:    getTagData
 377  * Signature: (JI[B)V
 378  */
 379 JNIEXPORT jbyteArray JNICALL Java_sun_java2d_cmm_lcms_LCMS_getTagNative
 380   (JNIEnv *env, jobject obj, jlong id, jint tagSig)
 381 {
 382     storeID_t sProf;
 383     TagSignature_t sig;
 384     cmsInt32Number tagSize;
 385 
 386     jbyte* dataArray = NULL;
 387     jbyteArray data = NULL;
 388 
 389     jint bufSize;
 390 
 391     sProf.j = id;
 392     sig.j = tagSig;
 393 
 394     if (tagSig == SigHead) {
 395         cmsBool status;
 396 
 397         // allocate java array
 398         bufSize = sizeof(cmsICCHeader);
 399         data = (*env)->NewByteArray(env, bufSize);
 400 
 401         if (data == NULL) {
 402             JNU_ThrowByName(env, "java/awt/color/CMMException",
 403                             "Unable to allocate buffer");
 404             return NULL;
 405         }
 406 
 407         dataArray = (*env)->GetByteArrayElements (env, data, 0);
 408 
 409         if (dataArray == NULL) {
 410            JNU_ThrowByName(env, "java/awt/color/CMMException",
 411                             "Unable to get buffer");
 412            return NULL;
 413         }
 414 
 415         status = _getHeaderInfo(sProf.pf, dataArray, bufSize);
 416 
 417         (*env)->ReleaseByteArrayElements (env, data, dataArray, 0);
 418 
 419         if (!status) {
 420             JNU_ThrowByName(env, "java/awt/color/CMMException",
 421                             "ICC Profile header not found");
 422             return NULL;
 423         }
 424 
 425         return data;
 426     }
 427 
 428     if (cmsIsTag(sProf.pf, sig.cms)) {
 429         tagSize = cmsReadRawTag(sProf.pf, sig.cms, NULL, 0);
 430     } else {
 431         JNU_ThrowByName(env, "java/awt/color/CMMException",
 432                         "ICC profile tag not found");
 433         return NULL;
 434     }
 435 
 436     // allocate java array
 437     data = (*env)->NewByteArray(env, tagSize);
 438     if (data == NULL) {

 439         JNU_ThrowByName(env, "java/awt/color/CMMException",
 440                         "Unable to allocate buffer");
 441         return NULL;
 442     }
 443 
 444     dataArray = (*env)->GetByteArrayElements (env, data, 0);
 445 
 446     if (dataArray == NULL) {
 447         JNU_ThrowByName(env, "java/awt/color/CMMException",
 448                         "Unable to get buffer");
 449         return NULL;
 450     }
 451 
 452     bufSize = cmsReadRawTag(sProf.pf, sig.cms, dataArray, tagSize);
 453 
 454     (*env)->ReleaseByteArrayElements (env, data, dataArray, 0);
 455 
 456     if (bufSize != tagSize) {
 457         JNU_ThrowByName(env, "java/awt/color/CMMException",
 458                         "Can not get tag data.");
 459         return NULL;
 460     }
 461     return data;
 462 }
 463 
 464 /*
 465  * Class:     sun_java2d_cmm_lcms_LCMS
 466  * Method:    setTagData
 467  * Signature: (JI[B)V
 468  */
 469 JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_setTagDataNative
 470   (JNIEnv *env, jobject obj, jlong id, jint tagSig, jbyteArray data)
 471 {
 472     storeID_t sProf;
 473     TagSignature_t sig;
 474     cmsBool status;
 475     jbyte* dataArray;
 476     int tagSize;
 477 
 478     sProf.j = id;
 479     sig.j = tagSig;
 480 
 481     if (JNU_IsNull(env, data)) {
 482         JNU_ThrowIllegalArgumentException(env, "Can not write tag data.");
 483         return;
 484     }
 485 
 486     tagSize =(*env)->GetArrayLength(env, data);
 487 
 488     dataArray = (*env)->GetByteArrayElements(env, data, 0);
 489 
 490     if (dataArray == NULL) {
 491         JNU_ThrowIllegalArgumentException(env, "Can not write tag data.");
 492         return;
 493     }
 494 
 495     if (tagSig == SigHead) {
 496         status = _setHeaderInfo(sProf.pf, dataArray, tagSize);
 497     } else {
 498         status = _writeCookedTag(sProf.pf, sig.cms, dataArray, tagSize);
 499     }
 500 
 501     (*env)->ReleaseByteArrayElements(env, data, dataArray, 0);
 502 
 503     if (!status) {
 504         JNU_ThrowIllegalArgumentException(env, "Can not write tag data.");
 505     }
 506 }
 507 
 508 void* getILData (JNIEnv *env, jobject img, jint* pDataType,
 509                  jobject* pDataObject) {
 510     void* result = NULL;
 511     *pDataType = (*env)->GetIntField (env, img, IL_dataType_fID);
 512     *pDataObject = (*env)->GetObjectField(env, img, IL_dataArray_fID);
 513     switch (*pDataType) {
 514         case DT_BYTE:
 515             result = (*env)->GetByteArrayElements (env, *pDataObject, 0);
 516             break;
 517         case DT_SHORT:
 518             result = (*env)->GetShortArrayElements (env, *pDataObject, 0);
 519             break;
 520         case DT_INT:
 521             result = (*env)->GetIntArrayElements (env, *pDataObject, 0);
 522             break;
 523         case DT_DOUBLE:
 524             result = (*env)->GetDoubleArrayElements (env, *pDataObject, 0);
 525             break;
 526     }
 527 
 528     return result;
 529 }
 530 
 531 void releaseILData (JNIEnv *env, void* pData, jint dataType,
 532                     jobject dataObject) {
 533     switch (dataType) {
 534         case DT_BYTE:
 535             (*env)->ReleaseByteArrayElements(env,dataObject,(jbyte*)pData,0);
 536             break;
 537         case DT_SHORT:
 538             (*env)->ReleaseShortArrayElements(env,dataObject,(jshort*)pData, 0);
 539             break;
 540         case DT_INT:
 541             (*env)->ReleaseIntArrayElements(env,dataObject,(jint*)pData,0);
 542             break;
 543         case DT_DOUBLE:
 544             (*env)->ReleaseDoubleArrayElements(env,dataObject,(jdouble*)pData,
 545                                                0);
 546             break;
 547     }
 548 }
 549 
 550 /*
 551  * Class:     sun_java2d_cmm_lcms_LCMS
 552  * Method:    colorConvert
 553  * Signature: (Lsun/java2d/cmm/lcms/LCMSTransform;Lsun/java2d/cmm/lcms/LCMSImageLayout;Lsun/java2d/cmm/lcms/LCMSImageLayout;)V
 554  */
 555 JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_colorConvert
 556   (JNIEnv *env, jclass obj, jobject trans, jobject src, jobject dst)
 557 {
 558     storeID_t sTrans;
 559     int srcDType, dstDType;
 560     int srcOffset, srcNextRowOffset, dstOffset, dstNextRowOffset;
 561     int width, height, i;
 562     void* inputBuffer;
 563     void* outputBuffer;
 564     char* inputRow;
 565     char* outputRow;
 566     jobject srcData, dstData;
 567     jboolean srcAtOnce = JNI_FALSE, dstAtOnce = JNI_FALSE;
 568 
 569     srcOffset = (*env)->GetIntField (env, src, IL_offset_fID);
 570     srcNextRowOffset = (*env)->GetIntField (env, src, IL_nextRowOffset_fID);
 571     dstOffset = (*env)->GetIntField (env, dst, IL_offset_fID);
 572     dstNextRowOffset = (*env)->GetIntField (env, dst, IL_nextRowOffset_fID);
 573     width = (*env)->GetIntField (env, src, IL_width_fID);
 574     height = (*env)->GetIntField (env, src, IL_height_fID);
 575 
 576     srcAtOnce = (*env)->GetBooleanField(env, src, IL_imageAtOnce_fID);
 577     dstAtOnce = (*env)->GetBooleanField(env, dst, IL_imageAtOnce_fID);
 578 
 579     sTrans.j = (*env)->GetLongField (env, trans, Trans_ID_fID);
 580 
 581     if (sTrans.xf == NULL) {
 582         J2dRlsTraceLn(J2D_TRACE_ERROR, "LCMS_colorConvert: transform == NULL");
 583         JNU_ThrowByName(env, "java/awt/color/CMMException",
 584                         "Cannot get color transform");
 585         return;
 586     }
 587 
 588 
 589     inputBuffer = getILData (env, src, &srcDType, &srcData);
 590 
 591     if (inputBuffer == NULL) {
 592         J2dRlsTraceLn(J2D_TRACE_ERROR, "");
 593         JNU_ThrowByName(env, "java/awt/color/CMMException",
 594                         "Cannot get input data");
 595         return;
 596     }
 597 
 598     outputBuffer = getILData (env, dst, &dstDType, &dstData);
 599 
 600     if (outputBuffer == NULL) {
 601         releaseILData(env, inputBuffer, srcDType, srcData);
 602         JNU_ThrowByName(env, "java/awt/color/CMMException",
 603                         "Cannot get output data");
 604         return;
 605     }
 606 
 607     inputRow = (char*)inputBuffer + srcOffset;
 608     outputRow = (char*)outputBuffer + dstOffset;
 609 
 610     if (srcAtOnce && dstAtOnce) {
 611         cmsDoTransform(sTrans.xf, inputRow, outputRow, width * height);
 612     } else {
 613         for (i = 0; i < height; i++) {
 614             cmsDoTransform(sTrans.xf, inputRow, outputRow, width);
 615             inputRow += srcNextRowOffset;
 616             outputRow += dstNextRowOffset;
 617         }
 618     }
 619 
 620     releaseILData(env, inputBuffer, srcDType, srcData);
 621     releaseILData(env, outputBuffer, dstDType, dstData);
 622 }
 623 
 624 /*
 625  * Class:     sun_java2d_cmm_lcms_LCMS
 626  * Method:    getProfileID
 627  * Signature: (Ljava/awt/color/ICC_Profile;)J
 628  */
 629 JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_getProfileID
 630   (JNIEnv *env, jclass cls, jobject pf)
 631 {
 632     return (*env)->GetLongField (env, pf, PF_ID_fID);
 633 }
 634 
 635 /*
 636  * Class:     sun_java2d_cmm_lcms_LCMS
 637  * Method:    initLCMS
 638  * Signature: (Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/Class;)V
 639  */
 640 JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_initLCMS
 641   (JNIEnv *env, jclass cls, jclass Trans, jclass IL, jclass Pf)
 642 {
 643     /* TODO: move initialization of the IDs to the static blocks of
 644      * corresponding classes to avoid problems with invalidating ids by class
 645      * unloading
 646      */
 647     Trans_profileIDs_fID = (*env)->GetFieldID (env, Trans, "profileIDs", "[J");
 648     Trans_renderType_fID = (*env)->GetFieldID (env, Trans, "renderType", "I");
 649     Trans_ID_fID = (*env)->GetFieldID (env, Trans, "ID", "J");
 650 
 651     IL_isIntPacked_fID = (*env)->GetFieldID (env, IL, "isIntPacked", "Z");
 652     IL_dataType_fID = (*env)->GetFieldID (env, IL, "dataType", "I");
 653     IL_pixelType_fID = (*env)->GetFieldID (env, IL, "pixelType", "I");
 654     IL_dataArray_fID = (*env)->GetFieldID(env, IL, "dataArray",
 655                                           "Ljava/lang/Object;");
 656     IL_width_fID = (*env)->GetFieldID (env, IL, "width", "I");
 657     IL_height_fID = (*env)->GetFieldID (env, IL, "height", "I");
 658     IL_offset_fID = (*env)->GetFieldID (env, IL, "offset", "I");
 659     IL_imageAtOnce_fID = (*env)->GetFieldID (env, IL, "imageAtOnce", "Z");
 660     IL_nextRowOffset_fID = (*env)->GetFieldID (env, IL, "nextRowOffset", "I");
 661 
 662     PF_ID_fID = (*env)->GetFieldID (env, Pf, "ID", "J");
 663 }
 664 
 665 static cmsBool _getHeaderInfo(cmsHPROFILE pf, jbyte* pBuffer, jint bufferSize)
 666 {
 667   cmsUInt32Number pfSize = 0;
 668   cmsUInt8Number* pfBuffer = NULL;
 669   cmsBool status = FALSE;
 670 
 671   if (!cmsSaveProfileToMem(pf, NULL, &pfSize) ||
 672       pfSize < sizeof(cmsICCHeader) ||
 673       bufferSize < sizeof(cmsICCHeader))
 674   {
 675     return FALSE;
 676   }
 677 
 678   pfBuffer = malloc(pfSize);
 679   if (pfBuffer == NULL) {
 680     return FALSE;
 681   }
 682 
 683   // load raw profile data into the buffer
 684   if (cmsSaveProfileToMem(pf, pfBuffer, &pfSize)) {
 685     memcpy(pBuffer, pfBuffer, sizeof(cmsICCHeader));
 686     status = TRUE;
 687   }
 688   free(pfBuffer);
 689   return status;
 690 }
 691 
 692 static cmsBool _setHeaderInfo(cmsHPROFILE pf, jbyte* pBuffer, jint bufferSize)
 693 {
 694   cmsICCHeader pfHeader = { 0 };
 695 
 696   if (pBuffer == NULL || bufferSize < sizeof(cmsICCHeader)) {
 697     return FALSE;
 698   }
 699 
 700   memcpy(&pfHeader, pBuffer, sizeof(cmsICCHeader));
 701 
 702   // now set header fields, which we can access using the lcms2 public API
 703   cmsSetHeaderFlags(pf, pfHeader.flags);
 704   cmsSetHeaderManufacturer(pf, pfHeader.manufacturer);
 705   cmsSetHeaderModel(pf, pfHeader.model);
 706   cmsSetHeaderAttributes(pf, pfHeader.attributes);
 707   cmsSetHeaderProfileID(pf, (cmsUInt8Number*)&(pfHeader.profileID));
 708   cmsSetHeaderRenderingIntent(pf, pfHeader.renderingIntent);
 709   cmsSetPCS(pf, pfHeader.pcs);
 710   cmsSetColorSpace(pf, pfHeader.colorSpace);
 711   cmsSetDeviceClass(pf, pfHeader.deviceClass);
 712   cmsSetEncodedICCversion(pf, pfHeader.version);
 713 
 714   return TRUE;
 715 }
 716 
 717 static cmsBool _writeCookedTag(cmsHPROFILE pfTarget,
 718                                cmsTagSignature sig,
 719                                jbyte *pData, jint size)
 720 {
 721     cmsBool status;
 722     cmsUInt32Number pfSize = 0;
 723     cmsUInt8Number* pfBuffer = NULL;
 724 
 725     cmsHPROFILE p = cmsCreateProfilePlaceholder(NULL);
 726     if (NULL != p) {
 727         cmsICCHeader hdr = { 0 };
 728 
 729         /* Populate the placeholder's header according to target profile */
 730         hdr.flags = cmsGetHeaderFlags(pfTarget);
 731         hdr.renderingIntent = cmsGetHeaderRenderingIntent(pfTarget);
 732         hdr.manufacturer = cmsGetHeaderManufacturer(pfTarget);
 733         hdr.model = cmsGetHeaderModel(pfTarget);
 734         hdr.pcs = cmsGetPCS(pfTarget);
 735         hdr.colorSpace = cmsGetColorSpace(pfTarget);
 736         hdr.deviceClass = cmsGetDeviceClass(pfTarget);
 737         hdr.version = cmsGetEncodedICCversion(pfTarget);
 738         cmsGetHeaderAttributes(pfTarget, &hdr.attributes);
 739         cmsGetHeaderProfileID(pfTarget, (cmsUInt8Number*)&hdr.profileID);
 740 
 741         cmsSetHeaderFlags(p, hdr.flags);
 742         cmsSetHeaderManufacturer(p, hdr.manufacturer);
 743         cmsSetHeaderModel(p, hdr.model);
 744         cmsSetHeaderAttributes(p, hdr.attributes);
 745         cmsSetHeaderProfileID(p, (cmsUInt8Number*)&(hdr.profileID));
 746         cmsSetHeaderRenderingIntent(p, hdr.renderingIntent);
 747         cmsSetPCS(p, hdr.pcs);
 748         cmsSetColorSpace(p, hdr.colorSpace);
 749         cmsSetDeviceClass(p, hdr.deviceClass);
 750         cmsSetEncodedICCversion(p, hdr.version);
 751 
 752 
 753         if (cmsWriteRawTag(p, sig, pData, size)) {
 754             if (cmsSaveProfileToMem(p, NULL, &pfSize)) {
 755                 pfBuffer = malloc(pfSize);
 756                 if (pfBuffer != NULL) {
 757                     /* load raw profile data into the buffer */
 758                     if (!cmsSaveProfileToMem(p, pfBuffer, &pfSize)) {
 759                         free(pfBuffer);
 760                         pfBuffer = NULL;
 761                     }
 762                 }
 763             }
 764         }
 765         cmsCloseProfile(p);
 766     }
 767 
 768     if (pfBuffer == NULL) {
 769         return FALSE;
 770     }
 771 
 772     /* re-open the placeholder profile */
 773     p = cmsOpenProfileFromMem(pfBuffer, pfSize);
 774     free(pfBuffer);
 775     status = FALSE;
 776 
 777     if (p != NULL) {
 778         /* Note that pCookedTag points to internal structures of the placeholder,
 779          * so this data is valid only while the placeholder is open.
 780          */
 781         void *pCookedTag = cmsReadTag(p, sig);
 782         if (pCookedTag != NULL) {
 783             status = cmsWriteTag(pfTarget, sig, pCookedTag);
 784         }
 785         pCookedTag = NULL;
 786         cmsCloseProfile(p);
 787     }
 788     return status;
 789 }
--- EOF ---