forked from hybridgroup/gocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgcodecs_test.go
146 lines (126 loc) · 3.35 KB
/
imgcodecs_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 gocv
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"testing"
)
func TestIMRead(t *testing.T) {
img := IMRead("images/face-detect.jpg", IMReadColor)
defer img.Close()
if img.Empty() {
t.Error("Invalid Mat in IMRead")
}
}
func TestIMWrite(t *testing.T) {
dir, _ := ioutil.TempDir("", "gocvtests")
tmpfn := filepath.Join(dir, "test.jpg")
img := IMRead("images/face-detect.jpg", IMReadColor)
defer img.Close()
if img.Empty() {
t.Error("Invalid read of Mat in IMWrite test")
}
result := IMWrite(tmpfn, img)
if !result {
t.Error("Invalid write of Mat in IMWrite test")
}
}
func TestIMWriteWithParams(t *testing.T) {
dir, _ := ioutil.TempDir("", "gocvtests")
tmpfn := filepath.Join(dir, "test.jpg")
img := IMRead("images/face-detect.jpg", IMReadColor)
defer img.Close()
if img.Empty() {
t.Error("Invalid read of Mat in IMWrite test")
}
result := IMWriteWithParams(tmpfn, img, []int{IMWriteJpegQuality, 60})
if !result {
t.Error("Invalid write of Mat in IMWrite test")
}
}
func TestIMEncode(t *testing.T) {
img := IMRead("images/face-detect.jpg", IMReadColor)
defer img.Close()
if img.Empty() {
t.Error("Invalid Mat in IMEncode test")
}
buf, err := IMEncode(PNGFileExt, img)
if err != nil {
t.Error(err)
}
if len(buf) < 43000 {
t.Errorf("Wrong buffer size in IMEncode test. Should have been %v\n", len(buf))
}
}
func ExampleIMEncodeWithParams() {
img := IMRead(path.Join(os.Getenv("GOPATH"), "src/gocv.io/x/gocv/images/face-detect.jpg"), IMReadColor)
if img.Empty() {
log.Fatal("Invalid Mat")
}
imgHandler := func(w http.ResponseWriter, req *http.Request) {
quality := 75
if q, err := strconv.Atoi(req.URL.Query().Get("q")); err == nil {
quality = q
}
buffer, err := IMEncodeWithParams(JPEGFileExt, img, []int{IMWriteJpegQuality, quality})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
w.Header().Set("Content-Type", "image/jpeg")
w.WriteHeader(http.StatusOK)
w.Write(buffer)
}
http.HandleFunc("/img", imgHandler)
fmt.Println("Open in browser http://127.0.0.1:8080/img?q=10 where q is a JPEG quality parameter")
log.Fatal(http.ListenAndServe("127.0.0.1:8080", nil))
}
func TestIMEncodeWithParams(t *testing.T) {
img := IMRead("images/face-detect.jpg", IMReadColor)
defer img.Close()
if img.Empty() {
t.Error("Invalid Mat in IMEncode test")
}
buf, err := IMEncodeWithParams(JPEGFileExt, img, []int{IMWriteJpegQuality, 75})
if err != nil {
t.Error(err)
}
if len(buf) < 18000 {
t.Errorf("Wrong buffer size in IMEncode test. Should have been %v\n", len(buf))
}
buf2, err := IMEncodeWithParams(JPEGFileExt, img, []int{IMWriteJpegQuality, 100})
if err != nil {
t.Error(err)
}
if len(buf2) < 18000 {
t.Errorf("Wrong buffer size in IMEncode test. Should have been %v\n", len(buf))
}
if len(buf) >= len(buf2) {
t.Errorf("Jpeg quality parameter does not work correctly\n")
}
}
func TestIMDecode(t *testing.T) {
content, err := ioutil.ReadFile("images/face-detect.jpg")
if err != nil {
t.Error("Invalid ReadFile in IMDecode")
}
dec, err := IMDecode(content, IMReadColor)
if err != nil {
t.Error(err.Error())
}
if dec.Empty() {
t.Error("Invalid Mat in IMDecode")
}
dec.Close()
dec, err = IMDecode([]byte{}, IMReadColor)
if err == nil {
t.Error("Should not decode empty array")
}
}