1 package nom.tam.fits.compression.algorithm.quant;
2
3 import java.nio.Buffer;
4 import java.nio.BufferOverflowException;
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
35
36
37 import java.nio.ByteBuffer;
38 import java.nio.DoubleBuffer;
39 import java.nio.FloatBuffer;
40 import java.nio.IntBuffer;
41
42 import nom.tam.fits.compression.algorithm.api.ICompressor;
43
44
45
46
47 @SuppressWarnings({"javadoc", "deprecation"})
48 public class QuantizeProcessor {
49
50 public static class DoubleQuantCompressor extends QuantizeProcessor implements ICompressor<DoubleBuffer> {
51 public DoubleQuantCompressor(QuantizeOption quantizeOption, ICompressor<IntBuffer> compressor) {
52 super(quantizeOption, compressor);
53 }
54
55 @Override
56 public boolean compress(DoubleBuffer buffer, ByteBuffer compressed) {
57 return super.compressGeneric(buffer, compressed);
58 }
59
60 @Override
61 public void decompress(ByteBuffer compressed, DoubleBuffer buffer) {
62 super.decompressGeneric(compressed, buffer);
63 }
64 }
65
66
67
68
69 public static class FloatQuantCompressor extends QuantizeProcessor implements ICompressor<FloatBuffer> {
70 public FloatQuantCompressor(QuantizeOption quantizeOption, ICompressor<IntBuffer> compressor) {
71 super(quantizeOption, compressor);
72 }
73
74 @Override
75 public boolean compress(FloatBuffer buffer, ByteBuffer compressed) {
76 return super.compressGeneric(buffer, compressed);
77 }
78
79 @Override
80 public void decompress(ByteBuffer compressed, FloatBuffer buffer) {
81 super.decompressGeneric(compressed, buffer);
82 }
83 }
84
85 private Quantize quantize;
86
87 protected final QuantizeOption quantizeOption;
88
89 private final ICompressor<IntBuffer> postCompressor;
90
91 public QuantizeProcessor(QuantizeOption quantizeOption, ICompressor<IntBuffer> compressor) {
92 this.quantizeOption = quantizeOption;
93 this.postCompressor = compressor;
94 quantize = new Quantize(quantizeOption);
95 }
96
97 protected QuantizeProcessor(QuantizeOption quantizeOption) {
98 this(quantizeOption, null);
99 }
100
101 public Quantize getQuantize() {
102 return quantize;
103 }
104
105 protected boolean compressGeneric(Buffer buffer, ByteBuffer compressed) throws BufferOverflowException {
106 IntBuffer intData = IntBuffer.wrap(new int[quantizeOption.getTileHeight() * quantizeOption.getTileWidth()]);
107 if (!autoQuantize(buffer, intData)) {
108 return false;
109 }
110 intData.rewind();
111 postCompressor.compress(intData, compressed);
112 return true;
113 }
114
115 protected <BufferType extends Buffer> void decompressGeneric(ByteBuffer compressed, BufferType buffer)
116 throws BufferOverflowException {
117 IntBuffer intData = IntBuffer.wrap(new int[quantizeOption.getTileHeight() * quantizeOption.getTileWidth()]);
118 postCompressor.decompress(compressed, intData);
119 intData.rewind();
120 unquantizeGeneric(intData, buffer);
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135 protected boolean autoQuantize(Buffer floating, IntBuffer quants) {
136 boolean success = quantize.guessQuantization(floating);
137 if (quants != null) {
138 quantizeGeneric(floating, quants);
139 }
140 return success;
141 }
142
143
144
145
146
147
148
149
150
151 @Deprecated
152 public boolean quantize(double[] doubles, IntBuffer quants) {
153 return autoQuantize(DoubleBuffer.wrap(doubles, 0, quantizeOption.getTileWidth() * quantizeOption.getTileHeight()),
154 quants);
155 }
156
157 private void quantizeGeneric(final Buffer fdata, final IntBuffer intData) throws BufferOverflowException {
158 if (fdata instanceof FloatBuffer) {
159 quantize((FloatBuffer) fdata, intData);
160 } else {
161 quantize((DoubleBuffer) fdata, intData);
162 }
163 }
164
165 private void unquantizeGeneric(final IntBuffer intData, final Buffer fdata) throws BufferOverflowException {
166 if (fdata instanceof FloatBuffer) {
167 unquantize(intData, (FloatBuffer) fdata);
168 } else {
169 unquantize(intData, (DoubleBuffer) fdata);
170 }
171 }
172
173
174
175
176
177
178
179
180
181
182
183 protected void quantize(final FloatBuffer fdata, final IntBuffer intData) throws BufferOverflowException {
184 quantizeOption.initDither();
185 while (fdata.hasRemaining()) {
186 intData.put(quantizeOption.toInt(fdata.get()));
187 }
188 }
189
190
191
192
193
194
195
196
197
198
199
200 protected void unquantize(final IntBuffer intData, final DoubleBuffer fdata) throws BufferOverflowException {
201 quantizeOption.initDither();
202 while (fdata.hasRemaining()) {
203 fdata.put((float) quantizeOption.toDouble(intData.get()));
204 }
205 }
206
207
208
209
210
211
212
213
214
215
216
217 protected void quantize(final DoubleBuffer fdata, final IntBuffer intData) throws BufferOverflowException {
218 quantizeOption.initDither();
219 while (fdata.hasRemaining()) {
220 intData.put(quantizeOption.toInt(fdata.get()));
221 }
222 }
223
224
225
226
227
228
229
230
231
232
233
234 protected void unquantize(final IntBuffer intData, final FloatBuffer fdata) throws BufferOverflowException {
235 quantizeOption.initDither();
236 while (fdata.hasRemaining()) {
237 fdata.put((float) quantizeOption.toDouble(intData.get()));
238 }
239 }
240
241 }