Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVS: check for label/macro instead of assuming they exist #3733

Merged
merged 3 commits into from
Nov 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 48 additions & 30 deletions components/formats-gpl/src/loci/formats/in/SVSReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public class SVSReader extends BaseTiffReader {
private ArrayList<String> dyeNames = new ArrayList<String>();

private transient Color displayColor = null;
private int labelIndex = -1;
private int macroIndex = -1;
private int extraImages = 0;

// -- Constructor --

Expand Down Expand Up @@ -181,11 +184,11 @@ public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h)
/* @see loci.formats.IFormatReader#openThumbBytes(int) */
@Override
public byte[] openThumbBytes(int no) throws FormatException, IOException {
if (core.size() == 1 || getSeries() >= getSeriesCount() - 2) {
return super.openThumbBytes(no);
if (core.size() == 1 || getSeries() >= getSeriesCount() - extraImages) {
return FormatTools.openThumbBytes(this, no);
}

int smallestSeries = getSeriesCount() - 3;
int smallestSeries = getSeriesCount() - extraImages - 1;
if (smallestSeries >= 0) {
int thisSeries = getSeries();
int resolution = getResolution();
Expand Down Expand Up @@ -218,6 +221,9 @@ public void close(boolean fileOnly) throws IOException {
time = null;
dyeNames.clear();
displayColor = null;
extraImages = 0;
labelIndex = -1;
macroIndex = -1;
}
}

Expand Down Expand Up @@ -279,6 +285,7 @@ protected void initStandardMetadata() throws FormatException, IOException {
if (comment == null) {
continue;
}
comments[i] = comment;
String[] lines = comment.split("\n");
String[] tokens;
String key, value;
Expand All @@ -295,6 +302,12 @@ else if (key.equals("OffsetZ")) {
zPosition[index] = DataTools.parseDouble(value);
}
}
else if (t.toLowerCase().indexOf("label") >= 0) {
labelIndex = i;
}
else if (t.toLowerCase().indexOf("macro") >= 0) {
macroIndex = i;
}
}
}
if (zPosition[index] != null) {
Expand All @@ -303,11 +316,23 @@ else if (key.equals("OffsetZ")) {
}
setSeries(0);

// based on whether label and/or macro are present,
// determine how many resolutions in pyramid

int resolutions = getSeriesCount();
if (labelIndex >= 0) {
resolutions--;
}
if (macroIndex >= 0) {
resolutions--;
}
extraImages = getSeriesCount() - resolutions;

// repopulate core metadata

// remove any invalid pyramid resolutions
IFD firstIFD = ifds.get(getIFDIndex(0, 0));
for (int s=1; s<getSeriesCount() - 2; s++) {
for (int s=1; s<resolutions; s++) {
int index = getIFDIndex(s, 0);
IFD ifd = ifds.get(index);
tiffParser.fillInIFD(ifd);
Expand All @@ -328,16 +353,17 @@ else if (key.equals("OffsetZ")) {
}
zPosition = uniqueZ.toArray(new Double[uniqueZ.size()]);
Arrays.sort(zPosition);
seriesCount = ((ifds.size() - 2) / uniqueZ.size()) + 2;
seriesCount = ((ifds.size() - extraImages) / uniqueZ.size()) + extraImages;

core.clear();
if (seriesCount > 2) {
if (seriesCount > extraImages) {
core.add();
for (int r=0; r < seriesCount - 2; r++) {
for (int r=0; r < seriesCount - extraImages; r++) {
core.add(0, new SVSCoreMetadata());
}
core.add(new SVSCoreMetadata());
core.add(new SVSCoreMetadata());
for (int extra=0; extra<extraImages; extra++) {
core.add(new SVSCoreMetadata());
}
}
else {
// Should never happen unless the SVS is corrupt?
Expand All @@ -352,8 +378,8 @@ else if (key.equals("OffsetZ")) {

SVSCoreMetadata ms = (SVSCoreMetadata) core.get(pos[0], pos[1]);

if (s == 0 && seriesCount > 2) {
ms.resolutionCount = seriesCount - 2;
if (s == 0 && seriesCount > extraImages) {
ms.resolutionCount = seriesCount - extraImages;
}

IFD ifd = ifds.get(getIFDIndex(s, 0));
Expand All @@ -364,7 +390,7 @@ else if (key.equals("OffsetZ")) {

ms.sizeX = (int) ifd.getImageWidth();
ms.sizeY = (int) ifd.getImageLength();
if (s < seriesCount - 2) {
if (s < seriesCount - extraImages) {
ms.sizeZ = uniqueZ.size();
}
else {
Expand Down Expand Up @@ -472,26 +498,18 @@ protected void initMetadataStore() throws FormatException {
store.setImageInstrumentRef(instrument, i);
store.setObjectiveSettingsID(objective, i);

if (hasFlattenedResolutions() || i > 2) {
if (hasFlattenedResolutions() || i > extraImages) {
store.setImageName("Series " + (i + 1), i);
}
else {
switch (i) {
case 0:
store.setImageName("", i);
break;
case 1:
// if there are only two images, assume that there is no label
if (lastImage == 1) {
store.setImageName("macro image", i);
}
else {
store.setImageName("label image", i);
}
break;
case 2:
store.setImageName("macro image", i);
break;
if (i == 0) {
store.setImageName("", i);
}
else if (i == labelIndex) {
store.setImageName("label image", i);
}
else if (i == macroIndex) {
store.setImageName("macro image", i);
}
}
String comment = ((SVSCoreMetadata) getCurrentCore()).comment;
Expand Down Expand Up @@ -538,7 +556,7 @@ protected void initMetadataStore() throws FormatException {

private int getIFDIndex(int coreIndex, int no) {
int index = coreIndex;
int coreCount = core.flattenedSize() - 2;
int coreCount = core.flattenedSize() - extraImages;
if (coreIndex > 0 && coreIndex < coreCount) {
if (core.get(0, 0).imageCount > 1) {
index++;
Expand Down