Beispiel #1
0
        protected override Task <ResultStatus> DoCommandOverride(ICommandContext commandContext)
        {
            // load the sound thumbnail image from the resources
            using (var imageStream = new MemoryStream(staticImageData))
                using (var image = Image.Load(imageStream))
                    using (var texTool = new TextureTool())
                        using (var texImage = texTool.Load(image, Parameters.SRgb))
                        {
                            // Rescale image so that it fits the thumbnail asked resolution
                            texTool.Decompress(texImage, texImage.Format.IsSRgb());
                            texTool.Resize(texImage, thumbnailSize.X, thumbnailSize.Y, Filter.Rescaling.Lanczos3);

                            // Save
                            using (var outputImageStream = MicrothreadLocalDatabases.DatabaseFileProvider.OpenStream(Url, VirtualFileMode.Create, VirtualFileAccess.Write))
                                using (var outputImage = texTool.ConvertToStrideImage(texImage))
                                {
                                    ThumbnailBuildHelper.ApplyThumbnailStatus(outputImage, DependencyBuildStatus);

                                    outputImage.Save(outputImageStream, ImageFileType.Png);

                                    commandContext.Logger.Verbose($"Thumbnail creation successful [{Url}] to ({outputImage.Description.Width}x{outputImage.Description.Height},{outputImage.Description.Format})");
                                }
                        }

            return(Task.FromResult(ResultStatus.Successful));
        }
Beispiel #2
0
        private void HandleResizing(TextureTool texTool, TexImage image)
        {
            if (Width != null && Height != null)
            {
                bool targetInPercent;
                var  width  = ParsePixelSize(Width, out targetInPercent);
                var  height = ParsePixelSize(Height, out targetInPercent);

                if (targetInPercent)
                {
                    texTool.Rescale(image, width / 100f, height / 100f, RescalingFilter);
                }
                else
                {
                    texTool.Resize(image, width, height, RescalingFilter);
                }
            }
            else if (Width != null && Height == null)
            {
                bool targetInPercent;
                var  width = ParsePixelSize(Width, out targetInPercent);

                if (targetInPercent)
                {
                    texTool.Rescale(image, width / 100f, 1, RescalingFilter);
                }
                else
                {
                    texTool.Resize(image, width, image.Height, RescalingFilter);
                }
            }
            else if (Width == null && Height != null)
            {
                bool targetInPercent;
                var  height = ParsePixelSize(Height, out targetInPercent);

                if (targetInPercent)
                {
                    texTool.Rescale(image, 1, height / 100f, RescalingFilter);
                }
                else
                {
                    texTool.Resize(image, image.Width, height, RescalingFilter);
                }
            }
        }
Beispiel #3
0
        protected override Task <ResultStatus> DoCommandOverride(ICommandContext commandContext)
        {
            var assetManager = new AssetManager();

            // Load image
            var image = assetManager.Load <Image>(InputUrl);

            // Initialize TextureTool library
            using (var texTool = new TextureTool())
                using (var texImage = texTool.Load(image))
                {
                    var outputFormat = Format.HasValue ? Format.Value : image.Description.Format;

                    // Apply transformations
                    texTool.Decompress(texImage);
                    if (IsAbsolute)
                    {
                        texTool.Resize(texImage, (int)Width, (int)Height, Filter.Rescaling.Lanczos3);
                    }
                    else
                    {
                        texTool.Rescale(texImage, Width / 100.0f, Height / 100.0f, Filter.Rescaling.Lanczos3);
                    }

                    // Generate mipmaps
                    if (GenerateMipmaps)
                    {
                        texTool.GenerateMipMaps(texImage, Filter.MipMapGeneration.Box);
                    }

                    // Convert/Compress to output format
                    texTool.Compress(texImage, outputFormat);

                    // Save
                    using (var outputImage = texTool.ConvertToParadoxImage(texImage))
                    {
                        assetManager.Save(OutputUrl, outputImage);

                        commandContext.Logger.Verbose("Compression successful [{3}] to ({0}x{1},{2})",
                                                      outputImage.Description.Width,
                                                      outputImage.Description.Height, outputImage.Description.Format, OutputUrl);
                    }
                }

            return(Task.FromResult(ResultStatus.Successful));
        }
