-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
192 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import "errors" | ||
|
||
// convertToGreyScale converts a matrix to greyscale equivalent | ||
func convertToGreyScale(matrix [][][4]uint32) ([][][4]uint32, error) { | ||
height := len(matrix) // Get the height of the matrix | ||
if height == 0 { | ||
return nil, errors.New("empty matrix") | ||
} | ||
|
||
width := len(matrix[0]) | ||
|
||
greyScaleImage := Make2D[[4]uint32](height, width) // Create a new image | ||
|
||
// Iterate through all pixels | ||
for y := 0; y < height; y++ { | ||
for x := range matrix[y] { | ||
current := matrix[y][x] // Convenience, cleans up the following line, does allocate more memory however | ||
r, g, b, a := current[0], current[1], current[2], current[3] // Thank you Go for not providing list expansion | ||
luminance := uint32(float64(r)*0.299 + float64(g)*0.587 + float64(b)*0.114) // Apply the formula | ||
greyScaleImage[y][x] = [4]uint32{luminance, luminance, luminance, a} // Write the luminance value, keeping the alpha as current | ||
} | ||
} | ||
return greyScaleImage, nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package matrix_image_manipulation | ||
package main | ||
|
||
import ( | ||
"errors" | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package matrix_image_manipulation | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
|
This file contains 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
This file contains 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