Debian Codegraph @

converter/pbm/pbmtopsg3.c

raw
1 /* pbmtopsg3
2
3 Reads a series of PBM images and writes a Postscript program
4 containing these images as individual pages with Fax-G3
5 (CCITT-Fiter) compression. (Useful for combining scanned pages into
6 a comfortably printable document.)
7
8 Copyright (C) 2001 Kristof Koehler
9 <[email protected]>
10
11 Netpbm adaptation by Bryan Henderson June 2001.
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <ctype.h>
28
29 #include "pm_c_util.h"
30 #include "pbm.h"
31 #include "shhopt.h"
32
33 struct cmdline_info {
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 * inputFilespec; /* Filespec of input file */
38 float dpi; /* requested resolution, dpi */
39 char * title; /* -title option. NULL for none */
40 };
41
42
43
44 static void
45 parseCommandLine(int argc, char ** argv,
46 struct cmdline_info *cmdlineP) {
47
48 optStruct3 opt;
49 unsigned int option_def_index = 0;
50 optEntry *option_def = malloc(100*sizeof(optEntry));
51
52 unsigned int dpiSpec, titleSpec;
53 float dpiOpt;
54 char * titleOpt;
55
56 OPTENT3(0, "dpi", OPT_FLOAT, &dpiOpt, &dpiSpec, 0);
57 OPTENT3(0, "title", OPT_STRING, &titleOpt, &titleSpec, 0);
58
59 opt.opt_table = option_def;
60 opt.short_allowed = FALSE;
61 opt.allowNegNum = FALSE;
62
63 pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
64
65 if (argc-1 == 0)
66 cmdlineP->inputFilespec = "-";
67 else {
68 cmdlineP->inputFilespec = argv[1];
69 if (argc-1 > 1)
70 pm_error("Too many arguments. The only argument is the input "
71 "file specification");
72 }
73
74 cmdlineP->dpi = dpiSpec ? dpiOpt : 72.0;
75 cmdlineP->title = titleSpec ? titleOpt : NULL;
76 }
77
78
79
80 static void
81 write85 ( unsigned int bits, int *col )
82 {
83 char buf[5] ;
84 if ( bits == 0 ) {
85 fputc ( 'z', stdout ) ;
86 *col += 1 ;
87 } else {
88 buf[4] = bits % 85 + '!' ;
89 bits /= 85 ;
90 buf[3] = bits % 85 + '!' ;
91 bits /= 85 ;
92 buf[2] = bits % 85 + '!' ;
93 bits /= 85 ;
94 buf[1] = bits % 85 + '!' ;
95 bits /= 85 ;
96 buf[0] = bits % 85 + '!' ;
97 fwrite ( buf, 1, 5, stdout ) ;
98 *col += 5 ;
99 }
100 if ( *col > 70 ) {
101 printf ( "\n" ) ;
102 *col = 0 ;
103 }
104 }
105
106
107
108 static void
109 writebits ( unsigned int *outbits, int *outbitsidx, int *col,
110 unsigned int bits, int n )
111 {
112 int k, m ;
113 unsigned int usedbits ;
114 while ( n > 0 ) {
115 if ( *outbitsidx == 0 )
116 *outbits = 0 ;
117 k = 32 - *outbitsidx ;
118 m = n > k ? k : n ;
119 usedbits = (bits >> (n-m)) & ((1<<m)-1) ;
120 *outbits |= usedbits << (k-m) ;
121 *outbitsidx += m ;
122 n -= m ;
123 if ( *outbitsidx == 32 ) {
124 write85 ( *outbits, col ) ;
125 *outbitsidx = 0 ;
126 }
127 }
128 }
129
130
131
132 static void
133 flushbits ( unsigned int *outbits, int *outbitsidx, int *col )
134 {
135 if ( *outbitsidx > 0 ) {
136 write85 ( *outbits, col ) ;
137 *outbitsidx = 0 ;
138 }
139 }
140
141
142
143 struct { unsigned int b, l ; } makeup[40][2] = {
144 { { 0x001b, 5 } /* 11011 */ , { 0x000f,10 } /* 0000001111 */ },
145 { { 0x0012, 5 } /* 10010 */ , { 0x00c8,12 } /* 000011001000 */ },
146 { { 0x0017, 6 } /* 010111 */ , { 0x00c9,12 } /* 000011001001 */ },
147 { { 0x0037, 7 } /* 0110111 */ , { 0x005b,12 } /* 000001011011 */ },
148 { { 0x0036, 8 } /* 00110110 */ , { 0x0033,12 } /* 000000110011 */ },
149 { { 0x0037, 8 } /* 00110111 */ , { 0x0034,12 } /* 000000110100 */ },
150 { { 0x0064, 8 } /* 01100100 */ , { 0x0035,12 } /* 000000110101 */ },
151 { { 0x0065, 8 } /* 01100101 */ , { 0x006c,13 } /* 0000001101100 */ },
152 { { 0x0068, 8 } /* 01101000 */ , { 0x006d,13 } /* 0000001101101 */ },
153 { { 0x0067, 8 } /* 01100111 */ , { 0x004a,13 } /* 0000001001010 */ },
154 { { 0x00cc, 9 } /* 011001100 */ , { 0x004b,13 } /* 0000001001011 */ },
155 { { 0x00cd, 9 } /* 011001101 */ , { 0x004c,13 } /* 0000001001100 */ },
156 { { 0x00d2, 9 } /* 011010010 */ , { 0x004d,13 } /* 0000001001101 */ },
157 { { 0x00d3, 9 } /* 011010011 */ , { 0x0072,13 } /* 0000001110010 */ },
158 { { 0x00d4, 9 } /* 011010100 */ , { 0x0073,13 } /* 0000001110011 */ },
159 { { 0x00d5, 9 } /* 011010101 */ , { 0x0074,13 } /* 0000001110100 */ },
160 { { 0x00d6, 9 } /* 011010110 */ , { 0x0075,13 } /* 0000001110101 */ },
161 { { 0x00d7, 9 } /* 011010111 */ , { 0x0076,13 } /* 0000001110110 */ },
162 { { 0x00d8, 9 } /* 011011000 */ , { 0x0077,13 } /* 0000001110111 */ },
163 { { 0x00d9, 9 } /* 011011001 */ , { 0x0052,13 } /* 0000001010010 */ },
164 { { 0x00da, 9 } /* 011011010 */ , { 0x0053,13 } /* 0000001010011 */ },
165 { { 0x00db, 9 } /* 011011011 */ , { 0x0054,13 } /* 0000001010100 */ },
166 { { 0x0098, 9 } /* 010011000 */ , { 0x0055,13 } /* 0000001010101 */ },
167 { { 0x0099, 9 } /* 010011001 */ , { 0x005a,13 } /* 0000001011010 */ },
168 { { 0x009a, 9 } /* 010011010 */ , { 0x005b,13 } /* 0000001011011 */ },
169 { { 0x0018, 6 } /* 011000 */ , { 0x0064,13 } /* 0000001100100 */ },
170 { { 0x009b, 9 } /* 010011011 */ , { 0x0065,13 } /* 0000001100101 */ },
171 { { 0x0008,11 } /* 00000001000 */ , { 0x0008,11 } /* 00000001000 */ },
172 { { 0x000c,11 } /* 00000001100 */ , { 0x000c,11 } /* 00000001100 */ },
173 { { 0x000d,11 } /* 00000001101 */ , { 0x000d,11 } /* 00000001101 */ },
174 { { 0x0012,12 } /* 000000010010 */ , { 0x0012,12 } /* 000000010010 */ },
175 { { 0x0013,12 } /* 000000010011 */ , { 0x0013,12 } /* 000000010011 */ },
176 { { 0x0014,12 } /* 000000010100 */ , { 0x0014,12 } /* 000000010100 */ },
177 { { 0x0015,12 } /* 000000010101 */ , { 0x0015,12 } /* 000000010101 */ },
178 { { 0x0016,12 } /* 000000010110 */ , { 0x0016,12 } /* 000000010110 */ },
179 { { 0x0017,12 } /* 000000010111 */ , { 0x0017,12 } /* 000000010111 */ },
180 { { 0x001c,12 } /* 000000011100 */ , { 0x001c,12 } /* 000000011100 */ },
181 { { 0x001d,12 } /* 000000011101 */ , { 0x001d,12 } /* 000000011101 */ },
182 { { 0x001e,12 } /* 000000011110 */ , { 0x001e,12 } /* 000000011110 */ },
183 { { 0x001f,12 } /* 000000011111 */ , { 0x001f,12 } /* 000000011111 */ }
184 } ;
185
186 struct { unsigned int b, l ; } term[64][2] = {
187 { { 0x0035, 8 } /* 00110101 */ , { 0x0037,10 } /* 0000110111 */ },
188 { { 0x0007, 6 } /* 000111 */ , { 0x0002, 3 } /* 010 */ },
189 { { 0x0007, 4 } /* 0111 */ , { 0x0003, 2 } /* 11 */ },
190 { { 0x0008, 4 } /* 1000 */ , { 0x0002, 2 } /* 10 */ },
191 { { 0x000b, 4 } /* 1011 */ , { 0x0003, 3 } /* 011 */ },
192 { { 0x000c, 4 } /* 1100 */ , { 0x0003, 4 } /* 0011 */ },
193 { { 0x000e, 4 } /* 1110 */ , { 0x0002, 4 } /* 0010 */ },
194 { { 0x000f, 4 } /* 1111 */ , { 0x0003, 5 } /* 00011 */ },
195 { { 0x0013, 5 } /* 10011 */ , { 0x0005, 6 } /* 000101 */ },
196 { { 0x0014, 5 } /* 10100 */ , { 0x0004, 6 } /* 000100 */ },
197 { { 0x0007, 5 } /* 00111 */ , { 0x0004, 7 } /* 0000100 */ },
198 { { 0x0008, 5 } /* 01000 */ , { 0x0005, 7 } /* 0000101 */ },
199 { { 0x0008, 6 } /* 001000 */ , { 0x0007, 7 } /* 0000111 */ },
200 { { 0x0003, 6 } /* 000011 */ , { 0x0004, 8 } /* 00000100 */ },
201 { { 0x0034, 6 } /* 110100 */ , { 0x0007, 8 } /* 00000111 */ },
202 { { 0x0035, 6 } /* 110101 */ , { 0x0018, 9 } /* 000011000 */ },
203 { { 0x002a, 6 } /* 101010 */ , { 0x0017,10 } /* 0000010111 */ },
204 { { 0x002b, 6 } /* 101011 */ , { 0x0018,10 } /* 0000011000 */ },
205 { { 0x0027, 7 } /* 0100111 */ , { 0x0008,10 } /* 0000001000 */ },
206 { { 0x000c, 7 } /* 0001100 */ , { 0x0067,11 } /* 00001100111 */ },
207 { { 0x0008, 7 } /* 0001000 */ , { 0x0068,11 } /* 00001101000 */ },
208 { { 0x0017, 7 } /* 0010111 */ , { 0x006c,11 } /* 00001101100 */ },
209 { { 0x0003, 7 } /* 0000011 */ , { 0x0037,11 } /* 00000110111 */ },
210 { { 0x0004, 7 } /* 0000100 */ , { 0x0028,11 } /* 00000101000 */ },
211 { { 0x0028, 7 } /* 0101000 */ , { 0x0017,11 } /* 00000010111 */ },
212 { { 0x002b, 7 } /* 0101011 */ , { 0x0018,11 } /* 00000011000 */ },
213 { { 0x0013, 7 } /* 0010011 */ , { 0x00ca,12 } /* 000011001010 */ },
214 { { 0x0024, 7 } /* 0100100 */ , { 0x00cb,12 } /* 000011001011 */ },
215 { { 0x0018, 7 } /* 0011000 */ , { 0x00cc,12 } /* 000011001100 */ },
216 { { 0x0002, 8 } /* 00000010 */ , { 0x00cd,12 } /* 000011001101 */ },
217 { { 0x0003, 8 } /* 00000011 */ , { 0x0068,12 } /* 000001101000 */ },
218 { { 0x001a, 8 } /* 00011010 */ , { 0x0069,12 } /* 000001101001 */ },
219 { { 0x001b, 8 } /* 00011011 */ , { 0x006a,12 } /* 000001101010 */ },
220 { { 0x0012, 8 } /* 00010010 */ , { 0x006b,12 } /* 000001101011 */ },
221 { { 0x0013, 8 } /* 00010011 */ , { 0x00d2,12 } /* 000011010010 */ },
222 { { 0x0014, 8 } /* 00010100 */ , { 0x00d3,12 } /* 000011010011 */ },
223 { { 0x0015, 8 } /* 00010101 */ , { 0x00d4,12 } /* 000011010100 */ },
224 { { 0x0016, 8 } /* 00010110 */ , { 0x00d5,12 } /* 000011010101 */ },
225 { { 0x0017, 8 } /* 00010111 */ , { 0x00d6,12 } /* 000011010110 */ },
226 { { 0x0028, 8 } /* 00101000 */ , { 0x00d7,12 } /* 000011010111 */ },
227 { { 0x0029, 8 } /* 00101001 */ , { 0x006c,12 } /* 000001101100 */ },
228 { { 0x002a, 8 } /* 00101010 */ , { 0x006d,12 } /* 000001101101 */ },
229 { { 0x002b, 8 } /* 00101011 */ , { 0x00da,12 } /* 000011011010 */ },
230 { { 0x002c, 8 } /* 00101100 */ , { 0x00db,12 } /* 000011011011 */ },
231 { { 0x002d, 8 } /* 00101101 */ , { 0x0054,12 } /* 000001010100 */ },
232 { { 0x0004, 8 } /* 00000100 */ , { 0x0055,12 } /* 000001010101 */ },
233 { { 0x0005, 8 } /* 00000101 */ , { 0x0056,12 } /* 000001010110 */ },
234 { { 0x000a, 8 } /* 00001010 */ , { 0x0057,12 } /* 000001010111 */ },
235 { { 0x000b, 8 } /* 00001011 */ , { 0x0064,12 } /* 000001100100 */ },
236 { { 0x0052, 8 } /* 01010010 */ , { 0x0065,12 } /* 000001100101 */ },
237 { { 0x0053, 8 } /* 01010011 */ , { 0x0052,12 } /* 000001010010 */ },
238 { { 0x0054, 8 } /* 01010100 */ , { 0x0053,12 } /* 000001010011 */ },
239 { { 0x0055, 8 } /* 01010101 */ , { 0x0024,12 } /* 000000100100 */ },
240 { { 0x0024, 8 } /* 00100100 */ , { 0x0037,12 } /* 000000110111 */ },
241 { { 0x0025, 8 } /* 00100101 */ , { 0x0038,12 } /* 000000111000 */ },
242 { { 0x0058, 8 } /* 01011000 */ , { 0x0027,12 } /* 000000100111 */ },
243 { { 0x0059, 8 } /* 01011001 */ , { 0x0028,12 } /* 000000101000 */ },
244 { { 0x005a, 8 } /* 01011010 */ , { 0x0058,12 } /* 000001011000 */ },
245 { { 0x005b, 8 } /* 01011011 */ , { 0x0059,12 } /* 000001011001 */ },
246 { { 0x004a, 8 } /* 01001010 */ , { 0x002b,12 } /* 000000101011 */ },
247 { { 0x004b, 8 } /* 01001011 */ , { 0x002c,12 } /* 000000101100 */ },
248 { { 0x0032, 8 } /* 00110010 */ , { 0x005a,12 } /* 000001011010 */ },
249 { { 0x0033, 8 } /* 00110011 */ , { 0x0066,12 } /* 000001100110 */ },
250 { { 0x0034, 8 } /* 00110100 */ , { 0x0067,12 } /* 000001100111 */ }
251 } ;
252
253
254 static void
255 writelength ( unsigned int *outbits, int *outbitsidx, int *col,
256 int bit, int length )
257 {
258 while ( length >= 64 ) {
259 int m = length / 64 ;
260 if ( m > 40 )
261 m = 40 ;
262 writebits ( outbits, outbitsidx, col,
263 makeup[m-1][bit].b, makeup[m-1][bit].l ) ;
264 length -= 64*m ;
265 }
266 writebits ( outbits, outbitsidx, col,
267 term[length][bit].b, term[length][bit].l ) ;
268 }
269
270
271
272 static void
273 doPage(FILE * const ifP,
274 unsigned int const pageNum,
275 double const dpi) {
276
277 int cols, rows, format;
278 bit * bitrow;
279 unsigned int row;
280 unsigned int outbits ;
281 int outbitsidx, col ;
282
283 pbm_readpbminit(ifP, &cols, &rows, &format);
284
285 bitrow = pbm_allocrow(cols);
286
287 pm_message("[%u]\n", pageNum);
288
289 printf ("%%%%Page: %u %u\n", pageNum, pageNum);
290 printf ("%u %u 1 [ %f 0 0 %f 0 %u ]\n"
291 "{ currentfile /ASCII85Decode filter\n"
292 " << /Columns %u /Rows %u /EndOfBlock false >> "
293 "/CCITTFaxDecode filter\n"
294 " image } exec\n",
295 cols, rows, dpi/72.0, -dpi/72.0, rows,
296 cols, rows) ;
297
298 outbitsidx = col = 0 ;
299 for (row = 0 ; row < rows; ++row) {
300 int lastbit, cnt ;
301 unsigned int j;
302
303 pbm_readpbmrow(ifP, bitrow, cols, format);
304
305 lastbit = cnt = 0 ;
306 for (j = 0; j < cols; ++j) {
307 if (bitrow[j] != lastbit) {
308 writelength(&outbits, &outbitsidx, &col, lastbit, cnt) ;
309 lastbit = 1 ^ lastbit ;
310 cnt = 0 ;
311 }
312 ++cnt;
313 }
314 writelength(&outbits, &outbitsidx, &col, lastbit, cnt);
315 }
316
317 flushbits(&outbits, &outbitsidx, &col) ;
318 printf("~>\nshowpage\n") ;
319
320 pbm_freerow(bitrow);
321 }
322
323
324
325 static void
326 doPages(FILE * const ifP,
327 unsigned int * const pagesP,
328 double const dpi) {
329
330 int eof;
331 unsigned int pagesDone;
332
333 eof = FALSE;
334 pagesDone = 0;
335
336 while (!eof) {
337 doPage(ifP, pagesDone + 1, dpi);
338 ++pagesDone;
339 pbm_nextimage(ifP, &eof);
340 }
341 *pagesP = pagesDone;
342 }
343
344
345
346 int
347 main(int argc,
348 char * argv[]) {
349
350 FILE *ifP;
351 unsigned int pages;
352
353 struct cmdline_info cmdline;
354
355 pbm_init(&argc, argv);
356
357 parseCommandLine(argc, argv, &cmdline);
358
359 ifP = pm_openr(cmdline.inputFilespec);
360
361 printf ("%%!PS-Adobe-3.0\n");
362 if (cmdline.title)
363 printf("%%%%Title: %s\n", cmdline.title) ;
364 printf ("%%%%Creator: pbmtopsg3, Copyright (C) 2001 Kristof Koehler\n"
365 "%%%%Pages: (atend)\n"
366 "%%%%EndComments\n") ;
367
368 doPages(ifP, &pages, cmdline.dpi);
369
370 printf ("%%%%Trailer\n"
371 "%%%%Pages: %u\n"
372 "%%%%EOF\n",
373 pages);
374
375 pm_close(ifP);
376 pm_close(stdout);
377
378 return 0;
379 }
380
381
382