Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/sun/awt/CImage.m
38829 views
1
/*
2
* Copyright (c) 2011, 2016, 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
#import "jni_util.h"
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
56
(NSImage *srcImage, jint *dstPixels, NSRect fromRect, NSRect toRect)
57
{
58
int width = toRect.size.width;
59
int height = toRect.size.height;
60
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
61
CGContextRef cgRef = CGBitmapContextCreate(dstPixels, width, height,
62
8, width * 4, colorspace,
63
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
64
CGColorSpaceRelease(colorspace);
65
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:cgRef flipped:NO];
66
CGContextRelease(cgRef);
67
NSGraphicsContext *oldContext = [[NSGraphicsContext currentContext] retain];
68
[NSGraphicsContext setCurrentContext:context];
69
[srcImage drawInRect:toRect
70
fromRect:fromRect
71
operation:NSCompositeSourceOver
72
fraction:1.0];
73
[NSGraphicsContext setCurrentContext:oldContext];
74
[oldContext release];
75
}
76
77
static NSBitmapImageRep* CImage_CreateImageRep(JNIEnv *env, jintArray buffer, jint width, jint height)
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
101
return imageRep;
102
}
103
104
/*
105
* Class: sun_lwawt_macosx_CImage
106
* Method: nativeCreateNSImageFromArray
107
* Signature: ([III)J
108
*/
109
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromArray
110
(JNIEnv *env, jclass klass, jintArray buffer, jint width, jint height)
111
{
112
jlong result = 0L;
113
114
JNF_COCOA_ENTER(env);
115
116
NSBitmapImageRep* imageRep = CImage_CreateImageRep(env, buffer, width, height);
117
if (imageRep) {
118
NSImage *nsImage = [[[NSImage alloc] initWithSize:NSMakeSize(width, height)] retain];
119
[nsImage addRepresentation:imageRep];
120
[imageRep release];
121
result = ptr_to_jlong(nsImage);
122
}
123
124
JNF_COCOA_EXIT(env);
125
126
return result;
127
}
128
129
/*
130
* Class: sun_lwawt_macosx_CImage
131
* Method: nativeCreateNSImageFromArrays
132
* Signature: ([[I[I[I)J
133
*/
134
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromArrays
135
(JNIEnv *env, jclass klass, jobjectArray buffers, jintArray widths, jintArray heights)
136
{
137
jlong result = 0L;
138
139
JNF_COCOA_ENTER(env);
140
141
jsize num = (*env)->GetArrayLength(env, buffers);
142
NSMutableArray * reps = [NSMutableArray arrayWithCapacity: num];
143
144
jint * ws = (*env)->GetIntArrayElements(env, widths, NULL);
145
if (ws != NULL) {
146
jint * hs = (*env)->GetIntArrayElements(env, heights, NULL);
147
if (hs != NULL) {
148
jsize i;
149
for (i = 0; i < num; i++) {
150
jintArray buffer = (*env)->GetObjectArrayElement(env, buffers, i);
151
152
NSBitmapImageRep* imageRep = CImage_CreateImageRep(env, buffer, ws[i], hs[i]);
153
if (imageRep) {
154
[reps addObject: imageRep];
155
}
156
}
157
158
(*env)->ReleaseIntArrayElements(env, heights, hs, JNI_ABORT);
159
}
160
(*env)->ReleaseIntArrayElements(env, widths, ws, JNI_ABORT);
161
}
162
if ([reps count]) {
163
NSImage *nsImage = [[[NSImage alloc] initWithSize:NSMakeSize(0, 0)] retain];
164
[nsImage addRepresentations: reps];
165
result = ptr_to_jlong(nsImage);
166
}
167
168
JNF_COCOA_EXIT(env);
169
170
return result;
171
}
172
173
/*
174
* Class: sun_lwawt_macosx_CImage
175
* Method: nativeCreateNSImageFromIconSelector
176
* Signature: (I)J
177
*/
178
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromIconSelector
179
(JNIEnv *env, jclass klass, jint selector)
180
{
181
NSImage *image = nil;
182
183
JNF_COCOA_ENTER(env);
184
185
IconRef iconRef;
186
if (noErr == GetIconRef(kOnSystemDisk, kSystemIconsCreator, selector, &iconRef)) {
187
image = [[[NSImage alloc] initWithIconRef:iconRef] retain];
188
ReleaseIconRef(iconRef);
189
}
190
191
JNF_COCOA_EXIT(env);
192
193
return ptr_to_jlong(image);
194
}
195
196
/*
197
* Class: sun_lwawt_macosx_CImage
198
* Method: nativeCreateNSImageFromFileContents
199
* Signature: (Ljava/lang/String;)J
200
*/
201
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromFileContents
202
(JNIEnv *env, jclass klass, jstring file)
203
{
204
NSImage *image = nil;
205
206
JNF_COCOA_ENTER(env);
207
208
NSString *path = JNFNormalizedNSStringForPath(env, file);
209
image = [[[NSImage alloc] initByReferencingFile:path] retain];
210
211
JNF_COCOA_EXIT(env);
212
213
return ptr_to_jlong(image);
214
}
215
216
/*
217
* Class: sun_lwawt_macosx_CImage
218
* Method: nativeCreateNSImageOfFileFromLaunchServices
219
* Signature: (Ljava/lang/String;)J
220
*/
221
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageOfFileFromLaunchServices
222
(JNIEnv *env, jclass klass, jstring file)
223
{
224
__block NSImage *image = nil;
225
226
JNF_COCOA_ENTER(env);
227
228
NSString *path = JNFNormalizedNSStringForPath(env, file);
229
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
230
image = [[[NSWorkspace sharedWorkspace] iconForFile:path] retain];
231
[image setScalesWhenResized:TRUE];
232
}];
233
234
JNF_COCOA_EXIT(env);
235
236
return ptr_to_jlong(image);
237
}
238
239
/*
240
* Class: sun_lwawt_macosx_CImage
241
* Method: nativeCreateNSImageFromImageName
242
* Signature: (Ljava/lang/String;)J
243
*/
244
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromImageName
245
(JNIEnv *env, jclass klass, jstring name)
246
{
247
NSImage *image = nil;
248
249
JNF_COCOA_ENTER(env);
250
251
image = [[NSImage imageNamed:JNFJavaToNSString(env, name)] retain];
252
253
JNF_COCOA_EXIT(env);
254
255
return ptr_to_jlong(image);
256
}
257
258
/*
259
* Class: sun_lwawt_macosx_CImage
260
* Method: nativeCopyNSImageIntoArray
261
* Signature: (J[IIIII)V
262
*/
263
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeCopyNSImageIntoArray
264
(JNIEnv *env, jclass klass, jlong nsImgPtr, jintArray buffer, jint sw, jint sh,
265
jint dw, jint dh)
266
{
267
JNF_COCOA_ENTER(env);
268
269
NSImage *img = (NSImage *)jlong_to_ptr(nsImgPtr);
270
jint *dst = (*env)->GetPrimitiveArrayCritical(env, buffer, NULL);
271
if (dst) {
272
NSRect fromRect = NSMakeRect(0, 0, sw, sh);
273
NSRect toRect = NSMakeRect(0, 0, dw, dh);
274
CImage_CopyNSImageIntoArray(img, dst, fromRect, toRect);
275
(*env)->ReleasePrimitiveArrayCritical(env, buffer, dst, JNI_ABORT);
276
}
277
278
JNF_COCOA_EXIT(env);
279
}
280
281
/*
282
* Class: sun_lwawt_macosx_CImage
283
* Method: nativeGetNSImageSize
284
* Signature: (J)Ljava/awt/geom/Dimension2D;
285
*/
286
JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CImage_nativeGetNSImageSize
287
(JNIEnv *env, jclass klass, jlong nsImgPtr)
288
{
289
jobject size = NULL;
290
291
JNF_COCOA_ENTER(env);
292
293
size = NSToJavaSize(env, [(NSImage *)jlong_to_ptr(nsImgPtr) size]);
294
295
JNF_COCOA_EXIT(env);
296
297
return size;
298
}
299
300
/*
301
* Class: sun_lwawt_macosx_CImage
302
* Method: nativeSetNSImageSize
303
* Signature: (JDD)V
304
*/
305
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeSetNSImageSize
306
(JNIEnv *env, jclass clazz, jlong image, jdouble w, jdouble h)
307
{
308
if (!image) return;
309
NSImage *i = (NSImage *)jlong_to_ptr(image);
310
311
JNF_COCOA_ENTER(env);
312
313
[i setScalesWhenResized:TRUE];
314
[i setSize:NSMakeSize(w, h)];
315
316
JNF_COCOA_EXIT(env);
317
}
318
319
/*
320
* Class: sun_lwawt_macosx_CImage
321
* Method: nativeResizeNSImageRepresentations
322
* Signature: (JDD)V
323
*/
324
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CImage_nativeResizeNSImageRepresentations
325
(JNIEnv *env, jclass clazz, jlong image, jdouble w, jdouble h)
326
{
327
if (!image) return;
328
NSImage *i = (NSImage *)jlong_to_ptr(image);
329
330
JNF_COCOA_ENTER(env);
331
332
NSImageRep *imageRep = nil;
333
NSArray *imageRepresentations = [i representations];
334
NSEnumerator *imageEnumerator = [imageRepresentations objectEnumerator];
335
while ((imageRep = [imageEnumerator nextObject]) != nil) {
336
[imageRep setSize:NSMakeSize(w, h)];
337
}
338
339
JNF_COCOA_EXIT(env);
340
}
341
342
NSComparisonResult getOrder(BOOL order){
343
return (NSComparisonResult) (order ? NSOrderedAscending : NSOrderedDescending);
344
}
345
346
/*
347
* Class: sun_lwawt_macosx_CImage
348
* Method: nativeGetNSImageRepresentationsCount
349
* Signature: (JDD)[Ljava/awt/geom/Dimension2D;
350
*/
351
JNIEXPORT jobjectArray JNICALL
352
Java_sun_lwawt_macosx_CImage_nativeGetNSImageRepresentationSizes
353
(JNIEnv *env, jclass clazz, jlong image, jdouble w, jdouble h)
354
{
355
if (!image) return NULL;
356
jobjectArray jreturnArray = NULL;
357
NSImage *img = (NSImage *)jlong_to_ptr(image);
358
359
JNF_COCOA_ENTER(env);
360
361
NSArray *imageRepresentations = [img representations];
362
if([imageRepresentations count] == 0){
363
return NULL;
364
}
365
366
NSArray *sortedImageRepresentations = [imageRepresentations
367
sortedArrayUsingComparator: ^(id obj1, id obj2) {
368
369
NSImageRep *imageRep1 = (NSImageRep *) obj1;
370
NSImageRep *imageRep2 = (NSImageRep *) obj2;
371
NSSize size1 = [imageRep1 size];
372
NSSize size2 = [imageRep2 size];
373
374
if (NSEqualSizes(size1, size2)) {
375
return getOrder([imageRep1 pixelsWide] <= [imageRep2 pixelsWide] &&
376
[imageRep1 pixelsHigh] <= [imageRep2 pixelsHigh]);
377
}
378
379
return getOrder(size1.width <= size2.width && size1.height <= size2.height);
380
}];
381
382
NSMutableArray *sortedPixelSizes = [[[NSMutableArray alloc] init] autorelease];
383
NSSize lastSize = [[sortedImageRepresentations lastObject] size];
384
385
NSUInteger i = [sortedImageRepresentations indexOfObjectPassingTest:
386
^BOOL(id obj, NSUInteger idx, BOOL *stop) {
387
NSSize imageRepSize = [obj size];
388
return (w <= imageRepSize.width && h <= imageRepSize.height)
389
|| NSEqualSizes(imageRepSize, lastSize);
390
}];
391
392
NSUInteger count = [sortedImageRepresentations count];
393
i = (i == NSNotFound) ? count - 1 : i;
394
NSSize bestFitSize = [[sortedImageRepresentations objectAtIndex: i] size];
395
396
for(; i < count; i++){
397
NSImageRep *imageRep = [sortedImageRepresentations objectAtIndex: i];
398
399
if (!NSEqualSizes([imageRep size], bestFitSize)) {
400
break;
401
}
402
403
NSSize pixelSize = NSMakeSize(
404
[imageRep pixelsWide], [imageRep pixelsHigh]);
405
[sortedPixelSizes addObject: [NSValue valueWithSize: pixelSize]];
406
}
407
408
count = [sortedPixelSizes count];
409
static JNF_CLASS_CACHE(jc_Dimension, "java/awt/Dimension");
410
jreturnArray = JNFNewObjectArray(env, &jc_Dimension, count);
411
CHECK_NULL_RETURN(jreturnArray, NULL);
412
413
for(i = 0; i < count; i++){
414
NSSize pixelSize = [[sortedPixelSizes objectAtIndex: i] sizeValue];
415
416
(*env)->SetObjectArrayElement(env, jreturnArray, i,
417
NSToJavaSize(env, pixelSize));
418
JNU_CHECK_EXCEPTION_RETURN(env, NULL);
419
}
420
421
JNF_COCOA_EXIT(env);
422
423
return jreturnArray;
424
}
425
426
/*
427
* Class: sun_lwawt_macosx_CImage
428
* Method: nativeGetPlatformImageBytes
429
* Signature: ([III)[B
430
*/
431
JNIEXPORT jbyteArray JNICALL Java_sun_lwawt_macosx_CImage_nativeGetPlatformImageBytes
432
(JNIEnv *env, jclass klass, jintArray buffer, jint width, jint height)
433
{
434
jbyteArray result = 0L;
435
436
JNF_COCOA_ENTER(env);
437
438
NSBitmapImageRep* imageRep = [CImage_CreateImageRep(env, buffer, width, height) autorelease];
439
if (imageRep) {
440
NSData *tiffImage = [imageRep TIFFRepresentation];
441
jsize tiffSize = (jsize)[tiffImage length];
442
result = (*env)->NewByteArray(env, tiffSize);
443
CHECK_NULL_RETURN(result, nil);
444
jbyte *tiffData = (jbyte *)(*env)->GetPrimitiveArrayCritical(env, result, 0);
445
CHECK_NULL_RETURN(tiffData, nil);
446
[tiffImage getBytes:tiffData];
447
(*env)->ReleasePrimitiveArrayCritical(env, result, tiffData, 0);
448
}
449
450
JNF_COCOA_EXIT(env);
451
452
return result;
453
}
454
455
/*
456
* Class: sun_lwawt_macosx_CImage
457
* Method: nativeCreateNSImageFromBytes
458
* Signature: ([B)J
459
*/
460
JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CImage_nativeCreateNSImageFromBytes
461
(JNIEnv *env, jclass klass, jbyteArray sourceData)
462
{
463
jlong result = 0L;
464
CHECK_NULL_RETURN(sourceData, 0L);
465
466
JNF_COCOA_ENTER(env);
467
468
jsize sourceSize = (*env)->GetArrayLength(env, sourceData);
469
if (sourceSize == 0) return 0L;
470
471
jbyte *sourceBytes = (*env)->GetPrimitiveArrayCritical(env, sourceData, NULL);
472
CHECK_NULL_RETURN(sourceBytes, 0L);
473
NSData *rawData = [NSData dataWithBytes:sourceBytes length:sourceSize];
474
NSImage *newImage = [[NSImage alloc] initWithData:rawData];
475
476
(*env)->ReleasePrimitiveArrayCritical(env, sourceData, sourceBytes, JNI_ABORT);
477
CHECK_NULL_RETURN(newImage, 0L);
478
479
result = ptr_to_jlong(newImage);
480
JNF_COCOA_EXIT(env);
481
482
return result;
483
}
484
485
486