Skip to content

Commit f937792

Browse files
committed
optimize code, fix typo in readme
1 parent e1d8aec commit f937792

9 files changed

+101
-125
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ PM> Install-Package ImageSharp.Community.Formats.Pdf
1818
using System.IO;
1919

2020
using SixLabors.ImageSharp;
21-
using SixLabors.ImageSharp.Formats.Jpeg;
21+
using SixLabors.ImageSharp.Formats.Png;
2222
using SixLabors.ImageSharp.Processing;
2323
using ImageSharpCommunity.Formats.Pdf;
2424

Diff for: src/ImageSharpCommunity.Formats.Pdf/ImageSharpCommunity.Formats.Pdf.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<RepositoryUrl>https://github.com/skttl/ImageSharpCommunity.Formats.Pdf</RepositoryUrl>
2121
<RootNamespace>ImageSharpCommunity.Formats.Pdf</RootNamespace>
2222
<Title>PDF Decoder for ImageSharp</Title>
23-
<Version>0.1.0</Version>
23+
<Version>0.2.0</Version>
2424
</PropertyGroup>
2525

2626
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
using SixLabors.ImageSharp;
2-
using SixLabors.ImageSharp.Formats;
1+
namespace ImageSharpCommunity.Formats.Pdf;
32

4-
namespace ImageSharpCommunity.Formats.Pdf
3+
public sealed class PdfConfigurationModule : IImageFormatConfigurationModule
54
{
6-
public sealed class PdfConfigurationModule : IImageFormatConfigurationModule
5+
public void Configure(Configuration configuration)
76
{
8-
public void Configure(Configuration configuration)
9-
{
10-
configuration.ImageFormatsManager.SetDecoder(PdfFormat.Instance, PdfDecoder.Instance);
11-
configuration.ImageFormatsManager.SetEncoder(PdfFormat.Instance, new PdfEncoder());
12-
configuration.ImageFormatsManager.AddImageFormatDetector(new PdfFormatDetector());
13-
}
7+
configuration.ImageFormatsManager.SetDecoder(PdfFormat.Instance, PdfDecoder.Instance);
8+
configuration.ImageFormatsManager.SetEncoder(PdfFormat.Instance, new PdfEncoder());
9+
configuration.ImageFormatsManager.AddImageFormatDetector(new PdfFormatDetector());
1410
}
1511
}

Diff for: src/ImageSharpCommunity.Formats.Pdf/PdfDecoder.cs

+39-53
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,59 @@
1-
using PdfLibCore;
2-
using SixLabors.ImageSharp;
3-
using SixLabors.ImageSharp.Formats;
4-
using SixLabors.ImageSharp.PixelFormats;
1+
namespace ImageSharpCommunity.Formats.Pdf;
52

6-
namespace ImageSharpCommunity.Formats.Pdf
3+
public class PdfDecoder : SpecializedImageDecoder<PdfDecoderOptions>
74
{
8-
public class PdfDecoder : SpecializedImageDecoder<PdfDecoderOptions>
5+
private PdfDecoder()
96
{
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+
}
288

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();
3013

31-
ArgumentNullException.ThrowIfNull(page, nameof(page));
14+
protected override PdfDecoderOptions CreateDefaultSpecializedOptions(DecoderOptions options) => new() { GeneralOptions = options };
3215

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));
3520

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));
3724

38-
ScaleToTargetSize(options.GeneralOptions, image);
25+
var (width, height) = GetDimensions(firstPageOfPdf);
3926

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);
4229

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);
4432

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);
4934

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+
}
5337

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);
5639

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));
6044

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));
6348

64-
var h = page.Size.Height;
65-
h = h != 0 ? h / 72 * 144D : 0;
49+
var (width, height) = GetDimensions(firstPageOfPdf);
6650

67-
width = (int)w;
68-
height = (int)h;
51+
return new ImageInfo(new PixelTypeInfo(4), new(width, height), null);
52+
}
6953

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);
7258
}
7359
}
+4-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using SixLabors.ImageSharp.Formats;
1+
namespace ImageSharpCommunity.Formats.Pdf;
22

3-
namespace ImageSharpCommunity.Formats.Pdf
3+
public sealed class PdfDecoderOptions : ISpecializedDecoderOptions
44
{
5-
public sealed class PdfDecoderOptions : ISpecializedDecoderOptions
6-
{
7-
/// <inheritdoc/>
8-
public DecoderOptions GeneralOptions { get; init; } = new();
9-
}
5+
/// <inheritdoc/>
6+
public DecoderOptions GeneralOptions { get; init; } = new();
107
}

