-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
cmprrle.c
192 lines (175 loc) · 5.82 KB
/
cmprrle.c
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
/*
****************************************************************************
* -- GRASS Development Team --
*
* MODULE: GRASS gis library
* FILENAME: cmprrle.c
* AUTHOR(S): Markus Metz
* PURPOSE: To provide generic RLE for compressing and
* decompressing data. Its primary use is in
* the storage and reading of GRASS rasters.
*
* ALGORITHM: Run Length Encoding
* DATE CREATED: Dec 18 2015
* COPYRIGHT: (C) 2015 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (version 2 or greater). Read the file COPYING that
* comes with GRASS for details.
*
*****************************************************************************/
/********************************************************************
* int *
* G_rle_compress (src, srz_sz, dst, dst_sz) *
* int src_sz, dst_sz; *
* unsigned char *src, *dst; *
* ---------------------------------------------------------------- *
* This function compresses data with RLE. *
* It uses an all or nothing call. *
* If you need a continuous compression scheme, you'll have to code *
* your own. *
* *
* The function either returns the number of bytes of compressed *
* data in dst, or an error code. *
* *
* Errors include: *
* -1 -- Compression failed. *
* -2 -- dst is too small. *
* *
* ================================================================ *
* int *
* G_rle_expand (src, src_sz, dst, dst_sz) *
* int src_sz, dst_sz; *
* unsigned char *src, *dst; *
* ---------------------------------------------------------------- *
* This function decompresses data compressed with RLE. *
* It is equivalent to a single pass call to an external expansion *
* function. *
* If you need a continuous expansion scheme, you'll have to code *
* your own. *
* *
* The function returns the number of bytes expanded into 'dst' or *
* and error code. *
* *
* Errors include: *
* -1 -- Expansion failed. *
* *
********************************************************************
*/
#include <grass/config.h>
#include <grass/gis.h>
#include <grass/glocale.h>
int
G_rle_compress(unsigned char *src, int src_sz, unsigned char *dst,
int dst_sz)
{
int i, nbytes;
unsigned char prev_b;
int cnt;
/* Catch errors early */
if (src == NULL || dst == NULL)
return -1;
/* Don't do anything if src is empty or smaller than 4 bytes */
if (src_sz <= 3)
return 0;
/* modified RLE:
* unit is 1 byte, only sequences longer than 1 are encoded
* single occurrences don't have a following count
* multiple occurrences are twice in dst, followed by the count
* example:
* ABBCCC
* is encoded as
* ABB2CC3
*/
prev_b = src[0];
cnt = 1;
nbytes = 0;
for (i = 1; i < src_sz; i++) {
if (prev_b != src[i] || cnt == 255) {
/* write to dst */
if (cnt == 1) {
if (nbytes >= dst_sz)
return -2;
dst[nbytes++] = prev_b;
}
else {
/* cnt > 1 */
if (nbytes >= dst_sz - 2)
return -2;
dst[nbytes++] = prev_b;
dst[nbytes++] = prev_b;
dst[nbytes++] = (unsigned char) cnt;
}
cnt = 0;
}
prev_b = src[i];
cnt++;
}
/* write out the last sequence */
if (cnt == 1) {
if (nbytes >= dst_sz)
return -2;
dst[nbytes++] = prev_b;
}
else {
if (nbytes >= dst_sz - 2)
return -2;
dst[nbytes++] = prev_b;
dst[nbytes++] = prev_b;
dst[nbytes++] = (unsigned char) cnt;
}
return nbytes;
}
int
G_rle_expand(unsigned char *src, int src_sz, unsigned char *dst,
int dst_sz)
{
int i, j, nbytes, cnt;
unsigned char prev_b;
/* Catch errors early */
if (src == NULL || dst == NULL)
return -1;
/* Don't do anything if src is empty */
if (src_sz <= 0)
return 0;
/* RLE expand */
prev_b = src[0];
cnt = 1;
nbytes = 0;
i = 1;
while (i < src_sz) {
/* single occurrences don't have a following count
* multiple occurrences are twice in src, followed by the count */
if (cnt == 2) {
if (i >= src_sz)
return -1;
cnt = src[i];
if (nbytes + cnt > dst_sz)
return -1;
for (j = 0; j < cnt; j++) {
dst[nbytes++] = prev_b;
}
cnt = 0;
i++;
if (i >= src_sz)
return nbytes;
}
if (cnt == 1) {
if (prev_b != src[i]) {
if (nbytes + cnt > dst_sz)
return -1;
dst[nbytes++] = prev_b;
cnt = 0;
}
}
prev_b = src[i];
cnt++;
i++;
}
if (nbytes >= dst_sz)
return -1;
if (cnt == 1)
dst[nbytes++] = prev_b;
return nbytes;
}
/* vim: set softtabstop=4 shiftwidth=4 expandtab: */