Skip to content

Commit aeb3cfd

Browse files
committed
adds a way to change the background value of sources
1 parent 48d43d2 commit aeb3cfd

File tree

4 files changed

+259
-1
lines changed

4 files changed

+259
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>sc.fiji</groupId>
1313
<artifactId>bigdataviewer-playground</artifactId>
14-
<version>0.11.3-SNAPSHOT</version>
14+
<version>0.12.0-SNAPSHOT</version>
1515

1616
<name>bigdataviewer-playground</name>
1717
<description>BigDataViewer Actions and GUI</description>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package bdv.util;
2+
3+
import bdv.viewer.Interpolation;
4+
import bdv.viewer.Source;
5+
import mpicbg.spim.data.sequence.VoxelDimensions;
6+
import net.imglib2.RandomAccessibleInterval;
7+
import net.imglib2.RealRandomAccessible;
8+
import net.imglib2.realtransform.AffineTransform3D;
9+
import net.imglib2.type.numeric.NumericType;
10+
import net.imglib2.view.ExtendedRandomAccessibleInterval;
11+
import net.imglib2.view.Views;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
public class OutOfBoundsColorChangedSource<T extends NumericType<T>> implements Source<T> {
16+
17+
protected static final Logger logger = LoggerFactory.getLogger(
18+
OutOfBoundsColorChangedSource.class);
19+
20+
transient protected final DefaultInterpolators<T> interpolators =
21+
new DefaultInterpolators<>();
22+
23+
/**
24+
* Origin source of type {@link T}
25+
*/
26+
final Source<T> origin;
27+
28+
final T outOfBoundsValue;
29+
30+
public OutOfBoundsColorChangedSource(Source<T> source, T outOfBoundsValue)
31+
{
32+
this.origin = source;
33+
this.outOfBoundsValue = outOfBoundsValue;
34+
}
35+
36+
@Override
37+
public boolean isPresent(int t) {
38+
return origin.isPresent(t);
39+
}
40+
41+
@Override
42+
public RandomAccessibleInterval<T> getSource(int t, int level) {
43+
return origin.getSource(t, level);
44+
}
45+
46+
@Override
47+
public RealRandomAccessible<T> getInterpolatedSource(int t, int level, Interpolation method) {
48+
ExtendedRandomAccessibleInterval<T, RandomAccessibleInterval<T>> eView =
49+
Views.extendValue(getSource(t, level), outOfBoundsValue);
50+
return Views.interpolate(eView, interpolators.get(method));
51+
}
52+
53+
@Override
54+
public void getSourceTransform(int t, int level, AffineTransform3D transform) {
55+
origin.getSourceTransform(t, level, transform);
56+
}
57+
58+
@Override
59+
public T getType() {
60+
return origin.getType();
61+
}
62+
63+
@Override
64+
public String getName() {
65+
return origin.getName();
66+
}
67+
68+
@Override
69+
public VoxelDimensions getVoxelDimensions() {
70+
return origin.getVoxelDimensions();
71+
}
72+
73+
@Override
74+
public int getNumMipmapLevels() {
75+
return origin.getNumMipmapLevels();
76+
}
77+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*-
2+
* #%L
3+
* BigDataViewer-Playground
4+
* %%
5+
* Copyright (C) 2019 - 2025 Nicolas Chiaruttini, EPFL - Robert Haase, MPI CBG - Christian Tischer, EMBL
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package sc.fiji.bdvpg.sourceandconverter.transform;
31+
32+
import bdv.util.OutOfBoundsColorChangedSource;
33+
import bdv.viewer.Source;
34+
import bdv.viewer.SourceAndConverter;
35+
import net.imglib2.Volatile;
36+
import net.imglib2.type.NativeType;
37+
import net.imglib2.type.numeric.ARGBType;
38+
import net.imglib2.type.numeric.NumericType;
39+
import net.imglib2.type.numeric.integer.UnsignedByteType;
40+
import net.imglib2.type.numeric.integer.UnsignedShortType;
41+
import net.imglib2.type.numeric.real.FloatType;
42+
import net.imglib2.type.volatiles.VolatileARGBType;
43+
import net.imglib2.type.volatiles.VolatileFloatType;
44+
import net.imglib2.type.volatiles.VolatileUnsignedByteType;
45+
import net.imglib2.type.volatiles.VolatileUnsignedShortType;
46+
import sc.fiji.bdvpg.sourceandconverter.SourceAndConverterHelper;
47+
48+
import java.util.function.Function;
49+
50+
public class SourceOutOfBoundsColorChanger<T extends NumericType<T> & NativeType<T>>
51+
implements Runnable, Function<SourceAndConverter<T>, SourceAndConverter<T>>
52+
{
53+
54+
final SourceAndConverter<T> sac_in;
55+
56+
final T outOfBoundsColor;
57+
58+
public SourceOutOfBoundsColorChanger(final SourceAndConverter<T> sac_in,
59+
final T outOfBoundsColor)
60+
{
61+
this.outOfBoundsColor = outOfBoundsColor;
62+
this.sac_in = sac_in;
63+
}
64+
65+
@Override
66+
public void run() {
67+
68+
}
69+
70+
public SourceAndConverter<T> get() {
71+
return apply(sac_in);
72+
}
73+
74+
@Override
75+
public SourceAndConverter<T> apply(final SourceAndConverter<T> src) {
76+
final Source<T> srcNewBg = new OutOfBoundsColorChangedSource<>(src.getSpimSource(), outOfBoundsColor);
77+
78+
SourceAndConverter<T> sac;
79+
if (src.asVolatile() != null) {
80+
SourceAndConverter<? extends Volatile<T>> vsac;
81+
Source<? extends Volatile<T>> vsrcNewBg;
82+
// Rah...
83+
Volatile<T> vOutOfBoundsColor;
84+
if (outOfBoundsColor instanceof UnsignedShortType) {
85+
vOutOfBoundsColor = (Volatile<T>) new VolatileUnsignedShortType(((UnsignedShortType) outOfBoundsColor).get());
86+
} else if (outOfBoundsColor instanceof UnsignedByteType) {
87+
vOutOfBoundsColor = (Volatile<T>) new VolatileUnsignedByteType(((UnsignedByteType) outOfBoundsColor).get());
88+
} else if (outOfBoundsColor instanceof ARGBType) {
89+
vOutOfBoundsColor = (Volatile<T>) new VolatileARGBType(((ARGBType) outOfBoundsColor).get());
90+
} else if (outOfBoundsColor instanceof FloatType) {
91+
vOutOfBoundsColor = (Volatile<T>) new VolatileFloatType(((FloatType) outOfBoundsColor).get());
92+
} else {
93+
throw new RuntimeException("Sorry, can't find matching volatile type of pixel class "+outOfBoundsColor.getClass().getSimpleName()+". Please contribute to bdv-playground SourceOutOfBoundsColorChanger class");
94+
}
95+
96+
vsrcNewBg = new OutOfBoundsColorChangedSource(src.asVolatile().getSpimSource(), (NumericType) vOutOfBoundsColor);
97+
vsac = new SourceAndConverter(vsrcNewBg, SourceAndConverterHelper
98+
.cloneConverter(src.asVolatile().getConverter(), src.asVolatile()));
99+
sac = new SourceAndConverter<>(srcNewBg, SourceAndConverterHelper
100+
.cloneConverter(src.getConverter(), src), vsac);
101+
}
102+
else {
103+
sac = new SourceAndConverter<>(srcNewBg, SourceAndConverterHelper
104+
.cloneConverter(src.getConverter(), src));
105+
}
106+
return sac;
107+
}
108+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*-
2+
* #%L
3+
* BigDataViewer-Playground
4+
* %%
5+
* Copyright (C) 2019 - 2025 Nicolas Chiaruttini, EPFL - Robert Haase, MPI CBG - Christian Tischer, EMBL
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
package sc.fiji.bdvpg;
30+
31+
import bdv.util.BdvHandle;
32+
import bdv.viewer.SourceAndConverter;
33+
import mpicbg.spim.data.generic.AbstractSpimData;
34+
import net.imagej.ImageJ;
35+
import net.imglib2.type.numeric.integer.UnsignedShortType;
36+
import sc.fiji.bdvpg.scijava.services.SourceAndConverterBdvDisplayService;
37+
import sc.fiji.bdvpg.scijava.services.SourceAndConverterService;
38+
import sc.fiji.bdvpg.services.SourceAndConverterServices;
39+
import sc.fiji.bdvpg.sourceandconverter.transform.SourceOutOfBoundsColorChanger;
40+
import sc.fiji.bdvpg.spimdata.importer.SpimDataFromXmlImporter;
41+
42+
public class SourceBgChangerDemo {
43+
44+
static ImageJ ij;
45+
46+
public static void main(String... args) {
47+
// Initializes static SourceService and Display Service
48+
49+
ij = new ImageJ();
50+
TestHelper.startFiji(ij);
51+
52+
// Creates a BdvHandle
53+
BdvHandle bdvHandle =
54+
ij.get(SourceAndConverterBdvDisplayService.class).getActiveBdv();
55+
56+
final String filePath = "src/test/resources/mri-stack.xml";
57+
// Import SpimData
58+
SpimDataFromXmlImporter importer = new SpimDataFromXmlImporter(filePath);
59+
//importer.run();
60+
61+
final AbstractSpimData<?> spimData = importer.get();
62+
63+
SourceAndConverter<UnsignedShortType> sac = (SourceAndConverter<UnsignedShortType>) ij.get(SourceAndConverterService.class)
64+
.getSourceAndConverterFromSpimdata(spimData)
65+
.get(0);
66+
67+
SourceAndConverter<UnsignedShortType> sourceBgModified = new SourceOutOfBoundsColorChanger<>(sac, new UnsignedShortType(2000)).get();
68+
69+
SourceAndConverterServices.getBdvDisplayService().show(bdvHandle,sourceBgModified);
70+
71+
//new TimepointAdapterAdder(bdvh).run();
72+
}
73+
}

0 commit comments

Comments
 (0)