package com.fr.third.JAI;
/*
* Copyright (c) 2001 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduct the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that Software is not designed,licensed or intended for use in
* the design, construction, operation or maintenance of any nuclear facility.
*/
import java.awt.image.DataBuffer;
import java.awt.image.RenderedImage;
import java.awt.image.SampleModel;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* A subclass of ImageCodec
that handles the
* PNM family of formats (PBM, PGM, PPM).
*
*
The PBM format encodes a single-banded, 1-bit image. The PGM
* format encodes a single-banded image of any bit depth between 1 and
* 32. The PPM format encodes three-banded images of any bit depth
* between 1 and 32. All formats have an ASCII and a raw
* representation.
*
*/
public final class PNMCodec extends ImageCodec {
/** Constructs an instance of PNMCodec
. */
public PNMCodec() {}
/** Returns the name of the format handled by this codec. */
public String getFormatName() {
return "pnm";
}
public Class getEncodeParamClass() {
return PNMEncodeParam.class;
}
public Class getDecodeParamClass() {
return Object.class;
}
public boolean canEncodeImage(RenderedImage im,
ImageEncodeParam param) {
SampleModel sampleModel = im.getSampleModel();
int dataType = sampleModel.getTransferType();
if ((dataType == DataBuffer.TYPE_FLOAT) ||
(dataType == DataBuffer.TYPE_DOUBLE)) {
return false;
}
int numBands = sampleModel.getNumBands();
if (numBands != 1 && numBands != 3) {
return false;
}
return true;
}
/**
* Instantiates a PNMImageEncoder
to write to the
* given OutputStream
.
*
* @param dst the OutputStream
to write to.
* @param param an instance of PNMEncodeParam
used to
* control the encoding process, or null
. A
* ClassCastException
will be thrown if
* param
is non-null but not an instance of
* PNMEncodeParam
.
*/
protected ImageEncoder createImageEncoder(OutputStream dst,
ImageEncodeParam param) {
PNMEncodeParam p = null;
if (param != null) {
p = (PNMEncodeParam)param; // May throw a ClassCast exception
}
return new PNMImageEncoder(dst, p);
}
/**
* Instantiates a PNMImageDecoder
to read from the
* given InputStream
.
*
*
By overriding this method, PNMCodec
is able to
* ensure that a ForwardSeekableStream
is used to
* wrap the source InputStream
instead of the a
* general (and more expensive) subclass of
* SeekableStream
. Since the PNM decoder does not
* require the ability to seek backwards in its input, this allows
* for greater efficiency.
*
* @param src the InputStream
to read from.
* @param param an instance of ImageDecodeParam
used to
* control the decoding process, or null
.
* This parameter is ignored by this class.
*/
protected ImageDecoder createImageDecoder(InputStream src,
ImageDecodeParam param) {
// Add buffering for efficiency
if (!(src instanceof BufferedInputStream)) {
src = new BufferedInputStream(src);
}
return new PNMImageDecoder(new ForwardSeekableStream(src), null);
}
/**
* Instantiates a PNMImageDecoder
to read from the
* given SeekableStream
.
*
* @param src the SeekableStream
to read from.
* @param param an instance of ImageDecodeParam
used to
* control the decoding process, or null
.
* This parameter is ignored by this class.
*/
protected ImageDecoder createImageDecoder(SeekableStream src,
ImageDecodeParam param) {
return new PNMImageDecoder(src, null);
}
/**
* Returns the number of bytes from the beginning of the data required
* to recognize it as being in PNM format.
*/
public int getNumHeaderBytes() {
return 2;
}
/**
* Returns true
if the header bytes indicate PNM format.
*
* @param header an array of bytes containing the initial bytes of the
* input data. */
public boolean isFormatRecognized(byte[] header) {
return ((header[0] == 'P') &&
(header[1] >= '1') &&
(header[1] <= '6'));
}
}