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:com.googlecode.fightinglayoutbugs.helpers.ImageHelper.java

public static BufferedImage pixelsToImage(int[][] pixels) {
    if (pixels == null) {
        return null;
    }/*from w  ww  .  j av  a2s. c  o  m*/
    int w = pixels.length;
    if (w > 0) {
        int h = pixels[0].length;
        if (h > 0) {
            BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < w; ++x) {
                int[] column = pixels[x];
                for (int y = 0; y < h; ++y) {
                    image.setRGB(x, y, column[y]);
                }
            }
            return image;
        } else {
            throw new IllegalArgumentException("pixels[0].length must not be 0.");
        }
    } else {
        throw new IllegalArgumentException("pixels.length must not be 0.");
    }
}

From source file:com.googlecode.fightinglayoutbugs.helpers.ImageHelper.java

public static BufferedImage pixelsToImage(boolean[][] pixels) {
    if (pixels == null) {
        return null;
    }//from  w ww.  j av  a2s .c  o m
    int w = pixels.length;
    if (w > 0) {
        int h = pixels[0].length;
        if (h > 0) {
            BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < w; ++x) {
                boolean[] column = pixels[x];
                for (int y = 0; y < h; ++y) {
                    image.setRGB(x, y, column[y] ? 0 : 0xFFFFFF);
                }
            }
            return image;
        } else {
            throw new IllegalArgumentException("pixels[0].length must not be 0.");
        }
    } else {
        throw new IllegalArgumentException("pixels.length must not be 0.");
    }
}

From source file:ImageProcessing.ImageProcessing.java

public static void processGrayscale(BufferedImage image) {
    //Converts a color image to a Grayscale image.
    for (int i = 0; i < image.getHeight(); i++) {
        for (int j = 0; j < image.getWidth(); j++) {
            int rgb = image.getRGB(j, i);
            Color currentPixel = new Color(rgb, true);
            //Find the average from all the color components for the given pixel.
            int grayLevel = (currentPixel.getRed() + currentPixel.getGreen() + currentPixel.getBlue()) / 3;

            int gray = (grayLevel << 16) + (grayLevel << 8) + grayLevel;
            image.setRGB(j, i, gray);
        }/*from   w w w .  j a v a  2s.  com*/
    }
}

From source file:nl.b3p.kaartenbalie.service.KBImageTool.java

/** Reads an image from an http input stream.
 *
 * @param is Inputstream/*from   w  ww  .j a v a2 s . c om*/
 * @param mime String representing the mime type of the image.
 *
 * @return BufferedImage
 *
 * @throws Exception
 */
// <editor-fold defaultstate="" desc="readImage(GetMethod method, String mime) method.">
public static BufferedImage readImage(InputStream is, String mime, ServiceProviderRequest wmsRequest)
        throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int bytesRead = 0;
    byte[] buffer = new byte[2048];
    while (bytesRead != -1) {
        bytesRead = is.read(buffer, 0, buffer.length);
        if (bytesRead > 0)
            baos.write(buffer, 0, bytesRead);
    }

    if (mime.indexOf(";") != -1) {
        mime = mime.substring(0, mime.indexOf(";"));
    }
    String mimeType = getMimeType(mime);
    if (mimeType == null) {
        String message = baos.toString();
        message = message.replaceAll("(\\r|\\n)", "");
        log.error("Response from server not understood (mime = " + mime + "): " + message);
        throw new Exception("Response from server not understood (mime = " + mime + "): " + message);
    }

    ImageReader ir = getReader(mimeType);
    if (ir == null) {
        log.error("no reader available for imageformat: " + mimeType.substring(mimeType.lastIndexOf("/") + 1));
        throw new Exception(
                "no reader available for imageformat: " + mimeType.substring(mimeType.lastIndexOf("/") + 1));
    }

    //TODO Make smarter.. Possibly faster... But keep reporting!
    wmsRequest.setBytesReceived(new Long(baos.size()));
    ImageInputStream stream = ImageIO.createImageInputStream(new ByteArrayInputStream(baos.toByteArray()));
    ir.setInput(stream, true);
    try {
        //if image is a png, has no alpha and has a tRNS then make that color transparent.
        BufferedImage i = ir.read(0);
        if (!i.getColorModel().hasAlpha() && ir.getImageMetadata(0) instanceof PNGMetadata) {
            PNGMetadata metadata = (PNGMetadata) ir.getImageMetadata(0);
            if (metadata.tRNS_present) {
                int alphaPix = (metadata.tRNS_red << 16) | (metadata.tRNS_green << 8) | (metadata.tRNS_blue);
                BufferedImage tmp = new BufferedImage(i.getWidth(), i.getHeight(), BufferedImage.TYPE_INT_ARGB);
                for (int x = 0; x < i.getWidth(); x++) {
                    for (int y = 0; y < i.getHeight(); y++) {
                        int rgb = i.getRGB(x, y);
                        rgb = (rgb & 0xFFFFFF) == alphaPix ? alphaPix : rgb;
                        tmp.setRGB(x, y, rgb);
                    }
                }
                i = tmp;
            }
        }
        return i;
    } finally {
        ir.dispose();
    }
}