Beispiel #4
0
        public void ResizeTest(string file)
        {
            TexImage image  = texTool.Load(TestTools.InputTestFolder + file);
            int      width  = image.Width;
            int      height = image.Height;

            Assert.IsTrue(image.MipmapCount > 1);

            texTool.Resize(image, width / 2, height / 2, Filter.Rescaling.Bicubic);
            Assert.IsTrue(image.Width == width / 2);
            Assert.IsTrue(image.Height == height / 2);
            Assert.IsTrue(image.MipmapCount == 1);

            Assert.IsTrue(TestTools.ComputeSHA1(image.Data, image.DataSize).Equals(TestTools.GetInstance().Checksum["TextureTool_Rescale_" + image.Name]));
            //Console.WriteLine("TextureTool_Rescale_" + image.Name + "." + TestTools.ComputeSHA1(image.Data, image.DataSize));

            image.Dispose();
        }
Beispiel #5
0
        public static ResultStatus ImportTextureImage(TextureTool textureTool, TexImage texImage, ImportParameters parameters, CancellationToken cancellationToken, Logger logger)
        {
            var assetManager = new ContentManager();

            // Apply transformations
            textureTool.Decompress(texImage, parameters.IsSRgb);

            // Special case when the input texture is monochromatic but it is supposed to be a color and we are working in SRGB
            // In that case, we need to transform it to a supported SRGB format (R8G8B8A8_UNorm_SRgb)
            // TODO: As part of a conversion phase, this code may be moved to a dedicated method in this class at some point
            if (parameters.TextureHint == TextureHint.Color && parameters.IsSRgb && (texImage.Format == PixelFormat.R8_UNorm || texImage.Format == PixelFormat.A8_UNorm))
            {
                textureTool.Convert(texImage, PixelFormat.R8G8B8A8_UNorm_SRgb);
            }

            if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
            {
                return(ResultStatus.Cancelled);
            }

            var fromSize   = new Size2(texImage.Width, texImage.Height);
            var targetSize = parameters.DesiredSize;

            // Resize the image
            if (parameters.IsSizeInPercentage)
            {
                targetSize = new Size2((int)(fromSize.Width * targetSize.Width / 100.0f), (int)(fromSize.Height * targetSize.Height / 100.0f));
            }

            // Find the target size
            targetSize = FindBestTextureSize(parameters, targetSize, logger);

            // Resize the image only if needed
            if (targetSize != fromSize)
            {
                textureTool.Resize(texImage, targetSize.Width, targetSize.Height, Filter.Rescaling.Lanczos3);
            }

            if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
            {
                return(ResultStatus.Cancelled);
            }

            // texture size is now determined, we can cache it
            var textureSize = new Int2(texImage.Width, texImage.Height);

            // determine the alpha format of the texture when set to Auto
            // Note: this has to be done before the ColorKey transformation in order to be able to take advantage of image file AlphaDepth information
            if (parameters.DesiredAlpha == AlphaFormat.Auto)
            {
                var colorKey   = parameters.ColorKeyEnabled? (Color?)parameters.ColorKeyColor : null;
                var alphaLevel = textureTool.GetAlphaLevels(texImage, new Rectangle(0, 0, textureSize.X, textureSize.Y), colorKey, logger);
                parameters.DesiredAlpha = alphaLevel.ToAlphaFormat();
            }

            // Apply the color key
            if (parameters.ColorKeyEnabled)
            {
                textureTool.ColorKey(texImage, parameters.ColorKeyColor);
            }

            if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
            {
                return(ResultStatus.Cancelled);
            }

            // Pre-multiply alpha only for relevant formats
            if (parameters.PremultiplyAlpha && texImage.Format.HasAlpha32Bits())
            {
                textureTool.PreMultiplyAlpha(texImage);
            }

            if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
            {
                return(ResultStatus.Cancelled);
            }


            // Generate mipmaps
            if (parameters.GenerateMipmaps)
            {
                var boxFilteringIsSupported = !texImage.Format.IsSRgb() || (MathUtil.IsPow2(textureSize.X) && MathUtil.IsPow2(textureSize.Y));
                textureTool.GenerateMipMaps(texImage, boxFilteringIsSupported? Filter.MipMapGeneration.Box: Filter.MipMapGeneration.Linear);
            }

            if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
            {
                return(ResultStatus.Cancelled);
            }


            // Convert/Compress to output format
            // TODO: Change alphaFormat depending on actual image content (auto-detection)?
            var outputFormat = DetermineOutputFormat(parameters, textureSize, texImage.Format);

            textureTool.Compress(texImage, outputFormat, (TextureConverter.Requests.TextureQuality)parameters.TextureQuality);

            if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
            {
                return(ResultStatus.Cancelled);
            }

            // Save the texture
            using (var outputImage = textureTool.ConvertToXenkoImage(texImage))
            {
                if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                {
                    return(ResultStatus.Cancelled);
                }

                assetManager.Save(parameters.OutputUrl, outputImage.ToSerializableVersion());

                logger.Verbose("Compression successful [{3}] to ({0}x{1},{2})", outputImage.Description.Width, outputImage.Description.Height, outputImage.Description.Format, parameters.OutputUrl);
            }

            return(ResultStatus.Successful);
        }
