1 package nom.tam.image.compression.tile.mask;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 import java.nio.ByteBuffer;
35 import java.nio.DoubleBuffer;
36 import java.nio.FloatBuffer;
37 import java.nio.IntBuffer;
38 import java.nio.LongBuffer;
39 import java.nio.ShortBuffer;
40
41 import nom.tam.fits.compression.algorithm.api.ICompressorControl;
42 import nom.tam.image.tile.operation.buffer.TileBuffer;
43 import nom.tam.util.type.ElementType;
44
45
46
47
48
49
50
51 public class NullPixelMaskRestorer extends AbstractNullPixelMask {
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public NullPixelMaskRestorer(TileBuffer tileBuffer, int tileIndex, long nullValue, ICompressorControl compressorControl) {
70 super(tileBuffer, tileIndex, nullValue, compressorControl);
71 }
72
73
74
75
76
77 public void restoreNulls() {
78
79 if (getMask() != null) {
80 ByteBuffer decompressed = ByteBuffer.allocate(getTileBuffer().getPixelSize());
81 getCompressorControl().decompress(getMask(), decompressed, getCompressorControl().option());
82 setMask(decompressed);
83 if (getTileBuffer().getBaseType().is(ElementType.DOUBLE)) {
84 restoreNullDoubles();
85 } else if (getTileBuffer().getBaseType().is(ElementType.FLOAT)) {
86 restoreNullFloats();
87 } else if (getTileBuffer().getBaseType().is(ElementType.LONG)) {
88 restoreNullLongs();
89 } else if (getTileBuffer().getBaseType().is(ElementType.INT)) {
90 restoreNullInts();
91 } else if (getTileBuffer().getBaseType().is(ElementType.SHORT)) {
92 restoreNullShorts();
93 } else if (getTileBuffer().getBaseType().is(ElementType.BYTE)) {
94 restoreNullBytes();
95 }
96 }
97 }
98
99 private void restoreNullBytes() {
100 ByteBuffer buffer = (ByteBuffer) getTileBuffer().getBuffer();
101 ByteBuffer nullMask = initializedMask(buffer.remaining());
102 byte nullValue = (byte) getNullValue();
103 for (int index = 0; index < nullMask.capacity(); index++) {
104 if (nullMask.get(index) == NULL_INDICATOR) {
105 buffer.put(index, nullValue);
106 }
107 }
108 }
109
110 private void restoreNullDoubles() {
111 DoubleBuffer buffer = (DoubleBuffer) getTileBuffer().getBuffer();
112 ByteBuffer nullMask = initializedMask(buffer.remaining());
113 for (int index = 0; index < nullMask.capacity(); index++) {
114 if (nullMask.get(index) == NULL_INDICATOR) {
115 buffer.put(index, Double.NaN);
116 }
117 }
118 }
119
120 private void restoreNullFloats() {
121 FloatBuffer buffer = (FloatBuffer) getTileBuffer().getBuffer();
122 ByteBuffer nullMask = initializedMask(buffer.remaining());
123 for (int index = 0; index < nullMask.capacity(); index++) {
124 if (nullMask.get(index) == NULL_INDICATOR) {
125 buffer.put(index, Float.NaN);
126 }
127 }
128 }
129
130 private void restoreNullInts() {
131 IntBuffer buffer = (IntBuffer) getTileBuffer().getBuffer();
132 ByteBuffer nullMask = initializedMask(buffer.remaining());
133 int nullValue = (int) getNullValue();
134 for (int index = 0; index < nullMask.capacity(); index++) {
135 if (nullMask.get(index) == NULL_INDICATOR) {
136 buffer.put(index, nullValue);
137 }
138 }
139 }
140
141 private void restoreNullLongs() {
142 LongBuffer buffer = (LongBuffer) getTileBuffer().getBuffer();
143 ByteBuffer nullMask = initializedMask(buffer.remaining());
144 long nullValue = getNullValue();
145 for (int index = 0; index < nullMask.capacity(); index++) {
146 if (nullMask.get(index) == NULL_INDICATOR) {
147 buffer.put(index, nullValue);
148 }
149 }
150 }
151
152 private void restoreNullShorts() {
153 ShortBuffer buffer = (ShortBuffer) getTileBuffer().getBuffer();
154 ByteBuffer nullMask = initializedMask(buffer.remaining());
155 short nullValue = (short) getNullValue();
156 for (int index = 0; index < nullMask.capacity(); index++) {
157 if (nullMask.get(index) == NULL_INDICATOR) {
158 buffer.put(index, nullValue);
159 }
160 }
161 }
162 }