Beispiel #1
0
        public void Test_RemoveAlpha()
        {
            string tempFile = GetTemporaryFileName(".png");

            try
            {
                using (MagickImage image = new MagickImage(Files.MagickNETIconPNG))
                {
                    Assert.IsTrue(image.HasAlpha);
                    image.ColorAlpha(new MagickColor("yellow"));
                    image.HasAlpha = true;
                    image.Write(tempFile);

                    image.Read(tempFile);

                    Assert.IsTrue(image.HasAlpha);

                    PngOptimizer optimizer = new PngOptimizer();
                    optimizer.LosslessCompress(tempFile);

                    image.Read(tempFile);
                    Assert.IsFalse(image.HasAlpha);
                }
            }
            finally
            {
                FileHelper.Delete(tempFile);
            }
        }
Beispiel #2
0
        public void Test_RemoveAlpha()
        {
            string tempFile = Path.GetTempPath() + Guid.NewGuid().ToString() + ".png";

            try
            {
                using (MagickImage image = new MagickImage(Files.MagickNETIconPNG))
                {
                    Assert.IsTrue(image.HasAlpha);
                    image.ColorAlpha(new MagickColor("yellow"));
                    image.HasAlpha = true;
                    image.Write(tempFile);

                    image.Read(tempFile);

                    // TODO: Figure out why this does not fail in a single run but does when all tests are run.
                    //Assert.IsTrue(image.HasAlpha);

                    PngOptimizer optimizer = new PngOptimizer();
                    optimizer.LosslessCompress(tempFile);

                    image.Read(tempFile);
                    Assert.IsFalse(image.HasAlpha);
                }
            }
            finally
            {
                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }
            }
        }
Beispiel #3
0
        private void Test(string imagePath, string exePath, string exeArgs, out OptimizerArgs argsOut, out long startingSize)
        {
            var inputStream = new MemoryStream();

            using (var testJpeg = File.OpenRead(imagePath))
            {
                testJpeg.CopyTo(inputStream);
            }

            var sut = new PngOptimizer();

            sut.ExePath = exePath;
            sut.AdditionalToolArguments = exeArgs;

            var args = new OptimizerArgs(inputStream);

            startingSize = args.Stream.Length;

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            sut.Process(args);
            stopwatch.Stop();
            output.WriteLine($"Time: {stopwatch.ElapsedMilliseconds}ms");

            argsOut = args;
        }
Beispiel #4
0
                public void ShouldNotOptimizeAnimatedPNG()
                {
                    PngOptimizer optimizer = new PngOptimizer();

                    using (TemporaryFile tempFile = new TemporaryFile(Files.Coders.AnimatedPNGexampleBouncingBeachBallPNG))
                    {
                        var result = optimizer.Compress(tempFile);
                        Assert.IsFalse(result);
                    }
                }
        public void QuantizationWillMakeOriginalPngSmaller()
        {
            var config    = new Mock <IRRConfiguration>();
            var optimizer = new PngOptimizer(new FileWrapper(), config.Object, new WuQuantizer());
            var original  = File.ReadAllBytes("testimages\\menu-sprite.png");

            var result = optimizer.OptimizePng(original, 0, false);

            Assert.True(result.Length < original.Length);
        }
        public void WillNotAlterOriginalImage()
        {
            var config = new Mock <IRRConfiguration>();

            config.Setup(x => x.ResourcePhysicalPath).Returns("testimages");
            var optimizer = new PngOptimizer(new FileWrapper(), config.Object, new WuQuantizer());
            var original  = File.ReadAllBytes("testimages\\menu-sprite.png");

            optimizer.OptimizePng(original, 5, false);

            Assert.Equal(original.Length, File.ReadAllBytes("testimages\\menu-sprite.png").Length);
        }
        public void CompressionWillMakeOriginalPngSmaller()
        {
            var config = new Mock <IRRConfiguration>();

            config.Setup(x => x.ResourcePhysicalPath).Returns("testimages");
            var optimizer = new PngOptimizer(new FileWrapper(), config.Object, new WuQuantizer());
            var original  = File.ReadAllBytes("testimages\\menu-sprite.png");

            var result = optimizer.OptimizePng(original, 5, true);

            Assert.True(result.Length < original.Length);
        }
        public void WillCleanupScratchFile()
        {
            var config = new Mock <IRRConfiguration>();

            config.Setup(x => x.ResourcePhysicalPath).Returns("testimages");
            var optimizer = new PngOptimizer(new FileWrapper(), config.Object, new WuQuantizer());
            var original  = File.ReadAllBytes("testimages\\menu-sprite.png");
            var fileCount = Directory.GetFiles("testimages").Length;

            optimizer.OptimizePng(original, 5, true);

            Assert.Equal(fileCount, Directory.GetFiles("testimages").Length);
        }
Beispiel #9
0
        public void Process(GetMediaStreamPipelineArgs args)
        {
            Assert.ArgumentNotNull(args, "args");

            if (args.Options.Thumbnail)
            {
                return;
            }

            MediaStream outputStream = args.OutputStream;

            if (outputStream == null)
            {
                return;
            }

            if (!outputStream.AllowMemoryLoading)
            {
                Tracer.Error("Could not resize image as it was larger than the maximum size allowed for memory processing. Media item: {0}", outputStream.MediaItem.Path);
                return;
            }

            string mimeType = args.MediaData.MimeType;

            if (!mimeType.StartsWith("image/", StringComparison.Ordinal))
            {
                return;
            }

            string extension = args.MediaData.Extension;

            IImageOptimizer optimizer = null;

            if (extension.Equals("png"))
            {
                optimizer = new PngOptimizer(outputStream.Stream);
            }

            if (extension.Equals("jpg") || extension.Equals("jpeg"))
            {
                optimizer = new JpegOptimizer(outputStream.Stream);
            }

            if (optimizer == null)
            {
                return;
            }

            var sw = new Stopwatch();

            sw.Start();

            var result = optimizer.Optimize();

            sw.Stop();

            if (result.Success)
            {
                outputStream.Stream.Close();

                Log.Info("Dianoga: optimized {0}.{1} ({2} bytes) - saved {3} bytes / {4:p}. Optimized in {5}ms.".FormatWith(args.OutputStream.MediaItem.MediaPath, args.OutputStream.MediaItem.Extension, result.SizeAfter, result.SizeBefore - result.SizeAfter, 1 - ((result.SizeAfter / (float)result.SizeBefore)), sw.ElapsedMilliseconds), this);

                args.OutputStream = new MediaStream(result.CreateResultStream(), outputStream.Extension, outputStream.MediaItem);
            }
            else
            {
                Log.Error("Dianoga: unable to optimize {0} because {1}".FormatWith(args.OutputStream.MediaItem.Name, result.ErrorMessage), this);
            }
        }