Skip to content

Commit c9dc3b0

Browse files
committed
Make SkiaImageExtensions public to allow conversion of PdfPig pdf images to SkiaSharp images
1 parent 83c46d0 commit c9dc3b0

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2024 BobLd
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License").
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System.IO;
16+
using UglyToad.PdfPig.Rendering.Skia.Helpers;
17+
using Xunit;
18+
19+
namespace UglyToad.PdfPig.Rendering.Skia.Tests
20+
{
21+
public class RenderImagesTests
22+
{
23+
24+
[Fact]
25+
public void PdfPigImageAsSkiaImages()
26+
{
27+
using (var document = PdfDocument.Open(Path.Combine("SpecificTestDocuments", "Shadows.at.Sundown.-.Lvl.11_removed.pdf"), SkiaRenderingParsingOptions.Instance))
28+
{
29+
var page = document.GetPage(1);
30+
foreach (var pdfImage in page.GetImages())
31+
{
32+
var skImage = pdfImage.GetSKImage();
33+
34+
Assert.NotNull(skImage);
35+
Assert.True(skImage.Width > 0);
36+
Assert.True(skImage.Height > 0);
37+
}
38+
}
39+
}
40+
}
41+
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424

2525
namespace UglyToad.PdfPig.Rendering.Skia.Helpers
2626
{
27-
internal static class SkiaImageExtensions
27+
/// <summary>
28+
/// SkiaSharp image extensions.
29+
/// </summary>
30+
public static class SkiaImageExtensions
2831
{
2932
private static bool IsValidColorSpace(IPdfImage pdfImage)
3033
{
@@ -56,7 +59,7 @@ private static bool HasAlphaChannel(this IPdfImage pdfImage)
5659
return pdfImage.MaskImage is not null || pdfImage.ImageDictionary.ContainsKey(NameToken.Mask);
5760
}
5861

59-
public static int GetRasterSize(this IPdfImage pdfImage)
62+
internal static int GetRasterSize(this IPdfImage pdfImage)
6063
{
6164
int width = pdfImage.WidthInSamples;
6265
int height = pdfImage.HeightInSamples;
@@ -379,6 +382,19 @@ private static bool TryGetGray8Bitmap(int width, int height, ReadOnlySpan<byte>
379382
return false;
380383
}
381384

385+
/// <summary>
386+
/// Converts the specified <see cref="IPdfImage"/> to an <see cref="SKImage"/> instance.
387+
/// </summary>
388+
/// <param name="pdfImage">The PDF image to convert.</param>
389+
/// <returns>
390+
/// An <see cref="SKImage"/> representation of the provided <paramref name="pdfImage"/>.
391+
/// If the conversion fails, a fallback mechanism is used to create the image from raw bytes.
392+
/// </returns>
393+
/// <exception cref="ArgumentNullException">Thrown if <paramref name="pdfImage"/> is <c>null</c>.</exception>
394+
/// <remarks>
395+
/// This method attempts to generate an <see cref="SKImage"/> using the image's data and color space.
396+
/// If the generation fails, it falls back to creating the image using encoded or raw byte data.
397+
/// </remarks>
382398
public static SKImage GetSKImage(this IPdfImage pdfImage)
383399
{
384400
if (pdfImage.TryGenerate(out var bitmap))

0 commit comments

Comments
 (0)