#11 early access to the raw xobjects for images.

temporary 'safe' untested implementation of seac for type 1 charstrings.
make structure public
bump version of package and project to 0.0.3 (it had accidentally increased to 0.0.5)
This commit is contained in:
Eliot Jones
2018-11-26 19:46:41 +00:00
parent 48fa4a4f15
commit 997979cc92
18 changed files with 199 additions and 46 deletions

View File

@@ -0,0 +1,37 @@
namespace UglyToad.PdfPig.XObjects
{
using System;
using Graphics;
using Tokenization.Scanner;
using Tokens;
internal class XObjectFactory
{
public XObjectImage CreateImage(XObjectContentRecord xObject, IPdfTokenScanner pdfScanner, bool isLenientParsing)
{
if (xObject == null)
{
throw new ArgumentNullException(nameof(xObject));
}
if (xObject.Type != XObjectType.Image)
{
throw new InvalidOperationException($"Cannot create an image from an XObject with type: {xObject.Type}.");
}
var width = xObject.Stream.StreamDictionary.Get<NumericToken>(NameToken.Width, pdfScanner).Int;
var height = xObject.Stream.StreamDictionary.Get<NumericToken>(NameToken.Height, pdfScanner).Int;
var isJpxDecode = xObject.Stream.StreamDictionary.TryGet(NameToken.Filter, out var token)
&& token is NameToken filterName
&& filterName.Equals(NameToken.JpxDecode);
var isImageMask = xObject.Stream.StreamDictionary.TryGet(NameToken.ImageMask, out var maskToken)
&& maskToken is BooleanToken maskBoolean
&& maskBoolean.Data;
return new XObjectImage(width, height, isJpxDecode, isImageMask, xObject.Stream.StreamDictionary,
xObject.Stream.Data);
}
}
}

View File

@@ -0,0 +1,67 @@
namespace UglyToad.PdfPig.XObjects
{
using System;
using System.Collections.Generic;
using Tokens;
using Util.JetBrains.Annotations;
/// <summary>
/// The raw stream from a PDF document representing an image XObject.
/// </summary>
public class XObjectImage
{
/// <summary>
/// The width of the image in samples.
/// </summary>
public int Width { get; }
/// <summary>
/// The height of the image in samples.
/// </summary>
public int Height { get; }
/// <summary>
/// The JPX filter encodes data using the JPEG2000 compression method.
/// A JPEG2000 data stream allows different versions of the image to be decoded
/// allowing for thumbnails to be extracted.
/// </summary>
public bool IsJpxEncoded { get; }
/// <summary>
/// Whether this image should be treated as an image maske.
/// </summary>
public bool IsImageMask { get; }
/// <summary>
/// The full dictionary for this Image XObject.
/// </summary>
[NotNull]
public DictionaryToken ImageDictionary { get; }
/// <summary>
/// The encoded bytes of this image, must be decoded via any
/// filters defined in the <see cref="ImageDictionary"/> prior to consumption.
/// </summary>
[NotNull]
public IReadOnlyList<byte> Bytes { get; }
/// <summary>
/// Creates a new <see cref="XObjectImage"/>.
/// </summary>
internal XObjectImage(int width, int height, bool isJpxEncoded, bool isImageMask, DictionaryToken imageDictionary, IReadOnlyList<byte> bytes)
{
Width = width;
Height = height;
IsJpxEncoded = isJpxEncoded;
IsImageMask = isImageMask;
ImageDictionary = imageDictionary ?? throw new ArgumentNullException(nameof(imageDictionary));
Bytes = bytes ?? throw new ArgumentNullException(nameof(bytes));
}
/// <inheritdoc />
public override string ToString()
{
return ImageDictionary.ToString();
}
}
}

View File

@@ -0,0 +1,9 @@
namespace UglyToad.PdfPig.XObjects
{
internal enum XObjectType
{
Image,
Form,
PostScript
}
}