package org.apache.xmlgraphics.image.writer.internal;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.xmlgraphics.image.codec.tiff.CompressionValue;
import org.apache.xmlgraphics.image.codec.tiff.TIFFEncodeParam;
import org.apache.xmlgraphics.image.codec.tiff.TIFFField;
import org.apache.xmlgraphics.image.codec.tiff.TIFFImageDecoder;
import org.apache.xmlgraphics.image.codec.tiff.TIFFImageEncoder;
import org.apache.xmlgraphics.image.writer.AbstractImageWriter;
import org.apache.xmlgraphics.image.writer.ImageWriterParams;
import org.apache.xmlgraphics.image.writer.MultiImageWriter;
import org.apache.xmlgraphics.image.writer.ResolutionUnit;
public class TIFFImageWriter extends AbstractImageWriter {
public void writeImage(RenderedImage image, OutputStream out)
throws IOException {
writeImage(image, out, null);
}
public void writeImage(RenderedImage image, OutputStream out,
ImageWriterParams params) throws IOException {
TIFFEncodeParam encodeParams = createTIFFEncodeParams(params);
TIFFImageEncoder encoder = new TIFFImageEncoder(out, encodeParams);
encoder.encode(image);
}
private TIFFEncodeParam createTIFFEncodeParams(ImageWriterParams params) {
TIFFEncodeParam encodeParams = new TIFFEncodeParam();
if (params == null) {
encodeParams.setCompression(CompressionValue.NONE);
} else {
encodeParams.setCompression(CompressionValue.getValue(params.getCompressionMethod()));
if (params.getResolution() != null) {
int numPixX;
int numPixY;
int denom;
if (ResolutionUnit.INCH == params.getResolutionUnit()) {
numPixX = params.getXResolution();
numPixY = params.getYResolution();
denom = 1;
} else {
float pixXSzMM = 25.4f / params.getXResolution().floatValue();
float pixYSzMM = 25.4f / params.getYResolution().floatValue();
numPixX = (int)(((1000 * 100) / pixXSzMM) + 0.5);
numPixY = (int)(((1000 * 100) / pixYSzMM) + 0.5);
denom = 100 * 100;
}
long [] xRational = {numPixX, denom};
long [] yRational = {numPixY, denom};
TIFFField [] fields = {
new TIFFField(TIFFImageDecoder.TIFF_RESOLUTION_UNIT,
TIFFField.TIFF_SHORT, 1,
new char[] {(char)params.getResolutionUnit().getValue()}),
new TIFFField(TIFFImageDecoder.TIFF_X_RESOLUTION,
TIFFField.TIFF_RATIONAL, 1,
new long[][] {xRational}),
new TIFFField(TIFFImageDecoder.TIFF_Y_RESOLUTION,
TIFFField.TIFF_RATIONAL, 1,
new long[][] {yRational})
};
encodeParams.setExtraFields(fields);
}
}
return encodeParams;
}
public String getMIMEType() {
return "image/tiff";
}
@Override
public MultiImageWriter createMultiImageWriter(OutputStream out) throws IOException {
return new TIFFMultiImageWriter(out);
}
@Override
public boolean supportsMultiImageWriter() {
return true;
}
private class TIFFMultiImageWriter implements MultiImageWriter {
private OutputStream out;
private TIFFEncodeParam encodeParams;
private TIFFImageEncoder encoder;
private Object context;
public TIFFMultiImageWriter(OutputStream out) throws IOException {
this.out = out;
}
public void writeImage(RenderedImage image, ImageWriterParams params) throws IOException {
if (encoder == null) {
encodeParams = createTIFFEncodeParams(params);
encoder = new TIFFImageEncoder(out, encodeParams);
}
context = encoder.encodeMultiple(context, image);
}
public void close() throws IOException {
if (encoder != null) {
encoder.finishMultiple(context);
}
encoder = null;
encodeParams = null;
out.flush();
}
}
}