1 package nom.tam.fits.compression.algorithm.rice;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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.rice.RiceCompressParameters;
37 import nom.tam.util.type.ElementType;
38
39
40
41
42
43
44
45
46
47 public class RiceCompressOption implements ICompressOption {
48
49
50 public static final int DEFAULT_RICE_BLOCKSIZE = 32;
51
52
53 public static final int DEFAULT_RICE_BYTEPIX = ElementType.INT.size();
54
55
56 private static final int[] VALID_BYTEPIX = {1, 2, 4, 8};
57
58
59 private static final int[] VALID_BLOCKSIZE = {16, 32};
60
61
62 private RiceCompressParameters parameters;
63
64
65 private final Config config;
66
67
68
69
70 public RiceCompressOption() {
71 config = new Config();
72 parameters = new RiceCompressParameters(this);
73 }
74
75 @Override
76 public RiceCompressOption copy() {
77 try {
78 RiceCompressOption copy = (RiceCompressOption) clone();
79 copy.parameters = parameters.copy(copy);
80 return copy;
81 } catch (CloneNotSupportedException e) {
82 throw new IllegalStateException("option could not be cloned", e);
83 }
84 }
85
86
87
88
89
90
91
92
93 public final int getBlockSize() {
94 return config.blockSize;
95 }
96
97
98
99
100
101
102
103
104 public final int getBytePix() {
105 return config.bytePix;
106 }
107
108 @Override
109 public RiceCompressParameters getCompressionParameters() {
110 return parameters;
111 }
112
113 @Override
114 public boolean isLossyCompression() {
115 return false;
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129 public RiceCompressOption setBlockSize(int value) throws IllegalArgumentException {
130 for (int i : VALID_BLOCKSIZE) {
131 if (value == i) {
132 config.blockSize = value;
133 return this;
134 }
135 }
136 throw new IllegalArgumentException("Invalid BYTEPIX value: " + value + " (must be 16 or 32)");
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public RiceCompressOption setBytePix(int value) throws IllegalArgumentException {
152 for (int i : VALID_BYTEPIX) {
153 if (value == i) {
154 config.bytePix = value;
155 return this;
156 }
157 }
158 throw new IllegalArgumentException("Invalid BYTEPIX value: " + value + " (must be 1, 2, 4, or 8)");
159 }
160
161 @Override
162 public void setParameters(ICompressParameters parameters) {
163 if (!(parameters instanceof RiceCompressParameters)) {
164 throw new IllegalArgumentException("Wrong type of parameters: " + parameters.getClass().getName());
165 }
166 this.parameters = (RiceCompressParameters) parameters.copy(this);
167 }
168
169 @Override
170 public RiceCompressOption setTileHeight(int value) {
171 return this;
172 }
173
174 @Override
175 public RiceCompressOption setTileWidth(int value) {
176 return this;
177 }
178
179 @Override
180 public <T> T unwrap(Class<T> clazz) {
181 if (clazz.isAssignableFrom(this.getClass())) {
182 return clazz.cast(this);
183 }
184 return null;
185 }
186
187
188
189
190
191
192
193
194 private static final class Config {
195 private int bytePix = DEFAULT_RICE_BYTEPIX;
196
197 private int blockSize = DEFAULT_RICE_BLOCKSIZE;
198 }
199 }