-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlzw.hpp
177 lines (168 loc) · 4.78 KB
/
lzw.hpp
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
#pragma once
#include "Filter.hpp"
#define LZW_RESET_CODE 256
#define LZW_EOF_CODE 257
#include "LZWDictionary.hpp"
class LZWFilter : Filter {
public:
void encode(File *in, File *out, uint64_t /*size*/, int /*info*/, int & /*headerSize*/) override {
LZWDictionary dic;
int parent = -1;
int code = 0;
int buffer = 0;
int bitsPerCode = 9;
int bitsUsed = 0;
bool done = false;
while( !done ) {
buffer = in->getchar();
if( buffer < 0 ) {
return;// 0;
}
for( int j = 0; j < 8; j++ ) {
code += code + ((buffer >> (7 - j)) & 1), bitsUsed++;
if( bitsUsed >= bitsPerCode ) {
if( code == LZW_EOF_CODE ) {
done = true;
break;
}
if( code == LZW_RESET_CODE ) {
dic.reset();
parent = -1;
bitsPerCode = 9;
} else {
if( code < dic.index ) {
if( parent != -1 ) {
dic.addEntry(parent, dic.dumpEntry(out, code));
} else {
out->putChar(code);
}
} else if( code == dic.index ) {
int a = dic.dumpEntry(out, parent);
out->putChar(a);
dic.addEntry(parent, a);
} else {
return;// 0;
}
parent = code;
}
bitsUsed = 0;
code = 0;
if((1u << bitsPerCode) == dic.index + 1 && dic.index < 4096 ) {
bitsPerCode++;
}
}
}
}
// return 1;
}
uint64_t decode(File * /*in*/, File * /*out*/, FMode /*fMode*/, uint64_t /*size*/, uint64_t & /*diffFound*/) override {
return 0;
}
};
static int encodeLzw(File *in, File *out, uint64_t size, int &headerSize) {
LZWDictionary dic;
int parent = -1;
int code = 0;
int buffer = 0;
int bitsPerCode = 9;
int bitsUsed = 0;
bool done = false;
while( !done ) {
buffer = in->getchar();
if( buffer < 0 ) {
return 0;
}
for( int j = 0; j < 8; j++ ) {
code += code + ((buffer >> (7 - j)) & 1), bitsUsed++;
if( bitsUsed >= bitsPerCode ) {
if( code == LZW_EOF_CODE ) {
done = true;
break;
}
if( code == LZW_RESET_CODE ) {
dic.reset();
parent = -1;
bitsPerCode = 9;
} else {
if( code < dic.index ) {
if( parent != -1 ) {
dic.addEntry(parent, dic.dumpEntry(out, code));
} else {
out->putChar(code);
}
} else if( code == dic.index ) {
int a = dic.dumpEntry(out, parent);
out->putChar(a);
dic.addEntry(parent, a);
} else {
return 0;
}
parent = code;
}
bitsUsed = 0;
code = 0;
if((1 << bitsPerCode) == dic.index + 1 && dic.index < 4096 ) {
bitsPerCode++;
}
}
}
}
return 1;
}
static inline void writeCode(File *f, const FMode mode, int *buffer, uint64_t *pos, int *bitsUsed, const int bitsPerCode, const int code,
uint64_t *diffFound) {
*buffer <<= bitsPerCode;
*buffer |= code;
(*bitsUsed) += bitsPerCode;
while((*bitsUsed) > 7 ) {
const uint8_t b = *buffer >> (*bitsUsed -= 8);
(*pos)++;
if( mode == FMode::FDECOMPRESS ) {
f->putChar(b);
} else if( mode == FMode::FCOMPARE && b != f->getchar()) {
*diffFound = *pos;
}
}
}
static uint64_t decodeLzw(File *in, File *out, FMode mode, uint64_t &diffFound) {
LZWDictionary dic;
uint64_t pos = 0;
int parent = -1;
int code = 0;
int buffer = 0;
int bitsPerCode = 9;
int bitsUsed = 0;
writeCode(out, mode, &buffer, &pos, &bitsUsed, bitsPerCode, LZW_RESET_CODE, &diffFound);
while((code = in->getchar()) >= 0 && diffFound == 0 ) {
int index = dic.findEntry(parent, code);
if( index < 0 ) { // entry not found
writeCode(out, mode, &buffer, &pos, &bitsUsed, bitsPerCode, parent, &diffFound);
if( dic.index > 4092 ) {
writeCode(out, mode, &buffer, &pos, &bitsUsed, bitsPerCode, LZW_RESET_CODE, &diffFound);
dic.reset();
bitsPerCode = 9;
} else {
dic.addEntry(parent, code, index);
if( dic.index >= (1 << bitsPerCode)) {
bitsPerCode++;
}
}
parent = code;
} else {
parent = index;
}
}
if( parent >= 0 ) {
writeCode(out, mode, &buffer, &pos, &bitsUsed, bitsPerCode, parent, &diffFound);
}
writeCode(out, mode, &buffer, &pos, &bitsUsed, bitsPerCode, LZW_EOF_CODE, &diffFound);
if( bitsUsed > 0 ) { // flush buffer
pos++;
if( mode == FMode::FDECOMPRESS ) {
out->putChar(uint8_t(buffer));
} else if( mode == FMode::FCOMPARE && uint8_t(buffer) != out->getchar()) {
diffFound = pos;
}
}
return pos;
}