corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * CVS identifier:
3 *
4 * $Id: ModuleSpec.java,v 1.24 2001/10/26 16:30:11 grosbois Exp $
5 *
6 * Class: ModuleSpec
7 *
8 * Description: Generic class for storing module specs
9 *
10 * from WTFilterSpec (Diego Santa Cruz)
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.image;
45 namespace CSJ2K.j2k
46 {
47  
48 /// <summary> This generic class is used to handle values to be used by a module for each
49 /// tile and component. It uses attribute to determine which value to use. It
50 /// should be extended by each module needing this feature.
51 ///
52 /// This class might be used for values that are only tile specific or
53 /// component specific but not both.
54 ///
55 /// <p>The attributes to use are defined by a hierarchy. The hierarchy is:
56 ///
57 /// <ul>
58 /// <li> Tile and component specific attribute</li>
59 /// <li> Tile specific default attribute</li>
60 /// <li> Component main default attribute</li>
61 /// <li> Main default attribute</li>
62 /// </ul></p>
63 ///
64 /// </summary>
65 public class ModuleSpec : System.ICloneable
66 {
67 virtual public ModuleSpec Copy
68 {
69 get
70 {
71 return (ModuleSpec) this.Clone();
72 }
73  
74 }
75  
76 /// <summary>The identifier for a specification module that applies only to
77 /// components
78 /// </summary>
79 public const byte SPEC_TYPE_COMP = 0;
80  
81 /// <summary>The identifier for a specification module that applies only to tiles </summary>
82 public const byte SPEC_TYPE_TILE = 1;
83  
84 /// <summary>The identifier for a specification module that applies both to tiles
85 /// and components
86 /// </summary>
87 public const byte SPEC_TYPE_TILE_COMP = 2;
88  
89 /// <summary>The identifier for default specification </summary>
90 public const byte SPEC_DEF = 0;
91  
92 /// <summary>The identifier for "component default" specification </summary>
93 public const byte SPEC_COMP_DEF = 1;
94  
95 /// <summary>The identifier for "tile default" specification </summary>
96 public const byte SPEC_TILE_DEF = 2;
97  
98 /// <summary>The identifier for a "tile-component" specification </summary>
99 public const byte SPEC_TILE_COMP = 3;
100  
101 /// <summary>The type of the specification module </summary>
102 protected internal int specType;
103  
104 /// <summary>The number of tiles </summary>
105 protected internal int nTiles = 0;
106  
107 /// <summary>The number of components </summary>
108 protected internal int nComp = 0;
109  
110 /// <summary>The spec type for each tile-component. The first index is the tile
111 /// index, the second is the component index.
112 /// </summary>
113 protected internal byte[][] specValType;
114  
115 /// <summary>Default value for each tile-component </summary>
116 protected internal System.Object def = null;
117  
118 /// <summary>The default value for each component. Null if no component
119 /// specific value is defined
120 /// </summary>
121 protected internal System.Object[] compDef = null;
122  
123 /// <summary>The default value for each tile. Null if no tile specific value is
124 /// defined
125 /// </summary>
126 protected internal System.Object[] tileDef = null;
127  
128 /// <summary>The specific value for each tile-component. Value of tile 16 component
129 /// 3 is accessible through the hash value "t16c3". Null if no
130 /// tile-component specific value is defined
131 /// </summary>
132 protected internal System.Collections.Hashtable tileCompVal;
133  
134 public virtual System.Object Clone()
135 {
136 ModuleSpec ms;
137 try
138 {
139 ms = (ModuleSpec) base.MemberwiseClone();
140 }
141 //UPGRADE_NOTE: Exception 'java.lang.CloneNotSupportedException' was converted to 'System.Exception' which has different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1100'"
142 catch (System.Exception)
143 {
144 throw new System.ApplicationException("Error when cloning ModuleSpec instance");
145 }
146 // Create a copy of the specValType array
147 ms.specValType = new byte[nTiles][];
148 for (int i = 0; i < nTiles; i++)
149 {
150 ms.specValType[i] = new byte[nComp];
151 }
152 for (int t = 0; t < nTiles; t++)
153 {
154 for (int c = 0; c < nComp; c++)
155 {
156 ms.specValType[t][c] = specValType[t][c];
157 }
158 }
159 // Create a copy of tileDef
160 if (tileDef != null)
161 {
162 ms.tileDef = new System.Object[nTiles];
163 for (int t = 0; t < nTiles; t++)
164 {
165 ms.tileDef[t] = tileDef[t];
166 }
167 }
168 // Create a copy of tileCompVal
169 if (tileCompVal != null)
170 {
171 ms.tileCompVal = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
172 System.String tmpKey;
173 System.Object tmpVal;
174 //UPGRADE_TODO: Method 'java.util.Enumeration.hasMoreElements' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationhasMoreElements'"
175 for (System.Collections.IEnumerator e = tileCompVal.Keys.GetEnumerator(); e.MoveNext(); )
176 {
177 //UPGRADE_TODO: Method 'java.util.Enumeration.nextElement' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationnextElement'"
178 tmpKey = ((System.String) e.Current);
179 tmpVal = tileCompVal[tmpKey];
180 ms.tileCompVal[tmpKey] = tmpVal;
181 }
182 }
183 return ms;
184 }
185  
186 /// <summary> Rotate the ModuleSpec instance by 90 degrees (this modifies only tile
187 /// and tile-component specifications).
188 ///
189 /// </summary>
190 /// <param name="nT">Number of tiles along horizontal and vertical axis after
191 /// rotation.
192 ///
193 /// </param>
194 public virtual void rotate90(Coord anT)
195 {
196 // Rotate specValType
197 byte[][] tmpsvt = new byte[nTiles][];
198 int ax, ay;
199 Coord bnT = new Coord(anT.y, anT.x);
200 for (int by = 0; by < bnT.y; by++)
201 {
202 for (int bx = 0; bx < bnT.x; bx++)
203 {
204 ay = bx;
205 ax = bnT.y - by - 1;
206 tmpsvt[ay * anT.x + ax] = specValType[by * bnT.x + bx];
207 }
208 }
209 specValType = tmpsvt;
210  
211 // Rotate tileDef
212 if (tileDef != null)
213 {
214 System.Object[] tmptd = new System.Object[nTiles];
215 for (int by = 0; by < bnT.y; by++)
216 {
217 for (int bx = 0; bx < bnT.x; bx++)
218 {
219 ay = bx;
220 ax = bnT.y - by - 1;
221 tmptd[ay * anT.x + ax] = tileDef[by * bnT.x + bx];
222 }
223 }
224 tileDef = tmptd;
225 }
226  
227 // Rotate tileCompVal
228 if (tileCompVal != null && tileCompVal.Count > 0)
229 {
230 System.Collections.Hashtable tmptcv = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
231 System.String tmpKey;
232 System.Object tmpVal;
233 int btIdx, atIdx;
234 int i1, i2;
235 int bx, by;
236 //UPGRADE_TODO: Method 'java.util.Enumeration.hasMoreElements' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationhasMoreElements'"
237 for (System.Collections.IEnumerator e = tileCompVal.Keys.GetEnumerator(); e.MoveNext(); )
238 {
239 //UPGRADE_TODO: Method 'java.util.Enumeration.nextElement' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationnextElement'"
240 tmpKey = ((System.String) e.Current);
241 tmpVal = tileCompVal[tmpKey];
242 i1 = tmpKey.IndexOf('t');
243 i2 = tmpKey.IndexOf('c');
244 btIdx = (System.Int32.Parse(tmpKey.Substring(i1 + 1, (i2) - (i1 + 1))));
245 bx = btIdx % bnT.x;
246 by = btIdx / bnT.x;
247 ay = bx;
248 ax = bnT.y - by - 1;
249 atIdx = ax + ay * anT.x;
250 tmptcv["t" + atIdx + tmpKey.Substring(i2)] = tmpVal;
251 }
252 tileCompVal = tmptcv;
253 }
254 }
255  
256 /// <summary> Constructs a 'ModuleSpec' object, initializing all the components and
257 /// tiles to the 'SPEC_DEF' spec val type, for the specified number of
258 /// components and tiles.
259 ///
260 /// </summary>
261 /// <param name="nt">The number of tiles
262 ///
263 /// </param>
264 /// <param name="nc">The number of components
265 ///
266 /// </param>
267 /// <param name="type">the type of the specification module i.e. tile specific,
268 /// component specific or both.
269 ///
270 /// </param>
271 public ModuleSpec(int nt, int nc, byte type)
272 {
273 nTiles = nt;
274 nComp = nc;
275 specValType = new byte[nt][];
276 for (int i = 0; i < nt; i++)
277 {
278 specValType[i] = new byte[nc];
279 }
280 switch (type)
281 {
282  
283 case SPEC_TYPE_TILE:
284 specType = SPEC_TYPE_TILE;
285 break;
286  
287 case SPEC_TYPE_COMP:
288 specType = SPEC_TYPE_COMP;
289 break;
290  
291 case SPEC_TYPE_TILE_COMP:
292 specType = SPEC_TYPE_TILE_COMP;
293 break;
294 }
295 }
296  
297 /// <summary> Sets default value for this module
298 ///
299 /// </summary>
300 public virtual void setDefault(System.Object value_Renamed)
301 {
302 def = value_Renamed;
303 }
304  
305 /// <summary> Gets default value for this module.
306 ///
307 /// </summary>
308 /// <returns> The default value (Must be casted before use)
309 ///
310 /// </returns>
311 public virtual System.Object getDefault()
312 {
313 return def;
314 }
315  
316 /// <summary> Sets default value for specified component and specValType tag if
317 /// allowed by its priority.
318 ///
319 /// </summary>
320 /// <param name="c">Component index
321 ///
322 /// </param>
323 public virtual void setCompDef(int c, System.Object value_Renamed)
324 {
325 if (specType == SPEC_TYPE_TILE)
326 {
327 //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'"
328 System.String errMsg = "Option whose value is '" + value_Renamed + "' cannot be " + "specified for components as it is a 'tile only' specific " + "option";
329 throw new System.ApplicationException(errMsg);
330 }
331 if (compDef == null)
332 {
333 compDef = new System.Object[nComp];
334 }
335 for (int i = 0; i < nTiles; i++)
336 {
337 if (specValType[i][c] < SPEC_COMP_DEF)
338 {
339 specValType[i][c] = SPEC_COMP_DEF;
340 }
341 }
342 compDef[c] = value_Renamed;
343 }
344  
345 /// <summary> Gets default value of the specified component. If no specification have
346 /// been entered for this component, returns default value.
347 ///
348 /// </summary>
349 /// <param name="c">Component index
350 ///
351 /// </param>
352 /// <returns> The default value for this component (Must be casted before
353 /// use)
354 ///
355 /// </returns>
356 /// <seealso cref="setCompDef">
357 ///
358 /// </seealso>
359 public virtual System.Object getCompDef(int c)
360 {
361 if (specType == SPEC_TYPE_TILE)
362 {
363 throw new System.ApplicationException("Illegal use of ModuleSpec class");
364 }
365 if (compDef == null || compDef[c] == null)
366 {
367 return getDefault();
368 }
369 else
370 {
371 return compDef[c];
372 }
373 }
374  
375 /// <summary> Sets default value for specified tile and specValType tag if allowed by
376 /// its priority.
377 ///
378 /// </summary>
379 /// <param name="c">Tile index.
380 ///
381 /// </param>
382 public virtual void setTileDef(int t, System.Object value_Renamed)
383 {
384 if (specType == SPEC_TYPE_COMP)
385 {
386 //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'"
387 System.String errMsg = "Option whose value is '" + value_Renamed + "' cannot be " + "specified for tiles as it is a 'component only' specific " + "option";
388 throw new System.ApplicationException(errMsg);
389 }
390 if (tileDef == null)
391 {
392 tileDef = new System.Object[nTiles];
393 }
394 for (int i = 0; i < nComp; i++)
395 {
396 if (specValType[t][i] < SPEC_TILE_DEF)
397 {
398 specValType[t][i] = SPEC_TILE_DEF;
399 }
400 }
401 tileDef[t] = value_Renamed;
402 }
403  
404 /// <summary> Gets default value of the specified tile. If no specification has been
405 /// entered, it returns the default value.
406 ///
407 /// </summary>
408 /// <param name="t">Tile index
409 ///
410 /// </param>
411 /// <returns> The default value for this tile (Must be casted before use)
412 ///
413 /// </returns>
414 /// <seealso cref="setTileDef">
415 ///
416 /// </seealso>
417 public virtual System.Object getTileDef(int t)
418 {
419 if (specType == SPEC_TYPE_COMP)
420 {
421 throw new System.ApplicationException("Illegal use of ModuleSpec class");
422 }
423 if (tileDef == null || tileDef[t] == null)
424 {
425 return getDefault();
426 }
427 else
428 {
429 return tileDef[t];
430 }
431 }
432  
433 /// <summary> Sets value for specified tile-component.
434 ///
435 /// </summary>
436 /// <param name="t">Tie index
437 ///
438 /// </param>
439 /// <param name="c">Component index
440 ///
441 /// </param>
442 public virtual void setTileCompVal(int t, int c, System.Object value_Renamed)
443 {
444 if (specType != SPEC_TYPE_TILE_COMP)
445 {
446 //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'"
447 System.String errMsg = "Option whose value is '" + value_Renamed + "' cannot be " + "specified for ";
448 switch (specType)
449 {
450  
451 case SPEC_TYPE_TILE:
452 errMsg += "components as it is a 'tile only' specific option";
453 break;
454  
455 case SPEC_TYPE_COMP:
456 errMsg += "tiles as it is a 'component only' specific option";
457 break;
458 }
459 throw new System.ApplicationException(errMsg);
460 }
461 if (tileCompVal == null)
462 tileCompVal = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
463 specValType[t][c] = SPEC_TILE_COMP;
464 tileCompVal["t" + t + "c" + c] = value_Renamed;
465 }
466  
467 /// <summary> Gets value of specified tile-component. This method calls getSpec but
468 /// has a public access.
469 ///
470 /// </summary>
471 /// <param name="t">Tile index
472 ///
473 /// </param>
474 /// <param name="c">Component index
475 ///
476 /// </param>
477 /// <returns> The value of this tile-component (Must be casted before use)
478 ///
479 /// </returns>
480 /// <seealso cref="setTileCompVal">
481 ///
482 /// </seealso>
483 /// <seealso cref="getSpec">
484 ///
485 /// </seealso>
486 public virtual System.Object getTileCompVal(int t, int c)
487 {
488 if (specType != SPEC_TYPE_TILE_COMP)
489 {
490 throw new System.ApplicationException("Illegal use of ModuleSpec class");
491 }
492 return getSpec(t, c);
493 }
494  
495 /// <summary> Gets value of specified tile-component without knowing if a specific
496 /// tile-component value has been previously entered. It first check if a
497 /// tile-component specific value has been entered, then if a tile specific
498 /// value exist, then if a component specific value exist. If not the
499 /// default value is returned.
500 ///
501 /// </summary>
502 /// <param name="t">Tile index
503 ///
504 /// </param>
505 /// <param name="c">Component index
506 ///
507 /// </param>
508 /// <returns> Value for this tile component.
509 ///
510 /// </returns>
511 protected internal virtual System.Object getSpec(int t, int c)
512 {
513 switch (specValType[t][c])
514 {
515  
516 case SPEC_DEF:
517 return getDefault();
518  
519 case SPEC_COMP_DEF:
520 return getCompDef(c);
521  
522 case SPEC_TILE_DEF:
523 return getTileDef(t);
524  
525 case SPEC_TILE_COMP:
526 return tileCompVal["t" + t + "c" + c];
527  
528 default:
529 throw new System.ArgumentException("Not recognized spec type");
530  
531 }
532 }
533  
534 /// <summary> Return the spec type of the given tile-component.
535 ///
536 /// </summary>
537 /// <param name="t">Tile index
538 ///
539 /// </param>
540 /// <param name="c">Component index
541 ///
542 /// </param>
543 public virtual byte getSpecValType(int t, int c)
544 {
545 return specValType[t][c];
546 }
547  
548 /// <summary> Whether or not specifications have been entered for the given
549 /// component.
550 ///
551 /// </summary>
552 /// <param name="c">Index of the component
553 ///
554 /// </param>
555 /// <returns> True if component specification has been defined
556 ///
557 /// </returns>
558 public virtual bool isCompSpecified(int c)
559 {
560 if (compDef == null || compDef[c] == null)
561 {
562 return false;
563 }
564 else
565 {
566 return true;
567 }
568 }
569  
570 /// <summary> Whether or not specifications have been entered for the given tile.
571 ///
572 /// </summary>
573 /// <param name="t">Index of the tile
574 ///
575 /// </param>
576 /// <returns> True if tile specification has been entered
577 ///
578 /// </returns>
579 public virtual bool isTileSpecified(int t)
580 {
581 if (tileDef == null || tileDef[t] == null)
582 {
583 return false;
584 }
585 else
586 {
587 return true;
588 }
589 }
590  
591 /// <summary> Whether or not a tile-component specification has been defined
592 ///
593 /// </summary>
594 /// <param name="t">Tile index
595 ///
596 /// </param>
597 /// <param name="c">Component index
598 ///
599 /// </param>
600 /// <returns> True if a tile-component specification has been defined.
601 ///
602 /// </returns>
603 public virtual bool isTileCompSpecified(int t, int c)
604 {
605 if (tileCompVal == null || tileCompVal["t" + t + "c" + c] == null)
606 {
607 return false;
608 }
609 else
610 {
611 return true;
612 }
613 }
614  
615 /// <summary> This method is responsible of parsing tile indexes set and component
616 /// indexes set for an option. Such an argument must follow the following
617 /// policy:<br>
618 ///
619 /// <tt>t&lt;indexes set&gt;</tt> or <tt>c&lt;indexes set&gt;</tt> where
620 /// tile or component indexes are separated by commas or a dashes.
621 ///
622 /// <p><u>Example:</u><br>
623 /// <li> <tt>t0,3,4</tt> means tiles with indexes 0, 3 and 4.<br>
624 /// <li> <tt>t2-4</tt> means tiles with indexes 2,3 and 4.<br>
625 ///
626 /// It returns a boolean array skteching which tile or component are
627 /// concerned by the next parameters.
628 ///
629 /// </summary>
630 /// <param name="word">The word to parse.
631 ///
632 /// </param>
633 /// <param name="maxIdx">Maximum authorized index
634 ///
635 /// </param>
636 /// <returns> Indexes concerned by this parameter.
637 ///
638 /// </returns>
639 public static bool[] parseIdx(System.String word, int maxIdx)
640 {
641 int nChar = word.Length; // Number of characters
642 char c = word[0]; // current character
643 int idx = - 1; // Current (tile or component) index
644 int lastIdx = - 1; // Last (tile or component) index
645 bool isDash = false; // Whether or not last separator was a dash
646  
647 bool[] idxSet = new bool[maxIdx];
648 int i = 1; // index of the current character
649  
650 while (i < nChar)
651 {
652 c = word[i];
653 if (System.Char.IsDigit(c))
654 {
655 if (idx == - 1)
656 {
657 idx = 0;
658 }
659 idx = idx * 10 + (c - '0');
660 }
661 else
662 {
663 if (idx == - 1 || (c != ',' && c != '-'))
664 {
665 throw new System.ArgumentException("Bad construction for " + "parameter: " + word);
666 }
667 if (idx < 0 || idx >= maxIdx)
668 {
669 throw new System.ArgumentException("Out of range index " + "in " + "parameter `" + word + "' : " + idx);
670 }
671  
672 // Found a comma
673 if (c == ',')
674 {
675 if (isDash)
676 {
677 // Previously found a dash, fill idxSet
678 for (int j = lastIdx + 1; j < idx; j++)
679 {
680 idxSet[j] = true;
681 }
682 }
683 isDash = false;
684 }
685 else
686 {
687 // Found a dash
688 isDash = true;
689 }
690  
691 // Udate idxSet
692 idxSet[idx] = true;
693 lastIdx = idx;
694 idx = - 1;
695 }
696 i++;
697 }
698  
699 // Process last found index
700 if (idx < 0 || idx >= maxIdx)
701 {
702 throw new System.ArgumentException("Out of range index in " + "parameter `" + word + "' : " + idx);
703 }
704 if (isDash)
705 {
706 for (int j = lastIdx + 1; j < idx; j++)
707 {
708 idxSet[j] = true;
709 }
710 }
711 idxSet[idx] = true;
712  
713 return idxSet;
714 }
715 }
716 }