Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/UglyToad.PdfPig.Tests/Filters/Ascii85FilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void DecodesWikipediaExample()
l(DId<j@<? 3r@:F % a + D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G
> 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());
Expand All @@ -39,7 +39,7 @@ public void ReplacesZWithEmptyBytes()
{
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());
Expand All @@ -55,7 +55,7 @@ public void ZInMiddleOf5CharacterSequenceThrows()
{
var bytes = Encoding.ASCII.GetBytes("qjzqo^");

Action action = () => filter.Decode(bytes, dictionary, 0);
Action action = () => filter.Decode(bytes, dictionary, TestFilterProvider.Instance, 0);

Assert.Throws<InvalidOperationException>(action);
}
Expand All @@ -65,7 +65,7 @@ public void SingleCharacterLastThrows()
{
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<ArgumentOutOfRangeException>(action);
}
Expand Down Expand Up @@ -110,7 +110,7 @@ public void DecodesEncodedPdfContent()
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());
Expand All @@ -134,7 +134,7 @@ public void DecodesEncodedPdfContentMissingEndOfDataSymbol()
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());
Expand All @@ -154,7 +154,7 @@ public void DecodeParallel()
{
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());
Expand All @@ -176,7 +176,7 @@ public void DecodeParallel()
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());
Expand Down
14 changes: 7 additions & 7 deletions src/UglyToad.PdfPig.Tests/Filters/AsciiHexDecodeFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void DecodesEncodedTextProperly()
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());

Expand All @@ -31,7 +31,7 @@ public void DecodesEncodedTextWithBracesProperly()
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());

Expand All @@ -47,7 +47,7 @@ public void DecodesEncodedTextWithWhitespaceProperly()
@"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());

Expand All @@ -61,7 +61,7 @@ public void DecodesEncodedTextLowercaseProperly()

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

Expand All @@ -75,7 +75,7 @@ public void DecodeWithInvalidCharactersThrows(string inputString)
{
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<InvalidOperationException>(action);
}
Expand All @@ -85,7 +85,7 @@ public void SubstitutesZeroForLastByte()
{
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());
Expand All @@ -101,7 +101,7 @@ public void DecodesEncodedTextStoppingAtLastBrace()

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void CanDecodeCCittFaxCompressedImageData()
};

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());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Tests/Filters/FlateFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void EncodeAndDecodePreservesInput()
{
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());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/UglyToad.PdfPig.Tests/Filters/RunLengthFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void CanDecodeRunLengthEncodedData()
1, 10, 19
};

var decoded = filter.Decode(data, new DictionaryToken(new Dictionary<NameToken, IToken>()), 1);
var decoded = filter.Decode(data, new DictionaryToken(new Dictionary<NameToken, IToken>()), TestFilterProvider.Instance, 1);

var expectedResult = new byte[]
{
Expand Down Expand Up @@ -53,7 +53,7 @@ public void StopsAtEndOfDataByte()
90, 6, 7
};

var decoded = filter.Decode(data, new DictionaryToken(new Dictionary<NameToken, IToken>()), 0);
var decoded = filter.Decode(data, new DictionaryToken(new Dictionary<NameToken, IToken>()), TestFilterProvider.Instance, 0);

var expectedResult = new byte[]
{
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Tests/Integration/FilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public sealed class NoFilter : IFilter
{
public bool IsSupported => false;

public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex)
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex)
{
throw new NotImplementedException();
}
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Tests/Integration/PdfParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void CanDecompressPngEncodedFlateStream()
});

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"
Expand Down
2 changes: 2 additions & 0 deletions src/UglyToad.PdfPig.Tests/TestFilterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

