use of javax.imageio.ImageTypeSpecifier in project openolat by klemens.
the class ImageHelperImpl method calcScaledSize.
private static SizeAndBufferedImage calcScaledSize(ImageInputStream stream, String suffix, int maxWidth, int maxHeight, boolean fill) {
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
if (iter.hasNext()) {
ImageReader reader = iter.next();
try {
reader.setInput(stream, true, true);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
Size size = new Size(width, height, false);
Size scaledSize = computeScaledSize(width, height, maxWidth, maxHeight, fill);
SizeAndBufferedImage all = new SizeAndBufferedImage(size, scaledSize);
int readerMinIndex = reader.getMinIndex();
ImageReadParam param = reader.getDefaultReadParam();
Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
while (imageTypes.hasNext()) {
try {
ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
param.setDestinationType(imageTypeSpecifier);
}
double memoryKB = (width * height * 4) / 1024d;
if (memoryKB > 2000) {
// check limit at 20MB
double free = Runtime.getRuntime().freeMemory() / 1024d;
if (free > memoryKB) {
all.setImage(reader.read(readerMinIndex, param));
} else {
// make sub sampling to save memory
int ratio = (int) Math.round(Math.sqrt(memoryKB / free));
param.setSourceSubsampling(ratio, ratio, 0, 0);
all.setImage(reader.read(readerMinIndex, param));
}
} else {
all.setImage(reader.read(readerMinIndex, param));
}
return all;
} catch (IllegalArgumentException e) {
log.warn(e.getMessage(), e);
}
}
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
reader.dispose();
}
} else {
log.error("No reader found for given format: " + suffix, null);
}
return null;
}
use of javax.imageio.ImageTypeSpecifier in project sldeditor by robward-scisys.
the class LegendManager method saveGridImage.
/**
* Save image.
*
* @param image the grid image
* @param formatName the format name
* @param destinationFile the output
* @param dpi the dpi
* @return true, if successful
* @throws IOException Signals that an I/O exception has occurred.
*/
private boolean saveGridImage(BufferedImage image, String formatName, File destinationFile, int dpi) throws IOException {
if (!Files.delete(destinationFile)) {
ConsoleManager.getInstance().information(this, String.format("Failed to delete '%s'", destinationFile.getAbsolutePath()));
}
boolean finish = false;
for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext() && !finish; ) {
ImageWriter writer = iw.next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();
ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
if (!(metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported())) {
setDPI(metadata, dpi);
final ImageOutputStream stream = ImageIO.createImageOutputStream(destinationFile);
try {
writer.setOutput(stream);
writer.write(metadata, new IIOImage(image, null, metadata), writeParam);
} finally {
stream.close();
}
finish = true;
}
}
return true;
}
Aggregations