-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.go
195 lines (156 loc) · 3.55 KB
/
transform.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package prism
//#cgo pkg-config: --libs-only-L opencv libturbojpeg
//#cgo CFLAGS: -O3 -Wno-error=unused-function
//#cgo LDFLAGS: -lopencv_imgproc -lopencv_core -lopencv_highgui -lturbojpeg
//#include "prism.h"
import "C"
import (
"errors"
"runtime"
"unsafe"
"github.com/rwcarlsen/goexif/exif"
)
func (img *Image) Resize(width, height int) (err error) {
img.m.Lock()
defer img.m.Unlock()
defer recoverWithError(&err)
resizedIplImg := allocTarget(img.iplImage, width, height)
C.cvResize(
unsafe.Pointer(img.iplImage),
unsafe.Pointer(resizedIplImg),
C.int(C.CV_INTER_AREA),
)
C.cvReleaseImage(&img.iplImage)
img.iplImage = resizedIplImg
return nil
}
func (img *Image) Reorient() (err error) {
if img.exif == nil {
return nil
}
orientation, err := img.exif.Get(exif.Orientation)
if err != nil {
return err
}
orientationValue, err := orientation.Int(0)
if err != nil {
return err
}
switch orientationValue {
case 2:
err = img.FlipH()
case 3:
err = img.Rotate180()
case 4:
err = img.FlipV()
case 5:
if err = img.Rotate90(); err == nil {
err = img.FlipH()
}
case 6:
err = img.Rotate90()
case 7:
if err = img.Rotate270(); err == nil {
err = img.FlipH()
}
case 8:
err = img.Rotate270()
}
return err
}
func (img *Image) Fit(width, height int) (err error) {
if width <= 0 && height <= 0 {
return
}
bounds := img.Bounds()
srcW := bounds.Dx()
srcH := bounds.Dy()
if srcW <= width && srcH <= height {
return
}
if width == 0 {
width = srcW
}
if height == 0 {
height = srcH
}
srcAspectRatio := float64(srcW) / float64(srcH)
maxAspectRatio := float64(width) / float64(height)
var newW, newH int
if srcAspectRatio > maxAspectRatio {
newW = width
newH = int(float64(newW) / srcAspectRatio)
} else {
newH = height
newW = int(float64(newH) * srcAspectRatio)
}
return img.Resize(newW, newH)
}
func (img *Image) Rotate90() (err error) {
img.m.Lock()
defer img.m.Unlock()
defer recoverWithError(&err)
newIplImg := allocTarget(img.iplImage, img.Bounds().Size().Y, img.Bounds().Size().X)
C.cvTranspose(
unsafe.Pointer(img.iplImage),
unsafe.Pointer(newIplImg),
)
C.cvReleaseImage(&img.iplImage)
img.iplImage = newIplImg
return flip(newIplImg, 1)
}
func (img *Image) Rotate180() error {
img.m.Lock()
defer img.m.Unlock()
return flip(img.iplImage, -1)
}
func (img *Image) Rotate270() (err error) {
img.m.Lock()
defer img.m.Unlock()
defer recoverWithError(&err)
newIplImg := allocTarget(img.iplImage, img.Bounds().Size().Y, img.Bounds().Size().X)
C.cvTranspose(
unsafe.Pointer(img.iplImage),
unsafe.Pointer(newIplImg),
)
C.cvReleaseImage(&img.iplImage)
img.iplImage = newIplImg
return flip(newIplImg, 0)
}
func (img *Image) FlipH() error {
img.m.Lock()
defer img.m.Unlock()
return flip(img.iplImage, 1)
}
func (img *Image) FlipV() error {
img.m.Lock()
defer img.m.Unlock()
return flip(img.iplImage, 0)
}
func flip(iplImage *C.IplImage, axis int) (err error) {
defer recoverWithError(&err)
C.cvFlip(
unsafe.Pointer(iplImage),
unsafe.Pointer(iplImage),
C.int(axis),
)
return nil
}
// create target image with new size, but same color depth and channels
func allocTarget(iplImage *C.IplImage, width, height int) *C.IplImage {
size := C.CvSize{width: C.int(width), height: C.int(height)}
return C.cvCreateImage(size, iplImage.depth, iplImage.nChannels)
}
func recoverWithError(err *error) {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
panic(r)
}
switch r.(type) {
case string:
*err = errors.New(r.(string))
default:
*err = r.(error)
}
}
}