View Javadoc
1   package nom.tam.fits.compression.algorithm.hcompress;
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 nom.tam.fits.compression.algorithm.api.ICompressOption;
35  import nom.tam.fits.compression.provider.param.api.ICompressParameters;
36  import nom.tam.fits.compression.provider.param.hcompress.HCompressParameters;
37  
38  /**
39   * Options to the HCompress compression algorithm. When compressing tables and images using the HCompress algorithm,
40   * users can control how exactly the compression is perfomed. When reading compressed FITS files, these options will be
41   * set automatically based on the header values recorded in the compressed HDU.
42   * 
43   * @see nom.tam.image.compression.hdu.CompressedImageHDU#setCompressAlgorithm(String)
44   * @see nom.tam.image.compression.hdu.CompressedImageHDU#getCompressOption(Class)
45   */
46  public class HCompressorOption implements ICompressOption {
47  
48      /** Shared configuration across copies. */
49      private final Config config;
50  
51      /** The parameters that represent settings for this option in the FITS headers and/or compressed data columns */
52      private HCompressParameters parameters;
53  
54      private int tileHeight;
55  
56      private int tileWidth;
57  
58      /**
59       * Creates a new set of options for HCompress.
60       */
61      public HCompressorOption() {
62          config = new Config();
63          setParameters(new HCompressParameters(this));
64      }
65  
66      @Override
67      public HCompressorOption copy() {
68          try {
69              HCompressorOption copy = (HCompressorOption) clone();
70              copy.parameters = parameters.copy(copy);
71              return copy;
72          } catch (CloneNotSupportedException e) {
73              throw new IllegalStateException("option could not be cloned", e);
74          }
75      }
76  
77      @Override
78      public HCompressParameters getCompressionParameters() {
79          return parameters;
80      }
81  
82      /**
83       * Returns the scale parameter value
84       * 
85       * @return the value of the scale parameter.
86       * 
87       * @see    #setScale(double)
88       */
89      public int getScale() {
90          return config.scale;
91      }
92  
93      @Override
94      public int getTileHeight() {
95          return tileHeight;
96      }
97  
98      @Override
99      public int getTileWidth() {
100         return tileWidth;
101     }
102 
103     @Override
104     public boolean isLossyCompression() {
105         return config.scale > 0 || config.smooth;
106     }
107 
108     /**
109      * Checks if smoothing is enabled
110      * 
111      * @return <code>true</code> if smoothing is enabled, otherwise <code>false</code>.
112      * 
113      * @see    #setSmooth(boolean)
114      */
115     public boolean isSmooth() {
116         return config.smooth;
117     }
118 
119     @Override
120     public void setParameters(ICompressParameters parameters) {
121         if (!(parameters instanceof HCompressParameters)) {
122             throw new IllegalArgumentException("Wrong type of parameters: " + parameters.getClass().getName());
123         }
124         this.parameters = (HCompressParameters) parameters;
125     }
126 
127     /**
128      * Sets the scale parameter
129      * 
130      * @param  value                    the new scale parameter, which will be rounded to the nearest integer value for
131      *                                      the actual implementation.
132      * 
133      * @return                          itself
134      * 
135      * @throws IllegalArgumentException if the scale value is negative
136      * 
137      * @see                             #getScale()
138      */
139     public HCompressorOption setScale(double value) throws IllegalArgumentException {
140         if (value < 0.0) {
141             throw new IllegalArgumentException("Scale value cannot be negative: " + value);
142         }
143         config.scale = (int) Math.round(value);
144         return this;
145     }
146 
147     /**
148      * Enabled or disables smoothing.
149      * 
150      * @param  value <code>true</code> to enable smoothing, or <code>false</code> to disable.
151      * 
152      * @return       itself
153      */
154     public HCompressorOption setSmooth(boolean value) {
155         config.smooth = value;
156         return this;
157     }
158 
159     @Override
160     public <T> T unwrap(Class<T> clazz) {
161         if (clazz.isAssignableFrom(this.getClass())) {
162             return clazz.cast(this);
163         }
164         return null;
165     }
166 
167     @Override
168     public HCompressorOption setTileHeight(int value) {
169         tileHeight = value;
170         return this;
171     }
172 
173     @Override
174     public HCompressorOption setTileWidth(int value) {
175         tileWidth = value;
176         return this;
177     }
178 
179     /**
180      * Stores configuration in a way that can be shared and modified across enclosing option copies.
181      * 
182      * @author Attila Kovacs
183      *
184      * @since  1.18
185      */
186     private static final class Config {
187 
188         private int scale;
189 
190         private boolean smooth;
191 
192     }
193 }