public class PNMCodec extends ImageCodec
The header of a PGM and PPM file stores a maximum sample value (such a value is not stored for PBM, where the maximum value is always 1). When in binary mode, PGM and PPM typically have a maximum sample value of 255, which makes PGM 8 bits per pixel and PPM 24 bits per pixel large. One sample will be stored as a single byte. However, there also exist binary PGM files with a maximum sample value larger than 255 and smaller than 65536. These files use two bytes per sample, in network byte order (big endian). I have yet to see PPM files with that property, but they are of course imagineable. 16 bpp
PNMCodec codec = new PNMCodec();
codec.setFile("test.ppm", CodecMode.LOAD);
codec.process();
codec.close();
PixelImage image = codec.getImage();
PNMCodec codec = new PNMCodec();
BilevelImage myFax = ...; // initialize
codec.setImage(myFax);
codec.setFile("out.pbm", CodecMode.SAVE);
codec.process();
codec.close();
| Modifier and Type | Field | Description |
|---|---|---|
private Boolean |
ascii |
|
private int |
columns |
|
private int |
height |
|
static int |
IMAGE_TYPE_BILEVEL |
Image type constant for bilevel images, stored in PBM files.
|
static int |
IMAGE_TYPE_COLOR |
Image type constant for RGB truecolor images, stored in PPM files.
|
private static String[] |
IMAGE_TYPE_FILE_EXTENSIONS |
|
static int |
IMAGE_TYPE_GRAY |
Image type constant for grayscale images, stored in PGM files.
|
static int |
IMAGE_TYPE_UNKNOWN |
Image type constant for images of unknown type.
|
private int |
imageType |
|
private PushbackInputStream |
in |
|
private int |
maxSample |
|
private DataOutput |
out |
|
private int |
width |
| Constructor | Description |
|---|---|
PNMCodec() |
| Modifier and Type | Method | Description |
|---|---|---|
static int |
determineImageTypeFromFileName(String fileName) |
Attempts to find the appropriate image type by looking at a file's name.
|
Boolean |
getAscii() |
Returns if ASCII mode was used for loading an image or will
be used to store an image.
|
String |
getFormatName() |
Returns the name of the file format supported by this codec.
|
String[] |
getMimeTypes() |
Return the MIME
(Multipurpose Internet Mail Extensions) type strings for this format, or
null
if none are available. |
static String |
getTypicalFileExtension(int imageType) |
Returns the typical file extension (including leading dot) for an
image type.
|
boolean |
isLoadingSupported() |
Returns if this codec is able to load images in the file format supported by this codec.
|
boolean |
isSavingSupported() |
Returns if this codec is able to save images in the file format supported by this codec.
|
private void |
load() |
Loads an image from a PNM input stream.
|
private int |
loadAsciiNumber() |
|
private void |
loadBilevelImage() |
|
private void |
loadBilevelImageAscii() |
|
private void |
loadBilevelImageBinary() |
|
private void |
loadColorImage() |
|
private void |
loadGrayImage() |
|
private String |
loadTextLine() |
|
private void |
loadType() |
Loads the first two characters (which are expected to be a capital P
followed by a decimal digit between 1 and 6, inclusively) and skips
following LF and CR characters.
|
void |
process() |
This method does the actual work of the operation.
|
private int |
read16BitBinaryValue() |
Read a 16-bit binary value in network (big-endian) order.
|
private void |
save() |
|
private void |
save(BilevelImage image) |
|
private void |
save(Gray16Image image) |
|
private void |
save(Gray8Image image) |
|
private void |
save(RGB24Image image) |
|
private void |
save(RGB48Image image) |
|
private void |
saveAsciiNumber(int number) |
|
private void |
saveHeader() |
|
void |
setAscii(boolean asciiMode) |
Specify whether ASCII mode is to be used when saving an image.
|
private void |
setMaximumSample(String line) |
|
private void |
setResolution(String line) |
|
String |
suggestFileExtension(PixelImage image) |
Attempts to suggest a filename extension.
|
appendComment, checkBounds, checkImageResolution, close, getBoundsHeight, getBoundsWidth, getBoundsX1, getBoundsX2, getBoundsY1, getBoundsY2, getComment, getDataInput, getDataOutput, getDpiX, getDpiY, getFileExtensions, getImage, getImageIndex, getInputAsDataInput, getInputStream, getMode, getNumComments, getOutputAsDataOutput, getOutputStream, getRandomAccessFile, hasBounds, initModeFromIOObjects, isRowRequired, isTileRequired, removeAllComments, removeBounds, setBounds, setBoundsIfNecessary, setDataInput, setDataOutput, setDpi, setFile, setFile, setImage, setImageIndex, setInputStream, setOutputStream, setRandomAccessFileclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitaddProgressListener, addProgressListeners, getAbort, removeProgressListener, setAbort, setProgress, setProgresspublic static final int IMAGE_TYPE_UNKNOWN
public static final int IMAGE_TYPE_BILEVEL
public static final int IMAGE_TYPE_GRAY
public static final int IMAGE_TYPE_COLOR
private static final String[] IMAGE_TYPE_FILE_EXTENSIONS
private Boolean ascii
private int columns
private int imageType
private PushbackInputStream in
private DataOutput out
private int height
private int maxSample
private int width
public static int determineImageTypeFromFileName(String fileName)
IMAGE_TYPE_BILEVEL for .pbm,
IMAGE_TYPE_GRAY for .pgm and
IMAGE_TYPE_COLOR for .ppm.
Otherwise, IMAGE_TYPE_UNKNOWN is returned.
To get a file extension given that you have an image type, use
getTypicalFileExtension(int).fileName - the file name to be examinedIMAGE_TYPE_xxx constants of this classpublic Boolean getAscii()
setAscii(boolean)public String getFormatName()
ImageCodecImageCodec must override this method.
When overriding, leave out any words in a particular language so
that this format name can be understood by everyone.
Usually it is enough to return the format creator plus a typical
abbreviation, e.g. Microsoft BMP or Portable Anymap (PNM).getFormatName in class ImageCodecpublic String[] getMimeTypes()
ImageCodecnull
if none are available.getMimeTypes in class ImageCodecpublic static String getTypicalFileExtension(int imageType)
null for IMAGE_TYPE_UNKNOWN.
To get the image type given that you have a file name, use
determineImageTypeFromFileName(java.lang.String).imageType - the image type for which the extension is requiredpublic boolean isLoadingSupported()
ImageCodectrue is returned this does not necessarily mean that all files in this
format can be read, but at least some.isLoadingSupported in class ImageCodecpublic boolean isSavingSupported()
ImageCodectrue is returned this does not necessarily mean that all types files in this
format can be written, but at least some.isSavingSupported in class ImageCodecprivate void load()
throws InvalidFileStructureException,
IOException,
MissingParameterException,
UnsupportedTypeException,
WrongFileFormatException,
WrongParameterException
ImageCodec.setInputStream(InputStream).InvalidFileStructureException - if the input stream is not a valid PNM stream (or unsupported)IOException - if there were problems reading from the input streamMissingParameterExceptionUnsupportedTypeExceptionWrongFileFormatExceptionWrongParameterExceptionprivate int loadAsciiNumber()
throws InvalidFileStructureException,
IOException
private void loadBilevelImage()
throws InvalidFileStructureException,
IOException,
WrongParameterException
private void loadBilevelImageAscii()
throws InvalidFileStructureException,
IOException
private void loadBilevelImageBinary()
throws InvalidFileStructureException,
IOException
private int read16BitBinaryValue()
throws IOException
IOException - If the underlying read operation failed.private void loadColorImage()
throws InvalidFileStructureException,
IOException
private void loadGrayImage()
throws InvalidFileStructureException,
IOException,
UnsupportedTypeException
private String loadTextLine() throws InvalidFileStructureException, IOException
private void loadType()
throws InvalidFileStructureException,
IOException,
WrongFileFormatException
WrongFileFormatException - if the input stream is not a PNM streamInvalidFileStructureException - if the format that
is described above was not encounteredIOException - if there were errors reading dataIllegalArgumentException - if the input stream was not given to this codecpublic void process()
throws MissingParameterException,
OperationFailedException
Operationprocess in class OperationMissingParameterException - if any mandatory parameter was not given to the operationWrongParameterException - if at least one of the input parameters was
not initialized appropriately (values out of the valid interval, etc.)OperationFailedExceptionprivate void save()
throws IOException,
MissingParameterException,
WrongParameterException
private void save(BilevelImage image) throws IOException
IOExceptionprivate void save(Gray8Image image) throws IOException
IOExceptionprivate void save(Gray16Image image) throws IOException
IOExceptionprivate void save(RGB24Image image) throws IOException
IOExceptionprivate void save(RGB48Image image) throws IOException
IOExceptionprivate void saveAsciiNumber(int number)
throws IOException
IOExceptionprivate void saveHeader()
throws IOException
IOExceptionpublic void setAscii(boolean asciiMode)
asciiMode - if true, ASCII mode is used, binary mode otherwiseprivate void setMaximumSample(String line) throws InvalidFileStructureException
InvalidFileStructureExceptionprivate void setResolution(String line) throws InvalidFileStructureException
InvalidFileStructureExceptionpublic String suggestFileExtension(PixelImage image)
ImageCodecPNMCodec).
This default implementation always returns null.suggestFileExtension in class ImageCodecimage - the image that is to be written to a filenull if no file extension can be recommended