Browse Source

BlackWhite, ColorBlindness, Grayscale

af/merge-core
James Jackson-South 9 years ago
parent
commit
fe040bbdac
  1. 2
      tests/ImageSharp.Tests/FileTestBase.cs
  2. 46
      tests/ImageSharp.Tests/Processing/ColorMatrix/BlackWhiteTest.cs
  3. 60
      tests/ImageSharp.Tests/Processing/ColorMatrix/ColorBlindnessTest.cs
  4. 31
      tests/ImageSharp.Tests/Processing/ColorMatrix/GrayscaleTest.cs
  5. 47
      tests/ImageSharp.Tests/Processors/Filters/BlackWhiteTest.cs
  6. 64
      tests/ImageSharp.Tests/Processors/Filters/ColorBlindnessTest.cs
  7. 8
      tests/ImageSharp.Tests/TestUtilities/Attributes/WithTestPatternImageAttribute.cs
  8. 20
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

2
tests/ImageSharp.Tests/FileTestBase.cs

@ -35,7 +35,7 @@ namespace ImageSharp.Tests
/// <summary>
/// The standard pixel formats enumerations
/// </summary>
public const PixelTypes StandardPixelTypes = PixelTypes.StandardImageClass | PixelTypes.Rgba32 | PixelTypes.Argb32;
public const PixelTypes StandardPixelTypes = PixelTypes.StandardImageClass | PixelTypes.Rgba32;
public static class Extensions
{

46
tests/ImageSharp.Tests/Processing/ColorMatrix/BlackWhiteTest.cs

@ -0,0 +1,46 @@
// <copyright file="BlackWhiteTest.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests.Processing.ColorMatrix
{
using ImageSharp.PixelFormats;
using Xunit;
public class BlackWhiteTest : FileTestBase
{
[Fact]
[WithFileCollection(nameof(AllBmpFiles), StandardPixelTypes)]
public void ImageShouldApplyBlackWhiteFilter<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.BlackWhite()
.DebugSave(provider, null, Extensions.Bmp);
}
}
[Fact]
[WithFileCollection(nameof(AllBmpFiles), StandardPixelTypes)]
public void ImageShouldApplyBlackWhiteFilterInBox<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> source = provider.GetImage())
using (var image = new Image<TPixel>(source))
{
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
image.BlackWhite(bounds)
.DebugSave(provider, null, Extensions.Bmp);
// Draw identical shapes over the bounded and compare to ensure changes are constrained.
image.Fill(NamedColors<TPixel>.HotPink, bounds);
source.Fill(NamedColors<TPixel>.HotPink, bounds);
ImageComparer.CheckSimilarity(image, source);
}
}
}
}

60
tests/ImageSharp.Tests/Processing/ColorMatrix/ColorBlindnessTest.cs

@ -0,0 +1,60 @@
// <copyright file="ColorBlindnessTest.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests.Processing.ColorMatrix
{
using ImageSharp.PixelFormats;
using ImageSharp.Processing;
using Xunit;
public class ColorBlindnessTest : FileTestBase
{
public static readonly TheoryData<ColorBlindness> ColorBlindnessFilters
= new TheoryData<ColorBlindness>
{
ColorBlindness.Achromatomaly,
ColorBlindness.Achromatopsia,
ColorBlindness.Deuteranomaly,
ColorBlindness.Deuteranopia,
ColorBlindness.Protanomaly,
ColorBlindness.Protanopia,
ColorBlindness.Tritanomaly,
ColorBlindness.Tritanopia
};
[Theory]
[WithFileCollection(nameof(AllBmpFiles), nameof(ColorBlindnessFilters), StandardPixelTypes)]
public void ImageShouldApplyColorBlindnessFilter<TPixel>(TestImageProvider<TPixel> provider, ColorBlindness colorBlindness)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.ColorBlindness(colorBlindness)
.DebugSave(provider, null, Extensions.Bmp);
}
}
[Theory]
[WithFileCollection(nameof(AllBmpFiles), nameof(ColorBlindnessFilters), StandardPixelTypes)]
public void ImageShouldApplyColorBlindnessFilterInBox<TPixel>(TestImageProvider<TPixel> provider, ColorBlindness colorBlindness)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> source = provider.GetImage())
using (var image = new Image<TPixel>(source))
{
var bounds = new Rectangle(10, 10, image.Width / 2, image.Height / 2);
image.ColorBlindness(colorBlindness, bounds)
.DebugSave(provider, null, Extensions.Bmp);
// Draw identical shapes over the bounded and compare to ensure changes are constrained.
image.Fill(NamedColors<TPixel>.HotPink, bounds);
source.Fill(NamedColors<TPixel>.HotPink, bounds);
ImageComparer.CheckSimilarity(image, source);
}
}
}
}

