Ejemplo n.º 1
0
        private static bool AreCompatibleIntegers(
            IReadOnlyList <Constant> arguments,
            IntegerSpec initialSpec = null)
        {
            var spec = initialSpec;

            foreach (var arg in arguments)
            {
                if (arg is IntegerConstant)
                {
                    var constant = (IntegerConstant)arg;
                    if (spec == null)
                    {
                        spec = constant.Spec;
                    }
                    else if (!spec.Equals(constant.Spec))
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
        /// <summary> Initialize all members with the given number of tiles and components.
        ///
        /// </summary>
        /// <param name="nt">Number of tiles
        ///
        /// </param>
        /// <param name="nc">Number of components
        ///
        /// </param>
        public DecoderSpecs(int nt, int nc)
        {
            // Quantization
            qts  = new QuantTypeSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);
            qsss = new QuantStepSizeSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);
            gbs  = new GuardBitsSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);

            // Wavelet transform
            wfs = new SynWTFilterSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);
            dls = new IntegerSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);

            // Component transformation
            cts = new CompTransfSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);

            // Entropy decoder
            ecopts = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);
            ers    = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);
            cblks  = new CBlkSizeSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);

            // Precinct partition
            pss = new PrecinctSizeSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, dls);

            // Codestream
            nls  = new IntegerSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
            pos  = new IntegerSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
            pcs  = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
            sops = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
            ephs = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
            pphs = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
            iccs = new ModuleSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE);
            pphs.setDefault((System.Object)false);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Creates an intrinsic attribute that encodes an integer spec.
 /// </summary>
 /// <param name="specification">An integer specification.</param>
 /// <returns>An intrinsic attribute.</returns>
 public static IntrinsicAttribute Create(IntegerSpec specification)
 {
     return(new IntrinsicAttribute(
                AttributeName,
                new Constant[]
     {
         new IntegerConstant(specification.Size),
         BooleanConstant.Create(specification.IsSigned)
     }));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Generates a random integer constant that conforms to a particular spec.
        /// </summary>
        /// <param name="random">A random number generator.</param>
        /// <param name="spec">The spec for the integer constant to generate.</param>
        /// <returns>An integer constant that conforms to its spec.</returns>
        public static IntegerConstant NextIntegerConstant(this Random random, IntegerSpec spec)
        {
            var bigNum = BigInteger.Zero;

            for (int i = 0; i < spec.Size / 30; i++)
            {
                bigNum = (bigNum << 30) | random.Next(0, 1 << 30);
            }
            bigNum = (bigNum << (spec.Size % 30)) | random.Next(0, 1 << (spec.Size % 30));
            return(new IntegerConstant(bigNum, spec).Normalized);
        }
Ejemplo n.º 5
0
        /// <summary> Initialize all members with the given number of tiles and components
        /// and the command-line arguments stored in a ParameterList instance
        ///
        /// </summary>
        /// <param name="nt">Number of tiles
        ///
        /// </param>
        /// <param name="nc">Number of components
        ///
        /// </param>
        /// <param name="imgsrc">The image source (used to get the image size)
        ///
        /// </param>
        /// <param name="pl">The ParameterList instance
        ///
        /// </param>
        public EncoderSpecs(int nt, int nc, BlkImgDataSrc imgsrc, ParameterList pl)
        {
            nTiles = nt;
            nComp  = nc;

            // ROI
            rois = new MaxShiftSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP);

            // Quantization
            pl.checkList(Quantizer.OPT_PREFIX, CSJ2K.j2k.util.ParameterList.toNameArray(Quantizer.ParameterInfo));
            qts  = new QuantTypeSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, pl);
            qsss = new QuantStepSizeSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, pl);
            gbs  = new GuardBitsSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, pl);

            // Wavelet transform
            wfs = new AnWTFilterSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, qts, pl);
            dls = new IntegerSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, pl, "Wlev");

            // Component transformation
            cts = new ForwCompTransfSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE, wfs, pl);

            // Entropy coder
            System.String[] strLcs = new System.String[] { "near_opt", "lazy_good", "lazy" };
            lcs = new StringSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, "Clen_calc", strLcs, pl);
            System.String[] strTerm = new System.String[] { "near_opt", "easy", "predict", "full" };
            tts = new StringSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, "Cterm_type", strTerm, pl);
            System.String[] strBoolean = new System.String[] { "on", "off" };
            sss   = new StringSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, "Cseg_symbol", strBoolean, pl);
            css   = new StringSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, "Ccausal", strBoolean, pl);
            rts   = new StringSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, "Cterminate", strBoolean, pl);
            mqrs  = new StringSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, "CresetMQ", strBoolean, pl);
            bms   = new StringSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, "Cbypass", strBoolean, pl);
            cblks = new CBlkSizeSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, pl);

            // Precinct partition
            pss = new PrecinctSizeSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE_COMP, imgsrc, dls, pl);

            // Codestream
            sops = new StringSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE, "Psop", strBoolean, pl);
            ephs = new StringSpec(nt, nc, ModuleSpec.SPEC_TYPE_TILE, "Peph", strBoolean, pl);
        }
