Debian Codegraph @

converter/pbm/pbmtoppa/ppa.c

raw
1 /* ppa.c
2 * functions to handle PPA communications
3 * Copyright (c) 1998 Tim Norman. See LICENSE for details.
4 * 2-25-98
5 *
6 * Mar 3, 1998 Jim Peterson <[email protected]>
7 *
8 * Restructured to accommodate both the HP820/1000, and HP720 series.
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "pm_config.h"
16 /* pm_config.h is necessary for __inline__ for those compilers that don't
17 define __inline__ themselves
18 */
19 #include "ppa.h"
20
21 /*
22 VLink packet structure:
23
24 Bytes Description
25 --------------------------------------------
26 1 Packet-start marker (always '$')
27 1 Channel (0: image data, 1: commands/responses(*), 2: autostatus(*), 128: pacing(*))
28 2 Data length (N)
29 N [remaining data (e.g., SCP or SCP2 packet when Channel=1)]
30
31 (*): responses, autostatus, and pacing are communicated from the printer to
32 the computer, and may be safely ignored.
33 */
34 static void
35 vlink_put(FILE *fptr, int channel, int length, void *data)
36 {
37 fputc ('$', fptr);
38 fputc (channel, fptr);
39 fputc (length>>8, fptr); fputc (length&0xFF, fptr);
40 fwrite (data, length, 1, fptr);
41 }
42
43
44
45 /*
46 SCP packet structure:
47
48 Bytes Description
49 --------------------------------------------
50 2 Command specifier
51 2 Command reference number
52 1 Priority
53 1 padding? (always zero)
54 2 Data length (N)
55 N [remaining data]
56
57 ComSpec ComRef Command
58 -------------------------------
59 35 1 Initialize1
60 101 2 Initialize2
61 21 1 Initialize3
62 19 1 Handle Media
63 18 1 Print Sweep
64 */
65 static void
66 scp_put(FILE *fptr, int comspec, int comref, int priority,
67 int length, void *data)
68 {
69 /* encapsulate the vlink_put call in here, to avoid a memcpy */
70 fputc ('$', fptr);
71 fputc (1, fptr);
72 fputc ((length+8)>>8, fptr); fputc ((length+8)&0xFF, fptr);
73
74 fputc (comspec>>8, fptr); fputc (comspec&0xFF, fptr);
75 fputc (comref>>8, fptr); fputc (comref&0xFF, fptr);
76 fputc (priority, fptr);
77 fputc (0, fptr);
78 fputc (length>>8, fptr); fputc (length&0xFF, fptr);
79
80 fwrite (data, length, 1, fptr);
81 }
82
83
84
85 /*
86 SCP2 packet structure:
87
88 Bytes Description
89 --------------------------------------------
90 2 Command specifier
91 2 Packet length (N)
92 1 Priority
93 1 padding? (always zero)
94 2 Command reference number
95 4 Channel 0 buffer increment
96 4 version number? (always 00 02 00 00)
97 N-16 [remaining data]
98
99 ComSpec ComRef Command
100 -------------------------------
101 0x186 1 Initialize1
102 0x18f 2 Initialize2
103 0x183 1 Initialize3
104 0x181 1 Handle Media
105 0x180 1 Print Sweep
106 */
107 static void
108 scp2_put(FILE *fptr,unsigned short comspec,unsigned short pkt_len_s16,
109 unsigned char priority,unsigned short comref,unsigned data_len,
110 void *data)
111 {
112 /* encapsulate the vlink_put call in here, to avoid a memcpy */
113 fputc ('$', fptr);
114 fputc (1, fptr);
115 fputc ((pkt_len_s16+16)>>8, fptr); fputc ((pkt_len_s16+16), fptr);
116
117 fputc (comspec>>8, fptr); fputc (comspec, fptr);
118 fputc ((pkt_len_s16+16)>>8, fptr); fputc ((pkt_len_s16+16), fptr);
119 fputc (priority, fptr);
120 fputc (0, fptr);
121 fputc (comref>>8, fptr);
122 fputc (comref, fptr);
123 fputc (data_len>>24, fptr); fputc (data_len>>16, fptr);
124 fputc (data_len>>8, fptr); fputc (data_len, fptr);
125 fputc (0, fptr);
126 fputc (2, fptr);
127 fputc (0, fptr);
128 fputc (0, fptr);
129
130 fwrite (data, pkt_len_s16, 1, fptr);
131 }
132
133
134
135 /*
136 SCP3 packet structure:
137
138 Bytes Description
139 --------------------------------------------
140 2 Command specifier
141 2 Packet length (N)
142 1 Priority
143 1 padding? (always zero)
144 2 Command reference number
145 4 Channel 0 buffer increment
146 4 version number? (always 01 04 00 00)
147 N-16 [remaining data]
148
149 ComSpec ComRef Command
150 -------------------------------
151 0x186 1 Initialize1
152 0x18C 16 Initialize Printer name
153 0x1A1 1 Initialize4?
154 0x18f 2 Initialize2
155 0x183 1 Initialize3
156 0x181 1 Handle Media
157 0x180 1 Print Sweep
158 */
159 static void
160 scp3_put(FILE *fptr,unsigned short comspec,unsigned short pkt_len_s16,
161 unsigned char priority,unsigned short comref,unsigned data_len,
162 void *data)
163 {
164 /* encapsulate the vlink_put call in here, to avoid a memcpy */
165 fputc ('$', fptr);
166 fputc (1, fptr);
167 fputc ((pkt_len_s16+16)>>8, fptr); fputc ((pkt_len_s16+16), fptr);
168
169 fputc (comspec>>8, fptr); fputc (comspec, fptr);
170 fputc ((pkt_len_s16+16)>>8, fptr); fputc ((pkt_len_s16+16), fptr);
171 fputc (priority, fptr);
172 fputc (0, fptr);
173 fputc (comref>>8, fptr);
174 fputc (comref, fptr);
175 fputc (data_len>>24, fptr); fputc (data_len>>16, fptr);
176 fputc (data_len>>8, fptr); fputc (data_len, fptr);
177 fputc (1, fptr);
178 fputc (4, fptr);
179 fputc (0, fptr);
180 fputc (0, fptr);
181
182 fwrite (data, pkt_len_s16, 1, fptr);
183 }
184
185
186
187 void ppa_init_job(ppa_stat* prn)
188 {
189 unsigned char init1[8] = { 0x00, 0x00, 0x01, 0xf4, 0x01, 0x00, 0x00, 0x00 };
190 unsigned char init2[4] = { 0xde, 0xad, 0xbe, 0xef };
191 unsigned char init3[8] = { 0xde, 0xad, 0xbe, 0xef, 0x02, 0x00, 0x00, 0x00 };
192 unsigned char init4[60] = "!!TAZ \x81*HP DeskJet 1000C Prin (Copy 2)*FILE!!\x00\x00\x00"; /* plus 0 terminator */
193 unsigned char init5[4] = { 0x01, 0x01, 0x00, 0x00 };
194
195 switch(prn->version)
196 {
197 case HP820:
198 scp_put (prn->fptr, 35, 1, 7, sizeof(init1), init1);
199 vlink_put (prn->fptr, 0, sizeof(init2), init2);
200 scp_put (prn->fptr, 101, 2, 7, sizeof(init3), init3);
201 break;
202 case HP720:
203 scp2_put (prn->fptr, 0x0186, sizeof(init1), 7, 1, 0, init1);
204 vlink_put(prn->fptr, 0, sizeof(init2), init2);
205 scp2_put (prn->fptr, 0x018f, sizeof(init3), 7, 2, 4, init3);
206 break;
207 case HP1000:
208 scp3_put (prn->fptr, 0x0186, sizeof(init1), 7, 16, 0, init1);
209 scp3_put (prn->fptr, 0x018C, sizeof(init4), 7, 1, 0, init4);
210 scp3_put (prn->fptr, 0x01A1, sizeof(init5), 7, 1, 0, init5);
211 vlink_put (prn->fptr, 0, sizeof(init2), init2);
212 scp3_put (prn->fptr, 0x018f, sizeof(init3), 7, 2, 4, init3);
213 break;
214 default:
215 fprintf(stderr,"ppa_init_job(): unknown printer version\n");
216 }
217 }
218
219
220
221 void ppa_end_print(ppa_stat* prn)
222 {
223 unsigned char pageA[4] = { 0x05, 0x01, 0x03, 0x84 };
224
225 if (prn->version == HP1000)
226 scp3_put (prn->fptr, 0x0181, sizeof(pageA), 7, 2, 0, pageA);
227 }
228
229
230
231 void ppa_init_page(ppa_stat* prn)
232 {
233 unsigned char pageA[16] = {0x28, 0x2d, 0x00, 0x41, 0x29, 0x2e, 0x00, 0x42,
234 0x29, 0x2e, 0x00, 0x42, 0x29, 0x2e, 0x00, 0x42 };
235 unsigned char pageB[16] = {0x28, 0x2d, 0x00, 0x41, 0x2d, 0x32, 0x00, 0x46,
236 0x2d, 0x32, 0x00, 0x46, 0x2d, 0x32, 0x00, 0x46 };
237
238 switch(prn->version)
239 {
240 case HP820:
241 scp_put (prn->fptr, 21, 1, 5, sizeof(pageA), pageA);
242 break;
243 case HP720:
244 scp2_put (prn->fptr, 0x0183, sizeof(pageB), 5, 1, 0, pageB);
245 break;
246 case HP1000:
247 scp3_put (prn->fptr, 0x0183, sizeof(pageA), 5, 1, 0, pageA);
248 break;
249 default:
250 fprintf(stderr,"ppa_init_page(): unknown printer version\n");
251 }
252 }
253
254
255
256 void ppa_load_page(ppa_stat* prn)
257 {
258 unsigned char loadA[4] = {0x01, 0x01, 0x09, 0x60 };
259 unsigned char loadB[4] = {0x01, 0x01, 0x12, 0xc0 };
260 unsigned char loadC[4] = {0x01, 0x01, 0x07, 0x08 };
261
262 switch(prn->version)
263 {
264 case HP820:
265 scp_put (prn->fptr, 19, 1, 7, sizeof(loadA), loadA);
266 break;
267 case HP720:
268 scp2_put (prn->fptr, 0x0181, sizeof(loadB), 7, 1, 0, loadB);
269 break;
270 case HP1000:
271 scp3_put (prn->fptr, 0x0181, sizeof(loadC), 7, 1, 0, loadC);
272 break;
273 default:
274 fprintf(stderr,"ppa_load_page(): unknown printer version\n");
275 }
276 }
277
278
279
280 void ppa_eject_page(ppa_stat* prn)
281 {
282 unsigned char loadA[4] = {0x02, 0x01, 0x09, 0x60 };
283 unsigned char loadB[4] = {0x02, 0x01, 0x12, 0xc0 };
284 unsigned char loadC[4] = {0x02, 0x01, 0x07, 0x08 };
285
286 switch(prn->version)
287 {
288 case HP820:
289 scp_put (prn->fptr, 19, 1, 7, sizeof(loadA), loadA);
290 break;
291 case HP720:
292 scp2_put (prn->fptr, 0x0181, sizeof(loadB), 7, 1, 0, loadB);
293 break;
294 case HP1000:
295 scp3_put (prn->fptr, 0x0181, sizeof(loadC), 7, 1, 0, loadC);
296 break;
297 default:
298 fprintf(stderr,"ppa_eject_page(): unknown printer version\n");
299 }
300 }
301
302
303
304 static int
305 compress(unsigned char *in, int num_lines_d2, int final_len,
306 unsigned char *iout)
307 {
308 unsigned char* out = iout;
309 int I,len=num_lines_d2;
310 for(I=0; I<final_len; I+=num_lines_d2, in+=num_lines_d2)
311 {
312 int i = 0;
313 while (i < len) {
314 /* Find the size of duplicate values */
315 int dup_len = 0;
316 while ((i + dup_len < len)
317 && (in[i + dup_len] == in[i])) {
318 dup_len++;
319 }
320 /* See if we have enough zeros to be worth compressing. */
321 /* I figure one is enough. */
322 if ((dup_len >= 1) && (in[i] == 0)) {
323 /* Output run of zeros. */
324 while (dup_len >= 128) {
325 /* Max is 128 */
326 *out++ = 0x00;
327 i += 128;
328 dup_len -= 128;
329 }
330 if (dup_len >= 1)
331 {
332 *out++ = dup_len;
333 i += dup_len;
334 }
335 /* See if we have enough non-zeros to be worth compressing. */
336 /* Here two should be enough. */
337 }
338 else if (dup_len >= 2)
339 {
340 /* Output run of duplicates. */
341 while (dup_len >= 64) {
342 /* Max is 64 */
343 *out++ = 0x80;
344 *out++ = in[i];
345 i += 64;
346 dup_len -= 64;
347 }
348 if (dup_len >= 2)
349 {
350 *out++ = dup_len + 0x80;
351 *out++ = in[i];
352 i += dup_len;
353 }
354 }
355 else
356 {
357 /* Look for two zeros, or three duplicates to end literal run. */
358 /* Note this is one more than the number to start a run. */
359 int lit_len = -1;
360 int add_more = 1;
361 while (add_more) {
362 lit_len++;
363 if (i + lit_len == len) add_more = 0;
364 /* Always add more if we are near the very end. */
365 if (i + lit_len < len - 3) {
366 char a = in[i + lit_len + 0];
367 char b = in[i + lit_len + 1];
368 char c = in[i + lit_len + 2];
369 /* See if there are enough zeros */
370 if ((a == b) && (b == 0)) add_more = 0;
371 /* See if there are enough duplicates */
372 if ((a == b) && (b == c)) add_more = 0;
373 }
374 }
375 /* Output run of literals. */
376 while (lit_len >= 64) {
377 /* Max is 64 */
378 int j;
379 *out++ = 0xc0;
380 for (j = i; j < i + 64; j++) {
381 *out++ = in[j];
382 }
383 i += 64;
384 lit_len -= 64;
385 }
386 if (lit_len) {
387 int j;
388 *out++ = lit_len + 0xc0;
389 for (j = i; j < i + lit_len; j++) {
390 *out++ = in[j];
391 }
392 i += lit_len;
393 }
394 }
395 }
396 }
397 return out-iout;
398 }
399
400
401
402 static void __inline__ place_2bytes(int x,unsigned char* y)
403 { y[0]=x>>8; y[1]=x; }
404 static void __inline__ place_4bytes(int x,unsigned char* y)
405 { place_2bytes(x>>16,y); place_2bytes(x,y+2); }
406
407 #define do_compress_data (1) /* Compress. */
408 /* The no-compression case has not been well tested 2015.05.31 */
409
410 void ppa_print_sweep(ppa_stat* prn,ppa_sweep_data* data)
411 {
412 unsigned char* pc, *tpc;
413 unsigned i,datasize=data->data_size;
414 unsigned char sweep_packet[144];
415 int sweep_packet_size;
416 unsigned short HP720constants[] = { 0x8ca0, 0x4650, 0x12c0 };
417 unsigned short HP820constants[] = { 0x4650, 0x1c20, 0x0960 };
418 unsigned short HP1000constants[] = { 0x4650, 0x2328, 0x0708 };
419 unsigned short* constants;
420 int nozzle_data_size;
421 int MF; /* Multiplicative Factor -- quick hack */
422
423 if(do_compress_data)
424 {
425 if( !( pc = malloc( datasize * 2 + 1 )) ) /* Worst case + margin */
426 {
427 fprintf(stderr,"ppa_print_sweep(): could not malloc storage for compressed data\n");
428 exit(-1);
429 }
430 datasize=compress(data->image_data,data->nozzle_data->pins_used_d2,datasize,pc);
431 }
432
433 /* send image data 16k at a time */
434 for(i=0, tpc= do_compress_data ? pc : data->image_data;
435 i<datasize; tpc+=16384, i+=16384)
436 vlink_put(prn->fptr, 0, datasize-i > 16384 ? 16384 : datasize-i, tpc);
437
438 /* memory leak fix courtesy of John McKown */
439 if (do_compress_data)
440 free (pc);
441
442 /* construct sweep packet */
443 switch(prn->version)
444 {
445 case HP820:
446 constants=HP820constants;
447 MF=1;
448 break;
449 case HP720:
450 constants=HP720constants;
451 MF=2;
452 break;
453 case HP1000:
454 constants=HP1000constants;
455 MF=1;
456 break;
457 default:
458 fprintf(stderr,"ppa_print_sweep(): unknown printer version\n");
459 return;
460 }
461
462 sweep_packet[0]=0;
463 sweep_packet[1]= do_compress_data;
464 sweep_packet[2]= data->direction==right_to_left ? 1 : 2;
465 sweep_packet[3]= data->in_color ? 14 : 1;
466
467 place_4bytes(datasize,sweep_packet+4);
468
469 memset(sweep_packet+8,0,8);
470
471 place_4bytes(MF*(data->vertical_pos+prn->y_offset),sweep_packet+16);
472 place_2bytes(constants[0],sweep_packet+20);
473 place_2bytes(MF*(data->left_margin+prn->x_offset),sweep_packet+22);
474 place_2bytes(MF*(data->right_margin+prn->x_offset),sweep_packet+24);
475 place_2bytes(constants[1],sweep_packet+26);
476 place_2bytes(constants[2],sweep_packet+28);
477 place_2bytes(0x100,sweep_packet+30);
478
479 if(data->next)
480 {
481 sweep_packet[32]= data->next->direction==right_to_left ? 1 : 2;
482 sweep_packet[33]= data->next->in_color ? 14 : 1;
483 place_4bytes(MF*(data->next->vertical_pos+prn->y_offset),sweep_packet+34);
484 place_2bytes(MF*(data->next->left_margin+prn->x_offset),sweep_packet+38);
485 place_2bytes(MF*(data->next->right_margin+prn->x_offset),sweep_packet+40);
486 place_2bytes(constants[1],sweep_packet+42);
487 place_2bytes(constants[2],sweep_packet+44);
488 }
489 else
490 memset(sweep_packet+32,0,14);
491 sweep_packet[46]=8;
492
493 nozzle_data_size=data->nozzle_data_size;
494 if(nozzle_data_size>6)
495 {
496 fprintf(stderr,"ppa_print_sweep(): truncating nozzle data from %d rows to 6 rows\n",nozzle_data_size);
497 nozzle_data_size=6;
498 }
499 sweep_packet[47]=nozzle_data_size;
500
501 for(i=0; i<nozzle_data_size; ++i)
502 {
503 unsigned char * const pc = sweep_packet + 48 + i*16;
504
505 place_2bytes(data->nozzle_data[i].DPI,pc);
506 place_2bytes(data->nozzle_data[i].pins_used_d2,pc+2);
507 place_2bytes(data->nozzle_data[i].unused_pins_p1,pc+4);
508 place_2bytes(data->nozzle_data[i].first_pin,pc+6);
509 place_2bytes(data->nozzle_data[i].pins_used_d2,pc+8);
510 place_2bytes(MF*(data->nozzle_data[i].left_margin+prn->x_offset),pc+10);
511 place_2bytes(MF*(data->nozzle_data[i].right_margin+prn->x_offset),pc+12);
512 pc[14]=data->nozzle_data[i].nozzle_delay;
513 pc[15]=0;
514 }
515
516 sweep_packet_size = data->in_color ? 144 : 80;
517
518 /* send sweep packet */
519 switch(prn->version)
520 {
521 case HP820:
522 scp_put (prn->fptr, 18, 1, 7, sweep_packet_size, sweep_packet);
523 break;
524 case HP720:
525 scp2_put (prn->fptr, 0x0180, sweep_packet_size, 7, 1, datasize, sweep_packet);
526 break;
527 case HP1000:
528 scp3_put (prn->fptr, 0x0180, sweep_packet_size, 7, 1, datasize, sweep_packet);
529 break;
530 default:
531 fprintf(stderr,"ppa_print_sweep(): unknown printer version\n");
532 return;
533 }
534 }
535
536
537
538 void ppa_print_sweeps(ppa_stat* prn,ppa_sweep_data* data)
539 {
540 ppa_sweep_data* current_sweep;
541 for(current_sweep=data; current_sweep; current_sweep=current_sweep->next)
542 ppa_print_sweep(prn,current_sweep);
543 }
544
545
546