|
|
@ -13,7 +13,9 @@ using Xunit; |
|
|
namespace SixLabors.ImageSharp.Tests |
|
|
namespace SixLabors.ImageSharp.Tests |
|
|
{ |
|
|
{ |
|
|
using System; |
|
|
using System; |
|
|
|
|
|
using System.Reflection; |
|
|
|
|
|
|
|
|
|
|
|
using SixLabors.ImageSharp.Memory; |
|
|
using SixLabors.ImageSharp.Processing; |
|
|
using SixLabors.ImageSharp.Processing; |
|
|
using SixLabors.ImageSharp.Processing.Quantization; |
|
|
using SixLabors.ImageSharp.Processing.Quantization; |
|
|
|
|
|
|
|
|
@ -61,44 +63,72 @@ namespace SixLabors.ImageSharp.Tests |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[Fact] |
|
|
public static readonly TheoryData<string> QuantizerNames = |
|
|
public void QuantizeImageShouldPreserveMaximumColorPrecision() |
|
|
new TheoryData<string> |
|
|
{ |
|
|
|
|
|
string path = TestEnvironment.CreateOutputDirectory("Quantize"); |
|
|
|
|
|
|
|
|
|
|
|
foreach (TestFile file in Files) |
|
|
|
|
|
{ |
|
|
|
|
|
using (Image<Rgba32> srcImage = Image.Load<Rgba32>(file.Bytes, out var mimeType)) |
|
|
|
|
|
{ |
|
|
{ |
|
|
using (Image<Rgba32> image = srcImage.Clone()) |
|
|
nameof(KnownQuantizers.Octree), |
|
|
{ |
|
|
nameof(KnownQuantizers.Palette), |
|
|
using (FileStream output = File.OpenWrite($"{path}/Octree-{file.FileName}")) |
|
|
nameof(KnownQuantizers.Wu) |
|
|
{ |
|
|
}; |
|
|
image.Mutate(x => x.Quantize(KnownQuantizers.Octree)); |
|
|
|
|
|
image.Save(output, mimeType); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
[Theory] |
|
|
} |
|
|
[WithFile(TestImages.Png.CalliphoraPartial, nameof(QuantizerNames), PixelTypes.Rgba32)] |
|
|
|
|
|
[WithFile(TestImages.Png.Bike, nameof(QuantizerNames), PixelTypes.Rgba32)] |
|
|
|
|
|
public void QuantizeImageShouldPreserveMaximumColorPrecision<TPixel>(TestImageProvider<TPixel> provider, string quantizerName) |
|
|
|
|
|
where TPixel:struct,IPixel<TPixel> |
|
|
|
|
|
{ |
|
|
|
|
|
provider.Configuration.MemoryManager = ArrayPoolMemoryManager.CreateWithModeratePooling(); |
|
|
|
|
|
|
|
|
using (Image<Rgba32> image = srcImage.Clone()) |
|
|
IQuantizer quantizer = GetQuantizer(quantizerName); |
|
|
{ |
|
|
|
|
|
using (FileStream output = File.OpenWrite($"{path}/Wu-{file.FileName}")) |
|
|
|
|
|
{ |
|
|
|
|
|
image.Mutate(x => x.Quantize(KnownQuantizers.Wu)); |
|
|
|
|
|
image.Save(output, mimeType); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
using (Image<Rgba32> image = srcImage.Clone()) |
|
|
using (Image<TPixel> image = provider.GetImage()) |
|
|
{ |
|
|
{ |
|
|
using (FileStream output = File.OpenWrite($"{path}/Palette-{file.FileName}")) |
|
|
image.Mutate(c => c.Quantize(quantizer)); |
|
|
{ |
|
|
image.DebugSave(provider); |
|
|
image.Mutate(x => x.Quantize(KnownQuantizers.Palette)); |
|
|
|
|
|
image.Save(output, mimeType); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
provider.Configuration.MemoryManager.ReleaseRetainedResources(); |
|
|
|
|
|
|
|
|
|
|
|
//string path = TestEnvironment.CreateOutputDirectory("Quantize");
|
|
|
|
|
|
|
|
|
|
|
|
//foreach (TestFile file in Files)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// using (Image<Rgba32> srcImage = Image.Load<Rgba32>(file.Bytes, out IImageFormat mimeType))
|
|
|
|
|
|
// {
|
|
|
|
|
|
// using (Image<Rgba32> 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<Rgba32> 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<Rgba32> 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] |
|
|
[Fact] |
|
|
|