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:examples.utils.CifarReader.java

public static BufferedImage getImageFromArray(double[] pixels, int width, int height) {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    for (int i = 0; i < pixels.length / IMAGE_DEPTH; ++i) {
        int rgb = new Color(Double.valueOf(pixels[i]).intValue(), Double.valueOf(pixels[i + 1024]).intValue(),
                Double.valueOf(pixels[i + 2048]).intValue()).getRGB();
        image.setRGB(i % IMAGE_WIDTH, i / IMAGE_HIGHT, rgb);
    }//from  www  . j a va2s . c  om
    return image;
}

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. This method uses nearest neighbor approximation, so it's quite fast. Unfortunately,
 * the result is nowhere near as nice looking as the createHeadlessSmoothBufferedImage method.
 * //w w  w  . ja  v a 2 s .  c o m
 * @param image  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 createHeadlessBufferedImage(BufferedImage image, int type, int width, int height) {
    if (type == ImageUtils.IMAGE_PNG && hasAlpha(image)) {
        type = BufferedImage.TYPE_INT_ARGB;
    } else {
        type = BufferedImage.TYPE_INT_RGB;
    }

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

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            bi.setRGB(x, y, image.getRGB(x * image.getWidth() / width, y * image.getHeight() / height));
        }
    }

    return bi;
}

From source file:ImageProc.java

