src/macosx/native/sun/awt/CImage.m

Print this page




  35 static void CImage_CopyArrayIntoNSImageRep
  36 (jint *srcPixels, jint *dstPixels, int width, int height)
  37 {
  38     int x, y;
  39     // TODO: test this on big endian systems (not sure if its correct)...
  40     for (y = 0; y < height; y++) {
  41         for (x = 0; x < width; x++) {
  42             jint pix = srcPixels[x];
  43             jint a = (pix >> 24) & 0xff;
  44             jint r = (pix >> 16) & 0xff;
  45             jint g = (pix >>  8) & 0xff;
  46             jint b = (pix      ) & 0xff;
  47             dstPixels[x] = (b << 24) | (g << 16) | (r << 8) | a;
  48         }
  49         srcPixels += width; // TODO: use explicit scanStride
  50         dstPixels += width;
  51     }
  52 }
  53 
  54 static void CImage_CopyNSImageIntoArray
  55 (NSImage *srcImage, jint *dstPixels, int width, int height)
  56 {


  57     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
  58     CGContextRef cgRef = CGBitmapContextCreate(dstPixels, width, height, 8, width * 4, colorspace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
  59     CGColorSpaceRelease(colorspace);
  60     NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:cgRef flipped:NO];
  61     CGContextRelease(cgRef);
  62     NSGraphicsContext *oldContext = [[NSGraphicsContext currentContext] retain];
  63     [NSGraphicsContext setCurrentContext:context];
  64     NSRect rect = NSMakeRect(0, 0, width, height);
  65     [srcImage drawInRect:rect
  66                 fromRect:rect
  67                operation:NSCompositeSourceOver
  68                 fraction:1.0];
  69     [NSGraphicsContext setCurrentContext:oldContext];
  70     [oldContext release];
  71 }
  72 
  73 static NSBitmapImageRep* CImage_CreateImageRep(JNIEnv *env, jintArray buffer, jint width, jint height)
  74 {
  75     NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
  76                                                                          pixelsWide:width
  77                                                                          pixelsHigh:height
  78                                                                       bitsPerSample:8
  79                                                                     samplesPerPixel:4
  80                                                                            hasAlpha:YES
  81                                                                            isPlanar:NO
  82                                                                      colorSpaceName:NSDeviceRGBColorSpace
  83                                                                        bitmapFormat:NSAlphaFirstBitmapFormat
  84                                                                         bytesPerRow:width*4 // TODO: use explicit scanStride
  85                                                                        bitsPerPixel:32];
  86 


 249  * Signature: (Ljava/lang/String;)J
 250  */
 251 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromImageName
 252 (JNIEnv *env, jclass klass, jstring name)
 253 {
 254     NSImage *image = nil;
 255 
 256 JNF_COCOA_ENTER(env);
 257 
 258     image = [NSImage imageNamed:JNFJavaToNSString(env, name)];
 259     if (image) CFRetain(image); // GC
 260 
 261 JNF_COCOA_EXIT(env);
 262 
 263     return ptr_to_jlong(image);
 264 }
 265 
 266 /*
 267  * Class:     sun_lwawt_macosx_CImage
 268  * Method:    nativeCopyNSImageIntoArray
 269  * Signature: (J[III)V
 270  */
 271 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeCopyNSImageIntoArray
 272 (JNIEnv *env, jclass klass, jlong nsImgPtr, jintArray buffer, jint w, jint h)
 273 {
 274 JNF_COCOA_ENTER(env);
 275 
 276     NSImage *img = (NSImage *)jlong_to_ptr(nsImgPtr);
 277     jint *dst = (*env)->GetPrimitiveArrayCritical(env, buffer, NULL);
 278     if (dst) {
 279         CImage_CopyNSImageIntoArray(img, dst, w, h);


 280         (*env)->ReleasePrimitiveArrayCritical(env, buffer, dst, JNI_ABORT);
 281     }
 282 
 283 JNF_COCOA_EXIT(env);
 284 }
 285 
 286 /*
 287  * Class:     sun_lwawt_macosx_CImage
 288  * Method:    nativeGetNSImageSize
 289  * Signature: (J)Ljava/awt/geom/Dimension2D;
 290  */
 291 JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CImage_nativeGetNSImageSize
 292 (JNIEnv *env, jclass klass, jlong nsImgPtr)
 293 {
 294     jobject size = NULL;
 295 
 296 JNF_COCOA_ENTER(env);
 297 
 298     size = NSToJavaSize(env, [(NSImage *)jlong_to_ptr(nsImgPtr) size]);
 299 
 300 JNF_COCOA_EXIT(env);
 301 
 302     return size;
 303 }


 326  * Method:    nativeResizeNSImageRepresentations
 327  * Signature: (JDD)V
 328  */
 329 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeResizeNSImageRepresentations
 330 (JNIEnv *env, jclass clazz, jlong image, jdouble w, jdouble h)
 331 {
 332     if (!image) return;
 333     NSImage *i = (NSImage *)jlong_to_ptr(image);
 334     
 335 JNF_COCOA_ENTER(env);
 336     
 337     NSImageRep *imageRep = nil;
 338     NSArray *imageRepresentations = [i representations];
 339     NSEnumerator *imageEnumerator = [imageRepresentations objectEnumerator];
 340     while ((imageRep = [imageEnumerator nextObject]) != nil) {
 341         [imageRep setSize:NSMakeSize(w, h)];
 342     }
 343     
 344 JNF_COCOA_EXIT(env);
 345 }


























































  35 static void CImage_CopyArrayIntoNSImageRep
  36 (jint *srcPixels, jint *dstPixels, int width, int height)
  37 {
  38     int x, y;
  39     // TODO: test this on big endian systems (not sure if its correct)...
  40     for (y = 0; y < height; y++) {
  41         for (x = 0; x < width; x++) {
  42             jint pix = srcPixels[x];
  43             jint a = (pix >> 24) & 0xff;
  44             jint r = (pix >> 16) & 0xff;
  45             jint g = (pix >>  8) & 0xff;
  46             jint b = (pix      ) & 0xff;
  47             dstPixels[x] = (b << 24) | (g << 16) | (r << 8) | a;
  48         }
  49         srcPixels += width; // TODO: use explicit scanStride
  50         dstPixels += width;
  51     }
  52 }
  53 
  54 static void CImage_CopyNSImageIntoArray
  55 (NSImage *srcImage, jint *dstPixels, NSRect fromRect, NSRect toRect)
  56 {
  57     int width = toRect.size.width;
  58     int height = toRect.size.height;
  59     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
  60     CGContextRef cgRef = CGBitmapContextCreate(dstPixels, width, height, 8, width * 4, colorspace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
  61     CGColorSpaceRelease(colorspace);
  62     NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:cgRef flipped:NO];
  63     CGContextRelease(cgRef);
  64     NSGraphicsContext *oldContext = [[NSGraphicsContext currentContext] retain];
  65     [NSGraphicsContext setCurrentContext:context];
  66     [srcImage drawInRect:toRect
  67                 fromRect:fromRect

  68                operation:NSCompositeSourceOver
  69                 fraction:1.0];
  70     [NSGraphicsContext setCurrentContext:oldContext];
  71     [oldContext release];
  72 }
  73 
  74 static NSBitmapImageRep* CImage_CreateImageRep(JNIEnv *env, jintArray buffer, jint width, jint height)
  75 {
  76     NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
  77                                                                          pixelsWide:width
  78                                                                          pixelsHigh:height
  79                                                                       bitsPerSample:8
  80                                                                     samplesPerPixel:4
  81                                                                            hasAlpha:YES
  82                                                                            isPlanar:NO
  83                                                                      colorSpaceName:NSDeviceRGBColorSpace
  84                                                                        bitmapFormat:NSAlphaFirstBitmapFormat
  85                                                                         bytesPerRow:width*4 // TODO: use explicit scanStride
  86                                                                        bitsPerPixel:32];
  87 


 250  * Signature: (Ljava/lang/String;)J
 251  */
 252 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromImageName
 253 (JNIEnv *env, jclass klass, jstring name)
 254 {
 255     NSImage *image = nil;
 256 
 257 JNF_COCOA_ENTER(env);
 258 
 259     image = [NSImage imageNamed:JNFJavaToNSString(env, name)];
 260     if (image) CFRetain(image); // GC
 261 
 262 JNF_COCOA_EXIT(env);
 263 
 264     return ptr_to_jlong(image);
 265 }
 266 
 267 /*
 268  * Class:     sun_lwawt_macosx_CImage
 269  * Method:    nativeCopyNSImageIntoArray
 270  * Signature: (J[IIIII)V
 271  */
 272 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeCopyNSImageIntoArray
 273 (JNIEnv *env, jclass klass, jlong nsImgPtr, jintArray buffer, jint sw, jint sh, jint dw, jint dh)
 274 {
 275     JNF_COCOA_ENTER(env);
 276     
 277     NSImage *img = (NSImage *)jlong_to_ptr(nsImgPtr);
 278     jint *dst = (*env)->GetPrimitiveArrayCritical(env, buffer, NULL);
 279     if (dst) {
 280         NSRect fromRect = NSMakeRect(0, 0, sw, sh);
 281         NSRect toRect = NSMakeRect(0, 0, dw, dh);
 282         CImage_CopyNSImageIntoArray(img, dst, fromRect, toRect);
 283         (*env)->ReleasePrimitiveArrayCritical(env, buffer, dst, JNI_ABORT);
 284     }
 285     
 286     JNF_COCOA_EXIT(env);
 287 }
 288 
 289 /*
 290  * Class:     sun_lwawt_macosx_CImage
 291  * Method:    nativeGetNSImageSize
 292  * Signature: (J)Ljava/awt/geom/Dimension2D;
 293  */
 294 JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CImage_nativeGetNSImageSize
 295 (JNIEnv *env, jclass klass, jlong nsImgPtr)
 296 {
 297     jobject size = NULL;
 298 
 299 JNF_COCOA_ENTER(env);
 300 
 301     size = NSToJavaSize(env, [(NSImage *)jlong_to_ptr(nsImgPtr) size]);
 302 
 303 JNF_COCOA_EXIT(env);
 304 
 305     return size;
 306 }


 329  * Method:    nativeResizeNSImageRepresentations
 330  * Signature: (JDD)V
 331  */
 332 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeResizeNSImageRepresentations
 333 (JNIEnv *env, jclass clazz, jlong image, jdouble w, jdouble h)
 334 {
 335     if (!image) return;
 336     NSImage *i = (NSImage *)jlong_to_ptr(image);
 337     
 338 JNF_COCOA_ENTER(env);
 339     
 340     NSImageRep *imageRep = nil;
 341     NSArray *imageRepresentations = [i representations];
 342     NSEnumerator *imageEnumerator = [imageRepresentations objectEnumerator];
 343     while ((imageRep = [imageEnumerator nextObject]) != nil) {
 344         [imageRep setSize:NSMakeSize(w, h)];
 345     }
 346     
 347 JNF_COCOA_EXIT(env);
 348 }
 349 
 350 /*
 351  * Class:     sun_lwawt_macosx_CImage
 352  * Method:    nativeGetNSImageRepresentationsCount
 353  * Signature: (JDD)Ljava/awt/geom/Dimension2D;
 354  */
 355 JNIEXPORT jobjectArray JNICALL Java_sun_lwawt_macosx_CImage_nativeGetNSImageRepresentationSizes
 356 (JNIEnv *env, jclass clazz, jlong image, jdouble w, jdouble h)
 357 {
 358     if (!image) return 0;
 359     jobjectArray jreturnArray = NULL;
 360     NSImage *img = (NSImage *)jlong_to_ptr(image);
 361 
 362 JNF_COCOA_ENTER(env);
 363 
 364     NSMutableArray *imagePixelSizes = [[NSMutableArray alloc] init];
 365 
 366     NSImageRep *imageRep = nil;
 367     NSEnumerator *imageEnumerator = [[img representations] objectEnumerator];
 368 
 369     while ((imageRep = [imageEnumerator nextObject]) != nil) {
 370         NSSize imageRepSize = [imageRep size];
 371         if (imageRepSize.width == w && imageRepSize.height == h){
 372             NSSize pixelSize = NSMakeSize([imageRep pixelsWide], [imageRep pixelsHigh]);
 373             [imagePixelSizes addObject: [NSValue valueWithSize: pixelSize]];
 374         }
 375     }
 376 
 377     NSArray *sortedPixelSizes = [imagePixelSizes sortedArrayUsingComparator: ^(id obj1, id obj2) {
 378         NSSize size1 = [obj1 sizeValue];
 379         NSSize size2 = [obj2 sizeValue];
 380         return (NSComparisonResult) ((size1.width <= size2.width && size1.height <= size2.height) ?
 381             NSOrderedAscending :
 382             NSOrderedDescending);
 383     }];
 384 
 385     jsize count = [sortedPixelSizes count];
 386     static JNF_CLASS_CACHE(jc_Dimension, "java/awt/Dimension");
 387     jreturnArray = JNFNewObjectArray(env, &jc_Dimension, count);
 388 
 389     jsize i;
 390     for(i = 0; i < count; i++){
 391         NSSize pixelSize = [[sortedPixelSizes objectAtIndex: i] sizeValue];
 392         
 393         (*env)->SetObjectArrayElement(env, jreturnArray, i, NSToJavaSize(env, pixelSize));
 394         if ((*env)->ExceptionOccurred(env)) {
 395             (*env)->ExceptionDescribe(env);
 396             (*env)->ExceptionClear(env);
 397             continue;
 398         }
 399     }
 400 
 401 JNF_COCOA_EXIT(env);
 402 
 403     return jreturnArray;
 404 }