Beispiel #6
0
            protected override Task <ResultStatus> DoCommandOverride(ICommandContext commandContext)
            {
                var assetManager = new ContentManager(MicrothreadLocalDatabases.ProviderService);

                var items = new Heightmap[] { new Heightmap(), };

                foreach (var heightmap in items)
                {
                    var source = Parameters.Source;
                    if (!string.IsNullOrEmpty(source))
                    {
                        using (var textureTool = new TextureTool())
                            using (var texImage = textureTool.Load(source, Parameters.IsSRgb))
                            {
                                // Resize the image if need

                                var size = Parameters.Resizing.Enabled ?
                                           Parameters.Resizing.Size :
                                           new Int2(texImage.Width, texImage.Height);

                                if (!HeightfieldColliderShapeDesc.IsValidHeightStickSize(size))
                                {
                                    continue;
                                }

                                if (texImage.Width != size.X || texImage.Height != size.Y)
                                {
                                    textureTool.Resize(texImage, size.X, size.Y, Filter.Rescaling.Nearest);
                                }

                                // Convert pixel format of the image

                                var heightfieldType = Parameters.HeightParameters.HeightType;

                                switch (heightfieldType)
                                {
                                case HeightfieldTypes.Float:
                                    switch (texImage.Format)
                                    {
                                    case PixelFormat.R32_Float:
                                        break;

                                    case PixelFormat.R32G32B32A32_Float:
                                    case PixelFormat.R16_Float:
                                        textureTool.Convert(texImage, PixelFormat.R32_Float);
                                        break;

                                    case PixelFormat.R16G16B16A16_UNorm:
                                    case PixelFormat.R16_UNorm:
                                        textureTool.Convert(texImage, PixelFormat.R16_SNorm);
                                        textureTool.Convert(texImage, PixelFormat.R32_Float);
                                        break;

                                    case PixelFormat.B8G8R8A8_UNorm:
                                    case PixelFormat.R8G8B8A8_UNorm:
                                    case PixelFormat.R8_UNorm:
                                        textureTool.Convert(texImage, PixelFormat.R8_SNorm);
                                        textureTool.Convert(texImage, PixelFormat.R32_Float);
                                        break;

                                    case PixelFormat.B8G8R8A8_UNorm_SRgb:
                                    case PixelFormat.B8G8R8X8_UNorm_SRgb:
                                    case PixelFormat.R8G8B8A8_UNorm_SRgb:
                                        textureTool.Convert(texImage, PixelFormat.R8_SNorm);
                                        textureTool.Convert(texImage, PixelFormat.R32_Float);
                                        break;

                                    default:
                                        continue;
                                    }
                                    break;

                                case HeightfieldTypes.Short:
                                    switch (texImage.Format)
                                    {
                                    case PixelFormat.R16_SNorm:
                                        break;

                                    case PixelFormat.R16G16B16A16_SNorm:
                                    case PixelFormat.R16G16B16A16_UNorm:
                                    case PixelFormat.R16_UNorm:
                                        textureTool.Convert(texImage, PixelFormat.R16_SNorm);
                                        break;

                                    case PixelFormat.R8G8B8A8_SNorm:
                                    case PixelFormat.B8G8R8A8_UNorm:
                                    case PixelFormat.R8G8B8A8_UNorm:
                                    case PixelFormat.R8_UNorm:
                                        textureTool.Convert(texImage, PixelFormat.R8_SNorm);
                                        textureTool.Convert(texImage, PixelFormat.R16_SNorm);
                                        break;

                                    case PixelFormat.B8G8R8A8_UNorm_SRgb:
                                    case PixelFormat.B8G8R8X8_UNorm_SRgb:
                                    case PixelFormat.R8G8B8A8_UNorm_SRgb:
                                        textureTool.Convert(texImage, PixelFormat.R8_SNorm);
                                        textureTool.Convert(texImage, PixelFormat.R16_SNorm);
                                        break;

                                    default:
                                        continue;
                                    }
                                    break;

                                case HeightfieldTypes.Byte:
                                    switch (texImage.Format)
                                    {
                                    case PixelFormat.R8_UNorm:
                                        break;

                                    case PixelFormat.R8G8B8A8_SNorm:
                                    case PixelFormat.B8G8R8A8_UNorm:
                                    case PixelFormat.R8G8B8A8_UNorm:
                                        textureTool.Convert(texImage, PixelFormat.R8_UNorm);
                                        break;

                                    case PixelFormat.B8G8R8A8_UNorm_SRgb:
                                    case PixelFormat.B8G8R8X8_UNorm_SRgb:
                                    case PixelFormat.R8G8B8A8_UNorm_SRgb:
                                        textureTool.Convert(texImage, PixelFormat.R8_UNorm);
                                        break;

                                    default:
                                        continue;
                                    }
                                    break;

                                default:
                                    continue;
                                }

                                // Range

                                var heightRange = Parameters.HeightParameters.HeightRange;

                                if (heightRange.Y < heightRange.X)
                                {
                                    continue;
                                }

                                // Read, scale and set heights

                                var heightScale = Parameters.HeightParameters.HeightScale;

                                if (Math.Abs(heightScale) < float.Epsilon)
                                {
                                    continue;
                                }

                                using (var image = textureTool.ConvertToXenkoImage(texImage))
                                {
                                    var pixelBuffer = image.PixelBuffer[0];

                                    switch (heightfieldType)
                                    {
                                    case HeightfieldTypes.Float:
                                    {
                                        heightmap.Floats = pixelBuffer.GetPixels <float>();

                                        var floatConversionParameters = Parameters.HeightParameters as FloatHeightmapHeightConversionParamters;
                                        if (floatConversionParameters == null)
                                        {
                                            continue;
                                        }

                                        float scale = 1f;

                                        if (floatConversionParameters.ScaleToFit)
                                        {
                                            var max = heightmap.Floats.Max(h => Math.Abs(h));
                                            if ((max - 1f) < float.Epsilon)
                                            {
                                                max = 1f;
                                            }
                                            scale = Math.Max(Math.Abs(heightRange.X), Math.Abs(heightRange.Y)) / max;
                                        }

                                        for (int i = 0; i < heightmap.Floats.Length; ++i)
                                        {
                                            heightmap.Floats[i] = MathUtil.Clamp(heightmap.Floats[i] * scale, heightRange.X, heightRange.Y);
                                        }
                                    }
                                    break;

                                    case HeightfieldTypes.Short:
                                    {
                                        heightmap.Shorts = pixelBuffer.GetPixels <short>();
                                    }
                                    break;

                                    case HeightfieldTypes.Byte:
                                    {
                                        heightmap.Bytes = pixelBuffer.GetPixels <byte>();
                                    }
                                    break;

                                    default:
                                        continue;
                                    }

                                    // Set rest of properties

                                    heightmap.HeightType  = heightfieldType;
                                    heightmap.Size        = size;
                                    heightmap.HeightRange = heightRange;
                                    heightmap.HeightScale = heightScale;
                                }
                            }
                    }
                }

                assetManager.Save(Url, items[0]);

                return(Task.FromResult(ResultStatus.Successful));
            }
        public static ResultStatus ImportAndSaveTextureImage(UFile sourcePath, string outputUrl, TextureAsset textureAsset, TextureConvertParameters parameters, bool separateAlpha, CancellationToken cancellationToken, Logger logger)
        {
            var assetManager = new AssetManager();

            using (var texTool = new TextureTool())
                using (var texImage = texTool.Load(sourcePath))
                {
                    // Apply transformations
                    texTool.Decompress(texImage);

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }

                    // Resize the image
                    if (textureAsset.IsSizeInPercentage)
                    {
                        texTool.Rescale(texImage, textureAsset.Width / 100.0f, textureAsset.Height / 100.0f, Filter.Rescaling.Lanczos3);
                    }
                    else
                    {
                        texTool.Resize(texImage, (int)textureAsset.Width, (int)textureAsset.Height, Filter.Rescaling.Lanczos3);
                    }

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }

                    // texture size is now determined, we can cache it
                    var textureSize = new Int2(texImage.Width, texImage.Height);

                    // Check that the resulting texture size is supported by the targeted graphics profile
                    if (!TextureSizeSupported(textureAsset.Format, parameters.GraphicsPlatform, parameters.GraphicsProfile, textureSize, textureAsset.GenerateMipmaps, logger))
                    {
                        return(ResultStatus.Failed);
                    }


                    // Apply the color key
                    if (textureAsset.ColorKeyEnabled)
                    {
                        texTool.ColorKey(texImage, textureAsset.ColorKeyColor);
                    }

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }


                    // Pre-multiply alpha
                    if (textureAsset.PremultiplyAlpha)
                    {
                        texTool.PreMultiplyAlpha(texImage);
                    }

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }


                    // Generate mipmaps
                    if (textureAsset.GenerateMipmaps)
                    {
                        texTool.GenerateMipMaps(texImage, Filter.MipMapGeneration.Box);
                    }

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }


                    // Convert/Compress to output format
                    // TODO: Change alphaFormat depending on actual image content (auto-detection)?
                    var outputFormat = DetermineOutputFormat(textureAsset.Format, textureAsset.Alpha, parameters.Platform, parameters.GraphicsPlatform, parameters.GraphicsProfile, textureSize, texImage.Format);
                    texTool.Compress(texImage, outputFormat, (TextureConverter.Requests.TextureQuality)parameters.TextureQuality);

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }


                    // Save the texture
                    if (separateAlpha)
                    {
                        TextureAlphaComponentSplitter.CreateAndSaveSeparateTextures(texTool, texImage, outputUrl, textureAsset.GenerateMipmaps);
                    }
                    else
                    {
                        using (var outputImage = texTool.ConvertToParadoxImage(texImage))
                        {
                            if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                            {
                                return(ResultStatus.Cancelled);
                            }

                            assetManager.Save(outputUrl, outputImage);

                            logger.Info("Compression successful [{3}] to ({0}x{1},{2})", outputImage.Description.Width, outputImage.Description.Height, outputImage.Description.Format, outputUrl);
                        }
                    }
                }

            return(ResultStatus.Successful);
        }
