/Users/lyon/j4p/src/j2d/file/PPMToolkit.java

1    // Glenn Josefiak 
2    // Fairfield University 
3    // SW513 
4    // Spring 2003 
5     
6    package j2d.file; 
7     
8    import ip.JPM.Decoders.PpmDecoder; 
9    import ip.JPM.Encoders.PpmEncoder; 
10    
11   import java.awt.*; 
12   import java.awt.image.BufferedImage; 
13   import java.awt.image.PixelGrabber; 
14   import java.io.*; 
15   import java.util.zip.GZIPInputStream; 
16   import java.util.zip.GZIPOutputStream; 
17    
18   /** 
19    * Wrapper for Jef Poskanzer's PPM classes to create a simple toolkit 
20    * interface for reading and writing PPM files. 
21    * (http://www.acme.com/java/) 
22    * 
23    * Note that the PPM format supports ASCII or binary representation 
24    * of the pixel data.  Both of these formats are supported for input. 
25    * For output, only binary pixel data is supported. 
26    *  
27    */ 
28   public class PPMToolkit { 
29    
30       /** 
31        * Read a PPM file (binary or ASCII formats 1-6) 
32        * 
33        * @param infile        The PPM image file. 
34        * @param zipped        Flag to select ZIP decompression. 
35        * @return              An AWT Image object containing the read data. 
36        */ 
37       public static Image getImage(File infile, boolean zipped)  
38           throws FileNotFoundException, IOException { 
39            
40           PpmDecoder decoder; 
41           GZIPInputStream gis = null; 
42           FileInputStream fis = null; 
43           // This next line makes up for the lousy API of PpmDecoder. 
44           // The decoder should already know its input stream and should 
45           // not need to be told again on readHeader() or readRow()! 
46           InputStream inputStreamAlias = null; 
47            
48           fis = new FileInputStream(infile); 
49           if (zipped){ 
50               gis = new GZIPInputStream(fis); 
51               inputStreamAlias = gis; 
52               decoder = new PpmDecoder(gis); 
53           }else{ 
54               inputStreamAlias = fis; 
55               decoder = new PpmDecoder(fis); 
56           } 
57            
58           BufferedImage outputImage = null; 
59           int width, height; 
60           int pixels[]; 
61           int rowPixels[]; 
62            
63           decoder.readHeader(inputStreamAlias); 
64           width = decoder.getWidth(); 
65           height = decoder.getHeight(); 
66           if (width == -1 || height == -1) 
67               throw new IOException(); 
68    
69           pixels = new int[width * height]; 
70           rowPixels = new int[width]; 
71            
72           for (int row = 0; row < height; row++) { 
73            
74               decoder.readRow(inputStreamAlias, row, rowPixels); 
75                
76               for (int i=0; i< width; i++){ 
77                   pixels[row*width + i] = rowPixels[i]; 
78               } 
79           } 
80            
81           outputImage = new BufferedImage(width,height, 
82                   BufferedImage.TYPE_INT_RGB); 
83           outputImage.setRGB(0,0,width, 
84                   height,pixels,0,width); 
85           return outputImage; 
86       } 
87        
88       /** 
89        * Write a PPM file (binary format 6) 
90        * 
91        * @param img           The AWT Image to be written. 
92        * @param outfile       The file location to which to write. 
93        * @param zipped        Flag to select ZIP compression. 
94        */ 
95       public static void saveImage(Image img, File outfile, boolean zipped) 
96           throws FileNotFoundException, IOException { 
97            
98           PpmEncoder encoder; 
99           FileOutputStream fos = null; 
100          GZIPOutputStream gos = null; 
101           
102          fos = new FileOutputStream(outfile); 
103          if (zipped){ 
104              gos = new GZIPOutputStream(fos); 
105              encoder = new PpmEncoder(img, gos); 
106          }else{ 
107              encoder = new PpmEncoder(img, fos); 
108          } 
109           
110          int width = img.getWidth(null); 
111          int height = img.getHeight(null); 
112          int rowPixels[] = new int[width]; 
113          int pixels[] = new int[width*height]; 
114   
115          try{ 
116              PixelGrabber pg = new PixelGrabber( 
117                      img, 0, 0, width, height, pixels, 0, width); 
118              pg.grabPixels(); 
119          }catch(InterruptedException ie){ 
120              // do nothing for now 
121          } 
122           
123          encoder.encodeStart(width, height); 
124          encoder.encodePixels(0,0,width,height, pixels, 0, width); 
125          encoder.encodeDone(); 
126           
127          if (gos != null){ 
128              gos.finish(); 
129              gos.close(); 
130          } 
131          if (fos != null) 
132              fos.close(); 
133      } 
134  }