Prechádzať zdrojové kódy

add debug flag to save intermediate files during processing

Corey Smith 4 rokov pred
rodič
commit
2a2cd96583
2 zmenil súbory, kde vykonal 15 pridanie a 10 odobranie
  1. 11 7
      src/backend.go
  2. 4 3
      src/main.go

+ 11 - 7
src/backend.go

@@ -11,7 +11,7 @@ import (
 	"time"
 )
 
-func Scan(brotherIP string, brotherPort int, resolution int, color string, rawinput string) ([][]byte, int, int) {
+func Scan(brotherIP string, brotherPort int, resolution int, color string, rawinput string, debug bool) ([][]byte, int, int) {
 	if rawinput == "" {
 		log.Println("Valid IP address, opening socket...")
 
@@ -25,8 +25,10 @@ func Scan(brotherIP string, brotherPort int, resolution int, color string, rawin
 		bytes, err := getScanBytes(socket)
 
 		HandleError(err)
-		err = os.WriteFile(".rawbytes", bytes, 0644)
-		HandleError(err)
+		if debug {
+			err = os.WriteFile(".rawbytes", bytes, 0644)
+			HandleError(err)
+		}
 		return removeHeaders(bytes), width, height
 	} else {
 		log.Println("Bypassing socket...")
@@ -116,7 +118,7 @@ readPackets:
 	return scanBytes, nil
 }
 
-func SaveImage(data []byte, width int, height int, name string, color string) {
+func SaveImage(data []byte, width int, height int, name string, color string, debug bool) {
 
 	log.Println("Saving image...")
 
@@ -153,9 +155,11 @@ func SaveImage(data []byte, width int, height int, name string, color string) {
 
 		err = tiff.Encode(file, img, &options)
 
-		rawName := fmt.Sprintf("%s.raw", name)
-		err = os.WriteFile(rawName, data, 0644)
-		HandleError(err)
+		if debug {
+			rawName := fmt.Sprintf("%s.raw", name)
+			err = os.WriteFile(rawName, data, 0644)
+			HandleError(err)
+		}
 	} else {
 		err := os.WriteFile(name, data, 0644)
 		HandleError(err)

+ 4 - 3
src/main.go

@@ -11,6 +11,7 @@ func main() {
 
 	brotherIP := flag.String("a", "192.168.0.157", "IP address of the Brother scanner")
 	color := flag.String("c", "CGRAY", "Color mode of the scan (CGRAY, TEXT)")
+	debug := flag.Bool("d", false, "Enable debug mode - save .rawbytes and .raw file")
 	rawinput := flag.String("i", "", "raw input file to parse instead of socket")
 	name := flag.String("n", "scan.tiff", "Name of the output file")
 	resolution := flag.Int("r", 300, "Resolution of the scan")
@@ -21,13 +22,13 @@ func main() {
 		HandleError(fmt.Errorf("invalid IP address: %s", *brotherIP))
 	}
 
-	rawImages, width, height := Scan(*brotherIP, brotherPort, *resolution, *color, *rawinput)
+	rawImages, width, height := Scan(*brotherIP, brotherPort, *resolution, *color, *rawinput, *debug)
 
 	for i, rawImage := range rawImages {
 		if i == len(rawImages)-1 {
-			SaveImage(rawImage, width, height, fmt.Sprintf("%s-%d.tiff", *name, i), *color)
+			SaveImage(rawImage, width, height, fmt.Sprintf("%s-%d.tiff", *name, i), *color, *debug)
 		} else {
-			go SaveImage(rawImage, width, height, fmt.Sprintf("%s-%d.tiff", *name, i), *color)
+			go SaveImage(rawImage, width, height, fmt.Sprintf("%s-%d.tiff", *name, i), *color, *debug)
 		}
 	}
 }