Skip to content

Commit 919f2f6

Browse files
committed
initial commit
0 parents  commit 919f2f6

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
bin/
3+
go.sum

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
build:
2+
go mod tidy
3+
go build -ldflags "-s -w" -o bin/ .
4+
5+
icon:
6+
go install github.com/akavel/rsrc@latest
7+
rsrc -ico go.ico
8+
9+
run:
10+
go mod tidy
11+
go run .
12+
13+
windows: icon build

go.ico

13.1 KB
Binary file not shown.

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module github.com/typomedia/iconize
2+
3+
go 1.19
4+
5+
require git.shangtai.net/staffan/go-ico v1.0.0-beta.2
6+
7+
require golang.org/x/image v0.0.0-20211028202545-6944b10bf410

main.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"image"
7+
"image/color"
8+
"log"
9+
"os"
10+
"path/filepath"
11+
"time"
12+
13+
"git.shangtai.net/staffan/go-ico"
14+
"golang.org/x/image/draw"
15+
)
16+
17+
func main() {
18+
19+
start := time.Now()
20+
21+
if len(os.Args) < 2 {
22+
os.Exit(1)
23+
}
24+
path := os.Args[1]
25+
26+
file, err := os.ReadFile(path)
27+
if err != nil {
28+
log.Fatal(err)
29+
}
30+
31+
img, _, err := image.Decode(bytes.NewReader(file))
32+
if err != nil {
33+
log.Fatal(err)
34+
}
35+
36+
width, height := size(img)
37+
size := max(width, height)
38+
39+
alpha := image.NewRGBA(image.Rect(0, 0, size, size))
40+
41+
rgba := color.RGBA{0, 0, 0, 0}
42+
draw.Draw(alpha, alpha.Bounds(), &image.Uniform{C: rgba}, image.Point{}, draw.Src)
43+
44+
draw.Draw(alpha, img.Bounds().Add(
45+
image.Point{
46+
X: (size - img.Bounds().Dx()) / 2,
47+
Y: (size - img.Bounds().Dy()) / 2,
48+
}), img, image.Point{}, draw.Over)
49+
50+
basename := name(path)
51+
icoFile, err := os.Create(basename + ".ico")
52+
if err != nil {
53+
log.Fatal(err)
54+
}
55+
defer icoFile.Close()
56+
57+
icon := ico.NewIcon()
58+
// https://learn.microsoft.com/en-us/windows/win32/uxguide/vis-icons
59+
sizes := []int{128, 256}
60+
for _, size := range sizes {
61+
resizedImg := scale(alpha, size, size)
62+
icon.AddPng(resizedImg)
63+
}
64+
65+
t := time.Now()
66+
elapsed := t.Sub(start)
67+
fmt.Println("Elapsed:", elapsed)
68+
69+
enc, err := icon.Encode()
70+
if err != nil {
71+
log.Fatal(err)
72+
}
73+
74+
icoFile.Write(enc)
75+
os.Exit(0)
76+
77+
}
78+
79+
func max(width, height int) int {
80+
size := width
81+
if height > width {
82+
size = height
83+
}
84+
return size
85+
}
86+
87+
func size(img image.Image) (int, int) {
88+
width := img.Bounds().Max.X
89+
height := img.Bounds().Max.Y
90+
return width, height
91+
}
92+
93+
func scale(img image.Image, width, height int) image.Image {
94+
resizedImg := image.NewRGBA(image.Rect(0, 0, width, height))
95+
draw.CatmullRom.Scale(resizedImg, resizedImg.Bounds(), img, img.Bounds(), draw.Over, nil)
96+
97+
return resizedImg
98+
}
99+
100+
func name(filePath string) string {
101+
fileName := filepath.Base(filePath)
102+
extension := filepath.Ext(fileName)
103+
return fileName[0 : len(fileName)-len(extension)]
104+
}

0 commit comments

Comments
 (0)