View Javadoc
1   package nom.tam.fits.compression.provider;
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.header.Compression;
35  
36  /**
37   * <p>
38   * (<i>for internal use</i>) Finds the name of the appropriate tile compressor class name given the algorithm used to
39   * quantize and compress the tile and the type of data the tile contains.
40   * </p>
41   * The name of the class is built of four parts:
42   * <ul>
43   * <li>the capitalized simple name of the base type of the elements in the tile (like Int, Long etc.);</li>
44   * <li>if a known quantize algorithm is used, the word "Quant", the word "Unknown" if the quantize algorithm is not
45   * recognized, nothing (i.e. the empty string) if it is null;</li>
46   * <li>the short name of the compression algorithm to use (Rice, PLIO, Gzip etc.) or the word "Unknown" if the algorithm
47   * is not supported;</li>
48   * <li>the suffix "Compressor"</li>
49   * </ul>
50   * <p>
51   * Following exception to above rules exist:
52   * </p>
53   * <ul>
54   * <li>If the primitive type is double or float, the quantize algorithm is ignored (as if it were specified as
55   * null)</li>
56   * </ul>
57   * See the associated unit tests for concrete examples.
58   */
59  @SuppressWarnings("javadoc")
60  public class CompressorControlNameComputer {
61  
62      private static final String COMPRESSOR_CLASS_SUFFIX = "Compressor";
63  
64      private static String standardizeBaseType(String simpleName) {
65          return Character.toUpperCase(simpleName.charAt(0)) + simpleName.substring(1).toLowerCase();
66      }
67  
68      private static String standardizeCompressionAlgorithm(String compressionAlgorithm) {
69          if (Compression.ZCMPTYPE_RICE_1.equalsIgnoreCase(compressionAlgorithm) || //
70                  Compression.ZCMPTYPE_RICE_ONE.equalsIgnoreCase(compressionAlgorithm)) {
71              return "Rice";
72          }
73          if (Compression.ZCMPTYPE_PLIO_1.equalsIgnoreCase(compressionAlgorithm)) {
74              return "PLIO";
75          }
76          if (Compression.ZCMPTYPE_HCOMPRESS_1.equalsIgnoreCase(compressionAlgorithm)) {
77              return "H";
78          }
79          if (Compression.ZCMPTYPE_GZIP_2.equalsIgnoreCase(compressionAlgorithm)) {
80              return "GZip2";
81          }
82          if (Compression.ZCMPTYPE_GZIP_1.equalsIgnoreCase(compressionAlgorithm)) {
83              return "GZip";
84          }
85          if (Compression.ZCMPTYPE_NOCOMPRESS.equalsIgnoreCase(compressionAlgorithm)) {
86              return "NoCompress";
87          }
88          return "Unknown";
89      }
90  
91      private static String standardizeQuantAlgorithm(String quantAlgorithm) {
92          if (quantAlgorithm != null) {
93              if (Compression.ZQUANTIZ_NO_DITHER.equalsIgnoreCase(quantAlgorithm) || //
94                      Compression.ZQUANTIZ_SUBTRACTIVE_DITHER_1.equalsIgnoreCase(quantAlgorithm) || //
95                      Compression.ZQUANTIZ_SUBTRACTIVE_DITHER_2.equalsIgnoreCase(quantAlgorithm)) {
96                  return "Quant";
97              }
98              return "Unknown";
99          }
100         return "";
101     }
102 
103     public CompressorControlNameComputer() {
104         super();
105     }
106 
107     public String createCompressorClassName(String quantAlgorithm, String compressionAlgorithm, Class<?> baseType) {
108         StringBuilder className = new StringBuilder();
109         className.append(standardizeBaseType(baseType.getSimpleName()));
110         if (className.indexOf(Float.class.getSimpleName()) < 0 && className.indexOf(Double.class.getSimpleName()) < 0) {
111             className.append(standardizeQuantAlgorithm(quantAlgorithm));
112         }
113         className.append(standardizeCompressionAlgorithm(compressionAlgorithm));
114         className.append(COMPRESSOR_CLASS_SUFFIX);
115         return className.toString();
116     }
117 }