summaryrefslogtreecommitdiff
path: root/test/ippevepcl.c
blob: 5da6d5aefdd84d8def7477247b084cee760e4047 (plain)
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
/*
 * Generic HP PCL printer command for ippeveprinter/CUPS.
 *
 * Copyright © 2019 by Apple Inc.
 *
 * Licensed under Apache License v2.0.  See the file "LICENSE" for more
 * information.
 */

/*
 * Include necessary headers...
 */

#include "ippevecommon.h"
#include "dither.h"


/*
 * Local globals...
 */

static unsigned		pcl_bottom,	/* Bottom line */
			pcl_left,	/* Left offset in line */
			pcl_right,	/* Right offset in line */
			pcl_top,	/* Top line */
			pcl_blanks;	/* Number of blank lines to skip */
static unsigned char	*pcl_line,	/* Line buffer */
			*pcl_comp;	/* Compression buffer */

/*
 * Local functions...
 */

static void	pcl_end_page(cups_page_header2_t *header, unsigned page);
static void	pcl_start_page(cups_page_header2_t *header, unsigned page);
static void	pcl_write_line(cups_page_header2_t *header, unsigned y, const unsigned char *line);


/*
 * 'main()' - Main entry for PCL printer command.
 */

int					/* O - Exit status */
main(int  argc,				/* I - Number of command-line arguments */
     char *argv[])			/* I - Command-line arguments */
{
  const char		*content_type;	/* Content type to print */
  int			fd;		/* Input file */
  cups_raster_t		*ras;		/* Raster stream */
  cups_page_header2_t	header;		/* Page header */
  unsigned		page = 0,	/* Current page */
			y;		/* Current line */
  unsigned char		*line;		/* Line buffer */


 /*
  * First make sure we can read what is being printed.
  */

  if ((content_type = getenv("CONTENT_TYPE")) == NULL)
  {
    fputs("ERROR: CONTENT_TYPE environment variable not set, aborting.\n", stderr);
    return (1);
  }
  else if (strcasecmp(content_type, "image/pwg-raster") && strcasecmp(content_type, "image/urf"))
  {
    fprintf(stderr, "ERROR: CONTENT_TYPE %s not supported.\n", content_type);
    return (1);
  }

 /*
  * Then get the input file...
  */

  if (argc == 1)
  {
    fd = 0;
  }
  else if (argc == 2)
  {
    if ((fd = open(argv[1], O_RDONLY)) < 0)
    {
      fprintf(stderr, "ERROR: Unable to open \"%s\": %s\n", argv[1], strerror(errno));
      return (1);
    }
  }
  else
  {
    fputs("ERROR: Too many arguments provided, aborting.\n", stderr);
    return (1);
  }

 /*
  * Open the raster stream and send pages...
  */

  if ((ras = cupsRasterOpen(fd, CUPS_RASTER_READ)) == NULL)
  {
    fputs("ERROR: Unable to read raster data, aborting.\n", stderr);
    return (1);
  }

  fputs("\033E", stdout);

  while (cupsRasterReadHeader2(ras, &header))
  {
    page ++;

    if (header.cupsColorSpace != CUPS_CSPACE_W && header.cupsColorSpace != CUPS_CSPACE_K)
    {
      fputs("ERROR: Unsupported color space, aborting.\n", stderr);
      break;
    }
    else if (header.cupsBitsPerColor != 1 && header.cupsBitsPerColor != 8)
    {
      fputs("ERROR: Unsupported bit depth, aborting.\n", stderr);
      break;
    }

    line = malloc(header.cupsBytesPerLine);

    pcl_start_page(&header, page);
    for (y = 0; y < header.cupsHeight; y ++)
    {
      if (cupsRasterReadPixels(ras, line, header.cupsBytesPerLine))
        pcl_write_line(&header, y, line);
      else
        break;
    }
    pcl_end_page(&header, page);

    free(line);
  }

  cupsRasterClose(ras);

  return (0);
}



/*
 * 'pcl_end_page()' - End of PCL page.
 */

static void
pcl_end_page(
    cups_page_header2_t *header,	/* Page header */
    unsigned            page)		/* I - Current page */
{
 /*
  * End graphics...
  */

  fputs("\033*r0B", stdout);

 /*
  * Formfeed as needed...
  */

  if (!(header->Duplex && (page & 1)))
    putchar('\f');

 /*
  * Free the output buffers...
  */

  free(pcl_line);
  free(pcl_comp);
}


/*
 * 'pcl_start_page()' - Start a PCL page.
 */

