Example usage for java.awt.image BufferedImage setRGB

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:vazkii.psi.client.core.helper.SharingHelper.java

public static String takeScreenshot() throws Exception {
    Minecraft mc = Minecraft.getMinecraft();

    ScaledResolution res = new ScaledResolution(mc);
    int screenWidth = mc.displayWidth;
    int screenHeight = mc.displayHeight;

    int scale = res.getScaleFactor();
    int width = 380 * scale;
    int height = 200 * scale;

    int left = screenWidth / 2 - width / 2;
    int top = screenHeight / 2 - height / 2;

    int i = width * height;

    if (pixelBuffer == null || pixelBuffer.capacity() < i) {
        pixelBuffer = BufferUtils.createIntBuffer(i);
        pixelValues = new int[i];
    }/*from   ww w . j  a  v a2 s .c o  m*/

    GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
    pixelBuffer.clear();

    GL11.glReadPixels(left, top, width, height, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV,
            (IntBuffer) pixelBuffer);

    pixelBuffer.get(pixelValues);
    TextureUtil.processPixelValues(pixelValues, width, height);
    BufferedImage bufferedimage = null;

    bufferedimage = new BufferedImage(width, height, 1);
    bufferedimage.setRGB(0, 0, width, height, pixelValues, 0, width);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ImageIO.write(bufferedimage, "png", stream);
    byte[] bArray = stream.toByteArray();
    String base64 = Base64.getEncoder().encodeToString(bArray);
    return base64;
}

From source file:com.xebia.visualreview.PixelComparator.java

