public static XndHolder NewXNDHolder() { XndHolder holder = new XndHolder(); holder.mNode = new XndNode(); return(holder); }
public static System.Drawing.Bitmap LoadPngBitmapFromTxPic(IO.XndHolder xnd) { #region Read Png var attr = xnd.Node.FindAttrib("PNG"); byte[] bytes = null; attr.BeginRead(); attr.Read(out bytes, (int)attr.Length); attr.EndRead(); var stream = new System.IO.MemoryStream(bytes); return(new Bitmap(stream)); #endregion }
public static void SaveXND(System.String file, XndHolder node) { if (node == null || node.mNode == null) { return; } var io = CEngine.Instance.FileManager.OpenFileForWrite(file, EFileType.Xnd); if (io == null) { return; } node.mNode.Save(io); }
public static XndHolder SyncLoadXND(System.String file) { var io = CEngine.Instance.FileManager.OpenFileForRead(file, EFileType.Xnd, false); if (io == null) { return(null); } XndNode node = new XndNode(); if (false == node.Load(io)) { return(null); } XndHolder holder = new XndHolder(); holder.mNode = node; return(holder); }
public static async System.Threading.Tasks.Task <XndHolder> LoadXND(System.String file, Thread.Async.EAsyncTarget threadTarget = Thread.Async.EAsyncTarget.AsyncIO) { return(await CEngine.Instance.EventPoster.Post(() => { var io = CEngine.Instance.FileManager.OpenFileForRead(file, EFileType.Xnd, false); if (io == null) { return null; } XndNode node = new XndNode(); if (false == node.Load(io)) { return null; } XndHolder holder = new XndHolder(); holder.mNode = node; return holder; }, threadTarget)); }
public static async System.Threading.Tasks.Task <XndHolder> LoadXND(System.String file) { return(await CEngine.Instance.EventPoster.Post(() => { var io = CEngine.Instance.FileManager.OpenFileForRead(file, EFileType.Xnd, false); if (io == null) { return null; } XndNode node = new XndNode(GetEmptyNativePointer()); if (false == node.Load(io)) { return null; } XndHolder holder = new XndHolder(); holder.mNode = node; return holder; })); }
public static bool SaveTxPic(IO.XndHolder xnd, ref CTxPicDesc txDesc, string filename, ETCFormat etcFormat = ETCFormat.RGBA8, int mipMapLevel = 0) { #region Read Pixels var imageimport = new EngineNS.Bricks.ImageImport.ImageImport(); EngineNS.Support.CBlobObject blob = new EngineNS.Support.CBlobObject(); imageimport.LoadTexture(filename, blob); int w = imageimport.GetWidth(); int h = imageimport.GetHeight(); int channels = imageimport.GetChannels(); byte[] data = blob.ToBytes();//new byte[w * h * channels]; System.Drawing.Imaging.PixelFormat format = System.Drawing.Imaging.PixelFormat.Undefined; if (channels == 4) { format = System.Drawing.Imaging.PixelFormat.Format32bppArgb; } else if (channels == 3) { format = System.Drawing.Imaging.PixelFormat.Format24bppRgb; } else if (channels == 1) { format = System.Drawing.Imaging.PixelFormat.Format32bppArgb; } var bitmap = new System.Drawing.Bitmap(w, h, format); int offset = 0; for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { if (channels == 4) { System.Drawing.Color color = System.Drawing.Color.FromArgb(data[offset + 3], data[offset], data[offset + 1], data[offset + 2]); offset += 4; bitmap.SetPixel(i, j, color); } else if (channels == 3) { System.Drawing.Color color = System.Drawing.Color.FromArgb(data[offset], data[offset + 1], data[offset + 2]); offset += 3; bitmap.SetPixel(i, j, color); } else if (channels == 1) { System.Drawing.Color color = System.Drawing.Color.FromArgb(data[offset++]); bitmap.SetPixel(i, j, color); } } } var tagBitmap = bitmap.Clone(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.PixelFormat.Format32bppArgb); #endregion if (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed) { txDesc.EtcFormat = ETCFormat.UNKNOWN; } txDesc.Width = w; txDesc.Height = h; SaveTxPic(xnd, ref txDesc, tagBitmap, etcFormat, mipMapLevel); return(true); }
public static void SaveTxPic(IO.XndHolder xnd, ref CTxPicDesc txDesc, Bitmap tagBitmap, ETCFormat etcFormat = ETCFormat.RGBA8, int mipMapLevel = 0) { #region Png var attr = xnd.Node.AddAttrib("PNG"); attr.BeginWrite(); var tagStream = new MemoryStream(); tagBitmap.Save(tagStream, System.Drawing.Imaging.ImageFormat.Png); var pngData = tagStream.ToArray(); attr.Write(pngData, pngData.Length); attr.EndWrite(); #endregion #region PngMips var mipsNode = xnd.Node.AddNode("PngMips", 0, 0); int curMip = 0; while (true) { var mipAttr = mipsNode.AddAttrib($"Mip_{curMip}"); curMip++; mipAttr.BeginWrite(); var mipStream = new MemoryStream(); tagBitmap.Save(mipStream, System.Drawing.Imaging.ImageFormat.Png); var pngMipData = mipStream.ToArray(); mipAttr.Write(pngMipData, pngMipData.Length); mipAttr.EndWrite(); if (txDesc.MipLevel == curMip) { break; } if (tagBitmap.Width == 1 && tagBitmap.Height == 1) { break; } var mipWidth = tagBitmap.Width / 2; var mipHeight = tagBitmap.Height / 2; if (mipWidth == 0) { mipWidth = 1; } if (mipHeight == 0) { mipHeight = 1; } //tagBitmap = EngineNS.BitmapProc.ScaleBitmap(tagBitmap, mipWidth, mipHeight); tagBitmap = EngineNS.BitmapProc.GenerateMip(tagBitmap, mipWidth, mipHeight); } #endregion if (CEngine.IsWriteEtc) { if (etcFormat != ETCFormat.UNKNOWN) { using (var etcBlob = EngineNS.Support.CBlobProxy2.CreateBlobProxy()) { unsafe { fixed(byte *dataPtr = &pngData[0]) { var texCompressor = new EngineNS.Bricks.TexCompressor.CTexCompressor(); texCompressor.EncodePng2ETC((IntPtr)dataPtr, (uint)pngData.Length, etcFormat, mipMapLevel, etcBlob); etcBlob.BeginRead(); } } if (etcBlob.DataLength >= 0) { var etcNode = xnd.Node.AddNode("EtcMips", 0, 0); int fmt = 0; int MipLevel = 0; etcBlob.Read(out fmt); etcBlob.Read(out MipLevel); var layer = new EngineNS.Bricks.TexCompressor.ETCLayer(); for (int i = 0; i < MipLevel; i++) { etcBlob.Read(out layer); byte[] etcMipData; etcBlob.Read(out etcMipData, (int)layer.Size); var mipAttr = etcNode.AddAttrib($"Mip_{i}"); mipAttr.BeginWrite(); mipAttr.Write(layer); mipAttr.Write(etcMipData, etcMipData.Length); mipAttr.EndWrite(); } } } } } attr = xnd.Node.AddAttrib("Desc"); txDesc.MipLevel = curMip; txDesc.EtcFormat = etcFormat; #region Desc attr.Version = 3; attr.BeginWrite(); unsafe { fixed(EngineNS.CTxPicDesc *descPin = &txDesc) { attr.Write((IntPtr)(descPin), sizeof(EngineNS.CTxPicDesc)); } } attr.EndWrite(); #endregion }