-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkko.h
303 lines (268 loc) · 7.21 KB
/
kko.h
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
//
// Created by filip on 24.4.19.
//
#ifndef KKO_KKO_H
#define KKO_KKO_H
#include <fstream>
#include <iterator>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <getopt.h>
/**
* file header
*/
typedef struct __attribute__ ((__packed__)) header{
uint8_t codeType:1; //0 static 1 dynamic
uint8_t model:3; // 0 none ...
uint8_t padding:4;
uint32_t totalSize;
uint32_t dataOffset;
}Header;
/**
* class simulating tree in vector
*/
class TreeInfo{
public:
///Succesors
int left;
int right;
///predecessor
int parent;
};
/**
*
* class represent one node in tree
*/
class mapItem{
unsigned size;
public:
void setSize(unsigned int size);
/// number of occurence current value in histogram
unsigned long value;
/// value of current value , vector is used in treebuilding
std::vector<uint8_t > keys;
/// info about node
TreeInfo tree;
///binari code of current node
std::vector<bool> code;
std::vector<uint8_t > *getKey(); //return key if is leaf otherwise return null;
mapItem();
/// debug functions
void print();
void printCode();
void printHistogram();
/**
*
* @return length of binary code
*/
unsigned int codeSize();
};
/**
* class represented tree
*/
class BTree{
/**
*
* @return vector with leafs
*/
std::vector<std::vector<uint8_t> > getLeafs();
public:
/// whole tree store in vector
std::vector<mapItem> tree;
/// index of root node
int root;
///constructors
explicit BTree(std::vector<mapItem>);
BTree();
/**
*
* @return depth of tree
*/
unsigned int treeHeight();
/**
*
* @param to specificate node key
* @return length from root to node with value to
*/
unsigned int pathLen(std::vector<uint8_t> to);
/**
* iterate over all nodes of tree and generate binary codes
* @return
*/
bool generateCodeBook();
/**
* generate binary code for current node
* @param to
* @return binary code
*/
std::vector<bool> generateCode(std::vector<uint8_t> to);
///debug function
void printCodebook(std::vector<std::vector<bool>>);
/**
* sort tree default asc if flag = false desc
* @param flag
*/
void sortTreeByCodeLength(bool flag = true); //BIG side effect destruct connecting in tree !!!!
void sortTreeByKey(); //sort by first key value used for sorting leafs
/**
* canonize current tree
* @return canonized tree
*/
std::vector<mapItem> canonize();
/**
* reconstruc tree from canonized form
*/
void reconstructTree();
};
/// class represents functionality of application
class HTree{
std::vector<mapItem> histogram;
/// input file
std::vector<uint8_t > buffer;
BTree tree;
//output file
std::ofstream * outbin;
//flag if use model
bool model;
bool generate_histogram();
/**
* set keys in tree before sorting
* @return
*/
bool set_keys();
/**
* merge last two nodes in tree from histogram
* @param histogram
* @return merged node
*/
mapItem mergeLast2(std::vector<mapItem> &histogram); //merge and delete used items
/**
* insert new node into histogram
* @param item
* @param histogram
* @return
*/
bool insertItem(mapItem item, std::vector<mapItem> &histogram);
/**
* set up tree info in vector - simulate tree in vector
* @param tree
* @return
*/
bool connectTree(std::vector<mapItem> &tree);
void openStream(std::string fileName);
void writeToFile(void * data, unsigned int size);
void closeStream();
/**
* code whole input file and every call return one bit value
* @param dict hash table used to for coding
* @return one bit value
*/
inline int readBit(std::vector<std::vector<bool>> &dict);
/**
*
* @param buffer read one bit from buffer
* @param padding return number of nonused bits in last byte
* @return
*/
inline int readBit(std::vector<uint8_t> &buffer, unsigned int padding);
/**
* modification for adaptive reading
* @param dict
* @return
*/
inline int readAdaptiveBit(std::vector<std::vector<bool>> &dict);
/**
* function counting time to rebuild treee
* @param counter actual index of current value in input file
* @param pixelValue value of proccesing value
* @param histogramValue value of current histogram value
* @return true if rebuild is needed
*/
bool heuristic(unsigned counter, unsigned pixelValue,unsigned histogramValue);
public:
HTree(std::vector<uint8_t > &buffer);
void setModel(bool model);
/// Interface for main
void runStaticCompress(std::string outFile);
void runStaticDeCompress(std::string outFile);
void runAdaptiveCompress(std::string outFile, std::vector<uint8_t> buffer );
void runAdaptiveDeCompress(std::string outFile);
/**
* build tree from histogram
* @return
*/
bool buildTree();
Header generateHeader(unsigned padding, unsigned offset, unsigned size, unsigned mode);
/**
* generate lengths of tree leafs
* @return
*/
std::vector<uint8_t > generateTreeCoding();
/**
* code data from input
* @param cnt padding
* @return
*/
std::vector<uint8_t > codeData(unsigned &cnt);
/**
* code data byte after byte according to dict
* @param cnt
* @param dict
* @param last write last byte if not padded at 8 bits
* @return
*/
unsigned int codeAdaptiveData(unsigned &cnt,std::vector<std::vector<bool>> &dict, bool last);
/**
* preprocess data with model, use atributes
*/
void applyModel();
/**
* preprocess buffer with model
* @param buffer
* @return changed buffer by model
*/
std::vector<uint8_t > applyModel(std::vector<uint8_t> buffer);
/**
* reconstruct data after applying model in code time
* @param buffer
* @return reconstructed data
*/
std::vector<uint8_t >useModel(std::vector<uint8_t> buffer);
/**
* decode data from buffer and return original data
* @param buffer input
* @param padding count non-used bits in last byte
* @return
*/
std::vector<uint8_t > decodeData(std::vector<uint8_t> &buffer,unsigned int padding);
/**
* decode data from buffer and return original data
* @param buffer
* @param padding
* @param newHistogram vector for stor actual histogram use for rebuild tree
* @return
*/
std::vector<uint8_t > decodeAdaptiveData(std::vector<uint8_t> &buffer,unsigned int padding,std::vector<mapItem> &newHistogram);
///debug functions
void printHistogram();
void printTree();
};
/**
*
* @param v input free vector
* @param value for writing into vector
* @param size number of bits to write from value
* @return return v
*/
std::vector<bool> & initBoolVector(std::vector<bool> &v, unsigned int value);
/**
* chceck if part is subvector of base
* @param base
* @param part
* @return
*/
bool subvector(std::vector<uint8_t > &base, std::vector<uint8_t > &part);
#endif //KKO_KKO_H