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

public Class PngReader

extends Object
Class Inheritance
Imports
java.io.IOException, .InputStream, java.util.Arrays, .HashSet, java.util.zip.CRC32, .Inflater, .InflaterInputStream, jogamp.opengl.util.pngj.ImageLine.SampleType, jogamp.opengl.util.pngj.chunks.ChunkHelper, .ChunkLoadBehaviour, .ChunkRaw, .ChunksList, .PngChunk, .PngChunkIDAT, .PngChunkIHDR, .PngChunkSkipped, .PngMetadata

Reads a PNG image, line by line.

The reading sequence is as follows:
1. At construction time, the header and IHDR chunk are read (basic image info)
2. Afterwards you can set some additional global options. Eg. setUnpackedMode(boolean), setCrcCheckDisabled().
3. Optional: If you call getMetadata() or getChunksLisk() before start reading the rows, all the chunks before IDAT are automatically loaded and available
4a. The rows are read onen by one of the readRowXXX methods: readRowInt(int), PngReader#readRowByte(int), etc, in order, from 0 to nrows-1 (you can skip or repeat rows, but not go backwards)
4b. Alternatively, you can read all rows, or a subset, in a single call: readRowsInt(), readRowsByte() ,etc. In general this consumes more memory, but for interlaced images this is equally efficient, and more so if reading a small subset of rows.
5. Read of the last row auyomatically loads the trailing chunks, and ends the reader.
6. end() forcibly finishes/aborts the reading and closes the stream

Field Summary

Modifier and TypeField and Description
protected final int
private int
private ChunkLoadBehaviour
protected final ChunksList
private boolean
protected CRC32
protected int
currentChunkGroup

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

see ChunksList

private final PngDeinterlacer
protected final String
filename

not necesarily a filename, can be a description - merely informative

protected InflaterInputStream
protected PngIDatChunkInputStream
public final ImageInfo
imgInfo

Basic image info - final and inmutable.

protected ImageLine
private Inflater
protected final InputStream
private final boolean
private int
private long
protected final PngMetadata
private long
protected byte[]
protected byte[]
protected byte[]
protected int
private boolean
private String[]
private HashSet<String>
private int
private boolean

Constructor Summary

AccessConstructor and Description
public
PngReader(final InputStream inputStream, final String
: Optional, can be a filename or a description. Just for error/debug messages
filenameOrDescription
)

Constructs a PngReader from an InputStream.

Method Summary

Modifier and TypeMethod and Description
private void
private void
private void
decodeLastReadRowToByte(final byte[] buffer, final int bytesRead)

private void
decodeLastReadRowToInt(final int[] buffer, final int bytesRead)

public void
end()

Normally this does nothing, but it can be used to force a premature closing.

private boolean
public ChunkLoadBehaviour
public ChunksList
getChunksList()

All loaded chunks (metada).

pack-priv long

Returns:

CRC of the raw pixels values
getCrctestVal
()

Just for testing.

pack-priv int
public int

Returns:

Total maximum bytes to load from ancillary ckunks.
getMaxBytesMetadata
()

public long

Returns:

Total maximum bytes to read.
getMaxTotalBytesRead
()

public PngMetadata
getMetadata()

High level wrapper over chunksList

public ImageLine
getRow(final int nrow)

Deprecated Now readRow(int) implements the same funcion. This method will be removed in future releases
public String[]

Returns:

Chunk-IDs to be skipped.
getSkipChunkIds
()

public int

Returns:

maximum size in bytes for individual ancillary chunks.
getSkipChunkMaxSize
()

pack-priv void
initCrctest()

Inits CRC object and enables CRC calculation

public boolean
isInterlaced()

Interlaced PNG is accepted -though not welcomed- now...

public boolean
protected void
logWarn(final String warn)

Logs/prints a warning.

private PngChunk
readChunk(final byte[] chunkid, final int clen, final boolean skipforced)

Reads chunkd from input stream, adds to ChunksList, and returns it.

private final void
readFirstChunks()

Reads chunks before first IDAT.

private void
readLastAndClose()

Reads last Internally called after having read the last line.

pack-priv void
readLastChunks()

Reads (and processes) chunks after last IDAT.

public ImageLine
readRow(final int nrow)

If called for first time, calls readRowInt.

public final int[]
readRow(final int[] buffer, final int nrow)

public ImageLine

Returns:

ImageLine object, also available as field. Data is in ImageLine#scanlineb (byte) field.
readRowByte
(final int
Row number, from 0 to rows-1. Increasing order.
nrow
)

