Example usage for java.awt.image ColorModel getRGB

List of usage examples for java.awt.image ColorModel getRGB

Introduction

In this page you can find the example usage for java.awt.image ColorModel getRGB.

Prototype

public int getRGB(Object inData) 

Source Link

Document

Returns the color/alpha components for the specified pixel in the default RGB color model format.

Usage

From source file:com.googlecode.fightinglayoutbugs.helpers.ImageHelper.java

public static int[][] imageToPixels(BufferedImage image) {
    if (image == null) {
        return null;
    }//  w  w w .  j a v  a2  s. com
    int w = image.getWidth();
    int h = image.getHeight();
    int[][] pixels = new int[w][h];
    Raster raster = image.getRaster();
    if (raster.getTransferType() == DataBuffer.TYPE_BYTE) {
        byte[] bytes = (byte[]) raster.getDataElements(0, 0, w, h, null);
        int bytesPerPixel = (bytes.length / (w * h));
        ColorModel colorModel = image.getColorModel();
        byte[] buf = new byte[bytesPerPixel];
        for (int x = 0; x < w; ++x) {
            for (int y = 0; y < h; ++y) {
                System.arraycopy(bytes, (x + y * w) * bytesPerPixel, buf, 0, bytesPerPixel);
                pixels[x][y] = colorModel.getRGB(buf) & 0xFFFFFF;
            }
        }
        return pixels;
    } else {
        throw new RuntimeException("transfer type " + raster.getTransferType() + " not implemented yet");
    }
}

From source file:com.googlecode.fightinglayoutbugs.helpers.ImageHelper.java

private static List<RectangularRegion> findSubImageInImage(BufferedImage subImage, BufferedImage image,
        int max) {
    Map<Integer, List<Point>> rgb2offsets = new HashMap<Integer, List<Point>>();
    int sw = subImage.getWidth();
    int sh = subImage.getHeight();
    for (int x = 0; x < sw; ++x) {
        for (int y = 0; y < sh; ++y) {
            int argb = subImage.getRGB(x, y);
            int a = argb >>> 24;
            if (a == 255) {
                Integer rgb = argb & 0xFFFFFF;
                List<Point> offsets = rgb2offsets.get(rgb);
                if (offsets == null) {
                    offsets = new ArrayList<Point>();
                    rgb2offsets.put(rgb, offsets);
                }//from w  w w.  ja v  a  2s .  co m
                offsets.add(new Point(x, y));
            }
        }
    }
    List<RectangularRegion> result = new ArrayList<RectangularRegion>();
    int w = image.getWidth();
    int h = image.getHeight();
    int[][] p = new int[w][h];
    Raster raster = image.getRaster();
    if (raster.getTransferType() == DataBuffer.TYPE_BYTE) {
        byte[] bytes = (byte[]) raster.getDataElements(0, 0, w, h, null);
        int bytesPerPixel = (bytes.length / (w * h));
        ColorModel colorModel = image.getColorModel();
        byte[] buf = new byte[bytesPerPixel];
        for (int x = 0; x < w; ++x) {
            for (int y = 0; y < h; ++y) {
                System.arraycopy(bytes, (x + y * w) * bytesPerPixel, buf, 0, bytesPerPixel);
                p[x][y] = colorModel.getRGB(buf) & 0xFFFFFF;
            }
        }
    } else if (raster.getTransferType() == DataBuffer.TYPE_INT) {
        for (int x = 0; x < w; ++x) {
            p[x] = (int[]) raster.getDataElements(x, 0, 1, h, null);
        }
    } else {
        throw new RuntimeException("findSubImageInImage not implemented for image transfer type "
                + raster.getTransferType() + " yet.");
    }
    for (int x = 0; x < w; ++x) {
        for (int y = 0; y < h; ++y) {
            Iterator<Map.Entry<Integer, List<Point>>> i = rgb2offsets.entrySet().iterator();
            compareWithSubImageLoop: while (i.hasNext()) {
                Map.Entry<Integer, List<Point>> mapEntry = i.next();
                int expectedRgb = mapEntry.getKey();
                for (Point offset : mapEntry.getValue()) {
                    int xx = x + offset.x;
                    int yy = y + offset.y;
                    if (xx >= w || yy >= h || expectedRgb != p[xx][yy]) {
                        break compareWithSubImageLoop;
                    }
                }
                if (!i.hasNext()) {
                    result.add(new RectangularRegion(x, y, x + (sw - 1), y + (sh - 1)));
                    if (result.size() == max) {
                        return result;
                    }
                }
            }
        }
    }
    return result;
}

From source file:MainClass.java

private void setThePixels(int x, int y, int width, int height, ColorModel cm, Object pixels, int offset,
        int scansize) {
    int sourceOffset = offset;
    int destinationOffset = y * savedWidth + x;
    boolean bytearray = (pixels instanceof byte[]);
    for (int yy = 0; yy < height; yy++) {
        for (int xx = 0; xx < width; xx++)
            if (bytearray)
                savedPixels[destinationOffset++] = cm.getRGB(((byte[]) pixels)[sourceOffset++] & 0xff);
            else//from w  w  w.ja  va2  s. co  m
                savedPixels[destinationOffset++] = cm.getRGB(((int[]) pixels)[sourceOffset++]);
        sourceOffset += (scansize - width);
        destinationOffset += (savedWidth - width);
    }
}

From source file:com.piaoyou.util.ImageUtil.java

