Search in sources :

Example 1 with BufferedImageWriter

use of loci.formats.gui.BufferedImageWriter in project digilib by robcast.

the class BioFormatsDocuImage method writeImage.

@Override
public void writeImage(String mt, OutputStream ostream) throws ImageOpException, FileOpException {
    logger.debug("writeImage");
    File outFile;
    String filext = ".jpg";
    if (mt.equals("image/png")) {
        filext = ".png";
    }
    try {
        outFile = File.createTempFile("biof_temp", filext);
    } catch (IOException e) {
        throw new FileOpException(e.toString());
    }
    // save image to file
    ImageWriter iw = new ImageWriter();
    /*
         * try { //iw.setMetadataRetrieve(new DummyMetadata());
         * //iw.setSeries(0); //iw.setId(outFile.getAbsolutePath());
         * //logger.debug("writer="+iw); } catch (FormatException e) { throw new
         * FileOpException(e.toString()); } catch (IOException e) { // TODO
         * Auto-generated catch block e.printStackTrace(); } /* if
         * (mt.endsWith("png")) { iw = }
         */
    BufferedImageWriter writer = BufferedImageWriter.makeBufferedImageWriter(iw);
    try {
        logger.debug("setting metadata " + meta);
        iw.setMetadataRetrieve(meta);
        logger.debug("writing to file  " + outFile);
        writer.setId(outFile.getAbsolutePath());
        logger.debug("fixing metadata " + meta);
        iw.setInterleaved(reader.isInterleaved());
        iw.setWriteSequentially(true);
        meta.setPixelsSizeX(new PositiveInteger(img.getWidth()), 0);
        meta.setPixelsSizeY(new PositiveInteger(img.getHeight()), 0);
        meta.setPixelsSizeC(new PositiveInteger(img.getColorModel().getNumComponents()), 0);
        logger.debug("saving image " + img);
        writer.saveImage(0, img);
        logger.debug("closing file");
        writer.close();
    } catch (FormatException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    // now send file
    FileInputStream inFile = null;
    try {
        inFile = new FileInputStream(outFile);
        byte[] dataBuffer = new byte[4096];
        int len;
        while ((len = inFile.read(dataBuffer)) != -1) {
            // copy out file
            ostream.write(dataBuffer, 0, len);
        }
    } catch (IOException e) {
        throw new FileOpException(e.toString());
    } finally {
        try {
            if (inFile != null) {
                inFile.close();
            }
        } catch (IOException e) {
        // nothing to do
        }
    }
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) FileOpException(digilib.io.FileOpException) BufferedImageWriter(loci.formats.gui.BufferedImageWriter) ImageWriter(loci.formats.ImageWriter) BufferedImageWriter(loci.formats.gui.BufferedImageWriter) IOException(java.io.IOException) File(java.io.File) FormatException(loci.formats.FormatException) FileInputStream(java.io.FileInputStream)

Example 2 with BufferedImageWriter

use of loci.formats.gui.BufferedImageWriter in project bioformats by openmicroscopy.

the class SumPlanes method main.

public static void main(String[] args) throws Exception {
    String id = args[0];
    BufferedImageReader r = new BufferedImageReader();
    System.out.print("Reading " + id);
    r.setId(id);
    int imageCount = r.getImageCount();
    BufferedImage[] images = new BufferedImage[imageCount];
    for (int i = 0; i < imageCount; i++) {
        System.out.print(".");
        images[i] = r.openImage(i);
    }
    r.close();
    System.out.println(" [done]");
    String outId = id + ".tif";
    BufferedImageWriter w = new BufferedImageWriter();
    System.out.print("Writing " + outId);
    w.setId(outId);
    w.saveImage(0, sum(images));
    w.close();
    System.out.println(" [done]");
}
Also used : BufferedImageWriter(loci.formats.gui.BufferedImageWriter) BufferedImageReader(loci.formats.gui.BufferedImageReader)

Aggregations

BufferedImageWriter (loci.formats.gui.BufferedImageWriter)2 FileOpException (digilib.io.FileOpException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 FormatException (loci.formats.FormatException)1 ImageWriter (loci.formats.ImageWriter)1 BufferedImageReader (loci.formats.gui.BufferedImageReader)1 PositiveInteger (ome.xml.model.primitives.PositiveInteger)1