From source file:nl.b3p.imagetool.ImageTool.java

/**
 *
 *//*from w w  w.  ja v  a  2 s  .c o m*/
public static BufferedImage changeColor(BufferedImage im, Color color, Color newColor) {
    for (int x = 0; x < im.getWidth(); x++) {
        for (int y = 0; y < im.getHeight(); y++) {
            if (im.getRGB(x, y) == color.getRGB()) {
                im.setRGB(x, y, newColor.getRGB());
            }
        }
    }
    return im;
}

From source file:Main.java

/**
 * Rotates given image by given degrees which should be a multiple of 90
 * @param source image to be rotated/*  w  w w.  j ava 2s  .c om*/
 * @param degrees the angle by which to rotate, should be a multiple of 90
 * @return the rotated image
 */
public static BufferedImage rotateByRightAngle(BufferedImage source, int degrees) {
    assert degrees % 90 == 0;
    degrees = degrees % 360;

    int w = source.getWidth();
    int h = source.getHeight();
    int w1, h1;
    switch (degrees) {
    case 90:
    case 270:
        w1 = h;
        h1 = w;
        break;
    default:
        w1 = w;
        h1 = h;
    }
    BufferedImage rotated = new BufferedImage(w1, h1, source.getType());

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            int v = source.getRGB(x, y);
            int x1, y1;
            switch (degrees) {
            case 90:
                x1 = h - y - 1;
                y1 = x;
                break;
            case 180:
                x1 = w - x - 1;
                y1 = h - y - 1;
                break;
            case 270:
                x1 = y;
                y1 = w - x - 1;
                break;
            default:
                x1 = x;
                y1 = y;
                break;
            }

            rotated.setRGB(x1, y1, v);
        }
    }

    return rotated;
}

From source file:com.uksf.mf.core.utility.loaders.ImageLoad.java

/**
 * Changes colour of all non-transparent pixels for given image to given colour
 * @param image - image to change colours in
 * @param newColour colour to change to/*from w ww . j  a v a  2 s .c o m*/
 * @return image with changed colours
 */
