bump product version to 4.1.6.2
[LibreOffice.git] / qadevOOo / runner / graphical / ImageHelper.java
blobbeea44fe056d4708663360b7da234f7e7b0f41bf
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package graphical;
21 import java.awt.Image;
22 import java.awt.image.PixelGrabber;
23 import java.awt.image.ImageObserver;
24 import java.io.File;
25 //import javax.imageio.ImageIO;
26 import java.lang.reflect.Method;
28 class ImageHelper
30 Image m_aImage;
31 int[] m_aPixels;
32 int m_w = 0;
33 int m_h = 0;
34 boolean m_bGrabbed = false;
36 private ImageHelper(Image _aImage)
38 m_aImage = _aImage;
40 // grab all (consume much memory)
41 m_w = getWidth();
42 m_h = getHeight();
43 int x = 0;
44 int y = 0;
45 m_aPixels = new int[m_w * m_h];
46 PixelGrabber pg = new PixelGrabber(m_aImage, x, y, m_w, m_h, m_aPixels, 0, m_w);
47 try
49 pg.grabPixels();
51 catch (InterruptedException e)
53 System.err.println("interrupted waiting for pixels!");
54 return;
56 if ((pg.getStatus() & ImageObserver.ABORT) != 0)
58 System.err.println("image fetch aborted or errored");
59 return;
61 m_bGrabbed = true;
63 public int getWidth() {return m_aImage.getWidth(null);}
64 public int getHeight() {return m_aImage.getHeight(null);}
65 // direct access to a pixel
66 public int getPixel(final int x, final int y)
68 return m_aPixels[y * m_w + x];
71 // Write down the current image to a file.
72 // public void storeImage(String _sFilename)
73 // {
74 // }
76 public static ImageHelper createImageHelper(String _sFilename)
77 throws java.io.IOException
79 Image aImage = null;
80 File aFile = new File(_sFilename);
81 Exception ex = null;
82 try {
83 Class<?> imageIOClass = Class.forName("javax.imageio.ImageIO");
84 Method readMethod = imageIOClass.getDeclaredMethod("read", new Class[]{java.io.File.class});
85 Object retValue = readMethod.invoke(imageIOClass, new Object[]{aFile});
86 aImage = (Image)retValue;
88 catch(java.lang.ClassNotFoundException e) {
89 ex = e;
91 catch(java.lang.NoSuchMethodException e) {
92 ex = e;
94 catch(java.lang.IllegalAccessException e) {
95 ex = e;
97 catch(java.lang.reflect.InvocationTargetException e) {
98 ex = e;
101 if (ex != null) {
102 // get Java version:
103 String javaVersion = System.getProperty("java.version");
104 throw new java.io.IOException(
105 "Cannot construct object with current Java version " +
106 javaVersion + ": " + ex.getMessage());
108 // aImage = ImageIO.read(aFile);
109 return new ImageHelper(aImage);