-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleSpec.cs
More file actions
572 lines (529 loc) · 17.4 KB
/
ModuleSpec.cs
File metadata and controls
572 lines (529 loc) · 17.4 KB
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
* CVS identifier:
*
* $Id: ModuleSpec.java,v 1.24 2001/10/26 16:30:11 grosbois Exp $
*
* Class: ModuleSpec
*
* Description: Generic class for storing module specs
*
* from WTFilterSpec (Diego Santa Cruz)
*
* COPYRIGHT:
*
* This software module was originally developed by Rapha�l Grosbois and
* Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
* Askel�f (Ericsson Radio Systems AB); and Bertrand Berthelot, David
* Bouchard, F�lix Henry, Gerard Mozelle and Patrice Onno (Canon Research
* Centre France S.A) in the course of development of the JPEG2000
* standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
* software module is an implementation of a part of the JPEG 2000
* Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
* Systems AB and Canon Research Centre France S.A (collectively JJ2000
* Partners) agree not to assert against ISO/IEC and users of the JPEG
* 2000 Standard (Users) any of their rights under the copyright, not
* including other intellectual property rights, for this software module
* with respect to the usage by ISO/IEC and Users of this software module
* or modifications thereof for use in hardware or software products
* claiming conformance to the JPEG 2000 Standard. Those intending to use
* this software module in hardware or software products are advised that
* their use may infringe existing patents. The original developers of
* this software module, JJ2000 Partners and ISO/IEC assume no liability
* for use of this software module or modifications thereof. No license
* or right to this software module is granted for non JPEG 2000 Standard
* conforming products. JJ2000 Partners have full right to use this
* software module for his/her own purpose, assign or donate this
* software module to any third party and to inhibit third parties from
* using this software module for non JPEG 2000 Standard conforming
* products. This copyright notice must be included in all copies or
* derivative works of this software module.
*
* Copyright (c) 1999/2000 JJ2000 Partners.
* */
using System;
using System.Collections.Generic;
using jpeg2000_decoder.Image;
/**
* This generic class is used to handle values to be used by a module for each
* tile and component. It uses attribute to determine which value to use. It
* should be extended by each module needing this feature.
*
* This class might be used for values that are only tile specific or
* component specific but not both.
*
* <p>The attributes to use are defined by a hierarchy. The hierarchy is:
*
* <ul>
* <li> Tile and component specific attribute</li>
* <li> Tile specific default attribute</li>
* <li> Component main default attribute</li>
* <li> Main default attribute</li>
* </ul></p>
* */
public class ModuleSpec {
/** The identifier for a specification module that applies only to
* components */
public const byte SPEC_TYPE_COMP = 0;
/** The identifier for a specification module that applies only to tiles */
public const byte SPEC_TYPE_TILE = 1;
/** The identifier for a specification module that applies both to tiles
* and components */
public const byte SPEC_TYPE_TILE_COMP = 2;
/** The identifier for default specification */
public const byte SPEC_DEF = 0;
/** The identifier for "component default" specification */
public const byte SPEC_COMP_DEF = 1;
/** The identifier for "tile default" specification */
public const byte SPEC_TILE_DEF = 2;
/** The identifier for a "tile-component" specification */
public const byte SPEC_TILE_COMP = 3;
/** The type of the specification module */
protected int specType;
/** The number of tiles */
protected int nTiles = 0;
/** The number of components */
protected int nComp = 0;
/** The spec type for each tile-component. The first index is the tile
* index, the second is the component index. */
protected byte[][] specValType;
/** Default value for each tile-component */
protected Object def = null;
/** The default value for each component. Null if no component
specific value is defined */
protected Object[] compDef = null;
/** The default value for each tile. Null if no tile specific value is
defined */
protected Object[] tileDef = null;
/** The specific value for each tile-component. Value of tile 16 component
* 3 is accessible through the hash value "t16c3". Null if no
* tile-component specific value is defined */
protected NullableDictionary<string,object> tileCompVal;
// public ModuleSpec getCopy() {
// return (ModuleSpec)this.clone();
// }
// protected Object clone() {
// ModuleSpec ms;
// try {
// ms = (ModuleSpec)super.clone();
// } catch(CloneNotSupportedException e) {
// throw new Error("Error when cloning ModuleSpec instance");
// }
// // Create a copy of the specValType array
// ms.specValType = new byte[nTiles][nComp];
// for(int t=0; t<nTiles; t++) {
// for(int c=0; c<nComp; c++) {
// ms.specValType[t][c] = specValType[t][c];
// }
// }
// // Create a copy of tileDef
// if(tileDef!=null) {
// ms.tileDef = new Object[nTiles];
// for(int t=0; t<nTiles; t++) {
// ms.tileDef[t] = tileDef[t];
// }
// }
// // Create a copy of tileCompVal
// if(tileCompVal!=null) {
// ms.tileCompVal = new Hashtable();
// String tmpKey;
// Object tmpVal;
// for(Enumeration e=tileCompVal.keys(); e.hasMoreElements(); ) {
// tmpKey = (String)e.nextElement();
// tmpVal = tileCompVal.get(tmpKey);
// ms.tileCompVal.put(tmpKey,tmpVal);
// }
// }
// return ms;
// }
/**
* Rotate the ModuleSpec instance by 90 degrees (this modifies only tile
* and tile-component specifications).
*
* @param nT Number of tiles along horizontal and vertical axis after
* rotation.
* */
public void rotate90(Coord anT) {
// Rotate specValType
byte[][] tmpsvt = new byte[nTiles][];
int ax,ay;
Coord bnT = new Coord(anT.Y,anT.X);
for(int by=0; by<bnT.Y; by++) {
for(int bx=0; bx<bnT.X; bx++) {
ay = bx;
ax = bnT.Y -by-1;
tmpsvt[ay*anT.X+ax] = specValType[by*bnT.X+bx];
}
}
specValType = tmpsvt;
// Rotate tileDef
if(tileDef!=null) {
Object[] tmptd = new Object[nTiles];
for(int by=0; by<bnT.Y; by++) {
for(int bx=0; bx<bnT.X; bx++) {
ay = bx;
ax = bnT.Y-by-1;
tmptd[ay*anT.X+ax] = tileDef[by*bnT.X+bx];
}
}
tileDef = tmptd;
}
// Rotate tileCompVal
if(tileCompVal!=null && tileCompVal.Count>0) {
NullableDictionary<string,object> tmptcv = new NullableDictionary<string, object>();
String tmpKey;
Object tmpVal;
int btIdx,atIdx;
int i1,i2;
int bx,by;
foreach(var key in tileCompVal.Keys) {
tmpVal = tileCompVal[key];
i1 = key.IndexOf('t');
i2 = key.IndexOf('c');
btIdx = int.Parse(key.Substring(i1+1,i2));
bx = btIdx%bnT.X;
by = btIdx/bnT.X;
ay = bx;
ax = bnT.Y-by-1;
atIdx = ax+ay*anT.X;
tmptcv["t"+atIdx+key.Substring(i2)] = tmpVal;
}
tileCompVal = tmptcv;
}
}
/**
* Constructs a 'ModuleSpec' object, initializing all the components and
* tiles to the 'SPEC_DEF' spec val type, for the specified number of
* components and tiles.
*
* @param nt The number of tiles
*
* @param nc The number of components
*
* @param type the type of the specification module i.e. tile specific,
* component specific or both.
* */
public ModuleSpec(int nt,int nc,byte type) {
nTiles = nt;
nComp = nc;
specValType = new byte[nt][];
for(int i = 0; i < specValType.Length; i++) specValType[i] = new byte[nc];
switch (type) {
case SPEC_TYPE_TILE:
specType = SPEC_TYPE_TILE;
break;
case SPEC_TYPE_COMP:
specType = SPEC_TYPE_COMP;
break;
case SPEC_TYPE_TILE_COMP:
specType = SPEC_TYPE_TILE_COMP;
break;
}
}
/**
* Sets default value for this module
* */
public void setDefault(Object value) {
def = value;
}
/**
* Gets default value for this module.
*
* @return The default value (Must be casted before use)
* */
public Object getDefault() {
return def;
}
/**
* Sets default value for specified component and specValType tag if
* allowed by its priority.
*
* @param c Component index
* */
public void setCompDef(int c, Object value) {
if ( specType == SPEC_TYPE_TILE ) {
String errMsg = "Option whose value is '"+value+"' cannot be "
+"specified for components as it is a 'tile only' specific "
+"option";
throw new Exception(errMsg);
}
if(compDef==null) {
compDef = new Object[nComp];
}
for(int i=0; i<nTiles; i++){
if(specValType[i][c]<SPEC_COMP_DEF) {
specValType[i][c] = SPEC_COMP_DEF;
}
}
compDef[c] = value;
}
/**
* Gets default value of the specified component. If no specification have
* been entered for this component, returns default value.
*
* @param c Component index
*
* @return The default value for this component (Must be casted before
* use)
*
* @see #setCompDef
* */
public Object getCompDef(int c) {
if ( specType == SPEC_TYPE_TILE ) {
throw new Exception("Illegal use of ModuleSpec class");
}
if(compDef==null || compDef[c]==null) {
return getDefault();
} else {
return compDef[c];
}
}
/**
* Sets default value for specified tile and specValType tag if allowed by
* its priority.
*
* @param c Tile index.
* */
public void setTileDef(int t, Object value) {
if ( specType == SPEC_TYPE_COMP ) {
String errMsg = "Option whose value is '"+value+"' cannot be "
+ "specified for tiles as it is a 'component only' specific "
+ "option";
throw new Exception(errMsg);
}
if(tileDef==null) {
tileDef = new Object[nTiles];
}
for(int i=0; i<nComp; i++) {
if(specValType[t][i]<SPEC_TILE_DEF) {
specValType[t][i] = SPEC_TILE_DEF;
}
}
tileDef[t] = value;
}
/**
* Gets default value of the specified tile. If no specification has been
* entered, it returns the default value.
*
* @param t Tile index
*
* @return The default value for this tile (Must be casted before use)
*
* @see #setTileDef
* */
public Object getTileDef(int t) {
if ( specType == SPEC_TYPE_COMP ) {
throw new Exception("Illegal use of ModuleSpec class");
}
if(tileDef==null || tileDef[t]==null) {
return getDefault();
} else {
return tileDef[t];
}
}
/**
* Sets value for specified tile-component.
*
* @param t Tie index
*
* @param c Component index
* */
public void setTileCompVal(int t,int c, Object value) {
if ( specType != SPEC_TYPE_TILE_COMP ) {
String errMsg = "Option whose value is '"+value+"' cannot be "
+ "specified for ";
switch (specType) {
case SPEC_TYPE_TILE:
errMsg += "components as it is a 'tile only' specific option";
break;
case SPEC_TYPE_COMP:
errMsg += "tiles as it is a 'component only' specific option";
break;
}
throw new Exception(errMsg);
}
if(tileCompVal==null)
tileCompVal = new NullableDictionary<string, object>();
specValType[t][c] = SPEC_TILE_COMP;
tileCompVal.Add("t"+t+"c"+c,value);
}
/**
* Gets value of specified tile-component. This method calls getSpec but
* has a public access.
*
* @param t Tile index
*
* @param c Component index
*
* @return The value of this tile-component (Must be casted before use)
*
* @see #setTileCompVal
*
* @see #getSpec
* */
public Object getTileCompVal(int t,int c) {
if ( specType != SPEC_TYPE_TILE_COMP ) {
throw new Exception("Illegal use of ModuleSpec class");
}
return getSpec(t,c);
}
/**
* Gets value of specified tile-component without knowing if a specific
* tile-component value has been previously entered. It first check if a
* tile-component specific value has been entered, then if a tile specific
* value exist, then if a component specific value exist. If not the
* default value is returned.
*
* @param t Tile index
*
* @param c Component index
*
* @return Value for this tile component.
* */
protected Object getSpec(int t,int c) {
switch(specValType[t][c]) {
case SPEC_DEF:
return getDefault();
case SPEC_COMP_DEF:
return getCompDef(c);
case SPEC_TILE_DEF:
return getTileDef(t);
case SPEC_TILE_COMP:
return tileCompVal["t"+t+"c"+c];
default:
throw new ArgumentException("Not recognized spec type");
}
}
/**
* Return the spec type of the given tile-component.
*
* @param t Tile index
*
* @param c Component index
* */
public byte getSpecValType(int t,int c) {
return specValType[t][c];
}
/**
* Whether or not specifications have been entered for the given
* component.
*
* @param c Index of the component
*
* @return True if component specification has been defined
* */
public bool isCompSpecified(int c){
if(compDef==null || compDef[c]==null) {
return false;
} else {
return true;
}
}
/**
* Whether or not specifications have been entered for the given tile.
*
* @param t Index of the tile
*
* @return True if tile specification has been entered
* */
public bool isTileSpecified(int t) {
if(tileDef==null || tileDef[t]==null) {
return false;
} else {
return true;
}
}
/**
* Whether or not a tile-component specification has been defined
*
* @param t Tile index
*
* @param c Component index
*
* @return True if a tile-component specification has been defined.
* */
public bool isTileCompSpecified(int t,int c) {
if(tileCompVal==null || tileCompVal["t"+t+"c"+c]==null) {
return false;
} else {
return true;
}
}
/**
* This method is responsible of parsing tile indexes set and component
* indexes set for an option. Such an argument must follow the following
* policy:<br>
*
* <tt>t<indexes set></tt> or <tt>c<indexes set></tt> where
* tile or component indexes are separated by commas or a dashes.
*
* <p><u>Example:</u><br>
* <li> <tt>t0,3,4</tt> means tiles with indexes 0, 3 and 4.<br>
* <li> <tt>t2-4</tt> means tiles with indexes 2,3 and 4.<br>
*
* It returns a boolean array skteching which tile or component are
* concerned by the next parameters.
*
* @param word The word to parse.
*
* @param maxIdx Maximum authorized index
*
* @return Indexes concerned by this parameter.
* */
public static bool[] parseIdx(string word, int maxIdx) {
int nChar = word.Length; // Number of characters
char c = word[0]; // current character
int idx = -1; // Current (tile or component) index
int lastIdx = -1; // Last (tile or component) index
bool isDash = false; // Whether or not last separator was a dash
bool[] idxSet = new bool[maxIdx];
int i=1; // index of the current character
while(i<nChar) {
c = word[i];
if(char.IsDigit(c)) {
if(idx==-1) {
idx = 0;
}
idx = idx*10+ (c-'0');
} else {
if(idx==-1 || (c!=',' && c!='-')) {
throw new ArgumentException("Bad construction for "+
"parameter: "+word);
}
if(idx<0 || idx>=maxIdx) {
throw new ArgumentException("Out of range index "+
"in "+
"parameter `"+word+
"' : "+idx);
}
// Found a comma
if(c==',') {
if(isDash) { // Previously found a dash, fill idxSet
for(int j=lastIdx+1; j<idx; j++){
idxSet[j] = true;
}
}
isDash = false;
} else {// Found a dash
isDash = true;
}
// Udate idxSet
idxSet[idx] = true;
lastIdx = idx;
idx=-1;
}
i++;
}
// Process last found index
if(idx<0 || idx>=maxIdx) {
throw new ArgumentException("Out of range index in "+
"parameter `"+word+"' : "+idx);
}
if(isDash) {
for(int j=lastIdx+1; j<idx; j++) {
idxSet[j] = true;
}
}
idxSet[idx] = true;
return idxSet;
}
}