/Users/lyon/j4p/src/j2d/filters/InvertFilter.java

1    // InvertFilter.java 
2    // InvertFilter is an ImageFilter that inverts a 
3    // BufferedImage's RGB color values. 
4    package j2d.filters; 
5     
6    // Java core packages 
7     
8    import ip.transforms.BufferedImageProcessor; 
9     
10   import java.awt.image.BufferedImage; 
11   import java.awt.image.BufferedImageOp; 
12   import java.awt.image.ByteLookupTable; 
13   import java.awt.image.LookupOp; 
14    
15   public class InvertFilter implements BufferedImageProcessor { 
16    
17       // apply color inversion filter to BufferedImage 
18       public BufferedImage process(BufferedImage image) { 
19           // create 256 color array and invert colors 
20           byte[] invertArray = new byte[256]; 
21    
22           for (int counter = 0; counter < 256; counter++) 
23               invertArray[counter] = (byte) (255 - counter); 
24    
25           // create filter to invert colors 
26           BufferedImageOp invertFilter = new LookupOp( 
27                   new ByteLookupTable(0, invertArray), null); 
28    
29           // apply filter to displayImage 
30           return invertFilter.filter(image, null); 
31    
32       } // end method processImage 
33   } 
34    
35    
36