File: rastertoepson.c

package info (click to toggle)
cups 1.4.4-7+squeeze10
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 25,548 kB
  • ctags: 9,583
  • sloc: ansic: 138,214; sh: 37,926; cpp: 25,469; makefile: 3,285; perl: 190; python: 127; php: 28
file content (1142 lines) | stat: -rw-r--r-- 22,952 bytes parent folder | download | duplicates (2)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
/*
 * "$Id: rastertoepson.c 8615 2009-05-12 03:22:10Z mike $"
 *
 *   EPSON ESC/P and ESC/P2 filter for the Common UNIX Printing System
 *   (CUPS).
 *
 *   Copyright 2007-2009 by Apple Inc.
 *   Copyright 1993-2007 by Easy Software Products.
 *
 *   These coded instructions, statements, and computer programs are the
 *   property of Apple Inc. and are protected by Federal copyright
 *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
 *   which should have been included with this file.  If this file is
 *   file is missing or damaged, see the license at "http://www.cups.org/".
 *
 *   This file is subject to the Apple OS-Developed Software exception.
 *
 * Contents:
 *
 *   Setup()        - Prepare the printer for printing.
 *   StartPage()    - Start a page of graphics.
 *   EndPage()      - Finish a page of graphics.
 *   Shutdown()     - Shutdown the printer.
 *   CompressData() - Compress a line of graphics.
 *   OutputLine()   - Output a line of graphics.
 *   main()         - Main entry and processing of driver.
 */

/*
 * Include necessary headers...
 */

#include <cups/cups.h>
#include <cups/ppd.h>
#include <cups/string.h>
#include <cups/i18n.h>
#include <cups/raster.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>


/*
 * Model numbers...
 */

#define EPSON_9PIN	0
#define EPSON_24PIN	1
#define EPSON_COLOR	2
#define EPSON_PHOTO	3
#define EPSON_ICOLOR	4
#define EPSON_IPHOTO	5


/*
 * Macros...
 */

#define pwrite(s,n) fwrite((s), 1, (n), stdout)


/*
 * Globals...
 */

unsigned char	*Planes[6],		/* Output buffers */
		*CompBuffer,		/* Compression buffer */
		*LineBuffers[2];	/* Line bitmap buffers */
int		Model,			/* Model number */
		NumPlanes,		/* Number of color planes */
		Feed,			/* Number of lines to skip */
		EjectPage;		/* Eject the page when done? */
int		DotBit,			/* Bit in buffers */
		DotBytes,		/* # bytes in a dot column */
		DotColumns,		/* # columns in 1/60 inch */
		LineCount,		/* # of lines processed */
		EvenOffset,		/* Offset into 'even' buffers */
		OddOffset,		/* Offset into 'odd' buffers */
		Shingling,		/* Shingle output? */
		Canceled;		/* Has the current job been canceled? */


/*
 * Prototypes...
 */

void	Setup(void);
void	StartPage(const ppd_file_t *ppd, const cups_page_header2_t *header);
void	EndPage(const cups_page_header2_t *header);
void	Shutdown(void);

void	CancelJob(int sig);
void	CompressData(const unsigned char *line, int length, int plane,
	             int type, int xstep, int ystep);
void	OutputLine(const cups_page_header2_t *header);
void	OutputRows(const cups_page_header2_t *header, int row);


/*
 * 'Setup()' - Prepare the printer for printing.
 */

void
Setup(void)
{
  const char	*device_uri;		/* The device for the printer... */


 /*
  * EPSON USB printers need an additional command issued at the
  * beginning of each job to exit from "packet" mode...
  */

  if ((device_uri = getenv("DEVICE_URI")) != NULL &&
      strncmp(device_uri, "usb:", 4) == 0 && Model >= EPSON_ICOLOR)
    pwrite("\000\000\000\033\001@EJL 1284.4\n@EJL     \n\033@", 29);
}


/*
 * 'StartPage()' - Start a page of graphics.
 */

