-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
109 lines (93 loc) · 2.58 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
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
101
102
103
104
105
106
107
108
109
package main
//go:generate go get github.com/rakyll/statik
//go:generate statik
import (
"bytes"
"flag"
"io/ioutil"
"image"
"image/draw"
"image/png"
"log"
"net/http"
"os"
"github.com/disintegration/imaging"
"github.com/mattn/go-sixel"
_ "github.com/mattn/longcat/statik"
"github.com/rakyll/statik/fs"
)
func loadImage(fs http.FileSystem, n string) (image.Image, error) {
f, err := fs.Open(n)
if err != nil {
log.Fatal(err)
}
defer f.Close()
return png.Decode(f)
}
func saveImage(filename string, img image.Image) error {
var buf bytes.Buffer
err := png.Encode(&buf, img)
if err != nil {
return err
}
return ioutil.WriteFile(filename, buf.Bytes(), 0644)
}
func main() {
var nlong int
var ncolumns int
var rinterval float64
var flipH bool
var flipV bool
var filename string
flag.IntVar(&nlong, "n", 1, "how long cat")
flag.IntVar(&ncolumns, "l", 1, "number of columns")
flag.Float64Var(&rinterval, "i", 1.0, "rate of intervals")
flag.BoolVar(&flipH, "r", false, "flip holizontal")
flag.BoolVar(&flipV, "R", false, "flip vertical")
flag.StringVar(&filename, "o", "", "output image file")
flag.Parse()
fs, err := fs.New()
if err != nil {
log.Fatal(err)
}
img1, _ := loadImage(fs, "/data01.png")
img2, _ := loadImage(fs, "/data02.png")
img3, _ := loadImage(fs, "/data03.png")
if flipH {
img1 = imaging.FlipH(img1)
img2 = imaging.FlipH(img2)
img3 = imaging.FlipH(img3)
}
width := int(float64(img1.Bounds().Dx()*(ncolumns-1))*rinterval) + img1.Bounds().Dx()
rect := image.Rect(0, 0, width, img1.Bounds().Dy()+img2.Bounds().Dy()*nlong+img3.Bounds().Dy())
canvas := image.NewRGBA(rect)
for col := 0; col < ncolumns; col++ {
x := int(float64(img1.Bounds().Dx()*col) * rinterval)
rect = image.Rect(x, 0, x+img1.Bounds().Dx(), img1.Bounds().Dy())
draw.Draw(canvas, rect, img1, image.ZP, draw.Over)
for i := 0; i < nlong; i++ {
rect = image.Rect(x, img1.Bounds().Dy()+img2.Bounds().Dy()*i, x+img1.Bounds().Dx(), img1.Bounds().Dy()+img2.Bounds().Dy()*(i+1))
draw.Draw(canvas, rect, img2, image.ZP, draw.Over)
}
rect = image.Rect(x, img1.Bounds().Dy()+img2.Bounds().Dy()*nlong, x+img1.Bounds().Dx(), img1.Bounds().Dy()+img2.Bounds().Dy()*nlong+img3.Bounds().Dy())
draw.Draw(canvas, rect, img3, image.ZP, draw.Over)
}
var output image.Image = canvas
if flipV {
output = imaging.FlipV(output)
}
if filename != "" {
err = saveImage(filename, output)
if err != nil {
log.Fatal(err)
}
return
}
var buf bytes.Buffer
err = sixel.NewEncoder(&buf).Encode(output)
if err != nil {
log.Fatal(err)
}
os.Stdout.Write(buf.Bytes())
os.Stdout.Sync()
}