about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--flexerilla_logo.jpgbin0 -> 15156 bytes
-rw-r--r--go.mod3
-rw-r--r--go.sum17
-rw-r--r--main.go192
4 files changed, 178 insertions, 34 deletions
diff --git a/flexerilla_logo.jpg b/flexerilla_logo.jpg
new file mode 100644
index 0000000..ed611c4
--- /dev/null
+++ b/flexerilla_logo.jpg
Binary files differdiff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..d6ee0fc
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module git.darknebu.la/pixeltsunami
+
+go 1.12
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..841c93f
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,17 @@
+github.com/cheggaaa/pb v2.0.6+incompatible h1:sutSx+mRaNbeJUMCAtyqNWU/tQ0B/xBm+hyb1JQmQYs=
+github.com/cheggaaa/pb v2.0.6+incompatible/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
+github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
+github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
+github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
+github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
+github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
+golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
+golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+gopkg.in/VividCortex/ewma.v1 v1.1.1 h1:tWHEKkKq802K/JT9RiqGCBU5fW3raAPnJGTE9ostZvg=
+gopkg.in/VividCortex/ewma.v1 v1.1.1/go.mod h1:TekXuFipeiHWiAlO1+wSS23vTcyFau5u3rxXUSXj710=
+gopkg.in/cheggaaa/pb.v2 v2.0.6 h1:L2KAo2l2ZQTzxmh8b9RdQpzgLpK2mX3paGCMJSUugBk=
+gopkg.in/cheggaaa/pb.v2 v2.0.6/go.mod h1:0CiZ1p8pvtxBlQpLXkHuUTpdJ1shm3OqCF1QugkjHL4=
+gopkg.in/fatih/color.v1 v1.7.0 h1:bYGjb+HezBM6j/QmgBfgm1adxHpzzrss6bj4r9ROppk=
+gopkg.in/fatih/color.v1 v1.7.0/go.mod h1:P7yosIhqIl/sX8J8UypY5M+dDpD2KmyfP5IRs5v/fo0=
+gopkg.in/mattn/go-runewidth.v0 v0.0.4 h1:r0P71TnzQDlNIcizCqvPSSANoFa3WVGtcNJf3TWurcY=
+gopkg.in/mattn/go-runewidth.v0 v0.0.4/go.mod h1:BmXejnxvhwdaATwiJbB1vZ2dtXkQKZGu9yLFCZb4msQ=
diff --git a/main.go b/main.go
index a5f5503..97de251 100644
--- a/main.go
+++ b/main.go
@@ -4,9 +4,10 @@ import (
 	"bytes"
 	"flag"
 	"fmt"
+	"image"
 	"log"
 	"net"
-	"time"
+	"os"
 )
 
 var (
@@ -14,7 +15,7 @@ var (
 	port = flag.String("port", "1337", "Server port")
 	address string
 
-	imagePath = flag.String("image", "foo.png", "Relative image path")
+	imagePath = flag.String("image", "", "Relative image path")
 	imageOffsetX = flag.Int("xoffset", 0, "xoffset")
 	imageOffsetY = flag.Int("yoffset", 0, "yoffset")
 
@@ -24,7 +25,7 @@ var (
 	testConn = flag.Bool("t", false, "test the connection before escalating completely")
 
 	fill = flag.Bool("fill", true, "fill the complete canvas")
-	color = flag.String("color", "000000", "define a color")
+	color = flag.String("col", "000000", "define a color")
 
 	cores = flag.Int("cores", 1, "Amount of cores to use")
 )
@@ -79,48 +80,171 @@ func buildSendString(start int, end int, doneChannel chan bool, stripBufferChann
 	stripBufferChannel <- stripBuffer
 }
 
-func main() {
+// openImage opens an image at the given path and returns an image.Image
+func openImage(imagePath string) image.Image {
+	log.Println("AA")
+	reader, openErr := os.Open(imagePath)
+	if openErr != nil {
+		log.Fatal(openErr)
+	}
+	log.Println("AB")
+
+	fmt.Println(*reader)
+
+	img, _, imageDecodeErr := image.Decode(reader)
+	if imageDecodeErr != nil {
+		log.Fatal(openErr)
+	}
+	log.Println("AC")
+
+	return img
+}
+
+func buildSendStringImage(image image.Image, start int, end int, doneChannel chan bool, stripBufferChannel chan bytes.Buffer) {
+
+	var imageHeight = image.Bounds().Max.Y
+	var stripBuffer bytes.Buffer
+
+	fmt.Printf("ImageHeight: %d", imageHeight)
+
+	for x := start; x < end; x++ {
+		for y := 0; y < imageHeight; y++ {
+			r, g, b, _ := image.At(x, y).RGBA()
+			fmt.Printf("%v %v %v", r, g, b)
+			command := fmt.Sprintf("PX %d %d %.2x%.2x%.2x", x, y, r, g, b)
+			stripBuffer.Write([]byte(command))
+		}
+	}
 
+	doneChannel <- true
+	stripBufferChannel <- stripBuffer
+}
+
+func main() {
 	log.Printf("[ ] %s -> %s:%s at (%d, %d)", *imagePath, *host, *port, imageOffsetX, imageOffsetY)
 	parseFlags()
 	testConnection()
 
-	doneChannel := make(chan bool)
-	stripBufferChannel := make(chan bytes.Buffer)
-	var completeBuffer bytes.Buffer
+	if *fill == true {
+		// define channels used to bundle the data generated and get information
+		doneChannel := make(chan bool)
+		stripBufferChannel := make(chan bytes.Buffer)
+		var completeBuffer bytes.Buffer
 
-	var stripwidth int = *canvasWidth / *cores
-	for thread := 0; thread < *cores; thread++ {
-		log.Printf("Starting thread %d", thread)
-		go buildSendString(thread * stripwidth, (thread + 1) * stripwidth, doneChannel, stripBufferChannel)
-	}
+		// calculate the width of the individual stripes
+		var stripwidth int = *canvasWidth / *cores
 
-	for thread := 0; thread < *cores; thread++ {
-		log.Printf("Thread %d done!", thread)
-		_ = <- doneChannel
+		// create a new buildStringBot generating the stripes
+		for thread := 0; thread < *cores; thread++ {
+			log.Printf("Starting thread %d", thread)
+			go buildSendString(thread * stripwidth, (thread + 1) * stripwidth, doneChannel, stripBufferChannel)
+		}
 
-		stripBufferChannelOutput := <- stripBufferChannel
-		completeBuffer.Write(stripBufferChannelOutput.Bytes())
-	}
+		// catch all threads
+		for thread := 0; thread < *cores; thread++ {
+			// get a "Done" message from each worker
+			_ = <- doneChannel
 
-	// write
-	connection, netDialError := net.Dial("tcp", address)
-	if netDialError != nil {
-		log.Fatal(netDialError)
-	}
+			// get the buffer generated and append it to the complete buffer by writing it there
+			stripBufferChannelOutput := <- stripBufferChannel
+			completeBuffer.Write(stripBufferChannelOutput.Bytes())
 
-	_, writeErr := connection.Write(completeBuffer.Bytes())
-	if writeErr != nil {
-		log.Fatal(writeErr)
-	}
+			log.Printf("Thread %d done!", thread)
+		}
 
-	connectionCloseError := connection.Close()
-	if connectionCloseError != nil {
-		log.Fatal(connectionCloseError)
+		// write the command to the server
+		connection, netDialError := net.Dial("tcp", address)
+		if netDialError != nil {
+			log.Fatal(netDialError)
+		}
+
+
+		i := 0
+		for i < 500 {
+			// actual write
+			_, writeErr := connection.Write(completeBuffer.Bytes())
+			if writeErr != nil {
+				log.Fatal(writeErr)
+			}
+
+			fmt.Printf(".")
+			i++
+		}
+
+		fmt.Printf("\n")
+
+		// close the connection
+		connectionCloseError := connection.Close()
+		if connectionCloseError != nil {
+			log.Fatal(connectionCloseError)
+		}
+
+		// cleanup
+		fmt.Printf("cleanup: %d -> %d", *cores * stripwidth, *canvasWidth)
 	}
 
-	// cleanup
-	fmt.Printf("cleanup: %d -> %d", *cores * stripwidth, *canvasWidth)
+	if *imagePath != "" {
+		log.Println("[ ] Drawing the image!")
+
+		log.Println("A")
+
+		// open the image
+		var image = openImage(*imagePath)
+		fmt.Println(image)
+		log.Println("b")
+
+		// define channels used to bundle the data generated and get informations
+		doneChannel := make(chan bool)
+		stripBufferChannel := make(chan bytes.Buffer)
+		var completeBuffer bytes.Buffer
+		log.Println("c")
+
+		// calculate the width of the individual stripes
+		var stripwidth int = image.Bounds().Max.X / *cores
+		log.Println("d")
 
-	time.Sleep(1 * time.Second)
-}
\ No newline at end of file
+		// create new buildSendString workers building the string that should be sent to the pixelflut server
+		for thread := 0; thread < *cores; thread++ {
+			log.Printf("Staring thread %d", thread)
+			go buildSendStringImage(image, thread * stripwidth, (thread+1) * stripwidth, doneChannel, stripBufferChannel)
+		}
+		log.Println("e")
+
+		// catch all the threads
+		for thread := 0; thread < *cores; thread++ {
+			// get a "Done" message from each worker
+			_ = <- doneChannel
+
+			// get the buffer generated and append it to the complete buffer by writing it there
+			stripBufferChannelOutput := <- stripBufferChannel
+			completeBuffer.Write(stripBufferChannelOutput.Bytes())
+
+			log.Printf("Thread %d done!")
+		}
+		log.Println("f")
+
+		// write the command to the server
+		connection, netDialError := net.Dial("tcp", address)
+		if netDialError != nil {
+			log.Fatal(netDialError)
+		}
+		log.Println("g")
+
+		// actual write
+		_, writeErr := connection.Write(completeBuffer.Bytes())
+		if writeErr != nil {
+			log.Fatal(writeErr)
+		}
+		log.Println("h")
+
+		// close the connection
+		connectionCloseError := connection.Close()
+		if connectionCloseError != nil {
+			log.Fatal(connectionCloseError)
+		}
+		log.Println("i")
+
+		// cleanup
+		fmt.Printf("cleanup: %d -> %d", *cores * stripwidth, *canvasWidth)
+	}
+}