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 $
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 public 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
);
61 } catch (InterruptedException e
) {
62 System
.err
.println("interrupted waiting for pixels!");
65 if ((pg
.getStatus() & ImageObserver
.ABORT
) != 0) {
66 System
.err
.println("image fetch aborted or errored");
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)
84 public static ImageHelper
createImageHelper(String _sFilename
)
85 throws java
.io
.IOException
88 File aFile
= new File(_sFilename
);
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
) {
99 catch(java
.lang
.NoSuchMethodException e
) {
102 catch(java
.lang
.IllegalAccessException e
) {
105 catch(java
.lang
.reflect
.InvocationTargetException e
) {
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
);