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: remove thumbnails and improve label/macro detection #4144

Merged
merged 4 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions components/formats-api/src/loci/formats/MetadataList.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,27 @@ public void set(int i1, int i2, T value) {
data.get(i1).set(i2, value);
}

/**
* Remove the array element at the specified indexes.
*
* @param i1 The primary array index
* @param i2 The secondary array index
* @return the removed element
*/
public T remove(int i1, int i2) {
return data.get(i1).remove(i2);
}

/**
* Remove the entire primary array element at the specified index.
*
* @param i1 The primary array index
* @return the removed element
*/
public List<T> remove(int i1) {
return data.remove(i1);
}

/**
* Add a empty primary array element.
*/
Expand Down
66 changes: 62 additions & 4 deletions components/formats-gpl/src/loci/formats/in/SVSReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public class SVSReader extends BaseTiffReader {

// -- Constants --

public static final String REMOVE_THUMBNAIL_KEY = "svs.remove_thumbnail";
public static final boolean REMOVE_THUMBNAIL_DEFAULT = true;

/** Logger for this class. */
private static final Logger LOGGER =
LoggerFactory.getLogger(SVSReader.class);
Expand Down Expand Up @@ -118,8 +121,19 @@ public SVSReader() {

public SVSReader(String name, String[] suffixes) {
super(name, suffixes);
}

}

// -- SVSReader API methods --

public boolean removeThumbnail() {
MetadataOptions options = getMetadataOptions();
if (options instanceof DynamicMetadataOptions) {
return ((DynamicMetadataOptions) options).getBoolean(
REMOVE_THUMBNAIL_KEY, REMOVE_THUMBNAIL_DEFAULT);
}
return REMOVE_THUMBNAIL_DEFAULT;
}

// -- IFormatReader API methods --

/* @see loci.formats.IFormatReader#fileGroupOption(String) */
Expand Down Expand Up @@ -389,9 +403,15 @@ protected void initStandardMetadata() throws FormatException, IOException {
for (int i=0; i<seriesCount; i++) {
setSeries(i);
int index = i;
tiffParser.fillInIFD(ifds.get(index));

String comment = ifds.get(index).getComment();
IFD currentIFD = ifds.get(index);
tiffParser.fillInIFD(currentIFD);

String comment = currentIFD.getComment();
int subfileType = currentIFD.getIFDIntValue(IFD.NEW_SUBFILE_TYPE);

// if there is no identifying comment, assign this IFD
// to the label or macro (if a label or macro was not already found)
if (comment == null) {
if (labelIndex == -1) {
labelIndex = i;
Expand All @@ -401,10 +421,15 @@ else if (macroIndex == -1) {
}
continue;
}

// when the comment exists, check it for any information that
// identifies the image type
comments[i] = comment;
String[] lines = comment.split("\n");
String[] tokens;
String key, value;
boolean foundLabel = false;
boolean foundMacro = false;
for (String line : lines) {
tokens = line.split("[|]");
for (String t : tokens) {
Expand All @@ -420,15 +445,28 @@ else if (key.equals("OffsetZ")) {
}
else if (t.toLowerCase().indexOf("label") >= 0) {
labelIndex = i;
foundLabel = true;
}
else if (t.toLowerCase().indexOf("macro") >= 0) {
macroIndex = i;
foundMacro = true;
}
}
}
if (zPosition[index] != null) {
uniqueZ.add(zPosition[index]);
}

// if the comment existed but didn't identify a label or macro
// check the subfile type to see if we suspect a label or macro anyway
if (!foundLabel && !foundMacro && subfileType != 0) {
if (labelIndex == -1) {
labelIndex = i;
}
else if (macroIndex == -1) {
macroIndex = i;
}
}
}
setSeries(0);

Expand Down Expand Up @@ -603,6 +641,26 @@ else if (t.toLowerCase().indexOf("macro") >= 0) {
setSeries(0);

core.reorder();

if (removeThumbnail()) {
// if the smallest resolution uses strips instead of tiles
// then it's a "thumbnail" image instead of a real resolution
// remove it by default, see https://github.com/ome/bioformats/issues/3757
IFD lastResolution = ifds.get(getIFDIndex(core.size(0) - 1, 0));
if (lastResolution.get(IFD.STRIP_BYTE_COUNTS) != null) {
int index = core.flattenedIndex(0, core.size(0) - 1);
core.remove(0, core.size(0) - 1);

// update the label and macro indexes
// otherwise image names won't be set correctly
if (index < labelIndex) {
labelIndex--;
}
if (index < macroIndex) {
macroIndex--;
}
}
}
}

/* @see loci.formats.BaseTiffReader#initMetadataStore() */
Expand Down