1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package convwatch;
25 
26 import java.awt.Image;
27 import java.awt.image.PixelGrabber;
28 import java.awt.image.ImageObserver;
29 import java.io.File;
30 //import javax.imageio.ImageIO;
31 import java.lang.reflect.Method;
32 
33 class ImageHelper
34 {
35     Image m_aImage;
36     int[] m_aPixels;
37     int m_w = 0;
38     int m_h = 0;
39     boolean m_bGrabbed = false;
40 
ImageHelper(Image _aImage)41     public ImageHelper(Image _aImage)
42     {
43          m_aImage = _aImage;
44 
45          // grab all (consume much memory)
46          m_w = getWidth();
47          m_h = getHeight();
48 		 int x = 0;
49 		 int y = 0;
50          m_aPixels = new int[m_w * m_h];
51          PixelGrabber pg = new PixelGrabber(m_aImage, x, y, m_w, m_h, m_aPixels, 0, m_w);
52          try {
53              pg.grabPixels();
54          } catch (InterruptedException e) {
55              System.err.println("interrupted waiting for pixels!");
56              return;
57          }
58          if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
59              System.err.println("image fetch aborted or errored");
60              return;
61          }
62          m_bGrabbed = true;
63     }
getWidth()64     public int getWidth() {return m_aImage.getWidth(null);}
getHeight()65     public int getHeight() {return m_aImage.getHeight(null);}
66     // direct access to a pixel
getPixel(int x, int y)67     public int getPixel(int x, int y)
68         {
69             return m_aPixels[y * m_w + x];
70         }
71 
72     // Write down the current image to a file.
73     // public void storeImage(String _sFilename)
74     //    {
75     //    }
76 
createImageHelper(String _sFilename)77     public static ImageHelper createImageHelper(String _sFilename)
78         throws java.io.IOException
79         {
80             Image aImage = null;
81             File aFile = new File(_sFilename);
82             Exception ex = null;
83             try {
84                 Class imageIOClass = Class.forName("javax.imageio.ImageIO");
85                 Method readMethod = imageIOClass.getDeclaredMethod("read", new Class[]{java.io.File.class});
86                 Object retValue = readMethod.invoke(imageIOClass, new Object[]{aFile});
87                 aImage = (Image)retValue;
88             }
89             catch(java.lang.ClassNotFoundException e) {
90                 ex = e;
91             }
92             catch(java.lang.NoSuchMethodException e) {
93                 ex = e;
94             }
95             catch(java.lang.IllegalAccessException e) {
96                 ex = e;
97             }
98             catch(java.lang.reflect.InvocationTargetException e) {
99                 ex = e;
100             }
101 
102             if (ex != null) {
103                 // get Java version:
104                 String javaVersion = System.getProperty("java.version");
105                 throw new java.io.IOException(
106                     "Cannot construct object with current Java version " +
107                     javaVersion + ": " + ex.getMessage());
108             }
109 //            aImage = ImageIO.read(aFile);
110             return new ImageHelper(aImage);
111         }
112 }
113