Source code: com/arranger/jarl/util/ImageUtil.java
1 package com.arranger.jarl.util;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.image.*;
6 import java.io.ByteArrayOutputStream;
7 import java.io.InputStream;
8 import java.io.File;
9
10 /**
11 * ImageUtil created on Feb 23, 2003
12 * Taken from http://javaalmanac.com/egs/java.awt.image/pkg.html#Images
13 */
14 public class ImageUtil {
15
16 public static final Color TRANSPARENT = new Color(0, 0, 0, 0);
17 public static final int TRANSPARENT_RGB = TRANSPARENT.getRGB();
18
19 /**
20 * Overlay a transparent image onto a graphics
21 * @param graphics
22 * @param transparentImage
23 */
24 public static void overlayTransparentImage(Graphics graphics, Image transparentImage) {
25 Graphics2D graphics2D = (Graphics2D) graphics;
26 Color color = graphics2D.getColor();
27 Composite composite = graphics2D.getComposite();
28 graphics2D.setColor(TRANSPARENT);
29 graphics2D.setComposite(AlphaComposite.SrcOver);
30 graphics2D.drawImage(transparentImage, null, null);
31 graphics2D.setColor(color);
32 graphics2D.setComposite(composite);
33 }
34
35 /**
36 * Overlay a transparent image onto a graphics
37 * @param graphics
38 * @param transparentImage
39 */
40 public static void overlayTransparentImage(Graphics graphics, Image transparentImage, float alphaLevel) {
41 Graphics2D graphics2D = (Graphics2D) graphics;
42 Color color = graphics2D.getColor();
43 Composite composite = graphics2D.getComposite();
44 graphics2D.setColor(TRANSPARENT);
45 graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphaLevel));
46 graphics2D.drawImage(transparentImage, null, null);
47 graphics2D.setColor(color);
48 graphics2D.setComposite(composite);
49 }
50
51 /**
52 * Creates a transparent image where the mask color is turned into an alpha channel transparency
53 * @param image image to make transparent
54 * @param maskColor color which is made transparent
55 * @return a new bufferedImage that has a transparency
56 *
57 * @see TransparentImageFilter
58 */
59 public static Image createTransparentImage(Image image, Color maskColor) {
60 Image transparentImage = Toolkit.getDefaultToolkit().createImage(
61 new FilteredImageSource(image.getSource(),
62 new TransparentImageFilter(maskColor)));
63
64 return transparentImage;
65 }
66
67 /**
68 * Used by {@link #createTransparentImage}
69 */
70 public static class TransparentImageFilter extends RGBImageFilter {
71
72 protected Color m_maskColor;
73 protected int m_maskRed;
74 protected int m_maskGreen;
75 protected int m_maskBlue;
76
77 public TransparentImageFilter(Color maskColor) {
78 m_maskColor = maskColor;
79 m_maskRed = maskColor.getRed();
80 m_maskGreen = maskColor.getGreen();
81 m_maskBlue = maskColor.getBlue();
82 }
83
84 public int filterRGB(int x, int y, int rgb) {
85 //need to return transparent if we're 'close' to the maskColor
86 int alpha = (rgb >> 24) & 0xFF;
87 int red = (rgb >> 16) & 0xFF;
88 int green = (rgb >> 8) & 0xFF;
89 int blue = (rgb >> 0) & 0xFF;
90
91 double totalDistance = Math.abs(m_maskRed - red);
92 totalDistance += Math.abs(m_maskGreen - green);
93 totalDistance += Math.abs(m_maskBlue - blue);
94 totalDistance /= 300; //compute average and get a pct
95
96 //the farther away the totalDistance the closer alpha is to 255
97 alpha = (int) (totalDistance * 0xFF);
98 rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
99
100 return rgb;
101 }
102 }
103
104 /**
105 * This will mask off any color outside of the {@link Shape}
106 */
107 public static class ShapeImageMaskFilter extends RGBImageFilter {
108
109 protected Shape m_shape;
110
111 public ShapeImageMaskFilter(Shape shape) {
112 m_shape = shape;
113 }
114
115 public int filterRGB(int x, int y, int rgb) {
116 if (m_shape.contains(x, y)) {
117 return rgb;
118 } else {
119 return ImageUtil.TRANSPARENT_RGB;
120 }
121 }
122 }
123
124 /**
125 * Creates a transparent image where the mask color is turned into an alpha channel transparency
126 * @param image image to make transparent
127 * @param maskColor color which is made transparent
128 * @return a new bufferedImage that has a transparency
129 */
130 public static Image createTransparentImageInverse(Image image, final Color maskColor) {
131 Image transparentImage = Toolkit.getDefaultToolkit().createImage(
132 new FilteredImageSource(image.getSource(),
133 new RGBImageFilter() {
134
135 public int filterRGB(int x, int y, int rgb) {
136 if (rgb != maskColor.getRGB()) {
137 return 0x00000000;
138 } else {
139 return rgb;
140 }
141 }
142 }));
143
144 return transparentImage;
145 }
146
147 /**
148 * Create a buffered image from another image
149 * @param image
150 */
151 public static BufferedImage toBufferedImage(Image image) {
152 if (image instanceof BufferedImage) {
153 return (BufferedImage) image;
154 }
155
156 // This code ensures that all the pixels in the image are loaded
157 image = new ImageIcon(image).getImage();
158
159 int type = (hasAlpha(image)) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
160
161 int height = image.getHeight(null);
162 int width = image.getWidth(null);
163
164 BufferedImage bimage = new BufferedImage(width, height, type);
165
166 // Copy image to buffered image
167 Graphics g = bimage.createGraphics();
168
169 // Paint the image onto the buffered image
170 g.drawImage(image, 0, 0, null);
171 g.dispose();
172
173 return bimage;
174 }
175
176
177
178 /**
179 * This method returns true if the specified image has transparent pixels
180 * @param image
181 */
182 public static boolean hasAlpha(Image image) {
183 // If buffered image, the color model is readily available
184 if (image instanceof BufferedImage) {
185 BufferedImage bimage = (BufferedImage) image;
186 return bimage.getColorModel().hasAlpha();
187 }
188
189 // Use a pixel grabber to retrieve the image's color model;
190 // grabbing a single pixel is usually sufficient, unless it's a file based image producer...
191 PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
192 try {
193 pg.grabPixels();
194 } catch (InterruptedException e) {
195 e.printStackTrace();
196 }
197
198 // Get the image's color model
199 ColorModel cm = pg.getColorModel();
200 return cm.hasAlpha();
201 }
202
203 /**
204 * Copy attributes
205 * @param src
206 * @param dest
207 */
208 public static void copyGraphics(Graphics src, Graphics dest) {
209 Graphics2D source = (Graphics2D) src;
210 Graphics2D destination = (Graphics2D) dest;
211
212 destination.setColor(source.getColor());
213 destination.setComposite(source.getComposite());
214 destination.setTransform(source.getTransform());
215 destination.setPaint(source.getPaint());
216 destination.setRenderingHints(source.getRenderingHints());
217 destination.setStroke(source.getStroke());
218 destination.setBackground(source.getBackground());
219 }
220
221 /**
222 * Loads an image from the icon class path
223 * @param name
224 * @return
225 * @throws Exception
226 */
227 public static Image loadResourceImage(String name) throws Exception {
228 InputStream is = name.getClass().getResourceAsStream(IconUtil.ICON_PATH + name);
229 ByteArrayOutputStream baos = new ByteArrayOutputStream();
230 IOUtil.copyStream(is, baos);
231 is.close();
232
233 return Toolkit.getDefaultToolkit().createImage(baos.toByteArray());
234 }
235
236 /**
237 * Loads an image from the icon class path
238 * @param fileName
239 * @return
240 * @throws Exception
241 */
242 public static Image loadImage(String fileName) throws Exception {
243 return IOUtil.load(new File(fileName));
244 }
245 }