-
Notifications
You must be signed in to change notification settings - Fork 4
/
resize-convolution.go
81 lines (67 loc) · 1.43 KB
/
resize-convolution.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
package main
import (
"flag"
"fmt"
"image"
"log"
"os"
"strconv"
"github.com/qeedquan/go-media/image/imageutil"
"github.com/qeedquan/go-media/image/resampler"
"github.com/qeedquan/go-media/math/ga"
)
var (
filter = flag.String("f", "blackman", "filter to use")
)
func main() {
log.SetFlags(0)
log.SetPrefix("resize: ")
flag.Usage = usage
flag.Parse()
if flag.NArg() != 4 {
usage()
}
f := resampler.GetFilter(*filter)
if f.Name == "" {
log.Fatalf("unknown filter %q", *filter)
}
m, err := imageutil.LoadRGBAFile(flag.Arg(0))
ck(err)
w, err := strconv.Atoi(flag.Arg(2))
ck(err)
h, err := strconv.Atoi(flag.Arg(3))
ck(err)
if w <= 0 || h <= 0 {
log.Fatalf("invalid dimension %dx%d!", w, h)
}
o := &resampler.Options{
BoundaryOp: resampler.BOUNDARY_CLAMP,
Filter: f,
SampleRange: ga.Vec2d{0, 1},
FilterScale: ga.Vec2d{1, 1},
SourceOff: ga.Vec2d{0, 0},
}
p := image.NewRGBA(image.Rect(0, 0, w, h))
resampler.ResizeImage(m, p, o)
err = imageutil.WriteRGBAFile(flag.Arg(1), p)
ck(err)
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: resize input output width height")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "available filters:\n")
for i, f := range resampler.Filters {
fmt.Fprintf(os.Stderr, "%v ", f.Name)
if i&3 == 3 {
fmt.Fprintln(os.Stderr)
}
}
fmt.Fprintln(os.Stderr)
os.Exit(2)
}
func ck(err error) {
if err != nil {
log.Fatal(err)
}
}