summaryrefslogtreecommitdiff
blob: 741b5925b58f7af4cbff1a1e0e6e5ef5371ddad2 (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
/* Copyright (C) 2001-2020 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.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/


/*
   This driver is written for Tektronix ink-jet 4696 and 4695 plotters.

   It may easily be adopted to the 4393 and 4394 models as well, simply by
   adding new device descriptors with other geometrical characteristics.
 */
#include "gdevprn.h"
#include "malloc_.h"

/* Thanks to Karsten Spang (spang@nbivax.nbi.dk) for contributing */
/* this code to Aladdin Enterprises. */

/* The device descriptor */
/* We need our own color mapping procedures. */
static dev_proc_map_rgb_color(tekink_map_rgb_color);
static dev_proc_map_color_rgb(tekink_map_color_rgb);
static dev_proc_print_page(tekink_print_page);
/* Since the print_page doesn't alter the device, this device can print in the background */
static gx_device_procs tekink_procs =
    prn_color_procs(gdev_prn_open, gdev_prn_bg_output_page, gdev_prn_close,
        tekink_map_rgb_color, tekink_map_color_rgb);

/*
   Device descriptor for the Tek 4696.
   The 4696 plotter uses roll media, thus the y size is arbitrary. The
   value below is chosen to make the image area A*-format like, i.e. the
   aspect ratio is close to sqrt(2).
*/
const gx_device_printer far_data gs_tek4696_device =
    prn_device(tekink_procs,"tek4696",
    85,120,	/* Page size in 10th of inches */
    120,120,	/* Resolution in DPI */
    0.0,0.0,0.0,0.0,	/* Margins */
    4,		/* Bits per pixel */
    tekink_print_page);

/* Color mapping.
   The tek inkjets use subtractive colors B=0 M=1 Y=2 C=3. These are
   represented as 4 bits B=1 M=2 Y=4 C=8 in a byte. This gives:
      White   =  0
      Black   =  1
      Magenta =  2
      Yellow  =  4
      Red     =  6
      Cyan    =  8
      Blue    = 10
      Green   = 12
   The remaining values are unused. (They give ugly results if sent to the
   plotter.) Of course this could have been compressed into 3 bits, but
   as the palette color memory device uses 8 bits anyway, this is easier,
   and perhaps faster.
*/

static gx_color_index rgb_to_index[8]={1,6,12,4,10,2,8,0};
static ushort index_to_rgb[16][3]={
    {65535,65535,65535}, /* White */
    {0,0,0}, /* Black */
    {65535,0,65535}, /* Magenta */
    {2,2,2}, /* Unused */
    {65535,65535,0}, /* Yellow */
    {2,2,2}, /* Unused */
    {65535,0,0}, /* Red */
    {2,2,2}, /* Unused */
    {0,65535,65535}, /* Cyan */
    {2,2,2}, /* Unused */
    {0,0,65535}, /* Blue */
    {2,2,2}, /* Unused */
    {0,65535,0}, /* Green */
    {2,2,2}, /* Unused */
    {2,2,2}, /* Unused */
    {2,2,2}  /* Unused */
};

/* Map an RGB color to a printer color. */
static gx_color_index
tekink_map_rgb_color(gx_device *dev, const gx_color_value cv[])
{
    gx_color_value r = cv[0];
    gx_color_value g = cv[1];
    gx_color_value b = cv[2];

    return(rgb_to_index[(((b>32767) << 2) + ((g>32767) << 1) +
                        (r>32767)) & 7]);
}

/* Map the printer color back to RGB. */
static int
tekink_map_color_rgb(gx_device *dev, gx_color_index color, ushort prgb[3])
{
    register ushort c = (ushort)color;
    register int i;
    if (c>15) return -1;
    if (index_to_rgb[c][0]==2) return -1;
    for (i=0;i<3;i++){
        prgb[i]=index_to_rgb[c][i];
    }
    return 0;
}

/* Send the page to the printer. */
static int
tekink_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{
    int line_size,color_line_size,scan_line,num_bytes,scan_lines,color_plane;
    int roll_paper,out_line,micro_line,pending_micro_lines,line_blank,
        blank_lines;
    byte *outdata,*indata1,*bdata1,*mdata1,*ydata1,*cdata1;
    register byte *indata,*bdatap,*mdatap,*ydatap,*cdatap;
    register byte bdata,mdata,ydata,cdata;
    register byte mask,inbyte;
    register byte *indataend,*outdataend;
    int code = 0;

    /* Allocate a temporary buffer for color separation.
       The buffer is partitioned into an input buffer and four
       output buffers for the color planes. The output buffers
       are allocated with an extra sentinel byte. */

    line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
    color_line_size=(pdev->width+7)/8;
    indata1=(byte *)malloc(line_size+4*(color_line_size+1));
    if (indata1==NULL)
        return_error(gs_error_VMerror);
    /* pointers to the partions */
    indataend=indata1+line_size;
    bdata1=indataend;
    mdata1=bdata1+(color_line_size+1);
    ydata1=mdata1+(color_line_size+1);
    cdata1=ydata1+(color_line_size+1);

    /* Does this device use roll paper? */
    roll_paper=!strcmp(pdev->dname,"tek4696");

    out_line=0;
    blank_lines=0;
    scan_lines=pdev->height;
    for (scan_line=0;scan_line<scan_lines;scan_line++){
        /* get data */
        code = gdev_prn_copy_scan_lines(pdev,scan_line,indata1,line_size);
        if (code < 0)
            goto xit;
        /* Separate data into color planes */
        bdatap = bdata1+1;
        mdatap = mdata1+1;
        ydatap = ydata1+1;
        cdatap = cdata1+1;
        bdata=0;
        mdata=0;
        cdata=0;
        ydata=0;
        mask=0x80;
        memset(indataend,0,4*(color_line_size+1));
        for (indata=indata1;indata<indataend;indata++){
            inbyte = *indata;
            if (inbyte&0x01) bdata|=mask;
            if (inbyte&0x02) mdata|=mask;
            if (inbyte&0x04) ydata|=mask;
            if (inbyte&0x08) cdata|=mask;
            mask>>=1;
            if (!mask){
                *(bdatap++) = bdata;
                *(mdatap++) = mdata;
                *(cdatap++) = cdata;
                *(ydatap++) = ydata;
                bdata=0;
                mdata=0;
                cdata=0;
                ydata=0;
                mask=0x80;
            }
        }
        if (mask!=0x80){
            *bdatap = bdata;
            *mdatap = mdata;
            *cdatap = cdata;
            *ydatap = ydata;
        }
        line_blank=1;
        /* Output each of the four color planes */
        for (color_plane=0;color_plane<4;color_plane++){
            outdata=indataend+(color_plane*(color_line_size+1));
            outdataend=outdata+color_line_size;

            /* Remove trailing spaces and output the color line if it is
               not blank */
            *outdata=0xff;
            while (!(*outdataend)) outdataend--;
            num_bytes=(outdataend-outdata);
            if (num_bytes!=0){
                line_blank=0;
                /* On encountering the first non-blank data, output pending
                   blank lines */
                if (blank_lines){
                    pending_micro_lines=((out_line+blank_lines+1)/4)-
                        (out_line/4);
                    for (micro_line=0;micro_line<pending_micro_lines;
                        micro_line++){
                        gp_fputs("\033A",prn_stream);
                    }
                    out_line+=blank_lines;
                    blank_lines=0;
                }
                gp_fprintf(prn_stream,"\033I%c%03d",'0'+(out_line%4)+
                           4*color_plane,num_bytes);
                gp_fwrite(outdata+1,1,num_bytes,prn_stream);
            }
        } /* loop over color planes */

        /* If this line is blank, and if it is a roll paper model,
           count the line. Otherwise output the line */
        if (line_blank&&roll_paper){
            /* Only increment the blank line count, if non blank lines
               have been encountered previously, i.e. skip leading blank
               lines. */
            if (out_line) blank_lines++;
        }
        else{
            if (out_line%4==3){
                /* Write micro line feed code */
                gp_fputs("\033A",prn_stream);
            }
            out_line++;
        }
    } /* loop over scan lines */

    /* if the number of scan lines written is not a multiple of four,
       write the final micro line feed code */
    if (out_line%4){
        gp_fputs("\033A",prn_stream);
    }
    /* Separate this plot from the next */
    if (roll_paper){
        gp_fputs("\n\n\n\n\n",prn_stream);
    }
    else{
        gp_fputs("\f",prn_stream);
    }

xit:
    /* Deallocate temp buffer */
    free(indata1);
    return code;
}