Beispiel #8
0
        public static ResultStatus ImportAndSaveTextureImage(UFile sourcePath, string outputUrl, TextureAsset textureAsset, TextureConvertParameters parameters, CancellationToken cancellationToken, Logger logger)
        {
            var assetManager = new AssetManager();

            using (var texTool = new TextureTool())
                using (var texImage = texTool.Load(sourcePath, textureAsset.SRgb))
                {
                    // Apply transformations
                    texTool.Decompress(texImage, textureAsset.SRgb);

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }

                    var fromSize   = new Size2(texImage.Width, texImage.Height);
                    var targetSize = new Size2((int)textureAsset.Width, (int)textureAsset.Height);

                    // Resize the image
                    if (textureAsset.IsSizeInPercentage)
                    {
                        targetSize = new Size2((int)(fromSize.Width * (float)textureAsset.Width / 100.0f), (int)(fromSize.Height * (float)textureAsset.Height / 100.0f));
                    }

                    // Find the target size
                    targetSize = FindBestTextureSize(textureAsset.Format, parameters.GraphicsPlatform, parameters.GraphicsProfile, fromSize, targetSize, textureAsset.GenerateMipmaps, logger);

                    // Resize the image only if needed
                    if (targetSize != fromSize)
                    {
                        texTool.Resize(texImage, targetSize.Width, targetSize.Height, Filter.Rescaling.Lanczos3);
                    }

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }

                    // texture size is now determined, we can cache it
                    var textureSize = new Int2(texImage.Width, texImage.Height);

                    // Apply the color key
                    if (textureAsset.ColorKeyEnabled)
                    {
                        texTool.ColorKey(texImage, textureAsset.ColorKeyColor);
                    }

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }


                    // Pre-multiply alpha
                    if (textureAsset.PremultiplyAlpha)
                    {
                        texTool.PreMultiplyAlpha(texImage);
                    }

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }


                    // Generate mipmaps
                    if (textureAsset.GenerateMipmaps)
                    {
                        var boxFilteringIsSupported = texImage.Format != PixelFormat.B8G8R8A8_UNorm_SRgb || (IsPowerOfTwo(textureSize.X) && IsPowerOfTwo(textureSize.Y));
                        texTool.GenerateMipMaps(texImage, boxFilteringIsSupported? Filter.MipMapGeneration.Box: Filter.MipMapGeneration.Linear);
                    }

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }


                    // Convert/Compress to output format
                    // TODO: Change alphaFormat depending on actual image content (auto-detection)?
                    var outputFormat = DetermineOutputFormat(textureAsset, parameters, textureSize, texImage.Format, parameters.Platform, parameters.GraphicsPlatform, parameters.GraphicsProfile);
                    texTool.Compress(texImage, outputFormat, (TextureConverter.Requests.TextureQuality)parameters.TextureQuality);

                    if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                    {
                        return(ResultStatus.Cancelled);
                    }


                    // Save the texture
                    if (parameters.SeparateAlpha)
                    {
                        //TextureAlphaComponentSplitter.CreateAndSaveSeparateTextures(texTool, texImage, outputUrl, textureAsset.GenerateMipmaps);
                    }
                    else
                    {
                        using (var outputImage = texTool.ConvertToParadoxImage(texImage))
                        {
                            if (cancellationToken.IsCancellationRequested) // abort the process if cancellation is demanded
                            {
                                return(ResultStatus.Cancelled);
                            }

                            assetManager.Save(outputUrl, outputImage.ToSerializableVersion());

                            logger.Info("Compression successful [{3}] to ({0}x{1},{2})", outputImage.Description.Width, outputImage.Description.Height, outputImage.Description.Format, outputUrl);
                        }
                    }
                }

            return(ResultStatus.Successful);
        }
            protected override Task <ResultStatus> DoCommandOverride(ICommandContext commandContext)
            {
                var assetManager = new ContentManager(MicrothreadLocalDatabases.ProviderService);

                var items = new Heightmap[] { new Heightmap(), };

                foreach (var heightmap in items)
                {
                    var source = Parameters.Source;
                    if (!string.IsNullOrEmpty(source))
                    {
                        using (var textureTool = new TextureTool())
                            using (var texImage = textureTool.Load(source, Parameters.IsSRgb))
                            {
                                // Resize the image if need

                                var size = Parameters.Size.Enabled && Parameters.Size.Size.X > 1 && Parameters.Size.Size.Y > 1 ?
                                           Parameters.Size.Size :
                                           new Int2(texImage.Width, texImage.Height);

                                if (texImage.Width != size.X || texImage.Height != size.Y)
                                {
                                    textureTool.Resize(texImage, size.X, size.Y, Filter.Rescaling.Nearest);
                                }

                                // Convert pixel format of the image

                                var heightfieldType = Parameters.Type;

                                switch (heightfieldType)
                                {
                                case HeightfieldTypes.Float:
                                    switch (texImage.Format)
                                    {
                                    case PixelFormat.R32_Float:
                                        break;

                                    case PixelFormat.R32G32B32A32_Float:
                                    case PixelFormat.R16_Float:
                                        textureTool.Convert(texImage, PixelFormat.R32_Float);
                                        break;

                                    case PixelFormat.R16G16B16A16_UNorm:
                                    case PixelFormat.R16_UNorm:
                                        textureTool.Convert(texImage, PixelFormat.R16_SNorm);
                                        textureTool.Convert(texImage, PixelFormat.R32_Float);
                                        break;

                                    case PixelFormat.B8G8R8A8_UNorm:
                                    case PixelFormat.R8G8B8A8_UNorm:
                                    case PixelFormat.R8_UNorm:
                                        textureTool.Convert(texImage, PixelFormat.R8_SNorm);
                                        textureTool.Convert(texImage, PixelFormat.R32_Float);
                                        break;

                                    case PixelFormat.B8G8R8A8_UNorm_SRgb:
                                    case PixelFormat.B8G8R8X8_UNorm_SRgb:
                                    case PixelFormat.R8G8B8A8_UNorm_SRgb:
                                        textureTool.Convert(texImage, PixelFormat.R8_SNorm);
                                        textureTool.Convert(texImage, PixelFormat.R32_Float);
                                        break;

                                    default:
                                        continue;
                                    }
                                    break;

                                case HeightfieldTypes.Short:
                                    switch (texImage.Format)
                                    {
                                    case PixelFormat.R16_SNorm:
                                        break;

                                    case PixelFormat.R16G16B16A16_SNorm:
                                    case PixelFormat.R16G16B16A16_UNorm:
                                    case PixelFormat.R16_UNorm:
                                        textureTool.Convert(texImage, PixelFormat.R16_SNorm);
                                        break;

                                    case PixelFormat.R8G8B8A8_SNorm:
                                    case PixelFormat.B8G8R8A8_UNorm:
                                    case PixelFormat.R8G8B8A8_UNorm:
                                    case PixelFormat.R8_UNorm:
                                        textureTool.Convert(texImage, PixelFormat.R8_SNorm);
                                        textureTool.Convert(texImage, PixelFormat.R16_SNorm);
                                        break;

                                    case PixelFormat.B8G8R8A8_UNorm_SRgb:
                                    case PixelFormat.B8G8R8X8_UNorm_SRgb:
                                    case PixelFormat.R8G8B8A8_UNorm_SRgb:
                                        textureTool.Convert(texImage, PixelFormat.R8_SNorm);
                                        textureTool.Convert(texImage, PixelFormat.R16_SNorm);
                                        break;

                                    default:
                                        continue;
                                    }
                                    break;

                                case HeightfieldTypes.Byte:
                                    switch (texImage.Format)
                                    {
                                    case PixelFormat.R8_UNorm:
                                        break;

                                    case PixelFormat.R8G8B8A8_SNorm:
                                    case PixelFormat.B8G8R8A8_UNorm:
                                    case PixelFormat.R8G8B8A8_UNorm:
                                        textureTool.Convert(texImage, PixelFormat.R8_UNorm);
                                        break;

                                    case PixelFormat.B8G8R8A8_UNorm_SRgb:
                                    case PixelFormat.B8G8R8X8_UNorm_SRgb:
                                    case PixelFormat.R8G8B8A8_UNorm_SRgb:
                                        textureTool.Convert(texImage, PixelFormat.R8_UNorm);
                                        break;

                                    default:
                                        continue;
                                    }
                                    break;

                                default:
                                    continue;
                                }

                                // Read, scale and set heights

                                using (var image = textureTool.ConvertToXenkoImage(texImage))
                                {
                                    var pixelBuffer = image.PixelBuffer[0];
                                    var scale       = Parameters.HeightScale;

                                    switch (heightfieldType)
                                    {
                                    case HeightfieldTypes.Float:
                                        heightmap.Floats = pixelBuffer.GetPixels <float>();
                                        for (int i = 0; i < heightmap.Floats.Length; ++i)
                                        {
                                            heightmap.Floats[i] *= scale;
                                        }
                                        break;

                                    case HeightfieldTypes.Short:
                                        heightmap.Shorts = pixelBuffer.GetPixels <short>();
                                        for (int i = 0; i < heightmap.Shorts.Length; ++i)
                                        {
                                            heightmap.Shorts[i] = (short)MathUtil.Clamp(heightmap.Shorts[i] * scale, short.MinValue, short.MaxValue);
                                        }
                                        break;

                                    case HeightfieldTypes.Byte:
                                        heightmap.Bytes = pixelBuffer.GetPixels <byte>();
                                        for (int i = 0; i < heightmap.Bytes.Length; ++i)
                                        {
                                            heightmap.Bytes[i] = (byte)MathUtil.Clamp(heightmap.Bytes[i] * scale, byte.MinValue, byte.MaxValue);
                                        }
                                        break;

                                    default:
                                        continue;
                                    }

                                    // Set rest of properties

                                    heightmap.HeightfieldType = heightfieldType;
                                    heightmap.Width           = size.X;
                                    heightmap.Length          = size.Y;
                                }
                            }
                    }
                }

                assetManager.Save(Url, items[0]);

                return(Task.FromResult(ResultStatus.Successful));
            }