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.lang.reflect.Array;
35  import java.nio.ByteBuffer;
36  
37  import nom.tam.fits.compression.algorithm.api.ICompressOption;
38  import nom.tam.fits.compression.algorithm.api.ICompressorControl;
39  import nom.tam.image.compression.tile.mask.AbstractNullPixelMask;
40  import nom.tam.image.compression.tile.mask.ImageNullPixelMask;
41  import nom.tam.image.tile.operation.AbstractTileOperation;
42  import nom.tam.image.tile.operation.ITileOperation;
43  import nom.tam.image.tile.operation.TileArea;
44  import nom.tam.util.type.ElementType;
45  
46  /**
47   * (<i>for internal use</i>) An abstract compression operation on an image tile.
48   */
49  abstract class TileCompressionOperation extends AbstractTileOperation implements ITileOperation {
50  
51      protected ByteBuffer compressedData;
52  
53      protected int compressedOffset;
54  
55      protected TileCompressionType compressionType;
56  
57      protected ICompressOption tileOptions;
58  
59      protected TileCompressionOperation(TiledImageCompressionOperation operation, int tileIndex, TileArea area) {
60          super(operation, tileIndex, area);
61      }
62  
63      @Override
64      public String toString() {
65          synchronized (this) {
66              return getClass().getSimpleName() + "(" + getTileIndex() + "," + compressionType + "," + compressedOffset + ")";
67          }
68      }
69  
70      private ByteBuffer convertToBuffer(Object data) {
71          return ElementType.forClass(data.getClass().getComponentType()).convertToByteBuffer(data);
72      }
73  
74      /**
75       * should the data of this tile be forced to case no data loss. This information is not relevant in all cases that
76       * it is ignored by default.
77       *
78       * @param value the value to set.
79       */
80      protected void forceNoLoss(boolean value) {
81      }
82  
83      protected synchronized byte[] getCompressedData() {
84          byte[] data = new byte[compressedData.limit()];
85          compressedData.rewind();
86          ElementType.BYTE.getArray(compressedData, data);
87          return data;
88      }
89  
90      protected ByteBuffer getCompressedWholeArea() {
91          return getTiledImageOperation().getCompressedWholeArea();
92      }
93  
94      protected synchronized TileCompressionType getCompressionType() {
95          return compressionType;
96      }
97  
98      protected ICompressorControl getCompressorControl() {
99          return getTiledImageOperation().getCompressorControl();
100     }
101 
102     protected ICompressorControl getGzipCompressorControl() {
103         return getTiledImageOperation().getGzipCompressorControl();
104     }
105 
106     protected synchronized TileCompressionOperation initTileOptions() {
107         tileOptions = getTiledImageOperation().compressOptions().copy() //
108                 .setTileWidth(getTileBuffer().getWidth()) //
109                 .setTileHeight(getTileBuffer().getHeight());
110         return this;
111     }
112 
113     protected synchronized TileCompressionOperation setCompressed(Object data, TileCompressionType type) {
114         if (data != null && Array.getLength(data) > 0) {
115             compressionType = type;
116             compressedData = convertToBuffer(data);
117             compressedOffset = 0;
118         }
119         return this;
120     }
121 
122     protected synchronized TileCompressionOperation setCompressedOffset(int value) {
123         compressedOffset = value;
124         return this;
125     }
126 
127     @Override
128     public TileCompressionOperation setDimensions(int dataOffset, int width, int height) {
129         super.setDimensions(dataOffset, width, height);
130         return this;
131     }
132 
133     /**
134      * set the buffer that describes the whole compressed image and let the tile create a slice of it from the position
135      * where the tile starts in the whole image. Attention this method is not thread-safe because it changes the
136      * position of the buffer parameter. This buffer is just as big as the image buffer but will be reduced to the
137      * needed size as a last step of the Compression.
138      *
139      * @param compressed the buffer that describes the whole image.
140      */
141     protected synchronized void setWholeImageCompressedBuffer(ByteBuffer compressed) {
142         compressed.position(compressedOffset * getBaseType().size());
143         compressedData = compressed.slice();
144         compressedOffset = 0;
145         // we do not limit this buffer but is expected not to write more than
146         // the uncompressed size.
147     }
148 
149     protected abstract AbstractNullPixelMask createImageNullPixelMask(ImageNullPixelMask imageNullPixelMask);
150 }