-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackbits.cs
61 lines (58 loc) · 1.47 KB
/
packbits.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
public static byte[]? PackBitsEncode(byte[] decodedArray)
{
List<byte> encodedList = new();
byte prevDb = 128;
byte reps = 1;
byte cpy = 1;
List<byte> cpyList = new();
for (int i = 0; i < decodedArray.Length; i++)
{
if (decodedArray[i] == decodedArray[i + 1])
{
reps++;
continue;
}
if (reps >= 2)
{
var temp = decodedArray[i];
encodedList.Add((byte)(257 - reps));
encodedList.Add(decodedArray[i]);
reps = 1;
}
else
{
}
}
return encodedList.ToArray();
}
public static byte[] PackBitsDecode(byte[] encodedArray)
{
List<byte> decodedList = new();
int leap = 1;
for (int i = 0; i < encodedArray.Length; i += leap)
{
int convertedBytes = 0;
byte conv = encodedArray[i];
if (conv >= 0 && conv <= 127)
{
byte cpy = (byte)(conv + 1);
for (int j = 0; j < cpy; j++)
{
decodedList.Add(encodedArray[i + 1 + j]);
convertedBytes++;
}
leap = 1 + convertedBytes;
}
else if (conv >= 129 && conv <= 255)
{
byte rep = (byte)((256 - conv) + 1);
for (int j = 0; j < rep; j++)
{
decodedList.Add(encodedArray[i + 1]);
}
leap = 2;
}
else leap = 2;
}
return decodedList.ToArray();
}