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+ }
0 commit comments