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:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage flipImageVertical(BufferedImage img) {
    if (img == null) {
        return null;
    }//from  w  w  w . j  av  a2s  . co  m

    if (img.getType() == 0) {
        img = convertToARGB(img);
    }
    BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
    for (int y = 0; y < out.getHeight(); y++) {
        for (int x = 0; x < out.getWidth(); x++) {
            out.setRGB(x, y, img.getRGB(x, img.getHeight() - y - 1));
        }
    }
    return out;
}

From source file:com.xuggle.xuggler.UtilsTest.java

@SuppressWarnings("deprecation")
@Test//w  ww.j  a va2  s  .  c om
public void testPictureToPictureWithRotate() {
    // note that the image is square in this test to make rotation
    // easier to handle

    int size = 50;
    int black = Color.BLACK.getRGB();
    int white = Color.WHITE.getRGB();

    // construct an image with black and white stripped columns

    BufferedImage image1 = new BufferedImage(size, size, BufferedImage.TYPE_3BYTE_BGR);
    for (int x = 0; x < size; ++x)
        for (int y = 0; y < size; ++y) {
            int color = x % 2 == 0 ? black : white;
            image1.setRGB(x, y, color);
        }

    // convert image1 to a picture and then back to image2

    BufferedImage image2 = Utils.videoPictureToImage(Utils.imageToVideoPicture(image1, 0));

    // rotae image2

    AffineTransform t = AffineTransform.getRotateInstance(Math.PI / 2, image2.getWidth() / 2,
            image2.getHeight() / 2);
    AffineTransformOp ato = new AffineTransformOp(t, AffineTransformOp.TYPE_BICUBIC);
    BufferedImage image3 = new BufferedImage(size, size, BufferedImage.TYPE_3BYTE_BGR);
    ato.filter(image2, image3);

    // convert image2 to a picture and then back to image3

    BufferedImage image4 = Utils.videoPictureToImage(Utils.imageToVideoPicture(image3, 0));

    // test that image4 now contains stripped rows (not columns)

    for (int x = 0; x < size; ++x)
        for (int y = 0; y < size; ++y) {
            int pixel = image4.getRGB(x, y);
            int color = y % 2 == 0 ? black : white;
            assertTrue("color value missmatch", pixel == color);
        }
}

From source file:edu.umn.cs.spatialHadoop.visualization.FrequencyMap.java

public BufferedImage asImage() {
    if (min >= max) {
        // Values not set. Autodetect
        min = Float.MAX_VALUE;/*  w w  w .  j a  v  a  2 s .c  o m*/
        max = -Float.MAX_VALUE;
        for (int x = 0; x < this.getWidth(); x++) {
            for (int y = 0; y < this.getHeight(); y++) {
                if (frequencies[x][y] < min)
                    min = frequencies[x][y];
                if (frequencies[x][y] > max)
                    max = frequencies[x][y];
            }
        }
    }
    BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    for (int x = 0; x < this.getWidth(); x++) {
        for (int y = 0; y < this.getHeight(); y++) {
            Color color = calculateColor(frequencies[x][y], min, max);
            image.setRGB(x, y, color.getRGB());
        }
    }
    return image;
}

From source file:com.xuggle.xuggler.UtilsTest.java

@SuppressWarnings("deprecation")
@Test//from w  w w.ja  v  a 2 s  . co m
public void testImageToImageRandomColor() {
    int w = 50;
    int h = 50;
    Random rnd = new Random();

    // construct an image of random colors

    BufferedImage image1 = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
    for (int x = 0; x < w; ++x)
        for (int y = 0; y < h; ++y) {
            Color c = new Color(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255));
            image1.setRGB(x, y, c.getRGB());
        }

    // convert image1 to a picture and then back to image2

    BufferedImage image2 = Utils.videoPictureToImage(Utils.imageToVideoPicture(image1, 0));

    // test that all the pixels in image2 are the same as image1

    for (int x = 0; x < w; ++x)
        for (int y = 0; y < h; ++y) {
            int pixel1 = image1.getRGB(x, y);
            int pixel2 = image2.getRGB(x, y);
            assertTrue("color value missmatch", pixel1 == pixel2);
        }
}

From source file:fr.gael.drb.cortex.topic.sentinel3.jai.operator.QuicklookSlstrRIF.java

/**
 * Compute the natural color QL from passed images. This method only 
 * assembles provided images as red/green/blue 8 bits channels.
 * @param red red channel image/*from   www.j a  va2 s.  c om*/
 * @param green green channel image
 * @param blue blue channel image
 * @return the assembled image.
 */
