Source code: non_com/media/jai/codec/PNMCodec.java
1 /*
2 * Copyright (c) 2001 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * -Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * -Redistribution in binary form must reproduct the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
15 * be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * This software is provided "AS IS," without a warranty of any kind. ALL
19 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
20 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
21 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
22 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
23 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
24 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
25 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
26 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
27 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
29 *
30 * You acknowledge that Software is not designed,licensed or intended for use in
31 * the design, construction, operation or maintenance of any nuclear facility.
32 */
33 package non_com.media.jai.codec;
34
35 import non_com.media.jai.codec.ForwardSeekableStream;
36 import non_com.media.jai.codec.ImageCodec;
37 import non_com.media.jai.codec.ImageDecodeParam;
38 import non_com.media.jai.codec.ImageDecoder;
39 import non_com.media.jai.codec.ImageEncodeParam;
40 import non_com.media.jai.codec.ImageEncoder;
41 import non_com.media.jai.codec.PNMEncodeParam;
42 import non_com.media.jai.codec.SeekableStream;
43 import java.awt.image.DataBuffer;
44 import java.awt.image.RenderedImage;
45 import java.awt.image.SampleModel;
46 import java.io.BufferedInputStream;
47 import java.io.IOException;
48 import java.io.InputStream;
49 import java.io.OutputStream;
50
51 /**
52 * A subclass of <code>ImageCodec</code> that handles the PNM family of formats
53 * (PBM, PGM, PPM). <p>
54 *
55 * The PBM format encodes a single-banded, 1-bit image. The PGM format encodes
56 * a single-banded image of any bit depth between 1 and 32. The PPM format
57 * encodes three-banded images of any bit depth between 1 and 32. All formats
58 * have an ASCII and a raw representation.
59 */
60 public final class PNMCodec extends ImageCodec {
61
62 /**
63 * Constructs an instance of <code>PNMCodec</code>.
64 */
65 public PNMCodec() {
66 }
67
68
69 /**
70 * Returns the name of the format handled by this codec.
71 *
72 * @return The FormatName value
73 */
74 public String getFormatName() {
75 return "pnm";
76 }
77
78
79 /**
80 * Gets the EncodeParamClass attribute of the PNMCodec object
81 *
82 * @return The EncodeParamClass value
83 */
84 public Class getEncodeParamClass() {
85 return PNMEncodeParam.class;
86 }
87
88
89 /**
90 * Gets the DecodeParamClass attribute of the PNMCodec object
91 *
92 * @return The DecodeParamClass value
93 */
94 public Class getDecodeParamClass() {
95 return Object.class;
96 }
97
98
99 /**
100 * Returns the number of bytes from the beginning of the data required to
101 * recognize it as being in PNM format.
102 *
103 * @return The NumHeaderBytes value
104 */
105 public int getNumHeaderBytes() {
106 return 2;
107 }
108
109
110 /**
111 * Returns <code>true</code> if the header bytes indicate PNM format.
112 *
113 * @param header an array of bytes containing the initial bytes of the input
114 * data.
115 * @return The FormatRecognized value
116 */
117 public boolean isFormatRecognized(byte[] header) {
118 return ((header[0] == 'P') &&
119 (header[1] >= '1') &&
120 (header[1] <= '6'));
121 }
122
123
124 /**
125 * Description of the Method
126 *
127 * @param im Description of Parameter
128 * @param param Description of Parameter
129 * @return Description of the Returned Value
130 */
131 public boolean canEncodeImage(RenderedImage im,
132 ImageEncodeParam param) {
133 SampleModel sampleModel = im.getSampleModel();
134
135 int dataType = sampleModel.getTransferType();
136 if ((dataType == DataBuffer.TYPE_FLOAT) ||
137 (dataType == DataBuffer.TYPE_DOUBLE)) {
138 return false;
139 }
140
141 int numBands = sampleModel.getNumBands();
142 if (numBands != 1 && numBands != 3) {
143 return false;
144 }
145
146 return true;
147 }
148
149
150 /**
151 * Instantiates a <code>PNMImageEncoder</code> to write to the given <code>OutputStream</code>
152 * .
153 *
154 * @param dst the <code>OutputStream</code> to write to.
155 * @param param an instance of <code>PNMEncodeParam</code> used to control
156 * the encoding process, or <code>null</code>. A <code>ClassCastException</code>
157 * will be thrown if <code>param</code> is non-null but not an instance
158 * of <code>PNMEncodeParam</code>.
159 * @return Description of the Returned Value
160 */
161 protected ImageEncoder createImageEncoder(OutputStream dst,
162 ImageEncodeParam param) {
163 PNMEncodeParam p = null;
164 if (param != null) {
165 p = (PNMEncodeParam) param;
166 // May throw a ClassCast exception
167 }
168
169 return new PNMImageEncoder(dst, p);
170 }
171
172
173 /**
174 * Instantiates a <code>PNMImageDecoder</code> to read from the given <code>InputStream</code>
175 * . <p>
176 *
177 * By overriding this method, <code>PNMCodec</code> is able to ensure that a
178 * <code>ForwardSeekableStream</code> is used to wrap the source <code>InputStream</code>
179 * instead of the a general (and more expensive) subclass of <code>SeekableStream</code>
180 * . Since the PNM decoder does not require the ability to seek backwards in
181 * its input, this allows for greater efficiency.
182 *
183 * @param src the <code>InputStream</code> to read from.
184 * @param param an instance of <code>ImageDecodeParam</code> used to control
185 * the decoding process, or <code>null</code>. This parameter is ignored
186 * by this class.
187 * @return Description of the Returned Value
188 */
189 protected ImageDecoder createImageDecoder(InputStream src,
190 ImageDecodeParam param) {
191 // Add buffering for efficiency
192 if (!(src instanceof BufferedInputStream)) {
193 src = new BufferedInputStream(src);
194 }
195 return new PNMImageDecoder(new ForwardSeekableStream(src), null);
196 }
197
198
199 /**
200 * Instantiates a <code>PNMImageDecoder</code> to read from the given <code>SeekableStream</code>
201 * .
202 *
203 * @param src the <code>SeekableStream</code> to read from.
204 * @param param an instance of <code>ImageDecodeParam</code> used to control
205 * the decoding process, or <code>null</code>. This parameter is ignored
206 * by this class.
207 * @return Description of the Returned Value
208 */
209 protected ImageDecoder createImageDecoder(SeekableStream src,
210 ImageDecodeParam param) {
211 return new PNMImageDecoder(src, null);
212 }
213 }