%@ page contentType="text/html; charset=UTF-8" %>
<%@ 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 listFilesForFolder(final File folder, String matchExt) {
ArrayList fileList = new ArrayList();
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 sortByComparator(Map unsortMap)
{
// Convert Map to List
List> list
= new LinkedList>(unsortMap.entrySet());
// Sort list with comparator, to compare the Map values
Collections.sort(list, new Comparator>() {
public int compare(Map.Entry o1,
Map.Entry o2) {
return (o2.getValue()).compareTo(o1.getValue());
}
});
// Convert sorted map back to a Map
Map sortedMap = new LinkedHashMap();
for (Iterator> it = list.iterator(); it.hasNext();) {
Map.Entry entry = it.next();
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
/*
public static void printMap(Map map)
{
for (Map.Entry entry : map.entrySet()) {
out.println("[Key] : " + entry.getKey()
+ " [Value] : " + entry.getValue() + "br />");
}
}
*/
%>
<%
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 imageFileList = listFilesForFolder(imagesDir,".gif");
File imageUrlBase = new File(imagesStr);
String action = request.getParameter("action");
if ((action != null) && action.equals("ratios")) {
if (!aspectRatioFile.exists()) {
Map ratioFreq = new HashMap();
Map> ratioList = new HashMap>();
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());
}
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("
");
}
try {
FileWriter foutFileWriter = new FileWriter(aspectRatioFile);
PrintWriter fout = new PrintWriter(foutFileWriter);
//out.println("");
fout.close();
/*
// Print out frequency counts
Iterator keyIterator = ratioFreq.keySet().iterator();
while (keyIterator.hasNext()){
String key = keyIterator.next();
Integer freq = ratioFreq.get(key);
out.println(key + ": " + freq + "
");
}
*/
}
catch (IOException e) {
e.printStackTrace();
}
}
}
else {
out.println("");
}
%>