001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: //$Id: ImageUtils.java,v 1.7 2005/02/16 11:28:14 jesper Exp $
023: package net.infonode.util; // Generated package name
024:
025: import net.infonode.util.math.Int4;
026:
027: import java.awt.*;
028: import java.awt.Image;
029: import java.awt.geom.AffineTransform;
030: import java.awt.image.ImageObserver;
031: import java.awt.image.PixelGrabber;
032: import java.net.URL;
033:
034: public final class ImageUtils {
035: public static final Image create(String filename)
036: throws ImageException {
037: Image image = Toolkit.getDefaultToolkit().createImage(filename);
038: waitImage(image);
039: return image;
040: }
041:
042: public static final Image create(URL url) throws ImageException {
043: Image image = Toolkit.getDefaultToolkit().createImage(url);
044: waitImage(image);
045: return image;
046: }
047:
048: public static final Image create(byte data[]) throws ImageException {
049: Image image = Toolkit.getDefaultToolkit().createImage(data);
050: waitImage(image);
051: return image;
052: }
053:
054: public static final void waitImage(Image image)
055: throws ImageException {
056: MediaTracker tracker = new MediaTracker(new Canvas()); //use dummy component
057: tracker.addImage(image, 1);
058:
059: try {
060: tracker.waitForID(1);
061: } catch (InterruptedException e) {
062: throw new ImageException("Interrupted when creating image!");
063: }
064: }
065:
066: public static final int[] getPixels(Image image)
067: throws ImageException {
068: return getPixels(image, 0, 0, image.getWidth(null), image
069: .getHeight(null));
070: }
071:
072: public static final int[] getPixels(Image image, int x, int y,
073: int width, int height) throws ImageException {
074: int[] pixels = new int[width * height];
075: PixelGrabber pg = new PixelGrabber(image, x, y, width, height,
076: pixels, 0, width);
077: try {
078: pg.grabPixels();
079: } catch (InterruptedException e) {
080: throw new ImageException("Interrupted waiting for pixels");
081: }
082:
083: if ((pg.getStatus() & ImageObserver.ABORT) != 0)
084: throw new ImageException("Image fetch aborted or errored");
085:
086: return pixels;
087: }
088:
089: public static final int getAlpha(int pixel) {
090: return (pixel >> 24) & 0xff;
091: }
092:
093: public static final int getRed(int pixel) {
094: return (pixel >> 16) & 0xff;
095: }
096:
097: public static final int getGreen(int pixel) {
098: return (pixel >> 8) & 0xff;
099: }
100:
101: public static final int getBlue(int pixel) {
102: return pixel & 0xff;
103: }
104:
105: public static final int createPixel(int red, int green, int blue) {
106: return (0xff << 24) | (red << 16) | (green << 8) | blue;
107: }
108:
109: public static int toIntColor(Int4 i) {
110: return ((i.getD() << 8) & 0xff000000) | (i.getA() & 0xff0000)
111: | ((i.getB() >> 8) & 0xff00)
112: | ((i.getC() >> 16) & 0xff);
113: }
114:
115: public static Int4 toInt4(Color c) {
116: return new Int4(c.getRed() << 16, c.getGreen() << 16, c
117: .getBlue() << 16, c.getAlpha() << 16);
118: }
119:
120: public static Color toColor(Int4 c) {
121: return new Color(c.getA() >> 16, c.getB() >> 16,
122: c.getC() >> 16, c.getD() >> 16);
123: }
124:
125: public static final int[] createGradientPixels(Color[] colors,
126: int width, int height) {
127: int[] pixels = new int[width * height];
128: createGradientPixels(colors, width, height, pixels);
129: return pixels;
130: }
131:
132: public static final int[] createGradientPixels(Color[] colors,
133: int width, int height, int[] pixels) {
134: int p = 0;
135:
136: Int4 c1 = toInt4(colors[0]);
137: Int4 c2 = toInt4(colors[1]);
138: Int4 dc1 = toInt4(colors[2]).sub(toInt4(colors[0])).div(height);
139: Int4 dc2 = toInt4(colors[3]).sub(toInt4(colors[1])).div(height);
140: Int4 d = new Int4();
141: Int4 c = new Int4();
142:
143: for (int y = 0; y < height; y++) {
144: d.set(c2).sub(c1).div(width);
145: c.set(c1);
146:
147: for (int stop = p + width; p < stop; p++) {
148: pixels[p] = toIntColor(c);
149: c.add(d);
150: }
151:
152: c1.add(dc1);
153: c2.add(dc2);
154: }
155:
156: return pixels;
157: }
158:
159: public static AffineTransform createTransform(Direction direction,
160: boolean horizontalFlip, boolean verticalFlip, int width,
161: int height) {
162: int hf = horizontalFlip ? -1 : 1;
163: int vf = verticalFlip ? -1 : 1;
164: int m00 = direction == Direction.RIGHT ? hf
165: : direction == Direction.LEFT ? -hf : 0;
166: int m01 = direction == Direction.DOWN ? -vf
167: : direction == Direction.UP ? vf : 0;
168: int m10 = direction == Direction.DOWN ? hf
169: : direction == Direction.UP ? -hf : 0;
170: int m11 = direction == Direction.RIGHT ? vf
171: : direction == Direction.LEFT ? -vf : 0;
172: return new AffineTransform(m00, m10, m01, m11,
173: m00 == -1 ? width : m01 == -1 ? height : 0,
174: m10 == -1 ? width : m11 == -1 ? height : 0);
175: }
176: }
|