summaryrefslogtreecommitdiff
path: root/tools/ippeveps.c
blob: cdedc8899a564244d669c19e6f553cbc21e2f9d5 (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
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
/*
 * Generic Adobe PostScript 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"
#if !CUPS_LITE
#  include <cups/ppd-private.h>
#endif /* !CUPS_LITE */
#include <limits.h>
#include <sys/wait.h>

#ifdef __APPLE__
#  define PDFTOPS CUPS_SERVERBIN "/filter/cgpdftops"
#else
#  define PDFTOPS CUPS_SERVERBIN "/filter/pdftops"
#endif /* __APPLE__ */


/*
 * Local globals...
 */

#if !CUPS_LITE
static ppd_file_t	*ppd = NULL;	/* PPD file data */
static _ppd_cache_t	*ppd_cache = NULL;
					/* IPP to PPD cache data */
#endif /* !CUPS_LITE */


/*
 * Local functions...
 */

static void	ascii85(const unsigned char *data, int length, int eod);
static void	dsc_header(int num_pages);
static void	dsc_page(int page);
static void	dsc_trailer(int num_pages);
static int	get_options(cups_option_t **options);
static int	jpeg_to_ps(const char *filename, int copies);
static int	pdf_to_ps(const char *filename, int copies, int num_options, cups_option_t *options);
static int	ps_to_ps(const char *filename, int copies);
static int	raster_to_ps(const char *filename);


/*
 * 'main()' - Main entry for PostScript 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 */
		*ipp_copies;		/* IPP_COPIES value */
  int		copies;			/* Number of copies */
  int		num_options;		/* Number of options */
  cups_option_t	*options;		/* Options */


 /*
  * Get print options...
  */

  num_options = get_options(&options);
  if ((ipp_copies = getenv("IPP_COPIES")) != NULL)
    copies = atoi(ipp_copies);
  else
    copies = 1;

 /*
  * Print it...
  */

  if (argc > 2)
  {
    fputs("ERROR: Too many arguments supplied, aborting.\n", stderr);
    return (1);
  }
  else 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, "application/pdf"))
  {
    return (pdf_to_ps(argv[1], copies, num_options, options));
  }
  else if (!strcasecmp(content_type, "application/postscript"))
  {
    return (ps_to_ps(argv[1], copies));
  }
  else if (!strcasecmp(content_type, "image/jpeg"))
  {
    return (jpeg_to_ps(argv[1], copies));
  }
  else if (!strcasecmp(content_type, "image/pwg-raster") || !strcasecmp(content_type, "image/urf"))
  {
    return (raster_to_ps(argv[1]));
  }
  else
  {
    fprintf(stderr, "ERROR: CONTENT_TYPE %s not supported.\n", content_type);
    return (1);
  }
}


/*
 * 'ascii85()' - Write binary data using a Base85 encoding...
 */