public static DiffReport processImage(File beforeFile, File afterFile, java.util.Map maskInfo,
        String compareSettings) {
    try {//from  w  w w  .j  av a 2 s . c o m
        int precision = getPrecision(compareSettings);

        PixelGrabber beforeGrab = grabImage(beforeFile);
        PixelGrabber afterGrab = grabImage(afterFile);

        int[] beforeData = null;
        int[] afterData = null;
        int beforeWidth = 0;
        int afterWidth = 0;
        int y1 = 0;
        int y2 = 0;

        if (beforeGrab.grabPixels()) {
            beforeWidth = beforeGrab.getWidth();
            y1 = beforeGrab.getHeight();
            beforeData = (int[]) beforeGrab.getPixels();
        }

        if (afterGrab.grabPixels()) {
            afterWidth = afterGrab.getWidth();
            y2 = afterGrab.getHeight();
            afterData = (int[]) afterGrab.getPixels();
        }

        int minX = Math.min(beforeWidth, afterWidth);
        int minY = Math.min(y1, y2);
        int diffWidth = Math.max(beforeWidth, afterWidth);
        int diffHeight = Math.max(y1, y2);
        int[] diffData = new int[diffWidth * diffHeight];
        int differentPixels = 0;
        boolean hasMask = (maskInfo != null);
        BufferedImage maskImage = generateMask(maskInfo, diffWidth, diffHeight);
        for (int y = 0; y < diffHeight; y++) {
            for (int x = 0; x < diffWidth; x++) {
                if (maskImage.getRGB(x, y) != DIFFCOLOUR) {
                    if (x >= minX || y >= minY || hasDifference(beforeData[y * beforeWidth + x],
                            afterData[y * afterWidth + x], precision)) {
                        diffData[y * diffWidth + x] = DIFFCOLOUR;
                        differentPixels++;
                    }
                }
            }
        }

        BufferedImage diffImage = new BufferedImage(diffWidth, diffHeight, BufferedImage.TYPE_INT_ARGB);
        diffImage.setRGB(0, 0, diffWidth, diffHeight, diffData, 0, diffWidth);

        DiffReport report = new DiffReport(beforeFile, afterFile, diffImage, differentPixels);
        if (hasMask) {
            report.setMaskImage(maskImage);
        }
        return report;

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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());
    }/* w  w  w  .  j a  va2s  .  com*/
    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 BufferedImage generateBordersImage(BufferedImage source, int trimmedWidth, int trimmedHeight)
        throws IOException {
    BufferedImage finalBorder = UIUtil.createImage(trimmedWidth + 2, trimmedHeight + 2,
            BufferedImage.TYPE_INT_ARGB);
    int cutW = source.getWidth() - 2;
    int cutH = source.getHeight() - 2;

    // left border
    BufferedImage leftBorder = UIUtil.createImage(1, cutH, BufferedImage.TYPE_INT_ARGB);
    leftBorder.setRGB(0, 0, 1, cutH, source.getRGB(0, 1, 1, cutH, null, 0, 1), 0, 1);
    verifyBorderImage(leftBorder);//ww  w.  ja  v  a2  s  .c o m
    leftBorder = resizeBorder(leftBorder, 1, trimmedHeight);
    finalBorder.setRGB(0, 1, 1, trimmedHeight, leftBorder.getRGB(0, 0, 1, trimmedHeight, null, 0, 1), 0, 1);

    // right border
    BufferedImage rightBorder = UIUtil.createImage(1, cutH, BufferedImage.TYPE_INT_ARGB);
    rightBorder.setRGB(0, 0, 1, cutH, source.getRGB(cutW + 1, 1, 1, cutH, null, 0, 1), 0, 1);
    verifyBorderImage(rightBorder);
    rightBorder = resizeBorder(rightBorder, 1, trimmedHeight);
    finalBorder.setRGB(trimmedWidth + 1, 1, 1, trimmedHeight,
            rightBorder.getRGB(0, 0, 1, trimmedHeight, null, 0, 1), 0, 1);

    // top border
    BufferedImage topBorder = UIUtil.createImage(cutW, 1, BufferedImage.TYPE_INT_ARGB);
    topBorder.setRGB(0, 0, cutW, 1, source.getRGB(1, 0, cutW, 1, null, 0, cutW), 0, cutW);
    verifyBorderImage(topBorder);
    topBorder = resizeBorder(topBorder, trimmedWidth, 1);
    finalBorder.setRGB(1, 0, trimmedWidth, 1, topBorder.getRGB(0, 0, trimmedWidth, 1, null, 0, trimmedWidth), 0,
            trimmedWidth);

    // bottom border
    BufferedImage bottomBorder = UIUtil.createImage(cutW, 1, BufferedImage.TYPE_INT_ARGB);
    bottomBorder.setRGB(0, 0, cutW, 1, source.getRGB(1, cutH + 1, cutW, 1, null, 0, cutW), 0, cutW);
    verifyBorderImage(bottomBorder);
    bottomBorder = resizeBorder(bottomBorder, trimmedWidth, 1);
    finalBorder.setRGB(1, trimmedHeight + 1, trimmedWidth, 1,
            bottomBorder.getRGB(0, 0, trimmedWidth, 1, null, 0, trimmedWidth), 0, trimmedWidth);

    return finalBorder;
}

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