void
StartPage(
    const ppd_file_t         *ppd,	/* I - PPD file */
    const cups_page_header2_t *header)	/* I - Page header */
{
  int	n, t;				/* Numbers */
  int	plane;				/* Looping var */


 /*
  * Send a reset sequence.
  */

  if (ppd && ppd->nickname && strstr(ppd->nickname, "OKIDATA") != NULL)
    printf("\033{A");	/* Set EPSON emulation mode */

  printf("\033@");

 /*
  * See which type of printer we are using...
  */

  switch (Model)
  {
    case EPSON_9PIN :
    case EPSON_24PIN :
        printf("\033P\022");		/* Set 10 CPI */

	if (header->HWResolution[0] == 360 || header->HWResolution[0] == 240)
	{
	  printf("\033x1");		/* LQ printing */
	  printf("\033U1");		/* Unidirectional */
	}
	else
	{
	  printf("\033x0");		/* Draft printing */
	  printf("\033U0");		/* Bidirectional */
	}

	printf("\033l%c\033Q%c", 0,	/* Side margins */
                      (int)(10.0 * header->PageSize[0] / 72.0 + 0.5));
	printf("\033\062\033C%c",	/* Page length in 1/6th inches */
		      (int)(header->PageSize[1] / 12.0 + 0.5));
	printf("\033N%c", 0);		/* Bottom margin */
        printf("\033O");		/* No perforation skip */

       /*
	* Setup various buffer limits...
	*/

        DotBytes   = header->cupsRowCount / 8;
	DotColumns = header->HWResolution[0] / 60;
        Shingling  = 0;

        if (Model == EPSON_9PIN)
	  printf("\033\063\030");	/* Set line feed */
	else
	  switch (header->HWResolution[0])
	  {
	    case 60:
	    case 120 :
	    case 240 :
        	printf("\033\063\030");	/* Set line feed */
		break;

	    case 180 :
	    case 360 :
        	Shingling = 1;

        	if (header->HWResolution[1] == 180)
        	  printf("\033\063\010");/* Set line feed */
		else
        	  printf("\033+\010");	/* Set line feed */
        	break;
	  }
        break;

    default :
       /*
	* Set graphics mode...
	*/

	pwrite("\033(G\001\000\001", 6);	/* Graphics mode */

       /*
	* Set the media size...
	*/

        if (Model < EPSON_ICOLOR)
	{
	  pwrite("\033(U\001\000", 5);		/* Resolution/units */
	  putchar(3600 / header->HWResolution[1]);
        }
	else
	{
	  pwrite("\033(U\005\000", 5);
	  putchar(1440 / header->HWResolution[1]);
	  putchar(1440 / header->HWResolution[1]);
	  putchar(1440 / header->HWResolution[0]);
	  putchar(0xa0);	/* n/1440ths... */
	  putchar(0x05);
	}

	n = header->PageSize[1] * header->HWResolution[1] / 72.0;

	pwrite("\033(C\002\000", 5);		/* Page length */
	putchar(n);
	putchar(n >> 8);

        if (ppd)
	  t = (ppd->sizes[1].length - ppd->sizes[1].top) *
	      header->HWResolution[1] / 72.0;
        else
	  t = 0;

	pwrite("\033(c\004\000", 5);		/* Top & bottom margins */
	putchar(t);
	putchar(t >> 8);
	putchar(n);
	putchar(n >> 8);

	if (header->HWResolution[1] == 720)
	{
	  pwrite("\033(i\001\000\001", 6);	/* Microweave */
	  pwrite("\033(e\002\000\000\001", 7);	/* Small dots */
	}

	pwrite("\033(V\002\000\000\000", 7);	/* Set absolute position 0 */

        DotBytes   = 0;
	DotColumns = 0;
        Shingling  = 0;
        break;
  }

 /*
  * Set other stuff...
  */

  if (header->cupsColorSpace == CUPS_CSPACE_CMY)
    NumPlanes = 3;
  else if (header->cupsColorSpace == CUPS_CSPACE_KCMY)
    NumPlanes = 4;
  else if (header->cupsColorSpace == CUPS_CSPACE_KCMYcm)
    NumPlanes = 6;
  else
    NumPlanes = 1;

  Feed = 0;				/* No blank lines yet */

 /*
  * Allocate memory for a line/row of graphics...
  */

  if ((Planes[0] = malloc(header->cupsBytesPerLine)) == NULL)
  {
    fputs("ERROR: Unable to allocate memory!\n", stderr);
    exit(1);
  }

  for (plane = 1; plane < NumPlanes; plane ++)
    Planes[plane] = Planes[0] + plane * header->cupsBytesPerLine / NumPlanes;

  if (header->cupsCompression || DotBytes)
  {
    if ((CompBuffer = calloc(2, header->cupsWidth)) == NULL)
    {
      fputs("ERROR: Unable to allocate memory!\n", stderr);
      exit(1);
    }
  }
  else
    CompBuffer = NULL;

  if (DotBytes)
  {
    if ((LineBuffers[0] = calloc(DotBytes,
                                 header->cupsWidth * (Shingling + 1))) == NULL)
    {
      fputs("ERROR: Unable to allocate memory!\n", stderr);
      exit(1);
    }

    LineBuffers[1] = LineBuffers[0] + DotBytes * header->cupsWidth;
    DotBit         = 128;
    LineCount      = 0;
    EvenOffset     = 0;
    OddOffset      = 0;
  }
}


