fix color decoding, skip transprent pixels

fixes #2
This commit is contained in:
Norwin Roosen 2020-02-04 13:40:30 +01:00
parent 5d5dc925b2
commit 26a4d1a6aa
1 changed files with 13 additions and 14 deletions

27
main.go
View File

@ -4,6 +4,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"image" "image"
"image/color"
_ "image/gif" _ "image/gif"
_ "image/jpeg" _ "image/jpeg"
_ "image/png" _ "image/png"
@ -13,7 +14,6 @@ import (
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"runtime/pprof" "runtime/pprof"
"strconv"
"time" "time"
) )
@ -100,30 +100,29 @@ func readImage(path string) (img image.Image) {
return img return img
} }
func intToHex(x uint32) string {
str := strconv.FormatInt(int64(x), 16)
if len(str) == 1 {
str = "0" + str
}
return str[0:2]
}
// 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_x, offset_y int) (commands [][]byte) {
max_x := img.Bounds().Max.X max_x := img.Bounds().Max.X
max_y := img.Bounds().Max.Y max_y := img.Bounds().Max.Y
commands = make([][]byte, max_x*max_y) commands = make([][]byte, max_x*max_y)
numCmds := 0
for x := 0; x < max_x; x++ { for x := 0; x < max_x; x++ {
for y := 0; y < max_y; y++ { for y := 0; y < max_y; y++ {
r, g, b, _ := img.At(x, y).RGBA() // ensure we're working with RGBA colors (non-alpha-pre-multiplied)
colStr := intToHex(r) + intToHex(g) + intToHex(b) c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA)
cmd := fmt.Sprintf("PX %d %d %s\n", x+offset_x, y+offset_y, colStr)
commands[x*max_y+y] = []byte(cmd) // ignore transparent pixels
if c.A != 0 {
cmd := fmt.Sprintf("PX %d %d %.2x%.2x%.2x\n",
x+offset_x, y+offset_y, c.R, c.G, c.B)
commands[numCmds] = []byte(cmd)
numCmds += 1
}
} }
} }
return commands return commands[:numCmds]
} }
// Splits messages into equally sized chunks // Splits messages into equally sized chunks