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 x, int y, int rgb) 

Source Link

Document

Sets a pixel in this BufferedImage to the specified RGB value.

Usage

From source file:de._13ducks.cor.graphics.GraphicsComponent.java

public static BufferedImage grayScale(BufferedImage im) {
    // Verwandelt ein Bild ein eine Grayscale-Version, behlt aber die Transparenz
    BufferedImage grayImage = new BufferedImage(im.getWidth(), im.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < im.getWidth(); x++) {
        for (int y = 0; y < im.getHeight(); y++) {
            int argb = im.getRGB(x, y);
            int a = (argb >> 24) & 0xff;
            int r = (argb >> 16) & 0xff;
            int g = (argb >> 8) & 0xff;
            int b = (argb) & 0xff;
            int l = (int) (.299 * r + .587 * g + .114 * b); //luminance
            grayImage.setRGB(x, y, (a << 24) + (l << 16) + (l << 8) + l);
        }/*from w w  w.j a v a 2  s  . c o m*/

    }
    return grayImage;
}

From source file:net.ymate.framework.commons.QRCodeHelper.java

public BufferedImage toBufferedImage() {
    int width = __matrix.getWidth();
    int height = __matrix.getHeight();
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            image.setRGB(x, y, __matrix.get(x, y) ? BLACK : WHITE);
        }//from   w ww. j a  v a  2 s. c  o  m
    }
    return image;
}

From source file:de.darkblue.bongloader2.utils.ToolBox.java

public static BufferedImage grayScaleAlpha(BufferedImage original) {

    int alpha, red, green, blue;
    int newPixel;

    BufferedImage avg_gray = new BufferedImage(original.getWidth(), original.getHeight(), original.getType());
    int[] avgLUT = new int[766];
    for (int i = 0; i < avgLUT.length; i++) {
        avgLUT[i] = (int) (i / 3);
    }// w w w. j a  v  a2  s.c om

    for (int x = 0; x < original.getWidth(); x++) {
        for (int y = 0; y < original.getHeight(); y++) {

            // Get pixels by R, G, B
            int color = original.getRGB(x, y);
            alpha = color & 0xFF000000;

            red = (color >> 16) & 0xFF;
            green = (color >> 8) & 0xFF;
            blue = color & 0xFF;

            newPixel = red + green + blue;
            newPixel = avgLUT[newPixel];
            // Return back to original format
            newPixel = newPixel | (newPixel << 8) | (newPixel << 16) | alpha;

            // Write pixels into image
            avg_gray.setRGB(x, y, newPixel);

        }
    }

    return avg_gray;

}

From source file:it.units.malelab.ege.util.DUMapper.java

