Created
May 1, 2018 05:01
-
-
Save ajstarks/9ce776afc3feed9ed957a24c5675555d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"image" | |
"image/png" | |
"os" | |
"github.com/srwiley/oksvg" | |
"github.com/srwiley/scanft" | |
"github.com/srwiley/rasterx" | |
) | |
func DrawIcon(iconPath string) image.Image { | |
icon, errSvg := oksvg.ReadIcon(iconPath, oksvg.IgnoreErrorMode) | |
if errSvg != nil { | |
return nil | |
} | |
w, h := int(icon.ViewBox.W), int(icon.ViewBox.H) | |
img := image.NewRGBA(image.Rect(0, 0, w, h)) | |
painter := scanFT.NewRGBAPainter(img) | |
scannerFT := scanFT.NewScannerFT(w, h, painter) | |
raster := rasterx.NewDasher(w, h, scannerFT) | |
icon.Draw(raster, 1.0) | |
return img | |
} | |
func SaveIcon(iconPath string) error { | |
var err error | |
img := DrawIcon(iconPath) | |
if img != nil { | |
err = SaveToPngFile(fmt.Sprintf("%s.png", iconPath), img) | |
if err != nil { | |
return err | |
} | |
} | |
return err | |
} | |
func SaveToPngFile(filePath string, m image.Image) error { | |
// Create the file | |
f, err := os.Create(filePath) | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
// Create Writer from file | |
b := bufio.NewWriter(f) | |
// Write the image into the buffer | |
err = png.Encode(b, m) | |
if err != nil { | |
return err | |
} | |
err = b.Flush() | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
func main() { | |
for _, f := range os.Args[1:] { | |
err := SaveIcon(f) | |
if err != nil { | |
fmt.Printf("%v\n", err) | |
continue | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment