Beispiel #1
0
        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));
        }
Beispiel #2
0
        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();
            }
        }
Beispiel #3
0
        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();
            }
        }