Example usage for java.awt.image Raster getSample

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

Introduction

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

Prototype

public int getSample(int x, int y, int b) 

Source Link

Document

Returns the sample in a specified band for the pixel located at (x,y) as an int.

Usage

From source file:fr.gael.drb.cortex.topic.sentinel3.jai.operator.QuicklookSlstrRIF.java

private BufferedImage toGrayScale(Raster in, PixelCorrection c, boolean invertColors, double lowerBound,
        double upperBound) {
    double offset = -lowerBound;
    double scaleFactor = 256. / (upperBound - lowerBound);
    int width = in.getWidth();
    int height = in.getHeight();

    // generate/*from   w w w. j  a v  a  2s  .c  o m*/
    BufferedImage out = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            int pixel = checkAndApplyCorrection(in.getSample(i, j, 0), c);
            if (pixel == c.nodata) {
                if (invertColors)
                    out.setRGB(i, j, new Color(255, 255, 255).getRGB());
                else
                    out.setRGB(i, j, new Color(0, 0, 0).getRGB());
                continue;
            }

            double normalized = (pixel + offset) * scaleFactor;
            int gray = (int) (Math.max(0, Math.min(255, normalized)));
            if (invertColors)
                gray = 255 - gray;
            out.setRGB(i, j, new Color(gray, gray, gray).getRGB());
        }
    }
    return out;
}

From source file:org.openqa.selenium.TakesScreenshotTest.java

private Set<String> getColors(BufferedImage image, final int stepX, final int stepY) throws IOException {
    Set<String> colors = new TreeSet<String>();

    int height = image.getHeight();
    int width = image.getWidth();
    assertTrue(width > 0);/*www .  j a  v  a2  s . co m*/
    assertTrue(height > 0);

    Raster raster = image.getRaster();
    String hex = "";
    int color = 0;
    for (int i = 0; i < width; i = i + stepX) {
        for (int j = 0; j < height; j = j + stepY) {
            hex = String.format("#%02x%02x%02x", (raster.getSample(i, j, 0)), (raster.getSample(i, j, 1)),
                    (raster.getSample(i, j, 2)));
            colors.add(hex);
        }
    }
    return colors;
}

From source file:org.geoserver.wps.gs.raster.algebra.JiffleScriptProcessTest.java

/**
 * Private method for ensuring the validity of the output image.
 * /*from   w w  w .  jav  a  2s. c  o m*/
 * @param outputImage RenderedImage extracted from the output coverage
 * @param inputCoverages Input Coverages used.
 * @param values
 */
private void checkExecution(RenderedImage outputImage, int[] values, GridCoverage2D... inputCoverages) {

    RenderedImage inputImage = inputCoverages[0].getRenderedImage();

    int numBands = outputImage.getSampleModel().getNumBands();

    int minTileX = outputImage.getMinTileX();
    int minTileY = outputImage.getMinTileY();
    int maxTileX = outputImage.getNumXTiles() + minTileX;
    int maxTileY = outputImage.getNumYTiles() + minTileY;

    int minX;
    int minY;
    int maxX;
    int maxY;

    Raster inputTile;
    Raster outputTile;

    int inputValue;
    int outputValue;
    // Cycle on each tile
    int valueOver0;

    for (int b = 0; b < numBands; b++) {

        valueOver0 = values[b];

        for (int xTile = minTileX; xTile < maxTileX; xTile++) {
            for (int yTile = minTileY; yTile < maxTileY; yTile++) {

                inputTile = inputImage.getTile(xTile, yTile);
                outputTile = outputImage.getTile(xTile, yTile);

                minX = inputTile.getMinX();
                minY = inputTile.getMinY();

                maxX = inputTile.getWidth() + minX;
                maxY = inputTile.getHeight() + minY;
                // Cycle on the x axis
                for (int x = minX; x < maxX; x++) {
                    // Cycle on the y axis
                    for (int y = minY; y < maxY; y++) {
                        inputValue = inputTile.getSample(x, y, b);
                        outputValue = outputTile.getSample(x, y, b);
                        // Check if the script operation is performed correctly
                        if (inputValue > 0) {
                            assertEquals(outputValue, valueOver0);
                        } else {
                            assertEquals(outputValue, LESS_THAN_ZERO_BAND_0);
                        }
                    }
                }
            }
        }
    }
}

From source file:org.apache.fop.render.pcl.PCLGenerator.java

/**
 * Paint a bitmap at the current cursor position. The bitmap must be a monochrome
 * (1-bit) bitmap image./*from  w  w  w .  j  a  v  a2 s.  c o m*/
 * @param img the bitmap image (must be 1-bit b/w)
 * @param resolution the resolution of the image (must be a PCL resolution)
 * @throws IOException In case of an I/O error
 */
public void paintMonochromeBitmap(RenderedImage img, int resolution) throws IOException {
    if (!isValidPCLResolution(resolution)) {
        throw new IllegalArgumentException("Invalid PCL resolution: " + resolution);
    }
    boolean monochrome = isMonochromeImage(img);
    if (!monochrome) {
        throw new IllegalArgumentException("img must be a monochrome image");
    }

    setRasterGraphicsResolution(resolution);
    writeCommand("*r0f" + img.getHeight() + "t" + img.getWidth() + "s1A");
    Raster raster = img.getData();

    Encoder encoder = new Encoder(img);
    // Transfer graphics data
    int imgw = img.getWidth();
    IndexColorModel cm = (IndexColorModel) img.getColorModel();
    if (cm.getTransferType() == DataBuffer.TYPE_BYTE) {
        DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
        MultiPixelPackedSampleModel packedSampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
                img.getWidth(), img.getHeight(), 1);
        if (img.getSampleModel().equals(packedSampleModel) && dataBuffer.getNumBanks() == 1) {
            //Optimized packed encoding
            byte[] buf = dataBuffer.getData();
            int scanlineStride = packedSampleModel.getScanlineStride();
            int idx = 0;
            int c0 = toGray(cm.getRGB(0));
            int c1 = toGray(cm.getRGB(1));
            boolean zeroIsWhite = c0 > c1;
            for (int y = 0, maxy = img.getHeight(); y < maxy; y++) {
                for (int x = 0, maxx = scanlineStride; x < maxx; x++) {
                    if (zeroIsWhite) {
                        encoder.add8Bits(buf[idx]);
                    } else {
                        encoder.add8Bits((byte) ~buf[idx]);
                    }
                    idx++;
                }
                encoder.endLine();
            }
        } else {
            //Optimized non-packed encoding
            for (int y = 0, maxy = img.getHeight(); y < maxy; y++) {
                byte[] line = (byte[]) raster.getDataElements(0, y, imgw, 1, null);
                for (int x = 0, maxx = imgw; x < maxx; x++) {
                    encoder.addBit(line[x] == 0);
                }
                encoder.endLine();
            }
        }
    } else {
        //Safe but slow fallback
        for (int y = 0, maxy = img.getHeight(); y < maxy; y++) {
            for (int x = 0, maxx = imgw; x < maxx; x++) {
                int sample = raster.getSample(x, y, 0);
                encoder.addBit(sample == 0);
            }
            encoder.endLine();
        }
    }

    // End raster graphics
    writeCommand("*rB");
}