/Users/lyon/j4p/src/j2d/imageproc/FalseColorProcessor.java

1    // Glenn Josefiak 
2    // Fairfield University 
3    // SW513 
4    // Spring 2003 
5     
6    package j2d.imageproc; 
7     
8    /** 
9     * This class provides some image processing on an image by generating false 
10    * color.  If the input image is color, it is converted to grayscale before 
11    * the false color is applied. 
12    * 
13    * The source of this method of false coloring is the following paper: 
14    * C.W.A.M. van Overfeld, "Color Waves: A Simple Heuristic for Choosing 
15    * False Colors", Journal of Graphics Tools, Vol. 2, No. 4: 45-50 
16    */ 
17   public class FalseColorProcessor extends ImageProcessor{ 
18    
19       private int lookupTable[][] = new int[3][256]; 
20    
21       /** 
22        * Create a new FalseColorProcessor 
23        */ 
24       public FalseColorProcessor(){ 
25           for (int i = 0; i<3; i++) 
26               for (int j = 0; j<256; j++) 
27                   lookupTable[i][j] = j; 
28       } 
29       /** 
30        * Implementation of ImageProcessor 
31        */  
32       public void performAlgorithm() throws Exception{ 
33           int pixels[]; 
34           int r, g, b, gray; 
35            
36           pixels = getPixels(); 
37    
38           for (int i = 0; i<pixels.length; i++){ 
39                    
40               // separate RGB components 
41               r = (pixels[i] & 0x00FF0000) >> 16; 
42               g = (pixels[i] & 0x0000FF00) >> 8; 
43               b = (pixels[i] & 0x000000FF); 
44                    
45               // if color, convert to grayscale here. 
46               gray = (r + g + b)/3;            
47                    
48               // add the false color 
49               r = lookupTable[0][gray]; 
50               g = lookupTable[1][gray]; 
51               b = lookupTable[2][gray]; 
52                    
53               // store the processed pixel 
54               pixels[i] = 0xFF000000 | (r << 16) | (g << 8) | b; 
55           } 
56    
57           setPixels(pixels); 
58       } 
59        
60       /** 
61        * Set the colorization parameters.  The values must be between 
62        * 0.01 and 0.5. 
63        */ 
64        
65       public void setColorization(double ar, double ag, double ab){ 
66           double a[] = new double[3]; 
67    
68           a[0] = ar; 
69           a[1] = ag; 
70           a[2] = ab; 
71    
72           for (int i = 0; i < 3; i++){ 
73               for (int j = 0; j<256; j++){ 
74                   lookupTable[i][j] = (int)(255 * (0.5 + 0.5*Math.sin(a[i] * j))); 
75               } 
76           } 
77       } 
78   }