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
|
/*
Copyright 2009 Red Hat, Inc. and/or its affiliates.
This program is licensed to you under the GNU General Public License,
version 2 or (at your option) any later version published by the Free
Software Foundation. See the file COPYING for details.
There is NO WARRANTY for this software, not even the implied
warranties of MERCHANTABILITY, NONINFRINGEMENT, or FITNESS FOR A
PARTICULAR PURPOSE.
This file incorporates work covered by the following copyright and
permission notice:
Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// External defines: PLT, RGBX/PLTXX/ALPHA, TO_RGB32.
// If PLT4/1 and TO_RGB32 are defined, we need CAST_PLT_DISTANCE (because then the number of
// pixels differ from the units used in the compression)
/*
For each output pixel type the following macros are defined:
OUT_PIXEL - the output pixel type
COPY_PIXEL(p, out) - assignes the pixel to the place pointed by out and increases
out. Used in RLE. Need special handling because in alpha we
copy only the pad byte.
COPY_REF_PIXEL(ref, out) - copies the pixel pointed by ref to the pixel pointed by out.
Increases ref and out.
COPY_COMP_PIXEL(encoder, out) - copies pixel from the compressed buffer to the decompressed
buffer. Increases out.
*/
#if !defined(LZ_RGB_ALPHA)
#define COPY_PIXEL(p, out) (*out++ = p)
#define COPY_REF_PIXEL(ref, out) (*out++ = *ref++)
#endif
// decompressing plt to plt
#ifdef LZ_PLT
#ifndef TO_RGB32
#define OUT_PIXEL one_byte_pixel_t
#define FNAME(name) lz_plt_##name
#define COPY_COMP_PIXEL(encoder, out) {out->a = decode(encoder); out++;}
#else // TO_RGB32
#define OUT_PIXEL rgb32_pixel_t
#define COPY_PLT_ENTRY(ent, out) { \
(out)->b = ent; \
(out)->g = (ent >> 8); \
(out)->r = (ent >> 16); \
(out)->pad = 0; \
}
#ifdef PLT8
#define FNAME(name) lz_plt8_to_rgb32_##name
#define COPY_COMP_PIXEL(encoder, out) { \
uint32_t rgb = encoder->palette->ents[decode(encoder)]; \
COPY_PLT_ENTRY(rgb, out); \
out++;}
#elif defined(PLT4_BE)
#define FNAME(name) lz_plt4_be_to_rgb32_##name
#define COPY_COMP_PIXEL(encoder, out){ \
uint8_t byte = decode(encoder); \
uint32_t rgb = encoder->palette->ents[((byte >> 4) & 0x0f) % (encoder->palette->num_ents)]; \
COPY_PLT_ENTRY(rgb, out); \
out++; \
rgb = encoder->palette->ents[(byte & 0x0f) % (encoder->palette->num_ents)]; \
COPY_PLT_ENTRY(rgb, out); \
out++; \
}
#define CAST_PLT_DISTANCE(dist) (dist*2)
#elif defined(PLT4_LE)
#define FNAME(name) lz_plt4_le_to_rgb32_##name
#define COPY_COMP_PIXEL(encoder, out){ \
uint8_t byte = decode(encoder); \
uint32_t rgb = encoder->palette->ents[(byte & 0x0f) % (encoder->palette->num_ents)]; \
COPY_PLT_ENTRY(rgb, out); \
out++; \
rgb = encoder->palette->ents[((byte >> 4) & 0x0f) % (encoder->palette->num_ents)]; \
COPY_PLT_ENTRY(rgb, out); \
out++; \
}
#define CAST_PLT_DISTANCE(dist) (dist*2)
#elif defined(PLT1_BE) // TODO store palette entries for direct access
#define FNAME(name) lz_plt1_be_to_rgb32_##name
#define COPY_COMP_PIXEL(encoder, out){ \
uint8_t byte = decode(encoder); \
int i; \
uint32_t fore = encoder->palette->ents[1]; \
uint32_t back = encoder->palette->ents[0]; \
for (i = 7; i >= 0; i--) \
{ \
if ((byte >> i) & 1) { \
COPY_PLT_ENTRY(fore, out); \
} else { \
COPY_PLT_ENTRY(back, out); \
} \
out++; \
} \
}
#define CAST_PLT_DISTANCE(dist) (dist*8)
#elif defined(PLT1_LE)
#define FNAME(name) lz_plt1_le_to_rgb32_##name
#define COPY_COMP_PIXEL(encoder, out){ \
uint8_t byte = decode(encoder); \
int i; \
uint32_t fore = encoder->palette->ents[1]; \
uint32_t back = encoder->palette->ents[0]; \
for (i = 0; i < 8; i++) \
{ \
if ((byte >> i) & 1) { \
COPY_PLT_ENTRY(fore, out); \
} else { \
COPY_PLT_ENTRY(back, out); \
} \
out++; \
} \
}
#define CAST_PLT_DISTANCE(dist) (dist*8)
#endif // PLT Type
#endif // TO_RGB32
#endif
#ifdef LZ_RGB16
#ifndef TO_RGB32
#define OUT_PIXEL rgb16_pixel_t
#define FNAME(name) lz_rgb16_##name
#define COPY_COMP_PIXEL(e, out) {*out = ((decode(e) << 8) | decode(e)); out++;}
#else
#define OUT_PIXEL rgb32_pixel_t
#define FNAME(name) lz_rgb16_to_rgb32_##name
#define COPY_COMP_PIXEL(e, out) { \
out->r = decode(e); \
out->b = decode(e); \
out->g = (((out->r) << 6) | ((out->b) >> 2)) & ~0x07; \
out->g |= (out->g >> 5); \
out->r = ((out->r << 1) & ~0x07)| ((out->r >> 4) & 0x07); \
out->b = (out->b << 3) | ((out->b >> 2) & 0x07); \
out->pad = 0; \
out++; \
}
#endif
#endif
#ifdef LZ_RGB24
#define OUT_PIXEL rgb24_pixel_t
#define FNAME(name) lz_rgb24_##name
#define COPY_COMP_PIXEL(e, out) {out->b = decode(e); out->g = decode(e); out->r = decode(e); out++;}
#endif
#ifdef LZ_RGB32
#define OUT_PIXEL rgb32_pixel_t
#define FNAME(name) lz_rgb32_##name
#define COPY_COMP_PIXEL(e, out) { \
out->b = decode(e); \
out->g = decode(e); \
out->r = decode(e); \
out->pad = 0; \
out++; \
}
#endif
#ifdef LZ_RGB_ALPHA
#define OUT_PIXEL rgb32_pixel_t
#define FNAME(name) lz_rgb_alpha_##name
#define COPY_PIXEL(p, out) {out->pad = p.pad; out++;}
#define COPY_REF_PIXEL(ref, out) {out->pad = ref->pad; out++; ref++;}
#define COPY_COMP_PIXEL(e, out) {out->pad = decode(e); out++;}
#endif
// return num of bytes in out_buf
static size_t FNAME(decompress)(Encoder *encoder, OUT_PIXEL *out_buf, int size)
{
OUT_PIXEL *op = out_buf;
OUT_PIXEL *op_limit = out_buf + size;
uint32_t ctrl = decode(encoder);
int loop = TRUE;
do {
const OUT_PIXEL *ref = op;
uint32_t len = ctrl >> 5;
uint32_t ofs = (ctrl & 31) << 8; // 5 MSb of distance
if (ctrl >= MAX_COPY) { // reference (dictionary/RLE)
/* retrieving the reference and the match length */
uint8_t code;
len--;
//ref -= ofs;
if (len == 7 - 1) { // match length is bigger than 7
do {
code = decode(encoder);
len += code;
} while (code == 255); // remaining of len
}
code = decode(encoder);
ofs += code;
/* match from 16-bit distance */
if (LZ_UNEXPECT_CONDITIONAL(code == 255)) {
if (LZ_EXPECT_CONDITIONAL((ofs - code) == (31 << 8))) {
ofs = decode(encoder) << 8;
ofs += decode(encoder);
ofs += MAX_DISTANCE;
}
}
#if defined(LZ_PLT) || defined(LZ_RGB_ALPHA)
len += 3; // length is biased by 2 + 1 (fixing bias)
#elif defined(LZ_RGB16)
len += 2; // length is biased by 1 + 1 (fixing bias)
#else
len += 1;
#endif
ofs += 1; // offset is biased by 1 (fixing bias)
#if defined(TO_RGB32)
#if defined(PLT4_BE) || defined(PLT4_LE) || defined(PLT1_BE) || defined(PLT1_LE)
ofs = CAST_PLT_DISTANCE(ofs);
len = CAST_PLT_DISTANCE(len);
#endif
#endif
ref -= ofs;
ASSERT(encoder->usr, op + len <= op_limit);
ASSERT(encoder->usr, ref + len <= op_limit);
ASSERT(encoder->usr, ref >= out_buf);
// TODO: optimize by not calling loop at least 3 times when not PLT_TO_RGB32 (len is
// always >=3). in PLT_TO_RGB32 len >= 3*number_of_pixels_per_byte
/* copying the match*/
if (ref == (op - 1)) { // run // TODO: this will never be called in PLT4/1_TO_RGB
// because the number of pixel copied is larger
// then one...
/* optimize copy for a run */
OUT_PIXEL b = *ref;
for (; len; --len) {
COPY_PIXEL(b, op);
ASSERT(encoder->usr, op <= op_limit);
}
} else {
for (; len; --len) {
COPY_REF_PIXEL(ref, op);
ASSERT(encoder->usr, op <= op_limit);
}
}
} else { // copy
ctrl++; // copy count is biased by 1
#if defined(TO_RGB32) && (defined(PLT4_BE) || defined(PLT4_LE) || defined(PLT1_BE) || \
defined(PLT1_LE))
ASSERT(encoder->usr, op + CAST_PLT_DISTANCE(ctrl) <= op_limit);
#else
ASSERT(encoder->usr, op + ctrl <= op_limit);
#endif
COPY_COMP_PIXEL(encoder, op);
ASSERT(encoder->usr, op <= op_limit);
for (--ctrl; ctrl; ctrl--) {
COPY_COMP_PIXEL(encoder, op);
ASSERT(encoder->usr, op <= op_limit);
}
}
if (LZ_EXPECT_CONDITIONAL(op < op_limit)) {
ctrl = decode(encoder);
} else {
loop = FALSE;
}
} while (LZ_EXPECT_CONDITIONAL(loop));
return (op - out_buf);
}
#undef LZ_PLT
#undef PLT8
#undef PLT4_BE
#undef PLT4_LE
#undef PLT1_BE
#undef PLT1_LE
#undef LZ_RGB16
#undef LZ_RGB24
#undef LZ_RGB32
#undef LZ_RGB_ALPHA
#undef TO_RGB32
#undef OUT_PIXEL
#undef FNAME
#undef COPY_PIXEL
#undef COPY_REF_PIXEL
#undef COPY_COMP_PIXEL
#undef COPY_PLT_ENTRY
#undef CAST_PLT_DISTANCE
|