-
Notifications
You must be signed in to change notification settings - Fork 4
/
ico.go
81 lines (73 loc) · 1.58 KB
/
ico.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 (
"errors"
"fmt"
"image"
"image/png"
"os"
"github.com/balibuild/bali/v2/makeico"
)
// IconMaker icon maker
type IconMaker struct {
images []image.Image
fds []*os.File
}
// Close close
func (m *IconMaker) Close() error {
var err error
for _, f := range m.fds {
if e := f.Close(); e != nil {
err = e
}
}
return err
}
// AddImage add image path
func (m *IconMaker) AddImage(name string) error {
fd, err := os.Open(name)
if err != nil {
fmt.Fprintf(os.Stderr, "unable open file: %v\n", err)
return err
}
m.fds = append(m.fds, fd)
img, err := png.Decode(fd)
if err != nil {
fmt.Fprintf(os.Stderr, "unable decode png: %v\n", err)
return err
}
m.images = append(m.images, img)
return nil
}
// Make create icon
func (m *IconMaker) Make() error {
if len(m.fds) == 0 {
return errors.New("no image add")
}
name := m.fds[0].Name() + ".ico"
fd, err := os.Create(name)
if err != nil {
return err
}
defer fd.Close()
if err := makeico.EncodePNG(fd, m.images...); err != nil {
return err
}
return nil
}
// .\ico.exe .\circular-chart.png .\circular-chart-256.png .\circular-chart-128.png .\circular-chart-64.png .\circular-chart-32.png .\circular-chart-24.png .\circular-chart-16.png
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "usage: %s png\n", os.Args[0])
os.Exit(1)
}
var m IconMaker
defer m.Close()
for i := 1; i < len(os.Args); i++ {
m.AddImage(os.Args[i])
}
if err := m.Make(); err != nil {
fmt.Fprintf(os.Stderr, "build icon error: %v\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "convert png to ico success\n")
}