| 1 |
package nom.tam.fits.compression.algorithm.gzip; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.nio.Buffer; |
| 5 |
import java.nio.ByteBuffer; |
| 6 |
import java.nio.ByteOrder; |
| 7 |
import java.nio.DoubleBuffer; |
| 8 |
import java.nio.FloatBuffer; |
| 9 |
import java.nio.IntBuffer; |
| 10 |
import java.nio.LongBuffer; |
| 11 |
import java.nio.ShortBuffer; |
| 12 |
import java.util.zip.GZIPInputStream; |
| 13 |
import java.util.zip.GZIPOutputStream; |
| 14 |
|
| 15 |
import nom.tam.fits.compression.algorithm.api.ICompressor; |
| 16 |
import nom.tam.util.ArrayFuncs; |
| 17 |
import nom.tam.util.ByteBufferInputStream; |
| 18 |
import nom.tam.util.ByteBufferOutputStream; |
| 19 |
import nom.tam.util.FitsIO; |
| 20 |
import nom.tam.util.type.ElementType; |
| 21 |
|
| 22 |
/* |
| 23 |
* #%L |
| 24 |
* nom.tam FITS library |
| 25 |
* %% |
| 26 |
* Copyright (C) 1996 - 2024 nom-tam-fits |
| 27 |
* %% |
| 28 |
* This is free and unencumbered software released into the public domain. |
| 29 |
* |
| 30 |
* Anyone is free to copy, modify, publish, use, compile, sell, or |
| 31 |
* distribute this software, either in source code form or as a compiled |
| 32 |
* binary, for any purpose, commercial or non-commercial, and by any |
| 33 |
* means. |
| 34 |
* |
| 35 |
* In jurisdictions that recognize copyright laws, the author or authors |
| 36 |
* of this software dedicate any and all copyright interest in the |
| 37 |
* software to the public domain. We make this dedication for the benefit |
| 38 |
* of the public at large and to the detriment of our heirs and |
| 39 |
* successors. We intend this dedication to be an overt act of |
| 40 |
* relinquishment in perpetuity of all present and future rights to this |
| 41 |
* software under copyright law. |
| 42 |
* |
| 43 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 44 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 45 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 46 |
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 47 |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 48 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 49 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 50 |
* #L% |
| 51 |
*/ |
| 52 |
|
| 53 |
/** |
| 54 |
* (<i>for internal use</i>) The GZIP compression algorithm. |
| 55 |
* |
| 56 |
* @param <T> The genetic type of element buffer to compress |
| 57 |
*/ |
| 58 |
@SuppressWarnings("javadoc") |
| 59 |
public abstract class GZipCompressor<T extends Buffer> implements ICompressor<T> { |
| 60 |
|
| 61 |
/** |
| 62 |
* Byte compress is a special case, the only one that does not extends GZipCompress because it can write the buffer |
| 63 |
* directly. |
| 64 |
*/ |
| 65 |
public static class ByteGZipCompressor extends GZipCompressor<ByteBuffer> { |
| 66 |
|
| 67 |
public ByteGZipCompressor() { |
| 68 |
super(1); |
| 69 |
nioBuffer = ByteBuffer.wrap(buffer); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
protected void getPixel(ByteBuffer pixelData, byte[] pixelBytes) { |
| 74 |
nioBuffer.put(pixelData); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
protected void setPixel(ByteBuffer pixelData, byte[] pixelBytes) { |
| 79 |
pixelData.put(nioBuffer); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public static class DoubleGZipCompressor extends GZipCompressor<DoubleBuffer> { |
| 84 |
|
| 85 |
protected static final int BYTE_SIZE_OF_DOUBLE = 8; |
| 86 |
|
| 87 |
public DoubleGZipCompressor() { |
| 88 |
super(BYTE_SIZE_OF_DOUBLE); |
| 89 |
nioBuffer = ByteBuffer.wrap(buffer).asDoubleBuffer(); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
protected void getPixel(DoubleBuffer pixelData, byte[] pixelBytes) { |
| 94 |
nioBuffer.put(pixelData); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
protected void setPixel(DoubleBuffer pixelData, byte[] pixelBytes) { |
| 99 |
pixelData.put(nioBuffer); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
public static class FloatGZipCompressor extends GZipCompressor<FloatBuffer> { |
| 104 |
|
| 105 |
protected static final int BYTE_SIZE_OF_FLOAT = 4; |
| 106 |
|
| 107 |
public FloatGZipCompressor() { |
| 108 |
super(BYTE_SIZE_OF_FLOAT); |
| 109 |
nioBuffer = ByteBuffer.wrap(buffer).asFloatBuffer(); |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
protected void getPixel(FloatBuffer pixelData, byte[] pixelBytes) { |
| 114 |
nioBuffer.put(pixelData); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
protected void setPixel(FloatBuffer pixelData, byte[] pixelBytes) { |
| 119 |
pixelData.put(nioBuffer); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public static class IntGZipCompressor extends GZipCompressor<IntBuffer> { |
| 124 |
|
| 125 |
protected static final int BYTE_SIZE_OF_INT = 4; |
| 126 |
|
| 127 |
public IntGZipCompressor() { |
| 128 |
super(BYTE_SIZE_OF_INT); |
| 129 |
nioBuffer = ByteBuffer.wrap(buffer).asIntBuffer(); |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
protected void getPixel(IntBuffer pixelData, byte[] pixelBytes) { |
| 134 |
nioBuffer.put(pixelData); |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
protected void setPixel(IntBuffer pixelData, byte[] pixelBytes) { |
| 139 |
pixelData.put(nioBuffer); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
public static class LongGZipCompressor extends GZipCompressor<LongBuffer> { |
| 144 |
|
| 145 |
protected static final int BYTE_SIZE_OF_LONG = 8; |
| 146 |
|
| 147 |
public LongGZipCompressor() { |
| 148 |
super(BYTE_SIZE_OF_LONG); |
| 149 |
nioBuffer = ByteBuffer.wrap(buffer).asLongBuffer(); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
protected void getPixel(LongBuffer pixelData, byte[] pixelBytes) { |
| 154 |
nioBuffer.put(pixelData); |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
protected void setPixel(LongBuffer pixelData, byte[] pixelBytes) { |
| 159 |
pixelData.put(nioBuffer); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
public static class ShortGZipCompressor extends GZipCompressor<ShortBuffer> { |
| 164 |
|
| 165 |
protected static final int BYTE_SIZE_OF_SHORT = 2; |
| 166 |
|
| 167 |
public ShortGZipCompressor() { |
| 168 |
super(BYTE_SIZE_OF_SHORT); |
| 169 |
nioBuffer = ByteBuffer.wrap(buffer).asShortBuffer(); |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
protected void getPixel(ShortBuffer pixelData, byte[] pixelBytes) { |
| 174 |
nioBuffer.put(pixelData); |
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
protected void setPixel(ShortBuffer pixelData, byte[] pixelBytes) { |
| 179 |
pixelData.put(nioBuffer); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
private final class TypeConversion<B extends Buffer> { |
| 184 |
|
| 185 |
private final ElementType<B> from; |
| 186 |
|
| 187 |
private final ElementType<T> to; |
| 188 |
|
| 189 |
private final B fromBuffer; |
| 190 |
|
| 191 |
private final T toBuffer; |
| 192 |
|
| 193 |
private final Object fromArray; |
| 194 |
|
| 195 |
private final Object toArray; |
| 196 |
|
| 197 |
private TypeConversion(ElementType<B> from) { |
| 198 |
this.from = from; |
| 199 |
to = getElementType(GZipCompressor.this.primitiveSize); |
| 200 |
toBuffer = GZipCompressor.this.nioBuffer; |
| 201 |
fromBuffer = from.asTypedBuffer(ByteBuffer.wrap(GZipCompressor.this.buffer)); |
| 202 |
fromArray = from.newArray(DEFAULT_GZIP_BUFFER_SIZE / from.size()); |
| 203 |
toArray = to.newArray(DEFAULT_GZIP_BUFFER_SIZE / to.size()); |
| 204 |
} |
| 205 |
|
| 206 |
int copy(int byteCount) { |
| 207 |
fromBuffer.rewind(); |
| 208 |
toBuffer.rewind(); |
| 209 |
from.getArray(fromBuffer, fromArray); |
| 210 |
ArrayFuncs.copyInto(fromArray, toArray); |
| 211 |
to.putArray(toBuffer, toArray); |
| 212 |
return byteCount * to.size() / from.size(); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
private static final int DEFAULT_GZIP_BUFFER_SIZE = 65536; |
| 217 |
|
| 218 |
private static final int MINIMAL_GZIP_BUFFER_SIZE = 65536; |
| 219 |
|
| 220 |
protected final int primitiveSize; |
| 221 |
|
| 222 |
protected byte[] buffer = new byte[DEFAULT_GZIP_BUFFER_SIZE]; |
| 223 |
|
| 224 |
protected T nioBuffer; |
| 225 |
|
| 226 |
private final byte[] sizeArray = new byte[ElementType.INT.size()]; |
| 227 |
|
| 228 |
private final IntBuffer sizeBuffer = ByteBuffer.wrap(sizeArray).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer(); |
| 229 |
|
| 230 |
public GZipCompressor(int primitiveSize) { |
| 231 |
this.primitiveSize = primitiveSize; |
| 232 |
} |
| 233 |
|
| 234 |
@Override |
| 235 |
public boolean compress(T pixelData, ByteBuffer compressed) { |
| 236 |
nioBuffer.rewind(); |
| 237 |
int pixelDataLimit = pixelData.limit(); |
| 238 |
try (GZIPOutputStream zip = createGZipOutputStream(pixelDataLimit, compressed)) { |
| 239 |
while (pixelData.hasRemaining()) { |
| 240 |
int count = Math.min(pixelData.remaining(), nioBuffer.capacity()); |
| 241 |
pixelData.limit(pixelData.position() + count); |
| 242 |
getPixel(pixelData, null); |
| 243 |
zip.write(buffer, 0, nioBuffer.position() * primitiveSize); |
| 244 |
nioBuffer.rewind(); |
| 245 |
pixelData.limit(pixelDataLimit); |
| 246 |
} |
| 247 |
} catch (IOException e) { |
| 248 |
throw new IllegalStateException("could not gzip data", e); |
| 249 |
} |
| 250 |
compressed.limit(compressed.position()); |
| 251 |
return true; |
| 252 |
} |
| 253 |
|
| 254 |
@Override |
| 255 |
public void decompress(ByteBuffer compressed, T pixelData) { |
| 256 |
nioBuffer.rewind(); |
| 257 |
TypeConversion<Buffer> typeConverter = getTypeConverter(compressed, pixelData.limit()); |
| 258 |
try (GZIPInputStream zip = createGZipInputStream(compressed)) { |
| 259 |
int count; |
| 260 |
while ((count = zip.read(buffer)) >= 0) { |
| 261 |
if (typeConverter != null) { |
| 262 |
count = typeConverter.copy(count); |
| 263 |
} |
| 264 |
nioBuffer.position(0); |
| 265 |
nioBuffer.limit(count / primitiveSize); |
| 266 |
setPixel(pixelData, null); |
| 267 |
} |
| 268 |
} catch (IOException e) { |
| 269 |
throw new IllegalStateException("could not gunzip data", e); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
@SuppressWarnings("unchecked") |
| 274 |
private <B extends Buffer> ElementType<B> getElementType(int size) { |
| 275 |
return (ElementType<B>) ElementType.forBitpix(size * FitsIO.BITS_OF_1_BYTE); |
| 276 |
} |
| 277 |
|
| 278 |
private TypeConversion<Buffer> getTypeConverter(ByteBuffer compressed, int nrOfPrimitiveElements) { |
| 279 |
if (compressed.limit() > FitsIO.BYTES_IN_INTEGER) { |
| 280 |
int oldPosition = compressed.position(); |
| 281 |
try { |
| 282 |
compressed.position(compressed.limit() - sizeArray.length); |
| 283 |
compressed.get(sizeArray); |
| 284 |
int uncompressedSize = sizeBuffer.get(0); |
| 285 |
if (uncompressedSize > 0) { |
| 286 |
compressed.position(oldPosition); |
| 287 |
if (uncompressedSize % nrOfPrimitiveElements == 0) { |
| 288 |
int compressedPrimitiveSize = uncompressedSize / nrOfPrimitiveElements; |
| 289 |
if (compressedPrimitiveSize != primitiveSize) { |
| 290 |
return new TypeConversion<>(getElementType(compressedPrimitiveSize)); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
} finally { |
| 295 |
compressed.position(oldPosition); |
| 296 |
} |
| 297 |
} |
| 298 |
return null; |
| 299 |
} |
| 300 |
|
| 301 |
protected GZIPInputStream createGZipInputStream(ByteBuffer compressed) throws IOException { |
| 302 |
return new GZIPInputStream(new ByteBufferInputStream(compressed), |
| 303 |
Math.min(compressed.limit() * 2, DEFAULT_GZIP_BUFFER_SIZE)); |
| 304 |
} |
| 305 |
|
| 306 |
protected GZIPOutputStream createGZipOutputStream(int length, ByteBuffer compressed) throws IOException { |
| 307 |
return new GZIPOutputStream(new ByteBufferOutputStream(compressed), |
| 308 |
Math.min(Math.max(length * 2, MINIMAL_GZIP_BUFFER_SIZE), DEFAULT_GZIP_BUFFER_SIZE)); |
| 309 |
} |
| 310 |
|
| 311 |
protected abstract void getPixel(T pixelData, byte[] pixelBytes); |
| 312 |
|
| 313 |
protected abstract void setPixel(T pixelData, byte[] pixelBytes); |
| 314 |
|
| 315 |
} |