changes required to autmatticaly test multiple colorspaces without significant rework for current tests.
Created
December 17, 2016 13:20
-
-
Save tocsoft/45be962c43b157ecdab040cf0907b8fd to your computer and use it in GitHub Desktop.
GenericxUnit tests for multiple color spaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // <copyright file="GeneralFormatTests.cs" company="James Jackson-South"> | |
| // Copyright (c) James Jackson-South and contributors. | |
| // Licensed under the Apache License, Version 2.0. | |
| // </copyright> | |
| namespace ImageSharp.Tests | |
| { | |
| using System; | |
| using System.IO; | |
| using System.Numerics; | |
| using Xunit; | |
| public class GeneralFormatTests_Color_uint : GeneralFormatTests<Color, uint> { } | |
| public class GeneralFormatTests_Alpha8_byte : GeneralFormatTests<Alpha8, byte> { } | |
| public abstract class GeneralFormatTests<TColor, TPacked> : FileTestBase | |
| where TColor : struct, IPackedPixel<TPacked> where TPacked : struct | |
| { | |
| [Fact] | |
| public void ResolutionShouldChange() | |
| { | |
| string path = CreateOutputDirectory("Resolution", typeof(TColor).Name); | |
| foreach (TestFile file in Files) | |
| { | |
| Image<TColor, TPacked> image = file.CreateImage<TColor, TPacked>(); | |
| using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) | |
| { | |
| image.VerticalResolution = 150; | |
| image.HorizontalResolution = 150; | |
| image.Save(output); | |
| } | |
| } | |
| } | |
| [Fact] | |
| public void ImageCanEncodeToString() | |
| { | |
| string path = CreateOutputDirectory("ToString", typeof(TColor).Name); | |
| foreach (TestFile file in Files) | |
| { | |
| Image<TColor, TPacked> image = file.CreateImage<TColor, TPacked>(); | |
| string filename = path + "/" + file.FileNameWithoutExtension + ".txt"; | |
| File.WriteAllText(filename, image.ToBase64String()); | |
| } | |
| } | |
| [Fact] | |
| public void DecodeThenEncodeImageFromStreamShouldSucceed() | |
| { | |
| string path = CreateOutputDirectory("Encode", typeof(TColor).Name); | |
| foreach (TestFile file in Files) | |
| { | |
| Image<TColor, TPacked> image = file.CreateImage<TColor, TPacked>(); | |
| //var image = file.CreateImage() | |
| // .To<Bgr565, ushort>() | |
| // .To<Bgra4444, ushort>() | |
| // .To<Bgra5551, ushort>() | |
| // .To<Byte4, uint>() | |
| // .To<HalfSingle, ushort>() | |
| // .To<HalfVector2, uint>() | |
| // .To<HalfVector4, ulong>() | |
| // .To<Rg32, uint>() | |
| // .To<Rgba1010102, uint>() | |
| // .To<NormalizedByte2, ushort>() | |
| // .To<NormalizedByte4, uint>() | |
| // .To<NormalizedShort2, uint>() | |
| // .To<NormalizedShort4, ulong>() | |
| // .To<Short2, uint>() | |
| // .To<Short4, ulong>(); | |
| // Image<Bgr565, ushort> image = file.CreateImage().To<Bgr565, ushort>(); | |
| // Image<Bgra4444, ushort> image = file.CreateImage().To<Bgra4444, ushort>(); | |
| // Image<Bgra5551, ushort> image = file.CreateImage().To<Bgra5551, ushort>(); | |
| // Image<Byte4, uint> image = file.CreateImage().To<Byte4, uint>(); | |
| // Image<HalfSingle, ushort> image = file.CreateImage().To<HalfSingle, ushort>(); | |
| // Image<HalfVector2, uint> image = file.CreateImage().To<HalfVector2, uint>(); | |
| // Image<HalfVector4, ulong> image = file.CreateImage().To<HalfVector4, ulong>(); | |
| // Image<Rg32, uint> image = file.CreateImage().To<Rg32, uint>(); | |
| // Image<Rgba1010102, uint> image = file.CreateImage().To<Rgba1010102, uint>(); | |
| // Image<Rgba64, ulong> image = file.CreateImage().To<Rgba64, ulong>(); | |
| // Image<NormalizedByte2, ushort> image = file.CreateImage().To<NormalizedByte2, ushort>(); | |
| // Image<NormalizedByte4, uint> image = file.CreateImage().To<NormalizedByte4, uint>(); | |
| // Image<NormalizedShort2, uint> image = file.CreateImage().To<NormalizedShort2, uint>(); | |
| // Image<NormalizedShort4, ulong> image = file.CreateImage().To<NormalizedShort4, ulong>(); | |
| // Image<Short2, uint> image = file.CreateImage().To<Short2, uint>(); | |
| // Image<Short4, ulong> image = file.CreateImage().To<Short4, ulong>(); | |
| using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) | |
| { | |
| image.Save(output); | |
| } | |
| } | |
| } | |
| [Fact] | |
| public void QuantizeImageShouldPreserveMaximumColorPrecision() | |
| { | |
| string path = CreateOutputDirectory("Quantize", typeof(TColor).Name); | |
| foreach (TestFile file in Files) | |
| { | |
| Image<TColor, TPacked> image = file.CreateImage<TColor, TPacked>(); | |
| // Copy the original pixels to save decoding time. | |
| TColor[] pixels = new TColor[image.Width * image.Height]; | |
| Array.Copy(image.Pixels, pixels, image.Pixels.Length); | |
| using (FileStream output = File.OpenWrite($"{path}/Octree-{file.FileName}")) | |
| { | |
| image.Quantize(Quantization.Octree) | |
| .Save(output, image.CurrentImageFormat); | |
| } | |
| image.SetPixels(image.Width, image.Height, pixels); | |
| using (FileStream output = File.OpenWrite($"{path}/Wu-{file.FileName}")) | |
| { | |
| image.Quantize(Quantization.Wu) | |
| .Save(output, image.CurrentImageFormat); | |
| } | |
| image.SetPixels(image.Width, image.Height, pixels); | |
| using (FileStream output = File.OpenWrite($"{path}/Palette-{file.FileName}")) | |
| { | |
| image.Quantize(Quantization.Palette) | |
| .Save(output, image.CurrentImageFormat); | |
| } | |
| } | |
| } | |
| [Fact] | |
| public void ImageCanConvertFormat() | |
| { | |
| string path = CreateOutputDirectory("Format", typeof(TColor).Name); | |
| foreach (TestFile file in Files) | |
| { | |
| Image<TColor, TPacked> image = file.CreateImage<TColor, TPacked>(); | |
| using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.gif")) | |
| { | |
| image.SaveAsGif(output); | |
| } | |
| using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.bmp")) | |
| { | |
| image.SaveAsBmp(output); | |
| } | |
| using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.jpg")) | |
| { | |
| image.SaveAsJpeg(output); | |
| } | |
| using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.png")) | |
| { | |
| image.SaveAsPng(output); | |
| } | |
| } | |
| } | |
| [Fact] | |
| public void ImageShouldPreservePixelByteOrderWhenSerialized() | |
| { | |
| string path = CreateOutputDirectory("Serialized", typeof(TColor).Name); | |
| foreach (TestFile file in Files) | |
| { | |
| Image<TColor, TPacked> image = file.CreateImage<TColor, TPacked>(); | |
| byte[] serialized; | |
| using (MemoryStream memoryStream = new MemoryStream()) | |
| { | |
| image.Save(memoryStream); | |
| memoryStream.Flush(); | |
| serialized = memoryStream.ToArray(); | |
| } | |
| using (MemoryStream memoryStream = new MemoryStream(serialized)) | |
| { | |
| Image<TColor, TPacked> image2 = new Image<TColor, TPacked>(memoryStream); | |
| using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) | |
| { | |
| image2.Save(output); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // <copyright file="TestImage.cs" company="James Jackson-South"> | |
| // Copyright (c) James Jackson-South and contributors. | |
| // Licensed under the Apache License, Version 2.0. | |
| // </copyright> | |
| using System.IO; | |
| namespace ImageSharp.Tests | |
| { | |
| public class TestFile | |
| { | |
| private readonly byte[] data; | |
| private readonly string file; | |
| public TestFile(string file) | |
| { | |
| this.file = file; | |
| using (FileStream stream = File.OpenRead(file)) | |
| { | |
| using (var ms = new MemoryStream()) | |
| { | |
| stream.CopyTo(ms); | |
| data = ms.ToArray(); | |
| } | |
| } | |
| } | |
| public string FileName | |
| { | |
| get | |
| { | |
| return Path.GetFileName(this.file); | |
| } | |
| } | |
| public string FileNameWithoutExtension | |
| { | |
| get | |
| { | |
| return Path.GetFileNameWithoutExtension(this.file); | |
| } | |
| } | |
| public string GetFileName(object value) | |
| { | |
| return this.FileNameWithoutExtension + "-" + value + Path.GetExtension(this.file); | |
| } | |
| public string GetFileNameWithoutExtension(object value) | |
| { | |
| return this.FileNameWithoutExtension + "-" + value; | |
| } | |
| public Image CreateImage() | |
| { | |
| using (var stream = new MemoryStream(data)) | |
| { | |
| return new Image(stream); | |
| } | |
| } | |
| public Image<TColor, TPacked> CreateImage<TColor, TPacked>() where TColor : struct, IPackedPixel<TPacked> where TPacked : struct | |
| { | |
| using (var stream = new MemoryStream(data)) | |
| { | |
| return new Image<TColor, TPacked>(stream); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment