Пример #1
0
        //============================================================
        // <T>序列化位图的一个区块。</T>
        //
        // @params output         输出流
        // @params colorQuantizer 颜色优化器
        // @params rect           序列化的矩形区域
        // @params colorCount     颜色数量
        //============================================================
        protected bool SerializeBlock(IOutput output, IColorQuantizer colorQuantizer, SIntRectangle rect, int colorCount)
        {
            FByteStream stream      = new FByteStream();
            int         blockWidth  = rect.Width;
            int         blockHeight = rect.Height;

            // 写入设置
            stream.WriteBool(_optionAlpha);
            stream.WriteUint16((ushort)blockWidth);
            stream.WriteUint16((ushort)blockHeight);
            // 构造调色板
            Color[] palette = null;
            if (_optionAlpha)
            {
                palette = colorQuantizer.MakePalette(colorCount).ToArray();
                if (0 == palette.Length)
                {
                    RMoCore.TrackConsole.Write(this, "SerializeBlock", "Palette color is empty.");
                    return(false);
                }
            }
            else
            {
                List <Color> colorList = colorQuantizer.MakePalette(colorCount);
                if (0 == colorList.Count)
                {
                    RMoCore.TrackConsole.Write(this, "SerializeBlock", "Palette color is empty.");
                    return(false);
                }
                colorList.Add(Color.FromArgb(0, 0, 0, 0));
                palette = colorList.ToArray();
            }
            // 输出调色板
            int paletteCount = palette.Length;

            stream.WriteUint16((ushort)paletteCount);
            //if(RResourceManager.IsColoPremultiplied) {
            //   foreach (Color color in palette) {
            //      if (_optionAlpha) {
            //         stream.WriteInt32(color.ToArgb() & 0x00FFFFFF);
            //      } else {
            //         byte a = color.A;
            //         byte r = (byte)(((float)color.R * (float)a) / 255.0f);
            //         byte g = (byte)(((float)color.G * (float)a) / 255.0f);
            //         byte b = (byte)(((float)color.B * (float)a) / 255.0f);
            //         stream.WriteInt32(Color.FromArgb(a, r, g, b).ToArgb());
            //      }
            //   }
            //} else if(RResourceManager.IsColoSkipProcess) {
            //   int skipAlpha = RResourceManager.ColoSkipAlpha;
            //   foreach(Color color in palette) {
            //      if(_optionAlpha) {
            //         stream.WriteInt32(color.ToArgb() & 0x00FFFFFF);
            //      } else {
            //         if(color.A < skipAlpha) {
            //            stream.WriteInt32(0);
            //         } else {
            //            stream.WriteInt32(color.ToArgb());
            //         }
            //      }
            //   }
            //} else {
            //   foreach(Color color in palette) {
            //      if(_optionAlpha) {
            //         stream.WriteInt32(color.ToArgb() & 0x00FFFFFF);
            //      } else {
            //         stream.WriteInt32(color.ToArgb());
            //      }
            //   }
            //}
            // _logger.Debug(this, "SerializeIndexed", "block_size={0}x{1}, color={2}, alpha={3}", blockWidth, blockHeight, paletteCount, _optionAlpha);
            // 输出颜色数组
            int x = rect.Left;
            int y = rect.Top;
            // 透明色索引
            int transparentIndex = palette.Length - 1;
            int size             = blockWidth * blockHeight;
            // 写入数组
            int postion = 0;

            byte[] bytes = null;
            if (_optionAlpha)
            {
                // 写入索引颜色和透明度
                bytes = new byte[size * 2];
                for (int h = 0; h < blockHeight; h++)
                {
                    for (int w = 0; w < blockWidth; w++)
                    {
                        Color color = _bitmap.GetPixel(x + w, y + h);
                        int   index = colorQuantizer.FindPaletteIndex(Color.FromArgb(255, color));
                        bytes[postion++] = (byte)index;
                        bytes[postion++] = color.A;
                    }
                }
            }
            else
            {
                // 写入带透明的索引颜色
                bytes = new byte[size];
                for (int h = 0; h < blockHeight; h++)
                {
                    for (int w = 0; w < blockWidth; w++)
                    {
                        Color color = _bitmap.GetPixel(x + w, y + h);
                        int   index = colorQuantizer.FindPaletteIndex(color);
                        bytes[postion++] = (byte)index;
                    }
                }
            }
            stream.WriteBytes(bytes, 0, postion);
            // 写入数据
            output.WriteInt32(stream.Length);
            output.WriteBytes(stream.Memory, 0, stream.Length);
            return(true);
        }
Пример #2
0
        //============================================================
        // <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);
        }