-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathopencv.go
169 lines (143 loc) · 3.94 KB
/
opencv.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package trez
//#cgo CFLAGS: -Wall -Wextra -Os -Wno-unused-function -Wno-unused-parameter
//#cgo linux pkg-config: opencv
//#cgo darwin pkg-config: opencv
//
//#include <opencv/highgui.h>
//#include <opencv/cv.h>
//
//uchar* ptr_from_mat(CvMat* mat){
// return mat->data.ptr;
//}
//
//void set_data_mat(CvMat* mat, void* ptr) {
// mat->data.ptr = ptr;
//}
import "C"
import (
"errors"
"math"
"unsafe"
)
var (
errNoData = errors.New("image data length is zero")
errInvalidSourceFormat = errors.New("invalid data source format")
errEncoding = errors.New("error during encoding")
)
func Resize(data []byte, options Options) ([]byte, error) {
if len(data) == 0 {
return nil, errNoData
}
// enable optimizations
C.cvUseOptimized(1)
// create a mat
mat := C.cvCreateMat(1, C.int(len(data)), C.CV_8UC1)
C.set_data_mat(mat, unsafe.Pointer(&data[0]))
// start decoding
src := C.cvDecodeImage(mat, C.CV_LOAD_IMAGE_COLOR)
C.cvReleaseMat(&mat)
// check it's a valid source
if src == nil || src.width == 0 || src.height == 0 {
return nil, errInvalidSourceFormat
}
defer C.cvReleaseImage(&src)
// set some defaults
if options.Width == 0 {
options.Width = int(src.width)
}
if options.Height == 0 {
options.Height = int(src.height)
}
// prepare the destination image
size := C.cvSize(C.int(options.Width), C.int(options.Height))
dst := C.cvCreateImage(size, src.depth, src.nChannels)
defer C.cvReleaseImage(&dst)
// get the x,y factor
xf := float64(dst.width) / float64(src.width)
yf := float64(dst.height) / float64(src.height)
rect := C.CvRect{}
switch options.Algo {
case FIT:
ratio := math.Min(xf, yf)
rect.width = C.int(math.Floor(float64(src.width) * ratio))
rect.height = C.int(math.Floor(float64(src.height) * ratio))
switch options.Gravity {
case CENTER:
rect.x = (dst.width - rect.width) / 2
rect.y = (dst.height - rect.height) / 2
case NORTH:
rect.x = (dst.width - rect.width) / 2
rect.y = 0
case NORTH_WEST:
rect.x = 0
rect.y = 0
case NORTH_EAST:
rect.x = (dst.width - rect.width)
rect.y = 0
case SOUTH:
rect.x = (dst.width - rect.width) / 2
rect.y = (dst.height - rect.height)
case SOUTH_WEST:
rect.x = 0
rect.y = (dst.height - rect.height)
case SOUTH_EAST:
rect.x = (dst.width - rect.width)
rect.y = (dst.height - rect.height)
case WEST:
rect.x = 0
rect.y = (dst.height - rect.height) / 2
case EAST:
rect.x = (dst.width - rect.width)
rect.y = (dst.height - rect.height) / 2
}
b, g, r := options.Background[2], options.Background[1], options.Background[0]
C.cvSet(unsafe.Pointer(dst), C.cvScalar(C.double(b), C.double(g), C.double(r), 0), nil)
C.cvSetImageROI(dst, rect)
C.cvResize(unsafe.Pointer(src), unsafe.Pointer(dst), C.CV_INTER_AREA)
C.cvResetImageROI(dst)
case FILL:
ratio := math.Max(xf, yf)
size := C.cvSize(
C.int(math.Ceil(float64(src.width)*ratio)),
C.int(math.Ceil(float64(src.height)*ratio)),
)
mid := C.cvCreateImage(size, src.depth, src.nChannels)
defer C.cvReleaseImage(&mid)
C.cvResize(unsafe.Pointer(src), unsafe.Pointer(mid), C.CV_INTER_AREA)
if int(mid.width) > options.Width {
rect.x = (mid.width - C.int(options.Width)) / 2
}
if int(mid.height) > options.Height {
rect.y = (mid.height - C.int(options.Height)) / 2
}
rect.width = dst.width
rect.height = dst.height
C.cvSetImageROI(mid, rect)
dst = (*C.IplImage)(C.cvClone(unsafe.Pointer(mid)))
C.cvResetImageROI(mid)
}
// set default compression
if options.Quality == 0 {
options.Quality = 95
}
compression := [3]C.int{
C.CV_IMWRITE_JPEG_QUALITY,
C.int(options.Quality),
0,
}
// encode
ext := C.CString(".jpg")
ret := C.cvEncodeImage(ext, unsafe.Pointer(dst), &compression[0])
C.free(unsafe.Pointer(ext))
if ret == nil {
return nil, errEncoding
}
ptr := C.ptr_from_mat(ret)
data = C.GoBytes(unsafe.Pointer(ptr), ret.step)
C.cvReleaseMat(&ret)
return data, nil
}
type ratio struct {
src float64
max float64
}