From bee8e49ebaf6ba5c3a89817fae283c44fd37b4c6 Mon Sep 17 00:00:00 2001 From: MrMelon Date: Mon, 7 Mar 2022 16:32:51 +0000 Subject: [PATCH] First commit --- .gitattributes | 1 + .gitignore | 9 +++++++++ .idea/.gitignore | 8 ++++++++ .idea/discord.xml | 7 +++++++ .idea/modules.xml | 8 ++++++++ .idea/svg2png.iml | 9 +++++++++ README.md | 4 ++++ go.mod | 14 ++++++++++++++ go.sum | 14 ++++++++++++++ svg2png.go | 29 +++++++++++++++++++++++++++++ 10 files changed, 103 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/discord.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/svg2png.iml create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 svg2png.go diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2ec00d --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# Environment variables file +.env + +# Distributable directory +dist/ + +# Test data and logs folders +.data/ +.idea/dataSources.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..d8e9561 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6bf3c39 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/svg2png.iml b/.idea/svg2png.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/svg2png.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..1d3e554 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +svg2png +=========== + +Convert SVG image to PNG image diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9751ace --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module tea.melonie54.xyz/sean/svg2png + +go 1.17 + +require ( + github.com/srwiley/oksvg v0.0.0-20220128195007-1f435e4c2b44 + github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 +) + +require ( + golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect + golang.org/x/net v0.0.0-20211118161319-6a13c67c3ce4 // indirect + golang.org/x/text v0.3.6 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..414a66a --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/srwiley/oksvg v0.0.0-20220128195007-1f435e4c2b44 h1:XPYXKIuH/n5zpUoEWk2jWV/SjEMNYmqDYmTgbjmhtaI= +github.com/srwiley/oksvg v0.0.0-20220128195007-1f435e4c2b44/go.mod h1:cNQ3dwVJtS5Hmnjxy6AgTPd0Inb3pW05ftPSX7NZO7Q= +github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 h1:oDMiXaTMyBEuZMU53atpxqYsSB3U1CHkeAu2zr6wTeY= +github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780/go.mod h1:mvWM0+15UqyrFKqdRjY6LuAVJR0HOVhJlEgZ5JWtSWU= +golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ= +golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= +golang.org/x/net v0.0.0-20211118161319-6a13c67c3ce4 h1:DZshvxDdVoeKIbudAdFEKi+f70l51luSy/7b76ibTY0= +golang.org/x/net v0.0.0-20211118161319-6a13c67c3ce4/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/svg2png.go b/svg2png.go new file mode 100644 index 0000000..60ccf40 --- /dev/null +++ b/svg2png.go @@ -0,0 +1,29 @@ +package svg2png + +import ( + "bytes" + "github.com/srwiley/oksvg" + "github.com/srwiley/rasterx" + "image" + "image/png" + "io" +) + +func RasterSvgToPng(r io.Reader) ([]byte, error) { + svg, err := oksvg.ReadIconStream(r, oksvg.StrictErrorMode) + if err != nil { + return nil, err + } + w, h := int(svg.ViewBox.W), int(svg.ViewBox.H) + img := image.NewRGBA(image.Rect(0, 0, w, h)) + scannerGV := rasterx.NewScannerGV(0, 0, img, img.Bounds()) + raster := rasterx.NewDasher(w, h, scannerGV) + svg.Draw(raster, 1) + + b := new(bytes.Buffer) + err = png.Encode(b, img) + if err != nil { + return nil, err + } + return b.Bytes(), nil +}