Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:openmicroscopy/bioformats into d…
Browse files Browse the repository at this point in the history
…icom-reader-precompressed
  • Loading branch information
melissalinkert committed Aug 8, 2024
2 parents 93e38d8 + 0a895ae commit 00ac6cc
Show file tree
Hide file tree
Showing 14 changed files with 557 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,14 @@ private boolean doTileConversion(IFormatWriter writer, String outputFile)
{
MetadataStore r = reader.getMetadataStore();
if ((r instanceof IPyramidStore) && ((IPyramidStore) r).getResolutionCount(reader.getSeries()) > 1) {
return true;
// if we asked to try a precompressed conversion,
// then the writer's tile sizes will have been set automatically
// according to the input data
// the conversion must then be performed tile-wise to match the tile sizes,
// even if precompression doesn't end up being possible
if (precompressed) {
return true;
}
}
return DataTools.safeMultiply64(width, height) >= DataTools.safeMultiply64(4096, 4096) ||
saveTileWidth > 0 || saveTileHeight > 0;
Expand Down
6 changes: 4 additions & 2 deletions components/formats-bsd/src/loci/formats/gui/PreviewPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ public void run() {

try { // catch-all for unanticipated exceptions
final String id = loadId;
if (id == lastId) continue;
if ((id == null && lastId == null) || (id != null && id.equals(lastId))) {
continue;
}
if (id != null && lastId != null) {
String[] files = reader.getUsedFiles();
boolean found = false;
Expand Down Expand Up @@ -260,7 +262,7 @@ public void run() {
lastId = null;
continue;
}
if (id != loadId) {
if (!id.equals(loadId)) {
SwingUtilities.invokeLater(refresher);
continue;
}
Expand Down
10 changes: 6 additions & 4 deletions components/formats-bsd/src/loci/formats/in/ICSReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1479,10 +1479,12 @@ else if (labels.equalsIgnoreCase("x y t")) {
MetadataTools.populatePixels(store, this, true);

// populate Image data
imageName = imageName.replace('/', File.separatorChar);
imageName = imageName.replace('\\', File.separatorChar);
imageName = imageName.substring(imageName.lastIndexOf(File.separator) + 1);
store.setImageName(imageName, 0);
if (imageName != null) {
imageName = imageName.replace('/', File.separatorChar);
imageName = imageName.replace('\\', File.separatorChar);
imageName = imageName.substring(imageName.lastIndexOf(File.separator) + 1);
store.setImageName(imageName, 0);
}

if (date != null) store.setImageAcquisitionDate(new Timestamp(date), 0);

Expand Down
69 changes: 69 additions & 0 deletions components/formats-bsd/src/loci/formats/in/MinimalTiffReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -705,4 +705,73 @@ protected void initTiffParser() {
tiffParser.setUse64BitOffsets(use64Bit);
}

/**
* Get the index of the tile corresponding to given IFD (plane)
* and tile XY indexes.
*
* @param ifd IFD for the requested tile's plane
* @param x tile X index
* @param y tile Y index
* @return corresponding tile index
*/
protected int getTileIndex(IFD ifd, int x, int y) throws FormatException {
int rows = (int) ifd.getTilesPerColumn();
int cols = (int) ifd.getTilesPerRow();

if (x < 0 || x >= cols) {
throw new IllegalArgumentException("X index " + x + " not in range [0, " + cols + ")");
}
if (y < 0 || y >= rows) {
throw new IllegalArgumentException("Y index " + y + " not in range [0, " + rows + ")");
}

return (cols * y) + x;
}

protected long getCompressedByteCount(IFD ifd, int x, int y) throws FormatException, IOException {
long[] byteCounts = ifd.getStripByteCounts();
int tileIndex = getTileIndex(ifd, x, y);
byte[] jpegTable = (byte[]) ifd.getIFDValue(IFD.JPEG_TABLES);
int jpegTableBytes = jpegTable == null ? 0 : jpegTable.length - 2;
long expectedBytes = byteCounts[tileIndex];
if (expectedBytes > 0) {
expectedBytes += jpegTableBytes;
}
if (expectedBytes < 0 || expectedBytes > Integer.MAX_VALUE) {
throw new IOException("Invalid compressed tile size: " + expectedBytes);
}
return expectedBytes;
}

protected byte[] copyTile(IFD ifd, byte[] buf, int x, int y) throws FormatException, IOException {
long[] offsets = ifd.getStripOffsets();
long[] byteCounts = ifd.getStripByteCounts();

int tileIndex = getTileIndex(ifd, x, y);

byte[] jpegTable = (byte[]) ifd.getIFDValue(IFD.JPEG_TABLES);
int jpegTableBytes = jpegTable == null ? 0 : jpegTable.length - 2;
long expectedBytes = getCompressedByteCount(ifd, x, y);

if (buf.length < expectedBytes) {
throw new IllegalArgumentException("Tile buffer too small: expected >=" +
expectedBytes + ", got " + buf.length);
}
else if (expectedBytes < 0 || expectedBytes > Integer.MAX_VALUE) {
throw new IOException("Invalid compressed tile size: " + expectedBytes);
}

if (jpegTable != null && expectedBytes > 0) {
System.arraycopy(jpegTable, 0, buf, 0, jpegTable.length - 2);
// skip over the duplicate SOI marker
tiffParser.getStream().seek(offsets[tileIndex] + 2);
tiffParser.getStream().readFully(buf, jpegTable.length - 2, (int) byteCounts[tileIndex]);
}
else if (byteCounts[tileIndex] > 0) {
tiffParser.getStream().seek(offsets[tileIndex]);
tiffParser.getStream().readFully(buf, 0, (int) byteCounts[tileIndex]);
}
return buf;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ protected void initFile(String id) throws FormatException, IOException {
Integer t = omexmlMeta.getPixelsSizeT(i).getValue();
Integer z = omexmlMeta.getPixelsSizeZ(i).getValue();
Integer c = omexmlMeta.getPixelsSizeC(i).getValue();
if (w == null || h == null || t == null || z == null | c == null) {
if (w == null || h == null || t == null || z == null || c == null) {
throw new FormatException("Image dimensions not found");
}

Expand Down
Loading

0 comments on commit 00ac6cc

Please sign in to comment.