/* 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: color_trace.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.util.*; import java.io.*; import java.awt.*; import java.awt.image.*; public class color_trace { static Image m_image; static int m_w, m_h, m_pixels[]; static int INTERVAL_SIZE; public color_trace() { } public static void main(String[] args) { if (args.length != 3) { System.err.println("usage: java color file.labels file.gif INTERVAL_SIZE"); return; } convert(args[0], args[1], args[2]); } public static void convert(String fnLabels, String fnPicture, String intervalsize) { m_image = Toolkit.getDefaultToolkit().getImage(fnPicture); MediaTracker mt = new MediaTracker(new Panel()); mt.addImage(m_image, 0); try {mt.waitForID(0);}catch(Exception e){handleEx(e);} try { INTERVAL_SIZE = Integer.parseInt(intervalsize); } catch (Exception e) { handleEx(e); } m_w = m_image.getWidth(new Panel()); m_h = m_image.getHeight(new Panel()); System.err.println("width = " + m_w); System.err.println("height = " + m_h); 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); } BufferedReader br = null; String s = ""; Vector phases = new Vector(); try { br = new BufferedReader(new FileReader(fnLabels)); } catch (Exception e) { handleEx(e); } while (true) { try { s = br.readLine(); } catch (Exception e) { handleEx(e); } if (s == null) break; int index = s.indexOf(" "); if (index == -1) return; String phase = s.substring(0, index); int PHASE = Integer.parseInt(phase, 10); phases.add(new Integer(PHASE)); } try { br.close(); } catch (Exception e) { handleEx(e); } //System.out.println(""+m_w+ " " + m_h); for (int i = 0; i < m_w; i++) { int phase = -1; if (i/INTERVAL_SIZE < phases.size()) phase = ((Integer)(phases.elementAt(i/INTERVAL_SIZE))).intValue(); Color c = indextocolor(phase); for (int j = 0; j < m_h; j++) { int index = j * m_w + i; int pixel = m_pixels[index]; int R = (pixel >> 16) & 0xff; int G = (pixel >> 8) & 0xff; int B = pixel & 0xff; int AVG = (R+G+B)/3; R = c.getRed(); G = c.getGreen(); B = c.getBlue(); R = R * AVG / 255; G = G * AVG / 255; B = B * AVG / 255; pixel = (R << 16) | (G << 8) | B; m_pixels[index] = pixel; /*if ((m_pixels[index] & 0xFFFFFF) != 0xFFFFFF) { m_pixels[index] = (c.getRed() << 16) | (c.getGreen() << 8) | (c.getBlue()); }*/ } } // paint the grid lines /*for (int i = 0; true; i++) { int col = i * INTERVAL_SIZE; if (col >= m_w) break; for (int j = 0; j < m_h; j+=2) { Color c = Color.darkGray; m_pixels[j*m_w+col] = (c.getRed() << 16) | (c.getGreen() << 8) | (c.getBlue()); } } for (int i = 0; true; i++) { int col = i * 10 * INTERVAL_SIZE; if (col >= m_w) break; for (int j = 0; j < m_h; j++) { Color c = Color.black; m_pixels[j*m_w+col] = (c.getRed() << 16) | (c.getGreen() << 8) | (c.getBlue()); } }*/ byte[][] red = new byte[m_w][m_h]; byte[][] green = new byte[m_w][m_h]; byte[][] blue = new byte[m_w][m_h]; for (int i = 0; i < m_h; i++) { for (int j = 0; j < m_w; j++) { int pixel = m_pixels[i * m_w + j]; byte r = (byte)((pixel >> 16) & 0xff); byte g = (byte)((pixel >> 8) & 0xff); byte b = (byte)((pixel) & 0xff); red[j][i] = r; green[j][i] = g; blue[j][i] = b; } } try { GIFEncoder g2 = new GIFEncoder(red, green, blue); File f2 = new File("colorful.gif"); FileOutputStream fos2 = new FileOutputStream(f2); g2.Write(fos2); } catch (Exception e) { handleEx(e); } //writePicture(m_pixels, "colored.ppm", m_w, m_h); } public static void handleEx(Exception e) { System.err.println("Exception occurred!" + e); e.printStackTrace(); } 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; } static void writePicture(int picture[], String filename, int WIDTH, int HEIGHT) { PrintWriter pw = null; try { File f = new File(filename); FileOutputStream fos = new FileOutputStream(f); pw = new PrintWriter(fos); } catch (Exception e) { handleEx(e); } pw.println("P3"); pw.println("" + WIDTH + " " + HEIGHT); pw.println("255"); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { int pixel = picture[i*m_w+j]; int red, green, blue; red = (pixel >> 16) & 0xFF; green = (pixel >> 8) & 0xFF; blue = (pixel) & 0xFF; pw.print("" + red + " " + green + " " + blue + " "); } pw.println(); } pw.println(); try { pw.close(); } catch (Exception e) { handleEx(e); } } }