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: ImageroReaderLoader.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.cmf.loader.image;
09:
10: import com.imagero.reader.ImageReader;
11: import com.imagero.reader.ReaderFactory;
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.io.ByteArrayOutputStream;
21: import java.io.PrintStream;
22: import java.util.Set;
23:
24: /**
25: * This is an image loader back-end that uses ImageroReader to load image
26: * files, if its classes are present in the classpath.
27: * <p>More information about ImageroReader can be obtained from <a
28: * href="http://reader.imagero.com">http://reader.imagero.com</a>.
29: *
30: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
31: * @version $Revision: 3634 $
32: * @since 1.0
33: */
34: public class ImageroReaderLoader extends ImageContentLoaderBackend {
35: public Image loadFromBytes(byte[] data, Set<String> errors)
36: throws ContentManagerException {
37: return new LoaderDelegate().load(data, errors);
38: }
39:
40: public boolean isBackendPresent() {
41: try {
42: return null != Class
43: .forName("com.imagero.reader.ReaderFactory");
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: Image image = null;
54:
55: PrintStream default_out = System.out;
56: PrintStream default_err = System.err;
57: try {
58: System.setOut(new PrintStream(
59: new ByteArrayOutputStream())); // remove output to standard output
60: System.setErr(new PrintStream(
61: new ByteArrayOutputStream())); // remove output to standard error
62:
63: ImageReader imagero = ReaderFactory.createReader(in);
64: if (imagero.getImageCount() > 0) {
65: ImageProducer producer = imagero.getProducer(0);
66:
67: // create an awt image from it and wait 'till it's fully loaded
68: image = Toolkit.getDefaultToolkit().createImage(
69: producer);
70: ImageWaiter.wait(image);
71: }
72: } catch (Throwable e) {
73: if (errors != null) {
74: errors
75: .add(ExceptionUtils
76: .getExceptionStackTrace(e));
77: }
78:
79: image = null;
80: } finally {
81: System.setOut(default_out); // restore default system standard output
82: System.setErr(default_err); // restore default system standard error output
83: }
84:
85: return image;
86: }
87: }
88: }
|