2020-05-09 20:50:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-05-10 15:40:56 +00:00
|
|
|
|
|
|
|
"opslag.de/schobers/tins2020/img"
|
2020-05-09 20:50:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func run() error {
|
|
|
|
flag.Parse()
|
|
|
|
args := flag.Args()
|
|
|
|
if len(args) < 1 {
|
|
|
|
return errors.New("no command specified")
|
|
|
|
}
|
|
|
|
switch args[0] {
|
|
|
|
case "setalpha":
|
|
|
|
flags := flag.NewFlagSet("setalpha", flag.ContinueOnError)
|
|
|
|
var alpha int
|
|
|
|
flags.IntVar(&alpha, "alpha", 127, "sets the target alpha")
|
|
|
|
flags.Parse(args[1:])
|
|
|
|
for _, path := range flags.Args() {
|
|
|
|
err := setAlpha(path, alpha)
|
|
|
|
if err != nil {
|
2020-05-09 23:40:54 +00:00
|
|
|
return fmt.Errorf("couldn't set alpha of '%s'; error: %v", path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "gray":
|
|
|
|
flags := flag.NewFlagSet("gray", flag.ContinueOnError)
|
|
|
|
flags.Parse(args[1:])
|
|
|
|
for _, path := range flags.Args() {
|
2020-05-10 07:11:08 +00:00
|
|
|
err := convertToGray(path)
|
2020-05-09 23:40:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't convert to grayscale of '%s'; error: %v", path, err)
|
2020-05-09 20:50:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-10 07:11:08 +00:00
|
|
|
case "crop":
|
|
|
|
flags := flag.NewFlagSet("crop", flag.ContinueOnError)
|
|
|
|
var rect string
|
|
|
|
var resize string
|
|
|
|
var dir string
|
|
|
|
flags.StringVar(&rect, "crop", "", "sets the crop rectangle \"x1,y1,y1,y2\"")
|
|
|
|
flags.StringVar(&resize, "resize", "", "sets the target size, \"width,height\", if not specified the crop rectangle size is used")
|
|
|
|
flags.StringVar(&dir, "dir", "", "crops all images in the specified directory (recursively)")
|
|
|
|
flags.Parse(args[1:])
|
2020-05-09 20:50:29 +00:00
|
|
|
|
2020-05-10 07:11:08 +00:00
|
|
|
cropRect, err := parseRect(rect)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("crop rectangle is invalid: error: %v", err)
|
|
|
|
}
|
|
|
|
var size *point
|
|
|
|
if len(resize) != 0 {
|
|
|
|
p, err := parsePoint(resize)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("resize parameter is invalid: error: %v", err)
|
2020-05-09 20:50:29 +00:00
|
|
|
}
|
2020-05-10 07:11:08 +00:00
|
|
|
size = &p
|
2020-05-09 20:50:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 07:11:08 +00:00
|
|
|
var files []string
|
|
|
|
if len(dir) == 0 {
|
|
|
|
files = flags.Args()
|
|
|
|
} else {
|
|
|
|
files, err = scanDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't find files to crop; error: %v", err)
|
2020-05-09 23:40:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 07:11:08 +00:00
|
|
|
for _, path := range files {
|
|
|
|
err := crop(path, cropRect, size)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't crop '%s'; error: %v", path, err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 15:40:56 +00:00
|
|
|
case "color":
|
|
|
|
flags := flag.NewFlagSet("color", flag.ContinueOnError)
|
|
|
|
var col string
|
|
|
|
flags.StringVar(&col, "color", "#ff0000", "target color")
|
|
|
|
flags.Parse(args[1:])
|
|
|
|
for _, path := range flags.Args() {
|
|
|
|
err := colorImage(path, img.MustHexColor(col))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't convert to grayscale of '%s'; error: %v", path, err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-09 20:50:29 +00:00
|
|
|
}
|
2020-05-10 07:11:08 +00:00
|
|
|
return nil
|
2020-05-09 20:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := run(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|