Search in sources :

Example 6 with TiffSaver

use of loci.formats.tiff.TiffSaver in project bioformats by openmicroscopy.

the class WritePrecompressedPlanes method main.

public static void main(String[] args) throws FormatException, IOException {
    // print usage if no args specified
    if (args.length == 0) {
        System.out.println("Usage: java WritePrecompressedPlanes " + "[input file 1] ... [input file n] [output file]");
        System.exit(0);
    }
    // first n - 1 args are the input files
    String[] inputFiles = new String[args.length - 1];
    System.arraycopy(args, 0, inputFiles, 0, inputFiles.length);
    // last arg is the output file
    String outputFile = args[args.length - 1];
    // open up one of the input files so that we can read the metadata
    // this assumes that the dimensions of the input files are the same
    ImageReader reader = new ImageReader();
    reader.setId(inputFiles[0]);
    int pixelType = reader.getPixelType();
    // write the pixel data to the output file
    RandomAccessOutputStream out = new RandomAccessOutputStream(outputFile);
    TiffSaver saver = new TiffSaver(out, outputFile);
    saver.setWritingSequentially(true);
    saver.setLittleEndian(reader.isLittleEndian());
    saver.setBigTiff(false);
    saver.writeHeader();
    for (int i = 0; i < inputFiles.length; i++) {
        RandomAccessInputStream in = new RandomAccessInputStream(inputFiles[i]);
        byte[] buf = new byte[(int) in.length()];
        in.readFully(buf);
        in.close();
        IFD ifd = new IFD();
        ifd.put(IFD.IMAGE_WIDTH, reader.getSizeX());
        ifd.put(IFD.IMAGE_LENGTH, reader.getSizeY());
        ifd.put(IFD.LITTLE_ENDIAN, reader.isLittleEndian());
        ifd.put(IFD.SAMPLE_FORMAT, FormatTools.isSigned(pixelType) ? 2 : FormatTools.isFloatingPoint(pixelType) ? 3 : 1);
        ifd.put(IFD.PLANAR_CONFIGURATION, 1);
        ifd.put(IFD.REUSE, out.length());
        out.seek(out.length());
        // this is very important
        // the data is already compressed in a single chunk, so setting the
        // number of rows per strip to something smaller than the full height
        // will require us to re-compress the data
        ifd.put(IFD.ROWS_PER_STRIP, reader.getSizeY());
        saver.writeImage(buf, ifd, i, pixelType, 0, 0, reader.getSizeX(), reader.getSizeY(), i == inputFiles.length - 1, reader.getRGBChannelCount(), true);
    }
    reader.close();
    out.close();
    // reset the TIFF file's compression flag
    // you cannot do this before the pixel data is written, otherwise
    // the pixels will be re-compressed
    saver = new TiffSaver(outputFile);
    for (int i = 0; i < inputFiles.length; i++) {
        RandomAccessInputStream in = new RandomAccessInputStream(outputFile);
        saver.overwriteLastIFDOffset(in);
        saver.overwriteIFDValue(in, i, IFD.COMPRESSION, TiffCompression.JPEG.getCode());
        in.close();
    }
    saver.getStream().close();
}
Also used : IFD(loci.formats.tiff.IFD) RandomAccessOutputStream(loci.common.RandomAccessOutputStream) TiffSaver(loci.formats.tiff.TiffSaver) RandomAccessInputStream(loci.common.RandomAccessInputStream) ImageReader(loci.formats.ImageReader)

Example 7 with TiffSaver

use of loci.formats.tiff.TiffSaver in project bioformats by openmicroscopy.

the class EditTiffG method saveFile.

