merged tag ooo/DEV300_m102
[LibreOffice.git] / qadevOOo / runner / graphical / ImageHelper.java
blobe016e08f0df18cde0bb31f0dead296159c29ef14
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 package graphical;
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;
37 class ImageHelper
39 Image m_aImage;
40 int[] m_aPixels;
41 int m_w = 0;
42 int m_h = 0;
43 boolean m_bGrabbed = false;
45 private ImageHelper(Image _aImage)
47 m_aImage = _aImage;
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
58 pg.grabPixels();
60 catch (InterruptedException e)
62 System.err.println("interrupted waiting for pixels!");
63 return;
65 if ((pg.getStatus() & ImageObserver.ABORT) != 0)
67 System.err.println("image fetch aborted or errored");
68 return;
70 m_bGrabbed = true;
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)
77 return m_aPixels[y * m_w + x];
80 // Write down the current image to a file.
81 // public void storeImage(String _sFilename)
82 // {
83 // }
85 public static ImageHelper createImageHelper(String _sFilename)
86 throws java.io.IOException
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;
97 catch(java.lang.ClassNotFoundException e) {
98 ex = e;
100 catch(java.lang.NoSuchMethodException e) {
101 ex = e;
103 catch(java.lang.IllegalAccessException e) {
104 ex = e;
106 catch(java.lang.reflect.InvocationTargetException e) {
107 ex = e;
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());
117 // aImage = ImageIO.read(aFile);
118 return new ImageHelper(aImage);