From f26e7d90a37b7090fc7863b8be54c5f848c7ec7f Mon Sep 17 00:00:00 2001 From: BobLd <38405645+BobLd@users.noreply.github.com> Date: Sun, 23 Feb 2025 09:19:15 +0000 Subject: [PATCH] Pass IFilterProvider to IFilter.Decode() and handle null in PdfExtensions.Resolve() --- .../Filters/Ascii85FilterTests.cs | 16 ++++++++-------- .../Filters/AsciiHexDecodeFilterTests.cs | 14 +++++++------- .../Filters/CcittFaxDecodeFilterTests.cs | 2 +- .../Filters/FlateFilterTests.cs | 2 +- .../Filters/RunLengthFilterTests.cs | 4 ++-- .../Integration/FilterTests.cs | 2 +- .../Integration/PdfParserTests.cs | 2 +- src/UglyToad.PdfPig.Tests/TestFilterProvider.cs | 2 ++ src/UglyToad.PdfPig/Content/InlineImage.cs | 13 ++++++++----- src/UglyToad.PdfPig/Filters/Ascii85Filter.cs | 2 +- .../Filters/AsciiHexDecodeFilter.cs | 2 +- .../Filters/CcittFaxDecodeFilter.cs | 2 +- src/UglyToad.PdfPig/Filters/DctDecodeFilter.cs | 2 +- src/UglyToad.PdfPig/Filters/FlateFilter.cs | 2 +- src/UglyToad.PdfPig/Filters/IFilter.cs | 3 ++- .../Filters/Jbig2DecodeFilter.cs | 2 +- src/UglyToad.PdfPig/Filters/JpxDecodeFilter.cs | 2 +- src/UglyToad.PdfPig/Filters/LzwFilter.cs | 2 +- src/UglyToad.PdfPig/Filters/RunLengthFilter.cs | 2 +- .../Graphics/InlineImageBuilder.cs | 9 +++------ src/UglyToad.PdfPig/PdfExtensions.cs | 17 ++++++++--------- .../Tokenization/Scanner/IPdfTokenScanner.cs | 2 +- .../Tokenization/Scanner/PdfTokenScanner.cs | 10 ++-------- 23 files changed, 56 insertions(+), 60 deletions(-) diff --git a/src/UglyToad.PdfPig.Tests/Filters/Ascii85FilterTests.cs b/src/UglyToad.PdfPig.Tests/Filters/Ascii85FilterTests.cs index 8e1fba65..73312b54 100644 --- a/src/UglyToad.PdfPig.Tests/Filters/Ascii85FilterTests.cs +++ b/src/UglyToad.PdfPig.Tests/Filters/Ascii85FilterTests.cs @@ -20,7 +20,7 @@ O@3BB / F * &OCAfu2 l(DId uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c~>"); - var result = filter.Decode(bytes, dictionary, 0); + var result = filter.Decode(bytes, dictionary, TestFilterProvider.Instance, 0); #if !NET string text = Encoding.ASCII.GetString(result.ToArray()); @@ -39,7 +39,7 @@ O@3BB / F * &OCAfu2 { var bytes = Encoding.ASCII.GetBytes("9jqo^zBlbD-"); - var result = filter.Decode(bytes, dictionary, 1); + var result = filter.Decode(bytes, dictionary, TestFilterProvider.Instance, 1); #if !NET string text = Encoding.ASCII.GetString(result.ToArray()); @@ -55,7 +55,7 @@ O@3BB / F * &OCAfu2 { var bytes = Encoding.ASCII.GetBytes("qjzqo^"); - Action action = () => filter.Decode(bytes, dictionary, 0); + Action action = () => filter.Decode(bytes, dictionary, TestFilterProvider.Instance, 0); Assert.Throws(action); } @@ -65,7 +65,7 @@ O@3BB / F * &OCAfu2 { var bytes = Encoding.ASCII.GetBytes("9jqo^B"); - Action action = () => filter.Decode(bytes, dictionary, 1); + Action action = () => filter.Decode(bytes, dictionary, TestFilterProvider.Instance, 1); Assert.Throws(action); } @@ -110,7 +110,7 @@ T()<(%'A;f?Ma+CT;%+E_a:A0>K&EZek1D/aN,F)u&6DBNA*A0>f4BOu4*+EM76E,9eK+B3(_<%9""p. F*22=@:F%a+=SF4C'moi+=Li?EZeh0FD)e-@<>p#@;]TuBl.9kATKCFGA(],AKYo5BOu4*+CT;%+C#7pF_Pr+@VfTuDf0B:+=SF4C'moi+= Li?EZek1DKKT1F`2DD/TboKAKY](@:s.m/h%oBC'mC/$>""*cF*)G6@;Q?_DIdZpC&~>"; - var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, 0); + var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, TestFilterProvider.Instance, 0); #if !NET string text = Encoding.ASCII.GetString(result.ToArray()); @@ -134,7 +134,7 @@ T()<(%'A;f?Ma+CT;%+E_a:A0>K&EZek1D/aN,F)u&6DBNA*A0>f4BOu4*+EM76E,9eK+B3(_<%9""p. F*22=@:F%a+=SF4C'moi+=Li?EZeh0FD)e-@<>p#@;]TuBl.9kATKCFGA(],AKYo5BOu4*+CT;%+C#7pF_Pr+@VfTuDf0B:+=SF4C'moi+= Li?EZek1DKKT1F`2DD/TboKAKY](@:s.m/h%oBC'mC/$>""*cF*)G6@;Q?_DIdZpC&"; - var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, 0); + var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, TestFilterProvider.Instance, 0); #if !NET string text = Encoding.ASCII.GetString(result.ToArray()); @@ -154,7 +154,7 @@ Li?EZek1DKKT1F`2DD/TboKAKY](@:s.m/h%oBC'mC/$>""*cF*)G6@;Q?_DIdZpC&"; { var bytes = Encoding.ASCII.GetBytes("9jqo^zBlbD-"); - var result = filter.Decode(bytes, dictionary, 1); + var result = filter.Decode(bytes, dictionary, TestFilterProvider.Instance, 1); #if !NET string text = Encoding.ASCII.GetString(result.ToArray()); @@ -176,7 +176,7 @@ T()<(%'A;f?Ma+CT;%+E_a:A0>K&EZek1D/aN,F)u&6DBNA*A0>f4BOu4*+EM76E,9eK+B3(_<%9""p. F*22=@:F%a+=SF4C'moi+=Li?EZeh0FD)e-@<>p#@;]TuBl.9kATKCFGA(],AKYo5BOu4*+CT;%+C#7pF_Pr+@VfTuDf0B:+=SF4C'moi+= Li?EZek1DKKT1F`2DD/TboKAKY](@:s.m/h%oBC'mC/$>""*cF*)G6@;Q?_DIdZpC&~>"; - var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, 0); + var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, TestFilterProvider.Instance, 0); #if !NET string text = Encoding.ASCII.GetString(result.ToArray()); diff --git a/src/UglyToad.PdfPig.Tests/Filters/AsciiHexDecodeFilterTests.cs b/src/UglyToad.PdfPig.Tests/Filters/AsciiHexDecodeFilterTests.cs index 04f0d4dd..38f7fe42 100644 --- a/src/UglyToad.PdfPig.Tests/Filters/AsciiHexDecodeFilterTests.cs +++ b/src/UglyToad.PdfPig.Tests/Filters/AsciiHexDecodeFilterTests.cs @@ -16,7 +16,7 @@ var input = Encoding.ASCII.GetBytes( "7368652073656C6C73207365617368656C6C73206F6E20746865207365612073686F7265"); - var decoded = new AsciiHexDecodeFilter().Decode(input, dictionary, 1); + var decoded = new AsciiHexDecodeFilter().Decode(input, dictionary, TestFilterProvider.Instance, 1); var decodedText = Encoding.ASCII.GetString(decoded.ToArray()); @@ -31,7 +31,7 @@ var input = Encoding.ASCII.GetBytes( "<7368652073656C6C73207365617368656C6C73206F6E20746865207365612073686F7265>"); - var decoded = new AsciiHexDecodeFilter().Decode(input, dictionary, 1); + var decoded = new AsciiHexDecodeFilter().Decode(input, dictionary, TestFilterProvider.Instance, 1); var decodedText = Encoding.ASCII.GetString(decoded.ToArray()); @@ -47,7 +47,7 @@ @"6F6E6365207 5706F6E206120 74696D6520696E 20612067616C6178792046617220466172204177 6179"); - var decoded = new AsciiHexDecodeFilter().Decode(input, dictionary, 1); + var decoded = new AsciiHexDecodeFilter().Decode(input, dictionary, TestFilterProvider.Instance, 1); var decodedText = Encoding.ASCII.GetString(decoded.ToArray()); @@ -61,7 +61,7 @@ var input = Encoding.ASCII.GetBytes("6f6e63652075706f6e20612074696d6520696e20612067616c61787920466172204661722041776179"); - var decoded = new AsciiHexDecodeFilter().Decode(input, dictionary, 1); + var decoded = new AsciiHexDecodeFilter().Decode(input, dictionary, TestFilterProvider.Instance, 1); var decodedText = Encoding.ASCII.GetString(decoded.ToArray()); @@ -75,7 +75,7 @@ { var input = Encoding.ASCII.GetBytes(inputString); - Action action = () => new AsciiHexDecodeFilter().Decode(input, dictionary, 1); + Action action = () => new AsciiHexDecodeFilter().Decode(input, dictionary, TestFilterProvider.Instance, 1); Assert.Throws(action); } @@ -85,7 +85,7 @@ { var input = Encoding.ASCII.GetBytes("AE5>"); - var decoded = new AsciiHexDecodeFilter().Decode(input, dictionary, 1); + var decoded = new AsciiHexDecodeFilter().Decode(input, dictionary, TestFilterProvider.Instance, 1); #pragma warning disable SYSLIB0001 var decodedText = Encoding.UTF7.GetString(decoded.ToArray()); @@ -101,7 +101,7 @@ var input = Encoding.ASCII.GetBytes("6f6e63652075706f6e20612074696d6520696e20612067616c61787920466172204661722041776179> There is stuff following the EOD."); - var decoded = new AsciiHexDecodeFilter().Decode(input, dictionary, 1); + var decoded = new AsciiHexDecodeFilter().Decode(input, dictionary, TestFilterProvider.Instance, 1); var decodedText = Encoding.ASCII.GetString(decoded.ToArray()); diff --git a/src/UglyToad.PdfPig.Tests/Filters/CcittFaxDecodeFilterTests.cs b/src/UglyToad.PdfPig.Tests/Filters/CcittFaxDecodeFilterTests.cs index aa680b26..cbd2d2be 100644 --- a/src/UglyToad.PdfPig.Tests/Filters/CcittFaxDecodeFilterTests.cs +++ b/src/UglyToad.PdfPig.Tests/Filters/CcittFaxDecodeFilterTests.cs @@ -31,7 +31,7 @@ }; var expectedBytes = ImageHelpers.LoadFileBytes("ccittfax-decoded.bin"); - var decodedBytes = filter.Decode(encodedBytes, new DictionaryToken(dictionary), 0); + var decodedBytes = filter.Decode(encodedBytes, new DictionaryToken(dictionary), TestFilterProvider.Instance, 0); Assert.Equal(expectedBytes, decodedBytes.ToArray()); } } diff --git a/src/UglyToad.PdfPig.Tests/Filters/FlateFilterTests.cs b/src/UglyToad.PdfPig.Tests/Filters/FlateFilterTests.cs index db2829f0..68289e87 100644 --- a/src/UglyToad.PdfPig.Tests/Filters/FlateFilterTests.cs +++ b/src/UglyToad.PdfPig.Tests/Filters/FlateFilterTests.cs @@ -17,7 +17,7 @@ { inputStream.Seek(0, SeekOrigin.Begin); var result = filter.Encode(inputStream, parameters, 0); - var decoded = filter.Decode(result, parameters, 0); + var decoded = filter.Decode(result, parameters, TestFilterProvider.Instance, 0); Assert.Equal(input, decoded.ToArray()); } } diff --git a/src/UglyToad.PdfPig.Tests/Filters/RunLengthFilterTests.cs b/src/UglyToad.PdfPig.Tests/Filters/RunLengthFilterTests.cs index 124147c0..85290801 100644 --- a/src/UglyToad.PdfPig.Tests/Filters/RunLengthFilterTests.cs +++ b/src/UglyToad.PdfPig.Tests/Filters/RunLengthFilterTests.cs @@ -24,7 +24,7 @@ 1, 10, 19 }; - var decoded = filter.Decode(data, new DictionaryToken(new Dictionary()), 1); + var decoded = filter.Decode(data, new DictionaryToken(new Dictionary()), TestFilterProvider.Instance, 1); var expectedResult = new byte[] { @@ -53,7 +53,7 @@ 90, 6, 7 }; - var decoded = filter.Decode(data, new DictionaryToken(new Dictionary()), 0); + var decoded = filter.Decode(data, new DictionaryToken(new Dictionary()), TestFilterProvider.Instance, 0); var expectedResult = new byte[] { diff --git a/src/UglyToad.PdfPig.Tests/Integration/FilterTests.cs b/src/UglyToad.PdfPig.Tests/Integration/FilterTests.cs index 735b6ae1..f9ca67cc 100644 --- a/src/UglyToad.PdfPig.Tests/Integration/FilterTests.cs +++ b/src/UglyToad.PdfPig.Tests/Integration/FilterTests.cs @@ -62,7 +62,7 @@ { public bool IsSupported => false; - public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, int filterIndex) + public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex) { throw new NotImplementedException(); } diff --git a/src/UglyToad.PdfPig.Tests/Integration/PdfParserTests.cs b/src/UglyToad.PdfPig.Tests/Integration/PdfParserTests.cs index 894d46ba..64f9c85c 100644 --- a/src/UglyToad.PdfPig.Tests/Integration/PdfParserTests.cs +++ b/src/UglyToad.PdfPig.Tests/Integration/PdfParserTests.cs @@ -96,7 +96,7 @@ }); var filter = new FlateFilter(); - var filtered = filter.Decode(streamBytes, dictionary, 0).ToArray(); + var filtered = filter.Decode(streamBytes, dictionary, TestFilterProvider.Instance, 0).ToArray(); var expected = "1 0 15 0 1 0 216 0 1 2 160 0 1 2 210 0 1 3 84 0 1 4 46 0 1 7 165 0 1 70 229 0 1 72 84 0 1 96 235 0 1 98 18 0 2 0 12 0 2 0 12 1 2 0 12 2 2 0 12 3 2 0 12 4 2 0 12 5 2 0 12 6 2 0 12 7 2 0 12 8" diff --git a/src/UglyToad.PdfPig.Tests/TestFilterProvider.cs b/src/UglyToad.PdfPig.Tests/TestFilterProvider.cs index 87f5d4a0..59394e29 100644 --- a/src/UglyToad.PdfPig.Tests/TestFilterProvider.cs +++ b/src/UglyToad.PdfPig.Tests/TestFilterProvider.cs @@ -6,6 +6,8 @@ internal class TestFilterProvider : ILookupFilterProvider { + public static readonly TestFilterProvider Instance = new TestFilterProvider(); + public IReadOnlyList GetFilters(DictionaryToken dictionary) { return new List(); diff --git a/src/UglyToad.PdfPig/Content/InlineImage.cs b/src/UglyToad.PdfPig/Content/InlineImage.cs index 7c8c0f5e..b13bf56c 100644 --- a/src/UglyToad.PdfPig/Content/InlineImage.cs +++ b/src/UglyToad.PdfPig/Content/InlineImage.cs @@ -64,8 +64,9 @@ RenderingIntent renderingIntent, bool interpolate, IReadOnlyList decode, - ReadOnlyMemory rawMemory, - IReadOnlyList filters, + ReadOnlyMemory rawMemory, + ILookupFilterProvider filterProvider, + IReadOnlyList filterNames, DictionaryToken streamDictionary, ColorSpaceDetails colorSpaceDetails) { @@ -79,8 +80,10 @@ Interpolate = interpolate; ImageDictionary = streamDictionary; RawMemory = rawMemory; - ColorSpaceDetails = colorSpaceDetails; - + ColorSpaceDetails = colorSpaceDetails; + + var filters = filterProvider.GetNamedFilters(filterNames); + var supportsFilters = true; foreach (var filter in filters) { @@ -97,7 +100,7 @@ for (var i = 0; i < filters.Count; i++) { var filter = filters[i]; - b = filter.Decode(b.Span, streamDictionary, i); + b = filter.Decode(b.Span, streamDictionary, filterProvider, i); } return b; diff --git a/src/UglyToad.PdfPig/Filters/Ascii85Filter.cs b/src/UglyToad.PdfPig/Filters/Ascii85Filter.cs index 5fafa077..f58f0bfb 100644 --- a/src/UglyToad.PdfPig/Filters/Ascii85Filter.cs +++ b/src/UglyToad.PdfPig/Filters/Ascii85Filter.cs @@ -27,7 +27,7 @@ public bool IsSupported { get; } = true; /// - public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, int filterIndex) + public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex) { Span asciiBuffer = stackalloc byte[5]; diff --git a/src/UglyToad.PdfPig/Filters/AsciiHexDecodeFilter.cs b/src/UglyToad.PdfPig/Filters/AsciiHexDecodeFilter.cs index d66871dc..0993ce65 100644 --- a/src/UglyToad.PdfPig/Filters/AsciiHexDecodeFilter.cs +++ b/src/UglyToad.PdfPig/Filters/AsciiHexDecodeFilter.cs @@ -28,7 +28,7 @@ public bool IsSupported { get; } = true; /// - public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, int filterIndex) + public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex) { Span pair = stackalloc byte[2]; var index = 0; diff --git a/src/UglyToad.PdfPig/Filters/CcittFaxDecodeFilter.cs b/src/UglyToad.PdfPig/Filters/CcittFaxDecodeFilter.cs index 7bce8d66..68d83835 100644 --- a/src/UglyToad.PdfPig/Filters/CcittFaxDecodeFilter.cs +++ b/src/UglyToad.PdfPig/Filters/CcittFaxDecodeFilter.cs @@ -20,7 +20,7 @@ public bool IsSupported { get; } = true; /// - public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, int filterIndex) + public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex) { var decodeParms = DecodeParameterResolver.GetFilterParameters(streamDictionary, filterIndex); diff --git a/src/UglyToad.PdfPig/Filters/DctDecodeFilter.cs b/src/UglyToad.PdfPig/Filters/DctDecodeFilter.cs index 4dd158db..0044192f 100644 --- a/src/UglyToad.PdfPig/Filters/DctDecodeFilter.cs +++ b/src/UglyToad.PdfPig/Filters/DctDecodeFilter.cs @@ -13,7 +13,7 @@ public bool IsSupported { get; } = false; /// - public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, int filterIndex) + public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex) { throw new NotSupportedException("The DST (Discrete Cosine Transform) Filter indicates data is encoded in JPEG format. " + "This filter is not currently supported but the raw data can be supplied to JPEG supporting libraries."); diff --git a/src/UglyToad.PdfPig/Filters/FlateFilter.cs b/src/UglyToad.PdfPig/Filters/FlateFilter.cs index 35099c92..fa57f741 100644 --- a/src/UglyToad.PdfPig/Filters/FlateFilter.cs +++ b/src/UglyToad.PdfPig/Filters/FlateFilter.cs @@ -31,7 +31,7 @@ public bool IsSupported { get; } = true; /// - public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, int filterIndex) + public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex) { var parameters = DecodeParameterResolver.GetFilterParameters(streamDictionary, filterIndex); diff --git a/src/UglyToad.PdfPig/Filters/IFilter.cs b/src/UglyToad.PdfPig/Filters/IFilter.cs index da5ceaac..f60b1203 100644 --- a/src/UglyToad.PdfPig/Filters/IFilter.cs +++ b/src/UglyToad.PdfPig/Filters/IFilter.cs @@ -18,8 +18,9 @@ /// /// The encoded bytes which were encoded using this filter. /// The dictionary of the (or other dictionary types, e.g. inline images) containing these bytes. + /// The filter provider. /// The position of this filter in the pipeline used to encode data. /// The decoded bytes. - ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, int filterIndex); + ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex); } } diff --git a/src/UglyToad.PdfPig/Filters/Jbig2DecodeFilter.cs b/src/UglyToad.PdfPig/Filters/Jbig2DecodeFilter.cs index f3240043..7060e76f 100644 --- a/src/UglyToad.PdfPig/Filters/Jbig2DecodeFilter.cs +++ b/src/UglyToad.PdfPig/Filters/Jbig2DecodeFilter.cs @@ -13,7 +13,7 @@ public bool IsSupported { get; } = false; /// - public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, int filterIndex) + public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex) { throw new NotSupportedException("The JBIG2 Filter for monochrome image data is not currently supported. " + "Try accessing the raw compressed data directly."); diff --git a/src/UglyToad.PdfPig/Filters/JpxDecodeFilter.cs b/src/UglyToad.PdfPig/Filters/JpxDecodeFilter.cs index 24d9dd81..a5af6964 100644 --- a/src/UglyToad.PdfPig/Filters/JpxDecodeFilter.cs +++ b/src/UglyToad.PdfPig/Filters/JpxDecodeFilter.cs @@ -13,7 +13,7 @@ public bool IsSupported { get; } = false; /// - public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, int filterIndex) + public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex) { throw new NotSupportedException("The JPX Filter (JPEG2000) for image data is not currently supported. " + "Try accessing the raw compressed data directly."); diff --git a/src/UglyToad.PdfPig/Filters/LzwFilter.cs b/src/UglyToad.PdfPig/Filters/LzwFilter.cs index bd086cd5..9d593e83 100644 --- a/src/UglyToad.PdfPig/Filters/LzwFilter.cs +++ b/src/UglyToad.PdfPig/Filters/LzwFilter.cs @@ -29,7 +29,7 @@ namespace UglyToad.PdfPig.Filters public bool IsSupported { get; } = true; /// - public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, int filterIndex) + public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex) { var parameters = DecodeParameterResolver.GetFilterParameters(streamDictionary, filterIndex); diff --git a/src/UglyToad.PdfPig/Filters/RunLengthFilter.cs b/src/UglyToad.PdfPig/Filters/RunLengthFilter.cs index e345d001..aa7d4315 100644 --- a/src/UglyToad.PdfPig/Filters/RunLengthFilter.cs +++ b/src/UglyToad.PdfPig/Filters/RunLengthFilter.cs @@ -16,7 +16,7 @@ public bool IsSupported { get; } = true; /// - public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, int filterIndex) + public ReadOnlyMemory Decode(ReadOnlySpan input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex) { using var output = new ArrayPoolBufferWriter(input.Length); diff --git a/src/UglyToad.PdfPig/Graphics/InlineImageBuilder.cs b/src/UglyToad.PdfPig/Graphics/InlineImageBuilder.cs index 1f89ac52..87287b1d 100644 --- a/src/UglyToad.PdfPig/Graphics/InlineImageBuilder.cs +++ b/src/UglyToad.PdfPig/Graphics/InlineImageBuilder.cs @@ -98,18 +98,15 @@ filterNames.Add(filterName); } - var filters = filterProvider.GetNamedFilters(filterNames); - var decodeRaw = GetByKeys(NameToken.Decode, NameToken.D, false) ?? new ArrayToken(Array.Empty()); var decode = decodeRaw.Data.OfType().Select(x => x.Double).ToArray(); var interpolate = GetByKeys(NameToken.Interpolate, NameToken.I, false)?.Data ?? false; - return new InlineImage(bounds, width, height, bitsPerComponent, isMask, renderingIntent, interpolate, decode, Bytes, - filters, - imgDic, - details); + return new InlineImage(bounds, width, height, bitsPerComponent, + isMask, renderingIntent, interpolate, decode, Bytes, + filterProvider, filterNames, imgDic, details); } #nullable disable diff --git a/src/UglyToad.PdfPig/PdfExtensions.cs b/src/UglyToad.PdfPig/PdfExtensions.cs index fe7d1f91..e69d0054 100644 --- a/src/UglyToad.PdfPig/PdfExtensions.cs +++ b/src/UglyToad.PdfPig/PdfExtensions.cs @@ -62,7 +62,7 @@ var transform = stream.Data; for (var i = 0; i < filters.Count; i++) { - transform = filters[i].Decode(transform.Span, stream.StreamDictionary, i); + transform = filters[i].Decode(transform.Span, stream.StreamDictionary, filterProvider, i); } return transform; @@ -78,7 +78,7 @@ var transform = stream.Data; for (var i = 0; i < filters.Count; i++) { - transform = filters[i].Decode(transform.Span, stream.StreamDictionary, i); + transform = filters[i].Decode(transform.Span, stream.StreamDictionary, filterProvider, i); } return transform; @@ -88,12 +88,12 @@ /// Returns an equivalent token where any indirect references of child objects are /// recursively traversed and resolved. /// - internal static T Resolve(this T token, IPdfTokenScanner scanner) where T : IToken + internal static T? Resolve(this T? token, IPdfTokenScanner scanner) where T : IToken { - return (T)ResolveInternal(token, scanner); + return (T?)ResolveInternal(token, scanner); } - private static IToken ResolveInternal(this IToken token, IPdfTokenScanner scanner) + private static IToken? ResolveInternal(this IToken? token, IPdfTokenScanner scanner) { if (token is StreamToken stream) { @@ -105,7 +105,7 @@ var resolvedItems = new Dictionary(); foreach (var kvp in dict.Data) { - var value = kvp.Value is IndirectReferenceToken reference ? scanner.Get(reference.Data).Data : kvp.Value; + var value = kvp.Value is IndirectReferenceToken reference ? scanner.Get(reference.Data)?.Data : kvp.Value; resolvedItems[NameToken.Create(kvp.Key)] = ResolveInternal(value, scanner); } @@ -117,14 +117,13 @@ var resolvedItems = new List(); for (int i = 0; i < arr.Length; i++) { - var value = arr.Data[i] is IndirectReferenceToken reference ? scanner.Get(reference.Data).Data : arr.Data[i]; + var value = arr.Data[i] is IndirectReferenceToken reference ? scanner.Get(reference.Data)?.Data : arr.Data[i]; resolvedItems.Add(ResolveInternal(value, scanner)); } return new ArrayToken(resolvedItems); } - var val = token is IndirectReferenceToken tokenReference ? scanner.Get(tokenReference.Data).Data : token; - return val; + return token is IndirectReferenceToken tokenReference ? scanner.Get(tokenReference.Data)?.Data : token; } } } diff --git a/src/UglyToad.PdfPig/Tokenization/Scanner/IPdfTokenScanner.cs b/src/UglyToad.PdfPig/Tokenization/Scanner/IPdfTokenScanner.cs index 5ba7269c..a19707da 100644 --- a/src/UglyToad.PdfPig/Tokenization/Scanner/IPdfTokenScanner.cs +++ b/src/UglyToad.PdfPig/Tokenization/Scanner/IPdfTokenScanner.cs @@ -15,7 +15,7 @@ /// /// The object number for the object to tokenize. /// The tokenized object. - ObjectToken Get(IndirectReference reference); + ObjectToken? Get(IndirectReference reference); /// /// Adds the token to an internal cache that will be returned instead of diff --git a/src/UglyToad.PdfPig/Tokenization/Scanner/PdfTokenScanner.cs b/src/UglyToad.PdfPig/Tokenization/Scanner/PdfTokenScanner.cs index 75c38ae9..71d35530 100644 --- a/src/UglyToad.PdfPig/Tokenization/Scanner/PdfTokenScanner.cs +++ b/src/UglyToad.PdfPig/Tokenization/Scanner/PdfTokenScanner.cs @@ -5,9 +5,7 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; - using System.IO; using System.Linq; - using System.Text; using System.Text.RegularExpressions; using Core; using Encryption; @@ -714,10 +712,8 @@ coreTokenScanner.DeregisterCustomTokenizer(tokenizer); } - -#nullable disable - - public ObjectToken Get(IndirectReference reference) + + public ObjectToken? Get(IndirectReference reference) { if (isDisposed) { @@ -769,8 +765,6 @@ return BruteForceFileToFindReference(reference); } -#nullable enable - public void ReplaceToken(IndirectReference reference, IToken token) { // Using 0 position as it isn't written to stream and this value doesn't