jdk/src/share/native/sun/awt/image/awt_parseImage.c
changeset 18258 ab42088d6e10
parent 16868 b5e2827ecc50
child 20799 4820649a142d
equal deleted inserted replaced
18257:35323587f1ff 18258:ab42088d6e10
   394 #endif
   394 #endif
   395 
   395 
   396     return 1;
   396     return 1;
   397 }
   397 }
   398 
   398 
       
   399 static int getColorModelType(JNIEnv *env, jobject jcmodel) {
       
   400     int type = UNKNOWN_CM_TYPE;
       
   401 
       
   402     if ((*env)->IsInstanceOf(env, jcmodel,
       
   403                  (*env)->FindClass(env, "java/awt/image/IndexColorModel")))
       
   404     {
       
   405         type = INDEX_CM_TYPE;
       
   406     } else if ((*env)->IsInstanceOf(env, jcmodel,
       
   407                  (*env)->FindClass(env, "java/awt/image/PackedColorModel")))
       
   408     {
       
   409         if  ((*env)->IsInstanceOf(env, jcmodel,
       
   410                 (*env)->FindClass(env, "java/awt/image/DirectColorModel"))) {
       
   411             type = DIRECT_CM_TYPE;
       
   412         }
       
   413         else {
       
   414             type = PACKED_CM_TYPE;
       
   415         }
       
   416     }
       
   417     else if ((*env)->IsInstanceOf(env, jcmodel,
       
   418                  (*env)->FindClass(env, "java/awt/image/ComponentColorModel")))
       
   419     {
       
   420         type = COMPONENT_CM_TYPE;
       
   421     }
       
   422 
       
   423     return type;
       
   424 }
       
   425 
   399 int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
   426 int awt_parseColorModel (JNIEnv *env, jobject jcmodel, int imageType,
   400                          ColorModelS_t *cmP) {
   427                          ColorModelS_t *cmP) {
   401     /*jmethodID jID;   */
   428     /*jmethodID jID;   */
   402     jobject jnBits;
   429     jobject jnBits;
       
   430     jsize   nBitsLength;
       
   431 
   403     int i;
   432     int i;
   404     static jobject s_jdefCM = NULL;
   433     static jobject s_jdefCM = NULL;
   405 
   434 
   406     if (JNU_IsNull(env, jcmodel)) {
   435     if (JNU_IsNull(env, jcmodel)) {
   407         JNU_ThrowNullPointerException(env, "null ColorModel object");
   436         JNU_ThrowNullPointerException(env, "null ColorModel object");
   419     cmP->isAlphaPre = (*env)->GetBooleanField(env,jcmodel,
   448     cmP->isAlphaPre = (*env)->GetBooleanField(env,jcmodel,
   420                                               g_CMisAlphaPreID);
   449                                               g_CMisAlphaPreID);
   421     cmP->transparency = (*env)->GetIntField(env, jcmodel,
   450     cmP->transparency = (*env)->GetIntField(env, jcmodel,
   422                                             g_CMtransparencyID);
   451                                             g_CMtransparencyID);
   423 
   452 
       
   453     jnBits = (*env)->GetObjectField(env, jcmodel, g_CMnBitsID);
       
   454     if (jnBits == NULL) {
       
   455         JNU_ThrowNullPointerException(env, "null nBits structure in CModel");
       
   456         return -1;
       
   457     }
       
   458 
       
   459     nBitsLength = (*env)->GetArrayLength(env, jnBits);
       
   460     if (nBitsLength != cmP->numComponents) {
       
   461         // invalid number of components?
       
   462         return -1;
       
   463     }
       
   464 
       
   465     cmP->nBits = NULL;
       
   466     if (SAFE_TO_ALLOC_2(cmP->numComponents, sizeof(jint))) {
       
   467         cmP->nBits = (jint *)malloc(cmP->numComponents * sizeof(jint));
       
   468     }
       
   469 
       
   470     if (cmP->nBits == NULL){
       
   471         JNU_ThrowOutOfMemoryError(env, "Out of memory");
       
   472         return -1;
       
   473     }
       
   474     (*env)->GetIntArrayRegion(env, jnBits, 0, cmP->numComponents,
       
   475                               cmP->nBits);
       
   476     cmP->maxNbits = 0;
       
   477     for (i=0; i < cmP->numComponents; i++) {
       
   478         if (cmP->maxNbits < cmP->nBits[i]) {
       
   479             cmP->maxNbits = cmP->nBits[i];
       
   480         }
       
   481     }
       
   482 
       
   483     cmP->is_sRGB = (*env)->GetBooleanField(env, cmP->jcmodel, g_CMis_sRGBID);
       
   484 
       
   485     cmP->csType = (*env)->GetIntField(env, cmP->jcmodel, g_CMcsTypeID);
       
   486 
       
   487     cmP->cmType = getColorModelType(env, jcmodel);
       
   488 
       
   489     cmP->isDefaultCM = FALSE;
       
   490     cmP->isDefaultCompatCM = FALSE;
       
   491 
       
   492     /* look for standard cases */
   424     if (imageType == java_awt_image_BufferedImage_TYPE_INT_ARGB) {
   493     if (imageType == java_awt_image_BufferedImage_TYPE_INT_ARGB) {
   425         cmP->isDefaultCM = TRUE;
   494         cmP->isDefaultCM = TRUE;
   426         cmP->isDefaultCompatCM = TRUE;
   495         cmP->isDefaultCompatCM = TRUE;
   427     } else if (imageType == java_awt_image_BufferedImage_TYPE_INT_ARGB_PRE ||
   496     } else if (imageType == java_awt_image_BufferedImage_TYPE_INT_ARGB_PRE ||
   428              imageType == java_awt_image_BufferedImage_TYPE_INT_RGB) {
   497                imageType == java_awt_image_BufferedImage_TYPE_INT_RGB ||
   429         cmP->isDefaultCompatCM = TRUE;
   498                imageType == java_awt_image_BufferedImage_TYPE_INT_BGR ||
   430     } else if (imageType == java_awt_image_BufferedImage_TYPE_INT_BGR ||
       
   431                imageType == java_awt_image_BufferedImage_TYPE_4BYTE_ABGR ||
   499                imageType == java_awt_image_BufferedImage_TYPE_4BYTE_ABGR ||
   432                imageType == java_awt_image_BufferedImage_TYPE_4BYTE_ABGR_PRE){
   500                imageType == java_awt_image_BufferedImage_TYPE_4BYTE_ABGR_PRE)
       
   501     {
   433         cmP->isDefaultCompatCM = TRUE;
   502         cmP->isDefaultCompatCM = TRUE;
   434     }
   503     }
   435     else {
   504     else {
   436         /* Figure out if this is the default CM */
   505         /* Figure out if this is the default CM */
   437         if (s_jdefCM == NULL) {
   506         if (s_jdefCM == NULL) {
   448         }
   517         }
   449         cmP->isDefaultCM = ((*env)->IsSameObject(env, s_jdefCM, jcmodel));
   518         cmP->isDefaultCM = ((*env)->IsSameObject(env, s_jdefCM, jcmodel));
   450         cmP->isDefaultCompatCM = cmP->isDefaultCM;
   519         cmP->isDefaultCompatCM = cmP->isDefaultCM;
   451     }
   520     }
   452 
   521 
       
   522     /* check whether image attributes correspond to default cm */
   453     if (cmP->isDefaultCompatCM) {
   523     if (cmP->isDefaultCompatCM) {
   454         cmP->cmType = DIRECT_CM_TYPE;
   524         if (cmP->csType != java_awt_color_ColorSpace_TYPE_RGB ||
   455         cmP->nBits = (jint *) malloc(sizeof(jint)*4);
   525             !cmP->is_sRGB)
   456         cmP->nBits[0] = cmP->nBits[1] = cmP->nBits[2] = cmP->nBits[3] = 8;
   526         {
   457         cmP->maxNbits = 8;
   527             return -1;
   458         cmP->is_sRGB = TRUE;
   528         }
   459         cmP->csType  = java_awt_color_ColorSpace_TYPE_RGB;
   529 
   460 
   530         for (i = 0; i < cmP->numComponents; i++) {
   461         return 1;
   531             if (cmP->nBits[i] != 8) {
   462     }
   532                 return -1;
   463 
   533             }
   464     jnBits = (*env)->GetObjectField(env, jcmodel, g_CMnBitsID);
   534         }
   465     if (jnBits == NULL) {
   535     }
   466         JNU_ThrowNullPointerException(env, "null nBits structure in CModel");
   536 
   467         return -1;
   537     /* Get index color model attributes */
   468     }
       
   469 
       
   470     cmP->nBits = NULL;
       
   471     if (SAFE_TO_ALLOC_2(cmP->numComponents, sizeof(jint))) {
       
   472         cmP->nBits = (jint *)malloc(cmP->numComponents * sizeof(jint));
       
   473     }
       
   474     if (cmP->nBits == NULL){
       
   475         JNU_ThrowOutOfMemoryError(env, "Out of memory");
       
   476         return -1;
       
   477     }
       
   478     (*env)->GetIntArrayRegion(env, jnBits, 0, cmP->numComponents,
       
   479                               cmP->nBits);
       
   480     cmP->maxNbits = 0;
       
   481     for (i=0; i < cmP->numComponents; i++) {
       
   482         if (cmP->maxNbits < cmP->nBits[i]) {
       
   483             cmP->maxNbits = cmP->nBits[i];
       
   484         }
       
   485     }
       
   486 
       
   487     cmP->is_sRGB = (*env)->GetBooleanField(env, cmP->jcmodel, g_CMis_sRGBID);
       
   488 
       
   489     cmP->csType = (*env)->GetIntField(env, cmP->jcmodel, g_CMcsTypeID);
       
   490 
       
   491     /* Find out what type of colol model */
       
   492     if (imageType == java_awt_image_BufferedImage_TYPE_BYTE_INDEXED ||
   538     if (imageType == java_awt_image_BufferedImage_TYPE_BYTE_INDEXED ||
   493         (*env)->IsInstanceOf(env, jcmodel,
   539         cmP->cmType == INDEX_CM_TYPE)
   494                  (*env)->FindClass(env, "java/awt/image/IndexColorModel")))
       
   495     {
   540     {
   496         cmP->cmType = INDEX_CM_TYPE;
       
   497         cmP->transIdx = (*env)->GetIntField(env, jcmodel, g_ICMtransIdxID);
   541         cmP->transIdx = (*env)->GetIntField(env, jcmodel, g_ICMtransIdxID);
   498         cmP->mapSize = (*env)->GetIntField(env, jcmodel, g_ICMmapSizeID);
   542         cmP->mapSize = (*env)->GetIntField(env, jcmodel, g_ICMmapSizeID);
   499         cmP->jrgb    = (*env)->GetObjectField(env, jcmodel, g_ICMrgbID);
   543         cmP->jrgb    = (*env)->GetObjectField(env, jcmodel, g_ICMrgbID);
   500         if (cmP->transIdx == -1) {
   544         if (cmP->transIdx == -1) {
   501             /* Need to find the transparent index */
   545             /* Need to find the transparent index */
   517                 /* Now what? No transparent pixel... */
   561                 /* Now what? No transparent pixel... */
   518                 cmP->transIdx = 0;
   562                 cmP->transIdx = 0;
   519             }
   563             }
   520         }
   564         }
   521     }
   565     }
   522     else if ((*env)->IsInstanceOf(env, jcmodel,
       
   523                  (*env)->FindClass(env, "java/awt/image/PackedColorModel")))
       
   524     {
       
   525         if  ((*env)->IsInstanceOf(env, jcmodel,
       
   526                 (*env)->FindClass(env, "java/awt/image/DirectColorModel"))){
       
   527             cmP->cmType = DIRECT_CM_TYPE;
       
   528         }
       
   529         else {
       
   530             cmP->cmType = PACKED_CM_TYPE;
       
   531         }
       
   532     }
       
   533     else if ((*env)->IsInstanceOf(env, jcmodel,
       
   534                  (*env)->FindClass(env, "java/awt/image/ComponentColorModel")))
       
   535     {
       
   536         cmP->cmType = COMPONENT_CM_TYPE;
       
   537     }
       
   538     else if ((*env)->IsInstanceOf(env, jcmodel,
       
   539               (*env)->FindClass(env, "java/awt/image/PackedColorModel")))
       
   540     {
       
   541         cmP->cmType = PACKED_CM_TYPE;
       
   542     }
       
   543     else {
       
   544         cmP->cmType = UNKNOWN_CM_TYPE;
       
   545     }
       
   546 
       
   547 
   566 
   548     return 1;
   567     return 1;
   549 }
   568 }
   550 
   569 
   551 void awt_freeParsedRaster(RasterS_t *rasterP, int freeRasterP) {
   570 void awt_freeParsedRaster(RasterS_t *rasterP, int freeRasterP) {
   646     HintS_t *hintP = &imageP->hints;
   665     HintS_t *hintP = &imageP->hints;
   647     RasterS_t *rasterP = &imageP->raster;
   666     RasterS_t *rasterP = &imageP->raster;
   648     ColorModelS_t *cmodelP = &imageP->cmodel;
   667     ColorModelS_t *cmodelP = &imageP->cmodel;
   649     int imageType = imageP->imageType;
   668     int imageType = imageP->imageType;
   650 
   669 
       
   670     // check whether raster and color model are compatible
       
   671     if (cmodelP->numComponents != rasterP->numBands) {
       
   672         if (cmodelP->cmType != INDEX_CM_TYPE) {
       
   673             return -1;
       
   674         }
       
   675     }
       
   676 
   651     hintP->numChans = imageP->cmodel.numComponents;
   677     hintP->numChans = imageP->cmodel.numComponents;
   652     hintP->colorOrder = NULL;
   678     hintP->colorOrder = NULL;
   653     if (SAFE_TO_ALLOC_2(hintP->numChans, sizeof(int))) {
   679     if (SAFE_TO_ALLOC_2(hintP->numChans, sizeof(int))) {
   654         hintP->colorOrder = (int *)malloc(hintP->numChans * sizeof(int));
   680         hintP->colorOrder = (int *)malloc(hintP->numChans * sizeof(int));
   655     }
   681     }
  1137     int maxBytes = w;
  1163     int maxBytes = w;
  1138 
  1164 
  1139     jsm = (*env)->GetObjectField(env, rasterP->jraster, g_RasterSampleModelID);
  1165     jsm = (*env)->GetObjectField(env, rasterP->jraster, g_RasterSampleModelID);
  1140     jdatabuffer = (*env)->GetObjectField(env, rasterP->jraster,
  1166     jdatabuffer = (*env)->GetObjectField(env, rasterP->jraster,
  1141                                          g_RasterDataBufferID);
  1167                                          g_RasterDataBufferID);
       
  1168     if (band >= numBands) {
       
  1169         JNU_ThrowInternalError(env, "Band out of range.");
       
  1170         return -1;
       
  1171     }
  1142     /* Here is the generic code */
  1172     /* Here is the generic code */
  1143     jdata = (*env)->NewIntArray(env, maxBytes*rasterP->numBands*maxLines);
  1173     jdata = (*env)->NewIntArray(env, maxBytes*rasterP->numBands*maxLines);
  1144     if (JNU_IsNull(env, jdata)) {
  1174     if (JNU_IsNull(env, jdata)) {
  1145         JNU_ThrowOutOfMemoryError(env, "Out of Memory");
  1175         JNU_ThrowOutOfMemoryError(env, "Out of Memory");
  1146         return -1;
  1176         return -1;
  1147     }
  1177     }
  1148     if (band >= 0) {
  1178     if (band >= 0) {
  1149         int dOff;
  1179         int dOff;
  1150         if (band >= numBands) {
       
  1151             (*env)->DeleteLocalRef(env, jdata);
       
  1152             JNU_ThrowInternalError(env, "Band out of range.");
       
  1153             return -1;
       
  1154         }
       
  1155         off = 0;
  1180         off = 0;
  1156         for (y=0; y < h; y+=maxLines) {
  1181         for (y=0; y < h; y+=maxLines) {
  1157             if (y+maxLines > h) {
  1182             if (y+maxLines > h) {
  1158                 maxBytes = w*numBands;
  1183                 maxBytes = w*numBands;
  1159                 maxLines = h - y;
  1184                 maxLines = h - y;