static void
ascii85(const unsigned char *data,	/* I - Data to write */
        int                 length,	/* I - Number of bytes to write */
        int                 eod)	/* I - 1 if this is the end, 0 otherwise */
{
  unsigned	b = 0;			/* Current 32-bit word */
  unsigned char	c[5];			/* Base-85 encoded characters */
  static int	col = 0;		/* Column */
  static unsigned char leftdata[4];	/* Leftover data at the end */
  static int	leftcount = 0;		/* Size of leftover data */


  length += leftcount;

  while (length > 3)
  {
    switch (leftcount)
    {
      case 0 :
          b = (unsigned)((((((data[0] << 8) | data[1]) << 8) | data[2]) << 8) | data[3]);
	  break;
      case 1 :
          b = (unsigned)((((((leftdata[0] << 8) | data[0]) << 8) | data[1]) << 8) | data[2]);
	  break;
      case 2 :
          b = (unsigned)((((((leftdata[0] << 8) | leftdata[1]) << 8) | data[0]) << 8) | data[1]);
	  break;
      case 3 :
          b = (unsigned)((((((leftdata[0] << 8) | leftdata[1]) << 8) | leftdata[2]) << 8) | data[0]);
	  break;
    }

    if (col >= 76)
    {
      col = 0;
      putchar('\n');
    }

    if (b == 0)
    {
      putchar('z');
      col ++;
    }
    else
    {
      c[4] = (b % 85) + '!';
      b /= 85;
      c[3] = (b % 85) + '!';
      b /= 85;
      c[2] = (b % 85) + '!';
      b /= 85;
      c[1] = (b % 85) + '!';
      b /= 85;
      c[0] = (unsigned char)(b + '!');

      fwrite(c, 1, 5, stdout);
      col += 5;
    }

    data      += 4 - leftcount;
    length    -= 4 - leftcount;
    leftcount = 0;
  }

  if (length > 0)
  {
    // Copy any remainder into the leftdata array...
    if (length > leftcount)
      memcpy(leftdata + leftcount, data, (size_t)(length - leftcount));

    memset(leftdata + length, 0, (size_t)(4 - length));

    leftcount = length;
  }

  if (eod)
  {
    // Do the end-of-data dance...
    if (col >= 76)
    {
      col = 0;
      putchar('\n');
    }

    if (leftcount > 0)
    {
      // Write the remaining bytes as needed...
      b = (unsigned)((((((leftdata[0] << 8) | leftdata[1]) << 8) | leftdata[2]) << 8) | leftdata[3]);

      c[4] = (b % 85) + '!';
      b /= 85;
      c[3] = (b % 85) + '!';
      b /= 85;
      c[2] = (b % 85) + '!';
      b /= 85;
      c[1] = (b % 85) + '!';
      b /= 85;
      c[0] = (unsigned char)(b + '!');

      fwrite(c, (size_t)(leftcount + 1), 1, stdout);

      leftcount = 0;
    }

    puts("~>");
    col = 0;
  }
}


/*
 * 'dsc_header()' - Write out a standard Document Structuring Conventions
 *                  PostScript header.
 */

static void
dsc_header(int num_pages)		/* I - Number of pages or 0 if not known */
{
  const char	*job_name = getenv("IPP_JOB_NAME");
					/* job-name value */


#if !CUPS_LITE
  const char	*job_id = getenv("IPP_JOB_ID");
					/* job-id value */

  ppdEmitJCL(ppd, stdout, job_id ? atoi(job_id) : 0, cupsUser(), job_name ? job_name : "Unknown");
#endif /* !CUPS_LITE */

  puts("%!PS-Adobe-3.0");
  puts("%%LanguageLevel: 2");
  printf("%%%%Creator: ippeveps/%d.%d.%d\n", CUPS_VERSION_MAJOR, CUPS_VERSION_MINOR, CUPS_VERSION_PATCH);
  if (job_name)
  {
    fputs("%%Title: ", stdout);
    while (*job_name)
    {
      if (*job_name >= 0x20 && *job_name < 0x7f)
        putchar(*job_name);
      else
        putchar('?');

      job_name ++;
    }
    putchar('\n');
  }
  if (num_pages > 0)
    printf("%%%%Pages: %d\n", num_pages);
  else
    puts("%%Pages: (atend)");
  puts("%%EndComments");

#if !CUPS_LITE
  if (ppd)
  {
    puts("%%BeginProlog");
    if (ppd->patches)
    {
      puts("%%BeginFeature: *JobPatchFile 1");
      puts(ppd->patches);
      puts("%%EndFeature");
    }
    ppdEmit(ppd, stdout, PPD_ORDER_PROLOG);
    puts("%%EndProlog");

    puts("%%BeginSetup");
    ppdEmit(ppd, stdout, PPD_ORDER_DOCUMENT);
    ppdEmit(ppd, stdout, PPD_ORDER_ANY);
    puts("%%EndSetup");
  }
#endif /* !CUPS_LITE */
}


/*
 * 'dsc_page()' - Mark the start of a page.
 */

