Index: other-projects/nz-flag-design/trunk/similarity-2d/index.jsp
===================================================================
--- other-projects/nz-flag-design/trunk/similarity-2d/index.jsp	(revision 29533)
+++ other-projects/nz-flag-design/trunk/similarity-2d/index.jsp	(revision 29533)
@@ -0,0 +1,149 @@
+<html>
+  <head>
+  </head>
+  <%@ page import="java.io.*,java.util.*,java.awt.image.*,javax.imageio.*" %>
+
+  <%!
+    public int GCD(int a, int b) {
+       if (b==0) return a;
+       return GCD(b,a%b);
+    }
+
+    ArrayList<File> listFilesForFolder(final File folder, String matchExt) {
+        ArrayList<File> fileList = new ArrayList<File>();
+
+        for (final File fileEntry : folder.listFiles()) {
+            if (fileEntry.isDirectory()) {
+                listFilesForFolder(fileEntry,matchExt);
+            } else {
+	         if (fileEntry.getName().toLowerCase().endsWith(matchExt)) {
+  	            fileList.add(fileEntry);
+		 }
+            }
+        }
+
+	return fileList;
+    }
+
+
+
+    public static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap) 
+    {
+ 
+	// Convert Map to List
+	List<Map.Entry<String, Integer>> list 
+	  = new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());
+ 
+	// Sort list with comparator, to compare the Map values
+	Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
+	      public int compare(Map.Entry<String, Integer> o1,
+                                 Map.Entry<String, Integer> o2) {
+	     	  return (o2.getValue()).compareTo(o1.getValue());
+	      }
+	});
+ 
+	// Convert sorted map back to a Map
+	Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
+	for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext();) {
+	    Map.Entry<String, Integer> entry = it.next();
+	    sortedMap.put(entry.getKey(), entry.getValue());
+	}
+												
+	return sortedMap;							
+    }
+/* 
+    public static void printMap(Map<String, Integer> map) 
+    {
+        for (Map.Entry<String, Integer> entry : map.entrySet()) {
+	    out.println("[Key] : " + entry.getKey() 
+                         + " [Value] : " + entry.getValue() + "br />");
+        }
+    }
+ */
+
+
+   %>
+
+  <%
+
+      String docBase = getServletContext().getRealPath("/");
+      File similarityDir = new File(docBase,"similarity-2d");
+      File imagesDir = new File(similarityDir,"Images");
+
+      ArrayList<File> imageFileList = listFilesForFolder(imagesDir,".gif");
+
+      File imageUrlBase = new File("Images");
+
+      String action = request.getParameter("action");
+
+      if (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;
+	
+	    //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 />");
+          }
+
+/*
+	  // 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 />");
+	  }
+*/
+
+      }
+      else {
+ 
+          for (final File fileEntry : imageFileList) {
+            File flagImageUrl = new File(imageUrlBase,fileEntry.getName());
+
+            out.println("<img src=\"" + flagImageUrl.getPath() + "\">");
+
+          }
+    }
+
+
+   %>
+
+  <body>
+  </body>
+
+</html>
