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