Пример #1
0
        public void GeneratesImageWithCustomHeightWidth(string inputFile, string resourcePath, int expectedOutput)
        {
            var config = GetConfiguration();

            config.IntermediateOutputPath += GetOutputDirectorySuffix((nameof(inputFile), inputFile), (nameof(expectedOutput), expectedOutput));
            var generator = new ImageResizeGenerator(config);

            var image = new OutputImage
            {
                Height     = expectedOutput,
                Width      = expectedOutput,
                InputFile  = Path.Combine(TestConstants.ImageDirectory, inputFile),
                OutputFile = Path.Combine(config.IntermediateOutputPath, "dotnetbot.png"),
                OutputLink = Path.Combine("Resources", resourcePath, "dotnetbot.png"),
                RequiresBackgroundColor = false,
                Scale           = 0,
                ShouldBeVisible = true,
                Watermark       = null
            };

            var ex = Record.Exception(() => generator.ProcessImage(image));

            Assert.Null(ex);

            VerifyImageContents(image);
        }
Пример #2
0
        public void AppliesWatermark(string inputImageName, string watermarkImage)
        {
            var config = GetConfiguration();

            config.IntermediateOutputPath += GetOutputDirectorySuffix((nameof(inputImageName), inputImageName), (nameof(watermarkImage), watermarkImage));
            var generator = new ImageResizeGenerator(config);

            var image = new OutputImage
            {
                Height     = 0,
                Width      = 0,
                InputFile  = Path.Combine(TestConstants.WatermarkImageDirectory, $"{inputImageName}.png"),
                OutputFile = Path.Combine(config.IntermediateOutputPath, $"{inputImageName}.png"),
                OutputLink = Path.Combine("Resources", "drawable-xxxhdpi", $"{inputImageName}.png"),
                RequiresBackgroundColor = false,
                Scale           = 1,
                ShouldBeVisible = true,
                Watermark       = new WatermarkConfiguration
                {
                    SourceFile = Path.Combine(TestConstants.WatermarkImageDirectory, $"{watermarkImage}.png")
                }
            };

            generator.ProcessImage(image);

            VerifyImageContents(image);
        }
Пример #3
0
        public void AppliesTextBanner(string text, double scale, WatermarkPosition position)
        {
            var config = GetConfiguration();

            config.IntermediateOutputPath += GetOutputDirectorySuffix((nameof(text), text), (nameof(scale), scale), (nameof(position), position));
            var generator = new ImageResizeGenerator(config);

            var image = new OutputImage
            {
                Height     = 0,
                Width      = 0,
                InputFile  = Path.Combine(TestConstants.ImageDirectory, "dotnetbot.png"),
                OutputFile = Path.Combine(config.IntermediateOutputPath, "dotnetbot.png"),
                OutputLink = Path.Combine("Resources", "drawable-xxxhdpi", "dotnetbot.png"),
                RequiresBackgroundColor = true,
                Scale           = scale,
                ShouldBeVisible = true,
                Watermark       = new WatermarkConfiguration
                {
                    Text     = text,
                    Position = position
                }
            };

            generator.ProcessImage(image);

            VerifyImageContents(image);
        }
Пример #4
0
        public void AppliesPadding(string inputFile, double paddingFactor)
        {
            var config = GetConfiguration();

            config.IntermediateOutputPath += GetOutputDirectorySuffix((nameof(paddingFactor), paddingFactor), (nameof(inputFile), inputFile));
            var generator = new ImageResizeGenerator(config);

            var outputFilename = $"{Path.GetFileNameWithoutExtension(inputFile)}.png";

            var image = new OutputImage
            {
                Height     = 0,
                Width      = 0,
                InputFile  = Path.Combine(TestConstants.ImageDirectory, inputFile),
                OutputFile = Path.Combine(config.IntermediateOutputPath, outputFilename),
                OutputLink = Path.Combine("Resources", "drawable-xxxhdpi", outputFilename),
                RequiresBackgroundColor = false,
                Scale           = 1.0,
                ShouldBeVisible = true,
                Watermark       = null,
                BackgroundColor = "Red",
                PaddingColor    = "Yellow",
                PaddingFactor   = paddingFactor
            };

            var ex = Record.Exception(() => generator.ProcessImage(image));

            Assert.Null(ex);

            VerifyImageContents(image);
        }
Пример #5
0
        public void GeneratesImageWithCustomHeightWidth(string resourcePath, int expectedOutput)
        {
            var config = GetConfiguration();

            config.IntermediateOutputPath += resourcePath;
            var generator = new ImageResizeGenerator(config);

            var image = new OutputImage
            {
                Height     = expectedOutput,
                Width      = expectedOutput,
                InputFile  = Path.Combine(TestConstants.ImageDirectory, "dotnetbot.png"),
                OutputFile = Path.Combine(config.IntermediateOutputPath, "dotnetbot.png"),
                OutputLink = Path.Combine("Resources", "drawable-xxxhdpi", "dotnetbot.png"),
                RequiresBackgroundColor = false,
                Scale             = 0,
                ShouldBeVisible   = true,
                WatermarkFilePath = null
            };

            var ex = Record.Exception(() => generator.ProcessImage(image));

            Assert.Null(ex);
            Assert.True(File.Exists(image.OutputFile));

            using var imageResource = Image.Load(image.OutputFile);
            Assert.Equal(expectedOutput, imageResource.Width);
        }
