summaryrefslogtreecommitdiffstats
path: root/client/glz_decode_tmpl.c
blob: 2c5d6a3c5322c8989ad6273e22ed7ce95760cf87 (plain)
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
/*
   Copyright (C) 2009 Red Hat, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// 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) glz_plt_##name
#define COPY_COMP_PIXEL(in, out) {(out)->a = *(in++); 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) glz_plt8_to_rgb32_##name
#define COPY_COMP_PIXEL(in, out, palette) {         \
    uint32_t rgb = palette->ents[*(in++)];          \
    COPY_PLT_ENTRY(rgb, out);                       \
    out++;                                          \
}
#elif defined(PLT4_BE)
#define FNAME(name) glz_plt4_be_to_rgb32_##name
#define COPY_COMP_PIXEL(in, out, palette){                                    \
    uint8_t byte = *(in++);                                                   \
    uint32_t rgb = palette->ents[((byte >> 4) & 0x0f) % (palette->num_ents)]; \
    COPY_PLT_ENTRY(rgb, out);                                                 \
    out++;                                                                    \
    rgb = palette->ents[(byte & 0x0f) % (palette->num_ents)];                 \
    COPY_PLT_ENTRY(rgb, out);                                                 \
    out++;                                                                    \
}
#define CAST_PLT_DISTANCE(dist) (dist*2)
#elif  defined(PLT4_LE)
#define FNAME(name) glz_plt4_le_to_rgb32_##name
#define COPY_COMP_PIXEL(in, out, palette){                                \
    uint8_t byte = *(in++);                                               \
    uint32_t rgb = palette->ents[(byte & 0x0f) % (palette->num_ents)];    \
    COPY_PLT_ENTRY(rgb, out);                                             \
    out++;                                                                \
    rgb = palette->ents[((byte >> 4) & 0x0f) % (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) glz_plt1_be_to_rgb32_##name
#define COPY_COMP_PIXEL(in, out, palette){                                \
    uint8_t byte = *(in++);                                               \
    int i;                                                                \
    uint32_t fore = palette->ents[1];                                     \
    uint32_t back = 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) glz_plt1_le_to_rgb32_##name
#define COPY_COMP_PIXEL(in, out, palette){                                \
    uint8_t byte = *(in++);                                               \
    int i;                                                                \
    uint32_t fore = palette->ents[1];                                     \
    uint32_t back = 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) glz_rgb16_##name
#define COPY_COMP_PIXEL(in, out) {*out = (*(in++)) << 8; *out |= *(in++); out++;}
#else
#define OUT_PIXEL rgb32_pixel_t
#define FNAME(name) glz_rgb16_to_rgb32_##name
#define COPY_COMP_PIXEL(in, out) {out->r = *(in++); out->b= *(in++);       \
    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) glz_rgb24_##name
#define COPY_COMP_PIXEL(in, out) {  \
    out->b = *(in++);               \
    out->g = *(in++);               \
    out->r = *(in++);               \
    out++;                          \
}
#endif

#ifdef LZ_RGB32
#define OUT_PIXEL rgb32_pixel_t
#define FNAME(name) glz_rgb32_##name
#define COPY_COMP_PIXEL(in, out) {  \
    out->b = *(in++);               \
    out->g = *(in++);               \
    out->r = *(in++);               \
    out->pad = 0;                   \
    out++;                          \
}
#endif

#ifdef LZ_RGB_ALPHA
#define OUT_PIXEL rgb32_pixel_t
#define FNAME(name) glz_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(in, out) {out->pad = *(in++); out++;}
#endif

// TODO: seperate into routines that decode to dist,len. and to a routine that
// actualy copies the data.

/* returns num of bytes read from in buf.
   size should be in PIXEL */
