1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10*ef39d40dSAndrew Rist * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ef39d40dSAndrew Rist * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19*ef39d40dSAndrew Rist * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package graphical; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import java.awt.Image; 27cdf0e10cSrcweir import java.awt.image.PixelGrabber; 28cdf0e10cSrcweir import java.awt.image.ImageObserver; 29cdf0e10cSrcweir import java.io.File; 30cdf0e10cSrcweir //import javax.imageio.ImageIO; 31cdf0e10cSrcweir import java.lang.reflect.Method; 32cdf0e10cSrcweir 33cdf0e10cSrcweir class ImageHelper 34cdf0e10cSrcweir { 35cdf0e10cSrcweir Image m_aImage; 36cdf0e10cSrcweir int[] m_aPixels; 37cdf0e10cSrcweir int m_w = 0; 38cdf0e10cSrcweir int m_h = 0; 39cdf0e10cSrcweir boolean m_bGrabbed = false; 40cdf0e10cSrcweir ImageHelper(Image _aImage)41cdf0e10cSrcweir private ImageHelper(Image _aImage) 42cdf0e10cSrcweir { 43cdf0e10cSrcweir m_aImage = _aImage; 44cdf0e10cSrcweir 45cdf0e10cSrcweir // grab all (consume much memory) 46cdf0e10cSrcweir m_w = getWidth(); 47cdf0e10cSrcweir m_h = getHeight(); 48cdf0e10cSrcweir int x = 0; 49cdf0e10cSrcweir int y = 0; 50cdf0e10cSrcweir m_aPixels = new int[m_w * m_h]; 51cdf0e10cSrcweir PixelGrabber pg = new PixelGrabber(m_aImage, x, y, m_w, m_h, m_aPixels, 0, m_w); 52cdf0e10cSrcweir try 53cdf0e10cSrcweir { 54cdf0e10cSrcweir pg.grabPixels(); 55cdf0e10cSrcweir } 56cdf0e10cSrcweir catch (InterruptedException e) 57cdf0e10cSrcweir { 58cdf0e10cSrcweir System.err.println("interrupted waiting for pixels!"); 59cdf0e10cSrcweir return; 60cdf0e10cSrcweir } 61cdf0e10cSrcweir if ((pg.getStatus() & ImageObserver.ABORT) != 0) 62cdf0e10cSrcweir { 63cdf0e10cSrcweir System.err.println("image fetch aborted or errored"); 64cdf0e10cSrcweir return; 65cdf0e10cSrcweir } 66cdf0e10cSrcweir m_bGrabbed = true; 67cdf0e10cSrcweir } getWidth()68cdf0e10cSrcweir public int getWidth() {return m_aImage.getWidth(null);} getHeight()69cdf0e10cSrcweir public int getHeight() {return m_aImage.getHeight(null);} 70cdf0e10cSrcweir // direct access to a pixel getPixel(final int x, final int y)71cdf0e10cSrcweir public int getPixel(final int x, final int y) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir return m_aPixels[y * m_w + x]; 74cdf0e10cSrcweir } 75cdf0e10cSrcweir 76cdf0e10cSrcweir // Write down the current image to a file. 77cdf0e10cSrcweir // public void storeImage(String _sFilename) 78cdf0e10cSrcweir // { 79cdf0e10cSrcweir // } 80cdf0e10cSrcweir createImageHelper(String _sFilename)81cdf0e10cSrcweir public static ImageHelper createImageHelper(String _sFilename) 82cdf0e10cSrcweir throws java.io.IOException 83cdf0e10cSrcweir { 84cdf0e10cSrcweir Image aImage = null; 85cdf0e10cSrcweir File aFile = new File(_sFilename); 86cdf0e10cSrcweir Exception ex = null; 87cdf0e10cSrcweir try { 88cdf0e10cSrcweir Class imageIOClass = Class.forName("javax.imageio.ImageIO"); 89cdf0e10cSrcweir Method readMethod = imageIOClass.getDeclaredMethod("read", new Class[]{java.io.File.class}); 90cdf0e10cSrcweir Object retValue = readMethod.invoke(imageIOClass, new Object[]{aFile}); 91cdf0e10cSrcweir aImage = (Image)retValue; 92cdf0e10cSrcweir } 93cdf0e10cSrcweir catch(java.lang.ClassNotFoundException e) { 94cdf0e10cSrcweir ex = e; 95cdf0e10cSrcweir } 96cdf0e10cSrcweir catch(java.lang.NoSuchMethodException e) { 97cdf0e10cSrcweir ex = e; 98cdf0e10cSrcweir } 99cdf0e10cSrcweir catch(java.lang.IllegalAccessException e) { 100cdf0e10cSrcweir ex = e; 101cdf0e10cSrcweir } 102cdf0e10cSrcweir catch(java.lang.reflect.InvocationTargetException e) { 103cdf0e10cSrcweir ex = e; 104cdf0e10cSrcweir } 105cdf0e10cSrcweir 106cdf0e10cSrcweir if (ex != null) { 107cdf0e10cSrcweir // get Java version: 108cdf0e10cSrcweir String javaVersion = System.getProperty("java.version"); 109cdf0e10cSrcweir throw new java.io.IOException( 110cdf0e10cSrcweir "Cannot construct object with current Java version " + 111cdf0e10cSrcweir javaVersion + ": " + ex.getMessage()); 112cdf0e10cSrcweir } 113cdf0e10cSrcweir // aImage = ImageIO.read(aFile); 114cdf0e10cSrcweir return new ImageHelper(aImage); 115cdf0e10cSrcweir } 116cdf0e10cSrcweir } 117