src/share/native/sun/java2d/cmm/lcms/LCMS.c

Print this page




 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 PF_ID_fID;
 121 
 122 JavaVM *javaVM;
 123 
 124 void errorHandler(cmsContext ContextID, cmsUInt32Number errorCode,
 125                   const char *errorText) {
 126     JNIEnv *env;
 127     char errMsg[ERR_MSG_SIZE];
 128 
 129     int count = snprintf(errMsg, ERR_MSG_SIZE,
 130                           "LCMS error %d: %s", errorCode, errorText);
 131     if (count < 0 || count >= ERR_MSG_SIZE) {
 132         count = ERR_MSG_SIZE - 1;
 133     }
 134     errMsg[count] = 0;
 135 
 136     (*javaVM)->AttachCurrentThread(javaVM, (void**)&env, NULL);
 137     JNU_ThrowByName(env, "java/awt/color/CMMException", errMsg);
 138 }
 139 


 220         J2dRlsTraceLn(J2D_TRACE_ERROR, "LCMS_createNativeTransform: "
 221                                        "sTrans.xf == NULL");
 222         JNU_ThrowByName(env, "java/awt/color/CMMException",
 223                         "Cannot get color transform");
 224     } else {
 225         Disposer_AddRecord(env, disposerRef, LCMS_freeTransform, sTrans.j);
 226     }
 227 
 228     if (iccArray != &_iccArray[0]) {
 229         free(iccArray);
 230     }
 231     return sTrans.j;
 232 }
 233 
 234 
 235 /*
 236  * Class:     sun_java2d_cmm_lcms_LCMS
 237  * Method:    loadProfile
 238  * Signature: ([B)J
 239  */
 240 JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_loadProfile
 241   (JNIEnv *env, jobject obj, jbyteArray data)
 242 {
 243     jbyte* dataArray;
 244     jint dataSize;
 245     storeID_t sProf;
 246 
 247     if (JNU_IsNull(env, data)) {
 248         JNU_ThrowIllegalArgumentException(env, "Invalid profile data");
 249         return 0L;
 250     }
 251 
 252     dataArray = (*env)->GetByteArrayElements (env, data, 0);
 253     dataSize = (*env)->GetArrayLength (env, data);
 254 
 255     if (dataArray == NULL) {
 256         JNU_ThrowIllegalArgumentException(env, "Invalid profile data");
 257         return 0L;
 258     }
 259 
 260     sProf.pf = cmsOpenProfileFromMem((const void *)dataArray,


 267     } else {
 268         /* Sanity check: try to save the profile in order
 269          * to force basic validation.
 270          */
 271         cmsUInt32Number pfSize = 0;
 272         if (!cmsSaveProfileToMem(sProf.pf, NULL, &pfSize) ||
 273             pfSize < sizeof(cmsICCHeader))
 274         {
 275             JNU_ThrowIllegalArgumentException(env, "Invalid profile data");
 276         }
 277     }
 278 
 279     return sProf.j;
 280 }
 281 
 282 /*
 283  * Class:     sun_java2d_cmm_lcms_LCMS
 284  * Method:    freeProfile
 285  * Signature: (J)V
 286  */
 287 JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_freeProfile
 288   (JNIEnv *env, jobject obj, jlong id)
 289 {
 290     storeID_t sProf;
 291 
 292     sProf.j = id;
 293     if (cmsCloseProfile(sProf.pf) == 0) {
 294         J2dRlsTraceLn1(J2D_TRACE_ERROR, "LCMS_freeProfile: cmsCloseProfile(%d)"
 295                        "== 0", id);
 296         JNU_ThrowByName(env, "java/awt/color/CMMException",
 297                         "Cannot close profile");
 298     }
 299 
 300 }
 301 
 302 /*
 303  * Class:     sun_java2d_cmm_lcms_LCMS
 304  * Method:    getProfileSize
 305  * Signature: (J)I
 306  */
 307 JNIEXPORT jint JNICALL Java_sun_java2d_cmm_lcms_LCMS_getProfileSize


 386 
 387     if (tagSig == SigHead) {
 388         result = sizeof(cmsICCHeader);
 389     } else {
 390       if (cmsIsTag(sProf.pf, sig.cms)) {
 391           result = cmsReadRawTag(sProf.pf, sig.cms, NULL, 0);
 392         } else {
 393             JNU_ThrowByName(env, "java/awt/color/CMMException",
 394                             "ICC profile tag not found");
 395         }
 396     }
 397 
 398     return result;
 399 }
 400 
 401 /*
 402  * Class:     sun_java2d_cmm_lcms_LCMS
 403  * Method:    getTagData
 404  * Signature: (JI[B)V
 405  */
 406 JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_getTagData
 407   (JNIEnv *env, jobject obj, jlong id, jint tagSig, jbyteArray data)
 408 {
 409     storeID_t sProf;
 410     TagSignature_t sig;
 411     cmsInt32Number tagSize;
 412 
 413     jbyte* dataArray;


 414     jint bufSize;
 415 
 416     sProf.j = id;
 417     sig.j = tagSig;
 418 
 419     if (tagSig == SigHead) {
 420         cmsBool status;
 421 
 422         bufSize =(*env)->GetArrayLength(env, data);

 423 
 424         if (bufSize < sizeof(cmsICCHeader)) {
 425            JNU_ThrowByName(env, "java/awt/color/CMMException",
 426                             "Insufficient buffer capacity");
 427            return;
 428         }
 429 
 430         dataArray = (*env)->GetByteArrayElements (env, data, 0);
 431 
 432         if (dataArray == NULL) {
 433            JNU_ThrowByName(env, "java/awt/color/CMMException",
 434                             "Unable to get buffer");
 435            return;
 436         }
 437 
 438         status = _getHeaderInfo(sProf.pf, dataArray, bufSize);
 439 
 440         (*env)->ReleaseByteArrayElements (env, data, dataArray, 0);
 441 
 442         if (!status) {
 443             JNU_ThrowByName(env, "java/awt/color/CMMException",
 444                             "ICC Profile header not found");

 445         }
 446 
 447         return;
 448     }
 449 
 450     if (cmsIsTag(sProf.pf, sig.cms)) {
 451         tagSize = cmsReadRawTag(sProf.pf, sig.cms, NULL, 0);
 452     } else {
 453         JNU_ThrowByName(env, "java/awt/color/CMMException",
 454                         "ICC profile tag not found");
 455         return;
 456     }
 457 
 458     // verify data buffer capacity
 459     bufSize = (*env)->GetArrayLength(env, data);
 460 
 461     if (tagSize < 0 || 0 > bufSize || tagSize > bufSize) {
 462         JNU_ThrowByName(env, "java/awt/color/CMMException",
 463                         "Insufficient buffer capacity.");
 464         return;
 465     }
 466 
 467     dataArray = (*env)->GetByteArrayElements (env, data, 0);
 468 
 469     if (dataArray == NULL) {
 470         JNU_ThrowByName(env, "java/awt/color/CMMException",
 471                         "Unable to get buffer");
 472         return;
 473     }
 474 
 475     bufSize = cmsReadRawTag(sProf.pf, sig.cms, dataArray, tagSize);
 476 
 477     (*env)->ReleaseByteArrayElements (env, data, dataArray, 0);
 478 
 479     if (bufSize != tagSize) {
 480         JNU_ThrowByName(env, "java/awt/color/CMMException",
 481                         "Can not get tag data.");

 482     }
 483     return;
 484 }
 485 
 486 /*
 487  * Class:     sun_java2d_cmm_lcms_LCMS
 488  * Method:    setTagData
 489  * Signature: (JI[B)V
 490  */
 491 JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_setTagData
 492   (JNIEnv *env, jobject obj, jlong id, jint tagSig, jbyteArray data)
 493 {
 494     storeID_t sProf;
 495     TagSignature_t sig;
 496     cmsBool status;
 497     jbyte* dataArray;
 498     int tagSize;
 499 
 500     sProf.j = id;
 501     sig.j = tagSig;
 502 
 503     if (JNU_IsNull(env, data)) {
 504         JNU_ThrowIllegalArgumentException(env, "Can not write tag data.");
 505         return;
 506     }
 507 
 508     tagSize =(*env)->GetArrayLength(env, data);
 509 
 510     dataArray = (*env)->GetByteArrayElements(env, data, 0);
 511 


 569     }
 570 }
 571 
 572 /*
 573  * Class:     sun_java2d_cmm_lcms_LCMS
 574  * Method:    colorConvert
 575  * Signature: (Lsun/java2d/cmm/lcms/LCMSTransform;Lsun/java2d/cmm/lcms/LCMSImageLayout;Lsun/java2d/cmm/lcms/LCMSImageLayout;)V
 576  */
 577 JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_colorConvert
 578   (JNIEnv *env, jclass obj, jobject trans, jobject src, jobject dst)
 579 {
 580     storeID_t sTrans;
 581     int srcDType, dstDType;
 582     int srcOffset, srcNextRowOffset, dstOffset, dstNextRowOffset;
 583     int width, height, i;
 584     void* inputBuffer;
 585     void* outputBuffer;
 586     char* inputRow;
 587     char* outputRow;
 588     jobject srcData, dstData;

 589 
 590     srcOffset = (*env)->GetIntField (env, src, IL_offset_fID);
 591     srcNextRowOffset = (*env)->GetIntField (env, src, IL_nextRowOffset_fID);
 592     dstOffset = (*env)->GetIntField (env, dst, IL_offset_fID);
 593     dstNextRowOffset = (*env)->GetIntField (env, dst, IL_nextRowOffset_fID);
 594     width = (*env)->GetIntField (env, src, IL_width_fID);
 595     height = (*env)->GetIntField (env, src, IL_height_fID);
 596 



 597     sTrans.j = (*env)->GetLongField (env, trans, Trans_ID_fID);
 598 
 599     if (sTrans.xf == NULL) {
 600         J2dRlsTraceLn(J2D_TRACE_ERROR, "LCMS_colorConvert: transform == NULL");
 601         JNU_ThrowByName(env, "java/awt/color/CMMException",
 602                         "Cannot get color transform");
 603         return;
 604     }
 605 
 606 
 607     inputBuffer = getILData (env, src, &srcDType, &srcData);
 608 
 609     if (inputBuffer == NULL) {
 610         J2dRlsTraceLn(J2D_TRACE_ERROR, "");
 611         JNU_ThrowByName(env, "java/awt/color/CMMException",
 612                         "Cannot get input data");
 613         return;
 614     }
 615 
 616     outputBuffer = getILData (env, dst, &dstDType, &dstData);
 617 
 618     if (outputBuffer == NULL) {
 619         releaseILData(env, inputBuffer, srcDType, srcData);
 620         JNU_ThrowByName(env, "java/awt/color/CMMException",
 621                         "Cannot get output data");
 622         return;
 623     }
 624 
 625     inputRow = (char*)inputBuffer + srcOffset;
 626     outputRow = (char*)outputBuffer + dstOffset;
 627 



 628     for (i = 0; i < height; i++) {
 629         cmsDoTransform(sTrans.xf, inputRow, outputRow, width);
 630         inputRow += srcNextRowOffset;
 631         outputRow += dstNextRowOffset;
 632     }

 633 
 634     releaseILData(env, inputBuffer, srcDType, srcData);
 635     releaseILData(env, outputBuffer, dstDType, dstData);
 636 }
 637 
 638 /*
 639  * Class:     sun_java2d_cmm_lcms_LCMS
 640  * Method:    getProfileID
 641  * Signature: (Ljava/awt/color/ICC_Profile;)J
 642  */
 643 JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_getProfileID
 644   (JNIEnv *env, jclass cls, jobject pf)
 645 {
 646     return (*env)->GetLongField (env, pf, PF_ID_fID);
 647 }
 648 
 649 /*
 650  * Class:     sun_java2d_cmm_lcms_LCMS
 651  * Method:    initLCMS
 652  * Signature: (Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/Class;)V
 653  */
 654 JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_initLCMS
 655   (JNIEnv *env, jclass cls, jclass Trans, jclass IL, jclass Pf)
 656 {
 657     /* TODO: move initialization of the IDs to the static blocks of
 658      * corresponding classes to avoid problems with invalidating ids by class
 659      * unloading
 660      */
 661     Trans_profileIDs_fID = (*env)->GetFieldID (env, Trans, "profileIDs", "[J");
 662     Trans_renderType_fID = (*env)->GetFieldID (env, Trans, "renderType", "I");
 663     Trans_ID_fID = (*env)->GetFieldID (env, Trans, "ID", "J");
 664 
 665     IL_isIntPacked_fID = (*env)->GetFieldID (env, IL, "isIntPacked", "Z");
 666     IL_dataType_fID = (*env)->GetFieldID (env, IL, "dataType", "I");
 667     IL_pixelType_fID = (*env)->GetFieldID (env, IL, "pixelType", "I");
 668     IL_dataArray_fID = (*env)->GetFieldID(env, IL, "dataArray",
 669                                           "Ljava/lang/Object;");
 670     IL_width_fID = (*env)->GetFieldID (env, IL, "width", "I");
 671     IL_height_fID = (*env)->GetFieldID (env, IL, "height", "I");
 672     IL_offset_fID = (*env)->GetFieldID (env, IL, "offset", "I");

 673     IL_nextRowOffset_fID = (*env)->GetFieldID (env, IL, "nextRowOffset", "I");
 674 
 675     PF_ID_fID = (*env)->GetFieldID (env, Pf, "ID", "J");
 676 }
 677 
 678 static cmsBool _getHeaderInfo(cmsHPROFILE pf, jbyte* pBuffer, jint bufferSize)
 679 {
 680   cmsUInt32Number pfSize = 0;
 681   cmsUInt8Number* pfBuffer = NULL;
 682   cmsBool status = FALSE;
 683 
 684   if (!cmsSaveProfileToMem(pf, NULL, &pfSize) ||
 685       pfSize < sizeof(cmsICCHeader) ||
 686       bufferSize < sizeof(cmsICCHeader))
 687   {
 688     return FALSE;
 689   }
 690 
 691   pfBuffer = malloc(pfSize);
 692   if (pfBuffer == NULL) {




 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 


 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,


 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


 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 jbyteArray JNICALL Java_sun_java2d_cmm_lcms_LCMS_getTagNative
 408   (JNIEnv *env, jobject obj, jlong id, jint tagSig)
 409 {
 410     storeID_t sProf;
 411     TagSignature_t sig;
 412     cmsInt32Number tagSize;
 413 
 414     jbyte* dataArray = NULL;
 415     jbyteArray data = NULL;
 416 
 417     jint bufSize;
 418 
 419     sProf.j = id;
 420     sig.j = tagSig;
 421 
 422     if (tagSig == SigHead) {
 423         cmsBool status;
 424 
 425         // allocate java array
 426         data = (*env)->NewByteArray(env, sizeof(cmsICCHeader));
 427     
 428         if (data == NULL) {
 429             JNU_ThrowByName(env, "java/awt/color/CMMException",
 430                             "Unable to allocate buffer");
 431             return NULL;
 432         }
 433  
 434         dataArray = (*env)->GetByteArrayElements (env, data, 0);
 435 
 436         if (dataArray == NULL) {
 437            JNU_ThrowByName(env, "java/awt/color/CMMException",
 438                             "Unable to get buffer");
 439            return;
 440         }
 441 
 442         status = _getHeaderInfo(sProf.pf, dataArray, bufSize);
 443 
 444         (*env)->ReleaseByteArrayElements (env, data, dataArray, 0);
 445 
 446         if (!status) {
 447             JNU_ThrowByName(env, "java/awt/color/CMMException",
 448                             "ICC Profile header not found");
 449             return NULL;
 450         }
 451 
 452         return data;
 453     }
 454 
 455     if (cmsIsTag(sProf.pf, sig.cms)) {
 456         tagSize = cmsReadRawTag(sProf.pf, sig.cms, NULL, 0);
 457     } else {
 458         JNU_ThrowByName(env, "java/awt/color/CMMException",
 459                         "ICC profile tag not found");
 460         return NULL;
 461     }
 462 
 463     // allocate java array
 464     data = (*env)->NewByteArray(env, tagSize);
 465     if (data == NULL) {

 466         JNU_ThrowByName(env, "java/awt/color/CMMException",
 467                         "Unable to allocate buffer");
 468         return NULL;
 469     }
 470     
 471     dataArray = (*env)->GetByteArrayElements (env, data, 0);
 472 
 473     if (dataArray == NULL) {
 474         JNU_ThrowByName(env, "java/awt/color/CMMException",
 475                         "Unable to get buffer");
 476         return NULL;
 477     }
 478 
 479     bufSize = cmsReadRawTag(sProf.pf, sig.cms, dataArray, tagSize);
 480 
 481     (*env)->ReleaseByteArrayElements (env, data, dataArray, 0);
 482 
 483     if (bufSize != tagSize) {
 484         JNU_ThrowByName(env, "java/awt/color/CMMException",
 485                         "Can not get tag data.");
 486         return NULL;
 487     }
 488     return data;
 489 }
 490 
 491 /*
 492  * Class:     sun_java2d_cmm_lcms_LCMS
 493  * Method:    setTagData
 494  * Signature: (JI[B)V
 495  */
 496 JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_setTagDataNative
 497   (JNIEnv *env, jobject obj, jlong id, jint tagSig, jbyteArray data)
 498 {
 499     storeID_t sProf;
 500     TagSignature_t sig;
 501     cmsBool status;
 502     jbyte* dataArray;
 503     int tagSize;
 504 
 505     sProf.j = id;
 506     sig.j = tagSig;
 507 
 508     if (JNU_IsNull(env, data)) {
 509         JNU_ThrowIllegalArgumentException(env, "Can not write tag data.");
 510         return;
 511     }
 512 
 513     tagSize =(*env)->GetArrayLength(env, data);
 514 
 515     dataArray = (*env)->GetByteArrayElements(env, data, 0);
 516 


 574     }
 575 }
 576 
 577 /*
 578  * Class:     sun_java2d_cmm_lcms_LCMS
 579  * Method:    colorConvert
 580  * Signature: (Lsun/java2d/cmm/lcms/LCMSTransform;Lsun/java2d/cmm/lcms/LCMSImageLayout;Lsun/java2d/cmm/lcms/LCMSImageLayout;)V
 581  */
 582 JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_colorConvert
 583   (JNIEnv *env, jclass obj, jobject trans, jobject src, jobject dst)
 584 {
 585     storeID_t sTrans;
 586     int srcDType, dstDType;
 587     int srcOffset, srcNextRowOffset, dstOffset, dstNextRowOffset;
 588     int width, height, i;
 589     void* inputBuffer;
 590     void* outputBuffer;
 591     char* inputRow;
 592     char* outputRow;
 593     jobject srcData, dstData;
 594     jboolean srcAtOnce = JNI_FALSE, dstAtOnce = JNI_FALSE;
 595 
 596     srcOffset = (*env)->GetIntField (env, src, IL_offset_fID);
 597     srcNextRowOffset = (*env)->GetIntField (env, src, IL_nextRowOffset_fID);
 598     dstOffset = (*env)->GetIntField (env, dst, IL_offset_fID);
 599     dstNextRowOffset = (*env)->GetIntField (env, dst, IL_nextRowOffset_fID);
 600     width = (*env)->GetIntField (env, src, IL_width_fID);
 601     height = (*env)->GetIntField (env, src, IL_height_fID);
 602 
 603     srcAtOnce = (*env)->GetBooleanField(env, src, IL_imageAtOnce_fID);
 604     dstAtOnce = (*env)->GetBooleanField(env, dst, IL_imageAtOnce_fID);
 605 
 606     sTrans.j = (*env)->GetLongField (env, trans, Trans_ID_fID);
 607 
 608     if (sTrans.xf == NULL) {
 609         J2dRlsTraceLn(J2D_TRACE_ERROR, "LCMS_colorConvert: transform == NULL");
 610         JNU_ThrowByName(env, "java/awt/color/CMMException",
 611                         "Cannot get color transform");
 612         return;
 613     }
 614 
 615 
 616     inputBuffer = getILData (env, src, &srcDType, &srcData);
 617 
 618     if (inputBuffer == NULL) {
 619         J2dRlsTraceLn(J2D_TRACE_ERROR, "");
 620         JNU_ThrowByName(env, "java/awt/color/CMMException",
 621                         "Cannot get input data");
 622         return;
 623     }
 624 
 625     outputBuffer = getILData (env, dst, &dstDType, &dstData);
 626 
 627     if (outputBuffer == NULL) {
 628         releaseILData(env, inputBuffer, srcDType, srcData);
 629         JNU_ThrowByName(env, "java/awt/color/CMMException",
 630                         "Cannot get output data");
 631         return;
 632     }
 633 
 634     inputRow = (char*)inputBuffer + srcOffset;
 635     outputRow = (char*)outputBuffer + dstOffset;
 636 
 637     if (srcAtOnce && dstAtOnce) {
 638         cmsDoTransform(sTrans.xf, inputRow, outputRow, width * height);
 639     } else {
 640         for (i = 0; i < height; i++) {
 641             cmsDoTransform(sTrans.xf, inputRow, outputRow, width);
 642             inputRow += srcNextRowOffset;
 643             outputRow += dstNextRowOffset;
 644         }
 645     }
 646 
 647     releaseILData(env, inputBuffer, srcDType, srcData);
 648     releaseILData(env, outputBuffer, dstDType, dstData);
 649 }
 650 
 651 /*
 652  * Class:     sun_java2d_cmm_lcms_LCMS
 653  * Method:    getProfileID
 654  * Signature: (Ljava/awt/color/ICC_Profile;)J
 655  */
 656 JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_getProfileID
 657   (JNIEnv *env, jclass cls, jobject pf)
 658 {
 659     return (*env)->GetLongField (env, pf, PF_ID_fID);
 660 }
 661 
 662 /*
 663  * Class:     sun_java2d_cmm_lcms_LCMS
 664  * Method:    initLCMS
 665  * Signature: (Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/Class;)V
 666  */
 667 JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_initLCMS
 668   (JNIEnv *env, jclass cls, jclass Trans, jclass IL, jclass Pf)
 669 {
 670     /* TODO: move initialization of the IDs to the static blocks of
 671      * corresponding classes to avoid problems with invalidating ids by class
 672      * unloading
 673      */
 674     Trans_profileIDs_fID = (*env)->GetFieldID (env, Trans, "profileIDs", "[J");
 675     Trans_renderType_fID = (*env)->GetFieldID (env, Trans, "renderType", "I");
 676     Trans_ID_fID = (*env)->GetFieldID (env, Trans, "ID", "J");
 677 
 678     IL_isIntPacked_fID = (*env)->GetFieldID (env, IL, "isIntPacked", "Z");
 679     IL_dataType_fID = (*env)->GetFieldID (env, IL, "dataType", "I");
 680     IL_pixelType_fID = (*env)->GetFieldID (env, IL, "pixelType", "I");
 681     IL_dataArray_fID = (*env)->GetFieldID(env, IL, "dataArray",
 682                                           "Ljava/lang/Object;");
 683     IL_width_fID = (*env)->GetFieldID (env, IL, "width", "I");
 684     IL_height_fID = (*env)->GetFieldID (env, IL, "height", "I");
 685     IL_offset_fID = (*env)->GetFieldID (env, IL, "offset", "I");
 686     IL_imageAtOnce_fID = (*env)->GetFieldID (env, IL, "imageAtOnce", "Z");
 687     IL_nextRowOffset_fID = (*env)->GetFieldID (env, IL, "nextRowOffset", "I");
 688 
 689     PF_ID_fID = (*env)->GetFieldID (env, Pf, "ID", "J");
 690 }
 691 
 692 static cmsBool _getHeaderInfo(cmsHPROFILE pf, jbyte* pBuffer, jint bufferSize)
 693 {
 694   cmsUInt32Number pfSize = 0;
 695   cmsUInt8Number* pfBuffer = NULL;
 696   cmsBool status = FALSE;
 697 
 698   if (!cmsSaveProfileToMem(pf, NULL, &pfSize) ||
 699       pfSize < sizeof(cmsICCHeader) ||
 700       bufferSize < sizeof(cmsICCHeader))
 701   {
 702     return FALSE;
 703   }
 704 
 705   pfBuffer = malloc(pfSize);
 706   if (pfBuffer == NULL) {