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 ************************************************************************/
30 import java
.awt
.Image
;
31 import java
.awt
.image
.PixelGrabber
;
32 import java
.awt
.image
.ImageObserver
;
34 //import javax.imageio.ImageIO;
35 import java
.lang
.reflect
.Method
;
43 boolean m_bGrabbed
= false;
45 public ImageHelper(Image _aImage
)
49 // grab all (consume much memory)
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
);
58 } catch (InterruptedException e
) {
59 System
.err
.println("interrupted waiting for pixels!");
62 if ((pg
.getStatus() & ImageObserver
.ABORT
) != 0) {
63 System
.err
.println("image fetch aborted or errored");
68 public int getWidth() {return m_aImage
.getWidth(null);}
69 public int getHeight() {return m_aImage
.getHeight(null);}
70 // direct access to a pixel
71 public int getPixel(int x
, int y
)
73 return m_aPixels
[y
* m_w
+ x
];
76 // Write down the current image to a file.
77 // public void storeImage(String _sFilename)
81 public static ImageHelper
createImageHelper(String _sFilename
)
82 throws java
.io
.IOException
85 File aFile
= new File(_sFilename
);
88 Class imageIOClass
= Class
.forName("javax.imageio.ImageIO");
89 Method readMethod
= imageIOClass
.getDeclaredMethod("read", new Class
[]{java
.io
.File
.class});
90 Object retValue
= readMethod
.invoke(imageIOClass
, new Object
[]{aFile
});
91 aImage
= (Image
)retValue
;
93 catch(java
.lang
.ClassNotFoundException e
) {
96 catch(java
.lang
.NoSuchMethodException e
) {
99 catch(java
.lang
.IllegalAccessException e
) {
102 catch(java
.lang
.reflect
.InvocationTargetException e
) {
108 String javaVersion
= System
.getProperty("java.version");
109 throw new java
.io
.IOException(
110 "Cannot construct object with current Java version " +
111 javaVersion
+ ": " + ex
.getMessage());
113 // aImage = ImageIO.read(aFile);
114 return new ImageHelper(aImage
);