-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PPMFrame.cs
executable file
·305 lines (276 loc) · 10.2 KB
/
PPMFrame.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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
namespace PPMLib
{
public class PPMFrame
{
private PPMLayer _layer1 = new PPMLayer();
private PPMLayer _layer2 = new PPMLayer();
private PaperColor _paperColor;
private Image _frame = new Image<Rgba32>(256, 192);
private int _animationIndex;
public byte _firstByteHeader;
public int _translateX;
public int _translateY;
/// <summary>
/// Overwrite frame data
/// </summary>
/// <param name="prev">Frame data to apply</param>
public void Overwrite(PPMFrame prev) // Just use current frame
{
if ((_firstByteHeader & 0x80) != 0)
{
return;
}
// < There is NOT a mistake anywhere
int ld0 = ((_translateX >= 0) ? (_translateX >> 3) : 0);
int pi0 = (_translateX >= 0) ? 0 : ((-_translateX) >> 3);
byte del = (byte)(_translateX >= 0 ? (_translateX & 7) : (((byte)(_translateX)) & 7));
byte ndel = (byte)(8 - del);
byte alpha = (byte)((1 << (8 - del)) - 1);
byte nalpha = (byte)~alpha;
int pi, ld;
if (_translateX >= 0)
{
for (int y = 0; y < 192; y++)
{
if (y < _translateY) continue;
if (y - _translateY >= 192) break;
ld = (y << 5) + ld0;
pi = ((y - _translateY) << 5) + pi0;
Layer1[ld] ^= (byte)(prev.Layer1[pi] & alpha);
Layer2[ld++] ^= (byte)(prev.Layer2[pi] & alpha);
while ((ld & 31) < 31)
{
Layer1[ld] ^= (byte)(((prev.Layer1[pi] & nalpha) >> ndel) |
((prev.Layer1[pi + 1] & alpha) << del));
Layer2[ld] ^= (byte)(((prev.Layer2[pi] & nalpha) >> ndel) |
((prev.Layer2[pi + 1] & alpha) << del));
ld++;
pi++;
}
Layer1[ld] ^= (byte)((prev.Layer1[pi] & nalpha) | (prev.Layer1[pi + 1] & alpha));
Layer2[ld] ^= (byte)((prev.Layer2[pi] & nalpha) | (prev.Layer2[pi + 1] & alpha));
}
}
else
{
for (ushort y = 0; y < 192; y++)
{
if (y < _translateY) continue;
if (y - _translateY >= 192) break;
ld = (y << 5) + ld0;
pi = ((y - _translateY) << 5) + pi0;
while ((pi & 31) < 31)
{
Layer1[ld] ^= (byte)(((prev.Layer1[pi] & nalpha) >> ndel) |
((prev.Layer1[pi + 1] & alpha) << del));
Layer2[ld] ^= (byte)(((prev.Layer2[pi] & nalpha) >> ndel) |
((prev.Layer2[pi + 1] & alpha) << del));
ld++;
pi++;
}
Layer1[ld] ^= (byte)(prev.Layer1[pi] & nalpha);
Layer2[ld] ^= (byte)(prev.Layer2[pi] & nalpha);
}
}
}
public PPMFrame CreateDiff0(PPMFrame prev)
{
var frame = new PPMFrame();
frame._firstByteHeader = _firstByteHeader;
//frame._firstByteHeader &= 0b00011111;
//frame._firstByteHeader |= 0x80;
for (int i = 0; i < 32 * 192; i++)
{
frame.Layer1[i] = (byte)(Layer1[i] ^ prev.Layer1[i]);
frame.Layer2[i] = (byte)(Layer2[i] ^ prev.Layer2[i]);
}
for (int y = 0; y < 192; y++)
{
frame.Layer1.SetLineEncoding(y, frame.Layer1.ChooseLineEncoding(y));
frame.Layer2.SetLineEncoding(y, frame.Layer1.ChooseLineEncoding(y));
}
// > TO DO : update line encodings
return frame;
}
public PPMLayer Layer1
{
get => _layer1;
}
public PPMLayer Layer2
{
get => _layer2;
}
public int AnimationIndex
{
get { return _animationIndex; }
set { _animationIndex = value; }
}
public PaperColor PaperColor
{
get { return _paperColor; }
set { _paperColor = value; }
}
public override string ToString()
{
return _firstByteHeader.ToString("X2");
}
/// <summary>
/// Converts frame data to flipnote PPM-format binary
/// </summary>
/// <returns>
/// A byte array containing animation frame data as should
/// be existent in a PPM file
/// </returns>
public byte[] ToByteArray()
{
var res = new List<byte>();
res.Add(_firstByteHeader);
for (int l = 0; l < 192; l++)
{
Layer1.SetLineEncoding(l, Layer1.ChooseLineEncoding(l));
Layer2.SetLineEncoding(l, Layer2.ChooseLineEncoding(l));
}
res.AddRange(Layer1._linesEncoding);
res.AddRange(Layer2._linesEncoding);
for (int l = 0; l < 192; L1PutLine(res, l++)) ;
for (int l = 0; l < 192; L2PutLine(res, l++)) ;
return res.ToArray();
}
private void L1PutLine(List<byte> lst, int y)
{
int compr = (int)Layer1.LinesEncoding(y);
if (compr == 0) return;
if (compr == 1)
{
var chks = new List<byte>();
uint flag = 0;
for (int i = 0; i < 32; i++)
{
byte chunk = 0;
for (int j = 0; j < 8; j++)
if (Layer1[8 * i + j, y])
chunk |= (byte)(1 << j);
if (chunk != 0x00)
{
flag |= (1u << (31 - i));
chks.Add(chunk);
}
}
lst.Add((byte)((flag & 0xFF000000u) >> 24));
lst.Add((byte)((flag & 0x00FF0000u) >> 16));
lst.Add((byte)((flag & 0x0000FF00u) >> 8));
lst.Add((byte)(flag & 0x000000FFu));
lst.AddRange(chks);
return;
}
if (compr == 2)
{
var chks = new List<byte>();
uint flag = 0;
for (int i = 0; i < 32; i++)
{
byte chunk = 0;
for (int j = 0; j < 8; j++)
if (Layer1[8 * i + j, y])
chunk |= (byte)(1 << j);
if (chunk != 0xFF)
{
flag |= (1u << (31 - i));
chks.Add(chunk);
}
}
lst.Add((byte)((flag & 0xFF000000u) >> 24));
lst.Add((byte)((flag & 0x00FF0000u) >> 16));
lst.Add((byte)((flag & 0x0000FF00u) >> 8));
lst.Add((byte)(flag & 0x000000FFu));
lst.AddRange(chks);
return;
}
if (compr == 3)
{
for (int i = 0; i < 32; i++)
{
byte chunk = 0;
for (int j = 0; j < 8; j++)
{
if (Layer1[8 * i + j, y])
chunk |= (byte)(1 << j);
}
lst.Add(chunk);
}
return;
}
}
private void L2PutLine(List<byte> lst, int y)
{
int compr = (int)Layer2.LinesEncoding(y);
if (compr == 0) return;
if (compr == 1)
{
var chks = new List<byte>();
uint flag = 0;
for (int i = 0; i < 32; i++)
{
byte chunk = 0;
for (int j = 0; j < 8; j++)
if (Layer2[8 * i + j, y])
chunk |= (byte)(1 << j);
if (chunk != 0x00)
{
flag |= (1u << (31 - i));
chks.Add(chunk);
}
}
lst.Add((byte)((flag & 0xFF000000u) >> 24));
lst.Add((byte)((flag & 0x00FF0000u) >> 16));
lst.Add((byte)((flag & 0x0000FF00u) >> 8));
lst.Add((byte)(flag & 0x000000FFu));
lst.AddRange(chks);
return;
}
if (compr == 2)
{
var chks = new List<byte>();
uint flag = 0;
for (int i = 0; i < 32; i++)
{
byte chunk = 0;
for (int j = 0; j < 8; j++)
if (Layer2[8 * i + j, y])
chunk |= (byte)(1 << j);
if (chunk != 0xFF)
{
flag |= (1u << (31 - i));
chks.Add(chunk);
}
}
lst.Add((byte)((flag & 0xFF000000u) >> 24));
lst.Add((byte)((flag & 0x00FF0000u) >> 16));
lst.Add((byte)((flag & 0x0000FF00u) >> 8));
lst.Add((byte)(flag & 0x000000FFu));
lst.AddRange(chks);
return;
}
if (compr == 3)
{
for (int i = 0; i < 32; i++)
{
byte chunk = 0;
for (int j = 0; j < 8; j++)
{
if (Layer2[8 * i + j, y])
chunk |= (byte)(1 << j);
}
lst.Add(chunk);
}
return;
}
}
}
}