//============================================================ // <T>序列化数据内容到输出流中。</T> // // @param output 输出流 //============================================================ public override void Serialize(IOutput output) { base.Serialize(output); // 写入宽度和高度 int width = _bitmap.Width; int height = _bitmap.Height; output.WriteInt16((short)width); output.WriteInt16((short)height); // 检测是否有Aplah值 bool channelA = RBitmap.HasChannel(_bitmap, EBitmapChannel.A); if (channelA) { output.WriteInt8(2); } else { output.WriteInt8(1); } // 保存图片信息 byte[] data = RBitmap.ToBytes(_bitmap); byte[] dataResult = new byte[data.Length * 2]; int length = RCompressJpg.Compress(dataResult, 0, dataResult.Length, data, 0, data.Length, _bitmap.Width, _bitmap.Height, _qualityColor, 1); output.WriteInt32(length); output.WriteBytes(dataResult, 0, length); // 保存图片A信息 if (channelA) { length = RCompressJpg.Compress(dataResult, 0, dataResult.Length, data, 0, data.Length, _bitmap.Width, _bitmap.Height, _qualityAlpha, 2); output.WriteInt32(length); output.WriteBytes(dataResult, 0, length); } }
//============================================================ // <T>更新属性。</T> //============================================================ public void Update() { // 测试有效区域 SIntRectangle rect = new SIntRectangle(); RBitmap.TestValidRectangle(_bitmap, rect, _validAlpha); // 设置属性 _size.Set(_bitmap.Width, _bitmap.Height); _validLocation.Set(rect.Left, rect.Top); _validSize.Set(rect.Width, rect.Height); }
//============================================================ // <T>导出资源。</T> //============================================================ public void Merge() { // 计算范围 int clipCount = ClipNotEmptyCount; FRsResourceClip firstClip = FristClip; int frameCount = 0; if (null != firstClip) { frameCount = firstClip.FrameCount; } else { RMoCore.TrackConsole.Write(this, "Merge", "Animatioin is valid, first clip is empty. (code={0})", Code); return; } int width = _size.Width * frameCount; int height = _size.Height * clipCount; // 计算是否合并 using (Bitmap bitmap = new Bitmap(width, height)) { // 合并图片 int y = 0; foreach (FRsResourceClip clip in _clips) { if (clip != null) { if (!clip.Frames.IsEmpty()) { for (int x = 0; x < frameCount; x++) { FRsResourceFrame frame = clip.Frames[x]; int cx = _size.Width * x; int cy = _size.Height * y; if (frame.ValidBitmap != null) { frame.MergeLocation.Set(cx, cy); RBitmap.Copy(frame.ValidBitmap, new SIntRectangle(0, 0, frame.ValidBitmap.Width, frame.ValidBitmap.Height), bitmap, frame.MergeLocation); } } y++; } } } // 存储图片 _mergeFileName = RContent2dManager.ResourceConsole.MergeDirectory + "\\" + Code + ".png"; RDirectory.MakeDirectoriesForFile(_mergeFileName); bitmap.Save(_mergeFileName); _mergeSize.Set(bitmap.Width, bitmap.Height); } }
//============================================================ // <T>序列化数据内容到输出流中。</T> // // @param output 输出流 //============================================================ public void Serialize2(IOutput output) { base.Serialize(output); // 写入宽度和高度 int width = _bitmap.Width; int height = _bitmap.Height; output.WriteInt16((short)width); output.WriteInt16((short)height); // 获得编码器 ImageCodecInfo jpgCodec = RBitmap.FindImageCodecInfo(EBitmap.Jpg); // 检测是否有Aplah值 bool channelA = RBitmap.HasChannel(_bitmap, EBitmapChannel.A); if (channelA) { output.WriteInt8(2); } else { output.WriteInt8(1); } // 保存图片RGB信息 using (Bitmap bitmapRgb = new Bitmap(width, height, PixelFormat.Format32bppRgb)){ EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)_qualityColor); RBitmap.CopyChannels(_bitmap, bitmapRgb, EBitmapChannels.RGB); using (MemoryStream buffer = new MemoryStream()){ bitmapRgb.Save(buffer, jpgCodec, parameters); byte[] data = buffer.ToArray(); output.WriteInt32(data.Length); output.WriteBytes(data, 0, data.Length); } } // 保存图片A信息 if (channelA) { using (Bitmap bitmapA = new Bitmap(width, height, PixelFormat.Format32bppRgb)){ EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)_qualityAlpha); RBitmap.CopyChannel(_bitmap, EBitmapChannel.A, bitmapA, EBitmapChannels.RGB, _validAlpha); using (MemoryStream buffer = new MemoryStream()){ bitmapA.Save(buffer, jpgCodec, parameters); byte[] data = buffer.ToArray(); output.WriteInt32(data.Length); output.WriteBytes(data, 0, data.Length); } } } }
//============================================================ // <T>打开资源。</T> //============================================================ public override void Open() { if (!_statusOpen) { base.Open(); // 打开图片资源 _bitmap.LoadFile(_fileName); int length = (int)RFile.GetFileLength(_fileName); _size.Width = _bitmap.Width; _size.Height = _bitmap.Height; // 测试有效区域 RBitmap.TestValidRectangle(_bitmap.Native, _validRectangle, _qualityAlpha); } }
//============================================================ // <T>加载指定通道的位图文件。</T> // // @param fileName 文件名称 // @param channels 通道 //============================================================ public void LoadFileChannel(string fileName, int sourceChannel = EBitmapChannel.R, int targetChannel = EBitmapChannel.A) { using (Bitmap bitmap = new Bitmap(fileName)) { // 获得属性 int width = bitmap.Width; int height = bitmap.Height; if (null == _bitmap) { _bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); RBitmap.Clear(_bitmap, Color.FromArgb(255, 0, 0, 0).ToArgb()); } // 复制通道 RBitmap.CopyChannel(bitmap, sourceChannel, _bitmap, targetChannel); } // 更新属性 Update(); }
//============================================================ // <T>加载指定通道的位图文件。</T> // // @param fileName 文件名称 // @param channels 通道 //============================================================ public void LoadFile(string fileName, int channels = ERsBitmapChannel.ChannelsARGB) { using (Bitmap bitmap = new Bitmap(fileName)) { // 获得属性 int width = bitmap.Width; int height = bitmap.Height; if (null == _bitmap) { _bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); RBitmap.Clear(_bitmap, Color.FromArgb(255, 0, 0, 0).ToArgb()); } // 复制通道 RBitmap.CopyChannels(bitmap, _bitmap, channels); } // 更新属性 Update(); }
//============================================================ // <T>序列化数据内容到输出流中。</T> // // @param output 输出流 //============================================================ public void SerializeBitmap(IOutput output) { // 处理位图 Bitmap bitmap = _bitmap; if ((_validLocation.X != 0) || (_validLocation.Y != 0) || (_validSize.Width != _bitmap.Width) || ((_validSize.Height != _bitmap.Height))) { bitmap = new Bitmap(_validSize.Width, _validSize.Height, PixelFormat.Format32bppArgb); // 复制图片 RBitmap.Copy(_bitmap, new SIntRectangle(_validLocation.X, _validLocation.Y, _validSize.Width, _validSize.Height), bitmap, new SIntPoint2(0, 0)); } // 保存图片信息 byte[] data = RBitmap.ToBytes(bitmap); //if(RResourceManager.IsColoPremultiplied) { // int colorLength = data.Length; // for(int n = 0; n < colorLength; n += 4) { // float a = (float)data[n + 3]; // data[n] = (byte)(((float)data[n] * a) / 255.0f); // data[n + 1] = (byte)(((float)data[n + 1] * a) / 255.0f); // data[n + 2] = (byte)(((float)data[n + 2] * a) / 255.0f); // } //} else if(RResourceManager.IsColoSkipProcess) { // int skipAlpha = RResourceManager.ColoSkipAlpha; // int colorLength = data.Length; // for(int n = 0; n < colorLength; n += 4) { // if(data[n + 3] < skipAlpha) { // data[n] = 0; // data[n + 1] = 0; // data[n + 2] = 0; // data[n + 3] = 0; // } // } //} byte[] dataResult = new byte[4096 + data.Length * 2]; int length = RCompressJpg.Compress(dataResult, 0, dataResult.Length, data, 0, data.Length, bitmap.Width, bitmap.Height, _qualityColor, 1); output.WriteBytes(dataResult, 0, length); // 释放图片 if (bitmap != _bitmap) { bitmap.Dispose(); bitmap = null; } }
//============================================================ // <T>序列化数据内容到输出流中。</T> // // @param output 输出流 //============================================================ public void Serialize2(IOutput output) { // 处理位图 Bitmap bitmap = _bitmap; if ((_validLocation.X != 0) || (_validLocation.Y != 0) || (_validSize.Width != _bitmap.Width) || ((_validSize.Height != _bitmap.Height))) { bitmap = new Bitmap(_validSize.Width, _validSize.Height, PixelFormat.Format32bppArgb); // 复制图片 RBitmap.Copy(_bitmap, new SIntRectangle(_validLocation.X, _validLocation.Y, _validSize.Width, _validSize.Height), bitmap, new SIntPoint2(0, 0)); } // 写入宽度和高度 int width = _bitmap.Width; int height = _bitmap.Height; output.WriteInt16((short)width); output.WriteInt16((short)height); // 写入有效区域 output.WriteInt16((short)_validLocation.X); output.WriteInt16((short)_validLocation.Y); output.WriteInt16((short)_validSize.Width); output.WriteInt16((short)_validSize.Height); // 获得编码器 ImageCodecInfo jpgCodec = RBitmap.FindImageCodecInfo(EBitmap.Jpg); // 检测是否有Aplah值 bool channelA = RBitmap.HasChannel(bitmap, EBitmapChannel.A); if (channelA) { output.WriteInt8(2); } else { output.WriteInt8(1); } // 保存图片RGB信息 using (Bitmap bitmapRgb = new Bitmap(_validSize.Width, _validSize.Height, PixelFormat.Format32bppRgb)) { EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)_qualityColor); RBitmap.CopyChannels(bitmap, bitmapRgb, EBitmapChannels.RGB); using (MemoryStream buffer = new MemoryStream()) { bitmapRgb.Save(buffer, jpgCodec, parameters); byte[] data = buffer.ToArray(); output.WriteInt32(data.Length); output.WriteBytes(data, 0, data.Length); } } // 保存图片A信息 if (channelA) { using (Bitmap bitmapA = new Bitmap(_validSize.Width, _validSize.Height, PixelFormat.Format32bppRgb)) { EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)_qualityAlpha); RBitmap.CopyChannel(bitmap, EBitmapChannel.A, bitmapA, EBitmapChannels.RGB, _validAlpha); using (MemoryStream buffer = new MemoryStream()) { bitmapA.Save(buffer, jpgCodec, parameters); byte[] data = buffer.ToArray(); output.WriteInt32(data.Length); output.WriteBytes(data, 0, data.Length); } } } // 释放图片 if (bitmap != _bitmap) { bitmap.Dispose(); bitmap = null; } }
//============================================================ // <T>序列化数据内容到输出流中。</T> // // @param output 输出流 //============================================================ public override void Serialize(IOutput output) { // 处理位图 Bitmap bitmap = _bitmap; if ((_validLocation.X != 0) || (_validLocation.Y != 0) || (_validSize.Width != _bitmap.Width) || ((_validSize.Height != _bitmap.Height))) { bitmap = new Bitmap(_validSize.Width, _validSize.Height, PixelFormat.Format32bppArgb); // 复制图片 RBitmap.Copy(_bitmap, new SIntRectangle(_validLocation.X, _validLocation.Y, _validSize.Width, _validSize.Height), bitmap, new SIntPoint2(0, 0)); } // 写入宽度和高度 int width = _bitmap.Width; int height = _bitmap.Height; output.WriteInt16((short)width); output.WriteInt16((short)height); // 写入有效区域 output.WriteInt16((short)_validLocation.X); output.WriteInt16((short)_validLocation.Y); output.WriteInt16((short)_validSize.Width); output.WriteInt16((short)_validSize.Height); // 检测是否有Aplah值 bool channelA = RBitmap.HasChannel(bitmap, EBitmapChannel.A); if (channelA) { output.WriteInt8(2); } else { output.WriteInt8(1); } // 保存图片信息 byte[] data = RBitmap.ToBytes(bitmap); //if(RResourceManager.IsColoPremultiplied) { // int colorLength = data.Length; // for (int n = 0; n < colorLength; n += 4) { // float a = (float)data[n + 3]; // data[n ] = (byte)(((float)data[n ] * a) / 255.0f); // data[n + 1] = (byte)(((float)data[n + 1] * a) / 255.0f); // data[n + 2] = (byte)(((float)data[n + 2] * a) / 255.0f); // } //}else if(RResourceManager.IsColoSkipProcess) { // int skipAlpha = RResourceManager.ColoSkipAlpha; // int colorLength = data.Length; // for(int n = 0; n < colorLength; n += 4) { // if(data[n + 3] < skipAlpha) { // data[n] = 0; // data[n + 1] = 0; // data[n + 2] = 0; // data[n + 3] = 0; // } // } //} byte[] dataResult = new byte[4096 + data.Length * 2]; int length = RCompressJpg.Compress(dataResult, 0, dataResult.Length, data, 0, data.Length, bitmap.Width, bitmap.Height, _qualityColor, 1); output.WriteInt32(length); output.WriteBytes(dataResult, 0, length); // 保存图片A信息 if (channelA) { length = RCompressJpg.Compress(dataResult, 0, dataResult.Length, data, 0, data.Length, bitmap.Width, bitmap.Height, _qualityAlpha, 3); output.WriteInt32(length); output.WriteBytes(dataResult, 0, length); } // 释放图片 if (bitmap != _bitmap) { bitmap.Dispose(); bitmap = null; } }