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: ImageJLoader.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.cmf.loader.image;
09:
10: import com.uwyn.rife.cmf.dam.exceptions.ContentManagerException;
11: import com.uwyn.rife.cmf.loader.ImageContentLoaderBackend;
12: import com.uwyn.rife.tools.ExceptionUtils;
13: import ij.ImagePlus;
14: import ij.io.Opener;
15: import java.awt.Image;
16: import java.io.ByteArrayInputStream;
17: import java.util.Set;
18:
19: /**
20: * This is an image loader back-end that uses ImageJ to load TIFF files, if
21: * its classes are present in the classpath.
22: * <p>More information about ImageJ can be obtained from <a
23: * href="http://rsb.info.nih.gov/ij/">http://rsb.info.nih.gov/ij/</a>.
24: *
25: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
26: * @version $Revision: 3634 $
27: * @since 1.0
28: */
29: public class ImageJLoader extends ImageContentLoaderBackend {
30: public Image loadFromBytes(byte[] data, Set<String> errors)
31: throws ContentManagerException {
32: return new LoaderDelegate().load(data, errors);
33: }
34:
35: public boolean isBackendPresent() {
36: try {
37: return null != Class.forName("ij.io.Opener");
38: } catch (ClassNotFoundException e) {
39: return false;
40: }
41: }
42:
43: private static class LoaderDelegate {
44: public Image load(byte[] data, Set<String> errors)
45: throws ContentManagerException {
46: ByteArrayInputStream in = new ByteArrayInputStream(data);
47: Image image = null;
48:
49: try {
50: ImagePlus imagej = new Opener().openTiff(in, "cmfdata");
51: if (imagej != null) {
52: image = imagej.getImage();
53: }
54: } catch (Throwable e) {
55: if (errors != null) {
56: errors
57: .add(ExceptionUtils
58: .getExceptionStackTrace(e));
59: }
60:
61: image = null;
62: }
63:
64: return image;
65: }
66: }
67: }
|