Ejemplo n.º 6
0
        private static IntegerSpec UnionIntSpecs(
            IntegerSpec firstSpec,
            params IntegerSpec[] otherSpecs)
        {
            var spec = firstSpec;

            foreach (var other in otherSpecs)
            {
                if (other.IsSigned != spec.IsSigned)
                {
                    IntegerSpec signed;
                    IntegerSpec unsigned;
                    if (other.IsSigned)
                    {
                        signed   = other;
                        unsigned = spec;
                    }
                    else
                    {
                        signed   = spec;
                        unsigned = other;
                    }

                    if (signed.Size <= unsigned.Size)
                    {
                        spec = new IntegerSpec(unsigned.Size + 1, true);
                    }
                    else
                    {
                        spec = signed;
                    }
                }
                else if (other.Size > spec.Size)
                {
                    spec = other;
                }
            }
            return(spec);
        }
Ejemplo n.º 7
0
        /// <summary> Initializes this object with the given source of image data and with
        /// all the decompositon parameters
        ///
        /// </summary>
        /// <param name="src">From where the image data should be obtained.
        ///
        /// </param>
        /// <param name="encSpec">The encoder specifications
        ///
        /// </param>
        /// <param name="cb0x">The horizontal coordinate of the code-block partition
        /// origin on the reference grid.
        ///
        /// </param>
        /// <param name="cb0y">The vertical coordinate of the code-block partition origin
        /// on the reference grid.
        ///
        /// </param>
        /// <seealso cref="ForwardWT">
        ///
        /// </seealso>
        public ForwWTFull(BlkImgDataSrc src, EncoderSpecs encSpec, int cb0x, int cb0y) : base(src)
        {
            this.src     = src;
            this.cb0x    = cb0x;
            this.cb0y    = cb0y;
            this.dls     = encSpec.dls;
            this.filters = encSpec.wfs;
            this.cblks   = encSpec.cblks;
            this.pss     = encSpec.pss;

            int ncomp  = src.NumComps;
            int ntiles = src.getNumTiles();

            currentSubband  = new SubbandAn[ncomp];
            decomposedComps = new DataBlk[ncomp];
            subbTrees       = new SubbandAn[ntiles][];
            for (int i = 0; i < ntiles; i++)
            {
                subbTrees[i] = new SubbandAn[ncomp];
            }
            lastn = new int[ncomp];
            lastm = new int[ncomp];
        }