private static void modifyMap(String fileName, float bins) throws IOException {
    Color[][] colorMap = new Color[3][];
    colorMap[0] = new Color[] { fromCode("000000"), fromCode("b36600"), fromCode("f3b300") };
    colorMap[1] = new Color[] { fromCode("376387"), fromCode("b3b3b3"), fromCode("f3e6b3") };
    colorMap[2] = new Color[] { fromCode("509dc2"), fromCode("b4d3e1"), fromCode("f3f3f3") };
    BufferedImage inImage = ImageIO.read(new File(fileName));
    BufferedImage outRGDImage = new BufferedImage(inImage.getWidth(), inImage.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    BufferedImage outCMImage = new BufferedImage(inImage.getWidth(), inImage.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    for (int x = 0; x < inImage.getWidth(); x++) {
        for (int y = 0; y < inImage.getHeight(); y++) {
            Color inColor = new Color(inImage.getRGB(x, y));
            Color outColor = new Color(
                    Math.min((float) Math.floor((float) inColor.getRed() / 255f * bins) / (bins - 1), 1f),
                    Math.min((float) Math.floor((float) inColor.getGreen() / 255f * bins) / (bins - 1), 1f), 0);
            outRGDImage.setRGB(x, y, outColor.getRGB());
            int cmRIndex = (int) Math.min((int) Math.floor((float) inColor.getRed() / 255f * 3), 2);
            int cmGIndex = (int) Math.min((int) Math.floor((float) inColor.getGreen() / 255f * 3), 2);
            outColor = colorMap[cmRIndex][cmGIndex];
            outCMImage.setRGB(x, y, outColor.getRGB());
        }//  w  ww.  j a va  2 s  .c o m
    }
    ImageIO.write(outRGDImage, "PNG",
            new File(fileName.replace(".png", String.format(".rgbdisc%d.png", (int) bins))));
    ImageIO.write(outCMImage, "PNG", new File(fileName.replace(".png", ".cm.png")));
}

From source file:gr.iti.mklab.reveal.forensics.util.Util.java

public static BufferedImage visualizeWithJet(double[][] inputGrayImage) {
    // Take a [0,1] single-channel image and return a Jet visualization
    double[][] map = JetMap.colorMap;
    BufferedImage outIm = new BufferedImage(inputGrayImage.length, inputGrayImage[0].length, 5);
    Color rgb;//from ww w  . j  ava2s . c o  m
    byte bytevalue;
    for (int ii = 0; ii < inputGrayImage.length; ii++) {
        for (int jj = 0; jj < inputGrayImage[0].length; jj++) {
            bytevalue = (byte) Math.round(inputGrayImage[ii][jj] * 63);
            rgb = new Color((float) map[bytevalue][0], (float) map[bytevalue][1],
                    (float) map[(byte) Math.round(inputGrayImage[ii][jj]) * 63][2]);
            outIm.setRGB(ii, jj, rgb.getRGB());
        }
    }
    return outIm;
}

From source file:vazkii.botania.client.core.handler.MiscellaneousIcons.java

@SubscribeEvent
public void dumpAtlas(ArrowLooseEvent evt) {
    if (!evt.getEntityPlayer().worldObj.isRemote
            || !((Boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment"))
            || !evt.getEntityPlayer().isSneaking())
        return;/*w w w . j  a  v a 2  s.  co  m*/
    Minecraft.getMinecraft().renderEngine.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);

    int width = GL11.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 0, GL11.GL_TEXTURE_WIDTH);
    int height = GL11.glGetTexLevelParameteri(GL11.GL_TEXTURE_2D, 0, GL11.GL_TEXTURE_HEIGHT);

    Botania.LOGGER.debug("Dumped atlas %d wide by %d tall%n", width, height);

    int pixels = width * height;

    IntBuffer buffer = BufferUtils.createIntBuffer(pixels);
    int[] pixelValues = new int[pixels];

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

    GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, buffer);

    buffer.get(pixelValues);

    BufferedImage bufferedimage = new BufferedImage(width, height, 2);

    for (int k = 0; k < height; ++k) {
        for (int l = 0; l < width; ++l) {
            bufferedimage.setRGB(l, k, pixelValues[k * width + l]);
        }
    }

    File mcFolder = Minecraft.getMinecraft().mcDataDir;
    File result = new File(mcFolder, "atlas.png");

    try {
        ImageIO.write(bufferedimage, "png", result);
    } catch (IOException e) {
        Botania.LOGGER.warn("Failed to dump debug atlas");
    }
}

From source file:imageprocessingproject.ImageHistogram.java

public static BufferedImage normalizeImage(BufferedImage image) {
    int height = image.getHeight();
    int width = image.getWidth();

    int r, g, b, minr = 255, ming = 255, minb = 255, maxr = 0, maxg = 0, maxb = 0;
    Color c;//  w  w w. j a  va2  s .  co  m

    BufferedImage tempImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            c = new Color(image.getRGB(i, j));
            if (minr > c.getRed()) {
                minr = c.getRed();
            }
            if (ming > c.getGreen()) {
                ming = c.getGreen();
            }
            if (minb > c.getBlue()) {
                minb = c.getBlue();
            }
            if (maxr < c.getRed()) {
                maxr = c.getRed();
            }
            if (maxg < c.getGreen()) {
                maxg = c.getGreen();
            }
            if (maxb < c.getBlue()) {
                maxb = c.getBlue();
            }
        }
    }

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            c = new Color(image.getRGB(i, j));
            r = (int) ((c.getRed() - minr) * 255 / (maxr - minr));
            g = (int) ((c.getGreen() - ming) * 255 / (maxg - ming));
            b = (int) ((c.getBlue() - minb) * 255 / (maxb - minb));
            tempImage.setRGB(i, j, new Color(r, g, b, c.getAlpha()).getRGB());
        }
    }

    return tempImage;
}

