java/cewolf-1.0/src/main/java/de/laures/cewolf/util/ImageHelper.java
changeset 1 639991d0808a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/cewolf-1.0/src/main/java/de/laures/cewolf/util/ImageHelper.java	Sat Feb 28 21:31:02 2009 +0100
     1.3 @@ -0,0 +1,106 @@
     1.4 +/* ================================================================
     1.5 + * Cewolf : Chart enabling Web Objects Framework
     1.6 + * ================================================================
     1.7 + *
     1.8 + * Project Info:  http://cewolf.sourceforge.net
     1.9 + * Project Lead:  Guido Laures (guido@laures.de);
    1.10 + *
    1.11 + * (C) Copyright 2002, by Guido Laures
    1.12 + *
    1.13 + * This library is free software; you can redistribute it and/or modify it under the terms
    1.14 + * of the GNU Lesser General Public License as published by the Free Software Foundation;
    1.15 + * either version 2.1 of the License, or (at your option) any later version.
    1.16 + *
    1.17 + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    1.18 + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.19 + * See the GNU Lesser General Public License for more details.
    1.20 + *
    1.21 + * You should have received a copy of the GNU Lesser General Public License along with this
    1.22 + * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    1.23 + * Boston, MA 02111-1307, USA.
    1.24 + */
    1.25 +
    1.26 +package de.laures.cewolf.util;
    1.27 +
    1.28 +import java.awt.Component;
    1.29 +import java.awt.Graphics;
    1.30 +import java.awt.Image;
    1.31 +import java.awt.MediaTracker;
    1.32 +import java.awt.image.BufferedImage;
    1.33 +import java.awt.image.ColorModel;
    1.34 +import java.awt.image.PixelGrabber;
    1.35 +
    1.36 +import org.apache.commons.logging.Log;
    1.37 +import org.apache.commons.logging.LogFactory;
    1.38 +
    1.39 +/** 
    1.40 + * Some simple image rendering helper methods.
    1.41 + * @author  Guido Laures 
    1.42 + */
    1.43 +public class ImageHelper {
    1.44 +
    1.45 +    private static final Component comp = new Component() { };
    1.46 +    private static final MediaTracker tracker = new MediaTracker(comp);
    1.47 +    private static final Log log = LogFactory.getLog(ImageHelper.class);
    1.48 +
    1.49 +    /** Creates a new instance of ImageHelper */
    1.50 +    private ImageHelper() {
    1.51 +    }
    1.52 +
    1.53 +    public static final Image loadImage(String fileName) {
    1.54 +        final Image image = java.awt.Toolkit.getDefaultToolkit().getImage(fileName);
    1.55 +        synchronized(tracker) {
    1.56 +            tracker.addImage(image, 0);
    1.57 +            try {
    1.58 +                tracker.waitForID(0, 0);
    1.59 +            } catch (InterruptedException e) {
    1.60 +                log.debug("INTERRUPTED while loading Image");
    1.61 +            }
    1.62 +            tracker.removeImage(image, 0);
    1.63 +        }
    1.64 +        return image;
    1.65 +    }
    1.66 +
    1.67 +    public static BufferedImage loadBufferedImage(String fileName) {
    1.68 +        Image image = loadImage(fileName);
    1.69 +        if (image instanceof BufferedImage) {
    1.70 +            return (BufferedImage)image;
    1.71 +        }
    1.72 +        /*        final boolean hasAlpha = hasAlpha(image);
    1.73 +       int transparency = Transparency.OPAQUE;
    1.74 +        if (hasAlpha) {
    1.75 +            transparency = Transparency.BITMASK;
    1.76 +        }*/
    1.77 +        int width = (int)Math.max(1.0, image.getWidth(null));
    1.78 +        int height = (int)Math.max(1.0, image.getHeight(null));
    1.79 +        // BufferedImage bimage = GRAPHICS_CONV.createCompatibleImage(width, height, transparency);
    1.80 +        BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    1.81 +        final Graphics g = bimage.createGraphics();
    1.82 +        g.drawImage(image, 0, 0, null);
    1.83 +        g.dispose();
    1.84 +        return bimage;
    1.85 +    }
    1.86 +
    1.87 +    public static boolean hasAlpha(Image image) {
    1.88 +        if (image instanceof BufferedImage) {
    1.89 +            return ((BufferedImage)image).getColorModel().hasAlpha();
    1.90 +        }
    1.91 +        PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
    1.92 +        try {
    1.93 +            pg.grabPixels();
    1.94 +        } catch (InterruptedException e) {
    1.95 +            e.printStackTrace();
    1.96 +        }
    1.97 +        ColorModel cm = pg.getColorModel();
    1.98 +        if(cm == null){
    1.99 +        	return false;
   1.100 +        }
   1.101 +        return cm.hasAlpha();
   1.102 +    }
   1.103 +
   1.104 +    public static BufferedImage createImage(int width, int height) {
   1.105 +        // return GRAPHICS_CONV.createCompatibleImage(width, height);
   1.106 +        return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
   1.107 +    }
   1.108 +
   1.109 +}