-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpotrace_test.go
92 lines (76 loc) · 2.15 KB
/
potrace_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package gotrace_test
import (
"crypto/sha1"
"encoding/hex"
"image/png"
"io"
"os"
"path/filepath"
"testing"
"github.com/gotranspile/gotrace"
)
func checkErr(t testing.TB, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
const testdata = "./testdata/"
func TestPotrace(t *testing.T) {
bm, err := gotrace.BitmapReadFile(filepath.Join(testdata, "stanford.pbm"), 0.5)
checkErr(t, err)
plist, err := gotrace.Trace(bm, nil)
checkErr(t, err)
// Test data generated with:
// potrace -b <backend> -o ./testdata/stanford.<ext> ./testdata/stanford.pbm
// sha1sum ./testdata/*
bi := gotrace.NewRenderConf()
fname := filepath.Join(testdata, "stanford.svg")
err = gotrace.RenderFile("svg", bi, fname, plist, bm.W, bm.H)
checkErr(t, err)
if h := hashFile(fname); h != "9aee25cf092f7228c4f14ba4607592487f4fab07" {
t.Errorf("unexpected hash for SVG: %s", h)
}
fname = filepath.Join(testdata, "stanford.pdf")
err = gotrace.RenderFile("pdf", bi, fname, plist, bm.W, bm.H)
checkErr(t, err)
if h := hashFile(fname); h != "347917e85988fc7f881244cb2f8adfa7ce04d495" {
t.Errorf("unexpected hash for PDF: %s", h)
}
fname = filepath.Join(testdata, "stanford.dxf")
err = gotrace.RenderFile("dxf", bi, fname, plist, bm.W, bm.H)
checkErr(t, err)
if h := hashFile(fname); h != "8c03dff3ce9f41a9b726963b7c6dda4f3dfffbab" {
t.Errorf("unexpected hash for DXF: %s", h)
}
}
func TestPotracePNG(t *testing.T) {
f, err := os.Open(filepath.Join(testdata, "stanford.png"))
checkErr(t, err)
defer f.Close()
img, err := png.Decode(f)
checkErr(t, err)
bm := gotrace.BitmapFromImage(img, nil)
plist, err := gotrace.Trace(bm, nil)
checkErr(t, err)
bi := gotrace.NewRenderConf()
fname := filepath.Join(testdata, "stanford.png.svg")
err = gotrace.RenderFile("svg", bi, fname, plist, bm.W, bm.H)
checkErr(t, err)
if h := hashFile(fname); h != "9aee25cf092f7228c4f14ba4607592487f4fab07" {
t.Errorf("unexpected hash for SVG: %s", h)
}
}
func hashFile(path string) string {
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
h := sha1.New()
_, err = io.Copy(h, f)
if err != nil {
panic(err)
}
return hex.EncodeToString(h.Sum(nil))
}