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