mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-11-28 17:47:12 +08:00
Consolidate DictionaryToken extension methods
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Tokens;
|
using Tokens;
|
||||||
|
using UglyToad.PdfPig.Util;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Decodes image data that has been encoded using either Group 3 or Group 4.
|
/// Decodes image data that has been encoded using either Group 3 or Group 4.
|
||||||
@@ -21,9 +22,9 @@
|
|||||||
{
|
{
|
||||||
var decodeParms = DecodeParameterResolver.GetFilterParameters(streamDictionary, filterIndex);
|
var decodeParms = DecodeParameterResolver.GetFilterParameters(streamDictionary, filterIndex);
|
||||||
|
|
||||||
var cols = decodeParms.GetInt(NameToken.Columns, 1728);
|
var cols = decodeParms.GetIntOrDefault(NameToken.Columns, 1728);
|
||||||
var rows = decodeParms.GetInt(NameToken.Rows, 0);
|
var rows = decodeParms.GetIntOrDefault(NameToken.Rows, 0);
|
||||||
var height = streamDictionary.GetInt(NameToken.Height, NameToken.H, 0);
|
var height = streamDictionary.GetIntOrDefault(NameToken.Height, NameToken.H, 0);
|
||||||
if (rows > 0 && height > 0)
|
if (rows > 0 && height > 0)
|
||||||
{
|
{
|
||||||
// PDFBOX-771, PDFBOX-3727: rows in DecodeParms sometimes contains an incorrect value
|
// PDFBOX-771, PDFBOX-3727: rows in DecodeParms sometimes contains an incorrect value
|
||||||
@@ -35,8 +36,8 @@
|
|||||||
rows = Math.Max(rows, height);
|
rows = Math.Max(rows, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
var k = decodeParms.GetInt(NameToken.K, 0);
|
var k = decodeParms.GetIntOrDefault(NameToken.K, 0);
|
||||||
var encodedByteAlign = decodeParms.GetBoolean(NameToken.EncodedByteAlign, false);
|
var encodedByteAlign = decodeParms.GetBooleanOrDefault(NameToken.EncodedByteAlign, false);
|
||||||
var compressionType = DetermineCompressionType(input, k);
|
var compressionType = DetermineCompressionType(input, k);
|
||||||
using (var stream = new CcittFaxDecoderStream(new MemoryStream(input.ToArray()), cols, compressionType, encodedByteAlign))
|
using (var stream = new CcittFaxDecoderStream(new MemoryStream(input.ToArray()), cols, compressionType, encodedByteAlign))
|
||||||
{
|
{
|
||||||
@@ -45,7 +46,7 @@
|
|||||||
ReadFromDecoderStream(stream, decompressed);
|
ReadFromDecoderStream(stream, decompressed);
|
||||||
|
|
||||||
// we expect black to be 1, if not invert the bitmap
|
// we expect black to be 1, if not invert the bitmap
|
||||||
var blackIsOne = decodeParms.GetBoolean(NameToken.BlackIs1, false);
|
var blackIsOne = decodeParms.GetBooleanOrDefault(NameToken.BlackIs1, false);
|
||||||
if (!blackIsOne)
|
if (!blackIsOne)
|
||||||
{
|
{
|
||||||
InvertBitmap(decompressed);
|
InvertBitmap(decompressed);
|
||||||
|
|||||||
@@ -2,8 +2,9 @@
|
|||||||
{
|
{
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Tokens;
|
using Tokens;
|
||||||
|
using UglyToad.PdfPig.Util;
|
||||||
|
|
||||||
internal static class DecodeParameterResolver
|
internal static class DecodeParameterResolver
|
||||||
{
|
{
|
||||||
public static DictionaryToken GetFilterParameters(DictionaryToken streamDictionary, int index)
|
public static DictionaryToken GetFilterParameters(DictionaryToken streamDictionary, int index)
|
||||||
@@ -18,9 +19,9 @@
|
|||||||
throw new ArgumentOutOfRangeException(nameof(index), "Index must be 0 or greater");
|
throw new ArgumentOutOfRangeException(nameof(index), "Index must be 0 or greater");
|
||||||
}
|
}
|
||||||
|
|
||||||
var filter = streamDictionary.GetDictionaryObject(NameToken.Filter, NameToken.F);
|
var filter = streamDictionary.GetObjectOrDefault(NameToken.Filter, NameToken.F);
|
||||||
|
|
||||||
var parameters = streamDictionary.GetDictionaryObject(NameToken.DecodeParms, NameToken.Dp);
|
var parameters = streamDictionary.GetObjectOrDefault(NameToken.DecodeParms, NameToken.Dp);
|
||||||
|
|
||||||
switch (filter)
|
switch (filter)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,8 +4,9 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Core;
|
using Core;
|
||||||
using Tokens;
|
using Tokens;
|
||||||
|
using UglyToad.PdfPig.Util;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The default implementation of the <see cref="T:UglyToad.PdfPig.Filters.IFilterProvider" />.
|
/// The default implementation of the <see cref="T:UglyToad.PdfPig.Filters.IFilterProvider" />.
|
||||||
@@ -60,7 +61,7 @@
|
|||||||
throw new ArgumentNullException(nameof(dictionary));
|
throw new ArgumentNullException(nameof(dictionary));
|
||||||
}
|
}
|
||||||
|
|
||||||
var token = dictionary.GetDictionaryObject(NameToken.Filter, NameToken.F);
|
var token = dictionary.GetObjectOrDefault(NameToken.Filter, NameToken.F);
|
||||||
if (token == null)
|
if (token == null)
|
||||||
{
|
{
|
||||||
return EmptyArray<IFilter>.Instance;
|
return EmptyArray<IFilter>.Instance;
|
||||||
|
|||||||
@@ -6,8 +6,9 @@
|
|||||||
using Core;
|
using Core;
|
||||||
using Parser.Parts;
|
using Parser.Parts;
|
||||||
using Tokenization.Scanner;
|
using Tokenization.Scanner;
|
||||||
using Tokens;
|
using Tokens;
|
||||||
|
using UglyToad.PdfPig.Util;
|
||||||
|
|
||||||
internal class FilterProviderWithLookup : ILookupFilterProvider
|
internal class FilterProviderWithLookup : ILookupFilterProvider
|
||||||
{
|
{
|
||||||
private readonly IFilterProvider inner;
|
private readonly IFilterProvider inner;
|
||||||
@@ -33,7 +34,7 @@
|
|||||||
throw new ArgumentNullException(nameof(dictionary));
|
throw new ArgumentNullException(nameof(dictionary));
|
||||||
}
|
}
|
||||||
|
|
||||||
var token = dictionary.GetDictionaryObject(NameToken.Filter, NameToken.F);
|
var token = dictionary.GetObjectOrDefault(NameToken.Filter, NameToken.F);
|
||||||
if (token == null)
|
if (token == null)
|
||||||
{
|
{
|
||||||
return EmptyArray<IFilter>.Instance;
|
return EmptyArray<IFilter>.Instance;
|
||||||
|
|||||||
@@ -47,49 +47,6 @@
|
|||||||
return typedToken;
|
return typedToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static IToken GetDictionaryObject(this DictionaryToken dictionary, NameToken name)
|
|
||||||
{
|
|
||||||
if (dictionary.TryGet(name, out var token))
|
|
||||||
{
|
|
||||||
return token;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static IToken GetDictionaryObject(this DictionaryToken dictionary, NameToken first, NameToken second)
|
|
||||||
{
|
|
||||||
if (dictionary.TryGet(first, out var token))
|
|
||||||
{
|
|
||||||
return token;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dictionary.TryGet(second, out token))
|
|
||||||
{
|
|
||||||
return token;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static int GetInt(this DictionaryToken dictionary, NameToken name, int defaultValue)
|
|
||||||
{
|
|
||||||
var numericToken = dictionary.GetDictionaryObject(name) as NumericToken;
|
|
||||||
return numericToken?.Int ?? defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static int GetInt(this DictionaryToken dictionary, NameToken first, NameToken second, int defaultValue)
|
|
||||||
{
|
|
||||||
var numericToken = dictionary.GetDictionaryObject(first, second) as NumericToken;
|
|
||||||
return numericToken?.Int ?? defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static bool GetBoolean(this DictionaryToken dictionary, NameToken name, bool defaultValue)
|
|
||||||
{
|
|
||||||
var booleanToken = dictionary.GetDictionaryObject(name) as BooleanToken;
|
|
||||||
return booleanToken?.Data ?? defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the decoded data from this stream.
|
/// Get the decoded data from this stream.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -75,9 +75,9 @@
|
|||||||
ILookupFilterProvider filterProvider,
|
ILookupFilterProvider filterProvider,
|
||||||
bool cannotRecurse = false)
|
bool cannotRecurse = false)
|
||||||
{
|
{
|
||||||
if (imageDictionary.GetDictionaryObject(NameToken.ImageMask, NameToken.Im) != null)
|
if (imageDictionary.GetObjectOrDefault(NameToken.ImageMask, NameToken.Im) != null)
|
||||||
{
|
{
|
||||||
var decodeRaw = imageDictionary.GetDictionaryObject(NameToken.Decode, NameToken.D) as ArrayToken
|
var decodeRaw = imageDictionary.GetObjectOrDefault(NameToken.Decode, NameToken.D) as ArrayToken
|
||||||
?? new ArrayToken(EmptyArray<IToken>.Instance);
|
?? new ArrayToken(EmptyArray<IToken>.Instance);
|
||||||
var decode = decodeRaw.Data.OfType<NumericToken>().Select(x => x.Data).ToArray();
|
var decode = decodeRaw.Data.OfType<NumericToken>().Select(x => x.Data).ToArray();
|
||||||
return IndexedColorSpaceDetails.Stencil(decode);
|
return IndexedColorSpaceDetails.Stencil(decode);
|
||||||
|
|||||||
@@ -9,6 +9,33 @@
|
|||||||
|
|
||||||
internal static class DictionaryTokenExtensions
|
internal static class DictionaryTokenExtensions
|
||||||
{
|
{
|
||||||
|
[CanBeNull]
|
||||||
|
public static IToken GetObjectOrDefault(this DictionaryToken token, NameToken name)
|
||||||
|
{
|
||||||
|
if (token.TryGet(name, out var obj))
|
||||||
|
{
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
[CanBeNull]
|
||||||
|
public static IToken GetObjectOrDefault(this DictionaryToken token, NameToken first, NameToken second)
|
||||||
|
{
|
||||||
|
if (token.TryGet(first, out var obj))
|
||||||
|
{
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token.TryGet(second, out obj))
|
||||||
|
{
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public static int GetInt(this DictionaryToken token, NameToken name)
|
public static int GetInt(this DictionaryToken token, NameToken name)
|
||||||
{
|
{
|
||||||
if (token == null)
|
if (token == null)
|
||||||
@@ -16,12 +43,9 @@
|
|||||||
throw new ArgumentNullException(nameof(token));
|
throw new ArgumentNullException(nameof(token));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!token.TryGet(name, out var keyedToken) || !(keyedToken is NumericToken numeric))
|
var numeric = token.GetObjectOrDefault(name) as NumericToken;
|
||||||
{
|
|
||||||
throw new PdfDocumentFormatException($"The dictionary did not contain a number with the key {name}. Dictionary way: {token}.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return numeric.Int;
|
return numeric?.Int ?? throw new PdfDocumentFormatException($"The dictionary did not contain a number with the key {name}. Dictionary way: {token}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int GetIntOrDefault(this DictionaryToken token, NameToken name, int defaultValue = 0)
|
public static int GetIntOrDefault(this DictionaryToken token, NameToken name, int defaultValue = 0)
|
||||||
@@ -31,12 +55,21 @@
|
|||||||
throw new ArgumentNullException(nameof(token));
|
throw new ArgumentNullException(nameof(token));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!token.TryGet(name, out var keyedToken) || !(keyedToken is NumericToken numeric))
|
var numeric = token.GetObjectOrDefault(name) as NumericToken;
|
||||||
|
|
||||||
|
return numeric?.Int ?? defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int GetIntOrDefault(this DictionaryToken token, NameToken first, NameToken second, int defaultValue = 0)
|
||||||
|
{
|
||||||
|
if (token == null)
|
||||||
{
|
{
|
||||||
return defaultValue;
|
throw new ArgumentNullException(nameof(token));
|
||||||
}
|
}
|
||||||
|
|
||||||
return numeric.Int;
|
var numeric = token.GetObjectOrDefault(first, second) as NumericToken;
|
||||||
|
|
||||||
|
return numeric?.Int ?? default;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long? GetLongOrDefault(this DictionaryToken token, NameToken name)
|
public static long? GetLongOrDefault(this DictionaryToken token, NameToken name)
|
||||||
@@ -46,12 +79,21 @@
|
|||||||
throw new ArgumentNullException(nameof(token));
|
throw new ArgumentNullException(nameof(token));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!token.TryGet(name, out var keyedToken) || !(keyedToken is NumericToken numeric))
|
var numeric = token.GetObjectOrDefault(name) as NumericToken;
|
||||||
|
|
||||||
|
return numeric?.Long;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool GetBooleanOrDefault(this DictionaryToken token, NameToken name, bool defaultValue)
|
||||||
|
{
|
||||||
|
if (token == null)
|
||||||
{
|
{
|
||||||
return null;
|
throw new ArgumentNullException(nameof(token));
|
||||||
}
|
}
|
||||||
|
|
||||||
return numeric.Long;
|
var boolean = token.GetObjectOrDefault(name) as BooleanToken;
|
||||||
|
|
||||||
|
return boolean?.Data ?? defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
[CanBeNull]
|
[CanBeNull]
|
||||||
@@ -62,12 +104,7 @@
|
|||||||
throw new ArgumentNullException(nameof(token));
|
throw new ArgumentNullException(nameof(token));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!token.TryGet(name, out var nameToken) || !(nameToken is NameToken result))
|
return token.GetObjectOrDefault(name) as NameToken;
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool TryGetOptionalTokenDirect<T>(this DictionaryToken token, NameToken name, IPdfTokenScanner scanner, out T result) where T : IToken
|
public static bool TryGetOptionalTokenDirect<T>(this DictionaryToken token, NameToken name, IPdfTokenScanner scanner, out T result) where T : IToken
|
||||||
|
|||||||
Reference in New Issue
Block a user