Ejemplo n.º 8
0
        /// <summary> Creates a new ProgressionSpec object for the specified number of tiles,
        /// components and the ParameterList instance.
        ///
        /// </summary>
        /// <param name="nt">The number of tiles
        ///
        /// </param>
        /// <param name="nc">The number of components
        ///
        /// </param>
        /// <param name="nl">The number of layer
        ///
        /// </param>
        /// <param name="dls">The number of decomposition levels specifications
        ///
        /// </param>
        /// <param name="type">the type of the specification module. The ProgressionSpec
        /// class should only be used only with the type ModuleSpec.SPEC_TYPE_TILE.
        ///
        /// </param>
        /// <param name="pl">The ParameterList instance
        ///
        /// </param>
        public ProgressionSpec(int nt, int nc, int nl, IntegerSpec dls, byte type, ParameterList pl) : base(nt, nc, type)
        {
            System.String param = pl.getParameter("Aptype");
            Progression[] prog;
            int           mode = -1;

            if (param == null)
            {
                // No parameter specified
                if (pl.getParameter("Rroi") == null)
                {
                    mode = checkProgMode("res");
                }
                else
                {
                    mode = checkProgMode("layer");
                }

                if (mode == -1)
                {
                    System.String errMsg = "Unknown progression type : '" + param + "'";
                    throw new System.ArgumentException(errMsg);
                }
                prog    = new Progression[1];
                prog[0] = new Progression(mode, 0, nc, 0, dls.Max + 1, nl);
                setDefault(prog);
                return;
            }

            SupportClass.Tokenizer stk = new SupportClass.Tokenizer(param);
            byte curSpecType           = SPEC_DEF;   // Specification type of the

            // current parameter
            bool[]        tileSpec    = null;  // Tiles concerned by the specification
            System.String word        = null;  // current word
            System.String errMsg2     = null;  // Error message
            bool          needInteger = false; // True if an integer value is expected
            int           intType     = 0;     // Type of read integer value (0=index of first

            // resolution level, 1= index of first component, 2=index of first
            // layer not included, 3= index of first resolution level not
            // included, 4= index of  first component not included
            System.Collections.ArrayList progression = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
            int         tmp     = 0;
            Progression curProg = null;

            while (stk.HasMoreTokens())
            {
                word = stk.NextToken();

                switch (word[0])
                {
                case 't':
                    // If progression were previously found, store them
                    if (progression.Count > 0)
                    {
                        // Ensure that all information has been taken
                        curProg.ce  = nc;
                        curProg.lye = nl;
                        curProg.re  = dls.Max + 1;
                        prog        = new Progression[progression.Count];
                        progression.CopyTo(prog);
                        if (curSpecType == SPEC_DEF)
                        {
                            setDefault(prog);
                        }
                        else if (curSpecType == SPEC_TILE_DEF)
                        {
                            for (int i = tileSpec.Length - 1; i >= 0; i--)
                            {
                                if (tileSpec[i])
                                {
                                    setTileDef(i, prog);
                                }
                            }
                        }
                    }
                    progression.Clear();
                    intType     = -1;
                    needInteger = false;

                    // Tiles specification
                    tileSpec    = parseIdx(word, nTiles);
                    curSpecType = SPEC_TILE_DEF;
                    break;

                default:
                    // Here, words is either a Integer (progression bound index)
                    // or a String (progression order type). This is determined by
                    // the value of needInteger.
                    if (needInteger)
                    {
                        // Progression bound info
                        try
                        {
                            tmp = (System.Int32.Parse(word));
                        }
                        catch (System.FormatException)
                        {
                            // Progression has missing parameters
                            throw new System.ArgumentException("Progression " + "order" + " specification " + "has missing " + "parameters: " + param);
                        }

                        switch (intType)
                        {
                        case 0:                                          // cs
                            if (tmp < 0 || tmp > (dls.Max + 1))
                            {
                                throw new System.ArgumentException("Invalid res_start " + "in '-Aptype'" + " option: " + tmp);
                            }
                            curProg.rs = tmp; break;

                        case 1:                                          // rs
                            if (tmp < 0 || tmp > nc)
                            {
                                throw new System.ArgumentException("Invalid comp_start " + "in '-Aptype' " + "option: " + tmp);
                            }
                            curProg.cs = tmp; break;

                        case 2:                                          // lye
                            if (tmp < 0)
                            {
                                throw new System.ArgumentException("Invalid layer_end " + "in '-Aptype'" + " option: " + tmp);
                            }
                            if (tmp > nl)
                            {
                                tmp = nl;
                            }
                            curProg.lye = tmp; break;

                        case 3:                                          // ce
                            if (tmp < 0)
                            {
                                throw new System.ArgumentException("Invalid res_end " + "in '-Aptype'" + " option: " + tmp);
                            }
                            if (tmp > (dls.Max + 1))
                            {
                                tmp = dls.Max + 1;
                            }
                            curProg.re = tmp; break;

                        case 4:                                          // re
                            if (tmp < 0)
                            {
                                throw new System.ArgumentException("Invalid comp_end " + "in '-Aptype'" + " option: " + tmp);
                            }
                            if (tmp > nc)
                            {
                                tmp = nc;
                            }
                            curProg.ce = tmp; break;
                        }

                        if (intType < 4)
                        {
                            intType++;
                            needInteger = true;
                            break;
                        }
                        else if (intType == 4)
                        {
                            intType     = 0;
                            needInteger = false;
                            break;
                        }
                        else
                        {
                            throw new System.ApplicationException("Error in usage of 'Aptype' " + "option: " + param);
                        }
                    }

                    if (!needInteger)
                    {
                        // Progression type info
                        mode = checkProgMode(word);
                        if (mode == -1)
                        {
                            errMsg2 = "Unknown progression type : '" + word + "'";
                            throw new System.ArgumentException(errMsg2);
                        }
                        needInteger = true;
                        intType     = 0;
                        if (progression.Count == 0)
                        {
                            curProg = new Progression(mode, 0, nc, 0, dls.Max + 1, nl);
                        }
                        else
                        {
                            curProg = new Progression(mode, 0, nc, 0, dls.Max + 1, nl);
                        }
                        progression.Add(curProg);
                    }
                    break;
                }         // switch
            }             // while

            if (progression.Count == 0)
            {
                // No progression defined
                if (pl.getParameter("Rroi") == null)
                {
                    mode = checkProgMode("res");
                }
                else
                {
                    mode = checkProgMode("layer");
                }
                if (mode == -1)
                {
                    errMsg2 = "Unknown progression type : '" + param + "'";
                    throw new System.ArgumentException(errMsg2);
                }
                prog    = new Progression[1];
                prog[0] = new Progression(mode, 0, nc, 0, dls.Max + 1, nl);
                setDefault(prog);
                return;
            }

            // Ensure that all information has been taken
            curProg.ce  = nc;
            curProg.lye = nl;
            curProg.re  = dls.Max + 1;

            // Store found progression
            prog = new Progression[progression.Count];
            progression.CopyTo(prog);

            if (curSpecType == SPEC_DEF)
            {
                setDefault(prog);
            }
            else if (curSpecType == SPEC_TILE_DEF)
            {
                for (int i = tileSpec.Length - 1; i >= 0; i--)
                {
                    if (tileSpec[i])
                    {
                        setTileDef(i, prog);
                    }
                }
            }

            // Check that default value has been specified
            if (getDefault() == null)
            {
                int ndefspec = 0;
                for (int t = nt - 1; t >= 0; t--)
                {
                    for (int c = nc - 1; c >= 0; c--)
                    {
                        if (specValType[t][c] == SPEC_DEF)
                        {
                            ndefspec++;
                        }
                    }
                }

                // If some tile-component have received no specification, they
                // receive the default progressiveness.
                if (ndefspec != 0)
                {
                    if (pl.getParameter("Rroi") == null)
                    {
                        mode = checkProgMode("res");
                    }
                    else
                    {
                        mode = checkProgMode("layer");
                    }
                    if (mode == -1)
                    {
                        errMsg2 = "Unknown progression type : '" + param + "'";
                        throw new System.ArgumentException(errMsg2);
                    }
                    prog    = new Progression[1];
                    prog[0] = new Progression(mode, 0, nc, 0, dls.Max + 1, nl);
                    setDefault(prog);
                }
                else
                {
                    // All tile-component have been specified, takes the first
                    // tile-component value as default.
                    setDefault(getTileCompVal(0, 0));
                    switch (specValType[0][0])
                    {
                    case SPEC_TILE_DEF:
                        for (int c = nc - 1; c >= 0; c--)
                        {
                            if (specValType[0][c] == SPEC_TILE_DEF)
                            {
                                specValType[0][c] = SPEC_DEF;
                            }
                        }
                        tileDef[0] = null;
                        break;

                    case SPEC_COMP_DEF:
                        for (int t = nt - 1; t >= 0; t--)
                        {
                            if (specValType[t][0] == SPEC_COMP_DEF)
                            {
                                specValType[t][0] = SPEC_DEF;
                            }
                        }
                        compDef[0] = null;
                        break;

                    case SPEC_TILE_COMP:
                        specValType[0][0]   = SPEC_DEF;
                        tileCompVal["t0c0"] = null;
                        break;
                    }
                }
            }
        }
 /// <summary> Creates a new PrecinctSizeSpec object for the specified number of tiles
 /// and components.
 ///
 /// </summary>
 /// <param name="nt">The number of tiles
 ///
 /// </param>
 /// <param name="nc">The number of components
 ///
 /// </param>
 /// <param name="type">the type of the specification module i.e. tile specific,
 /// component specific or both.
 ///
 /// </param>
 /// <param name="dls">Reference to the number of decomposition levels
 /// specification
 ///
 /// </param>
 public PrecinctSizeSpec(int nt, int nc, byte type, IntegerSpec dls) : base(nt, nc, type)
 {
     this.dls = dls;
 }
        /// <summary> Creates a new PrecinctSizeSpec object for the specified number of tiles
        /// and components and the ParameterList instance.
        ///
        /// </summary>
        /// <param name="nt">The number of tiles
        ///
        /// </param>
        /// <param name="nc">The number of components
        ///
        /// </param>
        /// <param name="type">the type of the specification module i.e. tile specific,
        /// component specific or both.
        ///
        /// </param>
        /// <param name="imgsrc">The image source (used to get the image size)
        ///
        /// </param>
        /// <param name="pl">The ParameterList instance
        ///
        /// </param>
        public PrecinctSizeSpec(int nt, int nc, byte type, BlkImgDataSrc imgsrc, IntegerSpec dls, ParameterList pl) : base(nt, nc, type)
        {
            this.dls = dls;

            // The precinct sizes are stored in a 2 elements vector array, the
            // first element containing a vector for the precincts width for each
            // resolution level and the second element containing a vector for the
            // precincts height for each resolution level. The precincts sizes are
            // specified from the highest resolution level to the lowest one
            // (i.e. 0).  If there are less elements than the number of
            // decomposition levels, the last element is used for all remaining
            // resolution levels (i.e. if the precincts sizes are specified only
            // for resolutions levels 5, 4 and 3, then the precincts size for
            // resolution levels 2, 1 and 0 will be the same as the size used for
            // resolution level 3).

            // Boolean used to know if we were previously reading a precinct's
            // size or if we were reading something else.
            bool wasReadingPrecinctSize = false;

            System.String param = pl.getParameter(optName);

            // Set precinct sizes to default i.e. 2^15 =
            // Markers.PRECINCT_PARTITION_DEF_SIZE
            System.Collections.ArrayList[] tmpv = new System.Collections.ArrayList[2];
            tmpv[0] = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));             // ppx
            tmpv[0].Add((System.Int32)CSJ2K.j2k.codestream.Markers.PRECINCT_PARTITION_DEF_SIZE);
            tmpv[1] = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));             // ppy
            tmpv[1].Add((System.Int32)CSJ2K.j2k.codestream.Markers.PRECINCT_PARTITION_DEF_SIZE);
            setDefault(tmpv);

            if (param == null)
            {
                // No precinct size specified in the command line so we do not try
                // to parse it.
                return;
            }

            // Precinct partition is used : parse arguments
            SupportClass.Tokenizer stk = new SupportClass.Tokenizer(param);
            byte curSpecType           = SPEC_DEF;   // Specification type of the

            // current parameter
            bool[] tileSpec = null; // Tiles concerned by the specification
            bool[] compSpec = null; // Components concerned by the specification
            int    ci, ti;          //i, xIdx removed

            bool endOfParamList = false;

            System.String word = null;             // current word
            System.Int32  w, h;
            System.String errMsg = null;

            while ((stk.HasMoreTokens() || wasReadingPrecinctSize) && !endOfParamList)
            {
                System.Collections.ArrayList[] v = new System.Collections.ArrayList[2];                 // v[0] : ppx, v[1] : ppy

                // We do not read the next token if we were reading a precinct's
                // size argument as we have already read the next token into word.
                if (!wasReadingPrecinctSize)
                {
                    word = stk.NextToken();
                }

                wasReadingPrecinctSize = false;

                switch (word[0])
                {
                case 't':                          // Tiles specification
                    tileSpec = parseIdx(word, nTiles);
                    if (curSpecType == SPEC_COMP_DEF)
                    {
                        curSpecType = SPEC_TILE_COMP;
                    }
                    else
                    {
                        curSpecType = SPEC_TILE_DEF;
                    }
                    break;


                case 'c':                          // Components specification
                    compSpec = parseIdx(word, nComp);
                    if (curSpecType == SPEC_TILE_DEF)
                    {
                        curSpecType = SPEC_TILE_COMP;
                    }
                    else
                    {
                        curSpecType = SPEC_COMP_DEF;
                    }
                    break;


                default:
                    if (!System.Char.IsDigit(word[0]))
                    {
                        errMsg = "Bad construction for parameter: " + word;
                        throw new System.ArgumentException(errMsg);
                    }

                    // Initialises Vector objects
                    v[0] = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));                             // ppx
                    v[1] = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));                             // ppy

                    while (true)
                    {
                        // Now get the precinct dimensions
                        try
                        {
                            // Get precinct width
                            w = System.Int32.Parse(word);

                            // Get next word in argument list
                            try
                            {
                                word = stk.NextToken();
                            }
                            catch (System.ArgumentOutOfRangeException)
                            {
                                errMsg = "'" + optName + "' option : could not " + "parse the precinct's width";
                                throw new System.ArgumentException(errMsg);
                            }
                            // Get precinct height
                            h = System.Int32.Parse(word);
                            if (w != (1 << MathUtil.log2(w)) || h != (1 << MathUtil.log2(h)))
                            {
                                errMsg = "Precinct dimensions must be powers of 2";
                                throw new System.ArgumentException(errMsg);
                            }
                        }
                        catch (System.FormatException)
                        {
                            errMsg = "'" + optName + "' option : the argument '" + word + "' could not be parsed.";
                            throw new System.ArgumentException(errMsg);
                        }
                        // Store packet's dimensions in Vector arrays
                        v[0].Add(w);
                        v[1].Add(h);

                        // Try to get the next token
                        if (stk.HasMoreTokens())
                        {
                            word = stk.NextToken();
                            if (!System.Char.IsDigit(word[0]))
                            {
                                // The next token does not start with a digit so
                                // it is not a precinct's size argument. We set
                                // the wasReadingPrecinctSize booleen such that we
                                // know that we don't have to read another token
                                // and check for the end of the parameters list.
                                wasReadingPrecinctSize = true;

                                if (curSpecType == SPEC_DEF)
                                {
                                    setDefault(v);
                                }
                                else if (curSpecType == SPEC_TILE_DEF)
                                {
                                    for (ti = tileSpec.Length - 1; ti >= 0; ti--)
                                    {
                                        if (tileSpec[ti])
                                        {
                                            setTileDef(ti, v);
                                        }
                                    }
                                }
                                else if (curSpecType == SPEC_COMP_DEF)
                                {
                                    for (ci = compSpec.Length - 1; ci >= 0; ci--)
                                    {
                                        if (compSpec[ci])
                                        {
                                            setCompDef(ci, v);
                                        }
                                    }
                                }
                                else
                                {
                                    for (ti = tileSpec.Length - 1; ti >= 0; ti--)
                                    {
                                        for (ci = compSpec.Length - 1; ci >= 0; ci--)
                                        {
                                            if (tileSpec[ti] && compSpec[ci])
                                            {
                                                setTileCompVal(ti, ci, v);
                                            }
                                        }
                                    }
                                }
                                // Re-initialize
                                curSpecType = SPEC_DEF;
                                tileSpec    = null;
                                compSpec    = null;

                                // Go back to 'normal' parsing
                                break;
                            }
                            else
                            {
                                // Next token starts with a digit so read it
                            }
                        }
                        else
                        {
                            // We have reached the end of the parameters list so
                            // we store the last precinct's sizes and we stop
                            if (curSpecType == SPEC_DEF)
                            {
                                setDefault(v);
                            }
                            else if (curSpecType == SPEC_TILE_DEF)
                            {
                                for (ti = tileSpec.Length - 1; ti >= 0; ti--)
                                {
                                    if (tileSpec[ti])
                                    {
                                        setTileDef(ti, v);
                                    }
                                }
                            }
                            else if (curSpecType == SPEC_COMP_DEF)
                            {
                                for (ci = compSpec.Length - 1; ci >= 0; ci--)
                                {
                                    if (compSpec[ci])
                                    {
                                        setCompDef(ci, v);
                                    }
                                }
                            }
                            else
                            {
                                for (ti = tileSpec.Length - 1; ti >= 0; ti--)
                                {
                                    for (ci = compSpec.Length - 1; ci >= 0; ci--)
                                    {
                                        if (tileSpec[ti] && compSpec[ci])
                                        {
                                            setTileCompVal(ti, ci, v);
                                        }
                                    }
                                }
                            }
                            endOfParamList = true;
                            break;
                        }
                    }     // while (true)
                    break;
                }         // switch
            }             // while
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Decodes an LNode as a constant value.
        /// </summary>
        /// <param name="node">The node to decode.</param>
        /// <param name="state">The decoder state to use.</param>
        /// <returns>A decoded constant.</returns>
        public override Constant Decode(LNode node, DecoderState state)
        {
            // Default-value constants.
            if (node.IsIdNamed(CodeSymbols.Default))
            {
                return(DefaultConstant.Instance);
            }

            // Type/field/method token constants.
            if (node.Calls(CodeSymbols.Typeof, 1))
            {
                return(new TypeTokenConstant(state.DecodeType(node.Args[0])));
            }
            else if (node.Calls(fieldofSymbol, 1))
            {
                return(new FieldTokenConstant(state.DecodeField(node.Args[0])));
            }
            else if (node.Calls(methodofSymbol, 1))
            {
                return(new MethodTokenConstant(state.DecodeMethod(node.Args[0])));
            }

            if (!FeedbackHelpers.AssertIsLiteral(node, state.Log))
            {
                return(null);
            }

            object value;
            Symbol typeMarker;

            // Custom literals.
            if (TryDecomposeCustomLiteral(node, out value, out typeMarker))
            {
                // Arbitrary-width integer literals.
                IntegerSpec spec;
                if (IntegerSpec.TryParse(typeMarker.Name, out spec))
                {
                    BigInteger integerVal;
                    if (BigInteger.TryParse(value.ToString(), out integerVal))
                    {
                        return(new IntegerConstant(integerVal, spec));
                    }
                    else
                    {
                        FeedbackHelpers.LogSyntaxError(
                            state.Log,
                            node,
                            FeedbackHelpers.QuoteEven(
                                "cannot parse ",
                                value.ToString(),
                                " as an integer."));
                        return(null);
                    }
                }
                else
                {
                    FeedbackHelpers.LogSyntaxError(
                        state.Log,
                        node,
                        FeedbackHelpers.QuoteEven(
                            "unknown custom literal type ",
                            typeMarker.Name,
                            "."));
                    return(null);
                }
            }

            value = node.Value;

            // Miscellaneous constants: null, strings, Booleans.
            if (value == null)
            {
                return(NullConstant.Instance);
            }
            else if (value is string)
            {
                return(new StringConstant((string)value));
            }
            else if (value is bool)
            {
                return(BooleanConstant.Create((bool)value));
            }
            // Floating-point numbers.
            else if (value is float)
            {
                return(new Float32Constant((float)value));
            }
            else if (value is double)
            {
                return(new Float64Constant((double)value));
            }
            // Fixed-width integer constants and characters.
            else if (value is char)
            {
                return(new IntegerConstant((char)value));
            }
            else if (value is sbyte)
            {
                return(new IntegerConstant((sbyte)value));
            }
            else if (value is short)
            {
                return(new IntegerConstant((short)value));
            }
            else if (value is int)
            {
                return(new IntegerConstant((int)value));
            }
            else if (value is long)
            {
                return(new IntegerConstant((long)value));
            }
            else if (value is byte)
            {
                return(new IntegerConstant((byte)value));
            }
            else if (value is ushort)
            {
                return(new IntegerConstant((ushort)value));
            }
            else if (value is uint)
            {
                return(new IntegerConstant((uint)value));
            }
            else if (value is ulong)
            {
                return(new IntegerConstant((ulong)value));
            }

            FeedbackHelpers.LogSyntaxError(
                state.Log,
                node,
                new Text("unknown literal type."));
            return(null);
        }