author | alexsch |
Tue, 04 Mar 2014 17:06:00 +0400 | |
changeset 23615 | a0825e77bfad |
parent 23328 | 4c53a6ebc779 |
child 23651 | e41298d0da2f |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
23319
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
2 |
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. |
12047 | 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 |
*/ |
|
23615
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
25 |
#import "jni_util.h" |
12047 | 26 |
|
27 |
#import <Cocoa/Cocoa.h> |
|
28 |
#import <JavaNativeFoundation/JavaNativeFoundation.h> |
|
29 |
||
30 |
#import "GeomUtilities.h" |
|
31 |
#import "ThreadUtilities.h" |
|
32 |
||
33 |
#import "sun_lwawt_macosx_CImage.h" |
|
34 |
||
35 |
||
36 |
static void CImage_CopyArrayIntoNSImageRep |
|
37 |
(jint *srcPixels, jint *dstPixels, int width, int height) |
|
38 |
{ |
|
39 |
int x, y; |
|
40 |
// TODO: test this on big endian systems (not sure if its correct)... |
|
41 |
for (y = 0; y < height; y++) { |
|
42 |
for (x = 0; x < width; x++) { |
|
43 |
jint pix = srcPixels[x]; |
|
44 |
jint a = (pix >> 24) & 0xff; |
|
45 |
jint r = (pix >> 16) & 0xff; |
|
46 |
jint g = (pix >> 8) & 0xff; |
|
47 |
jint b = (pix ) & 0xff; |
|
48 |
dstPixels[x] = (b << 24) | (g << 16) | (r << 8) | a; |
|
49 |
} |
|
50 |
srcPixels += width; // TODO: use explicit scanStride |
|
51 |
dstPixels += width; |
|
52 |
} |
|
53 |
} |
|
54 |
||
55 |
static void CImage_CopyNSImageIntoArray |
|
23615
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
56 |
(NSImage *srcImage, jint *dstPixels, NSRect fromRect, NSRect toRect) |
12047 | 57 |
{ |
23615
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
58 |
int width = toRect.size.width; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
59 |
int height = toRect.size.height; |
12047 | 60 |
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); |
23615
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
61 |
CGContextRef cgRef = CGBitmapContextCreate(dstPixels, width, height, |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
62 |
8, width * 4, colorspace, |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
63 |
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host); |
12047 | 64 |
CGColorSpaceRelease(colorspace); |
65 |
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:cgRef flipped:NO]; |
|
66 |
CGContextRelease(cgRef); |
|
67 |
NSGraphicsContext *oldContext = [[NSGraphicsContext currentContext] retain]; |
|
68 |
[NSGraphicsContext setCurrentContext:context]; |
|
23615
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
69 |
[srcImage drawInRect:toRect |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
70 |
fromRect:fromRect |
12047 | 71 |
operation:NSCompositeSourceOver |
72 |
fraction:1.0]; |
|
73 |
[NSGraphicsContext setCurrentContext:oldContext]; |
|
74 |
[oldContext release]; |
|
75 |
} |
|
76 |
||
12403
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
77 |
static NSBitmapImageRep* CImage_CreateImageRep(JNIEnv *env, jintArray buffer, jint width, jint height) |
12047 | 78 |
{ |
79 |
NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL |
|
80 |
pixelsWide:width |
|
81 |
pixelsHigh:height |
|
82 |
bitsPerSample:8 |
|
83 |
samplesPerPixel:4 |
|
84 |
hasAlpha:YES |
|
85 |
isPlanar:NO |
|
86 |
colorSpaceName:NSDeviceRGBColorSpace |
|
87 |
bitmapFormat:NSAlphaFirstBitmapFormat |
|
88 |
bytesPerRow:width*4 // TODO: use explicit scanStride |
|
89 |
bitsPerPixel:32]; |
|
90 |
||
91 |
jint *imgData = (jint *)[imageRep bitmapData]; |
|
92 |
if (imgData == NULL) return 0L; |
|
93 |
||
94 |
jint *src = (*env)->GetPrimitiveArrayCritical(env, buffer, NULL); |
|
95 |
if (src == NULL) return 0L; |
|
96 |
||
97 |
CImage_CopyArrayIntoNSImageRep(src, imgData, width, height); |
|
98 |
||
99 |
(*env)->ReleasePrimitiveArrayCritical(env, buffer, src, JNI_ABORT); |
|
100 |
||
12403
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
101 |
return imageRep; |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
102 |
} |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
103 |
|
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
104 |
/* |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
105 |
* Class: sun_lwawt_macosx_CImage |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
106 |
* Method: nativeCreateNSImageFromArray |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
107 |
* Signature: ([III)J |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
108 |
*/ |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
109 |
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromArray |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
110 |
(JNIEnv *env, jclass klass, jintArray buffer, jint width, jint height) |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
111 |
{ |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
112 |
jlong result = 0L; |
12047 | 113 |
|
12403
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
114 |
JNF_COCOA_ENTER(env); |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
115 |
|
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
116 |
NSBitmapImageRep* imageRep = CImage_CreateImageRep(env, buffer, width, height); |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
117 |
if (imageRep) { |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
118 |
NSImage *nsImage = [[NSImage alloc] initWithSize:NSMakeSize(width, height)]; |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
119 |
[nsImage addRepresentation:imageRep]; |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
120 |
[imageRep release]; |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
121 |
|
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
122 |
if (nsImage != nil) { |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
123 |
CFRetain(nsImage); // GC |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
124 |
} |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
125 |
|
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
126 |
result = ptr_to_jlong(nsImage); |
12047 | 127 |
} |
128 |
||
12403
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
129 |
JNF_COCOA_EXIT(env); |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
130 |
|
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
131 |
return result; |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
132 |
} |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
133 |
|
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
134 |
/* |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
135 |
* Class: sun_lwawt_macosx_CImage |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
136 |
* Method: nativeCreateNSImageFromArrays |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
137 |
* Signature: ([[I[I[I)J |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
138 |
*/ |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
139 |
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromArrays |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
140 |
(JNIEnv *env, jclass klass, jobjectArray buffers, jintArray widths, jintArray heights) |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
141 |
{ |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
142 |
jlong result = 0L; |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
143 |
|
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
144 |
JNF_COCOA_ENTER(env); |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
145 |
|
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
146 |
jsize num = (*env)->GetArrayLength(env, buffers); |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
147 |
NSMutableArray * reps = [NSMutableArray arrayWithCapacity: num]; |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
148 |
|
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
149 |
jint * ws = (*env)->GetIntArrayElements(env, widths, NULL); |
23319
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
150 |
if (ws != NULL) { |
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
151 |
jint * hs = (*env)->GetIntArrayElements(env, heights, NULL); |
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
152 |
if (hs != NULL) { |
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
153 |
jsize i; |
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
154 |
for (i = 0; i < num; i++) { |
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
155 |
jintArray buffer = (*env)->GetObjectArrayElement(env, buffers, i); |
12403
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
156 |
|
23319
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
157 |
NSBitmapImageRep* imageRep = CImage_CreateImageRep(env, buffer, ws[i], hs[i]); |
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
158 |
if (imageRep) { |
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
159 |
[reps addObject: imageRep]; |
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
160 |
} |
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
161 |
} |
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
162 |
|
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
163 |
(*env)->ReleaseIntArrayElements(env, heights, hs, JNI_ABORT); |
12403
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
164 |
} |
23319
b6523289a3b7
8034105: [parfait] JNI exception pending in jdk/src/macosx/native/sun/awt/CImage.m
serb
parents:
23257
diff
changeset
|
165 |
(*env)->ReleaseIntArrayElements(env, widths, ws, JNI_ABORT); |
12403
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
166 |
} |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
167 |
if ([reps count]) { |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
168 |
NSImage *nsImage = [[NSImage alloc] initWithSize:NSMakeSize(0, 0)]; |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
169 |
[nsImage addRepresentations: reps]; |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
170 |
|
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
171 |
if (nsImage != nil) { |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
172 |
CFRetain(nsImage); // GC |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
173 |
} |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
174 |
|
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
175 |
result = ptr_to_jlong(nsImage); |
50b3f2982b59
7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents:
12047
diff
changeset
|
176 |
} |
12047 | 177 |
|
178 |
JNF_COCOA_EXIT(env); |
|
179 |
||
180 |
return result; |
|
181 |
} |
|
182 |
||
183 |
/* |
|
184 |
* Class: sun_lwawt_macosx_CImage |
|
185 |
* Method: nativeCreateNSImageFromIconSelector |
|
186 |
* Signature: (I)J |
|
187 |
*/ |
|
188 |
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromIconSelector |
|
189 |
(JNIEnv *env, jclass klass, jint selector) |
|
190 |
{ |
|
191 |
NSImage *image = nil; |
|
192 |
||
193 |
JNF_COCOA_ENTER(env); |
|
194 |
||
195 |
IconRef iconRef; |
|
196 |
if (noErr == GetIconRef(kOnSystemDisk, kSystemIconsCreator, selector, &iconRef)) { |
|
197 |
image = [[NSImage alloc] initWithIconRef:iconRef]; |
|
198 |
if (image) CFRetain(image); // GC |
|
199 |
ReleaseIconRef(iconRef); |
|
200 |
} |
|
201 |
||
202 |
JNF_COCOA_EXIT(env); |
|
203 |
||
204 |
return ptr_to_jlong(image); |
|
205 |
} |
|
206 |
||
207 |
/* |
|
208 |
* Class: sun_lwawt_macosx_CImage |
|
209 |
* Method: nativeCreateNSImageFromFileContents |
|
210 |
* Signature: (Ljava/lang/String;)J |
|
211 |
*/ |
|
212 |
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromFileContents |
|
213 |
(JNIEnv *env, jclass klass, jstring file) |
|
214 |
{ |
|
215 |
NSImage *image = nil; |
|
216 |
||
217 |
JNF_COCOA_ENTER(env); |
|
218 |
||
219 |
NSString *path = JNFNormalizedNSStringForPath(env, file); |
|
220 |
image = [[NSImage alloc] initByReferencingFile:path]; |
|
221 |
if (image) CFRetain(image); // GC |
|
222 |
||
223 |
JNF_COCOA_EXIT(env); |
|
224 |
||
225 |
return ptr_to_jlong(image); |
|
226 |
} |
|
227 |
||
228 |
/* |
|
229 |
* Class: sun_lwawt_macosx_CImage |
|
230 |
* Method: nativeCreateNSImageOfFileFromLaunchServices |
|
231 |
* Signature: (Ljava/lang/String;)J |
|
232 |
*/ |
|
233 |
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageOfFileFromLaunchServices |
|
234 |
(JNIEnv *env, jclass klass, jstring file) |
|
235 |
{ |
|
236 |
__block NSImage *image = nil; |
|
237 |
||
238 |
JNF_COCOA_ENTER(env); |
|
239 |
||
240 |
NSString *path = JNFNormalizedNSStringForPath(env, file); |
|
15322
3638f33225ec
7179050: [macosx] Make LWAWT be able to run on AppKit thread
serb
parents:
12654
diff
changeset
|
241 |
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){ |
12047 | 242 |
image = [[NSWorkspace sharedWorkspace] iconForFile:path]; |
243 |
[image setScalesWhenResized:TRUE]; |
|
244 |
if (image) CFRetain(image); // GC |
|
245 |
}]; |
|
246 |
||
247 |
JNF_COCOA_EXIT(env); |
|
248 |
||
249 |
return ptr_to_jlong(image); |
|
250 |
} |
|
251 |
||
252 |
/* |
|
253 |
* Class: sun_lwawt_macosx_CImage |
|
254 |
* Method: nativeCreateNSImageFromImageName |
|
255 |
* Signature: (Ljava/lang/String;)J |
|
256 |
*/ |
|
257 |
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromImageName |
|
258 |
(JNIEnv *env, jclass klass, jstring name) |
|
259 |
{ |
|
260 |
NSImage *image = nil; |
|
261 |
||
262 |
JNF_COCOA_ENTER(env); |
|
263 |
||
264 |
image = [NSImage imageNamed:JNFJavaToNSString(env, name)]; |
|
265 |
if (image) CFRetain(image); // GC |
|
266 |
||
267 |
JNF_COCOA_EXIT(env); |
|
268 |
||
269 |
return ptr_to_jlong(image); |
|
270 |
} |
|
271 |
||
272 |
/* |
|
273 |
* Class: sun_lwawt_macosx_CImage |
|
274 |
* Method: nativeCopyNSImageIntoArray |
|
23615
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
275 |
* Signature: (J[IIIII)V |
12047 | 276 |
*/ |
277 |
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeCopyNSImageIntoArray |
|
23615
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
278 |
(JNIEnv *env, jclass klass, jlong nsImgPtr, jintArray buffer, jint sw, jint sh, |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
279 |
jint dw, jint dh) |
12047 | 280 |
{ |
281 |
JNF_COCOA_ENTER(env); |
|
282 |
||
283 |
NSImage *img = (NSImage *)jlong_to_ptr(nsImgPtr); |
|
284 |
jint *dst = (*env)->GetPrimitiveArrayCritical(env, buffer, NULL); |
|
285 |
if (dst) { |
|
23615
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
286 |
NSRect fromRect = NSMakeRect(0, 0, sw, sh); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
287 |
NSRect toRect = NSMakeRect(0, 0, dw, dh); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
288 |
CImage_CopyNSImageIntoArray(img, dst, fromRect, toRect); |
12047 | 289 |
(*env)->ReleasePrimitiveArrayCritical(env, buffer, dst, JNI_ABORT); |
290 |
} |
|
291 |
||
292 |
JNF_COCOA_EXIT(env); |
|
293 |
} |
|
294 |
||
295 |
/* |
|
296 |
* Class: sun_lwawt_macosx_CImage |
|
297 |
* Method: nativeGetNSImageSize |
|
298 |
* Signature: (J)Ljava/awt/geom/Dimension2D; |
|
299 |
*/ |
|
300 |
JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CImage_nativeGetNSImageSize |
|
301 |
(JNIEnv *env, jclass klass, jlong nsImgPtr) |
|
302 |
{ |
|
303 |
jobject size = NULL; |
|
304 |
||
305 |
JNF_COCOA_ENTER(env); |
|
306 |
||
307 |
size = NSToJavaSize(env, [(NSImage *)jlong_to_ptr(nsImgPtr) size]); |
|
308 |
||
309 |
JNF_COCOA_EXIT(env); |
|
310 |
||
311 |
return size; |
|
312 |
} |
|
313 |
||
314 |
/* |
|
315 |
* Class: sun_lwawt_macosx_CImage |
|
316 |
* Method: nativeSetNSImageSize |
|
317 |
* Signature: (JDD)V |
|
318 |
*/ |
|
319 |
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeSetNSImageSize |
|
320 |
(JNIEnv *env, jclass clazz, jlong image, jdouble w, jdouble h) |
|
321 |
{ |
|
322 |
if (!image) return; |
|
323 |
NSImage *i = (NSImage *)jlong_to_ptr(image); |
|
324 |
||
325 |
JNF_COCOA_ENTER(env); |
|
326 |
||
327 |
[i setScalesWhenResized:TRUE]; |
|
328 |
[i setSize:NSMakeSize(w, h)]; |
|
329 |
||
330 |
JNF_COCOA_EXIT(env); |
|
331 |
} |
|
23257 | 332 |
|
333 |
/* |
|
334 |
* Class: sun_lwawt_macosx_CImage |
|
335 |
* Method: nativeResizeNSImageRepresentations |
|
336 |
* Signature: (JDD)V |
|
337 |
*/ |
|
338 |
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeResizeNSImageRepresentations |
|
339 |
(JNIEnv *env, jclass clazz, jlong image, jdouble w, jdouble h) |
|
340 |
{ |
|
341 |
if (!image) return; |
|
342 |
NSImage *i = (NSImage *)jlong_to_ptr(image); |
|
343 |
||
344 |
JNF_COCOA_ENTER(env); |
|
345 |
||
346 |
NSImageRep *imageRep = nil; |
|
347 |
NSArray *imageRepresentations = [i representations]; |
|
348 |
NSEnumerator *imageEnumerator = [imageRepresentations objectEnumerator]; |
|
349 |
while ((imageRep = [imageEnumerator nextObject]) != nil) { |
|
350 |
[imageRep setSize:NSMakeSize(w, h)]; |
|
351 |
} |
|
352 |
||
353 |
JNF_COCOA_EXIT(env); |
|
354 |
} |
|
23615
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
355 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
356 |
NSComparisonResult getOrder(BOOL order){ |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
357 |
return (NSComparisonResult) (order ? NSOrderedAscending : NSOrderedDescending); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
358 |
} |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
359 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
360 |
/* |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
361 |
* Class: sun_lwawt_macosx_CImage |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
362 |
* Method: nativeGetNSImageRepresentationsCount |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
363 |
* Signature: (JDD)[Ljava/awt/geom/Dimension2D; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
364 |
*/ |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
365 |
JNIEXPORT jobjectArray JNICALL |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
366 |
Java_sun_lwawt_macosx_CImage_nativeGetNSImageRepresentationSizes |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
367 |
(JNIEnv *env, jclass clazz, jlong image, jdouble w, jdouble h) |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
368 |
{ |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
369 |
if (!image) return NULL; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
370 |
jobjectArray jreturnArray = NULL; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
371 |
NSImage *img = (NSImage *)jlong_to_ptr(image); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
372 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
373 |
JNF_COCOA_ENTER(env); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
374 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
375 |
NSArray *imageRepresentations = [img representations]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
376 |
if([imageRepresentations count] == 0){ |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
377 |
return NULL; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
378 |
} |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
379 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
380 |
NSArray *sortedImageRepresentations = [imageRepresentations |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
381 |
sortedArrayUsingComparator: ^(id obj1, id obj2) { |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
382 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
383 |
NSImageRep *imageRep1 = (NSImageRep *) obj1; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
384 |
NSImageRep *imageRep2 = (NSImageRep *) obj2; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
385 |
NSSize size1 = [imageRep1 size]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
386 |
NSSize size2 = [imageRep2 size]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
387 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
388 |
if (NSEqualSizes(size1, size2)) { |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
389 |
return getOrder([imageRep1 pixelsWide] <= [imageRep2 pixelsWide] && |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
390 |
[imageRep1 pixelsHigh] <= [imageRep2 pixelsHigh]); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
391 |
} |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
392 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
393 |
return getOrder(size1.width <= size2.width && size1.height <= size2.height); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
394 |
}]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
395 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
396 |
NSMutableArray *sortedPixelSizes = [[NSMutableArray alloc] init]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
397 |
NSSize lastSize = [[sortedImageRepresentations lastObject] size]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
398 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
399 |
NSUInteger i = [sortedImageRepresentations indexOfObjectPassingTest: |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
400 |
^BOOL(id obj, NSUInteger idx, BOOL *stop) { |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
401 |
NSSize imageRepSize = [obj size]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
402 |
return (w <= imageRepSize.width && h <= imageRepSize.height) |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
403 |
|| NSEqualSizes(imageRepSize, lastSize); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
404 |
}]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
405 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
406 |
NSUInteger count = [sortedImageRepresentations count]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
407 |
i = (i == NSNotFound) ? count - 1 : i; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
408 |
NSSize bestFitSize = [[sortedImageRepresentations objectAtIndex: i] size]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
409 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
410 |
for(; i < count; i++){ |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
411 |
NSImageRep *imageRep = [sortedImageRepresentations objectAtIndex: i]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
412 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
413 |
if (!NSEqualSizes([imageRep size], bestFitSize)) { |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
414 |
break; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
415 |
} |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
416 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
417 |
NSSize pixelSize = NSMakeSize( |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
418 |
[imageRep pixelsWide], [imageRep pixelsHigh]); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
419 |
[sortedPixelSizes addObject: [NSValue valueWithSize: pixelSize]]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
420 |
} |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
421 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
422 |
count = [sortedPixelSizes count]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
423 |
static JNF_CLASS_CACHE(jc_Dimension, "java/awt/Dimension"); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
424 |
jreturnArray = JNFNewObjectArray(env, &jc_Dimension, count); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
425 |
CHECK_NULL_RETURN(jreturnArray, NULL); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
426 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
427 |
for(i = 0; i < count; i++){ |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
428 |
NSSize pixelSize = [[sortedPixelSizes objectAtIndex: i] sizeValue]; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
429 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
430 |
(*env)->SetObjectArrayElement(env, jreturnArray, i, |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
431 |
NSToJavaSize(env, pixelSize)); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
432 |
JNU_CHECK_EXCEPTION_RETURN(env, NULL); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
433 |
} |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
434 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
435 |
JNF_COCOA_EXIT(env); |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
436 |
|
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
437 |
return jreturnArray; |
a0825e77bfad
8033534: [macosx] Get MultiResolution image from native system
alexsch
parents:
23328
diff
changeset
|
438 |
} |