corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * CVS identifier:
3 *
4 * $Id: AnWTFilterSpec.java,v 1.27 2001/05/08 16:11:37 grosbois Exp $
5 *
6 * Class: AnWTFilterSpec
7 *
8 * Description: Analysis filters specification
9 *
10 *
11 *
12 * COPYRIGHT:
13 *
14 * This software module was originally developed by Raphaël Grosbois and
15 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
16 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
17 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
18 * Centre France S.A) in the course of development of the JPEG2000
19 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
20 * software module is an implementation of a part of the JPEG 2000
21 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
22 * Systems AB and Canon Research Centre France S.A (collectively JJ2000
23 * Partners) agree not to assert against ISO/IEC and users of the JPEG
24 * 2000 Standard (Users) any of their rights under the copyright, not
25 * including other intellectual property rights, for this software module
26 * with respect to the usage by ISO/IEC and Users of this software module
27 * or modifications thereof for use in hardware or software products
28 * claiming conformance to the JPEG 2000 Standard. Those intending to use
29 * this software module in hardware or software products are advised that
30 * their use may infringe existing patents. The original developers of
31 * this software module, JJ2000 Partners and ISO/IEC assume no liability
32 * for use of this software module or modifications thereof. No license
33 * or right to this software module is granted for non JPEG 2000 Standard
34 * conforming products. JJ2000 Partners have full right to use this
35 * software module for his/her own purpose, assign or donate this
36 * software module to any third party and to inhibit third parties from
37 * using this software module for non JPEG 2000 Standard conforming
38 * products. This copyright notice must be included in all copies or
39 * derivative works of this software module.
40 *
41 * Copyright (c) 1999/2000 JJ2000 Partners.
42 * */
43 using System;
44 using CSJ2K.j2k.quantization;
45 using CSJ2K.j2k.util;
46 using CSJ2K.j2k;
47 namespace CSJ2K.j2k.wavelet.analysis
48 {
49  
50 /// <summary> This class extends ModuleSpec class for analysis filters specification
51 /// holding purpose.
52 ///
53 /// </summary>
54 /// <seealso cref="ModuleSpec">
55 ///
56 /// </seealso>
57 public class AnWTFilterSpec:ModuleSpec
58 {
59  
60 /// <summary>The reversible default filter </summary>
61 private const System.String REV_FILTER_STR = "w5x3";
62  
63 /// <summary>The non-reversible default filter </summary>
64 private const System.String NON_REV_FILTER_STR = "w9x7";
65  
66 /// <summary> Constructs a new 'AnWTFilterSpec' for the specified number of
67 /// components and tiles.
68 ///
69 /// </summary>
70 /// <param name="nt">The number of tiles
71 ///
72 /// </param>
73 /// <param name="nc">The number of components
74 ///
75 /// </param>
76 /// <param name="type">the type of the specification module i.e. tile specific,
77 /// component specific or both.
78 ///
79 /// </param>
80 /// <param name="qts">Quantization specifications
81 ///
82 /// </param>
83 /// <param name="pl">The ParameterList
84 ///
85 /// </param>
86 public AnWTFilterSpec(int nt, int nc, byte type, QuantTypeSpec qts, ParameterList pl):base(nt, nc, type)
87 {
88  
89 // Check parameters
90 pl.checkList(AnWTFilter.OPT_PREFIX, CSJ2K.j2k.util.ParameterList.toNameArray(AnWTFilter.ParameterInfo));
91  
92 System.String param = pl.getParameter("Ffilters");
93 bool isFilterSpecified = true;
94  
95 // No parameter specified
96 if (param == null)
97 {
98 isFilterSpecified = false;
99  
100 // If lossless compression, uses the reversible filters in each
101 // tile-components
102 if (pl.getBooleanParameter("lossless"))
103 {
104 setDefault(parseFilters(REV_FILTER_STR));
105 return ;
106 }
107  
108 // If no filter is specified through the command-line, use
109 // REV_FILTER_STR or NON_REV_FILTER_STR according to the
110 // quantization type
111 for (int t = nt - 1; t >= 0; t--)
112 {
113 for (int c = nc - 1; c >= 0; c--)
114 {
115 switch (qts.getSpecValType(t, c))
116 {
117  
118 case SPEC_DEF:
119 if (getDefault() == null)
120 {
121 if (pl.getBooleanParameter("lossless"))
122 setDefault(parseFilters(REV_FILTER_STR));
123 if (((System.String) qts.getDefault()).Equals("reversible"))
124 {
125 setDefault(parseFilters(REV_FILTER_STR));
126 }
127 else
128 {
129 setDefault(parseFilters(NON_REV_FILTER_STR));
130 }
131 }
132 specValType[t][c] = SPEC_DEF;
133 break;
134  
135 case SPEC_COMP_DEF:
136 if (!isCompSpecified(c))
137 {
138 if (((System.String) qts.getCompDef(c)).Equals("reversible"))
139 {
140 setCompDef(c, parseFilters(REV_FILTER_STR));
141 }
142 else
143 {
144 setCompDef(c, parseFilters(NON_REV_FILTER_STR));
145 }
146 }
147 specValType[t][c] = SPEC_COMP_DEF;
148 break;
149  
150 case SPEC_TILE_DEF:
151 if (!isTileSpecified(t))
152 {
153 if (((System.String) qts.getTileDef(t)).Equals("reversible"))
154 {
155 setTileDef(t, parseFilters(REV_FILTER_STR));
156 }
157 else
158 {
159 setTileDef(t, parseFilters(NON_REV_FILTER_STR));
160 }
161 }
162 specValType[t][c] = SPEC_TILE_DEF;
163 break;
164  
165 case SPEC_TILE_COMP:
166 if (!isTileCompSpecified(t, c))
167 {
168 if (((System.String) qts.getTileCompVal(t, c)).Equals("reversible"))
169 {
170 setTileCompVal(t, c, parseFilters(REV_FILTER_STR));
171 }
172 else
173 {
174 setTileCompVal(t, c, parseFilters(NON_REV_FILTER_STR));
175 }
176 }
177 specValType[t][c] = SPEC_TILE_COMP;
178 break;
179  
180 default:
181 throw new System.ArgumentException("Unsupported " + "specification " + "type");
182  
183 }
184 }
185 }
186 return ;
187 }
188  
189 // Parse argument
190 SupportClass.Tokenizer stk = new SupportClass.Tokenizer(param);
191 System.String word; // current word
192 byte curSpecType = SPEC_DEF; // Specification type of the
193 // current parameter
194 bool[] tileSpec = null; // Tiles concerned by the specification
195 bool[] compSpec = null; // Components concerned by the specification
196 AnWTFilter[][] filter;
197  
198 while (stk.HasMoreTokens())
199 {
200 word = stk.NextToken();
201  
202 switch (word[0])
203 {
204  
205 case 't':
206 // Tiles specification
207 case 'T': // Tiles specification
208 tileSpec = parseIdx(word, nTiles);
209 if (curSpecType == SPEC_COMP_DEF)
210 curSpecType = SPEC_TILE_COMP;
211 else
212 curSpecType = SPEC_TILE_DEF;
213 break;
214  
215 case 'c':
216 // Components specification
217 case 'C': // Components specification
218 compSpec = parseIdx(word, nComp);
219 if (curSpecType == SPEC_TILE_DEF)
220 curSpecType = SPEC_TILE_COMP;
221 else
222 curSpecType = SPEC_COMP_DEF;
223 break;
224  
225 case 'w':
226 // WT filters specification
227 case 'W': // WT filters specification
228 if (pl.getBooleanParameter("lossless") && word.ToUpper().Equals("w9x7".ToUpper()))
229 {
230 throw new System.ArgumentException("Cannot use non " + "reversible " + "wavelet transform with" + " '-lossless' option");
231 }
232  
233 filter = parseFilters(word);
234 if (curSpecType == SPEC_DEF)
235 {
236 setDefault(filter);
237 }
238 else if (curSpecType == SPEC_TILE_DEF)
239 {
240 for (int i = tileSpec.Length - 1; i >= 0; i--)
241 if (tileSpec[i])
242 {
243 setTileDef(i, filter);
244 }
245 }
246 else if (curSpecType == SPEC_COMP_DEF)
247 {
248 for (int i = compSpec.Length - 1; i >= 0; i--)
249 if (compSpec[i])
250 {
251 setCompDef(i, filter);
252 }
253 }
254 else
255 {
256 for (int i = tileSpec.Length - 1; i >= 0; i--)
257 {
258 for (int j = compSpec.Length - 1; j >= 0; j--)
259 {
260 if (tileSpec[i] && compSpec[j])
261 {
262 setTileCompVal(i, j, filter);
263 }
264 }
265 }
266 }
267  
268 // Re-initialize
269 curSpecType = SPEC_DEF;
270 tileSpec = null;
271 compSpec = null;
272 break;
273  
274  
275 default:
276 throw new System.ArgumentException("Bad construction for " + "parameter: " + word);
277  
278 }
279 }
280  
281 // Check that default value has been specified
282 if (getDefault() == null)
283 {
284 int ndefspec = 0;
285 for (int t = nt - 1; t >= 0; t--)
286 {
287 for (int c = nc - 1; c >= 0; c--)
288 {
289 if (specValType[t][c] == SPEC_DEF)
290 {
291 ndefspec++;
292 }
293 }
294 }
295  
296 // If some tile-component have received no specification, it takes
297 // the default value defined in ParameterList
298 if (ndefspec != 0)
299 {
300 if (((System.String) qts.getDefault()).Equals("reversible"))
301 setDefault(parseFilters(REV_FILTER_STR));
302 else
303 setDefault(parseFilters(NON_REV_FILTER_STR));
304 }
305 else
306 {
307 // All tile-component have been specified, takes the first
308 // tile-component value as default.
309 setDefault(getTileCompVal(0, 0));
310 switch (specValType[0][0])
311 {
312  
313 case SPEC_TILE_DEF:
314 for (int c = nc - 1; c >= 0; c--)
315 {
316 if (specValType[0][c] == SPEC_TILE_DEF)
317 specValType[0][c] = SPEC_DEF;
318 }
319 tileDef[0] = null;
320 break;
321  
322 case SPEC_COMP_DEF:
323 for (int t = nt - 1; t >= 0; t--)
324 {
325 if (specValType[t][0] == SPEC_COMP_DEF)
326 specValType[t][0] = SPEC_DEF;
327 }
328 compDef[0] = null;
329 break;
330  
331 case SPEC_TILE_COMP:
332 specValType[0][0] = SPEC_DEF;
333 tileCompVal["t0c0"] = null;
334 break;
335 }
336 }
337 }
338  
339 // Check consistency between filter and quantization type
340 // specification
341 for (int t = nt - 1; t >= 0; t--)
342 {
343 for (int c = nc - 1; c >= 0; c--)
344 {
345 // Reversible quantization
346 if (((System.String) qts.getTileCompVal(t, c)).Equals("reversible"))
347 {
348 // If filter is reversible, it is OK
349 if (isReversible(t, c))
350 continue;
351  
352 // If no filter has been defined, use reversible filter
353 if (!isFilterSpecified)
354 {
355 setTileCompVal(t, c, parseFilters(REV_FILTER_STR));
356 }
357 else
358 {
359 // Non reversible filter specified -> Error
360 throw new System.ArgumentException("Filter of " + "tile-component" + " (" + t + "," + c + ") does" + " not allow " + "reversible " + "quantization. " + "Specify '-Qtype " + "expounded' or " + "'-Qtype derived'" + "in " + "the command line.");
361 }
362 }
363 else
364 {
365 // No reversible quantization
366 // No reversible filter -> OK
367 if (!isReversible(t, c))
368 continue;
369  
370 // If no filter has been specified, use non-reversible
371 // filter
372 if (!isFilterSpecified)
373 {
374 setTileCompVal(t, c, parseFilters(NON_REV_FILTER_STR));
375 }
376 else
377 {
378 // Reversible filter specified -> Error
379 throw new System.ArgumentException("Filter of " + "tile-component" + " (" + t + "," + c + ") does" + " not allow " + "non-reversible " + "quantization. " + "Specify '-Qtype " + "reversible' in " + "the command line");
380 }
381 }
382 }
383 }
384 }
385  
386 /// <summary> Parse filters from the given word
387 ///
388 /// </summary>
389 /// <param name="word">String to parse
390 ///
391 /// </param>
392 /// <returns> Analysis wavelet filter (first dimension: by direction,
393 /// second dimension: by decomposition levels)
394 /// </returns>
395 private AnWTFilter[][] parseFilters(System.String word)
396 {
397 AnWTFilter[][] filt = new AnWTFilter[2][];
398 for (int i = 0; i < 2; i++)
399 {
400 filt[i] = new AnWTFilter[1];
401 }
402 if (word.ToUpper().Equals("w5x3".ToUpper()))
403 {
404 filt[0][0] = new AnWTFilterIntLift5x3();
405 filt[1][0] = new AnWTFilterIntLift5x3();
406 return filt;
407 }
408 else if (word.ToUpper().Equals("w9x7".ToUpper()))
409 {
410 filt[0][0] = new AnWTFilterFloatLift9x7();
411 filt[1][0] = new AnWTFilterFloatLift9x7();
412 return filt;
413 }
414 else
415 {
416 throw new System.ArgumentException("Non JPEG 2000 part I filter: " + word);
417 }
418 }
419  
420 /// <summary> Returns the data type used by the filters in this object, as defined in
421 /// the 'DataBlk' interface for specified tile-component.
422 ///
423 /// </summary>
424 /// <param name="t">Tile index
425 ///
426 /// </param>
427 /// <param name="c">Component index
428 ///
429 /// </param>
430 /// <returns> The data type of the filters in this object
431 ///
432 /// </returns>
433 /// <seealso cref="jj2000.j2k.image.DataBlk">
434 ///
435 /// </seealso>
436 public virtual int getWTDataType(int t, int c)
437 {
438 AnWTFilter[][] an = (AnWTFilter[][]) getSpec(t, c);
439 return an[0][0].DataType;
440 }
441  
442 /// <summary> Returns the horizontal analysis filters to be used in component 'n' and
443 /// tile 't'.
444 ///
445 /// <P>The horizontal analysis filters are returned in an array of
446 /// AnWTFilter. Each element contains the horizontal filter for each
447 /// resolution level starting with resolution level 1 (i.e. the analysis
448 /// filter to go from resolution level 1 to resolution level 0). If there
449 /// are less elements than the maximum resolution level, then the last
450 /// element is assumed to be repeated.
451 ///
452 /// </summary>
453 /// <param name="t">The tile index, in raster scan order
454 ///
455 /// </param>
456 /// <param name="c">The component index.
457 ///
458 /// </param>
459 /// <returns> The array of horizontal analysis filters for component 'n' and
460 /// tile 't'.
461 ///
462 /// </returns>
463 public virtual AnWTFilter[] getHFilters(int t, int c)
464 {
465 AnWTFilter[][] an = (AnWTFilter[][]) getSpec(t, c);
466 return an[0];
467 }
468  
469 /// <summary> Returns the vertical analysis filters to be used in component 'n' and
470 /// tile 't'.
471 ///
472 /// <P>The vertical analysis filters are returned in an array of
473 /// AnWTFilter. Each element contains the vertical filter for each
474 /// resolution level starting with resolution level 1 (i.e. the analysis
475 /// filter to go from resolution level 1 to resolution level 0). If there
476 /// are less elements than the maximum resolution level, then the last
477 /// element is assumed to be repeated.
478 ///
479 /// </summary>
480 /// <param name="t">The tile index, in raster scan order
481 ///
482 /// </param>
483 /// <param name="c">The component index.
484 ///
485 /// </param>
486 /// <returns> The array of horizontal analysis filters for component 'n' and
487 /// tile 't'.
488 ///
489 /// </returns>
490 public virtual AnWTFilter[] getVFilters(int t, int c)
491 {
492 AnWTFilter[][] an = (AnWTFilter[][]) getSpec(t, c);
493 return an[1];
494 }
495  
496 /// <summary>Debugging method </summary>
497 public override System.String ToString()
498 {
499 System.String str = "";
500 AnWTFilter[][] an;
501  
502 str += ("nTiles=" + nTiles + "\nnComp=" + nComp + "\n\n");
503  
504 for (int t = 0; t < nTiles; t++)
505 {
506 for (int c = 0; c < nComp; c++)
507 {
508 an = (AnWTFilter[][]) getSpec(t, c);
509  
510 str += ("(t:" + t + ",c:" + c + ")\n");
511  
512 // Horizontal filters
513 str += "\tH:";
514 for (int i = 0; i < an[0].Length; i++)
515 {
516 //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
517 str += (" " + an[0][i]);
518 }
519 // Horizontal filters
520 str += "\n\tV:";
521 for (int i = 0; i < an[1].Length; i++)
522 {
523 //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
524 str += (" " + an[1][i]);
525 }
526 str += "\n";
527 }
528 }
529  
530 return str;
531 }
532  
533 /// <summary> Check the reversibility of filters contained is the given
534 /// tile-component.
535 ///
536 /// </summary>
537 /// <param name="t">The index of the tile
538 ///
539 /// </param>
540 /// <param name="c">The index of the component
541 ///
542 /// </param>
543 public virtual bool isReversible(int t, int c)
544 {
545 // Note: no need to buffer the result since this method is
546 // normally called once per tile-component.
547 AnWTFilter[] hfilter = getHFilters(t, c), vfilter = getVFilters(t, c);
548  
549 // As soon as a filter is not reversible, false can be returned
550 for (int i = hfilter.Length - 1; i >= 0; i--)
551 if (!hfilter[i].Reversible || !vfilter[i].Reversible)
552 return false;
553 return true;
554 }
555 }
556 }