-
Notifications
You must be signed in to change notification settings - Fork 2
/
image.go
206 lines (165 loc) · 5.37 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"fmt"
"math"
"os"
"path/filepath"
"strconv"
"strings"
lnk "github.com/parsiya/golnk"
"github.com/wailsapp/wails/v2/pkg/runtime"
"gopkg.in/ini.v1"
)
var allowedImageExtensionsPng = []string{".ico", ".png", ".jpg", ".jpeg", ".bmp", ".webp", ".svg", ".exe", ".lnk", ".url"}
var allowedImageExtensionsIco = []string{".png", ".jpg", ".jpeg"}
func GetMaskPath(radius int) (string, error) {
if radius <= 0 {
radius = 1
} else if radius > 100 {
radius = 100
}
// Create a rounded rectangle mask
maskPath := filepath.Join(maskFolder, fmt.Sprintf("mask_r%d.png", radius))
// Check if the mask already exists
if _, err := os.Stat(maskPath); err == nil {
return maskPath, nil
}
// Round the corner radius to the closest integer
roundedRadius := int(math.Round(float64(radius) * 2.56))
maskArgs := []string{
imageMagickPath,
"-size", fmt.Sprintf("%dx%d", 256, 256), // Set the size of the canvas
"xc:none", // Create a blank canvas with transparency
"-fill", "white", // Set the fill color to white for the mask
"-draw", fmt.Sprintf("roundrectangle 0,0 %d,%d %d,%d", 255, 255, roundedRadius, roundedRadius), // Draw a rounded rectangle with the specified corner radius
"-alpha", "off", // Turn off alpha
maskPath, // Save the mask to the destination file
}
// Execute ImageMagick command to create mask
_, err := sendCommand(maskArgs...)
if err != nil {
return "", fmt.Errorf("error creating mask: %ws", err)
}
return maskPath, nil
}
// ConvertToIco converts an image to an ICO file with specified corner radius
func ConvertToIco(path string, destination string, settings IconPackSettings) error {
extension := filepath.Ext(path)
// Check if the input file has a valid image extension
if extension != ".png" {
return fmt.Errorf("invalid image extension: %s", extension)
}
maskPath, err := GetMaskPath(settings.CornerRadius)
if err != nil {
return err
}
args := []string{
imageMagickPath,
path,
maskPath,
"-alpha", "off",
"-compose", "CopyOpacity",
"-composite",
"-define", "icon:auto-resize=16,24,32,48,64,72,96,128,256",
"-channel", "A", // target the alpha channel
"-evaluate", "Multiply", fmt.Sprintf("%.2f", (float64(settings.Opacity) / 100.0)), // apply the opacity adjustment
"-channel", "RGBA", // reset channel targeting
destination,
}
// Execute ImageMagick command silently
_, err = sendCommand(args...)
if err != nil {
return fmt.Errorf("error converting image: %w", err)
}
return nil
}
func ConvertToPng(path string, destination string) error {
extension := filepath.Ext(path)
if !is_dir(path) && !contains(allowedImageExtensionsPng, extension) {
return fmt.Errorf("invalid image extension: %s", extension)
}
iconDestination := ""
iconLocation := ""
if extension == ".lnk" {
link, err := lnk.File(path)
if err != nil {
return fmt.Errorf("failed to open .lnk file: %w", err)
}
iconLocation := link.StringData.IconLocation
iconDestination = link.LinkInfo.LocalBasePath
if strings.ToLower(filepath.Ext(iconLocation)) == ".ico" {
err = ConvertToPng(iconLocation, destination)
if err == nil {
return nil
}
}
} else if extension == ".url" {
iniContent, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to open .url file: %w", err)
}
iniFile, err := ini.Load(iniContent)
if err != nil {
return fmt.Errorf("failed to parse .url file: %w", err)
}
section := iniFile.Section("InternetShortcut")
iconLocation = section.Key("IconFile").String()
return ConvertToPng(iconLocation, destination)
}
// Build magick command arguments
args := []string{extractIconPath, path, destination}
useImagick := extension != ".ico" && extension != ".exe" && extension != ".lnk" && !is_dir(path)
if useImagick {
args = []string{imageMagickPath, path, "-alpha", "on", "-background", "none", "-resize", "256x256\\!", destination}
}
runtime.LogDebugf(appContext, "ConvertToPng: %s", strings.Join(args, " "))
// Execute magick command silently
_, err := sendCommand(args...)
if err != nil {
if extension == ".lnk" {
if strings.ToLower(filepath.Ext(iconLocation)) != ".ico" {
err = ConvertToPng(iconLocation, destination)
if err == nil {
return nil
}
}
return ConvertToPng(iconDestination, destination)
}
return fmt.Errorf("error converting image: %w", err)
} else {
if !useImagick {
width, err := GetImageWidth(destination)
if err != nil {
return err
}
if width != 256 {
resizeArgs := []string{imageMagickPath, destination, "-resize", "256x256", destination}
runtime.LogDebugf(appContext, "ConvertToPng: %s", strings.Join(resizeArgs, " "))
// Execute magick command silently
_, err := sendCommand(resizeArgs...)
if err != nil {
return fmt.Errorf("error resizing image: %w", err)
}
}
}
}
return nil
}
func GetImageWidth(path string) (int, error) {
extension := filepath.Ext(path)
if !contains(allowedImageExtensionsIco, extension) {
return -1, fmt.Errorf("invalid image extension: %s", extension)
}
// Build magick command arguments
args := []string{imageMagickPath, "identify", "-ping", "-format", "%w", path}
// Execute magick command silently
output, err := sendCommand(args...)
if err != nil {
return -1, fmt.Errorf("error getting image width: %w\n%s", err, output)
}
width, err := strconv.Atoi(strings.TrimSpace(output))
if err != nil {
return -1, err
}
return width, nil
}