Skip to content

Commit

Permalink
Add a new option to preserve transparency
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Sep 23, 2021
1 parent 85da124 commit 4eaf9ba
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 6 deletions.
7 changes: 4 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static class ModuleInitializer
public static void Initialize()
{
VerifyDocNet.Initialize();
VerifyImageMagick.RegisterComparers(threshold: 0.05, ImageMagick.ErrorMetric.PerceptualHash);
VerifyImageMagick.RegisterComparers(threshold: 0.13, ImageMagick.ErrorMetric.PerceptualHash);
}
}
```
Expand All @@ -50,10 +50,11 @@ public static class ModuleInitializer
public Task VerifyPdf()
{
return Verifier.VerifyFile("sample.pdf")
.PreserveTransparency()
.PageDimensions(new(1080, 1920));
}
```
<sup><a href='/src/Tests/Samples.cs#L10-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifypdf' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Samples.cs#L10-L20' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifypdf' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -70,7 +71,7 @@ public Task VerifyPdfStream()
.PageDimensions(new(1080, 1920));
}
```
<sup><a href='/src/Tests/Samples.cs#L21-L31' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifypdfstream' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Samples.cs#L22-L32' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifypdfstream' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
2 changes: 1 addition & 1 deletion src/Tests/ModuleInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public static class ModuleInitializer
public static void Initialize()
{
VerifyDocNet.Initialize();
VerifyImageMagick.RegisterComparers(threshold: 0.05, ImageMagick.ErrorMetric.PerceptualHash);
VerifyImageMagick.RegisterComparers(threshold: 0.13, ImageMagick.ErrorMetric.PerceptualHash);
}
}
Binary file modified src/Tests/Samples.VerifyPdf.00.verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/Tests/Samples.VerifyPdf.01.verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/Tests/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Samples
public Task VerifyPdf()
{
return Verifier.VerifyFile("sample.pdf")
.PreserveTransparency()
.PageDimensions(new(1080, 1920));
}

Expand Down
21 changes: 21 additions & 0 deletions src/Verify.DocNet/VerifyDocNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,26 @@ static PageDimensions GetPageDimensions(this IReadOnlyDictionary<string, object>

return pageDimensions;
}

public static void PreserveTransparency(this VerifySettings settings)
{
settings.Context["VerifyDocNetPreserveTransparency"] = true;
}

public static SettingsTask PreserveTransparency(this SettingsTask settings)
{
settings.CurrentSettings.PreserveTransparency();
return settings;
}

static bool GetPreserveTransparency(this IReadOnlyDictionary<string, object> settings, bool preserveTransparency)
{
if (settings.TryGetValue("VerifyDocNetPreserveTransparency", out var value))
{
return (bool)value;
}

return preserveTransparency;
}
}
}
5 changes: 3 additions & 2 deletions src/Verify.DocNet/VerifyDocNet_Pdf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ static ConversionResult Convert(IDocReader document, IReadOnlyDictionary<string,
return new(null, targets);
}

static NaiveTransparencyRemover transparencyRemover = new();
static readonly NaiveTransparencyRemover TransparencyRemover = new();

static IEnumerable<Target> GetStreams(IDocReader document, IReadOnlyDictionary<string, object> settings)
{
var pagesToInclude = settings.GetPagesToInclude(document.GetPageCount());
var preserveTransparency = settings.GetPreserveTransparency(false);
for (var index = 0; index < pagesToInclude; index++)
{
using var reader = document.GetPageReader(index);

var rawBytes = reader.GetImage(transparencyRemover);
var rawBytes = preserveTransparency ? reader.GetImage() : reader.GetImage(TransparencyRemover);

var width = reader.GetPageWidth();
var height = reader.GetPageHeight();
Expand Down

0 comments on commit 4eaf9ba

Please sign in to comment.