Ignore:
Timestamp:
2015-03-13T10:25:17+13:00 (9 years ago)
Author:
davidb
Message:

Changes resulting from developing the aspect-ration JSON JSP script

Location:
other-projects/nz-flag-design/trunk/similarity-2d
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • other-projects/nz-flag-design/trunk/similarity-2d/display-flags.jsp

    r29766 r29784  
    4545      <script>
    4646    var is_jsp_active = false;
    47     var img_list = [ "Images/nz.gif" ];
     47    var img_list = [ "import/nz.gif" ];
    4848      </script>
    4949
  • other-projects/nz-flag-design/trunk/similarity-2d/flag-functions.jsp

    r29621 r29784  
    1 
    21  <%@ page import="java.io.*,java.util.*,java.awt.image.*,javax.imageio.*" %>
    3 
    42  <%!
    53    public int GCD(int a, int b) {
     
    3836    }
    3937
    40 
    41 
    4238    public static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap)
    4339    {
     
    7369    }
    7470 */
    75 
    76 
    7771   %>
    78 
  • other-projects/nz-flag-design/trunk/similarity-2d/index.jsp

    r29765 r29784  
    1414    // Set up a fallback position in case this page is accessed outside of a JSP context
    1515    // (i.e., the JSP blocks aren't executed)
    16     var img_list = [ "Images/nz.gif" ];
     16    var img_list = [ "import/nz.gif" ];
    1717  </script>
    1818
     
    8585      String docBase = getServletContext().getRealPath("/");
    8686      File similarityDir = new File(docBase,"similarity-2d");
    87       File imagesDir = new File(similarityDir,"Images");
     87
     88      String imagesStr = (request.getParameter("imagesDir")!=null) ? request.getParameter("imagesDir") : "import";
     89      File imagesDir = new File(similarityDir,imagesStr);
     90
     91      File aspectRatioFile = new File(similarityDir,"flag-aspect-ratio.json");
    8892
    8993      ArrayList<File> imageFileList = listFilesForFolder(imagesDir,".gif");
    9094
    91       File imageUrlBase = new File("Images");
     95      File imageUrlBase = new File(imagesStr);
    9296
    9397      String action = request.getParameter("action");
     
    9599      if ((action != null) && action.equals("ratios")) {
    96100
    97           Map<String, Integer> ratioFreq = new HashMap<String, Integer>();
    98           Map<String, ArrayList<File>> ratioList = new HashMap<String, ArrayList<File>>();
    99 
    100           for (final File fileEntry : imageFileList) {
    101 
    102             //out.println("flag " + fileEntry.getName());
    103 
    104             BufferedImage bimg = ImageIO.read(fileEntry);
    105         int width          = bimg.getWidth();
    106         int height         = bimg.getHeight();
    107         int gcd            = GCD(width,height);
    108 
    109             int gcd_width      = width  / gcd;
    110             int gcd_height     = height / gcd;
    111         String ratioStr    = gcd_width + ":" + gcd_height;
    112 
    113         if (!ratioFreq.containsKey(ratioStr)) {
    114           ratioFreq.put(ratioStr,1);
    115           ratioList.put(ratioStr,new ArrayList<File>());
    116         }
    117         else {
    118           ratioFreq.put(ratioStr,ratioFreq.get(ratioStr)+1);
    119           ratioList.get(ratioStr).add(fileEntry);
    120         }
    121 
    122         //double ratio       = (double)width/(double)height;
     101          if (!aspectRatioFile.exists()) {
     102          Map<String, Integer> ratioFreq = new HashMap<String, Integer>();
     103          Map<String, ArrayList<File>> ratioList = new HashMap<String, ArrayList<File>>();
     104
     105          for (final File fileEntry : imageFileList) {
     106         
     107          //out.println("flag " + fileEntry.getName());
     108
     109          BufferedImage bimg = ImageIO.read(fileEntry);
     110          int width          = bimg.getWidth();
     111          int height         = bimg.getHeight();
     112          int gcd            = GCD(width,height);
     113         
     114          int gcd_width      = width  / gcd;
     115          int gcd_height     = height / gcd;
     116
     117          if ((gcd_width>50) || (gcd_height>50)) {
     118              int rough_width  = (gcd_width+99)/100;
     119              int rough_height = (gcd_height+99)/100;
     120             
     121              int rough_gcd            = GCD(rough_width,rough_height);
     122             
     123              gcd_width      = rough_width  / rough_gcd;
     124              gcd_height     = rough_height / rough_gcd;
     125          }
     126
     127          String ratioStr    = gcd_width + ":" + gcd_height;
     128         
     129          if (!ratioFreq.containsKey(ratioStr)) {
     130              ratioFreq.put(ratioStr,1);
     131              ratioList.put(ratioStr,new ArrayList<File>());
     132          }
     133          else {
     134              ratioFreq.put(ratioStr,ratioFreq.get(ratioStr)+1);
     135              ratioList.get(ratioStr).add(fileEntry);
     136          }
     137         
     138          //double ratio       = (double)width/(double)height;
     139         
     140          //out.println(width + " x " + height + " ratio: " + ratioStr);
     141          //out.println("<br/>");
     142
     143
     144          }
     145         
     146          try {
     147
     148          FileWriter foutFileWriter = new FileWriter(aspectRatioFile);
     149          PrintWriter fout = new PrintWriter(foutFileWriter);
     150             
     151          //out.println("<script>");
     152          //out.print("var flagsRatioFreq = ");
     153          fout.println("{");
     154         
     155          Map<String, Integer> sortedRatioFreq = sortByComparator(ratioFreq);
     156          //printMap(sortedRatioFreq);
     157          int c = 0;
     158         
     159          for (Map.Entry<String, Integer> entry : sortedRatioFreq.entrySet()) {
     160              //out.println("[Key] : " + entry.getKey()
     161              //           + " [Value] : " + entry.getValue() + "<br />");
    123162   
    124         //out.println(width + " x " + height + " ratio: " + ratioStr);
    125         //out.println("<br/>");
    126 
    127 
    128           }
    129 
    130       Map<String, Integer> sortedRatioFreq = sortByComparator(ratioFreq);
    131       //printMap(sortedRatioFreq);
    132           for (Map.Entry<String, Integer> entry : sortedRatioFreq.entrySet()) {
    133           out.println("[Key] : " + entry.getKey()
    134                          + " [Value] : " + entry.getValue() + "<br />");
    135           }
     163              String ratio_key  = entry.getKey();
     164              Integer ratio_freq = entry.getValue();
     165
     166              if (ratio_freq>1) {
     167              if (c>0) {
     168                  fout.print(", ");
     169              }
     170              fout.println("\"" + ratio_key + "\" : ");
     171              fout.println("  { \"freq\"  : " + entry.getValue());
     172
     173              ArrayList<File> flag_list = ratioList.get(ratio_key);
     174              int flag_list_len = flag_list.size();
     175
     176              fout.print("   ,\"flags\" : [");
     177              for (int i=0; i<flag_list_len; i++) {
     178                  if (i>0) {
     179                  fout.print(", ");
     180                  }
     181                 
     182                  String flag_file_str = imageUrlBase + "/" + flag_list.get(i).getName();
     183
     184                  fout.print("\""+flag_file_str+"\"");
     185              }
     186              fout.println("]");
     187              fout.println("  }");
     188
     189              c++;
     190              }
     191          }
     192          fout.println("}");
     193
     194          //out.print(";");
     195          //out.println("</script>");
     196          fout.close();
    136197
    137198/*
    138       // Print out frequency counts
    139       Iterator<String> keyIterator   = ratioFreq.keySet().iterator();
    140 
    141       while (keyIterator.hasNext()){
    142         String  key   = keyIterator.next();
    143         Integer freq  = ratioFreq.get(key);
    144         out.println(key + ": " + freq + "<br />");
     199          // Print out frequency counts
     200          Iterator<String> keyIterator   = ratioFreq.keySet().iterator();
     201
     202          while (keyIterator.hasNext()){
     203            String  key   = keyIterator.next();
     204            Integer freq  = ratioFreq.get(key);
     205            out.println(key + ": " + freq + "<br />");
     206          }
     207*/
     208
     209          }
     210          catch (IOException e) {
     211          e.printStackTrace();
     212          }
     213
    145214      }
    146 */
    147 
    148215      }
    149216      else {
Note: See TracChangeset for help on using the changeset viewer.