public static void imagesc(File file, int[][] classificationMat, int nClass, int imgDim1, int imgDim2) {
    BufferedImage image = new BufferedImage(imgDim2, imgDim1, BufferedImage.TYPE_INT_RGB);
    int index, rgb;
    float[][] jet = colormapJet(nClass);
    for (int i = 0; i < imgDim1; i++) {
        for (int j = 0; j < imgDim2; j++) {
            index = classificationMat[i][j];
            if (index == -1) {
                image.setRGB(j, i, Color.BLACK.getRGB());
            } else {
                rgb = new Color(jet[index][0], jet[index][1], jet[index][2]).getRGB();
                image.setRGB(j, i, rgb);
            }//from   w w w  .  j  a v  a  2 s  . com
        }
    }
    try {
        ImageIO.write(image, "png", file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.bund.bfr.jung.JungUtils.java

private static Paint mixWith(Paint paint, Color mix) {
    if (paint instanceof Color) {
        Color c = (Color) paint;

        return new Color((c.getRed() + mix.getRed()) / 2, (c.getGreen() + mix.getGreen()) / 2,
                (c.getBlue() + mix.getBlue()) / 2, (c.getAlpha() + mix.getAlpha()) / 2);
    } else if (paint instanceof TexturePaint) {
        BufferedImage texture = ((TexturePaint) paint).getImage();
        BufferedImage mixed = new BufferedImage(texture.getWidth(), texture.getHeight(), texture.getType());

        for (int x = 0; x < texture.getWidth(); x++) {
            for (int y = 0; y < texture.getHeight(); y++) {
                mixed.setRGB(x, y, ((Color) mixWith(new Color(texture.getRGB(x, y)), mix)).getRGB());
            }//from w w w  .j a v  a2s. co  m
        }

        return new TexturePaint(mixed, new Rectangle(mixed.getWidth(), mixed.getHeight()));
    } else {
        return paint;
    }
}

From source file:org.uva.itast.blended.omr.BufferedImageUtil.java

/**
 * Dibuja de una marca de color blanco o negro en un BufferedImage
 * //w ww  . j  av a 2  s . c o m
 * @param img
 * @param x
 * @param y
 * @param color
 */
public static void putMarkBufferedImage(BufferedImage img, int x, int y, boolean color) {
    if (color) {
        img.setRGB(x, y, Color.BLACK.getRGB());
        img.setRGB(x + 1, y + 1, Color.BLACK.getRGB());
        img.setRGB(x - 1, y - 1, Color.BLACK.getRGB());
        img.setRGB(x + 1, y, Color.BLACK.getRGB());
        img.setRGB(x - 1, y, Color.BLACK.getRGB());
        img.setRGB(x, y + 1, Color.BLACK.getRGB());
        img.setRGB(x, y - 1, Color.BLACK.getRGB());
        img.setRGB(x + 1, y - 1, Color.BLACK.getRGB());
        img.setRGB(x - 1, y + 1, Color.BLACK.getRGB());
        img.setRGB(x - 1, y - 1, Color.BLACK.getRGB());
    } else {
        img.setRGB(x, y, Color.WHITE.getRGB());
        img.setRGB(x + 1, y + 1, Color.WHITE.getRGB());
        img.setRGB(x - 1, y - 1, Color.WHITE.getRGB());
        img.setRGB(x + 1, y, Color.WHITE.getRGB());
        img.setRGB(x - 1, y, Color.WHITE.getRGB());
        img.setRGB(x, y + 1, Color.WHITE.getRGB());
        img.setRGB(x, y - 1, Color.WHITE.getRGB());
        img.setRGB(x + 1, y - 1, Color.WHITE.getRGB());
        img.setRGB(x - 1, y + 1, Color.WHITE.getRGB());
        img.setRGB(x - 1, y - 1, Color.WHITE.getRGB());
    }

}

From source file:net.cloudkit.relaxation.CaptchaTest.java

public static void clearBorder(BufferedImage image) {
    final int width = image.getWidth();
    final int height = image.getHeight();

    // image.getMinX() image.getMinY()
    // /* w ww  .  j ava  2 s.com*/
    for (int x = 0; x < width; x++) {
        image.setRGB(x, 0, Color.WHITE.getRGB());
    }

    // ?
    for (int y = 0; y < height; y++) {
        image.setRGB(width - 1, y, Color.WHITE.getRGB());
    }

    // 
    for (int x = 0; x < width; x++) {
        image.setRGB(x, height - 1, Color.WHITE.getRGB());
    }

    // 
    for (int y = 0; y < height; y++) {
        image.setRGB(0, y, Color.WHITE.getRGB());
    }
}

From source file:com.iiitd.qre.CreateZSignedQR.java

public static BufferedImage toBufferedImage(ByteMatrix matrix) {
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int scale = 6;
    int margin = 3;
    BufferedImage image = new BufferedImage(scale * (width + 2 * margin), scale * (height + 2 * margin),
            BufferedImage.TYPE_INT_ARGB);

    int imgX, imgY = margin * scale;
    for (int y = 0; y < height; y++) {
        for (int i = 0; i < scale; ++i) {
            imgX = margin * scale;/*from   w w  w . j  a  va 2  s .co m*/
            for (int x = 0; x < width; x++) {
                for (int j = 0; j < scale; ++j) {
                    if (matrix.get(x, y) != 0)
                        image.setRGB(imgX, imgY, BLACK);
                    ++imgX;
                }
            }
            ++imgY;
        }
    }
    return image;
}

From source file:net.team2xh.crt.gui.util.GUIToolkit.java

/**
 * Provides a ImageIcon from a file name.
 *
 * @param path Picture file name/*from   w  w  w.j av a  2  s  .c o  m*/
 * @return     ImageIcon
 */
public static Image getIcon(String path) {
    Image img = null;
    Theme theme = Theme.getTheme();
    try {
        URI uri = theme.getClass().getResource(path).toURI();
        File file = new File(uri);
        img = ImageIO.read(file);

    } catch (IOException ex) {
        Logger.getLogger(GUIToolkit.class.getName()).log(Level.SEVERE, null, ex);
    } catch (URISyntaxException ex) {
        Exceptions.printStackTrace(ex);
    }

    if (img != null) {
        if (theme.getClass() == DarkTheme.class) {
            // If dark theme, invert colors
            // http://stackoverflow.com/a/25425107
            BufferedImage bi = (BufferedImage) img;
            for (int y = 0; y < bi.getHeight(); ++y) {
                for (int x = 0; x < bi.getWidth(); ++x) {
                    int c = bi.getRGB(x, y);
                    bi.setRGB(x, y, 0x00ffffff - (c | 0xff000000) | (c & 0xff000000));
                }
            }
        }
    }

    return img;
}

From source file:ImageProcessing.ImageProcessing.java

public static void processFlipVertical(BufferedImage image) {
    //Flips image vertically.
    for (int i = 0; i < image.getHeight() / 2; i++) {
        for (int j = 0; j < image.getWidth(); j++) {
            int upper_rgb = image.getRGB(j, i);
            int below_rgb = image.getRGB(j, image.getHeight() - (i + 1));
            image.setRGB(j, i, below_rgb);
            image.setRGB(j, image.getHeight() - (i + 1), upper_rgb);
        }/*from ww  w  . ja v  a2 s  .  co  m*/
    }
}

From source file:ImageProcessing.ImageProcessing.java

public static void processFlipHorizontal(BufferedImage image) {
    //Flips image horizontally.
    for (int i = 0; i < image.getHeight(); i++) {
        for (int j = 0; j < image.getWidth() / 2; j++) {
            int left_rgb = image.getRGB(j, i);
            int right_rgb = image.getRGB(image.getWidth() - (j + 1), i);

            image.setRGB(j, i, right_rgb);
            image.setRGB(image.getWidth() - (j + 1), i, left_rgb);
        }/*from   www  . j  a va  2 s.  co m*/
    }
}