static void
dsc_page(int page)			/* I - Page numebr (1-based) */
{
  printf("%%%%Page: (%d) %d\n", page, page);

  fprintf(stderr, "ATTR: job-impressions-completed=%d\n", page);

#if !CUPS_LITE
  if (ppd)
  {
    puts("%%BeginPageSetup");
    ppdEmit(ppd, stdout, PPD_ORDER_PAGE);
    puts("%%EndPageSetup");
  }
#endif /* !CUPS_LITE */
}


/*
 * 'dsc_trailer()' - Mark the end of the document.
 */

static void
dsc_trailer(int num_pages)		/* I - Number of pages */
{
  if (num_pages > 0)
  {
    puts("%%Trailer");
    printf("%%%%Pages: %d\n", num_pages);
    puts("%%EOF");
  }

#if !CUPS_LITE
  if (ppd && ppd->jcl_end)
    ppdEmitJCLEnd(ppd, stdout);
  else
#endif /* !CUPS_LITE */
    putchar(0x04);
}


/*
 * 'get_options()' - Get the PPD options corresponding to the IPP Job Template
 *                   attributes.
 */

static int				/* O - Number of options */
get_options(cups_option_t **options)	/* O - Options */
{
  int		num_options = 0;	/* Number of options */
  const char	*value;			/* Option value */
  pwg_media_t	*media = NULL;		/* Media mapping */
  int		num_media_col = 0;	/* Number of media-col values */
  cups_option_t	*media_col = NULL;	/* media-col values */
#if !CUPS_LITE
  const char	*choice;		/* PPD choice */
#endif /* !CUPS_LITE */


 /*
  * No options to start...
  */

  *options = NULL;

 /*
  * Media...
  */

  if ((value = getenv("IPP_MEDIA")) == NULL)
    if ((value = getenv("IPP_MEDIA_COL")) == NULL)
      if ((value = getenv("IPP_MEDIA_DEFAULT")) == NULL)
        value = getenv("IPP_MEDIA_COL_DEFAULT");

  if (value)
  {
    if (*value == '{')
    {
     /*
      * media-col value...
      */

      num_media_col = cupsParseOptions(value, 0, &media_col);
    }
    else
    {
     /*
      * media value - map to media-col.media-size-name...
      */

      num_media_col = cupsAddOption("media-size-name", value, 0, &media_col);
    }
  }

  if ((value = cupsGetOption("media-size-name", num_media_col, media_col)) != NULL)
  {
    media = pwgMediaForPWG(value);
  }
  else if ((value = cupsGetOption("media-size", num_media_col, media_col)) != NULL)
  {
    int		num_media_size;		/* Number of media-size values */
    cups_option_t *media_size;		/* media-size values */
    const char	*x_dimension,		/* x-dimension value */
		*y_dimension;		/* y-dimension value */

    num_media_size = cupsParseOptions(value, 0, &media_size);

    if ((x_dimension = cupsGetOption("x-dimension", num_media_size, media_size)) != NULL && (y_dimension = cupsGetOption("y-dimension", num_media_size, media_size)) != NULL)
      media = pwgMediaForSize(atoi(x_dimension), atoi(y_dimension));

    cupsFreeOptions(num_media_size, media_size);
  }

  if (media)
    num_options = cupsAddOption("PageSize", media->ppd, num_options, options);

#if !CUPS_LITE
 /*
  * Load PPD file and the corresponding IPP <-> PPD cache data...
  */

  if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL)
  {
    ppd_cache = _ppdCacheCreateWithPPD(ppd);

    /* TODO: Fix me - values are names, not numbers... Also need to support finishings-col */
    if ((value = getenv("IPP_FINISHINGS")) == NULL)
      value = getenv("IPP_FINISHINGS_DEFAULT");

    if (value)
    {
      char	*ptr;			/* Pointer into value */
      int	fin;			/* Current value */

      for (fin = strtol(value, &ptr, 10); fin > 0; fin = strtol(ptr + 1, &ptr, 10))
      {
	num_options = _ppdCacheGetFinishingOptions(ppd_cache, NULL, (ipp_finishings_t)fin, num_options, options);

	if (*ptr != ',')
	  break;
      }
    }

    if ((value = cupsGetOption("media-source", num_media_col, media_col)) != NULL)
    {
      if ((choice = _ppdCacheGetInputSlot(ppd_cache, NULL, value)) != NULL)
	num_options = cupsAddOption("InputSlot", choice, num_options, options);
    }

    if ((value = cupsGetOption("media-type", num_media_col, media_col)) != NULL)
    {
      if ((choice = _ppdCacheGetMediaType(ppd_cache, NULL, value)) != NULL)
	num_options = cupsAddOption("MediaType", choice, num_options, options);
    }

    if ((value = getenv("IPP_OUTPUT_BIN")) == NULL)
      value = getenv("IPP_OUTPUT_BIN_DEFAULT");

    if (value)
    {
      if ((choice = _ppdCacheGetOutputBin(ppd_cache, value)) != NULL)
	num_options = cupsAddOption("OutputBin", choice, num_options, options);
    }

    if ((value = getenv("IPP_SIDES")) == NULL)
      value = getenv("IPP_SIDES_DEFAULT");

    if (value && ppd_cache->sides_option)
    {
      if (!strcmp(value, "one-sided") && ppd_cache->sides_1sided)
	num_options = cupsAddOption(ppd_cache->sides_option, ppd_cache->sides_1sided, num_options, options);
      else if (!strcmp(value, "two-sided-long-edge") && ppd_cache->sides_2sided_long)
	num_options = cupsAddOption(ppd_cache->sides_option, ppd_cache->sides_2sided_long, num_options, options);
      else if (!strcmp(value, "two-sided-short-edge") && ppd_cache->sides_2sided_short)
	num_options = cupsAddOption(ppd_cache->sides_option, ppd_cache->sides_2sided_short, num_options, options);
    }

    if ((value = getenv("IPP_PRINT_QUALITY")) == NULL)
      value = getenv("IPP_PRINT_QUALITY_DEFAULT");

    if (value)
    {
      int		i;		/* Looping var */
      int		pq;		/* Print quality (0-2) */
      int		pcm = 1;	/* Print color model (0 = mono, 1 = color) */
      int		num_presets;	/* Number of presets */
      cups_option_t	*presets;	/* Presets */

      if (!strcmp(value, "draft"))
        pq = 0;
      else if (!strcmp(value, "high"))
        pq = 2;
      else
        pq = 1;

      if ((value = getenv("IPP_PRINT_COLOR_MODE")) == NULL)
	value = getenv("IPP_PRINT_COLOR_MODE_DEFAULT");

      if (value && !strcmp(value, "monochrome"))
	pcm = 0;

      num_presets = ppd_cache->num_presets[pcm][pq];
      presets     = ppd_cache->presets[pcm][pq];

      for (i = 0; i < num_presets; i ++)
	num_options = cupsAddOption(presets[i].name, presets[i].value, num_options, options);
    }

   /*
    * Mark the PPD with the options...
    */

    ppdMarkDefaults(ppd);
    cupsMarkOptions(ppd, num_options, *options);
  }