static size_t FNAME(decode)(GlzDecoderWindow &window, uint8_t* in_buf,
                            uint8_t *out_buf, int size,
                            DecodedImageWinId image_win_id, SpicePalette *plt,
                            GlzDecoderDebug &debug_calls)
{
    uint8_t      *ip = in_buf;
    OUT_PIXEL    *out_pix_buf = (OUT_PIXEL *)out_buf;
    OUT_PIXEL    *op = out_pix_buf;
    OUT_PIXEL    *op_limit = out_pix_buf + size;

    uint32_t ctrl = *(ip++);
    int loop = true;

    do {
        if (ctrl >= MAX_COPY) { // reference (dictionary/RLE)
            OUT_PIXEL *ref = op;
            uint32_t len = ctrl >> 5;
            uint8_t pixel_flag = (ctrl >> 4) & 0x01;
            uint32_t pixel_ofs = (ctrl & 0x0f);
            uint8_t image_flag;
            uint32_t image_dist;

            /* retrieving the referenced images, the offset of the first pixel,
               and the match length */

            uint8_t code;
            //len--; // TODO: why do we do this?

            if (len == 7) { // match length is bigger than 7
                do {
                    code = *(ip++);
                    len += code;
                } while (code == 255); // remaining of len
            }
            code = *(ip++);
            pixel_ofs += (code << 4);

            code = *(ip++);
            image_flag = (code >> 6) & 0x03;
            if (!pixel_flag) { // short pixel offset
                image_dist = code & 0x3f;
                for (int i = 0; i < image_flag; i++) {
                    code = *(ip++);
                    image_dist += (code << (6 + (8 * i)));
                }
            } else {
                pixel_flag = (code >> 5) & 0x01;
                pixel_ofs += (code & 0x1f) << 12;
                image_dist = 0;
                for (int i = 0; i < image_flag; i++) {
                    code = *(ip++);
                    image_dist += (code << 8 * i);
                }


                if (pixel_flag) { // very long pixel offset
                    code = *(ip++);
                    pixel_ofs += code << 17;
                }
            }

#if defined(LZ_PLT) || defined(LZ_RGB_ALPHA)
            len += 2; // length is biased by 2 (fixing bias)
#elif defined(LZ_RGB16)
            len += 1; // length is biased by 1  (fixing bias)
#endif
            if (!image_dist) {
                pixel_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)
            pixel_ofs = CAST_PLT_DISTANCE(pixel_ofs);
            len = CAST_PLT_DISTANCE(len);
#endif
#endif

            if (!image_dist) { // reference is inside the same image
                ref -= pixel_ofs;
                GLZ_ASSERT(debug_calls, ref + len <= op_limit);
                GLZ_ASSERT(debug_calls, ref >= out_pix_buf);
            } else {
                ref = (OUT_PIXEL *)window.get_ref_pixel(image_win_id, image_dist,
                                                        pixel_ofs);
            }

            GLZ_ASSERT(debug_calls, op + len <= op_limit);

            /* copying the match*/

            if (ref == (op - 1)) { // run (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);
                    GLZ_ASSERT(debug_calls, op <= op_limit);
                }
            } else {
                for (; len; --len) {
                    COPY_REF_PIXEL(ref, op);
                    GLZ_ASSERT(debug_calls, 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))
            GLZ_ASSERT(debug_calls, op + CAST_PLT_DISTANCE(ctrl) <= op_limit);
#else
            GLZ_ASSERT(debug_calls, op + ctrl <= op_limit);
#endif

#if defined(TO_RGB32) && defined(LZ_PLT)
            GLZ_ASSERT(debug_calls, plt);
            COPY_COMP_PIXEL(ip, op, plt);
#else
            COPY_COMP_PIXEL(ip, op);
#endif
            GLZ_ASSERT(debug_calls, op <= op_limit);

            for (--ctrl; ctrl; ctrl--) {
#if defined(TO_RGB32) && defined(LZ_PLT)
                GLZ_ASSERT(debug_calls, plt);
                COPY_COMP_PIXEL(ip, op, plt);
#else
                COPY_COMP_PIXEL(ip, op);
#endif
                GLZ_ASSERT(debug_calls, op <= op_limit);
            }
        } // END REF/COPY

        if (LZ_EXPECT_CONDITIONAL(op < op_limit)) {
            ctrl = *(ip++);
        } else {
            loop = false;
        }
    } while (LZ_EXPECT_CONDITIONAL(loop));

    return (ip - in_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