jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/CImage.java
author alexsch
Wed, 24 Aug 2016 00:23:49 +0400
changeset 40719 4ae72a69bd3b
parent 32682 6f1200d8999d
child 44752 97a2817b5a9b
permissions -rw-r--r--
8129854: Remove reflection from AWT/Swing classes Reviewed-by: serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 12808
diff changeset
     2
 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     4
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    10
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    15
 * accompanied this code).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    16
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    20
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    23
 * questions.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    24
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    25
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    26
package sun.lwawt.macosx;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    27
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    28
import java.awt.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    29
import java.awt.geom.Dimension2D;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    30
import java.awt.image.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    31
12403
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
    32
import java.util.Arrays;
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
    33
import java.util.List;
32682
6f1200d8999d 8029339: Custom MultiResolution image support on HiDPI displays
alexsch
parents: 30459
diff changeset
    34
import java.awt.image.MultiResolutionImage;
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
    35
import sun.awt.image.MultiResolutionCachedImage;
12403
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
    36
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    37
import sun.awt.image.SunWritableRaster;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    38
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    39
public class CImage extends CFRetainedResource {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    40
    private static native long nativeCreateNSImageFromArray(int[] buffer, int w, int h);
23652
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
    41
    private static native long nativeCreateNSImageFromBytes(byte[] buffer);
12403
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
    42
    private static native long nativeCreateNSImageFromArrays(int[][] buffers, int w[], int h[]);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    43
    private static native long nativeCreateNSImageFromFileContents(String file);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    44
    private static native long nativeCreateNSImageOfFileFromLaunchServices(String file);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    45
    private static native long nativeCreateNSImageFromImageName(String name);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    46
    private static native long nativeCreateNSImageFromIconSelector(int selector);
23652
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
    47
    private static native byte[] nativeGetPlatformImageBytes(int[] buffer, int w, int h);
23615
a0825e77bfad 8033534: [macosx] Get MultiResolution image from native system
alexsch
parents: 23328
diff changeset
    48
    private static native void nativeCopyNSImageIntoArray(long image, int[] buffer, int sw, int sh, int dw, int dh);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    49
    private static native Dimension2D nativeGetNSImageSize(long image);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    50
    private static native void nativeSetNSImageSize(long image, double w, double h);
23257
5aff11b9c9aa 8028212: Custom cursor HiDPI support
alexsch
parents: 12808
diff changeset
    51
    private static native void nativeResizeNSImageRepresentations(long image, double w, double h);
23615
a0825e77bfad 8033534: [macosx] Get MultiResolution image from native system
alexsch
parents: 23328
diff changeset
    52
    private static native Dimension2D[] nativeGetNSImageRepresentationSizes(long image, double w, double h);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    53
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    54
    static Creator creator = new Creator();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    55
    static Creator getCreator() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    56
        return creator;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    57
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    58
40719
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    59
    // This is used to create a CImage that represents the icon of the given file.
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    60
    public static Image createImageOfFile(String file, int width, int height) {
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    61
        return getCreator().createImageOfFile(file, width, height);
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    62
    }
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    63
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    64
    public static Image createSystemImageFromSelector(String iconSelector,
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    65
            int width, int height) {
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    66
        return getCreator().createSystemImageFromSelector(iconSelector, width, height);
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    67
    }
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    68
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    69
    public static Image createImageFromFile(String file, double width, double height) {
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    70
        return getCreator().createImageFromFile(file, width, height);
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    71
    }
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    72
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    73
    // This is used to create a CImage from a Image
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    74
    public static CImage createFromImage(final Image image) {
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    75
        return getCreator().createFromImage(image);
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    76
    }
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32682
diff changeset
    77
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    78
    public static class Creator {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    79
        Creator() { }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    80
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    81
        // This is used to create a CImage with an NSImage pointer. It MUST be a CFRetained
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    82
        // NSImage, and the CImage takes ownership of the non-GC retain. If callers need the
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    83
        // NSImage themselves, they MUST call retain on the NSImage themselves.
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
    84
        public Image createImageUsingNativeSize(final long image) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    85
            if (image == 0) return null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    86
            final Dimension2D size = nativeGetNSImageSize(image);
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
    87
            return createImage(image, size.getWidth(), size.getHeight());
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    88
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    89
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    90
        // the width and height passed in as a parameter could differ than the width and the height of the NSImage (image), in that case, the image will be scaled
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
    91
        Image createImage(long image, double width, double height) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    92
            if (image == 0) throw new Error("Unable to instantiate CImage with null native image reference.");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    93
            return createImageWithSize(image, width, height);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    94
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    95
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
    96
        public Image createImageWithSize(final long image, final double width, final double height) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    97
            final CImage img = new CImage(image);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    98
            img.resize(width, height);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    99
            return img.toImage();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   100
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   101
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   102
        // This is used to create a CImage that represents the icon of the given file.
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   103
        public Image createImageOfFile(final String file, final int width, final int height) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   104
            return createImage(nativeCreateNSImageOfFileFromLaunchServices(file), width, height);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   105
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   106
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   107
        public Image createImageFromFile(final String file, final double width, final double height) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   108
            final long image = nativeCreateNSImageFromFileContents(file);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   109
            nativeSetNSImageSize(image, width, height);
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   110
            return createImage(image, width, height);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   111
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   112
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   113
        public Image createSystemImageFromSelector(final String iconSelector, final int width, final int height) {
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   114
            return createImage(nativeCreateNSImageFromIconSelector(getSelectorAsInt(iconSelector)), width, height);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   115
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   116
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   117
        public Image createImageFromName(final String name, final int width, final int height) {
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   118
            return createImage(nativeCreateNSImageFromImageName(name), width, height);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   119
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   120
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   121
        public Image createImageFromName(final String name) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   122
            return createImageUsingNativeSize(nativeCreateNSImageFromImageName(name));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   123
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   124
12808
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   125
        private static int[] imageToArray(Image image, boolean prepareImage) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   126
            if (image == null) return null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   127
12808
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   128
            if (prepareImage && !(image instanceof BufferedImage)) {
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   129
                final MediaTracker mt = new MediaTracker(new Label());
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   130
                final int id = 0;
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   131
                mt.addImage(image, id);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   132
12808
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   133
                try {
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   134
                    mt.waitForID(id);
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   135
                } catch (InterruptedException e) {
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   136
                    return null;
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   137
                }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   138
12808
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   139
                if (mt.isErrorID(id)) {
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   140
                    return null;
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   141
                }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   142
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   143
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   144
            int w = image.getWidth(null);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   145
            int h = image.getHeight(null);
12808
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   146
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   147
            if (w < 0 || h < 0) {
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   148
                return null;
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   149
            }
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   150
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   151
            BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   152
            Graphics2D g2 = bimg.createGraphics();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   153
            g2.setComposite(AlphaComposite.Src);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   154
            g2.drawImage(image, 0, 0, null);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   155
            g2.dispose();
12808
20329ff2140c 7146550: [macosx] DnD test failure in createCompatibleWritableRaster()
bae
parents: 12403
diff changeset
   156
12403
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   157
            return ((DataBufferInt)bimg.getRaster().getDataBuffer()).getData();
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   158
        }
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   159
23652
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   160
        public byte[] getPlatformImageBytes(final Image image) {
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   161
            int[] buffer = imageToArray(image, false);
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   162
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   163
            if (buffer == null) {
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   164
                return null;
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   165
            }
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   166
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   167
            return nativeGetPlatformImageBytes(buffer, image.getWidth(null), image.getHeight(null));
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   168
        }
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   169
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   170
        /**
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   171
         * Translates a byte array which contains platform-specific image data in the given format into an Image.
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   172
         */
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   173
        public Image createImageFromPlatformImageBytes(final byte[] buffer) {
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   174
            return createImageUsingNativeSize(nativeCreateNSImageFromBytes(buffer));
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   175
        }
11515e3c3f85 8037371: [macosx] Test closed/java/awt/dnd/ImageTransferTest/ImageTransferTest.html fails
pchelko
parents: 23615
diff changeset
   176
12403
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   177
        // This is used to create a CImage from a Image
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   178
        public CImage createFromImage(final Image image) {
30459
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   179
            return createFromImage(image, true);
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   180
        }
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   181
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   182
        public CImage createFromImageImmediately(final Image image) {
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   183
            return createFromImage(image, false);
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   184
        }
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   185
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   186
        // This is used to create a CImage from a Image
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   187
        private CImage createFromImage(final Image image, final boolean prepareImage) {
23257
5aff11b9c9aa 8028212: Custom cursor HiDPI support
alexsch
parents: 12808
diff changeset
   188
            if (image instanceof MultiResolutionImage) {
5aff11b9c9aa 8028212: Custom cursor HiDPI support
alexsch
parents: 12808
diff changeset
   189
                List<Image> resolutionVariants
5aff11b9c9aa 8028212: Custom cursor HiDPI support
alexsch
parents: 12808
diff changeset
   190
                        = ((MultiResolutionImage) image).getResolutionVariants();
30459
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   191
                return createFromImages(resolutionVariants, prepareImage);
23257
5aff11b9c9aa 8028212: Custom cursor HiDPI support
alexsch
parents: 12808
diff changeset
   192
            }
5aff11b9c9aa 8028212: Custom cursor HiDPI support
alexsch
parents: 12808
diff changeset
   193
30459
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   194
            int[] buffer = imageToArray(image, prepareImage);
12403
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   195
            if (buffer == null) {
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   196
                return null;
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   197
            }
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   198
            return new CImage(nativeCreateNSImageFromArray(buffer, image.getWidth(null), image.getHeight(null)));
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   199
        }
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   200
30459
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   201
        public CImage createFromImages(final List<Image> images) {
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   202
            return createFromImages(images, true);
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   203
        }
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   204
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   205
        private CImage createFromImages(final List<Image> images, final boolean prepareImage) {
12403
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   206
            if (images == null || images.isEmpty()) {
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   207
                return null;
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   208
            }
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   209
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   210
            int num = images.size();
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   211
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   212
            int[][] buffers = new int[num][];
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   213
            int[] w = new int[num];
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   214
            int[] h = new int[num];
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   215
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   216
            num = 0;
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   217
30459
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   218
            for (final Image img : images) {
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   219
                buffers[num] = imageToArray(img, prepareImage);
12403
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   220
                if (buffers[num] == null) {
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   221
                    // Unable to process the image
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   222
                    continue;
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   223
                }
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   224
                w[num] = img.getWidth(null);
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   225
                h[num] = img.getHeight(null);
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   226
                num++;
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   227
            }
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   228
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   229
            if (num == 0) {
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   230
                return null;
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   231
            }
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   232
50b3f2982b59 7148275: [macosx] setIconImages() not working correctly (distorted icon when minimized)
anthony
parents: 12047
diff changeset
   233
            return new CImage(nativeCreateNSImageFromArrays(
30459
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   234
                    Arrays.copyOf(buffers, num),
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   235
                    Arrays.copyOf(w, num),
8787f2313433 8076106: [macosx] Drag image of TransferHandler does not honor MultiResolutionImage
alexsch
parents: 25859
diff changeset
   236
                    Arrays.copyOf(h, num)));
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   237
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   238
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   239
        static int getSelectorAsInt(final String fromString) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   240
            final byte[] b = fromString.getBytes();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   241
            final int len = Math.min(b.length, 4);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   242
            int result = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   243
            for (int i = 0; i < len; i++) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   244
                if (i > 0) result <<= 8;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   245
                result |= (b[i] & 0xff);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   246
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   247
            return result;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   248
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   249
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   250
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   251
    CImage(long nsImagePtr) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   252
        super(nsImagePtr, true);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   253
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   254
23615
a0825e77bfad 8033534: [macosx] Get MultiResolution image from native system
alexsch
parents: 23328
diff changeset
   255
    /** @return A MultiResolution image created from nsImagePtr, or null. */
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   256
    private Image toImage() {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   257
        if (ptr == 0) return null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   258
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   259
        final Dimension2D size = nativeGetNSImageSize(ptr);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   260
        final int w = (int)size.getWidth();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   261
        final int h = (int)size.getHeight();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   262
23615
a0825e77bfad 8033534: [macosx] Get MultiResolution image from native system
alexsch
parents: 23328
diff changeset
   263
        Dimension2D[] sizes
a0825e77bfad 8033534: [macosx] Get MultiResolution image from native system
alexsch
parents: 23328
diff changeset
   264
                = nativeGetNSImageRepresentationSizes(ptr,
a0825e77bfad 8033534: [macosx] Get MultiResolution image from native system
alexsch
parents: 23328
diff changeset
   265
                        size.getWidth(), size.getHeight());
a0825e77bfad 8033534: [macosx] Get MultiResolution image from native system
alexsch
parents: 23328
diff changeset
   266
24532
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   267
        return sizes == null || sizes.length < 2 ?
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   268
                new MultiResolutionCachedImage(w, h, (width, height)
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   269
                        -> toImage(w, h, width, height))
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   270
                : new MultiResolutionCachedImage(w, h, sizes, (width, height)
24831bd48764 8040279: [macosx] Do not use the base image in the MultiResolutionBufferedImage
alexsch
parents: 23666
diff changeset
   271
                        -> toImage(w, h, width, height));
23615
a0825e77bfad 8033534: [macosx] Get MultiResolution image from native system
alexsch
parents: 23328
diff changeset
   272
    }
a0825e77bfad 8033534: [macosx] Get MultiResolution image from native system
alexsch
parents: 23328
diff changeset
   273
a0825e77bfad 8033534: [macosx] Get MultiResolution image from native system
alexsch
parents: 23328
diff changeset
   274
    private BufferedImage toImage(int srcWidth, int srcHeight, int dstWidth, int dstHeight) {
a0825e77bfad 8033534: [macosx] Get MultiResolution image from native system
alexsch
parents: 23328
diff changeset
   275
        final BufferedImage bimg = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB_PRE);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   276
        final DataBufferInt dbi = (DataBufferInt)bimg.getRaster().getDataBuffer();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   277
        final int[] buffer = SunWritableRaster.stealData(dbi, 0);
23615
a0825e77bfad 8033534: [macosx] Get MultiResolution image from native system
alexsch
parents: 23328
diff changeset
   278
        nativeCopyNSImageIntoArray(ptr, buffer, srcWidth, srcHeight, dstWidth, dstHeight);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   279
        SunWritableRaster.markDirty(dbi);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   280
        return bimg;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   281
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   282
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   283
    /** If nsImagePtr != 0 then scale this NSImage. @return *this* */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   284
    CImage resize(final double w, final double h) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   285
        if (ptr != 0) nativeSetNSImageSize(ptr, w, h);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   286
        return this;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   287
    }
23257
5aff11b9c9aa 8028212: Custom cursor HiDPI support
alexsch
parents: 12808
diff changeset
   288
5aff11b9c9aa 8028212: Custom cursor HiDPI support
alexsch
parents: 12808
diff changeset
   289
    void resizeRepresentations(double w, double h) {
5aff11b9c9aa 8028212: Custom cursor HiDPI support
alexsch
parents: 12808
diff changeset
   290
        if (ptr != 0) nativeResizeNSImageRepresentations(ptr, w, h);
5aff11b9c9aa 8028212: Custom cursor HiDPI support
alexsch
parents: 12808
diff changeset
   291
    }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   292
}