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: JaiLoader.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.cmf.loader.image;
09:
10: import com.sun.media.jai.codec.SeekableStream;
11: import com.uwyn.rife.cmf.dam.exceptions.ContentManagerException;
12: import com.uwyn.rife.cmf.loader.ImageContentLoaderBackend;
13: import com.uwyn.rife.tools.ExceptionUtils;
14: import java.awt.Image;
15: import java.awt.image.BufferedImage;
16: import java.io.ByteArrayInputStream;
17: import java.io.ByteArrayOutputStream;
18: import java.io.PrintStream;
19: import java.util.Set;
20: import javax.media.jai.JAI;
21: import javax.media.jai.PlanarImage;
22:
23: /**
24: * This is an image loader back-end that uses Java Advanced Imaging to load
25: * image files, if its classes are present in the classpath.
26: * <p>More information about Java Advanced Imaging can be obtained from <a
27: * href="http://java.sun.com/products/java-media/jai">http://java.sun.com/products/java-media/jai</a>.
28: * <p>Plug-ins for additional formats can be obtained from <a
29: * href="http://java.sun.com/products/java-media/jai/downloads/download-iio.html">http://java.sun.com/products/java-media/jai/downloads/download-iio.html</a>.
30: *
31: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
32: * @version $Revision: 3634 $
33: * @since 1.0
34: */
35: public class JaiLoader extends ImageContentLoaderBackend {
36: public Image loadFromBytes(byte[] data, Set<String> errors)
37: throws ContentManagerException {
38: return new LoaderDelegate().load(data, errors);
39: }
40:
41: public boolean isBackendPresent() {
42: try {
43: return null != Class.forName("javax.media.jai.JAI");
44: } catch (ClassNotFoundException e) {
45: return false;
46: }
47: }
48:
49: private static class LoaderDelegate {
50: public Image load(byte[] data, Set<String> errors)
51: throws ContentManagerException {
52: ByteArrayInputStream in = new ByteArrayInputStream(data);
53: BufferedImage image = null;
54:
55: SeekableStream in_stream = SeekableStream.wrapInputStream(
56: in, false);
57: PlanarImage jai = JAI.create("stream", in_stream);
58: PrintStream default_err = System.err;
59: try {
60: System.setErr(new PrintStream(
61: new ByteArrayOutputStream())); // remove output to standard error
62:
63: image = jai.getAsBufferedImage();
64: } catch (Throwable e) {
65: if (errors != null) {
66: errors
67: .add(ExceptionUtils
68: .getExceptionStackTrace(e));
69: }
70:
71: image = null;
72: } finally {
73: System.setErr(default_err); // restore default system standard error output
74: }
75:
76: return image;
77: }
78: }
79: }
|