Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: Acme/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  package Acme.JPM.Encoders;
29  
30  import java.awt.Image;
31  import java.awt.image.*;
32  
33  import java.io.*;
34  
35  import java.util.*;
36  
37  
38  /**
39   * Write out an image as a PPM.
40   * <P>
41   * Writes an image onto a specified OutputStream in the PPM file format.
42   * <P>
43   * <A HREF="/resources/classes/Acme/JPM/Encoders/PpmEncoder.java">Fetch the software.</A><BR>
44   * <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
45   * <P>
46   * @see ToPpm
47   * @cvsversion    $Revision: 1.2 $, $Date: 2003/10/16 08:49:27 $
48   */
49  public class PpmEncoder extends ImageEncoder
50  {
51      //~ Constructors ///////////////////////////////////////////////////////////
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      {
58          super(img, out);
59      }
60  
61      /// Constructor.
62      // @param prod The ImageProducer to encode.
63      // @param out The stream to write the PPM to.
64      public PpmEncoder(ImageProducer prod, OutputStream out)
65          throws IOException
66      {
67          super(prod, out);
68      }
69  
70      //~ Methods ////////////////////////////////////////////////////////////////
71  
72      void encodeStart(int width, int height) throws IOException
73      {
74          writeString(out, "P6\n");
75          writeString(out, width + " " + height + "\n");
76          writeString(out, "255\n");
77      }
78  
79      static void writeString(OutputStream out, String str)
80          throws IOException
81      {
82          byte[] buf = str.getBytes();
83          out.write(buf);
84      }
85  
86      void encodeDone() throws IOException
87      {
88          // Nothing.
89      }
90  
91      void encodePixels(int x, int y, int w, int h, int[] rgbPixels, int off,
92          int scansize) throws IOException
93      {
94          byte[] ppmPixels = new byte[w * 3];
95  
96          for (int row = 0; row < h; ++row)
97          {
98              int rowOff = off + (row * scansize);
99  
100             for (int col = 0; col < w; ++col)
101             {
102                 int i = rowOff + col;
103                 int j = col * 3;
104                 ppmPixels[j] = (byte) ((rgbPixels[i] & 0xff0000) >> 16);
105                 ppmPixels[j + 1] = (byte) ((rgbPixels[i] & 0x00ff00) >> 8);
106                 ppmPixels[j + 2] = (byte) (rgbPixels[i] & 0x0000ff);
107             }
108 
109             out.write(ppmPixels);
110         }
111     }
112 }
113 ///////////////////////////////////////////////////////////////////////////////
114 //  END OF FILE.
115 ///////////////////////////////////////////////////////////////////////////////