#endif /* !CUPS_LITE */

  cupsFreeOptions(num_media_col, media_col);

  return (num_options);
}


/*
 * 'jpeg_to_ps()' - Convert a JPEG file to PostScript.
 */

static int				/* O - Exit status */
jpeg_to_ps(const char    *filename,	/* I - Filename */
           int           copies)	/* I - Number of copies */
{
  int		fd;			/* JPEG file descriptor */
  int		copy;			/* Current copy */
  int		width = 0,		/* Width */
		height = 0,		/* Height */
		depth = 0,		/* Number of colors */
		length;			/* Length of marker */
  unsigned char	buffer[65536],		/* Copy buffer */
		*bufptr,		/* Pointer info buffer */
		*bufend;		/* End of buffer */
  ssize_t	bytes;			/* Bytes in buffer */
  const char	*decode;		/* Decode array */
  float		page_left,		/* Left margin */
		page_top,		/* Top margin */
		page_width,		/* Page width in points */
		page_height,		/* Page heigth in points */
		x_factor,		/* X image scaling factor */
		y_factor,		/* Y image scaling factor */
		page_scaling;		/* Image scaling factor */
#if !CUPS_LITE
  ppd_size_t	*page_size;		/* Current page size */
#endif /* !CUPS_LITE */


 /*
  * Open the input file...
  */

  if (filename)
  {
    if ((fd = open(filename, O_RDONLY)) < 0)
    {
      fprintf(stderr, "ERROR: Unable to open \"%s\": %s\n", filename, strerror(errno));
      return (1);
    }
  }
  else
  {
    fd     = 0;
    copies = 1;
  }

 /*
  * Read the JPEG dimensions...
  */

  bytes = read(fd, buffer, sizeof(buffer));

  if (bytes < 3 || memcmp(buffer, "\377\330\377", 3))
  {
    fputs("ERROR: Not a JPEG image.\n", stderr);

    if (fd > 0)
      close(fd);

    return (1);
  }

  for (bufptr = buffer + 2, bufend = buffer + bytes; bufptr < bufend;)
  {
   /*
    * Scan the file for a SOFn marker, then we can get the dimensions...
    */

    if (*bufptr == 0xff)
    {
      bufptr ++;

      if (bufptr >= bufend)
      {
       /*
	* If we are at the end of the current buffer, re-fill and continue...
	*/

	if ((bytes = read(fd, buffer, sizeof(buffer))) <= 0)
	  break;

	bufptr = buffer;
	bufend = buffer + bytes;
      }

      if (*bufptr == 0xff)
	continue;

      if ((bufptr + 16) >= bufend)
      {
       /*
	* Read more of the marker...
	*/

	bytes = bufend - bufptr;

	memmove(buffer, bufptr, (size_t)bytes);
	bufptr = buffer;
	bufend = buffer + bytes;

	if ((bytes = read(fd, bufend, sizeof(buffer) - (size_t)bytes)) <= 0)
	  break;

	bufend += bytes;
      }

      length = (size_t)((bufptr[1] << 8) | bufptr[2]);

      if ((*bufptr >= 0xc0 && *bufptr <= 0xc3) || (*bufptr >= 0xc5 && *bufptr <= 0xc7) || (*bufptr >= 0xc9 && *bufptr <= 0xcb) || (*bufptr >= 0xcd && *bufptr <= 0xcf))
      {
       /*
	* SOFn marker, look for dimensions...
	*/

	width  = (bufptr[6] << 8) | bufptr[7];
	height = (bufptr[4] << 8) | bufptr[5];
	depth  = bufptr[8];
	break;
      }

     /*
      * Skip past this marker...
      */

      bufptr ++;
      bytes = bufend - bufptr;

      while (length >= bytes)
      {
	length -= bytes;

	if ((bytes = read(fd, buffer, sizeof(buffer))) <= 0)
	  break;

	bufptr = buffer;
	bufend = buffer + bytes;
      }

      if (length > bytes)
	break;

      bufptr += length;
    }
  }

  fprintf(stderr, "DEBUG: JPEG dimensions are %dx%dx%d\n", width, height, depth);

  if (width <= 0 || height <= 0 || depth <= 0)
  {
    fputs("ERROR: No valid image data in JPEG file.\n", stderr);

    if (fd > 0)
      close(fd);

    return (1);
  }

  fputs("ATTR: job-impressions=1\n", stderr);

 /*
  * Figure out the dimensions/scaling of the final image...
  */

#if CUPS_LITE
  page_left   = 18.0f;
  page_top    = 756.0f;
  page_width  = 576.0f;
  page_height = 720.0f;

#else
  if ((page_size = ppdPageSize(ppd, NULL)) != NULL)
  {
    page_left   = page_size->left;
    page_top    = page_size->top;
    page_width  = page_size->right - page_left;
    page_height = page_top - page_size->bottom;
  }
  else
  {
    page_left   = 18.0f;
    page_top    = 756.0f;
    page_width  = 576.0f;
    page_height = 720.0f;
  }
#endif /* CUPS_LITE */

  fprintf(stderr, "DEBUG: page_left=%.2f, page_top=%.2f, page_width=%.2f, page_height=%.2f\n", page_left, page_top, page_width, page_height);

  /* TODO: Support orientation/rotation, different print-scaling modes */
  x_factor = page_width / width;
  y_factor = page_height / height;

  if (x_factor > y_factor && (height * x_factor) <= page_height)
    page_scaling = x_factor;
  else
    page_scaling = y_factor;

  fprintf(stderr, "DEBUG: Scaled dimensions are %.2fx%.2f\n", width * page_scaling, height * page_scaling);

 /*
  * Write pages...
  */

  dsc_header(copies);

  for (copy = 1; copy <= copies; copy ++)
  {
    dsc_page(copy);

    if (depth == 1)
    {
      puts("/DeviceGray setcolorspace");
      decode = "0 1";
    }
    else if (depth == 3)
    {
      puts("/DeviceRGB setcolorspace");
      decode = "0 1 0 1 0 1";
    }
    else
    {
      puts("/DeviceCMYK setcolorspace");
      decode = "0 1 0 1 0 1 0 1";
    }

    printf("gsave %.3f %.3f translate %.3f %.3f scale\n", page_left + 0.5f * (page_width - width * page_scaling), page_top - 0.5f * (page_height - height * page_scaling), page_scaling, page_scaling);
    printf("<</ImageType 1/Width %d/Height %d/BitsPerComponent 8/ImageMatrix[1 0 0 -1 0 1]/Decode[%s]/DataSource currentfile/ASCII85Decode filter/DCTDecode filter/Interpolate true>>image\n", width, height, decode);

    if (fd > 0)
      lseek(fd, 0, SEEK_SET);

    while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
      ascii85(buffer, (int)bytes, 0);

    ascii85(buffer, 0, 1);

    puts("grestore showpage");
  }

  dsc_trailer(0);

  return (0);
}


