-
Notifications
You must be signed in to change notification settings - Fork 9
/
opencv_test.go
83 lines (71 loc) · 1.75 KB
/
opencv_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
package trez
import (
"bytes"
"fmt"
"image"
_ "image/jpeg"
"io/ioutil"
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const tmpdir = "/tmp/trez"
func cleanup() {
os.RemoveAll(tmpdir)
os.MkdirAll(tmpdir, 0775)
fmt.Printf("images available at %s\n", tmpdir)
}
func dumpImage(filename string, data []byte) {
err := ioutil.WriteFile(path.Join(tmpdir, filename+".jpg"), data, 0665)
if err != nil {
panic(err)
}
}
func TestResize(t *testing.T) {
cleanup()
assert := assert.New(t)
f, err := os.Open("testdata/American_Dad.jpg")
require.NoError(t, err)
src, err := ioutil.ReadAll(f)
require.NoError(t, err)
// just encode
data, err := Resize(src, Options{})
assert.NoError(err)
dumpImage("just_image", data)
im, _, err := image.DecodeConfig(bytes.NewReader(data))
assert.NoError(err)
assert.Equal(1024, im.Width)
assert.Equal(768, im.Height)
size := len(data)
// just quality
data, err = Resize(src, Options{Quality: 50})
assert.NoError(err)
dumpImage("just_quality", data)
im, _, err = image.DecodeConfig(bytes.NewReader(data))
assert.NoError(err)
assert.Equal(1024, im.Width)
assert.Equal(768, im.Height)
assert.True(size > len(data))
opts := []Options{
{Width: 200},
{Height: 200},
{Width: 200, Height: 200},
{Width: 150, Height: 120},
{Width: 2000, Height: 1020},
{Width: 2000, Height: 3000},
}
for _, opt := range opts {
for _, algo := range []Algo{FIT, FILL} {
opt.Algo = algo
opt.Gravity = WEST
opt.Background = [3]int{255, 255, 255}
data, err := Resize(src, opt)
assert.NoError(err)
dumpImage(fmt.Sprintf("%dx%d_%s", opt.Width, opt.Height, opt.Algo), data)
_, _, err = image.DecodeConfig(bytes.NewReader(data))
assert.NoError(err)
}
}
}