Search in sources :

Example 91 with ImageTypeSpecifier

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;
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) Size(org.olat.core.commons.services.image.Size) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 92 with ImageTypeSpecifier

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;
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Aggregations

ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)92 BufferedImage (java.awt.image.BufferedImage)36 IIOMetadata (javax.imageio.metadata.IIOMetadata)34 ImageWriter (javax.imageio.ImageWriter)30 IIOImage (javax.imageio.IIOImage)25 IOException (java.io.IOException)23 ImageOutputStream (javax.imageio.stream.ImageOutputStream)23 ColorModel (java.awt.image.ColorModel)21 ImageReader (javax.imageio.ImageReader)20 ImageWriteParam (javax.imageio.ImageWriteParam)20 SampleModel (java.awt.image.SampleModel)18 ImageReadParam (javax.imageio.ImageReadParam)16 Rectangle (java.awt.Rectangle)14 File (java.io.File)13 Iterator (java.util.Iterator)13 ColorSpace (java.awt.color.ColorSpace)11 IndexColorModel (java.awt.image.IndexColorModel)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 Point (java.awt.Point)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9