public void setPixels(int x, int y, int width, int height, ColorModel model, byte pixels[], int offset,
        int scansize) {
    // Store pixels in srcPixels array
    if (srcPixels == null)
        srcPixels = new int[srcWidth * srcHeight];
    for (int row = 0, destRow = y * srcWidth; row < height; row++, destRow += srcWidth) {
        int rowOff = offset + row * scansize;
        for (int col = 0; col < width; col++)
            // v1.2 : Added & 0xFF to disable sign bit
            srcPixels[destRow + x + col] = model.getRGB(pixels[rowOff + col] & 0xFF);
    }//from   w  ww .  j  a v a 2s. c  o  m
}

From source file:com.piaoyou.util.ImageUtil.java

public void setPixels(int x, int y, int width, int height, ColorModel model, int pixels[], int offset,
        int scansize) {
    // Store pixels in srcPixels array
    if (srcPixels == null)
        srcPixels = new int[srcWidth * srcHeight];
    for (int row = 0, destRow = y * srcWidth; row < height; row++, destRow += srcWidth) {
        int rowOff = offset + row * scansize;
        for (int col = 0; col < width; col++)
            // If model == null, consider it's the default RGB model
            srcPixels[destRow + x + col] = model == null ? pixels[rowOff + col]
                    : model.getRGB(pixels[rowOff + col]);
    }/*from ww  w .j a  v a 2  s.  c  om*/
}

From source file:GifEncoder.java

public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pixels, int off, int scansize) {
    int[] rgbPixels = new int[w];
    for (int row = 0; row < h; ++row) {
        int rowOff = off + row * scansize;
        for (int col = 0; col < w; ++col)
            rgbPixels[col] = model.getRGB(pixels[rowOff + col] & 0xff);
        try {//from  ww w .  j av a  2  s.co m
            encodePixelsWrapper(x, y + row, w, 1, rgbPixels, 0, w);
        } catch (IOException e) {
            iox = e;
            stop();
            return;
        }
    }
}

From source file:GifEncoder.java

public void setPixels(int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize) {
    if (model == rgbModel) {
        try {//from  w  ww.j  a  va 2 s .co  m
            encodePixelsWrapper(x, y, w, h, pixels, off, scansize);
        } catch (IOException e) {
            iox = e;
            stop();
            return;
        }
    } else {
        int[] rgbPixels = new int[w];
        for (int row = 0; row < h; ++row) {
            int rowOff = off + row * scansize;
            for (int col = 0; col < w; ++col)
                rgbPixels[col] = model.getRGB(pixels[rowOff + col]);
            try {
                encodePixelsWrapper(x, y + row, w, 1, rgbPixels, 0, w);
            } catch (IOException e) {
                iox = e;
                stop();
                return;
            }
        }
    }
}

From source file:org.apache.xmlgraphics.ps.PSImageUtils.java

/**
 * Extracts a packed RGB integer array of a RenderedImage.
 * @param img the image//from ww  w  .j a v a2s . c om
 * @param startX the starting X coordinate
 * @param startY the starting Y coordinate
 * @param w the width of the cropped image
 * @param h the height of the cropped image
 * @param rgbArray the prepared integer array to write to
 * @param offset offset in the target array
 * @param scansize width of a row in the target array
 * @return the populated integer array previously passed in as rgbArray parameter
 */
public static int[] getRGB(RenderedImage img, int startX, int startY, int w, int h, int[] rgbArray, int offset,
        int scansize) {
    Raster raster = img.getData();
    int yoff = offset;
    int off;
    Object data;
    int nbands = raster.getNumBands();
    int dataType = raster.getDataBuffer().getDataType();
    switch (dataType) {
    case DataBuffer.TYPE_BYTE:
        data = new byte[nbands];
        break;
    case DataBuffer.TYPE_USHORT:
        data = new short[nbands];
        break;
    case DataBuffer.TYPE_INT:
        data = new int[nbands];
        break;
    case DataBuffer.TYPE_FLOAT:
        data = new float[nbands];
        break;
    case DataBuffer.TYPE_DOUBLE:
        data = new double[nbands];
        break;
    default:
        throw new IllegalArgumentException("Unknown data buffer type: " + dataType);
    }

    if (rgbArray == null) {
        rgbArray = new int[offset + h * scansize];
    }

    ColorModel colorModel = img.getColorModel();
    for (int y = startY; y < startY + h; y++, yoff += scansize) {
        off = yoff;
        for (int x = startX; x < startX + w; x++) {
            rgbArray[off++] = colorModel.getRGB(raster.getDataElements(x, y, data));
        }
    }

    return rgbArray;
}

From source file:ucar.unidata.idv.ui.ImageGenerator.java

private static boolean isNotBlank(BufferedImage image) {
    // yes, i know this is bonkers. yes, the next step is to try sampling
    // to avoid iterating over each pixel.
    // tests on my linux machine typically find a non-blank pixel within the
    // first 100 iterations, and fewer than 5 retries to get a non-blank
    // *image* (typically 1-2 retries though)
    DataBuffer buf = image.getRaster().getDataBuffer();
    ColorModel model = image.getColorModel();
    boolean result = false;
    int i;//from  ww w  . ja va  2 s  .c o  m
    for (i = 0; i < buf.getSize(); i++) {
        // it's apparently not sufficient to simply grab the value directly;
        // Linux seems to store the "blank" value as -16777216 (-2^24, which
        // makes a lot of sense for images), while on OS X the blank value
        // is simply 0. i suspect the getRGB stuff is a candidate for
        // inlining by the JIT, but profiling is needed.
        int rgb = model.getRGB(buf.getElem(i));
        if (rgb != -16777216) {
            logger.trace("found non-blank value '{}' at index {}", rgb, i);
            result = true;
            break;
        }
    }
    logger.trace("{} iterations to return {}", i, result);
    return result;
}