-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrightness-optimized.go
45 lines (36 loc) · 1.15 KB
/
brightness-optimized.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
package filters
import (
"math"
"runtime"
"sync"
"time"
"go-image-processing/utilities"
)
func Brightness(path string, amount int) {
img, format, openMS, convertMS := open(path)
now := math.Round(float64(time.Now().UnixNano()) / 1000000)
amount = utilities.MaxMin(amount, 255, -255)
pixLen := len(img.Pix)
threads := runtime.NumCPU()
pixPerThread := getPixPerThread(pixLen, threads)
var wg sync.WaitGroup
processing := func(thread int) {
defer wg.Done()
startIndex := pixPerThread * thread
endIndex := clampMax(startIndex+pixPerThread, pixLen)
for i := startIndex; i < endIndex; i += 4 {
img.Pix[i] = uint8(utilities.MaxMin(int(img.Pix[i])+amount, 255, 0))
img.Pix[i+1] = uint8(utilities.MaxMin(int(img.Pix[i+1])+amount, 255, 0))
img.Pix[i+2] = uint8(utilities.MaxMin(int(img.Pix[i+2])+amount, 255, 0))
}
}
for t := 0; t < threads; t += 1 {
wg.Add(1)
go processing(t)
}
wg.Wait()
processMS := int(math.Round(float64(time.Now().UnixNano())/1000000) - now)
saveMS := save(img, format)
sum := openMS + convertMS + processMS + saveMS
println("open", openMS, "convert", convertMS, "process", processMS, "save", saveMS, "sum", sum)
}