001    /*
002     * To change this template, choose Tools | Templates
003     * and open the template in the editor.
004     */
005    
006    package com.nativelibs4java.util;
007    
008    import java.awt.Image;
009    import java.awt.image.BufferedImage;
010    import java.awt.image.DataBuffer;
011    import java.awt.image.DataBufferInt;
012    import java.awt.image.PixelGrabber;
013    import java.awt.image.WritableRaster;
014    import java.nio.IntBuffer;
015    
016    /**
017     *
018     * @author ochafik
019     */
020    public class ImageUtils {
021    
022            public static int[] getImageIntPixels(Image image, boolean allowDeoptimizingDirectRead) {
023                    return getImageIntPixels(image, 0, 0, image.getWidth(null), image.getHeight(null), allowDeoptimizingDirectRead);
024            }
025            public static int[] getImageIntPixels(Image image, int x, int y, int width, int height, boolean allowDeoptimizingDirectRead) {
026                    if (image instanceof BufferedImage) {
027                            BufferedImage bim = (BufferedImage)image;
028                            WritableRaster raster = bim.getRaster();
029                            if (allowDeoptimizingDirectRead &&
030                                            raster.getParent() == null &&
031                                            raster.getDataBuffer().getNumBanks() == 1)
032                            {
033                                    DataBuffer b = bim.getRaster().getDataBuffer();
034                                    if (b instanceof DataBufferInt) {
035                                            int[] array = ((DataBufferInt)b).getData();
036                                            return array;
037                                    }
038                            }
039                            return bim.getRGB(x, y, width, height, null, 0, width);
040                    }
041                    PixelGrabber grabber = new PixelGrabber(image, x, y, width, height, true);
042                    try {
043                            grabber.grabPixels();
044                            return (int[])grabber.getPixels();
045                    } catch (InterruptedException ex) {
046                            throw new RuntimeException("Pixel read operation was interrupted", ex);
047                    }
048            }
049    
050            public static void setImageIntPixels(BufferedImage image, boolean allowDeoptimizingDirectRead, IntBuffer pixels) {
051                    setImageIntPixels(image, 0, 0, image.getWidth(null), image.getHeight(null), allowDeoptimizingDirectRead, pixels);
052            }
053            public static void setImageIntPixels(BufferedImage bim, int x, int y, int width, int height, boolean allowDeoptimizingDirectRead, IntBuffer pixels) {
054                    WritableRaster raster = bim.getRaster();
055                    if (allowDeoptimizingDirectRead &&
056                                    raster.getParent() == null &&
057                                    raster.getDataBuffer().getNumBanks() == 1)
058                    {
059                            DataBuffer b = bim.getRaster().getDataBuffer();
060                            if (b instanceof DataBufferInt) {
061                                    IntBuffer.wrap(((DataBufferInt)b).getData()).put(pixels);
062                                    return;
063                            }
064                    }
065    
066                    IntBuffer b = IntBuffer.allocate(width * height);
067                    b.put(pixels);
068                    b.rewind();
069                    int[] array = b.array();
070                    bim.setRGB(x, y, width, height, array, 0, width);
071            }
072    }