forked from minotar/imgd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
452 lines (366 loc) · 13 KB
/
process.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package main
import (
"image"
"image/color"
"image/draw"
"image/png"
"io"
"math"
"strconv"
"github.com/ajstarks/svgo"
"github.com/disintegration/gift"
"github.com/disintegration/imaging"
"github.com/minotar/minecraft"
)
const (
HeadX = 8
HeadY = 8
HeadWidth = 8
HeadHeight = 8
HelmX = 40
HelmY = 8
TorsoX = 20
TorsoY = 20
TorsoWidth = 8
TorsoHeight = 12
Torso2X = 20
Torso2Y = 36
RaX = 44
RaY = 20
RaWidth = 4
RaHeight = 12
Ra2X = 44
Ra2Y = 36
RlX = 4
RlY = 20
RlWidth = 4
RlHeight = 12
Rl2X = 4
Rl2Y = 36
LaX = 36
LaY = 52
LaWidth = 4
LaHeight = 12
La2X = 52
La2Y = 52
LlX = 20
LlY = 52
LlWidth = 4
LlHeight = 12
Ll2X = 4
Ll2Y = 52
// The height of the 'bust' relative to the width of the body (16)
BustHeight = 16
)
type mcSkin struct {
Processed image.Image
Mode string
minecraft.Skin
}
// Sets skin.Processed to the face of the user.
func (skin *mcSkin) GetHead(width int) error {
skin.Processed = skin.cropHead(skin.Image)
skin.resize(width, imaging.NearestNeighbor)
return nil
}
// Sets skin.Processed to the face of the user overlaid with their helmet.
func (skin *mcSkin) GetHelm(width int) error {
skin.Processed = skin.cropHelm(skin.Image)
skin.resize(width, imaging.NearestNeighbor)
return nil
}
// Sets skin.Processed to an isometric render of the head from a top-left angle (showing 3 sides).
func (skin *mcSkin) GetCube(width int) error {
// Crop out the top of the head
topFlat := imaging.Crop(skin.Image, image.Rect(8, 0, 16, 8))
// Resize appropriately, so that it fills the `width` when rotated 45 def.
topFlat = imaging.Resize(topFlat, int(float64(width)*math.Sqrt(2)/3+1), 0, imaging.NearestNeighbor)
// Create the Gift filter
filter := gift.New(
gift.Rotate(45, color.Transparent, gift.LinearInterpolation),
)
bounds := filter.Bounds(topFlat.Bounds())
top := image.NewNRGBA(bounds)
// Draw it on the filter, then smush it!
filter.Draw(top, topFlat)
top = imaging.Resize(top, width+2, width/3, imaging.NearestNeighbor)
// Skew the front and sides at 15 degree angles to match up with the
// head that has been smushed
front := skin.cropHead(skin.Image).(*image.NRGBA)
side := imaging.Crop(skin.Image, image.Rect(0, 8, 8, 16))
front = imaging.Resize(front, width/2, int(float64(width)/1.75), imaging.NearestNeighbor)
side = imaging.Resize(side, width/2, int(float64(width)/1.75), imaging.NearestNeighbor)
front = skewVertical(front, math.Pi/12)
side = skewVertical(imaging.FlipH(side), math.Pi/-12)
// Create a new image to assemble upon
skin.Processed = image.NewNRGBA(image.Rect(0, 0, width, width))
// Draw each side
draw.Draw(skin.Processed.(draw.Image), image.Rect(0, width/6, width/2, width), side, image.Pt(0, 0), draw.Src)
draw.Draw(skin.Processed.(draw.Image), image.Rect(width/2, width/6, width, width), front, image.Pt(0, 0), draw.Src)
// Draw the top we created
draw.Draw(skin.Processed.(draw.Image), image.Rect(-1, 0, width+1, width/3), top, image.Pt(0, 0), draw.Over)
return nil
}
// Sets skin.Processed to the upper portion of the body (slightly higher cutoff than waist).
func (skin *mcSkin) GetBust(width int) error {
headImg := skin.cropHead(skin.Image).(*image.NRGBA)
upperBodyImg := skin.renderUpperBody()
bustImg := skin.addHead(upperBodyImg, headImg)
bustImg.Rect.Max.Y = BustHeight
skin.Processed = bustImg
skin.resize(width, imaging.NearestNeighbor)
return nil
}
// Sets skin.Processed to the upper portion of the body (slightly higher cutoff than waist) but with any armor which the user has.
func (skin *mcSkin) GetArmorBust(width int) error {
helmImg := skin.cropHelm(skin.Image).(*image.NRGBA)
upperArmorImg := skin.renderUpperArmor()
bustImg := skin.addHead(upperArmorImg, helmImg)
bustImg.Rect.Max.Y = BustHeight
skin.Processed = bustImg
skin.resize(width, imaging.NearestNeighbor)
return nil
}
// Sets skin.Processed to a front render of the body.
func (skin *mcSkin) GetBody(width int) error {
headImg := skin.cropHead(skin.Image).(*image.NRGBA)
upperBodyImg := skin.renderUpperBody()
lowerBodyImg := skin.renderLowerBody()
bodyImg := skin.addHead(upperBodyImg, headImg)
skin.Processed = skin.addLegs(bodyImg, lowerBodyImg)
skin.resize(width, imaging.NearestNeighbor)
return nil
}
// Sets skin.Processed to a front render of the body but with any armor which the user has.
func (skin *mcSkin) GetArmorBody(width int) error {
helmImg := skin.cropHelm(skin.Image).(*image.NRGBA)
upperArmorImg := skin.renderUpperArmor()
lowerArmorImg := skin.renderLowerArmor()
bodyImg := skin.addHead(upperArmorImg, helmImg)
skin.Processed = skin.addLegs(bodyImg, lowerArmorImg)
skin.resize(width, imaging.NearestNeighbor)
return nil
}
// Returns the torso and arms.
func (skin *mcSkin) renderUpperBody() *image.NRGBA {
// This will be the base.
upperBodyImg := image.NewNRGBA(image.Rect(0, 0, LaWidth+TorsoWidth+RaWidth, TorsoHeight))
torsoImg := imaging.Crop(skin.Image, image.Rect(TorsoX, TorsoY, TorsoX+TorsoWidth, TorsoY+TorsoHeight))
raImg := imaging.Crop(skin.Image, image.Rect(RaX, RaY, RaX+RaWidth, RaY+TorsoHeight))
// If it's an old skin, they don't have a Left Arm, so we'll just flip their right.
var laImg image.Image
if skin.is18Skin() {
laImg = imaging.Crop(skin.Image, image.Rect(LaX, LaY, LaX+LaWidth, LaY+TorsoHeight))
} else {
laImg = imaging.FlipH(raImg)
}
return skin.drawUpper(upperBodyImg, torsoImg, raImg, laImg.(*image.NRGBA))
}
// Returns the torso and arms but with any armor which the user has.
func (skin *mcSkin) renderUpperArmor() *image.NRGBA {
// This will be the base.
upperArmorBodyImg := skin.renderUpperBody()
// If it's an old skin, they don't have armor here.
if skin.is18Skin() {
// Get the armor layers from the skin and remove the Alpha.
torso2Img := imaging.Crop(skin.Image, image.Rect(Torso2X, Torso2Y, Torso2X+TorsoWidth, Torso2Y+TorsoHeight))
skin.removeAlpha(torso2Img)
la2Img := imaging.Crop(skin.Image, image.Rect(La2X, La2Y, La2X+LaWidth, La2Y+TorsoHeight))
skin.removeAlpha(la2Img)
ra2Img := imaging.Crop(skin.Image, image.Rect(Ra2X, Ra2Y, Ra2X+RaWidth, Ra2Y+TorsoHeight))
skin.removeAlpha(ra2Img)
return skin.drawUpper(upperArmorBodyImg, torso2Img, ra2Img, la2Img)
}
return upperArmorBodyImg
}
// Given a base, torso and arms, it will return them all arranged correctly.
func (skin *mcSkin) drawUpper(base, torso, la, ra *image.NRGBA) *image.NRGBA {
// Torso
fastDraw(base, torso, LaWidth, 0)
// Left Arm
fastDraw(base, la, 0, 0)
// Right Arm
fastDraw(base, ra, LaWidth+TorsoWidth, 0)
return base
}
// Returns the legs.
func (skin *mcSkin) renderLowerBody() *image.NRGBA {
// This will be the base.
lowerBodyImg := image.NewNRGBA(image.Rect(0, 0, LlWidth+RlWidth, LlHeight))
rlImg := imaging.Crop(skin.Image, image.Rect(RlX, RlY, RlX+RlWidth, RlY+RlHeight))
// If it's an old skin, they don't have a Left Leg, so we'll just flip their right.
var llImg image.Image
if skin.is18Skin() {
llImg = imaging.Crop(skin.Image, image.Rect(LlX, LlY, LlX+LlWidth, LlY+LlHeight))
} else {
llImg = imaging.FlipH(rlImg)
}
return skin.drawLower(lowerBodyImg, rlImg, llImg.(*image.NRGBA))
}
// Returns the legs but with any armor which the user has.
func (skin *mcSkin) renderLowerArmor() *image.NRGBA {
// This will be the base.
lowerArmorBodyImg := skin.renderLowerBody()
// If it's an old skin, they don't have armor here.
if skin.is18Skin() {
// Get the armor layers from the skin and remove the Alpha.
ll2Img := imaging.Crop(skin.Image, image.Rect(Ll2X, Ll2Y, Ll2X+LlWidth, Ll2Y+LlHeight))
skin.removeAlpha(ll2Img)
rl2Img := imaging.Crop(skin.Image, image.Rect(Rl2X, Rl2Y, Rl2X+RlWidth, Rl2Y+RlHeight))
skin.removeAlpha(rl2Img)
return skin.drawLower(lowerArmorBodyImg, ll2Img, rl2Img)
}
return lowerArmorBodyImg
}
// Given a base and legs, it will return them all arranged correctly.
func (skin *mcSkin) drawLower(base, ll, rl *image.NRGBA) *image.NRGBA {
// Left Leg
fastDraw(base, ll, 0, 0)
// Right Leg
fastDraw(base, rl, LlWidth, 0)
return base
}
// Rams the head onto the base (hopefully body...) to return a Frankenstein.
func (skin *mcSkin) addHead(base, head *image.NRGBA) *image.NRGBA {
base.Pix = append(make([]uint8, HeadHeight*base.Stride), base.Pix...)
base.Rect.Max.Y += HeadHeight
fastDraw(base, head, LaWidth, 0)
return base
}
// Attached the legs onto the base (likely body).
func (skin *mcSkin) addLegs(base, legs *image.NRGBA) *image.NRGBA {
base.Pix = append(base.Pix, make([]uint8, LlHeight*base.Stride)...)
base.Rect.Max.Y += LlHeight
fastDraw(base, legs, LaWidth, HeadHeight+TorsoHeight)
return base
}
// Writes the *processed* image as a PNG to the given writer.
func (skin *mcSkin) WritePNG(w io.Writer) error {
return png.Encode(w, skin.Processed)
}
// Writes the processed image as an svg.
func (skin *mcSkin) WriteSVG(w io.Writer) error {
canvas := svg.New(w)
bounds := skin.Processed.Bounds()
img := skin.Processed.(*image.NRGBA)
// Make a canvas the same size as the image.
canvas.Start(bounds.Max.X, bounds.Max.Y, `shape-rendering="crispEdges"`)
// Loop through every pixel in the image.
for y := 0; y < bounds.Max.Y; y += 1 {
for x := 0; x < bounds.Max.X; x += 1 {
ptr := y*img.Stride + x*4
// Only draw opaque pixels.
if img.Pix[ptr+3] == 0xFF {
canvas.Rect(x, y, 1, 1, "fill:rgb("+
strconv.Itoa(int(img.Pix[ptr]))+","+
strconv.Itoa(int(img.Pix[ptr+1]))+","+
strconv.Itoa(int(img.Pix[ptr+2]))+")")
}
}
}
canvas.End()
return nil
}
// Writes the *original* skin image as a png to the given writer.
func (skin *mcSkin) WriteSkin(w io.Writer) error {
return png.Encode(w, skin.Image)
}
// Resizes the skin to the given dimensions, keeping aspect ratio.
func (skin *mcSkin) resize(width int, filter imaging.ResampleFilter) {
if skin.Mode != "None" {
skin.Processed = imaging.Resize(skin.Processed, width, 0, filter)
}
}
// Removes the skin's alpha matte from the given image.
func (skin *mcSkin) removeAlpha(img *image.NRGBA) {
// If it's already a transparent image, do nothing
if skin.AlphaSig[3] == 0 {
return
}
// Otherwise loop through all the pixels. Check to see which ones match
// the alpha signature and set their opacity to be zero.
for i := 0; i < len(img.Pix); i += 4 {
if img.Pix[i+0] == skin.AlphaSig[0] &&
img.Pix[i+1] == skin.AlphaSig[1] &&
img.Pix[i+2] == skin.AlphaSig[2] &&
img.Pix[i+3] == skin.AlphaSig[3] {
img.Pix[i+3] = 0
}
}
}
// Checks if the skin is a 1.8 skin using its height.
func (skin *mcSkin) is18Skin() bool {
bounds := skin.Image.Bounds()
return bounds.Max.Y == 64
}
// Returns the head of the skin image.
func (skin *mcSkin) cropHead(img image.Image) image.Image {
return imaging.Crop(img, image.Rect(HeadX, HeadY, HeadX+HeadWidth, HeadY+HeadHeight))
}
// Returns the head of the skin image overlayed with the helm.
func (skin *mcSkin) cropHelm(img image.Image) image.Image {
headImg := skin.cropHead(img)
helmImg := imaging.Crop(img, image.Rect(HelmX, HelmY, HelmX+HeadWidth, HelmY+HeadHeight))
skin.removeAlpha(helmImg)
fastDraw(headImg.(*image.NRGBA), helmImg, 0, 0)
return headImg
}
// Draws the "src" onto the "dst" image at the given x/y bounds, maintaining
// the original size. Pixels with have an alpha of 0x00 are not draw, and
// all others are drawn with an alpha of 0xFF
func fastDraw(dst *image.NRGBA, src *image.NRGBA, x, y int) {
bounds := src.Bounds()
maxY := bounds.Max.Y
maxX := bounds.Max.X * 4
pointer := dst.PixOffset(x, y)
for row := 0; row < maxY; row += 1 {
for i := 0; i < maxX; i += 4 {
srcPx := row*src.Stride + i
dstPx := row*dst.Stride + i + pointer
if src.Pix[srcPx+3] != 0 {
dst.Pix[dstPx+0] = src.Pix[srcPx+0]
dst.Pix[dstPx+1] = src.Pix[srcPx+1]
dst.Pix[dstPx+2] = src.Pix[srcPx+2]
dst.Pix[dstPx+3] = 0xFF
}
}
}
}
func skewVertical(src *image.NRGBA, degrees float64) *image.NRGBA {
bounds := src.Bounds()
maxY := bounds.Max.Y
maxX := bounds.Max.X * 4
distance := float64(bounds.Max.X) * math.Tan(degrees)
shouldFlip := false
if distance < 0 {
distance = -distance
shouldFlip = true
}
newHeight := maxY + int(1+distance)
dst := image.NewNRGBA(image.Rect(0, 0, bounds.Max.X, newHeight))
step := distance
for x := 0; x < maxX; x += 4 {
for row := 0; row < maxY; row += 1 {
srcPx := row*src.Stride + x
dstLower := (int(step)+row)*dst.Stride + x
dstUpper := dstLower + dst.Stride
_, delta := math.Modf(step)
if src.Pix[srcPx+3] != 0 {
dst.Pix[dstLower+0] += uint8(float64(src.Pix[srcPx+0]) * (1 - delta))
dst.Pix[dstLower+1] += uint8(float64(src.Pix[srcPx+1]) * (1 - delta))
dst.Pix[dstLower+2] += uint8(float64(src.Pix[srcPx+2]) * (1 - delta))
dst.Pix[dstLower+3] += uint8(float64(src.Pix[srcPx+3]) * (1 - delta))
dst.Pix[dstUpper+0] += uint8(float64(src.Pix[srcPx+0]) * delta)
dst.Pix[dstUpper+1] += uint8(float64(src.Pix[srcPx+1]) * delta)
dst.Pix[dstUpper+2] += uint8(float64(src.Pix[srcPx+2]) * delta)
dst.Pix[dstUpper+3] += uint8(float64(src.Pix[srcPx+3]) * delta)
}
}
step -= distance / float64(bounds.Max.X)
}
if shouldFlip {
return imaging.FlipH(dst)
} else {
return dst
}
}