/Users/lyon/j4p/src/ip/JPM/Encoders/PpmEncoder.java

1    // PpmEncoder - write out an image as a PPM 
2    // 
3    // Copyright (C)1996,1998 by Jef Poskanzer <jef@acme.com>. All rights reserved. 
4    // 
5    // Redistribution and use in source and binary forms, with or without 
6    // modification, are permitted provided that the following conditions 
7    // are met: 
8    // 1. Redistributions of source code must retain the above copyright 
9    //    notice, this list of conditions and the following disclaimer. 
10   // 2. Redistributions in binary form must reproduce the above copyright 
11   //    notice, this list of conditions and the following disclaimer in the 
12   //    documentation and/or other materials provided with the distribution. 
13   // 
14   // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 
15   // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
16   // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
17   // ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 
18   // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
19   // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
20   // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
21   // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
22   // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
23   // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
24   // SUCH DAMAGE. 
25   // 
26   // Visit the ACME Labs Java page for up-to-date versions of this and other 
27   // fine Java utilities: http://www.acme.com/java/ 
28    
29   package ip.JPM.Encoders; 
30    
31   import java.awt.Image; 
32   import java.awt.image.ImageProducer; 
33   import java.io.IOException; 
34   import java.io.OutputStream; 
35    
36   /// Write out an image as a PPM. 
37   // <P> 
38   // Writes an image onto a specified OutputStream in the PPM file format. 
39   // <P> 
40   // <A HREF="/resources/classes/Acme/JPM/Encoders/PpmEncoder.java">Fetch the software.</A><BR> 
41   // <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A> 
42   // <P> 
43   // @see ToPpm 
44    
45   public class PpmEncoder extends ImageEncoder { 
46    
47       /// Constructor. 
48       // @param img The image to encode. 
49       // @param out The stream to write the PPM to. 
50       public PpmEncoder(Image img, 
51                         OutputStream out) 
52               throws IOException { 
53           super(img, out); 
54       } 
55    
56       /// Constructor. 
57       // @param prod The ImageProducer to encode. 
58       // @param out The stream to write the PPM to. 
59       public PpmEncoder(ImageProducer prod, 
60                         OutputStream out) 
61               throws IOException { 
62           super(prod, out); 
63       } 
64    
65    
66       public void encodeStart(int width, 
67                               int height) 
68               throws IOException { 
69           writeString(out, "P6\n"); 
70           writeString(out, 
71                       width + " " + height + "\n"); 
72           writeString(out, "255\n"); 
73       } 
74    
75       static void writeString(OutputStream out, 
76                               String str) 
77               throws IOException { 
78           byte[] buf = str.getBytes(); 
79           out.write(buf); 
80       } 
81    
82       public void encodePixels(int x, 
83                                int y, 
84                                int w, 
85                                int h, 
86                                int[] rgbPixels, 
87                                int off, 
88                                int scansize) 
89               throws IOException { 
90           byte[] ppmPixels = new byte[w * 3]; 
91           for (int row = 0; row < h; ++row) { 
92               int rowOff = off + row * scansize; 
93               for (int col = 0; col < w; ++col) { 
94                   int i = rowOff + col; 
95                   int j = col * 3; 
96                   ppmPixels[j] = 
97                   (byte) ((rgbPixels[i] & 0xff0000) >> 
98                           16); 
99                   ppmPixels[j + 1] = 
100                  (byte) ((rgbPixels[i] & 0x00ff00) >> 
101                          8); 
102                  ppmPixels[j + 2] = 
103                  (byte) (rgbPixels[i] & 0x0000ff); 
104              } 
105              out.write(ppmPixels); 
106          } 
107      } 
108   
109      public void encodeDone() throws IOException { 
110          // Nothing. 
111      } 
112   
113  } 
114