-
Notifications
You must be signed in to change notification settings - Fork 44
/
util.go
457 lines (366 loc) · 9.96 KB
/
util.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//package imgo is a golang image process lib
package imgo
import (
"errors"
"math"
"image"
"runtime"
)
type resamplingFilter struct {
Support float64
Kernel func(float64) float64
}
func ResizeForMatrix(filepath string, width int, height int)(imgMatrix [][][]uint8 , err error){
img,err1:=DecodeImage(filepath)
if err1 != nil {
err = err1
return
}
nrgba:=convertToNRGBA(img)
src:=Resize(nrgba,width,height)
imgMatrix = NewRGBAMatrix(height,width)
for i:=0;i<height;i++{
for j:=0;j<width;j++{
c:=src.At(j,i)
r,g,b,a:=c.RGBA()
imgMatrix[i][j][0]=uint8(r)
imgMatrix[i][j][1]=uint8(g)
imgMatrix[i][j][2]=uint8(b)
imgMatrix[i][j][3]=uint8(a)
}
}
return
}
// resize size of image
func Resize(src *image.NRGBA,width int, height int) *image.NRGBA {
dstW, dstH := width, height
if dstW < 0 || dstH < 0 {
return src
}
if dstW == 0 && dstH == 0 {
return src
}
srcW := src.Rect.Max.X
srcH := src.Rect.Max.Y
if srcW <= 0 || srcH <= 0 {
return src
}
// if new width or height is 0 then preserve aspect ratio, minimum 1px
if dstW == 0 {
tmpW := float64(dstH) * float64(srcW) / float64(srcH)
dstW = int(math.Max(1.0, math.Floor(tmpW+0.5)))
}
if dstH == 0 {
tmpH := float64(dstW) * float64(srcH) / float64(srcW)
dstH = int(math.Max(1.0, math.Floor(tmpH+0.5)))
}
var dst *image.NRGBA
var sinc = func(x float64) float64 {
if x == 0 {
return 1
}
return math.Sin(math.Pi*x) / (math.Pi * x)
}
var filter resamplingFilter = resamplingFilter{
Support: 3.0,
Kernel: func(x float64) float64 {
x = math.Abs(x)
if x < 3.0 {
return sinc(x) * sinc(x/3.0)
}
return 0
},
}
if filter.Support <= 0.0 {
// nearest-neighbor special case
dst = resizeNearest(src, dstW, dstH)
} else {
// two-pass resize
if srcW != dstW {
dst = resizeHorizontal(src, dstW, filter)
} else {
dst = src
}
if srcH != dstH {
dst = resizeVertical(dst, dstH, filter)
}
}
return dst
}
func resizeHorizontal(src *image.NRGBA, width int, filter resamplingFilter) *image.NRGBA {
srcBounds := src.Bounds()
srcW := srcBounds.Dx()
srcH := srcBounds.Dy()
srcMinX := srcBounds.Min.X
srcMinY := srcBounds.Min.Y
srcMaxX := srcBounds.Max.X
dstW := width
dstH := srcH
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
dX := float64(srcW) / float64(dstW)
scaleX := math.Max(dX, 1.0)
rX := math.Ceil(scaleX * filter.Support)
// divide image to parts for parallel processing
numGoroutines := runtime.NumCPU()
goMaxProcs := runtime.GOMAXPROCS(0)
if numGoroutines > goMaxProcs {
numGoroutines = goMaxProcs
}
if numGoroutines > dstW {
numGoroutines = dstW
}
partSize := dstW / numGoroutines
doneChan := make(chan bool, numGoroutines)
for part := 0; part < numGoroutines; part++ {
partStart := part * partSize
partEnd := (part + 1) * partSize
if part == numGoroutines-1 {
partEnd = dstW
}
go func(partStart, partEnd int) {
for dstX := partStart; dstX < partEnd; dstX++ {
fX := float64(srcMinX) + (float64(dstX)+0.5)*dX - 0.5
startX := int(math.Ceil(fX - rX))
if startX < srcMinX {
startX = srcMinX
}
endX := int(math.Floor(fX + rX))
if endX > srcMaxX-1 {
endX = srcMaxX - 1
}
// cache weights
weightSum := 0.0
weights := make([]float64, int(rX+2)*2)
for x := startX; x <= endX; x++ {
w := filter.Kernel((float64(x) - fX) / scaleX)
weightSum += w
weights[x-startX] = w
}
for dstY := 0; dstY < dstH; dstY++ {
srcY := srcMinY + dstY
r, g, b, a := 0.0, 0.0, 0.0, 0.0
for x := startX; x <= endX; x++ {
weight := weights[x-startX]
i := src.PixOffset(x, srcY)
r += float64(src.Pix[i+0]) * weight
g += float64(src.Pix[i+1]) * weight
b += float64(src.Pix[i+2]) * weight
a += float64(src.Pix[i+3]) * weight
}
r = math.Min(math.Max(r/weightSum, 0.0), 255.0)
g = math.Min(math.Max(g/weightSum, 0.0), 255.0)
b = math.Min(math.Max(b/weightSum, 0.0), 255.0)
a = math.Min(math.Max(a/weightSum, 0.0), 255.0)
j := dst.PixOffset(dstX, dstY)
dst.Pix[j+0] = uint8(r + 0.5)
dst.Pix[j+1] = uint8(g + 0.5)
dst.Pix[j+2] = uint8(b + 0.5)
dst.Pix[j+3] = uint8(a + 0.5)
}
}
doneChan <- true
}(partStart, partEnd)
}
// wait for goroutines to finish
for part := 0; part < numGoroutines; part++ {
<-doneChan
}
return dst
}
func resizeVertical(src *image.NRGBA, height int, filter resamplingFilter) *image.NRGBA {
srcBounds := src.Bounds()
srcW := srcBounds.Dx()
srcH := srcBounds.Dy()
srcMinX := srcBounds.Min.X
srcMinY := srcBounds.Min.Y
srcMaxY := srcBounds.Max.Y
dstW := srcW
dstH := height
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
dY := float64(srcH) / float64(dstH)
scaleY := math.Max(dY, 1.0)
rY := math.Ceil(scaleY * filter.Support)
// divide image to parts for parallel processing
numGoroutines := runtime.NumCPU()
goMaxProcs := runtime.GOMAXPROCS(0)
if numGoroutines > goMaxProcs {
numGoroutines = goMaxProcs
}
if numGoroutines > dstH {
numGoroutines = dstH
}
partSize := dstH / numGoroutines
doneChan := make(chan bool, numGoroutines)
for part := 0; part < numGoroutines; part++ {
partStart := part * partSize
partEnd := (part + 1) * partSize
if part == numGoroutines-1 {
partEnd = dstH
}
go func(partStart, partEnd int) {
for dstY := partStart; dstY < partEnd; dstY++ {
fY := float64(srcMinY) + (float64(dstY)+0.5)*dY - 0.5
startY := int(math.Ceil(fY - rY))
if startY < srcMinY {
startY = srcMinY
}
endY := int(math.Floor(fY + rY))
if endY > srcMaxY-1 {
endY = srcMaxY - 1
}
// cache weights
weightSum := 0.0
weights := make([]float64, int(rY+2)*2)
for y := startY; y <= endY; y++ {
w := filter.Kernel((float64(y) - fY) / scaleY)
weightSum += w
weights[y-startY] = w
}
for dstX := 0; dstX < dstW; dstX++ {
srcX := srcMinX + dstX
r, g, b, a := 0.0, 0.0, 0.0, 0.0
for y := startY; y <= endY; y++ {
weight := weights[y-startY]
i := src.PixOffset(srcX, y)
r += float64(src.Pix[i+0]) * weight
g += float64(src.Pix[i+1]) * weight
b += float64(src.Pix[i+2]) * weight
a += float64(src.Pix[i+3]) * weight
}
r = math.Min(math.Max(r/weightSum, 0.0), 255.0)
g = math.Min(math.Max(g/weightSum, 0.0), 255.0)
b = math.Min(math.Max(b/weightSum, 0.0), 255.0)
a = math.Min(math.Max(a/weightSum, 0.0), 255.0)
j := dst.PixOffset(dstX, dstY)
dst.Pix[j+0] = uint8(r + 0.5)
dst.Pix[j+1] = uint8(g + 0.5)
dst.Pix[j+2] = uint8(b + 0.5)
dst.Pix[j+3] = uint8(a + 0.5)
}
}
doneChan <- true
}(partStart, partEnd)
}
// wait for goroutines to finish
for part := 0; part < numGoroutines; part++ {
<-doneChan
}
return dst
}
// fast nearest-neighbor resize, no filtering
func resizeNearest(src *image.NRGBA, width, height int) *image.NRGBA {
dstW, dstH := width, height
srcBounds := src.Bounds()
srcW := srcBounds.Dx()
srcH := srcBounds.Dy()
srcMinX := srcBounds.Min.X
srcMinY := srcBounds.Min.Y
srcMaxX := srcBounds.Max.X
srcMaxY := srcBounds.Max.Y
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
dx := float64(srcW) / float64(dstW)
dy := float64(srcH) / float64(dstH)
// divide image to parts for parallel processing
numGoroutines := runtime.NumCPU()
goMaxProcs := runtime.GOMAXPROCS(0)
if numGoroutines > goMaxProcs {
numGoroutines = goMaxProcs
}
if numGoroutines > dstH {
numGoroutines = dstH
}
partSize := dstH / numGoroutines
doneChan := make(chan bool, numGoroutines)
for part := 0; part < numGoroutines; part++ {
partStart := part * partSize
partEnd := (part + 1) * partSize
if part == numGoroutines-1 {
partEnd = dstH
}
go func(partStart, partEnd int) {
for dstY := partStart; dstY < partEnd; dstY++ {
fy := float64(srcMinY) + (float64(dstY)+0.5)*dy - 0.5
for dstX := 0; dstX < dstW; dstX++ {
fx := float64(srcMinX) + (float64(dstX)+0.5)*dx - 0.5
srcX := int(math.Min(math.Max(math.Floor(fx+0.5), float64(srcMinX)), float64(srcMaxX)))
srcY := int(math.Min(math.Max(math.Floor(fy+0.5), float64(srcMinY)), float64(srcMaxY)))
srcOffset := src.PixOffset(srcX, srcY)
dstOffset := dst.PixOffset(dstX, dstY)
dst.Pix[dstOffset+0] = src.Pix[srcOffset+0]
dst.Pix[dstOffset+1] = src.Pix[srcOffset+1]
dst.Pix[dstOffset+2] = src.Pix[srcOffset+2]
dst.Pix[dstOffset+3] = src.Pix[srcOffset+3]
}
}
doneChan <- true
}(partStart, partEnd)
}
// wait for goroutines to finish
for part := 0; part < numGoroutines; part++ {
<-doneChan
}
return dst
}
// create a three dimenson slice
func New3DSlice(x int , y int , z int)(theSlice [][][]uint8){
theSlice = make([][][]uint8,x,x)
for i := 0; i < x; i++ {
s2 := make([][]uint8, y, y)
for j:=0 ; j < y; j++ {
s3 := make([]uint8,z,z)
s2[j] = s3
}
theSlice[i] = s2
}
return
}
// create a new rgba matrix
func NewRGBAMatrix(height int,width int)(rgbaMatrix [][][]uint8){
rgbaMatrix = New3DSlice(height,width,4)
return
}
func Matrix2Vector(imgMatrix [][][]uint8)(vector []uint8){
h:=len(imgMatrix)
w:=len(imgMatrix[0])
r:=len(imgMatrix[0][0])
length:=h*w*r
vector = make([]uint8,length)
for i:=0; i<h; i++ {
for j:=0; j<w; j++ {
for k:=0; k<r-1; k++ {
vector = append(vector,imgMatrix[i][j][k])
}
}
}
return
}
func Dot(x []uint8, y []uint8) float64 {
xlen:=len(x)
var sum float64 = 0
for i:=0; i<xlen; i++ {
sum = sum + float64(x[i])*float64(y[i])
}
return sum
}
type IterFunc func(i int, j int, k int, src [][][]uint8)[][][]uint8
func Iterator(filepath string, iter IterFunc)(imgMatrix [][][]uint8, err error ){
imgMatrix,err = Read(filepath)
if err != nil {
return
}
height:=len(imgMatrix)
width:=len(imgMatrix[0])
pix:=len(imgMatrix[0][0])
if height == 0 || width == 0 {
err = errors.New("The input of matrix is illegal!")
return
}
for i:=0; i<height; i++ {
for j:=0; j<width; j++ {
for k:=0; k<pix; k++ {
imgMatrix = iter(i,j,k,imgMatrix)
}
}
}
return
}