2015-11-26 15:41:45 -05:00
|
|
|
package imaging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
2017-06-01 10:37:05 -05:00
|
|
|
// parallel starts parallel image processing based on the current GOMAXPROCS value.
|
|
|
|
// If GOMAXPROCS = 1 it uses no parallelization.
|
|
|
|
// If GOMAXPROCS > 1 it spawns N=GOMAXPROCS workers in separate goroutines.
|
2015-11-26 15:41:45 -05:00
|
|
|
func parallel(dataSize int, fn func(partStart, partEnd int)) {
|
|
|
|
numGoroutines := 1
|
|
|
|
partSize := dataSize
|
|
|
|
|
2017-06-01 10:37:05 -05:00
|
|
|
numProcs := runtime.GOMAXPROCS(0)
|
|
|
|
if numProcs > 1 {
|
|
|
|
numGoroutines = numProcs
|
|
|
|
partSize = dataSize / (numGoroutines * 10)
|
|
|
|
if partSize < 1 {
|
|
|
|
partSize = 1
|
2015-11-26 15:41:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if numGoroutines == 1 {
|
|
|
|
fn(0, dataSize)
|
|
|
|
} else {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(numGoroutines)
|
|
|
|
idx := uint64(0)
|
|
|
|
|
|
|
|
for p := 0; p < numGoroutines; p++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
partStart := int(atomic.AddUint64(&idx, uint64(partSize))) - partSize
|
|
|
|
if partStart >= dataSize {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
partEnd := partStart + partSize
|
|
|
|
if partEnd > dataSize {
|
|
|
|
partEnd = dataSize
|
|
|
|
}
|
|
|
|
fn(partStart, partEnd)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 10:37:05 -05:00
|
|
|
// absint returns the absolute value of i.
|
2015-11-26 15:41:45 -05:00
|
|
|
func absint(i int) int {
|
|
|
|
if i < 0 {
|
|
|
|
return -i
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2017-06-01 10:37:05 -05:00
|
|
|
// clamp rounds and clamps float64 value to fit into uint8.
|
|
|
|
func clamp(x float64) uint8 {
|
|
|
|
v := int64(x + 0.5)
|
|
|
|
if v > 255 {
|
2015-11-26 15:41:45 -05:00
|
|
|
return 255
|
|
|
|
}
|
2017-06-01 10:37:05 -05:00
|
|
|
if v > 0 {
|
|
|
|
return uint8(v)
|
|
|
|
}
|
|
|
|
return 0
|
2015-11-26 15:41:45 -05:00
|
|
|
}
|