summaryrefslogtreecommitdiff
path: root/pcl/rtrstcmp.c
blob: d882684ae272f404b4cd713fd0296049102be643 (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
/* Portions Copyright (C) 2001 artofcode LLC.
   Portions Copyright (C) 1996, 2001 Artifex Software Inc.
   Portions Copyright (C) 1988, 2000 Aladdin Enterprises.
   This software is based in part on the work of the Independent JPEG Group.
   All Rights Reserved.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/ or
   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
   San Rafael, CA  94903, (415)492-9861, for further information. */
/*$Id$ */

/* rtrstcmp.c - raster decompression routines */

#include "string_.h"
#include "pcstate.h"
#include "rtrstcmp.h"

/*
 * Each of the decompression methods has the same structure. The operands are
 * are the seed row to be filled in and the buffer from which it is to be
 * filled. A size is provided for the latter.
 *
 * Adpative compression (mode 5) is not handled at this level, though it will
 * make use of these routines for decompressing individual raster rows.
 */

/*
 * Uncompressed data.
 */
  static void
uncompress_0(
    pcl_seed_row_t *    pout,
    const byte *        pin,
    int                 in_size
)
{
    int                 nbytes = (in_size > pout->size ? pout->size : in_size);

    memcpy(pout->pdata, pin, nbytes);
    if (!pout->is_blank)
        memset(pout->pdata + nbytes, 0, pout->size - nbytes);
    pout->is_blank = (in_size == 0);
}

/*
 * Run-length compression.
 */
  static void
uncompress_1(
    pcl_seed_row_t *    pout,
    const byte *        pin,
    int                 in_size
)
{
    int                 i = in_size / 2;
    byte *              pb = pout->pdata;
    byte *              plim = pb + pout->size;

    while (i-- > 0) {
        int     cnt = *pin++ + 1;
        byte    val = *pin++;

        if (cnt > plim - pb)
            cnt = plim - pb;
        while (cnt-- > 0)
            *pb++ = val;
    }
    if (!pout->is_blank)
        memset(pb, 0, plim - pb);
    pout->is_blank = (in_size == 0);
}

/*
 * TIFF "Packbits" compression.
 */
  static void
uncompress_2(
    pcl_seed_row_t *    pout,
    const byte *        pin,
    int                 in_size
)
{
    int                 i = in_size;
    byte *              pb = pout->pdata;
    byte *              plim = pb + pout->size;

    while (i-- > 0) {
        int             cntrl = *pin++;

        if (cntrl < 128) {
            uint            cnt = min(cntrl + 1, i);
            const byte *    ptmp = pin;

            i -= cnt;
            pin += cnt;
            if (cnt > plim - pb)
                cnt = plim - pb;
            while (cnt-- > 0)
                *pb++ = *ptmp++;

        } else if ((cntrl > 128) && (i-- > 0)) {
            int     cnt = min(257 - cntrl, plim - pb);
            int     val = *pin++;

            while (cnt-- > 0)
                *pb++ = val;
        }
    }
    if (!pout->is_blank)
        memset(pb, 0, plim - pb);
    pout->is_blank = (in_size == 0);
}

/*
 * Delta row compression
 */
  static void
uncompress_3(
    pcl_seed_row_t *    pout,
    const byte *        pin,
    int                 in_size
)
{
    int                 i = in_size;
    byte *              pb = pout->pdata;
    byte *              plim = pb + pout->size;

    while (i-- > 0) {
        uint            val = *pin++;
        uint            cnt = (val >> 5) + 1;
        uint            offset = val & 0x1f;
        const byte *    ptmp = 0;

        if ((offset == 0x1f) && (i-- > 0)) {
            uint    add_offset;

            do 
                offset += (add_offset = *pin++);
            while ((add_offset == 0xff) && (i-- > 0));
        }

        if (cnt > i)
            cnt = i;
        i -= cnt;
        ptmp = pin;
        pin += cnt;
        if ((pb += offset) >= plim)
           break;
        if (cnt > plim - pb)
            cnt = plim - pb;
        while (cnt-- > 0)
            *pb++ = *ptmp++;
    }
    pout->is_blank = (pout->is_blank && (in_size == 0));
}

/*
 * Adpative compression (mode 5) is handled at a higher level.
 */

/*
 * Compression mode 9.
 *
 * HP's documentation of this command is not completely clear regarding the 
 * interpretation of the replacement byte count for the run-length compression
 * case. The interpretation used here, based on the documentation in the
 * "PCL 5 Comparison Guide", October 1996 edition, pp. 2.94-2.96, is that the
 * replacement byte count refers to the number of output bytes replaced, and
 * as many input bytes as required are read until at leas this many output
 * bytes have been replaced.
 */
  static void
uncompress_9(
    pcl_seed_row_t *    pout,
    const byte *        pin,
    int                 in_size
)
{
    int                 i = in_size;
    byte *              pb = pout->pdata;
    byte *              plim = pb + pout->size;


    while (i-- > 0) {
        uint            val = *pin++;
        uint            cnt = 0;
        uint            offset = 0;
        bool            more_cnt = false;
        bool            more_offset = false;
        bool            comp = ((val & 0x80) != 0);

        if (comp) {
            offset = (val >> 5) & 0x3;
            more_offset = (offset == 0x3);
            cnt = (val & 0x1f) + 1;
            more_cnt = (cnt == 0x20);
        } else {
            offset = (val >> 3) & 0xf;
            more_offset = (offset == 0xf);
            cnt = (val & 0x7) + 1;
            more_cnt = (cnt == 0x8);
        }

        while (more_offset && (i-- > 0)) {
            uint    extra = *pin++;

            more_offset = (extra == 0xff);
            offset += extra;
        }
        while (more_cnt && (i-- > 0)) {
            uint    extra = *pin++;

            more_cnt = (extra == 0xff);
            offset += extra;
        }

        if ((pb += offset) >= plim)
            break;
        if (comp) {
            uint    j = i / 2;

            while (j-- > 0) {
                uint    rep_cnt = *pin++;
                uint    rep_val = *pin++;

                if (rep_cnt > plim - pb)
                    rep_cnt = plim - pb;
                while (rep_cnt-- > 0)
                    *pb++ = rep_val;
            }
            i -= 2 * j;
            
        } else {
            if (cnt > i)
                cnt = i;
            i -= cnt;
            pin += cnt;
            if (cnt > plim - pb)
                cnt = plim - pb;
            while (cnt-- > 0)
                *pb++ = *pin++;
        }

    }
    pout->is_blank = (pout->is_blank && (in_size == 0));

}


void    (*const pcl_decomp_proc[9 + 1])( pcl_seed_row_t *   pout,
                                         const byte *       pin,
                                         int                in_size 
                                         ) = {
    uncompress_0,
    uncompress_1,
    uncompress_2,
    uncompress_3,
    0, 0, 0, 0, 0,  /* modes 4 and 6 - 8 unused; mode 5 handled separately */
    uncompress_9
};