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

1    // Glenn Josefiak 
2    // Fairfield University 
3    // SW513 
4    // Spring 2003 
5     
6    package j2d.imageproc; 
7     
8    /** 
9     * This classes allows adjustment of an image using a "Pow 
10    * Function" for exponential stretching of pixel intensity. 
11    * Reference: Douglas A. Lyon, "Image Processing in Java" 
12    */ 
13    public class ExponentialStretchProcessor 
14           extends ImageProcessor { 
15    
16       private int lookupTable[] = new int[256]; 
17    
18       /** 
19        * Create a new ExponentialStretchProcessor 
20        */ 
21       public ExponentialStretchProcessor(){ 
22           for (int j = 0; j< 256; j++){ 
23               lookupTable[j] = j; 
24           } 
25       } 
26    
27       /** 
28        * Implementation of ImageProcessor 
29        */ 
30       public void performAlgorithm() throws Exception{ 
31           int pixels[]; 
32           int r, g, b; 
33    
34           pixels = getPixels(); 
35    
36           for (int i = 0; i<pixels.length; i++){ 
37    
38               // Separate RGB components 
39               r = (pixels[i] & 0x00FF0000) >> 16; 
40               g = (pixels[i] & 0x0000FF00) >> 8; 
41               b = (pixels[i] & 0x000000FF); 
42    
43               // Adjust the pixel 
44               r = lookupTable[r]; 
45               g = lookupTable[g]; 
46               b = lookupTable[b]; 
47    
48               // store the processed pixel 
49               pixels[i] = 0xFF000000 | (r << 16) | (g << 8) | b; 
50           } 
51    
52           setPixels(pixels); 
53       } 
54    
55       /** 
56        * Set the exponential coefficient. 
57        */ 
58    
59       public void setPower(double power){ 
60           double newj; 
61           for (int j = 0; j<256; j++){ 
62               // new intensity is a stretched version of the original. 
63               newj = 255.0 * Math.pow((j/255.0), power); 
64               // saturate if the stretching takes the intensities out of range. 
65               lookupTable[j] = (int)Math.min(255, newj); 
66               lookupTable[j] = Math.max(0, lookupTable[j]); 
67           } 
68       } 
69   } 
70