bugfix: correctly handle very thin images
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 7 Dec 2014 20:29:04 +0000 (22:29 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 7 Dec 2014 20:29:04 +0000 (22:29 +0200)
src/main/java/eu/svjatoslav/meviz/htmlindexer/metadata/fileTypes/Picture.java

index 9af52df..2b9b802 100755 (executable)
@@ -32,6 +32,13 @@ import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
 
 public class Picture extends AbstractFile {
 
+       private static void ensureNonzeroImageArea(final java.awt.Dimension result) {
+               if (result.width < 1)
+                       result.width = 1;
+               if (result.height < 1)
+                       result.height = 1;
+       }
+
        /**
         * Actual returned target thumbnail size will be adjusted from desired one
         * by attempting to generate as large as possible thumbnail, while not
@@ -53,6 +60,8 @@ public class Picture extends AbstractFile {
                                        desiredTargetDimension.width,
                                        (int) (desiredTargetDimension.width / inputImageWidthToHeightRatio));
 
+                       ensureNonzeroImageArea(result);
+
                        return result;
                } else {
 
@@ -60,6 +69,8 @@ public class Picture extends AbstractFile {
                                        (int) (desiredTargetDimension.height * inputImageWidthToHeightRatio),
                                        desiredTargetDimension.height);
 
+                       ensureNonzeroImageArea(result);
+
                        return result;
                }
        }
@@ -122,16 +133,15 @@ public class Picture extends AbstractFile {
        public static Image scaleImage(final Image srcImage, final int width,
                        final int height) {
 
-               ImageFilter filter;
-               filter = new java.awt.image.AreaAveragingScaleFilter(width, height);
+               final ImageFilter filter = new java.awt.image.AreaAveragingScaleFilter(
+                               width, height);
 
                final ImageProducer prod = new FilteredImageSource(
                                srcImage.getSource(), filter);
 
                final Image newImage = Toolkit.getDefaultToolkit().createImage(prod);
 
-               final ImageIcon imageIcon = new ImageIcon(newImage);
-               return imageIcon.getImage();
+               return new ImageIcon(newImage).getImage();
        }
 
        private static final long serialVersionUID = -4156533490858298387L;;