|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * ImageJ software for multidimensional image processing and analysis. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2014 - 2017 ImageJ 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 | + |
| 30 | +package net.imagej.ops.create.img; |
| 31 | + |
| 32 | +import org.scijava.Priority; |
| 33 | +import org.scijava.plugin.Parameter; |
| 34 | +import org.scijava.plugin.Plugin; |
| 35 | + |
| 36 | +import net.imagej.ops.Ops; |
| 37 | +import net.imagej.ops.special.computer.AbstractUnaryComputerOp; |
| 38 | +import net.imagej.ops.special.computer.Computers; |
| 39 | +import net.imagej.ops.special.computer.UnaryComputerOp; |
| 40 | +import net.imagej.ops.special.function.AbstractUnaryFunctionOp; |
| 41 | +import net.imglib2.Cursor; |
| 42 | +import net.imglib2.Dimensions; |
| 43 | +import net.imglib2.IterableInterval; |
| 44 | +import net.imglib2.img.Img; |
| 45 | +import net.imglib2.img.ImgFactory; |
| 46 | +import net.imglib2.img.array.ArrayImgFactory; |
| 47 | +import net.imglib2.type.NativeType; |
| 48 | +import net.imglib2.type.numeric.integer.UnsignedByteType; |
| 49 | +import net.imglib2.type.numeric.real.DoubleType; |
| 50 | + |
| 51 | +/** |
| 52 | + * Create an {@link Img} from an array using its type |
| 53 | + * {@code T}. |
| 54 | + * |
| 55 | + * @author Dasong Gao |
| 56 | + * @param <T> |
| 57 | + */ |
| 58 | +@Plugin(type = Ops.Create.Img.class, priority = Priority.HIGH_PRIORITY) |
| 59 | +public class CreateImgFromArray<T extends NativeType<T>> extends AbstractUnaryFunctionOp<byte[], Img<T>> implements |
| 60 | +Ops.Create.Img { |
| 61 | + |
| 62 | + @Parameter(required = true) |
| 63 | + private Dimensions dims; |
| 64 | + |
| 65 | + @Parameter(required = false) |
| 66 | + private ImgFactory<T> factory; |
| 67 | + |
| 68 | + private Img<T> output; |
| 69 | + |
| 70 | + private T type; |
| 71 | + |
| 72 | + public static void main(String[] args) { |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + @SuppressWarnings("unchecked") |
| 78 | + public Img<T> calculate(final byte[] input) { |
| 79 | + //output = (Img<T>) ops().run(CreateImgFromDimsAndType.class, dims, new UnsignedByteType()); |
| 80 | + //output = (Img<T>) ops().run(Ops.Create.Img.class, dims, new UnsignedByteType()); |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + type = (T) new UnsignedByteType(); |
| 85 | + if (factory == null) { |
| 86 | + factory = dims == null ? ops().create().imgFactory() : |
| 87 | + ops().create().imgFactory(dims); |
| 88 | + } |
| 89 | + output = Imgs.create((ImgFactory<T>) factory, dims, type); |
| 90 | + long[] imgDims = new long[dims.numDimensions()]; |
| 91 | + for (int i = 0; i < imgDims.length; i++) |
| 92 | + imgDims[i] = dims.dimension(i); |
| 93 | + Cursor<T> cursor = output.cursor(); |
| 94 | + while (cursor.hasNext()) { |
| 95 | + T value = cursor.next(); |
| 96 | + int inputIndex = 0; |
| 97 | + for (int i = 0; i < imgDims.length; i++) |
| 98 | + inputIndex += cursor.getLongPosition(i) * (i - 1 < 0 ? 1 : imgDims[i - 1]); |
| 99 | + value.set((T) new UnsignedByteType(input[inputIndex])); |
| 100 | + } |
| 101 | + return output; |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments