|
1 |
| -using PdfLibCore; |
2 |
| -using SixLabors.ImageSharp; |
3 |
| -using SixLabors.ImageSharp.Formats; |
4 |
| -using SixLabors.ImageSharp.PixelFormats; |
| 1 | +namespace ImageSharpCommunity.Formats.Pdf; |
5 | 2 |
|
6 |
| -namespace ImageSharpCommunity.Formats.Pdf |
| 3 | +public class PdfDecoder : SpecializedImageDecoder<PdfDecoderOptions> |
7 | 4 | {
|
8 |
| - public class PdfDecoder : SpecializedImageDecoder<PdfDecoderOptions> |
| 5 | + private PdfDecoder() |
9 | 6 | {
|
10 |
| - private PdfDecoder() |
11 |
| - { |
12 |
| - } |
13 |
| - |
14 |
| - /// <summary> |
15 |
| - /// Gets the current instance. |
16 |
| - /// </summary> |
17 |
| - public static PdfDecoder Instance { get; } = new PdfDecoder(); |
18 |
| - |
19 |
| - protected override PdfDecoderOptions CreateDefaultSpecializedOptions(DecoderOptions options) => new() { GeneralOptions = options }; |
20 |
| - |
21 |
| - protected override Image<TPixel> Decode<TPixel>(PdfDecoderOptions options, Stream stream, CancellationToken cancellationToken) |
22 |
| - { |
23 |
| - ArgumentNullException.ThrowIfNull(options, nameof(options)); |
24 |
| - ArgumentNullException.ThrowIfNull(stream, nameof(stream)); |
25 |
| - |
26 |
| - using var pdfDocument = new PdfDocument(stream); |
27 |
| - var page = pdfDocument?.Pages?.FirstOrDefault(); |
| 7 | + } |
28 | 8 |
|
29 |
| - TryGetWidthAndHeight(page, out var width, out var height); |
| 9 | + /// <summary> |
| 10 | + /// Gets the current instance. |
| 11 | + /// </summary> |
| 12 | + public static PdfDecoder Instance { get; } = new PdfDecoder(); |
30 | 13 |
|
31 |
| - ArgumentNullException.ThrowIfNull(page, nameof(page)); |
| 14 | + protected override PdfDecoderOptions CreateDefaultSpecializedOptions(DecoderOptions options) => new() { GeneralOptions = options }; |
32 | 15 |
|
33 |
| - using var bitmap = new PdfiumBitmap(width, height, true); |
34 |
| - page.Render(bitmap, PdfLibCore.Enums.PageOrientations.Normal, PdfLibCore.Enums.RenderingFlags.LcdText); |
| 16 | + protected override Image<TPixel> Decode<TPixel>(PdfDecoderOptions options, Stream stream, CancellationToken cancellationToken) |
| 17 | + { |
| 18 | + ArgumentNullException.ThrowIfNull(options, nameof(options)); |
| 19 | + ArgumentNullException.ThrowIfNull(stream, nameof(stream)); |
35 | 20 |
|
36 |
| - var image = Image.Load<TPixel>(bitmap.AsBmpStream()); |
| 21 | + using var pdfDocument = new PdfDocument(stream); |
| 22 | + var firstPageOfPdf = pdfDocument?.Pages?.FirstOrDefault(); |
| 23 | + ArgumentNullException.ThrowIfNull(firstPageOfPdf, nameof(firstPageOfPdf)); |
37 | 24 |
|
38 |
| - ScaleToTargetSize(options.GeneralOptions, image); |
| 25 | + var (width, height) = GetDimensions(firstPageOfPdf); |
39 | 26 |
|
40 |
| - return image; |
41 |
| - } |
| 27 | + using var bitmap = new PdfiumBitmap(width, height, true); |
| 28 | + firstPageOfPdf.Render(bitmap, PdfLibCore.Enums.PageOrientations.Normal, PdfLibCore.Enums.RenderingFlags.LcdText); |
42 | 29 |
|
43 |
| - protected override Image Decode(PdfDecoderOptions options, Stream stream, CancellationToken cancellationToken) => Decode<Rgba32>(options, stream, cancellationToken); |
| 30 | + using var bmpStream = bitmap.AsBmpStream(); |
| 31 | + var image = Image.Load<TPixel>(bmpStream); |
44 | 32 |
|
45 |
| - protected override ImageInfo Identify(DecoderOptions options, Stream stream, CancellationToken cancellationToken) |
46 |
| - { |
47 |
| - ArgumentNullException.ThrowIfNull(options, nameof(options)); |
48 |
| - ArgumentNullException.ThrowIfNull(stream, nameof(stream)); |
| 33 | + ScaleToTargetSize(options.GeneralOptions, image); |
49 | 34 |
|
50 |
| - using var pdfDocument = new PdfDocument(stream); |
51 |
| - var page = pdfDocument?.Pages?.FirstOrDefault(); |
52 |
| - TryGetWidthAndHeight(page, out int width, out int height); |
| 35 | + return image; |
| 36 | + } |
53 | 37 |
|
54 |
| - return new ImageInfo(new PixelTypeInfo(4), new(width, height), null); |
55 |
| - } |
| 38 | + protected override Image Decode(PdfDecoderOptions options, Stream stream, CancellationToken cancellationToken) => Decode<Rgba32>(options, stream, cancellationToken); |
56 | 39 |
|
57 |
| - private bool TryGetWidthAndHeight(PdfPage? page, out int width, out int height) |
58 |
| - { |
59 |
| - ArgumentNullException.ThrowIfNull(page, nameof(page)); |
| 40 | + protected override ImageInfo Identify(DecoderOptions options, Stream stream, CancellationToken cancellationToken) |
| 41 | + { |
| 42 | + ArgumentNullException.ThrowIfNull(options, nameof(options)); |
| 43 | + ArgumentNullException.ThrowIfNull(stream, nameof(stream)); |
60 | 44 |
|
61 |
| - var w = page.Size.Width; |
62 |
| - w = w != 0 ? w / 72 * 144D : 0; |
| 45 | + using var pdfDocument = new PdfDocument(stream); |
| 46 | + var firstPageOfPdf = pdfDocument?.Pages?.FirstOrDefault(); |
| 47 | + ArgumentNullException.ThrowIfNull(firstPageOfPdf, nameof(firstPageOfPdf)); |
63 | 48 |
|
64 |
| - var h = page.Size.Height; |
65 |
| - h = h != 0 ? h / 72 * 144D : 0; |
| 49 | + var (width, height) = GetDimensions(firstPageOfPdf); |
66 | 50 |
|
67 |
| - width = (int)w; |
68 |
| - height = (int)h; |
| 51 | + return new ImageInfo(new PixelTypeInfo(4), new(width, height), null); |
| 52 | + } |
69 | 53 |
|
70 |
| - return true; |
71 |
| - } |
| 54 | + private static (int width, int height) GetDimensions(PdfPage page) |
| 55 | + { |
| 56 | + ArgumentNullException.ThrowIfNull(page, nameof(page)); |
| 57 | + return ((int)page.Width, (int)page.Height); |
72 | 58 | }
|
73 | 59 | }
|
0 commit comments