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 convwatch;
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     public 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              pg.grabPixels();
58          } catch (InterruptedException e) {
59              System.err.println("interrupted waiting for pixels!");
60              return;
61          }
62          if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
63              System.err.println("image fetch aborted or errored");
64              return;
65          }
66          m_bGrabbed = true;
67     }
68     public int getWidth() {return m_aImage.getWidth(null);}
69     public int getHeight() {return m_aImage.getHeight(null);}
70     // direct access to a pixel
71     public int getPixel(int x, 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 
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