summaryrefslogtreecommitdiff
path: root/devices/gdevdjtc.c
blob: 67535ecf604a028cd4fa8d50d5a0ba94d40e83f2 (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
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/

/* HP DeskJet 500C driver */
#include "gdevprn.h"
#include "gdevpcl.h"
#include "malloc_.h"

/***
 *** Note: this driver was contributed by a user, Alfred Kayser:
 ***       please contact AKayser@et.tudelft.nl if you have questions.
 ***/

#ifndef SHINGLING        /* Interlaced, multi-pass printing */
#define SHINGLING 1      /* 0 = none, 1 = 50%, 2 = 25%, 2 is best & slowest */
#endif

#ifndef DEPLETION        /* 'Intelligent' dot-removal */
#define DEPLETION 1      /* 0 = none, 1 = 25%, 2 = 50%, 1 best for graphics? */
#endif                   /* Use 0 for transparencies */

#define X_DPI 300
#define Y_DPI 300
/* bytes per line for DeskJet Color */
#define LINE_SIZE ((X_DPI * 85 / 10 + 63) / 64 * 8)

/* The device descriptors */
static dev_proc_print_page(djet500c_print_page);

/* Since the print_page doesn't alter the device, this device can print in the background */
static void
djet500c_initialize_device_procs(gx_device *dev)
{
    gdev_prn_initialize_device_procs_bg(dev);

    set_dev_proc(dev, map_rgb_color, gdev_pcl_3bit_map_rgb_color);
    set_dev_proc(dev, map_color_rgb, gdev_pcl_3bit_map_color_rgb);
    set_dev_proc(dev, encode_color, gdev_pcl_3bit_map_rgb_color);
    set_dev_proc(dev, decode_color, gdev_pcl_3bit_map_color_rgb);
}

const gx_device_printer far_data gs_djet500c_device =
  prn_device(djet500c_initialize_device_procs, "djet500c",
    85,                /* width_10ths, 8.5" */
    120,		/* height_10ths, 12" */
    X_DPI, Y_DPI,
    0.25, 0.25, 0.25, 0.25,        /* margins */
    3, djet500c_print_page);

/* Forward references */
static int djet500c_print_page(gx_device_printer *, gp_file *);

static int mode2compress(byte *row, byte *end_row, byte *compressed);

/* The DeskJet 500C uses additive colors in separate planes. */
/* We only keep one bit of color, with 1 = R, 2 = G, 4 = B. */
/* Because the buffering routines assume 0 = white, */
/* we complement all the color components. */

/* Send the page to the printer.  For speed, compress each scan line, */
/* since computer-to-printer communication time is often a bottleneck. */
/* The DeskJet Color can compress (mode 2) */

static int
djet500c_print_page(gx_device_printer *pdev, gp_file *fprn)
{
    byte *bitData=NULL;
    byte *plane1=NULL;
    byte *plane2=NULL;
    byte *plane3=NULL;
    int bitSize=0;
    int planeSize=0;
    int code = 0;

    /* select the most compressed mode available & clear tmp storage */
    /* put printer in known state */
    gp_fputs("\033E",fprn);

    /* ends raster graphics to set raster graphics resolution */
    gp_fputs("\033*rbC", fprn);	/*  was \033*rB  */

    /* set raster graphics resolution -- 300 dpi */
    gp_fputs("\033*t300R", fprn);

    /* A4, skip perf, def. paper tray */
    gp_fputs("\033&l26a0l1H", fprn);

    /* RGB Mode */
    gp_fputs("\033*r3U", fprn);

    /* set depletion level */
    gp_fprintf(fprn, "\033*o%dD", DEPLETION);

    /* set shingling level */
    gp_fprintf(fprn, "\033*o%dQ", SHINGLING);

    /* move to top left of page & set current position */
    gp_fputs("\033*p0x0Y", fprn); /* cursor pos: 0,0 */

    gp_fputs("\033*b2M", fprn);	/*  mode 2 compression for now  */

    gp_fputs("\033*r0A", fprn);  /* start graf. left */

    /* Send each scan line in turn */
       {    int lnum;
        int num_blank_lines = 0;
        int lineSize = gdev_mem_bytes_per_scan_line((gx_device *)pdev);

        if (lineSize <= 0)
            return_error(gs_error_rangecheck);

        bitSize=lineSize;
        bitData=(byte*)malloc(bitSize+16);
        if (bitData == 0)
            return_error(gs_error_VMerror);

        for (lnum=0; lnum<pdev->height; lnum++)
        {
            byte *endData;

            code = gdev_prn_copy_scan_lines(pdev, lnum, bitData, lineSize);
            if (code < 0)
                goto xit;

            /* Identify and skip blank lines */
            endData = bitData + lineSize;
            while ( (endData>bitData) && (endData[-1] == 0) )
                endData--;
            if (endData == bitData)
                num_blank_lines++;
            else
            {
                int count, k, i, lineLen;
                endData = bitData + lineSize;

                /* Pad with 0s to fill out the last */
                /* block of 8 bytes. */
                memset(endData, 0, 7);

                lineLen=((endData-bitData)+7)/8;    /* Round to next 8multiple */
                if (planeSize<lineLen)
                {
                    if (plane1) free(plane1);
                    if (plane2) free(plane2);
                    if (plane3) free(plane3);
                    planeSize=lineLen;
                    plane1=(byte*)malloc(planeSize+8);
                    plane2=(byte*)malloc(planeSize+8);
                    plane3=(byte*)malloc(planeSize+8);
                    if (plane1 == NULL || plane2 == NULL || plane3 == NULL) {
                        code = gs_error_VMerror;
                        goto xit;
                    }
                }
                /* Transpose the data to get pixel planes. */
                for (k=i=0; k<lineLen; i+=8, k++)
                {
                   register ushort t, c;

                   /* Three smaller loops are better optimizable and use less
                      vars, so most of them can be in registers even on pc's */
                   for (c=t=0;t<8;t++)
                        c = (c<<1) | (bitData[t+i]&4);
                   plane3[k] = ~(byte)(c>>2);
                   for (c=t=0;t<8;t++)
                        c = (c<<1) | (bitData[t+i]&2);
                   plane2[k] = ~(byte)(c>>1);
                   for (c=t=0;t<8;t++)
                        c = (c<<1) | (bitData[t+i]&1);
                   plane1[k] = ~(byte)(c);
                }

                /* Skip blank lines if any */
                if (num_blank_lines > 0)
                {    /* move down from current position */
                    gp_fprintf(fprn, "\033*b%dY", num_blank_lines);
                    num_blank_lines = 0;
                }

                /* Transfer raster graphics */
                /* in the order R, G, B. */
                /* lineLen is at least bitSize/8, so bitData can easily be used to store
                   lineLen of bytes */
                /* P.s. mode9 compression is akward(??) to use, because the lineLenght's
                   are different, so we are stuck with mode 2, which is good enough */

                /* set the line width */
                gp_fprintf(fprn, "\033*r%dS", lineLen*8);

                count = mode2compress(plane1, plane1 + lineLen, bitData);
                gp_fprintf(fprn, "\033*b%dV", count);
                gp_fwrite(bitData, sizeof(byte), count, fprn);
                count = mode2compress(plane2, plane2 + lineLen, bitData);
                gp_fprintf(fprn, "\033*b%dV", count);
                gp_fwrite(bitData, sizeof(byte), count, fprn);
                count = mode2compress(plane3, plane3 + lineLen, bitData);
                gp_fprintf(fprn, "\033*b%dW", count);
                gp_fwrite(bitData, sizeof(byte), count, fprn);
            }
        }
    }
    /* end raster graphics */
    gp_fputs("\033*rbC", fprn);	/*  was \033*rB  */
    gp_fputs("\033*r1U", fprn);	/*  back to 1 plane  */

    /* put printer in known state */
    gp_fputs("\033E",fprn);

    /* eject page */
    gp_fputs("\033&l0H", fprn);

xit:
    /* release allocated memory */
    if (bitData) free(bitData);
    if (plane1) free(plane1);
    if (plane2) free(plane2);
    if (plane3) free(plane3);

    return code;
}

/*
 * Mode 2 Row compression routine for the HP DeskJet & LaserJet IIp.
 * Compresses data from row up to end_row, storing the result
 * starting at compressed.  Returns the number of bytes stored.
 * Runs of K<=127 literal bytes are encoded as K-1 followed by
 * the bytes; runs of 2<=K<=127 identical bytes are encoded as
 * 257-K followed by the byte.
 * In the worst case, the result is N+(N/127)+1 bytes long,
 * where N is the original byte count (end_row - row).
 * I can't use the general pcl version, because it assume even linelength's
 */
/* RJW: While the worst case given above is true for correctly
 * encoded runs, the code below does not achieve it. Take for
 * example ABBABBABB. This will be encoded as:
 *    <1 byte to signal literal run of 1>A
 *    <1 byte to signal a run of two>B
 *    Repeated 3 times.
 *
 * i.e. 12 bytes to encode 9. Thus the actual worse case is 4N/3+2
 * (e.g. for ABBA). GS gets away with this because it over
 * allocates.
 */
static int
mode2compress(byte *row, byte *end_row, byte *compressed)
{
    register byte *exam; /* word being examined in the row to compress */
    register byte *cptr = compressed; /* output pointer into compressed bytes */
    int i, count, len;
    byte test;

    exam = row;
    while (1)
    {
        test = *exam++;
        /* Advance exam until test==*exam  or exam==end_row */
        while ((test != *exam) && (exam < end_row))
            test = *exam++;
        /* row points to start of differing bytes,
           exam points to start of consequtive series
                    or to end of row */
        if (exam<end_row) exam--;
        len=exam-row;
        while (len>0)
        {
            count=len;
            if (count>127) count=127;
            *cptr++=count-1;
            for (i=0;i<count;i++) *cptr++ = *row++;
            len-=count;
        }
        if (exam>=end_row) break;     /* done */
        exam++;     /* skip first same byte */
        while ((test == *exam) && (exam < end_row))  /* skip all same bytes */
            exam++;
        /* exam points now first different word or to end of data */
        len = exam-row;
        while (len>0)
        {
            count=len;
            if (count>127) count=127;
            *cptr++=(257-count);
            *cptr++=test;
            len-=count;
        }
        if (exam>=end_row) break;            /* end of data */
        row = exam;    /* row points to first dissimular byte */
    }
    return (cptr-compressed);
}