예제 #1
0
        public int CodeSlice(ZPCodec zp)
        {
            if (_curbit < 0)
            {
                return 0;
            }

            if (IsNullSlice(_curbit, _curband) == 0)
            {
                for (int blockno = 0; blockno < _map.Nb; blockno++)
                {
                    int fbucket = Bandbuckets[_curband].Start;
                    int nbucket = Bandbuckets[_curband].Size;
                    DecodeBuckets(zp, _curbit, _curband, _map.Blocks[blockno], fbucket, nbucket);
                }
            }

            if (++_curband >= Bandbuckets.Length)
            {
                _curband = 0;
                _curbit++;

                if (NextQuant() == 0)
                {
                    _curbit = -1;

                    return 0;
                }
            }

            return 1;
        }
예제 #2
0
        public IDataCoder CreateCoder(Stream stream, bool encoding = false, bool compatibility = true)
        {
            ZPCodec codec = new ZPCodec {
                DjvuCompat = compatibility, Encoding = encoding
            };

            return(codec.Initializa(stream));
        }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public BSInputStream Init(Stream input)
        {
            zp = new ZPCodec().Init(input);

            for (int i = 0; i < ctx.Length;)
            {
                ctx[i++] = new MutableValue <sbyte>();
            }

            return(this);
        }
예제 #4
0
        private void DecodeBuckets(ZPCodec zp, int bit, int band, IWBlock blk, int fbucket, int nbucket)
        {
            int thres = _quantHi[band];
            int bbstate = 0;
            sbyte[] cstate = _coeffstate;
            int cidx = 0;

            for (int buckno = 0; buckno < nbucket; )
            {
                int bstatetmp = 0;
                short[] pcoeff = blk.GetBlock(fbucket + buckno);

                if (pcoeff == null)
                {
                    bstatetmp = 8;
                }
                else
                {
                    for (int i = 0; i < 16; i++)
                    {
                        int cstatetmp = cstate[cidx + i] & 1;

                        if (cstatetmp == 0)
                        {
                            if (pcoeff[i] != 0)
                            {
                                cstatetmp |= 2;
                            }
                            else
                            {
                                cstatetmp |= 8;
                            }
                        }

                        cstate[cidx + i] = (sbyte)cstatetmp;
                        bstatetmp |= cstatetmp;
                    }
                }

                _bucketstate[buckno] = (sbyte)bstatetmp;
                bbstate |= bstatetmp;
                buckno++;
                cidx += 16;
            }

            if ((nbucket < 16) || ((bbstate & 2) != 0))
            {
                bbstate |= 4;
            }
            else if ((bbstate & 8) != 0)
            {
                if (zp.Decoder(_ctxRoot) != 0)
                {
                    bbstate |= 4;
                }
            }

            if ((bbstate & 4) != 0)
            {
                for (int buckno = 0; buckno < nbucket; buckno++)
                {
                    if ((_bucketstate[buckno] & 8) != 0)
                    {
                        int ctx = 0;

                        //if (!DjVuOptions.NOCTX_BUCKET_UPPER && (band > 0))
                        if ((band > 0))
                        {
                            int k = (fbucket + buckno) << 2;
                            short[] b = blk.GetBlock(k >> 4);

                            if (b != null)
                            {
                                k &= 0xf;

                                if (b[k] != 0)
                                {
                                    ctx++;
                                }

                                if (b[k + 1] != 0)
                                {
                                    ctx++;
                                }

                                if (b[k + 2] != 0)
                                {
                                    ctx++;
                                }

                                if ((ctx < 3) && (b[k + 3] != 0))
                                {
                                    ctx++;
                                }
                            }
                        }

                        //if (!DjVuOptions.NOCTX_BUCKET_ACTIVE && ((bbstate & 2) != 0))
                        if (((bbstate & 2) != 0))
                        {
                            ctx |= 4;
                        }

                        if (zp.Decoder(_ctxBucket[band][ctx]) != 0)
                        {
                            _bucketstate[buckno] |= 4;
                        }
                    }
                }
            }

            if ((bbstate & 4) != 0)
            {
                cstate = _coeffstate;
                cidx = 0;

                for (int buckno = 0; buckno < nbucket; )
                {
                    if ((_bucketstate[buckno] & 4) != 0)
                    {
                        short[] pcoeff = blk.GetBlock(fbucket + buckno);

                        if (pcoeff == null)
                        {
                            pcoeff = blk.GetInitializedBlock(fbucket + buckno);

                            for (int i = 0; i < 16; i++)
                            {
                                if ((cstate[cidx + i] & 1) == 0)
                                {
                                    cstate[cidx + i] = 8;
                                }
                            }
                        }

                        int gotcha = 0;
                        int maxgotcha = 7;

                        //if (!DjVuOptions.NOCTX_EXPECT)
                        {
                            for (int i = 0; i < 16; i++)
                            {
                                if ((cstate[cidx + i] & 8) != 0)
                                {
                                    gotcha++;
                                }
                            }
                        }

                        for (int i = 0; i < 16; i++)
                        {
                            if ((cstate[cidx + i] & 8) != 0)
                            {
                                if (band == 0)
                                {
                                    thres = _quantLo[i];
                                }

                                int ctx = 0;

                                //if (!DjVuOptions.NOCTX_EXPECT)
                                {
                                    if (gotcha >= maxgotcha)
                                    {
                                        ctx = maxgotcha;
                                    }
                                    else
                                    {
                                        ctx = gotcha;
                                    }
                                }

                                //if (!DjVuOptions.NOCTX_ACTIVE && ((bucketstate[buckno] & 2) != 0))
                                if (((_bucketstate[buckno] & 2) != 0))
                                {
                                    ctx |= 8;
                                }

                                if (zp.Decoder(_ctxStart[ctx]) != 0)
                                {
                                    cstate[cidx + i] |= 4;

                                    int halfthres = thres >> 1;
                                    int coeff = (thres + halfthres) - (halfthres >> 2);

                                    if (zp.IWDecoder() != 0)
                                    {
                                        pcoeff[i] = (short)(-coeff);
                                    }
                                    else
                                    {
                                        pcoeff[i] = (short)coeff;
                                    }
                                }

                                //if (!DjVuOptions.NOCTX_EXPECT)
                                {
                                    if ((cstate[cidx + i] & 4) != 0)
                                    {
                                        gotcha = 0;
                                    }
                                    else if (gotcha > 0)
                                    {
                                        gotcha--;
                                    }
                                }
                            }
                        }
                    }

                    buckno++;
                    cidx += 16;
                }
            }

            if ((bbstate & 2) != 0)
            {
                cstate = _coeffstate;
                cidx = 0;

                for (int buckno = 0; buckno < nbucket; )
                {
                    if ((_bucketstate[buckno] & 2) != 0)
                    {
                        short[] pcoeff = blk.GetBlock(fbucket + buckno);

                        for (int i = 0; i < 16; i++)
                        {
                            if ((cstate[cidx + i] & 2) != 0)
                            {
                                int coeff = pcoeff[i];

                                if (coeff < 0)
                                {
                                    coeff = -coeff;
                                }

                                if (band == 0)
                                {
                                    thres = _quantLo[i];
                                }

                                if (coeff <= (3 * thres))
                                {
                                    coeff += (thres >> 2);

                                    if (zp.Decoder(_ctxMant) != 0)
                                    {
                                        coeff += (thres >> 1);
                                    }
                                    else
                                    {
                                        coeff = (coeff - thres) + (thres >> 1);
                                    }
                                }
                                else
                                {
                                    if (zp.IWDecoder() != 0)
                                    {
                                        coeff += (thres >> 1);
                                    }
                                    else
                                    {
                                        coeff = (coeff - thres) + (thres >> 1);
                                    }
                                }

                                if (pcoeff[i] > 0)
                                {
                                    pcoeff[i] = (short)coeff;
                                }
                                else
                                {
                                    pcoeff[i] = (short)(-coeff);
                                }
                            }
                        }
                    }

                    buckno++;
                    cidx += 16;
                }
            }
        }
