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 and Description |
---|---|
private java.lang.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 java.lang.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 java.io.PushbackInputStream |
in |
private int |
maxSample |
private java.io.DataOutput |
out |
private int |
width |
Constructor and Description |
---|
PNMCodec() |
Modifier and Type | Method and Description |
---|---|
static int |
determineImageTypeFromFileName(java.lang.String fileName)
Attempts to find the appropriate image type by looking at a file's name.
|
java.lang.Boolean |
getAscii()
Returns if ASCII mode was used for loading an image or will
be used to store an image.
|
java.lang.String |
getFormatName()
Returns the name of the file format supported by this codec.
|
java.lang.String[] |
getMimeTypes()
Return the MIME
(Multipurpose Internet Mail Extensions) type strings for this format, or
null
if none are available. |
static java.lang.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 java.lang.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(java.lang.String line) |
private void |
setResolution(java.lang.String line) |
java.lang.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, setRandomAccessFile
addProgressListener, addProgressListeners, getAbort, removeProgressListener, setAbort, setProgress, setProgress
public 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 java.lang.String[] IMAGE_TYPE_FILE_EXTENSIONS
private java.lang.Boolean ascii
private int columns
private int imageType
private java.io.PushbackInputStream in
private java.io.DataOutput out
private int height
private int maxSample
private int width
public static int determineImageTypeFromFileName(java.lang.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 java.lang.Boolean getAscii()
setAscii(boolean)
public java.lang.String getFormatName()
ImageCodec
ImageCodec
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 ImageCodec
public java.lang.String[] getMimeTypes()
ImageCodec
null
if none are available.getMimeTypes
in class ImageCodec
public static java.lang.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()
ImageCodec
true
is returned this does not necessarily mean that all files in this
format can be read, but at least some.isLoadingSupported
in class ImageCodec
public boolean isSavingSupported()
ImageCodec
true
is returned this does not necessarily mean that all types files in this
format can be written, but at least some.isSavingSupported
in class ImageCodec
private void load() throws InvalidFileStructureException, java.io.IOException, MissingParameterException, UnsupportedTypeException, WrongFileFormatException, WrongParameterException
ImageCodec.setInputStream(InputStream)
.InvalidFileStructureException
- if the input stream is not a valid PNM stream (or unsupported)java.io.IOException
- if there were problems reading from the input streamMissingParameterException
UnsupportedTypeException
WrongFileFormatException
WrongParameterException
private int loadAsciiNumber() throws InvalidFileStructureException, java.io.IOException
InvalidFileStructureException
java.io.IOException
private void loadBilevelImage() throws InvalidFileStructureException, java.io.IOException, WrongParameterException
InvalidFileStructureException
java.io.IOException
WrongParameterException
private void loadBilevelImageAscii() throws InvalidFileStructureException, java.io.IOException
InvalidFileStructureException
java.io.IOException
private void loadBilevelImageBinary() throws InvalidFileStructureException, java.io.IOException
InvalidFileStructureException
java.io.IOException
private int read16BitBinaryValue() throws java.io.IOException
java.io.IOException
- If the underlying read operation failed.private void loadColorImage() throws InvalidFileStructureException, java.io.IOException
InvalidFileStructureException
java.io.IOException
private void loadGrayImage() throws InvalidFileStructureException, java.io.IOException, UnsupportedTypeException
InvalidFileStructureException
java.io.IOException
UnsupportedTypeException
private java.lang.String loadTextLine() throws InvalidFileStructureException, java.io.IOException
InvalidFileStructureException
java.io.IOException
private void loadType() throws InvalidFileStructureException, java.io.IOException, WrongFileFormatException
WrongFileFormatException
- if the input stream is not a PNM streamInvalidFileStructureException
- if the format that
is described above was not encounteredjava.io.IOException
- if there were errors reading datajava.lang.IllegalArgumentException
- if the input stream was not given to this codecpublic void process() throws MissingParameterException, OperationFailedException
Operation
process
in class Operation
MissingParameterException
- 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.)OperationFailedException
private void save() throws java.io.IOException, MissingParameterException, WrongParameterException
java.io.IOException
MissingParameterException
WrongParameterException
private void save(BilevelImage image) throws java.io.IOException
java.io.IOException
private void save(Gray8Image image) throws java.io.IOException
java.io.IOException
private void save(Gray16Image image) throws java.io.IOException
java.io.IOException
private void save(RGB24Image image) throws java.io.IOException
java.io.IOException
private void save(RGB48Image image) throws java.io.IOException
java.io.IOException
private void saveAsciiNumber(int number) throws java.io.IOException
java.io.IOException
private void saveHeader() throws java.io.IOException
java.io.IOException
public void setAscii(boolean asciiMode)
asciiMode
- if true, ASCII mode is used, binary mode otherwiseprivate void setMaximumSample(java.lang.String line) throws InvalidFileStructureException
InvalidFileStructureException
private void setResolution(java.lang.String line) throws InvalidFileStructureException
InvalidFileStructureException
public java.lang.String suggestFileExtension(PixelImage image)
ImageCodec
PNMCodec
).
This default implementation always returns null
.suggestFileExtension
in class ImageCodec
image
- the image that is to be written to a filenull
if no file extension can be recommended