Skip to content

Commit

Permalink
Various fixes to allow retrieval of pixel data from truncated TIFFs.
Browse files Browse the repository at this point in the history
  • Loading branch information
melissalinkert committed Jan 4, 2011
1 parent 3e7c104 commit 48a5c38
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
7 changes: 6 additions & 1 deletion components/bio-formats/src/loci/formats/tiff/IFD.java
Expand Up @@ -709,6 +709,7 @@ public long[] getStripOffsets() throws FormatException {
if (isTiled() && offsets == null) {
offsets = getIFDLongArray(STRIP_OFFSETS);
}
if (offsets == null) return null;

for (int i=0; i<offsets.length; i++) {
if (offsets[i] < 0) {
Expand Down Expand Up @@ -750,6 +751,7 @@ public long[] getStripByteCounts() throws FormatException {
// don't rely on RowsPerStrip, since it's likely that if the file doesn't
// have the StripByteCounts tag, it also won't have the RowsPerStrip tag
long[] offsets = getStripOffsets();
if (offsets == null) return null;
int bytesPerSample = getBytesPerSample()[0];
long imageWidth = getImageWidth();
long imageLength = getImageLength();
Expand Down Expand Up @@ -877,7 +879,10 @@ public void printIFD() {
for (Integer tag : keySet()) {
Object value = get(tag);
String v = null;
if ((value instanceof Boolean) || (value instanceof Number) ||
if (value == null) {
LOGGER.trace("\t{}=null", getIFDTagName(tag.intValue()));
}
else if ((value instanceof Boolean) || (value instanceof Number) ||
(value instanceof String) || (value instanceof PhotoInterp) ||
(value instanceof TiffCompression) || (value instanceof TiffIFDEntry))
{
Expand Down
34 changes: 21 additions & 13 deletions components/bio-formats/src/loci/formats/tiff/TiffParser.java
Expand Up @@ -359,6 +359,7 @@ public IFD getIFD(long offset) throws IOException {
count = (int) ((inputLen - pointer) / bpe);
LOGGER.trace("getIFDs: truncated {} array elements for tag {}",
(oldCount - count), tag);
if (count < 0) count = oldCount;
}
if (count < 0 || count > in.length()) break;

Expand Down Expand Up @@ -400,6 +401,10 @@ public Object getIFDValue(TiffIFDEntry entry) throws IOException {
LOGGER.trace("Reading entry {} from {}; type={}, count={}",
new Object[] {entry.getTag(), offset, type, count});

if (offset >= in.length()) {
return null;
}

if (offset != in.getFilePointer()) {
in.seek(offset);
}
Expand Down Expand Up @@ -661,21 +666,24 @@ public byte[] getSamples(IFD ifd, byte[] buf, int x, int y,
long[] stripOffsets = ifd.getStripOffsets();
long[] stripByteCounts = ifd.getStripByteCounts();

int firstTile = (int) ((y / tileLength) * numTileCols + (x / tileWidth));
int lastTile =
(int) (((y + height) / tileLength) * numTileCols + (x / tileWidth));
lastTile = (int) Math.min(lastTile, stripOffsets.length - 1);
if (stripOffsets != null && stripByteCounts != null) {
long column = x / tileWidth;
int firstTile = (int) ((y / tileLength) * numTileCols + column);
int lastTile =
(int) (((y + height) / tileLength) * numTileCols + column);
lastTile = (int) Math.min(lastTile, stripOffsets.length - 1);

int offset = 0;
for (int tile=firstTile; tile<=lastTile; tile++) {
if (stripByteCounts[tile] == numSamples && pixel > 1) {
stripByteCounts[tile] *= pixel;
}

int offset = 0;
for (int tile=firstTile; tile<=lastTile; tile++) {
if (stripByteCounts[tile] == numSamples && pixel > 1) {
stripByteCounts[tile] *= pixel;
in.seek(stripOffsets[tile]);
int len = (int) Math.min(buf.length - offset, stripByteCounts[tile]);
in.read(buf, offset, len);
offset += len;
}

in.seek(stripOffsets[tile]);
int len = (int) Math.min(buf.length - offset, stripByteCounts[tile]);
in.read(buf, offset, len);
offset += len;
}
return buf;
}
Expand Down

0 comments on commit 48a5c38

Please sign in to comment.