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.
| Modifier and Type | Field and Description |
|---|---|
protected ChunkSeqReaderPng |
chunkseq |
ImageInfo |
imgInfo
Basic image info - final and inmutable.
|
protected IImageLineSet<? extends IImageLine> |
imlinesSet |
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 |
protected BufferedStreamFeeder |
streamFeeder |
| Constructor and Description |
|---|
PngReader(File file)
Constructs a PngReader opening a file.
|
PngReader(InputStream inputStream)
Construct a PngReader object from a stream, with default options.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addChunkToSkip(String chunkToSkip) |
void |
close()
Releases resources, and closes stream if corresponds.
|
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 |
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).
|
PngMetadata |
getMetadata()
High level wrapper over chunksList
|
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) |
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.
|
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 ChunkSeqReaderPng chunkseq
protected BufferedStreamFeeder streamFeeder
protected final PngMetadata metadata
protected int rowNum
protected IImageLineSet<? extends IImageLine> imlinesSet
public PngReader(InputStream inputStream)
inputStream - PNG streampublic 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 - ChunkLoadBehaviourpublic 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 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)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)
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 setShouldCloseStream(boolean shouldCloseStream)
default=true
public void end()
public void close()
public boolean isInterlaced()
public void setCrcCheckDisabled()
public ChunkSeqReaderPng getChunkseq()
ChunkSeqReaderPng objectCopyright © 2013. All rights reserved.