diff --git a/src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/MetadataUtils.java b/src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/MetadataUtils.java index e994d4b..e322a35 100644 --- a/src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/MetadataUtils.java +++ b/src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/MetadataUtils.java @@ -1,8 +1,6 @@ package org.janelia.saalfeldlab.n5.universe.metadata; import java.net.URISyntaxException; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Arrays; import java.util.HashMap; @@ -25,6 +23,7 @@ import net.imglib2.realtransform.Translation; import net.imglib2.realtransform.Translation2D; import net.imglib2.realtransform.Translation3D; +import net.imglib2.util.Pair; public class MetadataUtils { @@ -112,6 +111,32 @@ public static CoordinateTransformation[] buildScaleTranslationTransformList(f return coordinateTransformations; } + public static ScaleAndTranslation scaleTranslationFromCoordinateTransformations(final CoordinateTransformation[] cts) { + + if (cts == null || cts.length == 0) + return null; + + ScaleAndTranslation out = coordinateTransformToScaleAndTranslation(cts[0]); + for (int i = 1; i < cts.length; i++) { + out.preConcatenate(coordinateTransformToScaleAndTranslation(cts[i])); + } + return out; + } + + public static ScaleAndTranslation coordinateTransformToScaleAndTranslation(CoordinateTransformation ct) { + + if (ct.getType().equals(ScaleCoordinateTransformation.TYPE)) { + final double[] s = ((ScaleCoordinateTransformation)ct).getScale(); + return new ScaleAndTranslation(s, new double[s.length]); + } else if (ct.getType().equals(TranslationCoordinateTransformation.TYPE)) { + final double[] t = ((TranslationCoordinateTransformation)ct).getTranslation(); + final double[] s = new double[t.length]; + Arrays.fill(s, 1.0); + return new ScaleAndTranslation(s, t); + } + return null; + } + /** * Returns a new {@link N5SingleScaleMetadata} equal to the baseMetadata, but with * {@link DatasetAttributes} coming from datasetMetadata.ew diff --git a/src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/ome/ngff/v04/NgffSingleScaleAxesMetadata.java b/src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/ome/ngff/v04/NgffSingleScaleAxesMetadata.java index ea724c5..4ac44be 100644 --- a/src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/ome/ngff/v04/NgffSingleScaleAxesMetadata.java +++ b/src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/ome/ngff/v04/NgffSingleScaleAxesMetadata.java @@ -60,8 +60,8 @@ public NgffSingleScaleAxesMetadata(final String path, this.datasetAttributes = datasetAttributes; - // TODO generalize for translation first coordinateTransformations = MetadataUtils.buildScaleTranslationTransformList(scale, translation); + this.transform = MetadataUtils.scaleTranslationTransforms(scale, translation); } diff --git a/src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/ome/ngff/v04/NgffSingleScaleMetadataParser.java b/src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/ome/ngff/v04/NgffSingleScaleMetadataParser.java index 3bb2e81..9d6beb0 100644 --- a/src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/ome/ngff/v04/NgffSingleScaleMetadataParser.java +++ b/src/main/java/org/janelia/saalfeldlab/n5/universe/metadata/ome/ngff/v04/NgffSingleScaleMetadataParser.java @@ -8,6 +8,7 @@ import org.janelia.saalfeldlab.n5.N5Reader; import org.janelia.saalfeldlab.n5.N5Writer; import org.janelia.saalfeldlab.n5.universe.N5TreeNode; +import org.janelia.saalfeldlab.n5.universe.metadata.MetadataUtils; import org.janelia.saalfeldlab.n5.universe.metadata.N5MetadataParser; import org.janelia.saalfeldlab.n5.universe.metadata.N5MetadataWriter; import org.janelia.saalfeldlab.n5.universe.metadata.axes.Axis; @@ -19,6 +20,8 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; +import net.imglib2.realtransform.ScaleAndTranslation; + public class NgffSingleScaleMetadataParser implements N5MetadataParser, N5MetadataWriter { private final Gson gson; @@ -43,14 +46,9 @@ public Optional parseMetadata(final N5Reader n5, fi if (cts == null || cts.length == 0) return Optional.of(new NgffSingleScaleAxesMetadata(node.getPath(), null, null, null)); - double[] scale = null; - double[] translation = null; - for (int i = 0; i < cts.length; i++) { - if (cts[i] instanceof ScaleCoordinateTransformation) - scale = ((ScaleCoordinateTransformation)cts[i]).getScale(); - else if (cts[i] instanceof TranslationCoordinateTransformation) - translation = ((TranslationCoordinateTransformation)cts[i]).getTranslation(); - } + final ScaleAndTranslation scaleAndTranslation = MetadataUtils.scaleTranslationFromCoordinateTransformations(cts); + final double[] scale = scaleAndTranslation.getScaleCopy(); + final double[] translation = scaleAndTranslation.getTranslationCopy(); final DatasetAttributes dsetAttrs = n5.getDatasetAttributes(node.getPath()); if( OmeNgffMetadataParser.cOrder(dsetAttrs))