1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
13
/* $Id: gdevlxm.c 8250 2007-09-25 13:31:24Z giles $*/
15
* Lexmark 5700 ink-jet printer driver for Ghostscript
17
* defines the lxm5700m device for printing in black-and-white at 1200 dpi
18
* doesn't handle color or any other resolution.
19
* Native resolution appears to be 600 x 1200, but print bands are overlapped.
22
* gs -sOutputFile=/dev/lp0 -sDevice=lxm5700m -dHeadSeparation=15 file.ps
24
* where HeadSeparation varies from print-cartridge to print-cartridge and
25
* 16 (the default) usually works fine.
27
* Stephen Taylor setaylor@ma.ultranet.com staylor@cs.wpi.edu
33
/* The procedure descriptors */
34
/* declare functions */
35
static dev_proc_print_page(lxm5700m_print_page);
36
static dev_proc_get_params(lxm_get_params);
37
static dev_proc_put_params(lxm_put_params);
39
/* set up dispatch table. I follow gdevdjet in using gdev_prn_output_page */
40
static const gx_device_procs lxm5700m_procs =
41
prn_params_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
42
lxm_get_params, lxm_put_params);
44
/* The device descriptors */
46
/* define a subclass with useful state in it. */
47
typedef struct lxm_device_s { /* a sub-class of gx_device_printer */
53
/* Standard lxm5700m device */
54
lxm_device far_data gs_lxm5700m_device = {
55
prn_device_std_body(lxm_device, lxm5700m_procs, "lxm5700m",
56
DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
57
600, 600, /* x dpi, y dpi */
58
0.2, 0.0, 0.0, 0.0, /* margins */
59
1, lxm5700m_print_page),
60
16 /* default headSeparation value */
63
/* I don't know the whole protocol for the printer, but let me tell
64
* you about the fraction I use.
65
* Each page begins with a header, which I describe as init1, init2, init3 --
66
* (I separate them, because I've seen output in which those sections
67
* seemed to appear independently, but I've deleted the only code I
68
* had that actually used them separately.)
69
* Then there are a number of swipe commands, each of which describes one
70
* swipe of the printhead. Each swipe command begins with a fixed
71
* header, which gives the number of bytes in the command,
72
* left and right margins,
73
* a vertical offset, some noise characters I don't know the meaning of,
74
* and the table of bits. The bits are given one column at a time, using a
75
* simple compression scheme: a directory, consisting of two bytes, tells
76
* which sixteen-bit intervals of the 208-bit column contain 1-bits; then
77
* the appropriate number of sixteen-bit bit-maps follow. (A zero-bit in the
78
* directory indicates that a bitmap for that sector follows.) In the worst case,
79
* this scheme would be bigger than the uncompressed bitmap, but it seems to
80
* usually save 80-90%. The biggest complication of the bitmap scheme is this:
81
* There are two print-heads on the black cartridge, and they are 16 pixels
82
* (or headSeparation) apart. Odd columns of the swipe address one printhead,
83
* and even columns the other. On the following swipe, the printheads
84
* addressed by even and odd columns are reversed. I think the printheads might be
85
* staggered, but the output I've seen staggers things in software;
86
* adjacent vertical bits on the same head are not addressed; the missing
87
* bits are written in by the second head when it passes (16 columns later.)
88
* In my code, I call the state of addressing one head or the other "direction".
89
* Originally I thought that the printhead was writing on both leftward and
90
* rightward motion, and that which head was addressed by odd columns changed
91
* accordingly. I'm no longer sure this is true, but the head addressed by the
92
* even columns does alternate with each swipe.
95
* various output shorthands
100
0xA5,0, 3, 0x40,4,5, \
101
0xA5,0, 3, 0x40,4,6, \
102
0xA5,0, 3, 0x40,4,7, \
103
0xA5,0, 3, 0x40,4,8, \
104
0xA5,0, 4, 0x40,0xe0,0x0b, 3
107
0xA5,0, 11, 0x40,0xe0,0x41, 0,0,0,0,0,0,0, 2, \
108
0xA5,0, 6, 0x40, 5, 0,0,0x80,0 \
111
0x1b,'*', 7,0x73,0x30, \
112
0x1b,'*', 'm', 0, 0x14, 3, 0x84, 2, 0, 1, 0xf4, \
114
0x1b,'*', 'm', 0, 0x42, 0, 0, \
115
0xA5,0, 5, 0x40,0xe0,0x80, 8, 7, \
116
0x1b,'*', 'm', 0, 0x40, 0x15, 7, 0x0f, 0x0f \
119
0xA5,0, 6, 0x40, 3,3,0xc0,0x0f,0x0f \
125
#define outByte(b) putc(b, prn_stream)
129
/* number of pixels between even columns in output and odd ones*/
130
/* #define headSeparation 16 */
131
/* overlap between successive swipes of the print head */
133
/* height of printhead in pixels */
134
#define swipeHeight 208
135
/* number of shorts described by each column directory */
136
#define directorySize 13
138
/* ------ Driver procedures ------ */
141
/* Send the page to the printer. */
143
lxm5700m_print_page(gx_device_printer *pdev, FILE *prn_stream)
145
int lnum,minX, maxX, i, l, highestX, leastX, extent;
146
int direction = RIGHTWARD;
149
int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
150
/* Note that in_size is a multiple of 8. */
151
int in_size = line_size * (swipeHeight);
152
int swipeBuf_size = in_size;
153
byte *buf1 = (byte *)gs_malloc(pdev->memory, in_size, 1, "lxm_print_page(buf1)");
155
(byte *)gs_malloc(pdev->memory, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");
158
/* Check allocations */
159
if ( buf1 == 0 || swipeBuf == 0 ) {
161
quit_ignomiously: /* and a goto into an if statement is pretty ignomious! */
162
gs_free(pdev->memory, (char *)buf1, in_size, 1, "lxm_print_page(buf1)");
164
gs_free(pdev->memory, (char *)swipeBuf, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");
165
return_error(gs_error_VMerror);
168
{ /* Initialize the printer and reset the margins. */
169
static const char init_string[] = {
174
fwrite(init_string, 1, sizeof(init_string), prn_stream);
176
/* Print lines of graphics */
177
for (lnum=0; lnum < pdev->height-swipeHeight ; ) { /* increment in body */
182
{ /* test for blank scan lines. We maintain the */
183
/* loop invariant lnum <pdev->height, but modify lnum */
186
for (l=lnum; l<pdev->height; l++) {
187
/* Copy 1 scan line and test for all zero. */
188
gdev_prn_get_bits(pdev, l, in, &in_data);
189
if ( in_data[0] != 0 ||
190
memcmp((char *)in_data, (char *)in_data + 1, line_size - 1)
196
/* now l is the next non-blank scan line */
197
if (l >= pdev->height) {/* if there are no more bits on this page */
199
break; /* end the loop and eject the page*/
202
/* leave room for following swipe to reinforce these bits */
203
if (l-lnum > overLap) lnum = l - overLap;
205
/* if the first non-blank near bottom of page */
206
if (lnum >=pdev->height - swipeHeight) {
207
/* don't move the printhead over empty air*/
208
lnum = pdev->height - swipeHeight;
213
/* Copy the the scan lines. */
214
lcnt = gdev_prn_copy_scan_lines(pdev, lnum, in, in_size);
215
if ( lcnt < swipeHeight ) {
216
/* Pad with lines of zeros. */
217
memset(in + lcnt * line_size, 0,
218
in_size - lcnt * line_size);
221
/* compute right and left margin for this swipe */
224
for (l=0; l<swipeHeight; l++) {/* for each line of swipe */
225
for (i=0; i<minX; i++) {/* look for left-most non-zero byte*/
226
if (in[l*line_size+i] !=0) {
231
for (i=line_size-1; i>=maxX; i--) {/* look for right-most */
232
if (in[l*line_size+i] !=0) {
238
minX = (minX&(-2)); /* truncate to even */
239
maxX = (maxX+3)&-2; /* raise to even */
243
extent = highestX -leastX +1;
247
/* macro, not fcn call. Space penalty is modest, speed helps */
248
#define buffer_store(x) if(outp-swipeBuf>=swipeBuf_size) {\
249
gs_free(pdev->memory, (char *)swipeBuf, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");\
251
swipeBuf = (byte *)gs_malloc(pdev->memory, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");\
252
if (swipeBuf == 0) goto quit_ignomiously;\
256
{/* work out the bytes to store for this swipe*/
258
int sx, sxBy8, sxMask;
259
int words[directorySize];
264
int i,b,x, directory ;
266
/* want to set up pointers for (upto two) stripes covered by the output*/
268
/* now for each column covered by output: */
269
for (x=leastX; x<=highestX; x++) {
270
for (i=0; i<directorySize; i++) {
273
directory = 0x2000; /* empty directory != 0 */
275
/* prime loops: make comparisons here */
278
sx = (x&1)==1 ? x : x-(((lxm_device*)pdev)->headSeparation);
279
j1 = (x&1); /* even if x even, odd if x odd */
281
default: /* shouldn't happen ... but compilation checks */
283
sx = (x&1)==0 ? x : x-((lxm_device*)pdev)->headSeparation;
284
j1 = 1-(x&1); /* odd if x even, even if x odd */
289
sxMask = 0x80>>(sx%8);
291
/* loop through all the swipeHeight bits of this column */
292
for (i = 0, b=1, y= sxBy8+j1*line_size; i < directorySize; i++,b<<=1) {
294
for (j=j1,c=c1 /*,y=i*16*line_size+sxBy8*/; j<16; j+=2, y+=2*line_size, c>>=2) {
301
if (!sum) directory |=b;
304
buffer_store(directory>>8); buffer_store(directory&0xff);
305
if (directory != 0x3fff) {
306
for (i=0; i<directorySize; i++) {
308
buffer_store(words[i]>>8) ; buffer_store(words[i]&0xff);
316
{/* now write out header, then buffered bits */
319
/* compute size of swipe, needed for header */
320
int sz = 0x1a + outp - swipeBuf;
323
int deltaY = 2*(leastY - lastY); /* vert coordinates here are 1200 dpi */
325
outByte(0x1b); outByte('*'); outByte(3);
326
outByte(deltaY>>8); outByte(deltaY&0xff);
327
outByte(0x1b); outByte('*'); outByte(4); outByte(0); outByte(0);
328
outByte(sz>>8); outByte(sz&0xff); outByte(0); outByte(3);
329
outByte(1); outByte(1); outByte(0x1a);
331
outByte(extent>>8); outByte(extent&0xff);
332
outByte(leastX>>8); outByte(leastX&0xff);
333
outByte(highestX>>8); outByte(highestX&0xff);
334
outByte(0); outByte(0);
335
outByte(0x22); outByte(0x33); outByte(0x44);
336
outByte(0x55); outByte(1);
338
fwrite(swipeBuf,1,outp-swipeBuf,prn_stream);
342
}/* ends the loop for swipes of the print head.*/
344
/* Eject the page and reinitialize the printer */
346
static const char bottom[] = {
347
fin() /*, looks like I can get away with only this much ...
354
fwrite(bottom, 1, sizeof(bottom), prn_stream);
358
gs_free(pdev->memory, (char *)swipeBuf, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");
359
gs_free(pdev->memory, (char *)buf1, in_size, 1, "lxm_print_page(buf1)");
364
* There are a number of parameters which can differ between ink cartridges.
365
* The Windows driver asks you to recalibrate every time you load a new
367
* most of the parameters adjusted there relate to color, and so this
368
* monotone driver doesn't need them. However, the Lexmark 5700 black
369
* cartridge has two columns of dots, separated by about 16 pixels.
370
* This `head separation' distance can vary between cartridges, so
371
* we provide a parameter to set it. In my small experience I've not
372
* set the corresponding parameter in windows to anything greater than 17
373
* or smaller than 15, but it would seem that it can vary from 1 to 32,
374
* based on the calibration choices offered.
376
* As I understand the rules laid out in gsparams.h,
377
* lxm_get_params is supposed to return the current values of parameters
378
* and lxm_put_params is supposed to set up values in the lxm_device
379
* structure which can be used by the lxm5700m_print_page routine.
380
* I've copied my routines from gdevcdj.c
384
lxm_get_params(gx_device *pdev, gs_param_list *plist)
386
lxm_device* const ldev = (lxm_device*)pdev;
387
int code = gdev_prn_get_params(pdev, plist);
389
if ( code < 0 ) return code;
390
code = param_write_int(plist,
392
(int *)&(ldev->headSeparation));
397
/* put_params is supposed to check all the parameters before setting any. */
399
lxm_put_params(gx_device *pdev, gs_param_list *plist)
402
lxm_device* const ldev = (lxm_device*)pdev;
403
int trialHeadSeparation=ldev->headSeparation;
404
int code = param_read_int(plist, "HeadSeparation", &trialHeadSeparation);
406
if ( trialHeadSeparation < 1 || trialHeadSeparation > 32 )
407
param_signal_error(plist, "HeadSeparation", gs_error_rangecheck);
408
/* looks like param_signal_error is not expected to return */
409
ecode = gdev_prn_put_params(pdev, plist); /* call super class put_params */
410
if ( code < 0 ) return code;
411
if (ecode < 0) return ecode;
413
/* looks like everything okay; go ahead and set headSeparation */
414
ldev->headSeparation = trialHeadSeparation;
415
if ( code == 1) return ecode; /* I guess this means there is no "HeadSeparation" parameter */