Reads the row as BYTES, storing it in the imgLine property and returning it.

public final byte[]

Returns:

The scanline in the same passwd buffer if it was allocated, a newly allocated one otherwise
readRowByte
(byte[]
Prealocated buffer, or null.
buffer
,
final int
Row number (0 is top). Most be strictly greater than the last read row.
nrow
)

Reads a line and returns it as a byte[] array.

public ImageLine

Returns:

ImageLine object, also available as field. Data is in ImageLine#scanline (int) field.
readRowInt
(final int
Row number, from 0 to rows-1. Increasing order.
nrow
)

Reads the row as INT, storing it in the imgLine property and returning it.

public final int[]

Returns:

The scanline in the same passwd buffer if it was allocated, a newly allocated one otherwise
readRowInt
(int[]
Prealocated buffer, or null.
buffer
,
final int
Row number (0 is top). Most be strictly greater than the last read row.
nrow
)

Reads a line and returns it as a int[] array.

private int
readRowRaw(final int nrow)

public ImageLines

Returns:

Set of lines as a matrix
readRowsByte
(final int
Number of rows to be skipped
rowOffset
,
int
Total number of rows to be read. -1: read all available
nRows
,
final int
Row increment. If 1, we read consecutive lines; if 2, we read even/odd lines, etc
rowStep
)

Reads a set of lines and returns it as a ImageLines object, which wrapas a byte[][] matrix.

public ImageLines
readRowsByte()

Same as readRowsByte(0, imgInfo.rows, 1)

public ImageLines

Returns:

Set of lines as a ImageLines, which wraps a matrix
readRowsInt
(final int
Number of rows to be skipped
rowOffset
,
int
Total number of rows to be read. -1: read all available
nRows
,
final int
Row increment. If 1, we read consecutive lines; if 2, we read even/odd lines, etc
rowStep
)

Reads a set of lines and returns it as a ImageLines object, which wraps matrix.

public ImageLines
readRowsInt()

Same as readRowsInt(0, imgInfo.rows, 1)

public void
readSkippingAllRows()

Reads all the (remaining) file, skipping the pixels data.

public void
reuseBuffersFrom(final PngReader
A PngReader that has already finished reading pixels. Can be null.
other
)

Tries to reuse the allocated buffers from other already used PngReader object.

public void
setChunkLoadBehaviour(final ChunkLoadBehaviour chunkLoadBehaviour)

Determines which ancillary chunks (metada) are to be loaded

public void
setCrcCheckDisabled()

Disables the CRC integrity check in IDAT chunks and ancillary chunks, this gives a slight increase in reading speed for big files

public void
setMaxBytesMetadata(final int maxBytesChunksToLoad)

Set total maximum bytes to load from ancillary chunks (0: unlimited; default: 5Mb).

public void
setMaxTotalBytesRead(final long maxTotalBytesToRead)

Set total maximum bytes to read (0: unlimited; default: 200MB).

public void
setShouldCloseStream(final boolean shouldCloseStream)

if true, input stream will be closed after ending read

default=true

public void
setSkipChunkIds(final String[] skipChunksById)

Chunks ids to be skipped.

public void
setSkipChunkMaxSize(final int skipChunksBySize)

Set maximum size in bytes for individual ancillary chunks (0: unlimited; default: 2MB).

public void
setUnpackedMode(final boolean unPackedMode)

set/unset "unpackedMode" If false (default) packed types (bitdepth=1,2 or 4) will keep several samples packed in one element (byte or int) If true, samples will be unpacked on reading, and each element in the scanline will be sample.

public String
toString()

Overrides java.lang.Object.toString.

Basic info, for debugging.
private void
unfilterRow(final int nbytes)

private void
unfilterRowAverage(final int nbytes)

private void
unfilterRowNone(final int nbytes)

private void
unfilterRowPaeth(final int nbytes)

private void
unfilterRowSub(final int nbytes)

private void
unfilterRowUp(final int nbytes)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

buffersLenback to summary
protected final int buffersLen
bytesChunksLoadedback to summary
private int bytesChunksLoaded
chunkLoadBehaviourback to summary
private ChunkLoadBehaviour chunkLoadBehaviour
chunksListback to summary
protected final ChunksList chunksList
crcEnabledback to summary
private boolean crcEnabled
crctestback to summary
protected CRC32 crctest
currentChunkGroupback to summary
protected int currentChunkGroup

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