public void saveFile(File f) {
    RandomAccessInputStream in = null;
    RandomAccessOutputStream out = null;
    try {
        String xml = getXML();
        String path = f.getAbsolutePath();
        in = new RandomAccessInputStream(path);
        out = new RandomAccessOutputStream(path);
        TiffSaver saver = new TiffSaver(out, path);
        saver.overwriteComment(in, xml);
    } catch (FormatException exc) {
        showError(exc);
    } catch (IOException exc) {
        showError(exc);
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (Exception e) {
        }
        try {
            if (out != null)
                out.close();
        } catch (Exception e) {
        }
    }
}
Also used : RandomAccessOutputStream(loci.common.RandomAccessOutputStream) TiffSaver(loci.formats.tiff.TiffSaver) RandomAccessInputStream(loci.common.RandomAccessInputStream) IOException(java.io.IOException) FormatException(loci.formats.FormatException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 8 with TiffSaver

use of loci.formats.tiff.TiffSaver in project bioformats by openmicroscopy.

the class TiffSaverTest method setUp.

@BeforeMethod
public void setUp() throws IOException {
    ByteArrayHandle handle = new ByteArrayHandle(INITIAL_CAPACITY);
    out = new RandomAccessOutputStream(handle);
    in = new RandomAccessInputStream(handle);
    tiffSaver = new TiffSaver(out, handle);
    tiffParser = new TiffParser(in);
    ifd = new IFD();
    ifd.putIFDValue(IFD.IMAGE_WIDTH, 512);
    ifd.putIFDValue(IFD.IMAGE_DESCRIPTION, "comment");
}
Also used : IFD(loci.formats.tiff.IFD) RandomAccessOutputStream(loci.common.RandomAccessOutputStream) TiffSaver(loci.formats.tiff.TiffSaver) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 9 with TiffSaver

use of loci.formats.tiff.TiffSaver in project bioformats by openmicroscopy.

the class CommentSurgery method main.

public static void main(String[] args) throws Exception {
    // the -test flag will print proposed changes to stdout
    // rather than actually changing the comment
    boolean test = args[0].equals("-test");
    for (int i = 0; i < args.length; i++) {
        String id = args[i];
        if (!test)
            System.out.println(id + ": ");
        String xml = new TiffParser(id).getComment();
        if (xml == null) {
            System.out.println("ERROR: No OME-XML comment.");
            return;
        }
        int len = xml.length();
        if (test)
            System.out.println(xml);
        else {
            System.out.println(len + " -> " + xml.length());
            TiffSaver saver = new TiffSaver(id);
            RandomAccessInputStream in = new RandomAccessInputStream(id);
            saver.overwriteComment(in, xml);
            in.close();
            saver.close();
        }
    }
}
Also used : TiffSaver(loci.formats.tiff.TiffSaver) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 10 with TiffSaver

use of loci.formats.tiff.TiffSaver in project bioformats by openmicroscopy.

the class OMETiffWriter method saveComment.

private void saveComment(String file, String xml) throws IOException {
    if (out != null)
        out.close();
    out = new RandomAccessOutputStream(file);
    RandomAccessInputStream in = null;
    try {
        TiffSaver saver = new TiffSaver(out, file);
        saver.setBigTiff(isBigTiff);
        in = new RandomAccessInputStream(file);
        saver.overwriteLastIFDOffset(in);
        saver.overwriteComment(in, xml);
    } catch (FormatException exc) {
        IOException io = new IOException("Unable to append OME-XML comment");
        io.initCause(exc);
        throw io;
    } finally {
        if (out != null)
            out.close();
        if (in != null)
            in.close();
    }
}
Also used : RandomAccessOutputStream(loci.common.RandomAccessOutputStream) TiffSaver(loci.formats.tiff.TiffSaver) RandomAccessInputStream(loci.common.RandomAccessInputStream) IOException(java.io.IOException) FormatException(loci.formats.FormatException)

Aggregations

TiffSaver (loci.formats.tiff.TiffSaver)10 RandomAccessOutputStream (loci.common.RandomAccessOutputStream)9 RandomAccessInputStream (loci.common.RandomAccessInputStream)7 ByteArrayHandle (loci.common.ByteArrayHandle)4 IOException (java.io.IOException)3 FormatException (loci.formats.FormatException)3 TiffParser (loci.formats.tiff.TiffParser)3 Test (org.testng.annotations.Test)3 IFD (loci.formats.tiff.IFD)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ImageReader (loci.formats.ImageReader)1 BeforeMethod (org.testng.annotations.BeforeMethod)1