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:Main.java

public static double meanValue(BufferedImage image) {
    Raster raster = image.getRaster();
    double sum = 0.0;

    for (int y = 0; y < image.getHeight(); ++y) {
        for (int x = 0; x < image.getWidth(); ++x) {
            sum += raster.getSample(x, y, 0);
        }/* w w  w  .  j a  v a  2s.c om*/
    }
    return sum / (image.getWidth() * image.getHeight());
}

From source file:tooltip.ImageComparison.java

public static void setUp() throws Exception {
    boolean ret = true;
    System.out.println("Inside Setup C:\\Comcast Project Docs\\Automation\\CAAP AUTOMATION\\Selenium");
    //System.setProperty("webdriver.chrome.driver","C:\\Users\\ajavva001c\\Downloads\\chromedriver.exe");

    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("https://activator-web-qaauto.g1.app.cloud.comcast.net/Activate/comFlow");
    File url = new File("C:/Users/ajavva001c/HSD/unpacked.png");
    FileInputStream fi = new FileInputStream(url);
    BufferedImage bufImgOne = ImageIO.read(fi);
    String s1 = driver.findElement(By.xpath("//*[@id='responsive']/div/div/div[2]/div/ul/li[2]/img"))
            .getAttribute("src");
    URL urls = new URL(s1);
    System.out.println(urls);/*from   w w  w  .j  av a  2s . co  m*/
    BufferedImage bufImgOne1 = ImageIO.read(urls);
    Raster image = bufImgOne.getData();
    Raster image1 = bufImgOne1.getData();
    if (image.getNumBands() != image1.getNumBands() && image.getWidth() != image1.getWidth()
            && image.getHeight() != image1.getHeight()) {
        ret = false;
        System.out.println("fail");
    } else {
        search: for (int i = 0; i < image.getNumBands(); ++i) {
            for (int x = 0; x < image.getWidth(); ++x) {
                for (int y = 0; y < image.getHeight(); ++y) {
                    if (image.getSample(x, y, i) != image1.getSample(x, y, i)) {
                        ret = false;
                        break search;
                    }

                }
            }
        }
        System.out.println(ret);
    }
    driver.quit();
}

From source file:org.mrgeo.image.ImageStats.java

/**
 * Deserialize a Raster into an array of ImageStats objects. Used to reduce tile stats emitted by
 * a mapper.//from ww  w  .ja v  a  2  s . co  m
 * 
 * @param raster
 *          the raster containing stats measures as pixel values
 * @return an array of ImageStats objects
 */
static public ImageStats[] rasterToStats(final Raster raster) {
    final int bands = raster.getHeight();
    final ImageStats[] stats = initializeStatsArray(bands);
    for (int i = 0; i < bands; i++) {
        stats[i].min = raster.getSampleDouble(0, i, 0);
        stats[i].max = raster.getSampleDouble(1, i, 0);
        stats[i].sum = raster.getSampleDouble(2, i, 0);
        stats[i].count = raster.getSample(3, i, 0);
    }
    return stats;
}

From source file:it.tidalwave.imageio.test.ImageReaderTestSupport.java

/*******************************************************************************************************************
 *
 *
 ******************************************************************************************************************/
public static MessageDigest md5(final @Nonnull Raster raster) throws NoSuchAlgorithmException {
    final MessageDigest md5 = MessageDigest.getInstance("MD5");

    for (int b = 0; b < raster.getNumBands(); b++) {
        for (int y = 0; y < raster.getHeight(); y++) {
            for (int x = 0; x < raster.getWidth(); x++) {
                final int sample = raster.getSample(x, y, b) & 0xffff;
                md5.update((byte) ((sample >>> 24) & 0xff));
                md5.update((byte) ((sample >>> 16) & 0xff));
                md5.update((byte) ((sample >>> 8) & 0xff));
                md5.update((byte) ((sample >>> 0) & 0xff));
            }//  www.  ja v  a 2 s.  c  o m
        }
    }

    return md5;
}

From source file:it.tidalwave.imageio.test.ImageReaderTestSupport.java

/*******************************************************************************************************************
 *
 *
 ******************************************************************************************************************/
