| 1 |
package nom.tam.fits.compression.algorithm.hcompress; |
| 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 java.nio.Buffer; |
| 35 |
import java.nio.ByteBuffer; |
| 36 |
import java.nio.IntBuffer; |
| 37 |
import java.nio.ShortBuffer; |
| 38 |
|
| 39 |
import nom.tam.fits.compression.algorithm.api.ICompressor; |
| 40 |
import nom.tam.fits.compression.algorithm.quant.QuantizeProcessor.DoubleQuantCompressor; |
| 41 |
import nom.tam.fits.compression.algorithm.quant.QuantizeProcessor.FloatQuantCompressor; |
| 42 |
import nom.tam.util.ArrayFuncs; |
| 43 |
|
| 44 |
/** |
| 45 |
* (<i>for internal use</i>) Data compressor using the HCompress algorithm. |
| 46 |
* |
| 47 |
* @param <T> The generic type of buffer that accessed the type of elements needed for the compression |
| 48 |
*/ |
| 49 |
@SuppressWarnings("javadoc") |
| 50 |
public abstract class HCompressor<T extends Buffer> implements ICompressor<T> { |
| 51 |
|
| 52 |
public static class ByteHCompressor extends HCompressor<ByteBuffer> { |
| 53 |
|
| 54 |
private static final long BYTE_MASK_FOR_LONG = 0xFFL; |
| 55 |
|
| 56 |
/** |
| 57 |
* HCompress of byte streams with the default scale parameter of 0 and no smoothing (lossless compression). |
| 58 |
* |
| 59 |
* @since 1.19.1 |
| 60 |
* |
| 61 |
* @author Attila Kovacs |
| 62 |
*/ |
| 63 |
public ByteHCompressor() { |
| 64 |
this(new HCompressorOption()); |
| 65 |
} |
| 66 |
|
| 67 |
public ByteHCompressor(HCompressorOption options) { |
| 68 |
super(options); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public boolean compress(ByteBuffer buffer, ByteBuffer compressed) { |
| 73 |
byte[] byteArray = new byte[buffer.limit()]; |
| 74 |
buffer.get(byteArray); |
| 75 |
long[] longArray = new long[byteArray.length]; |
| 76 |
for (int index = 0; index < longArray.length; index++) { |
| 77 |
longArray[index] = byteArray[index] & BYTE_MASK_FOR_LONG; |
| 78 |
} |
| 79 |
compress(longArray, compressed); |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void decompress(ByteBuffer compressed, ByteBuffer buffer) { |
| 85 |
long[] longArray = new long[buffer.limit()]; |
| 86 |
decompress(compressed, longArray); |
| 87 |
for (long element : longArray) { |
| 88 |
buffer.put((byte) element); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
public static class DoubleHCompressor extends DoubleQuantCompressor { |
| 95 |
|
| 96 |
@SuppressWarnings("deprecation") |
| 97 |
public DoubleHCompressor(HCompressorQuantizeOption options) { |
| 98 |
super(options, new IntHCompressor((HCompressorOption) options.getCompressOption())); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
public static class FloatHCompressor extends FloatQuantCompressor { |
| 103 |
|
| 104 |
@SuppressWarnings("deprecation") |
| 105 |
public FloatHCompressor(HCompressorQuantizeOption options) { |
| 106 |
super(options, new IntHCompressor((HCompressorOption) options.getCompressOption())); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
public static class IntHCompressor extends HCompressor<IntBuffer> { |
| 111 |
|
| 112 |
/** |
| 113 |
* HCompress of 32-bit integer streams with the default scale parameter of 0 and no smoothing for lossless |
| 114 |
* compression. |
| 115 |
* |
| 116 |
* @since 1.19.1 |
| 117 |
* |
| 118 |
* @author Attila Kovacs |
| 119 |
*/ |
| 120 |
public IntHCompressor() { |
| 121 |
this(new HCompressorOption()); |
| 122 |
} |
| 123 |
|
| 124 |
public IntHCompressor(HCompressorOption options) { |
| 125 |
super(options); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public boolean compress(IntBuffer buffer, ByteBuffer compressed) { |
| 130 |
int[] intArray = new int[buffer.limit()]; |
| 131 |
buffer.get(intArray); |
| 132 |
long[] longArray = new long[intArray.length]; |
| 133 |
ArrayFuncs.copyInto(intArray, longArray); |
| 134 |
compress(longArray, compressed); |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void decompress(ByteBuffer compressed, IntBuffer buffer) { |
| 140 |
long[] longArray = new long[buffer.limit()]; |
| 141 |
decompress(compressed, longArray); |
| 142 |
for (long element : longArray) { |
| 143 |
buffer.put((int) element); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
public static class ShortHCompressor extends HCompressor<ShortBuffer> { |
| 150 |
|
| 151 |
/** |
| 152 |
* HCompress of 16-bit integer streams with the default scale parameter of 0 (lossless compression). |
| 153 |
* |
| 154 |
* @since 1.19.1 |
| 155 |
* |
| 156 |
* @author Attila Kovacs |
| 157 |
*/ |
| 158 |
public ShortHCompressor() { |
| 159 |
this(new HCompressorOption()); |
| 160 |
} |
| 161 |
|
| 162 |
public ShortHCompressor(HCompressorOption options) { |
| 163 |
super(options); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public boolean compress(ShortBuffer buffer, ByteBuffer compressed) { |
| 168 |
short[] shortArray = new short[buffer.limit()]; |
| 169 |
buffer.get(shortArray); |
| 170 |
long[] longArray = new long[shortArray.length]; |
| 171 |
ArrayFuncs.copyInto(shortArray, longArray); |
| 172 |
compress(longArray, compressed); |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void decompress(ByteBuffer compressed, ShortBuffer buffer) { |
| 178 |
long[] longArray = new long[buffer.limit()]; |
| 179 |
decompress(compressed, longArray); |
| 180 |
for (long element : longArray) { |
| 181 |
buffer.put((short) element); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
private final HCompress compress; |
| 187 |
|
| 188 |
private final HDecompress decompress; |
| 189 |
|
| 190 |
private final HCompressorOption options; |
| 191 |
|
| 192 |
public HCompressor(HCompressorOption options) { |
| 193 |
this.options = options; |
| 194 |
compress = new HCompress(); |
| 195 |
decompress = new HDecompress(); |
| 196 |
} |
| 197 |
|
| 198 |
private HCompress compress() { |
| 199 |
return compress; |
| 200 |
} |
| 201 |
|
| 202 |
protected void compress(long[] longArray, ByteBuffer compressed) { |
| 203 |
compress().compress(longArray, options.getTileHeight(), options.getTileWidth(), options.getScale(), compressed); |
| 204 |
} |
| 205 |
|
| 206 |
private HDecompress decompress() { |
| 207 |
return decompress; |
| 208 |
} |
| 209 |
|
| 210 |
protected void decompress(ByteBuffer compressed, long[] aa) { |
| 211 |
decompress().decompress(compressed, options.isSmooth(), aa); |
| 212 |
} |
| 213 |
} |