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(); } }
public static Byte[] CompressWithNvidia(this Bitmap srcImage, CompressionMode mode) { System.Diagnostics.Debug.Assert(IntPtr.Size == 8, "nvtt.dll(x64) requires x64 runtime"); srcImage = srcImage.Clone(); srcImage.SwapElements(2, 1, 0, 3); var inputOptions = new Nvidia.TextureTools.InputOptions(); inputOptions.SetTextureLayout(Nvidia.TextureTools.TextureType.Texture2D, srcImage.Width, srcImage.Height, 1); inputOptions.SetMipmapGeneration(false); inputOptions.SetGamma(1.0f, 1.0f); if ((mode & CompressionMode.Dxt1) == 0) { inputOptions.SetAlphaMode(Nvidia.TextureTools.AlphaMode.Premultiplied); } else { inputOptions.SetAlphaMode(Nvidia.TextureTools.AlphaMode.None); } var compressionOptions = new Nvidia.TextureTools.CompressionOptions(); compressionOptions.SetQuality(Nvidia.TextureTools.Quality.Normal); if ((mode & CompressionMode.Dxt1) != 0) { compressionOptions.SetFormat(Nvidia.TextureTools.Format.DXT1); } if ((mode & CompressionMode.Dxt3) != 0) { compressionOptions.SetFormat(Nvidia.TextureTools.Format.DXT3); } if ((mode & CompressionMode.Dxt5) != 0) { compressionOptions.SetFormat(Nvidia.TextureTools.Format.DXT5); } return(DxtDataHandler.Compress(srcImage.Data, srcImage.Width, srcImage.Height, inputOptions, compressionOptions)); }