Top Description Fields Constructors Methods
jogamp.opengl.util.pngj

public Class PngWriter

extends Object
Class Inheritance
Imports
java.io.IOException, .OutputStream, java.util.Arrays, .List, java.util.zip.Deflater, .DeflaterOutputStream, jogamp.opengl.util.pngj.ImageLine.SampleType, jogamp.opengl.util.pngj.chunks.ChunkCopyBehaviour, .ChunkHelper, .ChunksList, .ChunksListForWrite, .PngChunk, .PngChunkIEND, .PngChunkIHDR, .PngChunkSkipped, .PngChunkTextVar, .PngMetadata

Writes a PNG image

Field Summary

Modifier and TypeField and Description
private final ChunksListForWrite
private int
compLevel

zip compression level 0 - 9

protected int
currentChunkGroup

Current chunk grounp, (0-6) already read or reading

see ChunksList

private PngIDatChunkOutputStream
private DeflaterOutputStream
private int
deflaterStrategy

Deflate algortithm compression strategy

private final String
protected FilterWriteStrategy
filterStrat

PNG filter strategy

private final int[]
private int
public final ImageInfo
private final PngMetadata
private final OutputStream
protected byte[]
protected byte[]
protected byte[]
protected int
rowNum

last read row number, starting from 0

private boolean
private boolean

Constructor Summary

AccessConstructor and Description
public
PngWriter(final OutputStream outputStream, final ImageInfo imgInfo)

public
PngWriter(final OutputStream
Opened stream for binary writing
outputStream
,
final ImageInfo
Basic image parameters
imgInfo
,
final String
Optional, just for error/debug messages
filenameOrDescription
)

Constructs a new PngWriter from a output stream.

Method Summary

Modifier and TypeMethod and Description
public double
computeCompressionRatio()

Computes compressed size/raw size, approximate.

private void
copyChunks(final PngReader reader, final int copy_mask, final boolean onlyAfterIdat)

copy chunks from reader - copy_mask : see ChunksToWrite.COPY_XXX

If we are after idat, only considers those chunks after IDAT in PngReader

Todo

this should be more customizable

public void
copyChunksFirst(final PngReader
: PngReader object, already opened.
reader
,
final int
: Mask bit (OR), see ChunksToWrite.COPY_XXX constants
copy_mask
)

Copies first (pre IDAT) ancillary chunks from a PngReader.

public void
copyChunksLast(final PngReader
: PngReader object, already opened and fully read.
reader
,
final int
: Mask bit (OR), see ChunksToWrite.COPY_XXX constants
copy_mask
)

Copies last (post IDAT) ancillary chunks from a PngReader.

protected void
encodeRowFromByte(final byte[] row)

protected void
encodeRowFromInt(final int[] row)

public void
end()

Finalizes the image creation and closes the stream.

private void
filterAndSend(final int rown)

private void
filterRow(final int rown)

protected void
protected void
protected void
protected void
protected void
public ChunksListForWrite
getChunksList()

returns the chunks list (queued and writen chunks)

public String
getFilename()

Filename or description, from the optional constructor argument.

public PngMetadata
getMetadata()

High level wrapper over chunksList for metadata handling

private void
init()

public boolean
private void
prepareEncodeRow(final int rown)

private void
reportResultsForFilter(final int rown, final FilterType type, final boolean tentative)

public void
setCompLevel(final int
between 0 and 9 (default:6 , recommended: 6 or more)
compLevel
)

Sets compression level of ZIP algorithm.

public void
setDeflaterStrategy(final int deflaterStrategy)

Deflater strategy: one of Deflater.FILTERED Deflater.HUFFMAN_ONLY Deflater.DEFAULT_STRATEGY

Default: Deflater.FILTERED .

public void
setFilterType(final FilterType
One of the five prediction types or strategy to choose it (see PngFilterType) Recommended values: DEFAULT (default) or AGGRESIVE
filterType
)

Sets internal prediction filter type, or strategy to choose it.

public void
setIdatMaxSize(final int
default=0 : use defaultSize (32K)
idatMaxSize
)

Sets maximum size of IDAT fragments.

public void
setShouldCloseStream(final boolean shouldCloseStream)

if true, input stream will be closed after ending write

default=true

