Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/devices/gdevpcl.c
Line
Count
Source
1
/* Copyright (C) 2001-2023 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Utilities for PCL printers */
18
#include "gdevprn.h"
19
#include "gdevpcl.h"
20
#include "math_.h"
21
22
/* ------ Get paper size ------ */
23
24
/* Get the paper size code, based on width and height. */
25
int
26
gdev_pcl_paper_size(gx_device * dev)
27
7.96k
{
28
7.96k
    float width_inches = dev->width / dev->x_pixels_per_inch;
29
7.96k
    float height_inches = dev->height / dev->y_pixels_per_inch;
30
    /* The initial value for height_difference, and for code below, is
31
       unnecessary and is here just to stop the compiler from
32
       complaining about a possible uninitialized variable usage. */
33
7.96k
    float width_difference = -1.0, height_difference = -1.0;
34
7.96k
    float new_width_difference, new_height_difference;
35
7.96k
    int code = PAPER_SIZE_LETTER;
36
37
7.96k
    if (dev->width > dev->height) {
38
        /* Landscape orientation, switch width and height to find paper size */
39
42
        width_inches = dev->height / dev->y_pixels_per_inch;
40
42
        height_inches = dev->width / dev->x_pixels_per_inch;
41
42
    }
42
43
    /* Since we're telling the printer when to eject and start a new
44
       page, the paper height doesn't matter a great deal, as long as
45
       we ensure that it's at least as high as we want our pages to
46
       be.  However, the paper width is important, because on printers
47
       which center the paper in the input tray, having the wrong
48
       width will cause the image to appear in the wrong place on the
49
       paper (perhaps even partially missing the paper completely).
50
       Therefore, we choose our paper size by finding one whose width
51
       and height are both equal to or greater than the desired width
52
       and height and whose width is the closest to what we want.  We
53
       only pay close attention to the height when the widths of two
54
       different paper sizes are equal.
55
56
       We use "foo - bar > -0.01" instead of "foo >= bar" to avoid
57
       minor floating point and rounding errors.
58
    */
59
7.96k
#define CHECK_PAPER_SIZE(w,h,c)             \
60
151k
    new_width_difference = w - width_inches;          \
61
151k
    new_height_difference = h - height_inches;          \
62
151k
    if ((new_width_difference > -0.01) && (new_height_difference > -0.01) && \
63
151k
        ((width_difference == -1.0) ||           \
64
64.0k
         (new_width_difference < width_difference) ||       \
65
64.0k
         ((new_width_difference == width_difference) &&       \
66
55.9k
          (new_height_difference < height_difference)))) {     \
67
8.12k
      width_difference = new_width_difference;          \
68
8.12k
      height_difference = new_height_difference;        \
69
8.12k
      code = c;                 \
70
8.12k
    }
71
72
7.96k
    CHECK_PAPER_SIZE( 7.25, 10.5 , PAPER_SIZE_EXECUTIVE);
73
7.96k
    CHECK_PAPER_SIZE( 8.5 , 11.0 , PAPER_SIZE_LETTER);
74
7.96k
    CHECK_PAPER_SIZE( 8.5 , 14.0 , PAPER_SIZE_LEGAL);
75
7.96k
    CHECK_PAPER_SIZE(11.0 , 17.0 , PAPER_SIZE_LEDGER);
76
7.96k
    CHECK_PAPER_SIZE( 5.83,  8.27, PAPER_SIZE_A5);
77
7.96k
    CHECK_PAPER_SIZE( 8.27, 11.69, PAPER_SIZE_A4);
78
7.96k
    CHECK_PAPER_SIZE(11.69, 16.54, PAPER_SIZE_A3);
79
7.96k
    CHECK_PAPER_SIZE(16.54, 23.39, PAPER_SIZE_A2);
80
7.96k
    CHECK_PAPER_SIZE(23.39, 33.11, PAPER_SIZE_A1);
81
7.96k
    CHECK_PAPER_SIZE(33.11, 46.81, PAPER_SIZE_A0);
82
7.96k
    CHECK_PAPER_SIZE( 7.16, 10.12, PAPER_SIZE_JIS_B5);
83
7.96k
    CHECK_PAPER_SIZE(10.12, 14.33, PAPER_SIZE_JIS_B4);
84
7.96k
    CHECK_PAPER_SIZE( 3.94,  5.83, PAPER_SIZE_JPOST);
85
7.96k
    CHECK_PAPER_SIZE( 5.83,  7.87, PAPER_SIZE_JPOSTD);
86
7.96k
    CHECK_PAPER_SIZE( 3.87,  7.5 , PAPER_SIZE_MONARCH);
87
7.96k
    CHECK_PAPER_SIZE( 4.12,  9.5 , PAPER_SIZE_COM10);
88
7.96k
    CHECK_PAPER_SIZE( 4.33,  8.66, PAPER_SIZE_DL);
89
7.96k
    CHECK_PAPER_SIZE( 6.38,  9.01, PAPER_SIZE_C5);
90
7.96k
    CHECK_PAPER_SIZE( 6.93,  9.84, PAPER_SIZE_B5);
91
92
7.96k
#undef CHECK_PAPER_SIZE
93
94
7.96k
    return code;
95
7.96k
}
96
97
/* ------ Get page orientation ------ */
98
99
/* Get the page orientation, based on width and height. */
100
int
101
gdev_pcl_page_orientation(gx_device * dev)
102
8.20k
{
103
8.20k
    if (dev->height >= dev->width)
104
8.12k
        return PAGE_ORIENTATION_PORTRAIT;
105
84
    else
106
84
        return PAGE_ORIENTATION_LANDSCAPE;
107
8.20k
}
108
109
/* ------ Color mapping ------ */
110
111
/* The PaintJet and DeskJet 500C use additive colors in separate planes. */
112
/* We only keep one bit of color, with 1 = R, 2 = G, 4 = B. */
113
/* Because the buffering routines assume 0 = white, */
114
/* we complement all the color components. */
115
0
#define cv_shift (sizeof(gx_color_value) * 8 - 1)
116
117
/* Map an RGB color to a printer color. */
118
gx_color_index
119
gdev_pcl_3bit_map_rgb_color(gx_device * dev, const gx_color_value cv[])
120
0
{
121
0
    gx_color_value r, g, b;
122
0
    r = cv[0]; g = cv[1]; b = cv[2];
123
0
    return (((b >> cv_shift) << 2) + ((g >> cv_shift) << 1) + (r >> cv_shift)) ^ 7;
124
0
}
125
126
/* Map the printer color back to RGB. */
127
int
128
gdev_pcl_3bit_map_color_rgb(gx_device * dev, gx_color_index color,
129
                            gx_color_value prgb[3])
