summaryrefslogtreecommitdiff
path: root/cmd/pggraph/main.go
blob: fd61761b5b0c33b1c9faa44dd9333b701f428f2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2020 Nick White.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.

// pggraph creates a graph showing the proportion of black pixels
// for slices through a binarised image. This is useful to determine
// appropriate parameters to pass to the wipe functions in this
// module.
package main

import (
	"flag"
	"fmt"
	"image"
	"image/draw"
	_ "image/jpeg"
	_ "image/png"
	"io"
	"log"
	"os"
	"sort"

	chart "github.com/wcharczuk/go-chart"
	"rescribe.xyz/integral"
	"rescribe.xyz/preproc"
)

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
appropriate parameters to pass to the wipe functions in
this module.
`

const middlePercent = 20
const numTicks = 30

// sideways flips an image sideways
func sideways(img *image.Gray) *image.Gray {
	b := img.Bounds()
	newb := image.Rect(b.Min.Y, b.Min.X, b.Max.Y, b.Max.X)
	new := image.NewGray(newb)
	for x := b.Min.X; x < b.Max.X; x++ {
		for y := b.Min.Y; y < b.Max.Y; y++ {
			new.SetGray(y, x, img.GrayAt(x, y))
		}
	}
	return new
}

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 miny, maxy, fmt.Errorf("Not enough points to graph, only %d\n", len(points))
	}

	for x, _ := range points {
		xs = append(xs, x)
	}
	sort.Ints(xs)

	for _, x := range xs {
		xvals = append(xvals, float64(x))
		yvals = append(yvals, points[x])
	}

	mainSeries := chart.ContinuousSeries{
		Style: chart.Style{
			StrokeColor: chart.ColorBlue,
			FillColor:   chart.ColorAlternateBlue,
		},
		XValues: xvals,
		YValues: yvals,
	}

	numAroundMiddle := int(middlePercent / 2 * float64(len(xs)) / float64(100))
	if numAroundMiddle > 1 {
		for i := (len(xs) / 2) - numAroundMiddle; i < (len(xs)/2)+numAroundMiddle; i++ {
			midxs = append(midxs, xs[i])
		}
	} else {
		midxs = xs
	}

	for _, x := range midxs {
		midxvals = append(midxvals, float64(x))
		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,
	}

	minSeries := &chart.MinSeries{
		Style: chart.Style{
			StrokeColor:     chart.ColorAlternateGray,
			StrokeDashArray: []float64{5.0, 5.0},
		},
		InnerSeries: middleSeries,
	}

	maxSeries := &chart.MaxSeries{
		Style: chart.Style{
			StrokeColor:     chart.ColorAlternateGray,
			StrokeDashArray: []float64{5.0, 5.0},
		},
		InnerSeries: middleSeries,
	}

	var ticks []chart.Tick
	var tickEvery = xs[len(xs)-1] / numTicks
	for i := 0; i < xs[len(xs)-1]; i += tickEvery {
		ticks = append(ticks, chart.Tick{float64(i), fmt.Sprintf("%d", i)})
	}
	lastx := xs[len(xs)-1]
	ticks[len(ticks)-1] = chart.Tick{float64(lastx), fmt.Sprintf("%d", lastx)}

	graph := chart.Chart{
		Title:  title,
		Width:  1920,
		Height: 800,
		XAxis: chart.XAxis{
			Name:  "Pixel number",
			Ticks: ticks,
		},
		YAxis: chart.YAxis{
			Name: "Proportion of black pixels",
			Range: &chart.ContinuousRange{
				Min: 0.0,
				Max: 0.5,
			},
		},
		Series: []chart.Series{
			mainSeries,
			minSeries,
			maxSeries,
			chart.LastValueAnnotationSeries(minSeries),
			chart.LastValueAnnotationSeries(maxSeries),
		},
	}

	return miny, maxy, graph.Render(chart.PNG, w)
}

func main() {
	flag.Usage = func() {
		fmt.Fprintf(flag.CommandLine.Output(), usage)
		flag.PrintDefaults()
	}
	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()
		os.Exit(1)
	}

	f, err := os.Open(flag.Arg(0))
	defer f.Close()
	if err != nil {
		log.Fatalf("Could not open file %s: %v\n", flag.Arg(0), err)
	}
	img, _, err := image.Decode(f)
	if err != nil {
		log.Fatalf("Could not decode image: %v\n", err)
	}
	b := img.Bounds()
	if *vertical {
		gray := image.NewGray(b)
		draw.Draw(gray, b, img, b.Min, draw.Src)
		img = sideways(gray)
		b = img.Bounds()
	}
	intImg := integral.NewImage(b)
	draw.Draw(intImg, b, img, b.Min, draw.Src)

	points := make(map[int]float64)
	maxx := b.Dx() - 1
	for x := 0; x+*width < maxx; x += *width {
		points[x] = preproc.ProportionSlice(intImg, x, *width)
	}

	f, err = os.Create(flag.Arg(1))
	defer f.Close()
	if err != nil {
		log.Fatalf("Could not create file %s: %v\n", flag.Arg(1), err)
	}

	title := fmt.Sprintf("Proportion of black pixels over %s (width %d)", flag.Arg(0), *width)
	if *vertical {
		title += " (vertical)"
	}
	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)
	}
}