/*
 * 'EndPage()' - Finish a page of graphics.
 */

void
EndPage(
    const cups_page_header2_t *header)	/* I - Page header */
{
  if (DotBytes && header)
  {
   /*
    * Flush remaining graphics as needed...
    */

    if (!Shingling)
    {
      if (DotBit < 128 || EvenOffset)
        OutputRows(header, 0);
    }
    else if (OddOffset > EvenOffset)
    {
      OutputRows(header, 1);
      OutputRows(header, 0);
    }
    else
    {
      OutputRows(header, 0);
      OutputRows(header, 1);
    }
  }

 /*
  * Eject the current page...
  */

  putchar(12);				/* Form feed */
  fflush(stdout);

 /*
  * Free memory...
  */

  free(Planes[0]);

  if (CompBuffer)
    free(CompBuffer);

  if (DotBytes)
    free(LineBuffers[0]);
}


/*
 * 'Shutdown()' - Shutdown the printer.
 */

void
Shutdown(void)
{
 /*
  * Send a reset sequence.
  */

  printf("\033@");
}


/*
 * 'CancelJob()' - Cancel the current job...
 */

void
CancelJob(int sig)			/* I - Signal */
{
  (void)sig;

  Canceled = 1;
}


/*
 * 'CompressData()' - Compress a line of graphics.
 */