130
0
{
131
0
    ushort cc = (ushort) color ^ 7;
132
133
0
    prgb[0] = -(cc & 1);
134
0
    prgb[1] = -((cc >> 1) & 1);
135
0
    prgb[2] = -(cc >> 2);
136
0
    return 0;
137
0
}
138
139
/* ------ Compression ------ */
140
141
/*
142
 * Mode 2 Row compression routine for the HP DeskJet & LaserJet IIp.
143
 * Compresses data from row up to end_row, storing the result
144
 * starting at compressed.  Returns the number of bytes stored.
145
 * Runs of K<=127 literal bytes are encoded as K-1 followed by
146
 * the bytes; runs of 2<=K<=127 identical bytes are encoded as
147
 * 257-K followed by the byte.
148
 * In the worst case, the result is N+(N/127)+1 bytes long,
149
 * where N is the original byte count (end_row - row).
150
 * To speed up the search, we examine an entire word at a time.
151
 * We will miss a few blocks of identical bytes; tant pis.
152
 */
153
int
154
gdev_pcl_mode2compress_padded(const word * row, const word * end_row,
155
                              byte * compressed, bool pad)
156
4.99M
{
157
4.99M
    register const word *exam = row;  /* word being examined in the row to compress */
158
4.99M
    register byte *cptr = compressed; /* output pointer into compressed bytes */
159
160
28.9M
    while (exam < end_row) { /* Search ahead in the input looking for a run */
161
        /* of at least 4 identical bytes. */
162
23.9M
        const byte *compr = (const byte *)exam;
163
23.9M
        const byte *end_dis;
164
23.9M
        const word *next;
165
23.9M
        register word test = *exam;
166
167
58.8M
        while (((test << 8) ^ test) > 0xff) {
168
39.8M
            if (++exam >= end_row)
169
4.98M
                break;
170
34.8M
            test = *exam;
171
34.8M
        }
172
173
        /* Find out how long the run is */
174
23.9M
        end_dis = (const byte *)exam;
175
23.9M
        if (exam == end_row) { /* no run */
176
            /* See if any of the last 3 "dissimilar" bytes are 0. */
177
4.98M
            if (!pad && end_dis > compr && end_dis[-1] == 0) {
178
1.11M
                if (end_dis[-2] != 0)
179
91.0k
                    end_dis--;
180
1.02M
                else if (end_dis[-3] != 0)
181
268k
                    end_dis -= 2;
182
754k
                else
183
754k
                    end_dis -= 3;
184
1.11M
            }
185
4.98M
            next = --end_row;
186
18.9M
        } else {
187
18.9M
            next = exam + 1;
188
67.3M
            while (next < end_row && *next == test)
189
48.4M
                next++;
190
            /* See if any of the last 3 "dissimilar" bytes */
191
            /* are the same as the repeated byte. */
192
18.9M
            if (end_dis > compr && end_dis[-1] == (byte) test) {
193
13.4M
                if (end_dis[-2] != (byte) test)
194
744k
                    end_dis--;
195
12.6M
                else if (end_dis[-3] != (byte) test)
196
3.49M
                    end_dis -= 2;
197
9.17M
                else
198
9.17M
                    end_dis -= 3;
199
13.4M
            }
200
18.9M
        }
201
202
        /* Now [compr..end_dis) should be encoded as dissimilar, */
203
        /* and [end_dis..next) should be encoded as similar. */
204
        /* Note that either of these ranges may be empty. */
205
206
34.1M
        for (;;) {   /* Encode up to 127 dissimilar bytes */
207
34.1M
            uint count = end_dis - compr; /* uint for faster switch */
208
209
34.1M
            switch (count) { /* Use memcpy only if it's worthwhile. */
210
2.96M
                case 6:
211
2.96M
                    cptr[6] = compr[5];
212
9.10M
                case 5:
213
9.10M
                    cptr[5] = compr[4];
214
9.10M
                case 4:
215
9.10M
                    cptr[4] = compr[3];
216
9.10M
                case 3:
217
9.10M
                    cptr[3] = compr[2];
218
9.10M
                case 2:
219
9.10M
                    cptr[2] = compr[1];
220
9.10M
                case 1:
221
9.10M
                    cptr[1] = compr[0];
222
9.10M
                    *cptr = count - 1;
223
9.10M
                    cptr += count + 1;
224
23.9M
                case 0: /* all done */
225
23.9M
                    break;
226
10.2M
                default:
227
10.2M
                    if (count > 127)
228
179k
                        count = 127;
229
10.2M
                    *cptr++ = count - 1;
230
10.2M
                    memcpy(cptr, compr, count);
231
10.2M
                    cptr += count, compr += count;
232
10.2M
                    continue;
233
34.1M
            }
234
23.9M
            break;
235
34.1M
        }
236
237
23.9M
        {     /* Encode up to 127 similar bytes. */
238
            /* Note that count may be <0 at end of row. */
239
23.9M
            int count = (const byte *)next - end_dis;
240
241
43.1M
            while (count > 0) {
242
19.2M
                int this = (count > 127 ? 127 : count);
243
244
19.2M
                *cptr++ = 257 - this;
245
19.2M
                *cptr++ = (byte) test;
246
19.2M
                count -= this;
247
19.2M
            }
248
23.9M
            exam = next;
249
23.9M
        }
250
23.9M
    }
251
4.99M
    return (cptr - compressed);
252
4.99M
}
253
int
254
gdev_pcl_mode2compress(const word * row, const word * end_row,
255
                       byte * compressed)