31
tests/ImageSharp.Tests/Processors/Filters/GrayscaleTest.cs → tests/ImageSharp.Tests/Processing/ColorMatrix/GrayscaleTest.cs

@ -3,21 +3,27 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
namespace ImageSharp.Tests.Processing.ColorMatrix
{
using Xunit;
using ImageSharp.PixelFormats;
using ImageSharp.Processing;
using ImageSharp.PixelFormats;
using Xunit;
public class GrayscaleTest : FileTestBase
{
public static readonly TheoryData<GrayscaleMode> GrayscaleModeTypes
= new TheoryData<GrayscaleMode>
{
GrayscaleMode.Bt601,
GrayscaleMode.Bt709
};
/// <summary>
/// Use test patterns over loaded images to save decode time.
/// </summary>
[Theory]
[WithTestPatternImages(50, 50, PixelTypes.StandardImageClass, GrayscaleMode.Bt709)]
[WithTestPatternImages(50, 50, PixelTypes.StandardImageClass, GrayscaleMode.Bt601)]
[WithTestPatternImages(nameof(GrayscaleModeTypes), 50, 50, StandardPixelTypes)]
public void ImageShouldApplyGrayscaleFilterAll<TPixel>(TestImageProvider<TPixel> provider, GrayscaleMode value)
where TPixel : struct, IPixel<TPixel>
{
@ -37,21 +43,20 @@ namespace ImageSharp.Tests
}
[Theory]
[WithTestPatternImages(50, 50, PixelTypes.StandardImageClass, GrayscaleMode.Bt709)]
[WithTestPatternImages(50, 50, PixelTypes.StandardImageClass, GrayscaleMode.Bt601)]
[WithTestPatternImages(nameof(GrayscaleModeTypes), 50, 50, StandardPixelTypes)]
public void ImageShouldApplyGrayscaleFilterInBox<TPixel>(TestImageProvider<TPixel> provider, GrayscaleMode value)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> source = provider.GetImage())
using (Image<TPixel> image = new Image<TPixel>(source))
using (var image = new Image<TPixel>(source))
{
Rectangle rect = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2);
image.Grayscale(rect, value)
var bounds = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2);
image.Grayscale(bounds, value)
.DebugSave(provider, value.ToString());
// Let's draw identical shapes over the greyed areas and ensure that it didn't change the outer area
image.Fill(NamedColors<TPixel>.HotPink, rect);
source.Fill(NamedColors<TPixel>.HotPink, rect);
// Draw identical shapes over the bounded and compare to ensure changes are constrained.
image.Fill(NamedColors<TPixel>.HotPink, bounds);
source.Fill(NamedColors<TPixel>.HotPink, bounds);
ImageComparer.CheckSimilarity(image, source);
}
}

47
tests/ImageSharp.Tests/Processors/Filters/BlackWhiteTest.cs

@ -1,47 +0,0 @@
// <copyright file="BlackWhiteTest.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.IO;
using ImageSharp.PixelFormats;
using Xunit;
public class BlackWhiteTest : FileTestBase
{
[Fact]
public void ImageShouldApplyBlackWhiteFilter()
{
string path = this.CreateOutputDirectory("BlackWhite");
foreach (TestFile file in Files)
{
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{file.FileName}"))
{
image.BlackWhite().Save(output);
}
}
}
[Fact]
public void ImageShouldApplyBlackWhiteFilterInBox()
{
string path = this.CreateOutputDirectory("BlackWhite");
foreach (TestFile file in Files)
{
string filename = file.GetFileName("-InBox");
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
image.BlackWhite(new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output);
}
}
}
}
}