/*
 * 'pdf_to_ps()' - Convert a PDF file to PostScript.
 */

static int				/* O - Exit status */
pdf_to_ps(const char    *filename,	/* I - Filename */
	  int           copies,		/* I - Number of copies */
	  int           num_options,	/* I - Number of options */
	  cups_option_t *options)	/* I - options */
{
  int		status;			/* Exit status */
  char		tempfile[1024];		/* Temporary file */
  int		tempfd;			/* Temporary file descriptor */
  int		pid;			/* Process ID */
  const char	*pdf_argv[8];		/* Command-line arguments */
  char		pdf_options[1024];	/* Options */
  const char	*value;			/* Option value */
  const char	*job_id,		/* job-id value */
		*job_name;		/* job-name value */


 /*
  * Create a temporary file for the PostScript version...
  */

  if ((tempfd = cupsTempFd(tempfile, sizeof(tempfile))) < 0)
  {
    fprintf(stderr, "ERROR: Unable to create temporary file: %s\n", strerror(errno));
    return (1);
  }

 /*
  * Run cgpdftops or pdftops in the filter directory...
  */

  if ((value = cupsGetOption("PageSize", num_options, options)) != NULL)
    snprintf(pdf_options, sizeof(pdf_options), "PageSize=%s", value);
  else
    pdf_options[0] = '\0';

  if ((job_id = getenv("IPP_JOB_ID")) == NULL)
    job_id = "42";
  if ((job_name = getenv("IPP_JOB_NAME")) == NULL)
    job_name = "untitled";

  pdf_argv[0] = "printer";
  pdf_argv[1] = job_id;
  pdf_argv[2] = cupsUser();
  pdf_argv[3] = job_name;
  pdf_argv[4] = "1";
  pdf_argv[5] = pdf_options;
  pdf_argv[6] = filename;
  pdf_argv[7] = NULL;

  if ((pid = fork()) == 0)
  {
   /*
    * Child comes here...
    */

    close(1);
    dup2(tempfd, 1);
    close(tempfd);

    execv(PDFTOPS, (char * const *)pdf_argv);
    exit(errno);
  }
  else if (pid < 0)
  {
   /*
    * Unable to fork process...
    */

    perror("ERROR: Unable to start PDF filter");

    close(tempfd);
    unlink(tempfile);

    return (1);
  }
  else
  {
   /*
    * Wait for the filter to complete...
    */

    close(tempfd);

#  ifdef HAVE_WAITPID
    while (waitpid(pid, &status, 0) < 0);
#  else
    while (wait(&status) < 0);
#  endif /* HAVE_WAITPID */

    if (status)
    {
      if (WIFEXITED(status))
	fprintf(stderr, "ERROR: " PDFTOPS " exited with status %d.\n", WEXITSTATUS(status));
      else
	fprintf(stderr, "ERROR: " PDFTOPS " terminated with signal %d.\n", WTERMSIG(status));

      unlink(tempfile);
      return (1);
    }
  }

 /*
  * Copy the PostScript output from the command...
  */

  status = ps_to_ps(tempfile, copies);

  unlink(tempfile);

  return (status);
}


