/* UC Santa Barbara ArchLab www.cs.ucsb.edu/~arch Wavelet-Based Phase Classification Source Code www.cs.ucsb.edu/~arch/wavelet Copyright (c) 2006 The Regents of the University of California. All Rights Reserved. Permission to use, copy, modify, and distribute this software and its documentation for educational, research and non-profit purposes, without fee, and without a written agreement is hereby granted, provided that the above copyright notice, this paragraph and the following three paragraphs appear in all copies. Permission to incorporate this software into commercial products may be obtained by contacting the University of California. For information about obtaining such a license contact: Tim Sherwood This software program and documentation are copyrighted by The Regents of the University of California. The software program and documentation are supplied "as is", without any accompanying services from The Regents. The Regents does not warrant that the operation of the program will be uninterrupted or error-free. The end-user understands that the program was developed for research purposes and is advised not to rely exclusively on the program for any reason. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. Ted Huffmire, (www.cs.ucsb.edu/~huffmire, huffmire at cs dot ucsb dot edu) Tim Sherwood (www.cs.ucsb.edu/~sherwood, sherwood at cs dot ucsb dot edu) 14 June 2006 File: app.java Publication: Ted Huffmire and Tim Sherwood. Wavelet-Based Phase Classification. Proceedings of the Fifteenth International Conference on Parallel Architectures and Compilation Techniques (PACT'06), Seattle, Washington, September 16-20, 2006. */ import java.io.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.util.*; class img { float[] y_array; float[] wy; public img(pixel[] pixels) { init_weights(); y_array = new float[archpanel.IMAGESIZE * archpanel.IMAGESIZE]; for (int i = 0; i < archpanel.IMAGESIZE * archpanel.IMAGESIZE; i++) { float weight = wy[bin(i/archpanel.IMAGESIZE,i%archpanel.IMAGESIZE)]; y_array[i] = pixels[i].getY() * weight; } } public float[] getYArray() { return y_array; } public void init_weights() { wy = new float[6]; wy[0] = 4.04f; wy[1] = 0.78f; wy[2] = 0.46f; wy[3] = 0.42f; wy[4] = 0.41f; wy[5] = 0.32f; } public int level(float i) { int rv; double log2i; log2i = Math.log((double)(i + 1)) * 1.442695041; rv = (int)Math.floor(log2i); return rv; } int max(int a, int b) { if (a < b) return b; else return a; } int min(int a, int b) { if (a < b) return a; else return b; } int bin(float i, float j) { int rv; int levi = level(i); int levj = level(j); int max = max(levi, levj); rv = min(max, 5); return rv; } float myabs(float k) { if (k < 0) return -k; else return k; } } class archpanel extends Panel { public static final int IMAGESIZE = 16; public static final int K = 10; // number of clusters public static final int ITERATIONS = 20; // number of clustering iterations Image m_image = null; int m_w = 0, m_h = 0; int phase = -1, m_pixels[]; Image dbImage; Graphics dbg; // score m_s; Vector images = new Vector(); public Dimension getMinimumSize() { return getImageDimension(); } public Dimension getPreferredSize() { return getImageDimension(); } public archpanel(String fn, String fn2) { // m_s = new score(IMAGESIZE); m_image = Toolkit.getDefaultToolkit().getImage(fn); MediaTracker mt = new MediaTracker(this); mt.addImage(m_image, 0); try {mt.waitForID(0);}catch(Exception e){} m_w = m_image.getWidth(this); m_h = m_image.getHeight(this); PixelGrabber pg; m_pixels = new int[m_w * m_h]; pg = new PixelGrabber(m_image, 0, 0, m_w, m_h, m_pixels, 0, m_w); try { boolean success = pg.grabPixels(0); } catch (Exception e) { handleEx(e); } processImages(); /* img g = (img)images.elementAt(40); float[] array = g.getYArray(); String file = "40.ppm"; writePicture(array,file,false,IMAGESIZE); System.exit(1); */ BufferedReader br = null; try { br = new BufferedReader(new FileReader(fn2)); } catch (Exception e) { handleEx(e); } Vector miss = new Vector(); for (int i = 0; i < images.size(); i++) miss.add(new Integer(0)); int PHASE = 0; while (true) { String s = ""; try { s = br.readLine(); } catch (Exception e) { handleEx(e); } if (s == null) break; if (s.indexOf("*****") == 0) { s = s.substring(6); int index = s.indexOf(" *****"); if (index == -1) continue; String phase = s.substring(0, index); try { PHASE=Integer.parseInt(phase,10);}catch(Exception e) { handleEx(e);} continue; } else if (s.indexOf(":") == -1) { int freq = Integer.parseInt(s,10); //System.err.println("PHASE="+PHASE+" " + miss.size()); if (PHASE >= miss.size()) continue; int oldfreq = ((Integer)(miss.elementAt(PHASE))).intValue(); miss.setElementAt(new Integer(freq+oldfreq), PHASE); } } try { br.close(); } catch(Exception e) { handleEx(e); } // Assign a random clustering int[] classify = new int[images.size()]; for (int i = 0; i < images.size(); i++) { classify[i] = ((int)(Math.random() * 100.0)) % K; //System.err.println(""+classify[i]); } for (int w = 0; w < ITERATIONS; w++) { System.err.print("["+w+"] "); Vector centers = new Vector(); // What is the center of each cluster? for (int i = 0; i < K; i++) { Vector cluster = new Vector(); for (int j = 0; j < images.size(); j++) { if (classify[j] == i) { cluster.add(new Integer(j)); } } float[] average = new float[IMAGESIZE * IMAGESIZE]; for (int j = 0; j < IMAGESIZE * IMAGESIZE; j++) average[i] = 0.0f; for (int j = 0; j < cluster.size(); j++) { int c = ((Integer)(cluster.elementAt(j))).intValue(); img myimg = (img)(images.elementAt(c)); float[] y_array = myimg.getYArray(); for (int k = 0; k < IMAGESIZE * IMAGESIZE; k++) { average[k] += y_array[k]; } } Vector center = new Vector(); for (int k = 0; k < IMAGESIZE * IMAGESIZE; k++) { average[k] /= (float)(cluster.size()); center.add(new Float(average[k])); } centers.add(center); cluster = null; } System.err.print("."); // Assign each interval to the closest cluster for (int i = 0; i < images.size(); i++) { //System.err.print("*"); img myimg = (img)(images.elementAt(i)); float[] array1 = new float[IMAGESIZE * IMAGESIZE]; for (int j = 0; j < IMAGESIZE * IMAGESIZE; j++) { array1[j] = (myimg.getYArray())[j]; } float distance = Float.POSITIVE_INFINITY; int minindex = -1; for (int j = 0; j < centers.size(); j++) { //System.err.print("#"); Vector c = (Vector)(centers.elementAt(j)); float[] array2 = new float[IMAGESIZE * IMAGESIZE]; for (int k = 0; k < IMAGESIZE * IMAGESIZE; k++) { array2[k] = ((Float)(c.elementAt(k))).floatValue(); } float d = compare(array1, array2); if (d < distance) { distance = d; minindex = j; } } classify[i] = minindex; } centers = null; float[][] matrix1 = new float[images.size()][IMAGESIZE * IMAGESIZE]; float[][] matrix2 = new float[IMAGESIZE * IMAGESIZE][2]; float[][] result = new float[images.size()][2]; for (int i = 0; i < images.size(); i++) { img myimg = (img)(images.elementAt(i)); float[] y_array = myimg.getYArray(); for (int j = 0; j < IMAGESIZE * IMAGESIZE; j++) { matrix1[i][j] = y_array[j]; } } for (int i = 0; i < IMAGESIZE * IMAGESIZE; i++) { for (int j = 0; j < 2; j++) { matrix2[i][j] = (float)Math.random() * 2.0f - 1.0f; } } for (int i = 0; i < images.size(); i++) { for (int j = 0; j < 2; j++) { result[i][j] = 0.0f; for (int k = 0; k < IMAGESIZE * IMAGESIZE; k++) { result[i][j] += matrix1[i][k] * matrix2[k][j]; } } } PrintWriter pw = null; if (w == ITERATIONS-1) { try { File f = new File("wavelet.labels"); FileOutputStream fos = new FileOutputStream(f); pw = new PrintWriter(fos); } catch (Exception e) { handleEx(e); } for (int i = 0; i < images.size(); i++) { pw.println(""+classify[i]+" 0.00"); } pw.close(); } } System.exit(1); } public float compare(float[] a, float[] b) { float d = 0.0f; for (int i = 0; i < IMAGESIZE * IMAGESIZE; i++) { d += (a[i] - b[i]) * (a[i] - b[i]); } return d; } public void writePicture(float[] y_array, String filename, boolean intensity, int WIDTH) { PrintWriter pw = null; try { File f = new File(filename); FileOutputStream fos = new FileOutputStream(f); pw = new PrintWriter(fos); } catch (Exception e) { handleEx(e); } int HEIGHT = WIDTH; pw.println("P3"); pw.println("" + WIDTH + " " + HEIGHT); pw.println("255"); float max = Float.NEGATIVE_INFINITY; float min = Float.POSITIVE_INFINITY; for (int i = 0; i < HEIGHT * WIDTH; i++) { float val = y_array[i]; if (val > max) max = val; if (val < min) min = val; } float max2 = Float.NEGATIVE_INFINITY; for (int i = 0; i < HEIGHT * WIDTH; i++) { float val = y_array[i]; if (val > max2 && val < max) max2 = val; } float max3 = Float.NEGATIVE_INFINITY; for (int i = 0; i < HEIGHT * WIDTH; i++) { float val = y_array[i]; if (val > max3 && val < max2) max3 = val; } float max4 = Float.NEGATIVE_INFINITY; for (int i = 0; i < HEIGHT * WIDTH; i++) { float val = y_array[i]; if (val > max4 && val < max3) max4 = val; } for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { float val = y_array[i*WIDTH+j]; int pixel = (int)(255.0f * (val) / (max4)); if (pixel > 255) pixel = 255; if (pixel < 0) pixel = 0; pw.print("" + pixel + " " + pixel + " " + pixel + " "); } pw.println(); } pw.println(); try { pw.close(); } catch (Exception e) { handleEx(e); } } public void processImages() { int[] pixels2 = new int[400 * 400]; //System.err.print("Now processing slice #"); for (int i = 0; i < m_w / 20; i++) { //System.err.print(""+i+" "); Image big = getImage(i); Image scaled = big.getScaledInstance(IMAGESIZE, IMAGESIZE, Image.SCALE_DEFAULT); MediaTracker tracker2 = new MediaTracker(this); try { tracker2.addImage(scaled, 0); tracker2.waitForID(0); } catch (Exception e) { handleEx(e); } int w = scaled.getWidth(null); int h = scaled.getHeight(null); img I = processImage(scaled, w, h, i); //m_s.addImage(I); images.add(I); } } /* public float[] getscores(image image) { return m_s.ScoreQuery(image); }*/ /*public int[] score(image image) { float[] scores = m_s.ScoreQuery(image); int i, j, index; float[] best = new float[m_s.getSize()]; int[] indices = new int[m_s.getSize()]; float min = 999999.0f; for (i = 0; i < m_s.getSize(); i++) { index = -1; for (j = 0; j < scores.length; j++) { if (scores[j] < min) { min = scores[j]; index = j; } } if (index == -1) { System.err.println("sorting error 1."); } best[i] = min; indices[i] = index; if (scores[index] != min) { System.err.println("sorting error 2."); } scores[index] = 999999.0f; min = 999999.0f; } return indices; }*/ /*public image processQuery() { Image big = getImage(phase); Image scaled = big.getScaledInstance(IMAGESIZE, IMAGESIZE, Image.SCALE_DEFAULT); MediaTracker tracker2 = new MediaTracker(this); try { tracker2.addImage(scaled, 0); tracker2.waitForID(0); } catch (Exception e) { handleEx(e); } int w = scaled.getWidth(null); int h = scaled.getHeight(null); image I = processImage(scaled, w, h, 0); return I; }*/ public Image getImage(int PHASE) { int[] pixels = new int[20 * 400]; for (int row = 0; row < 400; row++) { for (int col = 0; col < 20; col++) { pixels[row*20 + col] = m_pixels[row*m_w+PHASE*20+col]; } } MemoryImageSource mis = new MemoryImageSource(20, 400, pixels, 0, 20); Image img = createImage(mis); MediaTracker tracker = new MediaTracker(this); try { tracker.addImage(img, 0); tracker.waitForID(0); } catch (Exception e) { handleEx(e); } return img; } public img processImage(Image scaled, int w, int h, int index) { PixelGrabber pg; int[] pixels = new int[w * h]; pg = new PixelGrabber(scaled, 0, 0, w, h, pixels, 0, w); try { boolean success = pg.grabPixels(0); } catch (Exception e) { System.err.println("processImage!" + e); } pixel[] pixels2 = new pixel[w * h]; int i, j; for (j = 0; j < h; j++) { for (i = 0; i < w; i++) { int p = pixels[j * w + i]; pixel p2 = new pixel(); int red = (p >> 16) & 0xff; int green = (p >> 8) & 0xff; int blue = (p) & 0xff; //int average = (red+green+blue)/3; //p2.setRGB((float)average/255.0f, (float)average/255.0f, (float)average/255.0f); if (red != 255 || green != 255 || blue != 255) p2.setRGB(0.0f, 0.0f, 0.0f); else p2.setRGB(1.0f, 1.0f, 1.0f); pixels2[j * w + i] = p2; } } image image = new image(); image.setWidth(w); image.setHeight(h); image.setIndex(index); image.setPixels(pixels2); decompose d = new decompose(); d.DecomposeImage(image, IMAGESIZE); img myimg = new img(image.getPixels()); return myimg; } void handleEx(Exception e) { System.err.println("Exception occurred!" + e); e.printStackTrace(); } /*public void update(Graphics g) { if (dbImage == null) { dbImage = createImage(m_w, m_h); dbg = dbImage.getGraphics(); } dbg.setColor(getBackground()); dbg.fillRect(0, 0, m_w, m_h); dbg.setColor(getForeground()); paint(dbg); g.drawImage(dbImage,0,0,this); }*/ public void paint(Graphics g) { if (m_image != null) g.drawImage(m_image, 0, 0, m_w, m_h, null); if (phase != -1) { Color old = g.getColor(); g.setColor(Color.yellow); g.drawRect(phase*20,0,20,1200); g.setColor(old); } } public Dimension getImageDimension() { if (m_w == 0 || m_h == 0) { m_w = m_image.getWidth(null); m_h = m_image.getHeight(null); } Dimension d = new Dimension(m_w, m_h); return d; } public void highlight(int x, int y) { phase = x/20; } } class archframe extends Frame implements MouseListener { int m_x, m_y; ScrollPane m_sp; archpanel m_p; int oldx = 0, oldy = 0; Image m_image = null; int[] indices = null; // float[] scores = null; public archframe(String fn, String fn2) { setSize(1100, 750); setBackground(Color.white); setLayout(null); m_sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS); m_p = new archpanel(fn, fn2); m_p.addMouseListener(this); Dimension d = m_p.getImageDimension(); m_sp.setSize(1024, 400); m_sp.add(m_p); add(m_sp); setTitle("Wavelet Image Query for Computer Architecture!"); } public void paint(Graphics g) { if (m_image != null) g.drawImage(m_image, 10, 410, 20, 400, null); if (indices != null) { for (int i = 0; i < indices.length && i < 20; i++) { // g.drawString(""+scores[indices[i]], 490, 420+i*15); Image img = m_p.getImage(indices[i]); if (img != null) g.drawImage(img, 30 + i * 20, 410, 20, 400, null); } } } public static final int NUM_COLORS = 27; static Color indextocolor(int index) { if (index == -1) return Color.white; else if (index == -2) return Color.black; else if (index == -3) return Color.darkGray; else if (index%NUM_COLORS == 0) return Color.red; else if (index%NUM_COLORS == 1) return Color.orange; else if (index%NUM_COLORS == 2) return Color.yellow; else if (index%NUM_COLORS == 3) return Color.green; else if (index%NUM_COLORS == 4) return Color.cyan; else if (index%NUM_COLORS == 5) return Color.blue; else if (index%NUM_COLORS == 6) return Color.magenta; else if (index%NUM_COLORS == 7) return Color.pink; else if (index%NUM_COLORS == 8) return new Color(0xA5,0x2A,0x2A); else if (index%NUM_COLORS == 9) return new Color(0x8A,0x2B,0xE2); else if (index%NUM_COLORS == 10) return new Color(0x7F,0xFF,0xD4); else if (index%NUM_COLORS == 11) return new Color(0x5F,0x9E,0xA0); else if (index%NUM_COLORS == 12) return new Color(0x7F,0xFF,0x00); else if (index%NUM_COLORS == 13) return new Color(0xD2,0x69,0x1E); else if (index%NUM_COLORS == 14) return new Color(0xFF,0x7F,0x50); else if (index%NUM_COLORS == 15) return new Color(0x64,0x95,0xED); else if (index%NUM_COLORS == 16) return new Color(0xDC,0x14,0x3C); else if (index%NUM_COLORS == 17) return new Color(0x00,0x00,0x8B); else if (index%NUM_COLORS == 18) return new Color(0xB8,0x86,0x0B); else if (index%NUM_COLORS == 19) return new Color(0xA9,0xA9,0xA9); else if (index%NUM_COLORS == 20) return new Color(0x00,0x64,0x00); else if (index%NUM_COLORS == 21) return new Color(0x8B,0x00,0x8B); else if (index%NUM_COLORS == 22) return new Color(0xBD,0xB7,0x6B); else if (index%NUM_COLORS == 23) return new Color(0xFF,0x14,0x93); else if (index%NUM_COLORS == 24) return new Color(0xAD,0xFF,0x2F); else if (index%NUM_COLORS == 25) return new Color(0xFF,0x45,0x00); else if (index%NUM_COLORS == 26) return new Color(0xA0,0x52,0x2D); else return Color.white; } public void handleEx(Exception e) { System.err.println("" + e); e.printStackTrace(); } /*public void setPosition(int x, int y) { m_p.highlight(x, y); m_p.repaint(); int phase = x/20; m_image = m_p.getImage(phase); image img = m_p.processQuery(); indices = m_p.score(img); scores = m_p.getscores(img); repaint(); }*/ public void mouseClicked(MouseEvent e) { if (e.getSource() instanceof archpanel) { //setPosition(e.getX(), e.getY()); oldx = e.getX(); oldy = e.getY(); } } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } } public class app { public static void main(String args[]) { if (args.length != 2) { System.err.println("Usage: java app firefox.gif firefox.pc"); return; } archframe f = new archframe(args[0], args[1]); f.setVisible(true); } }