Skip to content

Commit bbaeac8

Browse files
committed
internal/cmpimg: PDF comparison fixed
1 parent 59819ff commit bbaeac8

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

internal/cmpimg/cmpimg.go

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"image"
1313
"image/color"
1414
"image/draw"
15+
"io/ioutil"
1516
"math"
1617
"reflect"
1718
"strings"
@@ -81,23 +82,67 @@ func Equal(typ string, raw1, raw2 []byte) (bool, error) {
8182
}
8283

8384
func cmpPdf(pdf1, pdf2 *pdf.Reader) bool {
84-
n1 := pdf1.NumPage()
85-
n2 := pdf2.NumPage()
86-
if n1 != n2 {
85+
return cmpPdfValues(pdf1.Trailer(), pdf2.Trailer())
86+
}
87+
88+
func cmpPdfValues(v1, v2 pdf.Value) bool {
89+
if v1.Kind() != v2.Kind() {
8790
return false
8891
}
8992

90-
for i := 1; i <= n1; i++ {
91-
p1 := pdf1.Page(i).Content()
92-
p2 := pdf2.Page(i).Content()
93-
if !reflect.DeepEqual(p1, p2) {
93+
switch v1.Kind() {
94+
case pdf.String:
95+
return v1.String() == v2.String()
96+
case pdf.Integer:
97+
return v1.Int64() == v2.Int64()
98+
case pdf.Real:
99+
return v1.Float64() == v2.Float64()
100+
case pdf.Name:
101+
return v1.Name() == v2.Name()
102+
case pdf.Stream:
103+
r1 := v1.Reader()
104+
s1, err1 := ioutil.ReadAll(r1)
105+
r1.Close()
106+
r2 := v2.Reader()
107+
s2, err2 := ioutil.ReadAll(r2)
108+
r2.Close()
109+
if err1 != nil || err2 != nil || len(s1) != len(s2) {
110+
return false
111+
}
112+
if !bytes.Equal(s1, s2) {
113+
return false
114+
}
115+
fallthrough
116+
case pdf.Dict:
117+
keys1, keys2 := v1.Keys(), v2.Keys()
118+
if len(keys1) != len(keys2) {
119+
return false
120+
}
121+
for i, k := range keys1 {
122+
if k != keys2[i] {
123+
return false
124+
}
125+
if k == "CreationDate" || k == "Parent" || k == "Font" {
126+
continue
127+
}
128+
if !cmpPdfValues(v1.Key(k), v2.Key(k)) {
129+
return false
130+
}
131+
}
132+
return true
133+
case pdf.Array:
134+
if v1.Len() != v2.Len() {
94135
return false
95136
}
137+
count := v1.Len()
138+
for i := 0; i < count; i++ {
139+
if !cmpPdfValues(v1.Index(i), v2.Index(i)) {
140+
return false
141+
}
142+
}
143+
return true
96144
}
97-
98-
t1 := pdf1.Trailer().String()
99-
t2 := pdf2.Trailer().String()
100-
return t1 == t2
145+
return false
101146
}
102147

103148
// Diff calculates an intensity-scaled difference between images a and b
-8 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)