Skip to content

VerifyTests/Verify.ImageSharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1,198 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verify.ImageSharp

Discussions Build status NuGet Status

Extends Verify to allow verification of documents via ImageSharp.

See Milestones for release notes.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

Developed using JetBrains IDEs

JetBrains logo.

NuGet

Usage

[ModuleInitializer]
public static void Init() =>
    VerifyImageSharp.Initialize();

snippet source | anchor

Verify a file

[Test]
public Task VerifyImageFile() =>
    VerifyFile("sample.jpg");

snippet source | anchor

[Test]
public Task VerifyImageFileWithCustomEncoder() =>
    VerifyFile("sample.jpg")
        .EncodeAsPng();

snippet source | anchor

Two files are produced

Info file:

Samples.VerifyImageFile.verified.txt

{
  Width: 1599,
  Height: 1066,
  HorizontalResolution: 1.0,
  VerticalResolution: 1.0
}

snippet source | anchor

Image file:

Samples.VerifyImageFile.verified.jpg

Verify an SixLabors.ImageSharp.Image

An instance if an SixLabors.ImageSharp.Image can be verified using the following:

[Test]
public Task VerifyImage()
{
    var image = new Image<Rgba32>(11, 11)
    {
        [5, 5] = Rgba32.ParseHex("#0000FF")
    };
    return Verify(image);
}

snippet source | anchor

SSIM Image Comparison

By default, image comparison is byte-exact. To tolerate minor rendering differences (anti-aliasing, font hinting, subpixel rendering), enable SSIM (Structural Similarity Index) comparison by passing a threshold to Initialize:

[ModuleInitializer]
public static void Init() =>
    VerifyImageSharp.Initialize(ssimThreshold: 0.999);

SSIM returns a value between 0.0 (completely different) and 1.0 (identical). Images with an SSIM at or above the threshold are considered equal. Recommended thresholds:

  • 0.999 — tolerates anti-aliasing and subpixel rendering differences
  • 0.995 — tolerates minor font/layout shifts across OS versions
  • 0.99 — tolerates moderate rendering variation

Per-test threshold

Override the global threshold for a specific test:

[Test]
public Task VerifyImageWithSsimThreshold()
{
    var image = new Image<Rgba32>(11, 11)
    {
        [5, 5] = Rgba32.ParseHex("#0000FF")
    };
    return Verify(image)
        .SsimThreshold(0.95);
}

snippet source | anchor

Direct SSIM calculation

The SsimComparer class can also be used directly to get the raw SSIM value:

double ssim = SsimComparer.Calculate(receivedStream, verifiedStream);

File Samples

http://file-examples.com/

Icon

Swirl designed by BÖCK, RU from The Noun Project.

About

Extends Verify to allow verification of documents via ImageSharp.

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Contributors

Languages