@@ -3,42 +3,49 @@ package main
33import (
44 "bytes"
55 "fmt"
6+ "github.com/spf13/pflag"
7+ "github.com/typomedia/ico"
8+ "golang.org/x/image/draw"
69 "image"
710 "image/color"
11+ "io"
812 "log"
913 "os"
1014 "path/filepath"
11- "time"
12-
13- "github.com/spf13/pflag"
14- "github.com/typomedia/ico"
15- "golang.org/x/image/draw"
1615)
1716
1817func main () {
1918 var (
20- input string
21- output string
19+ input string
20+ data []byte
21+ err error
2222 )
2323
24- pflag .StringVarP (& output , "out" , "o" , "" , "output ico file" )
24+ outfile := pflag .StringP ("out" , "o" , "" , "outfile ico file" )
25+ version := pflag .BoolP ("version" , "V" , false , "show version" )
2526 pflag .Parse ()
2627
27- if len (pflag .Args ()) < 1 {
28- os .Exit (1 )
28+ if * version {
29+ fmt .
Println (
"iconize 0.2.0 <[email protected] >" )
30+ os .Exit (0 )
2931 }
3032
3133 input = pflag .Arg (0 )
32- fmt .Println ("Input:" , input )
33-
34- start := time .Now ()
35-
36- file , err := os .ReadFile (input )
34+ stat , _ := os .Stdin .Stat ()
35+
36+ switch true {
37+ case input != "" :
38+ data , err = os .ReadFile (input )
39+ case (stat .Mode () & os .ModeCharDevice ) == 0 :
40+ data , err = io .ReadAll (os .Stdin )
41+ default :
42+ log .Fatal ("no input data given" )
43+ }
3744 if err != nil {
3845 log .Fatal (err )
3946 }
4047
41- img , _ , err := image .Decode (bytes .NewReader (file ))
48+ img , _ , err := image .Decode (bytes .NewReader (data ))
4249 if err != nil {
4350 log .Fatal (err )
4451 }
@@ -48,7 +55,7 @@ func main() {
4855
4956 alpha := image .NewRGBA (image .Rect (0 , 0 , size , size ))
5057
51- rgba := color.RGBA {0 , 0 , 0 , 0 }
58+ rgba := color.RGBA {}
5259 draw .Draw (alpha , alpha .Bounds (), & image.Uniform {C : rgba }, image.Point {}, draw .Src )
5360
5461 draw .Draw (alpha , img .Bounds ().Add (
@@ -57,18 +64,6 @@ func main() {
5764 Y : (size - img .Bounds ().Dy ()) / 2 ,
5865 }), img , image.Point {}, draw .Over )
5966
60- basename := name (input ) + ".ico"
61- if output != "" {
62- basename = output
63- }
64- fmt .Println ("Output:" , basename )
65-
66- icoFile , err := os .Create (basename )
67- if err != nil {
68- log .Fatal (err )
69- }
70- defer icoFile .Close ()
71-
7267 icon := ico .NewIcon ()
7368 // https://learn.microsoft.com/en-us/windows/win32/uxguide/vis-icons
7469 sizes := []int {256 , 128 , 64 , 48 , 32 , 24 , 16 }
@@ -77,15 +72,24 @@ func main() {
7772 icon .AddPng (resizedImg )
7873 }
7974
80- t := time .Now ()
81- elapsed := t .Sub (start )
82- fmt .Println ("Elapsed:" , elapsed )
83-
8475 enc , err := icon .Encode ()
8576 if err != nil {
8677 log .Fatal (err )
8778 }
8879
80+ // write to stdout
81+ if * outfile == "" {
82+ fmt .Println (string (enc ))
83+ os .Exit (0 )
84+ }
85+
86+ // write to file
87+ icoFile , err := os .Create (* outfile )
88+ if err != nil {
89+ log .Fatal (err )
90+ }
91+ defer icoFile .Close ()
92+
8993 icoFile .Write (enc )
9094 os .Exit (0 )
9195
0 commit comments