Diff for: src/ImageSharpCommunity.Formats.Pdf/PdfEncoder.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
using SixLabors.ImageSharp;
2-
using SixLabors.ImageSharp.Formats;
1+
namespace ImageSharpCommunity.Formats.Pdf;
32

4-
namespace ImageSharpCommunity.Formats.Pdf
3+
public class PdfEncoder : ImageEncoder
54
{
6-
public class PdfEncoder : ImageEncoder
5+
protected override void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
76
{
8-
protected override void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)
9-
{
10-
throw new NotImplementedException();
11-
}
7+
throw new NotSupportedException("PDF encoding is not supported");
128
}
139
}

Diff for: src/ImageSharpCommunity.Formats.Pdf/PdfFormat.cs

+25-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
using SixLabors.ImageSharp.Formats;
1+
namespace ImageSharpCommunity.Formats.Pdf;
22

3-
namespace ImageSharpCommunity.Formats.Pdf
3+
public sealed class PdfFormat : IImageFormat
44
{
5-
public sealed class PdfFormat : IImageFormat
5+
private PdfFormat()
66
{
7-
private PdfFormat()
8-
{
9-
}
7+
}
108

11-
/// <summary>
12-
/// Gets the current instance.
13-
/// </summary>
14-
public static PdfFormat Instance { get; } = new();
15-
public string Name => "PDF";
9+
/// <summary>
10+
/// Gets the current instance of the <see cref="PdfFormat"/>.
11+
/// </summary>
12+
public static PdfFormat Instance { get; } = new();
1613

17-
public string DefaultMimeType => "application/pdf";
14+
/// <summary>
15+
/// Gets the name of the image format.
16+
/// </summary>
17+
public string Name => "PDF";
1818

19-
public IEnumerable<string> MimeTypes => new[] { "application/pdf" };
19+
/// <summary>
20+
/// Gets the default MIME type of the image format.
21+
/// </summary>
22+
public string DefaultMimeType => "application/pdf";
2023

21-
public IEnumerable<string> FileExtensions => new[] { "pdf" };
22-
}
24+
/// <summary>
25+
/// Gets the MIME types associated with the image format.
26+
/// </summary>
27+
public IEnumerable<string> MimeTypes { get; } = ImmutableArray.Create("application/pdf");
28+
29+
/// <summary>
30+
/// Gets the file extensions associated with the image format.
31+
/// </summary>
32+
public IEnumerable<string> FileExtensions { get; } = ImmutableArray.Create("pdf");
2333
}
+15-30
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,21 @@
1-
using SixLabors.ImageSharp.Formats;
2-
using System.Diagnostics.CodeAnalysis;
1+
namespace ImageSharpCommunity.Formats.Pdf;
32

4-
namespace ImageSharpCommunity.Formats.Pdf
3+
public class PdfFormatDetector : IImageFormatDetector
54
{
6-
public class PdfFormatDetector : IImageFormatDetector
7-
{
8-
public int HeaderSize => 16;
9-
10-
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
11-
{
12-
format = IsPDFFileFormat(header) ? PdfFormat.Instance : null;
13-
14-
return format != null;
15-
}
16-
17-
private bool IsPDFFileFormat(ReadOnlySpan<byte> header)
18-
{
19-
if (header.Length >= 4)
20-
{
21-
var first4 = header[..4];
5+
public int HeaderSize => 16;
226

23-
var shouldMatch = new byte[]{
24-
0x25, // %
25-
0x50, // P
26-
0x44, // D
27-
0x46, // F
28-
};
29-
30-
return first4.SequenceEqual(shouldMatch);
31-
}
7+
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
8+
{
9+
format = IsPDFFileFormat(header) ? PdfFormat.Instance : null;
10+
return format != null;
11+
}
3212

33-
return false;
34-
}
13+
private static bool IsPDFFileFormat(ReadOnlySpan<byte> header)
14+
{
15+
return header.Length >= 4 &&
16+
header[0] == 0x25 && // %
17+
header[1] == 0x50 && // P
18+
header[2] == 0x44 && // D
19+
header[3] == 0x46; // F
3520
}
3621
}

Diff for: src/ImageSharpCommunity.Formats.Pdf/Usings.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
global using PdfLibCore;
2+
global using SixLabors.ImageSharp;
3+
global using SixLabors.ImageSharp.Formats;
4+
global using SixLabors.ImageSharp.PixelFormats;
5+
global using System.Collections.Immutable;
6+
global using System.Diagnostics.CodeAnalysis;

0 commit comments

Comments
 (0)