-
Notifications
You must be signed in to change notification settings - Fork 6
/
imageflow_test.go
118 lines (108 loc) · 3.62 KB
/
imageflow_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package imageflow
import (
"io/ioutil"
"testing"
)
func TestJob(t *testing.T) {
job := newJob()
data, _ := ioutil.ReadFile("image.jpg")
command := []byte("{\"io\":[{\"io_id\":0,\"direction\":\"in\",\"io\":\"placeholder\"},{\"io_id\":1,\"direction\":\"out\",\"io\":\"placeholder\"}],\"framewise\":{\"steps\":[{\"decode\":{\"io_id\":0}},{\"constrain\":{\"mode\":\"within\",\"w\":400}},\"rotate_90\",{\"encode\":{\"io_id\":1,\"preset\":{\"pngquant\":{\"quality\":80}}}}]}}")
job.AddInput(0, data)
job.AddOutput(1)
err := job.Message(command)
result, _ := job.GetOutput(1)
ioutil.WriteFile("./output.jpg", result, 0644)
if err != nil {
t.Error(err)
t.Fail()
}
}
func TestStep(t *testing.T) {
step := NewStep()
_, errorInStep := step.Decode(NewURL("https://jpeg.org/images/jpeg2000-home.jpg")).FlipV().Watermark(NewFile("image.jpg"), nil, "within", PercentageFitBox{
X1: 0,
Y1: 0,
X2: 50,
Y2: 50,
}, 0.3, nil).ConstrainWithinW(400).FillRect(0, 0, 8, 8, Black{}).Branch(func(step *Steps) {
step.ConstrainWithin(100, 100).Rotate180().Region(Region{
X1: 0,
Y1: 0,
X2: 200,
Y2: 200,
BackgroundColor: Black{},
}).Branch(func(step *Steps) {
step.GrayscaleFlat().Encode(NewFile("gray_small.jpg"), MozJPEG{})
}).Encode(NewFile("small.jpg"), MozJPEG{})
}).DrawExact(func(steps *Steps) {
steps.Decode(NewFile("image.jpg"))
}, DrawExact{
X: 0,
Y: 0,
W: 100,
H: 100,
Blend: "overwrite",
}).ExpandCanvas(ExpandCanvas{Top: 10, Color: Black{}}).Encode(NewFile("medium.jpg"), MozJPEG{}).Execute()
if errorInStep != nil {
t.Error(errorInStep)
t.FailNow()
}
}
func BenchmarkSteps(b *testing.B) {
data, _ := ioutil.ReadFile("image.jpg")
for i := 0; i < b.N; i++ {
step := NewStep()
step.Decode(NewBuffer(data)).ConstrainWithinW(400).Branch(func(step *Steps) {
step.ConstrainWithin(100, 100).Region(Region{
X1: 0,
Y1: 0,
X2: 200,
Y2: 200,
BackgroundColor: Black{},
}).Rotate180().Encode(&Buffer{}, MozJPEG{})
}).Encode(&Buffer{}, MozJPEG{}).Execute()
}
}
func TestError(t *testing.T) {
job := newJob()
data, _ := ioutil.ReadFile("image.jpg")
command := []byte("\"io\":[{\"io_id\":0,\"direction\":\"in\",\"io\":\"placeholder\"},{\"io_id\":1,\"direction\":\"out\",\"io\":\"placeholder\"}],\"framewise\":{\"steps\":[{\"decode\":{\"io_id\":0}},{\"constrain\":{\"mode\":\"within\",\"w\":400}},\"rotate_90\",{\"encode\":{\"io_id\":1,\"preset\":{\"pngquant\":{\"quality\":80}}}}]}}")
job.AddInput(0, data)
job.AddOutput(1)
err := job.Message(command)
result, _ := job.GetOutput(1)
ioutil.WriteFile("./output.jpg", result, 0644)
if err == nil {
t.Error("Error should not be null")
t.Fail()
}
}
func TestIOOperation(t *testing.T) {
data, _ := ioutil.ReadFile("image.jpg")
step := NewStep()
m, _ := step.Decode(NewBuffer(data)).ConstrainWithinW(400).Branch(func(step *Steps) {
step.ConstrainWithin(100, 100).Region(Region{
X1: 0,
Y1: 0,
X2: 200,
Y2: 200,
BackgroundColor: Black{},
}).Rotate180().Encode(GetBuffer("key_1"), MozJPEG{})
}).Encode(GetBuffer("key_2"), MozJPEG{}).Execute()
if m["key_1"] == nil || m["key_2"] == nil {
t.Error("Buffer failed")
t.Fail()
}
}
func TestSteps_Watermark(t *testing.T) {
step := NewStep()
data, _ := ioutil.ReadFile("image.jpg")
m, _ := step.Decode(NewBuffer(data)).Watermark(NewBuffer(data), ConstraintGravity{
X: 100,
Y: 100,
}, "", nil, .5, nil).Encode(GetBuffer("key_1"), MozJPEG{}).Execute()
if m["key_1"] == nil {
t.Error("Buffer failed")
t.Fail()
}
}