public static void dumpRasterAsText(final @Nonnull Raster raster, final @Nonnull PrintWriter pw) {
    final int width = raster.getWidth();
    final int height = raster.getHeight();
    final int bandCount = raster.getNumBands();
    logger.fine("Dumping raster %d x %d x %d", width, height, bandCount);

    for (int y = 0; y < height; y++) {
        for (int b = 0; b < bandCount; b++) {
            pw.printf("y=%04d b=%1d : ", y, b);

            for (int x = 0; x < width; x++) {
                final int sample = raster.getSample(x, y, b) & 0xffff;
                pw.printf("%04x ", sample);
            }/* www  .  j ava2  s.  co  m*/

            pw.println();
        }
    }
}

From source file:org.mrgeo.image.ImageStats.java

/**
 * Computes pixel value statistics: min, max, sum, count, & mean for a Raster and returns an array
 * of ImageStats objects, one for each band in the image.
 * /*from  w w w.j  a  va  2  s  .  c om*/
 * @param raster
 *          the raster to compute stats for
 * @param nodata
 *          the value to ignore
 * @return an array of ImageStats objects
 */
static public void computeAndUpdateStats(final ImageStats[] tileStats, final Raster raster,
        final double[] nodata) throws RasterWritableException {
    final int type = raster.getTransferType();
    Number sample;
    for (int y = 0; y < raster.getHeight(); y++) {
        for (int x = 0; x < raster.getWidth(); x++) {
            for (int b = 0; b < raster.getNumBands(); b++) {
                switch (type) {
                case DataBuffer.TYPE_BYTE:
                case DataBuffer.TYPE_INT:
                case DataBuffer.TYPE_SHORT:
                case DataBuffer.TYPE_USHORT:
                    sample = raster.getSample(x, y, b);
                    break;
                case DataBuffer.TYPE_FLOAT:
                    sample = raster.getSampleFloat(x, y, b);
                    break;
                case DataBuffer.TYPE_DOUBLE:
                    sample = raster.getSampleDouble(x, y, b);
                    break;
                default:
                    throw new RasterWritableException(
                            "Error computing tile statistics. Unsupported raster data type");
                }
                updateStats(tileStats[b], sample, nodata[b]);
            }
        }
    }

}

From source file:org.xlrnet.tibaija.tools.fontgen.FontgenApplication.java

private Symbol importFile(Path path, Font font, String fontIdentifier) throws IOException, ImageReadException {
    LOGGER.info("Importing file {} ...", path.toAbsolutePath());

    BufferedImage image = Imaging.getBufferedImage(Files.newInputStream(path));
    int width = image.getWidth();
    int height = image.getHeight();
    int finalWidth = width / 2;
    int finalHeight = height / 2;

    if (width % 2 != 0 || height % 2 != 0) {
        LOGGER.warn("Width and height must be multiple of 2");
        return null;
    }/*from   w  ww . ja  v a 2s. c  o m*/

    Symbol symbol = new Symbol();
    PixelState[][] pixelStates = new PixelState[finalHeight][finalWidth];
    Raster imageData = image.getData();

    for (int y = 0; y < finalHeight; y++) {
        for (int x = 0; x < finalWidth; x++) {
            int sample = imageData.getSample(x * 2, y * 2, 0);
            PixelState pixelState = sample == 0 ? PixelState.ON : PixelState.OFF;
            pixelStates[y][x] = pixelState;
        }
    }

    symbol.setData(pixelStates);
    return symbol;
}

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

private BufferedImage toGrayScale(Raster in, PixelCorrection c, boolean invertColors, boolean ignoreBadStats) {
    int width = in.getWidth();
    int height = in.getHeight();
    // compute stats
    SummaryStatistics stats = new SummaryStatistics();
    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)
                stats.addValue(pixel);/*from www . ja  v a2s .c o m*/
        }
    }
    double lowerBound = Math.max(stats.getMin(), stats.getMean() - 3 * stats.getStandardDeviation());
    double upperBound = Math.min(stats.getMax(), stats.getMean() + 3 * stats.getStandardDeviation());

    if (!ignoreBadStats)
        if (Double.isNaN(stats.getMean()) || Double.isNaN(stats.getStandardDeviation())
                || stats.getStandardDeviation() < 1)
            throw new IllegalStateException("Ugly band stats. Acquired during night?");

    return toGrayScale(in, c, invertColors, lowerBound, upperBound);
}

From source file:org.photovault.image.RawConvOpImage.java

/**
 * Compute single tile of the image//from w  w  w.j  av a  2 s . c o  m
 * @param x
 * @param y
 * @return
 */
