func doPxl(img image.Image, size utils.Dimension, triangle Triangle, style Style, aliased bool) image.Image { nCPU := runtime.NumCPU() runtime.GOMAXPROCS(nCPU) var o draw.Image b := img.Bounds() c := make(chan int, nCPU) i := 0 // number of workers created. there may be a better way... switch style { case CROPPED: cols := b.Dx() / size.W rows := b.Dy() / size.H o = image.NewRGBA(image.Rect(0, 0, size.W*cols, size.H*rows)) for j, r := range utils.ChopRectangleToSizes(b, size.H, size.W, utils.IGNORE) { go pxlWorker(img, r, o, size, triangle, aliased, c) i = j } case FITTED: o = image.NewRGBA(img.Bounds()) for j, r := range utils.ChopRectangleToSizes(img.Bounds(), size.H, size.W, utils.SEPARATE) { go pxlWorker(img, r, o, size, triangle, aliased, c) i = j } } for j := 0; j < i; j++ { <-c } return o }
// Pixelate takes an Image and pixelates it into rectangles with the dimensions // given. The colour values in each region are averaged to produce the resulting // colours. func Pixelate(img image.Image, size utils.Dimension, style Style) image.Image { nCPU := runtime.NumCPU() runtime.GOMAXPROCS(nCPU) var o draw.Image b := img.Bounds() c := make(chan int, nCPU) i := 0 // like pxl. this may not be the best way switch style { case CROPPED: cols := b.Dx() / size.W rows := b.Dy() / size.H o = image.NewRGBA(image.Rect(0, 0, size.W*cols, size.H*rows)) for j, r := range utils.ChopRectangleToSizes(b, size.H, size.W, utils.IGNORE) { go paintAverage(img, r, o, c) i = j } case FITTED: o = image.NewRGBA(b) for j, r := range utils.ChopRectangleToSizes(b, size.H, size.W, utils.SEPARATE) { go paintAverage(img, r, o, c) i = j } } for j := 0; j < i; j++ { <-c } return o }