small refactor

This commit is contained in:
Norwin Roosen 2020-02-05 12:02:21 +01:00
parent 1c87615fdd
commit 2ac93b6ecd
1 changed files with 12 additions and 9 deletions

21
main.go
View File

@ -50,8 +50,10 @@ func main() {
defer pprof.StopCPUProfile() defer pprof.StopCPUProfile()
} }
offset := image.Pt(*image_offsetx, *image_offsety)
img := readImage(*image_path)
// Generate and split messages into equal chunks // Generate and split messages into equal chunks
commands := genCommands(readImage(*image_path), *image_offsetx, *image_offsety) commands := genCommands(img, offset)
if *shuffle { if *shuffle {
shuffleCommands(commands) shuffleCommands(commands)
} }
@ -101,14 +103,13 @@ func readImage(path string) (img image.Image) {
} }
// Creates message based on given image // Creates message based on given image
func genCommands(img image.Image, offset_x, offset_y int) (commands [][]byte) { func genCommands(img image.Image, offset image.Point) (commands [][]byte) {
max_x := img.Bounds().Max.X b := img.Bounds()
max_y := img.Bounds().Max.Y commands = make([][]byte, b.Size().X*b.Size().Y)
commands = make([][]byte, max_x*max_y)
numCmds := 0 numCmds := 0
for x := 0; x < max_x; x++ { for x := b.Min.X; x < b.Max.X; x++ {
for y := 0; y < max_y; y++ { for y := b.Min.Y; y < b.Max.Y; y++ {
// ensure we're working with RGBA colors (non-alpha-pre-multiplied) // ensure we're working with RGBA colors (non-alpha-pre-multiplied)
c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA) c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA)
@ -116,10 +117,12 @@ func genCommands(img image.Image, offset_x, offset_y int) (commands [][]byte) {
if c.A == 0 { if c.A == 0 {
continue continue
} }
// @incomplete: also send alpha? -> bandwidth tradeoff
// @speed: this sprintf call is quite slow..
cmd := fmt.Sprintf("PX %d %d %.2x%.2x%.2x\n", cmd := fmt.Sprintf("PX %d %d %.2x%.2x%.2x\n",
x+offset_x, y+offset_y, c.R, c.G, c.B) x + offset.X, y + offset.Y, c.R, c.G, c.B)
commands[numCmds] = []byte(cmd) commands[numCmds] = []byte(cmd)
numCmds += 1 numCmds++
} }
} }