image

Swapping the RGB Values of an Image

With this tutorial we shall show you how to swap RGB values in an Image. This is very useful when you are trying to create some effects to your Application.

Basically all you have to do in order to swapp RGD values of an Image is:

  • Load an image using ImageIcon and getImage method
  • Create a class that extends RGBImageFilter and override the filterRGB method which is executed for every pixel in the image. Inside this method you can do all the RGB transformations you need
  • Create a filtered source image using FilteredImageSource and provide the RGBImageFilter class you created in the previous step as an argument
  • And simply paint the buffered image in a new Frame
Let’s see how the code looks like:
package com.javacodegeeks.snippets.desktop;

import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.FilteredImageSource;
import java.awt.image.RGBImageFilter;

import javax.swing.ImageIcon;

public class RGBSwapping {

  static Image image;

  public static void main(String[] args) {

// Get image - change to where your image file is located!

image = new ImageIcon("image.png").getImage();

// Create the filtered source image

FilteredImageSource filteredImageSource = new FilteredImageSource(image.getSource(), new RedBlueSwapFilter());

// Create the filtered image

image = Toolkit.getDefaultToolkit().createImage(filteredImageSource);

// Create frame with specific title

Frame frame = new Frame("Example Frame");

// Add a component with a custom paint method

frame.add(new CustomPaintComponent());

// Display the frame

int frameWidth = 300;

int frameHeight = 300;

frame.setSize(frameWidth, frameHeight);

frame.setVisible(true);

  }

    /**
     * To draw on the screen, it is first necessary to subclass a Component 
     * and override its paint() method. The paint() method is automatically called 
     * by the windowing system whenever component's area needs to be repainted.
     */
    static class CustomPaintComponent extends Component {

  public void paint(Graphics g) {

    // Retrieve the graphics context; this object is used to paint shapes

    Graphics2D g2d = (Graphics2D)g;

    /**

* Draw an Image object

* The coordinate system of a graphics context is such that the origin is at the 

* northwest corner and x-axis increases toward the right while the y-axis increases 

* toward the bottom.

*/

    int x = 0;

    int y = 0;

    g2d.drawImage(image, x, y, this);

  }

    }

    // This filter swaps the red and blue values in an image
    static class RedBlueSwapFilter extends RGBImageFilter {

  public RedBlueSwapFilter() {

// The filter's operation does not depend on the

// pixel's location, so IndexColorModels can be

// filtered directly.

canFilterIndexColorModel = true;

  }

  // This method is called for every pixel in the image

  public int filterRGB(int x, int y, int rgb) {

return ((rgb & 0xff00ff00)

  | ((rgb & 0xff0000) >> 16)

  | ((rgb & 0xff) << 16));

  }
    }

}

 
This was an example on how to Swap the RGB values of an Image .

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button