Index: /other-projects/nz-flag-design/trunk/similarity-2d/display-flags.jsp
===================================================================
--- /other-projects/nz-flag-design/trunk/similarity-2d/display-flags.jsp	(revision 29783)
+++ /other-projects/nz-flag-design/trunk/similarity-2d/display-flags.jsp	(revision 29784)
@@ -45,5 +45,5 @@
       <script>
 	var is_jsp_active = false;
-	var img_list = [ "Images/nz.gif" ];
+	var img_list = [ "import/nz.gif" ];
       </script>
 
Index: /other-projects/nz-flag-design/trunk/similarity-2d/flag-functions.jsp
===================================================================
--- /other-projects/nz-flag-design/trunk/similarity-2d/flag-functions.jsp	(revision 29783)
+++ /other-projects/nz-flag-design/trunk/similarity-2d/flag-functions.jsp	(revision 29784)
@@ -1,5 +1,3 @@
-
   <%@ page import="java.io.*,java.util.*,java.awt.image.*,javax.imageio.*" %>
-
   <%!
     public int GCD(int a, int b) {
@@ -38,6 +36,4 @@
     }
 
-
-
     public static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap) 
     {
@@ -73,6 +69,3 @@
     }
  */
-
-
    %>
-
Index: /other-projects/nz-flag-design/trunk/similarity-2d/index.jsp
===================================================================
--- /other-projects/nz-flag-design/trunk/similarity-2d/index.jsp	(revision 29783)
+++ /other-projects/nz-flag-design/trunk/similarity-2d/index.jsp	(revision 29784)
@@ -14,5 +14,5 @@
     // Set up a fallback position in case this page is accessed outside of a JSP context
     // (i.e., the JSP blocks aren't executed)
-    var img_list = [ "Images/nz.gif" ];
+    var img_list = [ "import/nz.gif" ];
   </script>
 
@@ -85,9 +85,13 @@
       String docBase = getServletContext().getRealPath("/");
       File similarityDir = new File(docBase,"similarity-2d");
-      File imagesDir = new File(similarityDir,"Images");
+
+      String imagesStr = (request.getParameter("imagesDir")!=null) ? request.getParameter("imagesDir") : "import";
+      File imagesDir = new File(similarityDir,imagesStr);
+
+      File aspectRatioFile = new File(similarityDir,"flag-aspect-ratio.json");
 
       ArrayList<File> imageFileList = listFilesForFolder(imagesDir,".gif");
 
-      File imageUrlBase = new File("Images");
+      File imageUrlBase = new File(imagesStr);
 
       String action = request.getParameter("action");
