Skip to content

Commit

Permalink
Merge pull request #1268 from sidheshenator/imgscalr_bug_handling
Browse files Browse the repository at this point in the history
Image resizing exception handled. Image cropped instead
  • Loading branch information
rcordovano committed May 27, 2015
2 parents 5887d2a + f4e93f5 commit 94c825e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Core/src/org/sleuthkit/autopsy/coreutils/ImageUtils.java
Expand Up @@ -53,7 +53,7 @@ public class ImageUtils {
private static final Logger logger = Logger.getLogger(ImageUtils.class.getName());
private static final Image DEFAULT_ICON = new ImageIcon("/org/sleuthkit/autopsy/images/file-icon.png").getImage(); //NON-NLS
private static final List<String> SUPP_EXTENSIONS = Arrays.asList(ImageIO.getReaderFileSuffixes());
private static final List<String> SUPP_MIME_TYPES = new ArrayList(Arrays.asList(ImageIO.getReaderMIMETypes()));
private static final List<String> SUPP_MIME_TYPES = new ArrayList<>(Arrays.asList(ImageIO.getReaderMIMETypes()));
static {
SUPP_MIME_TYPES.add("image/x-ms-bmp");
}
Expand Down Expand Up @@ -259,17 +259,24 @@ private static Image generateAndSaveIcon(Content content, int iconSize, File sav
private static BufferedImage generateIcon(Content content, int iconSize) {

InputStream inputStream = null;
BufferedImage bi = null;
try {
inputStream = new ReadContentInputStream(content);
BufferedImage bi = ImageIO.read(inputStream);
bi = ImageIO.read(inputStream);
if (bi == null) {
logger.log(Level.WARNING, "No image reader for file: " + content.getName()); //NON-NLS
return null;
}
BufferedImage biScaled = ScalrWrapper.resizeFast(bi, iconSize);

return biScaled;
} catch (OutOfMemoryError e) {
} catch (IllegalArgumentException e) {
// if resizing does not work due to extremely small height/width ratio,
// crop the image instead.
BufferedImage biCropped = ScalrWrapper.cropImage(bi, Math.min(iconSize, bi.getWidth()), Math.min(iconSize, bi.getHeight()));
return biCropped;
}
catch (OutOfMemoryError e) {
logger.log(Level.WARNING, "Could not scale image (too large): " + content.getName(), e); //NON-NLS
return null;
} catch (Exception e) {
Expand Down
5 changes: 5 additions & 0 deletions CoreLibs/src/org/sleuthkit/autopsy/corelibs/ScalrWrapper.java
Expand Up @@ -20,6 +20,7 @@
package org.sleuthkit.autopsy.corelibs;

import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Method;

Expand Down Expand Up @@ -48,4 +49,8 @@ public static synchronized BufferedImage resizeFast(BufferedImage input, int siz
public static synchronized BufferedImage resizeFast(BufferedImage input, int width, int height) {
return Scalr.resize(input, Method.SPEED, Scalr.Mode.AUTOMATIC, width, height, Scalr.OP_ANTIALIAS);
}

public static synchronized BufferedImage cropImage(BufferedImage input, int width, int height) {
return Scalr.crop(input, width, height, (BufferedImageOp) null);
}
}

0 comments on commit 94c825e

Please sign in to comment.