Skip to content

Commit

Permalink
Refactor XObjectFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
BobLd committed Jan 19, 2025
1 parent 24b9a99 commit b7e2245
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions src/UglyToad.PdfPig/XObjects/XObjectFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace UglyToad.PdfPig.XObjects
{
using System;
using System.Collections.Generic;
using System.Linq;
using Content;
using Core;
Expand All @@ -21,7 +20,8 @@ public static class XObjectFactory
/// <summary>
/// Read the XObject image.
/// </summary>
public static XObjectImage ReadImage(XObjectContentRecord xObject, IPdfTokenScanner pdfScanner,
public static XObjectImage ReadImage(XObjectContentRecord xObject,
IPdfTokenScanner pdfScanner,
ILookupFilterProvider filterProvider,
IResourceStore resourceStore)
{
Expand All @@ -42,11 +42,14 @@ public static XObjectImage ReadImage(XObjectContentRecord xObject, IPdfTokenScan
var width = dictionary.Get<NumericToken>(NameToken.Width, pdfScanner).Int;
var height = dictionary.Get<NumericToken>(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)

Check warning on line 52 in src/UglyToad.PdfPig/XObjects/XObjectFactory.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 52 in src/UglyToad.PdfPig/XObjects/XObjectFactory.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 52 in src/UglyToad.PdfPig/XObjects/XObjectFactory.cs

View workflow job for this annotation

GitHub Actions / tests / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 52 in src/UglyToad.PdfPig/XObjects/XObjectFactory.cs

View workflow job for this annotation

GitHub Actions / build_and_publish_nightly

Converting null literal or possible null value to non-nullable type.

Check warning on line 52 in src/UglyToad.PdfPig/XObjects/XObjectFactory.cs

View workflow job for this annotation

GitHub Actions / tests / build

Converting null literal or possible null value to non-nullable type.
&& filterName.Equals(NameToken.JpxDecode);

int bitsPerComponent = 0;
Expand All @@ -65,51 +68,54 @@ 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))

Check warning on line 71 in src/UglyToad.PdfPig/XObjects/XObjectFactory.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 71 in src/UglyToad.PdfPig/XObjects/XObjectFactory.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 71 in src/UglyToad.PdfPig/XObjects/XObjectFactory.cs

View workflow job for this annotation

GitHub Actions / tests / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 71 in src/UglyToad.PdfPig/XObjects/XObjectFactory.cs

View workflow job for this annotation

GitHub Actions / build_and_publish_nightly

Converting null literal or possible null value to non-nullable type.

Check warning on line 71 in src/UglyToad.PdfPig/XObjects/XObjectFactory.cs

View workflow job for this annotation

GitHub Actions / tests / build

Converting null literal or possible null value to non-nullable type.
{
intent = renderingIntentToken.Data.ToRenderingIntent();
}

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;
}
}

var decodeParams = dictionary.GetObjectOrDefault(NameToken.DecodeParms, NameToken.Dp);
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);

Expand Down

0 comments on commit b7e2245

Please sign in to comment.