Index: /other-projects/nz-flag-design/trunk/similarity-2d/flag-aspect-ratios-json.jsp
===================================================================
--- /other-projects/nz-flag-design/trunk/similarity-2d/flag-aspect-ratios-json.jsp	(revision 29783)
+++ /other-projects/nz-flag-design/trunk/similarity-2d/flag-aspect-ratios-json.jsp	(revision 29783)
@@ -0,0 +1,118 @@
+<%@ page contentType="text/html; charset=UTF-8" %>
+  <%@ page import="java.io.*,java.util.*,java.awt.image.*,javax.imageio.*" %>
+  <%@include file="flag-functions.jsp" %>
+  <%
+      String docBase = getServletContext().getRealPath("/");
+      File similarityDir = new File(docBase,"similarity-2d");
+
+      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(imagesStr);
+
+      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) {
+	      
+	      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+49)/50;
+		  int rough_height = (gcd_height+49)/50;
+		  
+		  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>());
+		  ratioList.get(ratioStr).add(fileEntry);
+	      }
+	      else {
+		  ratioFreq.put(ratioStr,ratioFreq.get(ratioStr)+1);
+		  ratioList.get(ratioStr).add(fileEntry);
+	      }	      
+	  }	      
+	  try {
+	      
+	      FileWriter foutFileWriter = new FileWriter(aspectRatioFile);
+	      PrintWriter fout = new PrintWriter(foutFileWriter);
+              
+	      fout.println("{");
+	      
+	      Map<String, Integer> sortedRatioFreq = sortByComparator(ratioFreq);
+	      int c = 0;
+	      
+	      for (Map.Entry<String, Integer> entry : sortedRatioFreq.entrySet()) {
+		  
+		  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("}");
+
+	      fout.close();
+
+
+	  }
+	  catch (IOException e) {
+	      e.printStackTrace();
+	  }	  
+	  
+      }
+      try {
+	  FileReader fr = new FileReader(aspectRatioFile);
+	  BufferedReader bfr = new BufferedReader(fr);
+
+	  String line;
+	  while ( ( line = bfr.readLine( ) ) != null ) {
+	      out.print(line);
+	  }
+	  bfr.close();
+      }
+      catch (IOException e) {
+	  e.printStackTrace();
+      }
+   %>
