From 2ec417da6a6de0ee68bd922208859fcc9418fa4a Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Fri, 14 Feb 2020 12:39:16 +0100 Subject: [PATCH] always convert images to NRGBA this type is rpc-serializable, and we avoid repeated color conversion --- io.go | 11 +++++++++++ main.go | 2 +- pixelflut/api.go | 2 +- pixelflut/commands.go | 6 ++---- rpc/dottir.go | 2 +- rpc/ran.go | 5 ++--- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/io.go b/io.go index d4fa70c..60fe6e1 100644 --- a/io.go +++ b/io.go @@ -31,3 +31,14 @@ func writeImage(path string, img image.Image) { log.Fatal(err) } } + +func imgToNRGBA(img image.Image) *image.NRGBA { + b := img.Bounds() + r := image.NewNRGBA(b) + for x := b.Min.X; x < b.Max.X; x++ { + for y := b.Min.Y; y < b.Max.Y; y++ { + r.Set(x, y, img.At(x, y)) + } + } + return r +} diff --git a/main.go b/main.go index 1cd9dc7..a7ffc34 100644 --- a/main.go +++ b/main.go @@ -50,7 +50,7 @@ func main() { if *imgPath != "" { offset := image.Pt(*x, *y) - img := readImage(*imgPath) + img := imgToNRGBA(readImage(*imgPath)) // check connectivity by opening one test connection conn, err := net.Dial("tcp", *address) diff --git a/pixelflut/api.go b/pixelflut/api.go index 99bf9b2..c9266b4 100644 --- a/pixelflut/api.go +++ b/pixelflut/api.go @@ -14,7 +14,7 @@ import ( // using `conns` connections. Pixels are sent column wise, unless `shuffle` // is set. Stops when stop is closed. // @cleanup: use FlutTask{} as arg -func Flut(img image.Image, position image.Point, shuffle bool, address string, conns int, stop chan bool, wg *sync.WaitGroup) { +func Flut(img *image.NRGBA, position image.Point, shuffle bool, address string, conns int, stop chan bool, wg *sync.WaitGroup) { cmds := commandsFromImage(img, position) if shuffle { cmds.Shuffle() diff --git a/pixelflut/commands.go b/pixelflut/commands.go index 0c3c6c7..be90f38 100644 --- a/pixelflut/commands.go +++ b/pixelflut/commands.go @@ -33,16 +33,14 @@ func (c commands) Shuffle() { } // CommandsFromImage converts an image to the respective pixelflut commands -func commandsFromImage(img image.Image, offset image.Point) (cmds commands) { +func commandsFromImage(img *image.NRGBA, offset image.Point) (cmds commands) { b := img.Bounds() cmds = make([][]byte, b.Size().X*b.Size().Y) numCmds := 0 for x := b.Min.X; x < b.Max.X; x++ { for y := b.Min.Y; y < b.Max.Y; y++ { - // ensure we're working with RGBA colors (non-alpha-pre-multiplied) - c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA) - + c := img.At(x, y).(color.NRGBA) // ignore transparent pixels if c.A == 0 { continue diff --git a/rpc/dottir.go b/rpc/dottir.go index d904cf9..074c434 100644 --- a/rpc/dottir.go +++ b/rpc/dottir.go @@ -32,7 +32,7 @@ type Hevring struct { type FlutTask struct { Address string MaxConns int - Img *image.NRGBA // bug :imageType: should be image.Image, but can't be serialized. do conversion in task creation? + Img *image.NRGBA Offset image.Point Shuffle bool } diff --git a/rpc/ran.go b/rpc/ran.go index a521d56..03fbeb5 100644 --- a/rpc/ran.go +++ b/rpc/ran.go @@ -115,13 +115,12 @@ func SummonRán(address string, stopChan chan bool, wg *sync.WaitGroup) *Rán { } // SetTask assigns a FlutTask to Rán, distributing it to all clients -func (r *Rán) SetTask(img image.Image, offset image.Point, address string, maxConns int) { +func (r *Rán) SetTask(img *image.NRGBA, offset image.Point, address string, maxConns int) { // @incomplete: smart task creation: // fetch server state & sample foreign activity in image regions. assign // subregions to clients (per connection), considering their bandwidth. - // @bug :imageType - r.task = FlutTask{address, maxConns, img.(*image.NRGBA), offset, true} + r.task = FlutTask{address, maxConns, img, offset, true} for _, c := range r.clients { ack := FlutAck{} // @speed: should send tasks async