]> git.ipfire.org Git - thirdparty/cups.git/blame - filter/rastertopwg.c
Merge changes from CUPS 1.5svn-r9675.
[thirdparty/cups.git] / filter / rastertopwg.c
CommitLineData
07ed0e9a
MS
1/*
2 * "$Id$"
3 *
4 * CUPS raster to PWG raster format filter for CUPS.
5 *
6 * Copyright 2011 Apple Inc.
7 *
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/".
13 *
14 * This file is subject to the Apple OS-Developed Software exception.
15 *
16 * Contents:
17 *
18 * main() - Main entry for filter.
19 */
20
21/*
22 * Include necessary headers...
23 */
24
25#include <cups/cups.h>
26#include <cups/language-private.h>
27#include <cups/raster.h>
28#include <cups/string-private.h>
29#include <unistd.h>
30#include <fcntl.h>
31
32
33/*
34 * 'main()' - Main entry for filter.
35 */
36
37int /* O - Exit status */
38main(int argc, /* I - Number of command-line args */
39 char *argv[]) /* I - Command-line arguments */
40{
41 int fd; /* Raster file */
42 cups_raster_t *inras, /* Input raster stream */
43 *outras; /* Output raster stream */
44 cups_page_header2_t inheader, /* Input raster page header */
45 outheader; /* Output raster page header */
46 int y; /* Current line */
47 unsigned char *line; /* Line buffer */
48 int page = 0, /* Current page */
49 page_width, /* Actual page width */
50 page_height, /* Actual page height */
51 page_top, /* Top margin */
52 page_bottom, /* Bottom margin */
53 page_left, /* Left margin */
54 linesize, /* Bytes per line */
55 lineoffset; /* Offset into line */
56 unsigned char white; /* White pixel */
57
58
59 if (argc < 6 || argc > 7)
60 {
61 puts("Usage: rastertopwg job user title copies options [filename]");
62 return (1);
63 }
64 else if (argc == 7)
65 {
66 if ((fd = open(argv[6], O_RDONLY)) < 0)
67 {
68 perror("ERROR: Unable to open print file");
69 return (1);
70 }
71 }
72 else
73 fd = 0;
74
75 inras = cupsRasterOpen(fd, CUPS_RASTER_READ);
76 outras = cupsRasterOpen(1, CUPS_RASTER_WRITE_PWG);
77
78 while (cupsRasterReadHeader2(inras, &inheader))
79 {
80 /*
81 * Compute the real raster size...
82 */
83
84 page ++;
85
86 fprintf(stderr, "PAGE: %d %d\n", page, inheader.NumCopies);
87
88 page_width = (int)(inheader.cupsPageSize[0] * inheader.HWResolution[0] /
89 72.0);
90 page_height = (int)(inheader.cupsPageSize[1] * inheader.HWResolution[1] /
91 72.0);
92 page_left = (int)(inheader.cupsImagingBBox[0] *
93 inheader.HWResolution[0] / 72.0);
94 page_bottom = (int)(inheader.cupsImagingBBox[1] *
95 inheader.HWResolution[1] / 72.0);
96 page_top = page_height - page_bottom - inheader.cupsHeight;
97 linesize = (page_width * inheader.cupsBitsPerPixel + 7) / 8;
98 lineoffset = page_left * inheader.cupsBitsPerPixel / 8; /* Round down */
99
100 switch (inheader.cupsColorSpace)
101 {
102 case CUPS_CSPACE_W :
103 case CUPS_CSPACE_RGB :
104 case CUPS_CSPACE_SW :
105 case CUPS_CSPACE_SRGB :
106 case CUPS_CSPACE_ADOBERGB :
107 white = 255;
108 break;
109
110 case CUPS_CSPACE_K :
111 case CUPS_CSPACE_CMYK :
112 case CUPS_CSPACE_DEVICE1 :
113 case CUPS_CSPACE_DEVICE2 :
114 case CUPS_CSPACE_DEVICE3 :
115 case CUPS_CSPACE_DEVICE4 :
116 case CUPS_CSPACE_DEVICE5 :
117 case CUPS_CSPACE_DEVICE6 :
118 case CUPS_CSPACE_DEVICE7 :
119 case CUPS_CSPACE_DEVICE8 :
120 case CUPS_CSPACE_DEVICE9 :
121 case CUPS_CSPACE_DEVICEA :
122 case CUPS_CSPACE_DEVICEB :
123 case CUPS_CSPACE_DEVICEC :
124 case CUPS_CSPACE_DEVICED :
125 case CUPS_CSPACE_DEVICEE :
126 case CUPS_CSPACE_DEVICEF :
127 white = 0;
128 break;
129 default :
130 _cupsLangPrintFilter(stderr, "ERROR", _("Unsupported raster data."));
131 fprintf(stderr, "DEBUG: Unsupported cupsColorSpace %d on page %d.\n",
132 inheader.cupsColorSpace, page);
133 return (1);
134 }
135
136 if (inheader.cupsColorOrder != CUPS_ORDER_CHUNKED)
137 {
138 _cupsLangPrintFilter(stderr, "ERROR", _("Unsupported raster data."));
139 fprintf(stderr, "DEBUG: Unsupported cupsColorOrder %d on page %d.\n",
140 inheader.cupsColorOrder, page);
141 return (1);
142 }
143
144 if (inheader.cupsBitsPerPixel != 1 &&
145 inheader.cupsBitsPerColor != 8 && inheader.cupsBitsPerColor != 16)
146 {
147 _cupsLangPrintFilter(stderr, "ERROR", _("Unsupported raster data."));
148 fprintf(stderr, "DEBUG: Unsupported cupsBitsPerColor %d on page %d.\n",
149 inheader.cupsBitsPerColor, page);
150 return (1);
151 }
152
153 memcpy(&outheader, &inheader, sizeof(outheader));
154 outheader.cupsWidth = page_width;
155 outheader.cupsHeight = page_height;
156 outheader.cupsBytesPerLine = linesize;
157
158 if (!cupsRasterWriteHeader2(outras, &outheader))
159 {
160 _cupsLangPrintFilter(stderr, "ERROR", _("Error sending raster data."));
161 fprintf(stderr, "DEBUG: Unable to write header for page %d.\n", page);
162 return (1);
163 }
164
165 /*
166 * Copy raster data...
167 */
168
169 line = malloc(linesize);
170
171 memset(line, white, linesize);
172 for (y = page_top; y > 0; y --)
173 if (!cupsRasterWritePixels(outras, line, outheader.cupsBytesPerLine))
174 {
175 _cupsLangPrintFilter(stderr, "ERROR", _("Error sending raster data."));
176 fprintf(stderr, "DEBUG: Unable to write line %d for page %d.\n",
177 page_top - y + 1, page);
178 return (1);
179 }
180
181 for (y = inheader.cupsHeight; y > 0; y --)
182 {
183 cupsRasterReadPixels(inras, line + lineoffset, inheader.cupsBytesPerLine);
184 if (!cupsRasterWritePixels(outras, line, outheader.cupsBytesPerLine))
185 {
186 _cupsLangPrintFilter(stderr, "ERROR", _("Error sending raster data."));
187 fprintf(stderr, "DEBUG: Unable to write line %d for page %d.\n",
188 inheader.cupsHeight - y + page_top + 1, page);
189 return (1);
190 }
191 }
192
193 memset(line, white, linesize);
194 for (y = page_top; y > 0; y --)
195 if (!cupsRasterWritePixels(outras, line, outheader.cupsBytesPerLine))
196 {
197 _cupsLangPrintFilter(stderr, "ERROR", _("Error sending raster data."));
198 fprintf(stderr, "DEBUG: Unable to write line %d for page %d.\n",
199 page_bottom - y + page_top + inheader.cupsHeight + 1, page);
200 return (1);
201 }
202
203 free(line);
204 }
205
206 cupsRasterClose(inras);
207 if (fd)
208 close(fd);
209
210 cupsRasterClose(outras);
211
212 return (0);
213}
214
215
216/*
217 * End of "$Id$".
218 */