us.zuercher.gpx2map.image.netpbm
Class AbstractNetpbmImage

java.lang.Object
  extended by us.zuercher.gpx2map.image.netpbm.AbstractNetpbmImage
All Implemented Interfaces:
Image, NetpbmImage
Direct Known Subclasses:
PamImage, PpmImage

abstract class AbstractNetpbmImage
extends Object
implements NetpbmImage

AbstractNetpbmImage Provides basic operations that apply to all Netpbm format images.

Author:
Stephan Zuercher

Field Summary
protected  StringBuffer header
          Exact contents of the image's header.
protected  int height
          Height derived from image header.
private  int[][][] imageData
          Cached image rasters, if any.
protected  InputStream input
          Image input stream.
private  int originalMaxVal
          Maximum pixel color value derived from image header.
protected  int width
          Width derived from image header.
 
Constructor Summary
AbstractNetpbmImage(InputStream input)
          Construct a new AbstractNetpbmImage.
 
Method Summary
protected  int adjustToMaxVal255(int value)
          Utility method to convert a given Netpbm value to a maxval of 255.
 void close()
          Closes the image file.
 String getHeader()
          Returns an exact copy of the image file's header.
 int getHeight()
          Returns the image's height.
 int getMaxVal()
          Returns the image's maximum pixel color value.
protected  int getOriginalBytesPerPixelPerColor()
          Returns the number of bytes per pixel per color: 1 or 2.
 int[][][] getRaster()
          Implements Image.getRaster() by repeatedly calling Image.getNextRasterLine().
 int getWidth()
          Returns the image's width.
(package private) abstract  void readHeader()
          Reads the Netpbm image's header information and populates the fields of this class.
protected  String readWord(boolean skipPostFieldWS)
          Reads a "word" from the Netpbm image header.
protected  void setOriginalMaxVal(int originalMaxVal)
           
protected  void skipWS(boolean crLfOnly)
          Skips whitespace on the input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface us.zuercher.gpx2map.image.Image
getDepth, getNextRasterLine, hasAlphaChannel
 

Field Detail

header

protected StringBuffer header
Exact contents of the image's header.


input

protected final InputStream input
Image input stream.


width

protected int width
Width derived from image header.


height

protected int height
Height derived from image header.


originalMaxVal

private int originalMaxVal
Maximum pixel color value derived from image header. 1-65535.


imageData

private int[][][] imageData
Cached image rasters, if any.

Constructor Detail

AbstractNetpbmImage

AbstractNetpbmImage(InputStream input)
Construct a new AbstractNetpbmImage.

Parameters:
input - image input stream
Method Detail

readHeader

abstract void readHeader()
                  throws IOException
Reads the Netpbm image's header information and populates the fields of this class.

Throws:
IOException - if there's an error reading the image stream or if the stream does not conform to the Netpbm format.

getHeader

public String getHeader()
                 throws IOException
Returns an exact copy of the image file's header. This is useful for writing a new copy of the image with header comments unchanged.

Returns:
the image header
Throws:
IOException - if the image header cannot be read or is otherwise invalid

getWidth

public int getWidth()
Returns the image's width.

Specified by:
getWidth in interface Image
Returns:
the image's width.

getHeight

public int getHeight()
Returns the image's height.

Specified by:
getHeight in interface Image
Returns:
the image's height.

getMaxVal

public int getMaxVal()
Returns the image's maximum pixel color value. Always 255.

Specified by:
getMaxVal in interface NetpbmImage
Returns:
the image's maximum pixel color value, 255.

getOriginalBytesPerPixelPerColor

protected int getOriginalBytesPerPixelPerColor()
Returns the number of bytes per pixel per color: 1 or 2.

Returns:
the number of bytes per pixel per color: 1 or 2.

close

public void close()
           throws IOException
Closes the image file.

Specified by:
close in interface Image
Throws:
IOException - if there's an error closing the file

getRaster

public int[][][] getRaster()
                    throws IOException
Implements Image.getRaster() by repeatedly calling Image.getNextRasterLine().

Specified by:
getRaster in interface Image
Returns:
an array of image rasters for this image, as specified by Image.getRaster().
Throws:
IOException - if there's an error reading or closing the image.

readWord

protected String readWord(boolean skipPostFieldWS)
                   throws IOException
Reads a "word" from the Netpbm image header. Assumes that the first character read from the input stream is the first character of the word or of a header comment. If asked, skips whitespace following the word to prepare for the next call to this method.

Parameters:
skipPostFieldWS - skip whitespace after the word is read
Returns:
the word read
Throws:
IOException - if there's an error reading the file

skipWS

protected void skipWS(boolean crLfOnly)
               throws IOException
Skips whitespace on the input stream.

Parameters:
crLfOnly - if true, only skips carriage return or linefeed characters.
Throws:
IOException - if there's an error reading the file

setOriginalMaxVal

protected void setOriginalMaxVal(int originalMaxVal)

adjustToMaxVal255

protected int adjustToMaxVal255(int value)
Utility method to convert a given Netpbm value to a maxval of 255. Uses originalMaxVal as the basis for conversion.

Parameters:
value - value to convert
Returns:
value scaled to max val 255