|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Image processing operations for SciJava Ops. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2014 - 2024 SciJava developers. |
| 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 org.scijava.ops.image.convert; |
| 30 | + |
| 31 | +import net.imglib2.type.numeric.IntegerType; |
| 32 | +import net.imglib2.type.numeric.RealType; |
| 33 | +import net.imglib2.type.numeric.integer.*; |
| 34 | +import net.imglib2.type.numeric.real.DoubleType; |
| 35 | +import net.imglib2.type.numeric.real.FloatType; |
| 36 | + |
| 37 | +import java.util.function.Function; |
| 38 | + |
| 39 | +/** |
| 40 | + * Converters for converting between {@link RealType}s and {@link Number}s |
| 41 | + * |
| 42 | + * @author Gabriel Selzer |
| 43 | + */ |
| 44 | +public class ConvertNumbersToRealTypes<N extends Number, T extends RealType<T>, I extends IntegerType<I>> { |
| 45 | + |
| 46 | + // -- Numbers to RealTypes -- // |
| 47 | + |
| 48 | + // NB Combo conversion uses these to convert to any RealType |
| 49 | + |
| 50 | + /** |
| 51 | + * @input num the {@link Number} to convert |
| 52 | + * @output a {@link ByteType} containing the information in {@code num} |
| 53 | + * @implNote op names='engine.convert, convert.int8' |
| 54 | + */ |
| 55 | + public final Function<N, ByteType> numberToByteType = // |
| 56 | + num -> new ByteType(num.byteValue()); |
| 57 | + |
| 58 | + /** |
| 59 | + * @input num the {@link Number} to convert |
| 60 | + * @output a {@link UnsignedByteType} containing the information in {@code num} |
| 61 | + * @implNote op names='engine.convert, convert.uint8' |
| 62 | + */ |
| 63 | + public final Function<N, UnsignedByteType> numberToUnsignedByteType = // |
| 64 | + num -> new UnsignedByteType(num.shortValue()); |
| 65 | + |
| 66 | + /** |
| 67 | + * @input num the {@link Number} to convert |
| 68 | + * @output a {@link ShortType} containing the information in {@code num} |
| 69 | + * @implNote op names='engine.convert, convert.int16' |
| 70 | + */ |
| 71 | + public final Function<N, ShortType> numberToShortType = // |
| 72 | + num -> new ShortType(num.shortValue()); |
| 73 | + |
| 74 | + /** |
| 75 | + * @input num the {@link Number} to convert |
| 76 | + * @output a {@link UnsignedShortType} containing the information in {@code num} |
| 77 | + * @implNote op names='engine.convert, convert.uint16' |
| 78 | + */ |
| 79 | + public final Function<N, UnsignedShortType> numberToUnsignedShortType = // |
| 80 | + num -> new UnsignedShortType(num.intValue()); |
| 81 | + |
| 82 | + /** |
| 83 | + * @input num the {@link Number} to convert |
| 84 | + * @output a {@link IntType} containing the information in {@code num} |
| 85 | + * @implNote op names='engine.convert, convert.int32' |
| 86 | + */ |
| 87 | + public final Function<N, IntType> numberToIntType = // |
| 88 | + num -> new IntType(num.intValue()); |
| 89 | + |
| 90 | + /** |
| 91 | + * @input num the {@link Number} to convert |
| 92 | + * @output a {@link UnsignedIntType} containing the information in {@code num} |
| 93 | + * @implNote op names='engine.convert, convert.uint32' |
| 94 | + */ |
| 95 | + public final Function<N, UnsignedIntType> numberToUnsignedIntType = // |
| 96 | + num -> new UnsignedIntType(num.longValue()); |
| 97 | + |
| 98 | + /** |
| 99 | + * @input num the {@link Number} to convert |
| 100 | + * @output a {@link LongType} containing the information in {@code num} |
| 101 | + * @implNote op names='engine.convert, convert.int64' |
| 102 | + */ |
| 103 | + public final Function<N, LongType> numberToLongType = // |
| 104 | + num -> new LongType(num.longValue()); |
| 105 | + |
| 106 | + /** |
| 107 | + * @input num the {@link Number} to convert |
| 108 | + * @output a {@link FloatType} containing the information in {@code num} |
| 109 | + * @implNote op names='engine.convert, convert.float32' |
| 110 | + */ |
| 111 | + public final Function<N, FloatType> numberToFloatType = // |
| 112 | + num -> new FloatType(num.floatValue()); |
| 113 | + |
| 114 | + /** |
| 115 | + * NB This converter wins against those above in requests for e.g. |
| 116 | + * {@code Function<N, NativeType<T>>} |
| 117 | + * |
| 118 | + * @input num the {@link Number} to convert |
| 119 | + * @output a {@link DoubleType} containing the information in {@code num} |
| 120 | + * @implNote op names='engine.convert, convert.float64', priority=100 |
| 121 | + */ |
| 122 | + public final Function<N, DoubleType> numberToDoubleType = // |
| 123 | + num -> new DoubleType(num.doubleValue()); |
| 124 | + |
| 125 | + // -- RealTypes to Numbers -- // |
| 126 | + |
| 127 | + /** |
| 128 | + * @input integerType the {@link IntegerType} to convert |
| 129 | + * @output the {@link Byte}, converted from {@code integerType} |
| 130 | + * @implNote op names='engine.convert, convert.int8', priority="10" |
| 131 | + */ |
| 132 | + public final Function<I, Byte> integerTypeToByte = i -> (byte) i.getIntegerLong(); |
| 133 | + |
| 134 | + /** |
| 135 | + * NB potentially lossy, so lower priority |
| 136 | + * |
| 137 | + * @input realType the {@link RealType} to convert |
| 138 | + * @output the {@link Byte}, converted from {@code realType} |
| 139 | + * @implNote op names='engine.convert, convert.int8' |
| 140 | + */ |
| 141 | + public final Function<T, Byte> realTypeToByte = i -> (byte) i.getRealDouble(); |
| 142 | + |
| 143 | + /** |
| 144 | + * @input integerType the {@link IntegerType} to convert |
| 145 | + * @output the {@link Short}, converted from {@code integerType} |
| 146 | + * @implNote op names='engine.convert, convert.int16', priority="10" |
| 147 | + */ |
| 148 | + public final Function<I, Short> integerTypeToShort = i -> (short) i.getInteger(); |
| 149 | + |
| 150 | + /** |
| 151 | + * NB potentially lossy, so lower priority |
| 152 | + * |
| 153 | + * @input realType the {@link RealType} to convert |
| 154 | + * @output the {@link Short}, converted from {@code realType} |
| 155 | + * @implNote op names='engine.convert, convert.int16' |
| 156 | + */ |
| 157 | + public final Function<T, Short> realTypeToShort = i -> (short) i.getRealDouble(); |
| 158 | + |
| 159 | + /** |
| 160 | + * @input integerType the {@link IntegerType} to convert |
| 161 | + * @output the {@link Integer}, converted from {@code integerType} |
| 162 | + * @implNote op names='engine.convert, convert.int32', priority="10" |
| 163 | + */ |
| 164 | + public final Function<I, Integer> integerTypeToInteger = IntegerType::getInteger; |
| 165 | + |
| 166 | + /** |
| 167 | + * NB potentially lossy, so lower priority |
| 168 | + * |
| 169 | + * @input realType the {@link RealType} to convert |
| 170 | + * @output the {@link Integer}, converted from {@code realType} |
| 171 | + * @implNote op names='engine.convert, convert.int32' |
| 172 | + */ |
| 173 | + public final Function<T, Integer> realTypeToInteger = i -> (int) i.getRealDouble(); |
| 174 | + |
| 175 | + /** |
| 176 | + * @input integerType the {@link IntegerType} to convert |
| 177 | + * @output the {@link Long}, converted from {@code integerType} |
| 178 | + * @implNote op names='engine.convert, convert.int64', priority="10" |
| 179 | + */ |
| 180 | + public final Function<I, Long> integerTypeToLong = IntegerType::getIntegerLong; |
| 181 | + |
| 182 | + /** |
| 183 | + * NB potentially lossy, so lower priority |
| 184 | + * |
| 185 | + * @input realType the {@link RealType} to convert |
| 186 | + * @output the {@link Long}, converted from {@code realType} |
| 187 | + * @implNote op names='engine.convert, convert.int64' |
| 188 | + */ |
| 189 | + public final Function<T, Long> realTypeToLong = i -> (long) i.getRealDouble(); |
| 190 | + |
| 191 | + /** |
| 192 | + * @input realType the {@link RealType} to convert |
| 193 | + * @output the {@link Float}, converted from {@code realType} |
| 194 | + * @implNote op names='engine.convert, convert.float32' |
| 195 | + */ |
| 196 | + public final Function<T, Float> realTypeToFloat = RealType::getRealFloat; |
| 197 | + |
| 198 | + /** |
| 199 | + * @input realType the {@link RealType} to convert |
| 200 | + * @output the {@link Double}, converted from {@code realType} |
| 201 | + * @implNote op names='engine.convert, convert.float64' |
| 202 | + */ |
| 203 | + public final Function<T, Double> realTypeToDouble = RealType::getRealDouble; |
| 204 | +} |
0 commit comments