Author: jahewson
Date: Fri May 30 05:03:13 2014
New Revision: 1598457

URL: http://svn.apache.org/r1598457
Log:
PDFBOX-2101: Make ExtractImages dump only unique images

Modified:
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java

Modified: 
pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java?rev=1598457&r1=1598456&r2=1598457&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java 
(original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractImages.java 
Fri May 30 05:03:13 2014
@@ -23,7 +23,10 @@ import java.io.IOException;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.HashSet;
 
+import org.apache.pdfbox.cos.COSStream;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.pdfbox.pdmodel.PDResources;
@@ -46,6 +49,7 @@ import org.apache.pdfbox.util.ImageIOUti
 public class ExtractImages
 {
     private int imageCounter = 1;
+    private Set<COSStream> seen = new HashSet<COSStream>();
 
     private static final String PASSWORD = "-password";
     private static final String PREFIX = "-prefix";
@@ -192,16 +196,25 @@ public class ExtractImages
                 // write the images
                 if (xobject instanceof PDImageXObject)
                 {
+                    if (seen.contains(xobject.getCOSStream()))
+                    {
+                        // skip duplicate image
+                        continue;
+                    }
+                    seen.add(xobject.getCOSStream());
+
                     PDImageXObject image = (PDImageXObject)xobject;
                     String name = null;
                     if (addKey) 
                     {
-                        name = getUniqueFileName( prefix + "_" + key, 
image.getSuffix() );
+                        name = prefix + "-" + imageCounter + "_" + key;
                     }
                     else 
                     {
-                        name = getUniqueFileName( prefix, image.getSuffix() );
+                        name = prefix + "-" + imageCounter;
                     }
+                    imageCounter++;
+
                     System.out.println( "Writing image:" + name );
                     write2file( image, name );
                 }
@@ -252,19 +265,6 @@ public class ExtractImages
         }
     }
 
-    private String getUniqueFileName( String prefix, String suffix )
-    {
-        String uniqueName = null;
-        File f = null;
-        while( f == null || f.exists() )
-        {
-            uniqueName = prefix + "-" + imageCounter;
-            f = new File( uniqueName + "." + suffix );
-            imageCounter++;
-        }
-        return uniqueName;
-    }
-
     /**
      * This will print the usage requirements and exit.
      */


Reply via email to