void
CompressData(const unsigned char *line,	/* I - Data to compress */
             int                 length,/* I - Number of bytes */
	     int                 plane,	/* I - Color plane */
	     int                 type,	/* I - Type of compression */
	     int                 xstep,	/* I - X resolution */
	     int                 ystep)	/* I - Y resolution */
{
  const unsigned char	*line_ptr,	/* Current byte pointer */
        		*line_end,	/* End-of-line byte pointer */
        		*start;		/* Start of compression sequence */
  unsigned char      	*comp_ptr,	/* Pointer into compression buffer */
			temp;		/* Current byte */
  int   	        count;		/* Count of bytes for output */
  static int		ctable[6] = { 0, 2, 1, 4, 18, 17 };
					/* KCMYcm color values */


 /*
  * Setup pointers...
  */

  line_ptr = line;
  line_end = line + length;

 /*
  * Do depletion for 720 DPI printing...
  */

  if (ystep == 5)
  {
    for (comp_ptr = (unsigned char *)line; comp_ptr < line_end;)
    {
     /*
      * Grab the current byte...
      */

      temp = *comp_ptr;

     /*
      * Check adjacent bits...
      */

      if ((temp & 0xc0) == 0xc0)
        temp &= 0xbf;
      if ((temp & 0x60) == 0x60)
        temp &= 0xdf;
      if ((temp & 0x30) == 0x30)
        temp &= 0xef;
      if ((temp & 0x18) == 0x18)
        temp &= 0xf7;
      if ((temp & 0x0c) == 0x0c)
        temp &= 0xfb;
      if ((temp & 0x06) == 0x06)
        temp &= 0xfd;
      if ((temp & 0x03) == 0x03)
        temp &= 0xfe;

      *comp_ptr++ = temp;

     /*
      * Check the last bit in the current byte and the first bit in the
      * next byte...
      */

      if ((temp & 0x01) && comp_ptr < line_end && *comp_ptr & 0x80)
        *comp_ptr &= 0x7f;
    }
  }

  switch (type)
  {
    case 0 :
       /*
	* Do no compression...
	*/
	break;

    case 1 :
       /*
        * Do TIFF pack-bits encoding...
        */

	comp_ptr = CompBuffer;

	while (line_ptr < line_end)
	{
	  if ((line_ptr + 1) >= line_end)
	  {
	   /*
	    * Single byte on the end...
	    */

	    *comp_ptr++ = 0x00;
	    *comp_ptr++ = *line_ptr++;
	  }
	  else if (line_ptr[0] == line_ptr[1])
	  {
	   /*
	    * Repeated sequence...
	    */

	    line_ptr ++;
	    count = 2;

	    while (line_ptr < (line_end - 1) &&
        	   line_ptr[0] == line_ptr[1] &&
        	   count < 127)
	    {
              line_ptr ++;
              count ++;
	    }

	    *comp_ptr++ = 257 - count;
	    *comp_ptr++ = *line_ptr++;
	  }
	  else
	  {
	   /*
	    * Non-repeated sequence...
	    */

	    start    = line_ptr;
	    line_ptr ++;
	    count    = 1;

	    while (line_ptr < (line_end - 1) &&
        	   line_ptr[0] != line_ptr[1] &&
        	   count < 127)
	    {
              line_ptr ++;
              count ++;
	    }

	    *comp_ptr++ = count - 1;

	    memcpy(comp_ptr, start, count);
	    comp_ptr += count;
	  }
	}

        line_ptr = CompBuffer;
        line_end = comp_ptr;
	break;
  }

  putchar(0x0d);			/* Move print head to left margin */

  if (Model < EPSON_ICOLOR)
  {
   /*
    * Do graphics the "old" way...
    */

    if (NumPlanes > 1)
    {
     /*
      * Set the color...
      */

      if (plane > 3)
	printf("\033(r%c%c%c%c", 2, 0, 1, ctable[plane] & 15);
					  /* Set extended color */
      else if (NumPlanes == 3)
	printf("\033r%c", ctable[plane + 1]);
					  /* Set color */
      else
	printf("\033r%c", ctable[plane]);	/* Set color */
    }

   /*
    * Send a raster plane...
    */

    length *= 8;
    printf("\033.");			/* Raster graphics */
    putchar(type);
    putchar(ystep);
    putchar(xstep);
    putchar(1);
    putchar(length);
    putchar(length >> 8);
  }
  else
  {
   /*
    * Do graphics the "new" way...
    */

    printf("\033i");
    putchar(ctable[plane]);
    putchar(type);
    putchar(1);
    putchar(length & 255);
    putchar(length >> 8);
    putchar(1);
    putchar(0);
  }

  pwrite(line_ptr, line_end - line_ptr);
  fflush(stdout);
}


/*
 * 'OutputLine()' - Output a line of graphics.
 */

