Skip to content

Commit 65868d9

Browse files
committed
created CreateImgFromArray for UnsignedByteType
1 parent 8683dd3 commit 65868d9

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

src/main/java/net/imagej/ops/create/CreateNamespace.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ public Img<DoubleType> img(final int[] dims) {
107107
public Img<DoubleType> img(final long[] dims) {
108108
return img(new FinalDimensions(dims), new DoubleType());
109109
}
110+
111+
@OpMethod(op = net.imagej.ops.create.img.CreateImgFromArray.class)
112+
public <T extends NativeType<T>> Img<T> img(final byte[] in1,
113+
final Dimensions in2)
114+
{
115+
@SuppressWarnings("unchecked")
116+
final Img<T> result = (Img<T>) ops().run(
117+
Ops.Create.Img.class, in1, in2);
118+
return result;
119+
}
110120

111121
@OpMethod(op = net.imagej.ops.create.img.CreateImgFromDimsAndType.class)
112122
public <T extends NativeType<T>> Img<T> img(final Dimensions in1,
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

src/test/java/net/imagej/ops/create/CreateImgTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Random;
3636

3737
import net.imagej.ops.AbstractOpTest;
38+
import net.imagej.ops.create.img.CreateImgFromArray;
3839
import net.imagej.ops.create.img.CreateImgFromDimsAndType;
3940
import net.imagej.ops.create.img.CreateImgFromImg;
4041
import net.imagej.ops.create.img.CreateImgFromInterval;
@@ -64,7 +65,11 @@
6465
* @author Daniel Seebacher (University of Konstanz)
6566
* @author Tim-Oliver Buchholz (University of Konstanz)
6667
*/
68+
import org.junit.FixMethodOrder;
69+
import org.junit.Test;
70+
import org.junit.runners.MethodSorters;
6771

72+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
6873
public class CreateImgTest extends AbstractOpTest {
6974

7075
private static final int TEST_SIZE = 100;
@@ -133,6 +138,42 @@ public void testImgFromImg() {
133138
assertEquals(img.firstElement().getClass(), newImg.firstElement()
134139
.getClass());
135140
}
141+
142+
@Test
143+
public void testImgFromArray() {
144+
final Random randomGenerator = new Random();
145+
146+
for (int i = 0; i < 1; i++) {
147+
148+
// between 2 and 5 dimensions
149+
//final long[] dim = new long[randomGenerator.nextInt(4) + 2];
150+
//final long[] dim = new long[2];
151+
final long[] dim = {256, 1};
152+
153+
// source array length
154+
int srcLen = 1;
155+
156+
// between 2 and 10 pixels per dimensions
157+
for (int j = 0; j < dim.length; j++) {
158+
//dim[j] = randomGenerator.nextInt(9) + 2;
159+
//dim[j] = 100;
160+
srcLen *= dim[j];
161+
}
162+
163+
byte[] arr = new byte[srcLen];
164+
165+
for (int j = 0; j < srcLen; j++)
166+
arr[j] = (byte) (j % 256);
167+
168+
// create img
169+
@SuppressWarnings("unchecked")
170+
final Img<UnsignedByteType> img = (Img<UnsignedByteType>) ops.run(
171+
CreateImgFromArray.class, arr, dim);
172+
net.imglib2.img.display.imagej.ImageJFunctions.show(img);
173+
// assertArrayEquals("Image Dimensions:", dim, Intervals
174+
// .dimensionsAsLongArray(img));
175+
}
176+
}
136177

137178
@Test
138179
public void testImageFactory() {

0 commit comments

Comments
 (0)