Example usage for java.awt.image Raster getPixel

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

Introduction

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

Prototype

public double[] getPixel(int x, int y, double[] dArray) 

Source Link

Document

Returns the samples in an array of double for the specified pixel.

Usage

From source file:Main.java

/**
 * Trims the transparent pixels from the given {@link BufferedImage} (returns a sub-image).
 *
 * @param source The source image.//from   w ww  . j  av a 2s  .  c  o m
 * @return A new, trimmed image, or the source image if no trim is performed.
 */
public static BufferedImage trimmedImage(BufferedImage source) {
    final int minAlpha = 1;
    final int srcWidth = source.getWidth();
    final int srcHeight = source.getHeight();
    Raster raster = source.getRaster();
    int l = srcWidth, t = srcHeight, r = 0, b = 0;

    int alpha, x, y;
    int[] pixel = new int[4];
    for (y = 0; y < srcHeight; y++) {
        for (x = 0; x < srcWidth; x++) {
            raster.getPixel(x, y, pixel);
            alpha = pixel[3];
            if (alpha >= minAlpha) {
                l = Math.min(x, l);
                t = Math.min(y, t);
                r = Math.max(x, r);
                b = Math.max(y, b);
            }
        }
    }

    if (l > r || t > b) {
        // No pixels, couldn't trim
        return source;
    }

    return source.getSubimage(l, t, r - l + 1, b - t + 1);
}

From source file:TextureByReference.java

public static BufferedImage convertToCustomRGB(BufferedImage bImage) {
    if (bImage.getType() != BufferedImage.TYPE_INT_ARGB) {
        ImageOps.convertImage(bImage, BufferedImage.TYPE_INT_ARGB);
    }// w ww  .j  a va2 s.  c  o m

    int width = bImage.getWidth();
    int height = bImage.getHeight();

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = { 8, 8, 8 };
    ColorModel cm = new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE, 0);
    int[] bandOffset = { 0, 1, 2 };

    WritableRaster newRaster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 3, 3,
            bandOffset, null);
    byte[] byteData = ((DataBufferByte) newRaster.getDataBuffer()).getData();
    Raster origRaster = bImage.getData();
    int[] pixel = new int[4];
    int k = 0;
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            pixel = origRaster.getPixel(i, j, pixel);
            byteData[k++] = (byte) (pixel[0]);
            byteData[k++] = (byte) (pixel[1]);
            byteData[k++] = (byte) (pixel[2]);
        }
    }
    BufferedImage newImage = new BufferedImage(cm, newRaster, false, null);
    //  if (newImage.getType() == BufferedImage.TYPE_CUSTOM) {
    //    System.out.println("Type is custom");
    //  }
    return newImage;
}

From source file:TextureByReference.java

public static BufferedImage convertToCustomRGBA(BufferedImage bImage) {
    if (bImage.getType() != BufferedImage.TYPE_INT_ARGB) {
        ImageOps.convertImage(bImage, BufferedImage.TYPE_INT_ARGB);
    }//from  w  w w .  ja  va2  s.com

    int width = bImage.getWidth();
    int height = bImage.getHeight();

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = { 8, 8, 8, 8 };
    ColorModel cm = new ComponentColorModel(cs, nBits, true, false, Transparency.OPAQUE, 0);
    int[] bandOffset = { 0, 1, 2, 3 };

    WritableRaster newRaster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 4, 4,
            bandOffset, null);
    byte[] byteData = ((DataBufferByte) newRaster.getDataBuffer()).getData();
    Raster origRaster = bImage.getData();
    int[] pixel = new int[4];
    int k = 0;
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            pixel = origRaster.getPixel(i, j, pixel);
            byteData[k++] = (byte) (pixel[0]);
            byteData[k++] = (byte) (pixel[1]);
            byteData[k++] = (byte) (pixel[2]);
            byteData[k++] = (byte) (pixel[3]);
        }
    }
    BufferedImage newImage = new BufferedImage(cm, newRaster, false, null);
    //  if (newImage.getType() == BufferedImage.TYPE_CUSTOM) {
    //    System.out.println("Type is custom");
    //  }
    return newImage;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploaderThumbnailerTester_2.java

private int sumPixel(Raster imageData, int col, int row) {
    int pixelSum = 0;
    int[] pixel = imageData.getPixel(col, row, new int[0]);
    for (int value : pixel) {
        pixelSum += value;/*from  w  w  w.  j a v a 2  s.  c  o  m*/
    }
    return pixelSum;
}

From source file:exemplos.PegandoPixelsSemJAI.java

