1 package nom.tam.image.compression.tile;
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.util.logging.Logger;
36
37 import nom.tam.image.compression.tile.mask.ImageNullPixelMask;
38 import nom.tam.image.compression.tile.mask.NullPixelMaskRestorer;
39 import nom.tam.image.tile.operation.TileArea;
40
41 /**
42 * (<i>for internal use</i>) A parallel operation for decompressing a specific image or binary table tile. Each instance
43 * will be processed in a single thread, but operations on separate tiles can be (and will be) processed in parallel.
44 *
45 * @see TileCompressor
46 */
47 public class TileDecompressor extends TileCompressionOperation {
48
49 /**
50 * logger to log to.
51 */
52 private static final Logger LOG = Logger.getLogger(TileDecompressor.class.getName());
53
54 private NullPixelMaskRestorer nullPixelMaskRestorer;
55
56 /**
57 * Creates a new tile decompressor for a specific tile in the image.
58 *
59 * @param array the class that handles the compression of the entire image via parallel processing tiles.
60 * @param tileIndex the sequential index of the specific tile
61 * @param area the location and size of the time in the complete image
62 */
63 protected TileDecompressor(TiledImageCompressionOperation array, int tileIndex, TileArea area) {
64 super(array, tileIndex, area);
65 }
66
67 @Override
68 public void run() {
69 decompress();
70 getTileBuffer().finish();
71 }
72
73 private synchronized void decompress() {
74 initTileOptions();
75
76 tileOptions.getCompressionParameters().setTileIndex(getTileIndex());
77
78 if (compressionType == TileCompressionType.COMPRESSED) {
79 tileOptions.getCompressionParameters().getValuesFromColumn(getTileIndex());
80 getCompressorControl().decompress(compressedData, getTileBuffer().getBuffer(), tileOptions);
81 if (nullPixelMaskRestorer != null) {
82 nullPixelMaskRestorer.restoreNulls();
83 }
84 } else if (compressionType == TileCompressionType.GZIP_COMPRESSED) {
85 tileOptions.getCompressionParameters().getValuesFromColumn(getTileIndex());
86 getGzipCompressorControl().decompress(compressedData, getTileBuffer().getBuffer(), null);
87 } else if (compressionType == TileCompressionType.UNCOMPRESSED) {
88 Buffer typedBuffer = getBaseType().asTypedBuffer(compressedData);
89 getBaseType().appendBuffer(getTileBuffer().getBuffer(), typedBuffer);
90 } else {
91 LOG.severe("Unknown compression column");
92 throw new IllegalStateException("Unknown compression column");
93 }
94 }
95
96 @Override
97 protected synchronized NullPixelMaskRestorer createImageNullPixelMask(ImageNullPixelMask imageNullPixelMask) {
98 if (imageNullPixelMask != null) {
99 nullPixelMaskRestorer = imageNullPixelMask.createTileRestorer(getTileBuffer(), getTileIndex());
100 }
101 return nullPixelMaskRestorer;
102 }
103 }