1 /* Copyright (C) 2001-2006 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, 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.
12 */
13 /* $Id: gdevlxm.c 8250 2007-09-25 13:31:24Z giles $*/
14 /*
15  * Lexmark 5700 ink-jet printer driver for Ghostscript
16  *
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.
20  *
21  * I use the command
22  * gs -sOutputFile=/dev/lp0 -sDevice=lxm5700m -dHeadSeparation=15 file.ps
23  *
24  * where HeadSeparation varies from print-cartridge to print-cartridge and
25  * 16 (the default) usually works fine.
26  *
27  *   Stephen Taylor  setaylor@ma.ultranet.com  staylor@cs.wpi.edu
28  */
29 
30 #include "gdevprn.h"
31 #include "gsparams.h"
32 
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);
38 
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);
43 
44 /* The device descriptors */
45 
46 /* define a subclass with useful state in it. */
47 typedef struct lxm_device_s { /* a sub-class of gx_device_printer */
48     gx_device_common;
49     gx_prn_device_common;
50     int headSeparation;
51 } lxm_device;
52 
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 */
61 };
62 
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.
93  */
94 /*
95  * various output shorthands
96  */
97 
98 #define init1() \
99 	top(), \
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
105 
106 #define init2() \
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 \
109 
110 #define init3()  \
111 	0x1b,'*', 7,0x73,0x30, \
112 	0x1b,'*', 'm', 0, 0x14, 3, 0x84, 2, 0, 1, 0xf4, \
113 	0x1b,'*', 7,0x63, \
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  \
117 
118 #define top()  \
119 	0xA5,0, 6, 0x40, 3,3,0xc0,0x0f,0x0f \
120 
121 #define fin()  \
122 	0x1b,'*', 7, 0x65 \
123 
124 
125 #define outByte(b) putc(b, prn_stream)
126 
127 #define RIGHTWARD 0
128 #define LEFTWARD 1
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 */
132 #define overLap 104
133 /* height of printhead in pixels */
134 #define swipeHeight 208
135 /* number of shorts described by each column directory */
136 #define directorySize 13
137 
138 /* ------ Driver procedures ------ */
139 
140 
141 /* Send the page to the printer. */
142 static int
lxm5700m_print_page(gx_device_printer * pdev,FILE * prn_stream)143 lxm5700m_print_page(gx_device_printer *pdev, FILE *prn_stream)
144 {
145     int lnum,minX, maxX, i, l, highestX, leastX, extent;
146     int direction = RIGHTWARD;
147     int lastY = 0;
148 
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)");
154     byte *swipeBuf =
155 	(byte *)gs_malloc(pdev->memory, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");
156     byte *in = buf1;
157 
158     /* Check allocations */
159     if ( buf1 == 0 || swipeBuf == 0 ) {
160 	if ( buf1 )
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)");
163 	if ( swipeBuf )
164 	    gs_free(pdev->memory, (char *)swipeBuf, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");
165 	return_error(gs_error_VMerror);
166     }
167 
168     {	/* Initialize the printer and reset the margins. */
169 	static const char init_string[] = {
170 	    init1(),
171 	    init2(),
172 	    init3()
173 	};
174 	fwrite(init_string, 1, sizeof(init_string), prn_stream);
175     }
176     /* Print lines of graphics */
177     for (lnum=0; lnum < pdev->height-swipeHeight ; ) { /* increment in body */
178 	byte *in_data;
179 	register byte *outp;
180 	int lcnt;
181 
182 	{	/* test for blank scan lines.  We  maintain the */
183 	    /* loop invariant lnum <pdev->height, but modify lnum */
184 	    int l;
185 
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)
191 		     ) {
192 		    break;
193 		}
194 	    }/* end for l */
195 
196 	    /* now l is the next non-blank scan line */
197 	    if (l >= pdev->height) {/* if there are no more bits on this page */
198 		lnum = l;
199 		break;			/* end the loop and eject the page*/
200 	    }
201 
202 	    /* leave room for following swipe to reinforce these bits */
203 	    if (l-lnum > overLap) lnum = l - overLap;
204 
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;
209 	    }
210 	}
211 
212 
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);
219 	}
220 
221 	/* compute right and left margin for this swipe */
222 	minX = line_size;
223 	maxX = 0;
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) {
227 		    minX = i;
228 		    break;
229 		}
230 	    }
231 	    for (i=line_size-1; i>=maxX; i--) {/* look for right-most */
232 		if (in[l*line_size+i] !=0) {
233 		    maxX = i;
234 		    break;
235 		}
236 	    }
237 	}
238 	minX = (minX&(-2)); /* truncate to even */
239 	maxX = (maxX+3)&-2; /* raise to even */
240 
241 	highestX = maxX*8-1;
242 	leastX = minX*8;
243 	extent = highestX -leastX +1;
244 
245 	outp = swipeBuf;
246 
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)");\
250 	    swipeBuf_size*=2;\
251 	    swipeBuf = (byte *)gs_malloc(pdev->memory, swipeBuf_size, 1, "lxm_print_page(swipeBuf)");\
252 	    if (swipeBuf == 0) goto quit_ignomiously;\
253 	    break;}\
254 	else *outp++ = (x)
255 
256 	    {/* work out the bytes to store for this swipe*/
257 
258 		int sx, sxBy8, sxMask;
259 		int words[directorySize];
260 		bool f, sum;
261 		int retval=0;
262 		int j,c,y;
263 		int j1,c1;
264 		int i,b,x, directory ;
265 
266 		/* want to set up pointers for (upto two) stripes covered by the output*/
267 
268 		/* now for each column covered by output: */
269 		for (x=leastX; x<=highestX; x++) {
270 		    for (i=0; i<directorySize; i++) {
271 			words[i] = 0;
272 		    }
273 		    directory = 0x2000;	/* empty directory != 0 */
274 
275 		    /* prime loops: make comparisons here */
276 		    switch (direction) {
277 		    case(RIGHTWARD):
278 			sx = (x&1)==1 ? x : x-(((lxm_device*)pdev)->headSeparation);
279 			j1 = (x&1);	/* even if x even, odd if x odd */
280 			break;
281 		    default:	/* shouldn't happen ... but compilation checks */
282 		    case(LEFTWARD):
283 			sx = (x&1)==0 ? x : x-((lxm_device*)pdev)->headSeparation;
284 			j1 = 1-(x&1);	/* odd if x even, even if x odd */
285 		    }
286 		    c1 = 0x8000 >> j1;
287 
288 		    sxBy8 = sx/8;
289 		    sxMask = 0x80>>(sx%8);
290 
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) {
293 			sum = false;
294 			for (j=j1,c=c1 /*,y=i*16*line_size+sxBy8*/; j<16; j+=2, y+=2*line_size, c>>=2) {
295 			    f = (in[y]&sxMask);
296 			    if (f) {
297 				words[i] |= c;
298 				sum |= f;
299 			    }
300 			}
301 			if (!sum) directory |=b;
302 		    }
303 		    retval+=2;
304 		    buffer_store(directory>>8); buffer_store(directory&0xff);
305 		    if (directory != 0x3fff) {
306 			for (i=0; i<directorySize; i++) {
307 			    if (words[i] !=0) {
308 				buffer_store(words[i]>>8) ; buffer_store(words[i]&0xff);
309 				retval += 2;
310 			    }
311 			}
312 		    }
313 		}
314 #undef buffer_store
315 	    }
316 	{/* now write out header, then buffered bits */
317 	    int leastY = lnum;
318 
319 	    /* compute size of swipe, needed for header */
320 	    int sz = 0x1a + outp - swipeBuf;
321 
322 	    /* put out header*/
323 	    int deltaY = 2*(leastY - lastY);  /* vert coordinates here are 1200 dpi */
324 	    lastY = leastY;
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);
330 	    outByte(0);
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);
337 	    /* put out bytes */
338 	    fwrite(swipeBuf,1,outp-swipeBuf,prn_stream);
339 	}
340 	    lnum += overLap;
341 	    direction ^= 1;
342     }/* ends the loop for swipes of the print head.*/
343 
344     /* Eject the page and reinitialize the printer */
345     {
346 	static const char bottom[] = {
347 	    fin() /*,  looks like I can get away with only this much ...
348 	    init1(),
349 	    init3(),
350 	    fin()   ,
351 	    top(),
352 	    fin()  */
353 	};
354 	fwrite(bottom, 1, sizeof(bottom), prn_stream);
355     }
356     fflush(prn_stream);
357 
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)");
360     return 0;
361 }
362 
363 /*
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
366  * cartridge.
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.
375  *
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
381  */
382 
383 static int
lxm_get_params(gx_device * pdev,gs_param_list * plist)384 lxm_get_params(gx_device *pdev, gs_param_list *plist)
385 {
386     lxm_device* const ldev = (lxm_device*)pdev;
387     int code = gdev_prn_get_params(pdev, plist);
388 
389     if ( code < 0 ) return code;
390     code = param_write_int(plist,
391 			   "HeadSeparation",
392 			   (int *)&(ldev->headSeparation));
393 
394     return code;
395 }
396 
397 /* put_params is supposed to check all the parameters before setting any. */
398 static int
lxm_put_params(gx_device * pdev,gs_param_list * plist)399 lxm_put_params(gx_device *pdev, gs_param_list *plist)
400 {
401     int ecode;
402     lxm_device* const ldev = (lxm_device*)pdev;
403     int trialHeadSeparation=ldev->headSeparation;
404     int code = param_read_int(plist, "HeadSeparation", &trialHeadSeparation);
405 
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;
412 
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 */
416     return 0;
417 }
418