From 179bb4a20ad93d418e3e911f90f98b6099a3f0ef Mon Sep 17 00:00:00 2001
From: Nick White <git@njw.name>
Date: Mon, 24 Aug 2020 14:49:50 +0100
Subject: Add verbose option to print min/max values

---
 cmd/pggraph/main.go | 32 +++++++++++++++++++++++++++-----
 1 file changed, 27 insertions(+), 5 deletions(-)

(limited to 'cmd')

diff --git a/cmd/pggraph/main.go b/cmd/pggraph/main.go
index 2da5c57..fd61761 100644
--- a/cmd/pggraph/main.go
+++ b/cmd/pggraph/main.go
@@ -25,7 +25,7 @@ import (
 	"rescribe.xyz/preproc"
 )
 
-const usage = `Usage: pggraph [-vertical] [-width] inimg graphname
+const usage = `Usage: pggraph [-vertical] [-width] [-v] inimg graphname
 
 Creates a graph showing the proportion of black pixels for
 slices through a binarised image. This is useful to determine
@@ -49,14 +49,15 @@ func sideways(img *image.Gray) *image.Gray {
 	return new
 }
 
-func graph(title string, points map[int]float64, w io.Writer) error {
+func graph(title string, points map[int]float64, w io.Writer) (float64, float64, error) {
 	var xvals, yvals []float64
 	var xs []int
 	var midxvals, midyvals []float64
 	var midxs []int
+	var miny, maxy float64
 
 	if len(points) < 2 {
-		return fmt.Errorf("Not enough points to graph, only %d\n", len(points))
+		return miny, maxy, fmt.Errorf("Not enough points to graph, only %d\n", len(points))
 	}
 
 	for x, _ := range points {
@@ -92,6 +93,18 @@ func graph(title string, points map[int]float64, w io.Writer) error {
 		midyvals = append(midyvals, points[x])
 	}
 
+	miny = 100.0
+	maxy = 0.0
+	for _, x := range midxs {
+		y := points[x]
+		if y < miny {
+			miny = y
+		}
+		if y > maxy {
+			maxy = y
+		}
+	}
+
 	middleSeries := chart.ContinuousSeries{
 		XValues: midxvals,
 		YValues: midyvals,
@@ -131,6 +144,10 @@ func graph(title string, points map[int]float64, w io.Writer) error {
 		},
 		YAxis: chart.YAxis{
 			Name: "Proportion of black pixels",
+			Range: &chart.ContinuousRange{
+				Min: 0.0,
+				Max: 0.5,
+			},
 		},
 		Series: []chart.Series{
 			mainSeries,
@@ -141,7 +158,7 @@ func graph(title string, points map[int]float64, w io.Writer) error {
 		},
 	}
 
-	return graph.Render(chart.PNG, w)
+	return miny, maxy, graph.Render(chart.PNG, w)
 }
 
 func main() {
@@ -151,6 +168,7 @@ func main() {
 	}
 	vertical := flag.Bool("vertical", false, "Slice image vertically (from top to bottom) rather than horizontally")
 	width := flag.Int("width", 5, "Width of slice in pixels (height if in vertical mode)")
+	verbose := flag.Bool("v", false, "Print the minimum and maximum values of the middle section to the console")
 	flag.Parse()
 	if flag.NArg() < 2 {
 		flag.Usage()
@@ -192,8 +210,12 @@ func main() {
 	if *vertical {
 		title += " (vertical)"
 	}
-	err = graph(title, points, f)
+	miny, maxy, err := graph(title, points, f)
 	if err != nil {
 		log.Fatalf("Could not create graph: %v\n", err)
 	}
+
+	if *verbose {
+		fmt.Printf("%s %0.2f %0.2f\n", flag.Arg(0), miny, maxy)
+	}
 }
-- 
cgit v1.2.1-24-ge1ad