Inverting the red band of all pixels of a image in Swing

Use the class LookupOp and pass it an array of arrays, each representing the change in band.

In this example, the G(reen) and B(lue) band don’t change while the R(ed) pixels are made brighter.

import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
   private boolean firstTime = true;
   private BufferedImage sourceBi;
   private BufferedImage destBi;
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
   public void paint(Graphics graphics) {
      Graphics2D g2d = (Graphics2D) graphics;
      if (firstTime) {
         Image img = new javax.swing.ImageIcon("djkrush.jpg").getImage(); 
         sourceBi = toBufferedImage(img);
         destBi = new BufferedImage(sourceBi.getWidth(), sourceBi.getHeight(), 
         setSize(sourceBi.getWidth(), sourceBi.getHeight()*2);
         byte[][] intensity = new byte[3][256];
         int newRed;
         for(int i=0; i<256; i++) {
            newRed = (int) (i*1.5);  if (newRed > 255) newRed = 255;
            intensity[0][i] = (byte) newRed;
            intensity[1][i] = (byte) i;
            intensity[2][i] = (byte) i;
         ByteLookupTable blt = new ByteLookupTable(0, intensity);
         LookupOp op = new LookupOp(blt, null);
         destBi = op.filter(sourceBi, null);
         firstTime = false;
      g2d.drawImage(sourceBi, 0, 0, this);
      g2d.drawImage(destBi, 0, sourceBi.getHeight(), this);
   public static BufferedImage toBufferedImage(Image image) {
      BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), 
      // copy the original image
      Graphics g = bi.createGraphics();
      g.drawImage(image, 0, 0, null);
      return bi;
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(300, 150);

Image used:

To learn more about image processing, check out this expensive but excellent book: