001: /*
002: * $RCSfile: GuardBitsSpec.java,v $
003: * $Revision: 1.1 $
004: * $Date: 2005/02/11 05:02:17 $
005: * $State: Exp $
006: *
007: * Class: GuardBitsSpec
008: *
009: * Description: Guard bits specifications
010: *
011: * COPYRIGHT:
012: *
013: * This software module was originally developed by Raphaël Grosbois and
014: * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
015: * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
016: * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
017: * Centre France S.A) in the course of development of the JPEG2000
018: * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
019: * software module is an implementation of a part of the JPEG 2000
020: * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
021: * Systems AB and Canon Research Centre France S.A (collectively JJ2000
022: * Partners) agree not to assert against ISO/IEC and users of the JPEG
023: * 2000 Standard (Users) any of their rights under the copyright, not
024: * including other intellectual property rights, for this software module
025: * with respect to the usage by ISO/IEC and Users of this software module
026: * or modifications thereof for use in hardware or software products
027: * claiming conformance to the JPEG 2000 Standard. Those intending to use
028: * this software module in hardware or software products are advised that
029: * their use may infringe existing patents. The original developers of
030: * this software module, JJ2000 Partners and ISO/IEC assume no liability
031: * for use of this software module or modifications thereof. No license
032: * or right to this software module is granted for non JPEG 2000 Standard
033: * conforming products. JJ2000 Partners have full right to use this
034: * software module for his/her own purpose, assign or donate this
035: * software module to any third party and to inhibit third parties from
036: * using this software module for non JPEG 2000 Standard conforming
037: * products. This copyright notice must be included in all copies or
038: * derivative works of this software module.
039: *
040: * Copyright (c) 1999/2000 JJ2000 Partners.
041: *
042: *
043: *
044: */
045: package jj2000.j2k.quantization;
046:
047: import jj2000.j2k.util.*;
048: import jj2000.j2k.*;
049:
050: import java.util.*;
051:
052: import com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriteParamJava;
053:
054: /**
055: * This class extends ModuleSpec class in order to hold specifications about
056: * number of guard bits in each tile-component.
057: *
058: * @see ModuleSpec
059: * */
060: public class GuardBitsSpec extends ModuleSpec {
061:
062: private String defaultValue = "2";
063:
064: /**
065: * Constructs an empty 'GuardBitsSpec' with specified number of tile and
066: * components. This constructor is called by the decoder.
067: *
068: * @param nt Number of tiles
069: *
070: * @param nc Number of components
071: *
072: * @param type the type of the specification module i.e. tile specific,
073: * component specific or both.
074: * */
075: public GuardBitsSpec(int nt, int nc, byte type) {
076: super (nt, nc, type);
077: }
078:
079: /**
080: * Constructs a new 'GuardBitsSpec' for the specified number of components
081: * and tiles and the arguments of "-Qguard_bits" option.
082: *
083: * @param nt The number of tiles
084: *
085: * @param nc The number of components
086: *
087: * @param type the type of the specification module i.e. tile specific,
088: * component specific or both.
089: * */
090: public GuardBitsSpec(int nt, int nc, byte type,
091: J2KImageWriteParamJava wp, String values) {
092: super (nt, nc, type);
093:
094: if (values == null) {
095: setDefault(new Integer(defaultValue));
096: return;
097: //throw new IllegalArgumentException("Qguard_bits option not "+
098: // "specified");
099: }
100:
101: String param = values;
102: // Parse argument
103: StringTokenizer stk = new StringTokenizer(param);
104: String word; // current word
105: byte curSpecType = SPEC_DEF; // Specification type of the
106: // current parameter
107: boolean[] tileSpec = null; // Tiles concerned by the specification
108: boolean[] compSpec = null; // Components concerned by the specification
109: Integer value; // value of the guard bits
110:
111: while (stk.hasMoreTokens()) {
112: word = stk.nextToken().toLowerCase();
113:
114: switch (word.charAt(0)) {
115: case 't': // Tiles specification
116: tileSpec = parseIdx(word, nTiles);
117: if (curSpecType == SPEC_COMP_DEF)
118: curSpecType = SPEC_TILE_COMP;
119: else
120: curSpecType = SPEC_TILE_DEF;
121: break;
122: case 'c': // Components specification
123: compSpec = parseIdx(word, nComp);
124: if (curSpecType == SPEC_TILE_DEF)
125: curSpecType = SPEC_TILE_COMP;
126: else
127: curSpecType = SPEC_COMP_DEF;
128: break;
129: default: // Step size value
130: try {
131: value = new Integer(word);
132: } catch (NumberFormatException e) {
133: throw new IllegalArgumentException(
134: "Bad parameter for "
135: + "-Qguard_bits option" + " : "
136: + word);
137: }
138:
139: if (value.floatValue() <= 0.0f) {
140: throw new IllegalArgumentException(
141: "Guard bits value " + "must be positive : "
142: + value);
143: }
144:
145: if (curSpecType == SPEC_DEF) {
146: setDefault(value);
147: } else if (curSpecType == SPEC_TILE_DEF) {
148: for (int i = tileSpec.length - 1; i >= 0; i--)
149: if (tileSpec[i]) {
150: setTileDef(i, value);
151: }
152: } else if (curSpecType == SPEC_COMP_DEF) {
153: for (int i = compSpec.length - 1; i >= 0; i--)
154: if (compSpec[i]) {
155: setCompDef(i, value);
156: }
157: } else {
158: for (int i = tileSpec.length - 1; i >= 0; i--) {
159: for (int j = compSpec.length - 1; j >= 0; j--) {
160: if (tileSpec[i] && compSpec[j]) {
161: setTileCompVal(i, j, value);
162: }
163: }
164: }
165: }
166:
167: // Re-initialize
168: curSpecType = SPEC_DEF;
169: tileSpec = null;
170: compSpec = null;
171: break;
172: }
173: }
174:
175: // Check that default value has been specified
176: if (getDefault() == null) {
177: int ndefspec = 0;
178: for (int t = nt - 1; t >= 0; t--) {
179: for (int c = nc - 1; c >= 0; c--) {
180: if (specValType[t][c] == SPEC_DEF) {
181: ndefspec++;
182: }
183: }
184: }
185:
186: // If some tile-component have received no specification, it takes
187: // the default value
188: if (ndefspec != 0) {
189: setDefault(new Integer(defaultValue));
190: } else {
191: // All tile-component have been specified, takes the first
192: // tile-component value as default.
193: setDefault(getTileCompVal(0, 0));
194: switch (specValType[0][0]) {
195: case SPEC_TILE_DEF:
196: for (int c = nc - 1; c >= 0; c--) {
197: if (specValType[0][c] == SPEC_TILE_DEF)
198: specValType[0][c] = SPEC_DEF;
199: }
200: tileDef[0] = null;
201: break;
202: case SPEC_COMP_DEF:
203: for (int t = nt - 1; t >= 0; t--) {
204: if (specValType[t][0] == SPEC_COMP_DEF)
205: specValType[t][0] = SPEC_DEF;
206: }
207: compDef[0] = null;
208: break;
209: case SPEC_TILE_COMP:
210: specValType[0][0] = SPEC_DEF;
211: tileCompVal.put("t0c0", null);
212: break;
213: }
214: }
215: }
216: }
217:
218: }
|