4 * CUPS raster to PWG raster format filter for CUPS.
6 * Copyright 2011, 2014-2015 Apple Inc.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright law.
10 * Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * Include necessary headers...
21 #include <cups/cups-private.h>
22 #include <cups/raster.h>
28 * 'main()' - Main entry for filter.
31 int /* O - Exit status */
32 main(int argc
, /* I - Number of command-line args */
33 char *argv
[]) /* I - Command-line arguments */
35 int fd
; /* Raster file */
36 cups_raster_t
*inras
, /* Input raster stream */
37 *outras
; /* Output raster stream */
38 cups_page_header2_t inheader
, /* Input raster page header */
39 outheader
; /* Output raster page header */
40 unsigned y
; /* Current line */
41 unsigned char *line
; /* Line buffer */
42 unsigned page
= 0, /* Current page */
43 page_width
, /* Actual page width */
44 page_height
, /* Actual page height */
45 page_top
, /* Top margin */
46 page_bottom
, /* Bottom margin */
47 page_left
, /* Left margin */
48 linesize
, /* Bytes per line */
49 lineoffset
; /* Offset into line */
50 unsigned char white
; /* White pixel */
51 ppd_file_t
*ppd
; /* PPD file */
52 ppd_attr_t
*back
; /* cupsBackSize attribute */
53 _ppd_cache_t
*cache
; /* PPD cache */
54 _pwg_size_t
*pwg_size
; /* PWG media size */
55 _pwg_media_t
*pwg_media
; /* PWG media name */
56 int num_options
; /* Number of options */
57 cups_option_t
*options
= NULL
;/* Options */
58 const char *val
; /* Option value */
61 if (argc
< 6 || argc
> 7)
63 puts("Usage: rastertopwg job user title copies options [filename]");
68 if ((fd
= open(argv
[6], O_RDONLY
)) < 0)
70 perror("ERROR: Unable to open print file");
77 inras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
78 outras
= cupsRasterOpen(1, CUPS_RASTER_WRITE_PWG
);
80 ppd
= ppdOpenFile(getenv("PPD"));
81 back
= ppdFindAttr(ppd
, "cupsBackSide", NULL
);
83 num_options
= cupsParseOptions(argv
[5], 0, &options
);
86 cupsMarkOptions(ppd
, num_options
, options
);
88 cache
= ppd
? ppd
->cache
: NULL
;
90 while (cupsRasterReadHeader2(inras
, &inheader
))
93 * Show page device dictionary...
96 fprintf(stderr
, "DEBUG: Duplex = %d\n", inheader
.Duplex
);
97 fprintf(stderr
, "DEBUG: HWResolution = [ %d %d ]\n", inheader
.HWResolution
[0], inheader
.HWResolution
[1]);
98 fprintf(stderr
, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n", inheader
.ImagingBoundingBox
[0], inheader
.ImagingBoundingBox
[1], inheader
.ImagingBoundingBox
[2], inheader
.ImagingBoundingBox
[3]);
99 fprintf(stderr
, "DEBUG: Margins = [ %d %d ]\n", inheader
.Margins
[0], inheader
.Margins
[1]);
100 fprintf(stderr
, "DEBUG: ManualFeed = %d\n", inheader
.ManualFeed
);
101 fprintf(stderr
, "DEBUG: MediaPosition = %d\n", inheader
.MediaPosition
);
102 fprintf(stderr
, "DEBUG: NumCopies = %d\n", inheader
.NumCopies
);
103 fprintf(stderr
, "DEBUG: Orientation = %d\n", inheader
.Orientation
);
104 fprintf(stderr
, "DEBUG: PageSize = [ %d %d ]\n", inheader
.PageSize
[0], inheader
.PageSize
[1]);
105 fprintf(stderr
, "DEBUG: cupsWidth = %d\n", inheader
.cupsWidth
);
106 fprintf(stderr
, "DEBUG: cupsHeight = %d\n", inheader
.cupsHeight
);
107 fprintf(stderr
, "DEBUG: cupsMediaType = %d\n", inheader
.cupsMediaType
);
108 fprintf(stderr
, "DEBUG: cupsBitsPerColor = %d\n", inheader
.cupsBitsPerColor
);
109 fprintf(stderr
, "DEBUG: cupsBitsPerPixel = %d\n", inheader
.cupsBitsPerPixel
);
110 fprintf(stderr
, "DEBUG: cupsBytesPerLine = %d\n", inheader
.cupsBytesPerLine
);
111 fprintf(stderr
, "DEBUG: cupsColorOrder = %d\n", inheader
.cupsColorOrder
);
112 fprintf(stderr
, "DEBUG: cupsColorSpace = %d\n", inheader
.cupsColorSpace
);
113 fprintf(stderr
, "DEBUG: cupsCompression = %d\n", inheader
.cupsCompression
);
116 * Compute the real raster size...
121 fprintf(stderr
, "PAGE: %d %d\n", page
, inheader
.NumCopies
);
123 page_width
= (unsigned)(inheader
.cupsPageSize
[0] * inheader
.HWResolution
[0] / 72.0);
124 page_height
= (unsigned)(inheader
.cupsPageSize
[1] * inheader
.HWResolution
[1] / 72.0);
125 page_left
= (unsigned)(inheader
.cupsImagingBBox
[0] * inheader
.HWResolution
[0] / 72.0);
126 page_bottom
= (unsigned)(inheader
.cupsImagingBBox
[1] * inheader
.HWResolution
[1] / 72.0);
127 page_top
= page_height
- page_bottom
- inheader
.cupsHeight
;
128 linesize
= (page_width
* inheader
.cupsBitsPerPixel
+ 7) / 8;
129 lineoffset
= page_left
* inheader
.cupsBitsPerPixel
/ 8; /* Round down */
131 if (page_left
> page_width
|| page_top
> page_height
|| page_bottom
> page_height
)
133 _cupsLangPrintFilter(stderr
, "ERROR", _("Unsupported raster data."));
134 fprintf(stderr
, "DEBUG: Bad bottom/left/top margin on page %d.\n", page
);
138 switch (inheader
.cupsColorSpace
)
141 case CUPS_CSPACE_RGB
:
142 case CUPS_CSPACE_SW
:
143 case CUPS_CSPACE_SRGB
:
144 case CUPS_CSPACE_ADOBERGB
:
149 case CUPS_CSPACE_CMYK
:
150 case CUPS_CSPACE_DEVICE1
:
151 case CUPS_CSPACE_DEVICE2
:
152 case CUPS_CSPACE_DEVICE3
:
153 case CUPS_CSPACE_DEVICE4
:
154 case CUPS_CSPACE_DEVICE5
:
155 case CUPS_CSPACE_DEVICE6
:
156 case CUPS_CSPACE_DEVICE7
:
157 case CUPS_CSPACE_DEVICE8
:
158 case CUPS_CSPACE_DEVICE9
:
159 case CUPS_CSPACE_DEVICEA
:
160 case CUPS_CSPACE_DEVICEB
:
161 case CUPS_CSPACE_DEVICEC
:
162 case CUPS_CSPACE_DEVICED
:
163 case CUPS_CSPACE_DEVICEE
:
164 case CUPS_CSPACE_DEVICEF
:
169 _cupsLangPrintFilter(stderr
, "ERROR", _("Unsupported raster data."));
170 fprintf(stderr
, "DEBUG: Unsupported cupsColorSpace %d on page %d.\n",
171 inheader
.cupsColorSpace
, page
);
175 if (inheader
.cupsColorOrder
!= CUPS_ORDER_CHUNKED
)
177 _cupsLangPrintFilter(stderr
, "ERROR", _("Unsupported raster data."));
178 fprintf(stderr
, "DEBUG: Unsupported cupsColorOrder %d on page %d.\n",
179 inheader
.cupsColorOrder
, page
);
183 if (inheader
.cupsBitsPerPixel
!= 1 &&
184 inheader
.cupsBitsPerColor
!= 8 && inheader
.cupsBitsPerColor
!= 16)
186 _cupsLangPrintFilter(stderr
, "ERROR", _("Unsupported raster data."));
187 fprintf(stderr
, "DEBUG: Unsupported cupsBitsPerColor %d on page %d.\n",
188 inheader
.cupsBitsPerColor
, page
);
192 memcpy(&outheader
, &inheader
, sizeof(outheader
));
193 outheader
.cupsWidth
= page_width
;
194 outheader
.cupsHeight
= page_height
;
195 outheader
.cupsBytesPerLine
= linesize
;
197 outheader
.cupsInteger
[14] = 0; /* VendorIdentifier */
198 outheader
.cupsInteger
[15] = 0; /* VendorLength */
200 if ((val
= cupsGetOption("print-content-optimize", num_options
,
203 if (!strcmp(val
, "automatic"))
204 strlcpy(outheader
.OutputType
, "Automatic",
205 sizeof(outheader
.OutputType
));
206 else if (!strcmp(val
, "graphics"))
207 strlcpy(outheader
.OutputType
, "Graphics", sizeof(outheader
.OutputType
));
208 else if (!strcmp(val
, "photo"))
209 strlcpy(outheader
.OutputType
, "Photo", sizeof(outheader
.OutputType
));
210 else if (!strcmp(val
, "text"))
211 strlcpy(outheader
.OutputType
, "Text", sizeof(outheader
.OutputType
));
212 else if (!strcmp(val
, "text-and-graphics"))
213 strlcpy(outheader
.OutputType
, "TextAndGraphics",
214 sizeof(outheader
.OutputType
));
217 fputs("DEBUG: Unsupported print-content-optimize value.\n", stderr
);
218 outheader
.OutputType
[0] = '\0';
222 if ((val
= cupsGetOption("print-quality", num_options
, options
)) != NULL
)
224 unsigned quality
= (unsigned)atoi(val
); /* print-quality value */
226 if (quality
>= IPP_QUALITY_DRAFT
&& quality
<= IPP_QUALITY_HIGH
)
227 outheader
.cupsInteger
[8] = quality
;
230 fprintf(stderr
, "DEBUG: Unsupported print-quality %d.\n", quality
);
231 outheader
.cupsInteger
[8] = 0;
235 if ((val
= cupsGetOption("print-rendering-intent", num_options
,
238 if (!strcmp(val
, "absolute"))
239 strlcpy(outheader
.cupsRenderingIntent
, "Absolute",
240 sizeof(outheader
.cupsRenderingIntent
));
241 else if (!strcmp(val
, "automatic"))
242 strlcpy(outheader
.cupsRenderingIntent
, "Automatic",
243 sizeof(outheader
.cupsRenderingIntent
));
244 else if (!strcmp(val
, "perceptual"))
245 strlcpy(outheader
.cupsRenderingIntent
, "Perceptual",
246 sizeof(outheader
.cupsRenderingIntent
));
247 else if (!strcmp(val
, "relative"))
248 strlcpy(outheader
.cupsRenderingIntent
, "Relative",
249 sizeof(outheader
.cupsRenderingIntent
));
250 else if (!strcmp(val
, "relative-bpc"))
251 strlcpy(outheader
.cupsRenderingIntent
, "RelativeBpc",
252 sizeof(outheader
.cupsRenderingIntent
));
253 else if (!strcmp(val
, "saturation"))
254 strlcpy(outheader
.cupsRenderingIntent
, "Saturation",
255 sizeof(outheader
.cupsRenderingIntent
));
258 fputs("DEBUG: Unsupported print-rendering-intent value.\n", stderr
);
259 outheader
.cupsRenderingIntent
[0] = '\0';
263 if (inheader
.cupsPageSizeName
[0] &&
264 (pwg_size
= _ppdCacheGetSize(cache
, inheader
.cupsPageSizeName
)) != NULL
)
266 strlcpy(outheader
.cupsPageSizeName
, pwg_size
->map
.pwg
,
267 sizeof(outheader
.cupsPageSizeName
));
271 pwg_media
= _pwgMediaForSize((int)(2540.0 * inheader
.cupsPageSize
[0] /
273 (int)(2540.0 * inheader
.cupsPageSize
[1] /
277 strlcpy(outheader
.cupsPageSizeName
, pwg_media
->pwg
,
278 sizeof(outheader
.cupsPageSizeName
));
281 fprintf(stderr
, "DEBUG: Unsupported PageSize %.2fx%.2f.\n",
282 inheader
.cupsPageSize
[0], inheader
.cupsPageSize
[1]);
283 outheader
.cupsPageSizeName
[0] = '\0';
287 if (inheader
.Duplex
&& !(page
& 1) &&
288 back
&& _cups_strcasecmp(back
->value
, "Normal"))
290 if (_cups_strcasecmp(back
->value
, "Flipped"))
294 outheader
.cupsInteger
[1] = ~0U;/* CrossFeedTransform */
295 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
297 outheader
.cupsInteger
[3] = page_width
- page_left
-
300 outheader
.cupsInteger
[4] = page_top
;
302 outheader
.cupsInteger
[5] = page_width
- page_left
;
304 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
309 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
310 outheader
.cupsInteger
[2] = ~0U;/* FeedTransform */
312 outheader
.cupsInteger
[3] = page_left
;
314 outheader
.cupsInteger
[4] = page_bottom
;
316 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
318 outheader
.cupsInteger
[6] = page_height
- page_top
;
322 else if (_cups_strcasecmp(back
->value
, "ManualTumble"))
326 outheader
.cupsInteger
[1] = ~0U;/* CrossFeedTransform */
327 outheader
.cupsInteger
[2] = ~0U;/* FeedTransform */
329 outheader
.cupsInteger
[3] = page_width
- page_left
-
332 outheader
.cupsInteger
[4] = page_bottom
;
334 outheader
.cupsInteger
[5] = page_width
- page_left
;
336 outheader
.cupsInteger
[6] = page_height
- page_top
;
341 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
342 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
344 outheader
.cupsInteger
[3] = page_left
;
346 outheader
.cupsInteger
[4] = page_top
;
348 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
350 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
354 else if (_cups_strcasecmp(back
->value
, "Rotated"))
358 outheader
.cupsInteger
[1] = ~0U;/* CrossFeedTransform */
359 outheader
.cupsInteger
[2] = ~0U;/* FeedTransform */
361 outheader
.cupsInteger
[3] = page_width
- page_left
-
364 outheader
.cupsInteger
[4] = page_bottom
;
366 outheader
.cupsInteger
[5] = page_width
- page_left
;
368 outheader
.cupsInteger
[6] = page_height
- page_top
;
373 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
374 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
376 outheader
.cupsInteger
[3] = page_left
;
378 outheader
.cupsInteger
[4] = page_top
;
380 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
382 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
389 * Unsupported value...
392 fputs("DEBUG: Unsupported cupsBackSide value.\n", stderr
);
394 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
395 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
397 outheader
.cupsInteger
[3] = page_left
;
399 outheader
.cupsInteger
[4] = page_top
;
401 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
403 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
409 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
410 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
412 outheader
.cupsInteger
[3] = page_left
;
414 outheader
.cupsInteger
[4] = page_top
;
416 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
418 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
422 if (!cupsRasterWriteHeader2(outras
, &outheader
))
424 _cupsLangPrintFilter(stderr
, "ERROR", _("Error sending raster data."));
425 fprintf(stderr
, "DEBUG: Unable to write header for page %d.\n", page
);
430 * Copy raster data...
433 if (linesize
< inheader
.cupsBytesPerLine
)
434 linesize
= inheader
.cupsBytesPerLine
;
436 line
= malloc(linesize
);
438 memset(line
, white
, linesize
);
439 for (y
= page_top
; y
> 0; y
--)
440 if (!cupsRasterWritePixels(outras
, line
, outheader
.cupsBytesPerLine
))
442 _cupsLangPrintFilter(stderr
, "ERROR", _("Error sending raster data."));
443 fprintf(stderr
, "DEBUG: Unable to write line %d for page %d.\n",
444 page_top
- y
+ 1, page
);
448 for (y
= inheader
.cupsHeight
; y
> 0; y
--)
450 if (cupsRasterReadPixels(inras
, line
+ lineoffset
, inheader
.cupsBytesPerLine
) != inheader
.cupsBytesPerLine
)
452 _cupsLangPrintFilter(stderr
, "ERROR", _("Error reading raster data."));
453 fprintf(stderr
, "DEBUG: Unable to read line %d for page %d.\n",
454 inheader
.cupsHeight
- y
+ page_top
+ 1, page
);
458 if (!cupsRasterWritePixels(outras
, line
, outheader
.cupsBytesPerLine
))
460 _cupsLangPrintFilter(stderr
, "ERROR", _("Error sending raster data."));
461 fprintf(stderr
, "DEBUG: Unable to write line %d for page %d.\n",
462 inheader
.cupsHeight
- y
+ page_top
+ 1, page
);
467 memset(line
, white
, linesize
);
468 for (y
= page_bottom
; y
> 0; y
--)
469 if (!cupsRasterWritePixels(outras
, line
, outheader
.cupsBytesPerLine
))
471 _cupsLangPrintFilter(stderr
, "ERROR", _("Error sending raster data."));
472 fprintf(stderr
, "DEBUG: Unable to write line %d for page %d.\n",
473 page_bottom
- y
+ page_top
+ inheader
.cupsHeight
+ 1, page
);
480 cupsRasterClose(inras
);
484 cupsRasterClose(outras
);