Search in sources :

Example 16 with CMMException

use of java.awt.color.CMMException in project openolat by klemens.

the class ImageHelperImpl method scaleImage.

/**
 * @param image The image to scale
 * @param imageExt The extension if not given by the image file (optional)
 * @param scaledImaged the new scaled image
 * @param maxWidth the maximum width of the new scaled image
 * @param maxheight the maximum height of the new scaled image
 * @return
 */
@Override
public Size scaleImage(File image, String imageExt, File scaledImage, int maxWidth, int maxHeight, boolean fill) {
    ImageInputStream imageSrc = null;
    try {
        imageSrc = new FileImageInputStream(image);
        SizeAndBufferedImage scaledSize = calcScaledSize(imageSrc, imageExt, maxWidth, maxHeight, fill);
        if (scaledSize == null || scaledSize.image == null) {
            return null;
        }
        if (!scaledSize.getScaledSize().isChanged() && isSameFormat(image, imageExt, scaledImage)) {
            if (FileUtils.copyFileToFile(image, scaledImage, false)) {
                return scaledSize.getSize();
            }
        }
        BufferedImage bufferedImage = scaledSize.image;
        BufferedImage scaledBufferedImage = scaleTo(bufferedImage, scaledSize.getScaledSize());
        if (writeTo(scaledBufferedImage, scaledImage, scaledSize.getScaledSize(), getImageFormat(scaledImage))) {
            return scaledSize.getScaledSize();
        }
        return null;
    } catch (IOException e) {
        return null;
    // fxdiff FXOLAT-109: prevent red screen if the image has wrong EXIF data
    } catch (CMMException e) {
        return null;
    } finally {
        closeQuietly(imageSrc);
    }
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) CMMException(java.awt.color.CMMException) BufferedImage(java.awt.image.BufferedImage)

Example 17 with CMMException

use of java.awt.color.CMMException in project openolat by klemens.

the class ImageHelperImpl method scaleImage.

/**
 * @param image the image to scale
 * @param scaledImaged the new scaled image
 * @param maxSize the maximum size (height or width) of the new scaled image
 * @return
 */
@Override
public Size scaleImage(VFSLeaf image, VFSLeaf scaledImage, int maxWidth, int maxHeight, boolean fill) {
    OutputStream bos = null;
    ImageInputStream ins = null;
    try {
        ins = getInputStream(image);
        if (ins == null) {
            return null;
        }
        String extension = FileUtils.getFileSuffix(image.getName());
        SizeAndBufferedImage scaledSize = calcScaledSize(ins, extension, maxWidth, maxHeight, fill);
        if (scaledSize == null || scaledSize.getImage() == null) {
            return null;
        }
        ins = getInputStream(image);
        if (ins == null) {
            return null;
        }
        bos = new BufferedOutputStream(scaledImage.getOutputStream(false));
        if (!scaledSize.getScaledSize().isChanged() && isSameFormat(image, scaledImage)) {
            InputStream cloneIns = image.getInputStream();
            IOUtils.copy(cloneIns, bos);
            IOUtils.closeQuietly(cloneIns);
            return scaledSize.getScaledSize();
        } else {
            BufferedImage imageSrc = scaledSize.getImage();
            BufferedImage scaledSrc = scaleTo(imageSrc, scaledSize.getScaledSize());
            boolean scaled = writeTo(scaledSrc, bos, scaledSize.getScaledSize(), getImageFormat(scaledImage));
            if (scaled) {
                return scaledSize.getScaledSize();
            }
            return null;
        }
    } catch (IOException e) {
        return null;
    // fxdiff FXOLAT-109: prevent red screen if the image has wrong EXIF data
    } catch (CMMException e) {
        return null;
    } finally {
        closeQuietly(ins);
        FileUtils.closeSafely(bos);
    }
}
Also used : MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) FileInputStream(java.io.FileInputStream) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) CMMException(java.awt.color.CMMException) BufferedImage(java.awt.image.BufferedImage)

Aggregations

CMMException (java.awt.color.CMMException)17 BufferedImage (java.awt.image.BufferedImage)8 IOException (java.io.IOException)8 FileImageInputStream (javax.imageio.stream.FileImageInputStream)6 ImageInputStream (javax.imageio.stream.ImageInputStream)6 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)6 ImageLayoutException (sun.java2d.cmm.lcms.LCMSImageLayout.ImageLayoutException)5 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)3 ICC_Profile (java.awt.color.ICC_Profile)3 InputStream (java.io.InputStream)3 ComponentColorModel (java.awt.image.ComponentColorModel)2 ComponentSampleModel (java.awt.image.ComponentSampleModel)2 SampleModel (java.awt.image.SampleModel)2 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)2 BufferedOutputStream (java.io.BufferedOutputStream)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)2 ImageOutputStream (javax.imageio.stream.ImageOutputStream)2 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)2