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

public static void swapColors(BufferedImage img, Color... mapping) {
    int[] pixels = img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth());
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    for (int i = 0; i < mapping.length / 2; i++) {
        map.put(mapping[2 * i].getRGB(), mapping[2 * i + 1].getRGB());
    }//from  ww  w .  j a v  a 2s .c o  m
    for (int i = 0; i < pixels.length; i++) {
        if (map.containsKey(pixels[i]))
            pixels[i] = map.get(pixels[i]);
    }

    img.setRGB(0, 0, img.getWidth(), img.getHeight(), pixels, 0, img.getWidth());
}

From source file:de.mprengemann.intellij.plugin.androidicons.util.ImageUtils.java

private static void verifyBorderImage(BufferedImage border) throws IOException {
    int[] rgb = border.getRGB(0, 0, border.getWidth(), border.getHeight(), null, 0, border.getWidth());
    for (int aRgb : rgb) {
        if ((0xff000000 & aRgb) != 0) {
            if (aRgb != 0xff000000 && aRgb != 0xffff0000) {
                throw new IOException();
            }/*ww w .  java  2s  . c o m*/
        }
    }
}

From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java

private static void verifyBorderImage(BufferedImage border) throws Wrong9PatchException {
    int[] rgb = border.getRGB(0, 0, border.getWidth(), border.getHeight(), null, 0, border.getWidth());
    for (int aRgb : rgb) {
        if ((0xff000000 & aRgb) != 0) {
            if (aRgb != 0xff000000 && aRgb != 0xffff0000) {
                throw new Wrong9PatchException();
            }/*from  w  w w  .  j  a v  a 2  s .  c  om*/
        }
    }
}

From source file:com.htmlhifive.pitalium.core.io.FilePersisterTest.java

static int[] toRGB(BufferedImage image) {
    return image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
}

From source file:Main.java

private static IntBuffer getImageAsARGBIntBuffer(BufferedImage image) {
    DataBuffer buffer = image.getRaster().getDataBuffer();

    if (buffer instanceof DataBufferInt) {
        return IntBuffer.wrap(((DataBufferInt) buffer).getData());
    } else if (buffer instanceof DataBufferByte) {
        return ByteBuffer.wrap(((DataBufferByte) buffer).getData()).order(ByteOrder.BIG_ENDIAN).asIntBuffer();
    } else {//from ww w. j a v  a2 s .c  o  m
        int width = image.getWidth();
        int height = image.getHeight();
        int[] pixels = new int[width * height];
        image.getRGB(0, 0, width, height, pixels, 0, width);
        return IntBuffer.wrap(pixels);
    }
}

From source file:qupath.lib.algorithms.color.EstimateStainVectors.java

public static ColorDeconvolutionStains estimateStains(final BufferedImage img,
        final ColorDeconvolutionStains stainsOriginal, final double minStain, final double maxStain,
        final double ignorePercentage, final boolean checkColors) {
    int[] buf = img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth());
    int[] rgb = buf;

    float[] red = ColorDeconvolutionHelper.getRedOpticalDensities(rgb, stainsOriginal.getMaxRed(), null);
    float[] green = ColorDeconvolutionHelper.getGreenOpticalDensities(rgb, stainsOriginal.getMaxGreen(), null);
    float[] blue = ColorDeconvolutionHelper.getBlueOpticalDensities(rgb, stainsOriginal.getMaxBlue(), null);

    return estimateStains(buf, red, green, blue, stainsOriginal, minStain, maxStain, ignorePercentage,
            checkColors);/*from www. j  a  va2  s  .  co  m*/
}

From source file:com.oneis.utils.ImageColouring.java

/**
 * Transform the colours in an image./*from  www.  j  a  v  a2 s .  c  om*/
 *
 * @param Filename Full pathname to the source file
 * @param Method Which colouring method to use, use METHOD_* constants
 * @param Colours Array of 0xRRGGBB colours for recolouring
 * @param JPEGQuality If the image is a JPEG, the output quality for
 * reencoding
 * @param Output An OutputStream to write the file. Will be closed.
 */
static public void colourImage(String Filename, int Method, int[] Colours, int JPEGQuality, OutputStream Output)
        throws IOException {
    ColouringInfo colouringInfo = prepareForColouring(Method, Colours);

    String extension = Filename.substring(Filename.lastIndexOf('.') + 1, Filename.length());
    String outputFormat = extension;

    if (outputFormat.equals("gif")) {
        if (!colourImageGIF(Filename, colouringInfo, Output)) {
            throw new RuntimeException("Failed to directly colour GIF file");
        }
        return;
    }

    BufferedImage image = ImageIO.read(new File(Filename));
    int width = image.getWidth();
    int height = image.getHeight();

    // Rewrite the pixels
    int[] pixelBuffer = new int[width];
    for (int y = 0; y < height; ++y) {
        image.getRGB(0, y, width, 1, pixelBuffer, 0, width);
        doColouring(pixelBuffer, colouringInfo);
        image.setRGB(0, y, width, 1, pixelBuffer, 0, width);
    }

    // Writing is done the hard way to enable the JPEG quality to be set.
    // Get a writer
    Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(outputFormat);
    if (!iter.hasNext()) {
        throw new RuntimeException("Couldn't write image of type " + outputFormat);
    }

    ImageWriter writer = iter.next();

    ImageWriteParam iwparam = null;

    // Is it JPEG? If so, might want to set the quality
    if (outputFormat.equals("jpeg") || outputFormat.equals("jpg")) {
        // Clamp value
        int q = JPEGQuality;
        if (q < 10) {
            q = 10;
        }
        if (q > 100) {
            q = 100;
        }

        JPEGImageWriteParam jparam = new JPEGImageWriteParam(null);
        jparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        jparam.setCompressionQuality((float) (((float) q) / 100.0));
        iwparam = jparam;
    }

    ImageOutputStream output = ImageIO.createImageOutputStream(Output);
    writer.setOutput(output);
    writer.write(null, new IIOImage(image, null, null), iwparam);

    output.close();
}

