// // COSC 387 imageView // Marcus A. Maloof // Department of Computer Science // Georgetown University // Fall 2000 // // This Java application provides a basic tool for displaying a raw, // grayscale image format. The format of the image file should be: // // // ... // ... pixel-value_{n}> // // where n = * and each pixel value, pixel-value_{i}, // is an integer [0, 255], black being 0, white being 255. // // Compile the code using the command: // // javac imageView.java // // Then run it using the command: // // java imageView image.raw // // where image.raw is a file containing the image in the format described // above. // import java.io.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.util.StringTokenizer; /*=======================================================================*/ public class imageView extends Frame { ImageFile iF; ImageArea image; TextField textfield; public imageView(String filename) { super("COSC 387 imageView"); // Create the window iF = new ImageFile(filename); // Load in the image this.setLayout(new BorderLayout()); this.setSize(iF.width, iF.height); // Specify frame's size this.setResizable(false); // Prevent resizing // Create a ScrollPane ScrollPane pane = new ScrollPane(ScrollPane.SCROLLBARS_NEVER); pane.setSize(iF.width, iF.height); // Specify the pane's size this.add(pane, "Center"); // Add it to the frame PixelMap pixMap = new PixelMap(); // Create a grayscale pixMap.MakeGrayScale(); // pixel map image = new ImageArea(this, iF, pixMap); // Create an image area image.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { textfield.setText(iF.getText(e.getX(),e.getY())); } // mouseMoved }); image.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { textfield.setText(iF.getText(e.getX(),e.getY())); } // mouseDragged }); image.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent e) { image.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); } // mouseDragged }); image.addMouseListener(new MouseAdapter() { public void mouseExited(MouseEvent e) { image.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } // mouseDragged }); pane.add(image); // Add it to the ScrollPane MenuBar menubar = new MenuBar(); // Create a menubar this.setMenuBar(menubar); // Add it to the frame Menu file = new Menu("File"); // Create a File menu menubar.add(file); // Add to menubar textfield = new TextField(); textfield.setEditable(false); textfield.setFont(new Font("TimesRoman", Font.PLAIN, 9)); this.add(textfield, "South"); // Create a quit menu item with a shortcut, and add it to the menu MenuItem q; file.add(q = new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q))); // Create and register an action listener object for the menu item. q.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); // Create and register action listener for the window closing event. addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // Set the window size and pop it up. this.pack(); this.show(); } // imageView public static void main(String[] args) { if (args.length == 1) { new imageView(args[0]); } // if else { System.err.println("Usage: java imageView "); } // else } // main } // imageView class /*=======================================================================*/ class PixelMap { int map[]; int colors; public PixelMap() { colors = 256; map = new int[colors]; } // PixelMap public PixelMap(int colors) { this.colors = colors; map = new int[this.colors]; } // PixelMap public void MakeGrayScale() { int i, red, blue, green; map[0] = 0xff000000; for (i = 1; i < this.colors; i++) { map[i] = (255 << 24) | (i << 16) | (i << 8) | i; } // for map[this.colors-1] = 0xffffffff; } // MakeGrayScale public void MakeColored() { int i, red, blue, green; map[0] = 0xffffffff; for (i = 1; i < this.colors; i++) { red = i; green = (i<128) ? i << 1 : 255-(i<<1); blue = this.colors - i; map[i] = (255 << 24) | (red << 16) | (green << 8) | blue; } // for map[this.colors-1] = 0xff000000; } // MakeColored } // PixelMap class /*=======================================================================*/ class ImageFile { int width, height; String filename; int pixels[]; public ImageFile(String filename) { this.filename = filename; LoadImageFile(this.filename); } // ImageFile private void LoadImageFile(String filename) { int i = 0; try { BufferedReader bfr = new BufferedReader(new FileReader(filename)); String line; StringTokenizer tokens; if ((line = bfr.readLine()) != null) { tokens = new StringTokenizer(line, " "); this.width = Integer.parseInt(tokens.nextToken()); this.height = Integer.parseInt(tokens.nextToken()); this.pixels = new int[this.width * this.height]; while ((line = bfr.readLine()) != null) { tokens = new StringTokenizer(line, " "); while (tokens.hasMoreElements()) { this.pixels[i] = Integer.parseInt(tokens.nextToken()); i++; } // while } // while } // if bfr.close(); } // try catch (IOException e) { System.err.println(e); System.exit(-1); } // catch } // LoadImageFile public String getText(int x, int y) { if (x > 0 && x < this.width && y > 0 && y < this.height) { return new String("("+x+","+y+")="+this.pixels[width*y+x]); } // if else { return new String(); } // else } // getText } // ImageFile class /*=======================================================================*/ class ImageArea extends Component { Image theImage = null; protected Frame frame; public ImageArea(Frame frame, ImageFile iF, PixelMap pixMap) { int i, pixels[] = new int[iF.width * iF.height]; for (i = 0; i < iF.width*iF.height; i++) { pixels[i] = pixMap.map[iF.pixels[i]]; } // for this.frame = frame; theImage = createImage( new MemoryImageSource(iF.width, iF.height, pixels, 0, iF.width)); } // ImageArea public void paint (Graphics g) { update(g); } // update public void update(Graphics g) { g.drawImage(theImage, 0, 0, this); } // paint } // ImageArea class