2 * "$Id: rastertopwg.c 11558 2014-02-06 18:33:34Z msweet $"
4 * CUPS raster to PWG raster format filter for CUPS.
6 * Copyright 2011, 2014 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 * Compute the real raster size...
98 fprintf(stderr
, "PAGE: %d %d\n", page
, inheader
.NumCopies
);
100 page_width
= (unsigned)(inheader
.cupsPageSize
[0] * inheader
.HWResolution
[0] / 72.0);
101 page_height
= (unsigned)(inheader
.cupsPageSize
[1] * inheader
.HWResolution
[1] / 72.0);
102 page_left
= (unsigned)(inheader
.cupsImagingBBox
[0] * inheader
.HWResolution
[0] / 72.0);
103 page_bottom
= (unsigned)(inheader
.cupsImagingBBox
[1] * inheader
.HWResolution
[1] / 72.0);
104 page_top
= page_height
- page_bottom
- inheader
.cupsHeight
;
105 linesize
= (page_width
* inheader
.cupsBitsPerPixel
+ 7) / 8;
106 lineoffset
= page_left
* inheader
.cupsBitsPerPixel
/ 8; /* Round down */
108 switch (inheader
.cupsColorSpace
)
111 case CUPS_CSPACE_RGB
:
112 case CUPS_CSPACE_SW
:
113 case CUPS_CSPACE_SRGB
:
114 case CUPS_CSPACE_ADOBERGB
:
119 case CUPS_CSPACE_CMYK
:
120 case CUPS_CSPACE_DEVICE1
:
121 case CUPS_CSPACE_DEVICE2
:
122 case CUPS_CSPACE_DEVICE3
:
123 case CUPS_CSPACE_DEVICE4
:
124 case CUPS_CSPACE_DEVICE5
:
125 case CUPS_CSPACE_DEVICE6
:
126 case CUPS_CSPACE_DEVICE7
:
127 case CUPS_CSPACE_DEVICE8
:
128 case CUPS_CSPACE_DEVICE9
:
129 case CUPS_CSPACE_DEVICEA
:
130 case CUPS_CSPACE_DEVICEB
:
131 case CUPS_CSPACE_DEVICEC
:
132 case CUPS_CSPACE_DEVICED
:
133 case CUPS_CSPACE_DEVICEE
:
134 case CUPS_CSPACE_DEVICEF
:
139 _cupsLangPrintFilter(stderr
, "ERROR", _("Unsupported raster data."));
140 fprintf(stderr
, "DEBUG: Unsupported cupsColorSpace %d on page %d.\n",
141 inheader
.cupsColorSpace
, page
);
145 if (inheader
.cupsColorOrder
!= CUPS_ORDER_CHUNKED
)
147 _cupsLangPrintFilter(stderr
, "ERROR", _("Unsupported raster data."));
148 fprintf(stderr
, "DEBUG: Unsupported cupsColorOrder %d on page %d.\n",
149 inheader
.cupsColorOrder
, page
);
153 if (inheader
.cupsBitsPerPixel
!= 1 &&
154 inheader
.cupsBitsPerColor
!= 8 && inheader
.cupsBitsPerColor
!= 16)
156 _cupsLangPrintFilter(stderr
, "ERROR", _("Unsupported raster data."));
157 fprintf(stderr
, "DEBUG: Unsupported cupsBitsPerColor %d on page %d.\n",
158 inheader
.cupsBitsPerColor
, page
);
162 memcpy(&outheader
, &inheader
, sizeof(outheader
));
163 outheader
.cupsWidth
= page_width
;
164 outheader
.cupsHeight
= page_height
;
165 outheader
.cupsBytesPerLine
= linesize
;
167 outheader
.cupsInteger
[14] = 0; /* VendorIdentifier */
168 outheader
.cupsInteger
[15] = 0; /* VendorLength */
170 if ((val
= cupsGetOption("print-content-optimize", num_options
,
173 if (!strcmp(val
, "automatic"))
174 strlcpy(outheader
.OutputType
, "Automatic",
175 sizeof(outheader
.OutputType
));
176 else if (!strcmp(val
, "graphics"))
177 strlcpy(outheader
.OutputType
, "Graphics", sizeof(outheader
.OutputType
));
178 else if (!strcmp(val
, "photo"))
179 strlcpy(outheader
.OutputType
, "Photo", sizeof(outheader
.OutputType
));
180 else if (!strcmp(val
, "text"))
181 strlcpy(outheader
.OutputType
, "Text", sizeof(outheader
.OutputType
));
182 else if (!strcmp(val
, "text-and-graphics"))
183 strlcpy(outheader
.OutputType
, "TextAndGraphics",
184 sizeof(outheader
.OutputType
));
187 fprintf(stderr
, "DEBUG: Unsupported print-content-type \"%s\".\n", val
);
188 outheader
.OutputType
[0] = '\0';
192 if ((val
= cupsGetOption("print-quality", num_options
, options
)) != NULL
)
194 unsigned quality
= (unsigned)atoi(val
); /* print-quality value */
196 if (quality
>= IPP_QUALITY_DRAFT
&& quality
<= IPP_QUALITY_HIGH
)
197 outheader
.cupsInteger
[8] = quality
;
200 fprintf(stderr
, "DEBUG: Unsupported print-quality %d.\n", quality
);
201 outheader
.cupsInteger
[8] = 0;
205 if ((val
= cupsGetOption("print-rendering-intent", num_options
,
208 if (!strcmp(val
, "absolute"))
209 strlcpy(outheader
.cupsRenderingIntent
, "Absolute",
210 sizeof(outheader
.cupsRenderingIntent
));
211 else if (!strcmp(val
, "automatic"))
212 strlcpy(outheader
.cupsRenderingIntent
, "Automatic",
213 sizeof(outheader
.cupsRenderingIntent
));
214 else if (!strcmp(val
, "perceptual"))
215 strlcpy(outheader
.cupsRenderingIntent
, "Perceptual",
216 sizeof(outheader
.cupsRenderingIntent
));
217 else if (!strcmp(val
, "relative"))
218 strlcpy(outheader
.cupsRenderingIntent
, "Relative",
219 sizeof(outheader
.cupsRenderingIntent
));
220 else if (!strcmp(val
, "relative-bpc"))
221 strlcpy(outheader
.cupsRenderingIntent
, "RelativeBpc",
222 sizeof(outheader
.cupsRenderingIntent
));
223 else if (!strcmp(val
, "saturation"))
224 strlcpy(outheader
.cupsRenderingIntent
, "Saturation",
225 sizeof(outheader
.cupsRenderingIntent
));
228 fprintf(stderr
, "DEBUG: Unsupported print-rendering-intent \"%s\".\n",
230 outheader
.cupsRenderingIntent
[0] = '\0';
234 if (inheader
.cupsPageSizeName
[0] &&
235 (pwg_size
= _ppdCacheGetSize(cache
, inheader
.cupsPageSizeName
)) != NULL
)
237 strlcpy(outheader
.cupsPageSizeName
, pwg_size
->map
.pwg
,
238 sizeof(outheader
.cupsPageSizeName
));
242 pwg_media
= _pwgMediaForSize((int)(2540.0 * inheader
.cupsPageSize
[0] /
244 (int)(2540.0 * inheader
.cupsPageSize
[1] /
248 strlcpy(outheader
.cupsPageSizeName
, pwg_media
->pwg
,
249 sizeof(outheader
.cupsPageSizeName
));
252 fprintf(stderr
, "DEBUG: Unsupported PageSize %.2fx%.2f.\n",
253 inheader
.cupsPageSize
[0], inheader
.cupsPageSize
[1]);
254 outheader
.cupsPageSizeName
[0] = '\0';
258 if (inheader
.Duplex
&& !(page
& 1) &&
259 back
&& _cups_strcasecmp(back
->value
, "Normal"))
261 if (_cups_strcasecmp(back
->value
, "Flipped"))
265 outheader
.cupsInteger
[1] = ~0U;/* CrossFeedTransform */
266 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
268 outheader
.cupsInteger
[3] = page_width
- page_left
-
271 outheader
.cupsInteger
[4] = page_top
;
273 outheader
.cupsInteger
[5] = page_width
- page_left
;
275 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
280 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
281 outheader
.cupsInteger
[2] = ~0U;/* FeedTransform */
283 outheader
.cupsInteger
[3] = page_left
;
285 outheader
.cupsInteger
[4] = page_bottom
;
287 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
289 outheader
.cupsInteger
[6] = page_height
- page_top
;
293 else if (_cups_strcasecmp(back
->value
, "ManualTumble"))
297 outheader
.cupsInteger
[1] = ~0U;/* CrossFeedTransform */
298 outheader
.cupsInteger
[2] = ~0U;/* FeedTransform */
300 outheader
.cupsInteger
[3] = page_width
- page_left
-
303 outheader
.cupsInteger
[4] = page_bottom
;
305 outheader
.cupsInteger
[5] = page_width
- page_left
;
307 outheader
.cupsInteger
[6] = page_height
- page_top
;
312 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
313 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
315 outheader
.cupsInteger
[3] = page_left
;
317 outheader
.cupsInteger
[4] = page_top
;
319 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
321 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
325 else if (_cups_strcasecmp(back
->value
, "Rotated"))
329 outheader
.cupsInteger
[1] = ~0U;/* CrossFeedTransform */
330 outheader
.cupsInteger
[2] = ~0U;/* FeedTransform */
332 outheader
.cupsInteger
[3] = page_width
- page_left
-
335 outheader
.cupsInteger
[4] = page_bottom
;
337 outheader
.cupsInteger
[5] = page_width
- page_left
;
339 outheader
.cupsInteger
[6] = page_height
- page_top
;
344 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
345 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
347 outheader
.cupsInteger
[3] = page_left
;
349 outheader
.cupsInteger
[4] = page_top
;
351 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
353 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
360 * Unsupported value...
363 fprintf(stderr
, "DEBUG: Unsupported cupsBackSide \"%s\".\n", back
->value
);
365 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
366 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
368 outheader
.cupsInteger
[3] = page_left
;
370 outheader
.cupsInteger
[4] = page_top
;
372 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
374 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
380 outheader
.cupsInteger
[1] = 1; /* CrossFeedTransform */
381 outheader
.cupsInteger
[2] = 1; /* FeedTransform */
383 outheader
.cupsInteger
[3] = page_left
;
385 outheader
.cupsInteger
[4] = page_top
;
387 outheader
.cupsInteger
[5] = page_left
+ inheader
.cupsWidth
;
389 outheader
.cupsInteger
[6] = page_height
- page_bottom
;
393 if (!cupsRasterWriteHeader2(outras
, &outheader
))
395 _cupsLangPrintFilter(stderr
, "ERROR", _("Error sending raster data."));
396 fprintf(stderr
, "DEBUG: Unable to write header for page %d.\n", page
);
401 * Copy raster data...
404 line
= malloc(linesize
);
406 memset(line
, white
, linesize
);
407 for (y
= page_top
; y
> 0; y
--)
408 if (!cupsRasterWritePixels(outras
, line
, outheader
.cupsBytesPerLine
))
410 _cupsLangPrintFilter(stderr
, "ERROR", _("Error sending raster data."));
411 fprintf(stderr
, "DEBUG: Unable to write line %d for page %d.\n",
412 page_top
- y
+ 1, page
);
416 for (y
= inheader
.cupsHeight
; y
> 0; y
--)
418 cupsRasterReadPixels(inras
, line
+ lineoffset
, inheader
.cupsBytesPerLine
);
419 if (!cupsRasterWritePixels(outras
, line
, outheader
.cupsBytesPerLine
))
421 _cupsLangPrintFilter(stderr
, "ERROR", _("Error sending raster data."));
422 fprintf(stderr
, "DEBUG: Unable to write line %d for page %d.\n",
423 inheader
.cupsHeight
- y
+ page_top
+ 1, page
);
428 memset(line
, white
, linesize
);
429 for (y
= page_bottom
; y
> 0; y
--)
430 if (!cupsRasterWritePixels(outras
, line
, outheader
.cupsBytesPerLine
))
432 _cupsLangPrintFilter(stderr
, "ERROR", _("Error sending raster data."));
433 fprintf(stderr
, "DEBUG: Unable to write line %d for page %d.\n",
434 page_bottom
- y
+ page_top
+ inheader
.cupsHeight
+ 1, page
);
441 cupsRasterClose(inras
);
445 cupsRasterClose(outras
);
452 * End of "$Id: rastertopwg.c 11558 2014-02-06 18:33:34Z msweet $".