Example usage for java.awt.image BufferedImage getRGB

List of usage examples for java.awt.image BufferedImage getRGB

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getRGB.

Prototype

public int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) 

Source Link

Document

Returns an array of integer pixels in the default RGB color model (TYPE_INT_ARGB) and default sRGB color space, from a portion of the image data.

Usage

From source file:dk.dma.msinm.web.wms.WmsProxyServlet.java

/**
 * Masks out white colour/*from  w  w  w .j a v a  2  s.c o  m*/
 * @param image the image to mask out
 * @return the resulting image
 */
private BufferedImage transformWhiteToTransparent(BufferedImage image) {

    BufferedImage dest = image;
    if (image.getType() != BufferedImage.TYPE_INT_ARGB) {
        dest = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = dest.createGraphics();
        g2.drawImage(image, 0, 0, null);
        g2.dispose();

        image.flush();
    }

    // Mask out the white pixels
    final int width = image.getWidth();
    int[] imgData = new int[width];

    for (int y = 0; y < dest.getHeight(); y++) {
        // fetch a line of data from each image
        dest.getRGB(0, y, width, 1, imgData, 0, 1);
        // apply the mask
        for (int x = 0; x < width; x++) {
            for (Color col : MASKED_COLORS) {
                int colDist = Math.abs(col.getRed() - (imgData[x] >> 16 & 0x000000FF))
                        + Math.abs(col.getGreen() - (imgData[x] >> 8 & 0x000000FF))
                        + Math.abs(col.getBlue() - (imgData[x] & 0x000000FF));
                if (colDist <= COLOR_DIST) {
                    imgData[x] = 0x00FFFFFF & imgData[x];
                }
            }
        }
        // replace the data
        dest.setRGB(0, y, width, 1, imgData, 0, 1);
    }
    return dest;
}

From source file:org.niord.web.wms.WmsProxyServlet.java

/**
 * Masks out white colour// w  w w .  j av a2s  .c o m
 * @param image the image to mask out
 * @return the resulting image
 */
@SuppressWarnings("unused")
private BufferedImage transformWhiteToTransparent(BufferedImage image) {

    BufferedImage dest = image;
    if (image.getType() != BufferedImage.TYPE_INT_ARGB) {
        dest = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = dest.createGraphics();
        g2.drawImage(image, 0, 0, null);
        g2.dispose();

        image.flush();
    }

    // Mask out the white pixels
    final int width = image.getWidth();
    int[] imgData = new int[width];

    for (int y = 0; y < dest.getHeight(); y++) {
        // fetch a line of data from each image
        dest.getRGB(0, y, width, 1, imgData, 0, 1);
        // apply the mask
        for (int x = 0; x < width; x++) {
            for (Color col : MASKED_COLORS) {
                int colDist = Math.abs(col.getRed() - (imgData[x] >> 16 & 0x000000FF))
                        + Math.abs(col.getGreen() - (imgData[x] >> 8 & 0x000000FF))
                        + Math.abs(col.getBlue() - (imgData[x] & 0x000000FF));
                if (colDist <= COLOR_DIST) {
                    imgData[x] = 0x00FFFFFF & imgData[x];
                }
            }
        }
        // replace the data
        dest.setRGB(0, y, width, 1, imgData, 0, 1);
    }
    return dest;
}

From source file:org.pentaho.reporting.libraries.base.util.ResourceBundleSupport.java

/**
 * Creates a transparent image.  These can be used for aligning menu items.
 *
 * @param width  the width./*w w w.  j a v a 2s.c o  m*/
 * @param height the height.
 * @return the created transparent image.
 */
private BufferedImage createTransparentImage(final int width, final int height) {
    final BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final int[] data = img.getRGB(0, 0, width, height, null, 0, width);
    img.setRGB(0, 0, width, height, data, 0, width);
    return img;
}

From source file:oct.analysis.application.OCTSelection.java

