mirror of
https://github.com/jahruz67/wisp-open.git
synced 2026-08-08 18:14:08 +00:00
9
This commit is contained in:
@@ -5,6 +5,7 @@ package tray
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image/png"
|
"image/png"
|
||||||
"os"
|
"os"
|
||||||
@@ -12,7 +13,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"golang.org/x/image/ico"
|
|
||||||
"wis-free-v3/internal/config"
|
"wis-free-v3/internal/config"
|
||||||
"wis-free-v3/internal/logger"
|
"wis-free-v3/internal/logger"
|
||||||
"wis-free-v3/internal/platform"
|
"wis-free-v3/internal/platform"
|
||||||
@@ -180,16 +180,39 @@ func getDefaultIcon() []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func icoToPNG(data []byte) ([]byte, error) {
|
func icoToPNG(data []byte) ([]byte, error) {
|
||||||
img, err := ico.Decode(bytes.NewReader(data))
|
// Try to find a PNG image embedded inside the ICO file and return it.
|
||||||
if err != nil {
|
// ICO files contain entries that may be BMP/DIB or PNG encoded images.
|
||||||
return nil, err
|
if len(data) < 6 {
|
||||||
|
return nil, fmt.Errorf("invalid ico data")
|
||||||
}
|
}
|
||||||
|
count := int(binary.LittleEndian.Uint16(data[4:6]))
|
||||||
var buf bytes.Buffer
|
var bestOffset int
|
||||||
if err := png.Encode(&buf, img); err != nil {
|
var bestSize int
|
||||||
return nil, err
|
for i := 0; i < count; i++ {
|
||||||
|
entry := 6 + i*16
|
||||||
|
if entry+16 > len(data) {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
return buf.Bytes(), nil
|
bytesInRes := int(binary.LittleEndian.Uint32(data[entry+8 : entry+12]))
|
||||||
|
imageOffset := int(binary.LittleEndian.Uint32(data[entry+12 : entry+16]))
|
||||||
|
if bytesInRes == 0 || imageOffset+bytesInRes > len(data) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// PNG files start with the 8-byte signature 89 50 4E 47 0D 0A 1A 0A
|
||||||
|
if bytesInRes >= 8 && imageOffset+8 <= len(data) &&
|
||||||
|
bytes.Equal(data[imageOffset:imageOffset+8], []byte{0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n'}) {
|
||||||
|
if bytesInRes > bestSize {
|
||||||
|
bestSize = bytesInRes
|
||||||
|
bestOffset = imageOffset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if bestSize > 0 {
|
||||||
|
out := make([]byte, bestSize)
|
||||||
|
copy(out, data[bestOffset:bestOffset+bestSize])
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("no PNG image found in ico")
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateStatus updates the status text displayed in the tray menu.
|
// UpdateStatus updates the status text displayed in the tray menu.
|
||||||
|
|||||||
Reference in New Issue
Block a user