001: /* ================================================================
002: * Cewolf : Chart enabling Web Objects Framework
003: * ================================================================
004: *
005: * Project Info: http://cewolf.sourceforge.net
006: * Project Lead: Guido Laures (guido@laures.de);
007: *
008: * (C) Copyright 2002, by Guido Laures
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package de.laures.cewolf.util;
024:
025: import java.awt.Component;
026: import java.awt.Graphics;
027: import java.awt.Image;
028: import java.awt.MediaTracker;
029: import java.awt.image.BufferedImage;
030: import java.awt.image.ColorModel;
031: import java.awt.image.PixelGrabber;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: /**
037: * Some simple image rendering helper methods.
038: * @author Guido Laures
039: */
040: public class ImageHelper {
041:
042: private static final Component comp = new Component() {
043: };
044: private static final MediaTracker tracker = new MediaTracker(comp);
045: private static final Log log = LogFactory.getLog(ImageHelper.class);
046:
047: /** Creates a new instance of ImageHelper */
048: private ImageHelper() {
049: }
050:
051: public static final Image loadImage(String fileName) {
052: final Image image = java.awt.Toolkit.getDefaultToolkit()
053: .getImage(fileName);
054: synchronized (tracker) {
055: tracker.addImage(image, 0);
056: try {
057: tracker.waitForID(0, 0);
058: } catch (InterruptedException e) {
059: log.debug("INTERRUPTED while loading Image");
060: }
061: tracker.removeImage(image, 0);
062: }
063: return image;
064: }
065:
066: public static BufferedImage loadBufferedImage(String fileName) {
067: Image image = loadImage(fileName);
068: if (image instanceof BufferedImage) {
069: return (BufferedImage) image;
070: }
071: /* final boolean hasAlpha = hasAlpha(image);
072: int transparency = Transparency.OPAQUE;
073: if (hasAlpha) {
074: transparency = Transparency.BITMASK;
075: }*/
076: int width = (int) Math.max(1.0, image.getWidth(null));
077: int height = (int) Math.max(1.0, image.getHeight(null));
078: // BufferedImage bimage = GRAPHICS_CONV.createCompatibleImage(width, height, transparency);
079: BufferedImage bimage = new BufferedImage(width, height,
080: BufferedImage.TYPE_INT_RGB);
081: final Graphics g = bimage.createGraphics();
082: g.drawImage(image, 0, 0, null);
083: g.dispose();
084: return bimage;
085: }
086:
087: public static boolean hasAlpha(Image image) {
088: if (image instanceof BufferedImage) {
089: return ((BufferedImage) image).getColorModel().hasAlpha();
090: }
091: PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
092: try {
093: pg.grabPixels();
094: } catch (InterruptedException e) {
095: e.printStackTrace();
096: }
097: ColorModel cm = pg.getColorModel();
098: if (cm == null) {
099: return false;
100: }
101: return cm.hasAlpha();
102: }
103:
104: public static BufferedImage createImage(int width, int height) {
105: // return GRAPHICS_CONV.createCompatibleImage(width, height);
106: return new BufferedImage(width, height,
107: BufferedImage.TYPE_INT_RGB);
108: }
109:
110: }
|