Example #1
0
        //============================================================
        // <T>存储压缩文件。</T>
        //
        // @param fileName 文件名称
        // @param widthCount 横向分割个数
        // @param hightCount 纵向分割个数
        //============================================================
        public void StoreCompress(IOutput output, Bitmap bitmap, int colorCount, int widthCount, int hightCount)
        {
            // 输出图片尺寸
            int width  = bitmap.Width;
            int height = bitmap.Height;

            output.WriteUint16((ushort)width);
            output.WriteUint16((ushort)height);
            // 输出分割尺寸
            int splitWidth  = width / widthCount;
            int splitHeight = height / hightCount;
            int splitCount  = widthCount * hightCount;

            output.WriteUint16((ushort)splitCount);
            // 分割图片
            for (int y = 0; y < hightCount; y++)
            {
                for (int x = 0; x < widthCount; x++)
                {
                    FByteStream bs = new FByteStream();
                    bs.WriteUint16((ushort)splitWidth);
                    bs.WriteUint16((ushort)splitHeight);
                    // 绘制局部图形
                    FBitmap image = new FBitmap(splitWidth, splitHeight);
                    image.CopyFrom(bitmap, splitWidth * x, splitHeight * y);
                    // 存储索引图片
                    StoreIndexedColor(bs, image.Native, colorCount, x, y);
                    // 输出数据
                    output.WriteInt32(bs.Length);
                    output.WriteBytes(bs.Memory, 0, bs.Length);
                }
            }
        }
Example #2
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);
        }