jdk/src/macosx/native/sun/awt/CImage.m
changeset 12047 320a714614e9
child 12403 50b3f2982b59
equal deleted inserted replaced
12046:378aa3362868 12047:320a714614e9
       
     1 /*
       
     2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 #import <Cocoa/Cocoa.h>
       
    27 #import <JavaNativeFoundation/JavaNativeFoundation.h>
       
    28 
       
    29 #import "GeomUtilities.h"
       
    30 #import "ThreadUtilities.h"
       
    31 
       
    32 #import "sun_lwawt_macosx_CImage.h"
       
    33 
       
    34 
       
    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 /*
       
    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;
       
   137     if (noErr == GetIconRef(kOnSystemDisk, kSystemIconsCreator, selector, &iconRef)) {
       
   138         image = [[NSImage alloc] initWithIconRef:iconRef];
       
   139         if (image) CFRetain(image); // GC
       
   140         ReleaseIconRef(iconRef);
       
   141     }
       
   142 
       
   143 JNF_COCOA_EXIT(env);
       
   144 
       
   145     return ptr_to_jlong(image);
       
   146 }
       
   147 
       
   148 /*
       
   149  * Class:     sun_lwawt_macosx_CImage
       
   150  * Method:    nativeCreateNSImageFromFileContents
       
   151  * Signature: (Ljava/lang/String;)J
       
   152  */
       
   153 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromFileContents
       
   154 (JNIEnv *env, jclass klass, jstring file)
       
   155 {
       
   156     NSImage *image = nil;
       
   157 
       
   158 JNF_COCOA_ENTER(env);
       
   159 AWT_ASSERT_ANY_THREAD;
       
   160 
       
   161     NSString *path = JNFNormalizedNSStringForPath(env, file);
       
   162     image = [[NSImage alloc] initByReferencingFile:path];
       
   163     if (image) CFRetain(image); // GC
       
   164 
       
   165 JNF_COCOA_EXIT(env);
       
   166 
       
   167     return ptr_to_jlong(image);
       
   168 }
       
   169 
       
   170 /*
       
   171  * Class:     sun_lwawt_macosx_CImage
       
   172  * Method:    nativeCreateNSImageOfFileFromLaunchServices
       
   173  * Signature: (Ljava/lang/String;)J
       
   174  */
       
   175 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageOfFileFromLaunchServices
       
   176 (JNIEnv *env, jclass klass, jstring file)
       
   177 {
       
   178     __block NSImage *image = nil;
       
   179 
       
   180 JNF_COCOA_ENTER(env);
       
   181 AWT_ASSERT_ANY_THREAD;
       
   182 
       
   183     NSString *path = JNFNormalizedNSStringForPath(env, file);
       
   184     [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){
       
   185         image = [[NSWorkspace sharedWorkspace] iconForFile:path];
       
   186         [image setScalesWhenResized:TRUE];
       
   187         if (image) CFRetain(image); // GC
       
   188     }];
       
   189 
       
   190 JNF_COCOA_EXIT(env);
       
   191 
       
   192     return ptr_to_jlong(image);
       
   193 }
       
   194 
       
   195 /*
       
   196  * Class:     sun_lwawt_macosx_CImage
       
   197  * Method:    nativeCreateNSImageFromImageName
       
   198  * Signature: (Ljava/lang/String;)J
       
   199  */
       
   200 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromImageName
       
   201 (JNIEnv *env, jclass klass, jstring name)
       
   202 {
       
   203     NSImage *image = nil;
       
   204 
       
   205 JNF_COCOA_ENTER(env);
       
   206 AWT_ASSERT_ANY_THREAD;
       
   207 
       
   208     image = [NSImage imageNamed:JNFJavaToNSString(env, name)];
       
   209     if (image) CFRetain(image); // GC
       
   210 
       
   211 JNF_COCOA_EXIT(env);
       
   212 
       
   213     return ptr_to_jlong(image);
       
   214 }
       
   215 
       
   216 /*
       
   217  * Class:     sun_lwawt_macosx_CImage
       
   218  * Method:    nativeCopyNSImageIntoArray
       
   219  * Signature: (J[III)V
       
   220  */
       
   221 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeCopyNSImageIntoArray
       
   222 (JNIEnv *env, jclass klass, jlong nsImgPtr, jintArray buffer, jint w, jint h)
       
   223 {
       
   224 JNF_COCOA_ENTER(env);
       
   225 AWT_ASSERT_ANY_THREAD;
       
   226 
       
   227     NSImage *img = (NSImage *)jlong_to_ptr(nsImgPtr);
       
   228     jint *dst = (*env)->GetPrimitiveArrayCritical(env, buffer, NULL);
       
   229     if (dst) {
       
   230         CImage_CopyNSImageIntoArray(img, dst, w, h);
       
   231         (*env)->ReleasePrimitiveArrayCritical(env, buffer, dst, JNI_ABORT);
       
   232     }
       
   233 
       
   234 JNF_COCOA_EXIT(env);
       
   235 }
       
   236 
       
   237 /*
       
   238  * Class:     sun_lwawt_macosx_CImage
       
   239  * Method:    nativeGetNSImageSize
       
   240  * Signature: (J)Ljava/awt/geom/Dimension2D;
       
   241  */
       
   242 JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CImage_nativeGetNSImageSize
       
   243 (JNIEnv *env, jclass klass, jlong nsImgPtr)
       
   244 {
       
   245     jobject size = NULL;
       
   246 
       
   247 JNF_COCOA_ENTER(env);
       
   248 AWT_ASSERT_ANY_THREAD;
       
   249 
       
   250     size = NSToJavaSize(env, [(NSImage *)jlong_to_ptr(nsImgPtr) size]);
       
   251 
       
   252 JNF_COCOA_EXIT(env);
       
   253 
       
   254     return size;
       
   255 }
       
   256 
       
   257 /*
       
   258  * Class:     sun_lwawt_macosx_CImage
       
   259  * Method:    nativeSetNSImageSize
       
   260  * Signature: (JDD)V
       
   261  */
       
   262 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeSetNSImageSize
       
   263 (JNIEnv *env, jclass clazz, jlong image, jdouble w, jdouble h)
       
   264 {
       
   265     if (!image) return;
       
   266     NSImage *i = (NSImage *)jlong_to_ptr(image);
       
   267 
       
   268 JNF_COCOA_ENTER(env);
       
   269 
       
   270     [i setScalesWhenResized:TRUE];
       
   271     [i setSize:NSMakeSize(w, h)];
       
   272 
       
   273 JNF_COCOA_EXIT(env);
       
   274 }