public void
setUseUnPackedMode(final boolean useUnpackedMode)

If false (default), and image has bitdepth 1-2-4, the scanlines passed are assumed to be already packed.

protected int
private void
private void
private void
public void
writeRow(final ImageLine imgline)

Deprecated Better use writeRow(ImageLine imgline, int rownumber)
Writes line, checks that the row number is consistent with that of the ImageLine See writeRow(int[] newrow, int rown)
public void
writeRow(final ImageLine imgline, final int rownumber)

Writes line.

public void
writeRow(final int[] newrow)

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

public void
writeRow(final int[] newrow, final int rown)

Alias to writeRowInt

public void
writeRowByte(final byte[] newrow, final int rown)

Same semantics as writeRowInt but using bytes.

public void
writeRowInt(final int[]
Array of pixel values.

Warning

the array size should be exact (samplesPerRowP)

newrow
,
final int
Row number, from 0 (top) to rows-1 (bottom). This is just used as a check. Pass -1 if you want to autocompute it
rown
)

Writes a full image row.

public void
writeRowsByte(final byte[][] image)

Writes all the pixels, calling writeRowByte() for each image row

public void
writeRowsInt(final int[][] image)

Writes all the pixels, calling writeRowInt() for each image row

private void
writeSignatureAndIHDR()

Write id signature and also "IHDR" chunk

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

chunksListback to summary
private final ChunksListForWrite chunksList
compLevelback to summary
private int compLevel

zip compression level 0 - 9

currentChunkGroupback to summary
protected int currentChunkGroup

Current chunk grounp, (0-6) already read or reading

see ChunksList

datStreamback to summary
private PngIDatChunkOutputStream datStream
datStreamDeflatedback to summary
private DeflaterOutputStream datStreamDeflated
deflaterStrategyback to summary
private int deflaterStrategy

Deflate algortithm compression strategy

filenameback to summary
private final String filename
filterStratback to summary
protected FilterWriteStrategy filterStrat

PNG filter strategy

histoxback to summary
private final int[] histox
idatMaxSizeback to summary
private int idatMaxSize
imgInfoback to summary
public final ImageInfo imgInfo
metadataback to summary
private final PngMetadata metadata
osback to summary
private final OutputStream os
rowbback to summary
protected byte[] rowb
rowbfilterback to summary
protected byte[] rowbfilter
rowbprevback to summary
protected byte[] rowbprev
rowNumback to summary
protected int rowNum

last read row number, starting from 0

shouldCloseStreamback to summary
private boolean shouldCloseStream
unpackedModeback to summary
private boolean unpackedMode

Constructor Detail

