01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: JimiLoader.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.cmf.loader.image;
09:
10: import com.sun.jimi.core.Jimi;
11: import com.sun.jimi.core.raster.JimiRasterImage;
12: import com.uwyn.rife.cmf.dam.exceptions.ContentManagerException;
13: import com.uwyn.rife.cmf.loader.ImageContentLoaderBackend;
14: import com.uwyn.rife.tools.ExceptionUtils;
15: import com.uwyn.rife.tools.ImageWaiter;
16: import java.awt.Image;
17: import java.awt.Toolkit;
18: import java.awt.image.ImageProducer;
19: import java.io.ByteArrayInputStream;
20: import java.util.Set;
21:
22: /**
23: * This is an image loader back-end that uses Jimi to load image files, if its
24: * classes are present in the classpath.
25: * <p>More information about Jimi can be obtained from <a
26: * href="http://java.sun.com/products/jimi">http://java.sun.com/products/jimi</a>.
27: *
28: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
29: * @version $Revision: 3634 $
30: * @since 1.0
31: */
32: public class JimiLoader extends ImageContentLoaderBackend {
33: public Image loadFromBytes(byte[] data, Set<String> errors)
34: throws ContentManagerException {
35: return new LoaderDelegate().load(data, errors);
36: }
37:
38: public boolean isBackendPresent() {
39: try {
40: return null != Class.forName("com.sun.jimi.core.Jimi");
41: } catch (ClassNotFoundException e) {
42: return false;
43: }
44: }
45:
46: private static class LoaderDelegate {
47: public Image load(byte[] data, Set<String> errors)
48: throws ContentManagerException {
49: ByteArrayInputStream is = new ByteArrayInputStream(data);
50: Image image = null;
51:
52: try {
53: JimiRasterImage raster_image = Jimi.getRasterImage(is,
54: Jimi.SYNCHRONOUS);
55: ImageProducer producer = raster_image
56: .getImageProducer();
57:
58: // create an awt image from it and wait 'till it's fully loaded
59: image = Toolkit.getDefaultToolkit().createImage(
60: producer);
61: ImageWaiter.wait(image);
62: } catch (Throwable e) {
63: if (errors != null) {
64: errors
65: .add(ExceptionUtils
66: .getExceptionStackTrace(e));
67: }
68:
69: image = null;
70: }
71:
72: return image;
73: }
74: }
75: }
|