private static ImageIcon changeImageColour(ImageIcon image, int newColour) {
    LogHandler.logSeverity(INFO, "Converting image: " + image + " colour to: " + newColour);
    BufferedImage bufferedImage = new BufferedImage(image.getIconWidth(), image.getIconHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = bufferedImage.createGraphics();
    image.paintIcon(null, graphics, 0, 0);
    graphics.dispose();
    for (int x = 0; x < bufferedImage.getWidth(); x++) {
        for (int y = 0; y < bufferedImage.getHeight(); y++) {
            int colour = bufferedImage.getRGB(x, y);
            colour = (((colour >> 24) & 0xff) << 24) | (((colour & 0x00ff0000) >> 16) << 16)
                    | (((colour & 0x0000ff00) >> 8) << 8) | (colour & 0x000000ff);
            if (colour != COLOUR_TRANSPARENT.getRGB()) {
                newColour = (((colour >> 24) & 0xff) << 24) | (((newColour & 0x00ff0000) >> 16) << 16)
                        | (((newColour & 0x0000ff00) >> 8) << 8) | (newColour & 0x000000ff);
                bufferedImage.setRGB(x, y, newColour);
            }
        }
    }
    return new ImageIcon(bufferedImage);
}

From source file:de.bund.bfr.knime.gis.views.canvas.CanvasUtils.java

public static Paint mixColors(Color backgroundColor, List<Color> colors, List<Double> alphas,
        boolean checkedInsteadOfStriped) {
    double rb = backgroundColor.getRed() / 255.0;
    double gb = backgroundColor.getGreen() / 255.0;
    double bb = backgroundColor.getBlue() / 255.0;
    double ab = backgroundColor.getAlpha() / 255.0;
    List<Color> cs = new ArrayList<>();

    for (int i = 0; i < colors.size(); i++) {
        double alpha = alphas.get(i);

        if (alpha > 0.0) {
            double r = colors.get(i).getRed() / 255.0 * alpha + rb * (1 - alpha);
            double g = colors.get(i).getGreen() / 255.0 * alpha + gb * (1 - alpha);
            double b = colors.get(i).getBlue() / 255.0 * alpha + bb * (1 - alpha);
            double a = colors.get(i).getAlpha() / 255.0 * alpha + ab * (1 - alpha);

            cs.add(new Color((float) r, (float) g, (float) b, (float) a));
        }//from ww  w  .  ja  v a 2  s  . co m
    }

    if (cs.isEmpty()) {
        return backgroundColor;
    } else if (cs.size() == 1) {
        return cs.get(0);
    }

    BufferedImage img;
    int size = cs.size() * (checkedInsteadOfStriped ? EDGE_TEXTURE_SIZE : NODE_TEXTURE_SIZE);

    if (checkedInsteadOfStriped) {
        img = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);

        for (int x = 0; x < size; x++) {
            for (int y = 0; y < size; y++) {
                img.setRGB(x, y, cs.get((x / EDGE_TEXTURE_SIZE + y / EDGE_TEXTURE_SIZE) % cs.size()).getRGB());
            }
        }
    } else {
        img = new BufferedImage(size, 1, BufferedImage.TYPE_INT_ARGB);

        for (int x = 0; x < size; x++) {
            img.setRGB(x, 0, cs.get(x / NODE_TEXTURE_SIZE).getRGB());
        }
    }

    return new TexturePaint(img, new Rectangle(img.getWidth(), img.getHeight()));
}

From source file:nl.b3p.viewer.image.ImageTool.java

/** Reads an image from an http input stream.
 *
 * @param method Apache HttpClient GetMethod object
 * @param mime String representing the mime type of the image.
 *
 * @return BufferedImage//  w  w w  .  jav  a2  s. c o  m
 *
 * @throws Exception
 */
// <editor-fold defaultstate="" desc="readImage(GetMethod method, String mime) method.">
public static BufferedImage readImage(HttpMethod method, String mime) throws Exception {
    ImageReader ir = null;
    BufferedImage i = null;
    try {
        if (mime.indexOf(";") != -1) {
            mime = mime.substring(0, mime.indexOf(";"));
        }
        String mimeType = getMimeType(mime);

        /* TODO: Kijken waarom er geen mime type meer binnenkomt. Wellicht door de 
         * HttpClient vernieuwing in kaartenbalie ? */
        if (mimeType == null) {
            mimeType = "image/png";
        }

        if (mimeType == null) {
            log.error("Response from server not understood (mime = " + mime + "): "
                    + method.getResponseBodyAsString());
            throw new Exception("Response from server not understood (mime = " + mime + "): "
                    + method.getResponseBodyAsString());
        }

        ir = getReader(mimeType);
        if (ir == null) {
            log.error("no reader available for imageformat: "
                    + mimeType.substring(mimeType.lastIndexOf("/") + 1));
            throw new Exception("no reader available for imageformat: "
                    + mimeType.substring(mimeType.lastIndexOf("/") + 1));
        }
        //TODO Make smarter.. Possibly faster... But keep reporting!
        InputStream is = method.getResponseBodyAsStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int bytesRead = 0;
        byte[] buffer = new byte[2048];
        while (bytesRead != -1) {
            bytesRead = is.read(buffer, 0, buffer.length);
            if (bytesRead > 0) {
                baos.write(buffer, 0, bytesRead);
            }
        }
        ImageInputStream stream = ImageIO.createImageInputStream(new ByteArrayInputStream(baos.toByteArray()));
        ir.setInput(stream, true);
        i = ir.read(0);
        //if image is a png, has no alpha and has a tRNS then make that color transparent.
        if (!i.getColorModel().hasAlpha() && ir.getImageMetadata(0) instanceof PNGMetadata) {
            PNGMetadata metadata = (PNGMetadata) ir.getImageMetadata(0);
            if (metadata.tRNS_present) {
                int alphaPix = (metadata.tRNS_red << 16) | (metadata.tRNS_green << 8) | (metadata.tRNS_blue);
                BufferedImage tmp = new BufferedImage(i.getWidth(), i.getHeight(), BufferedImage.TYPE_INT_ARGB);
                for (int x = 0; x < i.getWidth(); x++) {
                    for (int y = 0; y < i.getHeight(); y++) {
                        int rgb = i.getRGB(x, y);
                        rgb = (rgb & 0xFFFFFF) == alphaPix ? alphaPix : rgb;
                        tmp.setRGB(x, y, rgb);
                    }
                }
                i = tmp;
            }
        }
    } finally {
        if (ir != null) {
            ir.dispose();
        }
    }
    return i;
}

