public class PNGEncoder extends Object implements PNGImageTypes
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.
Modifier and Type | Field and Description |
---|---|
protected ByteArrayOutputStream |
curStream |
protected DeflaterOutputStream |
dos |
protected OutputStream |
output |
protected static int[] |
signature |
PNG_TYPE_GRAY_SCALE, PNG_TYPE_GRAY_SCALE_ALPHA, PNG_TYPE_PALETTE, PNG_TYPE_TRUE_COLOR, PNG_TYPE_TRUE_COLOR_ALPHA
Constructor and Description |
---|
PNGEncoder() |
Modifier and Type | Method and Description |
---|---|
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(OutputStream out,
int width,
int height,
boolean useAlpha)
This starts a true color PNG.
|
void |
start(OutputStream 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(OutputStream out,
ImageData image)
Start the PNG file in preparation for output of an image.
|
void |
startImage(OutputStream out,
ImageData image,
boolean useAlpha)
Start the PNG file in preparation for output of an image.
|
void |
startOutput(OutputStream out)
This is the most basic way to start output.
|
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 imageDataImageType)
This writes the IHDR chunk, always using standard Deflator compression.
|
void |
writeHeader(int width,
int height,
int bitDepth,
int pngColorType)
This writes the IHDR chunk, always using standard Deflator compression.
|
void |
writeImage(ImageData image)
Use this after startImage() to write the actual image data.
|
void |
writeImage(OutputStream out,
ImageData 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(OutputStream out,
ImageData 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.
|
protected OutputStream output
protected static int[] signature
protected ByteArrayOutputStream curStream
protected DeflaterOutputStream dos
protected boolean usingAlpha()
protected void writeInt(int value, OutputStream out, CRC32 crc) throws IOException
IOException
protected void writeByte(int value, OutputStream out, CRC32 crc) throws IOException
IOException
public void writeChunk(String value, byte[] bytes) throws IOException
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.IOException
- if an error occurs writing to the Stream.public void writeChunk(String value, byte[] bytes, int offset, int length) throws IOException
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.IOException
- if an error occurs writing to the Stream.public void startOutput(OutputStream out) throws IOException
out
- the OutputStream to write to.IOException
- on an IO error.public void writeHeader(int width, int height, int bitDepth, int pngColorType) throws IOException
IOException
public void writeHeader(int width, int height, int imageDataImageType) throws IOException
IOException
public void writeEnd() throws IOException
IOException
public void startDataChunk() throws IOException
IOException
public void startDataChunk(int compressionLevel) throws IOException
compressionLevel
- one of the Deflater.XXX_COMPRESSION values.IOException
public void writeScanLine(byte[] line, int offset, int length) throws IOException
IOException
public void endDataChunk() throws IOException
IOException
public void closeOutput() throws IOException
IOException
public void start(OutputStream out, int width, int height, boolean useAlpha) throws IOException
IOException
public void start(OutputStream out, int width, int height, int bitDepth, int type) throws IOException
IOException
public void end() throws IOException
IOException
public void startImage(OutputStream out, ImageData image) throws IOException, IllegalArgumentException
out
- The output Stream. To write to a byte array use a new MemoryFile as the Stream.image
- The image to write.IOException
- If there is an error writing the data.IllegalArgumentException
- If there is an error with the image.public void startImage(OutputStream out, ImageData image, boolean useAlpha) throws IOException, IllegalArgumentException
out
- The output Stream. To write to a byte array use a new MemoryFile as the Stream.image
- The image to write.usesAlpha
- if this is true then an ALPHA channel will be saved in the image.IOException
- If there is an error writing the data.IllegalArgumentException
- If there is an error with the image.public void writeImage(ImageData image) throws IOException, IllegalArgumentException
image
- The image to write.IOException
- If there is an error writing the data.IllegalArgumentException
- If there is an error with the image.public void writeImage(OutputStream out, ImageData image) throws IOException, IllegalArgumentException
out
- The output Stream. To write to a byte array use a new MemoryFile as the Stream.image
- The image to write.IOException
- If there is an error writing the data.IllegalArgumentException
- If there is an error with the image.public void writeImage(OutputStream out, ImageData image, boolean useAlpha) throws IOException, IllegalArgumentException
out
- The output Stream. To write to a byte array use a new MemoryFile as the Stream.image
- The image to write.usesAlpha
- if this is true then an ALPHA channel will be saved in the image.IOException
- If there is an error writing the data.IllegalArgumentException
- If there is an error with the image.