예제 #5
0
 public virtual void Init(BinaryReader gbs, JB2Dictionary zdict)
 {
     this._zdict = zdict;
     _zp = new ZPCodec(gbs.BaseStream);
 }
예제 #6
0
        public virtual void Decode(BinaryReader bs)
        {
            if (_ycodec == null)
            {
                _cslice = _cserial = 0;
                _ymap = null;
            }

            byte serial = bs.ReadByte();
            if (serial != _cserial)
            {
                throw new IOException("Chunk does not bear expected serial number");
            }

            int nslices = _cslice + bs.ReadByte();

            if (_cserial == 0)
            {
                int major = bs.ReadByte();
                int minor = bs.ReadByte();

                if ((major & 0x7f) != 1)
                {
                    throw new IOException("File has been compressed with an incompatible Codec");
                }

                if (minor > 2)
                {
                    throw new IOException("File has been compressed with a more recent Codec");
                }

                int header3size = 5;

                if (minor < 2)
                {
                    header3size = 4;
                }

                int w = (bs.ReadByte() << 8);
                w |= bs.ReadByte();

                int h = (bs.ReadByte() << 8);
                h |= bs.ReadByte();
                _crcbDelay = 0;
                _crcbHalf = false;

                int b = bs.ReadByte();

                if (minor >= 2)
                {
                    _crcbDelay = 0x7f & b;
                }

                if (minor >= 2)
                {
                    _crcbHalf = ((0x80 & b) == 0);
                }

                if ((major & 0x80) != 0)
                {
                    _crcbDelay = -1;
                }

                _ymap = new IWMap().Init(w, h);
                _ycodec = new IWCodec().Init(_ymap);

                if (_crcbDelay >= 0)
                {
                    _cbmap = new IWMap().Init(w, h);
                    _crmap = new IWMap().Init(w, h);
                    _cbcodec = new IWCodec().Init(_cbmap);
                    _crcodec = new IWCodec().Init(_crmap);
                }
            }

            ZPCodec zp = new ZPCodec().Init(bs.BaseStream);

            for (int flag = 1; (flag != 0) && (_cslice < nslices); _cslice++)
            {
                flag = _ycodec.CodeSlice(zp);

                if ((_crcodec != null) && (_cbcodec != null) && (_crcbDelay <= _cslice))
                {
                    flag |= _cbcodec.CodeSlice(zp);
                    flag |= _crcodec.CodeSlice(zp);
                }
            }

            _cserial++;

            //    return nslices;
        }