Пример #6
0
        public void SetsCustomBackground()
        {
            var config    = GetConfiguration();
            var generator = new ImageResizeGenerator(config);

            var image = new OutputImage
            {
                Height     = 0,
                Width      = 0,
                InputFile  = Path.Combine(TestConstants.ImageDirectory, "dotnetbot.png"),
                OutputFile = Path.Combine(config.IntermediateOutputPath, "dotnetbot.png"),
                OutputLink = Path.Combine("Resources", "drawable-xxxhdpi", "dotnetbot.png"),
                RequiresBackgroundColor = true,
                Scale             = 1,
                ShouldBeVisible   = true,
                WatermarkFilePath = null,
                BackgroundColor   = "#8A2BE2"
            };

            generator.ProcessImage(image);

            using var inputImage  = Image.Load(image.InputFile);
            using var outputImage = Image.Load(image.OutputFile);
            using var inputClone  = inputImage.CloneAs <Rgba32>();
            using var outputClone = outputImage.CloneAs <Rgba32>();

            var comparedTransparentPixel = false;

            for (var y = 0; y < inputImage.Height; ++y)
            {
                var inputPixelRowSpan  = inputClone.GetPixelRowSpan(y);
                var outputPixelRowSpan = outputClone.GetPixelRowSpan(y);
                for (var x = 0; x < inputImage.Width; ++x)
                {
                    var startingPixel = inputPixelRowSpan[x];
                    if (startingPixel.A == 0)
                    {
                        comparedTransparentPixel = true;
                        var pixel = outputPixelRowSpan[x];
                        Assert.Equal(138, pixel.R);
                        Assert.Equal(43, pixel.G);
                        Assert.Equal(226, pixel.B);
                        Assert.Equal(255, pixel.A);
                    }
                }
            }

            Assert.True(comparedTransparentPixel);
        }
        public void AppliesWatermark(string inputImageName, string watermarkImage)
        {
            var config    = GetConfiguration();
            var generator = new ImageResizeGenerator(config);

            var image = new OutputImage
            {
                Height     = 0,
                Width      = 0,
                InputFile  = Path.Combine(TestConstants.WatermarkImageDirectory, $"{inputImageName}.png"),
                OutputFile = Path.Combine($"{config.IntermediateOutputPath}-{inputImageName}-{watermarkImage}", $"{inputImageName}.png"),
                OutputLink = Path.Combine("Resources", "drawable-xxxhdpi", $"{inputImageName}.png"),
                RequiresBackgroundColor = false,
                Scale           = 1,
                ShouldBeVisible = true,
                Watermark       = new WatermarkConfiguration
                {
                    SourceFile = Path.Combine(TestConstants.WatermarkImageDirectory, $"{watermarkImage}.png")
                }
            };

            generator.ProcessImage(image);

            using var inputImage  = Image.Load(image.InputFile);
            using var outputImage = Image.Load(image.OutputFile);
            using var inputClone  = inputImage.CloneAs <Rgba32>();
            using var outputClone = outputImage.CloneAs <Rgba32>();

            bool appliedWatermark;

            for (var y = 0; y < inputImage.Height; ++y)
            {
                var inputPixelRowSpan  = inputClone.GetPixelRowSpan(y);
                var outputPixelRowSpan = outputClone.GetPixelRowSpan(y);
                for (var x = 0; x < inputImage.Width; ++x)
                {
                    appliedWatermark = inputPixelRowSpan[x] == outputPixelRowSpan[x];
                    if (appliedWatermark)
                    {
                        return;
                    }
                }
            }

            _testOutputHelper.WriteLine("All pixels are the same in the Input and Output Images");
            Assert.True(false);
        }
Пример #8
0
        public void SetsDefaultBackground()
        {
            var config    = GetConfiguration();
            var generator = new ImageResizeGenerator(config);

            var image = new OutputImage
            {
                Height     = 0,
                Width      = 0,
                InputFile  = Path.Combine(TestConstants.ImageDirectory, "dotnetbot.png"),
                OutputFile = Path.Combine(config.IntermediateOutputPath, "dotnetbot.png"),
                OutputLink = Path.Combine("Resources", "drawable-xxxhdpi", "dotnetbot.png"),
                RequiresBackgroundColor = true,
                Scale           = 1,
                ShouldBeVisible = true,
                Watermark       = null
            };

            generator.ProcessImage(image);

            VerifyImageContents(image);
        }
        public void AppliesTextBanner(string text)
        {
            var config    = GetConfiguration();
            var generator = new ImageResizeGenerator(config);

            var image = new OutputImage
            {
                Height     = 0,
                Width      = 0,
                InputFile  = Path.Combine(TestConstants.ImageDirectory, "dotnetbot.png"),
                OutputFile = Path.Combine(config.IntermediateOutputPath, "dotnetbot.png"),
                OutputLink = Path.Combine("Resources", "drawable-xxxhdpi", "dotnetbot.png"),
                RequiresBackgroundColor = true,
                Scale           = .5,
                ShouldBeVisible = true,
                Watermark       = new WatermarkConfiguration
                {
                    Text = text
                }
            };

            generator.ProcessImage(image);
        }