/*
 * 'ps_to_ps()' - Copy PostScript to the standard output.
 */

static int				/* O - Exit status */
ps_to_ps(const char    *filename,	/* I - Filename */
	 int           copies)		/* I - Number of copies */
{
  FILE		*fp;				/* File to read from */
  int		copy,				/* Current copy */
		page,				/* Current page number */
		num_pages = 0,			/* Total number of pages */
		first_page,			/* First page */
		last_page;			/* Last page */
  const char	*page_ranges;			/* page-ranges option */
  long		first_pos = -1;			/* Offset for first page */
  char		line[1024];			/* Line from file */


 /*
  * Open the print file...
  */

  if (filename)
  {
    if ((fp = fopen(filename, "rb")) == NULL)
    {
      fprintf(stderr, "ERROR: Unable to open \"%s\": %s\n", filename, strerror(errno));
      return (1);
    }
  }
  else
  {
    copies = 1;
    fp     = stdin;
  }

 /*
  * Check page ranges...
  */

  if ((page_ranges = getenv("IPP_PAGE_RANGES")) != NULL)
  {
    if (sscanf(page_ranges, "%d-%d", &first_page, &last_page) != 2)
    {
      first_page = 1;
      last_page  = INT_MAX;
    }
  }
  else
  {
    first_page = 1;
    last_page  = INT_MAX;
  }

 /*
  * Write the PostScript header for the document...
  */

  dsc_header(0);

  first_pos = 0;

  while (fgets(line, sizeof(line), fp))
  {
    if (!strncmp(line, "%%Page:", 7))
      break;

    first_pos = ftell(fp);

    if (line[0] != '%')
      fputs(line, stdout);
  }

  if (!strncmp(line, "%%Page:", 7))
  {
    for (copy = 0; copy < copies; copy ++)
    {
      int copy_page = 0;		/* Do we copy the page data? */

      if (fp != stdin)
        fseek(fp, first_pos, SEEK_SET);

      page = 0;
      while (fgets(line, sizeof(line), fp))
      {
        if (!strncmp(line, "%%Page:", 7))
        {
          page ++;
          copy_page = page >= first_page && page <= last_page;

          if (copy_page)
          {
	    num_pages ++;
	    dsc_page(num_pages);
	  }
	}
	else if (copy_page)
	  fputs(line, stdout);
      }
    }
  }

  dsc_trailer(num_pages);

  fprintf(stderr, "ATTR: job-impressions=%d\n", num_pages / copies);

  if (fp != stdin)
    fclose(fp);

  return (0);
}


