src/macosx/classes/sun/lwawt/macosx/CImage.java

Print this page




  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) {


  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() {


  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 
  35 import sun.awt.image.SunWritableRaster;
  36 
  37 public class CImage extends CFRetainedResource {
  38     private static native long nativeCreateNSImageFromArray(int[] buffer, int w, int h);
  39     private static native long nativeCreateNSImageFromArrays(int[][] buffers, int w[], int h[]);
  40     private static native long nativeCreateNSImageFromFileContents(String file);
  41     private static native long nativeCreateNSImageOfFileFromLaunchServices(String file);
  42     private static native long nativeCreateNSImageFromImageName(String name);
  43     private static native long nativeCreateNSImageFromIconSelector(int selector);
  44     private static native void nativeCopyNSImageIntoArray(long image, int[] buffer, int w, int h);
  45     private static native Dimension2D nativeGetNSImageSize(long image);
  46     private static native void nativeSetNSImageSize(long image, double w, double h);
  47 
  48     static Creator creator = new Creator();
  49     static Creator getCreator() {
  50         return creator;
  51     }
  52 
  53     public static class Creator {
  54         Creator() { }
  55 
  56         // This is used to create a CImage with an NSImage pointer. It MUST be a CFRetained
  57         // NSImage, and the CImage takes ownership of the non-GC retain. If callers need the
  58         // NSImage themselves, they MUST call retain on the NSImage themselves.
  59         public BufferedImage createImageUsingNativeSize(final long image) {


  80         }
  81 
  82         public BufferedImage createImageFromFile(final String file, final double width, final double height) {
  83             final long image = nativeCreateNSImageFromFileContents(file);
  84             nativeSetNSImageSize(image, width, height);
  85             return createBufferedImage(image, width, height);
  86         }
  87 
  88         public BufferedImage createSystemImageFromSelector(final String iconSelector, final int width, final int height) {
  89             return createBufferedImage(nativeCreateNSImageFromIconSelector(getSelectorAsInt(iconSelector)), width, height);
  90         }
  91 
  92         public Image createImageFromName(final String name, final int width, final int height) {
  93             return createBufferedImage(nativeCreateNSImageFromImageName(name), width, height);
  94         }
  95 
  96         public Image createImageFromName(final String name) {
  97             return createImageUsingNativeSize(nativeCreateNSImageFromImageName(name));
  98         }
  99 
 100         private static int[] imageToArray(Image image) {

 101             if (image == null) return null;
 102 
 103             MediaTracker mt = new MediaTracker(new Label());
 104             final int id = 0;
 105             mt.addImage(image, id);
 106 
 107             try {
 108                 mt.waitForID(id);
 109             } catch (InterruptedException e) {
 110             }
 111 
 112             if (mt.isErrorID(id)) {
 113                 return null;
 114             }
 115 
 116             int w = image.getWidth(null);
 117             int h = image.getHeight(null);
 118             BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
 119             Graphics2D g2 = bimg.createGraphics();
 120             g2.setComposite(AlphaComposite.Src);
 121             g2.drawImage(image, 0, 0, null);
 122             g2.dispose();
 123             return ((DataBufferInt)bimg.getRaster().getDataBuffer()).getData();
 124         }
 125 
 126         // This is used to create a CImage from a Image
 127         public CImage createFromImage(final Image image) {
 128             int[] buffer = imageToArray(image);
 129             if (buffer == null) {
 130                 return null;
 131             }
 132             return new CImage(nativeCreateNSImageFromArray(buffer, image.getWidth(null), image.getHeight(null)));
 133         }
 134 
 135         public CImage createFromImages(List<Image> images) {
 136             if (images == null || images.isEmpty()) {
 137                 return null;
 138             }
 139 
 140             int num = images.size();
 141 
 142             int[][] buffers = new int[num][];
 143             int[] w = new int[num];
 144             int[] h = new int[num];
 145 
 146             num = 0;
 147 
 148             for (Image img : images) {
 149                 buffers[num] = imageToArray(img);
 150                 if (buffers[num] == null) {
 151                     // Unable to process the image
 152                     continue;
 153                 }
 154                 w[num] = img.getWidth(null);
 155                 h[num] = img.getHeight(null);
 156                 num++;
 157             }
 158 
 159             if (num == 0) {
 160                 return null;
 161             }
 162 
 163             return new CImage(nativeCreateNSImageFromArrays(
 164                         Arrays.copyOf(buffers, num),
 165                         Arrays.copyOf(w, num),
 166                         Arrays.copyOf(h, num)));
 167         }
 168 
 169         static int getSelectorAsInt(final String fromString) {
 170             final byte[] b = fromString.getBytes();
 171             final int len = Math.min(b.length, 4);
 172             int result = 0;
 173             for (int i = 0; i < len; i++) {
 174                 if (i > 0) result <<= 8;
 175                 result |= (b[i] & 0xff);
 176             }
 177             return result;
 178         }
 179     }
 180 
 181     CImage(long nsImagePtr) {
 182         super(nsImagePtr, true);
 183     }
 184 
 185     /** @return A BufferedImage created from nsImagePtr, or null. */
 186     public BufferedImage toImage() {