/Users/lyon/j4p/src/ip/color/FloatPlane.java

1    package ip.color; 
2     
3    import ip.gui.frames.ColorFrame; 
4    import ip.gui.frames.ImageFrame; 
5    import ip.gui.frames.ImageFrameInterface; 
6    import math.Mat3; 
7     
8     
9    abstract public class FloatPlane { 
10       public float r[][]; 
11       public float g[][]; 
12       public float b[][]; 
13       protected int width; 
14       protected int height; 
15       private ColorFrame parent; 
16    
17       public float rBar, gBar, bBar; 
18       public float min, max; 
19    
20       public FloatPlane(ColorFrame _cf) { 
21           copyFloats(_cf); 
22    
23       } 
24    
25       public void zeroOut() { 
26           r = new float[width][height]; 
27           g = new float[width][height]; 
28           b = new float[width][height]; 
29       } 
30    
31       public void oneOnF() { 
32           int w = width; 
33           int h = height; 
34           int xc = w / 2; 
35           int yc = h / 2; 
36           float rn[][] = new float[width][height]; 
37           float gn[][] = new float[width][height]; 
38           float bn[][] = new float[width][height]; 
39           double p[] = new double[2]; 
40           int red, green, blue; 
41           int xp, yp, i, j; 
42           double radiusMax = Math.sqrt((w / 2) * (w / 2) + (h / 2) * (h / 2)); 
43           for (int x = 0; x < w; x++) 
44               for (int y = 0; y < h; y++) { 
45                   double dx = x - xc; 
46                   double dy = y - yc; 
47                   double radius = Math.sqrt(dx * dx + dy * dy); 
48                   double a = Math.atan2(dy, dx); 
49    
50                   p[0] = radius * Math.cos(a); 
51                   p[1] = radius * Math.sin(a); 
52                   double oneOnF = radiusMax / (radius + .01); 
53                   xp = (int) p[0] + xc; 
54                   yp = (int) p[1] + yc; 
55                   if ((xp < w) && (yp < h) && (xp >= 0) && (yp >= 0)) { 
56                       rn[x][y] = (float) (oneOnF * r[xp][yp]); 
57                       gn[x][y] = (float) (oneOnF * g[xp][yp]); 
58                       bn[x][y] = (float) (oneOnF * b[xp][yp]); 
59                   } 
60               } 
61           r = rn; 
62           g = gn; 
63           b = bn; 
64       } 
65    
66       public void transpose() { 
67           float ro[][] = new float[r[0].length][r.length]; 
68           float go[][] = new float[r[0].length][r.length]; 
69           float bo[][] = new float[r[0].length][r.length]; 
70           for (int x = 0; x < r.length; x++) 
71               for (int y = 0; y < r[0].length; y++) { 
72                   ro[y][x] = r[x][y]; 
73                   go[y][x] = g[x][y]; 
74                   bo[y][x] = b[x][y]; 
75               } 
76           height = r.length; 
77           width = r[0].length; 
78           r = ro; 
79           g = go; 
80           b = bo; 
81       } 
82    
83    
84       public void copyFloats(ColorFrame _cf) { 
85           parent = _cf; 
86           ImageFrameInterface frame = parent; 
87           width = frame.getImageWidth(); 
88           ImageFrameInterface frame1 = parent; 
89           height = frame1.getImageHeight(); 
90           r = new float[width][height]; 
91           g = new float[width][height]; 
92           b = new float[width][height]; 
93           for (int x = 0; x < width; x++) 
94               for (int y = 0; y < height; y++) { 
95                   ImageFrame frame2 = parent; 
96                   r[x][y] = frame2.shortImageBean.getR()[x][y]; 
97                   ImageFrame frame3 = parent; 
98                   g[x][y] = frame3.shortImageBean.getG()[x][y]; 
99                   ImageFrame frame4 = parent; 
100                  b[x][y] = frame4.shortImageBean.getB()[x][y]; 
101              } 
102      } 
103   
104      public void linearTransform() { 
105          computeStats(); 
106          float Vmin = min; 
107          float Vmax = max; 
108          int Dmin = 0; 
109          int Dmax = 255; 
110          double deltaV = Vmax - Vmin; 
111          double deltaD = Dmax - Dmin; 
112          double c = deltaD / deltaV; 
113          double b = (Dmin * Vmax - Dmax * Vmin) / deltaV; 
114          linearTransform(c, b); 
115      } 
116   
117      public void linearTransform(double c, double br) { 
118          for (int x = 0; x < width; x++) 
119              for (int y = 0; y < height; y++) { 
120                  r[x][y] = (float) (c * r[x][y] + br); 
121                  g[x][y] = (float) (c * g[x][y] + br); 
122                  b[x][y] = (float) (c * b[x][y] + br); 
123              } 
124      } 
125   
126      public void computeStats() { 
127          min = Integer.MAX_VALUE; 
128          max = Integer.MIN_VALUE; 
129          rBar = 0; 
130          gBar = 0; 
131          bBar = 0; 
132          double N = width * height; 
133          for (int x = 0; x < width; x++) 
134              for (int y = 0; y < height; y++) { 
135                  rBar += r[x][y]; 
136                  gBar += g[x][y]; 
137                  bBar += b[x][y]; 
138                  min = Math.min(r[x][y], min); 
139                  min = Math.min(g[x][y], min); 
140                  min = Math.min(b[x][y], min); 
141                  max = Math.max(r[x][y], max); 
142                  max = Math.max(g[x][y], max); 
143                  max = Math.max(b[x][y], max); 
144              } 
145          rBar /= N; 
146          gBar /= N; 
147          bBar /= N; 
148      } 
149   
150   
151      public abstract void toRgb(); 
152   
153      public abstract void fromRgb(); 
154   
155      public float getMin(float a[][]) { 
156          float min = Float.MAX_VALUE; 
157          for (int x = 0; x < a.length; x++) 
158              for (int y = 0; y < a[0].length; y++) { 
159                  if (min > a[x][y]) min = a[x][y]; 
160              } 
161          return min; 
162      } 
163   
164      public float min(float m1, float m2, float m3) { 
165          if ((m1 <= m2) && (m1 <= m3)) return m1; 
166          if ((m2 <= m1) && (m2 <= m3)) return m2; 
167          return m3; 
168      } 
169   
170      public float max(float m1, float m2, float m3) { 
171          if ((m1 >= m2) && (m1 >= m3)) return m1; 
172          if ((m2 >= m1) && (m2 >= m3)) return m2; 
173          return m3; 
174      } 
175   
176      public float getMin() { 
177          return min(getMin(r), getMin(g), getMin(b)); 
178      } 
179   
180      public float getMax() { 
181          return max(getMax(r), getMax(g), getMax(b)); 
182      } 
183   
184      public void printStatistics() { 
185          System.out.println("Max:" + getMax()); 
186          System.out.println("Min:" + getMin()); 
187      } 
188  // set a in [0,1] 
189      public void normalize(float a[][]) { 
190          float min = getMin(a); 
191          if (min < 0) addArray(a, -min); 
192          if (min > 0) addArray(a, min); 
193          scaleArray(a, 1 / getMax(a)); 
194      } 
195   
196  // set r,g,b each in [0,1] 
197      public void normalize() { 
198          float min = getMin(); 
199          if (min < 0) min = -min; 
200          addArray(r, min); 
201          addArray(g, min); 
202          addArray(b, min); 
203          float s = 1 / getMax(); 
204          scaleArray(r, s); 
205          scaleArray(g, s); 
206          scaleArray(b, s); 
207      } 
208   
209      public void scaleArray(float a[][], float s) { 
210          for (int x = 0; x < width; x++) 
211              for (int y = 0; y < height; y++) 
212                  a[x][y] *= s; 
213      } 
214   
215      public void powArray(float a[][], float s) { 
216          for (int x = 0; x < width; x++) 
217              for (int y = 0; y < height; y++) 
218                  if (a[x][y] < 0) 
219                      a[x][y] = (float) (-Math.pow(-a[x][y], s)); 
220                  else 
221                      a[x][y] = (float) Math.pow(a[x][y], s); 
222      } 
223   
224   
225      public void pow(float s) { 
226          powArray(r, s); 
227          powArray(g, s); 
228          powArray(b, s); 
229      } 
230   
231      public void scale(float s) { 
232          scaleArray(r, s); 
233          scaleArray(g, s); 
234          scaleArray(b, s); 
235      } 
236   
237      public void addArray(float a[][], float s) { 
238          for (int x = 0; x < width; x++) 
239              for (int y = 0; y < height; y++) 
240                  a[x][y] += s; 
241      } 
242   
243   
244      public float getMax(float a[][]) { 
245          float max = Float.MIN_VALUE; 
246          for (int x = 0; x < a.length; x++) 
247              for (int y = 0; y < a[0].length; y++) { 
248                  if (max < a[x][y]) max = a[x][y]; 
249              } 
250          return max; 
251      } 
252   
253      public void convertSpace(Mat3 m) { 
254          float pel[]; 
255          for (int x = 0; x < width; x++) 
256              for (int y = 0; y < height; y++) { 
257                  pel = m.multiply(r[x][y], g[x][y], b[x][y]); 
258   
259                  r[x][y] = pel[0]; 
260                  g[x][y] = pel[1]; 
261                  b[x][y] = pel[2]; 
262              } 
263      } 
264   
265   
266      public void subSampleChroma2To1() { 
267          b = oneDSubsampleTwoTo1(b); 
268          g = oneDSubsampleTwoTo1(g); 
269      } 
270   
271      public float[][] oneDSubsampleTwoTo1(float f[][]) { 
272          int width = f.length; 
273          int height = f[0].length; 
274          float h[][] = new float[width][height]; 
275          double sum = 0; 
276   
277          for (int y = 0; y < height; y++) { 
278              for (int x = 0; x < width - 2; x = x + 2) { 
279                  float a = (float) ((f[x][y] + f[x + 1][y]) / 2.0); 
280                  h[x][y] = a; 
281                  h[x + 1][y] = a; 
282              } 
283          } 
284          return h; 
285      } 
286   
287      public void subSampleChroma4To1() { 
288          b = oneDSubsample4To1(b); 
289          g = oneDSubsample4To1(g); 
290      } 
291   
292   
293      public float[][] oneDSubsample4To1(float f[][]) { 
294          int width = f.length; 
295          int height = f[0].length; 
296          float h[][] = new float[width][height]; 
297          double sum = 0; 
298          for (int y = 0; y < height - 2; y = y + 2) { 
299              for (int x = 0; x < width - 2; x = x + 2) { 
300                  float a = (f[x][y] + f[x + 1][y] + f[x + 1][y + 1] + f[x][y + 1]) / 4f; 
301                  h[x][y] = a; 
302                  h[x + 1][y] = a; 
303                  h[x][y + 1] = a; 
304                  h[x + 1][y + 1] = a; 
305              } 
306          } 
307          return h; 
308      } 
309   
310      public void updateParent(float scaleFactor) { 
311          ImageFrameInterface frame = parent; 
312          int width1 = width; 
313          frame.setImageWidth(width1); 
314          ImageFrameInterface frame1 = parent; 
315          int height1 = height; 
316          frame1.setImageHeight(height1); 
317          ImageFrame frame2 = parent; 
318          short[][] r1 = new short[width][height]; 
319          frame2.shortImageBean.setR(r1); 
320          parent.setG(new short[width][height]); 
321          parent.setB(new short[width][height]); 
322          for (int x = 0; x < width; x++) 
323              for (int y = 0; y < height; y++) { 
324                  //ImageFrame frame2 = parent; 
325                  frame2.shortImageBean.getR()[x][y] = (short) (scaleFactor * r[x][y]); 
326                  ImageFrame frame3 = parent; 
327                  frame3.shortImageBean.getG()[x][y] = (short) (scaleFactor * g[x][y]); 
328                  ImageFrame frame4 = parent; 
329                  frame4.shortImageBean.getB()[x][y] = (short) (scaleFactor * b[x][y]); 
330              } 
331          parent.short2Image(); 
332      } 
333   
334      public void updateParent() { 
335          ImageFrameInterface frame = parent; 
336          int width1 = width; 
337          frame.setImageWidth(width1); 
338          ImageFrameInterface frame1 = parent; 
339          int height1 = height; 
340          frame1.setImageHeight(height1); 
341          ImageFrame frame2 = parent; 
342          short[][] r1 = new short[width][height]; 
343          frame2.shortImageBean.setR(r1); 
344          parent.setG(new short[width][height]); 
345          parent.setB(new short[width][height]); 
346          for (int x = 0; x < width; x++) 
347              for (int y = 0; y < height; y++) { 
348                  //ImageFrame frame2 = parent; 
349                  frame2.shortImageBean.getR()[x][y] = (short) r[x][y]; 
350                  ImageFrame frame3 = parent; 
351                  frame3.shortImageBean.getG()[x][y] = (short) g[x][y]; 
352                  ImageFrame frame4 = parent; 
353                  frame4.shortImageBean.getB()[x][y] = (short) b[x][y]; 
354              } 
355          parent.short2Image(); 
356      } 
357  }