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