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