View Javadoc
1   package nom.tam.image.compression.tile.mask;
2   
3   /*
4    * #%L
5    * nom.tam FITS library
6    * %%
7    * Copyright (C) 1996 - 2024 nom-tam-fits
8    * %%
9    * This is free and unencumbered software released into the public domain.
10   *
11   * Anyone is free to copy, modify, publish, use, compile, sell, or
12   * distribute this software, either in source code form or as a compiled
13   * binary, for any purpose, commercial or non-commercial, and by any
14   * means.
15   *
16   * In jurisdictions that recognize copyright laws, the author or authors
17   * of this software dedicate any and all copyright interest in the
18   * software to the public domain. We make this dedication for the benefit
19   * of the public at large and to the detriment of our heirs and
20   * successors. We intend this dedication to be an overt act of
21   * relinquishment in perpetuity of all present and future rights to this
22   * software under copyright law.
23   *
24   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30   * OTHER DEALINGS IN THE SOFTWARE.
31   * #L%
32   */
33  
34  import static org.junit.Assert.assertEquals;
35  import static org.junit.Assert.assertFalse;
36  
37  import java.lang.reflect.Constructor;
38  import java.nio.Buffer;
39  import java.nio.ByteBuffer;
40  import java.nio.DoubleBuffer;
41  import java.nio.FloatBuffer;
42  import java.nio.IntBuffer;
43  import java.nio.LongBuffer;
44  import java.nio.ShortBuffer;
45  import java.util.Arrays;
46  
47  import org.junit.Assert;
48  import org.junit.Test;
49  
50  import nom.tam.image.tile.operation.buffer.TileBuffer;
51  import nom.tam.image.tile.operation.buffer.TileBufferFactory;
52  import nom.tam.util.type.ElementType;
53  import nom.tam.util.type.PrimitiveTypes;
54  
55  public class ImageMaskTest {
56  
57      @Test
58      public void testByteMask() {
59          doTestByteMask("RICE_1", 2, 7);
60      }
61  
62      @Test
63      public void testNoByteMask() {
64          byte[][] result = doTestByteMask("RICE_1");
65          assertEquals(2, result.length);
66          assertEquals(0, result[0].length);
67          assertEquals(0, result[1].length);
68      }
69  
70      @Test(expected = IllegalStateException.class)
71      public void testWrongCompression() {
72          doTestByteMask("UNKNOWN1", 2, 7);
73      }
74  
75      @Test(expected = IllegalStateException.class)
76      public void testCompressionFailed() {
77          doTestByteMask("FAIL", 2, 7);
78      }
79  
80      private byte[][] doTestByteMask(String compression, int... nullIndexes) {
81          byte[] orgPixels = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
82          byte[] expectedPixels = Arrays.copyOf(orgPixels, orgPixels.length);
83          for (int index : nullIndexes) {
84              expectedPixels[index] = -1;
85          }
86          ByteBuffer expectedPixelBuffer = ByteBuffer.wrap(expectedPixels);
87  
88          ImageNullPixelMask mask = new ImageNullPixelMask(2, -1L, compression);
89          createTilePreserver(expectedPixelBuffer, mask, PrimitiveTypes.BYTE, 0);
90          createTilePreserver(expectedPixelBuffer, mask, PrimitiveTypes.BYTE, 1);
91          byte[][] preservedNulls = mask.getColumn();
92  
93          // now a new buffer with original floats but without the NaN
94          ByteBuffer actualPixelBuffer = ByteBuffer.allocate(expectedPixels.length);
95          actualPixelBuffer.put(orgPixels);
96  
97          // now restore the nulls
98          mask = new ImageNullPixelMask(2, -1L, "RICE_1");
99          NullPixelMaskRestorer rest1 = createTileRestorer(actualPixelBuffer, mask, PrimitiveTypes.BYTE, 0);
100         NullPixelMaskRestorer rest2 = createTileRestorer(actualPixelBuffer, mask, PrimitiveTypes.BYTE, 1);
101         mask.setColumn(preservedNulls);
102         rest1.restoreNulls();
103         rest2.restoreNulls();
104 
105         actualPixelBuffer.rewind();
106         for (int index = 0; index < orgPixels.length; index++) {
107             Assert.assertEquals(expectedPixels[index], actualPixelBuffer.get());
108         }
109         return preservedNulls;
110     }
111 
112     @Test
113     public void testDoubleMask() {
114         double[] orgPixels = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
115         double[] expectedPixels = Arrays.copyOf(orgPixels, orgPixels.length);
116         expectedPixels[2] = Float.NaN;
117         expectedPixels[7] = Float.NaN;
118         DoubleBuffer expectedPixelBuffer = DoubleBuffer.wrap(expectedPixels);
119 
120         ImageNullPixelMask mask = new ImageNullPixelMask(2, -1L, "RICE_1");
121         createTilePreserver(expectedPixelBuffer, mask, PrimitiveTypes.DOUBLE, 0);
122         createTilePreserver(expectedPixelBuffer, mask, PrimitiveTypes.DOUBLE, 1);
123         byte[][] preservedNulls = mask.getColumn();
124 
125         // now a new buffer with original floats but without the NaN
126         DoubleBuffer actualPixelBuffer = DoubleBuffer.allocate(expectedPixels.length);
127         actualPixelBuffer.put(orgPixels);
128 
129         // now restore the nulls
130         mask = new ImageNullPixelMask(2, -1L, "RICE_1");
131         NullPixelMaskRestorer rest1 = createTileRestorer(actualPixelBuffer, mask, PrimitiveTypes.DOUBLE, 0);
132         NullPixelMaskRestorer rest2 = createTileRestorer(actualPixelBuffer, mask, PrimitiveTypes.DOUBLE, 1);
133         mask.setColumn(preservedNulls);
134         rest1.restoreNulls();
135         rest2.restoreNulls();
136 
137         actualPixelBuffer.rewind();
138         for (int index = 0; index < orgPixels.length; index++) {
139             Assert.assertEquals(expectedPixels[index], actualPixelBuffer.get(), 0.00000001d);
140         }
141     }
142 
143     @Test
144     public void testFloatMask() {
145         float[] orgPixels = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
146         float[] expectedPixels = Arrays.copyOf(orgPixels, orgPixels.length);
147         expectedPixels[2] = Float.NaN;
148         expectedPixels[7] = Float.NaN;
149         FloatBuffer expectedPixelBuffer = FloatBuffer.wrap(expectedPixels);
150 
151         ImageNullPixelMask mask = new ImageNullPixelMask(2, -1L, "RICE_1");
152         createTilePreserver(expectedPixelBuffer, mask, PrimitiveTypes.FLOAT, 0);
153         createTilePreserver(expectedPixelBuffer, mask, PrimitiveTypes.FLOAT, 1);
154         byte[][] preservedNulls = mask.getColumn();
155 
156         // now a new buffer with original floats but without the NaN
157         FloatBuffer actualPixelBuffer = FloatBuffer.allocate(expectedPixels.length);
158         actualPixelBuffer.put(orgPixels);
159 
160         mask = new ImageNullPixelMask(2, -1L, "RICE_1");
161         NullPixelMaskRestorer rest1 = createTileRestorer(actualPixelBuffer, mask, PrimitiveTypes.FLOAT, 0);
162         NullPixelMaskRestorer rest2 = createTileRestorer(actualPixelBuffer, mask, PrimitiveTypes.FLOAT, 1);
163         mask.setColumn(preservedNulls);
164         rest1.restoreNulls();
165         rest2.restoreNulls();
166 
167         actualPixelBuffer.rewind();
168         for (int index = 0; index < orgPixels.length; index++) {
169             Assert.assertEquals(expectedPixels[index], actualPixelBuffer.get(), 0.00000001f);
170         }
171     }
172 
173     @Test
174     public void testIntMask() {
175         int[] orgPixels = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
176         int[] expectedPixels = Arrays.copyOf(orgPixels, orgPixels.length);
177         expectedPixels[2] = -1;
178         expectedPixels[7] = -1;
179         IntBuffer expectedPixelBuffer = IntBuffer.wrap(expectedPixels);
180 
181         ImageNullPixelMask mask = new ImageNullPixelMask(2, -1L, "RICE_1");
182         createTilePreserver(expectedPixelBuffer, mask, PrimitiveTypes.INT, 0);
183         createTilePreserver(expectedPixelBuffer, mask, PrimitiveTypes.INT, 1);
184         byte[][] preservedNulls = mask.getColumn();
185 
186         // now a new buffer with original floats but without the NaN
187         IntBuffer actualPixelBuffer = IntBuffer.allocate(expectedPixels.length);
188         actualPixelBuffer.put(orgPixels);
189 
190         // now restore the nulls
191         mask = new ImageNullPixelMask(2, -1L, "RICE_1");
192         NullPixelMaskRestorer rest1 = createTileRestorer(actualPixelBuffer, mask, PrimitiveTypes.INT, 0);
193         NullPixelMaskRestorer rest2 = createTileRestorer(actualPixelBuffer, mask, PrimitiveTypes.INT, 1);
194         mask.setColumn(preservedNulls);
195         rest1.restoreNulls();
196         rest2.restoreNulls();
197 
198         actualPixelBuffer.rewind();
199         for (int index = 0; index < orgPixels.length; index++) {
200             Assert.assertEquals(expectedPixels[index], actualPixelBuffer.get());
201         }
202     }
203 
204     @Test
205     public void testLongMask() {
206         long[] orgPixels = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
207         long[] expectedPixels = Arrays.copyOf(orgPixels, orgPixels.length);
208         expectedPixels[2] = -1;
209         expectedPixels[7] = -1;
210         LongBuffer expectedPixelBuffer = LongBuffer.wrap(expectedPixels);
211 
212         ImageNullPixelMask mask = new ImageNullPixelMask(2, -1L, "RICE_1");
213         createTilePreserver(expectedPixelBuffer, mask, PrimitiveTypes.LONG, 0);
214         createTilePreserver(expectedPixelBuffer, mask, PrimitiveTypes.LONG, 1);
215         byte[][] preservedNulls = mask.getColumn();
216 
217         // now a new buffer with original floats but without the NaN
218         LongBuffer actualPixelBuffer = LongBuffer.allocate(expectedPixels.length);
219         actualPixelBuffer.put(orgPixels);
220 
221         // now restore the nulls
222         mask = new ImageNullPixelMask(2, -1L, "RICE_1");
223         NullPixelMaskRestorer rest1 = createTileRestorer(actualPixelBuffer, mask, PrimitiveTypes.LONG, 0);
224         NullPixelMaskRestorer rest2 = createTileRestorer(actualPixelBuffer, mask, PrimitiveTypes.LONG, 1);
225         mask.setColumn(preservedNulls);
226         rest1.restoreNulls();
227         rest2.restoreNulls();
228 
229         actualPixelBuffer.rewind();
230         for (int index = 0; index < orgPixels.length; index++) {
231             Assert.assertEquals(expectedPixels[index], actualPixelBuffer.get());
232         }
233     }
234 
235     @Test
236     public void testShortMask() {
237         short[] orgPixels = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
238         short[] expectedPixels = Arrays.copyOf(orgPixels, orgPixels.length);
239         expectedPixels[2] = -1;
240         expectedPixels[7] = -1;
241         ShortBuffer expectedPixelBuffer = ShortBuffer.wrap(expectedPixels);
242 
243         ImageNullPixelMask mask = new ImageNullPixelMask(2, -1L, "RICE_1");
244         createTilePreserver(expectedPixelBuffer, mask, PrimitiveTypes.SHORT, 0);
245         createTilePreserver(expectedPixelBuffer, mask, PrimitiveTypes.SHORT, 1);
246         byte[][] preservedNulls = mask.getColumn();
247 
248         // now a new buffer with original floats but without the NaN
249         ShortBuffer actualPixelBuffer = ShortBuffer.allocate(expectedPixels.length);
250         actualPixelBuffer.put(orgPixels);
251 
252         // now restore the nulls
253         mask = new ImageNullPixelMask(2, -1L, "RICE_1");
254         NullPixelMaskRestorer rest1 = createTileRestorer(actualPixelBuffer, mask, PrimitiveTypes.SHORT, 0);
255         NullPixelMaskRestorer rest2 = createTileRestorer(actualPixelBuffer, mask, PrimitiveTypes.SHORT, 1);
256         mask.setColumn(preservedNulls);
257         rest1.restoreNulls();
258         rest2.restoreNulls();
259 
260         actualPixelBuffer.rewind();
261         for (int index = 0; index < orgPixels.length; index++) {
262             Assert.assertEquals(expectedPixels[index], actualPixelBuffer.get());
263         }
264     }
265 
266     @Test
267     public void testTileBufferFactoryPrivate() throws Exception {
268         Constructor<?>[] constrs = TileBufferFactory.class.getDeclaredConstructors();
269         assertEquals(constrs.length, 1);
270         assertFalse(constrs[0].isAccessible());
271         constrs[0].setAccessible(true);
272         constrs[0].newInstance();
273     }
274 
275     protected TileBuffer createTileBuffer(Buffer buffer, ElementType type) {
276         TileBuffer tileBuffer = TileBufferFactory.createTileBuffer(type, 0, 10, 10, 1);
277         tileBuffer.setData(buffer);
278         return tileBuffer;
279     }
280 
281     protected void createTilePreserver(Buffer buffer, ImageNullPixelMask mask, ElementType type, int tileIndex) {
282         TileBuffer tileBuffer = createTileBuffer(buffer, type);
283         mask.createTilePreserver(tileBuffer, tileIndex).preserveNull();
284     }
285 
286     protected NullPixelMaskRestorer createTileRestorer(Buffer buffer, ImageNullPixelMask mask, ElementType type,
287             int tileIndex) {
288         TileBuffer tileBuffer = createTileBuffer(buffer, type);
289         return mask.createTileRestorer(tileBuffer, tileIndex);
290     }
291 }