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 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 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 NSComparisonResult getOrder(BOOL order){ 351 return (NSComparisonResult) (order ? NSOrderedAscending : NSOrderedDescending); 352 } 353 354 /* 355 * Class: sun_lwawt_macosx_CImage 356 * Method: nativeGetNSImageRepresentationsCount 357 * Signature: (JDD)[Ljava/awt/geom/Dimension2D; 358 */ 359 JNIEXPORT jobjectArray JNICALL Java_sun_lwawt_macosx_CImage_nativeGetNSImageRepresentationSizes 360 (JNIEnv *env, jclass clazz, jlong image, jdouble w, jdouble h) 361 { 362 if (!image) return 0; 363 jobjectArray jreturnArray = NULL; 364 NSImage *img = (NSImage *)jlong_to_ptr(image); 365 366 JNF_COCOA_ENTER(env); 367 368 NSArray *imageRepresentations = [image representations]; 369 if([imageRepresentations count] == 0){ 370 return NULL; 371 } 372 373 NSArray *sortedImageRepresentations = [imageRepresentations sortedArrayUsingComparator: ^(id obj1, id obj2) { 374 375 NSImageRep *imageRep1 = (NSImageRep *) obj1; 376 NSImageRep *imageRep2 = (NSImageRep *) obj2; 377 NSSize size1 = [imageRep1 size]; 378 NSSize size2 = [imageRep2 size]; 379 380 if (NSEqualSizes(size1, size2)) { 381 return getOrder([imageRep1 pixelsWide] <= [imageRep2 pixelsWide] && 382 [imageRep1 pixelsHigh] <= [imageRep2 pixelsHigh]); 383 } 384 385 return getOrder(size1.width <= size2.width && size1.height <= size2.height); 386 }]; 387 388 NSMutableArray *sortedPixelSizes = [[NSMutableArray alloc] init]; 389 NSSize lastSize = [[sortedImageRepresentations lastObject] size]; 390 NSSize imageRepSize = NSZeroSize; 391 392 jsize i; 393 jsize count = [sortedImageRepresentations count]; 394 for(i = 0; i < count; i++){ 395 imageRepSize = [[sortedImageRepresentations objectAtIndex: i] size]; 396 397 if ((w <= imageRepSize.width && h <= imageRepSize.height) 398 || NSEqualSizes(imageRepSize, lastSize)) { 399 break; 400 } 401 } 402 403 NSSize bestFitSize = imageRepSize; 404 405 for(; i < count; i++){ 406 NSImageRep *imageRep = [sortedImageRepresentations objectAtIndex: i]; 407 408 if (!NSEqualSizes([imageRep size], bestFitSize)) { 409 break; 410 } 411 412 NSSize pixelSize = NSMakeSize([imageRep pixelsWide], [imageRep pixelsHigh]); 413 [sortedPixelSizes addObject: [NSValue valueWithSize: pixelSize]]; 414 } 415 416 count = [sortedPixelSizes count]; 417 static JNF_CLASS_CACHE(jc_Dimension, "java/awt/Dimension"); 418 jreturnArray = JNFNewObjectArray(env, &jc_Dimension, count); 419 420 for(i = 0; i < count; i++){ 421 NSSize pixelSize = [[sortedPixelSizes objectAtIndex: i] sizeValue]; 422 423 (*env)->SetObjectArrayElement(env, jreturnArray, i, NSToJavaSize(env, pixelSize)); 424 if ((*env)->ExceptionOccurred(env)) { 425 (*env)->ExceptionDescribe(env); 426 (*env)->ExceptionClear(env); 427 continue; 428 } 429 } 430 431 JNF_COCOA_EXIT(env); 432 433 return jreturnArray; 434 } |