public void writeImage(TIFFTranscoder transcoder, BufferedImage img,
TranscoderOutput output) throws TranscoderException {
TranscodingHints hints = transcoder.getTranscodingHints();
ImageWriter writer = ImageWriterRegistry.getInstance()
.getWriterFor("image/tiff");
ImageWriterParams params = new ImageWriterParams();
float PixSzMM = transcoder.getUserAgent().getPixelUnitToMillimeter();
int PixSzInch = (int)(25.4 / PixSzMM + 0.5);
params.setResolution(PixSzInch);
if (hints.containsKey(TIFFTranscoder.KEY_COMPRESSION_METHOD)) {
String method = (String)hints.get(TIFFTranscoder.KEY_COMPRESSION_METHOD);
//Values set here as defined in TIFFImageWriteParam of JAI Image I/O Tools
if ("packbits".equals(method)) {
params.setCompressionMethod("PackBits");
} else if ("deflate".equals(method)) {
params.setCompressionMethod("Deflate");
} else if ("lzw".equals(method)) {
params.setCompressionMethod("LZW");
} else if ("jpeg".equals(method)) {
params.setCompressionMethod("JPEG");
} else {
//nop
}
}
try {
OutputStream ostream = output.getOutputStream();
int w = img.getWidth();
int h = img.getHeight();
SinglePixelPackedSampleModel sppsm;
sppsm = (SinglePixelPackedSampleModel)img.getSampleModel();
int bands = sppsm.getNumBands();
int [] off = new int[bands];
for (int i = 0; i < bands; i++)
off[i] = i;
SampleModel sm = new PixelInterleavedSampleModel
(DataBuffer.TYPE_BYTE, w, h, bands, w * bands, off);
RenderedImage rimg = new FormatRed(GraphicsUtil.wrap(img), sm);
writer.writeImage(rimg, ostream, params);
ostream.flush();
} catch (IOException ex) {
throw new TranscoderException(ex);
}
}