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

Quick Search    Search Deep

Source code: com/memoire/acme/AcmePpmEncoder.java


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