2017-02-22 21:10:57 +00:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms and conditions defined in
|
|
|
|
* file 'LICENSE.md', which is part of this source code package.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2017-03-01 16:02:53 +00:00
|
|
|
"github.com/unidoc/unidoc/common"
|
2017-02-22 21:10:57 +00:00
|
|
|
. "github.com/unidoc/unidoc/pdf/core"
|
|
|
|
)
|
|
|
|
|
2017-03-01 16:02:53 +00:00
|
|
|
// XObjectForm (Table 95 in 8.10.2).
|
|
|
|
type XObjectForm struct {
|
|
|
|
Filter StreamEncoder
|
|
|
|
|
|
|
|
FormType PdfObject
|
|
|
|
BBox PdfObject
|
|
|
|
Matrix PdfObject
|
2017-04-04 12:09:26 +00:00
|
|
|
Resources *PdfPageResources
|
2017-03-01 16:02:53 +00:00
|
|
|
Group PdfObject
|
|
|
|
Ref PdfObject
|
|
|
|
MetaData PdfObject
|
|
|
|
PieceInfo PdfObject
|
|
|
|
LastModified PdfObject
|
|
|
|
StructParent PdfObject
|
|
|
|
StructParents PdfObject
|
|
|
|
OPI PdfObject
|
|
|
|
OC PdfObject
|
|
|
|
Name PdfObject
|
2017-03-04 17:27:23 +00:00
|
|
|
|
2017-03-01 16:02:53 +00:00
|
|
|
// Stream data.
|
|
|
|
Stream []byte
|
|
|
|
// Primitive
|
|
|
|
primitive *PdfObjectStream
|
|
|
|
}
|
|
|
|
|
2018-12-09 20:22:33 +02:00
|
|
|
// NewXObjectForm creates a brand new XObject Form. Creates a new underlying PDF object stream primitive.
|
2017-03-01 16:02:53 +00:00
|
|
|
func NewXObjectForm() *XObjectForm {
|
|
|
|
xobj := &XObjectForm{}
|
|
|
|
stream := &PdfObjectStream{}
|
2017-07-08 21:04:13 +00:00
|
|
|
stream.PdfObjectDictionary = MakeDict()
|
2017-03-01 16:02:53 +00:00
|
|
|
xobj.primitive = stream
|
|
|
|
return xobj
|
|
|
|
}
|
|
|
|
|
2018-12-09 20:22:33 +02:00
|
|
|
// NewXObjectFormFromStream builds the Form XObject from a stream object.
|
|
|
|
// TODO: Should this be exposed? Consider different access points.
|
2017-03-01 16:02:53 +00:00
|
|
|
func NewXObjectFormFromStream(stream *PdfObjectStream) (*XObjectForm, error) {
|
|
|
|
form := &XObjectForm{}
|
|
|
|
form.primitive = stream
|
|
|
|
|
|
|
|
dict := *(stream.PdfObjectDictionary)
|
|
|
|
|
|
|
|
encoder, err := NewEncoderFromStream(stream)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
form.Filter = encoder
|
|
|
|
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("Subtype"); obj != nil {
|
2017-03-01 16:02:53 +00:00
|
|
|
name, ok := obj.(*PdfObjectName)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Type error")
|
|
|
|
}
|
|
|
|
if *name != "Form" {
|
|
|
|
common.Log.Debug("Invalid form subtype")
|
|
|
|
return nil, errors.New("Invalid form subtype")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("FormType"); obj != nil {
|
2017-03-01 16:02:53 +00:00
|
|
|
form.FormType = obj
|
|
|
|
}
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("BBox"); obj != nil {
|
2017-03-01 16:02:53 +00:00
|
|
|
form.BBox = obj
|
|
|
|
}
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("Matrix"); obj != nil {
|
2017-03-01 16:02:53 +00:00
|
|
|
form.Matrix = obj
|
|
|
|
}
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := dict.Get("Resources"); obj != nil {
|
2017-03-04 17:27:23 +00:00
|
|
|
obj = TraceToDirectObject(obj)
|
|
|
|
d, ok := obj.(*PdfObjectDictionary)
|
|
|
|
if !ok {
|
|
|
|
common.Log.Debug("Invalid XObject Form Resources object, pointing to non-dictionary")
|
2018-06-27 16:48:32 +10:00
|
|
|
return nil, ErrTypeError
|
2017-03-04 17:27:23 +00:00
|
|
|
}
|
|
|
|
res, err := NewPdfPageResourcesFromDict(d)
|
|
|
|
if err != nil {
|
|
|
|
common.Log.Debug("Failed getting form resources")
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-04 12:09:26 +00:00
|
|
|
form.Resources = res
|
|
|
|
common.Log.Trace("Form resources: %#v", form.Resources)
|
2017-03-01 16:02:53 +00:00
|
|
|
}
|
2017-03-04 17:27:23 +00:00
|
|
|
|
2017-07-08 21:04:13 +00:00
|
|
|
form.Group = dict.Get("Group")
|
|
|
|
form.Ref = dict.Get("Ref")
|
|
|
|
form.MetaData = dict.Get("MetaData")
|
|
|
|
form.PieceInfo = dict.Get("PieceInfo")
|
|
|
|
form.LastModified = dict.Get("LastModified")
|
|
|
|
form.StructParent = dict.Get("StructParent")
|
|
|
|
form.StructParents = dict.Get("StructParents")
|
|
|
|
form.OPI = dict.Get("OPI")
|
|
|
|
form.OC = dict.Get("OC")
|
|
|
|
form.Name = dict.Get("Name")
|
2017-03-01 16:02:53 +00:00
|
|
|
|
|
|
|
form.Stream = stream.Stream
|
|
|
|
|
|
|
|
return form, nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:59:19 +00:00
|
|
|
// GetContainingPdfObject returns the XObject Form's containing object (indirect object).
|
2017-03-01 16:02:53 +00:00
|
|
|
func (xform *XObjectForm) GetContainingPdfObject() PdfObject {
|
|
|
|
return xform.primitive
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:59:19 +00:00
|
|
|
// GetContentStream returns the XObject Form's content stream.
|
2017-03-01 16:02:53 +00:00
|
|
|
func (xform *XObjectForm) GetContentStream() ([]byte, error) {
|
|
|
|
decoded, err := DecodeStream(xform.primitive)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return decoded, nil
|
|
|
|
}
|
|
|
|
|
2018-12-09 20:22:33 +02:00
|
|
|
// SetContentStream updates the content stream with specified encoding. If encoding is null, will use the xform.Filter
|
|
|
|
// object or Raw encoding if not set.
|
2017-04-09 16:49:15 +00:00
|
|
|
func (xform *XObjectForm) SetContentStream(content []byte, encoder StreamEncoder) error {
|
2017-03-14 13:04:51 +00:00
|
|
|
encoded := content
|
2017-04-09 16:49:15 +00:00
|
|
|
|
|
|
|
if encoder == nil {
|
|
|
|
if xform.Filter != nil {
|
|
|
|
encoder = xform.Filter
|
|
|
|
} else {
|
|
|
|
encoder = NewRawEncoder()
|
2017-03-14 13:04:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-09 16:49:15 +00:00
|
|
|
enc, err := encoder.EncodeBytes(encoded)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
encoded = enc
|
|
|
|
|
2017-04-04 12:09:26 +00:00
|
|
|
xform.Stream = encoded
|
2018-10-03 22:35:58 +00:00
|
|
|
xform.Filter = encoder
|
2017-03-14 13:04:51 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-11 04:37:00 +02:00
|
|
|
// ToPdfObject returns a stream object.
|
2017-03-01 16:02:53 +00:00
|
|
|
func (xform *XObjectForm) ToPdfObject() PdfObject {
|
|
|
|
stream := xform.primitive
|
|
|
|
|
|
|
|
dict := stream.PdfObjectDictionary
|
|
|
|
if xform.Filter != nil {
|
2017-04-04 12:09:26 +00:00
|
|
|
// Pre-populate the stream dictionary with the encoding related fields.
|
2017-03-01 16:02:53 +00:00
|
|
|
dict = xform.Filter.MakeStreamDict()
|
|
|
|
stream.PdfObjectDictionary = dict
|
|
|
|
}
|
|
|
|
dict.Set("Type", MakeName("XObject"))
|
|
|
|
dict.Set("Subtype", MakeName("Form"))
|
|
|
|
|
|
|
|
dict.SetIfNotNil("FormType", xform.FormType)
|
|
|
|
dict.SetIfNotNil("BBox", xform.BBox)
|
|
|
|
dict.SetIfNotNil("Matrix", xform.Matrix)
|
2017-04-04 12:09:26 +00:00
|
|
|
if xform.Resources != nil {
|
|
|
|
dict.SetIfNotNil("Resources", xform.Resources.ToPdfObject())
|
|
|
|
}
|
2017-03-01 16:02:53 +00:00
|
|
|
dict.SetIfNotNil("Group", xform.Group)
|
|
|
|
dict.SetIfNotNil("Ref", xform.Ref)
|
|
|
|
dict.SetIfNotNil("MetaData", xform.MetaData)
|
|
|
|
dict.SetIfNotNil("PieceInfo", xform.PieceInfo)
|
|
|
|
dict.SetIfNotNil("LastModified", xform.LastModified)
|
|
|
|
dict.SetIfNotNil("StructParent", xform.StructParent)
|
|
|
|
dict.SetIfNotNil("StructParents", xform.StructParents)
|
|
|
|
dict.SetIfNotNil("OPI", xform.OPI)
|
|
|
|
dict.SetIfNotNil("OC", xform.OC)
|
|
|
|
dict.SetIfNotNil("Name", xform.Name)
|
|
|
|
|
|
|
|
dict.Set("Length", MakeInteger(int64(len(xform.Stream))))
|
|
|
|
stream.Stream = xform.Stream
|
|
|
|
|
|
|
|
return stream
|
|
|
|
}
|
|
|
|
|
2017-02-22 21:10:57 +00:00
|
|
|
// XObjectImage (Table 89 in 8.9.5.1).
|
|
|
|
// Implements PdfModel interface.
|
|
|
|
type XObjectImage struct {
|
|
|
|
//ColorSpace PdfObject
|
|
|
|
Width *int64
|
|
|
|
Height *int64
|
|
|
|
ColorSpace PdfColorspace
|
|
|
|
BitsPerComponent *int64
|
2017-03-01 16:02:53 +00:00
|
|
|
Filter StreamEncoder
|
|
|
|
|
2017-02-24 17:38:41 +00:00
|
|
|
Intent PdfObject
|
|
|
|
ImageMask PdfObject
|
|
|
|
Mask PdfObject
|
2018-05-02 17:14:31 +10:00
|
|
|
Matte PdfObject
|
2017-02-24 17:38:41 +00:00
|
|
|
Decode PdfObject
|
|
|
|
Interpolate PdfObject
|
|
|
|
Alternatives PdfObject
|
|
|
|
SMask PdfObject
|
|
|
|
SMaskInData PdfObject
|
2017-06-19 16:43:17 +00:00
|
|
|
Name PdfObject // Obsolete. Currently read if available and write if available. Not setting on new created files.
|
2017-02-24 17:38:41 +00:00
|
|
|
StructParent PdfObject
|
|
|
|
ID PdfObject
|
|
|
|
OPI PdfObject
|
|
|
|
Metadata PdfObject
|
|
|
|
OC PdfObject
|
|
|
|
Stream []byte
|
2017-02-22 21:10:57 +00:00
|
|
|
// Primitive
|
|
|
|
primitive *PdfObjectStream
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewXObjectImage() *XObjectImage {
|
|
|
|
xobj := &XObjectImage{}
|
|
|
|
stream := &PdfObjectStream{}
|
2017-07-08 21:04:13 +00:00
|
|
|
stream.PdfObjectDictionary = MakeDict()
|
2017-02-22 21:10:57 +00:00
|
|
|
xobj.primitive = stream
|
|
|
|
return xobj
|
|
|
|
}
|
|
|
|
|
2018-12-09 20:22:33 +02:00
|
|
|
// NewXObjectImageFromImage creates a new XObject Image from an image object with default options.
|
2017-04-19 11:46:53 +00:00
|
|
|
// If encoder is nil, uses raw encoding (none).
|
2017-06-19 16:43:17 +00:00
|
|
|
func NewXObjectImageFromImage(img *Image, cs PdfColorspace, encoder StreamEncoder) (*XObjectImage, error) {
|
2017-02-22 21:10:57 +00:00
|
|
|
xobj := NewXObjectImage()
|
2018-01-11 16:25:04 +11:00
|
|
|
return UpdateXObjectImageFromImage(xobj, img, cs, encoder)
|
|
|
|
}
|
|
|
|
|
2018-05-02 17:14:31 +10:00
|
|
|
// UpdateXObjectImageFromImage creates a new XObject Image from an Image object `img` and default
|
|
|
|
// masks from xobjIn.
|
|
|
|
// The default masks are overriden if img.hasAlpha
|
|
|
|
// If `encoder` is nil, uses raw encoding (none).
|
|
|
|
func UpdateXObjectImageFromImage(xobjIn *XObjectImage, img *Image, cs PdfColorspace,
|
|
|
|
encoder StreamEncoder) (*XObjectImage, error) {
|
2018-04-27 10:06:36 +10:00
|
|
|
xobj := NewXObjectImage()
|
2017-02-22 21:10:57 +00:00
|
|
|
|
2017-04-19 11:46:53 +00:00
|
|
|
if encoder == nil {
|
|
|
|
encoder = NewRawEncoder()
|
|
|
|
}
|
|
|
|
|
|
|
|
encoded, err := encoder.EncodeBytes(img.Data)
|
|
|
|
if err != nil {
|
|
|
|
common.Log.Debug("Error with encoding: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
xobj.Filter = encoder
|
|
|
|
xobj.Stream = encoded
|
2017-02-22 21:10:57 +00:00
|
|
|
|
|
|
|
// Width and height.
|
|
|
|
imWidth := img.Width
|
|
|
|
imHeight := img.Height
|
|
|
|
xobj.Width = &imWidth
|
|
|
|
xobj.Height = &imHeight
|
|
|
|
|
|
|
|
// Bits.
|
|
|
|
xobj.BitsPerComponent = &img.BitsPerComponent
|
|
|
|
|
2017-03-14 13:04:51 +00:00
|
|
|
// Guess colorspace if not explicitly set.
|
|
|
|
if cs == nil {
|
|
|
|
if img.ColorComponents == 1 {
|
|
|
|
xobj.ColorSpace = NewPdfColorspaceDeviceGray()
|
|
|
|
} else if img.ColorComponents == 3 {
|
|
|
|
xobj.ColorSpace = NewPdfColorspaceDeviceRGB()
|
|
|
|
} else if img.ColorComponents == 4 {
|
|
|
|
xobj.ColorSpace = NewPdfColorspaceDeviceCMYK()
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("Colorspace undefined")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
xobj.ColorSpace = cs
|
2017-06-06 21:16:55 +00:00
|
|
|
}
|
2017-02-22 21:10:57 +00:00
|
|
|
|
2017-06-06 21:16:55 +00:00
|
|
|
if img.hasAlpha {
|
|
|
|
// Add the alpha channel information as a stencil mask (SMask).
|
|
|
|
// Has same width and height as original and stored in same
|
|
|
|
// bits per component (1 component, hence the DeviceGray channel).
|
|
|
|
smask := NewXObjectImage()
|
|
|
|
smask.Filter = encoder
|
|
|
|
encoded, err := encoder.EncodeBytes(img.alphaData)
|
|
|
|
if err != nil {
|
|
|
|
common.Log.Debug("Error with encoding: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
smask.Stream = encoded
|
|
|
|
smask.BitsPerComponent = &img.BitsPerComponent
|
|
|
|
smask.Width = &img.Width
|
|
|
|
smask.Height = &img.Height
|
|
|
|
smask.ColorSpace = NewPdfColorspaceDeviceGray()
|
|
|
|
xobj.SMask = smask.ToPdfObject()
|
2018-05-02 17:14:31 +10:00
|
|
|
} else {
|
|
|
|
xobj.SMask = xobjIn.SMask
|
|
|
|
xobj.ImageMask = xobjIn.ImageMask
|
|
|
|
if xobj.ColorSpace.GetNumComponents() == 1 {
|
|
|
|
smaskMatteToGray(xobj)
|
|
|
|
}
|
2017-03-14 13:04:51 +00:00
|
|
|
}
|
2017-02-22 21:10:57 +00:00
|
|
|
|
|
|
|
return xobj, nil
|
|
|
|
}
|
|
|
|
|
2018-05-02 17:14:31 +10:00
|
|
|
// smaskMatteToGray converts to gray the Matte value in the SMask image referenced by `xobj` (if
|
|
|
|
// there is one)
|
|
|
|
func smaskMatteToGray(xobj *XObjectImage) error {
|
|
|
|
if xobj.SMask == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-05 18:07:09 +10:00
|
|
|
stream, ok := xobj.SMask.(*PdfObjectStream)
|
|
|
|
if !ok {
|
|
|
|
common.Log.Debug("SMask is not *PdfObjectStream")
|
2018-06-27 16:48:32 +10:00
|
|
|
return ErrTypeError
|
2018-05-05 18:07:09 +10:00
|
|
|
}
|
2018-05-02 17:14:31 +10:00
|
|
|
dict := stream.PdfObjectDictionary
|
|
|
|
matte := dict.Get("Matte")
|
|
|
|
if matte == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gray, err := toGray(matte.(*PdfObjectArray))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
grayMatte := MakeArrayFromFloats([]float64{gray})
|
|
|
|
dict.SetIfNotNil("Matte", grayMatte)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// toGray converts a 1, 3 or 4 dimensional color `matte` to gray
|
|
|
|
// If `matte` is not a 1, 3 or 4 dimensional color then an error is returned
|
|
|
|
func toGray(matte *PdfObjectArray) (float64, error) {
|
|
|
|
colors, err := matte.ToFloat64Array()
|
|
|
|
if err != nil {
|
|
|
|
common.Log.Debug("Bad Matte array: matte=%s err=%v", matte, err)
|
|
|
|
}
|
|
|
|
switch len(colors) {
|
|
|
|
case 1:
|
|
|
|
return colors[0], nil
|
|
|
|
case 3:
|
|
|
|
cs := PdfColorspaceDeviceRGB{}
|
|
|
|
rgbColor, err := cs.ColorFromFloats(colors)
|
|
|
|
if err != nil {
|
|
|
|
return 0.0, err
|
|
|
|
}
|
|
|
|
return rgbColor.(*PdfColorDeviceRGB).ToGray().Val(), nil
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
cs := PdfColorspaceDeviceCMYK{}
|
|
|
|
cmykColor, err := cs.ColorFromFloats(colors)
|
|
|
|
if err != nil {
|
|
|
|
return 0.0, err
|
|
|
|
}
|
|
|
|
rgbColor, err := cs.ColorToRGB(cmykColor.(*PdfColorDeviceCMYK))
|
|
|
|
if err != nil {
|
|
|
|
return 0.0, err
|
|
|
|
}
|
|
|
|
return rgbColor.(*PdfColorDeviceRGB).ToGray().Val(), nil
|
|
|
|
}
|
|
|
|
err = errors.New("Bad Matte color")
|
|
|
|
common.Log.Error("toGray: matte=%s err=%v", matte, err)
|
|
|
|
return 0.0, err
|
|
|
|
}
|
|
|
|
|
2018-12-11 04:37:00 +02:00
|
|
|
// NewXObjectImageFromStream builds the image xobject from a stream object.
|
2017-02-22 21:10:57 +00:00
|
|
|
// An image dictionary is the dictionary portion of a stream object representing an image XObject.
|
|
|
|
func NewXObjectImageFromStream(stream *PdfObjectStream) (*XObjectImage, error) {
|
|
|
|
img := &XObjectImage{}
|
|
|
|
img.primitive = stream
|
|
|
|
|
|
|
|
dict := *(stream.PdfObjectDictionary)
|
|
|
|
|
2017-02-24 17:38:41 +00:00
|
|
|
encoder, err := NewEncoderFromStream(stream)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-02-22 21:10:57 +00:00
|
|
|
}
|
2017-02-24 17:38:41 +00:00
|
|
|
img.Filter = encoder
|
2017-02-22 21:10:57 +00:00
|
|
|
|
2017-08-04 17:47:55 +10:00
|
|
|
if obj := TraceToDirectObject(dict.Get("Width")); obj != nil {
|
2017-02-22 21:10:57 +00:00
|
|
|
iObj, ok := obj.(*PdfObjectInteger)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Invalid image width object")
|
|
|
|
}
|
|
|
|
iVal := int64(*iObj)
|
|
|
|
img.Width = &iVal
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("Width missing")
|
|
|
|
}
|
|
|
|
|
2017-08-04 17:47:55 +10:00
|
|
|
if obj := TraceToDirectObject(dict.Get("Height")); obj != nil {
|
2017-02-22 21:10:57 +00:00
|
|
|
iObj, ok := obj.(*PdfObjectInteger)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Invalid image height object")
|
|
|
|
}
|
|
|
|
iVal := int64(*iObj)
|
|
|
|
img.Height = &iVal
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("Height missing")
|
|
|
|
}
|
|
|
|
|
2017-08-04 17:47:55 +10:00
|
|
|
if obj := TraceToDirectObject(dict.Get("ColorSpace")); obj != nil {
|
2017-08-10 07:23:42 +00:00
|
|
|
cs, err := NewPdfColorspaceFromPdfObject(obj)
|
2017-02-22 21:10:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
img.ColorSpace = cs
|
2017-03-05 22:41:38 +00:00
|
|
|
} else {
|
|
|
|
// If not specified, assume gray..
|
|
|
|
common.Log.Debug("XObject Image colorspace not specified - assuming 1 color component")
|
|
|
|
img.ColorSpace = NewPdfColorspaceDeviceGray()
|
2017-02-22 21:10:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 17:47:55 +10:00
|
|
|
if obj := TraceToDirectObject(dict.Get("BitsPerComponent")); obj != nil {
|
2017-02-22 21:10:57 +00:00
|
|
|
iObj, ok := obj.(*PdfObjectInteger)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Invalid image height object")
|
|
|
|
}
|
|
|
|
iVal := int64(*iObj)
|
|
|
|
img.BitsPerComponent = &iVal
|
|
|
|
}
|
|
|
|
|
2017-07-08 21:04:13 +00:00
|
|
|
img.Intent = dict.Get("Intent")
|
|
|
|
img.ImageMask = dict.Get("ImageMask")
|
|
|
|
img.Mask = dict.Get("Mask")
|
|
|
|
img.Decode = dict.Get("Decode")
|
|
|
|
img.Interpolate = dict.Get("Interpolate")
|
|
|
|
img.Alternatives = dict.Get("Alternatives")
|
|
|
|
img.SMask = dict.Get("SMask")
|
|
|
|
img.SMaskInData = dict.Get("SMaskInData")
|
2018-05-02 17:14:31 +10:00
|
|
|
img.Matte = dict.Get("Matte")
|
2017-07-08 21:04:13 +00:00
|
|
|
img.Name = dict.Get("Name")
|
|
|
|
img.StructParent = dict.Get("StructParent")
|
|
|
|
img.ID = dict.Get("ID")
|
|
|
|
img.OPI = dict.Get("OPI")
|
|
|
|
img.Metadata = dict.Get("Metadata")
|
|
|
|
img.OC = dict.Get("OC")
|
2017-02-22 21:10:57 +00:00
|
|
|
|
|
|
|
img.Stream = stream.Stream
|
|
|
|
|
|
|
|
return img, nil
|
|
|
|
}
|
|
|
|
|
2018-12-11 04:37:00 +02:00
|
|
|
// SetImage updates XObject Image with new image data.
|
2017-03-14 13:04:51 +00:00
|
|
|
func (ximg *XObjectImage) SetImage(img *Image, cs PdfColorspace) error {
|
|
|
|
encoded, err := ximg.Filter.EncodeBytes(img.Data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ximg.Stream = encoded
|
|
|
|
|
|
|
|
// Width, height and bits.
|
|
|
|
ximg.Width = &img.Width
|
|
|
|
ximg.Height = &img.Height
|
|
|
|
ximg.BitsPerComponent = &img.BitsPerComponent
|
|
|
|
|
|
|
|
// Guess colorspace if not explicitly set.
|
|
|
|
if cs == nil {
|
|
|
|
if img.ColorComponents == 1 {
|
|
|
|
ximg.ColorSpace = NewPdfColorspaceDeviceGray()
|
|
|
|
} else if img.ColorComponents == 3 {
|
|
|
|
ximg.ColorSpace = NewPdfColorspaceDeviceRGB()
|
|
|
|
} else if img.ColorComponents == 4 {
|
|
|
|
ximg.ColorSpace = NewPdfColorspaceDeviceCMYK()
|
|
|
|
} else {
|
|
|
|
return errors.New("Colorspace undefined")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ximg.ColorSpace = cs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-09 20:22:33 +02:00
|
|
|
// SetFilter sets compression filter. Decodes with current filter sets and encodes the data with the new filter.
|
2017-04-19 11:46:53 +00:00
|
|
|
func (ximg *XObjectImage) SetFilter(encoder StreamEncoder) error {
|
|
|
|
encoded := ximg.Stream
|
|
|
|
decoded, err := ximg.Filter.DecodeBytes(encoded)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ximg.Filter = encoder
|
|
|
|
encoded, err = encoder.EncodeBytes(decoded)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ximg.Stream = encoded
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-09 20:22:33 +02:00
|
|
|
// ToImage converts an object to an Image which can be transformed or saved out.
|
2017-02-24 17:38:41 +00:00
|
|
|
// The image data is decoded and the Image returned.
|
2017-02-22 21:10:57 +00:00
|
|
|
func (ximg *XObjectImage) ToImage() (*Image, error) {
|
|
|
|
image := &Image{}
|
|
|
|
|
|
|
|
if ximg.Height == nil {
|
|
|
|
return nil, errors.New("Height attribute missing")
|
|
|
|
}
|
|
|
|
image.Height = *ximg.Height
|
|
|
|
|
|
|
|
if ximg.Width == nil {
|
|
|
|
return nil, errors.New("Width attribute missing")
|
|
|
|
}
|
|
|
|
image.Width = *ximg.Width
|
|
|
|
|
|
|
|
if ximg.BitsPerComponent == nil {
|
|
|
|
return nil, errors.New("Bits per component missing")
|
|
|
|
}
|
|
|
|
image.BitsPerComponent = *ximg.BitsPerComponent
|
|
|
|
|
2017-03-05 22:41:38 +00:00
|
|
|
image.ColorComponents = ximg.ColorSpace.GetNumComponents()
|
2017-03-04 17:27:23 +00:00
|
|
|
|
2017-02-22 21:10:57 +00:00
|
|
|
decoded, err := DecodeStream(ximg.primitive)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
image.Data = decoded
|
|
|
|
|
2017-03-05 22:41:38 +00:00
|
|
|
if ximg.Decode != nil {
|
|
|
|
darr, ok := ximg.Decode.(*PdfObjectArray)
|
|
|
|
if !ok {
|
|
|
|
common.Log.Debug("Invalid Decode object")
|
|
|
|
return nil, errors.New("Invalid type")
|
|
|
|
}
|
|
|
|
decode, err := darr.ToFloat64Array()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
image.decode = decode
|
|
|
|
}
|
|
|
|
|
2017-02-24 17:38:41 +00:00
|
|
|
return image, nil
|
2017-02-22 21:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ximg *XObjectImage) GetContainingPdfObject() PdfObject {
|
|
|
|
return ximg.primitive
|
|
|
|
}
|
|
|
|
|
2018-12-11 04:37:00 +02:00
|
|
|
// ToPdfObject returns a stream object.
|
2017-02-22 21:10:57 +00:00
|
|
|
func (ximg *XObjectImage) ToPdfObject() PdfObject {
|
|
|
|
stream := ximg.primitive
|
|
|
|
|
|
|
|
dict := stream.PdfObjectDictionary
|
2017-02-24 17:38:41 +00:00
|
|
|
if ximg.Filter != nil {
|
|
|
|
//dict.Set("Filter", ximg.Filter)
|
|
|
|
// Pre-populate the stream dictionary with the
|
|
|
|
// encoding related fields.
|
|
|
|
dict = ximg.Filter.MakeStreamDict()
|
|
|
|
stream.PdfObjectDictionary = dict
|
|
|
|
}
|
2017-02-22 21:10:57 +00:00
|
|
|
dict.Set("Type", MakeName("XObject"))
|
|
|
|
dict.Set("Subtype", MakeName("Image"))
|
|
|
|
dict.Set("Width", MakeInteger(*(ximg.Width)))
|
|
|
|
dict.Set("Height", MakeInteger(*(ximg.Height)))
|
|
|
|
|
|
|
|
if ximg.BitsPerComponent != nil {
|
|
|
|
dict.Set("BitsPerComponent", MakeInteger(*(ximg.BitsPerComponent)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ximg.ColorSpace != nil {
|
|
|
|
dict.SetIfNotNil("ColorSpace", ximg.ColorSpace.ToPdfObject())
|
|
|
|
}
|
|
|
|
dict.SetIfNotNil("Intent", ximg.Intent)
|
|
|
|
dict.SetIfNotNil("ImageMask", ximg.ImageMask)
|
|
|
|
dict.SetIfNotNil("Mask", ximg.Mask)
|
|
|
|
dict.SetIfNotNil("Decode", ximg.Decode)
|
|
|
|
dict.SetIfNotNil("Interpolate", ximg.Interpolate)
|
|
|
|
dict.SetIfNotNil("Alternatives", ximg.Alternatives)
|
|
|
|
dict.SetIfNotNil("SMask", ximg.SMask)
|
|
|
|
dict.SetIfNotNil("SMaskInData", ximg.SMaskInData)
|
2018-05-02 17:14:31 +10:00
|
|
|
dict.SetIfNotNil("Matte", ximg.Matte)
|
2017-02-22 21:10:57 +00:00
|
|
|
dict.SetIfNotNil("Name", ximg.Name)
|
|
|
|
dict.SetIfNotNil("StructParent", ximg.StructParent)
|
|
|
|
dict.SetIfNotNil("ID", ximg.ID)
|
|
|
|
dict.SetIfNotNil("OPI", ximg.OPI)
|
|
|
|
dict.SetIfNotNil("Metadata", ximg.Metadata)
|
|
|
|
dict.SetIfNotNil("OC", ximg.OC)
|
|
|
|
|
|
|
|
dict.Set("Length", MakeInteger(int64(len(ximg.Stream))))
|
|
|
|
stream.Stream = ximg.Stream
|
|
|
|
|
|
|
|
return stream
|
|
|
|
}
|