}
if ( original == null ) {
// If there are no instances, nothing can be exported
log.warn( "Error - no original image was found!!!" );
txw.commit();
throw new PhotovaultException( "No image file found to export photo" );
}
// Read the image
RenderedImage exportImage = null;
try {
File imageFile = original.getImageFile();
String fname = imageFile.getName();
int lastDotPos = fname.lastIndexOf( "." );
if ( lastDotPos <= 0 || lastDotPos >= fname.length()-1 ) {
throw new IOException( "Cannot determine file type extension of " + imageFile.getAbsolutePath() );
}
PhotovaultImageFactory imageFactory = new PhotovaultImageFactory();
PhotovaultImage img = null;
try {
/*
Do not read the image yet since setting raw conversion
parameters later may force a re-read.
*/
img = imageFactory.create(imageFile, false, false);
} catch (PhotovaultException ex) {
log.error( ex.getMessage() );
}
img.setCropBounds( this.getCropBounds() );
img.setRotation( prefRotation - original.getRotated() );
if ( channelMap != null ) {
img.setColorAdjustment( channelMap );
}
if ( img instanceof RawImage ) {
RawImage ri = (RawImage) img;
if ( rawSettings != null ) {
ri.setRawSettings( rawSettings );
} else if ( rawSettings == null ) {
// No raw settings for this photo yet, let's use
// the thumbnail settings
rawSettings = ri.getRawSettings();
txw.lock( rawSettings, Transaction.WRITE );
}
}
if ( width > 0 ) {
exportImage =img.getRenderedImage( width, height, false );
} else {
exportImage =img.getRenderedImage( 1.0, false );
}
} catch ( Exception e ) {
log.warn( "Error reading image: " + e.getMessage() );
txw.abort();
throw new PhotovaultException( "Error reading image: " + e.getMessage(), e );
}
// Reduce to 8 bit samples if we have more...
if ( exportImage.getSampleModel().getSampleSize( 0 ) == 16 ) {
double[] subtract = new double[1]; subtract[0] = 0;
double[] divide = new double[1]; divide[0] = 1./256.;
// Now we can rescale the pixels gray levels:
ParameterBlock pbRescale = new ParameterBlock();
pbRescale.add(divide);
pbRescale.add(subtract);
pbRescale.addSource( exportImage );
PlanarImage outputImage = (PlanarImage)JAI.create("rescale", pbRescale, null);
// Make sure it is a byte image - force conversion.
ParameterBlock pbConvert = new ParameterBlock();
pbConvert.addSource(outputImage);
pbConvert.add(DataBuffer.TYPE_BYTE);
exportImage = JAI.create("format", pbConvert);
}
// Try to determine the file type based on extension
String ftype = "jpg";
String imageFname = file.getName();
int extIndex = imageFname.lastIndexOf( "." ) + 1;
if ( extIndex > 0 ) {
ftype = imageFname.substring( extIndex );
}
try {
// Find a writer for that file extensions
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName( ftype );
if (iter.hasNext()) writer = (ImageWriter)iter.next();
if (writer != null) {
ImageOutputStream ios = null;
try {
// Prepare output file
ios = ImageIO.createImageOutputStream( file );
writer.setOutput(ios);
// Set some parameters
ImageWriteParam param = writer.getDefaultWriteParam();
// if bi has type ARGB and alpha is false, we have
// to tell the writer to not use the alpha
// channel: this is especially needed for jpeg
// files where imageio seems to produce wrong jpeg
// files right now...
// if (exportImage.getType() == BufferedImage.TYPE_INT_ARGB ) {
// // this is not so obvious: create a new
// // ColorModel without OPAQUE transparency and
// // no alpha channel.
// ColorModel cm = new ComponentColorModel(exportImage.getColorModel().getColorSpace(),
// false, false,
// Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
// // tell the writer to only use the first 3 bands (skip alpha)
// int[] bands = {0, 1, 2};
// param.setSourceBands(bands);
// // although the java documentation says that
// // SampleModel can be null, an exception is
// // thrown in that case therefore a 1*1
// // SampleModel that is compatible to cm is
// // created:
// param.setDestinationType(new ImageTypeSpecifier(cm,
// cm.createCompatibleSampleModel(1, 1)));
// }
// Write the image
writer.write(null, new IIOImage(exportImage, null, null), param);
// Cleanup
ios.flush();
} finally {
if (ios != null) ios.close();
writer.dispose();
if ( exportImage != null & exportImage instanceof PlanarImage ) {
((PlanarImage)exportImage).dispose();
System.gc();
}
}
}
} catch ( IOException e ) {
log.warn( "Error writing exported image: " + e.getMessage() );
txw.abort();
throw new PhotovaultException( "Error writing exported image: " + e.getMessage(), e );
}
txw.commit();
}