diff --git a/project/vc12/Nvidia.TextureTools.UnitTests/Test.cs b/project/vc12/Nvidia.TextureTools.UnitTests/Test.cs index d09b778..e3b3e03 100644 --- a/project/vc12/Nvidia.TextureTools.UnitTests/Test.cs +++ b/project/vc12/Nvidia.TextureTools.UnitTests/Test.cs @@ -1,6 +1,8 @@ using NUnit.Framework; using System; using Nvidia.TextureTools; +using System.Runtime.InteropServices; + namespace Nvidia.TextureTools.UnitTests { [TestFixture ()] public class Test { @@ -11,8 +13,56 @@ namespace Nvidia.TextureTools.UnitTests { var outputOptions = new OutputOptions (); var compressionOptions = new CompressionOptions (); var compressor = new Compressor (); + inputOptions.SetAlphaMode (AlphaMode.Premultiplied); + inputOptions.SetTextureLayout (TextureType.Texture2D, 128, 128, 1); + byte [] sourceData = new byte [128*128*4]; + var dataHandle = GCHandle.Alloc (sourceData, GCHandleType.Pinned); + var BeginImage = new OutputOptions.BeginImageHandler (BeginImageInternal); + var WriteData = new OutputOptions.OutputHandler (WriteDataInternal); + var EndImage = new OutputOptions.EndImageHandler (EndImageInternal); + var a = GCHandle.Alloc (BeginImage); + var b = GCHandle.Alloc (WriteData); + var c = GCHandle.Alloc (EndImage); + try { + var dataPtr = dataHandle.AddrOfPinnedObject (); + inputOptions.SetMipmapData (dataPtr, 128, 128, 1, 0, 0); + inputOptions.SetMipmapGeneration (false); + inputOptions.SetGamma (1.0f, 1.0f); + outputOptions.SetOutputHeader (false); + outputOptions.SetOutputOptionsOutputHandler (BeginImage, WriteData, EndImage); + var estsize = compressor.EstimateSize (inputOptions, compressionOptions); + Assert.True (compressor.Compress (inputOptions, compressionOptions, outputOptions)); + }finally { + a.Free (); + b.Free (); + c.Free (); + dataHandle.Free (); + } + } - compressor.Compress (inputOptions, compressionOptions, outputOptions); + byte [] buffer; + int offset; + + void BeginImageInternal (int size, int width, int height, int depth, int face, int miplevel) + { + buffer = new byte [size]; + offset = 0; + } + + bool WriteDataInternal (IntPtr data, int length) + { + Marshal.Copy (data, buffer, offset, length); + offset += length; + if (offset == buffer.Length) + { + + } + return true; + } + + void EndImageInternal () + { + Console.WriteLine ("EndImageInternal"); } } } diff --git a/project/vc12/Nvidia.TextureTools/TextureTools.cs b/project/vc12/Nvidia.TextureTools/TextureTools.cs index 3894da3..5aa0349 100644 --- a/project/vc12/Nvidia.TextureTools/TextureTools.cs +++ b/project/vc12/Nvidia.TextureTools/TextureTools.cs @@ -422,8 +422,9 @@ namespace Nvidia.TextureTools { #region Delegates public delegate void ErrorHandler(Error error); - private delegate void WriteDataDelegate(IntPtr data, int size); - private delegate void ImageDelegate(int size, int width, int height, int depth, int face, int miplevel); + public delegate bool OutputHandler(IntPtr data, int size); + public delegate void EndImageHandler(); + public delegate void BeginImageHandler (int size, int width, int height, int depth, int face, int miplevel); #endregion #region Bindings @@ -447,8 +448,8 @@ namespace Nvidia.TextureTools [DllImport("nvtt"), SuppressUnmanagedCodeSecurity] private extern static void nvttSetOutputOptionsOutputHeader(IntPtr outputOptions, bool b); - //[DllImport("nvtt"), SuppressUnmanagedCodeSecurity] - //private extern static void nvttSetOutputOptionsOutputHandler(IntPtr outputOptions, WriteDataDelegate writeData, ImageDelegate image); + [DllImport("nvtt"), SuppressUnmanagedCodeSecurity] + private extern static void nvttSetOutputOptionsOutputHandler(IntPtr outputOptions, IntPtr beginImageHandler, IntPtr outputHandler, IntPtr endImageHandler); #endregion @@ -477,6 +478,18 @@ namespace Nvidia.TextureTools } // @@ Add OutputHandler interface. + public void SetOutputOptionsOutputHandler (BeginImageHandler beginImageHandler, OutputHandler outputHandler, EndImageHandler endImageHandler) + { + IntPtr writeData = IntPtr.Zero; + IntPtr beginImage = IntPtr.Zero; + IntPtr endImage = IntPtr.Zero; + if (beginImageHandler != null || outputHandler != null || endImageHandler != null) { + writeData = Marshal.GetFunctionPointerForDelegate (outputHandler); + beginImage = Marshal.GetFunctionPointerForDelegate (beginImageHandler); + endImage = Marshal.GetFunctionPointerForDelegate (endImageHandler); + } + nvttSetOutputOptionsOutputHandler (this.options, beginImage, writeData, endImage); + } } #endregion