forked from liztio/cgo-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (44 loc) · 1.18 KB
/
main.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
package main
// #cgo pkg-config: MagickWand
// #include <wand/MagickWand.h>
import "C"
import (
"flag"
"fmt"
"math"
"os"
)
var (
inputFile = flag.String("in", ":logo", "The file to read in")
outputFile = flag.String("out", "logo.jpg", "The file to write out")
scale = flag.Float64("scale", 0.5, "how much to scale the image by")
)
func main() {
flag.Parse()
if *scale <= 0 {
fmt.Fprintf(os.Stderr, "Scale must be greater than zero\n")
os.Exit(1)
}
C.MagickWandGenesis()
// Create a wand
mw := C.NewMagickWand()
defer func() {
// Tidy up
if mw != nil {
C.DestroyMagickWand(mw)
}
C.MagickWandTerminus()
}()
// Read the input image
C.MagickReadImage(mw, C.CString(*inputFile))
// Resize it
height := float64(C.MagickGetImageHeight(mw))
width := float64(C.MagickGetImageWidth(mw))
newHeight := math.Round(math.Max(height*(*scale), 1))
newWidth := math.Round(math.Max(width*(*scale), 1))
// Resize the image using the Lanczos filter
// The blur factor is a "double", where > 1 is blurry, < 1 is sharp
C.MagickResizeImage(mw, C.ulong(newWidth), C.ulong(newHeight), C.LanczosFilter, 1)
// Write the new image
C.MagickWriteImage(mw, C.CString(*outputFile))
}