Example usage for java.awt.image Raster getWidth

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

Introduction

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

Prototype

public final int getWidth() 

Source Link

Document

Returns the width in pixels of the Raster.

Usage

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

/**
 * Compute single tile of the image/*from www.  j  a  v a2  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:org.squidy.designer.util.ImageUtils.java

public static Shape getShapeOfImage(BufferedImage image) {
    // Get the data
    Raster data = image.getData();
    ////from ww w .ja  v a  2  s. co m
    //      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;
}

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

/**
 * histogramByteHash -- compute a color histogram of an image
 *                      returned as a byte[]
 * @param img/*w  w  w  .ja  va  2 s  . c  o  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;
}

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   ww w .j  a v  a 2  s  . c om*/
    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();
}