private static BufferedImage generateBordersImage(BufferedImage source, int trimmedWidth, int trimmedHeight)
        throws Wrong9PatchException, IOException {
    BufferedImage finalBorder = UIUtil.createImage(trimmedWidth + 2, trimmedHeight + 2,
            BufferedImage.TYPE_INT_ARGB);
    int cutW = source.getWidth() - 2;
    int cutH = source.getHeight() - 2;

    // left border
    BufferedImage leftBorder = UIUtil.createImage(1, cutH, BufferedImage.TYPE_INT_ARGB);
    leftBorder.setRGB(0, 0, 1, cutH, source.getRGB(0, 1, 1, cutH, null, 0, 1), 0, 1);
    verifyBorderImage(leftBorder);//from  w  ww .java2  s. co m
    leftBorder = resizeBorder(leftBorder, 1, trimmedHeight);
    finalBorder.setRGB(0, 1, 1, trimmedHeight, leftBorder.getRGB(0, 0, 1, trimmedHeight, null, 0, 1), 0, 1);

    // right border
    BufferedImage rightBorder = UIUtil.createImage(1, cutH, BufferedImage.TYPE_INT_ARGB);
    rightBorder.setRGB(0, 0, 1, cutH, source.getRGB(cutW + 1, 1, 1, cutH, null, 0, 1), 0, 1);
    verifyBorderImage(rightBorder);
    rightBorder = resizeBorder(rightBorder, 1, trimmedHeight);
    finalBorder.setRGB(trimmedWidth + 1, 1, 1, trimmedHeight,
            rightBorder.getRGB(0, 0, 1, trimmedHeight, null, 0, 1), 0, 1);

    // top border
    BufferedImage topBorder = UIUtil.createImage(cutW, 1, BufferedImage.TYPE_INT_ARGB);
    topBorder.setRGB(0, 0, cutW, 1, source.getRGB(1, 0, cutW, 1, null, 0, cutW), 0, cutW);
    verifyBorderImage(topBorder);
    topBorder = resizeBorder(topBorder, trimmedWidth, 1);
    finalBorder.setRGB(1, 0, trimmedWidth, 1, topBorder.getRGB(0, 0, trimmedWidth, 1, null, 0, trimmedWidth), 0,
            trimmedWidth);

    // bottom border
    BufferedImage bottomBorder = UIUtil.createImage(cutW, 1, BufferedImage.TYPE_INT_ARGB);
    bottomBorder.setRGB(0, 0, cutW, 1, source.getRGB(1, cutH + 1, cutW, 1, null, 0, cutW), 0, cutW);
    verifyBorderImage(bottomBorder);
    bottomBorder = resizeBorder(bottomBorder, trimmedWidth, 1);
    finalBorder.setRGB(1, trimmedHeight + 1, trimmedWidth, 1,
            bottomBorder.getRGB(0, 0, trimmedWidth, 1, null, 0, trimmedWidth), 0, trimmedWidth);

    return finalBorder;
}

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);/*from w ww  . j  av a  2 s .  c o  m*/
        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;//from  w w  w. j a  v  a  2  s . c o  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;
}

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

private static BufferedImage rescaleBorder(BufferedImage image, int targetWidth, int targetHeight) {
    if (targetWidth == 0) {
        targetWidth = 1;//  w  ww. j  a v  a  2 s .  c  o m
    }
    if (targetHeight == 0) {
        targetHeight = 1;
    }

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

    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;
}

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

public static BufferedImage resizeNinePatchImage(ImageInformation information) throws IOException {
    BufferedImage image = ImageIO.read(information.getImageFile());
    if (MathUtils.floatEquals(information.getFactor(), 1f)) {
        return image;
    }// ww  w .ja v a 2s  .  com

    BufferedImage trimmedImage = trim9PBorder(image);
    ImageInformation trimmedImageInformation = ImageInformation.newBuilder(information)
            .setExportName(getExportName("trimmed", information.getExportName())).build();
    saveImageTempFile(trimmedImage, trimmedImageInformation);
    trimmedImage = resizeNormalImage(trimmedImage, trimmedImageInformation);
    saveImageTempFile(trimmedImage, ImageInformation.newBuilder(trimmedImageInformation)
            .setExportName(getExportName("trimmedResized", information.getExportName())).build());

    BufferedImage borderImage;

    int w = trimmedImage.getWidth();
    int h = trimmedImage.getHeight();

    try {
        borderImage = generateBordersImage(image, w, h);
    } catch (Exception e) {
        return null;
    }

    int[] rgbArray = new int[w * h];
    trimmedImage.getRGB(0, 0, w, h, rgbArray, 0, w);
    borderImage.setRGB(1, 1, w, h, rgbArray, 0, w);

    return borderImage;
}

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

/**
 * Transform the colours in an image.//from  w w  w  . ja  v a 2 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();
}