64
tests/ImageSharp.Tests/Processors/Filters/ColorBlindnessTest.cs

@ -1,64 +0,0 @@
// <copyright file="ColorBlindnessTest.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 ImageSharp.Processing;
using System.IO;
using ImageSharp.PixelFormats;
using Xunit;
public class ColorBlindnessTest : FileTestBase
{
public static readonly TheoryData<ColorBlindness> ColorBlindnessFilters
= new TheoryData<ColorBlindness>
{
ColorBlindness.Achromatomaly,
ColorBlindness.Achromatopsia,
ColorBlindness.Deuteranomaly,
ColorBlindness.Deuteranopia,
ColorBlindness.Protanomaly,
ColorBlindness.Protanopia,
ColorBlindness.Tritanomaly,
ColorBlindness.Tritanopia
};
[Theory]
[MemberData(nameof(ColorBlindnessFilters))]
public void ImageShouldApplyColorBlindnessFilter(ColorBlindness colorBlindness)
{
string path = this.CreateOutputDirectory("ColorBlindness");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(colorBlindness);
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
image.ColorBlindness(colorBlindness).Save(output);
}
}
}
[Theory]
[MemberData(nameof(ColorBlindnessFilters))]
public void ImageShouldApplyBrightnessFilterInBox(ColorBlindness colorBlindness)
{
string path = this.CreateOutputDirectory("ColorBlindness");
foreach (TestFile file in Files)
{
string filename = file.GetFileName(colorBlindness + "-InBox");
using (Image<Rgba32> image = file.CreateImage())
using (FileStream output = File.OpenWrite($"{path}/{filename}"))
{
image.ColorBlindness(colorBlindness, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output);
}
}
}
}
}

8
tests/ImageSharp.Tests/TestUtilities/Attributes/WithTestPatternImageAttribute.cs

@ -29,6 +29,7 @@ namespace ImageSharp.Tests
/// <summary>
/// Triggers passing an <see cref="TestImageProvider{TPixel}"/> that produces a test pattern image of size width * height
/// </summary>
/// <param name="memberData">The member data to apply to theories</param>
/// <param name="width">The required width</param>
/// <param name="height">The required height</param>
/// <param name="pixelTypes">The requested parameter</param>
@ -40,7 +41,14 @@ namespace ImageSharp.Tests
this.Height = height;
}
/// <summary>
/// Gets the width
/// </summary>
public int Width { get; }
/// <summary>
/// Gets the height
/// </summary>
public int Height { get; }
protected override string GetFactoryMethodName(MethodInfo testMethod) => "TestPattern";

20
tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

@ -1,4 +1,8 @@

// <copyright file="TestImageExtensions.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;
@ -21,8 +25,15 @@ namespace ImageSharp.Tests
public static void DebugSave<TPixel>(this Image<TPixel> image, ITestImageProvider provider, object settings = null, string extension = "png")
where TPixel : struct, IPixel<TPixel>
{
if (bool.TryParse(Environment.GetEnvironmentVariable("CI"), out bool isCi) && isCi)
{
return;
}
// We are running locally then we want to save it out
string tag = null;
string s = settings as string;
if (s != null)
{
tag = s;
@ -40,11 +51,8 @@ namespace ImageSharp.Tests
tag = string.Join("_", properties.ToDictionary(x => x.Name, x => x.GetValue(settings)).Select(x => $"{x.Key}-{x.Value}"));
}
}
if (!bool.TryParse(Environment.GetEnvironmentVariable("CI"), out bool isCi) || !isCi)
{
// We are running locally then we want to save it out
provider.Utility.SaveTestOutputFile(image, extension, tag: tag);
}
provider.Utility.SaveTestOutputFile(image, extension, tag: tag);
}
}
}

Loading…
Cancel
Save