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

Print this page




  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 /*
  74  * Class:     sun_lwawt_macosx_CImage
  75  * Method:    nativeCreateNSImageFromArray
  76  * Signature: ([III)J
  77  */
  78 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromArray
  79 (JNIEnv *env, jclass klass, jintArray buffer, jint width, jint height)
  80 {
  81     jlong result = 0L;
  82 
  83 JNF_COCOA_ENTER(env);
  84 AWT_ASSERT_ANY_THREAD;
  85 
  86     NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
  87                                                                          pixelsWide:width
  88                                                                          pixelsHigh:height
  89                                                                       bitsPerSample:8
  90                                                                     samplesPerPixel:4
  91                                                                            hasAlpha:YES
  92                                                                            isPlanar:NO
  93                                                                      colorSpaceName:NSDeviceRGBColorSpace
  94                                                                        bitmapFormat:NSAlphaFirstBitmapFormat
  95                                                                         bytesPerRow:width*4 // TODO: use explicit scanStride
  96                                                                        bitsPerPixel:32];
  97 
  98     jint *imgData = (jint *)[imageRep bitmapData];
  99     if (imgData == NULL) return 0L;
 100 
 101     jint *src = (*env)->GetPrimitiveArrayCritical(env, buffer, NULL);
 102     if (src == NULL) return 0L;
 103 
 104     CImage_CopyArrayIntoNSImageRep(src, imgData, width, height);
 105 
 106     (*env)->ReleasePrimitiveArrayCritical(env, buffer, src, JNI_ABORT);
 107 


















 108     NSImage *nsImage = [[NSImage alloc] initWithSize:NSMakeSize(width, height)];
 109     [nsImage addRepresentation:imageRep];
 110     [imageRep release];
 111 
 112     if (nsImage != nil) {
 113         CFRetain(nsImage); // GC
 114     }
 115 
 116     result = ptr_to_jlong(nsImage);


















































 117 
 118 JNF_COCOA_EXIT(env);
 119 
 120     return result;
 121 }
 122 
 123 /*
 124  * Class:     sun_lwawt_macosx_CImage
 125  * Method:    nativeCreateNSImageFromIconSelector
 126  * Signature: (I)J
 127  */
 128 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromIconSelector
 129 (JNIEnv *env, jclass klass, jint selector)
 130 {
 131     NSImage *image = nil;
 132 
 133 JNF_COCOA_ENTER(env);
 134 AWT_ASSERT_ANY_THREAD;
 135 
 136     IconRef iconRef;




  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 
  87     jint *imgData = (jint *)[imageRep bitmapData];
  88     if (imgData == NULL) return 0L;
  89 
  90     jint *src = (*env)->GetPrimitiveArrayCritical(env, buffer, NULL);
  91     if (src == NULL) return 0L;
  92 
  93     CImage_CopyArrayIntoNSImageRep(src, imgData, width, height);
  94 
  95     (*env)->ReleasePrimitiveArrayCritical(env, buffer, src, JNI_ABORT);
  96 
  97     return imageRep;
  98 }
  99 
 100 /*
 101  * Class:     sun_lwawt_macosx_CImage
 102  * Method:    nativeCreateNSImageFromArray
 103  * Signature: ([III)J
 104  */
 105 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromArray
 106 (JNIEnv *env, jclass klass, jintArray buffer, jint width, jint height)
 107 {
 108     jlong result = 0L;
 109 
 110 JNF_COCOA_ENTER(env);
 111 AWT_ASSERT_ANY_THREAD;
 112 
 113     NSBitmapImageRep* imageRep = CImage_CreateImageRep(env, buffer, width, height);
 114     if (imageRep) {
 115         NSImage *nsImage = [[NSImage alloc] initWithSize:NSMakeSize(width, height)];
 116         [nsImage addRepresentation:imageRep];
 117         [imageRep release];
 118 
 119         if (nsImage != nil) {
 120             CFRetain(nsImage); // GC
 121         }
 122 
 123         result = ptr_to_jlong(nsImage);
 124     }
 125 
 126 JNF_COCOA_EXIT(env);
 127 
 128     return result;
 129 }
 130 
 131 /*
 132  * Class:     sun_lwawt_macosx_CImage
 133  * Method:    nativeCreateNSImageFromArrays
 134  * Signature: ([[I[I[I)J
 135  */
 136 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromArrays
 137 (JNIEnv *env, jclass klass, jobjectArray buffers, jintArray widths, jintArray heights)
 138 {
 139     jlong result = 0L;
 140 
 141 JNF_COCOA_ENTER(env);
 142 AWT_ASSERT_ANY_THREAD;
 143 
 144     jsize num = (*env)->GetArrayLength(env, buffers);
 145     NSMutableArray * reps = [NSMutableArray arrayWithCapacity: num];
 146 
 147     jint * ws = (*env)->GetIntArrayElements(env, widths, NULL);
 148     jint * hs = (*env)->GetIntArrayElements(env, heights, NULL);
 149 
 150     jsize i;
 151     for (i = 0; i < num; i++) {
 152         jintArray buffer = (*env)->GetObjectArrayElement(env, buffers, i);
 153 
 154         NSBitmapImageRep* imageRep = CImage_CreateImageRep(env, buffer, ws[i], hs[i]);
 155         if (imageRep) {
 156             [reps addObject: imageRep];
 157         }
 158     }
 159 
 160     (*env)->ReleaseIntArrayElements(env, heights, hs, JNI_ABORT);
 161     (*env)->ReleaseIntArrayElements(env, widths, ws, JNI_ABORT);
 162 
 163     if ([reps count]) {
 164         NSImage *nsImage = [[NSImage alloc] initWithSize:NSMakeSize(0, 0)];
 165         [nsImage addRepresentations: reps];
 166         [reps release];
 167 
 168         if (nsImage != nil) {
 169             CFRetain(nsImage); // GC
 170         }
 171 
 172         result = ptr_to_jlong(nsImage);
 173     }
 174 
 175 JNF_COCOA_EXIT(env);
 176 
 177     return result;
 178 }
 179 
 180 /*
 181  * Class:     sun_lwawt_macosx_CImage
 182  * Method:    nativeCreateNSImageFromIconSelector
 183  * Signature: (I)J
 184  */
 185 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromIconSelector
 186 (JNIEnv *env, jclass klass, jint selector)
 187 {
 188     NSImage *image = nil;
 189 
 190 JNF_COCOA_ENTER(env);
 191 AWT_ASSERT_ANY_THREAD;
 192 
 193     IconRef iconRef;