us.zuercher.gpx2map.image.netpbm
Class WritablePpmImage

java.lang.Object
  extended by us.zuercher.gpx2map.image.netpbm.WritablePpmImage

public class WritablePpmImage
extends Object

WritablePpmImage is a write-only representation of a PPM image. It writes only the binary version of the PPM format, with optional gzip compression.

Author:
Stephan Zuercher

Field Summary
private  int bytesPerPixelPerColor
          Image bytes per pixel color.
private  boolean gzip
          Compression flag.
private  StringBuffer header
          Header buffer typically copied from another image.
private  int height
          Image height.
private static String MAGIC
          PPM magic token.
private  int maxVal
          Image maximum pixel color value.
private static String NL
          PPM header line separator.
private  OutputStream output
          Output stream.
private  File ppmImageFile
          PPM file location.
private  int rastersWritten
          Count of rastersWritten (compared to height).
private  int width
          Image width.
 
Constructor Summary
WritablePpmImage(File ppmImageFile, int width, int height, int maxVal, boolean gzip)
          Constructs a new WritablePpmImage with the given header information.
WritablePpmImage(File ppmImageFile, PpmImage headerSource, boolean gzip)
          Constructs a new WritablePpmImage, copying the image header from a PpmImage.
 
Method Summary
 void close()
          Closes the image file.
private  void writeHeader()
          Writes the PPM header.
 void writeRaster(int[][] raster)
          Write a raster line to the image file.
 void writeRaster(int[][] raster, int offset)
          Write a raster line to the image file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAGIC

private static final String MAGIC
PPM magic token.

See Also:
Constant Field Values

NL

private static final String NL
PPM header line separator.

See Also:
Constant Field Values

ppmImageFile

private final File ppmImageFile
PPM file location.


header

private final StringBuffer header
Header buffer typically copied from another image.


gzip

private final boolean gzip
Compression flag.


width

private final int width
Image width.


height

private final int height
Image height.


maxVal

private final int maxVal
Image maximum pixel color value. 1-65535.


bytesPerPixelPerColor

private final int bytesPerPixelPerColor
Image bytes per pixel color. 1 or 2.


output

private OutputStream output
Output stream.


rastersWritten

private int rastersWritten
Count of rastersWritten (compared to height).

Constructor Detail

WritablePpmImage

public WritablePpmImage(File ppmImageFile,
                        PpmImage headerSource,
                        boolean gzip)
                 throws IOException
Constructs a new WritablePpmImage, copying the image header from a PpmImage.

Parameters:
ppmImageFile - output file location
headerSource - source of header information
gzip - compression flag
Throws:
IOException - if the header source's header data cannot be read

WritablePpmImage

public WritablePpmImage(File ppmImageFile,
                        int width,
                        int height,
                        int maxVal,
                        boolean gzip)
Constructs a new WritablePpmImage with the given header information.

Parameters:
ppmImageFile - output file location
width - image width in pixels
height - image height in pixels
maxVal - maximum pixel color value (255 or 65535)
gzip - compression flag
Method Detail

writeRaster

public void writeRaster(int[][] raster)
                 throws IOException
Write a raster line to the image file. Each pixel is comprised of 3 integers: red, green, blue. Extra integers (e.g. alpha channels from a PAM image) are silently ignored. If the raster is wider than necessary, extra data is ignored.

Parameters:
raster - an array of pixel data of size int[width][3].
Throws:
IOException - if there's an error writing to the file
IllegalArgumentException - if more than height raster are written, or if the given raster line has the wrong width

writeRaster

public void writeRaster(int[][] raster,
                        int offset)
                 throws IOException
Write a raster line to the image file. Each pixel is comprised of 3 integers: red, green, blue. Extra integers (e.g. alpha channels from a PAM image) are silently ignored.

Parameters:
raster - an array of pixel data of size int[][3].
offset - offset into raster array
Throws:
IOException - if there's an error writing to the file
IllegalArgumentException - if more than height raster are written, or if the raster does not contain enough data to write a full width raster line (e.g. raster.length - offset < width)

close

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

Throws:
IOException - if there's an error flushing or closing the file
IllegalStateException - if height rasters have not been written

writeHeader

private void writeHeader()
                  throws IOException
Writes the PPM header.

Throws:
IOException - if there's an error writing to the file.