void
OutputLine(
    const cups_page_header2_t *header)	/* I - Page header */
{
  if (header->cupsRowCount)
  {
    int			width;
    unsigned char	*tempptr,
			*evenptr,
			*oddptr;
    register int	x;
    unsigned char	bit;
    const unsigned char	*pixel;
    unsigned char 	*temp;


   /*
    * Collect bitmap data in the line buffers and write after each buffer.
    */

    for (x = header->cupsWidth, bit = 128, pixel = Planes[0],
             temp = CompBuffer;
	 x > 0;
	 x --, temp ++)
    {
      if (*pixel & bit)
        *temp |= DotBit;

      if (bit > 1)
	bit >>= 1;
      else
      {
	bit = 128;
	pixel ++;
      }
    }

    if (DotBit > 1)
      DotBit >>= 1;
    else
    {
     /*
      * Copy the holding buffer to the output buffer, shingling as necessary...
      */

      if (Shingling && LineCount != 0)
      {
       /*
        * Shingle the output...
        */

        if (LineCount & 1)
        {
          evenptr = LineBuffers[1] + OddOffset;
          oddptr  = LineBuffers[0] + EvenOffset + DotBytes;
        }
        else
        {
          evenptr = LineBuffers[0] + EvenOffset;
          oddptr  = LineBuffers[1] + OddOffset + DotBytes;
        }

        for (width = header->cupsWidth, tempptr = CompBuffer;
             width > 0;
             width -= 2, tempptr += 2, oddptr += DotBytes * 2,
	         evenptr += DotBytes * 2)
        {
          evenptr[0] = tempptr[0];
          oddptr[0]  = tempptr[1];
        }
      }
      else
      {
       /*
        * Don't shingle the output...
        */

        for (width = header->cupsWidth, tempptr = CompBuffer,
                 evenptr = LineBuffers[0] + EvenOffset;
             width > 0;
             width --, tempptr ++, evenptr += DotBytes)
          *evenptr = tempptr[0];
      }

      if (Shingling && LineCount != 0)
      {
	EvenOffset ++;
	OddOffset ++;

	if (EvenOffset == DotBytes)
	{
	  EvenOffset = 0;
	  OutputRows(header, 0);
	}

	if (OddOffset == DotBytes)
	{
          OddOffset = 0;
	  OutputRows(header, 1);
	}
      }
      else
      {
	EvenOffset ++;

	if (EvenOffset == DotBytes)
	{
          EvenOffset = 0;
	  OutputRows(header, 0);
	}
      }

      DotBit = 128;
      LineCount ++;

      memset(CompBuffer, 0, header->cupsWidth);
    }
  }
  else
  {
    int	plane;		/* Current plane */
    int	bytes;		/* Bytes per plane */
    int	xstep, ystep;	/* X & Y resolutions */


   /*
    * Write a single line of bitmap data as needed...
    */

    xstep = 3600 / header->HWResolution[0];
    ystep = 3600 / header->HWResolution[1];
    bytes = header->cupsBytesPerLine / NumPlanes;

    for (plane = 0; plane < NumPlanes; plane ++)
    {
     /*
      * Skip blank data...
      */

      if (!Planes[plane][0] &&
          memcmp(Planes[plane], Planes[plane] + 1, bytes - 1) == 0)
	continue;

     /*
      * Output whitespace as needed...
      */

      if (Feed > 0)
      {
	pwrite("\033(v\002\000", 5);	/* Relative vertical position */
	putchar(Feed);
	putchar(Feed >> 8);

	Feed = 0;
      }

      CompressData(Planes[plane], bytes, plane, header->cupsCompression, xstep,
                   ystep);
    }

    Feed ++;
  }
}


/*
 * 'OutputRows()' - Output 8, 24, or 48 rows.
 */

