From 0efcaebc39bca81dc285a24a56ab325bbfd22190 Mon Sep 17 00:00:00 2001 From: MrMelon Date: Mon, 7 Mar 2022 16:36:41 +0000 Subject: [PATCH] First commit --- .gitattributes | 1 + .gitignore | 9 ++++++++ .idea/.gitignore | 8 +++++++ .idea/discord.xml | 7 +++++++ .idea/modules.xml | 8 +++++++ .idea/png2ico.iml | 9 ++++++++ .idea/vcs.xml | 6 ++++++ README.md | 4 ++++ go.mod | 3 +++ png2ico.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 108 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/png2ico.iml create mode 100644 .idea/vcs.xml create mode 100644 README.md create mode 100644 go.mod create mode 100644 png2ico.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..0a65ca2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/png2ico.iml b/.idea/png2ico.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/png2ico.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d7e436e --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +png2ico +=========== + +Convert PNG image to ICO image diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..36bbea8 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module tea.melonie54.xyz/sean/png2ico + +go 1.17 diff --git a/png2ico.go b/png2ico.go new file mode 100644 index 0000000..89c84fd --- /dev/null +++ b/png2ico.go @@ -0,0 +1,53 @@ +package png2ico + +import ( + "bytes" + "encoding/binary" +) + +type iconDir struct { + reserved uint16 + imageType uint16 + numImages uint16 +} + +type iconDirEntry struct { + imageWidth uint8 + imageHeight uint8 + numColors uint8 + reserved uint8 + colorPlanes uint16 + bitsPerPixel uint16 + sizeInBytes uint32 + offset uint32 +} + +func newIconDir() iconDir { + return iconDir{imageType: 1, numImages: 1} +} + +func newIconDirEntry() iconDirEntry { + return iconDirEntry{colorPlanes: 1, bitsPerPixel: 32, offset: 22} +} + +func ConvertPngToIco(im []byte, width, height int) ([]byte, error) { + id := newIconDir() + ide := newIconDirEntry() + + ide.sizeInBytes = uint32(len(im)) + ide.imageWidth = uint8(width) + ide.imageHeight = uint8(height) + bb := new(bytes.Buffer) + + err := binary.Write(bb, binary.LittleEndian, id) + if err != nil { + return nil, err + } + err = binary.Write(bb, binary.LittleEndian, ide) + if err != nil { + return nil, err + } + + bb.Write(im) + return bb.Bytes(), nil +}