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 graphical;
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     private 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          {
54              pg.grabPixels();
55          }
56          catch (InterruptedException e)
57          {
58              System.err.println("interrupted waiting for pixels!");
59              return;
60          }
61          if ((pg.getStatus() & ImageObserver.ABORT) != 0)
62          {
63              System.err.println("image fetch aborted or errored");
64              return;
65          }
66          m_bGrabbed = true;
67     }
getWidth()68     public int getWidth() {return m_aImage.getWidth(null);}
getHeight()69     public int getHeight() {return m_aImage.getHeight(null);}
70     // direct access to a pixel
getPixel(final int x, final int y)71     public int getPixel(final int x, final int y)
72         {
73             return m_aPixels[y * m_w + x];
74         }
75 
76     // Write down the current image to a file.
77     // public void storeImage(String _sFilename)
78     //    {
79     //    }
80 
createImageHelper(String _sFilename)81     public static ImageHelper createImageHelper(String _sFilename)
82         throws java.io.IOException
83         {
84             Image aImage = null;
85             File aFile = new File(_sFilename);
86             Exception ex = null;
87             try {
88                 Class imageIOClass = Class.forName("javax.imageio.ImageIO");
89                 Method readMethod = imageIOClass.getDeclaredMethod("read", new Class[]{java.io.File.class});
90                 Object retValue = readMethod.invoke(imageIOClass, new Object[]{aFile});
91                 aImage = (Image)retValue;
92             }
93             catch(java.lang.ClassNotFoundException e) {
94                 ex = e;
95             }
96             catch(java.lang.NoSuchMethodException e) {
97                 ex = e;
98             }
99             catch(java.lang.IllegalAccessException e) {
100                 ex = e;
101             }
102             catch(java.lang.reflect.InvocationTargetException e) {
103                 ex = e;
104             }
105 
106             if (ex != null) {
107                 // get Java version:
108                 String javaVersion = System.getProperty("java.version");
109                 throw new java.io.IOException(
110                     "Cannot construct object with current Java version " +
111                     javaVersion + ": " + ex.getMessage());
112             }
113 //            aImage = ImageIO.read(aFile);
114             return new ImageHelper(aImage);
115         }
116 }
117