forked from hawx/img
/
levels.go
100 lines (74 loc) · 2.66 KB
/
levels.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
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
package main
import (
"github.com/hawx/img/levels"
"github.com/hawx/img/utils"
"image"
)
var cmdLevels = &Command{
UsageLine: "levels [options]",
Short: "adjust image levels",
Long: `
Levels adjusts the levels of an image. You can set the black/white point or
give points in a curve to use. It can act on all colour channels (default
behaviour), or just those you want.
--red # Act on red channel
--green # Act on green channel
--blue # Act on blue channel
--auto # Auto adjust levels to fit
--auto-black # Auto adjust black point to fit
--auto-white # Auto adjust white point to fit
--black [n] # Set black point
--white [n] # Set white point
--curve [c] # Set curve. Argument is a list of 'point,value' pairs
# delimited by spaces, eg. --curve "0,0 33,40 66,60 100,100"
`,
}
var levelsRed, levelsGreen, levelsBlue bool
var levelsAuto, levelsAutoBlack, levelsAutoWhite bool
var levelsBlack, levelsWhite float64
var levelsCurve string
func init() {
cmdLevels.Run = runLevels
cmdLevels.Flag.BoolVar(&levelsRed, "red", false, "")
cmdLevels.Flag.BoolVar(&levelsGreen, "green", false, "")
cmdLevels.Flag.BoolVar(&levelsBlue, "blue", false, "")
cmdLevels.Flag.BoolVar(&levelsAuto, "auto", false, "")
cmdLevels.Flag.BoolVar(&levelsAutoBlack, "auto-black", false, "")
cmdLevels.Flag.BoolVar(&levelsAutoWhite, "auto-white", false, "")
cmdLevels.Flag.Float64Var(&levelsBlack, "black", 0, "")
cmdLevels.Flag.Float64Var(&levelsWhite, "white", 100, "")
cmdLevels.Flag.StringVar(&levelsCurve, "curve", "", "")
}
func runLevels(cmd *Command, args []string) {
i := utils.ReadStdin()
if !levelsRed && !levelsGreen && !levelsBlue {
levelsRed = true; levelsGreen = true; levelsBlue = true
}
if levelsRed {
i = runLevelsOnChannel(cmd, args, i, levels.RedChannel)
}
if levelsGreen {
i = runLevelsOnChannel(cmd, args, i, levels.GreenChannel)
}
if levelsBlue {
i = runLevelsOnChannel(cmd, args, i, levels.BlueChannel)
}
utils.WriteStdout(i)
}
func runLevelsOnChannel(cmd *Command, args []string, img image.Image,
ch levels.Channel) image.Image {
if levelsAuto {
img = levels.Auto(img, ch)
} else if levelsAutoBlack {
img = levels.AutoBlack(img, ch)
} else if levelsAutoWhite {
img = levels.AutoWhite(img, ch)
} else if utils.FlagVisited("black", cmd.Flag) {
img = levels.SetBlack(img, ch, levelsBlack)
} else if utils.FlagVisited("white", cmd.Flag) {
img = levels.SetWhite(img, ch, levelsWhite)
} else if utils.FlagVisited("curve", cmd.Flag) {
img = levels.SetCurve(img, ch, levels.ParseCurveString(levelsCurve))
}
return img
}