private RenderedImage naturalColors(Raster red, PixelCorrection rc, Raster green, PixelCorrection gc,
        Raster blue, PixelCorrection bc) {

    BufferedImage bred = toGrayScale(red, rc, false, false);
    BufferedImage bgreen = toGrayScale(green, gc, false, false);
    BufferedImage bblue = toGrayScale(blue, bc, false, false);

    BufferedImage quicklook = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_RGB);

    for (int j = 0; j < red.getHeight(); j++) {
        for (int i = 0; i < red.getWidth(); i++) {
            int cred = new Color(bred.getRGB(i, j)).getRed();
            int cgreen = new Color(bgreen.getRGB(i, j)).getGreen();
            int cblue = new Color(bblue.getRGB(i, j)).getBlue();

            quicklook.setRGB(i, j, new Color(cred, cgreen, cblue).getRGB());
        }
    }

    return quicklook;
}

From source file:algorithm.ImageImageFrameExpanding.java

/**
 * Colorises the image. (The metadata image is blue.)
 *//*from  w  w  w .  java2 s  .com*/
private void colorizeImage(BufferedImage image, int color) {
    for (int x = 0; x < image.getWidth(); x++) {
        for (int y = 0; y < METADATA_HEIGHT; y++) {
            image.setRGB(x, y, color);
        }
    }
}

From source file:com.hygenics.imaging.ImageCleanup.java

public byte[] reaverage(double factor, byte[] ibytes) {
    // tone red coloring
    BufferedImage color = BufferImage(ibytes);

    if (color != null) {
        BufferedImage averaged = new BufferedImage(color.getWidth(), color.getHeight(),
                BufferedImage.TYPE_INT_RGB);

        for (int i = 0; i < color.getWidth(); i++) {
            for (int j = 0; j < color.getHeight(); j++) {
                Color c = new Color(color.getRGB(i, j));

                averaged.setRGB(i, j,
                        new Color((int) Math.round(c.getRed() / factor), c.getGreen(), c.getBlue()).getRGB());
            }/*from  ww w. j a va 2s .c o  m*/
        }

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        // convert to jpeg for compression
        try {
            // use apache to read to a buffered image with certain metadata
            // and then convert to a jpg
            ImageIO.write(averaged, "jpg", bos);
            return ibytes = bos.toByteArray();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return ibytes;

}

From source file:org.csml.tommo.sugar.heatmap.MeanQualityMatrix.java

public BufferedImage createBufferedImage(ColorPaintScale scale) {

    BufferedImage bi = new BufferedImage(N, N, BufferedImage.TYPE_INT_RGB);

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            Color c = scale.getPaint(getMeanValues()[i][j]);
            bi.setRGB(i, N - 1 - j, c.getRGB());
        }/*  w  w  w  .j a  v  a  2s.c o  m*/
    }

    return bi;
}

From source file:haven.Utils.java

static void drawgay(BufferedImage t, BufferedImage img, Coord c) {
    Coord sz = imgsz(img);/*from w ww. ja  va  2 s  . c  om*/
    for (int y = 0; y < sz.y; y++) {
        for (int x = 0; x < sz.x; x++) {
            int p = img.getRGB(x, y);
            if (Utils.rgbm.getAlpha(p) > 128) {
                if ((p & 0x00ffffff) == 0x00ff0080)
                    t.setRGB(x + c.x, y + c.y, 0);
                else
                    t.setRGB(x + c.x, y + c.y, p);
            }
        }
    }
}

From source file:net.rptools.lib.image.ImageUtil.java

public static BufferedImage createOutline(BufferedImage sourceImage, Color color) {
    if (sourceImage == null) {
        return null;
    }/*from  www.  jav  a2 s.c  o m*/
    BufferedImage image = new BufferedImage(sourceImage.getWidth() + 2, sourceImage.getHeight() + 2,
            Transparency.BITMASK);

    for (int row = 0; row < image.getHeight(); row++) {
        for (int col = 0; col < image.getWidth(); col++) {
            int sourceX = col - 1;
            int sourceY = row - 1;

            // Pixel under current location
            if (sourceX >= 0 && sourceY >= 0 && sourceX <= sourceImage.getWidth() - 1
                    && sourceY <= sourceImage.getHeight() - 1) {
                int sourcePixel = sourceImage.getRGB(sourceX, sourceY);
                if (sourcePixel >> 24 != 0) {
                    // Not an empty pixel, don't overwrite it
                    continue;
                }
            }
            for (int i = 0; i < outlineNeighborMap.length; i++) {
                int[] neighbor = outlineNeighborMap[i];
                int x = sourceX + neighbor[0];
                int y = sourceY + neighbor[1];

                if (x >= 0 && y >= 0 && x <= sourceImage.getWidth() - 1 && y <= sourceImage.getHeight() - 1) {
                    if ((sourceImage.getRGB(x, y) >> 24) != 0) {
                        image.setRGB(col, row, color.getRGB());
                        break;
                    }
                }
            }
        }
    }
    return image;
}