Sfoglia il codice sorgente

feat: add save image method

v0lp3 4 anni fa
parent
commit
65780cd9dd
1 ha cambiato i file con 37 aggiunte e 0 eliminazioni
  1. 37 0
      src/main.go

+ 37 - 0
src/main.go

@@ -4,6 +4,9 @@ import (
 	"encoding/binary"
 	"flag"
 	"fmt"
+	"image"
+	"image/color"
+	"image/png"
 	"log"
 	"net"
 	"os"
@@ -40,6 +43,8 @@ func main() {
 
 	rawImage := RemoveHeaders(bytes)
 
+	SaveImage(rawImage, width, heigth, *name, *color)
+
 }
 
 func SendRequest(socket net.Conn, resolution int, _mode string, adf bool) (int, int) {
@@ -116,6 +121,38 @@ readPackets:
 	return scanBytes
 }
 
+func SaveImage(data []byte, width int, height int, name string, color string) {
+
+	log.Println("Saving image...")
+
+	_, compression := GetCompressionMode(color)
+
+	if compression != "JPEG" {
+
+		img := image.NewGray(image.Rectangle{
+			image.Point{0, 0},
+			image.Point{width, height},
+		})
+
+		for y := 0; y < height-40; y++ {
+
+			for x := 0; x < width; x++ {
+				img.SetGray(x, y, colorToGray(data[y*width+x]))
+			}
+		}
+
+		file, _ := os.Create(name)
+		png.Encode(file, img)
+
+	} else {
+		os.WriteFile(name, data, 0644)
+	}
+}
+
+func colorToGray(byte byte) color.Gray {
+	return color.Gray{Y: byte}
+}
+
 func HandleError(err error) {
 
 	if err != nil {