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      /** For thread synchronization */
60      protected Object lock = new Object();
61  
62      protected TileCompressionOperation(TiledImageCompressionOperation operation, int tileIndex, TileArea area) {
63          super(operation, tileIndex, area);
64      }
65  
66      @Override
67      public String toString() {
68          synchronized (lock) {
69              return getClass().getSimpleName() + "(" + getTileIndex() + "," + compressionType + "," + compressedOffset + ")";
70          }
71      }
72  
73      private ByteBuffer convertToBuffer(Object data) {
74          return ElementType.forClass(data.getClass().getComponentType()).convertToByteBuffer(data);
75      }
76  
77      /**
78       * should the data of this tile be forced to case no data loss. This information is not relevant in all cases that
79       * it is ignored by default.
80       *
81       * @param value the value to set.
82       */
83      protected void forceNoLoss(boolean value) {
84      }
85  
86      protected byte[] getCompressedData() {
87          synchronized (lock) {
88              byte[] data = new byte[compressedData.limit()];
89              compressedData.rewind();
90              ElementType.BYTE.getArray(compressedData, data);
91              return data;
92          }
93      }
94  
95      protected ByteBuffer getCompressedWholeArea() {
96          return getTiledImageOperation().getCompressedWholeArea();
97      }
98  
99      protected TileCompressionType getCompressionType() {
100         synchronized (lock) {
101             return compressionType;
102         }
103     }
104 
105     protected ICompressorControl getCompressorControl() {
106         return getTiledImageOperation().getCompressorControl();
107     }
108 
109     protected ICompressorControl getGzipCompressorControl() {
110         return getTiledImageOperation().getGzipCompressorControl();
111     }
112 
113     protected TileCompressionOperation initTileOptions() {
114         synchronized (lock) {
115             tileOptions = getTiledImageOperation().compressOptions().copy() //
116                     .setTileWidth(getTileBuffer().getWidth()) //
117                     .setTileHeight(getTileBuffer().getHeight());
118         }
119         return this;
120     }
121 
122     protected TileCompressionOperation setCompressed(Object data, TileCompressionType type) {
123         if (data != null && Array.getLength(data) > 0) {
124             synchronized (lock) {
125                 compressionType = type;
126                 compressedData = convertToBuffer(data);
127                 compressedOffset = 0;
128             }
129         }
130         return this;
131     }
132 
133     protected TileCompressionOperation setCompressedOffset(int value) {
134         synchronized (lock) {
135             compressedOffset = value;
136         }
137         return this;
138     }
139 
140     @Override
141     public TileCompressionOperation setDimensions(int dataOffset, int width, int height) {
142         super.setDimensions(dataOffset, width, height);
143         return this;
144     }
145 
146     /**
147      * set the buffer that describes the whole compressed image and let the tile create a slice of it from the position
148      * where the tile starts in the whole image. Attention this method is not thread-safe because it changes the
149      * position of the buffer parameter. This buffer is just as big as the image buffer but will be reduced to the
150      * needed size as a last step of the Compression.
151      *
152      * @param compressed the buffer that describes the whole image.
153      */
154     protected void setWholeImageCompressedBuffer(ByteBuffer compressed) {
155         synchronized (lock) {
156             compressed.position(compressedOffset * getBaseType().size());
157             compressedData = compressed.slice();
158             compressedOffset = 0;
159             // we do not limit this buffer but is expected not to write more than
160             // the uncompressed size.
161         }
162     }
163 
164     protected abstract AbstractNullPixelMask createImageNullPixelMask(ImageNullPixelMask imageNullPixelMask);
165 }