Debian Codegraph @

converter/pbm/pbmtoepson.c

raw
1 /* pbmtoeps.c - read a PBM image and produce Epson graphics
2 **
3 ** Copyright (C) 1990 by John Tiller ([email protected])
4 ** and Jef Poskanzer.
5 **
6 ** Permission to use, copy, modify, and distribute this software and its
7 ** documentation for any purpose and without fee is hereby granted, provided
8 ** that the above copyright notice appear in all copies and that both that
9 ** copyright notice and this permission notice appear in supporting
10 ** documentation. This software is provided "as is" without express or
11 ** implied warranty.
12 */
13
14 #define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
15 #define _BSD_SOURCE /* Make sure strcaseeq() is in nstring.h */
16 #include <stdio.h>
17
18 #include "pm_c_util.h"
19 #include "mallocvar.h"
20 #include "nstring.h"
21 #include "shhopt.h"
22
23 #include "pbm.h"
24
25
26 static char const esc = 033;
27
28 enum epsonProtocol {ESCP9, ESCP};
29
30 enum adjacence {ADJACENT_ANY, ADJACENT_YES, ADJACENT_NO};
31
32 struct CmdlineInfo {
33 /* All the information the user supplied in the command line,
34 in a form easy for the program to use.
35 */
36 const char * inputFileName; /* '-' if stdin */
37 unsigned int dpi; /* zero means "any" */
38 enum adjacence adjacence;
39 enum epsonProtocol protocol;
40 };
41
42
43
44 static void
45 parseCommandLine(int argc,
46 const char ** argv,
47 struct CmdlineInfo * cmdlineP ) {
48 /*----------------------------------------------------------------------------
49 Parse program command line described in Unix standard form by argc
50 and argv. Return the information in the options as *cmdlineP.
51
52 If command line is internally inconsistent (invalid options, etc.),
53 issue error message to stderr and abort program.
54
55 Note that the strings we return are stored in the storage that
56 was passed to us as the argv array. We also trash *argv.
57 -----------------------------------------------------------------------------*/
58 optEntry * option_def;
59 optStruct3 opt;
60 unsigned int option_def_index;
61
62 char * protocol;
63 unsigned int adjacentSpec, nonadjacentSpec;
64 unsigned int dpiSpec, protocolSpec;
65
66 MALLOCARRAY_NOFAIL(option_def, 100);
67
68 option_def_index = 0; /* incremented by OPTENT3 */
69 OPTENT3(0, "protocol", OPT_STRING, &protocol,
70 &protocolSpec, 0);
71 OPTENT3(0, "dpi", OPT_UINT, &cmdlineP->dpi,
72 &dpiSpec, 0);
73 OPTENT3(0, "adjacent", OPT_FLAG, NULL,
74 &adjacentSpec, 0);
75 OPTENT3(0, "nonadjacent", OPT_FLAG, NULL,
76 &nonadjacentSpec, 0);
77
78 opt.opt_table = option_def;
79 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
80 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
81
82 pm_optParseOptions4( &argc, argv, opt, sizeof(opt), 0);
83 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
84
85
86 if (!dpiSpec)
87 cmdlineP->dpi = 0;
88 else {
89 if (cmdlineP->dpi == 0)
90 pm_error("-dpi must be positive");
91 }
92
93 if (!protocolSpec)
94 cmdlineP->protocol = ESCP9;
95 else {
96 if (strcaseeq(protocol, "escp9"))
97 cmdlineP->protocol = ESCP9;
98 else if (strcaseeq(protocol, "escp"))
99 cmdlineP->protocol = ESCP;
100 else if (strcaseeq(protocol, "escp2"))
101 pm_error("This program cannot do ESC/P2. Try Pbmtoescp2.");
102 else
103 pm_error("Unrecognized value '%s' for -protocol. "
104 "Only recognized values are 'escp9' and 'escp'",
105 protocol);
106 }
107
108 if (adjacentSpec && nonadjacentSpec)
109 pm_error("You can't specify both -adjacent and -nonadjacent");
110 else if (adjacentSpec)
111 cmdlineP->adjacence = ADJACENT_YES;
112 else if (nonadjacentSpec)
113 cmdlineP->adjacence = ADJACENT_NO;
114 else
115 cmdlineP->adjacence = ADJACENT_ANY;
116
117 if (argc-1 < 1)
118 cmdlineP->inputFileName = "-";
119 else {
120 cmdlineP->inputFileName = argv[1];
121 if (argc-1 > 1)
122 pm_error("Too many arguments (%d). The only non-option argument "
123 "is the file name", argc-1);
124 }
125
126 free(option_def);
127 }
128
129
130
131 static unsigned int
132 lineWidth(const bit ** const stripeBits,
133 unsigned int const cols,
134 unsigned int const stripeRows) {
135 /*----------------------------------------------------------------------------
136 Return the column number just past the rightmost column of the stripe
137 stripeBits[] that contains at least some black.
138
139 The stripe is 'cols' wide by 'stripeRows' high.
140 -----------------------------------------------------------------------------*/
141 unsigned int col;
142 unsigned int endSoFar;
143
144 endSoFar = 0;
145
146 for (col = 0; col < cols; ++ col) {
147 unsigned int stripeRow; /* row number within stripe */
148
149 for (stripeRow = 0; stripeRow < stripeRows; ++stripeRow) {
150 if (stripeBits[stripeRow][col] == PBM_BLACK)
151 endSoFar = col+1;
152 }
153 }
154 return endSoFar;
155 }
156
157
158
159 static void
160 printStripe(const bit ** const stripeBits,
161 unsigned int const cols,
162 unsigned int const stripeRows,
163 char const m) {
164 /*----------------------------------------------------------------------------
165 Print one stripe (a group of rows printed with one pass of the print
166 head. The stripe is cols columns wide by stripeRows high.
167 stripeBits[row][col] is the pixel value for Row row, Column col within
168 the stripe.
169
170 'm' is the "m" parameter for the Select Bit Image command. It controls
171 such things as the horizontal density.
172 -----------------------------------------------------------------------------*/
173 unsigned int col;
174
175 /* Print header of Select Bit Image command */
176 printf("%c%c%c%c%c", esc, '*', m, cols % 256, cols / 256);
177
178 /* Print the data part of the Select Bit Image command */
179 for (col = 0; col < cols; ++col) {
180 unsigned int stripeRow;
181 int val;
182
183 val = 0;
184 for (stripeRow = 0; stripeRow < stripeRows; ++stripeRow)
185 if (stripeBits[stripeRow][col] == PBM_BLACK)
186 val |= (1 << (8-1-stripeRow));
187 putchar(val);
188 }
189 }
190
191
192
193 static void
194 computeM(enum epsonProtocol const protocol,
195 unsigned int const dpi,
196 enum adjacence const adjacence,
197 char * const mP) {
198 /*----------------------------------------------------------------------------
199 Compute the "m" parameter for the Select Bit Image command.
200 -----------------------------------------------------------------------------*/
201 switch (dpi) {
202 case 0:
203 /* Special value meaning "any dpi you feel is appropriate" */
204 if (adjacence == ADJACENT_NO)
205 *mP = 2;
206 else {
207 switch (protocol) {
208 case ESCP9: *mP = 5; break;
209 case ESCP: *mP = 6; break;
210 }
211 }
212 break;
213 case 60:
214 if (adjacence == ADJACENT_NO)
215 pm_error("You can't print at %u dpi "
216 "with adjacent dot printing", dpi);
217 *mP = 0;
218 break;
219 case 120:
220 *mP = adjacence == ADJACENT_NO ? 2 : 1;
221 break;
222 case 240:
223 if (adjacence == ADJACENT_YES)
224 pm_error("You can't print at %u dpi "
225 "without adjacent dot printing", dpi);
226 *mP = 3;
227 break;
228 case 80:
229 if (adjacence == ADJACENT_NO)
230 pm_error("You can't print at %u dpi "
231 "with adjacent dot printing", dpi);
232 *mP = 4;
233 break;
234 case 72:
235 if (protocol != ESCP9)
236 pm_error("%u dpi is possible only with the ESC/P 9-pin protocol",
237 dpi);
238 if (adjacence == ADJACENT_NO)
239 pm_error("You can't print at %u dpi "
240 "with adjacent dot printing", dpi);
241 *mP = 5;
242 break;
243 case 90:
244 if (adjacence == ADJACENT_NO)
245 pm_error("You can't print at %u dpi "
246 "with adjacent dot printing", dpi);
247 *mP = 6;
248 break;
249 case 144:
250 if (protocol != ESCP9)
251 pm_error("%u dpi is possible only with the ESC/P 9-pin protocol",
252 dpi);
253 if (adjacence == ADJACENT_NO)
254 pm_error("You can't print at %u dpi "
255 "with adjacent dot printing", dpi);
256 *mP = 7;
257 break;
258 default:
259 pm_error("Invalid DPI value: %u. This program knows only "
260 "60, 72, 80, 90, 120, 144, and 240.", dpi);
261 }
262 }
263
264
265
266 static void
267 convertToEpson(const bit ** const bits,
268 int const cols,
269 int const rows,
270 enum epsonProtocol const protocol,
271 unsigned int const dpi,
272 enum adjacence const adjacence) {
273
274 unsigned int const rowsPerStripe = 8;
275 unsigned int const stripeCt = (rows + rowsPerStripe-1) / rowsPerStripe;
276
277 unsigned int stripe;
278 char m;
279
280 computeM(protocol, dpi, adjacence, &m);
281
282 /* Change line spacing to 8/72 inches. */
283 printf("%c%c%c", esc, 'A', 8);
284
285 /* Write out the rows, one stripe at a time. A stripe is 8 rows --
286 the amount written in one pass of the print head. The bottommost
287 stripe can be fewer than 8 rows.
288 */
289
290 for (stripe = 0; stripe < stripeCt; ++stripe) {
291 const bit ** const stripeBits = &bits[stripe*rowsPerStripe];
292 unsigned int const stripeRows =
293 MIN(rowsPerStripe, rows - stripe * rowsPerStripe);
294 /* Number of rows in this stripe (8 for all but bottom stripe) */
295
296 unsigned int const endcol = lineWidth(stripeBits, cols, stripeRows);
297 /* Column where right margin (contiguous white area at right
298 end of stripe) begins. Zero if entire stripe is white.
299 */
300
301 if (endcol > 0)
302 printStripe(stripeBits, endcol, stripeRows, m);
303
304 putchar('\n');
305 }
306 putchar('\f');
307
308 /* Restore normal line spacing. */
309 printf("%c%c", esc, '@');
310 }
311
312
313
314 int
315 main(int argc, const char ** argv) {
316
317 struct CmdlineInfo cmdline;
318 FILE * ifP;
319 const bit** bits;
320 int rows, cols;
321
322 pm_proginit(&argc, argv);
323
324 parseCommandLine(argc, argv, &cmdline);
325
326 ifP = pm_openr(cmdline.inputFileName);
327
328 bits = (const bit **)pbm_readpbm(ifP, &cols, &rows);
329
330 pm_close(ifP);
331
332 convertToEpson(bits, cols, rows,
333 cmdline.protocol, cmdline.dpi, cmdline.adjacence);
334
335 pbm_freearray(bits, rows);
336
337 return 0;
338 }
339
340
341