protected override bool TryCopyFrom(BitmapContent sourceBitmap, Rectangle sourceRegion, Rectangle destinationRegion)
        {
            SurfaceFormat sourceFormat;

            if (!sourceBitmap.TryGetFormat(out sourceFormat))
            {
                return(false);
            }

            SurfaceFormat format;

            TryGetFormat(out format);

            // A shortcut for copying the entire bitmap to another bitmap of the same type and format
            if (format == sourceFormat && (sourceRegion == new Rectangle(0, 0, Width, Height)) && sourceRegion == destinationRegion)
            {
                SetPixelData(sourceBitmap.GetPixelData());
                return(true);
            }

            // TODO: Add a XNA unit test to see what it does
            // my guess is that this is invalid for DXT.
            //
            // Destination region copy is not yet supported
            if (destinationRegion != new Rectangle(0, 0, Width, Height))
            {
                return(false);
            }

            // If the source is not Vector4 or requires resizing, send it through BitmapContent.Copy
            if (!(sourceBitmap is PixelBitmapContent <Vector4>) || sourceRegion.Width != destinationRegion.Width || sourceRegion.Height != destinationRegion.Height)
            {
                try
                {
                    BitmapContent.Copy(sourceBitmap, sourceRegion, this, destinationRegion);
                    return(true);
                }
                catch (InvalidOperationException)
                {
                    return(false);
                }
            }

            // NVTT wants 8bit data in BGRA format.
            var colorBitmap = new PixelBitmapContent <Color>(sourceBitmap.Width, sourceBitmap.Height);

            BitmapContent.Copy(sourceBitmap, colorBitmap);
            var sourceData = colorBitmap.GetPixelData();
            var dataHandle = GCHandle.Alloc(sourceData, GCHandleType.Pinned);

            AlphaMode alphaMode;
            Format    outputFormat;
            var       alphaDither = false;

            switch (format)
            {
            case SurfaceFormat.Dxt1:
            case SurfaceFormat.Dxt1SRgb:
            {
                bool hasTransparency;
                PrepareNVTT_DXT1(sourceData, out hasTransparency);
                outputFormat = hasTransparency ? Format.DXT1a : Format.DXT1;
                alphaMode    = hasTransparency ? AlphaMode.Transparency : AlphaMode.None;
                alphaDither  = true;
                break;
            }

            case SurfaceFormat.Dxt3:
            case SurfaceFormat.Dxt3SRgb:
            {
                PrepareNVTT(sourceData);
                outputFormat = Format.DXT3;
                alphaMode    = AlphaMode.Transparency;
                break;
            }

            case SurfaceFormat.Dxt5:
            case SurfaceFormat.Dxt5SRgb:
            {
                PrepareNVTT(sourceData);
                outputFormat = Format.DXT5;
                alphaMode    = AlphaMode.Transparency;
                break;
            }

            default:
                throw new InvalidOperationException("Invalid DXT surface format!");
            }

            // Do all the calls to the NVTT wrapper within this handler
            // so we properly clean up if things blow up.
            try
            {
                var dataPtr = dataHandle.AddrOfPinnedObject();

                var inputOptions = new InputOptions();
                inputOptions.SetTextureLayout(TextureType.Texture2D, colorBitmap.Width, colorBitmap.Height, 1);
                inputOptions.SetMipmapData(dataPtr, colorBitmap.Width, colorBitmap.Height, 1, 0, 0);
                inputOptions.SetMipmapGeneration(false);
                inputOptions.SetGamma(1.0f, 1.0f);
                inputOptions.SetAlphaMode(alphaMode);

                var compressionOptions = new CompressionOptions();
                compressionOptions.SetFormat(outputFormat);
                compressionOptions.SetQuality(Quality.Normal);

                // TODO: This isn't working which keeps us from getting the
                // same alpha dither behavior on DXT1 as XNA.
                //
                // See https://github.com/MonoGame/MonoGame/issues/6259
                //
                //if (alphaDither)
                //compressionOptions.SetQuantization(false, false, true);

                var outputOptions = new OutputOptions();
                outputOptions.SetOutputHeader(false);
                outputOptions.SetOutputOptionsOutputHandler(NvttBeginImage, NvttWriteImage, NvttEndImage);

                var dxtCompressor = new Compressor();
                dxtCompressor.Compress(inputOptions, compressionOptions, outputOptions);
            }
            finally
            {
                dataHandle.Free();
            }

            return(true);
        }
