CImage.java revision 15526:dc69f25a52aa
1/*
2 * Copyright (c) 2011, 2012, 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
26package sun.lwawt.macosx;
27
28import java.awt.*;
29import java.awt.geom.Dimension2D;
30import java.awt.image.*;
31
32import java.util.Arrays;
33import java.util.List;
34import java.awt.image.MultiResolutionImage;
35import sun.awt.image.MultiResolutionCachedImage;
36
37import sun.awt.image.SunWritableRaster;
38
39public class CImage extends CFRetainedResource {
40    private static native long nativeCreateNSImageFromArray(int[] buffer, int w, int h);
41    private static native long nativeCreateNSImageFromBytes(byte[] buffer);
42    private static native long nativeCreateNSImageFromArrays(int[][] buffers, int w[], int h[]);
43    private static native long nativeCreateNSImageFromFileContents(String file);
44    private static native long nativeCreateNSImageOfFileFromLaunchServices(String file);
45    private static native long nativeCreateNSImageFromImageName(String name);
46    private static native long nativeCreateNSImageFromIconSelector(int selector);
47    private static native byte[] nativeGetPlatformImageBytes(int[] buffer, int w, int h);
48    private static native void nativeCopyNSImageIntoArray(long image, int[] buffer, int sw, int sh, int dw, int dh);
49    private static native Dimension2D nativeGetNSImageSize(long image);
50    private static native void nativeSetNSImageSize(long image, double w, double h);
51    private static native void nativeResizeNSImageRepresentations(long image, double w, double h);
52    private static native Dimension2D[] nativeGetNSImageRepresentationSizes(long image, double w, double h);
53
54    static Creator creator = new Creator();
55    static Creator getCreator() {
56        return creator;
57    }
58
59    // This is used to create a CImage that represents the icon of the given file.
60    public static Image createImageOfFile(String file, int width, int height) {
61        return getCreator().createImageOfFile(file, width, height);
62    }
63
64    public static Image createSystemImageFromSelector(String iconSelector,
65            int width, int height) {
66        return getCreator().createSystemImageFromSelector(iconSelector, width, height);
67    }
68
69    public static Image createImageFromFile(String file, double width, double height) {
70        return getCreator().createImageFromFile(file, width, height);
71    }
72
73    // This is used to create a CImage from a Image
74    public static CImage createFromImage(final Image image) {
75        return getCreator().createFromImage(image);
76    }
77
78    public static class Creator {
79        Creator() { }
80
81        // This is used to create a CImage with an NSImage pointer. It MUST be a CFRetained
82        // NSImage, and the CImage takes ownership of the non-GC retain. If callers need the
83        // NSImage themselves, they MUST call retain on the NSImage themselves.
84        public Image createImageUsingNativeSize(final long image) {
85            if (image == 0) return null;
86            final Dimension2D size = nativeGetNSImageSize(image);
87            return createImage(image, size.getWidth(), size.getHeight());
88        }
89
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
91        Image createImage(long image, double width, double height) {
92            if (image == 0) throw new Error("Unable to instantiate CImage with null native image reference.");
93            return createImageWithSize(image, width, height);
94        }
95
96        public Image createImageWithSize(final long image, final double width, final double height) {
97            final CImage img = new CImage(image);
98            img.resize(width, height);
99            return img.toImage();
100        }
101
102        // This is used to create a CImage that represents the icon of the given file.
103        public Image createImageOfFile(final String file, final int width, final int height) {
104            return createImage(nativeCreateNSImageOfFileFromLaunchServices(file), width, height);
105        }
106
107        public Image createImageFromFile(final String file, final double width, final double height) {
108            final long image = nativeCreateNSImageFromFileContents(file);
109            nativeSetNSImageSize(image, width, height);
110            return createImage(image, width, height);
111        }
112
113        public Image createSystemImageFromSelector(final String iconSelector, final int width, final int height) {
114            return createImage(nativeCreateNSImageFromIconSelector(getSelectorAsInt(iconSelector)), width, height);
115        }
116
117        public Image createImageFromName(final String name, final int width, final int height) {
118            return createImage(nativeCreateNSImageFromImageName(name), width, height);
119        }
120
121        public Image createImageFromName(final String name) {
122            return createImageUsingNativeSize(nativeCreateNSImageFromImageName(name));
123        }
124
125        private static int[] imageToArray(Image image, boolean prepareImage) {
126            if (image == null) return null;
127
128            if (prepareImage && !(image instanceof BufferedImage)) {
129                final MediaTracker mt = new MediaTracker(new Label());
130                final int id = 0;
131                mt.addImage(image, id);
132
133                try {
134                    mt.waitForID(id);
135                } catch (InterruptedException e) {
136                    return null;
137                }
138
139                if (mt.isErrorID(id)) {
140                    return null;
141                }
142            }
143
144            int w = image.getWidth(null);
145            int h = image.getHeight(null);
146
147            if (w < 0 || h < 0) {
148                return null;
149            }
150
151            BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
152            Graphics2D g2 = bimg.createGraphics();
153            g2.setComposite(AlphaComposite.Src);
154            g2.drawImage(image, 0, 0, null);
155            g2.dispose();
156
157            return ((DataBufferInt)bimg.getRaster().getDataBuffer()).getData();
158        }
159
160        public byte[] getPlatformImageBytes(final Image image) {
161            int[] buffer = imageToArray(image, false);
162
163            if (buffer == null) {
164                return null;
165            }
166
167            return nativeGetPlatformImageBytes(buffer, image.getWidth(null), image.getHeight(null));
168        }
169
170        /**
171         * Translates a byte array which contains platform-specific image data in the given format into an Image.
172         */
173        public Image createImageFromPlatformImageBytes(final byte[] buffer) {
174            return createImageUsingNativeSize(nativeCreateNSImageFromBytes(buffer));
175        }
176
177        // This is used to create a CImage from a Image
178        public CImage createFromImage(final Image image) {
179            return createFromImage(image, true);
180        }
181
182        public CImage createFromImageImmediately(final Image image) {
183            return createFromImage(image, false);
184        }
185
186        // This is used to create a CImage from a Image
187        private CImage createFromImage(final Image image, final boolean prepareImage) {
188            if (image instanceof MultiResolutionImage) {
189                List<Image> resolutionVariants
190                        = ((MultiResolutionImage) image).getResolutionVariants();
191                return createFromImages(resolutionVariants, prepareImage);
192            }
193
194            int[] buffer = imageToArray(image, prepareImage);
195            if (buffer == null) {
196                return null;
197            }
198            return new CImage(nativeCreateNSImageFromArray(buffer, image.getWidth(null), image.getHeight(null)));
199        }
200
201        public CImage createFromImages(final List<Image> images) {
202            return createFromImages(images, true);
203        }
204
205        private CImage createFromImages(final List<Image> images, final boolean prepareImage) {
206            if (images == null || images.isEmpty()) {
207                return null;
208            }
209
210            int num = images.size();
211
212            int[][] buffers = new int[num][];
213            int[] w = new int[num];
214            int[] h = new int[num];
215
216            num = 0;
217
218            for (final Image img : images) {
219                buffers[num] = imageToArray(img, prepareImage);
220                if (buffers[num] == null) {
221                    // Unable to process the image
222                    continue;
223                }
224                w[num] = img.getWidth(null);
225                h[num] = img.getHeight(null);
226                num++;
227            }
228
229            if (num == 0) {
230                return null;
231            }
232
233            return new CImage(nativeCreateNSImageFromArrays(
234                    Arrays.copyOf(buffers, num),
235                    Arrays.copyOf(w, num),
236                    Arrays.copyOf(h, num)));
237        }
238
239        static int getSelectorAsInt(final String fromString) {
240            final byte[] b = fromString.getBytes();
241            final int len = Math.min(b.length, 4);
242            int result = 0;
243            for (int i = 0; i < len; i++) {
244                if (i > 0) result <<= 8;
245                result |= (b[i] & 0xff);
246            }
247            return result;
248        }
249    }
250
251    CImage(long nsImagePtr) {
252        super(nsImagePtr, true);
253    }
254
255    /** @return A MultiResolution image created from nsImagePtr, or null. */
256    private Image toImage() {
257        if (ptr == 0) return null;
258
259        final Dimension2D size = nativeGetNSImageSize(ptr);
260        final int w = (int)size.getWidth();
261        final int h = (int)size.getHeight();
262
263        Dimension2D[] sizes
264                = nativeGetNSImageRepresentationSizes(ptr,
265                        size.getWidth(), size.getHeight());
266
267        return sizes == null || sizes.length < 2 ?
268                new MultiResolutionCachedImage(w, h, (width, height)
269                        -> toImage(w, h, width, height))
270                : new MultiResolutionCachedImage(w, h, sizes, (width, height)
271                        -> toImage(w, h, width, height));
272    }
273
274    private BufferedImage toImage(int srcWidth, int srcHeight, int dstWidth, int dstHeight) {
275        final BufferedImage bimg = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB_PRE);
276        final DataBufferInt dbi = (DataBufferInt)bimg.getRaster().getDataBuffer();
277        final int[] buffer = SunWritableRaster.stealData(dbi, 0);
278        nativeCopyNSImageIntoArray(ptr, buffer, srcWidth, srcHeight, dstWidth, dstHeight);
279        SunWritableRaster.markDirty(dbi);
280        return bimg;
281    }
282
283    /** If nsImagePtr != 0 then scale this NSImage. @return *this* */
284    CImage resize(final double w, final double h) {
285        if (ptr != 0) nativeSetNSImageSize(ptr, w, h);
286        return this;
287    }
288
289    void resizeRepresentations(double w, double h) {
290        if (ptr != 0) nativeResizeNSImageRepresentations(ptr, w, h);
291    }
292}
293