-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrle.cpp
74 lines (69 loc) · 1.36 KB
/
rle.cpp
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
/*********************************************
* Run length encoding of zeroes
**********************************************/
#include "rle.hpp"
inline int RLE::getMSB(unsigned int v)
{
int msb = 0;
while (v)
{
v >>= 1;
msb++;
}
return msb;
}
/*
Encode 8-bit input into 16-bit output
Runs of symbol 0 get encoded as a binary permutation of symbols 0 and 1,
all other symbols greater than 0 are coded as sym + 1.
*/
void RLE::encode(unsigned char *in, unsigned short *out, int *len)
{
int in_p = *len;
int out_p = 0;
for(int i = 0; i < in_p;)
{
if(in[i] == 0)
{
int run = 1;
while (in[i] == in[i+run] && (i+run) < in_p)
run++;
i += run;
int L = run + 1;
int msb = getMSB(L) - 1;
while(msb--)
out[out_p++] = (L >> msb) & 1;
}
else
{
out[out_p++] = in[i++] + 1;
}
}
*len = out_p;
}
/*
Decode 16-bit input into 8-bit output
*/
void RLE::decode(unsigned short *in, unsigned char *out, int *len, int real_len)
{
int in_p = *len;
int out_p = 0;
for(int i = 0; i < in_p;)
{
if(in[i] > 1)
{
out[out_p++] = in[i++] - 1;
}
else
{
int rle = 1;
while (in[i] <= 1 && i < in_p)
rle = (rle << 1) | in[i++];
rle -= 1;
while(rle--)
out[out_p++] = 0;
}
}
if(out_p != real_len) Error("rle mismatch!");
*len = out_p;
}