public void pegaPixel() throws IOException {

    File f = new File("D:\\ProjetosNetBeans\\PDI\\src\\imagens\\ebola.png"); //seleciona o arquivo
    BufferedImage image = ImageIO.read(f); //le o arquivo
    System.out.println("Dimenses: " + image.getWidth() + "x" + image.getHeight() + "pixels"); //exibe suas dimenses

    Raster raster = image.getRaster(); //pega os valores dos pixels
    double pixel[] = new double[3]; //cria um array com 3 posies
    int brancos = 0; //cria uma varivel para contar os pixels brancos
    for (int h = 0; h < image.getHeight(); h++)
        for (int w = 0; w < image.getWidth(); w++) {
            raster.getPixel(w, h, pixel);
            if ((pixel[0] == 255) && (pixel[1] == 255) && (pixel[2] == 255))//confirma se o RGB  igual a 255, ou seja, branco
                brancos++;/*from w w w .jav a  2 s. c om*/

        }

}

From source file:com.alibaba.simpleimage.codec.jpeg.JPEGDecoderFunctionTest.java

protected void compareImage(String name, BufferedImage left, BufferedImage right, boolean ignoreError)
        throws Exception {
    if (left.getWidth() != right.getWidth() || left.getHeight() != right.getHeight()) {
        assertTrue("size not equal", false);
    }/*from   w ww  .  j  a v  a 2s.  c o  m*/

    Raster leftRaster = left.getData();
    Raster rightRaster = right.getData();
    int[] leftPixes = new int[4];
    int[] rightPixes = new int[4];

    for (int x = 0; x < right.getWidth(); x++) {
        for (int y = 0; y < right.getHeight(); y++) {
            leftPixes = leftRaster.getPixel(x, y, leftPixes);
            rightPixes = rightRaster.getPixel(x, y, rightPixes);

            for (int i = 0; i < leftRaster.getNumBands(); i++) {
                if (Math.abs(leftPixes[i] - rightPixes[i]) > DIFF_VALUE) {
                    if (!ignoreError) {
                        assertTrue(name + "'s pix not equal, sub is " + (leftPixes[i] - rightPixes[i]), false);
                    }
                }

                leftPixes[i] = 0;
                rightPixes[i] = 0;
            }
        }
    }
}

From source file:omr.jai.TestImage3.java

private void dumpPixels (int x0,
                         int y0,
                         int w,
                         int h)
{
    Raster raster = image.getData();
    int[] pixel = null;
    System.out.print("pixels=");
    for (int y = y0; y < y0+h; y++) {
        System.out.println();/*from   w  w w .j av a 2s.  c o m*/
        for (int x = x0; x <= x0+w; x++) {
            pixel = raster.getPixel(x, y, pixel);
            System.out.print(" [");
            for (int i = 0; i < pixel.length; i++) {
                System.out.print(String.format("%3x", pixel[i]));
            }
            System.out.print("]");
        }
    }
    System.out.println();
}

From source file:org.apache.xmlgraphics.image.codec.png.PNGImageDecoder.java

