2020-02-06 13:39:24 +01:00
|
|
|
package pixelflut
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/hex"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"log"
|
2020-12-29 18:28:19 +01:00
|
|
|
"math/rand"
|
2020-02-06 13:39:24 +01:00
|
|
|
"net"
|
2020-02-13 22:26:50 +01:00
|
|
|
"sync"
|
2020-02-15 13:58:01 +01:00
|
|
|
|
|
|
|
"github.com/SpeckiJ/Hochwasser/render"
|
2020-02-06 13:39:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Flut asynchronously sends the given image to pixelflut server at `address`
|
2020-02-13 23:57:56 +01:00
|
|
|
// using `conns` connections. Pixels are sent column wise, unless `shuffle`
|
|
|
|
// is set. Stops when stop is closed.
|
2020-02-13 22:26:50 +01:00
|
|
|
// @cleanup: use FlutTask{} as arg
|
2020-12-29 18:28:19 +01:00
|
|
|
func Flut(img *image.NRGBA, position image.Point, shuffle, rgbsplit, randoffset, recreateConns bool, address string, conns int, stop chan bool, wg *sync.WaitGroup) {
|
2020-02-15 13:58:01 +01:00
|
|
|
var cmds commands
|
2020-12-29 18:28:19 +01:00
|
|
|
if rgbsplit {
|
2020-02-15 13:58:01 +01:00
|
|
|
// do a RGB split of white
|
2020-12-28 00:58:50 +01:00
|
|
|
imgmod := render.ImgColorFilter(img, color.NRGBA{0xff, 0xff, 0xff, 0xff}, color.NRGBA{0xff, 0, 0, 0xff})
|
2020-02-15 13:58:01 +01:00
|
|
|
cmds = append(cmds, commandsFromImage(imgmod, image.Pt(position.X-10, position.Y-10))...)
|
2020-12-28 00:58:50 +01:00
|
|
|
imgmod = render.ImgColorFilter(img, color.NRGBA{0xff, 0xff, 0xff, 0xff}, color.NRGBA{0, 0xff, 0, 0xff})
|
2020-02-15 13:58:01 +01:00
|
|
|
cmds = append(cmds, commandsFromImage(imgmod, image.Pt(position.X+10, position.Y))...)
|
2020-12-28 00:58:50 +01:00
|
|
|
imgmod = render.ImgColorFilter(img, color.NRGBA{0xff, 0xff, 0xff, 0xff}, color.NRGBA{0, 0, 0xff, 0xff})
|
2020-02-15 13:58:01 +01:00
|
|
|
cmds = append(cmds, commandsFromImage(imgmod, image.Pt(position.X-10, position.Y+10))...)
|
|
|
|
cmds = append(cmds, commandsFromImage(img, position)...)
|
|
|
|
} else {
|
|
|
|
cmds = commandsFromImage(img, position)
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:39:24 +01:00
|
|
|
if shuffle {
|
|
|
|
cmds.Shuffle()
|
|
|
|
}
|
2020-12-29 18:28:19 +01:00
|
|
|
|
|
|
|
var messages [][]byte
|
|
|
|
var maxX, maxY int
|
|
|
|
if randoffset {
|
|
|
|
maxX, maxY = CanvasSize(address)
|
|
|
|
messages = cmds.Chunk(1) // each connection should send the full img
|
|
|
|
} else {
|
|
|
|
messages = cmds.Chunk(conns)
|
|
|
|
}
|
2020-02-13 22:26:50 +01:00
|
|
|
|
|
|
|
bombWg := sync.WaitGroup{}
|
2020-12-29 18:28:19 +01:00
|
|
|
for i := 0; i < conns; i++ {
|
|
|
|
msg := messages[0]
|
|
|
|
if len(messages) > i {
|
|
|
|
msg = messages[i]
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:26:50 +01:00
|
|
|
bombWg.Add(1)
|
2020-12-29 18:28:19 +01:00
|
|
|
if randoffset {
|
|
|
|
msg = append(OffsetCmd(
|
|
|
|
rand.Intn(maxX-img.Bounds().Canon().Dx()),
|
|
|
|
rand.Intn(maxY-img.Bounds().Canon().Dy()),
|
|
|
|
), msg...)
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:26:50 +01:00
|
|
|
go bombAddress(msg, address, stop, &bombWg)
|
2020-02-06 13:39:24 +01:00
|
|
|
}
|
2020-02-13 22:26:50 +01:00
|
|
|
bombWg.Wait()
|
2020-02-13 23:57:56 +01:00
|
|
|
if wg != nil {
|
|
|
|
wg.Done()
|
|
|
|
}
|
2020-02-06 13:39:24 +01:00
|
|
|
}
|
|
|
|
|
2020-12-29 18:28:19 +01:00
|
|
|
// CanvasSize returns the size of the canvas as returned by the server
|
|
|
|
func CanvasSize(address string) (int, int) {
|
|
|
|
conn, err := net.Dial("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
conn.Write([]byte("SIZE\n"))
|
|
|
|
reader := bufio.NewReader(conn)
|
|
|
|
res, err := reader.ReadSlice('\n')
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return parseXY(res[5:])
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:39:24 +01:00
|
|
|
// FetchImage asynchronously uses `conns` to fetch pixels within `bounds` from
|
|
|
|
// a pixelflut server at `address`, and writes them into the returned Image.
|
2020-02-13 22:26:50 +01:00
|
|
|
func FetchImage(bounds image.Rectangle, address string, conns int, stop chan bool) (img *image.NRGBA) {
|
2020-02-06 13:39:24 +01:00
|
|
|
img = image.NewNRGBA(bounds)
|
2020-02-13 23:57:56 +01:00
|
|
|
cmds := cmdsFetchImage(bounds).Chunk(conns)
|
2020-02-06 13:39:24 +01:00
|
|
|
|
|
|
|
for i := 0; i < conns; i++ {
|
|
|
|
conn, err := net.Dial("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:26:50 +01:00
|
|
|
go readPixels(img, conn, stop)
|
2020-02-13 23:57:56 +01:00
|
|
|
go bombConn(cmds[i], conn, stop)
|
2020-02-06 13:39:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:26:50 +01:00
|
|
|
func readPixels(target *image.NRGBA, conn net.Conn, stop chan bool) {
|
2020-02-11 13:34:33 +01:00
|
|
|
reader := bufio.NewReader(conn)
|
|
|
|
col := make([]byte, 3)
|
2020-02-06 13:39:24 +01:00
|
|
|
for {
|
2020-02-13 22:26:50 +01:00
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return
|
2020-02-06 13:39:24 +01:00
|
|
|
|
2020-02-13 22:26:50 +01:00
|
|
|
default:
|
|
|
|
res, err := reader.ReadSlice('\n')
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2020-02-10 15:33:47 +01:00
|
|
|
}
|
2020-02-11 13:34:33 +01:00
|
|
|
|
2020-02-13 22:26:50 +01:00
|
|
|
// parse response ("PX <x> <y> <col>\n")
|
|
|
|
colorStart := len(res) - 7
|
2020-12-29 18:28:19 +01:00
|
|
|
x, y := parseXY(res[3 : colorStart-1])
|
2020-02-13 22:26:50 +01:00
|
|
|
hex.Decode(col, res[colorStart:len(res)-1])
|
|
|
|
|
|
|
|
target.SetNRGBA(x, y, color.NRGBA{col[0], col[1], col[2], 255})
|
|
|
|
}
|
2020-02-11 13:34:33 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-06 13:39:24 +01:00
|
|
|
|
2020-12-29 18:28:19 +01:00
|
|
|
func parseXY(xy []byte) (int, int) {
|
|
|
|
last := len(xy) - 1
|
|
|
|
yStart := last - 1 // y is at least one char long
|
|
|
|
for ; yStart >= 0; yStart-- {
|
|
|
|
if xy[yStart] == ' ' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x := asciiToInt(xy[:yStart])
|
|
|
|
y := asciiToInt(xy[yStart+1 : last])
|
|
|
|
return x, y
|
|
|
|
}
|
|
|
|
|
2020-02-11 13:34:33 +01:00
|
|
|
func asciiToInt(buf []byte) (v int) {
|
|
|
|
for _, c := range buf {
|
2020-02-13 22:26:50 +01:00
|
|
|
v = v*10 + int(c-'0')
|
2020-02-06 13:39:24 +01:00
|
|
|
}
|
2020-02-11 13:34:33 +01:00
|
|
|
return v
|
2020-02-06 13:39:24 +01:00
|
|
|
}
|