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:org.geotools.gce.imagemosaic.ImageMosaicReaderTest.java

@Test
//    @Ignore/*from  www  .jav a2 s  . c  o m*/
public void testRequestInOut() 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, so that the area is partly
    // inside the raster, and partly outside
    final ParameterValue<GridGeometry2D> ggp = AbstractGridFormat.READ_GRIDGEOMETRY2D.createValue();
    Envelope2D env = new Envelope2D(reader.getCrs(), 64887, 2499342, 646897 - 64887, 3155705 - 2499342);
    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);
    System.out.println(coverage.getEnvelope2D());
    System.out.println(env);
    assertTrue(coverage.getEnvelope2D().contains((Rectangle2D) env));

    // and that the color is the expected one given the background values provided
    RenderedImage ri = coverage.getRenderedImage();
    // ImageIO.write(ri, "PNG", new File("/tmp/mix.png"));
    System.out.println(ri.getNumXTiles());
    System.out.println(ri.getNumYTiles());
    int[] pixel = new int[4];
    Raster tile = ri.getTile(ri.getMinTileX() + ri.getNumXTiles() - 1,
            ri.getMinTileY() + ri.getNumYTiles() - 1);
    tile.getPixel(tile.getWidth() / 2, tile.getHeight() / 2, pixel);
    assertEquals(255, pixel[0]);
    assertEquals(0, pixel[1]);
    assertEquals(0, pixel[2]);
    assertEquals(255, pixel[3]);
}

From source file:org.geotools.process.raster.FootprintExtractionProcessTest.java

@Test
public void cloudExtractionWithoutDarkPixels() throws Exception {
    GeoTiffReader reader = null;/*from  w w w . ja v a  2  s.  c  om*/
    FeatureIterator<SimpleFeature> iter = null;
    GridCoverage2D cov = null;
    try {
        reader = new GeoTiffReader(cloudFile);
        cov = reader.read(null);

        // Exclude pixels with luminance less than 20.
        final int referenceLuminance = 10;
        List<Range<Integer>> exclusionRanges = Collections
                .singletonList(new Range<Integer>(Integer.class, 0, referenceLuminance));
        SimpleFeatureCollection fc = process.execute(cov, exclusionRanges, 10d, false, null, true, true, null,
                null);
        iter = fc.features();

        SimpleFeature feature = iter.next();
        Geometry geometry = (Geometry) feature.getDefaultGeometry();

        Raster raster = cov.getRenderedImage().getData();
        int[] darkPixel = new int[3];

        // These positions identify a couple of dark pixels of the cloud edge
        raster.getPixel(9, 13, darkPixel);
        double luminance = ImageUtilities.RGB_TO_GRAY_MATRIX[0][0] * darkPixel[0]
                + ImageUtilities.RGB_TO_GRAY_MATRIX[0][1] * darkPixel[1]
                + ImageUtilities.RGB_TO_GRAY_MATRIX[0][2] * darkPixel[2];
        assertTrue(luminance < referenceLuminance);

        raster.getPixel(15, 7, darkPixel);
        luminance = ImageUtilities.RGB_TO_GRAY_MATRIX[0][0] * darkPixel[0]
                + ImageUtilities.RGB_TO_GRAY_MATRIX[0][1] * darkPixel[1]
                + ImageUtilities.RGB_TO_GRAY_MATRIX[0][2] * darkPixel[2];
        assertTrue(luminance < referenceLuminance);

        // The computed polygon should have different shape due to dark pixels being excluded
        assertFalse(referenceGeometry.equalsExact(geometry, TOLERANCE));
    } finally {
        if (iter != null) {
            iter.close();
        }
        if (reader != null) {
            try {
                reader.dispose();
            } catch (Throwable t) {

            }
        }
        if (cov != null) {
            try {
                cov.dispose(true);
            } catch (Throwable t) {

            }
        }
    }
}

From source file:org.squidy.designer.util.ImageUtils.java

public static Shape getShapeOfImage(BufferedImage image) {
    // Get the data
    Raster data = image.getData();
    //// w  w  w  .ja va  2 s  .c om
    //      System.out.println("num of bands = " + data.getNumBands());
    // The colour of the pixel looking at
    // Shoulld have length of 4 (RGBA)
    int[] lookAt = null;
    // The map of all the points
    Point2D[][] pointMap = new Point2D[data.getWidth()][data.getHeight()];
    // The from point
    Point2D from = null;
    // The general path
    GeneralPath path = new GeneralPath();

    // Go round height
    for (int y = 0; y < data.getHeight(); y++) {
        // Go round width
        for (int x = 0; x < data.getWidth(); x++) {
            // Get the colour
            lookAt = data.getPixel(x, y, lookAt);
            // The alpha
            int a = lookAt[3];
            // If > then 0
            if (a > 0) {
                // Output 1
                //System.out.print(1);
                // Save point
                pointMap[x][y] = new Point2D.Double(x, y);

                if (from == null) {
                    from = pointMap[x][y];
                }
            } // 0
            else {
                // Output 0
                //System.out.print(0);
                // Nothing her
                pointMap[x][y] = null;
            }
        }
        // New line
        //System.out.println();
    }

    // Move it to the from
    if (from != null) {
        path.moveTo(from.getX(), from.getY());
        /*
         * Make the shape
         */
        // Go round height
        for (int y = 0; y < data.getHeight(); y++) {
            // Go round width
            for (int x = 0; x < data.getWidth(); x++) {
                // If the point is not null
                if (pointMap[x][y] != null) {
                    // Draw a line to
                    path.append(new Rectangle2D.Double(pointMap[x][y].getX(), pointMap[x][y].getY(), 1, 1),
                            true);
                    //               path.lineTo(pointMap[x][y].getX(), pointMap[x][y].getY());
                }
            }

        }
        path.closePath();
        // TODO: Put in the middle
        return path;
    }
    return null;
}