From 76dbd64dfeefed26f550a1e4e264b7e69f3ce97b Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 1 Apr 2018 00:10:13 +0200 Subject: [PATCH] refactored QuantizeImageShouldPreserveMaximumColorPrecision (hope it uses less memory now) --- .../Formats/GeneralFormatTests.cs | 96 ++++++++++++------- 1 file changed, 63 insertions(+), 33 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs index 26b5dca27..7fd58a005 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -13,7 +13,9 @@ using Xunit; namespace SixLabors.ImageSharp.Tests { using System; + using System.Reflection; + using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Quantization; @@ -61,44 +63,72 @@ namespace SixLabors.ImageSharp.Tests } } - [Fact] - public void QuantizeImageShouldPreserveMaximumColorPrecision() - { - string path = TestEnvironment.CreateOutputDirectory("Quantize"); - - foreach (TestFile file in Files) - { - using (Image srcImage = Image.Load(file.Bytes, out var mimeType)) + public static readonly TheoryData QuantizerNames = + new TheoryData { - using (Image image = srcImage.Clone()) - { - using (FileStream output = File.OpenWrite($"{path}/Octree-{file.FileName}")) - { - image.Mutate(x => x.Quantize(KnownQuantizers.Octree)); - image.Save(output, mimeType); + nameof(KnownQuantizers.Octree), + nameof(KnownQuantizers.Palette), + nameof(KnownQuantizers.Wu) + }; - } - } + [Theory] + [WithFile(TestImages.Png.CalliphoraPartial, nameof(QuantizerNames), PixelTypes.Rgba32)] + [WithFile(TestImages.Png.Bike, nameof(QuantizerNames), PixelTypes.Rgba32)] + public void QuantizeImageShouldPreserveMaximumColorPrecision(TestImageProvider provider, string quantizerName) + where TPixel:struct,IPixel + { + provider.Configuration.MemoryManager = ArrayPoolMemoryManager.CreateWithModeratePooling(); - using (Image image = srcImage.Clone()) - { - using (FileStream output = File.OpenWrite($"{path}/Wu-{file.FileName}")) - { - image.Mutate(x => x.Quantize(KnownQuantizers.Wu)); - image.Save(output, mimeType); - } - } + IQuantizer quantizer = GetQuantizer(quantizerName); - using (Image image = srcImage.Clone()) - { - using (FileStream output = File.OpenWrite($"{path}/Palette-{file.FileName}")) - { - image.Mutate(x => x.Quantize(KnownQuantizers.Palette)); - image.Save(output, mimeType); - } - } - } + using (Image image = provider.GetImage()) + { + image.Mutate(c => c.Quantize(quantizer)); + image.DebugSave(provider); } + + provider.Configuration.MemoryManager.ReleaseRetainedResources(); + + //string path = TestEnvironment.CreateOutputDirectory("Quantize"); + + //foreach (TestFile file in Files) + //{ + // using (Image srcImage = Image.Load(file.Bytes, out IImageFormat mimeType)) + // { + // using (Image image = srcImage.Clone()) + // { + // using (FileStream output = File.OpenWrite($"{path}/Octree-{file.FileName}")) + // { + // image.Mutate(x => x.Quantize(KnownQuantizers.Octree)); + // image.Save(output, mimeType); + // } + // } + + // using (Image image = srcImage.Clone()) + // { + // using (FileStream output = File.OpenWrite($"{path}/Wu-{file.FileName}")) + // { + // image.Mutate(x => x.Quantize(KnownQuantizers.Wu)); + // image.Save(output, mimeType); + // } + // } + + // using (Image image = srcImage.Clone()) + // { + // using (FileStream output = File.OpenWrite($"{path}/Palette-{file.FileName}")) + // { + // image.Mutate(x => x.Quantize(KnownQuantizers.Palette)); + // image.Save(output, mimeType); + // } + // } + // } + //} + } + + private static IQuantizer GetQuantizer(string name) + { + PropertyInfo property = typeof(KnownQuantizers).GetTypeInfo().GetProperty(name); + return (IQuantizer) property.GetMethod.Invoke(null, new object[0]); } [Fact]