@Override
public Raster computeTile(int x, int y) {
    if (contrastLut == null) {
        createLumLut();
    }
    Raster r = source.getTile(x, y);
    WritableRaster w = r.createCompatibleWritableRaster(r.getMinX(), r.getMinY(), r.getWidth(), r.getHeight());
    int startX = r.getMinX();
    int startY = r.getMinY();
    for (int l = startY; l < startY + r.getHeight(); l++) {
        for (int c = startX; c < startX + r.getWidth(); c++) {
            long sr = r.getSample(c, l, 0);
            long sg = r.getSample(c, l, 1);
            long sb = r.getSample(c, l, 2);
            long avg = (sr + sg + sb) / 3;
            long m = contrastLut[(int) avg];
            long[] pixel = new long[3];
            pixel[0] = (sr * m) >> 16;
            pixel[1] = (sg * m) >> 16;
            pixel[2] = (sb * m) >> 16;
            long clippedSum = 0;
            long totalHeadroom = 0;
            boolean clipped[] = new boolean[3];
            int channelsClipped = 0;
            int channelsUnclipped = 0;
            for (int n = 0; n < 3; n++) {
                if (pixel[n] > 65535) {
                    channelsClipped++;
                    clipped[n] = true;
                    clippedSum += pixel[n] - 65535;
                } else {
                    clipped[n] = false;
                    totalHeadroom += 65536 - pixel[n];
                    channelsUnclipped++;
                }
            }
            if (channelsClipped > 0) {
                for (int n = 0; n < 3; n++) {
                    if (!clipped[n]) {
                        // Spread the clipped energy to other channels so that
                        // they reach saturation at the same time
                        long headroom = 65536 - pixel[n];
                        pixel[n] += clippedSum * headroom / totalHeadroom;
                    }
                }
            }
            //                while ( channelsClipped > 0 && clippedSum > 0 &&
            //                        channelsUnclipped > 0 ) {
            //                    long spreaded = 0;
            //                    long spreadPerChan = clippedSum / channelsUnclipped +1;
            //                    for ( int n = 0; n < 3; n++ ) {
            //                        if ( !clipped[n] ) {
            //                            long add = Math.min( spreadPerChan, 65536 - pixel[n] );
            //                            pixel[n] += add;
            //                            spreaded += add;
            //                            if ( pixel[n] > 65535 ) {
            //                                channelsUnclipped--;
            //                            }
            //                        }
            //                    }
            //                    clippedSum -= spreaded;
            //                }
            try {
                w.setSample(c, l, 0, Math.min(65535, pixel[0]));
                w.setSample(c, l, 1, Math.min(65535, pixel[1]));
                w.setSample(c, l, 2, Math.min(65535, pixel[2]));
            } catch (ArrayIndexOutOfBoundsException e) {
                log.error(e);
            }
        }
    }
    return w;
}

From source file:haven.Utils.java

public static BufferedImage outline(BufferedImage img, Color col) {
    Coord sz = imgsz(img).add(2, 2);// w  w w .j a  v a  2s.com
    BufferedImage ol = TexI.mkbuf(sz);
    Object fcol = ol.getColorModel().getDataElements(col.getRGB(), null);
    Raster src = img.getRaster();
    WritableRaster dst = ol.getRaster();
    for (int y = 0; y < sz.y; y++) {
        for (int x = 0; x < sz.x; x++) {
            boolean t;
            if ((y == 0) || (x == 0) || (y == sz.y - 1) || (x == sz.x - 1)) {
                t = true;
            } else {
                t = src.getSample(x - 1, y - 1, 3) < 250;
            }
            if (!t)
                continue;
            if (((x > 1) && (y > 0) && (y < sz.y - 1) && (src.getSample(x - 2, y - 1, 3) >= 250))
                    || ((x > 0) && (y > 1) && (x < sz.x - 1) && (src.getSample(x - 1, y - 2, 3) >= 250))
                    || ((x < sz.x - 2) && (y > 0) && (y < sz.y - 1) && (src.getSample(x, y - 1, 3) >= 250))
                    || ((x > 0) && (y < sz.y - 2) && (x < sz.x - 1) && (src.getSample(x - 1, y, 3) >= 250)))
                dst.setDataElements(x, y, fcol);
        }
    }
    return (ol);
}