vp9 codec was slow and produced no keyframes. Trying vp8 instead.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / metadata / fileTypes / Picture.java
index 02a29b9..274b8ca 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Meviz - Various tools collection to work with multimedia.
  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public License
  * as published by the Free Software Foundation.
@@ -18,7 +18,6 @@ import java.awt.image.ImageFilter;
 import java.awt.image.ImageProducer;
 import java.io.File;
 import java.io.FileOutputStream;
-import java.io.IOException;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.List;
@@ -28,7 +27,6 @@ import javax.swing.ImageIcon;
 
 import eu.svjatoslav.commons.file.FilePathParser;
 import eu.svjatoslav.meviz.htmlindexer.Constants;
-import eu.svjatoslav.meviz.htmlindexer.ImageFormatError;
 import eu.svjatoslav.meviz.htmlindexer.Utils;
 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
 
@@ -36,52 +34,11 @@ public class Picture extends AbstractFile {
 
        private static final long serialVersionUID = -4156533490858298387L;
 
-       /**
-        * Picture dimensions.
-        */
-       private Dimension dimensions;
-
-       public Picture(final File parentDirectory, final String fileName)
-                       throws IOException, ImageFormatError {
-               super(parentDirectory, fileName);
-       }
-
-       private String computeThumbnailHash(final Dimension targetDimension) {
-
-               // compute new thumbnails hash number
-               final String forMagicHash = fileName + " "
-                               + String.valueOf(targetDimension.width) + " "
-                               + String.valueOf(targetDimension.height) + " "
-                               + String.valueOf(getFileLength()) + " "
-                               + Constants.THUMBNAIL_VERSION;
-
-               // System.out.println("Computing hash from: " + forMagicHash);
-               return Utils.getStringCrcAsHex(forMagicHash);
-       }
-
-       public Dimension getDimensions() {
-               return dimensions;
-       };
-
-       /**
-        * Get thumbnail file name for this image and desired thumbnail dimensions
-        * relative to designated thumbnails directory within parent directory.
-        */
-       public String getRelativeThumbnailFileName(final Dimension targetDimension) {
-               return FilePathParser.getFileNameWithoutExtension(fileName) + " ("
-                               + computeThumbnailHash(targetDimension) + ").jpeg";
-       }
-
-       @Override
-       public void updateFileMetainfo(final File parentDirectory)
-                       throws IOException {
-               BufferedImage image;
-               try {
-                       image = Utils.getBufferedImage(getFile(parentDirectory));
-               } catch (final ImageFormatError e) {
-                       throw new RuntimeException(e);
-               }
-               dimensions = new Dimension(image.getWidth(), image.getHeight());
+       private static void ensureNonzeroImageArea(final java.awt.Dimension result) {
+               if (result.width < 1)
+                       result.width = 1;
+               if (result.height < 1)
+                       result.height = 1;
        }
 
        /**
@@ -105,6 +62,8 @@ public class Picture extends AbstractFile {
                                        desiredTargetDimension.width,
                                        (int) (desiredTargetDimension.width / inputImageWidthToHeightRatio));
 
+                       ensureNonzeroImageArea(result);
+
                        return result;
                } else {
 
@@ -112,6 +71,8 @@ public class Picture extends AbstractFile {
                                        (int) (desiredTargetDimension.height * inputImageWidthToHeightRatio),
                                        desiredTargetDimension.height);
 
+                       ensureNonzeroImageArea(result);
+
                        return result;
                }
        }
@@ -123,7 +84,7 @@ public class Picture extends AbstractFile {
                final ArrayList<Dimension> result = new ArrayList<Dimension>();
                result.add(current);
 
-               while (current.getArea() > 100000) {
+               while (current.getArea() > 1000000) {
                        current = current.getScaled(0.5d);
                        result.add(current);
                }
@@ -174,16 +135,58 @@ 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();
+       };
+
+       /**
+        * Picture dimensions.
+        */
+       private Dimension dimensions;
+
+       public Picture(final File parentDirectory, final String fileName)
+                       throws Exception {
+               super(parentDirectory, fileName);
+       }
+
+       private String computeThumbnailHash(final Dimension targetDimension) {
+
+               // compute new thumbnails hash number
+               final String forMagicHash = fileName + " "
+                               + String.valueOf(targetDimension.width) + " "
+                               + String.valueOf(targetDimension.height) + " "
+                               + String.valueOf(getFileLength()) + " "
+                               + Constants.THUMBNAIL_VERSION;
+
+               // System.out.println("Computing hash from: " + forMagicHash);
+               return Utils.getStringCrcAsHex(forMagicHash);
+       }
+
+       public Dimension getDimensions() {
+               return dimensions;
+       }
+
+       /**
+        * Get thumbnail file name for this image and desired thumbnail dimensions
+        * relative to designated thumbnails directory within parent directory.
+        */
+       public String getRelativeThumbnailFileName(final Dimension targetDimension) {
+               return FilePathParser.getFileNameWithoutExtension(fileName) + " ("
+                               + computeThumbnailHash(targetDimension) + ").jpeg";
+       }
+
+       @Override
+       public void updateFileMetainfo(final File parentDirectory) throws Exception {
+               final BufferedImage image = Utils
+                               .getBufferedImage(getFile(parentDirectory));
+               dimensions = new Dimension(image.getWidth(), image.getHeight());
        }
 
 }