Debian Codegraph @

converter/pbm/pbmtoescp2.c

raw
1 /* pbmtoescp2.c - read a portable bitmap and produce Epson ESC/P2 raster
2 ** graphics output data for Epson Stylus printers
3 **
4 ** Copyright (C) 2003 by Ulrich Walcher ([email protected])
5 ** and Jef Poskanzer.
6 **
7 ** Permission to use, copy, modify, and distribute this software and its
8 ** documentation for any purpose and without fee is hereby granted, provided
9 ** that the above copyright notice appear in all copies and that both that
10 ** copyright notice and this permission notice appear in supporting
11 ** documentation. This software is provided "as is" without express or
12 ** implied warranty.
13 **
14 ** Major changes were made in July 2015 by Akira Urushibata.
15 ** Added 720 DPI capability.
16 ** Added -formfeed, -raw and -stripeheight.
17 ** Replaced Packbits run length encoding function. (Use library function.)
18 *
19 * ESC/P Reference Manual (1997)
20 * ftp://download.epson-europe.com/pub/download/182/epson18162eu.zip
21 * See Part 1 "ESC ." Print Raster Graphics
22 */
23
24 #include <string.h>
25
26 #include "pm_c_util.h"
27 #include "mallocvar.h"
28 #include "shhopt.h"
29 #include "runlength.h"
30 #include "pbm.h"
31
32 #define MAXCOLS (127 * 256 + 255) /* Limit in official Epson manual */
33
34 static char const esc = 033;
35
36 struct CmdlineInfo {
37 const char * inputFileName;
38 unsigned int resolution;
39 unsigned int compress;
40 unsigned int stripeHeight;
41 bool raw;
42 bool formfeed;
43 };
44
45
46
47 static void
48 parseCommandLine(int argc, const char ** argv,
49 struct CmdlineInfo *cmdlineP) {
50
51 optStruct3 opt;
52 unsigned int option_def_index;
53 optEntry * option_def;
54
55 unsigned int compressSpec, resolutionSpec, stripeHeightSpec,
56 rawSpec, formfeedSpec;
57
58 MALLOCARRAY(option_def, 100);
59
60 option_def_index = 0;
61 opt.opt_table = option_def;
62 opt.short_allowed = FALSE;
63 opt.allowNegNum = FALSE;
64 OPTENT3(0, "compress", OPT_UINT, &cmdlineP->compress,
65 &compressSpec, 0);
66 OPTENT3(0, "resolution", OPT_UINT, &cmdlineP->resolution,
67 &resolutionSpec, 0);
68 OPTENT3(0, "stripeheight", OPT_UINT, &cmdlineP->stripeHeight,
69 &stripeHeightSpec, 0);
70 OPTENT3(0, "raw", OPT_FLAG, NULL,
71 &rawSpec, 0);
72 OPTENT3(0, "formfeed", OPT_FLAG, NULL,
73 &formfeedSpec, 0);
74
75 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
76
77 if (argc-1 > 1)
78 pm_error("Too many arguments: %d. "
79 "Only argument is the filename", argc-1);
80
81 if (compressSpec) {
82 if (cmdlineP->compress != 0 && cmdlineP->compress != 1)
83 pm_error("Invalid -compress value: %u. Only 0 and 1 are valid.",
84 cmdlineP->compress);
85 } else
86 cmdlineP->compress = 1;
87
88 if (resolutionSpec) {
89 if (cmdlineP->resolution != 720 && cmdlineP->resolution != 360 &&
90 cmdlineP->resolution != 180)
91 pm_error("Invalid -resolution value: %u. "
92 "Only 180, 360 and 720 are valid.", cmdlineP->resolution);
93 } else
94 cmdlineP->resolution = 360;
95
96 if (stripeHeightSpec) {
97 if (cmdlineP->stripeHeight == 0 ||
98 cmdlineP->stripeHeight > 255)
99 pm_error("Invalid -stripeheight value: %u. "
100 "Should be 24, 8, or 1, and must be in the range 1-255",
101 cmdlineP->stripeHeight);
102 else if (cmdlineP->stripeHeight != 24 &&
103 cmdlineP->stripeHeight != 8 &&
104 cmdlineP->stripeHeight != 1)
105 pm_message("Proceeding with irregular -stripeheight value: %u. "
106 "Should be 24, 8, or 1.", cmdlineP->stripeHeight);
107 else if (cmdlineP->resolution == 720 &&
108 cmdlineP->stripeHeight != 1)
109 /* The official Epson manual mandates single-row stripes for
110 720 dpi high-resolution images.
111 */
112 pm_message("Proceeding with irregular -stripeheight value: %u. "
113 "Because resolution i 720dpi, should be 1.",
114 cmdlineP->stripeHeight);
115 } else
116 cmdlineP->stripeHeight = cmdlineP->resolution == 720 ? 1 : 24;
117
118 if (rawSpec && formfeedSpec)
119 pm_error("You cannot specify both -raw and -formfeed");
120 else {
121 cmdlineP->raw = rawSpec ? true : false ;
122 cmdlineP->formfeed = formfeedSpec ? true : false ;
123 }
124
125 if (argc-1 == 1)
126 cmdlineP->inputFileName = argv[1];
127 else
128 cmdlineP->inputFileName = "-";
129
130 free(option_def);
131 }
132
133
134
135 static void
136 writeSetup(unsigned int const hres) {
137
138 /* Set raster graphic mode. */
139 printf("%c%c%c%c%c%c", esc, '(', 'G', 1, 0, 1);
140
141 /* Set line spacing in units of 1/360 inches. */
142 printf("%c%c%c", esc, '+', 24 * hres / 10);
143 }
144
145
146
147 int
148 main(int argc, const char * argv[]) {
149
150 FILE * ifP;
151 int rows, cols;
152 int format;
153 unsigned int row;
154 unsigned int idx;
155 unsigned int outColByteCt;
156 unsigned int stripeByteCt;
157 unsigned int hres, vres;
158 unsigned char * inBuff;
159 unsigned char * bitrow[256];
160 unsigned char * compressedData;
161 struct CmdlineInfo cmdline;
162
163 pm_proginit(&argc, argv);
164
165 parseCommandLine(argc, argv, &cmdline);
166
167 ifP = pm_openr(cmdline.inputFileName);
168
169 pbm_readpbminit(ifP, &cols, &rows, &format);
170
171 if (cols > MAXCOLS)
172 pm_error("Image width is too large");
173
174 outColByteCt = pbm_packed_bytes(cols);
175 stripeByteCt = cmdline.stripeHeight * outColByteCt;
176
177 MALLOCARRAY(inBuff, stripeByteCt);
178 if (inBuff == NULL)
179 pm_error("Out of memory trying to create input buffer of %u bytes",
180 stripeByteCt);
181
182 if (cmdline.compress != 0)
183 pm_rlenc_allocoutbuf(&compressedData, stripeByteCt, PM_RLE_PACKBITS);
184 else
185 compressedData = NULL;
186
187 for (idx = 0; idx <= cmdline.stripeHeight; ++idx)
188 bitrow[idx]= &inBuff[idx * outColByteCt];
189
190 hres = vres = 3600 / cmdline.resolution;
191 /* Possible values for hres, vres: 20, 10, 5 */
192
193 if (!cmdline.raw)
194 writeSetup(hres);
195
196 /* Write out raster stripes */
197
198 for (row = 0; row < rows; row += cmdline.stripeHeight ) {
199 unsigned int const rowsThisStripe =
200 MIN(rows - row, cmdline.stripeHeight);
201 unsigned int const outCols = outColByteCt * 8;
202
203 if (rowsThisStripe > 0) {
204 unsigned int idx;
205
206 printf("%c%c%c%c%c%c%c%c", esc, '.', cmdline.compress, vres, hres,
207 cmdline.stripeHeight, outCols % 256, outCols / 256);
208
209 /* Read pbm rows, each padded to full byte */
210
211 for (idx = 0; idx < rowsThisStripe; ++idx) {
212 pbm_readpbmrow_packed (ifP, bitrow[idx], cols, format);
213 pbm_cleanrowend_packed(bitrow[idx], cols);
214 }
215
216 /* If at bottom pad with empty rows up to stripe height */
217 if (rowsThisStripe < cmdline.stripeHeight )
218 memset(bitrow[rowsThisStripe], 0,
219 (cmdline.stripeHeight - rowsThisStripe) * outColByteCt);
220
221 /* Write raster data */
222 if (cmdline.compress != 0) { /* compressed */
223 size_t compressedDataCt;
224
225 pm_rlenc_compressbyte(inBuff, compressedData, PM_RLE_PACKBITS,
226 stripeByteCt, &compressedDataCt);
227 fwrite(compressedData, compressedDataCt, 1, stdout);
228 } else /* uncompressed */
229 fwrite(inBuff, stripeByteCt, 1, stdout);
230
231 /* Emit newline to print the stripe */
232 putchar('\n');
233 }
234 }
235
236 free(inBuff);
237 free(compressedData);
238 pm_close(ifP);
239
240 /* Form feed */
241 if (cmdline.formfeed)
242 putchar('\f');
243
244 if (!cmdline.raw) {
245 /* Reset printer. a*/
246 printf("%c%c", esc, '@');
247 }
248
249 return 0;
250 }
251
252
253