jdk/src/macosx/native/sun/awt/CDataTransferer.m
changeset 23652 11515e3c3f85
parent 23651 e41298d0da2f
equal deleted inserted replaced
23651:e41298d0da2f 23652:11515e3c3f85
    52                               forKey:[NSNumber numberWithLong:sun_lwawt_macosx_CDataTransferer_CF_HTML]];
    52                               forKey:[NSNumber numberWithLong:sun_lwawt_macosx_CDataTransferer_CF_HTML]];
    53         [sStandardMappings setObject:NSPDFPboardType
    53         [sStandardMappings setObject:NSPDFPboardType
    54                               forKey:[NSNumber numberWithLong:sun_lwawt_macosx_CDataTransferer_CF_PDF]];
    54                               forKey:[NSNumber numberWithLong:sun_lwawt_macosx_CDataTransferer_CF_PDF]];
    55         [sStandardMappings setObject:NSURLPboardType
    55         [sStandardMappings setObject:NSURLPboardType
    56                               forKey:[NSNumber numberWithLong:sun_lwawt_macosx_CDataTransferer_CF_URL]];
    56                               forKey:[NSNumber numberWithLong:sun_lwawt_macosx_CDataTransferer_CF_URL]];
       
    57         [sStandardMappings setObject:NSPasteboardTypePNG
       
    58                               forKey:[NSNumber numberWithLong:sun_lwawt_macosx_CDataTransferer_CF_PNG]];
       
    59         [sStandardMappings setObject:(NSString*)kUTTypeJPEG
       
    60                               forKey:[NSNumber numberWithLong:sun_lwawt_macosx_CDataTransferer_CF_JPEG]];
    57     }
    61     }
    58     return sStandardMappings;
    62     return sStandardMappings;
    59 }
    63 }
    60 
    64 
    61 /*
    65 /*
   122     jstring returnValue = NULL;
   126     jstring returnValue = NULL;
   123 JNF_COCOA_ENTER(env);
   127 JNF_COCOA_ENTER(env);
   124     returnValue = JNFNSToJavaString(env, formatForIndex(index));
   128     returnValue = JNFNSToJavaString(env, formatForIndex(index));
   125 JNF_COCOA_EXIT(env);
   129 JNF_COCOA_EXIT(env);
   126     return returnValue;
   130     return returnValue;
   127 }
       
   128 
       
   129 /*
       
   130  * Class:     sun_lwawt_macosx_CDataTransferer
       
   131  * Method:    imageDataToPlatformImageBytes
       
   132  * Signature: ([III)[B
       
   133      */
       
   134 JNIEXPORT jbyteArray JNICALL Java_sun_lwawt_macosx_CDataTransferer_imageDataToPlatformImageBytes
       
   135 (JNIEnv *env, jobject obj, jintArray inPixelData, jint inWidth, jint inHeight)
       
   136 {
       
   137     jbyteArray returnValue = nil;
       
   138 JNF_COCOA_ENTER(env);
       
   139     UInt32 *rawImageData = (UInt32 *)(*env)->GetPrimitiveArrayCritical(env, inPixelData, 0);
       
   140 
       
   141     // The pixel data is in premultiplied ARGB format. That's exactly what
       
   142     // we need for the bitmap image rep.
       
   143     if (rawImageData != NULL) {
       
   144         NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
       
   145                                                                              pixelsWide:inWidth
       
   146                                                                              pixelsHigh:inHeight
       
   147                                                                           bitsPerSample:8
       
   148                                                                         samplesPerPixel:4
       
   149                                                                                hasAlpha:YES
       
   150                                                                                isPlanar:NO
       
   151                                                                          colorSpaceName:NSCalibratedRGBColorSpace
       
   152                                                                             bytesPerRow:(inWidth*4)
       
   153                                                                            bitsPerPixel:32];
       
   154 
       
   155         // Conver the ARGB data into RGBA data that the bitmap can draw.
       
   156         unsigned char *destData = [imageRep bitmapData];
       
   157         unsigned char *currentRowBase;
       
   158         jint x, y;
       
   159 
       
   160         for (y = 0; y < inHeight; y++) {
       
   161             currentRowBase = destData + y * (inWidth * 4);
       
   162             unsigned char *currElement = currentRowBase;
       
   163             for (x = 0; x < inWidth; x++) {
       
   164                 UInt32 currPixel = rawImageData[y * inWidth + x];
       
   165                 *currElement++ = ((currPixel & 0xFF0000) >> 16);
       
   166                 *currElement++ = ((currPixel & 0xFF00) >> 8);
       
   167                 *currElement++ = (currPixel & 0xFF);
       
   168                 *currElement++ = ((currPixel & 0xFF000000) >> 24);
       
   169             }
       
   170         }
       
   171 
       
   172         (*env)->ReleasePrimitiveArrayCritical(env, inPixelData, rawImageData, JNI_ABORT);
       
   173         NSData *tiffImage = [imageRep TIFFRepresentation];
       
   174         jsize tiffSize = (jsize)[tiffImage length]; // #warning 64-bit: -length returns NSUInteger, but NewByteArray takes jsize
       
   175         returnValue = (*env)->NewByteArray(env, tiffSize);
       
   176         CHECK_NULL_RETURN(returnValue, nil);
       
   177         jbyte *tiffData = (jbyte *)(*env)->GetPrimitiveArrayCritical(env, returnValue, 0);
       
   178         CHECK_NULL_RETURN(tiffData, nil);
       
   179         [tiffImage getBytes:tiffData];
       
   180         (*env)->ReleasePrimitiveArrayCritical(env, returnValue, tiffData, 0); // Do not use JNI_COMMIT, as that will not free the buffer copy when +ProtectJavaHeap is on.
       
   181         [imageRep release];
       
   182     }
       
   183 JNF_COCOA_EXIT(env);
       
   184     return returnValue;
       
   185 
       
   186 }
       
   187 
       
   188 static jobject getImageForByteStream(JNIEnv *env, jbyteArray sourceData)
       
   189 {
       
   190     CHECK_NULL_RETURN(sourceData, NULL);
       
   191 
       
   192     jsize sourceSize = (*env)->GetArrayLength(env, sourceData);
       
   193     if (sourceSize == 0) return NULL;
       
   194 
       
   195     jbyte *sourceBytes = (*env)->GetPrimitiveArrayCritical(env, sourceData, NULL);
       
   196     CHECK_NULL_RETURN(sourceBytes, NULL);
       
   197     NSData *rawData = [NSData dataWithBytes:sourceBytes length:sourceSize];
       
   198 
       
   199     NSImage *newImage = [[NSImage alloc] initWithData:rawData];
       
   200 
       
   201     (*env)->ReleasePrimitiveArrayCritical(env, sourceData, sourceBytes, JNI_ABORT);
       
   202     CHECK_NULL_RETURN(newImage, NULL);
       
   203 
       
   204     // The ownership of the NSImage is passed to the new CImage jobject. No need to release it.
       
   205     static JNF_CLASS_CACHE(jc_CImage, "sun/lwawt/macosx/CImage");
       
   206     static JNF_STATIC_MEMBER_CACHE(jm_CImage_getCreator, jc_CImage, "getCreator", "()Lsun/lwawt/macosx/CImage$Creator;");
       
   207     jobject creator = JNFCallStaticObjectMethod(env, jm_CImage_getCreator);
       
   208 
       
   209     static JNF_CLASS_CACHE(jc_CImage_Generator, "sun/lwawt/macosx/CImage$Creator");
       
   210     static JNF_MEMBER_CACHE(jm_CImage_Generator_createImageUsingNativeSize, jc_CImage_Generator, "createImageUsingNativeSize", "(J)Ljava/awt/image/BufferedImage;");
       
   211     return JNFCallObjectMethod(env, creator, jm_CImage_Generator_createImageUsingNativeSize, ptr_to_jlong(newImage)); // AWT_THREADING Safe (known object)
       
   212 }
       
   213 
       
   214 /*
       
   215  * Class:     sun_lwawt_macosx_CDataTransferer
       
   216  * Method:    getImageForByteStream
       
   217  * Signature: ([B)Ljava/awt/Image;
       
   218  */
       
   219 JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CDataTransferer_getImageForByteStream
       
   220   (JNIEnv *env, jobject obj, jbyteArray sourceData)
       
   221 {
       
   222     jobject img = NULL;
       
   223 JNF_COCOA_ENTER(env);
       
   224     img = getImageForByteStream(env, sourceData);
       
   225 JNF_COCOA_EXIT(env);
       
   226     return img;
       
   227 }
   131 }
   228 
   132 
   229 static jobjectArray CreateJavaFilenameArray(JNIEnv *env, NSArray *filenameArray)
   133 static jobjectArray CreateJavaFilenameArray(JNIEnv *env, NSArray *filenameArray)
   230 {
   134 {
   231     NSUInteger filenameCount = [filenameArray count];
   135     NSUInteger filenameCount = [filenameArray count];