Created
June 18, 2020 14:56
-
-
Save damithadayananda/6975a68fabd47be7aa0bdd310972cfe2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func greyScale(pixels *[][]color.Color) { | |
ppixels := *pixels | |
xLen:=len(ppixels) | |
yLen := len(ppixels[0]) | |
//create new image | |
newImage:=make([][]color.Color, xLen) | |
for i:=0;i<len(newImage);i++{ | |
newImage[i] = make([]color.Color,yLen) | |
} | |
//idea is processing pixels in parallel | |
wg := sync.WaitGroup{} | |
for x:=0;x<xLen;x++{ | |
for y:=0;y<yLen; y++{ | |
wg.Add(1) | |
go func(x,y int) { | |
pixel :=ppixels[x][y] | |
originalColor,ok := color.RGBAModel.Convert(pixel).(color.RGBA) | |
if !ok{ | |
fmt.Println("type conversion went wrong") | |
} | |
grey := uint8(float64(originalColor.R)*0.21 + float64(originalColor.G)*0.72 + float64(originalColor.B)*0.07) | |
col :=color.RGBA{ | |
grey, | |
grey, | |
grey, | |
originalColor.A, | |
} | |
newImage[x][y] = col | |
wg.Done() | |
}(x,y) | |
} | |
} | |
wg.Wait() | |
*pixels = newImage | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment