-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.go
299 lines (259 loc) · 7.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package images
import (
"fmt"
"errors"
"strings"
"os"
"image"
"image/jpeg"
"image/png"
"image/gif"
// _ "image/draw"
"github.com/mantyr/image/tiff"
"github.com/mantyr/image/bmp"
"crypto/sha256"
"encoding/hex"
"io"
)
type Image struct {
Image *image.NRGBA
Address string
Format string
Quality int
width int
height int
Error error
}
const (
FormatTIFF string = "tiff"
FormatJPEG string = "jpeg"
FormatGIF string = "gif"
FormatPNG string = "png"
FormatBMP string = "bmp"
)
var Formats = [5]string{"tiff", "jpeg", "gif", "png", "bmp"}
func OpenFile(file *os.File) (i *Image, err error) {
i, err = Open(file.Name())
return
}
func Open(address string) (i *Image, err error){
i = new(Image)
i.Address = address
i.Quality = 100
file, err := os.Open(address)
if err != nil {
i.Error = err
return
}
defer file.Close()
var img image.Image
img, i.Format, err = image.Decode(file)
if err != nil {
i.Error = err
return
}
i.Image = toNRGBA(img)
i.width = i.Image.Bounds().Max.X
i.height = i.Image.Bounds().Max.Y
return
}
func (i *Image) SetQuality(quality int) *Image {
if quality < 0 {
quality = 0
} else if quality > 100 {
quality = 100
}
i.Quality = quality
return i
}
func (i *Image) SetPNGNoCompression() *Image {
i.Quality = 100
return i
}
func (i *Image) SetPNGBestSpeed() *Image {
i.Quality = 50
return i
}
func (i *Image) SetPNGBestCompression() *Image {
i.Quality = 0
return i
}
func (i *Image) Save(params ...string) (err error) {
if i.Image == nil {
if i.Error != nil {
return i.Error
}
return errors.New("no image")
}
address := i.Address
if len(params) > 0 && strings.Trim(params[0], " \n\t\r") != "" {
address = params[0]
}
file, err := os.Create(address)
defer file.Close()
if err != nil {
return
}
err = i.write(file)
return
}
func (i *Image) GetHash() string {
if i.Image == nil {
return ""
}
hash := sha256.New()
err := i.write(hash)
if err != nil {
return ""
}
return hex.EncodeToString(hash.Sum(nil))
}
func (i *Image) write(file io.Writer) (err error) {
switch i.Format {
case FormatJPEG:
var rgba *image.RGBA
if i.Image.Opaque() {
rgba = &image.RGBA{
Pix: i.Image.Pix,
Stride: i.Image.Stride,
Rect: i.Image.Rect,
}
}
if rgba != nil {
err = jpeg.Encode(file, rgba, &jpeg.Options{Quality: i.Quality})
} else {
err = jpeg.Encode(file, i.Image, &jpeg.Options{Quality: i.Quality})
}
case FormatPNG:
var encoder *png.Encoder
if i.Quality == 100 {
encoder = &png.Encoder{CompressionLevel: png.NoCompression}
} else if i.Quality >= 50 {
encoder = &png.Encoder{CompressionLevel: png.BestSpeed}
} else if i.Quality < 50 {
encoder = &png.Encoder{CompressionLevel: png.BestCompression}
}
err = encoder.Encode(file, i.Image)
case FormatGIF:
err = gif.Encode(file, i.Image, &gif.Options{NumColors: 256})
case FormatTIFF:
err = tiff.Encode(file, i.Image, &tiff.Options{Compression: tiff.Uncompressed, Predictor: true})
case FormatBMP:
err = bmp.Encode(file, i.Image)
default:
err = errors.New("unsupported image format "+i.Format)
}
return
}
// Картинка больше прямоугольника
func (i *Image) ResizeOut(width, height int, params ...ResampleFilter) *Image {
var w_ratio float64 = float64(width) / i.Width64()
var h_ratio float64 = float64(height) / i.Height64()
if w_ratio == h_ratio {
} else if w_ratio > h_ratio {
height = int(i.Height64() * w_ratio)
} else {
width = int(i.Width64() * h_ratio)
}
return i.Resize(width, height, params...)
}
// Картинка меньше прямоугольника
func (i *Image) ResizeIn(width, height int, params ...ResampleFilter) *Image {
var w_ratio float64 = float64(width) / i.Width64()
var h_ratio float64 = float64(height) / i.Height64()
if w_ratio == h_ratio {
} else if w_ratio < h_ratio {
height = int(i.Height64() * w_ratio)
} else {
width = int(i.Width64() * h_ratio)
}
return i.Resize(width, height, params...)
}
// Картинка меньше прямоугольника, но если изначальный размер ещё меньше то картинка не растягивается
func (i *Image) ResizeMax(width, height int, params ...ResampleFilter) *Image {
if i.Height() <= height && i.Width() <= width {
return i
}
return i.ResizeIn(width, height, params...)
}
func (i *Image) ResizeCrop(width, height int, params ...ResampleFilter) (img *Image) {
img = i.ResizeOut(width, height, params...)
defer func() {
if r := recover(); r != nil {
img.Error = errors.New(fmt.Sprintf("%v", r))
}
}()
if img.Width() == width && img.Height() == height {
return
}
Min := image.Point{}
Min.X = int((img.Width() - width)/2)
Min.Y = int((img.Height() - height)/2)
Max := image.Point{}
Max.X = Min.X + width
Max.Y = Min.Y + height
res := image.Rectangle{Min: Min, Max: Max}
sub := img.Image.SubImage(res)
// img.Image = Clone(sub)
img.Image = toNRGBA(sub)
img.width = width
img.height= height
return
}
func (i *Image) Resize(width, height int, params ...ResampleFilter) (image *Image) {
image = new(Image)
image.width = i.Width()
image.height = i.Height()
image.Quality = 100
defer func() {
if r := recover(); r != nil {
image.Error = errors.New(fmt.Sprintf("%v", r))
}
}()
filter := Lanczos
if len(params) > 0 {
filter = params[0]
}
if filter.Support <= 0.0 {
image.Image, image.Error = i.resize(width, height)
} else {
if width != i.Width() {
image.Image, image.Error = i.resizeW(width, filter)
image.width = width
}
if image.Error != nil {
return
}
if height != i.Height() {
image.Image, image.Error = image.resizeH(height, filter)
image.height = height
}
}
image.Format = i.Format
return
}
// Превращает изображение в негатив или наоборот
func (i *Image) Negative() (image *Image) {
image = new(Image)
image.width = i.Width()
image.height = i.Height()
image.Quality = 100
defer func() {
if r := recover(); r != nil {
image.Error = errors.New(fmt.Sprintf("%v", r))
}
}()
image.Image, image.Error = i.negative()
image.Format = i.Format
return
}
// In the future be replaced by toYCbCr
func toNRGBA(i image.Image) *image.NRGBA {
srcBounds := i.Bounds()
if srcBounds.Min.X == 0 && srcBounds.Min.Y == 0 {
if src0, ok := i.(*image.NRGBA); ok {
return src0
}
}
return Clone(i)
}