static void
pcl_start_page(
    cups_page_header2_t *header,	/* I - Page header */
    unsigned            page)		/* I - Current page */
{
 /*
  * Setup margins to be 1/6" top and bottom and 1/4" or .135" on the
  * left and right.
  */

  pcl_top    = header->HWResolution[1] / 6;
  pcl_bottom = header->cupsHeight - header->HWResolution[1] / 6 - 1;

  if (header->PageSize[1] == 842)
  {
   /* A4 gets special side margins to expose an 8" print area */
    pcl_left  = (header->cupsWidth - 8 * header->HWResolution[0]) / 2;
    pcl_right = pcl_left + 8 * header->HWResolution[0] - 1;
  }
  else
  {
   /* All other sizes get 1/4" margins */
    pcl_left  = header->HWResolution[0] / 4;
    pcl_right = header->cupsWidth - header->HWResolution[0] / 4 - 1;
  }

  if (!header->Duplex || (page & 1))
  {
   /*
    * Set the media size...
    */

    printf("\033&l12D\033&k12H");	/* Set 12 LPI, 10 CPI */
    printf("\033&l0O");			/* Set portrait orientation */

    switch (header->PageSize[1])
    {
      case 540 : /* Monarch Envelope */
          printf("\033&l80A");
	  break;

      case 595 : /* A5 */
          printf("\033&l25A");
	  break;

      case 624 : /* DL Envelope */
          printf("\033&l90A");
	  break;

      case 649 : /* C5 Envelope */
          printf("\033&l91A");
	  break;

      case 684 : /* COM-10 Envelope */
          printf("\033&l81A");
	  break;

      case 709 : /* B5 Envelope */
          printf("\033&l100A");
	  break;

      case 756 : /* Executive */
          printf("\033&l1A");
	  break;

      case 792 : /* Letter */
          printf("\033&l2A");
	  break;

      case 842 : /* A4 */
          printf("\033&l26A");
	  break;

      case 1008 : /* Legal */
          printf("\033&l3A");
	  break;

      case 1191 : /* A3 */
          printf("\033&l27A");
	  break;

      case 1224 : /* Tabloid */
          printf("\033&l6A");
	  break;
    }

   /*
    * Set top margin and turn off perforation skip...
    */

    printf("\033&l%uE\033&l0L", 12 * pcl_top / header->HWResolution[1]);

    if (header->Duplex)
    {
      int mode = header->Duplex ? 1 + header->Tumble != 0 : 0;

      printf("\033&l%dS", mode);	/* Set duplex mode */
    }
  }
  else if (header->Duplex)
    printf("\033&a2G");			/* Print on back side */

 /*
  * Set graphics mode...
  */

  printf("\033*t%uR", header->HWResolution[0]);
					/* Set resolution */
  printf("\033*r%uS", pcl_right - pcl_left + 1);
					/* Set width */
  printf("\033*r%uT", pcl_bottom - pcl_top + 1);
					/* Set height */
  printf("\033&a0H\033&a%uV", 720 * pcl_top / header->HWResolution[1]);
					/* Set position */

  printf("\033*b2M");	/* Use PackBits compression */
  printf("\033*r1A");	/* Start graphics */

 /*
  * Allocate the output buffers...
  */

  pcl_blanks = 0;
  pcl_line   = malloc(header->cupsWidth / 8 + 1);
  pcl_comp   = malloc(2 * header->cupsBytesPerLine + 2);
}


/*
 * 'pcl_write_line()' - Write a line of raster data.
 */

static void
pcl_write_line(
    cups_page_header2_t *header,		/* I - Raster information */
    unsigned            y,		/* I - Line number */
    const unsigned char *line)		/* I - Pixels on line */
{
  unsigned	x;			/* Column number */
  unsigned char	bit,			/* Current bit */
		byte,			/* Current byte */
		*outptr,		/* Pointer into output buffer */
		*outend,		/* End of output buffer */
		*start,			/* Start of sequence */
		*compptr;		/* Pointer into compression buffer */
  unsigned	count;			/* Count of bytes for output */
  const unsigned char	*ditherline;	/* Pointer into dither table */


  if (line[0] == 255 && !memcmp(line, line + 1, pcl_right - pcl_left))
  {
   /*
    * Skip blank line...
    */

    pcl_blanks ++;
    return;
  }

 /*
  * Dither the line into the output buffer...
  */

  y &= 63;
  ditherline = threshold[y];

  for (x = pcl_left, bit = 128, byte = 0, outptr = pcl_line; x <= pcl_right; x ++, line ++)
  {
    if (*line <= ditherline[x & 63])
      byte |= bit;

    if (bit == 1)
    {
      *outptr++ = byte;
      byte      = 0;
      bit       = 128;
    }
    else
      bit >>= 1;
  }

  if (bit != 128)
    *outptr++ = byte;

 /*
  * Apply compression...
  */

  compptr = pcl_comp;
  outend  = outptr;
  outptr  = pcl_line;

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

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

      outptr ++;
      count = 2;

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

      *compptr++ = (unsigned char)(257 - count);
      *compptr++ = *outptr++;
    }
    else
    {
     /*
      * Non-repeated sequence...
      */

      start = outptr;
      outptr ++;
      count = 1;

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

      *compptr++ = (unsigned char)(count - 1);

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

 /*
  * Output the line...
  */

  if (pcl_blanks > 0)
  {
   /*
    * Skip blank lines first...
    */

    printf("\033*b%dY", pcl_blanks);
    pcl_blanks = 0;
  }

  printf("\033*b%dW", (int)(compptr - pcl_comp));
  fwrite(pcl_comp, 1, (size_t)(compptr - pcl_comp), stdout);
}