Skip to content

Commit

Permalink
Add scaling rounded by default to 4 decimal places in determining mip…
Browse files Browse the repository at this point in the history
…maptransforms and mipmapresolutions.
  • Loading branch information
kgabor committed May 13, 2024
1 parent fd47601 commit 8bc606c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/main/java/bdv/img/omezarr/XmlIoZarrImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import bdv.spimdata.XmlIoSpimDataMinimal;

import java.io.File;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

Expand Down Expand Up @@ -110,15 +111,13 @@ public ZarrImageLoader fromXml(final Element elem, final File basePath,
}

public static void main(String[] args) throws SpimDataException {
// final String fn = "/home/gkovacs/data/davidf_zarr_dataset.xml";
final String fn = "/home/gabor.kovacs/data/davidf_zarr_dataset.xml";
// final String fn = "/home/gabor.kovacs/data/zarr_reader_test_2022-11-16/bdv_zarr_test3.xml";
// final String fn = "/Users/kgabor/data/davidf_zarr_dataset.xml";
final String fn = "/home/gkovacs/data/davidf_sample_dataset/davidf_zarr_dataset.xml";
// final String fn = "/home/gabor.kovacs/data/davidf_zarr_dataset.xml";
final SpimDataMinimal spimData = new XmlIoSpimDataMinimal().load(fn);
final ViewerImgLoader imgLoader = (ViewerImgLoader) spimData.getSequenceDescription().getImgLoader();
final ViewerSetupImgLoader<?, ?> setupImgLoader = imgLoader.getSetupImgLoader(0);
int d = setupImgLoader.getImage(0).numDimensions();
setupImgLoader.getMipmapResolutions();
System.out.println(Arrays.toString(setupImgLoader.getMipmapTransforms()));
// BigDataViewer.open(spimData, "BigDataViewer Zarr Example", new ProgressWriterConsole(), ViewerOptions.options());
System.out.println("imgLoader = " + imgLoader);
System.out.println("setupimgLoader = " + setupImgLoader);
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/bdv/img/omezarr/ZarrImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
*/
public class ZarrImageLoader implements ViewerImgLoader, MultiResolutionImgLoader, Closeable {
// private final String zpath;
private static final int[] POWERS_OF_10 = {1, 10, 100, 1000, 10000, 100000,
1000000, 10000000, 100000000, 1000000000};
private final SortedMap<ViewId, String> zgroups;
private final AbstractSequenceDescription<?, ?, ?> seq;
private final MultiscaleImage.ZarrKeyValueReaderBuilder zarrKeyValueReaderBuilder;
Expand All @@ -83,6 +85,13 @@ public class ZarrImageLoader implements ViewerImgLoader, MultiResolutionImgLoade

private SortedMap<Integer, SetupImgLoader> setupImgLoaders;

public static double roundDouble(double s, int decimals) {
if (decimals < 0)
return s;
if (decimals >= POWERS_OF_10.length)
throw new IllegalArgumentException("Decimals out of range");
return Math.rint(s * POWERS_OF_10[decimals]) / (double) POWERS_OF_10[decimals];
}

public ZarrImageLoader(final MultiscaleImage.ZarrKeyValueReaderBuilder zarrKeyValueReaderBuilder, final SortedMap<ViewId, String> zgroups, final AbstractSequenceDescription<?, ?, ?> sequenceDescription) {
this.zgroups = zgroups;
Expand Down Expand Up @@ -296,6 +305,10 @@ private AffineTransform3D concatenateOMETransforms(final Multiscales mscales,
return affT;
}


private void calculateMipmapTransforms() {
calculateMipmapTransforms(4);
}
/**
* Create the 3D affine transformations and calculate the resolution factors
* for the multi resolution display relative to the first resolution.
Expand All @@ -315,7 +328,7 @@ private AffineTransform3D concatenateOMETransforms(final Multiscales mscales,
*
* <p>Set "mipmaptransforms" and "mipmapresolutions".</p>
*/
private void calculateMipmapTransforms() {
private void calculateMipmapTransforms(int decimals) {
// Assume everything is the same in case there are multiple timepoints
final Multiscales mscale = tpMmultiscaleImages.get(0).getMultiscales();
final int numResolutions = tpMmultiscaleImages.get(0).numResolutions();
Expand All @@ -335,15 +348,19 @@ private void calculateMipmapTransforms() {
for (int j = 1; j < numResolutions; ++j) {
final AffineTransform3D affT = T0inv.copy();
affT.concatenate(mipmaptransforms[j]);
affT.set(roundDouble(affT.get(0, 0), decimals),0,0);
affT.set(roundDouble(affT.get(1, 1), decimals),1,1);
affT.set(roundDouble(affT.get(2, 2), decimals),2,2);
mipmaptransforms[j] = affT;
}

// Copy out the scaling from the transformations to the mipmapresolutions
mipmapresolutions = new double[numResolutions][];
for (int j = 0; j < numResolutions; ++j) {
mipmapresolutions[j] = new double[]{mipmaptransforms[j].get(0, 0),
mipmapresolutions[j] = new double[]{
mipmaptransforms[j].get(0, 0),
mipmaptransforms[j].get(1, 1),
mipmaptransforms[j].get(2, 2)};
mipmaptransforms[j].get(2, 2) };
}

// Add pixel center correction
Expand All @@ -353,7 +370,6 @@ private void calculateMipmapTransforms() {
0.5 * (mipmapresolutions[j][1] - 1.),
0.5 * (mipmapresolutions[j][2] - 1.));
}

}

@Override
Expand Down

0 comments on commit 8bc606c

Please sign in to comment.