From source file:it.units.malelab.ege.util.DUMapper.java

private static void saveImage(String fileName, boolean margin, int scale, double[][]... data) {
    BufferedImage bi = new BufferedImage(data[0].length * scale, data[0][0].length * scale,
            BufferedImage.TYPE_INT_ARGB);
    for (int y = 0; y < data[0].length; y++) {
        for (int x = 0; x < data[0][y].length; x++) {
            Color color;//from   w  w w  .  j  a v  a2  s  .  c  o  m
            if (data.length == 1) {
                color = new Color((float) data[0][y][x], (float) data[0][y][x], (float) data[0][y][x], 1);
            } else {
                color = new Color((float) data[0][y][x], (float) data[1][y][x],
                        data.length >= 3 ? (float) data[2][y][x] : 0,
                        data.length >= 4 ? (float) data[3][y][x] : 1);
            }
            if (scale == 1) {
                bi.setRGB(y, x, color.getRGB());
            } else {
                for (int ix = x * scale + (margin ? 1 : 0); ix < (x + 1) * scale - (margin ? 1 : 0); ix++) {
                    for (int iy = y * scale + (margin ? 1 : 0); iy < (y + 1) * scale - (margin ? 1 : 0); iy++) {
                        bi.setRGB(iy, ix, color.getRGB());
                    }
                }
            }
        }
    }
    try {
        ImageIO.write(bi, "PNG", new File(fileName));
    } catch (IOException ex) {
        System.err.printf("Cannot save file \"%s\": %s", fileName, ex.getMessage());
    }
}

From source file:ImageUtils.java

/**
 * Creates a <code>BufferedImage</code> from an <code>Image</code>. This method can
 * function on a completely headless system. This especially includes Linux and Unix systems
 * that do not have the X11 libraries installed, which are required for the AWT subsystem to
 * operate. The resulting image will be smoothly scaled using bilinear filtering.
 * /*from   ww  w . ja v  a2  s . co m*/
 * @param source The image to convert
 * @param w The desired image width
 * @param h The desired image height
 * @return The converted image
 * @param type  int
 */
public static BufferedImage createHeadlessSmoothBufferedImage(BufferedImage source, int type, int width,
        int height) {
    if (type == ImageUtils.IMAGE_PNG && hasAlpha(source)) {
        type = BufferedImage.TYPE_INT_ARGB;
    } else {
        type = BufferedImage.TYPE_INT_RGB;
    }

    BufferedImage dest = new BufferedImage(width, height, type);

    int sourcex;
    int sourcey;

    double scalex = (double) width / source.getWidth();
    double scaley = (double) height / source.getHeight();

    int x1;
    int y1;

    double xdiff;
    double ydiff;

    int rgb;
    int rgb1;
    int rgb2;

    for (int y = 0; y < height; y++) {
        sourcey = y * source.getHeight() / dest.getHeight();
        ydiff = scale(y, scaley) - sourcey;

        for (int x = 0; x < width; x++) {
            sourcex = x * source.getWidth() / dest.getWidth();
            xdiff = scale(x, scalex) - sourcex;

            x1 = Math.min(source.getWidth() - 1, sourcex + 1);
            y1 = Math.min(source.getHeight() - 1, sourcey + 1);

            rgb1 = getRGBInterpolation(source.getRGB(sourcex, sourcey), source.getRGB(x1, sourcey), xdiff);
            rgb2 = getRGBInterpolation(source.getRGB(sourcex, y1), source.getRGB(x1, y1), xdiff);

            rgb = getRGBInterpolation(rgb1, rgb2, ydiff);

            dest.setRGB(x, y, rgb);
        }
    }

    return dest;
}

From source file:io.gameover.utilities.pixeleditor.Frame.java

public BufferedImage getAsBufferedImage(Color transparent) {
    BufferedImage bi = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    for (int i = 0; i < bi.getWidth(); i++) {
        for (int j = 0; j < bi.getHeight(); j++) {
            if (transparent != null && argb[i][j] == NO_COLOR_AS_INT) {
                bi.setRGB(i, j, transparent.getRGB());
            } else {
                bi.setRGB(i, j, argb[i][j]);
            }//from   ww w  . j  a  v a  2s  .c om
        }
    }
    return bi;
}