Skip to content
27 changes: 27 additions & 0 deletions components/formats-api/src/loci/formats/FormatTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,33 @@ public static Length getPhysicalSize(Double value, String unit) {
return null;
}

/**
* Calculate a physical size representing a scaled dimension (e.g. subresolution of a pyramid)
* using the original physical size and the scale factor between two lengths in pixels.
*
* @param original physical size of the source dimension
* @param originalPixels size in pixels of the source dimension
* @param scaledPixels size in pixels of the scaled dimension
*/
public static Length getScaledPhysicalSize(Length original, int originalPixels, int scaledPixels) {
if (scaledPixels == 0) {
return null;
}
double scale = (double) originalPixels / scaledPixels;
return getScaledPhysicalSize(original, scale);
}

/**
* Calculate a physical size representing a scaled dimension (e.g. subresolution of a pyramid)
* using the original physical size and the given scale factor.
*
* @param original physical size of the source dimension
* @param scale value by which to multiply the original size
*/
public static Length getScaledPhysicalSize(Length original, double scale) {
return new Length(original.value().doubleValue() * scale, original.unit());
}

/**
* Formats the input value for the physical size in X into a length in
* microns
Expand Down
14 changes: 11 additions & 3 deletions components/formats-bsd/src/loci/formats/in/BDVReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,17 @@ else if (bpp==4) {
}

if (setupSizes != null && setupSizes.size() == 3) {
store.setPixelsPhysicalSizeX(setupSizes.get(0), coreIndexToSeries(seriesCount));
store.setPixelsPhysicalSizeY(setupSizes.get(1), coreIndexToSeries(seriesCount));
store.setPixelsPhysicalSizeZ(setupSizes.get(2), coreIndexToSeries(seriesCount));
int seriesIndex = coreIndexToSeries(seriesCount);
Length scaledX = FormatTools.getScaledPhysicalSize(setupSizes.get(0),
core.get(0).sizeX, core.get(seriesCount).sizeX);
Length scaledY = FormatTools.getScaledPhysicalSize(setupSizes.get(1),
core.get(0).sizeY, core.get(seriesCount).sizeY);
Length scaledZ = FormatTools.getScaledPhysicalSize(setupSizes.get(2),
core.get(0).sizeZ, core.get(seriesCount).sizeZ);

store.setPixelsPhysicalSizeX(scaledX, seriesIndex);
store.setPixelsPhysicalSizeY(scaledY, seriesIndex);
store.setPixelsPhysicalSizeZ(scaledZ, seriesIndex);
}
if (getResolution() == 0) {
seriesNames.add(String.format("P_%s, W_%s_%s", coord.timepoint, coord.setup, coord.mipmapLevel));
Expand Down
27 changes: 21 additions & 6 deletions components/formats-bsd/src/loci/formats/in/BaseTiffReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public abstract class BaseTiffReader extends MinimalTiffReader {
"yyyy-MM-dd'T'HH:mm:ssZ"
};

protected Length physicalSizeX;
protected Length physicalSizeY;

// -- Constructors --

/** Constructs a new BaseTiffReader. */
Expand All @@ -91,6 +94,18 @@ public BaseTiffReader(String name, String[] suffixes) {
super(name, suffixes);
}

// -- IFormatReader API methods --

/* @see loci.formats.IFormatReader#close(boolean) */
@Override
public void close(boolean fileOnly) throws IOException {
super.close(fileOnly);
if (!fileOnly) {
physicalSizeX = null;
physicalSizeY = null;
}
}

// -- Internal BaseTiffReader API methods --

/** Populates the metadata hashtable and metadata store. */
Expand Down Expand Up @@ -474,14 +489,14 @@ protected void initMetadataStore() throws FormatException {
unit = getResolutionUnitFromComment(firstIFD);
}

Length sizeX = FormatTools.getPhysicalSizeX(pixX, unit);
Length sizeY = FormatTools.getPhysicalSizeY(pixY, unit);
physicalSizeX = FormatTools.getPhysicalSizeX(pixX, unit);
physicalSizeY = FormatTools.getPhysicalSizeY(pixY, unit);

if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, 0);
if (physicalSizeX != null) {
store.setPixelsPhysicalSizeX(physicalSizeX, 0);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, 0);
if (physicalSizeY != null) {
store.setPixelsPhysicalSizeY(physicalSizeY, 0);
}
store.setPixelsPhysicalSizeZ(null, 0);

Expand Down
23 changes: 18 additions & 5 deletions components/formats-bsd/src/loci/formats/in/FakeReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ public class FakeReader extends FormatReader {
private transient int fields = 0;
private transient int plateAcqs = 0;

private transient int resolutionCount = 1;
private transient int resolutionScale = DEFAULT_RESOLUTION_SCALE;

// Misc. debugging
private int sleepOpenBytes = 0;
private int sleepInitFile = 0;
Expand Down Expand Up @@ -586,6 +589,8 @@ public void close(boolean fileOnly) throws IOException {
plateCols = 0;
fields = 0;
plateAcqs = 0;
resolutionCount = 1;
resolutionScale = DEFAULT_RESOLUTION_SCALE;
labelPlanes = false;
excitationWavelengths.clear();
emissionWavelengths.clear();
Expand Down Expand Up @@ -690,8 +695,6 @@ protected void initFile(String id) throws FormatException, IOException {
boolean withInstrument = false;

int seriesCount = 1;
int resolutionCount = 1;
int resolutionScale = DEFAULT_RESOLUTION_SCALE;
int lutLength = 3;

String acquisitionDate = null;
Expand Down Expand Up @@ -1046,9 +1049,19 @@ public void reopenFile() throws IOException {
private void fillPhysicalSizes(MetadataStore store) {
if (physicalSizeX == null && physicalSizeY == null && physicalSizeZ == null) return;
for (int s=0; s<getSeriesCount(); s++) {
store.setPixelsPhysicalSizeX(physicalSizeX, s);
store.setPixelsPhysicalSizeY(physicalSizeY, s);
store.setPixelsPhysicalSizeZ(physicalSizeZ, s);
Length scaledX = physicalSizeX;
Length scaledY = physicalSizeY;
Length scaledZ = physicalSizeZ;
if (hasFlattenedResolutions()) {
int res = s % resolutionCount;
double resScale = Math.pow(resolutionScale, res);
scaledX = FormatTools.getScaledPhysicalSize(scaledX, resScale);
scaledY = FormatTools.getScaledPhysicalSize(scaledY, resScale);
scaledZ = FormatTools.getScaledPhysicalSize(scaledZ, resScale);
}
store.setPixelsPhysicalSizeX(scaledX, s);
store.setPixelsPhysicalSizeY(scaledY, s);
store.setPixelsPhysicalSizeZ(scaledZ, s);
}
}

Expand Down
Loading
Loading