From 59264173c434cddb9778f3d03c2be85fa7e8605e Mon Sep 17 00:00:00 2001 From: BobLd <38405645+BobLd@users.noreply.github.com> Date: Sun, 19 Jan 2025 20:19:15 +0000 Subject: [PATCH] Refactor XObjectFactory --- .../XObjects/XObjectFactory.cs | 62 ++++++++++--------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/src/UglyToad.PdfPig/XObjects/XObjectFactory.cs b/src/UglyToad.PdfPig/XObjects/XObjectFactory.cs index dd3fc64ae..13b927fb8 100644 --- a/src/UglyToad.PdfPig/XObjects/XObjectFactory.cs +++ b/src/UglyToad.PdfPig/XObjects/XObjectFactory.cs @@ -1,7 +1,6 @@ namespace UglyToad.PdfPig.XObjects { using System; - using System.Collections.Generic; using System.Linq; using Content; using Core; @@ -21,7 +20,8 @@ public static class XObjectFactory /// /// Read the XObject image. /// - public static XObjectImage ReadImage(XObjectContentRecord xObject, IPdfTokenScanner pdfScanner, + public static XObjectImage ReadImage(XObjectContentRecord xObject, + IPdfTokenScanner pdfScanner, ILookupFilterProvider filterProvider, IResourceStore resourceStore) { @@ -42,11 +42,14 @@ public static XObjectImage ReadImage(XObjectContentRecord xObject, IPdfTokenScan var width = dictionary.Get(NameToken.Width, pdfScanner).Int; var height = dictionary.Get(NameToken.Height, pdfScanner).Int; - var isImageMask = dictionary.TryGet(NameToken.ImageMask, pdfScanner, out BooleanToken? isMaskToken) - && isMaskToken.Data; + bool isImageMask = false; + if (dictionary.TryGet(NameToken.ImageMask, pdfScanner, out BooleanToken? isMaskToken)) + { + dictionary = dictionary.With(NameToken.ImageMask, isMaskToken); + isImageMask = isMaskToken.Data; + } - var isJpxDecode = dictionary.TryGet(NameToken.Filter, out var token) - && token is NameToken filterName + var isJpxDecode = dictionary.TryGet(NameToken.Filter, pdfScanner, out NameToken filterName) && filterName.Equals(NameToken.JpxDecode); int bitsPerComponent = 0; @@ -65,7 +68,7 @@ public static XObjectImage ReadImage(XObjectContentRecord xObject, IPdfTokenScan } var intent = xObject.DefaultRenderingIntent; - if (dictionary.TryGet(NameToken.Intent, out NameToken renderingIntentToken)) + if (dictionary.TryGet(NameToken.Intent, pdfScanner, out NameToken renderingIntentToken)) { intent = renderingIntentToken.Data.ToRenderingIntent(); } @@ -73,35 +76,26 @@ public static XObjectImage ReadImage(XObjectContentRecord xObject, IPdfTokenScan var interpolate = dictionary.TryGet(NameToken.Interpolate, pdfScanner, out BooleanToken? interpolateToken) && interpolateToken.Data; - DictionaryToken? filterDictionary = xObject.Stream.StreamDictionary; - if (xObject.Stream.StreamDictionary.TryGet(NameToken.Filter, out var filterToken) - && filterToken is IndirectReferenceToken) + if (dictionary.TryGet(NameToken.Filter, out var filterToken) && filterToken is IndirectReferenceToken) { - if (filterDictionary.TryGet(NameToken.Filter, pdfScanner, out ArrayToken? filterArray)) + if (dictionary.TryGet(NameToken.Filter, pdfScanner, out ArrayToken? filterArray)) { - filterDictionary = filterDictionary.With(NameToken.Filter, filterArray); + dictionary = dictionary.With(NameToken.Filter, filterArray); } - else if (filterDictionary.TryGet(NameToken.Filter, pdfScanner, out NameToken? filterNameToken)) + else if (dictionary.TryGet(NameToken.Filter, pdfScanner, out NameToken? filterNameToken)) { - filterDictionary = filterDictionary.With(NameToken.Filter, filterNameToken); - } - else - { - filterDictionary = null; + dictionary = dictionary.With(NameToken.Filter, filterNameToken); } } - var supportsFilters = filterDictionary != null; - if (filterDictionary != null) + var supportsFilters = true; + var filters = filterProvider.GetFilters(dictionary, pdfScanner); + foreach (var filter in filters) { - var filters = filterProvider.GetFilters(filterDictionary, pdfScanner); - foreach (var filter in filters) + if (!filter.IsSupported) { - if (!filter.IsSupported) - { - supportsFilters = false; - break; - } + supportsFilters = false; + break; } } @@ -109,7 +103,19 @@ public static XObjectImage ReadImage(XObjectContentRecord xObject, IPdfTokenScan if (decodeParams is IndirectReferenceToken refToken) { dictionary = dictionary.With(NameToken.DecodeParms, pdfScanner.Get(refToken.Data).Data); - } + } + + var jbig2GlobalsParams = dictionary.GetObjectOrDefault(NameToken.Jbig2Globals); + if (jbig2GlobalsParams is IndirectReferenceToken jbig2RefToken) + { + dictionary = dictionary.With(NameToken.Jbig2Globals, pdfScanner.Get(jbig2RefToken.Data).Data); + } + + var imParams = dictionary.GetObjectOrDefault(NameToken.Im); + if (imParams is IndirectReferenceToken imRefToken) + { + dictionary = dictionary.With(NameToken.Im, pdfScanner.Get(imRefToken.Data).Data); + } var streamToken = new StreamToken(dictionary, xObject.Stream.Data);