-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRLE.cpp
More file actions
355 lines (301 loc) · 7.9 KB
/
RLE.cpp
File metadata and controls
355 lines (301 loc) · 7.9 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
Copyright 2015 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
A local copy of the license and additional notices are located with the
source distribution at:
http://github.com/Esri/lerc/
Contributors: Thomas Maurer
*/
#include "Defines.h"
#include "RLE.h"
#include <cstring>
USING_NAMESPACE_LERC
// -------------------------------------------------------------------------- ;
size_t RLE::computeNumBytesRLE(const Byte* arr, size_t numBytes) const
{
if (arr == nullptr || numBytes == 0)
return 0;
const Byte* ptr = arr;
size_t sum = 0;
size_t cntOdd = 0;
size_t cntEven = 0;
size_t cntTotal = 0;
bool bOdd = true;
while (cntTotal < numBytes - 1)
{
if (*ptr != *(ptr + 1))
{
if (bOdd)
{
cntOdd++;
}
else // switch to odd mode
{
sum += 2 + 1;
bOdd = true;
cntOdd = 0;
cntEven = 0;
}
}
else
{
if (!bOdd)
{
cntEven++;
}
else
{
// count if we have enough equal bytes so it is worth switching to even
bool foundEnough = false;
if (cntTotal + m_minNumEven < numBytes)
{
int i = 1;
while (i < m_minNumEven && ptr[i] == ptr[0]) i++;
foundEnough = i < m_minNumEven ? false : true;
}
if (!foundEnough) // stay in odd mode
{
cntOdd++;
}
else // switch to even mode
{
if (cntOdd > 0)
sum += 2 + cntOdd;
bOdd = false;
cntOdd = 0;
cntEven = 0;
cntEven++;
}
}
}
ptr++;
cntTotal++;
if (cntOdd == 32767) // prevent short counters from overflow
{
sum += 2 + 32767;
cntOdd = 0;
}
if (cntEven == 32767)
{
sum += 2 + 1;
cntEven = 0;
}
}
// don't forget the last byte
if (bOdd)
{
cntOdd++;
sum += 2 + cntOdd;
}
else
{
sum += 2 + 1;
}
return sum + 2; // EOF short
}
// -------------------------------------------------------------------------- ;
bool RLE::compress(const Byte* arr, size_t numBytes,
Byte** arrRLE, size_t& numBytesRLE, bool verify) const
{
if (arr == nullptr || numBytes == 0)
return false;
numBytesRLE = computeNumBytesRLE(arr, numBytes);
*arrRLE = new Byte[numBytesRLE];
if (!*arrRLE)
return false;
const Byte* srcPtr = arr;
Byte* cntPtr = *arrRLE;
Byte* dstPtr = cntPtr + 2;
//size_t sum = 0;
size_t cntOdd = 0;
size_t cntEven = 0;
size_t cntTotal = 0;
bool bOdd = true;
while (cntTotal < numBytes - 1)
{
if (*srcPtr != *(srcPtr + 1))
{
*dstPtr++ = *srcPtr;
if (bOdd)
{
cntOdd++;
}
else // switch to odd mode
{
cntEven++;
writeCount(-(short)cntEven, &cntPtr, &dstPtr); // - sign for even cnts
//sum += 2 + 1;
bOdd = true;
cntOdd = 0;
cntEven = 0;
}
}
else
{
if (!bOdd)
{
cntEven++;
}
else
{
// count if we have enough equal bytes so it is worth switching to even
bool foundEnough = false;
if (cntTotal + m_minNumEven < numBytes)
{
int i = 1;
while (i < m_minNumEven && srcPtr[i] == srcPtr[0]) i++;
foundEnough = i < m_minNumEven ? false : true;
}
if (!foundEnough) // stay in odd mode
{
*dstPtr++ = *srcPtr;
cntOdd++;
}
else // switch to even mode
{
if (cntOdd > 0)
{
writeCount((short)cntOdd, &cntPtr, &dstPtr); // + sign for odd cnts
//sum += 2 + cntOdd;
}
bOdd = false;
cntOdd = 0;
cntEven = 0;
cntEven++;
}
}
}
if (cntOdd == 32767) // prevent short counters from overflow
{
writeCount((short)cntOdd, &cntPtr, &dstPtr);
//sum += 2 + 32767;
cntOdd = 0;
}
if (cntEven == 32767)
{
*dstPtr++ = *srcPtr;
writeCount(-(short)cntEven, &cntPtr, &dstPtr);
//sum += 2 + 1;
cntEven = 0;
}
srcPtr++;
cntTotal++;
}
// don't forget the last byte
*dstPtr++ = *srcPtr;
if (bOdd)
{
cntOdd++;
writeCount((short)cntOdd, &cntPtr, &dstPtr);
//sum += 2 + cntOdd;
}
else
{
cntEven++;
writeCount(-(short)cntEven, &cntPtr, &dstPtr);
//sum += 2 + 1;
}
writeCount(-32768, &cntPtr, &dstPtr); // write end of stream symbol
//sum += 2;
if (verify)
{
Byte* arr2 = nullptr;
size_t numBytes2 = 0;
if (!decompress(*arrRLE, numBytesRLE, &arr2, numBytes2) || numBytes2 != numBytes)
{
delete[] arr2;
return false;
}
int nCheck = memcmp(arr, arr2, numBytes);
delete[] arr2;
if (nCheck != 0)
return false;
}
return true;
}
// -------------------------------------------------------------------------- ;
bool RLE::decompress(const Byte* arrRLE, size_t nBytesRemainingIn, Byte** arr, size_t& numBytes)
{
if (!arrRLE || nBytesRemainingIn < 2)
return false;
// first count the encoded bytes
const Byte* srcPtr = arrRLE;
size_t nBytesRemaining = nBytesRemainingIn - 2;
size_t sum = 0;
short cnt = readCount(&srcPtr);
while (cnt != -32768)
{
sum += cnt < 0 ? -cnt : cnt;
size_t n = cnt > 0 ? cnt : 1;
if (nBytesRemaining < n + 2)
return false;
srcPtr += n;
cnt = readCount(&srcPtr);
nBytesRemaining -= n + 2;
}
numBytes = sum;
if (numBytes == 0)
{
*arr = nullptr;
return false;
}
*arr = new Byte[numBytes];
if (!*arr)
return false;
return decompress(arrRLE, nBytesRemainingIn, *arr, numBytes);
}
// -------------------------------------------------------------------------- ;
bool RLE::decompress(const Byte* arrRLE, size_t nBytesRemaining, Byte* arr, size_t arrSize)
{
if (!arrRLE || !arr || nBytesRemaining < 2)
return false;
const Byte* srcPtr = arrRLE;
size_t arrIdx = 0;
nBytesRemaining -= 2;
short cnt = readCount(&srcPtr);
while (cnt != -32768)
{
int i = (cnt <= 0) ? -cnt : cnt;
size_t m = (cnt <= 0) ? 1 : (size_t)i; // <= not < to fail gracefully in case of corrupted blob for old version <= 2 which had no checksum
if (nBytesRemaining < m + 2 || arrIdx + i > arrSize)
return false;
if (cnt > 0)
{
while (i--) arr[arrIdx++] = *srcPtr++;
}
else
{
Byte b = *srcPtr++;
while (i--) arr[arrIdx++] = b;
}
nBytesRemaining -= m + 2;
cnt = readCount(&srcPtr);
}
return true;
}
// -------------------------------------------------------------------------- ;
void RLE::writeCount(short cnt, Byte** ppCnt, Byte** ppDst)
{
SWAP_2(cnt); // write short's in little endian byte order, always
memcpy(*ppCnt, &cnt, sizeof(short));
*ppCnt = *ppDst;
*ppDst += 2;
}
// -------------------------------------------------------------------------- ;
short RLE::readCount(const Byte** ppCnt)
{
short cnt;
memcpy(&cnt, *ppCnt, sizeof(short));
SWAP_2(cnt);
*ppCnt += 2;
return cnt;
}
// -------------------------------------------------------------------------- ;