public class PngReader extends Object
Each row is read as an ImageLineInt
object (one int per sample), but this can be changed by setting a
different ImageLineFactory
Internally, this wraps a ChunkSeqReaderPng
with a BufferedStreamFeeder
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. setCrcCheckDisabled()
.
3. Optional: If you call getMetadata() or getChunksLisk() before start reading the rows, all the chunks before IDAT
are then loaded and available
4a. The rows are read in order by calling readRow()
. You can also call readRow(int)
to skip rows
-but you can't go backwards, at least not with this implementation. This method returns a IImageLine
object
which can be casted to the concrete class. This class returns by default a ImageLineInt
, but this can be
changed.
4b. Alternatively, you can read all rows, or a subset, in a single call: readRows()
,
readRows(int, int, int)
,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. Reading of the last row automatically loads the trailing chunks, and ends the reader.
6. end() also loads the trailing chunks, if not done, and finishes cleanly the reading and closes the stream.
See also PngReaderInt
(esentially the same as this, and slightly preferred) and PngReaderByte
(uses
byte instead of int to store the samples).
Modifier and Type | Field and Description |
---|---|
protected ChunkSeqReaderPng |
chunkseq
This object has most of the intelligence to parse the chunks and decompress the IDAT stream
|
ImageInfo |
imgInfo
Basic image info - final and inmutable.
|
protected IImageLineSet<? extends IImageLine> |
imlinesSet
Represents the set of lines (rows) being read.
|
boolean |
interlaced
flag: image was in interlaced format
|
static long |
MAX_BYTES_METADATA_DEFAULT
Defensive limit: refuse to load more than 5MB of ancillary metadata, see
setMaxBytesMetadata(long) and
also addChunkToSkip(String) |
static long |
MAX_CHUNK_SIZE_SKIP
Skip ancillary chunks greater than 2MB, see
setSkipChunkMaxSize(long) |
static long |
MAX_TOTAL_BYTES_READ_DEFAULT
Defensive limit: refuse to read more than 900MB, can be changed with
setMaxTotalBytesRead(long) |
protected PngMetadata |
metadata |
protected int |
rowNum
Current row number (reading or read), numbered from 0
|
protected BufferedStreamFeeder |
streamFeeder
Takes bytes from the InputStream and passes it to the ChunkSeqReaderPng.
|
Constructor and Description |
---|
PngReader(File file)
Constructs a PngReader opening a file.
|
PngReader(InputStream inputStream)
Constructs a PngReader object from a stream, with default options.
|
PngReader(InputStream inputStream,
boolean shouldCloseStream)
Same as
PngReader(InputStream) but allows to specify early if the stream must be closed |
Modifier and Type | Method and Description |
---|---|
void |
addChunkToSkip(String chunkToSkip) |
void |
close()
Releases resources, and closes stream if corresponds.
|
protected ChunkSeqReaderPng |
createChunkSeqReader()
called on construction time.
|
protected IImageLineSet<? extends IImageLine> |
createLineSet(boolean singleCursor,
int nlines,
int noffset,
int step)
By default this uses the factory (which, by default creates ImageLineInt).
|
void |
dontSkipChunk(String chunkToSkip) |
void |
end()
Reads till end of PNG stream and call close()
This should normally be called after reading the pixel data, to read the trailing chunks and close the stream.
|
ChunkSeqReaderPng |
getChunkseq()
Gets wrapped
ChunkSeqReaderPng object |
ChunksList |
getChunksList()
All loaded chunks (metada).
|
ChunksList |
getChunksList(boolean forceLoadingOfFirstChunks) |
ImageInfo |
getCurImgInfo() |
ImageInfo |
getImgInfo() |
PngMetadata |
getMetadata()
High level wrapper over chunksList
|
String |
getSimpleDigestHex()
Pseudo 64-bits digest computed over the basic image properties and the raw pixels data: it should coincide for
equivalent images encoded with different filters and compressors; but will not coincide for
interlaced/non-interlaced; also, this does not take into account the palette info.
|
boolean |
hasMoreRows()
True if last row has not yet been read
|
boolean |
isInterlaced()
Interlaced PNG is accepted -though not welcomed- now...
|
protected void |
loadAllInterlaced(int nRows,
int rowOffset,
int rowStep) |
void |
prepareSimpleDigestComputation()
Enables and prepare the simple digest computation.
|
protected void |
readFirstChunks()
Reads chunks before first IDAT.
|
IImageLine |
readRow()
Reads next row.
|
IImageLine |
readRow(int nrow)
The row number is mostly meant as a check, the rows must be called in ascending order (not necessarily consecutive)
|
IImageLineSet<? extends IImageLine> |
readRows()
Reads all rows in a ImageLineSet This is handy, but less memory-efficient (except for interlaced)
|
IImageLineSet<? extends IImageLine> |
readRows(int nRows,
int rowOffset,
int rowStep)
Reads a subset of rows.
|
void |
readSkippingAllRows()
Reads all the (remaining) file, skipping the pixels data.
|
void |
setChunkLoadBehaviour(ChunkLoadBehaviour chunkLoadBehaviour)
Determines which ancillary chunks (metadata) are to be loaded and which skipped.
|
void |
setChunksToSkip(String... chunksToSkip)
Chunks ids to be skipped.
|
void |
setCrcCheckDisabled()
Disables the CRC integrity check in IDAT chunks and ancillary chunks, this gives a slight increase in reading speed
for big files
|
void |
setLineSetFactory(IImageLineSetFactory<? extends IImageLine> factory)
Sets the factory that creates the ImageLine.
|
void |
setMaxBytesMetadata(long maxBytesMetadata)
Set total maximum bytes to load from ancillary chunks (0: unlimited; default: 5Mb).
If exceeded, some chunks will be skipped |
void |
setMaxTotalBytesRead(long maxTotalBytesToRead)
Set total maximum bytes to read (0: unlimited; default: 200MB).
|
void |
setShouldCloseStream(boolean shouldCloseStream)
if true, input stream will be closed after ending read
|
void |
setSkipChunkMaxSize(long skipChunkMaxSize)
Set maximum size in bytes for individual ancillary chunks (0: unlimited; default: 2MB).
|
String |
toString()
Basic info, for debugging.
|
String |
toStringCompact()
Basic info, in a compact format, apt for scripting COLSxROWS[dBITDEPTH][a][p][g][i] ( the default dBITDEPTH='d8' is
ommited)
|
public static final long MAX_TOTAL_BYTES_READ_DEFAULT
setMaxTotalBytesRead(long)
public static final long MAX_BYTES_METADATA_DEFAULT
setMaxBytesMetadata(long)
and
also addChunkToSkip(String)
public static final long MAX_CHUNK_SIZE_SKIP
setSkipChunkMaxSize(long)
public final ImageInfo imgInfo
public final boolean interlaced
protected final ChunkSeqReaderPng chunkseq
protected final BufferedStreamFeeder streamFeeder
protected final PngMetadata metadata
getMetadata()
protected int rowNum
protected IImageLineSet<? extends IImageLine> imlinesSet
readRows()
or for interlaced images (this later is
transparent to the user)public PngReader(InputStream inputStream)
Warning: In case of exception the stream is NOT closed.
Warning: By default the stream will be closed when this object is close()
d. See
PngReader(InputStream,boolean)
or setShouldCloseStream(boolean)
inputStream
- PNG streampublic PngReader(InputStream inputStream, boolean shouldCloseStream)
PngReader(InputStream)
but allows to specify early if the stream must be closedinputStream
- shouldCloseStream
- The stream will be closed in case of exception (constructor included) or normal
termination.public PngReader(File file)
file
- PNG image fileprotected void readFirstChunks()
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())
public void setChunkLoadBehaviour(ChunkLoadBehaviour chunkLoadBehaviour)
Additional restrictions may apply. See also setChunksToSkip(String...)
, addChunkToSkip(String)
,
setMaxBytesMetadata(long)
, setSkipChunkMaxSize(long)
chunkLoadBehaviour
- ChunkLoadBehaviour
public ChunksList getChunksList()
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
getMetadata()
public ChunksList getChunksList(boolean forceLoadingOfFirstChunks)
public PngMetadata getMetadata()
getChunksList()
public IImageLine readRow()
public boolean hasMoreRows()
public IImageLine readRow(int nrow)
public IImageLineSet<? extends IImageLine> readRows()
public IImageLineSet<? extends IImageLine> readRows(int nRows, int rowOffset, int rowStep)
This method should called once, and not be mixed with readRow()
nRows
- how many rows to read (default: imageInfo.rows; negative: autocompute)rowOffset
- rows to skip (default:0)rowStep
- step between rows to load( default:1)public void setLineSetFactory(IImageLineSetFactory<? extends IImageLine> factory)
factory
- protected IImageLineSet<? extends IImageLine> createLineSet(boolean singleCursor, int nlines, int noffset, int step)
See doc in IImageLineSetFactory.create(ImageInfo, boolean, int, int, int)
protected void loadAllInterlaced(int nRows, int rowOffset, int rowStep)
public void readSkippingAllRows()
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.public void setMaxTotalBytesRead(long maxTotalBytesToRead)
public void setMaxBytesMetadata(long maxBytesMetadata)
public void setSkipChunkMaxSize(long skipChunkMaxSize)
public void setChunksToSkip(String... chunksToSkip)
public void addChunkToSkip(String chunkToSkip)
public void dontSkipChunk(String chunkToSkip)
public void setShouldCloseStream(boolean shouldCloseStream)
default=true
public void end()
public void close()
public boolean isInterlaced()
public void setCrcCheckDisabled()
public ChunkSeqReaderPng getChunkseq()
ChunkSeqReaderPng
objectprotected ChunkSeqReaderPng createChunkSeqReader()
public void prepareSimpleDigestComputation()
getSimpleDigestHex()
public String getSimpleDigestHex()
prepareSimpleDigestComputation()
has been called, and all rows have been read. Not fool-proof, not
cryptografically secure, only for informal testing and duplicates detection.public String toStringCompact()
public ImageInfo getImgInfo()
public ImageInfo getCurImgInfo()
Copyright © 2014. All rights reserved.