/Users/lyon/j4p/src/j2d/imageproc/LinearMappingProcessor.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 the brightness (DC offset) and 
10    * contrast (intensity stretch) of an image. 
11    */ 
12   public class LinearMappingProcessor extends ImageProcessor{ 
13    
14       private int lookupTable[] = new int[256]; 
15       private int min; 
16       private int max; 
17    
18       /** 
19        * Create a new LinearMappingProcessor 
20        */ 
21       public LinearMappingProcessor(){ 
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 processing parameters. 
57        */ 
58    
59       public void setParameters(double offset, double slope){ 
60           double newj; 
61           for (int j = 0; j<256; j++){ 
62               // new intensity is a stretched and offset version of the original. 
63               newj = j*slope + offset; 
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       /** 
71        * Set offset and slope to maximize the use of the dynamic range. 
72        * To apply the parameters to the image, this must be followed by 
73        * a call to performAlgorithm(). 
74        * 
75        * @return              Two-element array.  Element 0 contains the 
76        *                      offset (brightness adjustment).  Element 1 
77        *                      contains the slope (contrast adjustment). 
78        */ 
79       public float[] setOptimalParameters(){ 
80           float params[] = new float[2]; 
81           int Dmin = 0; 
82           int Dmax = 255; 
83    
84           computeExtent(); 
85    
86           float deltaV = max - min; 
87           float deltaD = Dmax - Dmin; 
88    
89           params[1] = deltaD / deltaV; 
90           params[0] = (Dmin * max - Dmax * min) / deltaV; 
91    
92           setParameters(params[0], params[1]); 
93           return params; 
94       } 
95    
96       /** 
97        * Compute the minimum and maximum intensity values. 
98        */ 
99       private void computeExtent() { 
100          int pixels[]; 
101          int r, g, b; 
102          min = Integer.MAX_VALUE; 
103          max = Integer.MIN_VALUE; 
104   
105          pixels = getPixels(); 
106   
107          for (int i = 0; i<pixels.length; i++){ 
108   
109              // Separate RGB components 
110              r = (pixels[i] & 0x00FF0000) >> 16; 
111              g = (pixels[i] & 0x0000FF00) >> 8; 
112              b = (pixels[i] & 0x000000FF); 
113   
114              //min = Math.min((r+g+b)/3, min); 
115              //max = Math.max((r+g+b)/3, max); 
116              min = Math.min(r, min); 
117              min = Math.min(g, min); 
118              min = Math.min(b, min); 
119              max = Math.max(r, max); 
120              max = Math.max(g, max); 
121              max = Math.max(b, max); 
122          } 
123      } 
124  } 
125