Search in sources :

Example 1 with FileOpException

use of digilib.io.FileOpException in project digilib by robcast.

the class ImageJobDescription method getHiresSize.

/**
 * Return the size of the highest resolution image.
 *
 * @return
 * @throws IOException
 */
public ImageSize getHiresSize() throws IOException {
    // logger.debug("get_hiresSize()");
    if (hiresSize == null) {
        ImageSet fileset = getImageSet();
        ImageInput hiresFile = fileset.getBiggest();
        hiresSize = hiresFile.getSize();
        if (hiresSize == null) {
            throw new FileOpException("Can't get size from hires image file!");
        }
    }
    return hiresSize;
}
Also used : ImageInput(digilib.io.ImageInput) FileOpException(digilib.io.FileOpException) ImageSet(digilib.io.ImageSet)

Example 2 with FileOpException

use of digilib.io.FileOpException in project digilib by robcast.

the class ImageLoaderDocuImage method identify.

/* 
     * Check image size and type and store in ImageInput
     * @see digilib.image.ImageInfoDocuImage#identify(digilib.io.ImageInput)
     */
public ImageInput identify(ImageInput input) throws IOException {
    ImageInput ii = null;
    if (!reuseReader) {
        // try parent method first
        ii = super.identify(input);
        if (ii != null) {
            return ii;
        }
    }
    logger.debug("identifying (ImageIO) " + input);
    try {
        /*
             * try ImageReader
             */
        reader = getReader(input);
        // set size
        ImageSize d = new ImageSize(reader.getWidth(0), reader.getHeight(0));
        input.setSize(d);
        // set mime type
        if (input.getMimetype() == null) {
            if (input.hasFile()) {
                String t = FileOps.mimeForFile(input.getFile());
                input.setMimetype(t);
            } else {
                // FIXME: is format name a mime type???
                String t = reader.getFormatName();
                input.setMimetype(t);
            }
        }
        return input;
    } catch (FileOpException e) {
        // maybe just our class doesn't know what to do
        logger.error("ImageLoaderDocuimage unable to identify: " + e);
        return null;
    } finally {
        if (!reuseReader && reader != null) {
            reader.dispose();
        }
    }
}
Also used : ImageInput(digilib.io.ImageInput) ImageSize(digilib.util.ImageSize) FileOpException(digilib.io.FileOpException)

Example 3 with FileOpException

use of digilib.io.FileOpException in project digilib by robcast.

the class ImageLoaderDocuImage method writeImage.

/* 
     * (non-Javadoc)
     * @see digilib.image.DocuImageImpl#writeImage(java.lang.String, java.io.OutputStream)
     */
public void writeImage(String mt, OutputStream ostream) throws ImageOpException, FileOpException {
    logger.debug("writeImage");
    // setup output
    ImageWriter writer = null;
    ImageOutputStream imgout = null;
    try {
        imgout = ImageIO.createImageOutputStream(ostream);
        if (mt == "image/jpeg") {
            /*
                 * JPEG doesn't do transparency so we have to convert any RGBA
                 * image to RGB or we the client will think its CMYK :-( *Java2D
                 * BUG*
                 */
            if (img.getColorModel().hasAlpha()) {
                logger.debug("BARF: JPEG with transparency!!");
                BufferedImage rgbImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
                rgbImg.createGraphics().drawImage(img, null, 0, 0);
                img = rgbImg;
            }
            writer = ImageIO.getImageWritersByFormatName("jpeg").next();
            if (writer == null) {
                throw new ImageOpException("Unable to get JPEG writer");
            }
            ImageWriteParam param = writer.getDefaultWriteParam();
            if (quality > 1) {
                // change JPEG compression quality
                param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                param.setCompressionQuality(0.9f);
            }
            writer.setOutput(imgout);
            // render output
            logger.debug("writing JPEG");
            writer.write(null, new IIOImage(img, null, null), param);
        } else if (mt == "image/png") {
            // render output
            writer = ImageIO.getImageWritersByFormatName("png").next();
            if (writer == null) {
                throw new ImageOpException("Unable to get PNG writer");
            }
            writer.setOutput(imgout);
            logger.debug("writing PNG");
            writer.write(img);
        } else {
            // unknown mime type
            throw new ImageOpException("Unknown mime type: " + mt);
        }
    } catch (IOException e) {
        logger.error("Error writing image:", e);
        throw new FileOpException("Error writing image!", e);
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (imgout != null) {
            /* 
        		 * ImageOutputStream likes to keep ServletOutputStream and close it when disposed.
        		 * Thanks to Tom Van Wietmarschen's mail to tomcat-users on July 4, 2008!
        		 */
            try {
                imgout.close();
            } catch (IOException e) {
                logger.error("Error closing ImageOutputStream!", e);
            }
        }
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) FileOpException(digilib.io.FileOpException) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 4 with FileOpException

use of digilib.io.FileOpException 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 5 with FileOpException

use of digilib.io.FileOpException in project digilib by robcast.

the class JAIDocuImage method loadSubimage.

/* Load an image file into the Object. */
public void loadSubimage(ImageInput ii, Rectangle region, int subsample) throws FileOpException {
    logger.debug("loadSubimage");
    this.input = ii;
    if (ii.hasFile()) {
        img = JAI.create("fileload", ii.getFile().getAbsolutePath());
    } else if (ii.hasInputStream()) {
        img = JAI.create("stream", ii.getInputStream());
    } else {
        throw new FileOpException("unable to get data for image!");
    }
    if ((region.width < img.getWidth()) || (region.height < img.getHeight())) {
        // setup Crop
        ParameterBlock cp = new ParameterBlock();
        cp.addSource(img);
        cp.add((float) region.x);
        cp.add((float) region.y);
        cp.add((float) region.width);
        cp.add((float) region.height);
        logger.debug("loadSubimage: crop");
        img = JAI.create("crop", cp);
    }
    if (subsample > 1) {
        float sc = 1f / subsample;
        ParameterBlockJAI sp = new ParameterBlockJAI("scale");
        sp.addSource(img);
        sp.setParameter("xScale", sc);
        sp.setParameter("yScale", sc);
        sp.setParameter("interpolation", Interpolation.getInstance(Interpolation.INTERP_NEAREST));
        // scale
        logger.debug("loadSubimage: scale");
        img = JAI.create("scale", sp);
    }
}
Also used : ParameterBlock(java.awt.image.renderable.ParameterBlock) ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) FileOpException(digilib.io.FileOpException)

Aggregations

FileOpException (digilib.io.FileOpException)18 IOException (java.io.IOException)10 ImageInput (digilib.io.ImageInput)4 ImageSize (digilib.util.ImageSize)3 ParameterBlock (java.awt.image.renderable.ParameterBlock)3 File (java.io.File)3 ImageSet (digilib.io.ImageSet)2 BufferedImage (java.awt.image.BufferedImage)2 FileInputStream (java.io.FileInputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 ImageReadParam (javax.imageio.ImageReadParam)2 ImageReader (javax.imageio.ImageReader)2 FileImageInputStream (javax.imageio.stream.FileImageInputStream)2 ImageInputStream (javax.imageio.stream.ImageInputStream)2 FormatException (loci.formats.FormatException)2 DigilibConfiguration (digilib.conf.DigilibConfiguration)1 ImageJobDescription (digilib.image.ImageJobDescription)1 ImageOpException (digilib.image.ImageOpException)1 DocuDirCache (digilib.io.DocuDirCache)1 DocuDirent (digilib.io.DocuDirent)1