Esempio n. 2
0
        protected override bool TryCopyFrom(BitmapContent sourceBitmap, Rectangle sourceRegion, Rectangle destinationRegion)
        {
            SurfaceFormat sourceFormat;

            if (!sourceBitmap.TryGetFormat(out sourceFormat))
            {
                return(false);
            }

            SurfaceFormat format;

            TryGetFormat(out format);

            // A shortcut for copying the entire bitmap to another bitmap of the same type and format
            if (format == sourceFormat && (sourceRegion == new Rectangle(0, 0, Width, Height)) && sourceRegion == destinationRegion)
            {
                SetPixelData(sourceBitmap.GetPixelData());
                return(true);
            }

            // Destination region copy is not yet supported
            if (destinationRegion != new Rectangle(0, 0, Width, Height))
            {
                return(false);
            }

            // If the source is not Vector4 or requires resizing, send it through BitmapContent.Copy
            if (!(sourceBitmap is PixelBitmapContent <Vector4>) || sourceRegion.Width != destinationRegion.Width || sourceRegion.Height != destinationRegion.Height)
            {
                try
                {
                    BitmapContent.Copy(sourceBitmap, sourceRegion, this, destinationRegion);
                    return(true);
                }
                catch (InvalidOperationException)
                {
                    return(false);
                }
            }

            //SquishFlags targetFormat = SquishFlags.ColourClusterFit;
            Format outputFormat = Format.DXT1;

            switch (format)
            {
            case SurfaceFormat.Dxt1:
                outputFormat = Format.DXT1;
                break;

            case SurfaceFormat.Dxt1SRgb:
                outputFormat = Format.DXT1;
                break;

            case SurfaceFormat.Dxt3:
                outputFormat = Format.DXT3;
                break;

            case SurfaceFormat.Dxt3SRgb:
                outputFormat = Format.DXT3;
                break;

            case SurfaceFormat.Dxt5:
                outputFormat = Format.DXT5;
                break;

            case SurfaceFormat.Dxt5SRgb:
                outputFormat = Format.DXT5;
                break;

            default:
                return(false);
            }

            // libsquish requires RGBA8888
            var colorBitmap = new PixelBitmapContent <Color>(sourceBitmap.Width, sourceBitmap.Height);

            BitmapContent.Copy(sourceBitmap, colorBitmap);

            var sourceData = colorBitmap.GetPixelData();

            /*
             * var dataSize = Squish.GetStorageRequirements(colorBitmap.Width, colorBitmap.Height, targetFormat);
             * var data = new byte[dataSize];
             * var metric = new float[] { 1.0f, 1.0f, 1.0f };
             * Squish.CompressImage(sourceData, colorBitmap.Width, colorBitmap.Height, data, targetFormat, metric);
             * SetPixelData(data);
             */

            var dxtCompressor = new Compressor();
            var inputOptions  = new InputOptions();

            if (outputFormat != Format.DXT1)
            {
                inputOptions.SetAlphaMode(AlphaMode.Premultiplied);
            }
            else
            {
                inputOptions.SetAlphaMode(AlphaMode.None);
            }
            inputOptions.SetTextureLayout(TextureType.Texture2D, colorBitmap.Width, colorBitmap.Height, 1);

            // Small hack here. NVTT wants 8bit data in BGRA. Flip the B and R channels
            // again here.
            GraphicsUtil.BGRAtoRGBA(sourceData);
            var dataHandle = GCHandle.Alloc(sourceData, GCHandleType.Pinned);

            try
            {
                var dataPtr = dataHandle.AddrOfPinnedObject();

                inputOptions.SetMipmapData(dataPtr, colorBitmap.Width, colorBitmap.Height, 1, 0, 0);
                inputOptions.SetMipmapGeneration(false);
                inputOptions.SetGamma(1.0f, 1.0f);

                var outputOptions = new OutputOptions();
                outputOptions.SetOutputHeader(false);

                var handler = new DxtDataHandler(this);
                outputOptions.SetOutputHandler(handler.BeginImage, handler.WriteData);

                var compressionOptions = new CompressionOptions();
                compressionOptions.SetFormat(outputFormat);
                compressionOptions.SetQuality(Quality.Normal);

                dxtCompressor.Compress(inputOptions, compressionOptions, outputOptions);
            }
            finally
            {
                dataHandle.Free();
            }
            return(true);
        }