1 /*
2 * Created on Sep 19, 2003
3 * Copyright (c) 2003 William Lee. All rights reserved.
4 */
5 package portaview.util;
6
7 import java.awt.AlphaComposite;
8 import java.awt.Color;
9 import java.awt.Component;
10 import java.awt.Composite;
11 import java.awt.Graphics2D;
12 import java.awt.Image;
13 import java.awt.MediaTracker;
14 import java.awt.Toolkit;
15 import java.awt.geom.AffineTransform;
16 import java.awt.image.AffineTransformOp;
17 import java.awt.image.BufferedImage;
18 import java.awt.image.BufferedImageOp;
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.net.URL;
26
27 import javax.imageio.ImageIO;
28
29 import portaview.model.PortaViewModel;
30 import portaview.model.SlideSettings;
31
32 import com.sun.image.codec.jpeg.JPEGCodec;
33 import com.sun.image.codec.jpeg.JPEGImageDecoder;
34 import com.sun.image.codec.jpeg.JPEGImageEncoder;
35
36 /***
37 * Image utilities to process JPG images.
38 * @author <a href="mailto:wwlee1@uiuc.edu">William Lee</a>
39 * @version $Id: ImageUtils.java,v 1.9 2003/12/10 06:18:56 wlee Exp $
40 */
41 public class ImageUtils
42 {
43 private static String[] supportedFormat = { "jpg", "png" };
44
45 /***
46 * Returns the image associated with the url
47 * @param url url to get the image
48 * @return the Image
49 * @throws Exception when getting the image fails.
50 */
51 public static Image getImage(URL url) throws Exception
52 {
53 return Toolkit.getDefaultToolkit().getImage(url);
54 }
55
56 /***
57 * Similar to getImage(URL), load from a filename instead of the URL.
58 */
59 public static Image getImage(String filename) throws Exception
60 {
61 return Toolkit.getDefaultToolkit().getImage(filename);
62 }
63
64 /***
65 * Similar to getImage(String), load from a File instead of the file name.
66 */
67 public static Image getImage(File file) throws Exception
68 {
69 return Toolkit.getDefaultToolkit().getImage(file.toURL());
70 }
71
72 /***
73 * Returns the image given the JPEG encoding in a byte array.
74 * @param bytes byte array that contains the JPEG encoding of the image.
75 * @return the Image object.
76 */
77 public static Image getImage(byte[] bytes)
78 {
79 try
80 {
81 return ImageIO.read(new ByteArrayInputStream(bytes));
82 }
83 catch (IOException e)
84 {
85 // error
86 e.printStackTrace();
87 return null;
88 }
89 }
90
91 /***
92 * Returns a buffered image based on the file an component to draw on.
93 * @param file file to retrieve
94 * @param c component to draw on
95 * @return the BufferedImage object
96 * @throws Exception when loading of the image fails.
97 */
98 public static BufferedImage getBufferedImage(File file, Component c)
99 throws Exception
100 {
101 return getBufferedImage(getImage(file), c);
102 }
103
104 /***
105 * Similar to getBufferedImage(File, Component), load the BufferedImage from an Image.
106 * @param img image to load
107 * @param c component to draw on
108 * @return the BufferedImage object
109 * @throws Exception when fails to load.
110 */
111 public static BufferedImage getBufferedImage(Image img, Component c)
112 throws Exception
113 {
114 MediaTracker mt = new MediaTracker(c);
115 mt.addImage(img, 0);
116 mt.waitForID(0);
117 BufferedImage bi =
118 new BufferedImage(
119 img.getWidth(null),
120 img.getHeight(null),
121 BufferedImage.TYPE_INT_RGB);
122 Graphics2D biContext = bi.createGraphics();
123 biContext.drawImage(img, 0, 0, null);
124 return bi;
125 }
126
127 /***
128 * Returns a buffered image, resized to fit the frame specified by fwidth and fheight.
129 * Note that this will maintain the image's ratio.
130 *
131 * @param bi Buffered image that contains the source for the image.
132 * @param fwidth frame width
133 * @param fheight height
134 * @param c component to draw the image from.
135 * @return A new instance of BufferedImage
136 * @throws Exception
137 */
138 public static BufferedImage getBufferedImageInFrame(
139 BufferedImage bi,
140 int fwidth,
141 int fheight,
142 Component c)
143 throws Exception
144 {
145 AffineTransform at = new AffineTransform();
146 double scale = getScale(bi.getWidth(), bi.getHeight(), fwidth, fheight);
147 at.scale(scale, scale);
148 BufferedImageOp biop =
149 new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
150 BufferedImage target =
151 new BufferedImage(
152 (int) (bi.getWidth() * scale),
153 (int) (bi.getHeight() * scale),
154 BufferedImage.TYPE_INT_RGB);
155 Graphics2D writeBuffer = (Graphics2D) target.getGraphics();
156 // writeBuffer.scale(scale, scale);
157 writeBuffer.drawImage(bi, biop, 0, 0);
158 //writeBuffer.drawImage(bi, 0, 0, new JFrame());
159 return target;
160 //*/
161 }
162
163 /**
164 * Returns the scale.
165 * @param origWidth original width of the image
166 * @param origHeight original height of the image
167 * @param targetWidth target width of the image
168 * @param targetHeight target height of the image
169 * @return the scale from 0 to 1 for rescaling.
170 */
171 private static double getScale(
172 int origWidth,
173 int origHeight,
174 int targetWidth,
175 int targetHeight)
176 {
177 double imageRatio = (double) origWidth / (double) origHeight;
178 double paintRatio = (double) targetWidth / (double) targetHeight;
179 double scale = 1;
180 if (paintRatio < imageRatio)
181 {
182 scale = (double) targetWidth / (double) origWidth;
183 }
184 else
185 {
186 scale = (double) targetHeight / (double) origHeight;
187 }
188 return scale;
189 }
190
191 /***
192 * Returns a buffered image, resized to fit the frame specified by fwidth and fheight.
193 * Note that this will maintain the image's ratio.
194 *
195 * @param f file to load the image from
196 * @param fwidth frame width
197 * @param fheight height
198 * @param c component to draw the image from.
199 * @return A new instance of BufferedImage
200 * @throws Exception
201 */
202 public static BufferedImage getBufferedImageInFrame(
203 File f,
204 int fwidth,
205 int fheight,
206 Component c)
207 throws Exception
208 {
209 //BufferedImage bi = ImageIO.read(f);
210 BufferedImage bi = getBufferedImage(f, c);
211 return getBufferedImageInFrame(bi, fwidth, fheight, c);
212 }
213
214 /***
215 * Returns the JPEG encoding of a scaled image.
216 * @param f image file.
217 * @param width desired width for the image
218 * @param height desired height for image
219 * @return the JPEG encoding of the scaled image.
220 * @throws Exception when scaling fails.
221 */
222 public static byte[] getScaledImage(File f, int width, int height)
223 throws Exception
224 {
225 InputStream in = new FileInputStream(f);
226 JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
227 Image inImage = decoder.decodeAsBufferedImage();
228
229 double scale =
230 getScale(
231 inImage.getWidth(null),
232 inImage.getHeight(null),
233 width,
234 height);
235
236 int scaledW = (int) (scale * inImage.getWidth(null));
237 int scaledH = (int) (scale * inImage.getHeight(null));
238
239 BufferedImage outImage =
240 new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB);
241
242 AffineTransform tx = new AffineTransform();
243 if (scale < 1.0d)
244 {
245 tx.scale(scale, scale);
246 }
247
248 // Paint image.
249 Graphics2D g2d = outImage.createGraphics();
250 g2d.drawImage(inImage, tx, null);
251 g2d.dispose();
252
253 // JPEG-encode the image and write to file.
254 ByteArrayOutputStream out = new ByteArrayOutputStream();
255 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
256 encoder.encode(outImage);
257 out.close();
258 return out.toByteArray();
259 }
260
261 /***
262 * Draws border around the graphics with the width expressed as a ratio
263 * to the whole image.
264 * @param g graphic to draw on
265 * @param ratio goes from 0 to 30, which means the % of the picture.
266 * @param maxWidth maximum width of graphics
267 * @param maxHeight maximum height of graphics
268 * @param c the color of the border
269 */
270 public static void drawBorderRatio(
271 Graphics2D g,
272 float trans,
273 int ratio,
274 int maxWidth,
275 int maxHeight,
276 Color c)
277 {
278 drawBorderRatio(g, trans, ratio, 0, 0, maxWidth, maxHeight, c);
279 /*
280 int bwidth =
281 (int) (((maxWidth > maxHeight ? maxWidth : maxHeight)) * ratio)
282 / 100;
283 drawBorder(g, trans, bwidth, maxWidth, maxHeight, c);
284 */
285 }
286
287 /***
288 * Draws border around the graphics with the width expressed as a ratio
289 * to the whole image.
290 * @param g graphic to draw on
291 * @param ratio goes from 0 to 30, which means the % of the picture.
292 * @param xoffset the x coordinate offset
293 * @param yoffset the y coordinate offset
294 * @param maxWidth maximum width of graphics
295 * @param maxHeight maximum height of graphics
296 * @param c the color of the border
297 */
298 public static void drawBorderRatio(
299 Graphics2D g,
300 float trans,
301 int ratio,
302 int xoffset,
303 int yoffset,
304 int maxWidth,
305 int maxHeight,
306 Color c)
307 {
308 int bwidth =
309 (int) (((maxWidth > maxHeight ? maxWidth : maxHeight)) * ratio)
310 / 100;
311 drawBorder(g, trans, bwidth, xoffset, yoffset, maxWidth, maxHeight, c);
312 }
313
314 /***
315 * Draws border around the graphics
316 * @param g graphic to draw on
317 * @param bwidth border width
318 * @param xoffset the x coordinate offset
319 * @param yoffset the y coordinate offset
320 * @param maxWidth maximum width of graphics
321 * @param maxHeight maximum height of graphics
322 * @param c the color of the border
323 */
324 public static void drawBorder(
325 Graphics2D g,
326 float trans,
327 int bwidth,
328 int xoffset,
329 int yoffset,
330 int maxWidth,
331 int maxHeight,
332 Color c)
333 {
334 if (bwidth == 0)
335 return;
336 g.setColor(c);
337 Composite orig = g.getComposite();
338 if (trans > 1.0F)
339 {
340 g.setComposite(createComposite(1.0F));
341 }
342 else
343 {
344 g.setComposite(createComposite(trans));
345 }
346 // from upper left to lower left
347 g.fillRect(xoffset, yoffset, bwidth, maxHeight - bwidth);
348 // from lower left to lower right
349 g.fillRect(
350 xoffset,
351 yoffset + maxHeight - bwidth,
352 maxWidth - bwidth,
353 bwidth);
354 // from upper right to lower right
355 g.fillRect(
356 xoffset + maxWidth - bwidth,
357 yoffset + bwidth,
358 bwidth,
359 maxHeight);
360 // from upper left to upper right
361 g.fillRect(xoffset + bwidth, yoffset, maxWidth - bwidth, bwidth);
362 g.setComposite(orig);
363 }
364
365 /***
366 * Draws border around the graphics
367 * @param g graphic to draw on
368 * @param bwidth border width
369 * @param maxWidth maximum width of graphics
370 * @param maxHeight maximum height of graphics
371 * @param c the color of the border
372 */
373 public static void drawBorder(
374 Graphics2D g,
375 float trans,
376 int bwidth,
377 int maxWidth,
378 int maxHeight,
379 Color c)
380 {
381 drawBorder(g, trans, bwidth, 0, 0, maxWidth, maxHeight, c);
382
383 }
384
385 /***
386 * Create the alpha composite given a alpha value from 0 to 1.
387 * @param alpha transparency.
388 * @return the AlphaComposite object.
389 */
390 private static AlphaComposite createComposite(float alpha)
391 {
392 return (AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
393 }
394
395 /***
396 * Draw the image according to the specified settings.
397 * @param pvm
398 * @param bi
399 */
400 public static void drawBorder(PortaViewModel pvm, BufferedImage bi)
401 {
402 SlideSettings ss = pvm.getSlideSettings();
403 Graphics2D g = (Graphics2D) bi.getGraphics();
404 drawBorderRatio(
405 g,
406 (ss.getTransparency() / 10.0F),
407 ss.getBorder(),
408 bi.getWidth(),
409 bi.getHeight(),
410 ss.getBorderColor());
411 }
412
413 }
This page was automatically generated by Maven