@@ -95,55 +99,118 @@
       if ((action != null) && action.equals("ratios")) {
 
-          Map<String, Integer> ratioFreq = new HashMap<String, Integer>();
-          Map<String, ArrayList<File>> ratioList = new HashMap<String, ArrayList<File>>();
-
-          for (final File fileEntry : imageFileList) {
-
-            //out.println("flag " + fileEntry.getName());
-
-    	    BufferedImage bimg = ImageIO.read(fileEntry);
-	    int width          = bimg.getWidth();
-	    int height         = bimg.getHeight();
-	    int gcd            = GCD(width,height);
-
-            int gcd_width      = width  / gcd;
-            int gcd_height     = height / gcd;
-	    String ratioStr    = gcd_width + ":" + gcd_height;
-
-	    if (!ratioFreq.containsKey(ratioStr)) {
-	      ratioFreq.put(ratioStr,1);
-	      ratioList.put(ratioStr,new ArrayList<File>());
-	    }
-	    else {
-	      ratioFreq.put(ratioStr,ratioFreq.get(ratioStr)+1);
-	      ratioList.get(ratioStr).add(fileEntry);
-	    }
-
-	    //double ratio       = (double)width/(double)height;
+          if (!aspectRatioFile.exists()) {
+	      Map<String, Integer> ratioFreq = new HashMap<String, Integer>();
+	      Map<String, ArrayList<File>> ratioList = new HashMap<String, ArrayList<File>>();
+
+	      for (final File fileEntry : imageFileList) {
+		  
+		  //out.println("flag " + fileEntry.getName());
+
+		  BufferedImage bimg = ImageIO.read(fileEntry);
+		  int width          = bimg.getWidth();
+		  int height         = bimg.getHeight();
+		  int gcd            = GCD(width,height);
+		  
+		  int gcd_width      = width  / gcd;
+		  int gcd_height     = height / gcd;
+
+		  if ((gcd_width>50) || (gcd_height>50)) {
+		      int rough_width  = (gcd_width+99)/100;
+		      int rough_height = (gcd_height+99)/100;
+		      
+		      int rough_gcd            = GCD(rough_width,rough_height);
+		      
+		      gcd_width      = rough_width  / rough_gcd;
+		      gcd_height     = rough_height / rough_gcd;
+		  }
+
+		  String ratioStr    = gcd_width + ":" + gcd_height;
+		  
+		  if (!ratioFreq.containsKey(ratioStr)) {
+		      ratioFreq.put(ratioStr,1);
+		      ratioList.put(ratioStr,new ArrayList<File>());
+		  }
+		  else {
+		      ratioFreq.put(ratioStr,ratioFreq.get(ratioStr)+1);
+		      ratioList.get(ratioStr).add(fileEntry);
+		  }
+		  
+		  //double ratio       = (double)width/(double)height;
+		  
+		  //out.println(width + " x " + height + " ratio: " + ratioStr);
+		  //out.println("<br/>");
+
+
+	      }
+	      
+	      try {
+
+		  FileWriter foutFileWriter = new FileWriter(aspectRatioFile);
+		  PrintWriter fout = new PrintWriter(foutFileWriter);
+              
+		  //out.println("<script>");
+		  //out.print("var flagsRatioFreq = ");
+		  fout.println("{");
+	      
+		  Map<String, Integer> sortedRatioFreq = sortByComparator(ratioFreq);
+		  //printMap(sortedRatioFreq);
+		  int c = 0;
+		  
+		  for (Map.Entry<String, Integer> entry : sortedRatioFreq.entrySet()) {
+		      //out.println("[Key] : " + entry.getKey() 
+		      //           + " [Value] : " + entry.getValue() + "<br />");
 	
-	    //out.println(width + " x " + height + " ratio: " + ratioStr);
-	    //out.println("<br/>");
-
-
-          }
-
-	  Map<String, Integer> sortedRatioFreq = sortByComparator(ratioFreq);
-	  //printMap(sortedRatioFreq);
-          for (Map.Entry<String, Integer> entry : sortedRatioFreq.entrySet()) {
-	      out.println("[Key] : " + entry.getKey() 
-                         + " [Value] : " + entry.getValue() + "<br />");
-          }
+		      String ratio_key  = entry.getKey();
+		      Integer ratio_freq = entry.getValue();
+
+		      if (ratio_freq>1) {
+			  if (c>0) {
+			      fout.print(", ");
+			  }
+			  fout.println("\"" + ratio_key + "\" : ");
+			  fout.println("  { \"freq\"  : " + entry.getValue());
+
+			  ArrayList<File> flag_list = ratioList.get(ratio_key);
+			  int flag_list_len = flag_list.size();
+
+			  fout.print("   ,\"flags\" : [");
+			  for (int i=0; i<flag_list_len; i++) {
+			      if (i>0) {
+				  fout.print(", ");
+			      }
+			      
+			      String flag_file_str = imageUrlBase + "/" + flag_list.get(i).getName();
+
+			      fout.print("\""+flag_file_str+"\"");
+			  }
+			  fout.println("]");
+			  fout.println("  }");
+
+			  c++;
+		      }
+		  }
+		  fout.println("}");
+
+		  //out.print(";");
+		  //out.println("</script>");
+		  fout.close();
 
 /*
-	  // Print out frequency counts
-	  Iterator<String> keyIterator   = ratioFreq.keySet().iterator();
-
-	  while (keyIterator.hasNext()){
-	    String  key   = keyIterator.next();
-	    Integer freq  = ratioFreq.get(key);
-	    out.println(key + ": " + freq + "<br />");
+  	      // Print out frequency counts
+	      Iterator<String> keyIterator   = ratioFreq.keySet().iterator();
+
+	      while (keyIterator.hasNext()){
+	        String  key   = keyIterator.next();
+	        Integer freq  = ratioFreq.get(key);
+	        out.println(key + ": " + freq + "<br />");
+	      }
+*/
+
+	      }
+	      catch (IOException e) {
+		  e.printStackTrace();
+	      }
+
 	  }
-*/
-
       }
       else {
