Skip to content

Commit 61c25e4

Browse files
committed
Handle Image Mask rendering
1 parent 1d04286 commit 61c25e4

File tree

8 files changed

+64
-16
lines changed

8 files changed

+64
-16
lines changed
Binary file not shown.
222 KB
Loading
-483 Bytes
Loading

UglyToad.PdfPig.Rendering.Skia.Tests/PdfToImageHelper.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,14 @@ public static bool TestSinglePage(string pdfFile, int pageNumber, string expecte
215215
bim3.Encode(fs, SKEncodedImageFormat.Png, 100);
216216
}
217217

218+
string renderToSaveFile = Path.Combine(_errorFolder, $"{rootName}_{pageNumber}_rendered.png");
219+
220+
Directory.CreateDirectory(Path.GetDirectoryName(renderToSaveFile));
221+
using (var fs = new FileStream(renderToSaveFile, FileMode.Create))
222+
{
223+
actual.Encode(fs, SKEncodedImageFormat.Png, 100);
224+
}
225+
218226
return false;
219227
}
220228
}

UglyToad.PdfPig.Rendering.Skia.Tests/TestRendering.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ public class TestRendering
9494
{
9595
// These are not perfect yet and can be updated once the rendering is improved
9696
new object[]
97+
{
98+
// Output image is wrong - but renders JPX image
99+
"1_1.png",
100+
"1.pdf", 1, 2
101+
},
102+
new object[]
97103
{
98104
// Output image is wrong - but renders JPX image
99105
"68-1990-01_A_1.png",
@@ -401,7 +407,7 @@ public void TestAtScale(string expectedImage, string pdfFile, int pageNumber, in
401407
public void PdfPigSkiaTest(string expectedImage, string pdfFile, int pageNumber, int scale)
402408
{
403409
#if DEBUG
404-
//throw new System.ArgumentException("PdfPigSkiaTest needs to run in Release mode.");
410+
throw new System.ArgumentException("PdfPigSkiaTest needs to run in Release mode.");
405411
#endif
406412

407413
expectedImage = Path.Combine("pdfpig_skia", expectedImage);

UglyToad.PdfPig.Rendering.Skia.Tests/UglyToad.PdfPig.Rendering.Skia.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
</ItemGroup>
2727

2828
<ItemGroup>
29+
<None Update="Documents\1.pdf">
30+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
31+
</None>
2932
<None Update="Documents\11194059_2017-11_de_s.pdf">
3033
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3134
</None>
@@ -296,6 +299,9 @@
296299
<None Update="ExpectedImages\pdfpig_skia\11194059_2017-11_de_s_1.png">
297300
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
298301
</None>
302+
<None Update="ExpectedImages\pdfpig_skia\1_1.png">
303+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
304+
</None>
299305
<None Update="ExpectedImages\pdfpig_skia\2108.11480_1.png">
300306
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
301307
</None>

UglyToad.PdfPig.Rendering.Skia/SkiaStreamProcessor.Image.cs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,53 @@ private void RenderImage(IPdfImage image)
4949

5050
try
5151
{
52-
if (CurrentTransformationMatrix.A > 0 && CurrentTransformationMatrix.D > 0)
52+
53+
using (new SKAutoCanvasRestore(_canvas, true))
54+
using (var skImage = image.GetSKImage())
5355
{
54-
// No transformation to do
55-
using (var bitmap = image.GetSKImage())
56+
if (!(CurrentTransformationMatrix.A > 0) || !(CurrentTransformationMatrix.D > 0))
5657
{
57-
_canvas.DrawImage(bitmap, destRect, _paintCache.GetAntialiasing());
58+
var matrix = SKMatrix.CreateScale(Math.Sign(CurrentTransformationMatrix.A), Math.Sign(CurrentTransformationMatrix.D));
59+
60+
_canvas.SetMatrix(matrix);
61+
destRect = matrix.MapRect(destRect);
5862
}
59-
}
60-
else
61-
{
62-
SKMatrix matrix = SKMatrix.CreateScale(
63-
Math.Sign(CurrentTransformationMatrix.A),
64-
Math.Sign(CurrentTransformationMatrix.D));
6563

66-
using (var bitmap = image.GetSKImage())
67-
using (new SKAutoCanvasRestore(_canvas, true))
64+
if (!image.IsImageMask)
6865
{
69-
_canvas.SetMatrix(matrix);
70-
_canvas.DrawImage(bitmap, matrix.MapRect(destRect), _paintCache.GetAntialiasing());
66+
_canvas.DrawImage(skImage, destRect, _paintCache.GetAntialiasing());
67+
}
68+
else
69+
{
70+
// Draw image mask
71+
var colour = GetCurrentState().CurrentNonStrokingColor.ToSKColor(1);
72+
73+
byte refByte = image.Decode.Count == 2 &&
74+
(int)image.Decode[0] == 1 &&
75+
(int)image.Decode[1] == 0 ? byte.MaxValue : byte.MinValue;
76+
77+
using (var alphaMask = new SKBitmap(skImage.Width, skImage.Height, SKColorType.Bgra8888, SKAlphaType.Premul))
78+
{
79+
var span = skImage.PeekPixels().GetPixelSpan();
80+
81+
for (int y = 0; y < skImage.Height; y++)
82+
{
83+
for (int x = 0; x < skImage.Width; x++)
84+
{
85+
byte pixel = span[(y * skImage.Width) + x];
86+
if (pixel == refByte)
87+
{
88+
alphaMask.SetPixel(x, y, colour);
89+
}
90+
else
91+
{
92+
alphaMask.SetPixel(x, y, SKColors.Transparent);
93+
}
94+
}
95+
}
96+
97+
_canvas.DrawBitmap(alphaMask, destRect, _paintCache.GetAntialiasing());
98+
}
7199
}
72100
}
73101

UglyToad.PdfPig.Rendering.Skia/UglyToad.PdfPig.Rendering.Skia.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net462;net471;net6.0;net8.0</TargetFrameworks>
55
<LangVersion>12</LangVersion>
6-
<Version>0.1.10.2</Version>
6+
<Version>0.1.10.3</Version>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
</PropertyGroup>
99

0 commit comments

Comments
 (0)