ar.com.hjg.pngj
Class PngWriter

java.lang.Object
  extended by ar.com.hjg.pngj.PngWriter

public class PngWriter
extends java.lang.Object

Writes a PNG image, line by line.


Field Summary
 ImageInfo imgInfo
           
 
Constructor Summary
PngWriter(java.io.OutputStream outputStream, ImageInfo imgInfo)
           
PngWriter(java.io.OutputStream outputStream, ImageInfo imgInfo, java.lang.String filenameOrDescription)
          Constructs a new PngWriter from a output stream.
 
Method Summary
 double computeCompressionRatio()
          Computes compressed size/raw size, approximate.
 void copyChunksFirst(PngReader reader, int copy_mask)
          Copies first (pre IDAT) ancillary chunks from a PngReader.
 void copyChunksLast(PngReader reader, int copy_mask)
          Copies last (post IDAT) ancillary chunks from a PngReader.
 void end()
          Finalizes the image creation and closes the stream.
 ChunksListForWrite getChunkList()
           
 java.lang.String getFilename()
          Filename or description, from the optional constructor argument.
 PngMetadata getMetadata()
           
 void setCompLevel(int compLevel)
          Sets compression level of ZIP algorithm.
 void setDeflaterStrategy(int deflaterStrategy)
          Deflater strategy: one of Deflater.FILTERED Deflater.HUFFMAN_ONLY Deflater.DEFAULT_STRATEGY
 void setFilterType(FilterType filterType)
          Sets internal prediction filter type, or strategy to choose it.
 void setIdatMaxSize(int idatMaxSize)
          Sets maximum size of IDAT fragments.
 void setShouldCloseStream(boolean shouldCloseStream)
          if true, input stream will be closed after ending write default=true
 void writeRow(ImageLine imgline, int rownumber)
          Writes line.
 void writeRow(int[] newrow)
          Same as writeRow(int[] newrow, int rown), but does not check row number
 void writeRow(int[] newrow, int rown)
          Writes a full image row.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

imgInfo

public final ImageInfo imgInfo
Constructor Detail

PngWriter

public PngWriter(java.io.OutputStream outputStream,
                 ImageInfo imgInfo)

PngWriter

public PngWriter(java.io.OutputStream outputStream,
                 ImageInfo imgInfo,
                 java.lang.String filenameOrDescription)
Constructs a new PngWriter from a output stream. After construction nothing is writen yet. You still can set some parameters (compression, filters) and queue chunks before start writing the pixels.

See also FileHelper.createPngWriter() if available.

Parameters:
outputStream - Opened stream for binary writing
imgInfo - Basic image parameters
filenameOrDescription - Optional, just for error/debug messages
Method Detail

computeCompressionRatio

public double computeCompressionRatio()
Computes compressed size/raw size, approximate.

Actually: compressed size = total size of IDAT data , raw size = uncompressed pixel bytes = rows * (bytesPerRow + 1). This must be called after pngw.end()


copyChunksFirst

public void copyChunksFirst(PngReader reader,
                            int copy_mask)
Copies first (pre IDAT) ancillary chunks from a PngReader.

Should be called when creating an image from another, before starting writing lines, to copy relevant chunks.

Parameters:
reader - : PngReader object, already opened.
copy_mask - : Mask bit (OR), see ChunksToWrite.COPY_XXX constants

copyChunksLast

public void copyChunksLast(PngReader reader,
                           int copy_mask)
Copies last (post IDAT) ancillary chunks from a PngReader.

Should be called when creating an image from another, after writing all lines, before closing the writer, to copy additional chunks.

Parameters:
reader - : PngReader object, already opened and fully read.
copy_mask - : Mask bit (OR), see ChunksToWrite.COPY_XXX constants

end

public void end()
Finalizes the image creation and closes the stream. This MUST be called after writing the lines.


getChunkList

public ChunksListForWrite getChunkList()

getFilename

public java.lang.String getFilename()
Filename or description, from the optional constructor argument.


getMetadata

public PngMetadata getMetadata()

setCompLevel

public void setCompLevel(int compLevel)
Sets compression level of ZIP algorithm.

This must be called just after constructor, before starting writing.

See also setFilterType()

Parameters:
compLevel - between 0 and 9 (default:6 , recommended: 6 or more)

setDeflaterStrategy

public void setDeflaterStrategy(int deflaterStrategy)
Deflater strategy: one of Deflater.FILTERED Deflater.HUFFMAN_ONLY Deflater.DEFAULT_STRATEGY


setFilterType

public void setFilterType(FilterType filterType)
Sets internal prediction filter type, or strategy to choose it.

This must be called just after constructor, before starting writing.

See also setCompLevel()

Parameters:
filterType - One of the five prediction types or strategy to choose it (see PngFilterType) Recommended values: DEFAULT (default) or AGGRESIVE

setIdatMaxSize

public void setIdatMaxSize(int idatMaxSize)
Sets maximum size of IDAT fragments. This has little effect on performance you should rarely call this

Parameters:
idatMaxSize - default=0 : use defaultSize (32K)

setShouldCloseStream

public void setShouldCloseStream(boolean shouldCloseStream)
if true, input stream will be closed after ending write

default=true


writeRow

public void writeRow(ImageLine imgline,
                     int rownumber)
Writes line. See writeRow(int[] newrow, int rown)


writeRow

public void writeRow(int[] newrow)
Same as writeRow(int[] newrow, int rown), but does not check row number

Parameters:
newrow -

writeRow

public void writeRow(int[] newrow,
                     int rown)
Writes a full image row.

This must be called sequentially from n=0 to n=rows-1 One integer per sample , in the natural order: R G B R G B ... (or R G B A R G B A... if has alpha) The values should be between 0 and 255 for 8 bitspc images, and between 0- 65535 form 16 bitspc images (this applies also to the alpha channel if present) The array can be reused.

Parameters:
newrow - Array of pixel values
rown - Row number, from 0 (top) to rows-1 (bottom). This is just used as a check. Pass -1 if you want to autocompute it