package de.mpi.rgblab.apps;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import de.mpi.rgblab.color.ColorModel;
import de.mpi.rgblab.color.ColorTransformer;
import de.mpi.rgblab.color.ColorType;
import de.mpi.rgblab.data.ColorReader;
import de.mpi.rgblab.data.Data;
import de.mpi.rgblab.data.DataComparator;
import de.mpi.rgblab.data.DataReader;
import de.mpi.rgblab.gui.map.GuiMap;
import de.mpi.rgblab.gui.menu.MenuControl;
import de.mpi.rgblab.gui.palette.HCColorPanel;
import de.mpi.rgblab.gui.palette.LCHColorPanel;
import de.mpi.rgblab.math.numeric.Numeric;
import de.mpi.rgblab.utils.ImageResizer;
public class GuiApp extends MouseAdapter {
protected static JColorChooser tcc;
protected static JLabel banner;
public final static String dataFile = "usa_houshold_and_income_screen_coordinates.dat";
public final static String inputColors = "color_brewer2.dat";
public final static String vis_sal_Palette = "sal_vis_palette.dat";
/**
* @param args
*/
public static void main(String[] args) {
// computeSaliency(args);
// createAndShowGUI("usa_houshold_and_income_screen_coordinates_sorted.dat",
// "color.dat");
startGUIApp();
}
public static void startGUIApp() {
// user interface
Data data = new Data();
JFrame frame = new JFrame("Map");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// GraphicsEnvironment.getLocalGraphicsEnvironment()
// .getDefaultScreenDevice().setFullScreenWindow(frame);
GuiMap map = new GuiMap(data); // , vis_sal_Palette, inputColors );
MenuControl colorPicker = new MenuControl(map); // , vis_sal_Palette,
// inputColors );
frame.add(map, BorderLayout.CENTER);
frame.add(colorPicker, BorderLayout.PAGE_END);
// add(tcc, BorderLayout.EAST);
// frame.setUndecorated(true);
// Display the window.
frame.pack();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
//frame.setBackground(Color.BLACK);
frame.setVisible(true);
}
// public static void computeSaliency(String[] args) {
// if (args.length != 3) {
// System.out.println("three arguments: radius, computing type (1 indicates euclidian distance, 0 indicates z_score) and output file");
// return;
// }
// double time = System.currentTimeMillis();
// // try {
// // transform("usa_houshold_and_income_distribution.dat",
// // "usa_houshold_and_income_screen_coordinates.dat");
// // // sort("usa_houshold_and_income_screen_coordinates.dat",
// // // "usa_houshold_and_income_screen_coordinates_sorted.dat");
// // } catch (IOException e) {
// // e.printStackTrace();
// // System.out.println(e);
// // }
//
// // A = not using the sort file
// int r = Integer.valueOf(args[0]).intValue();
// int z = Integer.valueOf(args[1]).intValue();
//
// computeSaliency(r,
// "usa_houshold_and_income_screen_coordinates_sorted.dat",
// "color.dat", args[2], z);
//
// time = System.currentTimeMillis() - time;
// time /= 1000;
// System.out.println("seconds: " + time);
// }
// transform into screen coordinates, eliminate the duplicates and write the
// result into a file
private static void transform(String inputFile, String outputFile)
throws IOException {
// read the inputfile
Vector<Vector<Double>> inputData = new Vector<Vector<Double>>();
Vector<Integer> class_label = new Vector<Integer>();
DataReader fileReader = new DataReader(inputFile);
fileReader.get_data(inputData);
fileReader.get_class_label(class_label);
// transform into screen coordinates
Dimension windowSize = Toolkit.getDefaultToolkit().getScreenSize();
Vector<Vector<Integer>> scaled_pixelsMatrix = new Vector<Vector<Integer>>();
ImageResizer.scale_data_2_screen_res(inputData, scaled_pixelsMatrix,
windowSize.width, windowSize.height);
// eliminate the duplicates and write the result into a file
// Vector<Vector<Integer>> outputMatrix = null;
// Vector<Integer> outputColor = null;
eliminatePoints(scaled_pixelsMatrix, class_label, outputFile);
}
/**
*
* @param inputFile
* @param outputFile
* @throws IOException
*/
private static void sort(String inputFile, String outputFile)
throws IOException {
// read the input files
Comparator comp = new DataComparator();
Vector<Vector<Double>> inputData = new Vector<Vector<Double>>();
Vector<Integer> class_label = new Vector<Integer>();
DataReader fileReader = new DataReader(inputFile);
// "usa_houshold_and_income_distribution.dat");
fileReader.get_data(inputData);
fileReader.get_class_label(class_label);
for (int i = 0; i < inputData.size(); i++) {
Vector<Double> tmp = inputData.get(i);
tmp.add((Double) class_label.get(i).doubleValue());
inputData.set(i, tmp);
}
// convert vector to array
Vector[] inputArray = new Vector[inputData.size()];
inputData.copyInto(inputArray);
// sort
Arrays.sort(inputArray, comp);
// create output file
File file = new File(outputFile); // "usa_houshold_and_income_distribution_sorted.dat");
FileWriter fw = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fw);
// write to the output file
for (int i = 0; i < inputArray.length; i++) {
out.write(Double.toString((Double) inputArray[i].get(0)));
out.write('|');
out.write(Double.toString((Double) inputArray[i].get(1)));
out.write('|');
out.write(Integer.toString(((Double) inputArray[i].get(2))
.intValue()));
out.write(System.getProperty("line.separator"));
}
// flush and close
out.flush();
out.close();
}
/**
* start the application
*
* @param radius
* @param inputFile
* @param colorsFile
* @param outputFile
*/
private static void test(double radius, String inputFile,
String colorsFile, String outputFile) {
Vector<Vector<Double>> inputData = new Vector<Vector<Double>>();
Vector<Integer> class_label = new Vector<Integer>();
Vector<ColorModel> rgbColors = new Vector<ColorModel>();
// read the input data
DataReader fileReader = new DataReader(inputFile);
fileReader.get_data(inputData);
fileReader.get_class_label(class_label);
rgbColors = ColorReader.getColorsAsColorModel(colorsFile);
// copy the the vector to an array
ColorModel[] my_rgbColor = new ColorModel[rgbColors.size()];
rgbColors.copyInto(my_rgbColor);
// transfort to lab space
ColorModel labColor[] = ColorTransformer.rgb2lab(my_rgbColor);
// array to vector
Vector<ColorModel> my_labColor = new Vector<ColorModel>();
for (int i = 0; i < labColor.length; i++) {
my_labColor.addElement(labColor[i]);
}
int my_count1 = 0;
int count2 = 0;
int countIterator2 = 0;
// tmp variables
Iterator it_map_matrix1 = inputData.iterator();
// Iterator it_map_matrix2;
// Iterator it_tmp = it_map_matrix1;
double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
Vector<Double> row1, row2;
int classLabel = 0;
ColorModel tmp;
BufferedWriter out;
FileWriter fw;
ColorModel rgbWhite = new ColorModel(255, 255, 255, ColorType.RGB);
ColorModel labWhite = ColorTransformer.rgb2lab(rgbWhite);
System.out.println("START");
try {
File file = new File(outputFile);
fw = new FileWriter(file);
out = new BufferedWriter(fw);
Vector<ColorModel> nbh = new Vector<ColorModel>();
while (it_map_matrix1.hasNext()) {
row1 = (Vector<Double>) it_map_matrix1.next();
x1 = row1.elementAt(0);
y1 = row1.elementAt(1);
classLabel = 0;
count2 = countIterator2;
if (count2 == my_count1)
count2++;
while (count2 < inputData.size()) {
if (count2 != my_count1) {
row2 = (Vector<Double>) inputData.get(count2);
x2 = row2.elementAt(0);
y2 = row2.elementAt(1);
if ((x2 - x1) > radius)
break;
else if (x1 - x2 > radius) {
countIterator2++;
} else {
if ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) < Math
.pow(radius, 2)) {
classLabel = class_label.elementAt(count2);
tmp = (ColorModel) my_labColor
.elementAt(classLabel);
if (!nbh.contains(tmp))
nbh.addElement(tmp);
}
}
}
count2++;
}
classLabel = class_label.elementAt(my_count1);
tmp = (ColorModel) my_labColor.elementAt(classLabel);
nbh.add(labWhite);
// compute z score
double z = Numeric.compute_z_score(tmp, nbh);
// compute euclidian distance
// double z = euclDistance(tmp, nbh);
nbh.clear();
// whide color
// System.out.print(x1);
// System.out.print('|');
// System.out.print('|');
// System.out.print(y1);
// System.out.println(z);
out.write(Double.toString(x1));
out.write('|');
out.write(Double.toString(y1));
out.write('|');
out.write(Double.toString(z));
out.write(System.getProperty("line.separator"));
my_count1++;
// out.flush();
// if ((my_count1 % 1000) == 0)
// out.flush();
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void test2(double radius, String inputFile,
String colorsFile, String outputFile) {
Vector<Vector<Double>> inputData = new Vector<Vector<Double>>();
Vector<Integer> class_label = new Vector<Integer>();
Vector<ColorModel> rgbColors = new Vector<ColorModel>();
// read the input data
DataReader fileReader = new DataReader(inputFile);
fileReader.get_data(inputData);
fileReader.get_class_label(class_label);
rgbColors = ColorReader.getColorsAsColorModel(colorsFile);
// copy the the vector to an array
ColorModel[] my_rgbColor = new ColorModel[rgbColors.size()];
rgbColors.copyInto(my_rgbColor);
// transfort to lab space
ColorModel labColor[] = ColorTransformer.rgb2lab(my_rgbColor);
// array to vector
Vector<ColorModel> my_labColor = new Vector<ColorModel>();
for (int i = 0; i < labColor.length; i++) {
my_labColor.addElement(labColor[i]);
}
int my_count1 = 0;
int count2 = 0;
int countIterator2 = 0;
// tmp variables
Iterator it_map_matrix1 = inputData.iterator();
// Iterator it_map_matrix2;
// Iterator it_tmp = it_map_matrix1;
double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
Vector<Double> row1, row2;
int classLabel = 0;
ColorModel tmp;
BufferedWriter out;
FileWriter fw;
ColorModel rgbWhite = new ColorModel(255, 255, 255, ColorType.RGB);
ColorModel labWhite = ColorTransformer.rgb2lab(rgbWhite);
System.out.println("START");
try {
File file = new File(outputFile);
fw = new FileWriter(file);
out = new BufferedWriter(fw);
Vector<ColorModel> nbh = new Vector<ColorModel>();
boolean[] isAdded = new boolean[rgbColors.size()];
for (int j = 0; j < isAdded.length; j++)
isAdded[j] = false;
while (it_map_matrix1.hasNext()) {
row1 = (Vector<Double>) it_map_matrix1.next();
x1 = row1.elementAt(0);
y1 = row1.elementAt(1);
count2 = countIterator2;
if (count2 == my_count1)
count2++;
while (count2 < inputData.size()) {
row2 = (Vector<Double>) inputData.get(count2);
if (count2 != my_count1) {
x2 = row2.elementAt(0);
y2 = row2.elementAt(1);
if ((x2 - x1) > radius)
break;
else if (x1 - x2 > radius) {
countIterator2++;
} else {
if ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) < Math
.pow(radius, 2)) {
classLabel = class_label.elementAt(count2);
// tmp = (ColorModel) my_labColor
// .elementAt(classLabel);
isAdded[classLabel] = true;
// if (!nbh.contains(tmp))
// nbh.addElement(tmp);
}
}
}
count2++;
}
nbh.add(labWhite);
for (int j = 0; j < isAdded.length; j++) {
if (isAdded[j]) {
tmp = (ColorModel) my_labColor.elementAt(j);
nbh.addElement(tmp);
isAdded[j] = false;
}
}
classLabel = class_label.elementAt(my_count1);
tmp = (ColorModel) my_labColor.elementAt(classLabel);
// compute z score
double z = Numeric.compute_z_score(tmp, nbh);
// compute euclidian distance
// double z = euclDistance(tmp, nbh);
// System.out.print(x1);
// System.out.print('|');
// System.out.print('|');
// System.out.print(y1);
// System.out.println(z);
out.write(Double.toString(x1));
out.write('|');
out.write(Double.toString(y1));
out.write('|');
out.write(Double.toString(z));
out.write(System.getProperty("line.separator"));
nbh.clear();
my_count1++;
// out.flush();
// if ((my_count1 % 1000) == 0)
// out.flush();
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @param inputFile
* @param colorsFile
*/
private static void createAndShowGUI(String inputFile, String colorsFile) {
Vector<Vector<Double>> inputData = new Vector<Vector<Double>>();
Vector<Integer> class_label = new Vector<Integer>();
//
Vector<ColorModel> rgbColors = new Vector<ColorModel>();
//
// init the map
// Add the map component
DataReader fileReader = new DataReader(inputFile);
fileReader.get_data(inputData);
fileReader.get_class_label(class_label);
//
rgbColors = ColorReader.getColorsAsColorModel(colorsFile);
//
// ColorModel[] my_rgbColor = new ColorModel[rgbColors.size()];
// for (int i = 0; i < rgbColors.size(); i++) {
//
// my_rgbColor[i] = rgbColors.elementAt(i);
//
// }
//
// ColorModel labColor[] = ColorTransformer.rgb2lab(my_rgbColor);
//
// Vector<ColorModel> my_labColor = new Vector<ColorModel>();
//
// for (int i = 0; i < labColor.length; i++) {
// my_labColor.addElement(labColor[i]);
// }
//
// int my_count1 = 0;
//
// int radius = 10;
//
// // Iterator it_class_label = class_label.iterator();
// Iterator it_map_matrix1 = inputData.iterator();
// // FileOutputStream fos;
// BufferedWriter out;
// FileWriter fw;
// // DataOutputStream out;
//
// try {
// File file = new File("z_score_results2.dat");
// // fos = new FileOutputStream(file);
// fw = new FileWriter(file);
// out = new BufferedWriter(fw);
// // out = new DataOutputStream(fos);
//
// while (it_map_matrix1.hasNext()) {
// Vector<Double> row1 = (Vector<Double>) it_map_matrix1.next();
// double x1 = row1.elementAt(0);
// double y1 = row1.elementAt(1);
//
// Iterator it_map_matrix2 = inputData.iterator();
// Vector<ColorModel> nbh = new Vector<ColorModel>();
//
// int classLabel = 0;
// ColorModel tmp;
//
// int count2 = 0;
// while (it_map_matrix2.hasNext()) {
// Vector<Double> row2 = (Vector<Double>) it_map_matrix2
// .next();
// if (count2 != my_count1) {
// double x2 = row2.elementAt(0);
// double y2 = row2.elementAt(1);
//
// classLabel = class_label.elementAt(count2);
// tmp = (ColorModel) my_labColor.elementAt(classLabel);
//
// if ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) < Math
// .pow(radius, 2)) {
// if (!nbh.contains(tmp))
// nbh.addElement(tmp);
// }
// }
// count2++;
// }
// classLabel = class_label.elementAt(my_count1);
// tmp = (ColorModel) my_labColor.elementAt(classLabel);
//
// double z = numeric.compute_z_score(tmp, nbh);
// // System.out.print(x1);
// // System.out.print('|');
// // System.out.print('|');
// // System.out.print(y1);
// // System.out.println(z);
//
// out.write(Double.toString(x1));
// out.write('|');
// out.write(Double.toString(y1));
// out.write('|');
// out.write(Double.toString(z));
// out.write(System.getProperty("line.separator"));
//
// my_count1++;
//
// if ((my_count1 % 500) == 0)
// out.flush();
// }
//
// out.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// Iterator it = my_labColor.iterator();
// while (it.hasNext()) {
//
// Vector<ColorModel> nbh = new Vector<ColorModel>();
//
// int tmp_count=0;
//
// Iterator it2 = my_labColor.iterator();
// while (it2.hasNext()) {
//
// ColorModel tmp = (ColorModel) it2.next();
//
// if(my_count != tmp_count) {
//
// nbh.addElement(tmp);
//
// }
//
//
//
// tmp_count++;
//
//
// }
//
// my_count++;
// ColorModel tmp = (ColorModel) it.next();
// double z = numeric.compute_z_score(tmp, nbh);
// System.out.print("Color: ");
// System.out.println(tmp);
// System.out.print("Neighbourhood: ");
// System.out.println(nbh);
// System.out.print("ZScore: ");
// System.out.println(z);
// }
// Create and set up the window.
JFrame frame = new JFrame("Map");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set up the banner at the top of the window
banner = new JLabel("Color Chooser Demo", JLabel.CENTER);
banner.setForeground(Color.yellow);
banner.setBackground(Color.blue);
banner.setOpaque(true);
banner.setFont(new Font("SansSerif", Font.BOLD, 24));
banner.setPreferredSize(new Dimension(100, 65));
JPanel bannerPanel = new JPanel(new BorderLayout());
bannerPanel.add(banner, BorderLayout.CENTER);
bannerPanel.setBorder(BorderFactory.createTitledBorder("Banner"));
// Set up color chooser for setting text colorobj
tcc = new JColorChooser();
/**
* Add lch color panel
*/
LCHColorPanel hclColor = new LCHColorPanel();
// hclColor.addFocusListener(new FocusListener(){
// @Override
// public void focusGained(FocusEvent e) {
// System.out.println(e.getSource());
// System.out.println("Focus gained");
//
// }
// @Override
// public void focusLost(FocusEvent e) {
// // TODO Auto-generated method stub
// System.out.println("Focus lost");
// }
//
// });
// tcc.addChooserPanel(hclColor);
AbstractColorChooserPanel panels[] = { new LCHColorPanel(),
new HCColorPanel() };
tcc.setChooserPanels(panels);
// tcc.getSelectionModel().addChangeListener(this);
tcc.setBorder(BorderFactory.createTitledBorder("Choose Text Color"));
// tcc.setPreviewPanel(pre);
// add(bannerPanel, BorderLayout.CENTER);
// tcc.remove(1);
tcc.remove(1);
// GuiMap map = new GuiMap(inputData, class_label, rgbColors);
// frame.add(map);
frame.add(tcc, BorderLayout.PAGE_END);
// add(tcc, BorderLayout.EAST);
// Display the window.
frame.pack();
frame.setVisible(true);
}
/**
* eliminate the duplicates
*
* @param inputData
* @param colors
* @param outputFile
* @return
* @throws IOException
*/
private static HashMap eliminatePoints(Vector<Vector<Integer>> inputData,
Vector<Integer> colors, String outputFile) throws IOException {
HashMap hm = new HashMap(15000, 0.75f);
for (int i = 0; i < inputData.size(); i++) {
hm.put((Vector<Integer>) inputData.get(i), colors.get(i));
}
// create output file
File file = new File(outputFile);
FileWriter fw = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fw);
Set set = hm.entrySet();
Iterator i = set.iterator();
while (i.hasNext()) {
Map.Entry me = (Map.Entry) i.next();
Vector<Integer> tmp = (Vector<Integer>) me.getKey();
out.write(Integer.toString(tmp.get(0)));
out.write('|');
out.write(Integer.toString(tmp.get(1)));
out.write('|');
Integer tmpI = (Integer) me.getValue();
out.write(Integer.toString(tmpI));
out.write(System.getProperty("line.separator"));
}
// flush and close
out.flush();
out.close();
return hm;
}
}