2 * CUPS raster to PWG raster format filter for CUPS.
4 * Copyright 2011, 2014-2016 Apple Inc.
6 * These coded instructions, statements, and computer programs are the
7 * property of Apple Inc. and are protected by Federal copyright law.
8 * Distribution and use rights are outlined in the file "LICENSE.txt"
9 * which should have been included with this file. If this file is
10 * file is missing or damaged, see the license at "http://www.cups.org/".
12 * This file is subject to the Apple OS-Developed Software exception.
16 * Include necessary headers...
19 #include <cups/cups-private.h>
20 #include <cups/ppd-private.h>
21 #include <cups/raster.h>
27 * 'main()' - Main entry for filter.
30 int /* O - Exit status */
31 main(int argc
, /* I - Number of command-line args */
32 char *argv
[]) /* I - Command-line arguments */
34 const char *final_content_type
;
35 /* FINAL_CONTENT_TYPE env var */
36 int fd
; /* Raster file */
37 cups_raster_t
*inras
, /* Input raster stream */
38 *outras
; /* Output raster stream */
39 cups_page_header2_t inheader
, /* Input raster page header */
40 outheader
; /* Output raster page header */
41 unsigned y
; /* Current line */
42 unsigned char *line
; /* Line buffer */
43 unsigned page
= 0, /* Current page */
44 page_width
, /* Actual page width */
45 page_height
, /* Actual page height */
46 page_top
, /* Top margin */
47 page_bottom
, /* Bottom margin */
48 page_left
, /* Left margin */
49 linesize
, /* Bytes per line */
50 lineoffset
; /* Offset into line */
51 unsigned char white
; /* White pixel */
52 ppd_file_t
*ppd
; /* PPD file */
53 ppd_attr_t
*back
; /* cupsBackSide attribute */
54 _ppd_cache_t
*cache
; /* PPD cache */
55 pwg_size_t
*pwg_size
; /* PWG media size */
56 pwg_media_t
*pwg_media
; /* PWG media name */
57 int num_options
; /* Number of options */
58 cups_option_t
*options
= NULL
;/* Options */
59 const char *val
; /* Option value */
62 if (argc
< 6 || argc
> 7)
64 puts("Usage: rastertopwg job user title copies options [filename]");
69 if ((fd
= open(argv
[6], O_RDONLY
)) < 0)
71 perror("ERROR: Unable to open print file");
78 if ((final_content_type
= getenv("FINAL_CONTENT_TYPE")) == NULL
)
79 final_content_type
= "image/pwg-raster";
81 inras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
82 outras
= cupsRasterOpen(1, !strcmp(final_content_type
, "image/pwg-raster") ? CUPS_RASTER_WRITE_PWG
: CUPS_RASTER_WRITE_APPLE
);
84 ppd
= ppdOpenFile(getenv("PPD"));
85 back
= ppdFindAttr(ppd
, "cupsBackSide", NULL
);
87 num_options
= cupsParseOptions(argv
[5], 0, &options
);
90 cupsMarkOptions(ppd
, num_options
, options
);
92 cache
= ppd
? ppd
->cache
: NULL
;
94 while (cupsRasterReadHeader2(inras
, &inheader
))
97 * Show page device dictionary...
100 fprintf(stderr
, "DEBUG: Duplex = %d\n", inheader
.Duplex
);
101 fprintf(stderr
, "DEBUG: HWResolution = [ %d %d ]\n", inheader
.HWResolution
[0], inheader
.HWResolution
[1]);
102 fprintf(stderr
, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n", inheader
.ImagingBoundingBox
[0], inheader
.ImagingBoundingBox
[1], inheader
.ImagingBoundingBox
[2], inheader
.ImagingBoundingBox
[3]);
103 fprintf(stderr
, "DEBUG: Margins = [ %d %d ]\n", inheader
.Margins
[0], inheader
.Margins
[1]);
104 fprintf(stderr
, "DEBUG: ManualFeed = %d\n", inheader
.ManualFeed
);
105 fprintf(stderr
, "DEBUG: MediaPosition = %d\n", inheader
.MediaPosition
);
106 fprintf(stderr
, "DEBUG: NumCopies = %d\n", inheader
.NumCopies
);
107 fprintf(stderr
, "DEBUG: Orientation = %d\n", inheader
.Orientation
);
108 fprintf(stderr
, "DEBUG: PageSize = [ %d %d ]\n", inheader
.PageSize
[0], inheader
.PageSize
[1]);
109 fprintf(stderr
, "DEBUG: cupsWidth = %d\n", inheader
.cupsWidth
);
110 fprintf(stderr
, "DEBUG: cupsHeight = %d\n", inheader
.cupsHeight
);
111 fprintf(stderr
, "DEBUG: cupsMediaType = %d\n", inheader
.cupsMediaType
);
112 fprintf(stderr
, "DEBUG: cupsBitsPerColor = %d\n", inheader
.cupsBitsPerColor
);
113 fprintf(stderr
, "DEBUG: cupsBitsPerPixel = %d\n", inheader
.cupsBitsPerPixel
);
114 fprintf(stderr
, "DEBUG: cupsBytesPerLine = %d\n", inheader
.cupsBytesPerLine
);
115 fprintf(stderr
, "DEBUG: cupsColorOrder = %d\n", inheader
.cupsColorOrder
);
116 fprintf(stderr
, "DEBUG: cupsColorSpace = %d\n", inheader
.cupsColorSpace
);
117 fprintf(stderr
, "DEBUG: cupsCompression = %d\n", inheader
.cupsCompression
);
120 * Compute the real raster size...
125 fprintf(stderr
, "PAGE: %d %d\n", page
, inheader
.NumCopies
);
127 page_width
= (unsigned)(inheader
.cupsPageSize
[0] * inheader
.HWResolution
[0] / 72.0);
128 page_height
= (unsigned)(inheader
.cupsPageSize
[1] * inheader
.HWResolution
[1] / 72.0);
129 page_left
= (unsigned)(inheader
.cupsImagingBBox
[0] * inheader
.HWResolution
[0] / 72.0);
130 page_bottom
= (unsigned)(inheader
.cupsImagingBBox
[1] * inheader
.HWResolution
[1] / 72.0);
131 page_top
= page_height
- page_bottom
- inheader
.cupsHeight
;
132 linesize
= (page_width
* inheader
.cupsBitsPerPixel
+ 7) / 8;
133 lineoffset
= page_left
* inheader
.cupsBitsPerPixel
/ 8; /* Round down */
135 if (page_left
> page_width
|| page_top
> page_height
|| page_bottom
> page_height
)
137 _cupsLangPrintFilter(stderr
, "ERROR", _("Unsupported raster data."));
138 fprintf(stderr
, "DEBUG: Bad bottom/left/top margin on page %d.\n", page
);
142 switch (inheader
.cupsColorSpace
)
145 case CUPS_CSPACE_RGB
:
146 case CUPS_CSPACE_SW
:
147 case CUPS_CSPACE_SRGB
:
148 case CUPS_CSPACE_ADOBERGB
:
153 case CUPS_CSPACE_CMYK
:
154 case CUPS_CSPACE_DEVICE1
:
155 case CUPS_CSPACE_DEVICE2
:
156 case CUPS_CSPACE_DEVICE3
:
157 case CUPS_CSPACE_DEVICE4
:
158 case CUPS_CSPACE_DEVICE5
:
159 case CUPS_CSPACE_DEVICE6
:
160 case CUPS_CSPACE_DEVICE7
:
161 case CUPS_CSPACE_DEVICE8
:
162 case CUPS_CSPACE_DEVICE9
:
163 case CUPS_CSPACE_DEVICEA
:
164 case CUPS_CSPACE_DEVICEB
:
165 case CUPS_CSPACE_DEVICEC
:
166 case CUPS_CSPACE_DEVICED
:
167 case CUPS_CSPACE_DEVICEE
:
168 case CUPS_CSPACE_DEVICEF
:
173 _cupsLangPrintFilter(stderr
, "ERROR", _("Unsupported raster data."));
174 fprintf(stderr
, "DEBUG: Unsupported cupsColorSpace %d on page %d.\n",
175 inheader
.cupsColorSpace
, page
);
179 if (inheader
.cupsColorOrder
!= CUPS_ORDER_CHUNKED
)
181 _cupsLangPrintFilter(stderr
, "ERROR", _("Unsupported raster data."));
182 fprintf(stderr
, "DEBUG: Unsupported cupsColorOrder %d on page %d.\n",
183 inheader
.cupsColorOrder
, page
);
187 if (inheader
.cupsBitsPerPixel
!= 1 &&
188 inheader
.cupsBitsPerColor
!= 8 && inheader
.cupsBitsPerColor
!= 16)
190 _cupsLangPrintFilter(stderr
, "ERROR", _("Unsupported raster data."));
191 fprintf(stderr
, "DEBUG: Unsupported cupsBitsPerColor %d on page %d.\n",
192 inheader
.cupsBitsPerColor
, page
);
196 memcpy(&outheader
, &inheader
, sizeof(outheader
));
197 outheader
.cupsWidth
= page_width
;
198 outheader
.cupsHeight
= page_height
;
199 outheader
.cupsBytesPerLine
= linesize
;
201 outheader
.cupsInteger
[14] = 0; /* VendorIdentifier */
202 outheader
.cupsInteger
[15] = 0; /* VendorLength */
204 if ((val
= cupsGetOption("print-content-optimize", num_options
,
207 if (!strcmp(val
, "automatic"))
208 strlcpy(outheader
.OutputType
, "Automatic",
209 sizeof(outheader
.OutputType
));
210 else if (!strcmp(val
, "graphics"))
211 strlcpy(outheader
.OutputType
, "Graphics", sizeof(outheader
.OutputType
));
212 else if (!strcmp(val
, "photo"))
213 strlcpy(outheader
.OutputType
, "Photo", sizeof(outheader
.OutputType
));
214 else if (!strcmp(val
, "text"))
215 strlcpy(outheader
.OutputType
, "Text", sizeof(outheader
.OutputType
));
216 else if (!strcmp(val
, "text-and-graphics"))
217 strlcpy(outheader
.OutputType
, "TextAndGraphics",
218 sizeof(outheader
.OutputType
));
221 fputs("DEBUG: Unsupported print-content-optimize value.\n", stderr
);
222 outheader
.OutputType
[0] = '\0';
226 if ((val
= cupsGetOption("print-quality", num_options
, options
)) != NULL
)
228 unsigned quality
= (unsigned)atoi(val
); /* print-quality value */
230 if (quality
>= IPP_QUALITY_DRAFT
&& quality
<= IPP_QUALITY_HIGH
)
231 outheader
.cupsInteger
[8] = quality
;
234 fprintf(stderr
, "DEBUG: Unsupported print-quality %d.\n", quality
);
235 outheader
.cupsInteger
[8] = 0;
239 if ((val
= cupsGetOption("print-rendering-intent", num_options
,
242 if (!strcmp(val
, "absolute"))
243 strlcpy(outheader
.cupsRenderingIntent
, "Absolute",
244 sizeof(outheader
.cupsRenderingIntent
));
245 else if (!strcmp(val
, "automatic"))
246 strlcpy(outheader
.cupsRenderingIntent
, "Automatic",
247 sizeof(outheader
.cupsRenderingIntent
));
248 else if (!strcmp(val
, "perceptual"))
249 strlcpy(outheader
.cupsRenderingIntent
, "Perceptual",
250 sizeof(outheader
.cupsRenderingIntent
));
251 else if (!strcmp(val
, "relative"))
252 strlcpy(outheader
.cupsRenderingIntent
, "Relative",
253 sizeof(outheader
.cupsRenderingIntent
));
254 else if (!strcmp(val
, "relative-bpc"))
255 strlcpy(outheader
.cupsRenderingIntent
, "RelativeBpc",
256 sizeof(outheader
.cupsRenderingIntent
));
257 else if (!strcmp(val
, "saturation"))
258 strlcpy(outheader
.cupsRenderingIntent
, "Saturation",
259 sizeof(outheader
.cupsRenderingIntent
));
262 fputs("DEBUG: Unsupported print-rendering-intent value.\n", stderr
);
263 outheader
.cupsRenderingIntent
[0] = '\0';
267 if (inheader
.cupsPageSizeName
[0] &&
268 (pwg_size
= _ppdCacheGetSize(cache
, inheader
.cupsPageSizeName
)) != NULL
)
270 strlcpy(outheader
.cupsPageSizeName
, pwg_size
->map
.pwg
,
271 sizeof(outheader
.cupsPageSizeName
));
275 pwg_media
= pwgMediaForSize((int)(2540.0 * inheader
.cupsPageSize
[0] / 72.0),
276 (int)(2540.0 * inheader
.cupsPageSize
[1] / 72.0));
279 strlcpy(outheader
.cupsPageSizeName
, pwg_media
->pwg
,
280 sizeof(outheader
.cupsPageSizeName
));
283 fprintf(stderr
, "DEBUG: Unsupported PageSize %.2fx%.2f.\n",
284 inheader
.cupsPageSize
[0], inheader
.cupsPageSize
[1]);
285 outheader
.cupsPageSizeName
[0] = '\0';
289 if (inheader
.Duplex
&& !(page
& 1) &&
290 back
&& _cups_strcasecmp(back
->value
, "Normal"))
292 if (_cups_strcasecmp(back
->value
, "Flipped"))
296 outheader
.cupsInteger
[1] = ~0U;/* CrossFeedTransform */
297 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
299 outheader
.cupsInteger
[3] = page_width
- page_left
-
302 outheader
.cupsInteger
[4] = page_top
;
304 outheader
.cupsInteger
[5] = page_width
- page_left
;
306 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
311 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
312 outheader
.cupsInteger
[2] = ~0U;/* FeedTransform */
314 outheader
.cupsInteger
[3] = page_left
;
316 outheader
.cupsInteger
[4] = page_bottom
;
318 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
320 outheader
.cupsInteger
[6] = page_height
- page_top
;
324 else if (_cups_strcasecmp(back
->value
, "ManualTumble"))
328 outheader
.cupsInteger
[1] = ~0U;/* CrossFeedTransform */
329 outheader
.cupsInteger
[2] = ~0U;/* FeedTransform */
331 outheader
.cupsInteger
[3] = page_width
- page_left
-
334 outheader
.cupsInteger
[4] = page_bottom
;
336 outheader
.cupsInteger
[5] = page_width
- page_left
;
338 outheader
.cupsInteger
[6] = page_height
- page_top
;
343 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
344 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
346 outheader
.cupsInteger
[3] = page_left
;
348 outheader
.cupsInteger
[4] = page_top
;
350 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
352 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
356 else if (_cups_strcasecmp(back
->value
, "Rotated"))
360 outheader
.cupsInteger
[1] = ~0U;/* CrossFeedTransform */
361 outheader
.cupsInteger
[2] = ~0U;/* FeedTransform */
363 outheader
.cupsInteger
[3] = page_width
- page_left
-
366 outheader
.cupsInteger
[4] = page_bottom
;
368 outheader
.cupsInteger
[5] = page_width
- page_left
;
370 outheader
.cupsInteger
[6] = page_height
- page_top
;
375 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
376 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
378 outheader
.cupsInteger
[3] = page_left
;
380 outheader
.cupsInteger
[4] = page_top
;
382 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
384 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
391 * Unsupported value...
394 fputs("DEBUG: Unsupported cupsBackSide value.\n", stderr
);
396 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
397 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
399 outheader
.cupsInteger
[3] = page_left
;
401 outheader
.cupsInteger
[4] = page_top
;
403 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
405 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
411 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
412 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
414 outheader
.cupsInteger
[3] = page_left
;
416 outheader
.cupsInteger
[4] = page_top
;
418 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
420 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
424 if (!cupsRasterWriteHeader2(outras
, &outheader
))
426 _cupsLangPrintFilter(stderr
, "ERROR", _("Error sending raster data."));
427 fprintf(stderr
, "DEBUG: Unable to write header for page %d.\n", page
);
432 * Copy raster data...
435 if (linesize
< inheader
.cupsBytesPerLine
)
436 linesize
= inheader
.cupsBytesPerLine
;
438 line
= malloc(linesize
);
440 memset(line
, white
, linesize
);
441 for (y
= page_top
; y
> 0; y
--)
442 if (!cupsRasterWritePixels(outras
, line
, outheader
.cupsBytesPerLine
))
444 _cupsLangPrintFilter(stderr
, "ERROR", _("Error sending raster data."));
445 fprintf(stderr
, "DEBUG: Unable to write line %d for page %d.\n",
446 page_top
- y
+ 1, page
);
450 for (y
= inheader
.cupsHeight
; y
> 0; y
--)
452 if (cupsRasterReadPixels(inras
, line
+ lineoffset
, inheader
.cupsBytesPerLine
) != inheader
.cupsBytesPerLine
)
454 _cupsLangPrintFilter(stderr
, "ERROR", _("Error reading raster data."));
455 fprintf(stderr
, "DEBUG: Unable to read line %d for page %d.\n",
456 inheader
.cupsHeight
- y
+ page_top
+ 1, page
);
460 if (!cupsRasterWritePixels(outras
, line
, outheader
.cupsBytesPerLine
))
462 _cupsLangPrintFilter(stderr
, "ERROR", _("Error sending raster data."));
463 fprintf(stderr
, "DEBUG: Unable to write line %d for page %d.\n",
464 inheader
.cupsHeight
- y
+ page_top
+ 1, page
);
469 memset(line
, white
, linesize
);
470 for (y
= page_bottom
; y
> 0; y
--)
471 if (!cupsRasterWritePixels(outras
, line
, outheader
.cupsBytesPerLine
))
473 _cupsLangPrintFilter(stderr
, "ERROR", _("Error sending raster data."));
474 fprintf(stderr
, "DEBUG: Unable to write line %d for page %d.\n",
475 page_bottom
- y
+ page_top
+ inheader
.cupsHeight
+ 1, page
);
482 cupsRasterClose(inras
);
486 cupsRasterClose(outras
);