xref: /plan9/sys/src/cmd/gs/src/gdevlxm.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1989-1994, 1998, 1999 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gdevlxm.c,v 1.5 2004/08/10 13:02:36 stefan Exp $*/
18 /*
19  * Lexmark 5700 ink-jet printer driver for Ghostscript
20  *
21  * defines the lxm5700m device for printing in black-and-white at 1200 dpi
22  * doesn't handle color or any other resolution.
23  * Native resolution appears to be 600 x 1200, but print bands are overlapped.
24  *
25  * I use the command
26  * gs -sOutputFile=/dev/lp0 -sDevice=lxm5700m -dHeadSeparation=15 file.ps
27  *
28  * where HeadSeparation varies from print-cartridge to print-cartridge and
29  * 16 (the default) usually works fine.
30  *
31  *   Stephen Taylor  setaylor@ma.ultranet.com  staylor@cs.wpi.edu
32  */
33 
34 #include "gdevprn.h"
35 #include "gsparams.h"
36 
37 /* The procedure descriptors */
38 /* declare functions */
39 private dev_proc_print_page(lxm5700m_print_page);
40 private dev_proc_get_params(lxm_get_params);
41 private dev_proc_put_params(lxm_put_params);
42 
43 /* set up dispatch table.  I follow gdevdjet in using gdev_prn_output_page */
44 static const gx_device_procs lxm5700m_procs =
45     prn_params_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
46                      lxm_get_params, lxm_put_params);
47 
48 /* The device descriptors */
49 
50 /* define a subclass with useful state in it. */
51 typedef struct lxm_device_s { /* a sub-class of gx_device_printer */
52     gx_device_common;
53     gx_prn_device_common;
54     int headSeparation;
55 } lxm_device;
56 
57 /* Standard lxm5700m device */
58 lxm_device far_data gs_lxm5700m_device = {
59     prn_device_std_body(lxm_device, lxm5700m_procs, "lxm5700m",
60 	DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
61 	600, 600,	/* x dpi, y dpi */
62 	0.2, 0.0, 0.0, 0.0,			/* margins */
63 	1, lxm5700m_print_page),
64     16   /* default headSeparation value */
65 };
66 
67 /* I don't know the whole protocol for the printer, but let me tell
68  * you about the fraction I use.
69  * Each page begins with a header, which I describe as init1, init2, init3 --
70  *  (I separate them, because I've seen output in which those sections
71  *   seemed to appear independently, but I've deleted the only code I
72  *   had that actually used them separately.)
73  * Then there are a number of swipe commands, each of which describes one
74  * swipe of the printhead.  Each swipe command begins with a fixed
75  * header, which gives the number of bytes in the command,
76  * left and right margins,
77  * a vertical offset, some noise characters I don't know the meaning of,
78  * and the table of bits.  The bits are given one column at a time, using a
79  * simple compression scheme: a directory, consisting of two bytes, tells
80  * which sixteen-bit intervals of the 208-bit column contain 1-bits; then
81  * the appropriate number of sixteen-bit bit-maps follow.  (A zero-bit in the
82  * directory indicates that a bitmap for that sector follows.) In the worst case,
83  * this scheme would be bigger than the uncompressed bitmap, but it seems to
84  * usually save 80-90%.  The biggest complication of the bitmap scheme is this:
85  * There are two print-heads on the black cartridge, and they are 16 pixels
86  * (or headSeparation) apart.  Odd columns of the swipe address one printhead,
87  * and even columns the other.  On the following swipe, the printheads
88  * addressed by even and odd columns are reversed.  I think the printheads might be
89  * staggered, but the output I've seen staggers things in software;
90  * adjacent vertical bits on the same head are not addressed; the missing
91  * bits are written in by the second head when it passes (16 columns later.)
92  * In my code, I call the state of addressing one head or the other "direction".
93  * Originally I thought that the printhead was writing on both leftward and
94  * rightward motion, and that which head was addressed by odd columns changed
95  * accordingly.  I'm no longer sure this is true, but the head addressed by the
96  * even columns does alternate with each swipe.
97  */
98 /*
99  * various output shorthands
100  */
101 
102 #define init1() \
103 	top(), \
104 	0xA5,0, 3, 0x40,4,5, \
105 	0xA5,0, 3, 0x40,4,6, \
106 	0xA5,0, 3, 0x40,4,7, \
107 	0xA5,0, 3, 0x40,4,8, \
108 	0xA5,0, 4, 0x40,0xe0,0x0b, 3
109 
110 #define init2() \
111 	0xA5,0, 11, 0x40,0xe0,0x41, 0,0,0,0,0,0,0, 2, \
112 	0xA5,0, 6, 0x40, 5, 0,0,0x80,0 \
113 
114 #define init3()  \
115 	0x1b,'*', 7,0x73,0x30, \
116 	0x1b,'*', 'm', 0, 0x14, 3, 0x84, 2, 0, 1, 0xf4, \
117 	0x1b,'*', 7,0x63, \
118 	0x1b,'*', 'm', 0, 0x42,  0, 0, \
119 	0xA5,0, 5, 0x40,0xe0,0x80, 8, 7, \
120 	0x1b,'*', 'm', 0, 0x40, 0x15, 7, 0x0f, 0x0f  \
121 
122 #define top()  \
123 	0xA5,0, 6, 0x40, 3,3,0xc0,0x0f,0x0f \
124 
125 #define fin()  \
126 	0x1b,'*', 7, 0x65 \
127 
128 
129 #define outByte(b) putc(b, prn_stream)
130 
131 #define RIGHTWARD 0
132 #define LEFTWARD 1
133 /* number of pixels between even columns in output and odd ones*/
134 /* #define headSeparation 16 */
135 /* overlap between successive swipes of the print head */
136 #define overLap 104
137 /* height of printhead in pixels */
138 #define swipeHeight 208
139 /* number of shorts described by each column directory */
140 #define directorySize 13
141 
142 /* ------ Driver procedures ------ */
143 
144 
145 /* Send the page to the printer. */
146 private int
lxm5700m_print_page(gx_device_printer * pdev,FILE * prn_stream)147 lxm5700m_print_page(gx_device_printer *pdev, FILE *prn_stream)
148 {
149     int lnum,minX, maxX, i, l, highestX, leastX, extent;
150     int direction = RIGHTWARD;
151     int lastY = 0;
152 
153     int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
154     /* Note that in_size is a multiple of 8. */
155     int in_size = line_size * (swipeHeight);
156     int swipeBuf_size = in_size;
157     byte *buf1 = (byte *)gs_malloc(pdev->memory, in_size, 1, "lxm_print_page(buf1)");
158     byte *swipeBuf =
159 	(byte *)gs_malloc(pdev->memory, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");
160     byte *in = buf1;
161 
162     /* Check allocations */
163     if ( buf1 == 0 || swipeBuf == 0 ) {
164 	if ( buf1 )
165 quit_ignomiously: /* and a goto into an if statement is pretty ignomious! */
166 	gs_free(pdev->memory, (char *)buf1, in_size, 1, "lxm_print_page(buf1)");
167 	if ( swipeBuf )
168 	    gs_free(pdev->memory, (char *)swipeBuf, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");
169 	return_error(gs_error_VMerror);
170     }
171 
172     {	/* Initialize the printer and reset the margins. */
173 	static const char init_string[] = {
174 	    init1(),
175 	    init2(),
176 	    init3()
177 	};
178 	fwrite(init_string, 1, sizeof(init_string), prn_stream);
179     }
180     /* Print lines of graphics */
181     for (lnum=0; lnum < pdev->height-swipeHeight ; ) { /* increment in body */
182 	byte *in_data;
183 	register byte *outp;
184 	int lcnt;
185 
186 	{	/* test for blank scan lines.  We  maintain the */
187 	    /* loop invariant lnum <pdev->height, but modify lnum */
188 	    int l;
189 
190 	    for (l=lnum; l<pdev->height; l++) {
191 		/* Copy 1 scan line and test for all zero. */
192 		gdev_prn_get_bits(pdev, l, in, &in_data);
193 		if ( in_data[0] != 0 ||
194 		     memcmp((char *)in_data, (char *)in_data + 1, line_size - 1)
195 		     ) {
196 		    break;
197 		}
198 	    }/* end for l */
199 
200 	    /* now l is the next non-blank scan line */
201 	    if (l >= pdev->height) {/* if there are no more bits on this page */
202 		lnum = l;
203 		break;			/* end the loop and eject the page*/
204 	    }
205 
206 	    /* leave room for following swipe to reinforce these bits */
207 	    if (l-lnum > overLap) lnum = l - overLap;
208 
209 	    /* if the first non-blank near bottom of page */
210 	    if (lnum >=pdev->height - swipeHeight) {
211 		/* don't move the printhead over empty air*/
212 		lnum = pdev->height - swipeHeight;
213 	    }
214 	}
215 
216 
217 	/* Copy the the scan lines. */
218 	lcnt = gdev_prn_copy_scan_lines(pdev, lnum, in, in_size);
219 	if ( lcnt < swipeHeight ) {
220 	    /* Pad with lines of zeros. */
221 	    memset(in + lcnt * line_size, 0,
222 		   in_size - lcnt * line_size);
223 	}
224 
225 	/* compute right and left margin for this swipe */
226 	minX = line_size;
227 	maxX = 0;
228 	for (l=0; l<swipeHeight; l++) {/* for each line of swipe */
229 	    for (i=0; i<minX; i++) {/* look for left-most non-zero byte*/
230 		if (in[l*line_size+i] !=0) {
231 		    minX = i;
232 		    break;
233 		}
234 	    }
235 	    for (i=line_size-1; i>=maxX; i--) {/* look for right-most */
236 		if (in[l*line_size+i] !=0) {
237 		    maxX = i;
238 		    break;
239 		}
240 	    }
241 	}
242 	minX = (minX&(-2)); /* truncate to even */
243 	maxX = (maxX+3)&-2; /* raise to even */
244 
245 	highestX = maxX*8-1;
246 	leastX = minX*8;
247 	extent = highestX -leastX +1;
248 
249 	outp = swipeBuf;
250 
251 	/* macro, not fcn call.  Space penalty is modest, speed helps */
252 #define buffer_store(x) if(outp-swipeBuf>=swipeBuf_size) {\
253 	    gs_free(pdev->memory, (char *)swipeBuf, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");\
254 	    swipeBuf_size*=2;\
255 	    swipeBuf = (byte *)gs_malloc(pdev->memory, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");\
256 	    if (swipeBuf == 0) goto quit_ignomiously;\
257 	    break;}\
258 	else *outp++ = (x)
259 
260 	    {/* work out the bytes to store for this swipe*/
261 
262 		int sx, sxBy8, sxMask;
263 		int words[directorySize];
264 		bool f, sum;
265 		int retval=0;
266 		int j,c,y;
267 		int j1,c1;
268 		int i,b,x, directory ;
269 
270 		/* want to set up pointers for (upto two) stripes covered by the output*/
271 
272 		/* now for each column covered by output: */
273 		for (x=leastX; x<=highestX; x++) {
274 		    for (i=0; i<directorySize; i++) {
275 			words[i] = 0;
276 		    }
277 		    directory = 0x2000;	/* empty directory != 0 */
278 
279 		    /* prime loops: make comparisons here */
280 		    switch (direction) {
281 		    case(RIGHTWARD):
282 			sx = (x&1)==1 ? x : x-(((lxm_device*)pdev)->headSeparation);
283 			j1 = (x&1);	/* even if x even, odd if x odd */
284 			break;
285 		    default:	/* shouldn't happen ... but compilation checks */
286 		    case(LEFTWARD):
287 			sx = (x&1)==0 ? x : x-((lxm_device*)pdev)->headSeparation;
288 			j1 = 1-(x&1);	/* odd if x even, even if x odd */
289 		    }
290 		    c1 = 0x8000 >> j1;
291 
292 		    sxBy8 = sx/8;
293 		    sxMask = 0x80>>(sx%8);
294 
295 		    /* loop through all the swipeHeight bits of this column */
296 		    for (i = 0, b=1, y= sxBy8+j1*line_size; i < directorySize; i++,b<<=1) {
297 			sum = false;
298 			for (j=j1,c=c1 /*,y=i*16*line_size+sxBy8*/; j<16; j+=2, y+=2*line_size, c>>=2) {
299 			    f = (in[y]&sxMask);
300 			    if (f) {
301 				words[i] |= c;
302 				sum |= f;
303 			    }
304 			}
305 			if (!sum) directory |=b;
306 		    }
307 		    retval+=2;
308 		    buffer_store(directory>>8); buffer_store(directory&0xff);
309 		    if (directory != 0x3fff) {
310 			for (i=0; i<directorySize; i++) {
311 			    if (words[i] !=0) {
312 				buffer_store(words[i]>>8) ; buffer_store(words[i]&0xff);
313 				retval += 2;
314 			    }
315 			}
316 		    }
317 		}
318 #undef buffer_store
319 	    }
320 	{/* now write out header, then buffered bits */
321 	    int leastY = lnum;
322 
323 	    /* compute size of swipe, needed for header */
324 	    int sz = 0x1a + outp - swipeBuf;
325 
326 	    /* put out header*/
327 	    int deltaY = 2*(leastY - lastY);  /* vert coordinates here are 1200 dpi */
328 	    lastY = leastY;
329 	    outByte(0x1b); outByte('*'); outByte(3);
330 	    outByte(deltaY>>8); outByte(deltaY&0xff);
331 	    outByte(0x1b); outByte('*'); outByte(4); outByte(0); outByte(0);
332 	    outByte(sz>>8); outByte(sz&0xff); outByte(0); outByte(3);
333 	    outByte(1); outByte(1); outByte(0x1a);
334 	    outByte(0);
335 	    outByte(extent>>8); outByte(extent&0xff);
336 	    outByte(leastX>>8); outByte(leastX&0xff);
337 	    outByte(highestX>>8); outByte(highestX&0xff);
338 	    outByte(0); outByte(0);
339 	    outByte(0x22); outByte(0x33); outByte(0x44);
340 	    outByte(0x55); outByte(1);
341 	    /* put out bytes */
342 	    fwrite(swipeBuf,1,outp-swipeBuf,prn_stream);
343 	}
344 	    lnum += overLap;
345 	    direction ^= 1;
346     }/* ends the loop for swipes of the print head.*/
347 
348     /* Eject the page and reinitialize the printer */
349     {
350 	static const char bottom[] = {
351 	    fin() /*,  looks like I can get away with only this much ...
352 	    init1(),
353 	    init3(),
354 	    fin()   ,
355 	    top(),
356 	    fin()  */
357 	};
358 	fwrite(bottom, 1, sizeof(bottom), prn_stream);
359     }
360     fflush(prn_stream);
361 
362     gs_free(pdev->memory, (char *)swipeBuf, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");
363     gs_free(pdev->memory, (char *)buf1, in_size, 1, "lxm_print_page(buf1)");
364     return 0;
365 }
366 
367 /*
368  * There are a number of parameters which can differ between ink cartridges.
369  * The Windows driver asks you to recalibrate every time you load a new
370  * cartridge.
371  * most of the parameters adjusted there relate to color, and so this
372  * monotone driver doesn't need them.  However, the Lexmark 5700 black
373  * cartridge has two columns of dots, separated by about 16 pixels.
374  * This `head separation' distance can vary between cartridges, so
375  * we provide a parameter to set it.  In my small experience I've not
376  * set the corresponding parameter in windows to anything greater than 17
377  * or smaller than 15, but it would seem that it can vary from 1 to 32,
378  * based on the calibration choices offered.
379  *
380  * As I understand the rules laid out in gsparams.h,
381  * lxm_get_params is supposed to return the current values of parameters
382  * and lxm_put_params is supposed to set up values in the lxm_device
383  * structure which can be used by the lxm5700m_print_page routine.
384  * I've copied my routines from gdevcdj.c
385  */
386 
387 private int
lxm_get_params(gx_device * pdev,gs_param_list * plist)388 lxm_get_params(gx_device *pdev, gs_param_list *plist)
389 {
390     lxm_device* const ldev = (lxm_device*)pdev;
391     int code = gdev_prn_get_params(pdev, plist);
392 
393     if ( code < 0 ) return code;
394     code = param_write_int(plist,
395 			   "HeadSeparation",
396 			   (int *)&(ldev->headSeparation));
397 
398     return code;
399 }
400 
401 /* put_params is supposed to check all the parameters before setting any. */
402 private int
lxm_put_params(gx_device * pdev,gs_param_list * plist)403 lxm_put_params(gx_device *pdev, gs_param_list *plist)
404 {
405     int ecode;
406     lxm_device* const ldev = (lxm_device*)pdev;
407     int trialHeadSeparation=ldev->headSeparation;
408     int code = param_read_int(plist, "HeadSeparation", &trialHeadSeparation);
409 
410     if ( trialHeadSeparation < 1 || trialHeadSeparation > 32 )
411 	param_signal_error(plist, "HeadSeparation", gs_error_rangecheck);
412     /* looks like param_signal_error is not expected to return */
413     ecode = gdev_prn_put_params(pdev, plist);	/* call super class put_params */
414     if ( code < 0 ) return code;
415     if (ecode < 0) return ecode;
416 
417     /* looks like everything okay; go ahead and set headSeparation */
418     ldev->headSeparation = trialHeadSeparation;
419     if ( code == 1) return ecode; /* I guess this means there is no "HeadSeparation" parameter */
420     return 0;
421 }
422