private void processPixels(final int process, final Raster src, final WritableRaster dst, final int xOffset,
        final int step, final int y, final int width) {
    int srcX, dstX;

    // Create an array suitable for holding one pixel
    final int[] ps = src.getPixel(0, 0, (int[]) null);
    final int[] pd = dst.getPixel(0, 0, (int[]) null);

    dstX = xOffset;//from  ww  w.  ja va  2  s .  com
    switch (process) {
    case POST_NONE:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);
            dst.setPixel(dstX, y, ps);
            dstX += step;
        }
        break;

    case POST_GAMMA:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            for (int i = 0; i < this.inputBands; ++i) {
                final int x = ps[i];
                ps[i] = this.gammaLut[x];
            }

            dst.setPixel(dstX, y, ps);
            dstX += step;
        }
        break;

    case POST_GRAY_LUT:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            pd[0] = this.grayLut[ps[0]];

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_GRAY_LUT_ADD_TRANS:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int val = ps[0];
            pd[0] = this.grayLut[val];
            if (val == this.grayTransparentAlpha) {
                pd[1] = 0;
            } else {
                pd[1] = this.maxOpacity;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_PALETTE_TO_RGB:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int val = ps[0];
            pd[0] = this.redPalette[val];
            pd[1] = this.greenPalette[val];
            pd[2] = this.bluePalette[val];

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_PALETTE_TO_RGBA:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int val = ps[0];
            pd[0] = this.redPalette[val];
            pd[1] = this.greenPalette[val];
            pd[2] = this.bluePalette[val];
            pd[3] = this.alphaPalette[val];

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_ADD_GRAY_TRANS:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            int val = ps[0];
            if (this.performGammaCorrection) {
                val = this.gammaLut[val];
            }
            pd[0] = val;
            if (val == this.grayTransparentAlpha) {
                pd[1] = 0;
            } else {
                pd[1] = this.maxOpacity;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_ADD_RGB_TRANS:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int r = ps[0];
            final int g = ps[1];
            final int b = ps[2];
            if (this.performGammaCorrection) {
                pd[0] = this.gammaLut[r];
                pd[1] = this.gammaLut[g];
                pd[2] = this.gammaLut[b];
            } else {
                pd[0] = r;
                pd[1] = g;
                pd[2] = b;
            }
            if (r == this.redTransparentAlpha && g == this.greenTransparentAlpha
                    && b == this.blueTransparentAlpha) {
                pd[3] = 0;
            } else {
                pd[3] = this.maxOpacity;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_REMOVE_GRAY_TRANS:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int g = ps[0];
            if (this.performGammaCorrection) {
                pd[0] = this.gammaLut[g];
            } else {
                pd[0] = g;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_REMOVE_RGB_TRANS:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int r = ps[0];
            final int g = ps[1];
            final int b = ps[2];
            if (this.performGammaCorrection) {
                pd[0] = this.gammaLut[r];
                pd[1] = this.gammaLut[g];
                pd[2] = this.gammaLut[b];
            } else {
                pd[0] = r;
                pd[1] = g;
                pd[2] = b;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_GAMMA_EXP:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int val = ps[0];
            final int alpha = ps[1];
            final int gamma = this.gammaLut[val];
            pd[0] = gamma;
            pd[1] = gamma;
            pd[2] = gamma;
            pd[3] = alpha;

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_GRAY_ALPHA_EXP:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int val = ps[0];
            final int alpha = ps[1];
            pd[0] = val;
            pd[1] = val;
            pd[2] = val;
            pd[3] = alpha;

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_ADD_GRAY_TRANS_EXP:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            int val = ps[0];
            if (this.performGammaCorrection) {
                val = this.gammaLut[val];
            }
            pd[0] = val;
            pd[1] = val;
            pd[2] = val;
            if (val == this.grayTransparentAlpha) {
                pd[3] = 0;
            } else {
                pd[3] = this.maxOpacity;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_GRAY_LUT_ADD_TRANS_EXP:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int val = ps[0];
            final int val2 = this.grayLut[val];
            pd[0] = val2;
            pd[1] = val2;
            pd[2] = val2;
            if (val == this.grayTransparentAlpha) {
                pd[3] = 0;
            } else {
                pd[3] = this.maxOpacity;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;
    }
}

From source file:org.apache.xmlgraphics.image.codec.png.PNGRed.java

private void processPixels(final int process, final Raster src, final WritableRaster dst, final int xOffset,
        final int step, final int y, final int width) {
    int srcX, dstX;

    // Create an array suitable for holding one pixel
    final int[] ps = src.getPixel(0, 0, (int[]) null);
    final int[] pd = dst.getPixel(0, 0, (int[]) null);

    dstX = xOffset;/*from   ww w  .  java2 s.com*/
    switch (process) {
    case POST_NONE:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);
            dst.setPixel(dstX, y, ps);
            dstX += step;
        }
        break;

    case POST_GAMMA:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            for (int i = 0; i < this.inputBands; ++i) {
                final int x = ps[i];
                ps[i] = this.gammaLut[x];
            }

            dst.setPixel(dstX, y, ps);
            dstX += step;
        }
        break;

    case POST_GRAY_LUT:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            pd[0] = this.grayLut[ps[0]];

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_GRAY_LUT_ADD_TRANS:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int val = ps[0];
            pd[0] = this.grayLut[val];
            if (val == this.grayTransparentAlpha) {
                pd[1] = 0;
            } else {
                pd[1] = this.maxOpacity;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_PALETTE_TO_RGB:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int val = ps[0];
            pd[0] = this.redPalette[val];
            pd[1] = this.greenPalette[val];
            pd[2] = this.bluePalette[val];

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_PALETTE_TO_RGBA:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int val = ps[0];
            pd[0] = this.redPalette[val];
            pd[1] = this.greenPalette[val];
            pd[2] = this.bluePalette[val];
            pd[3] = this.alphaPalette[val];

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_ADD_GRAY_TRANS:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            int val = ps[0];
            if (this.performGammaCorrection) {
                val = this.gammaLut[val];
            }
            pd[0] = val;
            if (val == this.grayTransparentAlpha) {
                pd[1] = 0;
            } else {
                pd[1] = this.maxOpacity;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_ADD_RGB_TRANS:
        final boolean flagGammaCorrection = this.performGammaCorrection; // local
        // is
        // cheaper
        final int[] workGammaLut = this.gammaLut;
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int r = ps[0];
            final int g = ps[1];
            final int b = ps[2];
            if (flagGammaCorrection) {
                pd[0] = workGammaLut[r];
                pd[1] = workGammaLut[g];
                pd[2] = workGammaLut[b];
            } else {
                pd[0] = r;
                pd[1] = g;
                pd[2] = b;
            }
            if (r == this.redTransparentAlpha && g == this.greenTransparentAlpha
                    && b == this.blueTransparentAlpha) {
                pd[3] = 0;
            } else {
                pd[3] = this.maxOpacity;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_REMOVE_GRAY_TRANS:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int g = ps[0];
            if (this.performGammaCorrection) {
                pd[0] = this.gammaLut[g];
            } else {
                pd[0] = g;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_REMOVE_RGB_TRANS:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int r = ps[0];
            final int g = ps[1];
            final int b = ps[2];
            if (this.performGammaCorrection) {
                pd[0] = this.gammaLut[r];
                pd[1] = this.gammaLut[g];
                pd[2] = this.gammaLut[b];
            } else {
                pd[0] = r;
                pd[1] = g;
                pd[2] = b;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_GAMMA_EXP:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int val = ps[0];
            final int alpha = ps[1];
            final int gamma = this.gammaLut[val];
            pd[0] = gamma;
            pd[1] = gamma;
            pd[2] = gamma;
            pd[3] = alpha;

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_GRAY_ALPHA_EXP:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int val = ps[0];
            final int alpha = ps[1];
            pd[0] = val;
            pd[1] = val;
            pd[2] = val;
            pd[3] = alpha;

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_ADD_GRAY_TRANS_EXP:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            int val = ps[0];
            if (this.performGammaCorrection) {
                val = this.gammaLut[val];
            }
            pd[0] = val;
            pd[1] = val;
            pd[2] = val;
            if (val == this.grayTransparentAlpha) {
                pd[3] = 0;
            } else {
                pd[3] = this.maxOpacity;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;

    case POST_GRAY_LUT_ADD_TRANS_EXP:
        for (srcX = 0; srcX < width; srcX++) {
            src.getPixel(srcX, 0, ps);

            final int val = ps[0];
            final int val2 = this.grayLut[val];
            pd[0] = val2;
            pd[1] = val2;
            pd[2] = val2;
            if (val == this.grayTransparentAlpha) {
                pd[3] = 0;
            } else {
                pd[3] = this.maxOpacity;
            }

            dst.setPixel(dstX, y, pd);
            dstX += step;
        }
        break;
    }
}

From source file:org.geotools.gce.imagemosaic.ImageMosaicReaderTest.java

@Test
//    @Ignore/*ww w. ja v  a2 s  .  co  m*/
public void testRequestInHole() throws Exception {
    final AbstractGridFormat format = TestUtils.getFormat(rgbAURL);
    final ImageMosaicReader reader = TestUtils.getReader(rgbAURL, format);

    assertNotNull(reader);

    // ask to extract an area that is inside the coverage bbox, but in a hole (no data)
    final ParameterValue<GridGeometry2D> ggp = AbstractGridFormat.READ_GRIDGEOMETRY2D.createValue();
    Envelope2D env = new Envelope2D(reader.getCrs(), 500000, 3200000, 1000, 1000);
    GridGeometry2D gg = new GridGeometry2D(new GridEnvelope2D(0, 0, 100, 100), (Envelope) env);
    ggp.setValue(gg);

    // red background
    final ParameterValue<double[]> bgp = ImageMosaicFormat.BACKGROUND_VALUES.createValue();
    bgp.setValue(new double[] { 255, 0, 0, 255 });

    // read and check we actually got a coverage in the requested area
    GridCoverage2D coverage = reader.read(new GeneralParameterValue[] { ggp, bgp });
    assertNotNull(coverage);
    assertTrue(coverage.getEnvelope2D().intersects((Rectangle2D) env));

    // and that the color is the expected one given the background values provided
    RenderedImage ri = coverage.getRenderedImage();
    int[] pixel = new int[4];
    Raster tile = ri.getTile(ri.getMinTileX() + 1, ri.getMinTileY() + 1);
    tile.getPixel(tile.getMinX(), tile.getMinY(), pixel);
    assertEquals(255, pixel[0]);
    assertEquals(0, pixel[1]);
    assertEquals(0, pixel[2]);
    assertEquals(255, pixel[3]);
}