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 sun.awt.image.SunWritableRaster;
  33 
  34 public class CImage extends CFRetainedResource {
  35     private static native long nativeCreateNSImageFromArray(int[] buffer, int w, int h);
  36     private static native long nativeCreateNSImageFromFileContents(String file);
  37     private static native long nativeCreateNSImageOfFileFromLaunchServices(String file);
  38     private static native long nativeCreateNSImageFromImageName(String name);
  39     private static native long nativeCreateNSImageFromIconSelector(int selector);
  40     private static native void nativeCopyNSImageIntoArray(long image, int[] buffer, int w, int h);
  41     private static native Dimension2D nativeGetNSImageSize(long image);
  42     private static native void nativeSetNSImageSize(long image, double w, double h);
  43 
  44     static Creator creator = new Creator();
  45     static Creator getCreator() {
  46         return creator;
  47     }
  48 
  49     public static class Creator {
  50         Creator() { }
  51 
  52         // This is used to create a CImage with an NSImage pointer. It MUST be a CFRetained
  53         // NSImage, and the CImage takes ownership of the non-GC retain. If callers need the
  54         // NSImage themselves, they MUST call retain on the NSImage themselves.
  55         public BufferedImage createImageUsingNativeSize(final long image) {
  56             if (image == 0) return null;
  57             final Dimension2D size = nativeGetNSImageSize(image);
  58             return createBufferedImage(image, size.getWidth(), size.getHeight());
  59         }
  60 
  61         // 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
  62         BufferedImage createBufferedImage(long image, double width, double height) {
  63             if (image == 0) throw new Error("Unable to instantiate CImage with null native image reference.");
  64             return createImageWithSize(image, width, height);
  65         }
  66 
  67         public BufferedImage createImageWithSize(final long image, final double width, final double height) {
  68             final CImage img = new CImage(image);
  69             img.resize(width, height);
  70             return img.toImage();
  71         }
  72 
  73         // This is used to create a CImage that represents the icon of the given file.
  74         public BufferedImage createImageOfFile(final String file, final int width, final int height) {
  75             return createBufferedImage(nativeCreateNSImageOfFileFromLaunchServices(file), width, height);
  76         }
  77 
  78         public BufferedImage createImageFromFile(final String file, final double width, final double height) {
  79             final long image = nativeCreateNSImageFromFileContents(file);
  80             nativeSetNSImageSize(image, width, height);
  81             return createBufferedImage(image, width, height);
  82         }
  83 
  84         public BufferedImage createSystemImageFromSelector(final String iconSelector, final int width, final int height) {
  85             return createBufferedImage(nativeCreateNSImageFromIconSelector(getSelectorAsInt(iconSelector)), width, height);
  86         }
  87 
  88         public Image createImageFromName(final String name, final int width, final int height) {
  89             return createBufferedImage(nativeCreateNSImageFromImageName(name), width, height);
  90         }
  91 
  92         public Image createImageFromName(final String name) {
  93             return createImageUsingNativeSize(nativeCreateNSImageFromImageName(name));
  94         }
  95 
  96         // This is used to create a CImage from a Image
  97         public CImage createFromImage(final Image image) {
  98             if (image == null) return null;
  99 
 100             MediaTracker mt = new MediaTracker(new Label());
 101             final int id = 0;
 102             mt.addImage(image, id);
 103 
 104             try {
 105                 mt.waitForID(id);
 106             } catch (InterruptedException e) {
 107             }
 108 
 109             if (mt.isErrorID(id)) {
 110                 return null;
 111             }
 112 
 113             int w = image.getWidth(null);
 114             int h = image.getHeight(null);
 115             BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
 116             Graphics2D g2 = bimg.createGraphics();
 117             g2.setComposite(AlphaComposite.Src);
 118             g2.drawImage(image, 0, 0, null);
 119             g2.dispose();
 120             int[] buffer = ((DataBufferInt)bimg.getRaster().getDataBuffer()).getData();
 121             return new CImage(nativeCreateNSImageFromArray(buffer, w, h));
 122         }
 123 
 124         static int getSelectorAsInt(final String fromString) {
 125             final byte[] b = fromString.getBytes();
 126             final int len = Math.min(b.length, 4);
 127             int result = 0;
 128             for (int i = 0; i < len; i++) {
 129                 if (i > 0) result <<= 8;
 130                 result |= (b[i] & 0xff);
 131             }
 132             return result;
 133         }
 134     }
 135 
 136     CImage(long nsImagePtr) {
 137         super(nsImagePtr, true);
 138     }
 139 
 140     /** @return A BufferedImage created from nsImagePtr, or null. */
 141     public BufferedImage toImage() {
 142         if (ptr == 0) return null;
 143 
 144         final Dimension2D size = nativeGetNSImageSize(ptr);
 145         final int w = (int)size.getWidth();
 146         final int h = (int)size.getHeight();
 147 
 148         final BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
 149         final DataBufferInt dbi = (DataBufferInt)bimg.getRaster().getDataBuffer();
 150         final int[] buffer = SunWritableRaster.stealData(dbi, 0);
 151         nativeCopyNSImageIntoArray(ptr, buffer, w, h);
 152         SunWritableRaster.markDirty(dbi);
 153         return bimg;
 154     }
 155 
 156     /** If nsImagePtr != 0 then scale this NSImage. @return *this* */
 157     CImage resize(final double w, final double h) {
 158         if (ptr != 0) nativeSetNSImageSize(ptr, w, h);
 159         return this;
 160     }
 161 }