Example usage for java.awt.image Raster getPixels

List of usage examples for java.awt.image Raster getPixels

Introduction

In this page you can find the example usage for java.awt.image Raster getPixels.

Prototype

public double[] getPixels(int x, int y, int w, int h, double[] dArray) 

Source Link

Document

Returns a double array containing all samples for a rectangle of pixels, one sample per array element.

Usage

From source file:GraphicsUtil.java

public static void copyData_FALLBACK(Raster src, WritableRaster dst) {
    // System.out.println("Fallback copyData");

    int x0 = dst.getMinX();
    if (x0 < src.getMinX())
        x0 = src.getMinX();/*from  w  w w  . j  av a 2 s  . c  o m*/

    int y0 = dst.getMinY();
    if (y0 < src.getMinY())
        y0 = src.getMinY();

    int x1 = dst.getMinX() + dst.getWidth() - 1;
    if (x1 > src.getMinX() + src.getWidth() - 1)
        x1 = src.getMinX() + src.getWidth() - 1;

    int y1 = dst.getMinY() + dst.getHeight() - 1;
    if (y1 > src.getMinY() + src.getHeight() - 1)
        y1 = src.getMinY() + src.getHeight() - 1;

    int width = x1 - x0 + 1;
    int[] data = null;

    for (int y = y0; y <= y1; y++) {
        data = src.getPixels(x0, y, width, 1, data);
        dst.setPixels(x0, y, width, 1, data);
    }
}

From source file:GraphicsUtil.java

/**
 * Copies data from one bufferedImage to another paying attention
 * to the state of AlphaPreMultiplied./*from  ww w  . j a  va 2s  .  c  om*/
 *
 * @param src The source
 * @param srcRect The Rectangle of source data to be copied
 * @param dst The destination
 * @param destP The Place for the upper left corner of srcRect in dst.
 */
public static void copyData(BufferedImage src, Rectangle srcRect, BufferedImage dst, Point destP) {

    /*
     if (srcCS != dstCS)
    throw new IllegalArgumentException
        ("Images must be in the same ColorSpace in order "+
         "to copy Data between them");
     */
    boolean srcAlpha = src.getColorModel().hasAlpha();
    boolean dstAlpha = dst.getColorModel().hasAlpha();

    // System.out.println("Src has: " + srcAlpha +
    //                    " is: " + src.isAlphaPremultiplied());
    //
    // System.out.println("Dst has: " + dstAlpha +
    //                    " is: " + dst.isAlphaPremultiplied());

    if (srcAlpha == dstAlpha)
        if (!srcAlpha || src.isAlphaPremultiplied() == dst.isAlphaPremultiplied()) {
            // They match one another so just copy everything...
            copyData(src.getRaster(), dst.getRaster());
            return;
        }

    // System.out.println("Using Slow CopyData");

    int[] pixel = null;
    Raster srcR = src.getRaster();
    WritableRaster dstR = dst.getRaster();
    int bands = dstR.getNumBands();

    int dx = destP.x - srcRect.x;
    int dy = destP.y - srcRect.y;

    int w = srcRect.width;
    int x0 = srcRect.x;
    int y0 = srcRect.y;
    int y1 = y0 + srcRect.height - 1;

    if (!srcAlpha) {
        // Src has no alpha dest does so set alpha to 1.0 everywhere.
        // System.out.println("Add Alpha");
        int[] oPix = new int[bands * w];
        int out = (w * bands) - 1; // The 2 skips alpha channel
        while (out >= 0) {
            // Fill alpha channel with 255's
            oPix[out] = 255;
            out -= bands;
        }

        int b, in;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = w * (bands - 1) - 1;
            out = (w * bands) - 2; // The 2 skips alpha channel on last pix
            switch (bands) {
            case 4:
                while (in >= 0) {
                    oPix[out--] = pixel[in--];
                    oPix[out--] = pixel[in--];
                    oPix[out--] = pixel[in--];
                    out--;
                }
                break;
            default:
                while (in >= 0) {
                    for (b = 0; b < bands - 1; b++)
                        oPix[out--] = pixel[in--];
                    out--;
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, oPix);
        }
    } else if (dstAlpha && dst.isAlphaPremultiplied()) {
        // Src and dest have Alpha but we need to multiply it for dst.
        // System.out.println("Mult Case");
        int a, b, alpha, in, fpNorm = (1 << 24) / 255, pt5 = 1 << 23;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = bands * w - 1;
            switch (bands) {
            case 4:
                while (in >= 0) {
                    a = pixel[in];
                    if (a == 255)
                        in -= 4;
                    else {
                        in--;
                        alpha = fpNorm * a;
                        pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                        in--;
                        pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                        in--;
                        pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                        in--;
                    }
                }
                break;
            default:
                while (in >= 0) {
                    a = pixel[in];
                    if (a == 255)
                        in -= bands;
                    else {
                        in--;
                        alpha = fpNorm * a;
                        for (b = 0; b < bands - 1; b++) {
                            pixel[in] = (pixel[in] * alpha + pt5) >>> 24;
                            in--;
                        }
                    }
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, pixel);
        }
    } else if (dstAlpha && !dst.isAlphaPremultiplied()) {
        // Src and dest have Alpha but we need to divide it out for dst.
        // System.out.println("Div Case");
        int a, b, ialpha, in, fpNorm = 0x00FF0000, pt5 = 1 << 15;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = (bands * w) - 1;
            switch (bands) {
            case 4:
                while (in >= 0) {
                    a = pixel[in];
                    if ((a <= 0) || (a >= 255))
                        in -= 4;
                    else {
                        in--;
                        ialpha = fpNorm / a;
                        pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                        in--;
                        pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                        in--;
                        pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                        in--;
                    }
                }
                break;
            default:
                while (in >= 0) {
                    a = pixel[in];
                    if ((a <= 0) || (a >= 255))
                        in -= bands;
                    else {
                        in--;
                        ialpha = fpNorm / a;
                        for (b = 0; b < bands - 1; b++) {
                            pixel[in] = (pixel[in] * ialpha + pt5) >>> 16;
                            in--;
                        }
                    }
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, pixel);
        }
    } else if (src.isAlphaPremultiplied()) {
        int[] oPix = new int[bands * w];
        // Src has alpha dest does not so unpremult and store...
        // System.out.println("Remove Alpha, Div Case");
        int a, b, ialpha, in, out, fpNorm = 0x00FF0000, pt5 = 1 << 15;
        for (int y = y0; y <= y1; y++) {
            pixel = srcR.getPixels(x0, y, w, 1, pixel);
            in = (bands + 1) * w - 1;
            out = (bands * w) - 1;
            while (in >= 0) {
                a = pixel[in];
                in--;
                if (a > 0) {
                    if (a < 255) {
                        ialpha = fpNorm / a;
                        for (b = 0; b < bands; b++)
                            oPix[out--] = (pixel[in--] * ialpha + pt5) >>> 16;
                    } else
                        for (b = 0; b < bands; b++)
                            oPix[out--] = pixel[in--];
                } else {
                    in -= bands;
                    for (b = 0; b < bands; b++)
                        oPix[out--] = 255;
                }
            }
            dstR.setPixels(x0 + dx, y + dy, w, 1, oPix);
        }
    } else {
        // Src has unpremult alpha, dest does not have alpha,
        // just copy the color channels over.
        Rectangle dstRect = new Rectangle(destP.x, destP.y, srcRect.width, srcRect.height);
        for (int b = 0; b < bands; b++)
            copyBand(srcR, srcRect, b, dstR, dstRect, b);
    }
}

From source file:software.uncharted.image.ImageProcessing.java

/**
 * histogramByteHash -- compute a color histogram of an image
 *                      returned as a byte[]
 * @param img/*from ww  w  .  j  a  v a  2 s  .co  m*/
 * @return byte[] histogram
 */
public static byte[] histogramByteHash(BufferedImage img) {

    Raster raster = img.getData();
    int h = raster.getHeight();
    int w = raster.getWidth();
    int components = img.getColorModel().getNumComponents();
    int pixels = w * h;
    int[] colors = new int[pixels * components];
    raster.getPixels(0, 0, w, h, colors);
    int[] counts = new int[DISTINCT_COLORS];
    int grayScaleCount = 0;
    for (int i = 0; i < DISTINCT_COLORS; i++)
        counts[i] = 0;

    int cIndx = 0; // 'colours' array index
    for (int i = 0; i < pixels; i++) {
        int r = colors[cIndx] / COLOR_DIVISOR; // quantizes down to 'COLOR_DEPTH' range
        int g = (colors[cIndx + 1]) / COLOR_DIVISOR;
        int b = (colors[cIndx + 2]) / COLOR_DIVISOR;
        int truncColor = (r * COLOR_DEPTH + g) * COLOR_DEPTH + b; // converts 3D histogram values to 1D concatenated histogram
        counts[truncColor]++;
        if (r == g && r == b)
            grayScaleCount++;
        cIndx += components;
    }
    byte[] result = new byte[DISTINCT_COLORS];

    if (grayScaleCount > pixels * 0.95) {
        //---- grayscale image detected!
        // set black and white hist bins == max value
        // and set all other bins == hist values along one of the colour axes
        // (since r-axis vals = g-axis = b-axis for grayscale)
        counts[0] = pixels;
        counts[DISTINCT_COLORS - 1] = pixels;
        for (int i = 1; i < DISTINCT_COLORS - 1; i++) {
            counts[i] = 0;
        }
        for (int i = 0; i < pixels; i++) {
            int idx = colors[i * components] * (DISTINCT_COLORS - 2) / 256 + 1;
            counts[idx]++;
        }
    }

    //---- normalize final histogram
    for (int i = 0; i < DISTINCT_COLORS; i++) {
        //int count = (int)Math.ceil((counts[i]*RATIO_MULTIPLIER)/pixels);
        int count = (int) Math.round((counts[i] * RATIO_MULTIPLIER) / ((double) pixels * HIST_NORM_FACTOR));
        result[i] = (byte) (Math.min(count, RATIO_MULTIPLIER) & 0xFF); // Min here to handle potential saturation of hist values
    }

    return result;
}