-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathformat.go
152 lines (142 loc) · 3.46 KB
/
format.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
package main
import (
"errors"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"strings"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
"golang.org/x/image/webp"
)
type SupportedFormat struct {
Title string
Extensions []string
Function func(w io.ReadWriter, image image.Image, encode bool) (image.Image, error)
Encodable bool
}
var formats []SupportedFormat = []SupportedFormat{
{Title: "Bitmap Files", Extensions: []string{".bmp"},
Function: func(w io.ReadWriter, image image.Image, encode bool) (image.Image, error) {
if encode {
bmp.Encode(w, image)
return image, nil
} else {
return bmp.Decode(w)
}
}, Encodable: true},
{Title: "PNG", Extensions: []string{".png"},
Function: func(w io.ReadWriter, image image.Image, encode bool) (image.Image, error) {
if encode {
png.Encode(w, image)
return image, nil
} else {
return png.Decode(w)
}
}, Encodable: true},
{Title: "JPEG", Extensions: []string{".jpg", ".jpeg"},
Function: func(w io.ReadWriter, image image.Image, encode bool) (image.Image, error) {
if encode {
jpeg.Encode(w, image, &jpeg.Options{Quality: 100})
return image, nil
} else {
return jpeg.Decode(w)
}
}, Encodable: true},
{Title: "TIFF", Extensions: []string{".tif", ".tiff"},
Function: func(w io.ReadWriter, image image.Image, encode bool) (image.Image, error) {
if encode {
tiff.Encode(w, image, &tiff.Options{Compression: tiff.CCITTGroup4})
return image, nil
} else {
return tiff.Decode(w)
}
}, Encodable: true},
{Title: "WEBP", Extensions: []string{".webp"},
Function: func(w io.ReadWriter, image image.Image, encode bool) (image.Image, error) {
if encode {
return nil, errors.New("'webp' format encoding not supported")
} else {
return webp.Decode(w)
}
}, Encodable: false},
{Title: "GIF", Extensions: []string{".gif"},
Function: func(w io.ReadWriter, image image.Image, encode bool) (image.Image, error) {
if encode {
//gif.Encode(w, image, &gif.Options{})
//return image, nil
return nil, errors.New("'gif' format encoding not supported")
} else {
return gif.Decode(w)
}
}, Encodable: false},
}
func FindFormatFromExt(extension string) *SupportedFormat {
for i := range formats {
for _, ext := range formats[i].Extensions {
if strings.EqualFold(ext, extension) {
return &formats[i]
}
}
}
return nil
}
func FindFormatIndexFromExt(extension string) int {
for i := range formats {
for _, ext := range formats[i].Extensions {
if strings.EqualFold(ext, extension) {
return i
}
}
}
return 0
}
func GetFormatCount() int {
return len(formats)
}
func GetDialogFilters(onlyEncodable bool) string {
filter := ""
for _, format := range formats {
if onlyEncodable && !format.Encodable {
continue
}
filter += format.Title + " (*"
for j, ext := range format.Extensions {
if j != 0 {
filter += ", *"
}
filter += ext
}
filter += ")|*"
for j, ext := range format.Extensions {
if j != 0 {
filter += ";*"
}
filter += ext
}
filter += "|"
}
return filter
}
func GetSaveFileDialogFilters() string {
return GetDialogFilters(true)
}
func GetOpenFileDialogFilters() string {
filter := GetDialogFilters(false)
filter += "All Picture Files|*"
for i, format := range formats {
if i != 0 {
filter += ";*"
}
for j, ext := range format.Extensions {
if j != 0 {
filter += ";*"
}
filter += ext
}
}
filter += "|All Files|*.*|"
return filter
}