ewe.fx
Class PNGEncoder

java.lang.Object
  extended byewe.fx.PNGEncoder

public class PNGEncoder
extends Object

This class can be used to save a PNG encoded image. You can use a single method, writeImage() to save an existing image to a stream. You can also use it to save more data by calling the individual methods in the correct sequence.

In general you will call one of the start() methods, then writeHeader() to write the important information. You can then call the writeChunk() methods to write any extra chunks you want. Then call startDataChunk() to begin sending data. You can then call writeScanLine() to send out as many image scan lines as you want. Then call endDataChunk() to end and write a data chunk.

When you have written all the data chunks you want (you can send a single data chunk if you wish - but this takes up more memory) you then call writeEnd() to write the end of the PNG image and then closeOutput() if you want to close the stream as well.


Field Summary
protected  ByteArrayOutputStream curStream
           
protected  DeflaterOutputStream dos
           
protected  OutputStream output
           
protected static int[] signature
           
static int TYPE_GRAY_SCALE_SCALE
           
static int TYPE_GRAY_SCALE_SCALE_ALPHA
           
static int TYPE_PALETTE
           
static int TYPE_TRUE_COLOR
           
static int TYPE_TRUE_COLOR_ALPHA
           
 
Constructor Summary
PNGEncoder()
           
 
Method Summary
 void closeOutput()
          Closes the underlying output stream.
 void end()
          This ends the current data chunk, writes the end and closes the output.
 void endDataChunk()
          Closes the current data chunk and writes the compressed data for the current chunk.
 void start(Stream out, int width, int height, boolean useAlpha)
          This starts a true color PNG.
 void start(Stream out, int width, int height, int bitDepth, int type)
          This writes the signature and header, starts the data chunks and is ready for scan lines.
 void startDataChunk()
          This starts a chunk of scan line data using the default compression.
 void startDataChunk(int compressionLevel)
          This starts a chunk of scan line data using the specified compression leve.
 void startImage(Stream out, IImage image)
          Start the PNG file in preparation for output of an image.
 void startImage(Stream out, IImage image, boolean useAlpha)
          Start the PNG file in preparation for output of an image.
 void startOutput(OutputStream out)
           
 void startOutput(Stream os)
          This is the very basic start method.
protected  boolean usingAlpha()
           
protected  void writeByte(int value, OutputStream out, CRC32 crc)
           
 void writeChunk(String value, byte[] bytes)
          Write a PNG chunk to the image.
 void writeChunk(String value, byte[] bytes, int offset, int length)
          Write a PNG chunk to the image.
 void writeEnd()
          This writes the end chunk marking the end of the PNG file.
 void writeHeader(int width, int height, int bitDepth, int colorType)
          This writes the IHDR chunk, always using standard Deflator compression.
 void writeImage(IImage image)
          Use this after startImage() to write the actual image data.
 void writeImage(Stream out, IImage image)
          Write out an entire Image to the stream as a complete PNG image, as a TRUE_COLOR or TRUE_COLOR_ALPHA image.
 void writeImage(Stream out, IImage image, boolean useAlpha)
          This method will write out an entire image to the stream as a complete PNG image, as a TRUE_COLOR or TRUE_COLOR_ALPHA image.
protected  void writeInt(int value, OutputStream out, CRC32 crc)
           
 void writeScanLine(byte[] line, int offset, int length)
          Use this to write scan line data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

TYPE_GRAY_SCALE_SCALE

public static final int TYPE_GRAY_SCALE_SCALE
See Also:
Constant Field Values

TYPE_PALETTE

public static final int TYPE_PALETTE
See Also:
Constant Field Values

TYPE_GRAY_SCALE_SCALE_ALPHA

public static final int TYPE_GRAY_SCALE_SCALE_ALPHA
See Also:
Constant Field Values

TYPE_TRUE_COLOR

public static final int TYPE_TRUE_COLOR
See Also:
Constant Field Values

TYPE_TRUE_COLOR_ALPHA

public static final int TYPE_TRUE_COLOR_ALPHA
See Also:
Constant Field Values

output

protected OutputStream output

signature

protected static int[] signature

curStream

protected ByteArrayOutputStream curStream

dos

protected DeflaterOutputStream dos
Constructor Detail

PNGEncoder

public PNGEncoder()
Method Detail

usingAlpha

protected boolean usingAlpha()

writeInt

protected void writeInt(int value,
                        OutputStream out,
                        CRC32 crc)
                 throws IOException
Throws:
IOException

writeByte

protected void writeByte(int value,
                         OutputStream out,
                         CRC32 crc)
                  throws IOException
Throws:
IOException

writeChunk

public void writeChunk(String value,
                       byte[] bytes)
                throws IOException
Write a PNG chunk to the image.

Parameters:
value - The 4-character code for the chunk.
bytes - The bytes in the chunk to write (not including the length). This can be null, indicating no data.
Throws:
IOException - if an error occurs writing to the Stream.

