|
29 | 29 | package org.scijava.ops.image.convert; |
30 | 30 |
|
31 | 31 | import net.imglib2.type.NativeType; |
| 32 | +import net.imglib2.type.numeric.IntegerType; |
32 | 33 | import net.imglib2.type.numeric.RealType; |
33 | 34 | import net.imglib2.type.numeric.integer.*; |
34 | 35 | import net.imglib2.type.numeric.real.DoubleType; |
|
42 | 43 | * |
43 | 44 | * @author Gabriel Selzer |
44 | 45 | */ |
45 | | -public class NumbersToNativeTypes<N extends Number, T extends RealType<T>> { |
| 46 | +public class NumbersToNativeTypes<N extends Number, T extends RealType<T>, I extends IntegerType<I>> { |
46 | 47 |
|
47 | 48 | // -- Numbers to RealTypes -- // |
48 | 49 |
|
@@ -95,54 +96,93 @@ public class NumbersToNativeTypes<N extends Number, T extends RealType<T>> { |
95 | 96 | num -> new FloatType(num.floatValue()); |
96 | 97 |
|
97 | 98 | /** |
| 99 | + * NB This converter wins against those above in requests for e.g. |
| 100 | + * {@code Function<N, NativeType<T>>} |
| 101 | + * |
98 | 102 | * @input num the {@link Number} to convert |
99 | 103 | * @output a {@link DoubleType} containing the information in {@code num} |
100 | | - * @implNote op names='engine.convert, convert.float64' |
| 104 | + * @implNote op names='engine.convert, convert.float64', priority=100 |
101 | 105 | */ |
102 | 106 | public final Function<N, DoubleType> numberToDoubleType = // |
103 | 107 | num -> new DoubleType(num.doubleValue()); |
104 | 108 |
|
105 | 109 | // -- RealTypes to Numbers -- // |
106 | 110 |
|
107 | 111 | /** |
| 112 | + * @input realType the {@link ByteType} to convert |
| 113 | + * @output the {@link Byte}, converted from {@code realType} |
| 114 | + * @implNote op names='engine.convert, convert.int8', priority="10" |
| 115 | + */ |
| 116 | + public final Function<I, Byte> integerTypeToByte = i -> (byte) i.getIntegerLong(); |
| 117 | + |
| 118 | + /** |
| 119 | + * NB potentially lossy, so lower priority |
| 120 | + * |
108 | 121 | * @input realType the {@link ByteType} to convert |
109 | 122 | * @output the {@link Byte}, converted from {@code realType} |
110 | 123 | * @implNote op names='engine.convert, convert.int8' |
111 | 124 | */ |
112 | | - public final Function<ByteType, Byte> byteTypeToByte = ByteType::get; |
| 125 | + public final Function<T, Byte> realTypeToByte = i -> (byte) i.getRealDouble(); |
113 | 126 |
|
114 | 127 | /** |
| 128 | + * @input realType the {@link ShortType} to convert |
| 129 | + * @output the {@link Short}, converted from {@code realType} |
| 130 | + * @implNote op names='engine.convert, convert.int16', priority="10" |
| 131 | + */ |
| 132 | + public final Function<I, Short> integerTypeToShort = i -> (short) i.getInteger(); |
| 133 | + |
| 134 | + /** |
| 135 | + * NB potentially lossy, so lower priority |
| 136 | + * |
115 | 137 | * @input realType the {@link ShortType} to convert |
116 | 138 | * @output the {@link Short}, converted from {@code realType} |
117 | 139 | * @implNote op names='engine.convert, convert.int16' |
118 | 140 | */ |
119 | | - public final Function<ShortType, Short> shortTypeToShort = ShortType::get; |
| 141 | + public final Function<T, Short> realTypeToShort = i -> (short) i.getRealDouble(); |
120 | 142 |
|
121 | 143 | /** |
122 | 144 | * @input realType the {@link IntType} to convert |
123 | 145 | * @output the {@link Integer}, converted from {@code realType} |
| 146 | + * @implNote op names='engine.convert, convert.int32', priority="10" |
| 147 | + */ |
| 148 | + public final Function<I, Integer> integerTypeToInteger = IntegerType::getInteger; |
| 149 | + |
| 150 | + /** |
| 151 | + * NB potentially lossy, so lower priority |
| 152 | + * |
| 153 | + * @input realType the {@link RealType} to convert |
| 154 | + * @output the {@link Integer}, converted from {@code realType} |
124 | 155 | * @implNote op names='engine.convert, convert.int32' |
125 | 156 | */ |
126 | | - public final Function<IntType, Integer> intTypeToInteger = IntType::get; |
| 157 | + public final Function<T, Integer> realTypeToInteger = i -> (int) i.getRealDouble(); |
| 158 | + |
| 159 | + /** |
| 160 | + * @input integerType the {@link IntegerType} to convert |
| 161 | + * @output the {@link Long}, converted from {@code integerType} |
| 162 | + * @implNote op names='engine.convert, convert.int64', priority="10" |
| 163 | + */ |
| 164 | + public final Function<I, Long> integerTypeToLong = IntegerType::getIntegerLong; |
127 | 165 |
|
128 | 166 | /** |
129 | | - * @input realType the {@link LongType} to convert |
| 167 | + * NB potentially lossy, so lower priority |
| 168 | + * |
| 169 | + * @input realType the {@link RealType} to convert |
130 | 170 | * @output the {@link Long}, converted from {@code realType} |
131 | 171 | * @implNote op names='engine.convert, convert.int64' |
132 | 172 | */ |
133 | | - public final Function<LongType, Long> longTypeToLong = LongType::get; |
| 173 | + public final Function<T, Long> realTypeToLong = i -> (long) i.getRealDouble(); |
134 | 174 |
|
135 | 175 | /** |
136 | | - * @input num the {@link Number} to convert |
137 | | - * @output a {@link DoubleType} containing the information in {@code num} |
| 176 | + * @input realType the {@link RealType} to convert |
| 177 | + * @output the {@link Long}, converted from {@code realType} |
138 | 178 | * @implNote op names='engine.convert, convert.float32' |
139 | 179 | */ |
140 | | - public final Function<FloatType, Float> floatTypeToFloat = FloatType::get; |
| 180 | + public final Function<T, Float> realTypeToFloat = RealType::getRealFloat; |
141 | 181 |
|
142 | 182 | /** |
143 | | - * @input num the {@link Number} to convert |
144 | | - * @output a {@link DoubleType} containing the information in {@code num} |
| 183 | + * @input realType the {@link RealType} to convert |
| 184 | + * @output the {@link Long}, converted from {@code realType} |
145 | 185 | * @implNote op names='engine.convert, convert.float64' |
146 | 186 | */ |
147 | | - public final Function<DoubleType, Double> doubleTypeToDouble = DoubleType::get; |
| 187 | + public final Function<T, Double> realTypeToDouble = RealType::getRealDouble; |
148 | 188 | } |
0 commit comments