Browse Source

init

master
Martin/Geno 3 years ago
commit
b0cd98b305
No known key found for this signature in database GPG Key ID: 9D7D3C6BFF600C6A
  1. 24
      color.go
  2. 43
      img.go
  3. 87
      main.go
  4. 20
      part.go

24
color.go

@ -0,0 +1,24 @@
package main
import (
"image/color"
)
func getColor(c color.Color, width, height int) [][][4]uint32 {
var pixelmatrix [][][4]uint32
for x := 0; x < width; x++ {
var xRow [][4]uint32
for y := 0; y < height; y++ {
var pixel [4]uint32
pixel[0], pixel[1], pixel[2], pixel[3] = c.RGBA()
pixel[0] = pixel[0] >>8
pixel[1] = pixel[1] >>8
pixel[2] = pixel[2] >>8
pixel[3] = pixel[3] >>8
xRow = append(xRow, pixel)
}
pixelmatrix = append(pixelmatrix, xRow)
}
return pixelmatrix
}

43
img.go

@ -0,0 +1,43 @@
package main
import (
"os"
"image"
_ "image/jpeg"
_ "image/gif"
_ "image/png"
log "github.com/sirupsen/logrus"
)
func getImage(path string) [][][4]uint32 {
reader, err := os.Open(path)
if err != nil {
log.Fatalf("Could not load image: %s",err)
}
defer reader.Close()
m, _, err := image.Decode(reader)
if err != nil {
log.Fatal("could not find image in file", err)
}
bounds := m.Bounds()
var pixelmatrix [][][4]uint32
for x := bounds.Min.X; x < bounds.Max.X; x++ {
var xRow [][4]uint32
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
var pixel [4]uint32
pixel[0], pixel[1], pixel[2], pixel[3] = m.At(x, y).RGBA()
pixel[0] = pixel[0] >>8
pixel[1] = pixel[1] >>8
pixel[2] = pixel[2] >>8
pixel[3] = pixel[3] >>8
xRow = append(xRow, pixel)
}
pixelmatrix = append(pixelmatrix, xRow)
}
return pixelmatrix
}

87
main.go

@ -0,0 +1,87 @@
package main
import (
"net"
"flag"
"os"
"os/signal"
"syscall"
"fmt"
"image/color"
log "github.com/sirupsen/logrus"
)
var targetAddr string
var imgPath string
var goProc int
var startx int
var starty int
var partTotal int
var partCount int
var partBegin int
func main() {
flag.StringVar(&targetAddr, "addr", "151.217.177.136:1234", "address of target tcp")
flag.StringVar(&imgPath, "img", "", "path to image")
flag.IntVar(&goProc, "proc", 1, "count of procs (go routines)")
flag.IntVar(&startx, "start-x", 0, "move at x")
flag.IntVar(&starty, "start-y", 0, "move at y")
flag.IntVar(&partTotal, "part-total", 1, "total count of parts")
flag.IntVar(&partCount, "part-count", 1, "showed parts")
flag.IntVar(&partBegin, "part-begin", 0, "beginning at part x")
flag.Parse()
log.Infof("starting for %s with %d proc(s) | move by (%d,%d) | %d parts of %d begin at %d", targetAddr, goProc, startx, starty, partCount, partTotal, partBegin)
var pixelmatrix [][][4]uint32
// get matrix
if imgPath != "" {
pixelmatrix = getImage(imgPath)
}else{
pixelmatrix = getColor(color.RGBA{0xcc, 0xff, 0x33, 0xff}, 1024, 1024)
}
// transform
pixelmatrix = getPart(pixelmatrix, partCount, partTotal, partBegin)
// connection(s)
conn, err := net.Dial("tcp", targetAddr)
if err != nil {
log.Fatalf("not able to create connection: %s", err)
}
defer conn.Close()
// start painting
for s := 0; s < goProc; s++ {
go paint(conn, pixelmatrix, startx, starty)
}
// Wait for INT/TERM
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
log.Println("received", sig)
}
func paint(conn net.Conn, pixelmatrix [][][4]uint32, xstart, ystart int) {
for {
for x, row := range pixelmatrix {
for y, pixel := range row {
if pixel[3] != 0 {
msg := fmt.Sprintf("PX %d %d %02x%02x%02x\n", xstart+x, ystart+y, pixel[0], pixel[1], pixel[2])
conn.Write([]byte(msg))
}
}
}
}
}

20
part.go

@ -0,0 +1,20 @@
package main
import (
log "github.com/sirupsen/logrus"
)
func getPart(pixelmatrix [][][4]uint32, partCount, partTotal, partBegin int) [][][4]uint32 {
xLength := len(pixelmatrix)
if xLength < partTotal {
log.Fatal("more parts then rows not possible")
return nil
}
partSize := (xLength / partTotal)
// last part should return last rows
if partBegin + partCount == partTotal {
return pixelmatrix[ partBegin * partSize : ]
}
return pixelmatrix[ partBegin * partSize : (partBegin + partCount) * partSize]
}
Loading…
Cancel
Save