//
// LegacyTiffForm.java
//

/*
VisAD system for interactive analysis and visualization of numerical
data.  Copyright (C) 1996 - 2017 Bill Hibbard, Curtis Rueden, Tom
Rink, Dave Glowacki, Steve Emmerson, Tom Whittaker, Don Murray, and
Tommy Jasmin.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/

package visad.data.tiff;

import java.awt.Image;
import java.awt.image.*;
import java.lang.reflect.*;
import java.io.*;
import java.net.URL;
import java.rmi.RemoteException;
import visad.*;
import visad.data.*;
import visad.util.*;

/**
 * LegacyTiffForm is the old VisAD data form for the TIFF file format.
 * It relies on either ImageJ or JAI being available in the class path,
 * and is very inefficient when dealing with large multi-page TIFF files.
 * The following table indicates features that the form supports:

* *

* * * * * * * * * * * *
 uncompressedcompressed (LZW)
single imageread and writeread only (with JAI)
multi-pageread and writeread only (with JAI)

* * This form requires ImageJ, available from the * ImageJ web site. * * Note that features marked with "(with JAI)" also require * the Java Advanced Imaging (JAI) package, available at Sun's * * Java Advanced Imaging web site. * * Also, no support for reading TIFF data from URLs is provided. * However, the visad.data.jai package provides limited support for * importing single-image TIFF data from a URL. * * @deprecated Use TiffForm, or visad.data.bio.LociForm * with loci.formats.in.TiffReader and loci.formats.out.TiffWriter */ public class LegacyTiffForm extends Form implements FormFileInformer, FormBlockReader, FormProgressInformer { // -- Static fields -- /** Counter for TIFF form instantiation. */ private static int formCount = 0; /** Legal TIFF SUFFIXES. */ private static final String[] SUFFIXES = { "tif", "tiff" }; /** Message produced when attempting to use ImageJ without it installed. */ private static final String NO_IJ = "This feature requires ImageJ, " + "available online at http://rsb.info.nih.gov/ij/download.html"; /** Message produced when attempting to use JAI without it installed. */ private static final String NO_JAI = "This feature requires JAI, " + "available from Sun at http://java.sun.com/products/java-media/jai/"; // -- Fields -- /** Reflection tool for ImageJ and JAI calls. */ private ReflectedUniverse r; /** Flag indicating ImageJ is not installed. */ private boolean noImageJ = false; /** Flag indicating JAI is not installed. */ private boolean noJai = false; /** Filename of current TIFF stack. */ private String currentId; /** Number of images in current TIFF stack. */ private int numImages; /** Flag indicating whether ImageJ supports the current TIFF stack. */ private boolean canUseImageJ; /** Percent complete with current operation. */ private double percent; // -- Constructor -- /** Constructs a new TIFF file form. */ public LegacyTiffForm() { super("LegacyTiffForm" + formCount++); r = new ReflectedUniverse(); // ImageJ imports try { r.exec("import ij.ImagePlus"); r.exec("import ij.ImageStack"); r.exec("import ij.io.FileInfo"); r.exec("import ij.io.FileSaver"); r.exec("import ij.io.Opener"); r.exec("import ij.io.TiffDecoder"); r.exec("import ij.process.ByteProcessor"); r.exec("import ij.process.ColorProcessor"); r.exec("import ij.process.FloatProcessor"); r.exec("import ij.process.ImageProcessor"); r.exec("import ij.process.ShortProcessor"); } catch (VisADException exc) { noImageJ = true; } // JAI imports try { r.exec("import com.sun.media.jai.codec.ImageDecodeParam"); r.exec("import com.sun.media.jai.codec.ImageDecoder"); r.exec("import com.sun.media.jai.codec.ImageCodec"); } catch (VisADException exc) { noJai = true; } } // -- FormFileInformer methods -- /** Checks if the given string is a valid filename for a TIFF file. */ public boolean isThisType(String name) { for (int i=0; i 1) { // save as multi-page TIFF Object is = null; for (int i=0; i= numImages) { throw new BadFormException("Invalid image number: " + block_number); } Image img = null; if (canUseImageJ) { if (noImageJ) throw new BadFormException(NO_IJ); r.exec("stack = image.getStack()"); r.setVar("bn1", block_number + 1); r.exec("ip = stack.getProcessor(bn1)"); r.exec("img = ip.createImage()"); img = (Image) r.getVar("img"); } else { if (noJai) throw new BadFormException(NO_JAI); try { r.setVar("i", block_number); RenderedImage ri = (RenderedImage) r.exec("id.decodeAsRenderedImage(i)"); WritableRaster wr = ri.copyData(null); ColorModel cm = ri.getColorModel(); img = new BufferedImage(cm, wr, false, null); } catch (VisADException exc) { throw new BadFormException(exc); } } return DataUtility.makeField(img); } public int getBlockCount(String id) throws BadFormException, IOException, VisADException { if (!id.equals(currentId)) initFile(id); return numImages; } public void close() throws BadFormException, IOException, VisADException { } // -- FormProgressInformer methods -- public double getPercentComplete() { return percent; } // -- Helper methods -- /** * Converts a FlatField of the form ((x, y) -> value) or * ((x, y) -> (r, g, b)) to an ImageJ ImageProcessor object. */ private Object extractImage(FlatField field) throws VisADException { GriddedSet set = (GriddedSet) field.getDomainSet(); int[] wh = set.getLengths(); int w = wh[0]; int h = wh[1]; float[][] samples = field.getFloats(false); r.setVar("w", w); r.setVar("h", h); // HACK - detect "fake" 3-color images boolean fake3 = samples.length == 3 && samples[0] == samples[1] && samples[0] == samples[2]; if (samples.length == 3 && !fake3) { // 24-bit color is the best we can do int[] pixels = new int[samples[0].length]; for (int i=0; i hi) { hi = value; if (hi >= 65536) break; // need 32-bit floats } } if (lo >= 0 && hi < 256) { // 8-bit grayscale byte[] pixels = new byte[samples[0].length]; for (int i=0; i= 0 && hi < 65536) { // 16-bit grayscale short[] pixels = new short[samples[0].length]; for (int i=0; i 2) { System.out.println("To convert a file to TIFF, run:"); System.out.println(" java " + "visad.data.tiff.LegacyTiffForm in_file out_file"); System.out.println("To test read a TIFF file, run:"); System.out.println(" java visad.data.tiff.LegacyTiffForm in_file"); System.exit(2); } if (args.length == 1) { // Test read TIFF file LegacyTiffForm form = new LegacyTiffForm(); System.out.print("Reading " + args[0] + " "); Data data = form.open(args[0]); System.out.println("[done]"); System.out.println("MathType =\n" + data.getType().prettyString()); } else if (args.length == 2) { // Convert file to TIFF format System.out.print(args[0] + " -> " + args[1] + " "); DefaultFamily loader = new DefaultFamily("loader"); DataImpl data = loader.open(args[0]); loader = null; LegacyTiffForm form = new LegacyTiffForm(); form.save(args[1], data, true); System.out.println("[done]"); } System.exit(0); } }

Ads help maintain this website.