see ChunksList

deinterlacerback to summary
private final PngDeinterlacer deinterlacer
filenameback to summary
protected final String filename

not necesarily a filename, can be a description - merely informative

idatIstreamback to summary
protected InflaterInputStream idatIstream
iIdatCstreamback to summary
protected PngIDatChunkInputStream iIdatCstream
imgInfoback to summary
public final ImageInfo imgInfo

Basic image info - final and inmutable.

imgLineback to summary
protected ImageLine imgLine
inflaterback to summary
private Inflater inflater
inputStreamback to summary
protected final InputStream inputStream
interlacedback to summary
private final boolean interlaced
maxBytesMetadataback to summary
private int maxBytesMetadata
maxTotalBytesReadback to summary
private long maxTotalBytesRead
metadataback to summary
protected final PngMetadata metadata
offsetback to summary
private long offset
rowbback to summary
protected byte[] rowb
rowbfilterback to summary
protected byte[] rowbfilter
rowbprevback to summary
protected byte[] rowbprev
rowNumback to summary
protected int rowNum
shouldCloseStreamback to summary
private boolean shouldCloseStream
skipChunkIdsback to summary
private String[] skipChunkIds
skipChunkIdsSetback to summary
private HashSet<String> skipChunkIdsSet
skipChunkMaxSizeback to summary
private int skipChunkMaxSize
unpackedModeback to summary
private boolean unpackedMode

Constructor Detail

PngReaderback to summary
public PngReader(final InputStream inputStream, final String filenameOrDescription)

Constructs a PngReader from an InputStream.

See also FileHelper.createPngReader(File f) if available. Reads only the signature and first chunk (IDHR)

Parameters
filenameOrDescription:String

: Optional, can be a filename or a description. Just for error/debug messages

Method Detail

allocateBuffersback to summary
private void allocateBuffers()
closeback to summary
private void close()
decodeLastReadRowToByteback to summary
private void decodeLastReadRowToByte(final byte[] buffer, final int bytesRead)
decodeLastReadRowToIntback to summary
private void decodeLastReadRowToInt(final int[] buffer, final int bytesRead)
endback to summary
public void end()

Normally this does nothing, but it can be used to force a premature closing. Its recommended practice to call it after reading the image pixels.

firstChunksNotYetReadback to summary
private boolean firstChunksNotYetRead()
getChunkLoadBehaviourback to summary
public ChunkLoadBehaviour getChunkLoadBehaviour()
See Also
setChunkLoadBehaviour(ChunkLoadBehaviour)
getChunksListback to summary
public ChunksList getChunksList()

All loaded chunks (metada). If we have not yet end reading the image, this will include only the chunks before the pixels data (IDAT)

Critical chunks are included, except that all IDAT chunks appearance are replaced by a single dummy-marker IDAT chunk. These might be copied to the PngWriter

See Also
getMetadata()
getCrctestValback to summary
pack-priv long getCrctestVal()

Just for testing. TO be called after ending reading, only if initCrctest() was called before start

Returns:long

CRC of the raw pixels values

getCurrentChunkGroupback to summary
pack-priv int getCurrentChunkGroup()
getMaxBytesMetadataback to summary
public int getMaxBytesMetadata()
Returns:int

Total maximum bytes to load from ancillary ckunks.

getMaxTotalBytesReadback to summary
public long getMaxTotalBytesRead()
Returns:long

Total maximum bytes to read.

getMetadataback to summary
public PngMetadata getMetadata()

High level wrapper over chunksList

See Also
getChunksList()
getRowback to summary
public ImageLine getRow(final int nrow)

Deprecated

Now readRow(int) implements the same funcion. This method will be removed in future releases

getSkipChunkIdsback to summary
public String[] getSkipChunkIds()
Returns:String[]

Chunk-IDs to be skipped.

getSkipChunkMaxSizeback to summary
public int getSkipChunkMaxSize()
Returns:int

maximum size in bytes for individual ancillary chunks.

initCrctestback to summary
pack-priv void initCrctest()

Inits CRC object and enables CRC calculation

isInterlacedback to summary
public boolean isInterlaced()

Interlaced PNG is accepted -though not welcomed- now...

isUnpackedModeback to summary
public boolean isUnpackedMode()
See Also
PngReader#setUnpackedMode(boolean)
logWarnback to summary
protected void logWarn(final String warn)

Logs/prints a warning.

The default behaviour is print to stderr, but it can be overriden.

This happens rarely - most errors are fatal.

