Skip to content

Commit

Permalink
Push 16bit conversion deeper into loading mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
minnerbe committed Nov 21, 2024
1 parent 914c7b1 commit 22f0561
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.net.URLDecoder;
import java.nio.charset.Charset;

import net.imglib2.loops.LoopBuilder;
import net.imglib2.type.numeric.integer.UnsignedShortType;
import org.janelia.saalfeldlab.n5.DataType;
import org.janelia.saalfeldlab.n5.DatasetAttributes;
import org.janelia.saalfeldlab.n5.N5FSReader;
Expand Down Expand Up @@ -170,6 +172,10 @@ public abstract B buildImageProcessor(final int width,

public abstract RandomAccessibleInterval<A> setupTarget(B forImageProcessor);

protected A convert(final A pixel) {
return pixel;
}

public ImageProcessor load(final N5Reader reader,
final String dataSet,
final int width,
Expand All @@ -186,15 +192,8 @@ public ImageProcessor load(final N5Reader reader,
if (xAndYOffsets != null) {
slice = Views.offsetInterval(slice, xAndYOffsets, new long[] {0,1});
}
final IntervalView<Pair<A, A>> pairView = Views.interval(Views.pair(slice, target),
target);
final IterableInterval<Pair<A, A>> pairs = Views.flatIterable(pairView);

pairs.forEach(pair -> {
final A fromPixel = pair.getA();
final A toPixel = pair.getB();
toPixel.set(fromPixel);
});
LoopBuilder.setImages(slice, target).forEachPixel((s, t) -> t.set(convert(s)));

return imageProcessor;
}
Expand Down Expand Up @@ -232,6 +231,13 @@ public RandomAccessibleInterval<ShortType> setupTarget(final ShortProcessor forI
forImageProcessor.getWidth(),
forImageProcessor.getHeight());
}

@Override
// Since all 16bit HDF5 slices we open are converted .dat files, we need to properly convert the pixel values
protected ShortType convert(final ShortType pixel) {
pixel.set(UnsignedShortType.getCodedSignedShortChecked(32768 - pixel.get()));
return pixel;
}
};

public static Helper<FloatType, FloatProcessor> FLOAT_HELPER =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ private static void saveRenderStack(final JavaSparkContext sc,
.flatIterable(
Views.interval(
ArrayImgs.unsignedShorts(
(short[])convert16bit( currentProcessor ).getPixels(),
//(short[]) currentProcessor.getPixels(),
(short[]) currentProcessor.getPixels(),
currentProcessor.getWidth(),
currentProcessor.getHeight()),
outSlice));
Expand All @@ -223,16 +222,4 @@ private static void saveRenderStack(final JavaSparkContext sc,
N5Utils.saveNonEmptyBlock(block, anotherN5Writer, datasetName, gridBlock.gridPosition, new UnsignedShortType(0));
});
}

public static ShortProcessor convert16bit( final ShortProcessor currentProcessor )
{
final short[] array = (short[])currentProcessor.getPixels();

for (int i = 0; i < array.length; ++i) {
final short value = array[i];
array[i] = (value == 0) ? 0 : UnsignedShortType.getCodedSignedShort(32768 - array[i]);
}

return currentProcessor;
}
}

0 comments on commit 22f0561

Please sign in to comment.