256
4.99M
{
257
4.99M
    return gdev_pcl_mode2compress_padded(row, end_row, compressed, false);
258
4.99M
}
259
260
/*
261
 * Mode 3 compression routine for the HP LaserJet III family.
262
 * Compresses bytecount bytes starting at current, storing the result
263
 * in compressed, comparing against and updating previous.
264
 * Returns the number of bytes stored.  In the worst case,
265
 * the number of bytes is bytecount+(bytecount/8)+1.
266
 */
267
int
268
gdev_pcl_mode3compress(int bytecount, const byte * current, byte * previous, byte * compressed)
269
4.99M
{
270
4.99M
    register const byte *cur = current;
271
4.99M
    register byte *prev = previous;
272
4.99M
    register byte *out = compressed;
273
4.99M
    const byte *end = current + bytecount;
274
275
31.8M
    while (cur < end) {   /* Detect a maximum run of unchanged bytes. */
276
31.8M
        const byte *run = cur;
277
31.8M
        register const byte *diff;
278
31.8M
        const byte *stop;
279
31.8M
        int offset, cbyte;
280
281
987M
        while (cur < end && *cur == *prev) {
282
955M
            cur++, prev++;
283
955M
        }
284
31.8M
        if (cur == end)
285
4.99M
            break;    /* rest of row is unchanged */
286
        /* Detect a run of up to 8 changed bytes. */
287
        /* We know that *cur != *prev. */
288
26.8M
        diff = cur;
289
26.8M
        stop = (end - cur > 8 ? cur + 8 : end);
290
111M
        do {
291
111M
            *prev++ = *cur++;
292
111M
        }
293
111M
        while (cur < stop && *cur != *prev);
294
        /* Now [run..diff) are unchanged, and */
295
        /* [diff..cur) are changed. */
296
        /* Generate the command byte(s). */
297
26.8M
        offset = diff - run;
298
26.8M
        cbyte = (cur - diff - 1) << 5;
299
26.8M
        if (offset < 31)
300
22.8M
            *out++ = cbyte + offset;
301
4.00M
        else {
302
4.00M
            *out++ = cbyte + 31;
303
4.00M
            offset -= 31;
304
4.00M
            while (offset >= 255)
305
736
                *out++ = 255, offset -= 255;
306
4.00M
            *out++ = offset;
307
4.00M
        }
308
        /* Copy the changed data. */
309
138M
        while (diff < cur)
310
111M
            *out++ = *diff++;
311
26.8M
    }
312
4.99M
    return out - compressed;
313
4.99M
}
314
315
/*
316
 * Mode 9 2D compression for the HP DeskJets . This mode can give
317
 * very good compression ratios, especially if there are areas of flat
318
 * colour (or blank areas), and so is 'highly recommended' for colour
319
 * printing in particular because of the very large amounts of data which
320
 * can be generated
321
 */
