use of com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriterSpi in project bioformats by openmicroscopy.
the class JAIIIOServiceImpl method writeImage.
/* @see JAIIIOService#writeImage(OutputStream, BufferedImage, JPEG2000CodecOptions) */
@Override
public void writeImage(OutputStream out, BufferedImage img, JPEG2000CodecOptions options) throws IOException, ServiceException {
ImageOutputStream ios = ImageIO.createImageOutputStream(out);
IIORegistry registry = IIORegistry.getDefaultInstance();
Iterator<J2KImageWriterSpi> iter = ServiceLoader.load(J2KImageWriterSpi.class).iterator();
registry.registerServiceProviders(iter);
J2KImageWriterSpi spi = registry.getServiceProviderByClass(J2KImageWriterSpi.class);
J2KImageWriter writer = new J2KImageWriter(spi);
writer.setOutput(ios);
String filter = options.lossless ? J2KImageWriteParam.FILTER_53 : J2KImageWriteParam.FILTER_97;
IIOImage iioImage = new IIOImage(img, null, null);
J2KImageWriteParam param = (J2KImageWriteParam) writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("JPEG2000");
if (!options.lossless) {
param.setEncodingRate(options.quality);
}
param.setLossless(options.lossless);
param.setFilter(filter);
param.setCodeBlockSize(options.codeBlockSize);
param.setComponentTransformation(false);
if (options.tileWidth > 0 && options.tileHeight > 0) {
param.setTiling(options.tileWidth, options.tileHeight, options.tileGridXOffset, options.tileGridYOffset);
}
if (options.numDecompositionLevels != null) {
param.setNumDecompositionLevels(options.numDecompositionLevels.intValue());
}
writer.write(null, iioImage, param);
ios.close();
}
Aggregations