readChunkback to summary
private PngChunk readChunk(final byte[] chunkid, final int clen, final boolean skipforced)

Reads chunkd from input stream, adds to ChunksList, and returns it. If it's skipped, a PngChunkSkipped object is created

readFirstChunksback to summary
private final void readFirstChunks()

Reads chunks before first IDAT. Normally this is called automatically

Position before: after IDHR (crc included) Position after: just after the first IDAT chunk id

This can be called several times (tentatively), it does nothing if already run

(Note: when should this be called? in the constructor? hardly, because we loose the opportunity to call setChunkLoadBehaviour() and perhaps other settings before reading the first row? but sometimes we want to access some metadata (plte, phys) before. Because of this, this method can be called explicitly but is also called implicititly in some methods (getMetatada(), getChunksList())

readLastAndCloseback to summary
private void readLastAndClose()

Reads last Internally called after having read the last line. It reads extra chunks after IDAT, if present.

readLastChunksback to summary
pack-priv void readLastChunks()

Reads (and processes) chunks after last IDAT.

readRowback to summary
public ImageLine readRow(final int nrow)

If called for first time, calls readRowInt. Elsewhere, it calls the appropiate readRowInt/readRowByte

In general, specifying the concrete readRowInt/readRowByte is preferrable

See Also
<a href="jogamp.opengl.util.pngj.PngReader.html#readRowByte-int-" class="jlink"><code class="coderef">readRowByte(int)</code></a>
readRowback to summary
public final int[] readRow(final int[] buffer, final int nrow)
See Also
readRowInt(int[], int)
readRowByteback to summary
public ImageLine readRowByte(final int nrow)

Reads the row as BYTES, storing it in the imgLine property and returning it. The row must be greater or equal than the last read row. This method allows to pass the same row that was last read.

Parameters
nrow:int

Row number, from 0 to rows-1. Increasing order.

Returns:ImageLine

ImageLine object, also available as field. Data is in ImageLine#scanlineb (byte) field.

readRowByteback to summary
public final byte[] readRowByte(byte[] buffer, final int nrow)

Reads a line and returns it as a byte[] array.

You can pass (optionally) a prealocatted buffer.

If the bitdepth is less than 8, the bytes are packed - unless unpackedMode is true.
If the bitdepth is 16, the least significant byte is lost.

Parameters
buffer:byte[]

Prealocated buffer, or null.

nrow:int

Row number (0 is top). Most be strictly greater than the last read row.

Returns:byte[]

The scanline in the same passwd buffer if it was allocated, a newly allocated one otherwise

readRowIntback to summary
public ImageLine readRowInt(final int nrow)

Reads the row as INT, storing it in the imgLine property and returning it. The row must be greater or equal than the last read row.

Parameters
nrow:int

Row number, from 0 to rows-1. Increasing order.

Returns:ImageLine

ImageLine object, also available as field. Data is in ImageLine#scanline (int) field.

readRowIntback to summary
public final int[] readRowInt(int[] buffer, final int nrow)

Reads a line and returns it as a int[] array.

You can pass (optionally) a prealocatted buffer.

If the bitdepth is less than 8, the bytes are packed - unless unpackedMode is true.

Parameters
buffer:int[]

Prealocated buffer, or null.

nrow:int

Row number (0 is top). Most be strictly greater than the last read row.

Returns:int[]

The scanline in the same passwd buffer if it was allocated, a newly allocated one otherwise

readRowRawback to summary
private int readRowRaw(final int nrow)
readRowsByteback to summary
public ImageLines readRowsByte(final int rowOffset, int nRows, final int rowStep)

Reads a set of lines and returns it as a ImageLines object, which wrapas a byte[][] matrix. Internally it reads all lines, but decodes and stores only the wanted ones. This starts and ends the reading, and cannot be combined with other reading methods.

This it's more efficient (speed an memory) that doing calling readRowByte() for each desired line only if the image is interlaced.

Notice that the columns in the matrix is not the pixel width of the image, but rather pixels x channels

Parameters
rowOffset:int

Number of rows to be skipped

nRows:int

Total number of rows to be read. -1: read all available

rowStep:int

Row increment. If 1, we read consecutive lines; if 2, we read even/odd lines, etc

Returns:ImageLines

Set of lines as a matrix

See Also
to read about the format of each row. Notice that if the bitdepth is 16 this will lose information
readRowsByteback to summary
public ImageLines readRowsByte()

Same as readRowsByte(0, imgInfo.rows, 1)

See Also
readRowsByte(int, int, int)
readRowsIntback to summary
public ImageLines readRowsInt(final int rowOffset, int nRows, final int rowStep)

Reads a set of lines and returns it as a ImageLines object, which wraps matrix. Internally it reads all lines, but decodes and stores only the wanted ones. This starts and ends the reading, and cannot be combined with other reading methods.

This it's more efficient (speed an memory) that doing calling readRowInt() for each desired line only if the image is interlaced.

Notice that the columns in the matrix is not the pixel width of the image, but rather pixels x channels

Parameters
rowOffset:int

Number of rows to be skipped

nRows:int

Total number of rows to be read. -1: read all available

rowStep:int

Row increment. If 1, we read consecutive lines; if 2, we read even/odd lines, etc

Returns:ImageLines

Set of lines as a ImageLines, which wraps a matrix

See Also
to read about the format of each row
readRowsIntback to summary
public ImageLines readRowsInt()

Same as readRowsInt(0, imgInfo.rows, 1)

See Also
readRowsInt(int, int, int)
readSkippingAllRowsback to summary
public void readSkippingAllRows()

Reads all the (remaining) file, skipping the pixels data. This is much more efficient that calling readRow(), specially for big files (about 10 times faster!), because it doesn't even decompress the IDAT stream and disables CRC check Use this if you are not interested in reading pixels,only metadata.

reuseBuffersFromback to summary
public void reuseBuffersFrom(final PngReader other)

Tries to reuse the allocated buffers from other already used PngReader object. This will have no effect if the buffers are smaller than necessary. It also reuses the inflater.

Parameters
other:PngReader

A PngReader that has already finished reading pixels. Can be null.

setChunkLoadBehaviourback to summary
public void setChunkLoadBehaviour(final ChunkLoadBehaviour chunkLoadBehaviour)

Determines which ancillary chunks (metada) are to be loaded

Parameters
chunkLoadBehaviour:ChunkLoadBehaviour

ChunkLoadBehaviour

setCrcCheckDisabledback to summary
public void setCrcCheckDisabled()

Disables the CRC integrity check in IDAT chunks and ancillary chunks, this gives a slight increase in reading speed for big files

setMaxBytesMetadataback to summary
public void setMaxBytesMetadata(final int maxBytesChunksToLoad)

Set total maximum bytes to load from ancillary chunks (0: unlimited; default: 5Mb).
If exceeded, some chunks will be skipped

setMaxTotalBytesReadback to summary
public void setMaxTotalBytesRead(final long maxTotalBytesToRead)

Set total maximum bytes to read (0: unlimited; default: 200MB).
These are the bytes read (not loaded) in the input stream. If exceeded, an exception will be thrown.

setShouldCloseStreamback to summary
public void setShouldCloseStream(final boolean shouldCloseStream)

if true, input stream will be closed after ending read

default=true

setSkipChunkIdsback to summary
public void setSkipChunkIds(final String[] skipChunksById)

Chunks ids to be skipped.
These chunks will be skipped (the CRC will not be checked) and the chunk will be saved as a PngChunkSkipped object. See also setSkipChunkMaxSize

setSkipChunkMaxSizeback to summary
public void setSkipChunkMaxSize(final int skipChunksBySize)

Set maximum size in bytes for individual ancillary chunks (0: unlimited; default: 2MB).
Chunks exceeding this length will be skipped (the CRC will not be checked) and the chunk will be saved as a PngChunkSkipped object. See also setSkipChunkIds

setUnpackedModeback to summary
public void setUnpackedMode(final boolean unPackedMode)

set/unset "unpackedMode"
If false (default) packed types (bitdepth=1,2 or 4) will keep several samples packed in one element (byte or int)
If true, samples will be unpacked on reading, and each element in the scanline will be sample. This implies more processing and memory, but it's the most efficient option if you intend to read individual pixels.
This option should only be set before start reading.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Basic info, for debugging.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

Annotations
@Override
unfilterRowback to summary
private void unfilterRow(final int nbytes)
unfilterRowAverageback to summary
private void unfilterRowAverage(final int nbytes)
unfilterRowNoneback to summary
private void unfilterRowNone(final int nbytes)
unfilterRowPaethback to summary
private void unfilterRowPaeth(final int nbytes)
unfilterRowSubback to summary
private void unfilterRowSub(final int nbytes)
unfilterRowUpback to summary
private void unfilterRowUp(final int nbytes)