322
int
323
gdev_pcl_mode9compress(int bytecount, const byte *current,
324
                       const byte *previous, byte *compressed)
325
0
{
326
0
    register const byte *cur = current;
327
0
    register const byte *prev = previous;
328
0
    register byte *out = compressed;
329
0
    const byte *end = current + bytecount;
330
331
0
    while (cur < end) {   /* Detect a run of unchanged bytes. */
332
0
        const byte *run = cur;
333
0
        register const byte *diff;
334
0
        int offset;
335
336
0
        while (cur < end && *cur == *prev) {
337
0
            cur++, prev++;
338
0
        }
339
0
        if (cur == end)
340
0
            break;   /* rest of row is unchanged */
341
        /* Detect a run of changed bytes. */
342
        /* We know that *cur != *prev. */
343
0
        diff = cur;
344
0
        do {
345
0
            prev++;
346
0
            cur++;
347
0
        }
348
0
        while (cur < end && *cur != *prev);
349
        /* Now [run..diff) are unchanged, and */
350
        /* [diff..cur) are changed. */
351
0
        offset = diff - run;
352
0
        {
353
0
            const byte *stop_test = cur - 4;
354
0
            int dissimilar, similar;
355
356
0
            while (diff < cur) {
357
0
                const byte *compr = diff;
358
0
                const byte *next; /* end of run */
359
0
                byte value = 0;
360
361
0
                while (diff <= stop_test &&
362
0
                       ((value = *diff) != diff[1] ||
363
0
                        value != diff[2] ||
364
0
                        value != diff[3]))
365
0
                    diff++;
366
367
                /* Find out how long the run is */
368
0
                if (diff > stop_test) /* no run */
369
0
                    next = diff = cur;
370
0
                else {
371
0
                    next = diff + 4;
372
0
                    while (next < cur && *next == value)
373
0
                        next++;
374
0
                }
375
376
0
#define MAXOFFSETU 15
377
0
#define MAXCOUNTU 7
378
                /* output 'dissimilar' bytes, uncompressed */
379
0
                if ((dissimilar = diff - compr)) {
380
0
                    int temp, i;
381
382
0
                    if ((temp = --dissimilar) > MAXCOUNTU)
383
0
                        temp = MAXCOUNTU;
384
0
                    if (offset < MAXOFFSETU)
385
0
                        *out++ = (offset << 3) | (byte) temp;
386
0
                    else {
387
0
                        *out++ = (MAXOFFSETU << 3) | (byte) temp;
388
0
                        offset -= MAXOFFSETU;
389
0
                        while (offset >= 255) {
390
0
                            *out++ = 255;
391
0
                            offset -= 255;
392
0
                        }
393
0
                        *out++ = offset;
394
0
                    }
395
0
                    if (temp == MAXCOUNTU) {
396
0
                        temp = dissimilar - MAXCOUNTU;
397
0
                        while (temp >= 255) {
398
0
                            *out++ = 255;
399
0
                            temp -= 255;
400
0
                        }
401
0
                        *out++ = (byte) temp;
402
0
                    }
403
0
                    for (i = 0; i <= dissimilar; i++)
404
0
                        *out++ = *compr++;
405
0
                    offset = 0;
406
0
                }   /* end uncompressed */
407
0
#undef MAXOFFSETU
408
0
#undef MAXCOUNTU
409
410
0
#define MAXOFFSETC 3
411
0
#define MAXCOUNTC 31
412
                /* output 'similar' bytes, run-length encoded */
413
0
                if ((similar = next - diff)) {
414
0
                    int temp;
415
416
0
                    if ((temp = (similar -= 2)) > MAXCOUNTC)
417
0
                        temp = MAXCOUNTC;
418
0
                    if (offset < MAXOFFSETC)
419
0
                        *out++ = 0x80 | (offset << 5) | (byte) temp;
420
0
                    else {
421
0
                        *out++ = 0x80 | (MAXOFFSETC << 5) | (byte) temp;
422
0
                        offset -= MAXOFFSETC;
423
0
                        while (offset >= 255) {
424
0
                            *out++ = 255;
425
0
                            offset -= 255;
426
0
                        }
427
0
                        *out++ = offset;
428
0
                    }
429
0
                    if (temp == MAXCOUNTC) {
430
0
                        temp = similar - MAXCOUNTC;
431
0
                        while (temp >= 255) {
432
0
                            *out++ = 255;
433
0
                            temp -= 255;
434
0
                        }
435
0
                        *out++ = (byte) temp;
436
0
                    }
437
0
                    *out++ = value;
438
0
                    offset = 0;
439
0
                }   /* end compressed */
440
0
#undef MAXOFFSETC
441
0
#undef MAXCOUNTC
442
443
0
                diff = next;
444
0
            }
445
0
        }
446
0
    }
447
0
    return out - compressed;
448
0
}