PngWriterback to summary
public PngWriter(final OutputStream outputStream, final ImageInfo imgInfo)
PngWriterback to summary
public PngWriter(final OutputStream outputStream, final ImageInfo imgInfo, final 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:OutputStream

Opened stream for binary writing

imgInfo:ImageInfo

Basic image parameters

filenameOrDescription:String

Optional, just for error/debug messages

Method Detail

computeCompressionRatioback to summary
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()

copyChunksback to summary
private void copyChunks(final PngReader reader, final int copy_mask, final boolean onlyAfterIdat)

copy chunks from reader - copy_mask : see ChunksToWrite.COPY_XXX

If we are after idat, only considers those chunks after IDAT in PngReader

Todo

this should be more customizable

copyChunksFirstback to summary
public void copyChunksFirst(final PngReader reader, final 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

: PngReader object, already opened.

copy_mask:int

: Mask bit (OR), see ChunksToWrite.COPY_XXX constants

copyChunksLastback to summary
public void copyChunksLast(final PngReader reader, final 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

: PngReader object, already opened and fully read.

copy_mask:int

: Mask bit (OR), see ChunksToWrite.COPY_XXX constants

encodeRowFromByteback to summary
protected void encodeRowFromByte(final byte[] row)
encodeRowFromIntback to summary
protected void encodeRowFromInt(final int[] row)
endback to summary
public void end()

Finalizes the image creation and closes the stream. This MUST be called after writing the lines.

filterAndSendback to summary
private void filterAndSend(final int rown)
filterRowback to summary
private void filterRow(final int rown)
filterRowAverageback to summary
protected void filterRowAverage()
filterRowNoneback to summary
protected void filterRowNone()
filterRowPaethback to summary
protected void filterRowPaeth()
filterRowSubback to summary
protected void filterRowSub()
filterRowUpback to summary
protected void filterRowUp()
getChunksListback to summary
public ChunksListForWrite getChunksList()

returns the chunks list (queued and writen chunks)

getFilenameback to summary
public String getFilename()

Filename or description, from the optional constructor argument.

getMetadataback to summary
public PngMetadata getMetadata()

High level wrapper over chunksList for metadata handling

initback to summary
private void init()
isUnpackedModeback to summary
public boolean isUnpackedMode()
prepareEncodeRowback to summary
private void prepareEncodeRow(final int rown)
reportResultsForFilterback to summary
private void reportResultsForFilter(final int rown, final FilterType type, final boolean tentative)
setCompLevelback to summary
public void setCompLevel(final int compLevel)

Sets compression level of ZIP algorithm.

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

See also setFilterType()

Parameters
compLevel:int

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

setDeflaterStrategyback to summary
public void setDeflaterStrategy(final int deflaterStrategy)

Deflater strategy: one of Deflater.FILTERED Deflater.HUFFMAN_ONLY Deflater.DEFAULT_STRATEGY

Default: Deflater.FILTERED . This should be changed very rarely.

setFilterTypeback to summary
public void setFilterType(final 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:FilterType

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

setIdatMaxSizeback to summary
public void setIdatMaxSize(final int idatMaxSize)

Sets maximum size of IDAT fragments. This has little effect on performance you should rarely call this

Parameters
idatMaxSize:int

default=0 : use defaultSize (32K)

setShouldCloseStreamback to summary
public void setShouldCloseStream(final boolean shouldCloseStream)

if true, input stream will be closed after ending write

default=true

setUseUnPackedModeback to summary
public void setUseUnPackedMode(final boolean useUnpackedMode)

If false (default), and image has bitdepth 1-2-4, the scanlines passed are assumed to be already packed.

If true, each element is a sample, the writer will perform the packing if necessary.

Warning

when using writeRow(ImageLine, int) (recommended) the packed flag of the ImageLine object overrides (and overwrites!) this field.

sumRowbfilterback to summary
protected int sumRowbfilter()
writeEndChunkback to summary
private void writeEndChunk()
writeFirstChunksback to summary
private void writeFirstChunks()
writeLastChunksback to summary
private void writeLastChunks()
writeRowback to summary
public void writeRow(final ImageLine imgline)

Deprecated

Better use writeRow(ImageLine imgline, int rownumber)

Writes line, checks that the row number is consistent with that of the ImageLine See writeRow(int[] newrow, int rown)

writeRowback to summary
public void writeRow(final ImageLine imgline, final int rownumber)

Writes line. See writeRow(int[] newrow, int rown) The packed flag of the imageline is honoured!

See Also
writeRowInt(int[], int)
writeRowback to summary
public void writeRow(final int[] newrow)

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

writeRowback to summary
public void writeRow(final int[] newrow, final int rown)

Alias to writeRowInt

See Also
writeRowInt(int[], int)
writeRowByteback to summary
public void writeRowByte(final byte[] newrow, final int rown)

Same semantics as writeRowInt but using bytes. Each byte is still a sample. If 16bitdepth, we are passing only the most significant byte (and hence losing some info)

See Also
PngWriter#writeRowInt(int[], int)
writeRowIntback to summary
public void writeRowInt(final int[] newrow, final 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.

Warning

the array might be modified in some cases (unpacked row with low bitdepth)

Parameters
newrow:int[]

Array of pixel values.

Warning

the array size should be exact (samplesPerRowP)

rown:int

Row number, from 0 (top) to rows-1 (bottom). This is just used as a check. Pass -1 if you want to autocompute it

writeRowsByteback to summary
public void writeRowsByte(final byte[][] image)

Writes all the pixels, calling writeRowByte() for each image row

writeRowsIntback to summary
public void writeRowsInt(final int[][] image)

Writes all the pixels, calling writeRowInt() for each image row

writeSignatureAndIHDRback to summary
private void writeSignatureAndIHDR()

Write id signature and also "IHDR" chunk