1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.lwawt.macosx;
  27 
  28 import java.awt.*;
  29 import java.awt.geom.Dimension2D;
  30 import java.awt.image.*;
  31 
  32 import java.util.Arrays;
  33 import java.util.List;
  34 import sun.awt.image.MultiResolutionImage;
  35 import sun.awt.image.MultiResolutionBufferedImage;
  36 
  37 import sun.awt.image.SunWritableRaster;
  38 
  39 public class CImage extends CFRetainedResource {
  40     private static native long nativeCreateNSImageFromArray(int[] buffer, int w, int h);
  41     private static native long nativeCreateNSImageFromArrays(int[][] buffers, int w[], int h[]);
  42     private static native long nativeCreateNSImageFromFileContents(String file);
  43     private static native long nativeCreateNSImageOfFileFromLaunchServices(String file);
  44     private static native long nativeCreateNSImageFromImageName(String name);
  45     private static native long nativeCreateNSImageFromIconSelector(int selector);
  46     private static native void nativeCopyNSImageIntoArray(long image, int[] buffer, int sw, int sh, int dw, int dh);
  47     private static native Dimension2D nativeGetNSImageSize(long image);
  48     private static native void nativeSetNSImageSize(long image, double w, double h);
  49     private static native void nativeResizeNSImageRepresentations(long image, double w, double h);
  50     private static native Dimension2D[] nativeGetNSImageRepresentationSizes(long image, double w, double h);
  51 
  52     static Creator creator = new Creator();
  53     static Creator getCreator() {
  54         return creator;
  55     }
  56 
  57     public static class Creator {
  58         Creator() { }
  59 
  60         // This is used to create a CImage with an NSImage pointer. It MUST be a CFRetained
  61         // NSImage, and the CImage takes ownership of the non-GC retain. If callers need the
  62         // NSImage themselves, they MUST call retain on the NSImage themselves.
  63         public BufferedImage createImageUsingNativeSize(final long image) {
  64             if (image == 0) return null;
  65             final Dimension2D size = nativeGetNSImageSize(image);
  66             return createBufferedImage(image, size.getWidth(), size.getHeight());
  67         }
  68 
  69         // 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
  70         BufferedImage createBufferedImage(long image, double width, double height) {
  71             if (image == 0) throw new Error("Unable to instantiate CImage with null native image reference.");
  72             return createImageWithSize(image, width, height);
  73         }
  74 
  75         public BufferedImage createImageWithSize(final long image, final double width, final double height) {
  76             final CImage img = new CImage(image);
  77             img.resize(width, height);
  78             return img.toImage();
  79         }
  80 
  81         // This is used to create a CImage that represents the icon of the given file.
  82         public BufferedImage createImageOfFile(final String file, final int width, final int height) {
  83             return createBufferedImage(nativeCreateNSImageOfFileFromLaunchServices(file), width, height);
  84         }
  85 
  86         public BufferedImage createImageFromFile(final String file, final double width, final double height) {
  87             final long image = nativeCreateNSImageFromFileContents(file);
  88             nativeSetNSImageSize(image, width, height);
  89             return createBufferedImage(image, width, height);
  90         }
  91 
  92         public BufferedImage createSystemImageFromSelector(final String iconSelector, final int width, final int height) {
  93             return createBufferedImage(nativeCreateNSImageFromIconSelector(getSelectorAsInt(iconSelector)), width, height);
  94         }
  95 
  96         public Image createImageFromName(final String name, final int width, final int height) {
  97             return createBufferedImage(nativeCreateNSImageFromImageName(name), width, height);
  98         }
  99 
 100         public Image createImageFromName(final String name) {
 101             return createImageUsingNativeSize(nativeCreateNSImageFromImageName(name));
 102         }
 103 
 104         private static int[] imageToArray(Image image, boolean prepareImage) {
 105             if (image == null) return null;
 106 
 107             if (prepareImage && !(image instanceof BufferedImage)) {
 108                 final MediaTracker mt = new MediaTracker(new Label());
 109                 final int id = 0;
 110                 mt.addImage(image, id);
 111 
 112                 try {
 113                     mt.waitForID(id);
 114                 } catch (InterruptedException e) {
 115                     return null;
 116                 }
 117 
 118                 if (mt.isErrorID(id)) {
 119                     return null;
 120                 }
 121             }
 122 
 123             int w = image.getWidth(null);
 124             int h = image.getHeight(null);
 125 
 126             if (w < 0 || h < 0) {
 127                 return null;
 128             }
 129 
 130             BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
 131             Graphics2D g2 = bimg.createGraphics();
 132             g2.setComposite(AlphaComposite.Src);
 133             g2.drawImage(image, 0, 0, null);
 134             g2.dispose();
 135 
 136             return ((DataBufferInt)bimg.getRaster().getDataBuffer()).getData();
 137         }
 138 
 139         public CImage createFromImageImmediately(final Image image) {
 140             int[]  buffer = imageToArray(image, false);
 141 
 142             if (buffer == null) {
 143                 return null;
 144             }
 145 
 146             return new CImage(nativeCreateNSImageFromArray(buffer, image.getWidth(null),
 147                                                            image.getHeight(null)));
 148         }
 149 
 150         // This is used to create a CImage from a Image
 151         public CImage createFromImage(final Image image) {
 152             if (image instanceof MultiResolutionImage) {
 153                 List<Image> resolutionVariants
 154                         = ((MultiResolutionImage) image).getResolutionVariants();
 155                 return createFromImages(resolutionVariants);
 156             }
 157 
 158             int[] buffer = imageToArray(image, true);
 159             if (buffer == null) {
 160                 return null;
 161             }
 162             return new CImage(nativeCreateNSImageFromArray(buffer, image.getWidth(null), image.getHeight(null)));
 163         }
 164 
 165         public CImage createFromImages(List<Image> images) {
 166             if (images == null || images.isEmpty()) {
 167                 return null;
 168             }
 169 
 170             int num = images.size();
 171 
 172             int[][] buffers = new int[num][];
 173             int[] w = new int[num];
 174             int[] h = new int[num];
 175 
 176             num = 0;
 177 
 178             for (Image img : images) {
 179                 buffers[num] = imageToArray(img, true);
 180                 if (buffers[num] == null) {
 181                     // Unable to process the image
 182                     continue;
 183                 }
 184                 w[num] = img.getWidth(null);
 185                 h[num] = img.getHeight(null);
 186                 num++;
 187             }
 188 
 189             if (num == 0) {
 190                 return null;
 191             }
 192 
 193             return new CImage(nativeCreateNSImageFromArrays(
 194                         Arrays.copyOf(buffers, num),
 195                         Arrays.copyOf(w, num),
 196                         Arrays.copyOf(h, num)));
 197         }
 198 
 199         static int getSelectorAsInt(final String fromString) {
 200             final byte[] b = fromString.getBytes();
 201             final int len = Math.min(b.length, 4);
 202             int result = 0;
 203             for (int i = 0; i < len; i++) {
 204                 if (i > 0) result <<= 8;
 205                 result |= (b[i] & 0xff);
 206             }
 207             return result;
 208         }
 209     }
 210 
 211     CImage(long nsImagePtr) {
 212         super(nsImagePtr, true);
 213     }
 214 
 215     /** @return A MultiResolution image created from nsImagePtr, or null. */
 216     private BufferedImage toImage() {
 217         if (ptr == 0) return null;
 218 
 219         final Dimension2D size = nativeGetNSImageSize(ptr);
 220         final int w = (int)size.getWidth();
 221         final int h = (int)size.getHeight();
 222 
 223         Dimension2D[] sizes
 224                 = nativeGetNSImageRepresentationSizes(ptr,
 225                         size.getWidth(), size.getHeight());
 226 
 227         if (sizes == null || sizes.length < 2) {
 228             return toImage(w, h, w, h);
 229         }
 230 
 231         BufferedImage[] images = new BufferedImage[sizes.length];
 232         int currentImageIndex = 0;
 233 
 234         for (int i = 0; i < sizes.length; i++) {
 235             int imageRepWidth = (int) sizes[i].getWidth();
 236             int imageRepHeight = (int) sizes[i].getHeight();
 237 
 238             if(imageRepHeight <= w && imageRepHeight <= h){
 239                 currentImageIndex = i;
 240             }
 241             images[i] = toImage(w, h, imageRepWidth, imageRepHeight);
 242         }
 243         return new MultiResolutionBufferedImage(BufferedImage.TYPE_INT_ARGB_PRE,
 244                 currentImageIndex, images);
 245     }
 246 
 247     private BufferedImage toImage(int srcWidth, int srcHeight, int dstWidth, int dstHeight) {
 248         final BufferedImage bimg = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB_PRE);
 249         final DataBufferInt dbi = (DataBufferInt)bimg.getRaster().getDataBuffer();
 250         final int[] buffer = SunWritableRaster.stealData(dbi, 0);
 251         nativeCopyNSImageIntoArray(ptr, buffer, srcWidth, srcHeight, dstWidth, dstHeight);
 252         SunWritableRaster.markDirty(dbi);
 253         return bimg;
 254     }
 255 
 256     /** If nsImagePtr != 0 then scale this NSImage. @return *this* */
 257     CImage resize(final double w, final double h) {
 258         if (ptr != 0) nativeSetNSImageSize(ptr, w, h);
 259         return this;
 260     }
 261 
 262     void resizeRepresentations(double w, double h) {
 263         if (ptr != 0) nativeResizeNSImageRepresentations(ptr, w, h);
 264     }
 265 }