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