Skip to content

Commit

Permalink
perf: use VirtualStackAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
bogovicj committed Aug 7, 2024
1 parent bcee066 commit 976766a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
24 changes: 14 additions & 10 deletions src/main/java/org/janelia/saalfeldlab/n5/ij/N5IJUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@

import ij.ImagePlus;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.converter.Converter;
import net.imglib2.converter.Converters;
import net.imglib2.exception.ImgLibException;
import net.imglib2.img.Img;
import net.imglib2.img.VirtualStackAdapter;
import net.imglib2.img.cell.LazyCellImg;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.img.imageplus.ImagePlusImg;
import net.imglib2.img.imageplus.ImagePlusImgs;
import net.imglib2.img.imageplus.IntImagePlus;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.ARGBType;
import net.imglib2.type.numeric.NumericType;
Expand Down Expand Up @@ -614,19 +615,22 @@ public static void saveMaskedUnsignedByte(
* the ImagePlus
* @return the wrapped image
*/
public static Img<UnsignedIntType> wrapRgbAsInt(final ImagePlus imp) {
public static RandomAccessibleInterval<UnsignedIntType> wrapRgbAsInt(final ImagePlus image) {

if (imp.getType() != ImagePlus.COLOR_RGB)
return null;
if (image.getType() != ImagePlus.COLOR_RGB)
throw new IllegalArgumentException();

final IntImagePlus<UnsignedIntType> container = new IntImagePlus<>(imp);
final RandomAccessibleInterval<ARGBType> wimg = VirtualStackAdapter.wrapRGBA(image);
return Converters.convertRAI(wimg,
new Converter<ARGBType, UnsignedIntType>() {

// create a Type that is linked to the container
final UnsignedIntType linkedType = new UnsignedIntType(container);
@Override
public void convert(ARGBType input, UnsignedIntType output) {

// pass it to the DirectAccessContainer
container.setLinkedType(linkedType);
output.set(input.get());
}
},
new UnsignedIntType());

return container;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
package org.janelia.saalfeldlab.n5.ij;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
Expand Down Expand Up @@ -119,6 +118,7 @@
import net.imglib2.Interval;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.RealRandomAccessible;
import net.imglib2.img.VirtualStackAdapter;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.interpolation.randomaccess.NLinearInterpolatorFactory;
import net.imglib2.realtransform.AffineGet;
Expand Down Expand Up @@ -496,7 +496,7 @@ public <T extends RealType<T> & NativeType<T>, M extends N5DatasetMetadata, N ex
.zarrDimensionSeparator("/")
.openReader(rootWithFormatPrefix);
doGroupExistsWarning = n5Reader.exists("");
} catch (N5Exception e) {
} catch (final N5Exception e) {
// an exception may be thrown if the container does not exist
// in which case we should skip group existence check
// because it will be created below
Expand Down Expand Up @@ -594,9 +594,9 @@ public <T extends RealType<T> & NativeType<T>, M extends N5DatasetMetadata, N ex
}

// update metadata to reflect this scale level, returns new metadata instance
currentMetadata = (M)metadataForThisScale(dset, currentMetadata, downsampleMethod,
currentMetadata = (M)metadataForThisScale(dset, currentMetadata, downsampleMethod,
baseResolution,
currentAbsoluteDownsampling,
currentAbsoluteDownsampling,
currentResolution,
currentTranslation);

Expand Down Expand Up @@ -900,7 +900,7 @@ protected <T extends NumericType<T>> RandomAccessibleInterval<T> getBaseImage()
if (image.getType() == ImagePlus.COLOR_RGB)
baseImg = (RandomAccessibleInterval<T>)(N5IJUtils.wrapRgbAsInt(image));
else
baseImg = (RandomAccessibleInterval<T>)ImageJFunctions.wrap(image);
baseImg = (RandomAccessibleInterval<T>)VirtualStackAdapter.wrap(image);

return baseImg;
}
Expand Down Expand Up @@ -1046,7 +1046,7 @@ protected <M extends N5Metadata> void writeMetadata(final M metadata, final N5Wr
if (writer != null)
try {
((N5MetadataWriter<M>)writer).writeMetadata(metadata, n5, dataset);
} catch (Exception e) {}
} catch (final Exception e) {}
}
}

Expand All @@ -1061,7 +1061,7 @@ protected N5SingleScaleMetadata buildN5VMetadata(
* N5Viewer metadata doesn't have a way to directly represent offset.
* Rather, the half-pixel offsets that averaging downsampling introduces
* are assumed when downsampling factors are not equal to ones.
*
*
* As a result, we use downsampling factors with average downsampling,
* but set the factors to one otherwise.
*/
Expand Down Expand Up @@ -1151,7 +1151,7 @@ protected NgffSingleScaleAxesMetadata buildNgffMetadata(
* Because {@link N5Writer}s create the root group on creation by default,
* if the given dataset is the root, we should ignore its existence
* for the purpose of warning the user if it was created by this plugin
* by the N5Writer.
* by the N5Writer.
*
* @param n5 the n5 writer
* @param dataset the dataset
Expand Down Expand Up @@ -1226,7 +1226,7 @@ private static String needOverwrite(final N5Reader n5, final String path, final
return currentPath;
}

// also need to overwrite if the given path
// also need to overwrite if the given path
// has any child group or datasets
if (n5.exists(path)) {
final String[] children = n5.list(path);
Expand Down Expand Up @@ -1399,7 +1399,7 @@ private final boolean promptHomeDirectoryWarning(final String root) {
return true;
}

} catch (IOException e) {}
} catch (final IOException e) {}

return false;
}
Expand Down Expand Up @@ -1428,7 +1428,7 @@ private final boolean promptOverwriteWarning(final N5Writer n5, final String roo
try {
final LegacyApplicationFrame appFrame = (LegacyApplicationFrame)ui.getDefaultUI().getApplicationFrame();
parentFrame = appFrame.getComponent();
} catch (Exception e) {
} catch (final Exception e) {
parentFrame = new JFrame();
}

Expand Down

0 comments on commit 976766a

Please sign in to comment.