-
Notifications
You must be signed in to change notification settings - Fork 1
/
SkinResolver.cs
163 lines (141 loc) · 5.35 KB
/
SkinResolver.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Flurl.Http;
namespace MinecraftLaunch.Skin {
public class SkinResolver {
private byte[] ImageBytes = null!;
public SkinResolver(string url) {
ImageBytes = url.GetBytesAsync().Result;
}
public SkinResolver(byte[] bytes) {
ImageBytes = bytes;
}
public SkinResolver(FileInfo filePath) {
if (filePath.Exists) {
ImageBytes = File.ReadAllBytes(filePath.FullName);
} else {
throw new FileNotFoundException();
}
}
/// <summary>
/// 裁剪皮肤图片头像
/// </summary>
/// <returns>裁剪后图片</returns>
public Image<Rgba32> CropSkinHeadBitmap() {
Image<Rgba32> head = (Image<Rgba32>)Image.Load(ImageBytes);
head.Mutate(x => x.Crop(Rectangle.FromLTRB(8, 8, 16, 16)));
Image<Rgba32> hat = (Image<Rgba32>)Image.Load(ImageBytes);
hat.Mutate(x => x.Crop(Rectangle.FromLTRB(40, 8, 48, 16)));
Image<Rgba32> endImage = new Image<Rgba32>(8, 8);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
endImage[i, j] = head[i, j];
if (hat[i, j].A == 255) {
endImage[i, j] = hat[i, j];
}
}
}
return ResizeImage(endImage, 60, 60);
}
/// <summary>
/// 裁剪皮肤图片身体
/// </summary>
/// <typeparam name="TPixel"></typeparam>
/// <param name="skin"></param>
/// <returns></returns>
public Image<Rgba32> CropSkinBodyBitmap() {
var skin = ImageHelper.ConvertToImage(ImageBytes);
Image<Rgba32> Body = CopyBitmap(skin);
Body.Mutate(x => x.Crop(Rectangle.FromLTRB(20, 20, 28, 32)));
return ResizeImage(Body, 60, 90);
}
/// <summary>
/// 裁剪皮肤图片右手
/// </summary>
/// <typeparam name="TPixel"></typeparam>
/// <param name="skin"></param>
/// <returns></returns>
public Image<Rgba32> CropRightHandBitmap() {
var skin = ImageHelper.ConvertToImage(ImageBytes);
Image<Rgba32> Arm = CopyBitmap(skin);
Arm.Mutate(x => x.Crop(Rectangle.FromLTRB(35, 52, 39, 64)));
return ResizeImage(Arm, 30, 90);
}
/// <summary>
/// 裁剪皮肤图片左手
/// </summary>
/// <typeparam name="TPixel"></typeparam>
/// <param name="skin"></param>
/// <returns></returns>
public Image<Rgba32> CropLeftHandBitmap() {
var skin = ImageHelper.ConvertToImage(ImageBytes);
Image<Rgba32> Arm = CopyBitmap(skin);
Arm.Mutate(x => x.Crop(Rectangle.FromLTRB(44, 20, 48, 32)));
return ResizeImage(Arm, 30, 90);
}
/// <summary>
/// 裁剪皮肤图片右腿
/// </summary>
/// <typeparam name="TPixel"></typeparam>
/// <param name="skin"></param>
/// <returns></returns>
public Image<Rgba32> CropRightLegBitmap() {
var skin = ImageHelper.ConvertToImage(ImageBytes);
Image<Rgba32> Leg = CopyBitmap(skin);
Leg.Mutate(x => x.Crop(Rectangle.FromLTRB(20, 52, 24, 64)));
return ResizeImage(Leg, 30, 90);
}
/// <summary>
/// 裁剪皮肤图片左腿
/// </summary>
/// <typeparam name="TPixel"></typeparam>
/// <param name="skin"></param>
/// <returns></returns>
public Image<Rgba32> CropLeftLegBitmap() {
var skin = ImageHelper.ConvertToImage(ImageBytes);
Image<Rgba32> Leg = CopyBitmap(skin);
Leg.Mutate(x => x.Crop(Rectangle.FromLTRB(4, 20, 8, 32)));
return ResizeImage(Leg, 30, 90);
}
/// <summary>
/// 重置图片长宽
/// </summary>
/// <typeparam name="TPixel"></typeparam>
/// <param name="image"></param>
/// <param name="w"></param>
/// <param name="h"></param>
/// <returns></returns>
public Image<Rgba32> ResizeImage(Image<Rgba32> image, int w, int h) {
Image<Rgba32> image2 = new(w, h);
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
double tmp;
tmp = image.Width / (double)w;
double realW = tmp * (i);
tmp = image.Height / (double)h;
double realH = (tmp) * (j);
image2[i, j] = image[(int)realW, (int)realH];
}
}
return image2;
}
/// <summary>
/// 基础裁剪方法
/// </summary>
/// <typeparam name="TPixel"></typeparam>
/// <param name="image"></param>
/// <returns></returns>
private Image<Rgba32> CopyBitmap(Image<Rgba32> image) {
Image<Rgba32> tmp = new(image.Width, image.Height);
for (int i = 0; i < image.Width; i++) {
for (int j = 0; j < image.Height; j++) {
tmp[i, j] = image[i, j];
}
}
return tmp;
}
}
}