Skip to content

Commit 471e522

Browse files
committed
Handle SoftMask and use SKAlphaType.Unpremul for image
1 parent da076f1 commit 471e522

File tree

5 files changed

+130
-163
lines changed

5 files changed

+130
-163
lines changed
1.49 KB
Loading
-8.05 KB
Loading

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private static SKBitmap diffImages(SKBitmap bim1, SKBitmap bim2)
8282
bim3 = createEmptyDiffImage(minWidth, minHeight, maxWidth, maxHeight);
8383
}
8484

85-
bim3.SetPixel(x, y, new SKColor(rDiff, gDiff, bDiff));
85+
bim3.SetPixel(x, y, new SKColor(Math.Min((byte)125, rDiff), Math.Min((byte)125, gDiff), Math.Min((byte)125, bDiff)));
8686
}
8787
else
8888
{

UglyToad.PdfPig.Rendering.Skia/Helpers/SkiaExtensions.cs

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ public static SKRect ToSKRect(this PdfRectangle rect, float height)
7878
return new SKRect(left, top, right, bottom);
7979
}
8080

81+
public static SKRectI ToSKRectI(this PdfRectangle rect, float height)
82+
{
83+
double left = rect.Left;
84+
double top = height - rect.Top;
85+
double right = left + rect.Width;
86+
double bottom = top + rect.Height;
87+
return new SKRectI((int)left, (int)top, (int)right, (int)bottom); // TODO - rounding
88+
}
89+
8190
public static SKPoint ToSKPoint(this PdfPoint pdfPoint, float height)
8291
{
8392
return new SKPoint((float)pdfPoint.X, (float)(height - pdfPoint.Y));
@@ -221,24 +230,24 @@ public static SKPathFillType ToSKPathFillType(this FillingRule fillingRule)
221230

222231
public static SKColor ToSKColor(this IColor pdfColor, double alpha)
223232
{
224-
var color = SKColors.Black;
225233
if (pdfColor is not null)
226234
{
227235
var (r, g, b) = pdfColor.ToRGBValues();
228-
color = new SKColor(Convert.ToByte(r * 255), Convert.ToByte(g * 255), Convert.ToByte(b * 255));
236+
return new SKColor(Convert.ToByte(r * 255), Convert.ToByte(g * 255), Convert.ToByte(b * 255),
237+
Convert.ToByte(alpha * 255));
229238
}
230239

231-
return color.WithAlpha(Convert.ToByte(alpha * 255));
240+
return SKColors.Black.WithAlpha(Convert.ToByte(alpha * 255));
232241
}
233242

234243
public static SKMatrix ToSkMatrix(this TransformationMatrix transformationMatrix)
235244
{
236-
return new SKMatrix((float)transformationMatrix.A, (float)transformationMatrix.C, (float)transformationMatrix.E,
237-
(float)transformationMatrix.B, (float)transformationMatrix.D, (float)transformationMatrix.F,
238-
0, 0, 1);
245+
return new SKMatrix((float)transformationMatrix.A, (float)transformationMatrix.C,
246+
(float)transformationMatrix.E,
247+
(float)transformationMatrix.B, (float)transformationMatrix.D, (float)transformationMatrix.F,
248+
0, 0, 1);
239249
}
240250

241-
/*
242251
private static bool doBlending = false;
243252

244253
public static SKBlendMode ToSKBlendMode(this BlendMode blendMode)
@@ -248,12 +257,12 @@ public static SKBlendMode ToSKBlendMode(this BlendMode blendMode)
248257
return SKBlendMode.SrcOver;
249258
}
250259

260+
// https://pdfium.googlesource.com/pdfium/+/refs/heads/main/core/fxge/skia/fx_skia_device.cpp
251261
switch (blendMode)
252262
{
253-
// Standard separable blend modes
254-
case BlendMode.Normal:
255-
case BlendMode.Compatible:
256-
return SKBlendMode.SrcOver; // TODO - Check if correct
263+
// 11.3.5.2 Separable blend modes
264+
case BlendMode.Normal: // aka Compatible
265+
return SKBlendMode.SrcOver;
257266

258267
case BlendMode.Multiply:
259268
return SKBlendMode.Multiply;
@@ -288,7 +297,7 @@ public static SKBlendMode ToSKBlendMode(this BlendMode blendMode)
288297
case BlendMode.Exclusion:
289298
return SKBlendMode.Exclusion;
290299

291-
// Standard nonseparable blend modes
300+
// 11.3.5.3 Non-separable blend modes
292301
case BlendMode.Hue:
293302
return SKBlendMode.Hue;
294303

@@ -305,37 +314,5 @@ public static SKBlendMode ToSKBlendMode(this BlendMode blendMode)
305314
throw new NotImplementedException($"Cannot convert blend mode '{blendMode}' to SKBlendMode.");
306315
}
307316
}
308-
*/
309-
310-
/*
311-
public static void ApplySMask(this SKBitmap image, SKBitmap smask)
312-
{
313-
// What about 'Alpha source' flag?
314-
SKBitmap scaled;
315-
if (!image.Info.Rect.Equals(smask.Info.Rect))
316-
{
317-
scaled = new SKBitmap(image.Info);
318-
if (!smask.ScalePixels(scaled, SKFilterQuality.High))
319-
{
320-
// log
321-
}
322-
}
323-
else
324-
{
325-
scaled = smask;
326-
}
327-
328-
for (int x = 0; x < image.Width; x++)
329-
{
330-
for (int y = 0; y < image.Height; y++)
331-
{
332-
var pix = image.GetPixel(x, y);
333-
byte alpha = scaled.GetPixel(x, y).Red; // Gray CS (r = g = b)
334-
image.SetPixel(x, y, pix.WithAlpha(alpha));
335-
}
336-
}
337-
scaled.Dispose();
338-
}
339-
*/
340317
}
341318
}

0 commit comments

Comments
 (0)