//============================================================ // <T>序列化位图数据。</T> // // @params output 输出流 // @params colorCount 调色板颜色数量 // @params pixelCount 分块的像素个数 //============================================================ public bool Compress(IOutput output, int compressCd, int colorCount, int pixelCount) { // 保存数据 using (FByteStream stream = new FByteStream()) { Serialize(stream, colorCount, pixelCount); // 压缩数据 byte[] data = null; if (compressCd == ERsCompress.NoneValue) { data = stream.ToArray(); } else if (compressCd == ERsCompress.DeflateValue) { //using(FRsCompressFile compress = new FRsCompressFile(ERsCompress.Deflate, stream, RResourceManager.CompressBlockSplit)) { using (FCompressFile compress = new FCompressFile(stream)) { data = compress.Compress(); } if (data.Length > stream.Length) { compressCd = ERsCompress.NoneValue; data = stream.ToArray(); } } else if (compressCd == ERsCompress.LzmaValue) { using (FLzmaFile compress = new FLzmaFile(stream)) { data = compress.CompressNative(); } if (data.Length > stream.Length) { compressCd = ERsCompress.NoneValue; data = stream.ToArray(); } } output.WriteInt32(compressCd); output.WriteInt32(data.Length); output.WriteBytes(data, 0, data.Length); } return(true); }
//============================================================ // <T>压缩字节数据。</T> // // @return 字节数据 //============================================================ public byte[] CompressBytes() { byte[] data = null; // Deflate压缩 string compressCd = _compressCd; switch (compressCd) { case ERsCompress.Deflate: using (FCompressFile file = new FCompressFile()) { file.Append(_memory, 0, _length); if (_blockSize > 0) { data = file.BlockCompress(_blockSize); } else { data = file.Compress(); } } break; case ERsCompress.Lzma: using (FLzmaFile file = new FLzmaFile()) { file.Append(_memory, 0, _length); if (_blockSize > 0) { data = file.BlockCompress(_blockSize); } else { data = file.Compress(); } } break; case ERsCompress.LzmaAlchemy: using (FLzmaFile file = new FLzmaFile()) { file.Append(_memory, 0, _length); if (_blockSize > 0) { data = file.BlockCompressNative(_blockSize); } else { data = file.CompressNative(); } } break; } // 检查大小,如果压缩后更大,则不压缩数据 if (data.Length > _length) { data = ToArray(); compressCd = ERsCompress.None; } // 计算效验码 _vertifyCode = RByte.ComputeHash32(data, 0, data.Length); // 写入文件 byte[] result = null; using (FByteStream stream = new FByteStream()) { // 写入信息 stream.WriteUint8((byte)ERsCompress.Parse(compressCd)); stream.WriteInt32(_length); stream.WriteInt32(_vertifyCode); stream.WriteInt32(_blockSize); // 写入数据 if ((ERsCompress.None != compressCd) && (_blockSize > 0)) { stream.WriteBytes(data, 0, data.Length); } else { stream.WriteInt32(1); stream.WriteInt32(data.Length); stream.WriteBytes(data, 0, data.Length); } result = stream.ToArray(); } return(result); }