Brightening an image in Swing

To brighten an image, you can use the class LookupOp. This class works with one or more tables that will be applied to the color values of the pixels. To brighten an RGB image, the RGB values have to be increased (higher values means brighter). The lookup table reflects these changes.

The following example shows the original image, an image that has been brightened by adding 30 to every pixel color (every R,G and B, and all values that are higher than 255 are reset to 255) and an image where brightening is based on the rule that says that midrange colors are adjusted more than dark or bright colors.

Main.java:

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;
   private BufferedImage destBi2;
 
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      }); 
   } 
 
   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(), 
                                    BufferedImage.TYPE_INT_RGB);
         destBi2 = new BufferedImage(sourceBi.getWidth(), sourceBi.getHeight(), 
                                    BufferedImage.TYPE_INT_RGB);
         setSize(sourceBi.getWidth(), sourceBi.getHeight()*3);
 
         short[] intensity1 = new short[256];
         for(int i=0; i<256; i++) {
            short newVal = (short) (i + 30);
            if (newVal > 255) newVal = 255;
            intensity1[i] = newVal;
         }
  
         byte[] intensity2 = new byte[256];
         for(int i=0; i<256; i++) {
            intensity2[i] = (byte) (Math.sqrt((float)i/255.0) * 255);
         }
 
         ShortLookupTable slt = new ShortLookupTable(0, intensity1);
         LookupOp op = new LookupOp(slt, null);
         destBi = op.filter(sourceBi, null);
 
         ByteLookupTable blt = new ByteLookupTable(0, intensity2);
         LookupOp op2 = new LookupOp(blt, null);
         destBi2 = op2.filter(sourceBi, null);
 
         firstTime = false;
      }
 
      g2d.drawImage(sourceBi, 0, 0, this);
      g2d.drawImage(destBi, 0, sourceBi.getHeight(), this);
      g2d.drawImage(destBi2, 0, sourceBi.getHeight()*2, this);
   }
 
   public static BufferedImage toBufferedImage(Image image) {
      BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), 
                                           BufferedImage.TYPE_INT_RGB); 
 
      // copy the original image
      Graphics g = bi.createGraphics();
    
      g.drawImage(image, 0, 0, null);
      g.dispose();
 
      return bi;
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setVisible(true);
      main.setSize(300, 150);
   }
}

Image used:

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