1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package graphical;
29 
30 import java.awt.Image;
31 import java.awt.image.PixelGrabber;
32 import java.awt.image.ImageObserver;
33 import java.io.File;
34 //import javax.imageio.ImageIO;
35 import java.lang.reflect.Method;
36 
37 class ImageHelper
38 {
39     Image m_aImage;
40     int[] m_aPixels;
41     int m_w = 0;
42     int m_h = 0;
43     boolean m_bGrabbed = false;
44 
45     private ImageHelper(Image _aImage)
46     {
47          m_aImage = _aImage;
48 
49          // grab all (consume much memory)
50          m_w = getWidth();
51          m_h = getHeight();
52 		 int x = 0;
53 		 int y = 0;
54          m_aPixels = new int[m_w * m_h];
55          PixelGrabber pg = new PixelGrabber(m_aImage, x, y, m_w, m_h, m_aPixels, 0, m_w);
56          try
57          {
58              pg.grabPixels();
59          }
60          catch (InterruptedException e)
61          {
62              System.err.println("interrupted waiting for pixels!");
63              return;
64          }
65          if ((pg.getStatus() & ImageObserver.ABORT) != 0)
66          {
67              System.err.println("image fetch aborted or errored");
68              return;
69          }
70          m_bGrabbed = true;
71     }
72     public int getWidth() {return m_aImage.getWidth(null);}
73     public int getHeight() {return m_aImage.getHeight(null);}
74     // direct access to a pixel
75     public int getPixel(final int x, final int y)
76         {
77             return m_aPixels[y * m_w + x];
78         }
79 
80     // Write down the current image to a file.
81     // public void storeImage(String _sFilename)
82     //    {
83     //    }
84 
85     public static ImageHelper createImageHelper(String _sFilename)
86         throws java.io.IOException
87         {
88             Image aImage = null;
89             File aFile = new File(_sFilename);
90             Exception ex = null;
91             try {
92                 Class imageIOClass = Class.forName("javax.imageio.ImageIO");
93                 Method readMethod = imageIOClass.getDeclaredMethod("read", new Class[]{java.io.File.class});
94                 Object retValue = readMethod.invoke(imageIOClass, new Object[]{aFile});
95                 aImage = (Image)retValue;
96             }
97             catch(java.lang.ClassNotFoundException e) {
98                 ex = e;
99             }
100             catch(java.lang.NoSuchMethodException e) {
101                 ex = e;
102             }
103             catch(java.lang.IllegalAccessException e) {
104                 ex = e;
105             }
106             catch(java.lang.reflect.InvocationTargetException e) {
107                 ex = e;
108             }
109 
110             if (ex != null) {
111                 // get Java version:
112                 String javaVersion = System.getProperty("java.version");
113                 throw new java.io.IOException(
114                     "Cannot construct object with current Java version " +
115                     javaVersion + ": " + ex.getMessage());
116             }
117 //            aImage = ImageIO.read(aFile);
118             return new ImageHelper(aImage);
119         }
120 }
121