/Users/lyon/j4p/src/face/ImageCanvas.java

1    package face; 
2     
3    import java.awt.*; 
4    import java.awt.image.MemoryImageSource; 
5     
6    /** 
7     * Canvas used to display any type of image. As long as its submitted 
8     * in byte[], int[] or double[] format with the appropiate 
9     * width and height. Also the image is converted into grayscale. 
10    * 
11    */ 
12   class ImageCanvas extends Canvas { 
13    
14    
15       private Image memImage = null;      // image constructed from PPM data 
16    
17       /** 
18        * Image displayed from the given array. 
19        * 
20        * @param bytes the byte array with the each element being an 8bit RGB 
21        * tuple 
22        * @param width The width of the iamge 
23        * @param height The height of the image 
24        */ 
25       public void readImage(byte[] bytes, int width, int height) { 
26    
27           int pix[] = new int[width * height]; 
28           int index = 0; 
29           int ofs = 0; 
30    
31           for (index = 0; index < pix.length - 2; index++) { 
32               pix[index] = 255 << 24 /*alpha*/ | 
33                       (bytes[ofs] & 0xFF) << 16 /*R*/ | 
34                       (bytes[ofs] & 0xFF) << 8 /*G*/ | 
35                       (bytes[ofs] & 0xFF) /*B*/; 
36               ofs += 1; 
37           } 
38    
39           memImage = createImage(new MemoryImageSource(width, height, pix, 0, width)); 
40           repaint(); 
41       } 
42    
43       /** 
44        * Image displayed from the given array. 
45        * 
46        * @param doubles the double array with the each element being an 64bit RGB 
47        * tuple. The alpha color is reset to FF and only the 24bits (from left to 
48        * right of each element are displayed). 
49        * 
50        * @param width The width of the iamge 
51        * @param height The height of the image 
52        */ 
53    
54       public void readImage(double[] doubles, int width, int height) { 
55    
56    
57           // construct Image from binary PPM color data. 
58    
59    
60           int w = width; 
61           int h = height; 
62    
63           int pix[] = new int[w * h]; 
64           int index = 0; 
65           int avg = 0; 
66           for (index = 0; index < pix.length - 2; index++) { 
67               //avg = (int) ((doubles[index] + doubles[index+1] + doubles[index+2]) / 3); 
68               avg = (int) doubles[index]; 
69               pix[index] = 255 << 24 | /* avg << 16 | avg << 8 |*/ avg; 
70    
71    
72           } 
73    
74           memImage = createImage(new MemoryImageSource(width, height, pix, 0, width)); 
75           repaint(); 
76       } 
77    
78       /** 
79        * Image displayed from the given array. 
80        * 
81        * @param ints the int array with the each element being an 32bit RGB 
82        * tuple. No conversion done. 
83        * 
84        * @param width The width of the iamge 
85        * @param height The height of the image 
86        */ 
87    
88       public void readImage(int[] ints, int width, int height) { 
89    
90           memImage = createImage(new MemoryImageSource(width, height, ints, 0, width)); 
91           repaint(); 
92       } 
93    
94       /** 
95        * Paint on our given object the given image. 
96        * 
97        */ 
98       public void paint(Graphics g) { 
99           Dimension d = getSize();      // get size of drawing area 
100          g.setColor(getBackground());  // clear drawing area 
101          g.fillRect(0, 0, d.width, d.height); 
102          g.setColor(getForeground()); 
103   
104          if (memImage != null) { 
105              g.drawImage(memImage, 0, 0, this); 
106          } 
107      } 
108  }