/
contrast.go
53 lines (41 loc) · 1.28 KB
/
contrast.go
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
package cmd
import (
"github.com/Nyarum/hadfield"
"github.com/Nyarum/img/contrast"
"github.com/Nyarum/img/utils"
)
var (
contrastLinear, contrastSigmoidal bool
contrastFactor, contrastMidpoint float64
)
func Contrast() *hadfield.Command {
cmd := &hadfield.Command{
Usage: "contrast [options]",
Short: "adjust image contrast",
Long: `
Contrast takes an image from STDIN, adjusts the contrast and prints the result
to STDOUT
--factor <n> # Factor to shift contrast by (default: 1.0)
--midpoint <n> # Set where midtones fall in image (default: 0.5)
--linear # Use linear function
--sigmoidal # Use sigmoidal function
`,
}
cmd.Run = runContrast
cmd.Flag.Float64Var(&contrastFactor, "factor", 1.0, "")
cmd.Flag.Float64Var(&contrastMidpoint, "midpoint", 0.5, "")
cmd.Flag.BoolVar(&contrastLinear, "linear", false, "")
cmd.Flag.BoolVar(&contrastSigmoidal, "sigmoidal", false, "")
return cmd
}
func runContrast(cmd *hadfield.Command, args []string) {
i, data := utils.ReadStdin()
if contrastSigmoidal {
i = contrast.Sigmoidal(i, contrastFactor, contrastMidpoint)
} else if contrastLinear {
i = contrast.Linear(i, contrastFactor)
} else {
i = contrast.Adjust(i, contrastFactor)
}
utils.WriteStdout(i, data)
}