listImage

C:\ece595_06\class16>java listImage

test3.png

  0  23  46  69  93 116 139 162 186 209 232
  0  23  46  69  93 116 139 162 186 209 232
  0  23  49 155 225 229 191 165 186 209 232
  0  23 145 232 232 232 232 201 186 209 232
  0  23 223 232 232 232 232 230 186 209 232
  0  23 223 232 232 232 232 230 186 209 232
  0  23 145 232 232 232 232 201 186 209 232
  0  23  49 155 225 229 191 165 186 209 232
  0  23  46  69  93 116 139 162 186 209 232
  0  23  46  69  93 116 139 162 186 209 232
  0  23  46  69  93 116 139 162 186 209 232


listImage.java

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;



public class listImage {
    
   public static void listGray(BufferedImage img, String cmt) {
      int i, j, val, r, g, b;
      int nrow = img.getHeight();
      int ncol = img.getWidth();
      System.out.format("%n%s%n%n",cmt);
      for (j=0; j<nrow; j++) {
         for (i=0; i<ncol; i++) {
               val = img.getRGB(i,j);
	       r = (val>>16)&0xFF;
               g = (val>>8)&0xFF;
               b = val&0xFF;
               val = (r+g+b)/3;
               System.out.format("%3d ",val);
               }
         System.out.format("%n");
       }
   }
    public static BufferedImage readImage(String filename) {
       BufferedImage img;
       try {
           img = ImageIO.read(new File(filename));
           return img;
       } catch (IOException e) {
		//System.out.println(e); // e.getMessage());
		System.out.println(filename + " not found");
		System.exit(-1);
       }
       return null;
    }

   public static void main(String [] args) {
     String filename = "test3.png";
     if (args.length>0) filename = args[0];
     BufferedImage img = readImage(filename);
     listGray(img, filename);
   }


}


Results




Maintained by John Loomis, updated Tue Oct 15 08:26:57 2013