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