void
OutputRows(
    const cups_page_header2_t *header,	/* I - Page image header */
    int                      row)	/* I - Row number (0 or 1) */
{
  unsigned	i, n;			/* Looping vars */
  int		dot_count,		/* Number of bytes to print */
                dot_min;		/* Minimum number of bytes */
  unsigned char *dot_ptr,		/* Pointer to print data */
		*ptr;			/* Current data */


  dot_min = DotBytes * DotColumns;

  if (LineBuffers[row][0] != 0 ||
      memcmp(LineBuffers[row], LineBuffers[row] + 1,
             header->cupsWidth * DotBytes - 1))
  {
   /*
    * Skip leading space...
    */

    i         = 0;
    dot_count = header->cupsWidth * DotBytes;
    dot_ptr   = LineBuffers[row];

    while (dot_count >= dot_min && dot_ptr[0] == 0 &&
           memcmp(dot_ptr, dot_ptr + 1, dot_min - 1) == 0)
    {
      i         ++;
      dot_ptr   += dot_min;
      dot_count -= dot_min;
    }

   /*
    * Skip trailing space...
    */

    while (dot_count >= dot_min && dot_ptr[dot_count - dot_min] == 0 &&
           memcmp(dot_ptr + dot_count - dot_min,
	          dot_ptr + dot_count - dot_min + 1, dot_min - 1) == 0)
      dot_count -= dot_min;

   /*
    * Position print head for printing...
    */

    if (i == 0)
      putchar('\r');
    else
    {
      putchar(0x1b);
      putchar('$');
      putchar(i & 255);
      putchar(i >> 8);
    }

   /*
    * Start bitmap graphics for this line...
    */

    printf("\033*");			/* Select bit image */
    switch (header->HWResolution[0])
    {
      case 60 : /* 60x60/72 DPI gfx */
          putchar(0);
          break;
      case 120 : /* 120x60/72 DPI gfx */
          putchar(1);
          break;
      case 180 : /* 180 DPI gfx */
          putchar(39);
          break;
      case 240 : /* 240x72 DPI gfx */
          putchar(3);
          break;
      case 360 : /* 360x180/360 DPI gfx */
	  if (header->HWResolution[1] == 180)
	  {
            if (Shingling && LineCount != 0)
              putchar(40);		/* 360x180 fast */
            else
              putchar(41);		/* 360x180 slow */
	  }
	  else
          {
	    if (Shingling && LineCount != 0)
              putchar(72);		/* 360x360 fast */
            else
              putchar(73);		/* 360x360 slow */
          }
          break;
    }

    n = (unsigned)dot_count / DotBytes;
    putchar(n & 255);
    putchar(n / 256);

   /*
    * Write the graphics data...
    */

    if (header->HWResolution[0] == 120 ||
        header->HWResolution[0] == 240)
    {
     /*
      * Need to interleave the dots to avoid hosing the print head...
      */

      for (n = dot_count / 2, ptr = dot_ptr; n > 0; n --, ptr += 2)
      {
        putchar(*ptr);
	putchar(0);
      }

     /*
      * Move the head back and print the odd bytes...
      */

      if (i == 0)
	putchar('\r');
      else
      {
	putchar(0x1b);
	putchar('$');
	putchar(i & 255);
	putchar(i >> 8);
      }

      if (header->HWResolution[0] == 120)
      	printf("\033*\001");		/* Select bit image */
      else
      	printf("\033*\003");		/* Select bit image */

      n = (unsigned)dot_count / DotBytes;
      putchar(n & 255);
      putchar(n / 256);

      for (n = dot_count / 2, ptr = dot_ptr + 1; n > 0; n --, ptr += 2)
      {
	putchar(0);
        putchar(*ptr);
      }
    }
    else
      pwrite(dot_ptr, dot_count);
  }

 /*
  * Feed the paper...
  */

  putchar('\n');

  if (Shingling && row == 1)
  {
    if (header->HWResolution[1] == 360)
      printf("\n\n\n\n");
    else
      printf("\n");
  }

  fflush(stdout);

 /*
  * Clear the buffer...
  */

  memset(LineBuffers[row], 0, header->cupsWidth * DotBytes);
}


/*
 * 'main()' - Main entry and processing of driver.
 */

