예제 #1
0
        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);
        }
예제 #2
0
파일: TextureHelper.cs 프로젝트: zetz/xenko
        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);
        }
예제 #3
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);
        }