Debian Codegraph @

converter/pbm/pbmtoepsi.c

raw
1 /* pbmtoepsi.c
2 **
3 ** by Doug Crabill, based heavily on pbmtoascii
4 **
5 ** Converts a pbm file to an encapsulated PostScript style bitmap.
6 ** Note that it does NOT covert the pbm file to PostScript, only to
7 ** a bitmap to be added to a piece of PostScript generated elsewhere.
8 **
9 ** Copyright (C) 1988 by Jef Poskanzer.
10 **
11 ** Permission to use, copy, modify, and distribute this software and its
12 ** documentation for any purpose and without fee is hereby granted, provided
13 ** that the above copyright notice appear in all copies and that both that
14 ** copyright notice and this permission notice appear in supporting
15 ** documentation. This software is provided "as is" without express or
16 ** implied warranty.
17 */
18
19 /*
20 *
21 * Official guide from Adobe:
22 *
23 * Encapsulated PostScript File Format Specification
24 * http://partners.adobe.com/public/developer/en/ps/5002.EPSF_Spec.pdf
25 *
26 */
27
28 #include "pm_c_util.h"
29 #include "mallocvar.h"
30 #include "shhopt.h"
31 #include "pbm.h"
32
33 struct CmdlineInfo {
34 /* All the information the user supplied in the command line,
35 in a form easy for the program to use.
36 */
37 const char * inputFileName;
38
39 unsigned int dpiX; /* horiz component of DPI option */
40 unsigned int dpiY; /* vert component of DPI option */
41 unsigned int bbonly;
42
43 unsigned int verbose;
44 };
45
46
47
48 static void
49 parseDpi(char * const dpiOpt,
50 unsigned int * const dpiXP,
51 unsigned int * const dpiYP) {
52
53 char *dpistr2;
54 unsigned int dpiX, dpiY;
55
56 dpiX = strtol(dpiOpt, &dpistr2, 10);
57 if (dpistr2 == dpiOpt)
58 pm_error("Invalid value for -dpi: '%s'. Must be either number "
59 "or NxN ", dpiOpt);
60 else {
61 if (*dpistr2 == '\0') {
62 *dpiXP = dpiX;
63 *dpiYP = dpiX;
64 } else if (*dpistr2 == 'x') {
65 char * dpistr3;
66
67 dpistr2++; /* Move past 'x' */
68 dpiY = strtol(dpistr2, &dpistr3, 10);
69 if (dpistr3 != dpistr2 && *dpistr3 == '\0') {
70 *dpiXP = dpiX;
71 *dpiYP = dpiY;
72 } else {
73 pm_error("Invalid value for -dpi: '%s'. Must be either "
74 "number or NxN", dpiOpt);
75 }
76 }
77 }
78 }
79
80
81
82 static void
83 parseCommandLine(int argc, const char ** const argv,
84 struct CmdlineInfo * const cmdlineP) {
85 /*----------------------------------------------------------------------------
86 Note that the file spec array we return is stored in the storage that
87 was passed to us as the argv array.
88 -----------------------------------------------------------------------------*/
89 optEntry * option_def;
90 optStruct3 opt;
91 unsigned int option_def_index;
92
93 char * dpiOpt;
94 unsigned int dpiOptSpec;
95
96 MALLOCARRAY_NOFAIL(option_def, 100);
97
98 option_def_index = 0; /* incremented by OPTENTRY */
99 OPTENT3(0, "bbonly", OPT_FLAG, NULL, &cmdlineP->bbonly, 0);
100 OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0);
101 OPTENT3(0, "dpi", OPT_STRING, &dpiOpt, &dpiOptSpec, 0);
102
103 opt.opt_table = option_def;
104 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
105 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
106
107 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
108 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
109
110
111 if (dpiOptSpec)
112 parseDpi(dpiOpt, &cmdlineP->dpiX, &cmdlineP->dpiY);
113 else
114 cmdlineP->dpiX = cmdlineP->dpiY = 72;
115
116 if ((argc-1) > 1)
117 pm_error("Too many arguments (%d). Only argument is input file name",
118 argc-1);
119
120 if (argc-1 == 0)
121 cmdlineP->inputFileName = "-";
122 else
123 cmdlineP->inputFileName = argv[1];
124 }
125
126
127
128 static void
129 findPrincipalImage(bit ** const bits,
130 unsigned int const rows,
131 unsigned int const cols,
132 unsigned int * const topP,
133 unsigned int * const bottomP,
134 unsigned int * const leftP,
135 unsigned int * const rightP) {
136 /*----------------------------------------------------------------------------
137 Find the foreground image on a white background.
138
139 Find the image in the pixels bits[][], which is 'rows' rows by
140 'cols' columns.
141
142 Return the boundaries of the foreground image as *topP, *bottomP, *leftP,
143 and *rightP.
144
145 If the image is all white, consider the entire image foreground.
146 -----------------------------------------------------------------------------*/
147 unsigned int top, bottom, left, right;
148 unsigned int row;
149
150 /* Initial values */
151 top = rows;
152 bottom = 0;
153 left = cols;
154 right = 0;
155
156 for (row = 0; row < rows; ++row) {
157 unsigned int col;
158 for (col = 0; col < cols; ++col) {
159 if (bits[row][col] == PBM_BLACK) {
160 if (row < top)
161 top = row;
162 if (row > bottom)
163 bottom = row;
164 if (col < left)
165 left = col;
166 if (col > right)
167 right = col;
168 }
169 }
170 }
171
172 if (top > bottom) {
173 /* No black pixels encountered */
174 pm_message("Blank page");
175 top = 0;
176 left = 0;
177 bottom = rows - 1;
178 right = cols - 1;
179 }
180
181 *topP = top;
182 *bottomP = bottom;
183 *leftP = left;
184 *rightP = right;
185 }
186
187
188
189 static void
190 outputBoundingBox(int const top, int const bottom,
191 int const left, int const right,
192 int const rows,
193 unsigned int const dpiX, unsigned int const dpiY) {
194
195 float const xScale = 72.0 / dpiX;
196 float const yScale = 72.0 / dpiY;
197
198 printf("%%%%BoundingBox: %d %d %d %d\n",
199 ROUND(left*xScale), ROUND((rows - bottom)*yScale),
200 ROUND(right*xScale), ROUND((rows - top)*yScale));
201 }
202
203
204
205 static unsigned char
206 eightPixels(bit ** const bits,
207 int const row,
208 int const col,
209 int const cols) {
210 /*----------------------------------------------------------------------------
211 Compute a byte that represents the 8 pixels starting at Column 'col' of
212 row 'row' of the raster 'bits'. The most significant bit of the result
213 represents the leftmost pixel, with 1 meaning black. (Note that this is
214 the opposite of Postscript.)
215
216 The row is 'cols' columns wide, so fill on the right with white if there
217 are not eight pixels in the row starting with Column 'col'.
218 -----------------------------------------------------------------------------*/
219 unsigned int bitPos;
220 unsigned char octet;
221
222 octet = 0; /* initial value */
223
224 for (bitPos = 0; bitPos < 8; ++bitPos) {
225 octet <<= 1;
226 if (col + bitPos < cols)
227 if (bits[row][col + bitPos] == PBM_BLACK)
228 octet += 1;
229 }
230 return(octet);
231 }
232
233
234
235 int
236 main(int argc, const char * argv[]) {
237
238 struct CmdlineInfo cmdline;
239 FILE * ifP;
240 bit ** bits;
241 int rows, cols;
242 unsigned int top, bottom, left, right;
243 /* boundaries of principal part of image -- i.e. excluding white
244 borders
245 */
246
247 pm_proginit(&argc, argv);
248
249 parseCommandLine(argc, argv, &cmdline);
250
251 ifP = pm_openr(cmdline.inputFileName);
252 bits = pbm_readpbm(ifP, &cols, &rows);
253 pm_close(ifP);
254
255 findPrincipalImage(bits, rows, cols, &top, &bottom, &left, &right);
256
257 printf("%%!PS-Adobe-2.0 EPSF-1.2\n");
258
259 outputBoundingBox(top, bottom, left, right, rows,
260 cmdline.dpiX, cmdline.dpiY);
261
262 if (!cmdline.bbonly) {
263 int row;
264 printf("%%%%BeginPreview: %d %d 1 %d\n",
265 right - left + 1, bottom - top + 1, bottom - top + 1);
266
267 for (row = top; row <= bottom; row++) {
268 unsigned int col;
269 unsigned int outChars;
270 printf("%% ");
271
272 outChars = 2; /* initial value */
273
274 for (col = left; col <= right; col += 8) {
275 if (outChars == 72) {
276 printf("\n%% ");
277 outChars = 2;
278 }
279
280 printf("%02x", eightPixels(bits, row, col, cols));
281 outChars += 2;
282 }
283 if (outChars > 0)
284 printf("\n");
285 }
286 printf("%%%%EndImage\n");
287 printf("%%%%EndPreview\n");
288 }
289 return 0;
290 }
291
292
293