int					/* O - Exit status */
main(int  argc,				/* I - Number of command-line arguments */
     char *argv[])			/* I - Command-line arguments */
{
  int			fd;		/* File descriptor */
  cups_raster_t		*ras;		/* Raster stream for printing */
  cups_page_header2_t	header;		/* Page header from file */
  ppd_file_t		*ppd;		/* PPD file */
  int			page;		/* Current page */
  int			y;		/* Current line */
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
  struct sigaction action;		/* Actions for POSIX signals */
#endif /* HAVE_SIGACTION && !HAVE_SIGSET */


 /*
  * Make sure status messages are not buffered...
  */

  setbuf(stderr, NULL);

 /*
  * Check command-line...
  */

  if (argc < 6 || argc > 7)
  {
   /*
    * We don't have the correct number of arguments; write an error message
    * and return.
    */

    _cupsLangPrintf(stderr,
                    _("Usage: %s job-id user title copies options [file]\n"),
                    "rastertoepson");
    return (1);
  }

 /*
  * Open the page stream...
  */

  if (argc == 7)
  {
    if ((fd = open(argv[6], O_RDONLY)) == -1)
    {
      _cupsLangPrintf(stderr, _("ERROR: Unable to open raster file - %s\n"),
                      strerror(errno));
      sleep(1);
      return (1);
    }
  }
  else
    fd = 0;

  ras = cupsRasterOpen(fd, CUPS_RASTER_READ);

 /*
  * Register a signal handler to eject the current page if the
  * job is cancelled.
  */

  Canceled = 0;

#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
  sigset(SIGTERM, CancelJob);
#elif defined(HAVE_SIGACTION)
  memset(&action, 0, sizeof(action));

  sigemptyset(&action.sa_mask);
  action.sa_handler = CancelJob;
  sigaction(SIGTERM, &action, NULL);
#else
  signal(SIGTERM, CancelJob);
#endif /* HAVE_SIGSET */

 /*
  * Initialize the print device...
  */

  ppd = ppdOpenFile(getenv("PPD"));
  if (ppd)
    Model = ppd->model_number;

  Setup();

 /*
  * Process pages as needed...
  */

  page = 0;

  while (cupsRasterReadHeader2(ras, &header))
  {
   /*
    * Write a status message with the page number and number of copies.
    */

    if (Canceled)
      break;

    page ++;

    fprintf(stderr, "PAGE: %d %d\n", page, header.NumCopies);

   /*
    * Start the page...
    */

    StartPage(ppd, &header);

   /*
    * Loop for each line on the page...
    */

    for (y = 0; y < header.cupsHeight; y ++)
    {
     /*
      * Let the user know how far we have progressed...
      */

      if (Canceled)
	break;

      if ((y & 127) == 0)
        _cupsLangPrintf(stderr, _("INFO: Printing page %d, %d%% complete...\n"),
                        page, 100 * y / header.cupsHeight);

     /*
      * Read a line of graphics...
      */

      if (cupsRasterReadPixels(ras, Planes[0], header.cupsBytesPerLine) < 1)
        break;

     /*
      * Write it to the printer...
      */

      OutputLine(&header);
    }

   /*
    * Eject the page...
    */

    EndPage(&header);

    if (Canceled)
      break;
  }

 /*
  * Shutdown the printer...
  */

  Shutdown();

  ppdClose(ppd);

 /*
  * Close the raster stream...
  */

  cupsRasterClose(ras);
  if (fd != 0)
    close(fd);

 /*
  * If no pages were printed, send an error message...
  */

  if (page == 0)
  {
    _cupsLangPuts(stderr, _("ERROR: No pages found!\n"));
    return (1);
  }
  else
  {
    _cupsLangPuts(stderr, _("INFO: Ready to print.\n"));
    return (0);
  }
}


/*
 * End of "$Id: rastertoepson.c 8615 2009-05-12 03:22:10Z mike $".
 */