/Users/lyon/j4p/src/face/JPGFile.java

1    package face; 
2     
3    import com.sun.image.codec.jpeg.JPEGCodec; 
4    import com.sun.image.codec.jpeg.JPEGImageDecoder; 
5    import com.sun.image.codec.jpeg.JPEGImageEncoder; 
6     
7    import java.awt.image.BufferedImage; 
8    import java.io.FileInputStream; 
9    import java.io.FileNotFoundException; 
10   import java.io.FileOutputStream; 
11   import java.io.IOException; 
12    
13   /** 
14    * JPG File reader/writer. Uses native com.sun libraries (which 
15    * may deprecate at any time) 
16    * 
17    * 
18    * @author Konrad Rzeszutek 
19    * @version 1.0 
20    */ 
21   public class JPGFile implements ImageFileFormat { 
22    
23       private byte bytes[] = null;      // bytes which make up binary PPM image 
24       private double doubles[] = null; 
25       private String filename = null;     // filename for PPM image 
26       private int height = 0; 
27       private int width = 0; 
28    
29       /** 
30        * Read the PPM File. 
31        * 
32        * @throws   java.io.FileNotFoundException   if the directory/image specified is wrong 
33        * @throws   java.io.IOException  if there are problems reading the file. 
34        */ 
35       public JPGFile(String filename) throws FileNotFoundException, IOException { 
36           this.filename = filename; 
37           readImage(); 
38       } 
39    
40       /** 
41        * Get the height of the PPM image. 
42        * 
43        * @return the height of the image. 
44        */ 
45       public int getHeight() { 
46           return height; 
47       } 
48    
49       /** 
50        * Get the width of the PPM image. 
51        * 
52        * @return  the width of the image. 
53        */ 
54       public int getWidth() { 
55           return width; 
56       } 
57    
58       /** 
59        * Get the data as byte array. Data is of any type that 
60        * has been read from the file (usually 8bit RGB) 
61        * 
62        * @return  The data of the image. 
63        */ 
64       public byte[] getBytes() { 
65           return bytes; 
66       } 
67    
68       /** 
69        * Get the data as double array. Data is of any type that 
70        * has been read from the file (usually 8bit RGB put into an 64bit double) 
71        * 
72        * @return  The data of the image. 
73        */ 
74       public double[] getDouble() { 
75           return doubles; 
76       } 
77    
78       /** 
79        * Write to <code>fn</code> file the <code>data</code> using the 
80        * <code>width, height</code> variables. Data is assumed to be 8bit RGB. 
81        * 
82        * @throws   java.io.FileNotFoundException   if the directory/image specified is wrong 
83        * @throws   java.io.IOException  if there are problems reading the file. 
84        */ 
85       public static void writeImage(String fn, byte[] data, int width, int height) 
86               throws FileNotFoundException, IOException { 
87    
88           FileOutputStream fOut = new FileOutputStream(fn); 
89           JPEGImageEncoder jpeg_encode = JPEGCodec.createJPEGEncoder(fOut); 
90    
91           int ints[] = new int[data.length]; 
92           for (int i = 0; i < data.length; i++) 
93               ints[i] = 255 << 24 | 
94                       (data[i] & 0xff) << 16 | 
95                       (data[i] & 0xff) << 8 | 
96                       (data[i] & 0xff); 
97    
98           BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
99           image.setRGB(0, 0, width, height, ints, 0, width); 
100   
101          jpeg_encode.encode(image); 
102          fOut.close(); 
103      } 
104   
105      /** 
106       * Read the image from the specified file. 
107       * 
108       * @throws  java.io.FileNotFoundException pretty obvious 
109       * @throws  java.io.IOException filesystem related problems 
110       */ 
111      private void readImage() throws FileNotFoundException, IOException { 
112   
113          FileInputStream fIn = new FileInputStream(filename); 
114          JPEGImageDecoder jpeg_decode = JPEGCodec.createJPEGDecoder(fIn); 
115          BufferedImage image = jpeg_decode.decodeAsBufferedImage(); 
116   
117          width = image.getWidth(); 
118          height = image.getHeight(); 
119   
120          int[] rgbdata = new int[width * height]; 
121   
122          image.getRGB(0, 0, width, height, rgbdata, 0, width); 
123   
124   
125          bytes = new byte[rgbdata.length]; 
126          doubles = new double[rgbdata.length]; 
127   
128          for (int i = 0; i < bytes.length; i++) { 
129              bytes[i] = (byte) (rgbdata[i] & 0xFF); 
130              doubles[i] = (double) (rgbdata[i]); 
131          } 
132      } 
133  } 
134