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;
}
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();
}
}
}
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!");
}
}
Aggregations