From source file:nl.b3p.imagetool.ImageTool.java

/**
 * Reads an image from an http input stream.
 *
 * @param method Apache HttpClient GetMethod object
 * @param mime String representing the mime type of the image.
 *
 * @return BufferedImage//w w w.j  ava  2  s.  c om
 *
 * @throws Exception
 */
// <editor-fold defaultstate="" desc="readImage(GetMethod method, String mime) method.">
public static BufferedImage readImage(InputStream is, String mime) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int bytesRead = 0;
    byte[] buffer = new byte[2048];
    while (bytesRead != -1) {
        bytesRead = is.read(buffer, 0, buffer.length);
        if (bytesRead > 0) {
            baos.write(buffer, 0, bytesRead);
        }
    }
    ImageReader ir = null;
    BufferedImage i = null;
    try {
        if (mime.indexOf(";") != -1) {
            mime = mime.substring(0, mime.indexOf(";"));
        }
        String mimeType = getMimeType(mime);

        /* TODO: Kijken waarom er geen mime type meer binnenkomt. Wellicht door de
         * HttpClient vernieuwing in kaartenbalie ? */
        if (mimeType == null) {
            mimeType = "image/png";
        }

        if (mimeType == null) {
            log.error("Response from server not understood (mime = " + mime + "): " + baos.toString());
            throw new Exception(
                    "Response from server not understood (mime = " + mime + "): " + baos.toString());
        }

        ir = getReader(mimeType);
        if (ir == null) {
            log.error("no reader available for imageformat: "
                    + mimeType.substring(mimeType.lastIndexOf("/") + 1));
            throw new Exception("no reader available for imageformat: "
                    + mimeType.substring(mimeType.lastIndexOf("/") + 1));
        }
        //TODO Make smarter.. Possibly faster... But keep reporting!
        ImageInputStream stream = ImageIO.createImageInputStream(new ByteArrayInputStream(baos.toByteArray()));
        ir.setInput(stream, true);
        i = ir.read(0);
        //if image is a png, has no alpha and has a tRNS then make that color transparent.
        if (!i.getColorModel().hasAlpha() && ir.getImageMetadata(0) instanceof PNGMetadata) {
            PNGMetadata metadata = (PNGMetadata) ir.getImageMetadata(0);
            if (metadata.tRNS_present) {
                int alphaPix = (metadata.tRNS_red << 16) | (metadata.tRNS_green << 8) | (metadata.tRNS_blue);
                BufferedImage tmp = new BufferedImage(i.getWidth(), i.getHeight(), BufferedImage.TYPE_INT_ARGB);
                for (int x = 0; x < i.getWidth(); x++) {
                    for (int y = 0; y < i.getHeight(); y++) {
                        int rgb = i.getRGB(x, y);
                        rgb = (rgb & 0xFFFFFF) == alphaPix ? alphaPix : rgb;
                        tmp.setRGB(x, y, rgb);
                    }
                }
                i = tmp;
            }
        }
    } finally {
        if (ir != null) {
            ir.dispose();
        }
    }
    return i;
}