internal class TestFilterProvider : ILookupFilterProvider
{
public static readonly TestFilterProvider Instance = new TestFilterProvider();

public IReadOnlyList<IFilter> GetFilters(DictionaryToken dictionary)
{
return new List<IFilter>();
Expand Down
13 changes: 8 additions & 5 deletions src/UglyToad.PdfPig/Content/InlineImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ internal InlineImage(PdfRectangle bounds, int widthInSamples, int heightInSample
RenderingIntent renderingIntent,
bool interpolate,
IReadOnlyList<double> decode,
ReadOnlyMemory<byte> rawMemory,
IReadOnlyList<IFilter> filters,
ReadOnlyMemory<byte> rawMemory,
ILookupFilterProvider filterProvider,
IReadOnlyList<NameToken> filterNames,
DictionaryToken streamDictionary,
ColorSpaceDetails colorSpaceDetails)
{
Expand All @@ -79,8 +80,10 @@ internal InlineImage(PdfRectangle bounds, int widthInSamples, int heightInSample
Interpolate = interpolate;
ImageDictionary = streamDictionary;
RawMemory = rawMemory;
ColorSpaceDetails = colorSpaceDetails;

ColorSpaceDetails = colorSpaceDetails;

var filters = filterProvider.GetNamedFilters(filterNames);

var supportsFilters = true;
foreach (var filter in filters)
{
Expand All @@ -97,7 +100,7 @@ internal InlineImage(PdfRectangle bounds, int widthInSamples, int heightInSample
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;
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Filters/Ascii85Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public sealed class Ascii85Filter : IFilter
public bool IsSupported { get; } = true;

/// <inheritdoc />
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex)
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex)
{
Span<byte> asciiBuffer = stackalloc byte[5];

Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Filters/AsciiHexDecodeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed class AsciiHexDecodeFilter : IFilter
public bool IsSupported { get; } = true;

/// <inheritdoc />
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex)
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex)
{
Span<byte> pair = stackalloc byte[2];
var index = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Filters/CcittFaxDecodeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public sealed class CcittFaxDecodeFilter : IFilter
public bool IsSupported { get; } = true;

/// <inheritdoc />
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex)
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex)
{
var decodeParms = DecodeParameterResolver.GetFilterParameters(streamDictionary, filterIndex);

Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Filters/DctDecodeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class DctDecodeFilter : IFilter
public bool IsSupported { get; } = false;

/// <inheritdoc />
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex)
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> 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.");
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Filters/FlateFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public sealed class FlateFilter : IFilter
public bool IsSupported { get; } = true;

/// <inheritdoc />
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex)
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex)
{
var parameters = DecodeParameterResolver.GetFilterParameters(streamDictionary, filterIndex);

Expand Down
3 changes: 2 additions & 1 deletion src/UglyToad.PdfPig/Filters/IFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public interface IFilter
/// </summary>
/// <param name="input">The encoded bytes which were encoded using this filter.</param>
/// <param name="streamDictionary">The dictionary of the <see cref="StreamToken"/> (or other dictionary types, e.g. inline images) containing these bytes.</param>
/// <param name="filterProvider">The filter provider.</param>
/// <param name="filterIndex">The position of this filter in the pipeline used to encode data.</param>
/// <returns>The decoded bytes.</returns>
ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex);
ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex);
}
}
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Filters/Jbig2DecodeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class Jbig2DecodeFilter : IFilter
public bool IsSupported { get; } = false;

/// <inheritdoc />
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex)
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> 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.");
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Filters/JpxDecodeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class JpxDecodeFilter : IFilter
public bool IsSupported { get; } = false;

/// <inheritdoc />
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex)
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> 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.");
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Filters/LzwFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed class LzwFilter : IFilter
public bool IsSupported { get; } = true;

/// <inheritdoc />
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex)
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex)
{
var parameters = DecodeParameterResolver.GetFilterParameters(streamDictionary, filterIndex);

Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Filters/RunLengthFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class RunLengthFilter : IFilter
public bool IsSupported { get; } = true;

/// <inheritdoc />
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex)
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, IFilterProvider filterProvider, int filterIndex)
{
using var output = new ArrayPoolBufferWriter<byte>(input.Length);

Expand Down
9 changes: 3 additions & 6 deletions src/UglyToad.PdfPig/Graphics/InlineImageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,15 @@ internal InlineImage CreateInlineImage(
filterNames.Add(filterName);
}

var filters = filterProvider.GetNamedFilters(filterNames);

var decodeRaw = GetByKeys<ArrayToken>(NameToken.Decode, NameToken.D, false) ?? new ArrayToken(Array.Empty<IToken>());

var decode = decodeRaw.Data.OfType<NumericToken>().Select(x => x.Double).ToArray();

var interpolate = GetByKeys<BooleanToken>(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
Expand Down
Loading
Loading