writeChunk

public void writeChunk(String value,
                       byte[] bytes,
                       int offset,
                       int length)
                throws IOException
Write a PNG chunk to the image.

Parameters:
value - The 4-character code for the chunk.
bytes - The bytes in the chunk to write (not including the length). This can be null, indicating no data, in which case length must also be 0.
offset - The start location in the bytes.
length - The number of bytes.
Throws:
IOException - if an error occurs writing to the Stream.

startOutput

public void startOutput(Stream os)
                 throws IOException
This is the very basic start method. It simply writes the PNG signature and nothing else.

Throws:
IOException

startOutput

public void startOutput(OutputStream out)
                 throws IOException
Throws:
IOException

writeHeader

public void writeHeader(int width,
                        int height,
                        int bitDepth,
                        int colorType)
                 throws IOException
This writes the IHDR chunk, always using standard Deflator compression.

Throws:
IOException

writeEnd

public void writeEnd()
              throws IOException
This writes the end chunk marking the end of the PNG file. It does not close the stream.

Throws:
IOException

startDataChunk

public void startDataChunk()
                    throws IOException
This starts a chunk of scan line data using the default compression.

Throws:
IOException

startDataChunk

public void startDataChunk(int compressionLevel)
                    throws IOException
This starts a chunk of scan line data using the specified compression leve.

Throws:
IOException

writeScanLine

public void writeScanLine(byte[] line,
                          int offset,
                          int length)
                   throws IOException
Use this to write scan line data. This need not be a full line.

Throws:
IOException

endDataChunk

public void endDataChunk()
                  throws IOException
Closes the current data chunk and writes the compressed data for the current chunk.

Throws:
IOException

closeOutput

public void closeOutput()
                 throws IOException
Closes the underlying output stream. Make sure you end any open data chunks and call writeEnd() first.

Throws:
IOException

start

public void start(Stream out,
                  int width,
                  int height,
                  boolean useAlpha)
           throws IOException
This starts a true color PNG. This writes the signature and header, starts the data chunks and is ready for scan lines.

Throws:
IOException

start

public void start(Stream out,
                  int width,
                  int height,
                  int bitDepth,
                  int type)
           throws IOException
This writes the signature and header, starts the data chunks and is ready for scan lines.

Throws:
IOException

end

public void end()
         throws IOException
This ends the current data chunk, writes the end and closes the output.

Throws:
IOException

startImage

public void startImage(Stream out,
                       IImage image)
                throws IOException,
                       IllegalArgumentException
Start the PNG file in preparation for output of an image. The header will be written to the file but no data is written. At this point you can write optional chunks using writeChunk() and then you can use writeImage(IImage image) to write the image data, after which you must use writeEnd() and closeOutput() to end the file.

Parameters:
out - The output Stream. To write to a byte array use a new MemoryFile as the Stream.
image - The image to write.
Throws:
IOException - If there is an error writing the data.
IllegalArgumentException - If there is an error with the image.

startImage

public void startImage(Stream out,
                       IImage image,
                       boolean useAlpha)
                throws IOException,
                       IllegalArgumentException
Start the PNG file in preparation for output of an image. The header will be written to the file but no data is written. At this point you can write optional chunks using writeChunk() and then you can use writeImage(IImage image) to write the image data, after which you must use writeEnd() and closeOutput() to end the file.

Parameters:
out - The output Stream. To write to a byte array use a new MemoryFile as the Stream.
image - The image to write.
Throws:
IOException - If there is an error writing the data.
IllegalArgumentException - If there is an error with the image.

writeImage

public void writeImage(IImage image)
                throws IOException,
                       IllegalArgumentException
Use this after startImage() to write the actual image data. After calling this you can write additional chunks using writeChunk(). When complete you must call writeEnd() and closeOutput().

Parameters:
image - The image to write.
Throws:
IOException - If there is an error writing the data.
IllegalArgumentException - If there is an error with the image.

writeImage

public void writeImage(Stream out,
                       IImage image)
                throws IOException,
                       IllegalArgumentException
Write out an entire Image to the stream as a complete PNG image, as a TRUE_COLOR or TRUE_COLOR_ALPHA image. It will not close the output stream.

Parameters:
out - The output Stream. To write to a byte array use a new MemoryFile as the Stream.
image - The image to write.
Throws:
IOException - If there is an error writing the data.
IllegalArgumentException - If there is an error with the image.

writeImage

public void writeImage(Stream out,
                       IImage image,
                       boolean useAlpha)
                throws IOException,
                       IllegalArgumentException
This method will write out an entire image to the stream as a complete PNG image, as a TRUE_COLOR or TRUE_COLOR_ALPHA image. It will not close the output stream.

Parameters:
out - The output Stream. To write to a byte array use a new MemoryFile as the Stream.
image - The image to write.
Throws:
IOException - If there is an error writing the data.
IllegalArgumentException - If there is an error with the image.