1 // 2 // PixelGrabber Example 3 // 4 // by Ozgur Balsoy 5 // November 20, 1996 6 // 7 // Reference: Sun On-line documentation. 8 // http://www.arl.hpc.mil/PET/training/IC001/projects/tutorials/Java/ 9 // Sun/api/java.awt.image.PixelGrabber.html 10 // 11 12 import java.awt.*; 13 import java.awt.image.*; 14 import java.applet.*; 15 import java.util.*; 16 17 public class grabber extends Applet { 18 private TextField tf; 19 private TextArea ta; 20 private Label status; 21 private Canvas drawarea; 22 private Image img=null; 23 int ppmline=0; 24 25 public void init() { 26 27 setLayout(new GridLayout(1,2)); 28 29 Panel left=new Panel(); 30 left.setLayout(new BorderLayout()); 31 32 Panel topleft = new Panel(); 33 topleft.setLayout(new GridLayout(1, 4)); 34 35 topleft.add(tf=new TextField()); 36 37 Panel buttonsP = new Panel(); 38 buttonsP.add(new Button("Load GIF")); 39 buttonsP.add(new Button("PPM Output")); 40 topleft.add(buttonsP); 41 42 left.add("North", topleft); 43 44 left.add("Center", drawarea=new Canvas()); 45 46 left.add("South", status=new Label()); 47 48 add(left); // left side of the applet. 49 add(ta=new TextArea()); // right side. 50 51 status.setText("See GIF files below."); 52 } 53 54 public void paint(Graphics appletg) { 55 56 Graphics g=drawarea.getGraphics(); 57 if (img != null) { 58 g.drawImage(img, 0, 0, this); 59 g.dispose(); 60 } 61 } 62 63 public boolean action(Event evt, Object arg) { 64 String s=(String) arg; 65 66 if(s.equals("Load GIF")) { 67 img=getImage(getCodeBase(), tf.getText()); 68 repaint(); 69 } 70 else if (s.equals("PPM Output")) getPixs(); 71 72 return true; 73 } 74 75 void getPixs() { 76 77 int w=30, h=30; 78 79 // PPM output starts. 80 ta.setText("P3\n"); 81 ta.appendText(Integer.toString(w) + " " + Integer.toString(h) + "\n"); 82 ta.appendText("255\n"); 83 handlepixels(img, 0, 0, w, h); 84 85 } 86 87 public void handlesinglepixel(int x, int y, int pixel) { 88 89 90 // pixel is in RGB model. It is a 4-byte long integer. 91 // It is converted into the string representation of PPM pixels. 92 // ie. Hex 0x00102030 becomes "16 32 48" 93 String s= Integer.toString((pixel & 0x00FF0000) >> 16) + " " 94 + Integer.toString((pixel & 0x0000FF00) >> 8) + " " 95 + Integer.toString(pixel & 0x000000FF) + " "; 96 // 97 // If we use PPM RAW format, then we have to use ascii characters instead. 98 // 99 // 100 // char c[]= {(char) ((pixel & 0x00FF0000) >> 16), 101 // (char) ((pixel & 0x0000FF00) >> 8) , 102 // (char) (pixel & 0x000000FF) }; 103 // String s=new String(c); 104 105 if (ppmline++>=3) { // comment out if you use RAW format. 106 ta.appendText(s + "\n"); // comment out if you use RAW format. 107 ppmline=0; // comment out if you use RAW format. 108 } else // comment out if you use RAW format. 109 ta.appendText(s); 110 } 111 112 public void handlepixels(Image img, int x, int y, int w, int h) { 113 int[] pixels = new int[w * h]; 114 PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w); 115 try { 116 pg.grabPixels(); 117 } catch (InterruptedException e) { 118 System.err.println("interrupted waiting for pixels!"); 119 return; 120 } 121 if ((pg.status() & ImageObserver.ABORT) != 0) { 122 System.err.println("image fetch aborted or errored"); 123 return; 124 } 125 for (int j = 0; j < h; j++) { 126 for (int i = 0; i < w; i++) { 127 handlesinglepixel(x+i, y+j, pixels[j * w + i]); 128 } 129 } 130 } 131 } 132 133 134