View Javadoc
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.ByteBuffer;
35  
36  import nom.tam.image.compression.tile.mask.ImageNullPixelMask;
37  import nom.tam.image.compression.tile.mask.NullPixelMaskPreserver;
38  import nom.tam.image.tile.operation.TileArea;
39  import nom.tam.util.type.ElementType;
40  
41  /**
42   * (<i>for internal use</i>) A parallel operation for compressing 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 TileDecompressor
46   */
47  public class TileCompressor extends TileCompressionOperation {
48  
49      private boolean forceNoLoss = false;
50  
51      private NullPixelMaskPreserver nullPixelMaskPerserver;
52  
53      /**
54       * Creates a new tile compressor for a specific tile in the image.
55       * 
56       * @param array     the class that handles the compression of the entire image via parallel processing tiles.
57       * @param tileIndex the sequential index of the specific tile
58       * @param area      the location and size of the time in the complete image
59       */
60      protected TileCompressor(TiledImageCompressionOperation array, int tileIndex, TileArea area) {
61          super(array, tileIndex, area);
62      }
63  
64      @Override
65      public void run() {
66          compress();
67      }
68  
69      /**
70       * lets close the gaps in the data as soon as the previous tiles are also compressed. the compressed data of the
71       * first tile is used to append the complete block.
72       */
73      private synchronized void compactCompressedData() {
74          if (getTileIndex() > 0) {
75              // wait for the previous tile to finish.
76              getPreviousTileOperation().waitForResult();
77              ByteBuffer compressedWholeArea = getCompressedWholeArea();
78              compressedOffset = compressedWholeArea.position();
79              ElementType.BYTE.appendBuffer(compressedWholeArea, compressedData);
80              replaceCompressedBufferWithTargetArea(compressedWholeArea);
81          } else {
82              compressedOffset = 0;
83              getCompressedWholeArea().position(compressedData.limit());
84          }
85      }
86  
87      private synchronized void compress() {
88          initTileOptions();
89  
90          compressedData.limit(getTileBuffer().getPixelSize() * getBaseType().size());
91          compressionType = TileCompressionType.COMPRESSED;
92          boolean compressSuccess = false;
93          boolean tryNormalCompression = !(tileOptions.isLossyCompression() && forceNoLoss);
94  
95          tileOptions.getCompressionParameters().setTileIndex(getTileIndex());
96  
97          if (tryNormalCompression) {
98              compressSuccess = getCompressorControl().compress(getTileBuffer().getBuffer(), compressedData, tileOptions);
99              if (compressSuccess) {
100                 if (nullPixelMaskPerserver != null) {
101                     nullPixelMaskPerserver.preserveNull();
102                 }
103                 tileOptions.getCompressionParameters().setValuesInColumn(getTileIndex());
104             }
105         }
106 
107         if (!compressSuccess) {
108             compressionType = TileCompressionType.GZIP_COMPRESSED;
109             compressedData.rewind();
110             getTileBuffer().getBuffer().rewind();
111             compressSuccess = getGzipCompressorControl().compress(getTileBuffer().getBuffer(), compressedData, null);
112             if (compressSuccess) {
113                 tileOptions.getCompressionParameters().setValuesInColumn(getTileIndex());
114             }
115         }
116 
117         if (!compressSuccess) {
118             compressionType = TileCompressionType.UNCOMPRESSED;
119             compressedData.rewind();
120             getTileBuffer().getBuffer().rewind();
121             getBaseType().appendToByteBuffer(compressedData, getTileBuffer().getBuffer());
122         }
123 
124         compressedData.limit(compressedData.position());
125         compressedData.rewind();
126 
127         compactCompressedData();
128     }
129 
130     private synchronized void replaceCompressedBufferWithTargetArea(ByteBuffer compressedWholeArea) {
131         int compressedSize = compressedData.limit();
132         int latest = compressedWholeArea.position();
133         compressedWholeArea.position(compressedOffset);
134         compressedData = compressedWholeArea.slice();
135         compressedData.limit(compressedSize);
136         compressedWholeArea.position(latest);
137     }
138 
139     @Override
140     protected synchronized NullPixelMaskPreserver createImageNullPixelMask(ImageNullPixelMask imageNullPixelMask) {
141         if (imageNullPixelMask != null) {
142             nullPixelMaskPerserver = imageNullPixelMask.createTilePreserver(getTileBuffer(), getTileIndex());
143         }
144         return nullPixelMaskPerserver;
145     }
146 
147     @Override
148     protected synchronized void forceNoLoss(boolean value) {
149         forceNoLoss = value;
150     }
151 }