Fix for outputing Function type 0 (colorspace)

This commit is contained in:
Gunnsteinn Hall 2018-10-03 14:56:03 +00:00
parent efb03409b4
commit b39c7065e8
5 changed files with 136 additions and 6 deletions

View File

@ -6,8 +6,13 @@
package model
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
"github.com/unidoc/unidoc/pdf/core"
"github.com/unidoc/unidoc/pdf/internal/testutils"
)
func TestSeparationCS1(t *testing.T) {
@ -44,3 +49,128 @@ func TestDeviceNCS1(t *testing.T) {
//t.Errorf("Test not implemented yet")
}
// Bug with outputing Separation colorspaces using a Function0 function.
func TestColorspaceLoading(t *testing.T) {
rawpdf := []byte(`
10 0 obj
<<
/ColorSpace <<
/R99 99 0 R
/R86 86 0 R
/R88 88 0 R
>>
>>
endobj
99 0 obj
[/Separation /All 86 0 R 87 0 R]
endobj
86 0 obj
[/ICCBased 85 0 R]
endobj
88 0 obj
[/Separation /Black 86 0 R 87 0 R]
endobj
85 0 obj
<<
/N 3
/Alternate /DeviceRGB
/Length 3144
>>
stream
!!STREAMDATA2!!
endstream endobj
87 0 obj
<<
/Size [255]
/BitsPerSample 8
/Domain [0 1]
/Length 765
/Encode [0 254]
/Decode [0 1 0 1 0 1]
/Range [0 1 0 1 0 1]
/FunctionType 0
>>
stream
!!STREAMDATA1!!
endstream endobj
`)
functype0, err := ioutil.ReadFile(`testdata/cs_functype0.bin`)
if err != nil {
t.Fatalf("Error: %v", err)
}
iccdata, err := ioutil.ReadFile(`testdata/iccstream.bin`)
if err != nil {
t.Fatalf("Error: %v", err)
}
rawpdf = bytes.Replace(rawpdf, []byte("!!STREAMDATA1!!"), functype0, 1)
rawpdf = bytes.Replace(rawpdf, []byte("!!STREAMDATA2!!"), iccdata, 1)
//fmt.Printf("%s\n", rawpdf)
objMap, err := testutils.ParseIndirectObjects(string(rawpdf))
if err != nil {
t.Fatalf("Error: %v", err)
}
if len(objMap) != 6 {
t.Fatalf("len(objMap) != 6 (%d)", len(objMap))
}
resourceDict, ok := core.GetDict(objMap[10])
if !ok {
t.Fatalf("Resource object missing dictionary")
}
resources, err := NewPdfPageResourcesFromDict(resourceDict)
if err != nil {
t.Fatalf("Error loading resources: %v", err)
}
fmt.Printf("Out\n")
fmt.Printf("%s\n", resources.ToPdfObject())
outDict, ok := core.GetDict(resources.ToPdfObject())
if !ok {
t.Fatalf("error")
}
fmt.Printf("%s\n", outDict.DefaultWriteString())
r99, ok := resources.GetColorspaceByName("R99")
if !ok {
t.Fatalf("error")
}
array, ok := core.GetArray(r99.ToPdfObject())
if !ok {
t.Fatalf("error")
}
if array.Len() != 4 {
t.Fatalf("len != 4 (got %d)", array.Len())
}
name, ok := core.GetName(array.Get(0))
if !ok || *name != "Separation" {
t.Fatalf("Unexpected value")
}
name, ok = core.GetName(array.Get(1))
if !ok || *name != "All" {
t.Fatalf("Unexpected value")
}
ind, ok := core.GetIndirect(array.Get(2))
if !ok {
t.Fatalf("error")
}
if ind.ObjectNumber != 86 {
t.Fatalf("Incorrect obj num")
}
f, ok := core.GetStream(array.Get(3))
if !ok {
t.Fatalf("Stream get error")
}
if f.ObjectNumber != 87 {
t.Fatalf("Incorrect function obj number (got %d)", f.ObjectNumber)
}
}

View File

@ -229,8 +229,7 @@ func newPdfFunctionType0FromStream(stream *PdfObjectStream) (*PdfFunctionType0,
}
func (this *PdfFunctionType0) ToPdfObject() PdfObject {
container := this.container
if container != nil {
if this.container == nil {
this.container = &PdfObjectStream{}
}
@ -267,10 +266,10 @@ func (this *PdfFunctionType0) ToPdfObject() PdfObject {
// TODO: Encode.
// Either here, or automatically later on when writing out.
dict.Set("Length", MakeInteger(int64(len(this.rawData))))
container.Stream = this.rawData
this.container.Stream = this.rawData
container.PdfObjectDictionary = dict
return container
this.container.PdfObjectDictionary = dict
return this.container
}
func (this *PdfFunctionType0) Evaluate(x []float64) ([]float64, error) {

View File

@ -530,7 +530,7 @@ func (this *PdfReader) buildPageList(node *PdfIndirectObject, parent *PdfIndirec
if !ok {
return errors.New("Node missing Type (Required)")
}
common.Log.Trace("buildPageList node type: %s", *objType)
common.Log.Trace("buildPageList node type: %s (%+v)", *objType, node)
if *objType == "Page" {
p, err := this.newPdfPageFromDict(nodeDict)
if err != nil {

1
pdf/model/testdata/cs_functype0.bin vendored Normal file
View File

@ -0,0 +1 @@
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>羲梟斅嬅沆莢桁坂標頗充朮朮寨埰窒猝楫拙全碇傳日欌壹幽邑幼五迂僚偃醴連愼壓押率循恂塞洩舌輻査死薄腹蔔寐璞粕吏輞埋兩菱蘆撞冷東喇唐塘菊吉桔辜驅鷗芥藁蠱奸漑皆훗角<EFBFBD>팔훅훌컸팅팍쩨컨컬읔쩜쩝옥읍응슴옘씨뻬스슨뭡뻗뻠맏뭍뭐떳링렁뉠떨떫껨눙눴교껐께<EFBFBD>괼굅<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ЗカØぇぉ┤<EFBFBD><EFBFBD>ⅶ┐└ⅱㄴ·짛젪줋 옞젥젧쐿옕옗썭씂쐺솠썥썧뿗솙솚뼐삎뿓뵕뼋뼍뱯뵏뵒몥뱪뱬룖몡몢럯릫룒뙊럨럪땷뙄뙆닂뒊뒌뇡늽늿뀍뇙뇛꼩뀇뀉굝꼤꼦걙<EFBFBD>굙~<7E>~<7E>}|~~{~}{}|z||y{{x{zxzywyyvyxuxwuwvtvvsvurutrtsqsspsrorqoqpnppmoolonknmkmljllikkhkjhjigihfhheggdgfdfeceebddadc`cb`ba_aa^``]`_]_^\^^[]]Z]\Z\[Y[ZXZZWYYVXXUWWTVVSUURTTQTSPSRORQNQPNPOMONLNMKMLJLKIKJHJIGIIFHHEGGDFFCEEBDDACC@CB@BA?A@>@?=?><>=;==:<<9;:8:9798687566455344233122/11.0/-/.,.-+,,*++)**())'((&''%&&$%%#$$"##!"" !! 

BIN
pdf/model/testdata/iccstream.bin vendored Normal file

Binary file not shown.