1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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 ************************************************************************/
33 import java
.awt
.Image
;
34 import java
.awt
.image
.PixelGrabber
;
35 import java
.awt
.image
.ImageObserver
;
37 //import javax.imageio.ImageIO;
38 import java
.lang
.reflect
.Method
;
46 boolean m_bGrabbed
= false;
48 private ImageHelper(Image _aImage
)
52 // grab all (consume much memory)
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
);
63 catch (InterruptedException e
)
65 System
.err
.println("interrupted waiting for pixels!");
68 if ((pg
.getStatus() & ImageObserver
.ABORT
) != 0)
70 System
.err
.println("image fetch aborted or errored");
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)
88 public static ImageHelper
createImageHelper(String _sFilename
)
89 throws java
.io
.IOException
92 File aFile
= new File(_sFilename
);
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
) {
103 catch(java
.lang
.NoSuchMethodException e
) {
106 catch(java
.lang
.IllegalAccessException e
) {
109 catch(java
.lang
.reflect
.InvocationTargetException e
) {
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
);