/Users/lyon/j4p/src/ip/ppm/PpmEncoder.java

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