ff68b2c90a862019ecedd404eaf7095955e0e8ee
[mir.git] / source / tool / ImageTool.java
1 package tool;
2
3 import java.awt.Transparency;
4 import java.awt.image.ColorModel;
5 import java.awt.image.DataBuffer;
6 import java.awt.image.SampleModel;
7
8 import java.io.File;
9 import java.io.FileInputStream;
10 import java.io.FileNotFoundException;
11
12 import javax.media.jai.JAI;
13 import javax.media.jai.PlanarImage;
14
15
16 /**
17  * This class displays basic information about the image.
18  */
19 public class ImageTool {
20   /**
21    * The application entry point.
22    * @param args the command line arguments.
23    */
24   public static void main(String[] args) throws FileNotFoundException {
25     // We need one argument: the image filename.
26     if (args.length != 1) {
27       System.err.println("Usage: java tool.ImageTool image");
28       System.exit(0);
29     }
30     FileInputStream fi = new FileInputStream(args[0]);
31     // Open the image (using the name passed as a command line parameter)
32     PlanarImage pi = JAI.create("ImageRead", fi);
33
34     // Display image data. First, the image size (non-JAI related).
35     File image = new File(args[0]);
36     System.out.println("Image file size:" + image.length() + " bytes.");
37
38     // Now let's display the image dimensions and coordinates.
39     System.out.print("Dimensions: ");
40     System.out.print(pi.getWidth() + "x" + pi.getHeight() + " pixels");
41     System.out.println(" (from " + pi.getMinX() + "," + pi.getMinY() + " to " +
42                        pi.getMaxX() + "," + pi.getMaxY() + ")");
43
44     // Tiles number, dimensions and coordinates.
45     System.out.print("Tiles:");
46     System.out.print(pi.getTileWidth() + "x" + pi.getTileHeight() + " pixels" +
47                      " (" + pi.getNumXTiles() + "x" + pi.getNumYTiles() +
48                      " tiles)");
49     System.out.print(" (from " + pi.getMinTileX() + "," + pi.getMinTileY() +
50                      " to " + pi.getMaxTileX() + "," + pi.getMaxTileY() + ")");
51     System.out.println(" offset: " + pi.getTileGridXOffset() + "," +
52                        pi.getTileGridXOffset());
53
54     // Display info about the SampleModel of the image.
55     SampleModel sm = pi.getSampleModel();
56     System.out.println("Number of bands: " + sm.getNumBands());
57     System.out.print("Data type: ");
58
59     switch (sm.getDataType()) {
60     case DataBuffer.TYPE_BYTE:
61       System.out.println("byte");
62
63       break;
64
65     case DataBuffer.TYPE_SHORT:
66       System.out.println("short");
67
68       break;
69
70     case DataBuffer.TYPE_USHORT:
71       System.out.println("unsigned short");
72
73       break;
74
75     case DataBuffer.TYPE_INT:
76       System.out.println("int");
77
78       break;
79
80     case DataBuffer.TYPE_FLOAT:
81       System.out.println("float");
82
83       break;
84
85     case DataBuffer.TYPE_DOUBLE:
86       System.out.println("double");
87
88       break;
89
90     case DataBuffer.TYPE_UNDEFINED:
91       System.out.println("undefined");
92
93       break;
94     }
95
96     // Display info about the ColorModel of the image.
97     ColorModel cm = pi.getColorModel();
98     System.out.println("Number of color components: " + cm.getNumComponents());
99     System.out.println("Bits per pixel: " + cm.getPixelSize());
100     System.out.print("Transparency: ");
101
102     switch (cm.getTransparency()) {
103     case Transparency.OPAQUE:
104       System.out.println("opaque");
105
106       break;
107
108     case Transparency.BITMASK:
109       System.out.println("bitmask");
110
111       break;
112
113     case Transparency.TRANSLUCENT:
114       System.out.println("translucent");
115
116       break;
117     }
118   }
119 }