public static Byte[] Compress(Byte[] data, int width, int height, Nvidia.TextureTools.InputOptions inOptions, Nvidia.TextureTools.CompressionOptions compressionOptions) { var dxtCompressor = new Nvidia.TextureTools.Compressor(); var dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned); try { var dataPtr = dataHandle.AddrOfPinnedObject(); inOptions.SetMipmapData(dataPtr, width, height, 1, 0, 0); var outOptions = new Nvidia.TextureTools.OutputOptions(); outOptions.SetOutputHeader(false); using (var dataHandler = new DxtDataHandler(outOptions)) { dxtCompressor.Compress(inOptions, compressionOptions, outOptions); return(dataHandler._buffer); } } finally { dataHandle.Free(); } }
private DxtDataHandler(Nvidia.TextureTools.OutputOptions outputOptions) { _WriteData = new Nvidia.TextureTools.OutputOptions.WriteDataDelegate(WriteDataInternal); _BeginImage = new Nvidia.TextureTools.OutputOptions.ImageDelegate(BeginImageInternal); // Keep the delegate from being re-located or collected by the garbage collector. delegateHandleBeginImage = GCHandle.Alloc(_BeginImage); delegateHandleWriteData = GCHandle.Alloc(_WriteData); outputOptions.SetOutputHandler(_BeginImage, _WriteData); }
public static Byte[] CompressWithNvidia(this Bitmap srcImage, CompressionMode mode) { Assert.AreEqual(8, IntPtr.Size, "nvtt.dll(x64) requires x64 runtime"); var dxtCompressor = new Nvidia.TextureTools.Compressor(); var inputOptions = new Nvidia.TextureTools.InputOptions(); if ((mode & CompressionMode.Dxt1) == 0) { inputOptions.SetAlphaMode(Nvidia.TextureTools.AlphaMode.Premultiplied); } else { inputOptions.SetAlphaMode(Nvidia.TextureTools.AlphaMode.None); } inputOptions.SetTextureLayout(Nvidia.TextureTools.TextureType.Texture2D, srcImage.Width, srcImage.Height, 1); Nvidia.TextureTools.Format outputFormat = Nvidia.TextureTools.Format.DXT1; if ((mode & CompressionMode.Dxt3) != 0) { outputFormat = Nvidia.TextureTools.Format.DXT3; } if ((mode & CompressionMode.Dxt5) != 0) { outputFormat = Nvidia.TextureTools.Format.DXT5; } srcImage = srcImage.Clone(); srcImage.SwapElements(2, 1, 0, 3); var dataHandle = GCHandle.Alloc(srcImage.Data, GCHandleType.Pinned); try { var dataPtr = dataHandle.AddrOfPinnedObject(); inputOptions.SetMipmapData(dataPtr, srcImage.Width, srcImage.Height, 1, 0, 0); inputOptions.SetMipmapGeneration(false); inputOptions.SetGamma(1.0f, 1.0f); var compressionOptions = new Nvidia.TextureTools.CompressionOptions(); compressionOptions.SetFormat(outputFormat); compressionOptions.SetQuality(Nvidia.TextureTools.Quality.Normal); var outputOptions = new Nvidia.TextureTools.OutputOptions(); outputOptions.SetOutputHeader(false); using (var dataHandler = new DxtDataHandler(outputOptions)) { dxtCompressor.Compress(inputOptions, compressionOptions, outputOptions); return(dataHandler._buffer); } } finally { dataHandle.Free(); } }