/*
 * 'raster_to_ps()' - Convert PWG Raster/Apple Raster to PostScript.
 *
 * The current implementation locally-decodes the raster data and then writes
 * whole, non-blank lines as 1-line high images with base-85 encoding, resulting
 * in between 10 and 20 times larger output.  A alternate implementation (if it
 * is deemed necessary) would be to implement a PostScript decode procedure that
 * handles the modified packbits decompression so that we just have the base-85
 * encoding overhead (25%).  Furthermore, Level 3 PostScript printers also
 * support Flate compression.
 *
 * That said, the most efficient path with the highest quality is for Clients
 * to supply PDF files and us to use the existing PDF to PostScript conversion
 * filters.
 */

static int				/* O - Exit status */
raster_to_ps(const char *filename)	/* I - Filename */
{
  int			fd;		/* Input file */
  cups_raster_t		*ras;		/* Raster stream */
  cups_page_header2_t	header;		/* Page header */
  int			page = 0;	/* Current page */
  unsigned		y;		/* Current line */
  unsigned char		*line;		/* Line buffer */
  unsigned char		white;		/* White color */
  const char		*decode;	/* Image decode array */


 /*
  * Open the input file...
  */

  if (filename)
  {
    if ((fd = open(filename, O_RDONLY)) < 0)
    {
      fprintf(stderr, "ERROR: Unable to open \"%s\": %s\n", filename, strerror(errno));
      return (1);
    }
  }
  else
  {
    fd = 0;
  }

 /*
  * 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);
  }

  dsc_header(0);

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

    fprintf(stderr, "DEBUG: Page %d: %ux%ux%u\n", page, header.cupsWidth, header.cupsHeight, header.cupsBitsPerPixel);

    if (header.cupsColorSpace != CUPS_CSPACE_W && header.cupsColorSpace != CUPS_CSPACE_SW && header.cupsColorSpace != CUPS_CSPACE_K && header.cupsColorSpace != CUPS_CSPACE_RGB && header.cupsColorSpace != CUPS_CSPACE_SRGB)
    {
      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);

    dsc_page(page);

    puts("gsave");
    printf("%.6f %.6f scale\n", 72.0f / header.HWResolution[0], 72.0f / header.HWResolution[1]);

    switch (header.cupsColorSpace)
    {
      case CUPS_CSPACE_W :
      case CUPS_CSPACE_SW :
          decode = "0 1";
          puts("/DeviceGray setcolorspace");
          white = 255;
          break;

      case CUPS_CSPACE_K :
          decode = "0 1";
          puts("/DeviceGray setcolorspace");
          white = 0;
          break;

      default :
          decode = "0 1 0 1 0 1";
          puts("/DeviceRGB setcolorspace");
          white = 255;
          break;
    }

    printf("gsave /L{grestore gsave 0 exch translate <</ImageType 1/Width %u/Height 1/BitsPerComponent %u/ImageMatrix[1 0 0 -1 0 1]/DataSource currentfile/ASCII85Decode filter/Decode[%s]>>image}bind def\n", header.cupsWidth, header.cupsBitsPerColor, decode);

    for (y = header.cupsHeight; y > 0; y --)
    {
      if (cupsRasterReadPixels(ras, line, header.cupsBytesPerLine))
      {
        if (line[0] != white || memcmp(line, line + 1, header.cupsBytesPerLine - 1))
        {
          printf("%d L\n", y - 1);
          ascii85(line, (int)header.cupsBytesPerLine, 1);
        }
      }
      else
        break;
    }

    fprintf(stderr, "DEBUG: y=%d at end...\n", y);

    puts("grestore grestore");
    puts("showpage");

    free(line);
  }

  cupsRasterClose(ras);

  dsc_trailer(page);

  fprintf(stderr, "ATTR: job-impressions=%d\n", page);

  return (0);
}