src/macosx/native/sun/awt/CImage.m
Print this page
*** 50,71 ****
dstPixels += width;
}
}
static void CImage_CopyNSImageIntoArray
! (NSImage *srcImage, jint *dstPixels, int width, int height)
{
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGContextRef cgRef = CGBitmapContextCreate(dstPixels, width, height, 8, width * 4, colorspace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
CGColorSpaceRelease(colorspace);
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:cgRef flipped:NO];
CGContextRelease(cgRef);
NSGraphicsContext *oldContext = [[NSGraphicsContext currentContext] retain];
[NSGraphicsContext setCurrentContext:context];
! NSRect rect = NSMakeRect(0, 0, width, height);
! [srcImage drawInRect:rect
! fromRect:rect
operation:NSCompositeSourceOver
fraction:1.0];
[NSGraphicsContext setCurrentContext:oldContext];
[oldContext release];
}
--- 50,72 ----
dstPixels += width;
}
}
static void CImage_CopyNSImageIntoArray
! (NSImage *srcImage, jint *dstPixels, NSRect fromRect, NSRect toRect)
{
+ int width = toRect.size.width;
+ int height = toRect.size.height;
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGContextRef cgRef = CGBitmapContextCreate(dstPixels, width, height, 8, width * 4, colorspace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
CGColorSpaceRelease(colorspace);
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:cgRef flipped:NO];
CGContextRelease(cgRef);
NSGraphicsContext *oldContext = [[NSGraphicsContext currentContext] retain];
[NSGraphicsContext setCurrentContext:context];
! [srcImage drawInRect:toRect
! fromRect:fromRect
operation:NSCompositeSourceOver
fraction:1.0];
[NSGraphicsContext setCurrentContext:oldContext];
[oldContext release];
}
*** 264,288 ****
}
/*
* Class: sun_lwawt_macosx_CImage
* Method: nativeCopyNSImageIntoArray
! * Signature: (J[III)V
*/
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeCopyNSImageIntoArray
! (JNIEnv *env, jclass klass, jlong nsImgPtr, jintArray buffer, jint w, jint h)
{
! JNF_COCOA_ENTER(env);
NSImage *img = (NSImage *)jlong_to_ptr(nsImgPtr);
jint *dst = (*env)->GetPrimitiveArrayCritical(env, buffer, NULL);
if (dst) {
! CImage_CopyNSImageIntoArray(img, dst, w, h);
(*env)->ReleasePrimitiveArrayCritical(env, buffer, dst, JNI_ABORT);
}
! JNF_COCOA_EXIT(env);
}
/*
* Class: sun_lwawt_macosx_CImage
* Method: nativeGetNSImageSize
--- 265,291 ----
}
/*
* Class: sun_lwawt_macosx_CImage
* Method: nativeCopyNSImageIntoArray
! * Signature: (J[IIIII)V
*/
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeCopyNSImageIntoArray
! (JNIEnv *env, jclass klass, jlong nsImgPtr, jintArray buffer, jint sw, jint sh, jint dw, jint dh)
{
! JNF_COCOA_ENTER(env);
NSImage *img = (NSImage *)jlong_to_ptr(nsImgPtr);
jint *dst = (*env)->GetPrimitiveArrayCritical(env, buffer, NULL);
if (dst) {
! NSRect fromRect = NSMakeRect(0, 0, sw, sh);
! NSRect toRect = NSMakeRect(0, 0, dw, dh);
! CImage_CopyNSImageIntoArray(img, dst, fromRect, toRect);
(*env)->ReleasePrimitiveArrayCritical(env, buffer, dst, JNI_ABORT);
}
! JNF_COCOA_EXIT(env);
}
/*
* Class: sun_lwawt_macosx_CImage
* Method: nativeGetNSImageSize
*** 341,345 ****
--- 344,404 ----
[imageRep setSize:NSMakeSize(w, h)];
}
JNF_COCOA_EXIT(env);
}
+
+ /*
+ * Class: sun_lwawt_macosx_CImage
+ * Method: nativeGetNSImageRepresentationsCount
+ * Signature: (JDD)Ljava/awt/geom/Dimension2D;
+ */
+ JNIEXPORT jobjectArray JNICALL Java_sun_lwawt_macosx_CImage_nativeGetNSImageRepresentationSizes
+ (JNIEnv *env, jclass clazz, jlong image, jdouble w, jdouble h)
+ {
+ if (!image) return 0;
+ jobjectArray jreturnArray = NULL;
+ NSImage *img = (NSImage *)jlong_to_ptr(image);
+
+ JNF_COCOA_ENTER(env);
+
+ NSMutableArray *imagePixelSizes = [[NSMutableArray alloc] init];
+
+ NSImageRep *imageRep = nil;
+ NSEnumerator *imageEnumerator = [[img representations] objectEnumerator];
+
+ while ((imageRep = [imageEnumerator nextObject]) != nil) {
+ NSSize imageRepSize = [imageRep size];
+ if (imageRepSize.width == w && imageRepSize.height == h){
+ NSSize pixelSize = NSMakeSize([imageRep pixelsWide], [imageRep pixelsHigh]);
+ [imagePixelSizes addObject: [NSValue valueWithSize: pixelSize]];
+ }
+ }
+
+ NSArray *sortedPixelSizes = [imagePixelSizes sortedArrayUsingComparator: ^(id obj1, id obj2) {
+ NSSize size1 = [obj1 sizeValue];
+ NSSize size2 = [obj2 sizeValue];
+ return (NSComparisonResult) ((size1.width <= size2.width && size1.height <= size2.height) ?
+ NSOrderedAscending :
+ NSOrderedDescending);
+ }];
+
+ jsize count = [sortedPixelSizes count];
+ static JNF_CLASS_CACHE(jc_Dimension, "java/awt/Dimension");
+ jreturnArray = JNFNewObjectArray(env, &jc_Dimension, count);
+
+ jsize i;
+ for(i = 0; i < count; i++){
+ NSSize pixelSize = [[sortedPixelSizes objectAtIndex: i] sizeValue];
+
+ (*env)->SetObjectArrayElement(env, jreturnArray, i, NSToJavaSize(env, pixelSize));
+ if ((*env)->ExceptionOccurred(env)) {
+ (*env)->ExceptionDescribe(env);
+ (*env)->ExceptionClear(env);
+ continue;
+ }
+ }
+
+ JNF_COCOA_EXIT(env);
+
+ return jreturnArray;
+ }
\ No newline at end of file