-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ani.cs
244 lines (199 loc) · 7.04 KB
/
Ani.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
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
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using Vector2 = (int x, int y);
// Reference:https://www.cnblogs.com/youlin/p/3282263.html
public static class Ani
{
public static void Convert()
{
#region 0 Input
List<Bitmap> images = [];
int width = -1, height = -1;
while (true)
{
Console.Write($"图片文件位置 (索引:{images.Count},留空继续): ");
string s = Console.ReadLine();
if (string.IsNullOrEmpty(s))
break;
Bitmap bm = (Bitmap)Image.FromFile(s);
images.Add(bm);
if (width == -1 && height == -1)
{
width = bm.Width;
height = bm.Height;
}
if (width != bm.Width || height != bm.Height)
return;
}
Console.Write("播放速率 (单位为1/60秒): ");
int displayRate = int.Parse(Console.ReadLine());
List<int> rates = [];
for (int g = 0; g < images.Count; g++)
{
Console.Write($"第 {rates.Count} 帧播放速率 (单位为1/60秒,留空为 {displayRate}): ");
string s = Console.ReadLine();
if (string.IsNullOrEmpty(s))
{
rates.Add(displayRate);
continue;
}
int i = int.Parse(s);
rates.Add(i);
}
Console.Write("热点X坐标: ");
int hotspotX = int.Parse(Console.ReadLine());
Console.Write("热点Y坐标: ");
int hotspotY = int.Parse(Console.ReadLine());
List<Vector2> hotspots = [];
for (int g = 0; g < images.Count; g++)
{
Console.Write($"第 {hotspots.Count} 帧热点X坐标(留空为 {hotspotX}): ");
string s = Console.ReadLine();
int x;
if (string.IsNullOrEmpty(s))
x = hotspotX;
else
x = int.Parse(s);
Console.Write($"第 {hotspots.Count} 帧热点Y坐标(留空为 {hotspotY}): ");
s = Console.ReadLine();
int y;
if (string.IsNullOrEmpty(s))
y = hotspotY;
else
y = int.Parse(s);
hotspots.Add(new(x, y));
}
Console.Write("输出文件位置: ");
string outputPath = Console.ReadLine();
int len = GetData(out byte[] buffer, width, height, images, displayRate, rates, hotspots);
byte[] val = buffer[..len];
Console.WriteLine(len);
File.WriteAllBytes(outputPath, val);
#endregion
}
// 这个函数来获取ANI的数据!!!!
public static int GetData(out byte[] buffer, int width, int height, List<Bitmap> images, int rate, List<int> rates, List<Vector2> hotspots)
{
BinaryWriter bw = new(new MemoryStream());
int datalistoffset = 0;
int fullfilesize = 0;
#region 1 Header
// Header
// RIFF (52 49 46 46)
bw.Write([0x52, 0x49, 0x46, 0x46]);
// File Length
int fileLengthPos = 0x0004;
bw.Write([0, 0, 0, 0]);
// ACON
// ACON (41 43 4F 4E)
bw.Write([0x41, 0x43, 0x4F, 0x4E]);
datalistoffset += 8 + sizeof(int);
#endregion
#region 2 ANIHeader
// ANIH Sign
// anih (61 6E 69 68)
bw.Write([0x61, 0x6E, 0x69, 0x68]);
// Anih struct
AniHeader anih = new()
{
dwNumFrames = images.Count,
dwNumSteps = images.Count,
dwWidth = width,
dwHeight = height,
dwDisplayRate = rate,
dwFlags = AniHeader.Flags.IconOrCursor | AniHeader.Flags.HasSeqSegment,
};
bw.Write(anih.dwHeaderSize); // 我不知道为啥,但是cursorworkshop导出的这里就多一个这个
datalistoffset += 4 + sizeof(int);
int size = Marshal.SizeOf(anih);
nint b = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(anih, b, false);
byte[] bytes = new byte[size];
Marshal.Copy(b, bytes, 0, size);
bw.Write(bytes);
datalistoffset += size;
#endregion
#region 3 SEQ
// SEQ sign
// "seq " (73 65 71 20)
bw.Write([0x73, 0x65, 0x71, 0x20]);
// Data length
bw.Write(images.Count * sizeof(int));
for (int i = 0; i < images.Count; i++)
bw.Write(i);
datalistoffset += 4 + sizeof(int) + images.Count * sizeof(int);
#endregion
#region 4 Rate
// Rate sign
// rate (72 61 74 65)
bw.Write([0x72, 0x61, 0x74, 0x65]);
// Data length
bw.Write(rates.Count * sizeof(int));
datalistoffset += 4 + rates.Count * sizeof(int) + sizeof(int);
foreach (int i in rates)
bw.Write(i);
#endregion
#region 5 List (Frame data)
// List sign
// LIST (4C 49 53 54)
bw.Write([0x4C, 0x49, 0x53, 0x54]);
datalistoffset += 4;
fullfilesize += datalistoffset;
// Data Length
bw.Write([0, 0, 0, 0]);
int datalenpos = datalistoffset;
// Fram sign
// fram (66 72 61 6D)
bw.Write([0x66, 0x72, 0x61, 0x6D]);
fullfilesize += sizeof(int);
int datalength = 4;
for (int i = 0; i < images.Count; i++)
{
// Icon sign
// icon (69 63 6F 6E)
bw.Write([0x69, 0x63, 0x6F, 0x6E]);
int len = Cur.GetData(out byte[] buf, images[i], hotspots[i].x, hotspots[i].y);
bw.Write(len);
byte[] trimedbuf = buf[..len];
bw.Write(trimedbuf);
datalength += len + 4 + sizeof(int);
}
fullfilesize += datalength;
buffer = ((MemoryStream)bw.BaseStream).GetBuffer();
byte[] ffsb = BitConverter.GetBytes(fullfilesize);
buffer[fileLengthPos] = ffsb[0];
buffer[fileLengthPos + 1] = ffsb[1];
buffer[fileLengthPos + 2] = ffsb[2];
buffer[fileLengthPos + 3] = ffsb[3];
ffsb = BitConverter.GetBytes(datalength);
buffer[datalenpos] = ffsb[0];
buffer[datalenpos + 1] = ffsb[1];
buffer[datalenpos + 2] = ffsb[2];
buffer[datalenpos + 3] = ffsb[3];
return fullfilesize;
#endregion
}
// _anih
[StructLayout(LayoutKind.Sequential)]
private struct AniHeader
{
public int dwHeaderSize = 36;
public int dwNumFrames; // Frame count
public int dwNumSteps; // Begin frame
public int dwWidth; // Image width
public int dwHeight; // Image height
public int dwBitCount = 32; // Color pixel bit count
public int dwNumPlanes = 1; // Planes number
public int dwDisplayRate; // Frame delay, unit: 1/60 second
public Flags dwFlags; // Flags
public AniHeader()
{ }
[Flags]
public enum Flags : int
{
IconOrCursor = 0b00000001,
HasSeqSegment = 0b00000010,
}
}
}