-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcjpeg_test.go
146 lines (122 loc) · 2.72 KB
/
cjpeg_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package mozjpegbin
import (
"net/http"
"os"
"io"
"github.com/stretchr/testify/assert"
"testing"
"image/jpeg"
"fmt"
"image"
"image/color"
)
func init() {
downloadFile("https://upload.wikimedia.org/wikipedia/commons/e/e3/Avola-Syracuse-Sicilia-Italy_-_Creative_Commons_by_gnuckx_%283858115914%29.jpg", "source.jpg")
}
func downloadFile(url, target string) {
_, err := os.Stat(target)
if err != nil {
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Error while downloading test image: %v\n", err)
panic(err)
}
defer resp.Body.Close()
f, err := os.Create(target)
if err != nil {
panic(err)
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
if err != nil {
panic(err)
}
}
}
func TestEncodeImage(t *testing.T) {
c := NewCJpeg()
f, err := os.Open("source.jpg")
assert.Nil(t, err)
img, err := jpeg.Decode(f)
assert.Nil(t, err)
c.InputImage(img)
c.OutputFile("target.jpg")
err = c.Run()
assert.Nil(t, err)
validateJpg(t)
}
func TestEncodeImage2(t *testing.T) {
const width, height = 256, 256
// Create a colored image of the given width and height.
img := image.NewNRGBA(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
img.Set(x, y, color.NRGBA{
R: uint8((x + y) & 255),
G: uint8((x + y) << 1 & 255),
B: uint8((x + y) << 2 & 255),
A: 255,
})
}
}
c := NewCJpeg()
c.InputImage(img)
c.OutputFile("target.jpg")
err := c.Run()
assert.Nil(t, err)
validateJpgImage(t, img)
}
func TestEncodeReader(t *testing.T) {
c := NewCJpeg()
f, err := os.Open("source.jpg")
assert.Nil(t, err)
c.Input(f)
c.OutputFile("target.jpg")
err = c.Run()
assert.Nil(t, err)
validateJpg(t)
}
func TestEncodeFile(t *testing.T) {
c := NewCJpeg()
c.Quality(100)
c.Optimize(true)
c.InputFile("source.jpg")
c.OutputFile("target.jpg")
err := c.Run()
assert.Nil(t, err)
validateJpg(t)
}
func TestEncodeWriter(t *testing.T) {
f, err := os.Create("target.jpg")
assert.Nil(t, err)
defer f.Close()
c := NewCJpeg()
c.InputFile("source.jpg")
c.Output(f)
err = c.Run()
assert.Nil(t, err)
f.Close()
validateJpg(t)
}
func TestCJpegVersion(t *testing.T) {
v, err := NewCJpeg().Version()
assert.Nil(t, err)
assert.NotEmpty(t, v)
}
func validateJpg(t *testing.T) {
//defer os.Remove("target.jpg")
fSource, err := os.Open("source.jpg")
assert.Nil(t, err)
imgSource, err := jpeg.Decode(fSource)
assert.Nil(t, err)
validateJpgImage(t, imgSource)
}
func validateJpgImage(t *testing.T, imgSource image.Image) {
//defer os.Remove("target.jpg")
fTarget, err := os.Open("target.jpg")
assert.Nil(t, err)
defer fTarget.Close()
imgTarget, err := jpeg.Decode(fTarget)
assert.Nil(t, err)
assert.Equal(t, imgSource.Bounds(), imgTarget.Bounds())
}