summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/disintegration/imaging/utils.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/disintegration/imaging/utils.go')
-rw-r--r--vendor/github.com/disintegration/imaging/utils.go88
1 files changed, 50 insertions, 38 deletions
diff --git a/vendor/github.com/disintegration/imaging/utils.go b/vendor/github.com/disintegration/imaging/utils.go
index 9f5926aaa..3b6ad2e49 100644
--- a/vendor/github.com/disintegration/imaging/utils.go
+++ b/vendor/github.com/disintegration/imaging/utils.go
@@ -1,53 +1,38 @@
package imaging
import (
+ "image"
"runtime"
"sync"
- "sync/atomic"
)
-// 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.
-func parallel(dataSize int, fn func(partStart, partEnd int)) {
- numGoroutines := 1
- partSize := dataSize
-
- numProcs := runtime.GOMAXPROCS(0)
- if numProcs > 1 {
- numGoroutines = numProcs
- partSize = dataSize / (numGoroutines * 10)
- if partSize < 1 {
- partSize = 1
- }
+// parallel processes the data in separate goroutines.
+func parallel(start, stop int, fn func(<-chan int)) {
+ count := stop - start
+ if count < 1 {
+ return
}
- if numGoroutines == 1 {
- fn(0, dataSize)
- } else {
- var wg sync.WaitGroup
- wg.Add(numGoroutines)
- idx := uint64(0)
+ procs := runtime.GOMAXPROCS(0)
+ if procs > count {
+ procs = count
+ }
- 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)
- }
- }()
- }
+ c := make(chan int, count)
+ for i := start; i < stop; i++ {
+ c <- i
+ }
+ close(c)
- wg.Wait()
+ var wg sync.WaitGroup
+ for i := 0; i < procs; i++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ fn(c)
+ }()
}
+ wg.Wait()
}
// absint returns the absolute value of i.
@@ -69,3 +54,30 @@ func clamp(x float64) uint8 {
}
return 0
}
+
+func reverse(pix []uint8) {
+ if len(pix) <= 4 {
+ return
+ }
+ i := 0
+ j := len(pix) - 4
+ for i < j {
+ pix[i+0], pix[j+0] = pix[j+0], pix[i+0]
+ pix[i+1], pix[j+1] = pix[j+1], pix[i+1]
+ pix[i+2], pix[j+2] = pix[j+2], pix[i+2]
+ pix[i+3], pix[j+3] = pix[j+3], pix[i+3]
+ i += 4
+ j -= 4
+ }
+}
+
+func toNRGBA(img image.Image) *image.NRGBA {
+ if img, ok := img.(*image.NRGBA); ok {
+ return &image.NRGBA{
+ Pix: img.Pix,
+ Stride: img.Stride,
+ Rect: img.Rect.Sub(img.Rect.Min),
+ }
+ }
+ return Clone(img)
+}