Search in sources :

Example 16 with FileOpException

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

the class ImageLoaderDocuImage method getReader.

/**
 * Get an ImageReader for the image file.
 *
 * @return
 */
public ImageReader getReader(ImageInput input) throws IOException {
    logger.debug("get ImageReader for " + input);
    if (reuseReader && reader != null) {
        logger.debug("reuseing ImageReader");
        return reader;
    }
    ImageInputStream istream = null;
    if (input.hasImageInputStream()) {
        // ImageInputStream input
        istream = input.getImageInputStream();
    } else if (input.hasFile()) {
        // file only input
        RandomAccessFile rf = new RandomAccessFile(input.getFile(), "r");
        istream = new FileImageInputStream(rf);
    } else {
        throw new FileOpException("Unable to get data from ImageInput");
    }
    Iterator<ImageReader> readers;
    String mt = null;
    if (input.hasMimetype()) {
        // check hasMimetype first or we might get into a loop
        mt = input.getMimetype();
    } else {
        // try file extension
        mt = FileOps.mimeForFile(input.getFile());
    }
    if (mt == null) {
        logger.debug("No mime-type. Trying automagic.");
        readers = ImageIO.getImageReaders(istream);
    } else {
        logger.debug("File type:" + mt);
        readers = ImageIO.getImageReadersByMIMEType(mt);
    }
    if (!readers.hasNext()) {
        throw new FileOpException("Can't find Reader to load File!");
    }
    ImageReader reader = readers.next();
    if (reader == null) {
        throw new FileOpException("Error getting Reader to load File!");
    }
    logger.debug("ImageIO: this reader: " + reader.getClass());
    /*
        while (readers.hasNext()) { 
        	logger.debug("ImageIO: next reader: " + readers.next().getClass()); 
        }
        */
    reader.setInput(istream);
    return reader;
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) RandomAccessFile(java.io.RandomAccessFile) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) FileOpException(digilib.io.FileOpException) ImageReader(javax.imageio.ImageReader)

Example 17 with FileOpException

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

the class ImageLoaderDocuImage method loadSubimage.

/* 
     * Load an image file into the Object.
     * 
     * @see digilib.image.DocuImageImpl#loadSubimage(digilib.io.ImageInput, java.awt.Rectangle, int)
     */
public void loadSubimage(ImageInput ii, Rectangle region, int prescale) throws FileOpException {
    logger.debug("loadSubimage");
    this.input = ii;
    // ImageReader reader = null;
    try {
        reader = getReader(ii);
        /*
             * set up reader parameters
             */
        ImageReadParam readParam = reader.getDefaultReadParam();
        readParam.setSourceRegion(region);
        if (prescale > 1) {
            readParam.setSourceSubsampling(prescale, prescale, 0, 0);
        }
        if (imageHacks.get(Hacks.setDestSrgb)) {
            /*
				 * try to set target color space to sRGB
				 */
            for (Iterator<ImageTypeSpecifier> i = reader.getImageTypes(0); i.hasNext(); ) {
                ImageTypeSpecifier type = (ImageTypeSpecifier) i.next();
                ColorModel cm = type.getColorModel();
                ColorSpace cs = cm.getColorSpace();
                logger.debug("loadSubimage: possible color model:" + cm + " color space:" + cs);
                if (cs.getNumComponents() < 3 && !imageHacks.get(Hacks.setDestSrgbForNonRgb)) {
                    // if the first type is not RGB do nothing
                    logger.debug("loadSubimage: image is not RGB " + type);
                    break;
                }
                if (cs.isCS_sRGB()) {
                    logger.debug("loadSubimage: substituted sRGB destination type " + type);
                    readParam.setDestinationType(type);
                    break;
                }
            }
        }
        /*
             * read image
             */
        logger.debug("loadSubimage: loading..");
        img = reader.read(0, readParam);
        logger.debug("loadSubimage: loaded " + img);
        // invalidate image size if it was set
        imageSize = null;
        /*
             * downconvert highcolor images
             */
        if (img.getColorModel().getComponentSize(0) > 8) {
            logger.debug("loadSubimage: converting to 8bit");
            int type = BufferedImage.TYPE_INT_RGB;
            if (img.getColorModel().hasAlpha()) {
                type = BufferedImage.TYPE_INT_ARGB;
            }
            BufferedImage lcImg = new BufferedImage(img.getWidth(), img.getHeight(), type);
            lcImg.createGraphics().drawImage(img, null, 0, 0);
            img = lcImg;
        }
    } catch (IOException e) {
        throw new FileOpException("Unable to load File!", e);
    } finally {
        if (!reuseReader && reader != null) {
            reader.dispose();
        }
    }
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) IndexColorModel(java.awt.image.IndexColorModel) ColorModel(java.awt.image.ColorModel) ColorSpace(java.awt.color.ColorSpace) FileOpException(digilib.io.FileOpException) IOException(java.io.IOException) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) BufferedImage(java.awt.image.BufferedImage)

Example 18 with FileOpException

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

the class ImageLoaderDocuImage method loadImage.

/* 
     * load image file
     * @see digilib.image.DocuImageImpl#loadImage(digilib.io.ImageInput)
     */
public void loadImage(ImageInput ii) throws FileOpException {
    logger.debug("loadImage: " + ii);
    this.input = ii;
    try {
        if (ii.hasImageInputStream()) {
            img = ImageIO.read(ii.getImageInputStream());
        } else if (ii.hasFile()) {
            img = ImageIO.read(ii.getFile());
        }
    } catch (IOException e) {
        throw new FileOpException("Error reading image!", e);
    }
    if (img == null) {
        throw new FileOpException("Unable to read image!");
    }
}
Also used : FileOpException(digilib.io.FileOpException) IOException(java.io.IOException)

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