-
Notifications
You must be signed in to change notification settings - Fork 0
/
LZW.cs
184 lines (156 loc) · 3.65 KB
/
LZW.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
using System;
using System.Collections.Generic;
using System.IO;
namespace SylverInk
{
public partial class LZW
{
private string? C = string.Empty;
private List<byte> BitStream { get; } = [];
private Dictionary<string, uint> Codes { get; } = [];
private Stream? FileStream;
private List<byte> Incoming { get; } = [];
private int MaxRange { get; } = 24;
private uint NextCode = 258U;
private bool Open = false;
public List<byte> Outgoing { get; } = [];
private Dictionary<uint, string> Packets { get; } = [];
private int Range = 9;
private string W = string.Empty;
private bool Writing = false;
public void Close()
{
if (Open && Writing)
{
if (!W.Equals(string.Empty))
WriteCode(Codes[W]);
WriteCode(257U);
WriteCode(0U);
byte[] bits = [.. BitStream];
var bitSize = bits.Length;
byte b = 0;
int j = 1;
for (int i = 0; i < bitSize; i++)
{
b += (byte)(bits[i] << (8 - j));
if (j++ == 8)
{
j = 1;
Outgoing.Add(b);
b = 0;
}
}
}
BitStream.Clear();
C = string.Empty;
Codes.Clear();
Open = false;
Packets.Clear();
W = string.Empty;
Writing = false;
}
public void Compress(byte[] data)
{
for (int i = 0; i < data.Length; i++)
{
C = FromByte(data[i]);
string wc = W + C;
if (!Codes.TryAdd(wc, NextCode))
W = wc;
else
{
var entry = Codes[W];
WriteCode(entry);
NextCode++;
W = C;
UpdateRange(NextCode);
}
}
}
public byte[] Decompress(int byteCount = 1)
{
if (W.Equals(string.Empty))
{
var k = ReadCode();
if (k != 257U)
{
W = Packets[k];
for (int i = 0; i < W.Length; i++)
Incoming.Add((byte)W[i]);
}
}
while (Incoming.Count == 0 || Incoming.Count < byteCount)
{
var k = ReadCode();
if (k == 257U)
break;
if (!Packets.TryGetValue(k, out C))
C = $"{W}{W[0]}";
for (int i = 0; i < C.Length; i++)
Incoming.Add((byte)C[i]);
Packets.Add(NextCode, $"{W}{C[0]}");
NextCode++;
W = C;
UpdateRange(NextCode + 1);
}
var limit = Math.Min(byteCount, Incoming.Count);
var range = Incoming[..limit];
Incoming.RemoveRange(0, limit);
return [.. range];
}
private static string FromByte(byte c) => $"{(char)c}";
private static string FromChar(char c) => $"{c}";
public void Init(Stream? _fileStream = null, bool _writing = false)
{
FileStream = _fileStream ?? FileStream;
Open = true;
Writing = _writing;
InitDictionary();
}
private void InitDictionary()
{
Codes.Clear();
NextCode = 258U;
Packets.Clear();
Range = 9;
for (char i = (char)0; i < 256; i++)
{
Codes[FromChar(i)] = i;
Packets[i] = FromChar(i);
}
}
private uint ReadCode()
{
uint code = 0;
while (BitStream.Count < Range)
{
var n = FileStream?.ReadByte();
if (n == -1)
return 257U;
byte b = (byte)(n ?? 0);
for (int i = 1; i <= 8; i++)
BitStream.Add((byte)((b >> (8 - i)) & 1));
}
for (int i = 1; i <= Range; i++)
code += (uint)(BitStream[i - 1] << (Range - i));
BitStream.RemoveRange(0, Range);
return code;
}
private void UpdateRange(uint lastCode)
{
for (int i = Range; i <= MaxRange; i++)
{
if (lastCode < (uint)Math.Pow(2, i))
break;
Range = i + 1;
}
if (lastCode >= (uint)Math.Pow(2, MaxRange + 1))
throw new ApplicationException("Serialized database has exceeded the maximum capacity for restricted LZW compression.", new IndexOutOfRangeException());
}
private void WriteCode(uint code)
{
for (int i = 1; i <= Range; i++)
BitStream.Add((byte)((code >> (Range - i)) & 1));
}
}
}