public final List<LinePoint> getLrpAcrossOCT(BufferedImage oct) {
    LinkedList<LinePoint> lrp = new LinkedList<>();
    int leftEdge = getSelectionLeftEdgeCoordinate();

    //iterate over each row of pixels across the OCT and calculate average pixel intensity
    for (int y = oct.getHeight() - 1; y >= 0; y--) {
        int yVal = y;
        //calculate average pixel grayscale intensity
        int avgReflectivity = (int) Math
                .round(Arrays.stream(oct.getRGB(leftEdge, yVal, width, 1, null, 0, width))
                        .map(Util::calculateGrayScaleValue).average().getAsDouble());
        //add LRP value to return series
        lrp.add(new LinePoint(avgReflectivity, y));
    }//from  w  w w .jav a  2  s. c o  m

    return lrp;
}

From source file:oct.analysis.application.OCTSelection.java

public final XYSeries getLrpSeriesFromOCT(BufferedImage oct) {
    XYSeries lrp = new XYSeries(selectionName + " LRP");
    lrp.setKey(selectionName);/*  w w w. j av a  2s . c  o  m*/

    int leftEdge = getSelectionLeftEdgeCoordinate();

    double value = -1;
    //iterate over each row of pixels in the selection area and calculate average pixel intensity
    for (int y = yPositionOnOct + height - 1; y >= yPositionOnOct; y--) {
        int yVal = y;
        //calculate average pixel grayscale intensity
        double curPixelIntensity = Arrays.stream(oct.getRGB(leftEdge, yVal, width, 1, null, 0, width))
                .map(Util::calculateGrayScaleValue).average().getAsDouble();
        //smooth the LRP to provide a higher quality LRP signal
        if (value <= -1) {
            //initialize the first value for the smoothing filter
            value = curPixelIntensity;
        } else {
            //smooth the LRP signal
            value += ((curPixelIntensity - value) / selMngr.getLrpSmoothingFactor());
        }
        //add LRP value to return series
        lrp.add(oct.getHeight() - y, value);
    }

    return lrp;
}

From source file:me.Wundero.Ray.utils.TextUtils.java

private static void computeCharWidths() throws IOException {
    InputStream iStream = TextUtils.class.getResourceAsStream("ascii.png");
    BufferedImage img = ImageIO.read(iStream);
    iStream.close();//from w  w w  .j a  va 2s  .  c o  m
    int width = img.getWidth();
    int height = img.getHeight();
    int[] imgData = new int[width * height];
    img.getRGB(0, 0, width, height, imgData, 0, width);
    int charH = height / 16;
    int charW = width / 16;
    float lvt_9_1_ = 8.0F / (float) charW;
    for (int idx = 0; idx < 256; ++idx) {
        if (idx == 32) {
            ASCII_PNG_CHAR_WIDTHS[idx] = 4;
            continue;
        }
        int col = idx % 16;
        int row = idx / 16;
        int offX;
        for (offX = charW - 1; offX >= 0; --offX) {
            int imgX = col * charW + offX;
            boolean hasValue = true;
            for (int offY = 0; offY < charH && hasValue; ++offY) {
                int imgY = (row * charW + offY) * width;
                if ((imgData[imgX + imgY] >> 24 & 255) != 0) {
                    hasValue = false;
                }
            }
            if (!hasValue) {
                break;
            }
        }
        ++offX;
        ASCII_PNG_CHAR_WIDTHS[idx] = (int) (0.5D + (double) ((float) offX * lvt_9_1_)) + 1;
    }
}

From source file:ResourceBundleSupport.java

/**
 * Creates a transparent image.  These can be used for aligning menu items.
 *
 * @param width  the width./*  ww  w. j a v  a  2 s. co  m*/
 * @param height the height.
 * @return the created transparent image.
 */
private BufferedImage createTransparentImage(final int width, final int height) {
    final BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final int[] data = img.getRGB(0, 0, width, height, null, 0, width);
    Arrays.fill(data, 0x00000000);
    img.setRGB(0, 0, width, height, data, 0, width);
    return img;
}