42 lines
583 B
Go
42 lines
583 B
Go
package imaging
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// parallel processes the data in separate goroutines.
|
|
func parallel(start, stop int, fn func(<-chan int)) {
|
|
count := stop - start
|
|
if count < 1 {
|
|
return
|
|
}
|
|
|
|
procs := min(2, count)
|
|
|
|
c := make(chan int, count)
|
|
for i := start; i < stop; i++ {
|
|
c <- i
|
|
}
|
|
close(c)
|
|
|
|
var wg sync.WaitGroup
|
|
for range procs {
|
|
wg.Go(func() {
|
|
fn(c)
|
|
})
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
// clamp rounds and clamps float64 value to fit into uint8.
|
|
func clamp(x float64) uint8 {
|
|
v := int64(x + 0.5)
|
|
if v > 255 {
|
|
return 255
|
|
}
|
|
if v > 0 {
|
|
return uint8(v)
|
|
}
|
|
return 0
|
|
}
|