This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCharacterImage.go
219 lines (178 loc) · 6.59 KB
/
CharacterImage.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
package arn
import (
"bytes"
"fmt"
"image"
"net/http"
"path"
"time"
"github.com/aerogo/http/client"
"github.com/akyoto/imageserver"
)
const (
// CharacterImageLargeWidth is the minimum width in pixels of a large character image.
// We subtract 6 pixels due to border removal which can remove up to 6 pixels.
CharacterImageLargeWidth = 225 - 6
// CharacterImageLargeHeight is the minimum height in pixels of a large character image.
// We subtract 6 pixels due to border removal which can remove up to 6 pixels.
CharacterImageLargeHeight = 350 - 6
// CharacterImageMediumWidth is the minimum width in pixels of a medium character image.
CharacterImageMediumWidth = 112
// CharacterImageMediumHeight is the minimum height in pixels of a medium character image.
CharacterImageMediumHeight = 112
// CharacterImageSmallWidth is the minimum width in pixels of a small character image.
CharacterImageSmallWidth = 56
// CharacterImageSmallHeight is the minimum height in pixels of a small character image.
CharacterImageSmallHeight = 56
// CharacterImageWebPQuality is the WebP quality of character images.
CharacterImageWebPQuality = 70
// CharacterImageJPEGQuality is the JPEG quality of character images.
CharacterImageJPEGQuality = 70
// CharacterImageQualityBonusLowDPI ...
CharacterImageQualityBonusLowDPI = 12
// CharacterImageQualityBonusLarge ...
CharacterImageQualityBonusLarge = 10
// CharacterImageQualityBonusMedium ...
CharacterImageQualityBonusMedium = 15
// CharacterImageQualityBonusSmall ...
CharacterImageQualityBonusSmall = 15
)
// Define the character image outputs
var characterImageOutputs = []imageserver.Output{
// Original at full size
&imageserver.OriginalFile{
Directory: path.Join(Root, "images/characters/original/"),
Width: 0,
Height: 0,
Quality: 0,
},
// JPEG - Large
&imageserver.JPEGFile{
Directory: path.Join(Root, "images/characters/large/"),
Width: CharacterImageLargeWidth,
Height: CharacterImageLargeHeight,
Quality: CharacterImageJPEGQuality + CharacterImageQualityBonusLowDPI + CharacterImageQualityBonusLarge,
},
// JPEG - Medium
&imageserver.JPEGFile{
Directory: path.Join(Root, "images/characters/medium/"),
Width: CharacterImageMediumWidth,
Height: CharacterImageMediumHeight,
Quality: CharacterImageJPEGQuality + CharacterImageQualityBonusLowDPI + CharacterImageQualityBonusMedium,
},
// JPEG - Small
&imageserver.JPEGFile{
Directory: path.Join(Root, "images/characters/small/"),
Width: CharacterImageSmallWidth,
Height: CharacterImageSmallHeight,
Quality: CharacterImageJPEGQuality + CharacterImageQualityBonusLowDPI + CharacterImageQualityBonusSmall,
},
// WebP - Large
&imageserver.WebPFile{
Directory: path.Join(Root, "images/characters/large/"),
Width: CharacterImageLargeWidth,
Height: CharacterImageLargeHeight,
Quality: CharacterImageWebPQuality + CharacterImageQualityBonusLowDPI + CharacterImageQualityBonusLarge,
},
// WebP - Medium
&imageserver.WebPFile{
Directory: path.Join(Root, "images/characters/medium/"),
Width: CharacterImageMediumWidth,
Height: CharacterImageMediumHeight,
Quality: CharacterImageWebPQuality + CharacterImageQualityBonusLowDPI + CharacterImageQualityBonusMedium,
},
// WebP - Small
&imageserver.WebPFile{
Directory: path.Join(Root, "images/characters/small/"),
Width: CharacterImageSmallWidth,
Height: CharacterImageSmallHeight,
Quality: CharacterImageWebPQuality + CharacterImageQualityBonusLowDPI + CharacterImageQualityBonusSmall,
},
}
// Define the high DPI character image outputs
var characterImageOutputsHighDPI = []imageserver.Output{
// NOTE: We don't save "large" images in double size because that's usually the maximum size anyway.
// JPEG - Medium
&imageserver.JPEGFile{
Directory: path.Join(Root, "images/characters/medium/"),
Width: CharacterImageMediumWidth * 2,
Height: CharacterImageMediumHeight * 2,
Quality: CharacterImageJPEGQuality + CharacterImageQualityBonusMedium,
},
// JPEG - Small
&imageserver.JPEGFile{
Directory: path.Join(Root, "images/characters/small/"),
Width: CharacterImageSmallWidth * 2,
Height: CharacterImageSmallHeight * 2,
Quality: CharacterImageJPEGQuality + CharacterImageQualityBonusSmall,
},
// WebP - Medium
&imageserver.WebPFile{
Directory: path.Join(Root, "images/characters/medium/"),
Width: CharacterImageMediumWidth * 2,
Height: CharacterImageMediumHeight * 2,
Quality: CharacterImageWebPQuality + CharacterImageQualityBonusMedium,
},
// WebP - Small
&imageserver.WebPFile{
Directory: path.Join(Root, "images/characters/small/"),
Width: CharacterImageSmallWidth * 2,
Height: CharacterImageSmallHeight * 2,
Quality: CharacterImageWebPQuality + CharacterImageQualityBonusSmall,
},
}
// CharacterImage ...
type CharacterImage AnimeImage
// SetImageBytes accepts a byte buffer that represents an image file and updates the character image.
func (character *Character) SetImageBytes(data []byte) error {
// Decode
img, format, err := image.Decode(bytes.NewReader(data))
if err != nil {
return err
}
return character.SetImage(&imageserver.MetaImage{
Image: img,
Format: format,
Data: data,
})
}
// SetImage sets the character image to the given MetaImage.
func (character *Character) SetImage(metaImage *imageserver.MetaImage) error {
var lastError error
// Save the different image formats and sizes in low DPI
for _, output := range characterImageOutputs {
err := output.Save(metaImage, character.ID)
if err != nil {
lastError = err
}
}
// Save the different image formats and sizes in high DPI
for _, output := range characterImageOutputsHighDPI {
err := output.Save(metaImage, character.ID+"@2")
if err != nil {
lastError = err
}
}
character.Image.Extension = metaImage.Extension()
character.Image.Width = metaImage.Image.Bounds().Dx()
character.Image.Height = metaImage.Image.Bounds().Dy()
character.Image.AverageColor = GetAverageColor(metaImage.Image)
character.Image.LastModified = time.Now().Unix()
return lastError
}
// DownloadImage ...
func (character *Character) DownloadImage(url string) error {
response, err := client.Get(url).End()
// Cancel the import if image could not be fetched
if err != nil {
return err
}
if response.StatusCode() != http.StatusOK {
return fmt.Errorf("Image response status code: %d", response.StatusCode())
}
return character.SetImageBytes(response.Bytes())
}
// HasImage returns true if the character has an image.
func (character *Character) HasImage() bool {
return character.Image.Extension != "" && character.Image.Width > 0
}