From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java

private static void enforceBorderColors(BufferedImage inputImage) {
    Graphics2D g = inputImage.createGraphics();
    g.setBackground(new Color(0, 0, 0, 0));
    g.clearRect(1, 1, inputImage.getWidth() - 2, inputImage.getHeight() - 2);
    g.dispose();/*  w  w  w  .j a v  a 2 s .  com*/
    int w = inputImage.getWidth();
    int h = inputImage.getHeight();
    int[] rgb = new int[w * h];

    inputImage.getRGB(0, 0, w, h, rgb, 0, w);

    for (int i = 0; i < rgb.length; i++) {
        if ((0xff000000 & rgb[i]) != 0) {
            rgb[i] = 0xff000000;
        }
    }
    inputImage.setRGB(0, 0, w, h, rgb, 0, w);
}

From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java

private static BufferedImage resizeBorder(final BufferedImage border, int targetWidth, int targetHeight)
        throws IOException {
    if (targetWidth > border.getWidth() || targetHeight > border.getHeight()) {
        BufferedImage endImage = rescaleBorder(border, targetWidth, targetHeight);
        enforceBorderColors(endImage);//  w w w  .j  a  v  a 2 s . c om
        return endImage;
    }

    int w = border.getWidth();
    int h = border.getHeight();
    int[] data = border.getRGB(0, 0, w, h, null, 0, w);
    int[] newData = new int[targetWidth * targetHeight];

    float widthRatio = (float) Math.max(targetWidth - 1, 1) / (float) Math.max(w - 1, 1);
    float heightRatio = (float) Math.max(targetHeight - 1, 1) / (float) Math.max(h - 1, 1);

    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            if ((0xff000000 & data[y * w + x]) != 0) {
                int newX = Math.min(Math.round(x * widthRatio), targetWidth - 1);
                int newY = Math.min(Math.round(y * heightRatio), targetHeight - 1);
                newData[newY * targetWidth + newX] = data[y * w + x];
            }
        }
    }

    BufferedImage img = UIUtil.createImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
    img.setRGB(0, 0, targetWidth, targetHeight, newData, 0, targetWidth);

    return img;
}

From source file:de.mprengemann.intellij.plugin.androidicons.util.ImageUtils.java

private static BufferedImage rescaleBorder(BufferedImage image, int targetWidth, int targetHeight)
        throws IOException {
    if (targetWidth == 0) {
        targetWidth = 1;/*ww w  .  jav a2  s .co m*/
    }
    if (targetHeight == 0) {
        targetHeight = 1;
    }

    if (targetHeight > 1 && targetWidth > 1) {
        throw new IOException();
    }

    int w = image.getWidth();
    int h = image.getHeight();
    int[] data = image.getRGB(0, 0, w, h, null, 0, w);
    int[] newData = new int[targetWidth * targetHeight];

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            newData[y * targetWidth + x] = 0x00;
        }
    }

    List<Integer> startPositions = new ArrayList<Integer>();
    List<Integer> endPositions = new ArrayList<Integer>();

    boolean inBlock = false;
    if (targetHeight == 1) {
        for (int x = 0; x < w; x++) {
            if ((0xff000000 & data[x]) != 0) {
                if (!inBlock) {
                    inBlock = true;
                    startPositions.add(x);
                }
            } else if (inBlock) {
                endPositions.add(x - 1);
                inBlock = false;
            }
        }
        if (inBlock) {
            endPositions.add(w - 1);
        }
    } else {
        for (int y = 0; y < h; y++) {
            if ((0xff000000 & data[y]) != 0) {
                if (!inBlock) {
                    inBlock = true;
                    startPositions.add(y);
                }
            } else if (inBlock) {
                endPositions.add(y - 1);
                inBlock = false;
            }
        }
        if (inBlock) {
            endPositions.add(h - 1);
        }
    }
    try {
        SplineInterpolator interpolator = new SplineInterpolator();
        PolynomialSplineFunction function = interpolator.interpolate(
                new double[] { 0f, 1f, Math.max(w - 1, h - 1) },
                new double[] { 0f, 1f, Math.max(targetHeight - 1, targetWidth - 1) });
        for (int i = 0; i < startPositions.size(); i++) {
            int start = startPositions.get(i);
            int end = endPositions.get(i);
            for (int j = (int) function.value(start); j <= (int) function.value(end); j++) {
                newData[j] = 0xff000000;
            }
        }

        BufferedImage img = UIUtil.createImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        img.setRGB(0, 0, targetWidth, targetHeight, newData, 0, targetWidth);
        return img;
    } catch (Exception e) {
        Logger.getInstance(ImageUtils.class).error("resizeBorder", e);
    }

    return null;
}