franta-hg@1: /* ================================================================ franta-hg@1: * Cewolf : Chart enabling Web Objects Framework franta-hg@1: * ================================================================ franta-hg@1: * franta-hg@1: * Project Info: http://cewolf.sourceforge.net franta-hg@1: * Project Lead: Guido Laures (guido@laures.de); franta-hg@1: * franta-hg@1: * (C) Copyright 2002, by Guido Laures franta-hg@1: * franta-hg@1: * This library is free software; you can redistribute it and/or modify it under the terms franta-hg@1: * of the GNU Lesser General Public License as published by the Free Software Foundation; franta-hg@1: * either version 2.1 of the License, or (at your option) any later version. franta-hg@1: * franta-hg@1: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; franta-hg@1: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. franta-hg@1: * See the GNU Lesser General Public License for more details. franta-hg@1: * franta-hg@1: * You should have received a copy of the GNU Lesser General Public License along with this franta-hg@1: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, franta-hg@1: * Boston, MA 02111-1307, USA. franta-hg@1: */ franta-hg@1: franta-hg@1: package de.laures.cewolf.util; franta-hg@1: franta-hg@1: import java.awt.Component; franta-hg@1: import java.awt.Graphics; franta-hg@1: import java.awt.Image; franta-hg@1: import java.awt.MediaTracker; franta-hg@1: import java.awt.image.BufferedImage; franta-hg@1: import java.awt.image.ColorModel; franta-hg@1: import java.awt.image.PixelGrabber; franta-hg@1: franta-hg@1: import org.apache.commons.logging.Log; franta-hg@1: import org.apache.commons.logging.LogFactory; franta-hg@1: franta-hg@1: /** franta-hg@1: * Some simple image rendering helper methods. franta-hg@1: * @author Guido Laures franta-hg@1: */ franta-hg@1: public class ImageHelper { franta-hg@1: franta-hg@1: private static final Component comp = new Component() { }; franta-hg@1: private static final MediaTracker tracker = new MediaTracker(comp); franta-hg@1: private static final Log log = LogFactory.getLog(ImageHelper.class); franta-hg@1: franta-hg@1: /** Creates a new instance of ImageHelper */ franta-hg@1: private ImageHelper() { franta-hg@1: } franta-hg@1: franta-hg@1: public static final Image loadImage(String fileName) { franta-hg@1: final Image image = java.awt.Toolkit.getDefaultToolkit().getImage(fileName); franta-hg@1: synchronized(tracker) { franta-hg@1: tracker.addImage(image, 0); franta-hg@1: try { franta-hg@1: tracker.waitForID(0, 0); franta-hg@1: } catch (InterruptedException e) { franta-hg@1: log.debug("INTERRUPTED while loading Image"); franta-hg@1: } franta-hg@1: tracker.removeImage(image, 0); franta-hg@1: } franta-hg@1: return image; franta-hg@1: } franta-hg@1: franta-hg@1: public static BufferedImage loadBufferedImage(String fileName) { franta-hg@1: Image image = loadImage(fileName); franta-hg@1: if (image instanceof BufferedImage) { franta-hg@1: return (BufferedImage)image; franta-hg@1: } franta-hg@1: /* final boolean hasAlpha = hasAlpha(image); franta-hg@1: int transparency = Transparency.OPAQUE; franta-hg@1: if (hasAlpha) { franta-hg@1: transparency = Transparency.BITMASK; franta-hg@1: }*/ franta-hg@1: int width = (int)Math.max(1.0, image.getWidth(null)); franta-hg@1: int height = (int)Math.max(1.0, image.getHeight(null)); franta-hg@1: // BufferedImage bimage = GRAPHICS_CONV.createCompatibleImage(width, height, transparency); franta-hg@1: BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); franta-hg@1: final Graphics g = bimage.createGraphics(); franta-hg@1: g.drawImage(image, 0, 0, null); franta-hg@1: g.dispose(); franta-hg@1: return bimage; franta-hg@1: } franta-hg@1: franta-hg@1: public static boolean hasAlpha(Image image) { franta-hg@1: if (image instanceof BufferedImage) { franta-hg@1: return ((BufferedImage)image).getColorModel().hasAlpha(); franta-hg@1: } franta-hg@1: PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false); franta-hg@1: try { franta-hg@1: pg.grabPixels(); franta-hg@1: } catch (InterruptedException e) { franta-hg@1: e.printStackTrace(); franta-hg@1: } franta-hg@1: ColorModel cm = pg.getColorModel(); franta-hg@1: if(cm == null){ franta-hg@1: return false; franta-hg@1: } franta-hg@1: return cm.hasAlpha(); franta-hg@1: } franta-hg@1: franta-hg@1: public static BufferedImage createImage(int width, int height) { franta-hg@1: // return GRAPHICS_CONV.createCompatibleImage(width, height); franta-hg@1: return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); franta-hg@1: } franta-hg@1: franta-hg@1: }