public void ZPCodecTest001() { ZPCodec codec = new ZPCodec(); Assert.NotNull(codec.FFZT); Assert.Equal <int>(256, codec.FFZT.Length); }
public void ZPCodecTest006() { ZPCodec codec = new ZPCodec(null, true, false); Assert.False(codec.DjvuCompat); Assert.True(codec.Encoding); }
public void IWEncoderTest001() { string filePath = Path.Combine(Util.ArtifactsDataPath, "testbzz.obz"); byte[] buffer = Util.ReadFileToEnd(filePath); using (MemoryStream stream = new MemoryStream()) using (ZPCodec codec = new ZPCodec(stream, true, true)) { Assert.True(codec.DjvuCompat); Assert.True(codec.Encoding); Assert.Same(stream, codec.DataStream); Assert.Equal(0, stream.Position); for (int b = 0; b < buffer.Length; b++) { byte data = buffer[b]; for (int i = 0; i < 8; i++) { bool value = ((data >> i) & 1) == 1; codec.IWEncoder(value); } } codec.Flush(); Assert.Equal(buffer.Length, stream.Position); } }
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); }
public void ZPCodecTest003() { ZPCodec codec = new ZPCodec(); Assert.True(codec.DjvuCompat); Assert.False(codec.Encoding); }
public void IWEncoderTest002() { using (MemoryStream stream = new MemoryStream()) using (ZPCodec codec = new ZPCodec(stream, true, true)) { Assert.True(codec.DjvuCompat); Assert.True(codec.Encoding); Assert.Same(stream, codec.DataStream); Assert.Equal(0, stream.Position); const ulong hashLong = 0x0184b6730184b673; for (int i = 0; i < 64; i++) { bool value = ((hashLong >> i) & 1) == 1; codec.IWEncoder(value); } codec.Flush(); Assert.Equal(8, stream.Position); ulong codedHash = BitConverter.ToUInt64(stream.GetBuffer(), 0); Assert.Equal(0x7fde92317fde9231u, codedHash); } }
public void CodeSliceTest002() { var map = new InterWaveMap(); var codec = new InterWaveDecoder(map); var coder = new ZPCodec(); Assert.Equal(1, codec.CodeSlice(coder)); }
public void ZPCodecTest005() { using (ZPCodec codec = new ZPCodec(null, true)) { Assert.True(codec.DjvuCompat); Assert.True(codec.Encoding); } }
public void ZPCodecTest002() { byte[] buffer = BzzCompressedTestBuffer; using (MemoryStream stream = new MemoryStream(buffer, false)) using (ZPCodec codec = new ZPCodec(stream)) { Assert.NotNull(codec.DataStream); } }
public void ZPCodecTest008() { using (MemoryStream stream = new MemoryStream()) using (ZPCodec codec = new ZPCodec(stream, false, false)) { Assert.NotNull(codec.DataStream); Assert.Same(stream, codec.DataStream); Assert.False(codec.DjvuCompat); Assert.False(codec.Encoding); } }
public void FFZTest() { using (MemoryStream stream = new MemoryStream()) using (ZPCodec codec = new ZPCodec(stream, true, true)) { Assert.True(codec.DjvuCompat); Assert.True(codec.Encoding); Assert.Same(stream, codec.DataStream); // TODO Finish test implementation sbyte[] ffzt = codec.FFZT; } }
public void ZPCodecTest004() { ZPCodec codec = new ZPCodec(); Assert.True(codec.DjvuCompat); Assert.False(codec.Encoding); ZPTable[] table = codec.CreateDefaultTable(); for (int i = 0; i < table.Length; i++) { var row = table[i]; Assert.Equal <uint>(row.PValue, codec._PArray[i]); Assert.Equal <uint>(row.MValue, codec._MArray[i]); Assert.Equal <uint>(row.Down, codec._Down[i]); Assert.Equal <uint>(row.Up, codec._Up[i]); } }
public void DefaultTable001() { ZPCodec codec = new ZPCodec(); Assert.True(codec.DjvuCompat); Assert.False(codec.Encoding); ZPTable[] table = codec.CreateDefaultTable(); ZPTable[] defaultTable = codec.DefaultTable; Assert.NotSame(table, codec.DefaultTable); Assert.Same(defaultTable, codec.DefaultTable); for (int i = 0; i < table.Length; i++) { var row = table[i]; var defRow = defaultTable[i]; Assert.Equal <uint>(row.PValue, defRow.PValue); Assert.Equal <uint>(row.MValue, defRow.MValue); Assert.Equal <uint>(row.Down, defRow.Down); Assert.Equal <uint>(row.Up, defRow.Up); } }
public void Init(IBinaryReader gbs, JB2Dictionary zdict) { this._ZDict = zdict; _Coder = new ZPCodec(gbs.BaseStream); }
/// <summary> /// Encodes one data chunk into output stream. Settings parameter controls /// how much data is generated.The chunk data is written to Stream /// with no IFF header. Successive calls to EncodeChunk encode /// successive chunks.You must call CloseCodec after encoding the last /// chunk of a file. /// </summary> /// <param name="stream"></param> /// <param name="settings"></param> /// <returns></returns> public int EncodeChunk(Stream stream, InterWaveEncoderSettings settings) { // Check if (settings.Slices == 0 && settings.Bytes == 0 && settings.Decibels == 0) { throw new DjvuArgumentException("Encoder needs stop condition", nameof(settings)); } if (_YMap == null) { throw new DjvuInvalidOperationException($"Cannot encode! Target encoder map is null {nameof(_YMap)}"); } // Open if (_YEncoder == null) { _CSlices = _CSerial = _CBytes = 0; _YEncoder = new InterWaveEncoder(_YMap); if (_CrMap != null && _CbMap != null) { _CbEncoder = new InterWaveEncoder(_CbMap); _CrEncoder = new InterWaveEncoder(_CrMap); } } // Prepare zcodec slices int flag = 1; int nslices = 0; using (MemoryStream coderStream = new MemoryStream()) { float estdb = -1.0f; ZPCodec zp = new ZPCodec(coderStream, true, true); while (flag != 0) { if (settings.Decibels > 0 && estdb >= settings.Decibels) { break; } if (settings.Bytes > 0 && coderStream.Position + _CBytes >= settings.Bytes) { break; } if (settings.Slices > 0 && nslices + _CSlices >= settings.Slices) { break; } flag = _YEncoder.CodeSlice(zp); if (flag != 0 && settings.Decibels > 0) { if (_YEncoder._CurrentBand == 0 || estdb >= settings.Decibels - DecibelPrune) { estdb = _YEncoder.EstimateDecibel(_dBFrac); } } if (_CrEncoder != null && _CbEncoder != null && _CSlices + nslices >= _CrCbDelay) { flag |= _CbEncoder.CodeSlice(zp); flag |= _CrEncoder.CodeSlice(zp); } nslices++; } zp.Flush(); // Write primary header stream.WriteByte((byte)_CSerial); stream.WriteByte((byte)(nslices)); // Write extended header if (_CSerial == 0) { byte major = InterWaveCodec.MajorVersion; if (!(_CrMap != null && _CbMap != null)) { major |= 0x80; } stream.WriteByte(major); stream.WriteByte(InterWaveCodec.MinorVersion); byte xhi = (byte)((_YMap.Width >> 8) & 0xff); byte xlo = (byte)(_YMap.Width & 0xff); byte yhi = (byte)((_YMap.Height >> 8) & 0xff); byte ylo = (byte)(_YMap.Height & 0xff); byte crCbDelay = (byte)(_CrCbHalf ? 0x00 : 0x80); crCbDelay |= (byte)(_CrCbDelay >= 0 ? _CrCbDelay : 0x00); stream.WriteByte(xhi); stream.WriteByte(xlo); stream.WriteByte(yhi); stream.WriteByte(ylo); stream.WriteByte(crCbDelay); } byte[] buffer = coderStream.GetBuffer(); stream.Write(buffer, 0, (int)coderStream.Position); _CBytes += (int)coderStream.Position; } _CSlices += nslices; _CSerial += 1; return(flag); }
public virtual void Init(BinaryReader gbs, JB2Dictionary zdict) { this._zdict = zdict; _zp = new ZPCodec(gbs.BaseStream); }
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; }
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; } } }