Skip to content

Instantly share code, notes, and snippets.

@svlapin
Last active July 29, 2019 20:37
Show Gist options
  • Save svlapin/3f1851dc770e1502a2f1ecf4554b9a56 to your computer and use it in GitHub Desktop.
Save svlapin/3f1851dc770e1502a2f1ecf4554b9a56 to your computer and use it in GitHub Desktop.
package rndimage
import (
"image"
"image/color"
"math/rand"
"sync"
)
const numWorkers = 10
func setPointWorker(rgba *image.RGBA, points <-chan image.Point, wg *sync.WaitGroup) {
rnd := rand.New(rand.NewSource(rand.Int63()))
defer wg.Done()
for p := range points {
rgba.Set(p.X, p.Y, color.RGBA{
R: uint8(rnd.Intn(256)),
G: uint8(rnd.Intn(256)),
B: uint8(rnd.Intn(256)),
A: uint8(rnd.Intn(256)),
})
}
}
// Generate - generates `image.RGBA` of given `width` and `height` filling it with random pixel colors
func Generate(width, height int) *image.RGBA {
rgba := image.NewRGBA(image.Rectangle{Min: image.Point{0, 0}, Max: image.Point{width, height}})
points := make(chan image.Point, width*height)
var wg sync.WaitGroup
wg.Add(numWorkers)
for i := 0; i < numWorkers; i++ {
go setPointWorker(rgba, points, &wg)
}
for i := 0; i < width; i++ {
for k := 0; k < height; k++ {
points <- image.Point{i, k}
}
}
close(points)
wg.Wait()
return rgba
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment