Java BufferedImage Invert invertImage(BufferedImage img, boolean newImage)

Here you can find the source of invertImage(BufferedImage img, boolean newImage)

Description

Inverts the given image.

License

Open Source License

Parameter

Parameter Description
img The image to invert
newImage Whether to return a new image or invert the given image.

Return

the inverted image

Declaration

static public BufferedImage invertImage(BufferedImage img, boolean newImage) 

Method Source Code

//package com.java2s;

import java.awt.image.BufferedImage;

public class Main {
    /**//  w  ww .j  a va2  s. c o  m
     *    Inverts the given image.
     *   @param img The image to invert
     *   @param newImage Whether to return a new image or invert the given image.
     *   @return the inverted image  
     */
    static public BufferedImage invertImage(BufferedImage img, boolean newImage) {
        BufferedImage returnImage = img;
        if (newImage)
            returnImage = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());

        for (int y = 0; y < img.getHeight(); y++) {
            for (int x = 0; x < img.getWidth(); x++) {
                int in = img.getRGB(x, y);
                returnImage.setRGB(x, y, (in & 0xff000000) | ((~in) & 0x00ffffff));
            }
        }

        return returnImage;
    }
}

Related

  1. invert(BufferedImage inImg)
  2. invertImage(BufferedImage image)
  3. invertImage(BufferedImage image)
  4. invertImage(final BufferedImage savedImage)