/Users/lyon/j4p/src/ip/gui/frames/ColorFrame.java

1    package ip.gui.frames; 
2     
3    import ip.color.*; 
4    import j2d.ShortImageBean; 
5    import utils.Print; 
6     
7    import java.awt.*; 
8    import java.awt.event.ActionEvent; 
9     
10   public class ColorFrame extends MartelliFrame { 
11       private String noFloatMsg = "Make a float image first, use rgbtoxxx"; 
12       private Menu colorMenu = getMenu("Color"); 
13       private Menu swapMenu = getMenu("Swap"); 
14       private Menu triStimulusMenu = getMenu("TriStimulus"); 
15       private Menu quantizationMenu = getMenu("Quantization..."); 
16       private Menu medianCutMenu = getMenu("Median Cut..."); 
17       private Menu linearCutMenu = getMenu("linear Cut..."); 
18       private Menu wuMenu = getMenu("Wu"); 
19       private Menu rgbMenu = getMenu("RGBto..."); 
20       private Menu octreeMenu = getMenu("Octree cut..."); 
21       private MenuItem swapGreenAndBlue_mi 
22               = addMenuItem(swapMenu, "green and blue"); 
23       private MenuItem octree256_mi = addMenuItem(octreeMenu, "to 256"); 
24       private MenuItem octree128_mi = addMenuItem(octreeMenu, "to 128"); 
25       private MenuItem octree64_mi = addMenuItem(octreeMenu, "to 64"); 
26       private MenuItem octree32_mi = addMenuItem(octreeMenu, "to 32"); 
27       private MenuItem octree16_mi = addMenuItem(octreeMenu, "to 16"); 
28       private MenuItem octree8_mi = addMenuItem(octreeMenu, "to 8"); 
29       private MenuItem octree4_mi = addMenuItem(octreeMenu, "to 4"); 
30       private MenuItem octree2_mi = addMenuItem(octreeMenu, "to 2"); 
31       private MenuItem linearCut21_mi = 
32               addMenuItem(linearCutMenu, "cut 21 bits->3 bit image"); 
33       private MenuItem linearCut18_mi = 
34               addMenuItem(linearCutMenu, "cut 18 bits->6 bit image"); 
35       private MenuItem linearCut15_mi = 
36               addMenuItem(linearCutMenu, "cut 15 bits->9 bit image"); 
37       private MenuItem linearCut12_mi = 
38               addMenuItem(linearCutMenu, "cut 12 bits->12 bit image"); 
39       private MenuItem linearCut9_mi = 
40               addMenuItem(linearCutMenu, "cut 9 bits->15 bit image"); 
41       private MenuItem linearCut6_mi = 
42               addMenuItem(linearCutMenu, "cut 6 bits->18 bit image"); 
43       private MenuItem linearCut3_mi = 
44               addMenuItem(linearCutMenu, "cut 3 bits->21 bit image"); 
45       private MenuItem medianCut256_mi = 
46               addMenuItem(medianCutMenu, "to 256"); 
47       private MenuItem medianCut128_mi = 
48               addMenuItem(medianCutMenu, "to 128"); 
49       private MenuItem medianCut64_mi = 
50               addMenuItem(medianCutMenu, "to 64"); 
51       private MenuItem medianCut32_mi = 
52               addMenuItem(medianCutMenu, "to 32"); 
53       private MenuItem medianCut16_mi = 
54               addMenuItem(medianCutMenu, "to 16"); 
55       private MenuItem medianCut8_mi = 
56               addMenuItem(medianCutMenu, "to 8"); 
57       private MenuItem medianCut4_mi = 
58               addMenuItem(medianCutMenu, "to 4"); 
59       private MenuItem wu256_mi = 
60               addMenuItem(wuMenu, "to 256"); 
61       private MenuItem wu128_mi = 
62               addMenuItem(wuMenu, "to 128"); 
63       private MenuItem wu64_mi = 
64               addMenuItem(wuMenu, "to 64"); 
65       private MenuItem wu32_mi = 
66               addMenuItem(wuMenu, "to 32"); 
67       private MenuItem wu16_mi = 
68               addMenuItem(wuMenu, "to 16"); 
69       private MenuItem wu8_mi = 
70               addMenuItem(wuMenu, "to 8"); 
71       private MenuItem wu4_mi = 
72               addMenuItem(wuMenu, "to 4"); 
73       private MenuItem wu2_mi = 
74               addMenuItem(wuMenu, "to 2"); 
75       private MenuItem printColors_mi = 
76               addMenuItem(quantizationMenu, "printColors"); 
77       private MenuItem printNumberOfColors_mi = 
78               addMenuItem(quantizationMenu, "print number of colors"); 
79       private MenuItem printSNR_mi = 
80               addMenuItem(getFileMenu(), "printSNR relative to child"); 
81       private MenuItem copyToFloatPlane_mi = 
82               addMenuItem(quantizationMenu, "copy to floatPlane"); 
83       private MenuItem subSampleChroma4To1_mi = 
84               addMenuItem(triStimulusMenu, "subSampleChroma4To1"); 
85       private MenuItem subSampleChroma2To1_mi = 
86               addMenuItem(triStimulusMenu, "subSampleChroma2To1"); 
87       private MenuItem rgb2yuv_mi = 
88               addMenuItem(rgbMenu, "rgb2yuv"); 
89       private MenuItem yuv2rgb_mi = 
90               addMenuItem(triStimulusMenu, "yuv2rgb"); 
91       private MenuItem rgb2iyq_mi = 
92               addMenuItem(rgbMenu, "rgb2iyq"); 
93       private MenuItem iyq2rgb_mi = 
94               addMenuItem(triStimulusMenu, "iyq2rgb"); 
95       private MenuItem rgb2hsb_mi = 
96               addMenuItem(rgbMenu, "rgb2hsb"); 
97       private MenuItem yiqSkinSegment_mi = 
98               addMenuItem(rgbMenu, "yiq skin segment"); 
99       private MenuItem hsb2rgb_mi = 
100              addMenuItem(triStimulusMenu, "hsb2rgb"); 
101      //MenuItem rgb2hls_mi = 
102      //  addMenuItem(rgbMenu,"rgb2hls"); 
103      //MenuItem hls2rgb_mi = 
104      //  addMenuItem(triStimulusMenu,"hls2rgb "); 
105   
106      private MenuItem rgb2xyzd65_mi = 
107              addMenuItem(rgbMenu, "rgb SMPTE-C 2xyzd65"); 
108      private MenuItem xyzd652rgb_mi = 
109              addMenuItem(triStimulusMenu, "xyzd652 rgb SMPTE-C"); 
110      private MenuItem rgb2Ccir601_2cbcr_mi = 
111              addMenuItem(rgbMenu, "rgb NTSC 2Ccir601_2cbcr"); 
112      private MenuItem ccir601_2cbcr2rgb_mi = 
113              addMenuItem(triStimulusMenu, "Ccir601_2cbcr2 rgb NTSC"); 
114      private ColorHash ch = null; 
115      private FloatPlane fp = null; 
116   
117      public void actionPerformed(ActionEvent e) { 
118          if (match(e, yiqSkinSegment_mi)) { 
119              skinSegment(); 
120              return; 
121          } 
122          if (match(e, swapGreenAndBlue_mi)) { 
123              swapGreenAndBlue(this); 
124              return; 
125          } 
126          if (match(e, printSNR_mi)) { 
127              printSNR(); 
128              return; 
129          } 
130          if (match(e, copyToFloatPlane_mi)) { 
131              copyToFloatPlane(); 
132              return; 
133          } 
134          if (match(e, subSampleChroma4To1_mi)) { 
135              subSampleChroma4To1(); 
136              return; 
137          } 
138          if (match(e, subSampleChroma2To1_mi)) { 
139              subSampleChroma2To1(); 
140              return; 
141          } 
142          if (match(e, rgb2iyq_mi)) { 
143              rgb2iyq(); 
144              return; 
145          } 
146          if (match(e, iyq2rgb_mi)) { 
147              iyq2rgb(); 
148              return; 
149          } 
150          if (match(e, rgb2Ccir601_2cbcr_mi)) { 
151              rgb2Ccir601_2cbcr(); 
152              return; 
153          } 
154          if (match(e, ccir601_2cbcr2rgb_mi)) { 
155              ccir601_2cbcr2rgb(); 
156              return; 
157          } 
158          if (match(e, rgb2xyzd65_mi)) { 
159              rgb2xyzd65(); 
160              return; 
161          } 
162          if (match(e, xyzd652rgb_mi)) { 
163              xyzd652rgb(); 
164              return; 
165          } 
166          if (match(e, rgb2hsb_mi)) { 
167              rgb2hsb(); 
168              return; 
169          } 
170          if (match(e, hsb2rgb_mi)) { 
171              hsb2rgb(); 
172              return; 
173          } 
174          //if (match(e, rgb2hls_mi)) { 
175          //  rgb2hls(); 
176          //  return; 
177          //} 
178          //if (match(e, hls2rgb_mi)) { 
179          //  hls2rgb(); 
180          //  return; 
181          //} 
182          if (match(e, rgb2yuv_mi)) { 
183              rgb2yuv(); 
184              return; 
185          } 
186          if (match(e, yuv2rgb_mi)) { 
187              yuv2rgb(); 
188              return; 
189          } 
190          if (match(e, medianCut256_mi)) { 
191              medianCut(256); 
192              return; 
193          } 
194          if (match(e, medianCut128_mi)) { 
195              medianCut(128); 
196              return; 
197          } 
198          if (match(e, medianCut64_mi)) { 
199              medianCut(64); 
200              return; 
201          } 
202          if (match(e, medianCut32_mi)) { 
203              medianCut(32); 
204              return; 
205          } 
206          if (match(e, medianCut16_mi)) { 
207              medianCut(16); 
208              return; 
209          } 
210          if (match(e, medianCut8_mi)) { 
211              medianCut(8); 
212              return; 
213          } 
214          if (match(e, medianCut4_mi)) { 
215              medianCut(4); 
216              return; 
217          } 
218          if (match(e, octree256_mi)) { 
219              octree(256); 
220              return; 
221          } 
222          if (match(e, octree128_mi)) { 
223              octree(128); 
224              return; 
225          } 
226          if (match(e, octree64_mi)) { 
227              octree(64); 
228              return; 
229          } 
230          if (match(e, octree32_mi)) { 
231              octree(32); 
232              return; 
233          } 
234          if (match(e, octree16_mi)) { 
235              octree(16); 
236              return; 
237          } 
238          if (match(e, octree8_mi)) { 
239              octree(8); 
240              return; 
241          } 
242          if (match(e, octree4_mi)) { 
243              octree(4); 
244              return; 
245          } 
246          if (match(e, octree2_mi)) { 
247              octree(2); 
248              return; 
249          } 
250          if (match(e, wu256_mi)) { 
251              wu(256); 
252              return; 
253          } 
254          if (match(e, wu128_mi)) { 
255              wu(128); 
256              return; 
257          } 
258          if (match(e, wu64_mi)) { 
259              wu(64); 
260              return; 
261          } 
262          if (match(e, wu32_mi)) { 
263              wu(32); 
264              return; 
265          } 
266          if (match(e, wu16_mi)) { 
267              wu(16); 
268              return; 
269          } 
270          if (match(e, wu8_mi)) { 
271              wu(8); 
272              return; 
273          } 
274          if (match(e, wu4_mi)) { 
275              wu(4); 
276              return; 
277          } 
278          if (match(e, wu2_mi)) { 
279              wu(2); 
280              return; 
281          } 
282          if (match(e, linearCut21_mi)) { 
283              linearCut(7, 7, 7); 
284              return; 
285          } 
286          if (match(e, linearCut18_mi)) { 
287              linearCut(6, 6, 6); 
288              return; 
289          } 
290          if (match(e, linearCut15_mi)) { 
291              linearCut(5, 5, 5); 
292              return; 
293          } 
294          if (match(e, linearCut12_mi)) { 
295              linearCut(4, 4, 4); 
296              return; 
297          } 
298          if (match(e, linearCut9_mi)) { 
299              linearCut(3, 3, 3); 
300              return; 
301          } 
302          if (match(e, linearCut6_mi)) { 
303              linearCut(2, 2, 2); 
304              return; 
305          } 
306          if (match(e, linearCut3_mi)) { 
307              linearCut(1, 1, 1); 
308              return; 
309          } 
310          if (match(e, printColors_mi)) { 
311              printColors(); 
312              return; 
313          } 
314          if (match(e, printNumberOfColors_mi)) { 
315              printNumberOfColors(); 
316              return; 
317          } 
318          super.actionPerformed(e); 
319      } 
320   
321      public ColorFrame(String title) { 
322          super(title); 
323          initMenu(); 
324      } 
325   
326      public static void swapGreenAndBlue(ColorFrame colorFrame) { 
327          ShortImageBean sib = colorFrame.shortImageBean; 
328          short temp[][] = sib.getG(); 
329          colorFrame.setG(sib.getB()); 
330          colorFrame.setB(temp); 
331          colorFrame.short2Image(); 
332      } 
333   
334      public void printSNR() { 
335          Print.println("SNR, in dB = " + getSNRinDb()); 
336          Print.println( 
337                  "There are " + 
338                  getImageWidth() * getImageHeight() + 
339                  " pixels"); 
340      } 
341   
342      public void octree(int numberOfColors) { 
343          ip.color.Octree ot = new ip.color.Octree(); 
344          ot.octreeQuantization(shortImageBean.getR(), 
345                  shortImageBean.getG(), 
346                  shortImageBean.getB(), numberOfColors); 
347          swapGreenAndBlue(this); 
348      } 
349   
350      // Compute (13.9) 
351      public double getSNRinDb() { 
352          if (getChild() == null) { 
353              System.out.println("Make child first!"); 
354              return 0; 
355          } 
356          // Math.log is a natural log 
357          // but want a common log. 
358          final ShortImageBean sib = getChild().getShortImageBean(); 
359          return shortImageBean.getSNRinDb(sib); 
360      } 
361   
362      public void copyToFloatPlane() { 
363          if (fp == null) { 
364              Print.println("Make a float plane first!"); 
365              return; 
366          } 
367          fp.copyFloats(this); 
368      } 
369   
370      public void subSampleChroma4To1() { 
371          rgb2Ccir601_2cbcr(); 
372          fp.subSampleChroma4To1(); 
373          fp.toRgb(); 
374          fp.linearTransform(); 
375          fp.updateParent(); 
376      } 
377   
378      public void subSampleChroma2To1() { 
379          rgb2Ccir601_2cbcr(); 
380          fp.subSampleChroma2To1(); 
381          fp.toRgb(); 
382          fp.linearTransform(); 
383          fp.updateParent(); 
384      } 
385   
386      public void rgb2Ccir601_2cbcr() { 
387          fp = new Ccir601_2cbcr(this); 
388          fp.fromRgb(); 
389          fp.updateParent(); 
390      } 
391   
392      public void ccir601_2cbcr2rgb() { 
393          if (fp == null) { 
394              Print.println(noFloatMsg); 
395              return; 
396          } 
397          fp.toRgb(); 
398          fp.updateParent(); 
399      } 
400   
401      public void rgb2xyzd65() { 
402          fp = new Xyzd65(this); 
403          fp.fromRgb(); 
404          fp.updateParent(255); 
405      } 
406   
407      public void xyzd652rgb() { 
408          if (fp == null) { 
409              System.out.println(noFloatMsg); 
410              return; 
411          } 
412          fp.toRgb(); 
413          fp.updateParent(); 
414      } 
415   
416      public void rgb2iyq() { 
417          fp = new Yiq(this); 
418          fp.fromRgb(); 
419          fp.updateParent(); 
420      } 
421   
422      public void skinSegment() { 
423          fp = new Yiq(this); 
424          fp.fromRgb(); 
425          Yiq y = (Yiq) fp; 
426          y.skinChromaKey(); 
427          fp.toRgb(); 
428          fp.updateParent(); 
429      } 
430   
431      public void iyq2rgb() { 
432          if (fp == null) { 
433              Print.println(noFloatMsg); 
434              return; 
435          } 
436          fp.toRgb(); 
437          fp.updateParent(); 
438      } 
439   
440      public void rgb2hsb() { 
441          fp = new Hsb(this); 
442          fp.fromRgb(); 
443          fp.updateParent(255); 
444      } 
445   
446      public void hsb2rgb() { 
447          if (fp == null) { 
448              Print.println(noFloatMsg); 
449              return; 
450          } 
451          fp.toRgb(); 
452          fp.updateParent(); 
453      } 
454   
455      public void rgb2yuv() { 
456          fp = new Yuv(this); 
457          fp.fromRgb(); 
458          fp.updateParent(); 
459      } 
460   
461      public void yuv2rgb() { 
462          if (fp == null) { 
463              Print.println(noFloatMsg); 
464              return; 
465          } 
466          fp.toRgb(); 
467          fp.updateParent(); 
468      } 
469   
470      public void rgb2hls() { 
471          fp = new Hls(this); 
472          fp.fromRgb(); 
473          fp.updateParent(); 
474      } 
475   
476      public void hls2rgb() { 
477          if (fp == null) { 
478              Print.println(noFloatMsg); 
479              return; 
480          } 
481          fp.toRgb(); 
482          fp.updateParent(); 
483      } 
484   
485      public void wu(int k) { 
486          Wu w = new Wu(); 
487          w.wuQuantization(shortImageBean.getR(), 
488                  shortImageBean.getG(), 
489                  shortImageBean.getB(), 
490                  getImageWidth(), 
491                  getImageHeight(), 
492                  k); 
493          short2Image(); 
494          repaint(); 
495      } 
496   
497      public void medianCut(int k) { 
498          MedianCut mc = new MedianCut(shortImageBean.getPels(), 
499                  getImageWidth(), 
500                  getImageHeight()); 
501          setImage(mc.convert(k)); 
502          repaint(); 
503      } 
504   
505      public void linearCut(int sr, int sg, int sb) { 
506          ShortImageBean.linearCut(shortImageBean.getR(), sr); 
507          ShortImageBean.linearCut(shortImageBean.getG(), sg); 
508          ShortImageBean.linearCut(shortImageBean.getB(), sb); 
509          short2Image(); 
510          repaint(); 
511      } 
512   
513      private void initMenu() { 
514          colorMenu.add(swapMenu); 
515          quantizationMenu.add(linearCutMenu); 
516          quantizationMenu.add(medianCutMenu); 
517          quantizationMenu.add(wuMenu); 
518          quantizationMenu.add(octreeMenu); 
519          colorMenu.add(quantizationMenu); 
520          triStimulusMenu.add(rgbMenu); 
521          colorMenu.add(triStimulusMenu); 
522          getSpatialFilterMenu().add(colorMenu); 
523      } 
524   
525      public void printNumberOfColors() { 
526          Print.println("Computing Colors"); 
527          Print.println("There are " + 
528                  computeNumberOfColors() + 
529                  " colors in this image"); 
530      } 
531   
532      public int computeNumberOfColors() { 
533          ch = new ColorHash(); 
534          ch.addShortArrays(shortImageBean.getR(), 
535                  shortImageBean.getG(), 
536                  shortImageBean.getB()); 
537          return ch.countColors(); 
538      } 
539   
540      public void printColors() { 
541          if (ch == null) computeNumberOfColors(); 
542          ch.printColors(); 
543      } 
544  } 
545   
546