forked from hawx/img
/
pxl.go
63 lines (50 loc) · 1.56 KB
/
pxl.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
package main
import (
"github.com/hawx/img/pixelate"
"github.com/hawx/img/utils"
)
var cmdPxl = &Command{
UsageLine: "pxl [options]",
Short: "pxls image",
Long: `
Pxl takes an image from STDIN, pxls it by averaging the colour in large
rectangles, and prints the result to STDOUT
--alias # Do not use antialiasing
--left # Use only left triangles
--right # Use only right triangles
--cols <num> # Split into <num> columns
--rows <num> # Split into <num> rows
--size <HxW> # Size of pixel to pxl with (default: 20x20)
`,
}
var pxlAlias, pxlLeft, pxlRight, pxlBoth bool
var pxlSize utils.Dimension = utils.Dimension{20, 20}
var pxlRows, pxlCols int
func init() {
cmdPxl.Run = runPxl
cmdPxl.Flag.BoolVar(&pxlAlias, "alias", false, "")
cmdPxl.Flag.BoolVar(&pxlLeft, "left", false, "")
cmdPxl.Flag.BoolVar(&pxlRight, "right", false, "")
cmdPxl.Flag.Var(&pxlSize, "size", "")
cmdPxl.Flag.IntVar(&pxlRows, "rows", -1, "")
cmdPxl.Flag.IntVar(&pxlCols, "cols", -1, "")
}
func runPxl(cmd *Command, args []string) {
i := utils.ReadStdin()
triangle := pixelate.BOTH
if pxlLeft { triangle = pixelate.LEFT }
if pxlRight { triangle = pixelate.RIGHT }
if pxlRows > 0 && pxlCols > 0 {
pxlSize = utils.SizeForRowsAndCols(i, pxlRows, pxlCols)
} else if pxlRows > 0 {
pxlSize = utils.SizeForRows(i, pxlRows)
} else if pxlCols > 0 {
pxlSize = utils.SizeForCols(i, pxlCols)
}
if pxlAlias {
i = pixelate.AliasedPxl(i, pxlSize, triangle)
} else {
i = pixelate.Pxl(i, pxlSize, triangle)
}
utils.WriteStdout(i)
}