|
| 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